Location: PHPKode > scripts > Google Dancer > google-dancer/googledancer.class.php
<?php 
/**
* This file contains the googledancer class that checks if Google is "dancing".
*
* @package googledancer
*/

/**
* googledancer class
*
* This class allows to check if Google is "dancing".
*
* @package googledancer
* @author Setec Astronomy
* @version 1.0
* @abstract Check if Google. is "dancing".
* @copyright 2004
* @example sample.php A sample code.
* @link http://setecastronomy.stufftoread.com
*/
class googledancer {
	
	/**
 	* @access private
	*/
	var $main_server = '';
	/**
 	* @access private
	*/
	var $alt_servers = array ();
	/**
 	* @access private
	*/
	var $domains = array ();
	
	/**
	* Default constructor
	*
	* This is the default constructor of googledancer class.
	*/
	// public function __construct ()
	function googledancer () {
		if (!function_exists ('fsockopen')) {
			die ('googledancer class needs the fsockopen function!');
		}
		$this->main_server = 'www.google.com';
		$this->alt_servers = array ('www.google.ch', 'www.google.at');
		$this->domains = array ('yahoo.com', 'cnn.com', 'amazon.com');
	}
	
	/**
 	* @access private
	*/
	// private safe_set (&$var_true, $var_false = '') {
	function safe_set (&$var_true, $var_false = '') {
		if (!isset ($var_true)) { 
			$var_true = $var_false; 
		}
		return $var_true;
	}

