Author: meigrafd Language: php
Description: beta Timestamp: 2018-02-18 21:49:03 +0000
View raw paste Reply
  1. <?php
  2. // v0.4.3 - GET version
  3. ini_set('track_errors', 1);
  4. ini_set('display_errors', 1);
  5. ini_set('log_errors', 1);
  6. $AGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1';
  7. ini_set('user_agent', $AGENT);
  8.  
  9. #----------------------------------------------
  10.  
  11. $forum_categories = array(
  12.     'Tutorials & Anleitungen',
  13.     'News',
  14.     'FAQ',
  15.     'Allgemeines',
  16.     'Raspberry Pi 3',
  17.     'Raspberry Pi 2, Modell B und B+',
  18.     'Raspberry Pi A, A+ und Zero',
  19.     'Compute Module',
  20. );
  21.  
  22. $old_forum_prefixs = array(
  23.     'shellscript',
  24.     'raspbian-noobs',
  25.     'raspbian',
  26.     'openelec',
  27.     'libreelec',
  28.     'java',
  29. );
  30.  
  31. $replace_umlauts_array = array(
  32.     'ae' => '',
  33.     'ue' => '',
  34.     'oe' => '',
  35. );
  36.  
  37. $search_url = 'https://forum-raspberrypi.de/search/';
  38. $search_fields = array(
  39.     'q' => '', // Suche nach Begriff (searchTerm)
  40.     'subjectOnly' => 1, // Nur Betreff durchsuchen
  41.     'findThreads' => 1, // Ergebnisse als Themen anzeigen
  42.     'types[]' => 'com.woltlab.wbb.post', // Suche in: Forum
  43. );
  44.  
  45. #----------------------------------------------
  46.  
  47. $forum_categories_file = '/tmp/.forum_categories';
  48. $usage_counter_file = '/tmp/.count_fris_b';
  49.  
  50. $DEBUG = isset($_GET['debug']) ? $_GET['debug'] : false;
  51. $old_url = isset($_POST['old_url']) ? $_POST['old_url'] : '';
  52.  
  53. if (empty($old_url)) {
  54.     echo "<form name='url' action='' method='POST'>\n";
  55.     echo "  <b class='tab'>Old Forum URL:</b> <input name='old_url' size='50'>\n";
  56.     echo "  <script type='text/javascript'>document.url.old_url.focus();</script>";
  57.     echo "  <input type='submit' value='Find'>\n";
  58.     echo "</form>\n";
  59.     if (file_exists($usage_counter_file)) {
  60.         $usage_counter = @file_get_contents($usage_counter_file);
  61.         echo "<div style='position:absolute; left:10px; bottom:0; height:30px; width:90%; overflow:hidden;'>\n";
  62.         echo "  <font size='0'>page used: ".$usage_counter."</font>\n";
  63.         echo "</div>\n";
  64.     }
  65.     exit(0);
  66. }
  67.  
  68. if ($DEBUG) { echo "URL: ".$old_url."<br/><br/>\n"; }
  69.  
  70. #----------------------------------------------
  71.  
  72. if (file_exists($usage_counter_file)) {
  73.     $usage_counter = @file_get_contents($usage_counter_file);
  74.     $usage_counter++;
  75.     @file_put_contents($usage_counter_file, $usage_counter);
  76. } else {
  77.     $usage_counter = 1;
  78.     @file_put_contents($usage_counter_file, $usage_counter);
  79. }
  80.  
  81. #----------------------------------------------
  82.  
  83. if ($DEBUG) {
  84.     $forum_categories = get_forum_categories();
  85.     echo "<pre>";
  86.     var_dump($forum_categories);
  87.     echo "</pre>";
  88. } else {
  89.     if (file_exists($forum_categories_file)) {
  90.         $forum_categories = file($forum_categories_file);
  91.     } else {
  92.         $forum_categories = get_forum_categories();
  93.     }
  94. }
  95.  
  96. #----------------------------------------------
  97.  
  98. $parsed_url = parse_url($old_url);
  99. $old_path = ltrim($parsed_url['path'], '/');
  100. if (!empty($parsed_url['query'])) { $post_id = preg_split('/\=/', $parsed_url['query'])[1]; }
  101.  
  102. #----------------------------------------------
  103.  
  104. // Remove 'Thread-' from old path
  105. $search_path = preg_replace("/^\bThread-\b/", '', $old_path, 1);
  106.  
  107. // Remove old forum prefix's, only one if found.
  108. foreach($old_forum_prefixs AS $prefix) {
  109. if (preg_match("/^\b$prefix\b/i", $search_path)) {            
  110.         $search_path = preg_replace("/^\b$prefix\b/siU", '', $search_path, 1);            
  111.         break;
  112.     }
  113. }
  114.  
  115. // Remove all '-' from search_path
  116. $search_path = preg_replace("/-/", ' ', $search_path);
  117.  
  118. #----------------------------------------------
  119.  
  120. // Remove category word from search_path, only the first word of category
  121. $first_search_path_word = preg_split('/ /', $search_path)[0];
  122. foreach($forum_categories AS $c => $category) {
  123.     $first_category_word = preg_split('/ /', $category)[0];
  124.     $first_category_word_s = substr($first_category_word, 0, -1);
  125.     if (preg_match("/".preg_quote($first_category_word, '/')."/siU", $first_search_path_word)) {
  126.         $search_path = preg_replace("/\b$first_category_word\b/siU", '', $search_path, 1);
  127.         break;
  128.     } elseif (preg_match("/".preg_quote($first_category_word_s, '/')."/siU", $first_search_path_word)) {
  129.         $search_path = preg_replace("/\b$first_category_word_s\b/siU", '', $search_path, 1);
  130.         break;
  131.     }
  132. }
  133.  
  134. #----------------------------------------------
  135.  
  136. $search_path = trim($search_path);
  137.  
  138. $search_path = str_replace(
  139.     array_keys($replace_umlauts_array),
  140.     array_values($replace_umlauts_array),
  141.     $search_path
  142. );
  143.  
  144. $search_fields['q'] = $search_path;
  145. $postvars = http_build_query($search_fields);
  146. $redirect_url = $search_url."?".$postvars;
  147.  
  148. #----------------------------------------------
  149.  
  150. if ($DEBUG) {
  151.     echo "Searching for: <b>".$search_path."</b><br/>\n";
  152.     echo "<pre>";
  153.     var_dump($postvars);
  154.     echo "</pre>\n";
  155. }
  156.  
  157. #----------------------------------------------
  158.  
  159. $search_result_content = @file_get_contents($redirect_url) OR die("Could not access Forum...");
  160. $regex = '<div class="containerHeadline">.*<h3><a href="(.*)">(.*)<\/a><\/h3>';
  161. if (preg_match_all("/$regex/siU", $search_result_content, $matches)) {
  162.     $new_url = $matches[1][0];
  163. }
  164.  
  165. if (isset($new_url)) {                  
  166.     $new_url = preg_split('/\?postID\=/', $new_url)[0];
  167.     if (isset($post_id)) {
  168.         $new_url = $new_url."?postID=".$post_id."#post".$post_id;
  169.     }
  170.    
  171.     if ($DEBUG) {
  172.         echo "<pre>";
  173.         var_dump($new_url);
  174.         echo "</pre>\n";
  175.     } else {
  176.         redirect($new_url);
  177.     }
  178. }
  179.  
  180. if ($DEBUG) {
  181.     echo "<pre>";
  182.     var_dump($redirect_url);
  183.     echo "</pre>\n";
  184. } else {
  185.     redirect($redirect_url);
  186. }
  187.  
  188. #----------------------------------------------
  189.  
  190. function redirect($url, $statusCode=303) {
  191.     header('Location: ' . $url, true, $statusCode);
  192.     die();
  193. }
  194.  
  195. function get_forum_categories() {
  196.     $new_content = @file_get_contents("https://forum-raspberrypi.de/forum/") OR die("Could not access Forum...");
  197.     $regex = '<div class="wbbBoardMain">.*<div class="containerHeadline">.*<h3><a href="(.*)">(.*)<\/a><\/h3>';
  198.     if (preg_match_all("/$regex/siU", $new_content, $matches)) {
  199.         $forum_categories = array();
  200.         foreach($matches[1] AS $c => $url) {
  201.             $forum_categories[] = $matches[2][$c];
  202.         }
  203.     }
  204.     @unlink($forum_categories_file);
  205.     foreach($forum_categories AS $category) {
  206.         @file_put_contents($forum_categories_file, "$category\n", FILE_APPEND);
  207.     }
  208.     return $forum_categories;
  209. }
  210. ?>
  211.  
View raw paste Reply