1037 lines
31 KiB
PHP
1037 lines
31 KiB
PHP
<?php
|
||
/*
|
||
[zde] (C) 2009 Inc.
|
||
Auth: 2009-12 ddcai $
|
||
E-Mail ddcai#21cn.com
|
||
$Action: 公共函数
|
||
*/
|
||
|
||
function toHtml($txt){
|
||
$txt = str_replace(" "," ",$txt);
|
||
$txt = str_replace("<","<",$txt);
|
||
$txt = str_replace(">",">",$txt);
|
||
$txt = preg_replace("/[\r\n]{1,}/is","<br/>",$txt);
|
||
return $txt;
|
||
}
|
||
|
||
function delHtml($txt){
|
||
$txt = str_replace("<p>","",$txt);
|
||
$txt = str_replace("</p>","",$txt);
|
||
return $txt;
|
||
}
|
||
|
||
//把字符串转化为可以在JS数组里输出的字符串
|
||
function jsToHtml($mystr){
|
||
if(empty($mystr)){
|
||
return "";
|
||
}
|
||
$mystr = str_replace("\r\n","",$mystr);
|
||
$mystr = str_replace("'","\\'",$mystr);
|
||
return $mystr;
|
||
}
|
||
|
||
|
||
function cutstr($string, $length, $dot = ' ...',$charset='utf-8') {
|
||
/*
|
||
功能:按长度截取字符串
|
||
$string, $length, $dot,$charset
|
||
字符串,要取长度,截断符,编码
|
||
*/
|
||
if(strlen($string) <= $length) {
|
||
return $string;
|
||
}
|
||
|
||
$string = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string);
|
||
|
||
$strcut = '';
|
||
if(strtolower($charset) == 'utf-8') {
|
||
|
||
$n = $tn = $noc = 0;
|
||
while($n < strlen($string)) {
|
||
|
||
$t = ord($string[$n]);
|
||
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
|
||
$tn = 1; $n++; $noc++;
|
||
} elseif(194 <= $t && $t <= 223) {
|
||
$tn = 2; $n += 2; $noc += 2;
|
||
} elseif(224 <= $t && $t <= 239) {
|
||
$tn = 3; $n += 3; $noc += 2;
|
||
} elseif(240 <= $t && $t <= 247) {
|
||
$tn = 4; $n += 4; $noc += 2;
|
||
} elseif(248 <= $t && $t <= 251) {
|
||
$tn = 5; $n += 5; $noc += 2;
|
||
} elseif($t == 252 || $t == 253) {
|
||
$tn = 6; $n += 6; $noc += 2;
|
||
} else {
|
||
$n++;
|
||
}
|
||
|
||
if($noc >= $length) {
|
||
break;
|
||
}
|
||
}
|
||
if($noc > $length) {
|
||
$n -= $tn;
|
||
}
|
||
$strcut = substr($string, 0, $n);
|
||
|
||
} else {
|
||
for($i = 0; $i < $length; $i++) {
|
||
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
|
||
}
|
||
}
|
||
$strcut = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $strcut);
|
||
|
||
return $strcut.$dot;
|
||
}
|
||
|
||
|
||
function change_format($time,$time2,$myformat="d"){
|
||
/**
|
||
*功能:返回两个时间差
|
||
*参数:$time,$time2 时间1,时间2
|
||
*参数:$myformat 返回的时间差格式
|
||
*/
|
||
$sY = 31536000;//年
|
||
$sM = 2592000;//月(三十天计算)
|
||
$sW = 604800;//星期
|
||
$sD = 86400;//天
|
||
$sH = 3600;//小时
|
||
$sI = 60;//分钟
|
||
$sS = 1;//秒
|
||
|
||
$tmp_time = strtotime($time)-strtotime($time2);
|
||
switch($myformat){
|
||
case "y"://年
|
||
$tmp_time = $tmp_time/$sY;
|
||
break;
|
||
case "w"://星期
|
||
$tmp_time = $tmp_time/$sW;
|
||
break;
|
||
case "d"://天
|
||
$tmp_time = $tmp_time/$sD;
|
||
break;
|
||
case "h"://小时
|
||
$tmp_time = $tmp_time/$sH;
|
||
break;
|
||
case "i"://分钟
|
||
$tmp_time = $tmp_time/$sI;
|
||
break;
|
||
case "s"://秒
|
||
$tmp_time = $tmp_time;
|
||
break;
|
||
default://默认返回天
|
||
$tmp_time = $tmp_time/$sD;
|
||
break;
|
||
}
|
||
return $tmp_time;
|
||
}
|
||
|
||
//防RSS并进行转义
|
||
function get_param2($param_name,$convert='')
|
||
{
|
||
$param_value = "";
|
||
if(isset($_POST[$param_name])){
|
||
$param_value = trim($_POST[$param_name]);
|
||
}else if(isset($_GET[$param_name])){
|
||
$param_value = trim($_GET[$param_name]);
|
||
}
|
||
$param_value = RemoveXSS($param_value);
|
||
if(!get_magic_quotes_gpc()){//加上检查数据防sql注入
|
||
$param_value = sql_addslashes($param_value);
|
||
}
|
||
if ('int' == strtolower($convert)){
|
||
if (strlen($param_value)>10){
|
||
$param_value = preg_replace('/[^\d]/is','',$param_value);
|
||
}
|
||
else $param_value = intval($param_value);
|
||
}
|
||
|
||
return $param_value;
|
||
}
|
||
|
||
//不转义接收参数
|
||
function get_param3($param_name,$convert=''){
|
||
$param_value = "";
|
||
if(isset($_POST[$param_name])){
|
||
$param_value = $_POST[$param_name];
|
||
}else if(isset($_GET[$param_name])){
|
||
$param_value = $_GET[$param_name];
|
||
}
|
||
if(get_magic_quotes_gpc()){//如果打开发转义,则不能进行转义的,用户接收参数
|
||
$param_value = stripslashes($param_value);
|
||
}
|
||
if ('int' == strtolower($convert)){
|
||
if (strlen($param_value)>10){
|
||
$param_value = preg_replace('/[^\d]/is','',$param_value);
|
||
}
|
||
else $param_value = intval($param_value);
|
||
}
|
||
|
||
return $param_value;
|
||
}
|
||
|
||
//进行转义,但不进行RSS
|
||
function get_param($param_name,$convert='')
|
||
{
|
||
$param_value = "";
|
||
if(isset($_POST[$param_name])){
|
||
$param_value = trim($_POST[$param_name]);
|
||
}else if(isset($_GET[$param_name])){
|
||
$param_value = trim($_GET[$param_name]);
|
||
}
|
||
if(!get_magic_quotes_gpc()){//加上检查数据防sql注入
|
||
$param_value = sql_addslashes($param_value);
|
||
}
|
||
if ('int' == strtolower($convert)){
|
||
if (strlen($param_value)>10){
|
||
$param_value = preg_replace('/[^\d]/is','',$param_value);
|
||
}
|
||
else $param_value = intval($param_value);
|
||
}
|
||
|
||
return $param_value;
|
||
}
|
||
|
||
function sql_addslashes($value){
|
||
if (empty($value)){
|
||
return $value;
|
||
}else{
|
||
return is_array($value) ? array_map('sql_addslashes', $value) : addslashes($value);
|
||
}
|
||
}
|
||
/**
|
||
* @去除XSS(跨站脚本攻击)的函数
|
||
* @par $val 字符串参数,可能包含恶意的脚本代码如<script language="javascript">alert("hello world");</script>
|
||
* @return 处理后的字符串
|
||
* @Recoded By Androidyue
|
||
**/
|
||
function RemoveXSS($val) {
|
||
// remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
|
||
// this prevents some character re-spacing such as <java\0script>
|
||
// note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
|
||
$val = preg_replace('/([\x00-\x08\x0b-\x0c\x0e-\x19])/', '', $val);
|
||
|
||
// straight replacements, the user should never need these since they're normal characters
|
||
// this prevents like <IMG SRC=@avascript:alert('XSS')>
|
||
$search = 'abcdefghijklmnopqrstuvwxyz';
|
||
$search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||
$search .= '1234567890!@#$%^&*()';
|
||
$search .= '~`";:?+/={}[]-_|\'\\';
|
||
for ($i = 0; $i < strlen($search); $i++) {
|
||
// ;? matches the ;, which is optional
|
||
// 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
|
||
|
||
// @ @ search for the hex values
|
||
$val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
|
||
// @ @ 0{0,7} matches '0' zero to seven times
|
||
$val = preg_replace('/(�{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
|
||
}
|
||
|
||
// now the only remaining whitespace attacks are \t, \n, and \r
|
||
$ra1 = Array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
|
||
$ra2 = Array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
|
||
$ra = array_merge($ra1, $ra2);
|
||
|
||
$found = true; // keep replacing as long as the previous round replaced something
|
||
while ($found == true) {
|
||
$val_before = $val;
|
||
for ($i = 0; $i < sizeof($ra); $i++) {
|
||
$pattern = '/';
|
||
for ($j = 0; $j < strlen($ra[$i]); $j++) {
|
||
if ($j > 0) {
|
||
$pattern .= '(';
|
||
$pattern .= '(&#[xX]0{0,8}([9ab]);)';
|
||
$pattern .= '|';
|
||
$pattern .= '|(�{0,8}([9|10|13]);)';
|
||
$pattern .= ')*';
|
||
}
|
||
$pattern .= $ra[$i][$j];
|
||
}
|
||
$pattern .= '/i';
|
||
$replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag
|
||
$val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
|
||
if ($val_before == $val) {
|
||
// no replacements were made, so exit the loop
|
||
$found = false;
|
||
}
|
||
}
|
||
}
|
||
return $val;
|
||
}
|
||
|
||
//返回随机密码
|
||
function cai_get_pwd($mylen=8){
|
||
//密码长度:$mylen
|
||
if(empty($mylen)){
|
||
$mylen = 8;
|
||
}
|
||
$array="2123456789";
|
||
$authnum = '';
|
||
for($i=0;$i<$mylen;$i++){
|
||
$authnum .=substr($array,mt_rand(0,9),1);
|
||
}
|
||
return $authnum;
|
||
}
|
||
|
||
//返回访问者的IP
|
||
function return_user_ip(){
|
||
$onlineip="127.0.0.1";
|
||
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
|
||
$onlineip = getenv('HTTP_CLIENT_IP');
|
||
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
|
||
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
|
||
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
|
||
$onlineip = getenv('REMOTE_ADDR');
|
||
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
|
||
$onlineip = $_SERVER['REMOTE_ADDR'];
|
||
}
|
||
$onlineip = explode(',', $onlineip);
|
||
return $onlineip[0];
|
||
}
|
||
|
||
//设置cookie
|
||
function isetcookie($var, $value, $life=0){
|
||
global $cookiedomain, $cookiepath,$_SERVER;
|
||
if(!empty($life)){
|
||
$life = time() + $life * 86400;
|
||
}
|
||
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
|
||
if($cookiedomain!=""){
|
||
setcookie($var, $value, $life,$cookiepath,$cookiedomain,$_SERVER['SERVER_PORT']==443?1:0);
|
||
}else{
|
||
setcookie($var, $value, $life,$cookiepath);
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
*分页函数
|
||
*参数:$num,$perpage,$curr_page,$mpurl,
|
||
*说明:总数量,每页条数,当前页号,连接的URL
|
||
*
|
||
**/
|
||
//分页
|
||
function multi($num, $perpage, $curr_page, $mpurl) {
|
||
$multipage = '';
|
||
if($num > $perpage) {
|
||
$pages = ceil($num / $perpage);//求总页数
|
||
$multipage .= "<a href=\"$mpurl&page=1\">首页</a> <a href=\"$mpurl&page=".(($curr_page>0?$curr_page:1)-1)."\">上一页</a> ";
|
||
|
||
$multipage .= "<a href=\"$mpurl&page=".($curr_page>=$pages?$pages:$curr_page+1)."\">下一页</a> <a href=\"$mpurl&page=$pages\" >最后页</a><select name='showpage' id='showpage' onchange='location.href=\"".$mpurl."&page=\"+this.options[this.selectedIndex].value;'>";
|
||
for($i=1;$i<=$pages;$i++){
|
||
if($i==$curr_page){
|
||
$multipage .= '<option value="'.$i.'" selected="selected">'.$i.'/'.$pages.'</option>';
|
||
}else{
|
||
$multipage .= '<option value="'.$i.'">'.$i.'/'.$pages.'</option>';
|
||
}
|
||
}
|
||
$multipage .= "</select>";
|
||
}
|
||
return $multipage;
|
||
}
|
||
|
||
/**
|
||
*分页函数
|
||
*参数:$num,$perpage,$curr_page,$mpurl,
|
||
*说明:总数量,每页条数,当前页号,连接的URL
|
||
*
|
||
**/
|
||
//分页
|
||
function multitwo($num, $perpage, $curr_page, $mpurl, $pagenum=3){
|
||
$multipage = '';
|
||
if($num > $perpage) {
|
||
$pages = ceil($num / $perpage);//求总页数
|
||
$multipage .= "<a class='page-prev' href=\"$mpurl&page=".(($curr_page>0?$curr_page:1)-1)."\">上一页</a><span style='width: 20px;'> </span>";
|
||
|
||
//显示当前页前后5项
|
||
$startpage = $curr_page <= 5 ? 1 : $curr_page-$pagenum;
|
||
$endpage = $pages<=$curr_page+$pagenum ? $pages : $curr_page+$pagenum;
|
||
for($i = $startpage; $i <= $endpage; $i++){
|
||
if($i == $curr_page){
|
||
$multipage .= "<a class='page-cur' href=\"$mpurl&page=$i\">[".$i."]</a>";
|
||
}else{
|
||
$multipage .= "<a href=\"$mpurl&page=$i\">[".$i."]</a>";
|
||
}
|
||
}
|
||
$multipage .= "<a class='page-next' href=\"$mpurl&page=".($curr_page>=$pages?$pages:$curr_page+1)."\">下一页</a>";
|
||
}
|
||
return $multipage;
|
||
}
|
||
|
||
|
||
|
||
|
||
//=========begin 操作数据库通用
|
||
/* *
|
||
* 检查相关记录是否存在
|
||
*
|
||
* $table 数据库表名
|
||
* $value 与$fields对应的值
|
||
* */
|
||
function exist_check($conn,$table,$value = array()){
|
||
$where = '';
|
||
foreach($value as $key=>$val){
|
||
if($key!=""){
|
||
$where .= " AND `".$key."`='".$val."' ";
|
||
}
|
||
}
|
||
|
||
$exist= "SELECT 1 FROM ".get_table($table)." WHERE 1 ".$where;
|
||
//die($exist);
|
||
$res = $conn->Query($exist);
|
||
if($conn->NumRows($res)>0){
|
||
return true;
|
||
}else{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/* *
|
||
* 在数据库表$table中增加一条记录
|
||
*
|
||
* $table 数据库表名
|
||
* $value 与$field对应的值
|
||
* */
|
||
function add_record($conn,$table,$value = array(),$show_bug=false){
|
||
$field_str = '';
|
||
$value_str = '';
|
||
foreach($value as $key=>$val) {
|
||
if($key!=""){
|
||
$field_str .= '`'.$key.'`,';
|
||
$value_str .= "'".$val."',";
|
||
}
|
||
}
|
||
|
||
$field_str = substr($field_str,0,-1);
|
||
$value_str = substr($value_str,0,-1);
|
||
|
||
$insert_sql = "INSERT INTO ".get_table($table)." ($field_str) VALUES($value_str);";
|
||
//echo $insert_sql;
|
||
$conn->Query($insert_sql);
|
||
|
||
$result = array();
|
||
$result['rows'] = $conn->AffectedRows();
|
||
$result['id'] = $conn->InsertID();
|
||
if($show_bug==true){
|
||
$result['error'] = $insert_sql;
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
/* *
|
||
* 在数据库表$table中更新一条记录
|
||
*
|
||
* $table 数据库表名
|
||
* $value 与$field对应的值
|
||
* $where_ 条件字段与值
|
||
* */
|
||
|
||
function update_record($conn,$table,$value = array(),$where_value = array(),$where='')
|
||
{
|
||
$update_str = '';
|
||
|
||
foreach($value as $key=>$val)
|
||
{
|
||
if($key!=""){
|
||
if(strstr($val,$key.'+')!=false){
|
||
$update_str .= "`".$key."`=".$val.",";
|
||
}elseif(strstr($val,$key.'-')!=false){
|
||
$update_str .= "`".$key."`=".$val.",";
|
||
}else{
|
||
$update_str .= "`".$key."`='".$val."',";
|
||
}
|
||
|
||
}
|
||
}
|
||
if(empty($where))
|
||
{
|
||
foreach($where_value as $key=>$val)
|
||
{
|
||
if($key!=""){
|
||
$where .= " AND `".$key."`='".$val."' ";
|
||
}
|
||
}
|
||
}
|
||
$update_str = substr($update_str,0,-1);
|
||
|
||
$sql = "UPDATE ".get_table($table)." SET $update_str WHERE 1 $where;";
|
||
//echo $sql;exit;
|
||
$conn->Query($sql);
|
||
return $conn->AffectedRows();
|
||
}
|
||
|
||
/* *
|
||
* 在数据库表$table中删除(物理删除)或更新(逻辑删除)一条记录
|
||
*
|
||
* $table 数据库表名
|
||
* $where_ 条件字段与值
|
||
* $value 与$field对应的值
|
||
* */
|
||
|
||
function delete_record($conn,$table,$where_value = array(),$value = array())
|
||
{
|
||
$update_str = '';
|
||
$where = '';
|
||
foreach($where_value as $key=>$val)
|
||
{
|
||
if($key!=""){
|
||
$where .= " AND `".$key."`='".$val."' ";
|
||
}
|
||
}
|
||
if(count($value)>0)
|
||
{
|
||
foreach($value as $key=>$val)
|
||
{
|
||
if($key!=""){
|
||
$update_str .= "`".$key."`='".$val."',";
|
||
}
|
||
}
|
||
|
||
$update_str = substr($update_str,0,-1);
|
||
|
||
$sql = "UPDATE ".get_table($table)." SET $update_str WHERE 1 $where;";
|
||
}else{
|
||
$sql = "DELETE FROM ".get_table($table)." WHERE 1 $where;";
|
||
}
|
||
$conn->Query($sql);
|
||
return $conn->AffectedRows();
|
||
}
|
||
|
||
/**
|
||
* 获取数据库表$table中字段$info的信息
|
||
*
|
||
* $table 数据库表名
|
||
* $fields 条件字段名
|
||
* $value 字段($fields)对应值
|
||
* $info 返回信息字段名
|
||
* $all 是否返回所有记录
|
||
**/
|
||
function get_info($conn,$table,$info = array(),$where='',$OrderBy='',$all = false){
|
||
if(!empty($info)){
|
||
$str = implode(',',$info);
|
||
}else{
|
||
$str = '*';
|
||
}
|
||
if ($OrderBy != ""){
|
||
$where .= $OrderBy;
|
||
}
|
||
$sql= "SELECT $str FROM ".get_table($table)." WHERE 1 ".$where.' ';
|
||
|
||
|
||
$res = $conn->Query($sql);
|
||
//echo("sql:".$sql);
|
||
if($conn->NumRows($res)>0)
|
||
{
|
||
if(!$all)
|
||
{
|
||
$arr = $conn->FetchArray($res);
|
||
if( isset($arr['user_nickname']) ){
|
||
$arr['user_nickname'] = emoji_decode($arr['user_nickname']);
|
||
}
|
||
return $arr;
|
||
}else{
|
||
$all_record = array();
|
||
//$all_record[] = $arr;
|
||
while($arr = $conn->FetchArray($res))
|
||
{
|
||
if( isset($arr['user_nickname']) ){
|
||
$arr['user_nickname'] = emoji_decode($arr['user_nickname']);
|
||
}
|
||
$all_record[] = $arr;
|
||
}
|
||
|
||
return $all_record;
|
||
}
|
||
}else{
|
||
return array();
|
||
}
|
||
}
|
||
|
||
function get_table($myname,$db='',$inc=''){
|
||
if(empty($db)){
|
||
global $TABLE_NAME_INC,$MYSQL_DB;
|
||
return $MYSQL_DB.".`".$TABLE_NAME_INC.$myname."`";
|
||
}else{
|
||
return $db.".`".$inc.$myname."`";
|
||
}
|
||
}
|
||
//=========end 操作数据库通用
|
||
|
||
function www_setcookie($var, $value, $life = 0, $prefix = 1) {
|
||
global $cookiepre, $cookiedomain, $cookiepath, $timestamp, $_SERVER;
|
||
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
|
||
setcookie(($prefix ? $cookiepre : '').$var, $value,
|
||
$life ? $timestamp + $life : 0, $cookiepath,
|
||
$cookiedomain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0);
|
||
}
|
||
|
||
//==============================================
|
||
//==============begin 记录运行时间================
|
||
//返回毫秒的时间
|
||
function microtime_float(){
|
||
list($usec, $sec) = explode(" ", microtime());
|
||
return ((float)$usec + (float)$sec);
|
||
}
|
||
|
||
/**
|
||
* 页面完成时调用,记录页面执行时间
|
||
*
|
||
*/
|
||
function sys_log_shutdown_handler(){
|
||
// 1 开始与结束时间
|
||
$sys_log_end_time = microtime_float();
|
||
|
||
// 2 运行时间
|
||
$diff = number_format( $sys_log_end_time - $GLOBALS['SYS_START_TIME'], '4' );
|
||
|
||
// 3 写入到文件中
|
||
if( $diff > SYS_LOG_MAX_SECOND ){
|
||
sys_log_write_content( $diff );
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 建立日志目录,递归建立
|
||
*
|
||
* @param str $param 路径名
|
||
*/
|
||
function sys_log_mkdir( $param ) {
|
||
if( !file_exists( $param ) ) {
|
||
sys_log_mkdir( dirname( $param ) );
|
||
@mkdir($param);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 保存日志
|
||
*/
|
||
function sys_log_write_content( $msg ,$path="sys_log",$f_name = ""){
|
||
// 1 每天一个日志文件
|
||
$logDir = WEBPATH_DIR.'cache'.DS.$path.DS;
|
||
sys_log_mkdir( $logDir );
|
||
if($f_name==''){
|
||
$logFile = $logDir.date('Y_m_d').'_log.txt';
|
||
}else{
|
||
$logFile = $logDir.$f_name."_".date('Y_m_d').'_log.txt';
|
||
}
|
||
// 2 日志内容: 时间-毫秒-开始(结束)-执行时间-当前链接
|
||
$content = date("Y-m-d H:i:s").' '.$msg;
|
||
$content .= ' '.(isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:"");
|
||
$content .= ' '.(isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'直接输入');
|
||
$content .= "\r\n";
|
||
// 3 写入到指定文件中
|
||
@file_put_contents( $logFile, $content, FILE_APPEND );
|
||
return true;
|
||
}
|
||
//==============end 记录运行时间================
|
||
//============================================
|
||
|
||
|
||
|
||
//==================begin 模拟post发数据
|
||
/*
|
||
* $header='ContentType:application/x-www-form-urlencoded\r\n'
|
||
*/
|
||
function post_request($url,$param=null,$method='post',$header='')
|
||
{
|
||
$ch = curl_init();
|
||
curl_setopt ($ch, CURLOPT_URL, $url);
|
||
// if($method=='post')
|
||
// {
|
||
$send_data = $param;
|
||
curl_setopt($ch, CURLOPT_POST, 1);
|
||
//添加变量
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, $send_data);
|
||
// }
|
||
if(!empty($header)){
|
||
curl_setopt($ch, CURLOPT_HEADER, $header);
|
||
}
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||
//设置超时时间为1秒,超过1秒则关闭连接
|
||
curl_setopt($ch,CURLOPT_TIMEOUT,1);
|
||
$tmp_sources = curl_exec($ch);
|
||
curl_close($ch);
|
||
return $tmp_sources;
|
||
}
|
||
//==================end 模拟post发数据
|
||
|
||
|
||
//AJAX 返回数据
|
||
function return_ajax($return_arr,$type=true){
|
||
if($type){//如果是不要求汉写转编码的话
|
||
return json_encode($return_arr,JSON_UNESCAPED_UNICODE);
|
||
}else{
|
||
return json_encode($return_arr);
|
||
}
|
||
}
|
||
|
||
|
||
/*ajax分页函数*/
|
||
function getPage($sum ,$url,$curpage = 1,$perpage = 5,$numberpage = 5){
|
||
$totalpage=ceil($sum/$perpage);
|
||
$pevpage=$curpage-1;
|
||
$nextpage=$curpage+1;
|
||
|
||
if($curpage > 1){
|
||
$page['fri']= $pevpage;
|
||
$tol2 = $pevpage;
|
||
}else{
|
||
$page['fri']= '1';
|
||
$tol2 = 1;
|
||
}
|
||
if($curpage < $totalpage){
|
||
$page['two']= $nextpage;
|
||
$tol2 = $nextpage-1;
|
||
}else{
|
||
$page['two']= $totalpage;
|
||
$tol2 = $totalpage;
|
||
}
|
||
$page["tol"] .= $tol2.'/'.$totalpage;
|
||
return $page;
|
||
}
|
||
|
||
function https_request($url,$data = null){
|
||
$curl = curl_init();
|
||
curl_setopt($curl, CURLOPT_URL, $url);
|
||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
|
||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
|
||
if (!empty($data)){
|
||
curl_setopt($curl, CURLOPT_POST, 1);
|
||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
|
||
}
|
||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
||
$output = curl_exec($curl);
|
||
curl_close($curl);
|
||
return $output;
|
||
}
|
||
|
||
//模版发送微信消息
|
||
function send_tmpinfo( $openid , $data , $appid , $secret, $url, $tmplmsg){ //用户id 发送的数据 appid secretid
|
||
$token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
|
||
$json = https_request( $token_url );
|
||
$result = json_decode( $json );
|
||
$acc_token = $result -> access_token;
|
||
$send_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$acc_token;
|
||
if($url){
|
||
$template_msg = array( 'touser' => $openid , 'template_id' => $tmplmsg , 'url'=>$url , 'topcolor' => '#000000' , 'data'=> $data );
|
||
}else{
|
||
$template_msg = array( 'touser' => $openid , 'template_id' => $tmplmsg , 'topcolor' => '#000000' , 'data'=> $data );
|
||
}
|
||
$template_data = urldecode( json_encode( $template_msg ) );
|
||
$temp_resutl = https_request( $send_url , $template_data );
|
||
$temp_data = json_decode($temp_resutl);
|
||
return $temp_data;
|
||
}
|
||
|
||
//判断是否微信进入
|
||
function is_weixin(){
|
||
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
//去掉微信分享时URL里多出来的reuserid=XXX的值
|
||
function get_wx_back_url($state){
|
||
$my_recommend = '';
|
||
if(strstr($state,'reuserid')){
|
||
$tmp_state = explode('?',$state);
|
||
if(isset($tmp_state[1]) && $tmp_state[1]!=''){
|
||
$tmp_url_arr = explode('&',$tmp_state[1]);
|
||
foreach($tmp_url_arr as $val){
|
||
if($val!='' && strstr($val,'=')){
|
||
$tmp_val_arr = explode('=',$val);
|
||
if((isset($tmp_val_arr[0]) && $tmp_val_arr[0] == 'reuserid')
|
||
&& ( isset($tmp_val_arr[1]) && $tmp_val_arr[1]!='')){
|
||
$my_recommend = 'reuserid='.intval($tmp_val_arr[1]);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
if($my_recommend!=''){
|
||
$state = str_replace('&'.$my_recommend,'',$state);
|
||
$state = str_replace($my_recommend,'',$state);
|
||
|
||
}
|
||
return $state;
|
||
}
|
||
|
||
//获取推荐人id
|
||
function get_recommend($state){
|
||
if(strstr($state,'reuserid')){
|
||
$tmp_state = explode('?',$state);
|
||
if(isset($tmp_state[1]) && $tmp_state[1]!=''){
|
||
$tmp_url_arr = explode('&',$tmp_state[1]);
|
||
foreach($tmp_url_arr as $val){
|
||
if($val!='' && strstr($val,'=')){
|
||
$tmp_val_arr = explode('=',$val);
|
||
if((isset($tmp_val_arr[0]) && $tmp_val_arr[0] == 'reuserid')
|
||
&& ( isset($tmp_val_arr[1]) && $tmp_val_arr[1]!='')){
|
||
return $my_recommend = intval($tmp_val_arr[1]);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
|
||
//===================================================================
|
||
//=============begin 身份证
|
||
/**
|
||
* 判断身份证号码的正确性
|
||
*
|
||
* $cardnum 身份证号码
|
||
* $flag 2格式错误 3地区错误 4 校验码错误 5生日错误 6格式正确成年人 7格式正确未成年人
|
||
**/
|
||
function checkcardnum($cardnum){
|
||
$aCity = array(11=>"北京",12=>"天津",13=>"河北",14=>"山西",15=>"内蒙古",
|
||
21=>"辽宁",22=>"吉林",23=>"黑龙江",
|
||
31=>"上海",32=>"江苏",33=>"浙江",34=>"安徽",35=>"福建",36=>"江西",37=>"山东",
|
||
41=>"河南",42=>"湖北",43=>"湖南",44=>"广东",45=>"广西",46=>"海南",
|
||
50=>"重庆",51=>"四川",52=>"贵州",53=>"云南",54=>"西藏",
|
||
61=>"陕西",62=>"甘肃",63=>"青海",64=>"宁夏",65=>"新疆",
|
||
71=>"台湾",81=>"香港",82=>"澳门",
|
||
91=>"国外");
|
||
//先对格式验证
|
||
if(!preg_match('/^([\d]{15}|[\d]{18}|[\d]{17}x)$/i',$cardnum)){
|
||
$flag = 2; //格式错误
|
||
return $flag;
|
||
}else{
|
||
//把15位的转化成18位
|
||
if(strlen($cardnum)==15){
|
||
$cardnum = idcard_15to18($cardnum);
|
||
}
|
||
|
||
//验证地区
|
||
if(!array_key_exists(substr($cardnum,0,2),$aCity)){
|
||
$flag = 3; //地区错误
|
||
return $flag;
|
||
}
|
||
//校验码有效性检查
|
||
if (idcard_verify_number($cardnum) != strtoupper(substr($cardnum, 17, 1))){
|
||
$flag = 4; //校验码错误
|
||
return $flag;
|
||
}
|
||
//验证生日 18岁以下未成年人
|
||
if(!checkdate(substr($cardnum,10,2),substr($cardnum,12,2),substr($cardnum,6,4))){
|
||
$flag = 5; //生日错误
|
||
return $flag;
|
||
}
|
||
//判断是否是成年人
|
||
if(checkadult($cardnum)){
|
||
$flag = 6;//格式正确成年人
|
||
return $flag;
|
||
}else{
|
||
$flag = 7;//格式正确未成年人
|
||
return $flag;
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
/***************
|
||
* 简单判断用户是否是成年人
|
||
*
|
||
* $cardnum 身份证号码(必须保证是18位的,15位的要先转换成18位)
|
||
* $flag 返回值 1成年人 0未成年人
|
||
****************/
|
||
function checkadult($cardnum){
|
||
$tyear=intval(substr($cardnum,6,4));
|
||
$tmonth=intval(substr($cardnum,10,2));
|
||
$tday=intval(substr($cardnum,12,2));
|
||
$yeardiff = intval(date("Y",THIS_DATETIME))-$tyear;
|
||
if($yeardiff == 18){//年满18
|
||
$monthdiff = intval(date("m",THIS_DATETIME))-$tmonth;
|
||
if($monthdiff > 0){//月份已满18
|
||
return 1;
|
||
}else if($monthdiff == 0){//如果月份刚好满18岁
|
||
$daydiff = intval(date("d",THIS_DATETIME))-$tday;
|
||
if($daydiff > -1){//日已满18
|
||
return 1;
|
||
}else{//日未满18
|
||
return 0;
|
||
}
|
||
}else{//月份未满18
|
||
return 0;
|
||
}
|
||
}else if($yeardiff > 18){//大于18
|
||
return 1;
|
||
}else{//未满18
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
// 计算身份证校验码,根据国家标准GB 11643-1999
|
||
function idcard_verify_number($idcard_base){
|
||
//加权因子
|
||
$factor=array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);
|
||
//校验码对应值
|
||
$verify_number_list=array('1','0','X','9','8','7','6','5','4','3','2');
|
||
$checksum=0;
|
||
for($i=0;$i<strlen($idcard_base);$i++){
|
||
$checksum += substr($idcard_base,$i,1)*$factor[$i];
|
||
}
|
||
$mod=strtoupper($checksum % 11);
|
||
$verify_number=$verify_number_list[$mod];
|
||
|
||
return $verify_number;
|
||
}
|
||
|
||
//将15位身份证升级到18位
|
||
function idcard_15to18($idcard){
|
||
if(strlen($idcard)!=15){
|
||
return false;
|
||
}else{
|
||
//如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码
|
||
if(array_search(substr($idcard,12,3),array('996','997','998','999'))!=false){
|
||
$idcard=substr($idcard,0,6).'18'.substr($idcard,6,9);
|
||
}else{
|
||
$idcard=substr($idcard,0,6).'19'.substr($idcard,6,9);
|
||
}
|
||
}
|
||
$idcard=$idcard.idcard_verify_number($idcard);
|
||
return $idcard;
|
||
}
|
||
|
||
/*************
|
||
*功能:通过身份证获取用户出生年月日
|
||
*@param string $cardnum 用户身份证号
|
||
*return array
|
||
************/
|
||
function get_user_birth($cardnum){
|
||
$year_month_day = array('m_birthyear'=>0,'m_birthmonth'=>0,'m_birthday'=>0);
|
||
if(checkcardnum($cardnum) != 6 && checkcardnum($cardnum) != 7){
|
||
return $year_month_day;
|
||
}else{
|
||
if(strlen($cardnum)==15){
|
||
$cardnum = idcard_15to18($cardnum);//15位转18位身份证号码
|
||
}
|
||
$year_month_day['m_birthyear'] = intval(substr($cardnum,6,4));
|
||
$year_month_day['m_birthmonth'] = intval(substr($cardnum,10,2));
|
||
$year_month_day['m_birthday'] = intval(substr($cardnum,12,2));
|
||
|
||
return $year_month_day;
|
||
}
|
||
}
|
||
//=============end 身份证
|
||
//===================================================================
|
||
|
||
|
||
|
||
|
||
|
||
//把用户输入的文本转义(主要针对特殊符号和emoji表情)
|
||
function emoji_encode($str){
|
||
if(empty($str)){
|
||
return $str;
|
||
}
|
||
$str = preg_replace_callback('/[\xf0-\xf7].{3}/', function($r) { return '@E' . base64_encode($r[0]);}, $str);
|
||
return $str;
|
||
}
|
||
|
||
//解码(主要针对特殊符号和emoji表情)
|
||
function emoji_decode($str){
|
||
if(empty($str)){
|
||
return $str;
|
||
}
|
||
$str = preg_replace_callback('/@E(.{6}==)/', function ($r) {
|
||
return base64_decode($r[1]);
|
||
}, $str);
|
||
return $str;
|
||
}
|
||
|
||
|
||
//二维数组按某个key进行排序
|
||
/*
|
||
* $arrays 要排序的二维数组
|
||
* $sort_key 二维数组中需要排序的key
|
||
* $orderby 排序方式(SORT_ASC ->按升序排列。(A-Z);SORT_DESC ->按降序排列。(Z-A))
|
||
* $datatype 需要排序的数据类型(SORT_NUMERIC ->将每一项按数字顺序排列;SORT_REGULAR ->将每一项按常规顺序排列。;SORT_STRING ->将每一项按字母顺序排列)
|
||
$person = array(
|
||
array('id'=>1,'name'=>'fdsfd','weight'=>100,'height'=>180),
|
||
array('id'=>2,'name'=>'erwrds','weight'=>53,'height'=>150),
|
||
array('id'=>3,'name'=>'jerry','weight'=>53,'height'=>156),
|
||
array('id'=>4,'name'=>'fdsafdf','weight'=>53,'height'=>151),
|
||
array('id'=>5,'name'=>'fdsatyu','weight'=>80,'height'=>200),
|
||
array('id'=>6,'name'=>'uyt6576','weight'=>95,'height'=>110),
|
||
array('id'=>7,'name'=>'gfdkljo','weight'=>70,'height'=>170)
|
||
);
|
||
$person = get_arrar_sort($person,'weight',SORT_DESC);
|
||
$person = get_arrar_sort($person,'height',SORT_DESC);
|
||
var_dump($person);
|
||
echo('<hr>');
|
||
*/
|
||
function get_arrar_sort($arrays,$sort_key,$orderby=SORT_ASC,$datatype=SORT_NUMERIC){
|
||
if(is_array($arrays)){
|
||
//把数组中要排序的key拿出来变为顺序的一个数级先
|
||
$key_arrays = array();
|
||
foreach ($arrays as $array){
|
||
if(!isset($array[$sort_key])){
|
||
return $arrays;
|
||
}
|
||
if(is_array($array)){
|
||
$key_arrays[] = $array[$sort_key];
|
||
}else{
|
||
return false;
|
||
}
|
||
}
|
||
}else{
|
||
return false;
|
||
}
|
||
//按指得的key进行排序
|
||
array_multisort($key_arrays,$orderby,$datatype,$arrays);
|
||
return $arrays;
|
||
}
|
||
|
||
//生成验证码功能
|
||
function get_code_img($myname='check_code'){
|
||
//生成验证码图片
|
||
Header("Content-type: image/PNG");
|
||
srand((double)microtime()*1000000);//播下一个生成随机数字的种子,以方便下面随机数生成的使用
|
||
//将随机数存入session中
|
||
$_SESSION['check_code']="";
|
||
$im = imagecreate(50,22); //制定图片背景大小
|
||
|
||
$black = ImageColorAllocate($im, 0,0,0); //设定三种颜色
|
||
$white = ImageColorAllocate($im, 255,255,255);
|
||
$gray = ImageColorAllocate($im, 255,255,255);
|
||
imagefill($im,0,0,$gray); //采用区域填充法,设定(0,0)
|
||
|
||
$authnum=rand(1000,9999);
|
||
//将四位整数验证码绘入图片
|
||
$_SESSION[$myname]=$authnum;
|
||
imagestring($im, 5, 10, 3, $authnum, $black);
|
||
// 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 座标处(图像的左上角为 0, 0)。
|
||
//如果 font 是 1,2,3,4 或 5,则使用内置字体
|
||
for($i=0;$i<200;$i++) //加入干扰象素
|
||
{
|
||
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
|
||
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
|
||
}
|
||
ImagePNG($im);
|
||
ImageDestroy($im);
|
||
}
|
||
|
||
//判断客户端的操作系统
|
||
function check_tel_sys($str){
|
||
if(strripos($str,"Android")!==false){//如果是安桌手机
|
||
return 1;
|
||
}else if(strripos($str,"ios")!==false){//如果是苹果手机
|
||
return 2;
|
||
}else{//如果是其它手机
|
||
return 3;
|
||
}
|
||
}
|
||
|
||
|