<?php
/********************************************************************
 *                              DirCMS缓存类
 *                            -------------------
 *   begin                : 2010-4-12
 *	 update               : 2010-12-8 去除更新标签缓存操作
 *   copyright            : (C) 2009 DirCMS
 *   email                : uuturn@126.com
 *
 *   $Id: cache.class.php,v 1.0.0 end--2010-4-20 M Exp $
 ********************************************************************/

class cache
{
	public $db;

	function __construct()
	{
		@set_time_limit(0);
		global $db;
		$this->db=$db;
		$expiretime=time()-CACHE_COUNT_TTL;
		if($db)$this->db->query('DELETE FROM `'.DB_PRE.'counts` WHERE `'.DB_PRE.'counts`.`updatetime`<'.$expiretime,true);
	}

	function cache_all()
	{			
		$this->cache_model();
		$this->cache_author();
		$this->cache_area();
		$this->cache_member_type();
		$this->cache_copyfrom();
		$this->cache_posid();
		$this->cache_category();
		$this->cache_membergroup();
		$this->cache_membergrade();
		$this->cache_memberhonor();
		$this->cache_role();
		$this->cache_adminrole();
		$this->cache_link();
		$this->cache_adspos();
		$this->cache_adsfile();
		$this->cache_cargomethod();
		$this->cache_paymethod();
		$this->cache_pointcard();
		$this->cache_cardid();
		$this->cache_plus();
		$this->cache_template();
		clear_tag_cache();
		return true;
	}

	function cache_adsfile()
	{
		$DIRCMS['t']='Dircms';
		$r=cache_read("adspos.cache.php",DIRCMS_ROOT.'data/cache/'); ///////// 所有广告位
		if($r)foreach($r as $_r)
		{
			unset($contents); ////////// 不释放的话会出错 2010-6-24
			$__r=cache_read("ads_pos".$_r['id'].".cache.php",DIRCMS_ROOT.'data/cache/');
			$___r=cache_read("adspos".$_r['id'].".cache.php",DIRCMS_ROOT.'data/cache/');

			if($___r['option'])  ////////////////全部列出
			{
				foreach($__r as $_v)
				{
					if($_v['type']==1) ///图片广告
					{
						$contents[]='<a href="'.$DIRCMS['site_url'].'ads.php?id='.$_v['id'].'" title="'.$_v['alt'].'" target="_blank"><img src="'.$DIRCMS['site_url'].$_v['imageurl'].'" width="'.$___r['width'].'" height="'.$___r['height'].'" alt="'.$_v['alt'].'"/></a>';
					}
					if($_v['type']==2) ///Flash广告
					{
						$contents[]='<OBJECT width="'.$___r['width'].'" height="'.$___r['height'].'">
<PARAM NAME=movie value="'.$DIRCMS['site_url'].$_v['flashurl'].'">
<PARAM NAME=quality value=high>
<PARAM NAME=wmode value='.$_v['wmode'].'>
<EMBED width="'.$___r['width'].'" height="'.$___r['height'].'" src="'.$DIRCMS['site_url'].$_v['flashurl'].'" quality=high wmode=transparent TYPE="application/x-shockwave-flash"></EMBED>
</OBJECT>';
					}
					if($_v['type']==3) ///文本广告
					{
						$contents[]=$_v['text'];
					}
					if($_v['type']==4) ///文字链广告
					{
						$contents[]='<a href="'.$DIRCMS['site_url'].'ads.php?id='.$_v['id'].'" title="'.$_v['code'].'" target="_blank">'.$_v['code'].'</a>';
					}
					if($_v['type']==5) ///代码广告
					{
						$contents[]=stripslashes(stripslashes(base64_decode($_v['code'])));
					}
				}	
			}
			else ///////////////// 随机读取一个
			{
				$rand=get_rand_array_value($__r);
				if($rand['type']==1) ///图片广告
				{
					$contents[]='<a href="'.$DIRCMS['site_url'].'ads.php?id='.$rand['id'].'" title="'.$rand['alt'].'" target="_blank"><img src="'.$DIRCMS['site_url'].$rand['imageurl'].'" width="'.$___r['width'].'" height="'.$___r['height'].'"  alt="'.$rand['alt'].'"/></a>';
				}
				if($rand['type']==2) ///Flash广告
				{
					$contents[]='<OBJECT width="'.$___r['width'].'" height="'.$___r['height'].'">
<PARAM NAME=movie value="'.$DIRCMS['site_url'].$rand['flashurl'].'">
<PARAM NAME=quality value=high>
<PARAM NAME=wmode value='.$rand['wmode'].'>
<EMBED width="'.$___r['width'].'" height="'.$___r['height'].'" src="'.$DIRCMS['site_url'].$rand['flashurl'].'" quality=high wmode=transparent TYPE="application/x-shockwave-flash"></EMBED>
</OBJECT>';
				}
				if($rand['type']==3) ///文本广告
				{
					$contents[]=$rand['text'];
				}
				if($rand['type']==4) ///文字链广告
				{
					$contents[]='<a href="'.$DIRCMS['site_url'].'ads.php?id='.$rand['id'].'" title="'.$rand['code'].'" target="_blank">'.$rand['code'].'</a>';
				}
				if($rand['type']==5) ///代码广告
				{
					$contents[]=stripslashes(stripslashes(base64_decode($rand['code'])));
				}
			}

			ob_start();
			include template($___r['template']);
			create_html(DIRCMS_ROOT.'data/ads/'.$_r['id'].'.html');
		}
	}

