<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

class evercookie
{
	public static function cache()
	{
		Debug_HackerConsole_Main::disable();
		// we don't have a cookie, user probably deleted it, force cache
		if (!$_COOKIE["evercookie_cache"]) {
			header("HTTP/1.1 304 Not Modified");
			exit;
		}
		
		header('Content-Type: text/html');
		header('Last-Modified: Wed, 30 Jun 2010 21:36:48 GMT');
		header('Expires: Tue, 31 Dec 2030 23:30:45 GMT');
		header('Cache-Control: private, max-age=630720000');
		
		echo $_COOKIE["evercookie_cache"];
		
		exit;
	}
	
	public static function etag()
	{
		Debug_HackerConsole_Main::disable();
		// we don't have a cookie, so we're not setting it
		if (!$_COOKIE["evercookie_etag"]) {
			// read our etag and pass back
			if (!function_exists('apache_request_headers')) {
				function apache_request_headers()
				{
					// Source: http://www.php.net/manual/en/function.apache-request-headers.php#70810
					$arh     = array();
					$rx_http = '/\AHTTP_/';
					foreach ($_SERVER as $key => $val) {
						if (preg_match($rx_http, $key)) {
							$arh_key    = preg_replace($rx_http, '', $key);
							$rx_matches = array();
							// do some nasty string manipulations to restore the original letter case
							// this should work in most cases
							$rx_matches = explode('_', $arh_key);
							if (count($rx_matches) > 0 and strlen($arh_key) > 2) {
								foreach ($rx_matches as $ak_key => $ak_val)
									$rx_matches[$ak_key] = ucfirst($ak_val);
								$arh_key = implode('-', $rx_matches);
							}
							$arh[$arh_key] = $val;
						}
					}
					return ($arh);
				}
			}
		
			$headers = apache_request_headers();
			echo $headers['If-None-Match'];
		
			exit;
		}
		
		// set our etag
		header('Etag: ' . $_COOKIE["evercookie_etag"]);
		echo $_COOKIE["evercookie_etag"];
		exit;
	}
	
	public static function png()
	{
		Debug_HackerConsole_Main::disable();
		// we don't have a cookie, user probably deleted it, force cache
		if (!$_COOKIE["evercookie_png"]) {
			header("HTTP/1.1 304 Not Modified");
			exit;
		}
		
		// width of 200 means 600 bytes (3 RGB bytes per pixel)
		$x = 200;
		$y = 1;
		
		$gd = imagecreatetruecolor($x, $y);
		
		$data_arr = str_split($_COOKIE["evercookie_png"]);
		
		$x = 0;
		$y = 0;
		for ($i = 0; $i < count($data_arr); $i += 3) {
			$color = imagecolorallocate($gd, ord($data_arr[$i]), ord($data_arr[$i+1]), ord($data_arr[$i+2]));
			imagesetpixel($gd, $x++, $y, $color);
		}
		 
		header('Content-Type: image/png');
		header('Last-Modified: Wed, 30 Jun 2010 21:36:48 GMT');
		header('Expires: Tue, 31 Dec 2030 23:30:45 GMT');
		header('Cache-Control: private, max-age=630720000');
		
		// boom. headshot.
		imagepng($gd);
		exit;
	}
}