Advertisement
Guest User

locator go

a guest
Dec 10th, 2015
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.28 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.                 foreach($keywords as $key)//cycle through keywords
  34.                 {
  35.                     if(stristr($json_dump->threads[$i]->posts[0]->sub,$key)!=FALSE)//compare the title to the keywords, case insenstive.
  36.                     {  
  37.                         $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
  38.                         if(checkForMarkerFromThreadJson($threadJson)==true)
  39.                         {
  40.                             writeRawText("thread.cache", $json_dump->threads[$i]->posts[0]->no);
  41.                             markerFound($threadJson);
  42.                             return true;
  43.                         }
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.     }
  49.  
  50.    
  51.     // couldn't find a thread, so tell the user and drop a link back to the locator
  52.     // alternatively, we could just redirect to the locator without any information however it's probably best to let the user know what is happening.
  53.     echo "<br/><br/>";
  54.     echo "<center><b>Sorry!</b>  Could not find current thread.</center>";
  55.     echo "<center><a href='http://boards.4chan.org/mlp/catalog#s=general'>Click to check the catalog.</a></center>";
  56.     return false;
  57. }
  58.  
  59. function writeRawText($fileURI, $content)
  60. {
  61.     $fp = fopen($fileURI, "w+");
  62.         if(!$fp) {
  63.           //echo "cache failure";
  64.           return false;
  65.         }
  66.       fputs($fp, $content);
  67.       fclose($fp);
  68. }
  69. function readRawText($fileURI, $mode)
  70. {
  71.  
  72.     $raw = "";
  73.     $handle = @fopen($fileURI, $mode);
  74.     if($handle==false)
  75.     {
  76.         return false;
  77.     }
  78.     while (!feof($handle))
  79.     {
  80.         $raw .= fread($handle,8192);
  81.     }
  82.     fclose($handle);
  83.     return $raw;
  84. }
  85. function getThreadJson($threadID)
  86. {
  87.     $threadRaw = readRawText("http://a.4cdn.org/mlp/thread/". $threadID. ".json", "r");
  88.     if($threadRaw==false)
  89.     {
  90.         //echo "Thread failed to load, please refresh the page.<br/>";
  91.         return false;
  92.     }
  93.     $threadJson = json_decode($threadRaw);//push the json data into something php can read
  94.     return $threadJson;
  95. }
  96. function getpageJson($pageNum)
  97. {
  98.     $pageRaw = readRawText("http://a.4cdn.org/mlp/". $pageNum. ".json", "r");
  99.     if($pageRaw==false)
  100.     {
  101.         //echo "4chan page failed to load, please refresh the page.</br>";
  102.         return false;
  103.     }
  104.     $pageJson = json_decode($pageRaw);//push the json data into something php can read
  105.     return $pageJson;
  106. }
  107. function checkForMarkerFromThreadJson($threadJson)
  108. {
  109.     return true;
  110.    
  111.     for($i=0;$i<=sizeof($threadJson->posts)-1;$i++)//cycle through every post in thread
  112.     {
  113.         if(!empty($threadJson->posts[$i]->filename))//make sure the post has an image (by checking if post data contains a filename)
  114.         {
  115.             if($threadJson->posts[$i]->md5 == "YgIC5DRjGYcY2F4I+vJkOw==")//compare image md5 with the known marker md5
  116.             {
  117.                 return true;
  118.             }
  119.         }
  120.     }
  121.     return false;
  122. }
  123. function markerFound($threadJson)
  124. {
  125.     // quick hack to make this a /go page
  126.     redirect("http://boards.4chan.org/mlp/thread/". $threadJson->posts[0]->no); //302 "Moved" Redirect to current thread
  127. }
  128. function redirect($url, $statusCode = 303)
  129. {
  130.     if (headers_sent() === false)
  131.     {
  132.         header('Location: ' . $url, true, $statusCode);
  133.     } else { // couldn't send headers, so drop a meta refresh
  134.         echo "<meta http-equiv='Location' content='" . $url . "'/>";
  135.         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>";
  136.     }
  137.    die();
  138. }
  139. function checkCache()
  140. {
  141. //1 read thread from file (if none exists, skip to 3)
  142. //2 cycle through posts in thread for the marker to hit the md5
  143. //3 if it doesn't, return false, run the normal findthread() and write the result to the cache
  144.  
  145. $cachedThread = readRawText("thread.cache", "r");//read the file
  146.  
  147.     if($cachedThread!=false)//if it finds text in the file
  148.     {
  149.         $json = getThreadJson($cachedThread);
  150.             if ($json!=false)
  151.             {
  152.             if(!$json->posts[0]->archived && checkForMarkerFromThreadJson($json)==true)
  153.             {
  154.                 markerFound($json);
  155.             }
  156.             else
  157.             {
  158.                 findthread();
  159.             }
  160.         }
  161.         else
  162.         {
  163.         findthread();
  164.         }
  165.     }
  166.     else
  167.     {
  168.         findthread();
  169.     }
  170. }
  171. checkCache();
  172. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement