<?php
!defined('FRAMEWORK_PATH') && exit('FRAMEWORK_PATH not defined.');

include POST_PATH.'core/control/common_control.class.php';

class test_control extends common_control {
	
	function __construct(&$conf) {
		parent::__construct($conf);
		$this->_checked = array('post'=>' class="checked"');
	}
	public function on_topic_subject(){
		$page=misc::page();
		$pagesize=2000;
		$start=($page-1)*$pagesize;
		$list=$this->topic->index_fetch(array(),array('tid'=>1),$start,$pagesize);
		if(empty($list)) $this->message('OK');
		foreach($list as $v){
		   $t=misc::safe_str($v['subject']);
           $arr=array(
			   'tid'=>$v['tid'],
			   'fid'=>$v['fid'],
			   'subject'=>$t,
			   'subject_md5'=>md5($t),
			   'dateline'=>$v['dateline']
		   );
		   $this->topic_subject->_create($arr);
		}
		$page++;
		$this->message('next:',1,'?test-topic_subject-page-'.$page.'.htm');
		
	}
	public function on_rp(){
		
	    $page=misc::page();
		$pagesize=2000;
		$start=($page-1)*$pagesize;
		
		$list=$this->topic->index_fetch(array(),array('tid'=>1),$start,$pagesize);	
		!$list && exit('ok');
		$arr=array(1=>'一夜情',2=>'E夜情',3=>'约会',4=>'交友',5=>'一夜晴');
		foreach($list as $v){
		   $n=rand(1,5);
		   //$v['subject']=str_replace('一夜情贴吧','交友贴吧',$v['subject']);
		   //$v['message']=str_replace('一夜情吧','交友吧',$v['message']);
		   //$v['subject']=str_replace('一夜情',$arr[$n],$v['subject']);
		   //$v['subject']=str_replace(array(4,5,6,7,8,9,'三','五','六','七','九','口交','性交','做爱'),'*',$v['message']);
		   $v['dateline']+=17280000;
		   $v['lasttime']+=17280000;
		   $this->topic->update($v);
		}
		$this->message($start,2,"?test-rp-page-".($page+1).'.htm');
		echo 'ok';
	}
	public function on_rppost(){
		
	    $page=misc::page();
		$pagesize=2000;
		$start=($page-1)*$pagesize;
		
		$list=$this->post->index_fetch(array(),array('pid'=>1),$start,$pagesize);	
		!$list && exit('ok');
		$arr=array(1=>'一夜情',2=>'E夜情',3=>'约会',4=>'交友',5=>'一夜晴');
		foreach($list as $v){
		   $n=rand(1,5);
		   //$v['subject']=str_replace('一夜情贴吧','交友贴吧',$v['subject']);
		   //$v['message']=str_replace('一夜情吧','交友吧',$v['message']);
		   //$v['message']=str_replace('一夜情',$arr[$n],$v['message']);
		   //$v['subject']=str_replace(array(4,5,6,7,8,9,'三','五','六','七','九','口交','性交','做爱'),'*',$v['message']);
		   $v['dateline']+=17280000;
		   $this->post->update($v);
		}
		$this->message($start,2,"?test-rppost-page-".($page+1).'.htm');
		echo 'ok';
	}
	public function on_rpuser(){
	    $page=misc::page();
		$pagesize=2000;
		$start=($page-1)*$pagesize;
		
		$list=$this->member->index_fetch(array(),array('uid'=>1),$start,$pagesize);	
		!$list && exit('ok');
		$arr=array(1=>'y',2=>'s',3=>'t');
		foreach($list as $v){
		   $n=rand(1,3);
		   $u=$v['username'];
		   $v['username']=str_replace(array(4,5,6,7,8,9,'三','五','六','七','九','口交','性交','做爱'),array('s','w','l','q','b','j','x','c','v','m','n','k','t','u'),$v['username']);
		   $v['username']!=$u && $v['username'].=rand(1000,9999);
		   $this->member->update($v);
		}
		$this->message($start,2,"?test-rpuser-page-".($page+1).'.htm');
		echo 'ok';
		
		
	}
	public function on_index(){

		exit('ok');
		$page=misc::page();
		$pagesize=2000;
		$start=($page-1)*$pagesize;
		
		$list=$this->post->index_fetch(array(),array('pid'=>1),$start,$pagesize);
		if(empty($list)) exit('OK');
		foreach($list as $v){
			$topic=$this->topic->index_fetch(array('tid'=>$v['tid']),array(),0,1);
			$topic=array_pop($topic);
			if($topic){
				$v['fid']=$topic['fid'];
				$where=array('tid'=>$v['tid'],'pid'=>array('<='=>$v['pid']));
				$count=$this->post->index_count($where);
				$v['floor']=$count;
				$this->post->update(array('pid'=>$v['pid'],'fid'=>$v['fid'],'floor'=>$count));
			}
			
		}
		
		$this->message($start.'-'.($start+$pagesize),3,'?test-index-page-'.($page+1).'.htm');
	
		$list=$this->forum->index_fetch(array(),array(),0,1000);
		foreach($list as $v){
			$c=$this->category->read($v['cat_id']);
			if($c['pid']>0){
				$v['cat_tid']=$c['pid'];
			}else{
				$v['cat_tid']=$c['id'];
			}
			$this->forum->update($v);
			
		}
		
		echo 'ok';
		
	}
	// sae 下载 tmp 包。
	public function on_sae() {
		$filename = $this->conf['upload_path']."tmp.zip";
		if(!is_file($filename)) {
			$this->make_tmp($this->conf);
		}
		if(!is_file($filename)) {
			$this->message('生成 tmp 目录文件失败。', 0);
		}
		
		$timefmt = date('D, d M Y H:i:s', $_SERVER['time']).' GMT';
		header('Date: '.$timefmt);
                header('Last-Modified: '.$timefmt);
		header('Expires: '.$timefmt);
               // header('Cache-control: max-age=0, must-revalidate, post-check=0, pre-check=0');
                header('Cache-control: max-age=86400');
		header('Content-Transfer-Encoding: binary');
		header("Pragma: public");
		header('Content-Disposition: attachment; filename=tmp.zip');
		header('Content-Type: application/octet-stream');
		readfile($filename);
		unlink($filename);
		exit;
	}
	// 生成 tmp 缓存，仅仅在SAE下需要。
	private function make_tmp($conf) {
		//global $bbsconf;
		//$conf = $bbsconf;
		//$tmppath = IN_SAE ? FRAMEWORK_TMP_TMP_PATH.'tmp/' : FRAMEWORK_TMP_PATH;	// 这样比较保险，但是目前看来没有必要。
		$tmppath = FRAMEWORK_TMP_TMP_PATH;
		
		$runtimefile = $tmppath.'_runtime.php';
		if (!is_file($runtimefile)) {
			$content = '';
			$content .= php_strip_whitespace(FRAMEWORK_PATH.'core/core.class.php');
			$content .= php_strip_whitespace(FRAMEWORK_PATH.'core/misc.class.php');
			$content .= php_strip_whitespace(FRAMEWORK_PATH.'core/base_control.class.php');
			$content .= php_strip_whitespace(FRAMEWORK_PATH.'core/base_model.class.php');
			$content .= php_strip_whitespace(FRAMEWORK_PATH.'lib/log.class.php');
			$content .= php_strip_whitespace(FRAMEWORK_PATH.'lib/xn_exception.class.php');
			$content .= php_strip_whitespace(FRAMEWORK_PATH.'lib/encrypt.func.php');
			$content .= php_strip_whitespace(FRAMEWORK_PATH.'lib/template.class.php');
			$content .= php_strip_whitespace(FRAMEWORK_PATH.'db/db.interface.php');
			$content .= php_strip_whitespace(FRAMEWORK_PATH.'db/db_mysql.class.php');
			$content .= php_strip_whitespace(FRAMEWORK_PATH.'cache/cache.interface.php');
			$content .= php_strip_whitespace(FRAMEWORK_PATH.'cache/cache_memcache.class.php');
			file_put_contents($runtimefile, $content);
			unset($content);
		}
		$conf['plugin_disable']=1;
		// 获取插件目录
		$pluginpaths = $conf['plugin_disable'] ? array() : core::get_paths($conf['plugin_path'], TRUE);
		
		// 遍历 control
		foreach(($pluginpaths + $conf['control_path']) as $path) {
			
			// 如果有相关的 app path, 这只读取该目录
			if(is_dir($path.$conf['app_id'])) {
				$path = $path.$conf['app_id'].'/';
			}
			
			foreach((array)glob($path."*_control.class.php") as $file) {
				if(!is_file($file)) continue;
				$filename = substr(strrchr($file, '/'), 1);
				$objfile = $tmppath.$conf['app_id']."_control_$filename";
				
				$s = file_get_contents($file);
				core::process_include($conf, $s);
				
				$_SERVER['preg_replace_callback_arg'] = $conf;
				$s = preg_replace_callback('#\t*\/\/\s*hook\s+([^\s]+)#is', 'core::process_hook_callback', $s);
				
				core::process_urlrewrite($conf, $s);
				file_put_contents($objfile, $s);
				unset($s);
			}
		}

		// 遍历 view，插入点的 .htm 编译是多余的，不过不碍事。
		$view = new template($conf);
		foreach(($pluginpaths + $conf['view_path']) as $path) {
			// 如果有相关的 app path, 这只读取该目录
			if(is_dir($path.$conf['app_id'])) {
				$path = $path.$conf['app_id'].'/';
			}
			foreach((array)glob($path."*.html") as $file) {
				if(!is_file($file)) continue;
				$filename = substr(strrchr($file, '/'), 1);
				$objfile = $tmppath.$conf['app_id']."_view_$filename.php";
				$s = $view->complie($file);
				file_put_contents($objfile, $s);
			}
		}
		unset($view);
		
		// 遍历 model，公共
		foreach(($pluginpaths + $conf['model_path']) as $path) {
			foreach((array)glob($path."*.class.php") as $file) {
				if(!is_file($file)) continue;
				$filename = substr(strrchr($file, '/'), 1);
				$objfile = $tmppath."model_$filename";
				$s = file_get_contents($file);
				
				$_SERVER['preg_replace_callback_arg'] = $conf;
				$s = preg_replace_callback('#\t*\/\/\s*hook\s+([^\s]+)#is', 'core::process_hook_callback', $s);
				
				core::process_urlrewrite($conf, $s);
				file_put_contents($objfile, $s);
				unset($s);
			}
		}
		
		// --------> bbsadmin start
		
		$conf2 = $conf;
		$adminconf = include POST_PATH.'admin/conf/conf.php';
		$adminconf += $conf;
		$conf = $adminconf;
		
		// 遍历 bbsadmin control
		foreach(($pluginpaths + $conf['control_path']) as $path) {
			
			// 如果有相关的 app path, 这只读取该目录
			if(is_dir($path.$conf['app_id'])) {
				$path = $path.$conf['app_id'].'/';
			}
			foreach((array)glob($path."*_control.class.php") as $file) {
				if(!is_file($file)) continue;
				$filename = substr(strrchr($file, '/'), 1);
				$objfile = $tmppath.$conf['app_id']."_control_$filename";
				
				$s = file_get_contents($file);
				core::process_include($conf, $s);
				
				$_SERVER['preg_replace_callback_arg'] = $conf;
				$s = preg_replace_callback('#\t*\/\/\s*hook\s+([^\s]+)#is', 'core::process_hook_callback', $s);

				core::process_urlrewrite($conf, $s);
				file_put_contents($objfile, $s);
				unset($s);
			}
		}
		
		// 遍历 bbsadmin view
		$view = new template($conf);
		
		foreach(($conf['view_path'] + $pluginpaths) as $path) {
			// 如果有相关的 app path, 则只读取该目录
			if(is_dir($path.$conf['app_id'])) {
				$path = $path.$conf['app_id'].'/';
			}

			foreach((array)glob($path."*.html") as $file) {
				
				if(!is_file($file)) continue;
				$filename = substr(strrchr($file, '/'), 1);
				$objfile = $tmppath.$conf['app_id']."_view_$filename.php";
				$s = $view->complie($file);
				file_put_contents($objfile, $s);
				
			}
		}
		unset($view);
		
		// 遍历插件目录，找到插件目录下的 control, view
		// start end before after
		
		$conf = $conf2;
		
		// --------> bbsadmin end
		
		// 打包
		//if(IN_SAE) {
			echo $tmppath.'tmp.zip';
			xn_zip::zip($tmppath.'tmp.zip', $tmppath);
			copy($tmppath.'tmp.zip', $this->conf['upload_path']."tmp.zip");
		//}
	}

}
?>