bengine.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <script type="text/javascript">
  2. //JS to load posts when you click on them to edit
  3. window.postsLoaded = {};
  4. function toggle(id) {
  5. var foo = document.getElementById(id);
  6. if (foo.style.display == 'block') {
  7. foo.style.display = 'none';
  8. } else {
  9. foo.style.display = 'block';
  10. }
  11. }
  12. function loadpost(fragment_url,element_id) {
  13. if (window.postsLoaded[element_id]) return;
  14. var element = document.getElementById(element_id);
  15. element.innerHTML = 'Loading ...';
  16. var xmlhttp = new XMLHttpRequest();
  17. xmlhttp.open("GET", fragment_url);
  18. xmlhttp.onreadystatechange = function() {
  19. if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  20. element.innerHTML = xmlhttp.responseText;
  21. window.postsLoaded[element_id] = true;
  22. }
  23. }
  24. xmlhttp.send(null);
  25. }
  26. </script>
  27. <!--Post Creation DOM-->
  28. <p class="posteditortitle">
  29. <a id="newpostlink" href="javascript:toggle('newpost')" title="Toggle hiding of New Post editor">
  30. Create New Post
  31. </a>
  32. </p>
  33. <div id="newpost" class="disabled">
  34. <form method="POST">
  35. <input type="hidden" name="id" value="CHANGEME" />
  36. Post Title:<br />
  37. <input id="newposttitle" name="title" class="cooltext" />
  38. Post Body: <span style="font-style: italic;">
  39. All HTML Tags accepted! Please see <a href="" title="Manual">here</a> regarding how to disable auto-formatting,
  40. or if additional help is required.
  41. </span>
  42. <textarea id="newposttext" name="content"></textarea><br />
  43. <input type="hidden" name="app" value="blog" />
  44. <input type="submit" name="mod" value="Create Post" class="coolbutton" />
  45. </form>
  46. <hr />
  47. </div>
  48. <?php
  49. /*Initialize vars, get directory contents*/
  50. $postincrementer = 0;
  51. $JSAIDS = "";
  52. $dir = $basedir.'/blog/';
  53. $postlisting = scandir($dir);
  54. rsort($postlisting, SORT_NUMERIC);
  55. /*Post Manipulation*/
  56. if (!empty($_POST["id"])) {
  57. /*Post Deletion*/
  58. if ($_POST["mod"] == "Delete Post") {
  59. $fh = unlink($dir.'/'.$_POST["id"]);
  60. if (!$fh) die("ERROR: couldn't delete ".$_POST['id'].", check permissions");
  61. echo "Deleted ".$_POST["id"]."<br />";
  62. /*Post Editing*/
  63. } elseif ($_POST["mod"] == "Commit Edit") {
  64. $fh = fopen($dir.'/'.$_POST["id"], 'w');
  65. if (!$fh) die("ERROR: couldn't write to ".$_POST['id'].", check permissions");
  66. fwrite($fh,stripslashes($_POST["content"]));
  67. fclose($fh);
  68. echo "Edited ".$_POST["id"]."<br />";
  69. /*Post Creation*/
  70. } elseif ($_POST["mod"] == "Create Post") {
  71. $pnum = intval(substr($postlisting[0],0,strpos($postlisting[0],'-'))) + 1;
  72. $id = $dir.$pnum."-".$_POST["title"].".post";
  73. /*echo $id."<br />";
  74. var_dump($postlisting[0]);
  75. var_dump($_POST);
  76. die("FOOBAR");*/
  77. $fh = fopen($id, 'w');
  78. if (!$fh) die("ERROR: Couldn't Write ".$id.", check permissions");
  79. fwrite($fh,stripslashes($_POST["content"]));
  80. fclose($fh);
  81. echo "Created ".$id."<br />";
  82. /*Catchall*/
  83. } else {
  84. die("Nothing to do");
  85. }
  86. }
  87. /*Spit out the posts into the DOM so that they can be edited/deleted*/
  88. $postlisting = scandir($dir);
  89. rsort($postlisting, SORT_NUMERIC);
  90. foreach ($postlisting as $key=>$val) {
  91. $id = basename($val);
  92. $posttitle = strstr($val,'.', true);
  93. if (!empty($posttitle)) {
  94. $postincrementer++;
  95. print "
  96. <p id=\"post".$postincrementer."\" class=\"posteditortitle\">
  97. <a id=\"link".$postincrementer."\" href=\"javascript:toggle('postcontent".$postincrementer."')\" title=\"Toggle hiding of post editor\">
  98. ".$posttitle."
  99. </a>
  100. </p>
  101. <div id=\"postcontent".$postincrementer."\" class=\"disabled\">
  102. <form method=\"POST\">
  103. <input type=\"hidden\" name=\"id\" value=\"$id\" />
  104. <textarea id=\"innerHTML".$postincrementer."\" name=\"content\" \">
  105. </textarea><br />
  106. <input type=\"hidden\" name=\"app\" value=\"blog\" />
  107. <input type=\"submit\" name=\"mod\" value=\"Commit Edit\" class=\"coolbutton\">
  108. <input type=\"submit\" name=\"mod\" value=\"Delete Post\" class=\"coolbutton\">
  109. </form>
  110. </div>";
  111. $JSAIDS.="document.getElementById('link".$postincrementer."').addEventListener('click',function () {loadpost('index.php?app=blog&get_fragment=".urlencode($val)."','innerHTML".$postincrementer."',false);});\nwindow.postsLoaded['innerHTML".$postincrementer."'] = false;";
  112. }
  113. }
  114. print "<script type=\"text/javascript\">\n
  115. window.onload = function() { $JSAIDS };\n
  116. </script>";
  117. ?>