	function cache_link()
	{
		cache_delete("cache_link.cache.php",DIRCMS_ROOT.'data/cache/');
		$r=$this->db->fetch_all("SELECT * FROM `".DB_PRE."link` WHERE ispassed=1 ORDER BY ".DB_PRE."link.orderby ASC");
		cache_write("cache_link.cache.php",$r,DIRCMS_ROOT.'data/cache/');
	}

	function cache_plus()
	{
		cache_delete("cache_plus.cache.php",DIRCMS_ROOT.'data/cache/');
		$r=$this->db->fetch_all("SELECT * FROM `".DB_PRE."plus` ORDER BY ".DB_PRE."plus.id DESC");
		cache_write("cache_plus.cache.php",$r,DIRCMS_ROOT.'data/cache/');
	}

	function cache_pointcard()
	{
		cache_delete("cache_pointcard.cache.php",DIRCMS_ROOT.'data/cache_pay/');
		$r=$this->db->fetch_all("SELECT * FROM `".DB_PRE."pointcard` ORDER BY `".DB_PRE."pointcard`.`orderby` ASC");
		cache_write("cache_pointcard.cache.php",$r,DIRCMS_ROOT.'data/cache_pay/');
		if($r)foreach($r as $_r)
		{
			cache_write("cache_pointcard".$_r['id'].".cache.php",$_r,DIRCMS_ROOT.'data/cache_pay/');
		}
	}
	function cache_cardid()
	{
		cache_delete("cache_cardid.cache.php",DIRCMS_ROOT.'data/cache_pay/');
		$r=$this->db->fetch_all("SELECT id,cardid,userid,username,endtime,point,amount FROM `".DB_PRE."card` WHERE `".DB_PRE."card`.`status`=1 ORDER BY `".DB_PRE."card`.`id` DESC");
		cache_write("cache_cardid.cache.php",$r,DIRCMS_ROOT.'data/cache_pay/');
	}
	function cache_cargomethod()
	{
		cache_delete("cache_cargomethod.cache.php",DIRCMS_ROOT.'data/cache/');
		$r=$this->db->fetch_all("SELECT * FROM `".DB_PRE."cargo_method` ORDER BY `".DB_PRE."cargo_method`.`orderby` ASC");
		cache_write("cache_cargomethod.cache.php",$r,DIRCMS_ROOT.'data/cache/');
		if($r)foreach($r as $_r)
		{
			cache_write("cache_cargomethod".$_r['id'].".cache.php",$_r,DIRCMS_ROOT.'data/cache/');
		}
	}

