Advertisement
Guest User

Untitled

a guest
Nov 1st, 2015
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.84 KB | None | 0 0
  1. <?php
  2.   class google_search
  3.   {
  4.     private $http;
  5.    
  6.     public function __construct()
  7.     {
  8.       $this->http = new http;
  9.     }
  10.    
  11.     private function parse($content)
  12.     {
  13.       if (defined('DEV')) echo '[gsearch] Parse content' . PHP_EOL;
  14.      
  15.       $result = array();
  16.      
  17.       if (preg_match_all('#<li class="g">(.+?)<\/div><\/li>#is', $content, $matches))
  18.       {
  19.         foreach ($matches[1] as $block)
  20.         {
  21.           $item = array();
  22.          
  23.           if (preg_match('#<h3 class="r"><a href="\/url\?q=(.+?)\&amp\;#is', $block, $block_matches))
  24.           {
  25.             $item['url'] = $block_matches[1];
  26.           }
  27.          
  28.           if (preg_match('#<h3 class="r">(.+?)<\/h3>#is', $block, $block_matches))
  29.           {
  30.             $item['title'] = html_entity_decode(strip_tags($block_matches[1]));
  31.           }
  32.          
  33.           if (preg_match('#<span class="st">(.+?)<\/span>#is', $block, $block_matches))
  34.           { // ???
  35.             $item['description'] = html_entity_decode(strip_tags($block_matches[1]));
  36.           }
  37.          
  38.           if (preg_match('#href="\/url\?q=(http:\/\/webcache\..+?)\&amp\;#is', $block, $block_matches))
  39.           {
  40.             $item['cache'] = rawurldecode($block_matches[1]);
  41.           }
  42.          
  43.           $result[] = $item;
  44.         }
  45.       }
  46.       elseif (preg_match_all('#<!--m-->(.+?)<!--n-->#is', $content, $matches))
  47.       {
  48.         foreach ($matches[1] as $block)
  49.         {
  50.           $item = array();
  51.          
  52.           if (preg_match('#<h3 class="r"><a href="(.+?)"#is', $block, $block_matches))
  53.           {
  54.             $item['url'] = $block_matches[1];
  55.           }
  56.          
  57.           if (preg_match('#<h3 class="r">(.+?)<\/h3>#is', $block, $block_matches))
  58.           {
  59.             $item['title'] = strip_tags($block_matches[1]);
  60.           }
  61.          
  62.           if (preg_match('#<span class="st">(.+?)<\/span>#is', $block, $block_matches))
  63.           {
  64.             $item['description'] = $block_matches[1];
  65.           }
  66.          
  67.           if (preg_match('#href="(http:\/\/webcache\..+?)"#is', $block, $block_matches))
  68.           {
  69.             $item['cache'] = $block_matches[1];
  70.           }
  71.          
  72.           $result[] = $item;
  73.         }
  74.       }
  75.       else
  76.       {
  77.         i()->error->userWarning('Cant parse google response');
  78.         return false;
  79.       }
  80.      
  81.       return $result;
  82.     }
  83.    
  84.     private function captcha()
  85.     {
  86.       $content = $this->http->response()->content();
  87.      
  88.       if (!preg_match('#src="(\/sorry\/image\?id=.*\&amp;hl=en)"#is', $content, $matches))
  89.       {
  90.         return false;
  91.       }
  92.      
  93.       $captchaUrl   = 'https://www.google.com' . $matches[1];
  94.       $captchaImage = $this->http->get($captchaUrl);
  95.      
  96.       if (defined('DEV')) echo '[gsearch] Get captcha image: ' .  $captchaUrl . PHP_EOL;
  97.      
  98.       $this->http->request()->content(array(
  99.         'method'   => 'base64',
  100.         'key'      => '???',
  101.         'regsense' => 1,
  102.         'body'     => base64_encode($captchaImage),
  103.       ));
  104.      
  105.       $content = $this->http->post('http://anti-captcha.com/in.php');
  106.      
  107.       @list($status, $captchaId) = explode('|', $content);
  108.      
  109.       if ($status != 'OK')
  110.       {
  111.         if (defined('DEV')) echo '[gsearch] Cant create captcha request: ' . $status . PHP_EOL;
  112.        
  113.         i()->error->userWarning('Create captcha: ' . $status);
  114.         return false;
  115.       }
  116.      
  117.       if (defined('DEV')) echo '[gsearch] Captcha request success' . PHP_EOL;
  118.      
  119.       while (true)
  120.       {
  121.         usleep(500000);
  122.        
  123.         $this->http->request()->content(array(
  124.           'key'    => '???',
  125.           'action' => 'get',
  126.           'id'     => $captchaId,
  127.         ));
  128.        
  129.         $content = $this->http->post('http://anti-captcha.com/res.php');
  130.        
  131.         @list($status, $captchaText) = explode('|', $response);
  132.        
  133.         if ($status == 'CAPCHA_NOT_READY')
  134.         {
  135.           continue;
  136.         }
  137.        
  138.         break;
  139.       }
  140.      
  141.       if (defined('DEV')) echo '[gsearch] Captcha check status: ' . $status . PHP_EOL;
  142.      
  143.       if ($status != 'OK')
  144.       {
  145.         i()->error->userWarning('Request captcha: ' . $status);
  146.         return false;
  147.       }
  148.      
  149.       if (!preg_match('#\<input type="hidden" name="id" value="(.+?)"\>#is', $content, $matches))
  150.       {
  151.         return false;
  152.       }
  153.      
  154.       $captchaFormId = $matches[1];
  155.      
  156.       $captchaForm = array(
  157.         'id'       => $captchaFormId,
  158.         'continue' => '',
  159.         'captcha'  => $captchaText,
  160.         'submit'   => 'ќтправить',
  161.       );
  162.      
  163.       if (defined('DEV')) echo '[gsearch] Submit google captcha form' . PHP_EOL;
  164.      
  165.       $this->request('https://ipv4.google.com/sorry/CaptchaRedirect' . '?' . http_build_query($captchaForm, '', '&'));
  166.     }
  167.    
  168.     private function request($url)
  169.     {
  170.       if (defined('DEV')) echo '[gsearch] Request url: ' . $url . PHP_EOL;
  171.      
  172.       $this->http->get($url);
  173.      
  174.       if ($this->http->response()->code() == 302)
  175.       {
  176.         $prevUrl = $url;
  177.         $url     = $http->response()->header('Location');
  178.        
  179.         if (defined('DEV')) echo '[gsearch] Request redirect url: ' . $url . PHP_EOL;
  180.        
  181.         $this->http->request()->referer($prevUrl);
  182.         $this->http->request()->userAgent('Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36');
  183.         $this->http->get($url);
  184.       }
  185.     }
  186.    
  187.     public function search($text)
  188.     {
  189.       return $this->parse(file_get_contents(i()->config->get('app/path/temp') . 'test2.txt'));
  190.      
  191.       if (defined('DEV')) echo '[gsearch] Search text: ' . $text . PHP_EOL;
  192.      
  193.       $query = array
  194.       (
  195.         'q'      => $text,
  196.         'start'  => 0,
  197.         'num'    => 100,
  198.         'hl'     => 'en',
  199.         'ie'     => 'UTF-8',
  200.         'gws_rd' => 'cr',
  201.       );
  202.      
  203.       $searchUrl = 'https://www.google.com/search?' . http_build_query($query, '', '&');
  204.      
  205.       $this->request($searchUrl);
  206.    
  207.       if ($this->http->response()->code() != 200)
  208.       { // Need captcha
  209.         if (defined('DEV')) echo '[gsearch] Need captcha' . PHP_EOL;
  210.        
  211.         $this->captcha();
  212.        
  213.         if (!$this->http->response()->code() != 200)
  214.         {
  215.           if (defined('DEV')) echo '[gsearch] Request Error hack captcha' . PHP_EOL;
  216.          
  217.           i()->error->userWarning('Cant hack captcha');
  218.           return false;
  219.         }
  220.       }
  221.      
  222.       if (defined('DEV')) echo '[gsearch] Parse google search response' . PHP_EOL;
  223.      
  224.       file_put_contents(i()->config->get('app/path/temp') . 'test2.txt', $this->http->response()->content());
  225.      
  226.       return $this->parse($this->http->response()->content());
  227.     }
  228.   }
  229. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement