Location: PHPKode > scripts > Orca Ringmaker > orca-ringmaker/orm3/statistics.php
<?php /* ***** Orca Ringmaker - Statistics Processing *************** */


/* ****************************************************************** */
/* ***** Classes **************************************************** */
class ORM_statAbacus {
	var $hits = array();
	var $clks = array();
	var $errr = array();

	function __sleep() {
		while (list($key, $value) = each($this->hits)) $this->hits[$key] = implode(":", $value);
		while (list($key, $value) = each($this->clks)) $this->clks[$key] = implode(":", $value);
		if (isset($this->errr)) $this->errr = implode(":", $this->errr);
		return array("hits", "clks", "errr");
	}

	function __wakeup() {
		while (list($key, $value) = each($this->hits)) $this->hits[$key] = explode(":", $value);
		while (list($key, $value) = each($this->clks)) $this->clks[$key] = explode(":", $value);
		if (isset($this->errr)) $this->errr = explode(":", $this->errr);
	}

	function ORM_statAbacus($slots) {
		$this->hits['total'] = array_fill(0, $slots, 0);
		$this->clks['total'] = array_fill(0, $slots, 0);
		if ($slots < 100) {
			$this->hits['prev'] = array_fill(0, $slots, 0);
			$this->hits['next'] = array_fill(0, $slots, 0);
			$this->hits['rand'] = array_fill(0, $slots, 0);
			$this->hits['site'] = array_fill(0, $slots, 0);
			$this->clks['prev'] = array_fill(0, $slots, 0);
			$this->clks['next'] = array_fill(0, $slots, 0);
			$this->clks['rand'] = array_fill(0, $slots, 0);
			$this->errr = array_fill(0, $slots, 0);
		}
	}
}

class ORM_ringStatistics {
	var $ring;
	var $site = array();
	var $ids = array();
	var $now = 0;