	/**
 	* @access private
	*/
	// private queryHTTP ($url, $timeout = 10) {
	function queryHTTP ($url, $timeout = 10) {
		$parsed = parse_url ($url);
		$this->safe_set ($parsed['host']);
		$this->safe_set ($parsed['port'], 80);
		$this->safe_set ($parsed['path']);
		$this->safe_set ($parsed['query']);
		$fp = fsockopen ($parsed['host'], $parsed['port'], $errno, $errstr, $timeout); 
		if ($fp !== false) { 
			fputs ($fp, "GET " . $parsed['path'] . "?" . $parsed['query'] . " HTTP/1.0\r\n"); 
			fputs ($fp, "Accept: */*\r\n"); 
			fputs ($fp, "Referer: http://setecastronomy.stufftoread.com/\r\n"); 
			fputs ($fp, "Accept-Language: en\r\n"); 
			fputs ($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\n"); 
			fputs ($fp, "Host: " . $parsed['host'] . "\r\n"); 
			fputs($fp, "Connection: close\r\n\r\n");
			$result = '';
			while (!feof ($fp)) { 
				$result .= fgets ($fp, 128); 
			} 
		  	fclose ($fp); 
			return $result;
		} 
		return false;
	}
	
	/**
	* The getBackLinks method
	*
	* This method returns an array with the backlinks count about some important websites.
	* @param integer $timeout the timeout value used in the fsockopen function.
	* @return mixed it returns an array or false if it fails
	*/
	// public function getBackLinks ($timeout = 10) {
	function getBackLinks ($timeout = 10) {
		$result = array ();
		$servers[] = $this->main_server;
		$servers = array_merge ($servers, $this->alt_servers);
		foreach ($this->domains as $domain) {
			foreach ($servers as $server) {
				$url = 'http://' . $server . '/search?hl=en&ie=UTF-8&q=link%3A' . urlencode ($domain);
				$content = $this->queryHTTP ($url);
				if ($content !== false) {
					if (preg_match ('/ of about <b>([\d,]*?)<\/b> linking /si', $content, $matches)) {
						$result[$domain][$server] = str_replace (',', '', $matches[1]);
					} else {
						$result[$domain][$server] = 0;
					}	
				}
			}
		}
		return $result;
	}

	/**
	* The analizeBackLinks method
	*
	* This method analyse the {@link googledancer::getBackLinks() googledancer::getBackLinks()} return value and 
	* returns a float with the percentage probability that Google is dancing.
	* @param array $results {@link googledancer::getBackLinks() googledancer::getBackLinks()} return value
	* @return mixed it returns a float value with the percentage or false if it fails
	*/
	function analizeBackLinks ($results) {
		if (!is_array ($results)) {
			return false;
		}
		$diff_count = 0;
		foreach ($results as $domain => $values) {
			if (is_array ($values) && isset ($values[$this->main_server])) {
				foreach ($this->alt_servers as $server) {
					if (isset ($values[$server]) && is_numeric ($values[$server]) && ($values[$server] > 0)) {
						$diff_count += abs ($values[$this->main_server] - $values[$server]) / $values[$server];
					} else {
						return false;
					}
				}
			} else {
				return false;
			}
		}
		return $diff_count * 100;
	}

	/**
	* The getIndexedPages method
	*
	* This method returns an array with the indexed pages count about some important websites.
	* @param integer $timeout the timeout value used in the fsockopen function.
	* @return mixed it returns an array or false if it fails
	*/
	// public function getIndexedPages ($timeout = 10) {
	function getIndexedPages ($timeout = 10) {
		$result = array ();
		$servers[] = $this->main_server;
		$servers = array_merge ($servers, $this->alt_servers);
		foreach ($this->domains as $domain) {
			foreach ($servers as $server) {
				$url = 'http://' . $server . '/search?hl=en&ie=UTF-8&q=site%3A' . urlencode ($domain);
				$content = $this->queryHTTP ($url);
				if ($content !== false) {
					if (preg_match ('/ of about <b>([\d,]*?)<\/b> from /si', $content, $matches)) {
						$result[$domain][$server] = str_replace (',', '', $matches[1]);
					} else {
						$result[$domain][$server] = 0;
					}	
				}
			}
		}
		return $result;
	}

	/**
	* The analizeIndexedPages method
	*
	* This method analyse the {@link googledancer::getIndexedPages() googledancer::getIndexedPages()} return value and 
	* returns a float with the percentage probability that Google is dancing.
	* @param array $results {@link googledancer::getIndexedPages() googledancer::getIndexedPages()} return value
	* @return mixed it returns a float value with the percentage or false if it fails
	*/
	function analizeIndexedPages ($results) {
		if (!is_array ($results)) {
			return false;
		}
		$diff_count = 0;
		foreach ($results as $domain => $values) {
			if (is_array ($values) && isset ($values[$this->main_server])) {
				foreach ($this->alt_servers as $server) {
					if (isset ($values[$server]) && is_numeric ($values[$server]) && ($values[$server] > 0)) {
						$diff_count += abs ($values[$this->main_server] - $values[$server]) / $values[$server];
					} else {
						return false;
					}
				}
			} else {
				return false;
			}
		}
		return $diff_count * 100;
	}
	
	/**
	* The getResults method
	*
	* This method returns an array with the results of the given query.
	* @param string $keywords the keyword used for the query.
	* @param integer $resultcount the number fo results to grab (10, 20, 50, 100).
	* @param integer $timeout the timeout value used in the fsockopen function.
	* @return mixed it returns an array or false if it fails
	*/
	function getResults ($keywords = '', $resultcount = 10, $timeout = 10) {
		$result = array ();
		$servers[] = $this->main_server;
		$servers = array_merge ($servers, $this->alt_servers);
		foreach ($servers as $server) {
			$url = 'http://' . $server . '/search?hl=en&ie=UTF-8&q=' . urlencode ($keywords);
			$content = $this->queryHTTP ($url);
			if ($content !== false) {
				if (preg_match_all ('/<p class=g><a href=(.*?) /si', $content, $matches)) {
					$result[$server] = $matches[1];
				} else {
					$result[$server] = array ();
				}	
			}
		}
		return $result;
	}
	
	/**
	* The analizeResults method
	*
	* This method analyse the {@link googledancer::getResults() googledancer::getResults()} return value and 
	* returns a float with the percentage probability that Google is dancing.
	* @param array $results {@link googledancer::getResults() googledancer::getResults()} return value
	* @return mixed it returns a float value with the percentage or false if it fails
	*/
	function analizeResults ($results) {
		if (!is_array ($results) || !isset ($results[$this->main_server])) {
			return false;
		}
		$result_count = 0;
		$diff_result_count = 0;
		$diff_index_count = 0;
		foreach ($this->alt_servers as $server) {
			if (isset ($results[$server])) {
				$diff_result_count += count ($results[$server]) - count (array_intersect ($results[$this->main_server], $results[$server]));
				$diff_index_count += count ($results[$server]) - count (array_intersect_assoc ($results[$this->main_server], $results[$server]));
				$result_count += count ($results[$server]) * 2;
			} else {
				return false;
			}
		}		
		if ($result_count == 0) {
			$result = 0;
		} else {
			$result = ($diff_result_count + $diff_index_count) / $result_count * 100;
		}
		return $result;
	}
}
?>
Return current item: Google Dancer