<html>
<head></head>
<body>

<?php

//function to get html code
//returns code or error
function get_URL($url){
	$status="";
	$content="";
	//function from http://www.php-mysql-tutorial.com/wikis/php-tutorial/reading-a-remote-file-using-php.aspx
	// make sure curl is installed
	if (function_exists('curl_init')) {
	   // initialize a new curl resource
	   $ch = curl_init();

	   // set the url to fetch
	   curl_setopt($ch, CURLOPT_URL, $url);

	   // don't give me the headers just the content
	   curl_setopt($ch, CURLOPT_HEADER, 0);

	   // return the value instead of printing the response to browser
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

	   // use a user agent to mimic a browser
	   curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');

	   $content = curl_exec($ch);	 
	   $response = curl_getinfo( $ch );
   	   // remember to always close the session and free all resources
	   curl_close($ch);

	    if ($response['http_code'] == 200 || $response['http_code'] == 301)
    	{
			$status='ok';
		} 
		else{
			$status=$response['http_code'];
		}


	} else {
	   // curl library is not installed so we better use something else
	   $content="false";
	   echo "no curl";
	   phpinfo(); //curl not installed
	} 
		$result[]=array($url, $status, $content);
		return $result;
}

//---------------------------------------------------------------------
//FUNCTIONS TO SCAN TRACKING CODES 

//1.Check if the html has the
//google analytics tracking code
function check_GoogleAnalytics($html){
	$result="Google Analytics";
	//var $code_seed="google-analytics.com/ga.js";//id to search for to assure tracking code is present
	$code_seed="_gat._getTracker";//id to search for to assure tracking code is present
	//string substr  (  string $string  ,  int $start  [,  int $length  ] )
	//int strpos  (  string $haystack  ,  mixed $needle  [,  int $offset = 0  ] )
	$pos=strpos( $html, $code_seed);

	if($pos){
		$account="";
		
		$pos_ini=strpos( $html, "_gat._getTracker(");
		$pos_end=strpos( $html, ";",$pos_ini);
		$account=substr($html, $pos_ini,$pos_end-$pos_ini);

		$result=$result."(".$account.")";
	}
	else{$result=$result."(False)";}
	//echo $result;
	return $result;
}

//2.Check if the html has the
//New google analytics tracking code
function check_GoogleAnalyticsNew($html){
	$result="Google Analytics NEW";
	//var $code_seed="google-analytics.com/ga.js";//id to search for to assure tracking code is present
	$code_seed="_setAccount";//id to search for to assure tracking code is present
	//string substr  (  string $string  ,  int $start  [,  int $length  ] )
	//int strpos  (  string $haystack  ,  mixed $needle  [,  int $offset = 0  ] )
	$pos=strpos( $html, $code_seed);

	if($pos){
		$account="";
		$pos_ini=strpos( $html, "_setAccount");
		$pos_end=strpos( $html, ";",$pos_ini);
		$account=substr($html, $pos_ini,$pos_end-$pos_ini);

		$result=$result."(".$account.")";
	}
	else{$result=$result."(False)";}
	//echo $result;
	return $result;
}

//3.Check if the html has the
//google analytics urchin tracking code
function check_GA_Urchin($html){
	$result="Google Analytics[urchin]";
	//var $code_seed="google-analytics.com/ga.js";//id to search for to assure tracking code is present
	$code_seed="www.google-analytics.com/urchin.js";//id to search for to assure tracking code is present
	//string substr  (  string $string  ,  int $start  [,  int $length  ] )
	//int strpos  (  string $haystack  ,  mixed $needle  [,  int $offset = 0  ] )
	$pos=strpos( $html, $code_seed);

	if($pos){
		$account="";
		$pos_ini=strpos( $html, "_uacct");
		$pos_end=strpos( $html, ";",$pos_ini);
		$account=substr($html, $pos_ini,$pos_end-$pos_ini);

		$result=$result."(".$account.")";
	}
	else{$result=$result."(False)";}
	//echo $result;
	return $result;
}

