Location: PHPKode > scripts > KubeStats > control/ajax.php
<?php
require_once("../includes/config.php");
require_once("../includes/functions/func.global.php");
require_once("../includes/functions/loggincheck.php");

// Connect to database
db_connect($config);

if($_GET['cmd'] == 'delete')
{
	mysql_query("DELETE FROM `".$config['db']['pre']."sites` WHERE `site_id` = '".addslashes($_GET['id'])."' LIMIT 1");
	echo 'deleted';
	exit;
}
elseif($_GET['cmd'] == 'add')
{
	$errors = 0;
	
	if($errors == 0)
	{
		mysql_query("INSERT INTO `".$config['db']['pre']."sites` ( `site_title` , `site_url` ) VALUES ('".addslashes($_GET['site_title'])."', '".addslashes($_GET['site_url'])."');");
	}
	
	echo 'added';
	exit;
}
elseif($_GET['cmd'] == 'view')
{
	$sites = array();
	$today_day = date("j");
	$today_month = date("n");
	$today_year = date("Y");
	$yester_time = strtotime("-1 day");
	$yester_day = date("j",$yester_time);
	$yester_month = date("n",$yester_time);
	$yester_year = date("Y",$yester_time);
	
	$query = "SELECT site_id,site_title,site_url FROM ".$config['db']['pre']."sites ORDER BY site_title";
	$query_result = @mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
	while ($info = @mysql_fetch_array($query_result))
	{
		$visits_today = mysql_fetch_row(mysql_query("SELECT visits FROM ".$config['db']['pre']."days WHERE site_id='".$info['site_id']."' AND day='".$today_day."' AND month='".$today_month."' AND year='".$today_year."' LIMIT 1"));
		$visits_yesterday = mysql_fetch_row(mysql_query("SELECT visits FROM ".$config['db']['pre']."days WHERE site_id='".$info['site_id']."' AND day='".$yester_day."' AND month='".$yester_month."' AND year='".$yester_year."' LIMIT 1"));
	
		$sites[$info['site_id']] = $info;
		if(isset($visits_today[0]))
		{
			$sites[$info['site_id']]['today'] = $visits_today[0];
		}
		else
		{
			$sites[$info['site_id']]['today'] = 0;
		}
		$sites[$info['site_id']]['yesterday'] = 0;
	}

	if(count($sites) == 0)
	{
		echo 'You havn\'t added any sites yet, <a href="javascript:change_page(\'add\');">click here</a> to add one.';
	}
	else
	{
		echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
		foreach ($sites as $key => $info)
		{
			if($info['site_title'] == '')
			{
				$info['site_title'] = '&nbsp;';
			}
		
			echo '<tr id="site'.$info['site_id'].'">';
			echo '<td style="font-size:14px;"><a href="javascript:opensite('.$info['site_id'].');">'.$info['site_title'].'</a></td>';
			echo '<td width="88"><a href="javascript:getcode('.$info['site_id'].');"><img src="images/code.gif" width="78" height="16" border="0"></a></td>';
			echo '<td width="88" align="left" valign="middle"><a href="javascript:opensite('.$info['site_id'].');"><img src="images/stats.gif" width="78" height="16" border="0"></a></td>';
			echo '<td width="58" height="20" align="left" valign="middle"><a href="javascript:editsite('.$info['site_id'].');"><img src="images/editfull.gif" width="48" height="16" border="0"></a></td>';
			echo '<td width="64" align="left" valign="middle"><a href="javascript:deletesite('.$info['site_id'].');"><img src="images/delete.gif" width="64" height="16" border="0"></a></td>';
			echo '</tr>';
		}
		echo '</table>';
	}
	
	exit;
}
elseif($_GET['cmd'] == 'summary')
{
	$start_stamp = strtotime("-7 day");
	$end_stamp = date("U");	

	$start_disp = date("j M",$start_stamp);
	$end_disp = date("j M",$end_stamp);

	echo '<div align="center"><img src="images/legend.gif"></div><img id="graph" src="graphs.php?id='.$_GET['id'].'&rand='.rand().'&time='.time().'&start_stamp='.$start_stamp.'&end_stamp='.$end_stamp.'"><br>';
	echo '<div align="center"><table width="200" border="0" cellpadding="0" cellspacing="0"><tr><td width="40" align="left"><a href="javascript:movegraphleft();">&lt;</a></td><td align="center"><span id="startdate">'.$start_disp.'</span> - <span id="enddate">'.$end_disp.'</span></td><td width="40" align="right"><a href="javascript:movegraphright();">&gt;</a></td></tr></table></div>';

	exit;
}
elseif($_GET['cmd'] == 'browsers')
{
	echo '<div align="center"><br><br><img id="browsers" src="graphs.php?id='.$_GET['id'].'&rand='.rand().'&time='.time().'&cmd=browsers"><br><br></div>';
	exit;
}
elseif($_GET['cmd'] == 'referers')
{
	$count = 0;
	$ref = array();

	$query = "SELECT url,count FROM ".$config['db']['pre']."referer WHERE site_id='".addslashes($_GET['id'])."' ORDER BY count DESC LIMIT 30";
	$query_result = @mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
	while ($info = @mysql_fetch_array($query_result))
	{
		$ref[$count] = $info;
		
		$count++;
	}
	
	if($count == 0)
	{
		echo 'No referers have been recorded yet';
	}
	else
	{
		echo '<table border="0" cellspacing="0" cellpadding="2">';
		echo '<tr><td style="padding-right:10px;"><strong>Hits</strong></td><td style="padding-right:10px;"><strong>URL</strong></td></tr>';
		foreach ($ref as $key => $info)
		{
			echo '<tr><td style="padding-right:10px;">'.$info['count'].'</td><td style="padding-right:10px;"><a href="'.$info['url'].'" target="_blank">'.substr($info['url'],0,60).'</a></td></tr>';
		}
		echo '</table>';
	}
	
	exit;
}
elseif($_GET['cmd'] == 'edit')
{
	$query = "SELECT site_id,site_title,site_url FROM ".$config['db']['pre']."sites WHERE site_id='".addslashes($_GET['id'])."' LIMIT 1";
	$query_result = @mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
	$info = @mysql_fetch_array($query_result);

	echo '<form name="form1" method="post" action=""><table width="500" border="0" cellspacing="0" cellpadding="0"><tr><td width="120" height="30">Site Title: </td><td><input name="site_title" type="text" id="site_title" value="'.stripslashes($info['site_title']).'"></td></tr><tr><td height="30">Site Url:</td><td><input name="site_url" type="text" id="site_url" value="'.stripslashes($info['site_url']).'"></td></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td><input type="button" name="Submit" value="Edit Site" onclick="save_site();"></td></tr></table></form>';
	exit;
}
elseif($_GET['cmd'] == 'save')
{
	$errors = 0;
	
	if($errors == 0)
	{
		mysql_query("UPDATE `stat_sites` SET `site_title` = '".addslashes($_GET['site_title'])."',`site_url` = '".addslashes($_GET['site_url'])."' WHERE `site_id` =".addslashes($_GET['site_id'])." LIMIT 1 ;");
	}
	
	echo 'added';
	exit;
}
elseif($_GET['cmd'] == 'search')
{
	$count = 0;
	
	$engines = array();
	$searches = array();
	$engines[0] = 'MSN';
	$engines[1] = 'Google';
	$engines[2] = 'Yahoo';
	$engines[3] = 'Live Search';

	$query = "SELECT search_word,search_engine,search_count FROM ".$config['db']['pre']."searches WHERE site_id='".addslashes($_GET['id'])."' ORDER BY search_count DESC LIMIT 30";
	$query_result = @mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
	while ($info = @mysql_fetch_array($query_result))
	{
		$searches[$count] = $info;
		
		$count++;
	}
	
	if($count == 0)
	{
		echo 'No search keywords have been recorded yet';
	}
	else
	{
		echo '<table border="0" cellspacing="0" cellpadding="2">';
		echo '<tr><td style="padding-right:10px;"><strong>Hits</strong></td><td style="padding-right:10px;"><strong>Keyword</strong></td><td style="padding-right:10px;"><strong>Search Engine</strong></td></tr>';
		foreach ($searches as $key => $info)
		{
			echo '<tr><td style="padding-right:10px;">'.$info['search_count'].'</td><td style="padding-right:10px;">'.$info['search_word'].'</td><td style="padding-right:10px;">'.$engines[$info['search_engine']].'</td></tr>';
		}
		echo '</table>';
	}
	
	exit;
}
elseif($_GET['cmd'] == 'visits')
{
	$count = 0;
	
	$visits = array();

	$query = "SELECT time,ip,host,referrer,page FROM ".$config['db']['pre']."recent WHERE site_id='".addslashes($_GET['id'])."' ORDER BY time DESC LIMIT 30";
	$query_result = @mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
	while ($info = @mysql_fetch_array($query_result))
	{
		$visits[$count] = $info;
		
		$count++;
	}
	
	if($count == 0)
	{
		echo 'No search keywords have been recorded yet';
	}
	else
	{
		echo '<table border="0" cellspacing="0" cellpadding="2">';
		echo '<tr><td style="padding-right:10px;"><strong>Date</strong></td><td style="padding-right:10px;"><strong>IP</strong></td><td style="padding-right:10px;"><strong>Page</strong></td><td style="padding-right:10px;"><strong>Referrer</strong></td></tr>';
		foreach ($visits as $key => $info)
		{
			if($info['referrer'] == '')
			{
				echo '<tr><td style="padding-right:10px;">'.date("j M H:i",$info['time']).'</td><td style="padding-right:10px;">'.$info['ip'].'</td><td style="padding-right:10px;"><a href="'.$info['page'].'" target="_blank">'.substr(str_replace('www.','',str_replace('http://','',$info['page'])),0,25).'</a></td><td style="padding-right:10px;">Direct Link</td></tr>';
			}
			else
			{
				echo '<tr><td style="padding-right:10px;">'.date("j M H:i",$info['time']).'</td><td style="padding-right:10px;">'.$info['ip'].'</td><td style="padding-right:10px;"><a href="'.$info['page'].'" target="_blank">'.substr(str_replace('www.','',str_replace('http://','',$info['page'])),0,25).'</a></td><td style="padding-right:10px;"><a href="'.$info['referrer'].'" target="_blank">'.substr(str_replace('http://','',$info['referrer']),0,20).'</a></td></tr>';
			}
		}
		echo '</table>';
	}
	
	exit;
}
?>
Return current item: KubeStats