	function ORM_ringStatistics() {
		global $_DDATA, $_SDATA, $_VDATA;

		@set_time_limit(300);
		ignore_user_abort(true);

		$this->ring = array(
			"days" => new ORM_statAbacus(56),
			"hours" => new ORM_statAbacus(1344),
			"browsers" => array()
		);

		if ($csv = @fopen("{$_SERVER['DOCUMENT_ROOT']}/{$_SDATA['directory']}/browsers.txt", "r")) {
			while ($row = fgetcsv($csv, 1024))
				if (count($row) == 3 && $row[0]{0} != "#")
					$this->ring['browsers'][$row[0]] = array($row[2], $row[1], 0);
			fclose($csv);
		}

		$delete = mysql_query("DELETE FROM `{$_DDATA['tablestat']}` WHERE `date`<(UNIX_TIMESTAMP()-4838400);", $_DDATA['link']);
		$optimize = mysql_query("OPTIMIZE TABLE `{$_DDATA['tablestat']}`;", $_DDATA['link']);

		$drop = mysql_query("DROP TABLE `{$_DDATA['tablestat']}_temp`;", $_DDATA['link']);
		$create = mysql_query("CREATE TABLE `{$_DDATA['tablestat']}_temp` SELECT * FROM `{$_DDATA['tablestat']}`;", $_DDATA['link']);

		$select = mysql_query("SELECT `id`, `title` FROM `{$_DDATA['tablesite']}` WHERE `status`='active';", $_DDATA['link']);
		while ($row = mysql_fetch_assoc($select))
			$this->site[$this->ids[] = $row['id']] = array("title" => $row['title'], "days" => new ORM_statAbacus(56));
		sort($this->ids);

		$mark = time();
		$this->now = $mark + $_VDATA['stats.timezone.offset'] * 3600;
		$adjHours = $mark + (60 - gmdate("i", $this->now)) * 60 + (60 - gmdate("s", $this->now));
		$adjDays = $adjHours + (24 - gmdate("G", $this->now)) * 3600;

		$all = mysql_unbuffered_query("SELECT * FROM `{$_DDATA['tablestat']}_temp` ORDER BY `date` DESC;");
		while ($row = mysql_fetch_assoc($all)) {
			$day = floor(($adjDays - $row['date']) / 86400);
			$hour = floor(($adjHours - $row['date']) / 3600);

			if ($day < 56) {
				if ($row['success'] == "yes") {
					if (count($this->ring['browsers'])) {
						reset($this->ring['browsers']);
						while (list($key, $value) = each($this->ring['browsers'])) {
							if (strlen($value[0])) {
								if (stristr($row['ua'], $value[0]) !== false) {
									$this->ring['browsers'][$key][2]++;
									break;
								}
							} else $this->ring['browsers'][$key][2]++;
						}
					}

					if ($row['to'] && isset($this->site[$row['to']])) {
						if ($day < 56) {
							$this->ring['days']->hits['total'][$day]++;
							$this->site[$row['to']]['days']->hits['total'][$day]++;
							switch ($row['type']) {
								case "next":
									$this->ring['days']->hits['next'][$day]++;
									$this->site[$row['to']]['days']->hits['next'][$day]++;
									break;
								case "prev":
									$this->ring['days']->hits['prev'][$day]++;
									$this->site[$row['to']]['days']->hits['prev'][$day]++;
									break;
								case "rand":
									$this->ring['days']->hits['rand'][$day]++;
									$this->site[$row['to']]['days']->hits['rand'][$day]++;
									break;
								case "site":
									$this->ring['days']->hits['site'][$day]++;
									$this->site[$row['to']]['days']->hits['site'][$day]++;
							}
						}
						if ($hour < 1344) $this->ring['hours']->hits['total'][$hour]++;
					}
					if ($row['from'] && isset($this->site[$row['from']])) {
						if ($day < 56) {
							$this->ring['days']->clks['total'][$day]++;
							$this->site[$row['from']]['days']->clks['total'][$day]++;
							switch ($row['type']) {
								case "next":
									$this->ring['days']->clks['next'][$day]++;
									$this->site[$row['from']]['days']->clks['next'][$day]++;
									break;
								case "prev":
									$this->ring['days']->clks['prev'][$day]++;
									$this->site[$row['from']]['days']->clks['prev'][$day]++;
									break;
								case "rand":
									$this->ring['days']->clks['rand'][$day]++;
									$this->site[$row['from']]['days']->clks['rand'][$day]++;
							}
						}
						if ($hour < 1344) $this->ring['hours']->clks['total'][$hour]++;
					}

				} else {
					$this->ring['days']->errr[$day]++;
					if ($row['to'] != "0") $this->site[$row['to']]['days']->errr[$day]++;
				}
			}
		}
		$drop = mysql_query("DROP TABLE `{$_DDATA['tablestat']}_temp`;", $_DDATA['link']);
	}
}


/* ****************************************************************** */
/* ***** Begin Processing ******************************************* */
$_SDATA['actcount'] = 0;
$_SDATA['allcount'] = 0;


