<?php
/********************************************************************
 *                              DirCMS静态化类
 *                            -------------------
 *   begin                : 2010-5-16
 *	 update               : 2010-11-13 
 *   copyright            : (C) 2009 DirCMS
 *   website              : http://www.dircms.net
 *
 *   $Id: html.class.php, 2010/9/1 M Exp $
 ********************************************************************/

class html
{
	private $cache;

	function __construct()
	{	
		global $DIRCMS;
		@set_time_limit(0);
	}

	function cache()
	{
		require_once(DIRCMS_ROOT.'include/cache.class.php');
		$this->cache=new cache();
		return $this->cache->cache_all();
	}

	function index()
	{
		global $DIRCMS,$_username,$_userid,$_d;
		if(!ISHTML) return true;
		$head['title']=$DIRCMS['site_name'].'-'.$DIRCMS['meta_title'].$_powerby;
		$head['keywords']=$DIRCMS['meta_keywords'];
		$head['description']=$DIRCMS['meta_description'];
		$DIRCMS['t']=$head['title'].$_d;
		ob_start();
		include template('index');
		$file=DIRCMS_ROOT.'index.html';
		$re=create_html($file);
		return $re;
	}

	function category($catid=0,$page=0)
	{
		global $DIRCMS,$_username,$_userid,$_d,$db;
		if(!$catid) return true;
		global $DIRCMS;
		$catid_r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
		$finalcatid=cache_read('finalcatid.cache.php',DIRCMS_ROOT.'data/cache_category/');
		if(!$catid_r['ishtml'] && !$catid_r['ispage'])return true;
		unset($catid_r['description']);
		@extract($catid_r);
		$dir_catid=$catid=$id;

		$dir_search=$searchoption=searchoption($catid);
		$dir_postion=$position=get_pos($catid);
		$dir_url=$DIRCMS['site_url'].$catid_r['url'];
		$dir_catname=$catid_r['categoryname'];
		$head['title'] = $categoryname.'-'.($meta_title?$meta_title : $DIRCMS['site_name']).$_powerby;
		$head['keywords'] = $meta_keywords;
		$head['description'] = $meta_description;
		$DIRCMS['t']=$head['title'].$_d;
		if(!$catid_r['ispage']) // 非单页
		{
			$template=in_array($catid,$finalcatid)?$listtlp:$categorytlp;
			ob_start();
			include template($template);
			$file=$page?$catid_r['categorydir'].'/'.$page.'.html':$catid_r['categorydir'].'/index.html';
			
			if($page==1)
			{
				$indexfile=$catid_r['categorydir'].'/index.html';
				create_html($indexfile);
				include template($template);
			}
			return create_html($file);
		}
		else // 单页
		{
			$pagecontent=$db->fetch_one("SELECT * FROM `".DB_PRE."page` WHERE `".DB_PRE."page`.`catid`={$catid}");
			@extract($pagecontent);
			make_dir($pagecontent['dir']);
			ob_start();
			include template($pagecontent['template']);
			$content=ob_get_contents();
			ob_clean();
			return @file_put_contents(DIRCMS_ROOT.$pagecontent['dir'].'/'.$pagecontent['filename'].'.html',stripslashes($content));
		}
	}

	function content($id,$catid=0)
	{
		global $DIRCMS,$_username,$_userid,$db;
		require_once(DIRCMS_ROOT.'include/admin/content.class.php');
		$c=new content();
		if(!$id)showmsg('请先添加内容，再生成!');
		$data=$c->get($id,$catid);
		if(!$data) return false;
		$dir_contentid=$id;
		$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
		$dir_catid=$catid=$r['id'];
		$dir_catname=$catname=$r['categoryname'];
		$dir_search=$searchoption=searchoption($catid);
		@extract($r,EXTR_SKIP);
		@extract($data,EXTR_OVERWRITE);
		$dir_postion=$position=get_pos($catid);
		$dir_url=$DIRCMS['site_url'].$data['url'];
		ob_start();
		if($r['ishtml'] && !$groupid && !$point) //2010-10-28 特殊权限的话不能静态化
		{
			if($r['content_html']=='content_html_1') //栏目名/日期/ID.html
			{
				$dir=$r['categorydir'].'/'.date('YW',$inputtime).'/';
				@mkdir(DIRCMS_ROOT.$dir);
			}
			else if($r['content_html']=='content_html_2') //HTML/日期/ID.html
			{
				$dir='html/'.date('YW',$inputtime).'/';
				@mkdir(DIRCMS_ROOT.$dir);
			}
			else
			{
				if($r['content_html']=='content_html_3') //日期/内容/ID.html
				{
					$dir=date('Y',$inputtime).'/'.date('m',$inputtime).'/';
					@mkdir(DIRCMS_ROOT.date('Y',$inputtime));
					@mkdir(DIRCMS_ROOT.$dir);
				}
			}

			make_dir($dir);
			$content=$content?$content:''; 
			$c=preg_split('/<div style=\"page-break-after: always\"><span style=\"display: none\">&nbsp;<\/span><\/div>/i',$content);
			$pages=count($c)>1?count($c):0;
			if($pages)
			{
				for($i=1;$i<=$pages;$i++)
				{
					$tp=$i==1?$dir.$id.'.html':$dir.$id.'_'.$i.'.html';
					$pagestring.=' <a href="'.$tp.'">'.($i==1?'1':($i==$pages?'2':$i)).'</a> ';
				}
				$dir_page=$pagestring;
				for($pagenumi=1;$pagenumi<=$pages;$pagenumi++)
				{
					ob_start();
					$t=$pagenumi-1;
					$content=$c[$t];
					include template($template);
					$file=$pagenumi==1?DIRCMS_ROOT.$dir.$id.'.html':DIRCMS_ROOT.$dir.$id.'_'.$pagenumi.'.html';
					create_html($file);
				}
			}
			else{
				include template($template);
				$file=DIRCMS_ROOT.$dir.$id.'.html';
				create_html($file);
			}
		}	
		return true;
	}
}
?>