<?
error_reporting(0);

$_POST['action'] ? $action = $_POST['action'] : $action = $_GET['action'];

if ( $action == 'check_site' )
{
	$google   = new GoogleScraper( 'http://www.google.pl/search?q=site:' . urlencode( $_POST['url'] ) . '&num=10&start=0&hl=pl&gws_rd=cr,ssl', null, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36' );
  echo $result   = $google -> initGoogle();
}

if ( $action == 'get_sites' )
{
  $google   = new GoogleScraper( 'http://www.google.pl/search?num=10&hl=pl&q=' . urlencode( $_POST['phrase'] ) . '&gws_rd=cr,ssl', null, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36' );
  echo $result   = $google -> initGoogle();
  exit;
}

if ( $action == 'test' )
{  
  $url 	  = 'https://www.google.pl/search?q=' . urlencode( 'strony www Rzeszów' ) . '&num=10&start=0&hl=pl&lr=lang_pl&tbs=lr:lang_1pl&near=' . urlencode( 'Rzeszów' );
  
  $google   = new GoogleScraper( $url );
  echo $result   = $google -> initGoogle();
  exit;
}

if ( $action == 'check_rank' )
{
	$phrase = $_POST['phrase'];
	$_POST['localization'] != '' ? $localization = $_POST['localization'] : $localization = 'Polska';
	$num = $_POST['num'];
	$start = $_POST['start'];
  $proxy = $_POST['proxy'];
  $userpasswd = $_POST['userpasswd'];
	
  $url 	  = 'http://www.google.pl/search?q=' . str_replace( ' ', '+', $_POST['phrase'] ) . '&start=' . $_POST['start'];
	
	if ( $_POST['localization'] )
		$url .= '&uule=' . uule( $_POST['localization'] );
	
	if ( $_POST['num'] != 10 )
		$url .= '&num=' . $_POST['num'];
	
  $google   = new GoogleScraper( $url );
  $google -> _proxy = $proxy;
  $google -> _userpasswd = $userpasswd;
  echo $result = $google -> initGoogle();
  exit;
}

if ( $action == 'check_version' )
{
  echo '1.219';
  exit;
}

if ( $action == 'update_script' )
{
  $curl = curl_init();
  curl_setopt( $curl, CURLOPT_HEADER, false );
  curl_setopt( $curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" );
  curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $curl, CURLOPT_VERBOSE, true );
  curl_setopt( $curl, CURLOPT_URL, 'http://www.projectpro.pl/updates/update_proxy.zip' );
  curl_setopt( $curl, CURLOPT_TIMEOUT, 60 );
  curl_setopt( $curl, CURLOPT_MAXREDIRS, 60 );
  $newUpdate = curl_exec( $curl );
  curl_close( $curl );

  $dlHandler = fopen( 'update_proxy.zip' , 'w');
  if ( !fwrite( $dlHandler, $newUpdate ) ) {
    exit();
  }
  fclose( $dlHandler );
  
  chmod( "monitoring.php", 0644 );

  if ( file_exists( 'update_proxy.zip' ) )
  {
    $zipHandle = @zip_open( 'update_proxy.zip' );
    $out .= '<ul>';

    while ( $aF = zip_read( $zipHandle ) )
    {
      $thisFileName = zip_entry_name( $aF );
      $thisFileDir  = dirname( $thisFileName );

      if ( substr( $thisFileName , -1 , 1 ) == '/' )
        continue;

      if ( !is_dir ( $thisFileDir ) )
      {
        mkdir ( $thisFileDir );
        $out .= '<li style="margin-left: 30px;">Utworzono katalog ' . $thisFileDir . '</li>';
      }

      if ( !is_dir( $thisFileName ) )
      {
        $out .= '<li style="margin-left: 50px;">' . $thisFileName . '...........';
        $contents   = zip_entry_read( $aF , zip_entry_filesize( $aF ) );
        $contents   = str_replace( "\r\n" , "\n" , $contents );
        $updateThis = '';

        $updateThis = fopen( $thisFileName , 'w' );
        fwrite( $updateThis , $contents );
        fclose( $updateThis );
        unset( $contents );
        $out .= ' zaktualizowano</li>';
      }
    }
    $out .= '</ul>';
    echo '1.219';
    unlink( 'update_proxy.zip' );
  }
}

class GoogleScraper {
  
  var $_headers    = array(
                      'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
                      'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36',
                      'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36',
                      'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36',
                      'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2226.0 Safari/537.36',
                      'Mozilla/5.0 (Windows NT 6.4; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36',
                      'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36',
                      'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2224.3 Safari/537.36',
                      'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2215.93 Safari/537.36'
                    );
  var $_cookie    = '';
  var $_header    = '';
  var $_proxy = '';
  var $_userpasswd = '';

  function __construct( $url, $header ) 
  {
    $this -> _url       = $url;
    $this -> _header    = $header;
  }
 
  function getpagedata( $url, $retry = 0 )
  {	
    $this -> _header ? $header = $this -> _header : $header = $this -> _headers[ rand( 0, count( $this -> _headers ) - 1 ) ];
    
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_HEADER, true );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_REFERER, 'www.google.pl' );
    curl_setopt( $ch, CURLOPT_USERAGENT, $header );
    curl_setopt( $ch, CURLOPT_INTERFACE, $_SERVER['SERVER_ADDR'] );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_MAXREDIRS, 5 );
    curl_setopt( $ch, CURLOPT_ENCODING, "gzip,deflate" );
    if ( $this -> _proxy ) {      
      $proxy = explode( ':', $this -> _proxy );      
      curl_setopt( $ch, CURLOPT_PROXYTYPE, 'HTTP' );
      curl_setopt( $ch, CURLOPT_PROXYPORT, $proxy[1] );
      curl_setopt( $ch, CURLOPT_PROXY, $proxy[0] );
      curl_setopt( $ch, CURLOPT_PROXYUSERPWD, $this -> _userpasswd );
    }
    $result = curl_exec( $ch );
    curl_close( $ch );
    
    if ( preg_match("|Location: (https?://\S+)|", $result, $m ) and $retry < 5 )
      return $this -> getpagedata( $m[1], $retry + 1 );
    
    return $result;
  }
  
  function initGoogle() 
  {
    return $this -> getpagedata( $this -> _url, $this -> _header );
  }
}

