[php]view plaincopyprint?
- <?php
- /**
- *[Discuz!](C)2001-2099ComsenzInc.
- *ThisisNOTafreeware,useissubjecttolicenseterms
- *
- *$Id:function_core.php288902012-03-1902:05:42Zliudongdong$
- */
- if(!defined(‘IN_DISCUZ‘)){
- exit(‘AccessDenied‘);
- }
- //通用函数集合
- define(‘DISCUZ_CORE_FUNCTION‘,true);
- /**
- *系统错误处理
- *@param<type>$message错误信息
- *@param<type>$show是否显示信息
- *@param<type>$save是否存入日志
- *@param<type>$halt是否中断访问
- */
- functionsystem_error($message,$show=true,$save=true,$halt=true){
- require_oncelibfile(‘class/error‘);
- discuz_error::system_error($message,$show,$save,$halt);
- }
- /**
- *更新session
- *@global<type>$_G
- *@staticvarboolean$updated
- *@paramboolean$force
- *@returnboolean
- */
- functionupdatesession($force=false){
- global$_G;
- static$updated=false;
- if(!$updated){
- if($_G[‘uid‘]){
- if($_G[‘cookie‘][‘ulastactivity‘]){
- $ulastactivity=authcode($_G[‘cookie‘][‘ulastactivity‘],‘DECODE‘);
- }else{
- $ulastactivity=getuserprofile(‘lastactivity‘);
- dsetcookie(‘ulastactivity‘,authcode($ulastactivity,‘ENCODE‘),31536000);
- }
- }
- $discuz=&discuz_core::instance();
- //note更新在线时间
- $oltimespan=$_G[‘setting‘][‘oltimespan‘];
- $lastolupdate=$discuz->session->var[‘lastolupdate‘];
- if($_G[‘uid‘]&&$oltimespan&&TIMESTAMP-($lastolupdate?$lastolupdate:$ulastactivity)>$oltimespan*60){
- DB::query("UPDATE".DB::table(‘common_onlinetime‘)."
- SETtotal=total+‘$oltimespan‘,thismonth=thismonth+‘$oltimespan‘,lastupdate=‘".TIMESTAMP."‘
- WHEREuid=‘{$_G[‘uid‘]}‘");
- if(!DB::affected_rows()){
- DB::insert(‘common_onlinetime‘,array(
- ‘uid‘=>$_G[‘uid‘],
- ‘thismonth‘=>$oltimespan,
- ‘total‘=>$oltimespan,
- ‘lastupdate‘=>TIMESTAMP,
- ));
- }
- $discuz->session->set(‘lastolupdate‘,TIMESTAMP);
- }
- foreach($discuz->session->varas$k=>$v){
- if(isset($_G[‘member‘][$k])&&$k!=‘lastactivity‘){
- $discuz->session->set($k,$_G[‘member‘][$k]);
- }
- }
- foreach($_G[‘action‘]as$k=>$v){
- $discuz->session->set($k,$v);
- }
- $discuz->session->update();
- $updated=true;
- if($_G[‘uid‘]&&TIMESTAMP-$ulastactivity>21600){
- if($oltimespan&&TIMESTAMP-$ulastactivity>43200){
- $total=DB::result_first("SELECTtotalFROM".DB::table(‘common_onlinetime‘)."WHEREuid=‘$_G[uid]‘");
- DB::update(‘common_member_count‘,array(‘oltime‘=>round(intval($total)/60)),"uid=‘$_G[uid]‘",1);
- }
- dsetcookie(‘ulastactivity‘,authcode(TIMESTAMP,‘ENCODE‘),31536000);
- DB::update(‘common_member_status‘,array(‘lastip‘=>$_G[‘clientip‘],‘lastactivity‘=>TIMESTAMP,‘lastvisit‘=>TIMESTAMP),"uid=‘$_G[uid]‘",1);
- }
- }
- return$updated;
- }
- /**
- *获取microtimefloat数值,为了兼容php4
- *@return<float>
- */
- functiondmicrotime(){
- returnarray_sum(explode(‘‘,microtime()));
- }
- /**
- *设置全局$_G中的变量
- *@global<array>$_G
- *@param<string>$key键
- *@param<string>$value值
- *@param<mix>$group组(准备废弃,尽量不用)
- *@returntrue
- *
- *@example
- *setglobal(‘test‘,1);//$_G[‘test‘]=1;
- *setglobal(‘config/test/abc‘)=2;//$_G[‘config‘][‘test‘][‘abc‘]=2;
- *
- */
- functionsetglobal($key,$value,$group=null){
- global$_G;
- $k=explode(‘/‘,$group===null?$key:$group.‘/‘.$key);
- switch(count($k)){
- case1:$_G[$k[0]]=$value;break;
- case2:$_G[$k[0]][$k[1]]=$value;break;
- case3:$_G[$k[0]][$k[1]][$k[2]]=$value;break;
- case4:$_G[$k[0]][$k[1]][$k[2]][$k[3]]=$value;break;
- case5:$_G[$k[0]][$k[1]][$k[2]][$k[3]][$k[4]]=$value;break;
- }
- returntrue;
- }
- /**
- *获取全局变量$_G当中的某个数值
- *@global$_G
- *@param<type>$key
- *@param<type>$group计划废弃的参数,不建议使用
- *@return<mix>
- *
- *$v=getglobal(‘test‘);//$v=$_G[‘test‘]
- *$v=getglobal(‘test/hello/ok‘);//$v=$_G[‘test‘][‘hello‘][‘ok‘]
- */
- functiongetglobal($key,$group=null){
- global$_G;
- $k=explode(‘/‘,$group===null?$key:$group.‘/‘.$key);
- switch(count($k)){
- case1:returnisset($_G[$k[0]])?$_G[$k[0]]:null;break;
- case2:returnisset($_G[$k[0]][$k[1]])?$_G[$k[0]][$k[1]]:null;break;
- case3:returnisset($_G[$k[0]][$k[1]][$k[2]])?$_G[$k[0]][$k[1]][$k[2]]:null;break;
- case4:returnisset($_G[$k[0]][$k[1]][$k[2]][$k[3]])?$_G[$k[0]][$k[1]][$k[2]][$k[3]]:null;break;
- case5:returnisset($_G[$k[0]][$k[1]][$k[2]][$k[3]][$k[4]])?$_G[$k[0]][$k[1]][$k[2]][$k[3]][$k[4]]:null;break;
- }
- returnnull;
- }
- /**
- *取出get,post,cookie当中的某个变量
- *
- *@paramstring$kkey值
- *@paramstring$type类型
- *@returnmix
- */
- functiongetgpc($k,$type=‘GP‘){
- $type=strtoupper($type);
- switch($type){
- case‘G‘:$var=&$_GET;break;
- case‘P‘:$var=&$_POST;break;
- case‘C‘:$var=&$_COOKIE;break;
- default:
- if(isset($_GET[$k])){
- $var=&$_GET;
- }else{
- $var=&$_POST;
- }
- break;
- }
- returnisset($var[$k])?$var[$k]:NULL;
- }
- /**
- *根据uid获取用户基本数据
- *@staticvararray$users存放已经获取的用户的信息,避免重复查库
- *@param<int>$uid
- *@return<array>
- */
- functiongetuserbyuid($uid){
- static$users=array();
- if(empty($users[$uid])){
- $users[$uid]=DB::fetch_first("SELECT*FROM".DB::table(‘common_member‘)."WHEREuid=‘$uid‘");
- }
- return$users[$uid];
- }
- /**
- *获取当前用户的扩展资料
- *@param$field字段
- */
- functiongetuserprofile($field){
- global$_G;
- if(isset($_G[‘member‘][$field])){
- return$_G[‘member‘][$field];
- }
- static$tablefields=array(
- ‘count‘=>array(‘extcredits1‘,‘extcredits2‘,‘extcredits3‘,‘extcredits4‘,‘extcredits5‘,‘extcredits6‘,‘extcredits7‘,‘extcredits8‘,‘friends‘,‘posts‘,‘threads‘,‘digestposts‘,‘doings‘,‘blogs‘,‘albums‘,‘sharings‘,‘attachsize‘,‘views‘,‘oltime‘,‘todayattachs‘,‘todayattachsize‘),
- ‘status‘=>array(‘regip‘,‘lastip‘,‘lastvisit‘,‘lastactivity‘,‘lastpost‘,‘lastsendmail‘,‘invisible‘,‘buyercredit‘,‘sellercredit‘,‘favtimes‘,‘sharetimes‘,‘profileprogress‘),
- ‘field_forum‘=>array(‘publishfeed‘,‘customshow‘,‘customstatus‘,‘medals‘,‘sightml‘,‘groupterms‘,‘authstr‘,‘groups‘,‘attentiongroup‘),
- ‘field_home‘=>array(‘videophoto‘,‘spacename‘,‘spacedescription‘,‘domain‘,‘addsize‘,‘addfriend‘,‘menunum‘,‘theme‘,‘spacecss‘,‘blockposition‘,‘recentnote‘,‘spacenote‘,‘privacy‘,‘feedfriend‘,‘acceptemail‘,‘magicgift‘,‘stickblogs‘),
- ‘profile‘=>array(‘realname‘,‘gender‘,‘birthyear‘,‘birthmonth‘,‘birthday‘,‘constellation‘,‘zodiac‘,‘telephone‘,‘mobile‘,‘idcardtype‘,‘idcard‘,‘address‘,‘zipcode‘,‘nationality‘,‘birthprovince‘,‘birthcity‘,‘resideprovince‘,‘residecity‘,‘residedist‘,‘residecommunity‘,‘residesuite‘,‘graduateschool‘,‘company‘,‘education‘,‘occupation‘,‘position‘,‘revenue‘,‘affectivestatus‘,‘lookingfor‘,‘bloodtype‘,‘height‘,‘weight‘,‘alipay‘,‘icq‘,‘qq‘,‘yahoo‘,‘msn‘,‘taobao‘,‘site‘,‘bio‘,‘interest‘,‘field1‘,‘field2‘,‘field3‘,‘field4‘,‘field5‘,‘field6‘,‘field7‘,‘field8‘),
- ‘verify‘=>array(‘verify1‘,‘verify2‘,‘verify3‘,‘verify4‘,‘verify5‘,‘verify6‘,‘verify7‘),
- );
- $profiletable=‘‘;
- foreach($tablefieldsas$table=>$fields){
- if(in_array($field,$fields)){
- $profiletable=$table;
- break;
- }
- }
- if($profiletable){
- $data=array();
- if($_G[‘uid‘]){
- $data=DB::fetch_first("SELECT".implode(‘,‘,$tablefields[$profiletable])."FROM".DB::table(‘common_member_‘.$profiletable)."WHEREuid=‘$_G[uid]‘");
- }
- if(!$data){
- foreach($tablefields[$profiletable]as$k){
- $data[$k]=‘‘;
- }
- }
- $_G[‘member‘]=array_merge(is_array($_G[‘member‘])?$_G[‘member‘]:array(),$data);
- return$_G[‘member‘][$field];
- }
- }
- /**
- *对字符串或者输入进行addslashes操作
- *@param<mix>$string
- *@param<int>$force
- *@return<mix>
- */
- functiondaddslashes($string,$force=1){
- if(is_array($string)){
- $keys=array_keys($string);
- foreach($keysas$key){
- $val=$string[$key];
- unset($string[$key]);
- $string[addslashes($key)]=daddslashes($val,$force);
- }
- }else{
- $string=addslashes($string);
- }
- return$string;
- }
- /**
- *对字符串进行加密和解密
- *@param<string>$string
- *@param<string>$operationDECODE解密|ENCODE加密
- *@param<string>$key当为空的时候,取全局密钥
- *@param<int>$expiry有效期,单位秒
- *@return<string>
- */
- functionauthcode($string,$operation=‘DECODE‘,$key=‘‘,$expiry=0){
- $ckey_length=4;
- $key=md5($key!=‘‘?$key:getglobal(‘authkey‘));
- $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)){
- returnsubstr($result,26);
- }else{
- return‘‘;
- }
- }else{
- return$keyc.str_replace(‘=‘,‘‘,base64_encode($result));
- }
- }
- functionfsocketopen($hostname,$port=80,&$errno,&$errstr,$timeout=15){
- $fp=‘‘;
- if(function_exists(‘fsockopen‘)){
- $fp=@fsockopen($hostname,$port,$errno,$errstr,$timeout);
- }elseif(function_exists(‘pfsockopen‘)){
- $fp=@pfsockopen($hostname,$port,$errno,$errstr,$timeout);
- }elseif(function_exists(‘stream_socket_client‘)){
- //notephp5支持
- $fp=@stream_socket_client($hostname.‘:‘.$port,$errno,$errstr,$timeout);
- }
- return$fp;
- }
- /**
- *远程文件文件请求兼容函数
- */
- functiondfsockopen($url,$limit=0,$post=‘‘,$cookie=‘‘,$bysocket=FALSE,$ip=‘‘,$timeout=15,$block=TRUE){
- require_oncelibfile(‘function/filesock‘);
- return_dfsockopen($url,$limit,$post,$cookie,$bysocket,$ip,$timeout,$block);
- }
- /**
- *HTML转义字符
- *@param$string-字符串
- *@return返回转义好的字符串
- */
- functiondhtmlspecialchars($string){
- if(is_array($string)){
- foreach($stringas$key=>$val){
- $string[$key]=dhtmlspecialchars($val);
- }
- }else{
- $string=s