Guest User

locator go

a guest
Jan 6th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. <?php
  2. /* This is a hack of the thread locator to provide the old /go functionality from arch.413chan.net. Issue a redirect to the currently marked thread, also drop meta refresh tag in case headers can't be sent, and finally, if that doesn't work, just provide a link to the current marked thread
  3.  
  4. rev1: simple hack of existing code. removed stylesheet and timing code. added redirect() function, changed markerFound() function to issue the redirect, rather than writing out the locator's usual HTML page.
  5.  
  6. contact - fluxy@derpymail.com
  7. */
  8.  
  9. /*This is a totally remade Thread locator.
  10. It uses the 4chan json API for speed and independance from other services which could cause shit to break.
  11. It's also a lot more accurate than the previous locator.
  12. The old one would only check for the first 21kb image in the thread, and give up if that wasn't it. This will search every image until the thread ends, then move on.
  13. It will search every thread with the keyword in the title, on every page, and offer a substitute if the marker isn't posted.
  14.  
  15. rev1: keyword support, better thread finding.
  16. rev2: makes sure thread info exists in the json before trying to display it (cuts down wait times and possible crashes)
  17. rev3: fixed the previously very broken keyword system because i'm dumb and php hates me. Added support for tripcode/anon OPs. Added Images counter. re-indented codes.
  18. Feb 2013:
  19. rev4: Caches thread IDs of current thread to save from having to search through 4chan each time someone requests the thread, cleaned up code a lot, optimized keywords
  20. I still don't want to include the sub keywords to the search function, as it takes a really really long time to search with them, and people rarely adhere to them anyway.
  21. */
  22. function findthread()
  23. {
  24. $keywords = array("mlp g", "mlpg", "my little pony general", "mlp: g");
  25. //$keywords_sub = array("hub", "mlp", "pony", "ponies", "twilight", "sparkle", "rainbow", "dash", "pinkie", "pie", "rarity", "fluttershy", "applejack");
  26. for($x=0;$x<=10;$x++) //cycle through every page on /mlp/
  27. {
  28. $json_dump = getpageJson($x);
  29. for($i=0;$i<=sizeof($json_dump->threads);$i++) //cycle through all the threads on this page
  30. {
  31. if(!empty($json_dump->threads[$i]->posts[0]->sub)) //make sure the thread has a title (or sub (im guessing "subtext" or something))
  32. {
  33. if(preg_match("/^mlp general\b/i", $json_dump->threads[$i]->posts[0]->sub))
  34. {
  35. $threadJson = getThreadJson($json_dump->threads[$i]->posts[0]->no); //do this once, to save from having to load the page from 4chan more than once
  36. writeRawText("thread.cache", $json_dump->threads[$i]->posts[0]->no);
  37. markerFound($threadJson);
  38. return true;
  39. }
  40. }
  41. }
  42. }
  43.  
  44.  
  45. // couldn't find a thread, so tell the user and drop a link back to the locator
  46. // alternatively, we could just redirect to the locator without any information however it's probably best to let the user know what is happening.
  47. echo "<br/><br/>";
  48. echo "<center><b>Sorry!</b> Could not find current thread.</center>";
  49. echo "<center><a href='http://boards.4chan.org/mlp/catalog#s=general'>Click to check the catalog.</a></center>";
  50. return false;
  51. }
  52.  
  53. function writeRawText($fileURI, $content)
  54. {
  55. $fp = fopen($fileURI, "w+");
  56. if(!$fp) {
  57. //echo "cache failure";
  58. return false;
  59. }
  60. fputs($fp, $content);
  61. fclose($fp);
  62. }
  63. function readRawText($fileURI, $mode)
  64. {
  65.  
  66. $raw = "";
  67. $handle = @fopen($fileURI, $mode);
  68. if($handle==false)
  69. {
  70. return false;
  71. }
  72. while (!feof($handle))
  73. {
  74. $raw .= fread($handle,8192);
  75. }
  76. fclose($handle);
  77. return $raw;
  78. }
  79. function getThreadJson($threadID)
  80. {
  81. $threadRaw = readRawText("http://a.4cdn.org/mlp/thread/". $threadID. ".json", "r");
  82. if($threadRaw==false)
  83. {
  84. //echo "Thread failed to load, please refresh the page.<br/>";
  85. return false;
  86. }
  87. $threadJson = json_decode($threadRaw);//push the json data into something php can read
  88. return $threadJson;
  89. }
  90. function getpageJson($pageNum)
  91. {
  92. $pageRaw = readRawText("http://a.4cdn.org/mlp/". $pageNum. ".json", "r");
  93. if($pageRaw==false)
  94. {
  95. //echo "4chan page failed to load, please refresh the page.</br>";
  96. return false;
  97. }
  98. $pageJson = json_decode($pageRaw);//push the json data into something php can read
  99. return $pageJson;
  100. }
  101. function checkForMarkerFromThreadJson($threadJson)
  102. {
  103. return true;
  104.  
  105. for($i=0;$i<=sizeof($threadJson->posts)-1;$i++)//cycle through every post in thread
  106. {
  107. if(!empty($threadJson->posts[$i]->filename))//make sure the post has an image (by checking if post data contains a filename)
  108. {
  109. if($threadJson->posts[$i]->md5 == "YgIC5DRjGYcY2F4I+vJkOw==")//compare image md5 with the known marker md5
  110. {
  111. return true;
  112. }
  113. }
  114. }
  115. return false;
  116. }
  117. function markerFound($threadJson)
  118. {
  119. // quick hack to make this a /go page
  120. redirect("http://boards.4chan.org/mlp/thread/". $threadJson->posts[0]->no); //302 "Moved" Redirect to current thread
  121. }
  122. function redirect($url, $statusCode = 303)
  123. {
  124. if (headers_sent() === false)
  125. {
  126. header('Location: ' . $url, true, $statusCode);
  127. } else { // couldn't send headers, so drop a meta refresh
  128. echo "<meta http-equiv='Location' content='" . $url . "'/>";
  129. echo "<center>Oops! Could not write location header: headers already sent. You should be redirected to the correct thread in a few seconds. If not, <a href='" . $url . "'>Click here to go to the current marked thread</a></center>";
  130. }
  131. die();
  132. }
  133. function checkCache()
  134. {
  135. //1 read thread from file (if none exists, skip to 3)
  136. //2 cycle through posts in thread for the marker to hit the md5
  137. //3 if it doesn't, return false, run the normal findthread() and write the result to the cache
  138.  
  139. $cachedThread = readRawText("thread.cache", "r");//read the file
  140.  
  141. if($cachedThread!=false)//if it finds text in the file
  142. {
  143. $json = getThreadJson($cachedThread);
  144. if ($json!=false)
  145. {
  146. if(!$json->posts[0]->archived && checkForMarkerFromThreadJson($json)==true)
  147. {
  148. markerFound($json);
  149. }
  150. else
  151. {
  152. findthread();
  153. }
  154. }
  155. else
  156. {
  157. findthread();
  158. }
  159. }
  160. else
  161. {
  162. findthread();
  163. }
  164. }
  165. checkCache();
  166. ?>
Add Comment
Please, Sign In to add comment