<?php

/**
 *--------------------------------------
 * 通用
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-27
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class CommonCtrlr extends IndexCtrlr
{
	/* 验证码图片 */
	public function captcha_img()
	{
		$name = in_array(ARequest::get('name'), array('vcode', 'test')) ? ARequest::get('name', 'get') : 'vcode';

		$fonts = array('spacing' => 2, 'size' => 16, 'font' => PUBLIC_PATH.D_S.'font/font.ttf');
		$ac = new ACaptcha(90, 30, 5, $fonts);
		ASession::set($name, strtolower($ac->text));
		$ac->create_image();
	}

	/* 获取会员信息 */
	function get_member_info()
	{
		/* 会员开关检查 */
		$o_m = M('Option')->get_option('member');
		if($o_m['switch'])
		{
			if(ASession::get('member_id'))
			{
				if(!ACookie::get('member_id'))
				{
					$_MI = M('Member')->field('m_userid,m_username')->where('member_id=\''.ASession::get('member_id').'\'')->find();
					ACookie::set('member_id', ASession::get('member_id'));
					ACookie::set('m_userid', $_MI['m_userid']);
					ACookie::set('m_username', $_MI['m_username']);
				}
			}
			elseif(ACookie::get('member_id') and !ASession::get('member_id'))
			{
				$_MI = M('Member')->field('m_userid,m_status')->where('member_id=\''.ACookie::get('member_id').'\'')->find();
				if($_MI['m_userid'] == ACookie::get('m_userid'))
				{
					ASession::set('member_id', ACookie::get('member_id'));
					ASession::set('m_status', $_MI['m_status']);
					ASession::set('ml_rank', M('Member')->get_mlRank(ACookie::get('member_id')));
				}
				else
				{
					ASession::clear();
					ACookie::clear();
				}
			}
			$this->display('home/clip/member_info');
		}
	}

	/* 任务 */
	public function task()
	{
		/* 执行任务 */
		$tasks = explode('|', ARequest::get('task'));
		foreach($tasks as $task)
		{
			switch($task)
			{
				case 'update_html_index':
					$this->update_html_index_do();
					break;
				case 'update_html_channel_index':
					$this->update_html_channel_index_do();
					break;
				case 'update_html_channel_list':
					$this->update_html_channel_list_do();
					break;
				case 'update_html_archive':
					$this->update_html_archive_do();
					break;
			}
		}

		if(!get_licence())
		{
			echo '$("body").append("<div style=\"position:absolute;right:0;bottom:0;width:20px;height:14px;text-indent:-9999px;overflow:hidden;\" class=\"powe'.'redby\">pow'.'ered by <a target=\"_blank\" href=\"'.SOFT_AUTHOR_URL.'\">'.SOFT_NAME.' '.SOFT_CODENAME.'</a></div>");';
		}

		exit();
	}

	private function update_html_index_do()
	{
		$_o = M('Option')->get_option('core');
		if(!$_o['html_switch'])
		{
			return;
		}

		$htmlFile = APP_PATH.D_S.'index'.C('HTML.FILE_SUFFIX');
		if((file_exists($htmlFile) and 0 == $_o['html_expire_index'])
			or (time() < filemtime($htmlFile) + $_o['html_expire_index']))
		{
			return;
		}

		/* 计划任务 */
		$this->assign('TASK', 'update_html_index');

		$this->build_html('index'.C('HTML.FILE_SUFFIX'), APP_PATH, 'home/index');

	}

	private function update_html_channel_index_do()
	{
		$_o = M('Option')->get_option('core');
		if(!$_o['html_switch'])
		{
			return;
		}

		$archiveChannelId = intval(ARequest::get('archive_channel_id'));
		$_ACI = M('ArchiveChannel')->get_channelInfo($archiveChannelId);
		if(empty($_ACI) or !$_ACI['ac_is_html'] or 1 != $_ACI['ac_type'])
		{
			return;
		}

		/* 获取html文件名 */
		$_dir = str_replace('{uwa_path}', '', trim($_ACI['ac_html_dir'], '/'));
		$file = $_dir.'/'.trim($_ACI['ac_html_index'], '/').C('HTML.FILE_SUFFIX');

		$htmlFile = APP_PATH.D_S.$file;
		if((file_exists($htmlFile) and 0 == $_o['html_expire_list'])
			or (time() < filemtime($htmlFile) + $_o['html_expire_list']))
		{
			return;
		}

		$this->assign('_V', $_ACI);
		/* 定义当前频道ID */
		$this->assign('AC_ID', $archiveChannelId);
		/* 定义当前位置 */
		$this->assign('POSITION', $_ACI['ac_position']);
		/* 计划任务 */
		$this->assign('TASK', 'update_html_channel_index&archive_channel_id='.$archiveChannelId);

		$this->build_html($file, APP_PATH, 'home/'.$_ACI['ac_tpl_index']);
	}

	private function update_html_channel_list_do()
	{
		$_o = M('Option')->get_option('core');
		if(!$_o['html_switch'] or 0 == $_o['html_expire_list'])
		{
			return;
		}

		$archiveChannelId = intval(ARequest::get('archive_channel_id'));
		$_GET[C('VAR.PAGE')] = intval(ARequest::get(C('VAR.PAGE'))) ? intval(ARequest::get(C('VAR.PAGE'))) : 1;
		$_ACI = M('ArchiveChannel')->get_channelInfo($archiveChannelId);
		if(empty($_ACI) or !$_ACI['ac_is_html'])
		{
			return;
		}

		/* 获取html文件名 */
		$_dir = str_replace('{uwa_path}/', '', trim($_ACI['ac_html_dir'], '/'));

		$pyc = get_instance('Pinyin');
		$naming = str_replace(
			array(
				'{ac_py}',
				'{page}',
				'{ac_id}'),
			array(
				$pyc->get_pinyin($_ACI['ac_name'], 'utf-8'),
				'_page_',
				$_ACI['archive_channel_id']),
			$_ACI['ac_html_naming_list']
		);
		$file = $_dir.'/'.trim($naming, '/').C('HTML.FILE_SUFFIX');

		$htmlFile = APP_PATH.D_S.str_replace('_page_', ARequest::get(C('VAR.PAGE')), $file);

		if((file_exists($htmlFile) and 0 == $_o['html_expire_list'])
			or (time() < filemtime($htmlFile) + $_o['html_expire_list']))
		{
			return;
		}

		$where = array();
		$where['__ARCHIVE__.a_status'] = array('EQ', 1);

		$_ACL = M('ArchiveChannel')->get_channelList(0, $archiveChannelId);
		$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'), $archiveChannelId);
		$where['__ARCHIVE__.archive_channel_id'] = array('IN', implode(',', $act->get_leafid($archiveChannelId)));

		$order = '`a_rank` DESC, `a_edit_time` DESC';

		/* 获取分页信息 */
		$rowsNum = M('Archive')->where($where)->count();
		$p = new APage($rowsNum, $_ACI['ac_page_size'], __APP__.'/'.$file);

		if($p->__get('totalPages') < ARequest::get(C('VAR.PAGE')))
		{
			return;
		}

		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		$this->assign('_V', $_ACI);
		/* 定义当前频道ID */
		$this->assign('AC_ID', $archiveChannelId);
		/* 定义当前位置 */
		$this->assign('POSITION', $_ACI['ac_position']);

		/* 档案列表 */
		$_AL = M('Archive')->get_archiveList($where, $order, $limit, $_ACI['archive_model_id']);
		$this->assign('_L', $_AL);

		/* 计划任务 */
		$this->assign('TASK', 'update_html_channel_list&archive_channel_id='.$archiveChannelId.'&'.C('VAR.PAGE').'='.ARequest::get(C('VAR.PAGE')));

		$this->build_html(str_replace('_page_', ARequest::get(C('VAR.PAGE')), $file), APP_PATH, 'home/'.$_ACI['ac_tpl_list']);

		/* 列表类型,生成index */
		if(1 == ARequest::get(C('VAR.PAGE')) and 1 != $_ACI['ac_type'])
		{
			$this->build_html($_dir.'/'.trim($_ACI['ac_html_index'], '/').C('HTML.FILE_SUFFIX'), APP_PATH, 'home/'.$_ACI['ac_tpl_list']);
		}

		/* 处理新下页 */
		if($p->__get('totalPages') == ARequest::get(C('VAR.PAGE')))
		{
			return;
		}
		ARequest::set(C('VAR.PAGE'), ARequest::get(C('VAR.PAGE')) + 1);
		$htmlFileNext = APP_PATH.D_S.str_replace('_page_', ARequest::get(C('VAR.PAGE')), $file);
		if(file_exists($htmlFileNext))
		{
			return;
		}
		$this->update_html_channel_list_do();
	}

	private function update_html_archive_do()
	{
		$_o = M('Option')->get_option('core');
		if(!$_o['html_switch'])
		{
			return;
		}

		$archiveId = intval(ARequest::get('archive_id'));
		$_AI = M('Archive')->get_archiveInfo($archiveId);
		if(empty($_AI)
			or !$_AI['ac_is_html']
			or 0 != $_AI['ac_view_ml_rank']
			or !$_AI['a_is_html']
			or 1 != $_AI['a_status']
			or 0 != $_AI['a_cost_points'])
		{
			return;
		}

		/* 获取html文件名 */
		$_dir = '';
		if(0 == $_AI['a_html_path'])
		{
			$_dir = str_replace('{uwa_path}/', '', trim($_AI['ac_html_dir'], '/'));
		}

		$pyc = get_instance('Pinyin');

		/* 判断文件名 */
		if(!empty($_AI['a_html_naming']))
		{
			$naming = $_AI['a_html_naming'];
		}
		else
		{
			$naming = $_AI['ac_html_naming_archive'];
		}
		$naming = str_replace(
			array(
				'{ac_py}',
				'{ac_id}',
				'{Y}',
				'{M}',
				'{D}',
				'{a_py}',
				'{a_id}'),
			array(
				$pyc->get_pinyin($_AI['ac_name'], 'utf-8'),
				$_AI['archive_channel_id'],
				date('Y', $_AI['a_add_time']),
				date('m', $_AI['a_add_time']),
				date('d', $_AI['a_add_time']),
				$pyc->get_pinyin($_AI['a_title'], 'utf-8'),
				$_AI['archive_id']),
			$naming
		);
		$file = $_dir.'/'.trim($naming, '/');

		$htmlFile = APP_PATH.D_S.$file;

		if((file_exists($htmlFile) and 0 == $_o['html_expire_archive'])
			or (time() < filemtime($htmlFile) + $_o['html_expire_archive']))
		{
			return;
		}

		/* 定义当前频道ID */
		$this->assign('AC_ID', $_AI['archive_channel_id']);
		/* 定义当前档案ID */
		$this->assign('A_ID', $_AI['archive_id']);
		/* 频道信息 */
		$_ACI = M('ArchiveChannel')->get_channelInfo($_AI['archive_channel_id']);
		$_AI['ac_sibling'] = $_ACI['ac_sibling']; /* 获取同级分类 */
		/* 定义当前位置 */
		$this->assign('POSITION', $_ACI['ac_position'].' &raquo; '.$_AI['a_title']);
		/* 计划任务 */
		$this->assign('TASK', 'update_html_archive&archive_id='.$archiveId);

		/* 处理自动分页字段 */
		foreach($_ACI['am_field'] as $field => $params)
		{
			if(1 == $params['f_is_paging'])
			{
				$pagingField = $field;
				break;
			}
		}

		/* 获取模板 */
		if(!empty($_AI['a_tpl']))
		{
			$tpl = 'home/'.$_AI['a_tpl'];
		}
		else
		{
			$tpl = 'home/'.$_AI['ac_tpl_archive'];
		}

		if(isset($pagingField) and false !== strpos($_AI[$pagingField], '#uwa_paging#'))
		{
			$_title = $_AI['a_title'];
			$_content = explode('#uwa_paging#', $_AI[$pagingField]);

			$rowsNum = count($_content);
			/* 创建分页 */
			foreach($_content as $key => $_c)
			{
				$_GET[C('VAR.PAGE')] = $key + 1;
				$p = new APage($rowsNum, 1, __APP__.'/'.$file.'-_page_'.C('HTML.FILE_SUFFIX'));
				$this->assign('PAGE_LIST', $p->show());
				$_AI[$pagingField] = ltrim($_content[ARequest::get(C('VAR.PAGE')) - 1], '</p>');
				$_AI['a_title'] = $_title.'('.ARequest::get(C('VAR.PAGE')).')';
				$this->assign('_V', $_AI);
				$this->build_html($file.'-'.ARequest::get(C('VAR.PAGE')).C('HTML.FILE_SUFFIX'), APP_PATH, $tpl);
				/* 创建默认页 */
				if(1 == ARequest::get(C('VAR.PAGE')))
				{
					$this->build_html($file.C('HTML.FILE_SUFFIX'), APP_PATH, $tpl);
				}
			}
		}
		else
		{
			$this->assign('PAGE_LIST', '');

			$this->assign('_V', $_AI);
			$this->build_html($file.C('HTML.FILE_SUFFIX'), APP_PATH, $tpl);
		}
	}
}

?>