<?


//  THIS FILE CONTAINS STAT-RELATED FUNCTIONS
//  FUNCTIONS IN THIS CLASS:
//    update_stats()
//    update_refurls()













// THIS FUNCTION UPDATES THE LATEST ROW IN THE STATS TABLE
// INPUT: $type REPRESENTING WHICH STAT TO INCREMENT
// OUTPUT: 
function update_stats($type) {
	global $database;

	// GET CURRENT TIME AND TIME ONE DAY AGO
	$time_dayago = mktime(0, 0, 0, date('n', time()), date('j', time())-1, date('Y', time()));

	// CHECK IF A DAY HAS PASSED, IF YES, INSERT NEW ROW
	$day = $database->database_fetch_assoc($database->database_query("SELECT stat_date FROM se_stats ORDER BY stat_id DESC LIMIT 1"));
	if($day[stat_date] <= $time_dayago) {
	  $nexttime = mktime(0, 0, 0, date('n', time()), date('j', time()), date('Y', time()));
	  $database->database_query("INSERT INTO se_stats (stat_date) VALUES ('$nexttime')");
	}

	// INCREASE REQUESTED STAT VALUE
	switch($type) {

	  case "views":
	    $database->database_query("UPDATE se_stats SET stat_views=stat_views+1 ORDER BY stat_id DESC LIMIT 1");
	    break;

	  case "visits":
	    $database->database_query("UPDATE se_stats SET stat_visits=stat_visits+1 ORDER BY stat_id DESC LIMIT 1");
	    break;

	  case "logins":
	    $database->database_query("UPDATE se_stats SET stat_logins=stat_logins+1 ORDER BY stat_id DESC LIMIT 1");
	    break;

	  case "signups":
	    $database->database_query("UPDATE se_stats SET stat_signups=stat_signups+1 ORDER BY stat_id DESC LIMIT 1");
	    break;

	  case "friends":
	    $database->database_query("UPDATE se_stats SET stat_friends=stat_friends+1 ORDER BY stat_id DESC LIMIT 1");
	    break;

	}

} // END update_stats() FUNCTION









// THIS FUNCTION GETS THE CURRENT VIEWER'S REFERRING URL AND ADDS IT TO REF URL STATS TABLE
// INPUT:
// OUTPUT: 
function update_refurls() {
	global $database;

	// IF URL IS NOT EMPTY
	$referring_url = $_SERVER["HTTP_REFERER"];
	if($referring_url != "") {

	  // IS URL ALREADY IN DATABASE? IF YES, ADD TO HITS. IF NO, ADD NEW ROW
	  $referring_url = str_replace("http://www.", "http://", $referring_url);
	  $database->database_query("INSERT INTO se_statrefs (statref_hits, statref_url) VALUES ('1', '$referring_url')
					ON DUPLICATE KEY UPDATE statref_hits=statref_hits+1");

	  // IF 1000 ROWS REACHED, DELETE ONE TO MAKE ROOM
	  $refurl_totalrows = $database->database_num_rows($database->database_query("SELECT statref_id FROM se_statrefs"));
	  if($refurl_totalrows >= 1000) { $database->database_query("DELETE FROM se_statrefs WHERE statref_hits='1' ORDER BY statref_id ASC LIMIT 1"); }
	}

} // END update_refurls FUNCTION

?>