<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2005, J. Carlos Nieto <xiam@users.sourceforge.net>
	*	This program is Free Software.
	*
	*	@package	Gekko
	*	@subpackage	/lib
	*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
	*	@author		J. Carlos Nieto <xiam@users.sf.net>
	*	@version	CVS: $Id:$
	*	@link		http://www.gekkoware.org
	*/

	/*
		Those are core functions supposed for being used in every
		script
	*/

	if (!defined("IN-GEKKO")) die("Get a life!");

	define ("GEKKO_CACHEDIR", GEKKO_ROOT."temp/cache/");
	define ("GEKKO_CACHELIFE", 60*15);

	$GLOBALS["cache"] = Array();

	Class Cache {
		var $cachedir;
		function Cache() {
			if (!file_exists($this->cachedir)) {
				mkdir($this->cachedir, 0777);
				if (!is_writable($this->cachedir)) chmod($this->cachedir, 0777);
			}

			$dp = opendir(GEKKO_CACHEDIR);
			while (($file = readdir($dp)) !== false) {
				if ($file != "." && $file != "..") {
					$abs_file = GEKKO_CACHEDIR.$file;
					if (@filectime($abs_file)+3600 < time()) {
						@unlink($abs_file);
					}
				}
			}
			closedir($dp);

			$GLOBALS["_cache"] = Array();
		}
		function Set($variable, $value) {
			$GLOBALS["_cache"][$variable] = $value;
		}
		function Get($variable) {
			return isset($GLOBALS["_cache"][$variable]) ? $GLOBALS["_cache"][$variable] : false;
		}
		function Save($content) {
			$key = $this->cache_genkey($content);
			echo $key;
			$file = $this->cachedir.$key;
			echo $file;
			$fh = fopen($file, "w");
			fwrite($content, $fh);
			fclose($fh);
			return $key;
		}
	}

	// initializing and cleaning cache working directory
	function cache_init() {
		if (!file_exists(GEKKO_CACHEDIR)) {
			@mkdir(GEKKO_CACHEDIR);
			@chmod(GEKKO_CACHEDIR, 0775);
		}
		$dp = opendir(GEKKO_CACHEDIR);
		while (($file = readdir($dp)) !== false) {
			if ($file != "." && $file != "..") {
				$abs_file = GEKKO_CACHEDIR.$file;
				if (filectime($abs_file)+GEKKO_CACHELIFE < time()) {
					unlink($abs_file);
				}
			}
		}
		closedir($dp);
	}

	// assigns a cache filename according to it's md5 hash and length
	function cache_assign_key(&$content, $append = null) {
		return md5($content).".".strlen($content).($append ? ".$append" : "");
	}

	// saves cache data to disk
	function cache_save($key, &$content) {
		if (!defined("IN-INSTALL"))
			if (($fh = @fopen(GEKKO_CACHEDIR.$key, "w")) !== false) {
				fwrite($fh, $content);
				fclose($fh);
			} else {
				trigger_error("Please verify that ".GEKKO_CACHEDIR." is writable.", E_USER_WARNING);
			}
	}

	// returns last modification time for a cache key
	function cache_check_life($key) {
		return file_exists(GEKKO_CACHEDIR.$key) ? filemtime(GEKKO_CACHEDIR.$key) : false;
	}

	// loads cache $key into $buff referenced variable
	function cache_read($key, &$buff) {
		$file = GEKKO_CACHEDIR.$key;
		if (file_exists($file)) {
			$fh = fopen($file, "r");
			if (($fs = filesize($file)) > 0) {
				$buff = fread($fh, $fs);
			}
			fclose($fh);
			return true;
		}
		return false;
	}

	// sets/gets a cached version for a repetitive almost-static function
	// such as database user ID query. Use with care ;)
	function cache_func($func) {
		$cache = "f.".md5($func);
		if (!isset($GLOBALS["cache"][$cache])) {
			eval('$return = '.str_replace('\'', '\\\'', $func).';');
			$GLOBALS["cache"][$cache] = $return;
		}
		return $GLOBALS["cache"][$cache];
	}
?>
