<?php

/**
 *--------------------------------------
 * 公共函数库
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-2
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

/* 菜单获取页面地址 $urlType 0:compose, 1:direct */
function get_url($urlStr, $urlType = 0)
{
	if(0 == $urlType)
	{
		/* 单页 */
		if(false !== strpos($urlStr, 'single_page/show_single_page'))
		{
			$_t_a = explode('=', $urlStr);
			if(isset($_t_a[1]))
			{
				$_spi = M('SinglePage')->get_singlePageInfo($_t_a[1]);
			}
			if(!empty($_spi))
			{
				return $_spi['sp_url'];
			}
		}
		/* 档案频道封面 */
		if(false !== strpos($urlStr, 'archive/show_channel'))
		{
			$_t_a = explode('=', $urlStr);
			if(isset($_t_a[1]))
			{
				$_aci = M('ArchiveChannel')->get_channelInfo($_t_a[1]);
			}
			if(!empty($_aci))
			{
				return $_aci['ac_url'];
			}

		}
		/* 档案内容 */
		if(false !== strpos($urlStr, 'archive/show_archive'))
		{
			$_t_a = explode('=', $urlStr);
			if(isset($_t_a[1]))
			{
				$_ai = M('Archive')->get_archiveInfo($_t_a[1]);
			}
			if(!empty($_ai))
			{
				return $_ai['a_url'];
			}
		}
		return Url::U($urlStr);
	}
	return $urlStr;
}

/* 添加数组斜线 */
function addslashes_array($array)
{
	if(is_array($array))
	{
		foreach($array as $n=>$v)
		{
			$b[$n]=addslashes_array($v);
		}
		return $b;
	}
	else
	{
		return addslashes($array);
	}
}

/* 删除数组斜线 */
function stripslashes_array($array)
{
	$array = is_array($array) ?
		array_map('stripslashes_array', $array) :
		stripslashes($array);
	return $array;
}

/* 检查交互 */
function check_interaction($interactionName = 'interaction')
{
	$_o_i = M('Option')->get_option('interaction');

	/* 操作间隔太短 */
	if(!I($interactionName, $_o_i['feedback_interval']))
	{
		A()->error(L('_TRY_LATER_'), AServer::get_preUrl());
	}
	I($interactionName);

	/* 防止表单重复提交 */
	$timeKey = ARequest::get('timeKey');
	if(empty($timeKey))
	{
		A()->error(L('VERIFY_FAILED'), AServer::get_preUrl());
	}
	if(ASession::get('timeKey') == ARequest::get('timeKey'))
	{
		A()->error(L('DUPLICATE_SUBMISSION'), AServer::get_preUrl());
	}
	ASession::set('timeKey', ARequest::get('timeKey'));
	/* 验证码检查 */
	if($_o_i['captcha'])
	{
		$vcode = strtolower(trim(ARequest::get('vcode')));
		$s_vcode = ASession::get('vcode');
		if($vcode != $s_vcode or empty($s_vcode))
		{
			ASession::del('vcode');
			A()->error(L('VCODE_ERROR'), AServer::get_preUrl());
		}
	}
}

/* 获取许可证 */
function get_licence($remote = false)
{
	$licenceFile = CFG_PATH.D_S.substr(md5(AServer::get_env('SERVER_NAME')), 0, 16).'.cer';
	if(!file_exists($licenceFile))
	{
		return null;
	}
	$licence = unserialize(ACrypt::decrypt(include($licenceFile), SOFT_NAME.SOFT_CODENAME));
	if($licence['domain'] != AServer::get_env('SERVER_NAME'))
	{
		return null;
	}
	if($remote and false) /*TODO:  */
	{
		return null;
	}
	return $licence;
}
?>