mbengine.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. //TODO have include file here for string size validation function on titles, XSS Prevention (?)
  3. // Function for creating a post, used twice in the code below (thus it is encapsulated).
  4. function write_post($fh=null) {
  5. //Pull in config due to function scoping
  6. extract(json_decode(file_get_contents('config/main.json'),true));
  7. $errors = array();//Create empty error array
  8. //Validation checks
  9. $url = stripslashes($_POST["URL"]);
  10. if (empty($_POST['URL'])) {
  11. $errors[] = "No url provided.";
  12. } else if (!filter_var($url,FILTER_VALIDATE_URL)) {
  13. $errors[] = '"'.$url.'" is not a valid ASCII URL.';
  14. }
  15. if (!empty($_POST["IMG"]) && !filter_var(stripslashes($_POST["IMG"]),FILTER_VALIDATE_URL)) {
  16. $errors[] = 'Image "'.$url.'" is not a valid ASCII URL.';
  17. }
  18. if (!empty($_POST["AUD"]) && !filter_var(stripslashes($_POST["AUD"]),FILTER_VALIDATE_URL)) {
  19. $errors[] = 'Audio "'.$url.'" is not a valid ASCII URL.';
  20. }
  21. if (!empty($_POST["VID"]) && !filter_var(stripslashes($_POST["VID"]),FILTER_VALIDATE_URL)) {
  22. $errors[] = 'Video "'.$url.'" is not a valid ASCII URL.';
  23. }
  24. /*TODO Need to do extra validation here to prevent folks from doing something stupid
  25. (like inserting executable code or large hex dumps of files). FILTER_VALIDATE_URL should catch
  26. most of this, but especially on the title and commentary I can't be sure.*/
  27. if (!count($errors)) {//All POST Vars needed to construct a coherent posting are here, let's go
  28. include_once("config/users.inc");//Import userland functions to figure out who's posting
  29. $postBody = array(
  30. "title" => stripslashes($_POST["title"]),
  31. "url" => $url,
  32. "image" => stripslashes($_POST["IMG"]),
  33. "audio" => stripslashes($_POST["AUD"]),
  34. "video" => stripslashes($_POST["VID"]),
  35. "comment" => stripslashes($_POST["comment"]),
  36. "poster" => $poster
  37. );//XXX Note here that if editing, it changes poster to whoever last edited the post
  38. if(empty($fh)) {//If none was passed in, we need to make one
  39. $tdtime = new DateTime(null, new DateTimeZone($timezone));
  40. $today = $tdtime->format('m.d.y');
  41. $now = $tdtime->format('H:i:s');
  42. $newsdir = $_SERVER["DOCUMENT_ROOT"].'/'.$basedir.$microblogdir;
  43. @mkdir($newsdir.$today);
  44. $fh = fopen($newsdir.$today."/".$now, 'w');
  45. if (!$fh) die("ERROR: couldn't write $newsdir$today/$now to $newsdir$today, check permissions");
  46. }
  47. fwrite($fh,json_encode($postBody));
  48. fclose($fh);
  49. } else {//Print errors at the top, since we didn't have what we needed from POST
  50. $message = 'Could not post due to errors:<br /><ul style="color: red; list-type: disc;">';
  51. foreach ($errors as $err) {$message .= "<li>$err</li>";}
  52. $message .= '</ul>POST Variable Dump below:<br /><em style="color: red; font-size: .75em;">'.print_r($_POST, true).'</em>';
  53. echo $message;
  54. }
  55. }
  56. //Microblog Posting engine - also used to display a form for submitting stories
  57. if($_SERVER['REQUEST_METHOD'] == 'POST') {//Don't do anything unless we are POSTing
  58. if(empty($_POST["id"])) {//See if we need to post something new
  59. write_post();
  60. } else {//OK, so we've established that the post has an ID. Let's see if we're editing/deleting a post.
  61. if (!empty($_POST["action"]) && $_POST["action"] == 'Delete') {//BLANKING IN PROGRESS
  62. $res = unlink($_POST["id"]);
  63. if (!$res) {
  64. header("HTTP/1.1 500 Internal Server Error");
  65. die("ERROR: couldn't delete ".$_POST['id'].", check permissions");
  66. }
  67. echo "Deleted ".$_POST["id"]."<br />";
  68. } else {//Attempt editing, first detecting whether content is json
  69. $fh = fopen($_POST["id"], 'w');
  70. if (!$fh) {
  71. header("HTTP/1.1 500 Internal Server Error");
  72. die("ERROR (500): couldn't open ".$_POST['id'].", check permissions");
  73. }
  74. if(empty($_POST["type"]) && !empty($_POST["content"])) {//Do some munging if it's just raw text
  75. $content = stripslashes($_POST["content"]);
  76. } else {//Process the JSON Post, write to file
  77. write_post($fh);
  78. }
  79. fwrite($fh,$content);//Just write the blob ,TODO validation
  80. fclose($fh);
  81. echo "Edited ".$_POST["id"]."<br />";
  82. }
  83. }
  84. }
  85. //DOM below
  86. ?>
  87. <div id="mbengine">
  88. <div id="submissions">
  89. <p class="title">Submissions:</p>
  90. <form id="Submissions" method="POST">
  91. Title *<br /><input class="cooltext" type="text" name="title" placeholder="Iowa Man Destroys Moon" />
  92. URL *<br /><input class="cooltext" type="text" name="URL" placeholder="https://oneweirdtrick.scam" />
  93. Image<br /><input class="cooltext" type="text" name="IMG" placeholder="https://gifdump.tld/Advice_Dog.jpg" />
  94. Audio<br /><input class="cooltext" type="text" name="AUD" placeholder="https://soundclod.com/static.mp3"/>
  95. Video<br /><input class="cooltext" type="text" name="VID" placeholder="https://youvimeo.tv/infomercial.mp4" />
  96. Comments:<br /><textarea class="cooltext" name="comment" placeholder="Potzrebie"></textarea>
  97. <input class="coolbutton" type="submit" value="Publish" text="Publish" />
  98. </form>
  99. </div>
  100. <div id="stories">
  101. <?php
  102. $editable = 1;
  103. include $_SERVER["DOCUMENT_ROOT"].'/'.$config['basedir']."sys/microblog.inc";
  104. ?>
  105. </div>
  106. </div>