if ($_DDATA['online']) {
	if ($_VDATA['stats.lock'] == "yes" && time() - $_VDATA['stats.date'] > 300)
		ORM_setData("stats.lock", "no");

	if ($_VDATA['stats.type'] != "none") {
		if (isset($_GET['key']) && $_GET['key'] == $_VDATA['stats.key'] && $_VDATA['stats.lock'] == "no") {
			set_error_handler(create_function('$errno, $errstr', 'ORM_setData("stats.lock", "no"); exit();'));

			ORM_setData("stats.lock", "yes");
			ORM_setData("stats.cache", serialize($_CACHE = new ORM_ringStatistics()));
			$optimize = mysql_query("OPTIMIZE TABLE `{$_DDATA['tablesite']}`;", $_DDATA['link']);
			ORM_setData("stats.date", time());
			ORM_setData("stats.lock", "no");
			exit();
		}

		if (!$_VDATA['stats.cache']) {
			if ($_VDATA['stats.lock'] == "no") {
				ORM_setData("stats.key", $key = md5(mt_rand(1, 9999999)));
				$st = pfsockopen($_SERVER['HTTP_HOST'], 80, $erstr, $errno, 5);
				@fwrite($st, "GET {$_SERVER['PHP_SELF']}?key=$key HTTP/1.0\r\nHost: {$_SERVER['HTTP_HOST']}\r\n\r\n");
			}

		} else {
			$_CACHE = unserialize($_VDATA['stats.cache']);

			$interval = ($_VDATA['stats.type'] == "hourly") ? 3600 : 86400;
			if (time() - $interval > $_VDATA['stats.date'] && $_VDATA['stats.lock'] == "no") {
				ORM_setData("stats.key", $key = md5(mt_rand(1, 9999999)));
				$st = pfsockopen($_SERVER['HTTP_HOST'], 80, $erstr, $errno, 5);
				@fwrite($st, "GET {$_SERVER['PHP_SELF']}?key=$key HTTP/1.0\r\nHost: {$_SERVER['HTTP_HOST']}\r\n\r\n");
			}
		}
	} else $_CACHE = new ORM_ringStatistics();

	if (isset($_CACHE) && is_object($_CACHE)) {

		foreach ($_LANG['range'] as $key => $range) {
			$_XDATA['ring']['days']['hits']['total'][$key] = array_sum(array_slice($_CACHE->ring['days']->hits['total'], 0, $key));

			if ($_USER['action'] == "Stats") {
				$_XDATA['ring']['days']['hits']['prev'][$key] = array_sum(array_slice($_CACHE->ring['days']->hits['prev'], 0, $key));
				$_XDATA['ring']['days']['hits']['next'][$key] = array_sum(array_slice($_CACHE->ring['days']->hits['next'], 0, $key));
				$_XDATA['ring']['days']['hits']['rand'][$key] = array_sum(array_slice($_CACHE->ring['days']->hits['rand'], 0, $key));
				$_XDATA['ring']['days']['hits']['site'][$key] = array_sum(array_slice($_CACHE->ring['days']->hits['site'], 0, $key));

				$_XDATA['ring']['days']['clks']['total'][$key] = array_sum(array_slice($_CACHE->ring['days']->clks['total'], 0, $key));
				$_XDATA['ring']['days']['clks']['prev'][$key] = array_sum(array_slice($_CACHE->ring['days']->clks['prev'], 0, $key));
				$_XDATA['ring']['days']['clks']['next'][$key] = array_sum(array_slice($_CACHE->ring['days']->clks['next'], 0, $key));
				$_XDATA['ring']['days']['clks']['rand'][$key] = array_sum(array_slice($_CACHE->ring['days']->clks['rand'], 0, $key));
			}

			foreach ($_CACHE->ids as $id) {
				$_XDATA['site'][$id]['days']['hits']['total'][$key] = array_sum(array_slice($_CACHE->site[$id]['days']->hits['total'], 0, $key));

				if ($_USER['action'] == "Stats") {
					$_XDATA['site'][$id]['days']['hits']['prev'][$key] = array_sum(array_slice($_CACHE->site[$id]['days']->hits['prev'], 0, $key));
					$_XDATA['site'][$id]['days']['hits']['next'][$key] = array_sum(array_slice($_CACHE->site[$id]['days']->hits['next'], 0, $key));
					$_XDATA['site'][$id]['days']['hits']['rand'][$key] = array_sum(array_slice($_CACHE->site[$id]['days']->hits['rand'], 0, $key));
					$_XDATA['site'][$id]['days']['hits']['site'][$key] = array_sum(array_slice($_CACHE->site[$id]['days']->hits['site'], 0, $key));

					$_XDATA['site'][$id]['days']['clks']['total'][$key] = array_sum(array_slice($_CACHE->site[$id]['days']->clks['total'], 0, $key));
					$_XDATA['site'][$id]['days']['clks']['prev'][$key] = array_sum(array_slice($_CACHE->site[$id]['days']->clks['prev'], 0, $key));
					$_XDATA['site'][$id]['days']['clks']['next'][$key] = array_sum(array_slice($_CACHE->site[$id]['days']->clks['next'], 0, $key));
					$_XDATA['site'][$id]['days']['clks']['rand'][$key] = array_sum(array_slice($_CACHE->site[$id]['days']->clks['rand'], 0, $key));
				}
			}

			$_XDATA['ring']['days']['errr'][$key] = array_sum(array_slice($_CACHE->ring['days']->errr, 0, $key));
			foreach ($_CACHE->ids as $id)
				$_XDATA['site'][$id]['days']['errr'][$key] = array_sum(array_slice($_CACHE->site[$id]['days']->errr, 0, $key));
		}

		if ($_USER['action'] == "Stats") {
			$_XDATA['ring']['days']['avg']['hits'] = $_XDATA['ring']['days']['avg']['clks'] = array_fill(0, 7, 0);
			for ($x = 7, $hStart = $cStart = 1; $x >= 0; $x--) {
				for ($y = 6; $y >= 0; $y--) {
					if ($_CACHE->ring['days']->hits['total'][$x * 7 + $y] && $hStart == 1) $hStart = $x + 1;
					$_XDATA['ring']['days']['avg']['hits'][$y] += $_CACHE->ring['days']->hits['total'][$x * 7 + $y];
					if ($_CACHE->ring['days']->clks['total'][$x * 7 + $y] && $cStart == 1) $cStart = $x + 1;
					$_XDATA['ring']['days']['avg']['clks'][$y] += $_CACHE->ring['days']->clks['total'][$x * 7 + $y];
				}
			}
			for ($y = 0; $y < 7; $y++) {
				$_XDATA['ring']['days']['avg']['hits'][$y] /= $hStart;
				$_XDATA['ring']['days']['avg']['clks'][$y] /= $cStart;
			}

			$_XDATA['ring']['hours']['avg']['hits'] = $_XDATA['ring']['hours']['avg']['clks'] = array_fill(0, 24, 0);
			for ($x = 55, $hStart = $cStart = 1; $x >= 0; $x--) {
				for ($y = 23; $y >= 0; $y--) {
					if ($_CACHE->ring['hours']->hits['total'][$x * 24 + $y] && $hStart == 1) $hStart = $x + 1;
					$_XDATA['ring']['hours']['avg']['hits'][$y] += $_CACHE->ring['hours']->hits['total'][$x * 24 + $y];
					if ($_CACHE->ring['hours']->clks['total'][$x * 24 + $y] && $cStart == 1) $cStart = $x + 1;
					$_XDATA['ring']['hours']['avg']['clks'][$y] += $_CACHE->ring['hours']->clks['total'][$x * 24 + $y];
				}
			}
			for ($y = 0; $y < 24; $y++) {
				$_XDATA['ring']['hours']['avg']['hits'][$y] /= $hStart;
				$_XDATA['ring']['hours']['avg']['clks'][$y] /= $cStart;
			}
		}

	} else if ($_USER['action'] == "Stats") {
		unset($_CACHE);

		$_USER['action'] = "Blank";
		$_SDATA['success'][] = $_LANG['06g'];
	}

	list($_SDATA['allcount']) = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM `{$_DDATA['tablesite']}`;", $_DDATA['link']));
	list($_SDATA['actcount']) = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM `{$_DDATA['tablesite']}` WHERE `status`='active';", $_DDATA['link']));
	list($_SDATA['usrcount']) = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM `{$_DDATA['tableuser']}` WHERE `level`>0;", $_DDATA['link']));
}

?>
Return current item: Orca Ringmaker