mbengine.inc 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. //TODO have include file here for string size validation function
  3. //Microblog Posting engine - also used to display a form for submitting stories
  4. if (!empty($_POST["title"])) {//First check for a title in your POST data. If so, assume we're posting something new
  5. $errors = array();//Create empty error array
  6. //Validation checks
  7. $url = stripslashes($_POST["URL"]);
  8. if (empty($_POST['URL'])) {$errors[] = "No url provided.";}
  9. else if (!filter_var($url,FILTER_VALIDATE_URL)) {$errors[] = '"'.$url.'" is not a valid ASCII URL.';}
  10. if (!empty($_POST["IMG"]) && !filter_var(stripslashes($_POST["IMG"]),FILTER_VALIDATE_URL)) {$errors[] = 'Image "'.$url.'" is not a valid ASCII URL.';}
  11. if (!empty($_POST["AUD"]) && !filter_var(stripslashes($_POST["AUD"]),FILTER_VALIDATE_URL)) {$errors[] = 'Audio "'.$url.'" is not a valid ASCII URL.';}
  12. if (empty($_POST["comment"]) && empty($_POST["IMG"]) && empty($_POST["AUD"])) {$errors[] = "Post content is blank, Please add a comment or media to the posting.";}
  13. //TODO Need to do extra validation here to prevent folks from doing something stupid like inserting executable code or large hex dumps of files
  14. if (!count($errors)) {//All POST Vars needed to construct a coherent posting are here, let's format our data and write this thing
  15. include_once("config/users.inc");//Import userland functions to figure out who's posting
  16. $postBody = array(
  17. "title" => stripslashes($_POST["title"]),
  18. "url" => $url,
  19. "image" => stripslashes($_POST["IMG"]),
  20. "audio" => stripslashes($_POST["AUD"]),
  21. "comment" => stripslashes($_POST["comment"]),
  22. "poster" => $poster
  23. );
  24. $tdtime = new DateTime(null, new DateTimeZone($timezone));
  25. $today = $tdtime->format('m.d.y');
  26. $now = $tdtime->format('H:i:s');
  27. $newsdir = $_SERVER["DOCUMENT_ROOT"].'/'.$basedir.$microblogdir;
  28. @mkdir($newsdir.$today);
  29. $fh = fopen($newsdir.$today."/".$now, 'w');
  30. if (!$fh) die("ERROR: couldn't write $newsdir$today/$now to $newsdir$today, check permissions");
  31. fwrite($fh,json_encode($postBody));
  32. fclose($fh);
  33. } else {//Print errors at the top, since we didn't have what we needed from POST
  34. $message = 'Could not post due to errors:<br /><ul style="color: red; list-type: disc;">';
  35. foreach ($errors as $err) {$message .= "<li>$err</li>";}
  36. $message .= '</ul>POST Variable Dump below:<br /><em style="color: red; font-size: .75em;">'.print_r($_POST, true).'</em>';
  37. echo $message;
  38. }
  39. } elseif (!empty($_POST["id"])) {//If no title is supplied, let's see if we're editing/deleting a post.
  40. if (empty($_POST["content"])) {//BLANKING IN PROGRESS
  41. $res = unlink($_POST["id"]);
  42. if (!$res) die("ERROR: couldn't delete ".$_POST['id'].", check permissions");
  43. echo "Deleted ".$_POST["id"]."<br />";
  44. } else {//Attempt editing, first detecting whether content is json
  45. $fh = fopen($_POST["id"], 'w');
  46. if (!$fh) die("ERROR: couldn't write to ".$_POST['id'].", check permissions");
  47. if(is_null(json_decode($_POST["content"]))) {$content = stripslashes($_POST["content"]);}//Do some munging if it's just raw text
  48. else {$content = $_POST["content"];}//JSON should be formatted correctly already
  49. fwrite($fh,$content);//Just write the blob ,TODO validation
  50. fclose($fh);
  51. echo "Edited ".$_POST["id"]."<br />";
  52. }
  53. }
  54. //DOM below
  55. ?>
  56. <div style="width: 100%; display: table;">
  57. <div style="width: 20%; display: table-cell;">
  58. <p class="title">Submissions:</p>
  59. <form id="Submissions" method="POST">
  60. Title<br /><input class="cooltext" type="text" name="title" />
  61. URL<br /><input class="cooltext" type="text" name="URL" />
  62. Image<br /><input class="cooltext" type="text" name="IMG" />
  63. Audio<br /><input class="cooltext" type="text" name="AUD" />
  64. Comments:<br /><textarea class="cooltext" name="comment">Potzrebie</textarea>
  65. <input class="coolbutton" type="submit" value="Publish" text="Publish" />
  66. </form>
  67. </div>
  68. <div id="stories">
  69. <?php
  70. $editable = 1;
  71. include $_SERVER["DOCUMENT_ROOT"].'/'.$basedir."sys/microblog.inc";
  72. ?>
  73. </div>
  74. </div>