Author: meigrafd Language: php
Description: alpha Timestamp: 2018-02-18 21:48:10 +0000
View raw paste Reply
  1. <?php
  2. // v0.2.3 - POST version
  3. ini_set('track_errors', 0);
  4. ini_set('display_errors', 0);
  5. ini_set('log_errors', 0);
  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. $forum_categories = array(
  11.     'Tutorials & Anleitungen',
  12.     'News',
  13.     'FAQ',
  14.     'Allgemeines',
  15.     'Raspberry Pi 3',
  16.     'Raspberry Pi 2, Modell B und B+',
  17.     'Raspberry Pi A, A+ und Zero',
  18.     'Compute Module',
  19. );
  20.  
  21. $old_forum_prefixs = array(
  22.     'shellscript',
  23.     'raspbian-noobs',
  24.     'raspbian',
  25.     'openelec',
  26.     'libreelec',
  27.     'java',
  28. );
  29.  
  30. $replace_umlauts_array = array(
  31.     'ae' => '',
  32.     'ue' => '',
  33.     'oe' => '',
  34. );
  35.  
  36. $search_url = 'https://forum-raspberrypi.de/search/';
  37. $search_fields = array(
  38.     'q' => '', // Suche nach Begriff (searchTerm)
  39.     'subjectOnly' => 1, // Nur Betreff durchsuchen
  40.     'findThreads' => 1, // Ergebnisse als Themen anzeigen
  41.     'types[]' => 'com.woltlab.wbb.post', // Suche in: Forum
  42. );
  43.  
  44. $old_url = isset($_POST['old_url']) ? $_POST['old_url'] : '';
  45. if (empty($old_url)) {
  46.     echo "<form name='url' action='' method='POST'>\n";
  47.     echo "  <b class='tab'>Old Forum URL:</b> <input name='old_url' size='50'>\n";
  48.     echo "  <input type='submit' value='Find'>\n";
  49.     echo "  <script type='text/javascript'>document.url.old_url.focus();</script>";
  50.     echo "</form>\n";
  51.  
  52.     if (file_exists("/tmp/.count_fris")) {
  53.         $usage_counter = @file_get_contents("/tmp/.count_fris");
  54.         echo "<div style='position:absolute; left:10px; bottom:0; height:30px; width:50%; overflow:hidden;'>\n";
  55.         echo "  <font size='0'>page used: ".$usage_counter."</font>\n";
  56.         echo "</div>\n";
  57.     }
  58.  
  59. } else {
  60.     $DEBUG = isset($_GET['debug']) ? $_GET['debug'] : false;
  61.     if ($DEBUG) { echo "URL: ".$old_url."<br/><br/>\n"; }
  62.    
  63.    
  64.     if (file_exists("/tmp/.count_fris")) {
  65.         $usage_counter = @file_get_contents("/tmp/.count_fris");
  66.         $usage_counter++;
  67.         @file_put_contents("/tmp/.count_fris", $usage_counter);
  68.     } else {
  69.         $usage_counter = 1;
  70.         @file_put_contents("/tmp/.count_fris", $usage_counter);
  71.     }
  72.    
  73.     $new_content = @file_get_contents("https://forum-raspberrypi.de/forum/") OR die("Could not access Forum...");
  74.     $regex = '<div class="wbbBoardMain">.*<div class="containerHeadline">.*<h3><a href="(.*)">(.*)<\/a><\/h3>';
  75.     if (preg_match_all("/$regex/siU", $new_content, $matches)) {
  76.         $forum_categories = array();
  77.         foreach($matches[1] AS $c => $url) {
  78.             $forum_categories[] = $matches[2][$c];
  79.         }
  80.     }
  81.    
  82.     if ($DEBUG) {
  83.         echo "<pre>";
  84.         var_dump($forum_categories);
  85.         echo "</pre>";
  86.     }
  87.    
  88.     $old_url = rtrim($old_url, '/');
  89.     $parsed_url = parse_url($old_url);
  90.     $old_path = ltrim($parsed_url['path'], '/');
  91.    
  92.     // Remove 'Thread-' from old path
  93.     $search_path = preg_replace("/^\bThread-\b/", '', $old_path, 1);
  94.  
  95.     // Remove old forum prefix's, only one if found.
  96.     foreach($old_forum_prefixs AS $prefix) {
  97.     if (preg_match("/^\b$prefix\b/i", $search_path)) {            
  98.             $search_path = preg_replace("/^\b$prefix\b/siU", '', $search_path, 1);            
  99.             break;
  100.         }
  101.     }
  102.  
  103.     // Remove all '-' from search_path
  104.     $search_path = preg_replace("/-/", ' ', $search_path);
  105.    
  106.     $first_search_path_word = strtok($search_path, ' ');
  107.    
  108.     foreach($forum_categories AS $c => $category) {
  109.         $first_category_word = strtok($category, ' ');
  110.         $first_category_word_s = substr($first_category_word, 0, -1);
  111.  
  112.         if (preg_match("/".preg_quote($first_category_word, '/')."/siU", $first_search_path_word)) {
  113.             $search_path = preg_replace("/\b$first_category_word\b/siU", '', $search_path, 1);
  114.             break;
  115.         }
  116.         if (preg_match("/".preg_quote($first_category_word_s, '/')."/siU", $first_search_path_word)) {
  117.             $search_path = preg_replace("/\b$first_category_word_s\b/siU", '', $search_path, 1);
  118.             break;
  119.         }
  120.     }
  121.    
  122.     $search_path = trim($search_path);
  123.    
  124.     $search_path = str_replace(
  125.         array_keys($replace_umlauts_array),
  126.         array_values($replace_umlauts_array),
  127.         $search_path
  128.     );
  129.    
  130.     $search_fields['q'] = $search_path;
  131.     $postvars = http_build_query($search_fields);
  132.    
  133.     if ($DEBUG) { echo "Searching for: <b>".$search_path."</b><br/>\n"; }
  134.     if ($DEBUG) {
  135.         echo "<pre>";
  136.         var_dump($postvars);
  137.         echo "</pre>\n";
  138.     }
  139.    
  140.     $ch = curl_init();
  141.         CURLOPT_URL => $search_url,
  142.         CURLOPT_POST => true,
  143.         CURLOPT_HEADER => false,
  144.         CURLOPT_SSL_VERIFYPEER => false,
  145.         CURLOPT_RETURNTRANSFER => $DEBUG,
  146.         CURLOPT_POSTFIELDS => $postvars,
  147.         CURLOPT_USERAGENT => $AGENT,  
  148.         CURLOPT_CONNECTTIMEOUT => 10,
  149.         CURLOPT_TIMEOUT => 30,
  150.         CURLOPT_FOLLOWLOCATION => !$DEBUG,
  151.         CURLOPT_FAILONERROR => true,
  152.     ));
  153.    
  154.     $response = curl_exec($ch);
  155.     if ($response === false){
  156.         echo "<br/><br/>Error: ".curl_error($ch)." - Code: ".curl_errno($ch)."<br/><br/>\n";
  157.     }
  158.     $info = curl_getinfo($ch);
  159.     curl_close($ch);
  160.    
  161.     if ($DEBUG) {
  162.         echo "curl getinfos: ";
  163.         echo "<pre>";
  164.         var_dump($info);
  165.         echo "</pre>";
  166.     }
  167.  
  168.  
  169. }
  170. ?>
  171.  
View raw paste Reply