<?php
/************************************************
* response ajax request
*************************************************/
class Ajax
{
/* 传递过来的数据 */
protected $postInfo;
/* 处理后的数据 */
private $ajaxInfo;
/* 编码 */
protected $encode;
/* 初始化编码和接收的数据, 将接收的数据解码
* @param $info 接收的数据
* @param $encode 编码
*/
public function __construct($encode = 'utf-8')
{
$this->encode = $encode;
$this->postInfo = empty($_POST) ? (empty($_GET) ? '' : $_GET) : $_POST;
if(is_array($this->postInfo))
{
foreach ($this->postInfo as $key => $val)
{
$this->ajaxInfo[$key] = $this->unescape($val);
}
}
else {
$this->ajaxInfo = $this->unescape($info);
}
}
/* get $ajaxInfo */
protected function getVal()
{
return $this->ajaxInfo;
}
/* 对接收数据进行操作 */
public function doInsertVal() {}
/* 返回数据 */
public function doReqVal() {}
/* 解码 js unescape() */
public function unescape($str)
{
$str = rawurldecode($str);
preg_match_all("/%u.{4}|&#x.{4};|&#d+;|.+/U",$str,$r);
$ar = $r[0];
foreach($ar as $k => $v) {
if(substr($v,0,2) == "%u") {
$restr = substr($v, -4);
if (!eregi("WIN", PHP_OS)){
$restr=substr($restr, 2, 2).substr($restr, 0, 2);
}
$ar[$k] = iconv("UCS-2", $this->encode, pack("H4", $restr));
} elseif (substr($v, 0, 3) == "&#x") {
$ar[$k] = iconv("UCS-2", $this->encode, pack("H4",substr($v,3,-1)));
} elseif(substr($v, 0, 2) == "&#") {
$ar[$k] = iconv("UCS-2", $this->encode, pack("n",substr($v,2,-1)));
}
}
return join('',$ar);
}
/* 编码 js escape() */
public function escape($str)
{
$sublen=strlen($str);
$reString="";
for ($i=0;$i<$sublen;$i++){
if(ord($str[$i])>=127){
$tmpString=bin2hex(iconv($this->encode,"ucs-2",substr($str,$i,2))); //此处GBK为目标代码的编码格式,请实际情况修改
if (!eregi("WIN",PHP_OS)){
$tmpString=substr($tmpString,2,2).substr($tmpString,0,2);
}
$reString.="%u".$tmpString;
$i++;
} else {
$reString.="%".dechex(ord($str[$i]));
}
}
return $reString;
}
}
?>
评论0