$val) {
$string[$key] = saddslashes($val);
}
} else {
$string = addslashes($string);
}
return $string;
}
/**
*在52服务器上执行mongodb搜索语句
* @param type $db
* @param type $coll
* @return type
*/
function getCollection_52($db,$coll)
{
$conn=getMongo_52();
$db=$conn->selectDB($db);
$collection=$db->selectCollection($coll);
return $collection;
}
/**
* 链接52mongodb
* @return \MongoClient
*/
function getMongo_52()
{
try {
$conn=new MongoClient("192.168.1.52:52017");
} catch (Exception $e) {
echo 'connect mongo error:52';exit;
}
return $conn;
}
/**
* 取消HTML代码
* @param type $string
* @return type
*/
function shtmlspecialchars($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = shtmlspecialchars($val);
}
} else {
$string = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string));
}
return $string;
}
/**
* 字符串加密解密
* @param string $string
* @param string $operation ENCODE:加密|DECODE:解密
* @param type $key
* @param type $expiry
* @return string
*/
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
$ckey_length = 0; // 随机密钥长度 取值 0-32;
// 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
// 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
// 当此值为 0 时,则不产生随机密钥
$key = md5($key ? $key : UC_KEY);
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
$cryptkey = $keya.md5($keya.$keyc);
$key_length = strlen($cryptkey);
$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if($operation == 'DECODE') {
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
return $keyc.str_replace('=', '', base64_encode($result));
}
}
/**
* 检查邮箱是否有效
* @param type $email
* @return type
*/
function isemail($email) {
return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
}
/**
* 过滤关键字符
* @param type $str
* @return type
*/
function inputFilter($str)
{
if(empty($str))
{
return;
}
if($str=="")
{
return $str;
}
$str=trim($str);
$str=str_replace("&","&",$str);
$str=str_replace(">",">",$str);
$str=str_replace("<","<",$str);
$str=str_replace(chr(34),"&",$str);
$str=str_replace(chr(39),"'",$str);
$str=str_replace(chr(13),"
",$str);
$str=str_replace("'","''",$str);
$str=str_replace("select","select",$str);
$str=str_replace("join","join",$str);
$str=str_replace("union","union",$str);
$str=str_replace("where","where",$str);
$str=str_replace("insert","insert",$str);
$str=str_replace("delete","delete",$str);
$str=str_replace("update","update",$str);
$str=str_replace("like","like",$str);
$str=str_replace("drop","drop",$str);
$str=str_replace("create","create",$str);
$str=str_replace("modify","modify",$str);
$str=str_replace("rename","rename",$str);
$str=str_replace("alter","alter",$str);
$str=str_replace("cast","cas",$str);
return $str;
}
/**
* 截取字符串,中英文
* @param type $str
* @param type $start
* @param type $len
* @return type
*/
function msubstr($str, $start, $len) {
$tmpstr = "";
$strlen = $start + $len;
for($i = 0; $i < $strlen; $i++) {
if(ord(substr($str, $i, 1)) > 0xa0) {
$tmpstr .= substr($str, $i, 2);
$i++;
} else
$tmpstr .= substr($str, $i, 1);
}
return $tmpstr;
}
/**
* 获取IP
* @return type
*/
function getip(){
//判断服务器是否允许$_SERVER
if(isset($_SERVER)){
if(isset($_SERVER[HTTP_X_FORWARDED_FOR])){
$realip = $_SERVER[HTTP_X_FORWARDED_FOR];
}elseif(isset($_SERVER[HTTP_CLIENT_IP])) {
$realip = $_SERVER[HTTP_CLIENT_IP];
}else{
$realip = $_SERVER[REMOTE_ADDR];
}
}else{
//不允许就使用getenv获取
if(getenv("HTTP_X_FORWARDED_FOR")){
$realip = getenv( "HTTP_X_FORWARDED_FOR");
}elseif(getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
}else{
$realip = getenv("REMOTE_ADDR");
}
}
$realip=trim($realip,',');
$arr_ip=explode(',',$realip);
return $arr_ip[count($arr_ip)-1];
}
/**
* 创建目录
* @param type $typedir 要创建的目录
*/
function createDir($typedir)
{
if(!is_dir($typedir))
{
mkdir($typedir,0744,true);
}
}
// 单例模式的数据库操作函数 - qiangwan_hwzs
function getinstance2() {
require_once S_ROOT . 'source/libs/Db2.class.php';
return Dbhelper1::getinstance();
}
function getonerecord2($query) {
global $tp;
$db = getinstance2();
$db->localPath=$tp;
return $db->getonerecord($query);
}
function getrecord_array($query){
global $tp;
$db = getinstance2();
$data=array();
$db->localPath=$tp;
$resource=$db->exec_db($query);
if($resource){
while($row= mysqli_fetch_array($resource,MYSQLI_ASSOC)){
$data[]=$row;
}
}
return $data;
}
function exec_db_insertid2($query) {
global $tp;
$db = getinstance2();
$db->localPath=$tp;
return $db->exec_db_insertid($query);
}
function exec_db2($query) {
global $tp;
$db = getinstance2();
$db->localPath=$tp;
return $db->exec_db($query);
}
/**
* 获取单条记录
* @param type $query_str
* @return type
*/
// function getonerecord($query_str)
// {
// $result=exec_db($query_str);
// $row=mysql_fetch_array($result,MYSQL_ASSOC);
// return $row;
// }
//连接数据库
// function exec_db($query_str)
// {
// global $tp;
// $dblink=mysql_connect(DBHOST,DBUSER,DBPW,1,131072);
// if(!$dblink)
// {
// @dolog('mysql链接错误记录','域名:'.$_SERVER['SERVER_NAME']."\n".$tp."\n".$query_str);
// die("Query failed:");
// }
// mysql_select_db(DBNAME);
// mysql_query("SET NAMES 'utf-8'");
//
// $result=mysql_query($query_str) or die("Query failed:".dolog('sql语句错误','域名:'.$_SERVER['SERVER_NAME']."\n".$tp."\n".$query_str));
// mysql_close($dblink);
// return $result;
// }
//返回插入的ID
// function exec_db_insertid($query_str)
// {
// global $tp;
// $dblink=mysql_connect(DBHOST,DBUSER,DBPW,1,131072);
// mysql_select_db(DBNAME);
// mysql_set_charset('utf8',$dblink);
// $result=mysql_query($query_str) or die("Query failed:".mysql_error());
// $insertid = mysql_insert_id();
// mysql_close($dblink);
// return $insertid;
// }
/**
* 获取链接数据库的 resource资源
* @return type
*/
// function getdblink()
// {
// $dblink=mysql_connect(DBHOST,DBUSER,DBPW,1,131072) or die("Connect to db failed");
// mysql_select_db(DBNAME,$dblink);
// mysql_query("SET NAMES 'utf8'",$dblink);
// return $dblink;
// }
// function exec_db_ps($query_str,$dblink)
// {
// $result=mysql_query($query_str,$dblink);
// if(!$result)
// {
// die("Query failed:".mysql_error());
// }
// return $result;
// }
/**
*
* @param string $query_str SQL 语句
* @param source $dblink 连接mysql资源
* @return array
*/
// function getrecord_array($query_str,$dblink){
// $dataArr=array();
// $result=exec_db_ps($query_str,$dblink);
// while($row= mysql_fetch_array($result,MYSQL_ASSOC)){
// $dataArr[]=$row;
// }
// return $dataArr;
// }
// function getonerecord_ps($query_str,$dblink)
// {
// $result=exec_db_ps($query_str,$dblink);
// $row=mysql_fetch_array($result,MYSQL_ASSOC);
// return $row;
// }
//本地的数据库插入最后的ID
// function get_insert_id($query_str)
// {
// global $tp;
// $dblink=mysql_connect(DBHOST,DBUSER,DBPW,1,131072) or die("Connect to db failed");
// mysql_select_db(DBNAME);
// mysql_query("SET NAMES 'utf8'");
// $result=mysql_query($query_str) or die("Query failed:". mysql_error().@dolog("connectMysqliError",$query_str."\n". mysql_errno()));
// $id=mysql_insert_id();
// mysql_close($dblink);
// return $id;
// }
//
function CheckEmail($email)
{
if (ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+",$email))
{
return true;
}
else
{
return false;
}
}
function curl_file_get_contents($url,$is_ajax=false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
function init_smarty()
{
include_once(S_ROOT."source/libs/Smarty.class.php");
$tpl = new Smarty();
$tpl->template_dir = S_ROOT."templates/";
$tpl->compile_dir = S_ROOT."templates_c/";
$tpl->cache_dir = S_ROOT."cache/";
$tpl->left_delimiter = '';
return $tpl;
}
function init_smarty_2()
{ global $appid;
include_once(S_ROOT."source/libs/Smarty.class.php");
$tpl = new Smarty();
$tpl->template_dir = S_ROOT."new_tpl/tpl5/";
$tpl->compile_dir = S_ROOT."new_tpl/tpl5_c/";
$tpl->cache_dir = S_ROOT."new_tpl/cache/";
$tpl->left_delimiter = '';
return $tpl;
}
function init_smarty_10()
{
include_once(S_ROOT."source/libs/Smarty.class.php");
$tpl = new Smarty();
$tpl->template_dir = S_ROOT."new_tpl/tpl10/";
$tpl->compile_dir = S_ROOT."new_tpl/tpl10_c/";
$tpl->cache_dir = S_ROOT."new_tpl/cache/";
$tpl->left_delimiter = '';
return $tpl;
}
/**
*
* @global type $tpl
* @param type $message
* @param type $url_forward
*/
function cpmessage($message,$url_forward='')
{
global $tpl;
if(!empty($url_forward))
{
$message .= "";
}else
{
$message .= "";
}
echo $message;
exit;
}
function cpmessage2($message,$url_forward='')
{
if(!empty($url_forward))
{
$message .= "";
}else
{
$message .= "";
}
echo $message;
exit;
}
/**
* 获取ip相应的地址
* @param type $ip
* @return string
*/
function getiparea($ip)
{
include_once(S_ROOT."source/ip/qqwry.php");
$QQWry=new QQWry;
$QQWry->QQWry($ip);
$area=trim($QQWry->Country);
if($area=="局域网")
{
$area="中国";
}
return $area;
}
/**
* 分页类
*/
function urlAnalyze($page) {
$url = $_SERVER['REQUEST_URI'];
$parse_url = parse_url($url);
$url_query = isset($parse_url['query']) ? $parse_url['query'] : '';
if (!empty($url_query)) {
if (strpos($url_query, 'page=') === false) {
$url .= '&page=PAGE';
} else {
$url = str_replace('page=' . trim($_GET['page']), 'page=PAGE', $url);
}
} else {
$url .= '?page=PAGE';
}
return $url;
}
/**
* 生成分页的HTML
* @param type $total
* @param type $page
* @param type $pagesize
* @return string
*/
function getPager($total, $page, $pagesize) {
$pageData = array();
$pageData['pageCount'] = ceil($total / $pagesize);
if ($page > $pageData['pageCount']) {
$page = $pageData['pageCount'];
}
if ($page - 1 > 0) {
$pageData['prevPage'] = $page - 1;
} else {
$pageData['prevPage'] = 1;
}
if ($pageData['pageCount'] >= $page + 1) {
$pageData['nextPage'] = $page + 1;
} else {
$pageData['nextPage'] = $pageData['pageCount'];
}
$pageData['lastPage'] = $pageData['pageCount'];
$nextPageUrl = urlAnalyze($page);
$html = '