microblog.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. if ($editable) { //Insert the Only JS the project should have, all it does is toggle a div
  3. echo "
  4. <script type=\"text/javascript\">
  5. function switchMenu(obj) {
  6. var el = document.getElementById(obj);
  7. if ( el.style.display != 'none' ) {
  8. el.style.display = 'none';
  9. }
  10. else {
  11. el.style.display = '';
  12. }
  13. }
  14. </script>\n";
  15. }
  16. echo '<p class="title"><a title="RSS" class="rss" href="/'.$basedir.$rssdir.'microblog.php"></a> '.$microblogtitle;
  17. //Set important times - $tdtime is today's date, $oldtime is the oldest known date a tCMS install had nuze for - defaults to today then searches microblog dir for entries to set date
  18. $tdtime = new DateTime(null, new DateTimeZone($timezone));
  19. $oldtime = clone $tdtime;
  20. //limit results of directory read to first entry -- much faster than doing it with PHP once you get a large filelist.
  21. exec("ls -tr1 ".$_SERVER["DOCUMENT_ROOT"].'/'.$basedir.$microblogdir." |head -1", $cmd_out);
  22. if(!empty($cmd_out[0])) {
  23. $oldtime = $oldtime = DateTime::createFromFormat('m.d.y', $cmd_out[0], new DateTimeZone($timezone));
  24. }
  25. $oldtime->sub(new DateInterval('P1D'));
  26. /*$today and $tmrw refer to times relative to what is passed by GET params -
  27. $today is the date requested by GET, $tmrw is bool designating whether $today is something other than $tdtime
  28. error indicates whether you supplied a bogus GET param for date.*/
  29. $tmrw = 0;
  30. $error = 0;
  31. $today = clone $tdtime;
  32. if(!empty($_GET["date"])) {
  33. $today = DateTime::createFromFormat('m.d.y', $_GET["date"], new DateTimeZone ($timezone));
  34. //Catch bogus input, set $tmwr to TRUE if $today was set to something other than today's date
  35. if (!filter_var($_GET["date"],FILTER_VALIDATE_REGEXP,array('options' => array('regexp' => "/^(0[1-9]|1[012])[.](0[1-9]|[12][0-9]|3[01])[.]\d\d/")))) {
  36. echo "</p>That's a funny looking date you provided there, mister.\n";
  37. $error=1;
  38. }
  39. else if ($today > $tdtime) { //catch if day supplied by GET is IN THE FUTURE
  40. echo "</p>Welcome to the future<br /><img style=\"max-width:100%; padding-left: auto; padding-right: auto;\" src=\"http://gunshowcomic.com/comics/20090930.png\" />\n";
  41. $error=1;
  42. }
  43. else if ($today < $tdtime) {
  44. $tmrw = 1;
  45. }
  46. }
  47. /*Catch if day in question has no news -
  48. If not, display day before (or before that if still no news.
  49. $oldtime used here to tell when to stop looping back)*/
  50. if (!$error) {
  51. while (empty($todaysnews)) {
  52. $todaysnews = ""; //Set it to be something empty to prevent logspam
  53. $yesterday = clone $today;//This may look strange, but it'll make sense later
  54. $tomorrow = clone $today;
  55. $tomorrow->add(new DateInterval('P1D'));
  56. $yesterday->sub(new DateInterval('P1D'));
  57. //Detect if We're at the end of postings
  58. if ($yesterday < $oldtime) {
  59. echo " (".$today->format('m.d.y')."):</p><hr />";
  60. echo "For me, it was a beginning, but for you it is the end of the road.<br /><hr />\n";
  61. if ($oldtime != $tdtime) {
  62. $tomorrow = clone $oldtime;
  63. $tomorrow->add(new DateInterval('P1D'));
  64. $tomorrow = $tomorrow->format('m.d.y');
  65. }
  66. $todaysnews = "end";
  67. }
  68. if ($todaysnews != "end") {
  69. //Get news from directory if any exists for that day, glob will return empty if nothing is in dir
  70. $todaysnews = glob($_SERVER["DOCUMENT_ROOT"].'/'.$basedir.$microblogdir.$today->format('m.d.y')."/*");
  71. //Set display date for today's news, set $today to be yesterday in order to get while loop to recurse correctly
  72. $realtime = $today->format('m.d.y');
  73. if(!empty($_GET['fwd']) && $_GET['fwd']) {//Check whether we are traversing forward or backward in time
  74. $today = clone $tomorrow;
  75. } else { //Default to going back
  76. $today = clone $yesterday;
  77. }
  78. //Finish by setting times for Yesterday and Tomorrow so that they can be used in links below
  79. $tomorrow = clone $yesterday;
  80. $tomorrow->add(new DateInterval('P2D'));
  81. $tomorrow = $tomorrow->format('m.d.y');
  82. $yesterday = $yesterday->format('m.d.y');
  83. }
  84. }
  85. if ($todaysnews != "end") {
  86. echo " (".$realtime."):</p><hr />\n";
  87. foreach ($todaysnews as $i) {
  88. $fh = fopen($i,'r');
  89. $fc = fread($fh,10000); //If a microblog item is more than 1kb, you are doing something wrong.
  90. fclose($fh);
  91. $json = json_decode($fc);
  92. if(is_null($json)) {
  93. echo $fc;
  94. } elseif (!empty($json->title) && !empty($json->url) && !empty($json->poster)) {
  95. $out = '<h3 class="blogtitles">
  96. <a href="'.$json->url.'">'.$json->title.'</a>
  97. <a class="usericon '.$json->poster.'" title="Posted by '.$json->poster.'"></a>
  98. </h3>';
  99. if(!empty($json->image)) {
  100. $out .= '<img class="mblogimg" src="'.$json->image.'" />';
  101. }
  102. if(!empty($json->audio)) {
  103. $out .= '<audio src="'.$json->audio.'" controls>
  104. Download Audio
  105. <a href="'.$json->audio.'">Here</a><br />
  106. </audio>';
  107. }
  108. if(!empty($json->video)) {
  109. $out .= '<video src="'.$json->video.'" controls>
  110. Download Video
  111. <a href="'.$json->video.'">Here</a><br />
  112. </video>';
  113. }
  114. if(!empty($json->comment)) {
  115. $out .= $json->comment;
  116. }
  117. $out .= '<hr />';
  118. echo $out;
  119. } #Note that if nothing works out here, I'm just opting not to show anything.
  120. if ($editable) {
  121. $id=basename($i);
  122. $editblock = "
  123. <a style=\"display: inline-block;\" onclick=\"switchMenu('$id');\">[Edit]</a>
  124. <div style=\"display: none;\" id=\"$id\">
  125. <form style=\"display: inline\" method=\"POST\">
  126. <input type=\"hidden\" name=\"id\" value=\"$i\" />
  127. <input type='hidden' name='action' value='Edit' />";
  128. if(is_null($json)) {
  129. $editblock .= "<textarea class=\"mbedit_text\" name=\"content\">$fc</textarea>";
  130. } else {
  131. $editblock .= '<input type="hidden" name="type" value="JSON" />
  132. Title: <input class="cooltext" type="text" name="title" value="'.$json->title.'" /><br />
  133. URL: <input class="cooltext" type="text" name="URL" value="'.$json->url.'" /><br />
  134. Image: <input class="cooltext" type="text" name="IMG" value="'.$json->image.'" /><br />
  135. Audio: <input class="cooltext" type="text" name="AUD" value="'.$json->audio.'" /><br />
  136. Video: <input class="cooltext" type="text" name="VID" value="'.$json->video.'" /><br />
  137. Comments: <textarea class="cooltext" name="comment">'.$json->comment.'</textarea>';
  138. }
  139. $editblock .= "<input class=\"coolbutton mbedit_button\" type=\"submit\" Value=\"Edit\" />
  140. </form>
  141. <form style=\"display: inline\" method=\"POST\">
  142. <input type=\"hidden\" name=\"id\" value=\"$i\" />
  143. <input type='hidden' name='action' value='Delete' />
  144. <input class=\"coolbutton mbedit_button\" type=\"submit\" value=\"Delete\" />
  145. </form>
  146. </div>
  147. <hr class=\"clear\" />";
  148. echo $editblock;
  149. }
  150. }
  151. echo "<a style=\"float: left;\" title=\"skips empty days\" href=\"?nav=2&date=".$yesterday."&fwd=0\">Older Entries</a>\n";
  152. }
  153. if ($tmrw) {
  154. echo "<a style=\"float: right;\" href=\"?nav=2&date=".$tomorrow."&fwd=1\">Newer Entries</a>\n";
  155. }
  156. }
  157. ?>