bengine.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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="submit" name="mod" value="Create Post" class="coolbutton" />
  44. </form>
  45. <hr />
  46. </div>
  47. <?php
  48. /*Initialize vars, get directory contents*/
  49. $postincrementer = 0;
  50. $JSAIDS = "";
  51. $dir = $_SERVER['DOCUMENT_ROOT'].$basedir.'/'.$blogdir;
  52. $postlisting = scandir($dir);
  53. rsort($postlisting, SORT_NUMERIC);
  54. /*Post Manipulation*/
  55. if ($_POST["id"] != "") {
  56. /*Post Deletion*/
  57. if ($_POST["mod"] == "Delete Post") {
  58. $fh = unlink($_POST["id"]);
  59. if (!$fh) die("ERROR: couldn't delete ".$_POST['id'].", check permissions");
  60. echo "Deleted ".$_POST["id"]."<br />";
  61. /*Post Editing*/
  62. } elseif ($_POST["mod"] == "Commit Edit") {
  63. $fh = fopen($_POST["id"], 'w');
  64. if (!$fh) die("ERROR: couldn't write to ".$_POST['id'].", check permissions");
  65. fwrite($fh,stripslashes($_POST["content"]));
  66. fclose($fh);
  67. echo "Edited ".$_POST["id"]."<br />";
  68. /*Post Creation*/
  69. } elseif ($_POST["mod"] == "Create Post") {
  70. $pnum = intval(substr($postlisting[0],0,strpos($postlisting[0],'-'))) + 1;
  71. $id = $dir.$pnum."-".$_POST["title"].".post";
  72. /*echo $id."<br />";
  73. var_dump($postlisting[0]);
  74. var_dump($_POST);
  75. die("FOOBAR");*/
  76. $fh = fopen($id, 'w');
  77. if (!$fh) die("ERROR: Couldn't Write ".$id.", check permissions");
  78. fwrite($fh,stripslashes($_POST["content"]));
  79. fclose($fh);
  80. echo "Created ".$id."<br />";
  81. /*Catchall*/
  82. } else {
  83. die("Nothing to do");
  84. }
  85. }
  86. /*Spit out the posts into the DOM so that they can be edited/deleted*/
  87. $postlisting = scandir($dir);
  88. rsort($postlisting, SORT_NUMERIC);
  89. foreach ($postlisting as $key=>$val) {
  90. $id = $_SERVER["DOCUMENT_ROOT"].$basedir.$blogdir.basename($val);
  91. $posttitle = strstr($val,'.', true);
  92. if (!empty($posttitle)) {
  93. $postincrementer++;
  94. print "
  95. <p id=\"post".$postincrementer."\" class=\"posteditortitle\">
  96. <a id=\"link".$postincrementer."\" href=\"javascript:toggle('postcontent".$postincrementer."')\" title=\"Toggle hiding of post editor\">
  97. ".$posttitle."
  98. </a>
  99. </p>
  100. <div id=\"postcontent".$postincrementer."\" class=\"disabled\">
  101. <form method=\"POST\">
  102. <input type=\"hidden\" name=\"id\" value=\"$id\" />
  103. <textarea id=\"innerHTML".$postincrementer."\" name=\"content\" \">
  104. </textarea><br />
  105. <input type=\"submit\" name=\"mod\" value=\"Commit Edit\" class=\"coolbutton\">
  106. <input type=\"submit\" name=\"mod\" value=\"Delete Post\" class=\"coolbutton\">
  107. </form>
  108. </div>";
  109. $JSAIDS.="document.getElementById('link".$postincrementer."').addEventListener('click',function () {loadpost('/".$basedir.$blogdir.$val."','innerHTML".$postincrementer."',false);});\nwindow.postsLoaded['innerHTML".$postincrementer."'] = false;";
  110. }
  111. }
  112. print "<script type=\"text/javascript\">\n
  113. window.onload = function() { $JSAIDS };\n
  114. </script>";
  115. ?>