	function cache_paymethod()
	{
		cache_delete("cache_paymethod.cache.php",DIRCMS_ROOT.'data/cache/');
		$r=$this->db->fetch_all("SELECT * FROM `".DB_PRE."pay_method` WHERE enabled=1 ORDER BY `".DB_PRE."pay_method`.`orderby` ASC");
		cache_write("cache_paymethod.cache.php",$r,DIRCMS_ROOT.'data/cache/');
		if($r)foreach($r as $_r)
		{
			cache_write("cache_paymethod".$_r['id'].".cache.php",$_r,DIRCMS_ROOT.'data/cache/');
		}
	}

	function cache_adspos()
	{
		cache_delete("adspos.cache.php",DIRCMS_ROOT.'data/cache/');
		$r=$this->db->fetch_all("SELECT * FROM `".DB_PRE."adspos` WHERE ispassed=1 ORDER BY ".DB_PRE."adspos.id DESC");
		cache_write("adspos.cache.php",$r,DIRCMS_ROOT.'data/cache/');
		if($r)foreach($r as $_r)
		{
			cache_delete("adspos".$_r['id'].".cache.php",DIRCMS_ROOT.'data/cache/');
			$__r=$this->db->fetch_one("SELECT * FROM `".DB_PRE."adspos` WHERE ispassed=1 AND id=".intval($_r['id']));
			cache_write("adspos".$_r['id'].".cache.php",$__r,DIRCMS_ROOT.'data/cache/');
		}

		$this->cache_ads();
	}

	function cache_ads()
	{
		$r=cache_read("adspos.cache.php",DIRCMS_ROOT.'data/cache/'); ///////// 所有广告位
		if($r)foreach($r as $_r)
		{
			cache_delete("ads_pos".$_r['id'].".cache.php",DIRCMS_ROOT.'data/cache/');
			$__r=$this->db->fetch_all("SELECT * FROM `".DB_PRE."ads` WHERE ispassed=1 AND adsposid=".intval($_r['id']));		
			cache_write("ads_pos".$_r['id'].".cache.php",$__r,DIRCMS_ROOT.'data/cache/');
		}
	}

