query( 'SELECT id, proxy, user_id FROM pro_proxy_servers WHERE enabled = 1 AND bg < NOW() ORDER BY used ASC LIMIT 1' ); if ( $query -> rowCount() ) { while ( $row = $query -> fetch() ) { $curl = curl_init(); curl_setopt( $curl , CURLOPT_URL , $row['proxy'] ); curl_setopt( $curl , CURLOPT_POST , 1 ); curl_setopt( $curl , CURLOPT_POSTFIELDS , 'action=check_site&url=' . $url ); curl_setopt( $curl , CURLOPT_RETURNTRANSFER , 1 ); curl_setopt( $curl, CURLOPT_ENCODING, "gzip,deflate" ); $site = curl_exec( $curl ); curl_close( $curl ); if ( $debug ) { $data = file_get_contents( 'data/gs-' . date( 'Y-m-d' ) . '.txt' ); $data = $row['proxy'] . ' - ' . $site . ' - ' . $url . chr( 13 ) . chr( 10 ) . $data; file_put_contents( 'data/gs-' . date( 'Y-m-d' ) . '.txt', $data ); } if ( $site == -1 ) $db -> query( 'UPDATE pro_proxy_servers SET used = NOW(), bgd = DATE_ADD( NOW(), INTERVAL 1 HOUR ) WHERE id = ' . $row['id'] ); else $db -> query( 'UPDATE pro_proxy_servers SET used = NOW() WHERE id = ' . $row['id'] ); } } $query -> closeCursor(); return $site; } public function getSitesByKeywords( $phrase ) { global $db; $query = $db -> query( 'SELECT id, proxy, bg FROM pro_proxy_servers WHERE enabled = 1 AND ( bgd < NOW() OR bgd IS NULL ) ORDER BY used ASC LIMIT 1' ); if ( $query -> rowCount() ) while ( $row = $query -> fetch() ) { $curl = curl_init(); curl_setopt( $curl, CURLOPT_URL, $row['proxy'] ); curl_setopt( $curl, CURLOPT_HEADER, false ); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $curl, CURLOPT_POSTFIELDS, 'action=get_sites&phrase=' . $phrase ); curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 10 ); curl_setopt( $curl, CURLOPT_TIMEOUT, 10 ); curl_setopt( $curl, CURLOPT_ENCODING, "gzip,deflate" ); $result = curl_exec( $curl ); if ( !$result ) { $db -> query( 'UPDATE pro_proxy_servers SET used = NOW(), bg = ' . ( $row['bg'] + 1 ) . ', bgd = DATE_ADD( NOW(), INTERVAL ' . ( $row['bg'] + 1 ) * 15 . ' MINUTE ) WHERE id = ' . $row['id'] ); return -1; } if ( strpos( $result, 'Our systems have detected unusual traffic' ) !== false or strpos( $result, 'Aby kontynuowa' ) !== false or strpos( $result, 'Our systems have detected unusual traffic' ) !== false or strpos( $result, 'sending automated queries' ) !== false or strpos( $result, 'That’s an error' ) !== false ) { $db -> query( 'UPDATE pro_proxy_servers SET used = NOW(), bg = ' . ( $row['bg'] + 1 ) . ', bgd = DATE_ADD( NOW(), INTERVAL ' . ( $row['bg'] + 1 ) * 15 . ' MINUTE ) WHERE id = ' . $row['id'] ); return -1; } $db -> query( 'UPDATE pro_proxy_servers SET used = NOW(), bg = 0 WHERE id = ' . $row['id'] ); } else return -1; if ( strpos( $result, $phrase ) !== false ) { $results = self::parse_urls( $result ); foreach ( $results as $link ) $sites .= $link['href'] . '|||'; } if ( !$sites ) return -1; return $sites; } public static function parse_urls( $result ) { $results = array(); $doc = new DOMDocument(); $doc -> loadHTML( $result ); $div = $doc -> getElementById( 'res' ); $ol_a = $div -> getElementsByTagName( 'ol' ); foreach ( $ol_a as $ol ) { foreach ( $ol -> childNodes as $div ) { if ( $div -> tagName == 'div' and $div -> getAttribute( 'class' ) != '' ) { $a_a = $div -> getElementsByTagName( 'a' ); foreach ( $a_a as $a ) { if ( strpos( $a -> getAttribute( 'class' ), 'rllt__action' ) !== false and $a -> getAttribute( 'onmousedown' ) != '' ) { unset( $row ); $row['type'] = 'map'; $row['href'] = $a -> getAttribute( 'href' ); $results[] = $row; } else if ( $a -> getAttribute( 'onmousedown' ) != '' and $a -> getAttribute( 'class' ) == '' ) { unset( $row ); $row['type'] = 'organic'; $row['href'] = $a -> getAttribute( 'href' ); $results[] = $row; } } } } } if ( empty( $results ) ) { $doc = new DOMDocument(); $doc -> loadHTML( $result ); $div = $doc -> getElementById( 'res' ); $ol_a = $div -> getElementsByTagName( 'ol' ); foreach ( $ol_a as $ol ) { $h3_a = $ol -> getElementsByTagName( 'h3' ); foreach ( $h3_a as $h3 ) { $a_a = $h3 -> getElementsByTagName( 'a' ); foreach ( $a_a as $a ) { if ( $a -> getAttribute( 'class' ) == 'l' and $a -> getAttribute( 'onmousedown' ) != '' ) { unset( $row ); $row['type'] = 'map'; $row['href'] = $a -> getAttribute( 'href' ); $results[] = $row; } if ( $a -> getAttribute( 'class' ) == '' and $a -> getAttribute( 'onmousedown' ) != '' ) { unset( $row ); $row['type'] = 'organic'; $row['href'] = $a -> getAttribute( 'href' ); $results[] = $row; } } } } } /* 12.02.2016 */ if ( empty( $results ) ) { $doc = new DOMDocument(); $doc -> loadHTML( $result ); $div = $doc -> getElementById( 'res' ); $div_g_array = $div -> getElementsByTagName( 'div' ); foreach ( $div_g_array as $div_a ) { if ( $div_a -> getAttribute( 'class' ) == 'g' or $div_a -> getAttribute( 'class' ) == '_gt' ) { if ( $div_a -> getAttribute( 'class' ) == 'g' ) { $h3_a = $div_a -> getElementsByTagName( 'h3' ); foreach ( $h3_a as $h3 ) { $a_a = $h3 -> getElementsByTagName( 'a' ); foreach ( $a_a as $a ) { if ( $a -> getAttribute( 'class' ) == '' and $a -> getAttribute( 'onmousedown' ) != '' ) { unset( $row ); $row['type'] = 'organic'; $row['href'] = $a -> getAttribute( 'href' ); $results[] = $row; } } } } else if ( $div_a -> getAttribute( 'class' ) == '_gt' ) { $a_a = $div_a -> getElementsByTagName( 'a' ); foreach ( $a_a as $a ) { if ( $a -> getAttribute( 'class' ) == 'rllt__action-button _Jrh' and $a -> getAttribute( 'onmousedown' ) != '' ) { unset( $row ); $row['type'] = 'map'; $row['href'] = $a -> getAttribute( 'href' ); $results[] = $row; } } } } } } return $results; } } ?>