<?php
class HtmlAction extends Action{
    public function checkUser($level){
		if(empty($_SESSION['aid'])) $this->redirect('/Admin/Index/login');
		if(!empty($level) && !in_array($level,$_SESSION['levels'])) $this->error('您没有此权限!.');
	}
    public function index(){
		$this->checkUser('8_1_1');
		$config = file_get_contents('config/config.inc.php');
		if(preg_match('/\'ISHTML\'=>1/',$config)) $html = 1;
		else $html = 0;
		$this->assign('html',$html);
		$this->display();
	}
	public function html(){
		$this->checkUser('8_1_3');
		clearCache();
		$list['ishtml'] = $id = (int)$_GET['id'];
		if($id==0) $old = 1;
		else $old = 0;
		$config = file_get_contents('config/config.inc.php');
		$config = preg_replace('/\'ISHTML\'=>'.$old.'/','\'ISHTML\'=>'.$id,$config);
		if(file_put_contents('config/config.inc.php',$config)){
			$filename = C('ROOT').'/index.html';
			$url = C('URL').'/index.php';
			if($id==0){
				@unlink($filename);
			}else{
				$content = file_get_contents($url);
				file_put_contents($filename,$content);
			}
			$this->success('操作成功!');
		}else{
			$this->error('请检查您对config目录是否有写入权限!');
		}	
	}
	public function clearCache(){
		$this->checkUser('8_1_2');
		clearCache();
		$this->success('清空缓存成功!^_^!');
	}
	public function makeIndex(){
		$this->checkUser('8_1_4');
		$this->display();
	}
	public function checkIndex(){
		if(empty($_POST['submit'])) exit;
		$this->checkUser('8_1_5');
		$filename = C('ROOT').'/index.html';
		$url = C('URL').'/index.php';
		$content = file_get_contents($url);
		file_put_contents($filename,$content);
		$this->redirect("/Admin/Html/chenggong");
	}
	public function nlist(){
		$this->checkUser('8_2_1');
		$data = M('Nav');
		$list = $data->where('type=1')->field("id,name,pid,path,concat(path,'-',id) as bpath")->order('bpath')->select();
		foreach($list as $key=>$value){
			$list[$key]['count']=count(explode('-',$value['bpath']));
		}
		$this->assign('list',$list);
		$this->display();
	}
	public function checkList(){
		if(empty($_POST['submit'])) exit;
		$this->checkUser('8_2_2');
		$type = (int)$_POST['type'];
		$id = (int)$_POST['pid'];
		$zl = (int)$_POST['zl'];
		$data = M('Nav');
		if($id==0){
			$list = $data->where("type = $type")->field('id,dir,listname')->select();
		}else if($zl == 1){
			$list = $data->where("id = $id")->field('id,dir,listname')->select();
		}else{
			$list = $data->where("concat('-',path,'-') like '%-{$id}-%' OR id={$id}")->field('id,dir,listname')->select();
		}
		$newsdata = M('News');
		if(function_exists('curl_init')){
			// 初始化一个 cURL 对象
			$ch = curl_init();
			foreach($list as $key=>$value){
				$pid = $value['id'];
				$clist = $data->where("concat('-',path,'-') like '%-{$pid}-%' OR id={$pid}")->field('id,dir,listname')->select();
				$pid = array();
				foreach($clist as $k=>$v){
					$pid[] = $v['id'];
				}
				$pid = implode(',',$pid);
				$count = $newsdata->where("pid in ($pid)")->count('id');
				$page = ceil($count/C('listnews'));
				if($type==1) $url = C('URL').'/index.php/Home/Index/nlist/id/'.$pid;
				else $url = C('URL').'/index.php/Home/Index/plist/id/'.$pid;
				// 设置你需要抓取的URL
				curl_setopt($ch, CURLOPT_URL, $url);
				// 设置header
				curl_setopt($ch, CURLOPT_HEADER, 0);
				// 设置cURL 参数，要求结果保存到字符串中还是输出到屏幕上。
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				// 运行cURL，请求网页
				$content = curl_exec($ch);
				if(!is_dir(C('ROOT').'/'.$value['dir'])) mkdir(C('ROOT').'/'.$value['dir'],0700,true);
				file_put_contents(C('ROOT').'/'.$value['dir'].'/'.'index.html',$content);
				if($page>0){
					$name = $value['listname'];
					$string = str_replace('{lid}',$pid,$name);
					for($i=1;$i<=$page;$i++){
						$url.= '/p/'.$i;
						curl_setopt($ch, CURLOPT_URL, $url);
						curl_setopt($ch, CURLOPT_HEADER, 0);
						curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
						$content = curl_exec($ch);
						$string = str_replace('{page}',$i,$name);
						file_put_contents(C('ROOT').'/'.$value['dir'].'/'.$string,$content);
					}
				}
			}
			curl_close($ch);
		}else{
			foreach($list as $key=>$value){
				$pid = $value['id'];
				$count = $newsdata->where("pid = $pid")->count('id');
				$page = ceil($count/C('listnews'));
				if($type==1) $url = C('URL').'/index.php/Home/Index/nlist/id/'.$pid;
				else $url = C('URL').'/index.php/Home/Index/plist/id/'.$pid;
				$content = file_get_contents($url);
				if(!is_dir(C('ROOT').'/'.$value['dir'])) mkdir(C('ROOT').'/'.$value['dir'],0700,true);
				file_put_contents(C('ROOT').'/'.$value['dir'].'/'.'index.html',$content);
				if($page>0){
					$name = $value['listname'];
					$string = str_replace('{lid}',$pid,$name);
					for($i=1;$i<=$page;$i++){
						$url.= '/p/'.$i;
						$content = file_get_contents($url);
						$string = str_replace('{page}',$i,$name);
						file_put_contents(C('ROOT').'/'.$value['dir'].'/'.$string,$content);
					}
				}
			}
		}
		$this->redirect("/Admin/Html/chenggong");
	}
	public function news(){
		$this->checkUser('8_3_1');
		$data = M('Nav');
		$list = $data->where('type=1')->field("id,name,pid,path,concat(path,'-',id) as bpath")->order('bpath')->select();
		foreach($list as $key=>$value){
			$list[$key]['count']=count(explode('-',$value['bpath']));
		}
		$this->assign('list',$list);
		$this->display();
	}
	public function checkNews(){
		if(empty($_POST['submit'])) exit;
		$this->checkUser('8_3_2');
		$pid = (int)$_POST['pid'];
		$time = $_POST['time'];
		$startid = (int)$_POST['startid'];
		$stopid = (int)$_POST['stopid'];
		$pagesize = (int)$_POST['pagesize'];
		$now = $_SERVER['REQUEST_TIME'];
		if($time != -1){
			$time = $now-$time*86400;
			$where = "time >= $time";
		}else{
			if(empty($startid)){
				if(empty($stopid))  $where = '1=1';
				else $where = "id <= $stopid";
			}else{
				if(empty($stopid)) $where = "id >= $startid";
				else $where = "id >= $startid AND id <= $stopid";
			}
		}
		$save['ismake'] = 0;
		$nav  =  M('Nav');
		$data = M('News');
		$list = $nav->where("concat('-',path,'-') like '%-{$pid}-%' AND type=1 OR id={$pid} ")->field('id,name')->select();
		foreach($list as $key=>$value){
			$id = $value['id'];
			$wheres = $where." AND pid = $id";
			$data->where($wheres)->save($save);
			$this->newsHtml($id,$wheres,$pagesize);
			echo '<div style="padding-left:50px;">【<font color="red">'.$value['name'].'</font>】分类下的新闻已生成完了HTML！</div>';
		}
		$this->redirect("/Admin/Html/chenggong");
	}
	public function newsHtml($id,$where,$pagesize){
		$this->checkUser('8_3_2');
		if(!empty($_GET['id'])) $id = $_GET['id'];
		if(!empty($_GET['where'])) $where = $_GET['where'];
		if(!empty($_GET['pagesize'])) $pagesize = $_GET['pagesize'];
		$data = M('News');
		$navdata  = M('Nav');
		$nav = $navdata->field('id,dir,listname,conname')->where("id = $id")->find();
		$wheres = $where.' AND ismake=0';
		$list = $data->field('id,time')->where($wheres)->select();
		$count = count($list);
		if(!is_dir(C('ROOT').'/'.$nav['dir'])) mkdir(C('ROOT').'/'.$nav['dir'],0700,true);
		if(!empty($list)){
			$i = 1;
			if(function_exists('curl_init')){
				// 初始化一个 cURL 对象
				$ch = curl_init();
				foreach($list as $key=>$value){
					if($i>$count){
						break;
					}elseif($i>$pagesize){
						$this->newsHtml($id,$where,$pagesize);
					}else{
						$nid = $value['id'];
						$url = C('URL').'/index.php/Home/Index/news/id/'.$nid;
						curl_setopt($ch, CURLOPT_URL, $url);
						curl_setopt($ch, CURLOPT_HEADER, 0);
						curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
						$content = curl_exec($ch);
						$name = $nav['conname'];
						$string = str_replace('{id}',$nid,$name);
						$string = str_replace('{Y}',date('Y',$value['time']),$string);
						$string = C('ROOT').'/'.$nav['dir'].'/'.$string;
						file_put_contents($string,$content);
						$ismake['id'] = $nid;
						$ismake['ismake'] = '1';
						$data->save($ismake);
						echo '<div style="padding-left:50px;">&nbsp;&nbsp;ID为 <font color=blue>'.$nid.' </font>的新闻,已生成完毕!</div>';
						$i++;
					}
				}
				curl_close($ch);
			}else{
				foreach($list as $key=>$value){
					if($i>$count){
						break;
					}elseif($i>$pagesize){
						$this->newsHtml($id,$where,$pagesize);
					}else{
						$nid = $value['id'];
						$url = C('URL').'/index.php/Home/Index/news/id/'.$nid;
						$content = file_get_contents($url);
						$name = $nav['conname'];
						$string = str_replace('{id}',$nid,$name);
						$string = str_replace('{Y}',date('Y',$value['time']),$string);
						$string = C('ROOT').'/'.$nav['dir'].'/'.$string;
						file_put_contents($string,$content);
						$ismake['id'] = $nid;
						$ismake['ismake'] = '1';
						$data->save($ismake);					
						echo '<div style="padding-left:50px;">&nbsp;&nbsp;ID为 <font color="blue">'.$nid.' </font>的新闻,已生成完毕!</div>';
						$i++;
					}
				}
			}
		}
	}
	public function page(){
		$this->checkUser('8_6_1');
		$data = M('Page');
		$list = $data->field('id,name')->select();
		$this->assign('list',$list);
		$this->display();
	}
	public function checkPage(){
		$this->checkUser('8_6_1');
		$data = M('Page');
		$id = (int)$_POST['id'];
		if($id==0){
			$list = $data->field('id,pagename')->select();
			foreach($list as $key=>$value){
				$nid = $value['id'];
				$url = C('URL').'/index.php/Home/Index/page/id/'.$nid;
				$content = file_get_contents($url);
				$file = C('ROOT').'/page/'.$value['pagename'];
				if(!is_dir(C('ROOT').'/page')) mkdir(C('ROOT').'/page',0700,true);
				file_put_contents($file,$content);
			}
		}else{
			$list = $data->field('id,pagename')->where("id = $id")->find();
			$nid = $list['id'];
			$url = C('URL').'/index.php/Home/Index/page/id/'.$nid;
			$content = file_get_contents($url);
			$file = C('ROOT').'/page/'.$list['pagename'];
			if(!is_dir(C('ROOT').'/page')) mkdir(C('ROOT').'/page',0700,true);
			file_put_contents($file,$content);
		}
		$this->success('已成功!^_^');
		
	}
	public function chenggong(){
		echo '<div style="margin:20px 0 0 50px;padding:10px;border:1px solid #ccc;width:200px;text-align:center;">';
		echo '已全部完成!^_^';
		echo '</div>';
	}
}
?>