<?php
/**
 * Page caching controls
 *
 * @name cache.php
 * @verision <version>
 * @package <package>
 * @author Rashaud Teague <rashaud.teague@gmail.com>
 * @since 08/14/2009
 * @license GNU GPL <http://www.gnu.org/licenses/>
 */


class Cache {
	public $cache_directory = 'cache/';
	private $cache_expire = 1;
	
	public function __construct() {  }
	
	public function create($page_title, $override = false) {
		global $pc;
		
		$page_title = str_replace(' ', '_', $page_title);
		$this->delete($page_title);
		$cache_filename = $page_title.'.html';
		
		$protocol = explode('/', $_SERVER['SERVER_PROTOCOL']);
		
		$lines = file(strtolower($protocol[0]).'://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']).'?p='.$page_title);
		$page_content = "";
		foreach ($lines as $line)
			$page_content .= $line;
		
		$this->update_last_update();
		$last_update = $this->get_last_update();
		
		if (!$override) {
			if (time() > (int)$last_update + (60 * $this->cache_expire)) {
				print (int)$last_update + (60 * $this->cache_expire).'<br />';
				die();
				$pfile = fopen($this->cache_directory.$cache_filename, "w");
				fwrite($pfile, '<!-- '.time().' -->'."\n");
				fwrite($pfile, $page_content);
				fclose($pfile);
			}
			return;
		}
		
		$pfile = fopen($this->cache_directory.$cache_filename, "w");
		fwrite($pfile, '<!-- '.time().' -->'."\n");
		fwrite($pfile, $page_content);
		fclose($pfile);
	}
	
	public function get($page_title) {
		$page_title = str_replace(' ', '_', $page_title);
		$cache_filename = $page_title.'.html';
		$lines = file($this->cache_directory.$cache_filename);
		foreach ($lines as $line) print $line;
	}
	
	public function check($page_title) {
		$page_title = str_replace(' ', '_', $page_title);
		$cache_filename = $page_title.'.html';
		return (file_exists($this->cache_directory.$cache_filename)) ? true : false;
	}
	
	public function delete($page_title) {
		$page_title = str_replace(' ', '_', $page_title);
		$cache_filename = $page_title.'.html';
		if (file_exists($this->cache_directory.$cache_filename)) unlink($this->cache_directory.$cache_filename);
	}
	
	public function configure() {
		$dir = opendir('.');
		
		$found = false;
		while ($file = readdir($dir)) {
			if (preg_match("/^".substr($this->cache_directory, 0, strlen($this->cache_directory)-1)."/i", $file)) {
				$found = true;
				break;
			}
		}
		
		if (!$found) {
			mkdir(substr($this->cache_directory, 0, strlen($this->cache_directory)-1));
			$pfile = fopen($this->cache_directory.'.config.txt', 'w');
			fwrite($pfile, 'LastClean='.time()."\n".'LastUpdate='.time());
			fclose($pfile);
		}
		
		if (!file_exists($this->cache_directory.'.config.txt')) {
			$pfile = fopen($this->cache_directory.'.config.txt', 'w');
			fwrite($pfile, 'LastClean='.time()."\n".'LastUpdate='.time());
			fclose($pfile);
			return;
		}
	}
	
	public function clean() {
		global $pc;
		if (!file_exists($this->cache_directory.'.config.txt')) {
			$pfile = fopen($this->cache_directory.'.config.txt', 'w');
			fwrite($pfile, 'LastClean='.time()."\n".'LastUpdate='.time());
			fclose($pfile);
			return;
		}
		
		$lines = file($this->cache_directory.'.config.txt');
		$line_split = explode('=', $lines[0]);
		if (time() > (int)$line_split[1] + (60 * 60)) {
			$dir = opendir(substr($this->cache_directory, 0, strlen($this->cache_directory)-1));
			while ($files = readdir($dir)) {
				if ($files == '.config.txt' || $files == '.' || $files == '..') continue;
				$fsplit = explode('.', $files);
				$page_name = '';
				for ($i = 0; $i < sizeof($fsplit)-1; $i++)
					$page_name .= $fsplit[$i];
				if (!$pc->page_exists($page_name))
					$this->delete($page_name);
			}
			closedir($dir);
		}
		$this->update_clean();
	}
	
	public function all($override = false) {
		global $db;
		
		$lines = file($this->cache_directory.'.config.txt');
		if (sizeof($lines) < 2) {
			$pfile = fopen($this->cache_directory.'.config.txt', 'w');
			fwrite($pfile, 'LastClean='.time()."\n".'LastUpdate='.time());
			fclose($pfile);
		}
		$line_split = explode('=', $lines[1]);
		
		$sql = "
		SELECT
			title
		FROM
			pages
		";
		
		if (!$override) {
			if (time() > (int)$line_split[1] + (60 * $this->cache_expire)) {
				$this->update_last_update();
				$db->exe_sql($sql);
		
				while ($row = $db->fetch_array()) {
					$this->create($row['title']);
				}
			}
			return;
		}
		
		$db->exe_sql($sql);
		while ($row = $db->fetch_array())
			$this->create($row['title']);
		
		$this->update_last_update();
	}
	
	public function update_clean() {
		$lines = file($this->cache_directory.'.config.txt');
		$lines[0] = 'LastClean='.time()."\n";
		$pfile = fopen($this->cache_directory.'.config.txt', 'w');
		foreach ($lines as $line) fwrite($pfile, $line);
		fclose($pfile);
	}
	
	public function update_last_update() {
		$lines = file($this->cache_directory.'.config.txt');
		$lines[1] = 'LastUpdate='.time()."\n";
		$pfile = fopen($this->cache_directory.'.config.txt', 'w');
		foreach ($lines as $line) fwrite($pfile, $line);
		fclose($pfile);
	}
	
	public function get_last_update() {
		$lines = file($this->cache_directory.'.config.txt');
		if (sizeof($lines) < 2) {
			$pfile = fopen($this->cache_directory.'.config.txt', 'w');
			fwrite($pfile, 'LastClean='.time()."\n".'LastUpdate='.time());
			fclose($pfile);
		}
		$line_split = explode('=', $lines[1]);
		return $line_split[1];
	}
	
	public function get_last_clean() {
		$lines = file($this->cache_directory.'.config.txt');
		if (sizeof($lines) < 2) {
			$pfile = fopen($this->cache_directory.'.config.txt', 'w');
			fwrite($pfile, 'LastClean='.time()."\n".'LastUpdate='.time());
			fclose($pfile);
		}
		$line_split = explode('=', $lines[0]);
		return $line_split[1];
	}
}
?>