	function cache_adminrole()
	{
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."admin");
		if($r)foreach($r as $_r)
		{
			cache_delete('admin'.$_r['memberid'].'_role.inc.php',DIRCMS_ROOT.'data/cache_admin/');
			cache_write('admin'.$_r['memberid'].'_role.inc.php',explode(',',$_r['roleid']),DIRCMS_ROOT.'data/cache_admin/');
		}
		$r=$this->db->fetch_all("SELECT `".DB_PRE."admin`.`memberid`,`".DB_PRE."admin`.`categorypriv` FROM `".DB_PRE."admin`");
		foreach($r as $_r)
		{
			$_categorypriv=$_r['categorypriv']?explode(',',$_r['categorypriv']):'';
			cache_delete('admin'.$_r['memberid'].'_categorypriv.inc.php',DIRCMS_ROOT.'data/cache_admin/');
			cache_write('admin'.$_r['memberid'].'_categorypriv.inc.php',$_categorypriv,DIRCMS_ROOT.'data/cache_admin/');
		}
	}

	function cache_model()
	{
		cache_delete("cache_model.cache.php",DIRCMS_ROOT.'data/cache_model/');
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."model WHERE isinstall=1 ORDER BY ".DB_PRE."model.isinstall DESC");
		cache_write("cache_model.cache.php",$r,DIRCMS_ROOT.'data/cache_model/');
		if($r)foreach($r as $v)
		{
			cache_write("cache_model".$v['id'].".cache.php",$v,DIRCMS_ROOT.'data/cache_model/');
			$this->cache_fields($v['id']);
			$this->cache_type($v['id']);
		}
		cache_delete("cache_membermodel.cache.php",DIRCMS_ROOT.'data/cache_model/');
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."membermodel WHERE isinstall=1 ORDER BY ".DB_PRE."membermodel.isinstall DESC");
		cache_write("cache_membermodel.cache.php",$r,DIRCMS_ROOT.'data/cache_model/');
		if($r)foreach($r as $v)
		{
			cache_write("cache_membermodel".$v['id'].".cache.php",$v,DIRCMS_ROOT.'data/cache_model/');
			$this->cache_fields($v['id']);
			$this->cache_type($v['id']);
		}
	}

	function cache_fields($modelid)
	{

		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."db_fields WHERE ".DB_PRE."db_fields.modelid=".intval($modelid)." AND ".DB_PRE."db_fields.disabled=0 ORDER BY ".DB_PRE."db_fields.orderby ASC");
		cache_delete("cache_model".$modelid.".fields.cache.php",DIRCMS_ROOT.'data/cache_model/');
		cache_write("cache_model".$modelid.".fields.cache.php",$r,DIRCMS_ROOT.'data/cache_model/');
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."memberdb_fields WHERE ".DB_PRE."memberdb_fields.modelid=".intval($modelid)." AND ".DB_PRE."memberdb_fields.disabled=0 ORDER BY ".DB_PRE."memberdb_fields.orderby ASC");
		cache_delete("cache_model_m".$modelid.".fields.cache.php",DIRCMS_ROOT.'data/cache_model/');
		cache_write("cache_model_m".$modelid.".fields.cache.php",$r,DIRCMS_ROOT.'data/cache_model/');
	}

	function cache_type($modelid)
	{
		cache_delete('type'.$modelid.'.cache.php',DIRCMS_ROOT.'data/cache/');
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."type WHERE islocked=0 AND ".DB_PRE."type.modelid=".intval($modelid));
		cache_write('type'.$modelid.'.cache.php',$r,DIRCMS_ROOT.'data/cache/');

		cache_delete('type.cache.php',DIRCMS_ROOT.'data/cache/');
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."type WHERE islocked=0 ORDER BY ".DB_PRE."type.orderby ASC");
		cache_write('type.cache.php',$r,DIRCMS_ROOT.'data/cache/');
	}

	function cache_author()
	{
		cache_delete('author.cache.php',DIRCMS_ROOT.'data/cache/');
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."author WHERE ".DB_PRE."author.islocked=0 ORDER BY ".DB_PRE."author.orderby ASC,id DESC");
		cache_write('author.cache.php',$r,DIRCMS_ROOT.'data/cache/');
	}

	function cache_area()
	{
		require_once(DIRCMS_ROOT.'include/select.class.php');
		$s=new select();
		$s->getSelect('info[parentid]','无（作为一级地区）',DB_PRE.'area',true,'area');
		$s->getSelect('info[areaid]','请选择地区',DB_PRE.'area',true,'area');

		$t_array=array();
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."area ORDER BY orderby ASC");
		{
			cache_delete('all_area.cache.php',DIRCMS_ROOT.'data/cache/');
			cache_write('all_area.cache.php',$r,DIRCMS_ROOT.'data/cache/');
			if($r)foreach($r as $_v)
			{
				if(!$this->db->fetch_one("SELECT * FROM ".DB_PRE."area WHERE parentid=".intval($_v['id'])." ORDER BY orderby ASC"))
				{
					$t_array[]=$_v;
				}
			}
			if($r)
			{
				cache_delete('all_area.cache.php',DIRCMS_ROOT.'data/cache/');
				cache_write('all_area.cache.php',$r,DIRCMS_ROOT.'data/cache/');
				foreach($r as $_r)
				{
					cache_delete('area'.$_r['id'].'.cache.php',DIRCMS_ROOT.'data/cache/');
					cache_write('area'.$_r['id'].'.cache.php',$_r,DIRCMS_ROOT.'data/cache/');
				}
			}
		}
		cache_delete('area.cache.php',DIRCMS_ROOT.'data/cache/');
		cache_write('area.cache.php',$t_array,DIRCMS_ROOT.'data/cache/');
	}

	function cache_member_type()
	{
		require_once(DIRCMS_ROOT.'include/select.class.php');
		$s=new select();
		$s->getSelect('info[parentid]','无（作为一级类型）',DB_PRE.'member_type',true,'type');
		$s=new select();
		$s->getSelect('info[typeid]','请选择开通类型',DB_PRE.'member_type',true,'type');
		$s=new select();
		$s->getSelect('config[typeid]','选择会员类型',DB_PRE.'member_type',true,'type');

		$t_array=array();
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."member_type ORDER BY orderby ASC");
		{
			if($r)foreach($r as $_v)
			{
				if(!$this->db->fetch_one("SELECT * FROM ".DB_PRE."member_type WHERE parentid=".intval($_v['id'])." ORDER BY orderby ASC"))
				{
					$t_array[]=$_v;
				}
				cache_delete('member_type'.$_v['id'].'.cache.php',DIRCMS_ROOT.'data/cache/');
				cache_write('member_type'.$_v['id'].'.cache.php',$_v,DIRCMS_ROOT.'data/cache/');
			}
			if($r)
			{
				cache_delete('all_member_type.cache.php',DIRCMS_ROOT.'data/cache/');
				cache_write('all_member_type.cache.php',$r,DIRCMS_ROOT.'data/cache/');
				foreach($r as $_r)
				{
					cache_delete('member_type'.$_r['id'].'.cache.php',DIRCMS_ROOT.'data/cache/');
					cache_write('member_type'.$_r['id'].'.cache.php',$_r,DIRCMS_ROOT.'data/cache/');
				}
			}
		}
		cache_delete('member_type.cache.php',DIRCMS_ROOT.'data/cache/');
		cache_write('member_type.cache.php',$t_array,DIRCMS_ROOT.'data/cache/');
	}

	function cache_copyfrom()
	{
		cache_delete('copyfrom.cache.php',DIRCMS_ROOT.'data/cache/');
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."copyfrom ORDER BY ".DB_PRE."copyfrom.orderby ASC LIMIT 0,15");
		cache_write('copyfrom.cache.php',$r,DIRCMS_ROOT.'data/cache/');
	}

	function cache_posid()
	{
		cache_delete('posid.cache.php',DIRCMS_ROOT.'data/cache/');
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."posid ORDER BY ".DB_PRE."posid.orderby ASC");
		cache_write('posid.cache.php',$r,DIRCMS_ROOT.'data/cache/');
	}

	function cache_role()
	{
		cache_delete('role.cache.php',DIRCMS_ROOT.'data/cache/');
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."role WHERE disabled=0 ORDER BY ".DB_PRE."role.orderby ASC");
		if($r)foreach($r as $_r)
		{
			cache_delete('role'.$_r['id'].'.cache.php',DIRCMS_ROOT.'data/cache/');
			cache_write('role'.$_r['id'].'.cache.php',$_r,DIRCMS_ROOT.'data/cache/');
		}
		cache_write('role.cache.php',$r,DIRCMS_ROOT.'data/cache/');
	}

	function cache_template()
	{
		global $DIRCMS,$info;
		if($info)
		{
			$r=cache_read('template.cache.php',TPL_ROOT.TPL_NAME.'/');
			if(!$r||!is_array($r))$r=array();
			foreach($r as $value)
			{
				$t[$value['template']]=$value;
			}
			$r=$t;
			if($info)$r[$info['template']]=$info;
			cache_write('template.cache.php',$r,TPL_ROOT.TPL_NAME.'/');
			cache_write('template.cache.php',$r,DIRCMS_ROOT.'data/cache/');
		}
	}

	function cache_template_delete()
	{
		global $DIRCMS,$id;
		$r=cache_read('template.cache.php',TPL_ROOT.TPL_NAME.'/');
		if($id)unset($r[$id]);
		cache_write('template.cache.php',$r,TPL_ROOT.TPL_NAME.'/');
		cache_write('template.cache.php',$r,DIRCMS_ROOT.'data/cache/');
	}

	function cache_category()  // 2010-11-27  优化
	{
		foreach (glob(DIRCMS_ROOT."data/cache_category/*.php") as $filename)
		{
			@unlink($filename);
		}
		$parent=$id=$catid=array();
		$finalcatid=array();
		$finalcategory=array();
		cache_delete('category.cache.php',DIRCMS_ROOT.'data/cache_category/');
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."category ORDER BY ".DB_PRE."category.orderby ASC");
		if($r)
		{
			cache_write('category.cache.php',$r,DIRCMS_ROOT.'data/cache_category/');
			foreach($r as $_r)
			{
				if(!$_r['parentid'])$parent[]=$_r;
				cache_write('category'.$_r['id'].'.cache.php',$_r,DIRCMS_ROOT.'data/cache_category/');
				$catid[]=$_r['id'];
				if(!$this->db->fetch_one("SELECT id FROM ".DB_PRE."category WHERE ".DB_PRE."category.parentid=".$_r['id']." AND ".DB_PRE."category.isshow=1 AND ".DB_PRE."category.ispage=0 ") && !$_r['ispage']&& $_r['isshow'])
				{
					$finalcatid[]=$_r['id'];
					$finalcategory[]=$_r;
				}
				$t=$this->db->fetch_all("SELECT * FROM ".DB_PRE."category WHERE ".DB_PRE."category.parentid=".$_r['id']." ORDER BY ".DB_PRE."category.orderby ASC");
				
				$id=array();
				if($t)cache_write('parent'.$_r['id'].'.cache.php',$t,DIRCMS_ROOT.'data/cache_category/');
				foreach($t as $_t)
				{
					$id[]=$_t['id'];
				}
				cache_write('parentid'.$_r['id'].'.cache.php',$id,DIRCMS_ROOT.'data/cache_category/');
			}
			cache_write('parent.cache.php',$parent,DIRCMS_ROOT.'data/cache_category/');
			cache_write('catid.cache.php',$catid,DIRCMS_ROOT.'data/cache_category/');
			cache_write('finalcategory.cache.php',$finalcategory,DIRCMS_ROOT.'data/cache_category/');
			cache_write('finalcatid.cache.php',$finalcatid,DIRCMS_ROOT.'data/cache_category/');
		}
	}

	function cache_membergroup()
	{
		cache_delete('membergroup.cache.php',DIRCMS_ROOT.'data/cache/');
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."membergroup WHERE ".DB_PRE."membergroup.isdisabled='0' ORDER BY ".DB_PRE."membergroup.orderby ASC");
		if(is_array($r))cache_write('membergroup.cache.php',$r,DIRCMS_ROOT.'data/cache/');
		if(is_array($r))foreach($r as $val)
		{
			cache_delete('membergroup'.$val['id'].'.cache.php',DIRCMS_ROOT.'data/cache/');
			cache_write('membergroup'.$val['id'].'.cache.php',$val,DIRCMS_ROOT.'data/cache/');
		}
	}

	function cache_membergrade()
	{
		cache_delete('membergrade.cache.php',DIRCMS_ROOT.'data/cache/');
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."membergrade WHERE ".DB_PRE."membergrade.status=1 ORDER BY ".DB_PRE."membergrade.grade ASC");
		if(is_array($r))cache_write('membergrade.cache.php',$r,DIRCMS_ROOT.'data/cache/');
		if(is_array($r))foreach($r as $val)
		{
			cache_delete('membergrade'.$val['grade'].'.cache.php',DIRCMS_ROOT.'data/cache/');
			cache_write('membergrade'.$val['grade'].'.cache.php',$val,DIRCMS_ROOT.'data/cache/');
		}
	}

	function cache_memberhonor()
	{
		cache_delete('cache_memberhonor.cache.php',DIRCMS_ROOT.'data/cache/');
		$r=$this->db->fetch_all("SELECT * FROM ".DB_PRE."memberhonor WHERE ".DB_PRE."memberhonor.status=1 ORDER BY ".DB_PRE."memberhonor.point ASC");
		if(is_array($r))cache_write('memberhonor.cache.php',$r,DIRCMS_ROOT.'data/cache/');
		if(is_array($r))foreach($r as $val)
		{
			cache_delete('memberhonor'.$val['id'].'.cache.php',DIRCMS_ROOT.'data/cache/');
			cache_write('memberhonor'.$val['id'].'.cache.php',$val,DIRCMS_ROOT.'data/cache/');
		}
	}
}
?>