<?php
//stats
if(!defined('AUTHACCESS')){echo "Not Allowed";exit();}
$bots = array("AhrefsBot",
"Googlebot",
"MJ12bot",
"Bingbot",
"Slurp",
"DuckDuckBot",
"Baiduspider",
"YandexBot",
"sogou",
"exabot",
"SeznamBot",
"domainsbot",
"dotbot",
"robot",
"semrush",
"BLEXBot");






global $do_stats;
if($do_stats == 'yes') {

}else {
	//echo "Error : did not stat";
	return;
}
$tdate = date("Y-m-d");


$ref = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "unknown";

$mcstat = $_SERVER['REMOTE_ADDR'] . "|" . $_SERVER['REQUEST_URI'] . "|" . $ref . "|" . date("H:i:s") . "|" . $_SERVER['HTTP_USER_AGENT']  . "\n";

$thisbot = false;
$search_array = array_map('strtolower', $bots);
$usragent = $_SERVER['HTTP_USER_AGENT'];
foreach($search_array as $bot) {
	if (strpos($usragent, $bot) !== false){
		$thisbot = true;	
	}
}
if ($thisbot){
		$out_file ="mcstats/" . $tdate . "_bots.txt";
} else {
		$out_file ="mcstats/" . $tdate . ".txt";	
}


//$word = $_SERVER['HTTP_USER_AGENT'];
//if (in_array(strtolower($word), $search_array)) { 
//	$out_file ="mcstats/" . $tdate . "_bots.txt";
//} else {
//	$out_file ="mcstats/" . $tdate . ".txt";
//}

$file = fopen("$out_file","a");

// exclusive lock
if (flock($file,LOCK_EX)) {
	fwrite($file,$mcstat);
  	fflush($file);
 	 // release lock
  	flock($file,LOCK_UN);
} else {
 	 echo "Error locking file!";
}
fclose($file);


$se = false;

if(strpos($ref, strtolower("google")) !== false){
	$out_file ="mcstats/google_" . $tdate . ".txt";
	$se = true;
}
if(strpos($ref, strtolower("bing")) !== false){
	$out_file ="mcstats/bing_" . $tdate . ".txt";
	$se = true;

}


if ($se) {

	$file = fopen("$out_file","a");
	// exclusive lock
	if (flock($file,LOCK_EX)) {
		fwrite($file,$mcstat);
	  	fflush($file);
	 	 // release lock
	  	flock($file,LOCK_UN);
	} else {
	 	 echo "Error locking file!";
	}
	fclose($file);


}

?>
