<?php

class Profiler {
	private static $data = array();

	public static $profiling = true;
	public static $clientIp = array('127.0.0.1', '83.17.124.90', '212.77.105.136');

	public static function pageInfo() {
		if(self::init()) {
			self::log('page', 'HTTP_HOST', $_SERVER["HTTP_HOST"]);
			self::log('page', 'SERVER_ADDR', $_SERVER["SERVER_ADDR"]);
			$queries = self::getLog('mysql');
			$totalTime = 0;

			foreach($queries as $query) {
				$totalTime = ($totalTime+$query['value']);
			}
			self::log('page', 'Sql count', count($queries));
			self::log('page', 'Sql total time', $totalTime);
		}
	}


	public static function log($type, $message, $value) {
		if(self::init()) {
			self::$data[$type][] = array('message'=>$message, 'value'=>$value);
		}
	}

	public static function getLog($type) {
		if(isset(self::$data[$type])) {
			return self::$data[$type];
		} else {
			return array();
		}
	}

	public static function navigator() {
		if(self::init()) {

			if(isset($_GET['profileError'])) {
				if($_GET['profileError'] == 'true') {
					setcookie('error',1 , 0, '/');
				}else {
					setcookie('error',null , 0, '/');
					unset($_COOKIE['error']);
				}

			}

			if(isset($_COOKIE['error'])) {
				error_reporting(E_ALL);
				ini_set('display_errors', true);
			}

			if(isset($_GET['profileSql'])) {
				if($_GET['profileSql'] == 'true') {
					setcookie('sql',1 , 0, '/');
				}else {
					setcookie('sql',null , 0, '/');
					unset($_COOKIE['sql']);
				}

			}

			if(isset($_GET['profilePhp'])) {
				if($_GET['profilePhp'] == 'true') {
					setcookie('php',1 , 0, '/');
				}else {
					setcookie('php',null , 0, '/');
					unset($_COOKIE['php']);
				}
			}
		}
	}

	public static function debugInfo() {

		if(self::init()) {

			self::pageInfo();


			$showError = false;
			if(isset($_COOKIE['error'])) {
				$showError = true;
			}
			if(isset($_GET['profileError'])) {
				if($_GET['profileError'] == 'true') {
					$showError = true;
				} else {
					$showError = false;
				}
			}

			$showSql = false;
			if(isset($_COOKIE['sql'])) {
				$showSql = true;
			}
			if(isset($_GET['profileSql'])) {
				if($_GET['profileSql'] == 'true') {
					$showSql = true;
				} else {
					$showSql = false;
				}
			}

			$showPhp = false;
			if(isset($_COOKIE['php'])) {
				$showPhp = true;
			}
			if(isset($_GET['profilePhp'])) {
				if($_GET['profilePhp'] == 'true') {
					$showPhp = true;
				} else {
					$showPhp = false;
				}
			}

			if($showSql) {
				self::sqlDisplay();
			}
			if($showPhp) {
				self::phpInfoDisplay();
			}

			self::commonDisplay();
		}
	}

	private static function commonDisplay() {
		?>
<table style='background-color: white; position:fixed; bottom:0; left:0; border: 1px solid #000000;' onclick="if(document.getElementById('profilerDialog').style.display=='none'){document.getElementById('profilerDialog').style.display='block';}else{document.getElementById('profilerDialog').style.display='none';}">
	<tr><td>
					<?
					foreach(self::getLog('page') as $item) {
						echo $item['message'].':'.$item['value'].', ';
					}
					?>
		</td></tr>
</table>
<div id="profilerDialog" style="display: none; background-color: white; position:fixed; bottom:0; left:0; width: 300px; height: 60px; border: 1px solid #000000;">
	lista sql: <input type="checkbox" <?if(isset($_COOKIE['sql'])) {echo"checked='checked'";}?> onclick="if(this.checked){document.location.href='?profileSql=true'}else{document.location.href='?profileSql=false';}" />
	<br />
	phpinfo: <input type="checkbox" <?if(isset($_COOKIE['php'])) {echo"checked='checked'";}?> onclick="if(this.checked){document.location.href='?profilePhp=true'}else{document.location.href='?profilePhp=false';}" />
	<br />
	errorreporting-all: <input type="checkbox" <?if(isset($_COOKIE['error'])) {echo"checked='checked'";}?> onclick="if(this.checked){document.location.href='?profileError=true'}else{document.location.href='?profileError=false';}" />
</div>
	<?
	}

	private static function sqlDisplay() {
		if(self::$profiling == true) {
			$queries = self::getLog('mysql');
			echo"<table style='background-color: white;'>";
			$totalTime = 0;

			foreach($queries as $query) {
				?>
<tr><td><?= $query['message']; ?></td><td><?= $query['value']; ?></td></tr>
				<?
				$totalTime = ($totalTime+$query['value']);
			}

$sqlStart = self::getLog('mysqlStart');
$sqlEnd = self::getLog('mysqlEnd');
if(isset($sqlEnd[0]['value']) && isset($sqlStart[0]['value'])) {
$sqlTime = (($sqlEnd[0]['value'])+($sqlStart[0]['value']));
}
if(isset($sqlTime)){
echo"<tr><td>Czas nawiazania polaczenia: </td><td>".date("Y-d-m h:i:s", mktime(abs($sqlStart[0]['value'])))."</td></tr>";
echo"<tr><td>Czas trwania polaczenia: </td><td>".$sqlTime."</td></tr>";}
echo"<tr><td><br /></td><td><br /></td></tr>";
			echo"</table>";
		}
	}

	private static function phpInfoDisplay() {
		phpinfo();
	}

	private static function init() {
		$return = false;
		if(self::$profiling) {
			$return =  true;
		}
		if(in_array($_SERVER['REMOTE_ADDR'], self::$clientIp) && self::$profiling) {
			$return = true;
		} else {
			$return = false;
		}

		return $return;
	}

}

?>