<?php
$module["datacache"]["name"]="Datacache Module";
$module["datacache"]["ver"]="0.1.0";
/**
 * Project:   astat - simple site engine
 * File:        /inc/datacache.class.php
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @link http://www.astat.org SVN: $URL$
 * @copyright 2010 becast.at
 * @author Bernhard Jaud <bernhard at becast dot at>
 * @package astat core
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version $Id: 0d1ea7fd993472fd4931ac5d71e72207ff77d05f $
*/
class datacache {
	
	var $data = array();
	var $expires = array();

	function __construct(){
		global $config, $db;
		$res=$db->query('SELECT `cache`,`content`,`expire` FROM `' . $config['prefix'] . 'datacache`');
		while($row=$db->fetch_object($res)){
			$this->data[$row->cache]=@unserialize(base64_decode($row->content));
			$this->expires[$row->cache]=$row->expire;
		}
	}

	//Fetch userdata
	function read($cache){
		global $config, $db;
		if(isset($this->data[$cache]) && $this->data[$cache]){
			return $this->data[$cache];
		}else{
			$res=$db->query('SELECT `cache`,`content` FROM `' . $config['prefix'] . 'datacache` WHERE `cache`=\''.$db->escape($cache).'\' LIMIT 1') or die($db->error());
			$row=$db->fetch_row($res);
			if(!$row[0]){
				$data=false;
			}else{
				$data = @unserialize(base64_decode($row[1]));
			}
			return $data;
		}
		
		
	}

	function set($name,$data,$expires=0){
	 global $db,$config;
		$data=base64_encode(serialize($data));
		$test=$this->read($name);
		if($test){
			$res=$db->query('UPDATE `' . $config['prefix'] . 'datacache` SET `content`=\''.$data.'\',`expire`=\''.$expires.'\' WHERE `cache`=\''.$db->escape($name).'\'');
			if($res){
				return true;
			}
		}else{
			$res=$db->query('INSERT INTO `' . $config['prefix'] . 'datacache` (`cache`,`content`,`expire`) VALUES (\''.$db->escape($name).'\',\''.$data.'\',\''.$expires.'\')');
			if($res){
				return true;
			}
		}
		return false;
	}

	function is_expired($name){
		global $db,$config;
		if(isset($this->expires[$name])){
			if($this->expires[$name]<time() && $this->expires[$name]!=0){
				return true;
			}else{
				return false;
			}
		}
		$res=$db->query('SELECT `cache` FROM `' . $config['prefix'] . 'datacache` WHERE `cache`=\''.$db->escape($name).'\' AND `expire`< \''.time().'\' AND `expire`<>\'0\' LIMIT 1');
		$row=$db->fetch_row($res);
		if($row[0]){
			return true;
		}
		return false;
	}
	
	function update_sidebars(){
		global $db,$config;
		
		$res = $db->query('SELECT `name`, `content`, `file` FROM `' . $config['prefix'] . 'navigation` WHERE `side`=\'l\' ORDER BY `sort`');
		while ($row=$db->fetch_row($res)){
			$nav['l'][]=array('name'=>$row[0],'content'=>$row[1],'file'=>$row[2]);
		}
		
		$res = $db->query('SELECT `name`, `content`, `file` FROM `' . $config['prefix'] . 'navigation` WHERE `side`=\'r\' ORDER BY `sort`');
		while ($row=$db->fetch_row($res)){
			$nav['r'][]=array('name'=>$row[0],'content'=>$row[1],'file'=>$row[2]);
		}
		$this->set('sidebar',$nav);
	}
	
}
?>