function uule($city) 
{
	$secretkey = array_merge(range('A','Z'), range('a','z'), range('0','9'), array('-', '_'));
	return trim('w+CAIQICI'.$secretkey[strlen($city)%count($secretkey)].base64_encode($city), '=');
}

function getHost($page, $www = false ) 
{
  preg_match('@^(?:http://)?([^/]+)@i', $page, $matches);
  if ( !$www )
    $matches[1] = str_replace("www.", "", $matches[1]);
  return $matches[1];
}

function curl_redir_exec( $ch , $debug = "" )
{
	static $curl_loops = 0;
	static $curl_max_loops = 20;

	if ($curl_loops++ >= $curl_max_loops)
	{
		$curl_loops = 0;
		return FALSE;
	}
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$data = curl_exec($ch);
	$debbbb = $data;
	@list ( $header , $data ) = explode( "\n\n" , $data , 2);
	$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

	if ($http_code == 301 || $http_code == 302) {
		$matches = array();
		preg_match('/Location:(.*?)\n/', $header, $matches);
		$url = @parse_url(trim(array_pop($matches)));
		if (!$url)
		{
			$curl_loops = 0;
			return $data;
		}
		$last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
		$new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');
		curl_setopt($ch, CURLOPT_URL, $new_url);
		return curl_redir_exec($ch);
	} else {
		$curl_loops=0;
		return $debbbb;
	}
}
return false;