//4.Check if the html has the
//Nielsen SiteCensus tracking code
function check_Nielsen($html){
	$result="Nielsen";
	//var $code_seed="google-analytics.com/ga.js";//id to search for to assure tracking code is present
	$code_seed="secure-uk.imrworldwide.com";//id to search for to assure tracking code is present
	$pos_ini=strpos( $html, $code_seed);
	if($pos_ini){
		$result=$result."(True";
		//account
		$pos_ini=strpos( $html, "_rsCI");
		$pos_end=strpos( $html, ";",$pos_ini);
		if(($pos_ini>0)&&($pos_end>0)){
		$account=substr($html, $pos_ini,$pos_end-$pos_ini);
		$result=$result.",".$account;
		}
		
		//language
		$pos_ini=strpos( $html, "_rsCG");
		$pos_end=strpos( $html, ";",$pos_ini);
		if(($pos_ini>0)&&($pos_end>0)){
		$account=substr($html, $pos_ini,$pos_end-$pos_ini);
		$result=$result.",".$account;
		}
		
		//script version
		$pos_ini=strpos( $html, "src=\"//secure-uk.imrworldwide.com");
		if(($pos_ini>0)&&($pos_end>0)){
		$pos_end=strpos( $html, "js\"",$pos_ini);
		$account=substr($html, $pos_ini,$pos_end+2-$pos_ini);
		$result=$result.",".$account;
		}
		
		//end
		$result=$result.")";
	}
	else{$result=$result."(False)";}
	//echo $result;
	return $result;
}

//funtion to check tracking codes
function check_tracking($html){
	if(isset($html)){
		$lstTrackingCodes[]=check_GoogleAnalytics($html);
		$lstTrackingCodes[]=check_GoogleAnalyticsNew($html);
		//$lstTrackingCodes[]=check_GA_Urchin($html);
		$lstTrackingCodes[]=check_Nielsen($html);
		
		//print_r($lstTrackingCodes);
		return $lstTrackingCodes;
	}
}

//---------------------------------------------------------------------
//TESTING START
function print_url_tracking($html_report){
	//print_r($html_report[0][0]);//url
	//print_r($html_report[0][1]);//status
	//print_r($html_report[0][2]);//html
	//print_r($html_report[0][3]);//array tracking codes
	echo "<table border=\"1\">\n";
		echo "<tr>\n";
			echo "<td>".$html_report[0][0]."</td>";
			echo "<td>".$html_report[0][1]."</td>";	
			echo "<td>";
				//print_r($html_report[0][3]);
				foreach($html_report[0][3] as $item){
					echo $item."||";
				}
			echo "</td>";		
		echo "</tr>\n";
	echo "</table>\n";
}

echo "<h1>Testing</h1>";
if(!isset($HTTP_GET_VARS['url'])){
	$url='http://www.caixagalicia.es/wvio004_contenido/esp/pags/wvio004p_partdepositos.asp';
	$url1="http://www.librodeapuntes.es";
	//$url="http://www.fundacioncaixagalicia.org/";
	//$url="http://www.elpais.com";
	//$url="http://www.meneame.net/";
	//$url="http://www.barrapunto.com";
	//$url="http://www.aleydasolis.com/";
}else{$url=$HTTP_GET_VARS['url'];}

//call function
//$html_report=get_URL($url);
//$html_report[0][3]=check_tracking($html_report[0][2]);
//print_url_tracking($html_report);

//$html_report=get_URL($url1);
//$html_report[0][3]=check_tracking($html_report[0][2]);
//print_url_tracking($html_report);
/*
//print
	print_r($html_report[0][0]);//url
	//print_r($html_report[0][2]);//html
	print_r($html_report[0][1]);//status
	print_r($html_report[0][3]);//array tracking codes
*/
//TESTING END
//---------------------------------------------------------------------
?>
</body>
</html>
