microblog.inc 7.7 KB

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