分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

【discuzX2】/source/function/function_core.php通用核心函数库文件分析

发布时间:2023-09-06 01:09责任编辑:傅花花关键词:暂无标签
[php]view plaincopyprint?
  1. <?php
  2. /**
  3. *[Discuz!](C)2001-2099ComsenzInc.
  4. *ThisisNOTafreeware,useissubjecttolicenseterms
  5. *
  6. *$Id:function_core.php288902012-03-1902:05:42Zliudongdong$
  7. */
  8. if(!defined(‘IN_DISCUZ‘)){
  9. exit(‘AccessDenied‘);
  10. }
  11. //通用函数集合
  12. define(‘DISCUZ_CORE_FUNCTION‘,true);
  13. /**
  14. *系统错误处理
  15. *@param<type>$message错误信息
  16. *@param<type>$show是否显示信息
  17. *@param<type>$save是否存入日志
  18. *@param<type>$halt是否中断访问
  19. */
  20. functionsystem_error($message,$show=true,$save=true,$halt=true){
  21. require_oncelibfile(‘class/error‘);
  22. discuz_error::system_error($message,$show,$save,$halt);
  23. }
  24. /**
  25. *更新session
  26. *@global<type>$_G
  27. *@staticvarboolean$updated
  28. *@paramboolean$force
  29. *@returnboolean
  30. */
  31. functionupdatesession($force=false){
  32. global$_G;
  33. static$updated=false;
  34. if(!$updated){
  35. if($_G[‘uid‘]){
  36. if($_G[‘cookie‘][‘ulastactivity‘]){
  37. $ulastactivity=authcode($_G[‘cookie‘][‘ulastactivity‘],‘DECODE‘);
  38. }else{
  39. $ulastactivity=getuserprofile(‘lastactivity‘);
  40. dsetcookie(‘ulastactivity‘,authcode($ulastactivity,‘ENCODE‘),31536000);
  41. }
  42. }
  43. $discuz=&discuz_core::instance();
  44. //note更新在线时间
  45. $oltimespan=$_G[‘setting‘][‘oltimespan‘];
  46. $lastolupdate=$discuz->session->var[‘lastolupdate‘];
  47. if($_G[‘uid‘]&&$oltimespan&&TIMESTAMP-($lastolupdate?$lastolupdate:$ulastactivity)>$oltimespan*60){
  48. DB::query("UPDATE".DB::table(‘common_onlinetime‘)."
  49. SETtotal=total+‘$oltimespan‘,thismonth=thismonth+‘$oltimespan‘,lastupdate=‘".TIMESTAMP."‘
  50. WHEREuid=‘{$_G[‘uid‘]}‘");
  51. if(!DB::affected_rows()){
  52. DB::insert(‘common_onlinetime‘,array(
  53. ‘uid‘=>$_G[‘uid‘],
  54. ‘thismonth‘=>$oltimespan,
  55. ‘total‘=>$oltimespan,
  56. ‘lastupdate‘=>TIMESTAMP,
  57. ));
  58. }
  59. $discuz->session->set(‘lastolupdate‘,TIMESTAMP);
  60. }
  61. foreach($discuz->session->varas$k=>$v){
  62. if(isset($_G[‘member‘][$k])&&$k!=‘lastactivity‘){
  63. $discuz->session->set($k,$_G[‘member‘][$k]);
  64. }
  65. }
  66. foreach($_G[‘action‘]as$k=>$v){
  67. $discuz->session->set($k,$v);
  68. }
  69. $discuz->session->update();
  70. $updated=true;
  71. if($_G[‘uid‘]&&TIMESTAMP-$ulastactivity>21600){
  72. if($oltimespan&&TIMESTAMP-$ulastactivity>43200){
  73. $total=DB::result_first("SELECTtotalFROM".DB::table(‘common_onlinetime‘)."WHEREuid=‘$_G[uid]‘");
  74. DB::update(‘common_member_count‘,array(‘oltime‘=>round(intval($total)/60)),"uid=‘$_G[uid]‘",1);
  75. }
  76. dsetcookie(‘ulastactivity‘,authcode(TIMESTAMP,‘ENCODE‘),31536000);
  77. DB::update(‘common_member_status‘,array(‘lastip‘=>$_G[‘clientip‘],‘lastactivity‘=>TIMESTAMP,‘lastvisit‘=>TIMESTAMP),"uid=‘$_G[uid]‘",1);
  78. }
  79. }
  80. return$updated;
  81. }
  82. /**
  83. *获取microtimefloat数值,为了兼容php4
  84. *@return<float>
  85. */
  86. functiondmicrotime(){
  87. returnarray_sum(explode(‘‘,microtime()));
  88. }
  89. /**
  90. *设置全局$_G中的变量
  91. *@global<array>$_G
  92. *@param<string>$key键
  93. *@param<string>$value值
  94. *@param<mix>$group组(准备废弃,尽量不用)
  95. *@returntrue
  96. *
  97. *@example
  98. *setglobal(‘test‘,1);//$_G[‘test‘]=1;
  99. *setglobal(‘config/test/abc‘)=2;//$_G[‘config‘][‘test‘][‘abc‘]=2;
  100. *
  101. */
  102. functionsetglobal($key,$value,$group=null){
  103. global$_G;
  104. $k=explode(‘/‘,$group===null?$key:$group.‘/‘.$key);
  105. switch(count($k)){
  106. case1:$_G[$k[0]]=$value;break;
  107. case2:$_G[$k[0]][$k[1]]=$value;break;
  108. case3:$_G[$k[0]][$k[1]][$k[2]]=$value;break;
  109. case4:$_G[$k[0]][$k[1]][$k[2]][$k[3]]=$value;break;
  110. case5:$_G[$k[0]][$k[1]][$k[2]][$k[3]][$k[4]]=$value;break;
  111. }
  112. returntrue;
  113. }
  114. /**
  115. *获取全局变量$_G当中的某个数值
  116. *@global$_G
  117. *@param<type>$key
  118. *@param<type>$group计划废弃的参数,不建议使用
  119. *@return<mix>
  120. *
  121. *$v=getglobal(‘test‘);//$v=$_G[‘test‘]
  122. *$v=getglobal(‘test/hello/ok‘);//$v=$_G[‘test‘][‘hello‘][‘ok‘]
  123. */
  124. functiongetglobal($key,$group=null){
  125. global$_G;
  126. $k=explode(‘/‘,$group===null?$key:$group.‘/‘.$key);
  127. switch(count($k)){
  128. case1:returnisset($_G[$k[0]])?$_G[$k[0]]:null;break;
  129. case2:returnisset($_G[$k[0]][$k[1]])?$_G[$k[0]][$k[1]]:null;break;
  130. case3:returnisset($_G[$k[0]][$k[1]][$k[2]])?$_G[$k[0]][$k[1]][$k[2]]:null;break;
  131. case4:returnisset($_G[$k[0]][$k[1]][$k[2]][$k[3]])?$_G[$k[0]][$k[1]][$k[2]][$k[3]]:null;break;
  132. 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;
  133. }
  134. returnnull;
  135. }
  136. /**
  137. *取出get,post,cookie当中的某个变量
  138. *
  139. *@paramstring$kkey值
  140. *@paramstring$type类型
  141. *@returnmix
  142. */
  143. functiongetgpc($k,$type=‘GP‘){
  144. $type=strtoupper($type);
  145. switch($type){
  146. case‘G‘:$var=&$_GET;break;
  147. case‘P‘:$var=&$_POST;break;
  148. case‘C‘:$var=&$_COOKIE;break;
  149. default:
  150. if(isset($_GET[$k])){
  151. $var=&$_GET;
  152. }else{
  153. $var=&$_POST;
  154. }
  155. break;
  156. }
  157. returnisset($var[$k])?$var[$k]:NULL;
  158. }
  159. /**
  160. *根据uid获取用户基本数据
  161. *@staticvararray$users存放已经获取的用户的信息,避免重复查库
  162. *@param<int>$uid
  163. *@return<array>
  164. */
  165. functiongetuserbyuid($uid){
  166. static$users=array();
  167. if(empty($users[$uid])){
  168. $users[$uid]=DB::fetch_first("SELECT*FROM".DB::table(‘common_member‘)."WHEREuid=‘$uid‘");
  169. }
  170. return$users[$uid];
  171. }
  172. /**
  173. *获取当前用户的扩展资料
  174. *@param$field字段
  175. */
  176. functiongetuserprofile($field){
  177. global$_G;
  178. if(isset($_G[‘member‘][$field])){
  179. return$_G[‘member‘][$field];
  180. }
  181. static$tablefields=array(
  182. ‘count‘=>array(‘extcredits1‘,‘extcredits2‘,‘extcredits3‘,‘extcredits4‘,‘extcredits5‘,‘extcredits6‘,‘extcredits7‘,‘extcredits8‘,‘friends‘,‘posts‘,‘threads‘,‘digestposts‘,‘doings‘,‘blogs‘,‘albums‘,‘sharings‘,‘attachsize‘,‘views‘,‘oltime‘,‘todayattachs‘,‘todayattachsize‘),
  183. ‘status‘=>array(‘regip‘,‘lastip‘,‘lastvisit‘,‘lastactivity‘,‘lastpost‘,‘lastsendmail‘,‘invisible‘,‘buyercredit‘,‘sellercredit‘,‘favtimes‘,‘sharetimes‘,‘profileprogress‘),
  184. ‘field_forum‘=>array(‘publishfeed‘,‘customshow‘,‘customstatus‘,‘medals‘,‘sightml‘,‘groupterms‘,‘authstr‘,‘groups‘,‘attentiongroup‘),
  185. ‘field_home‘=>array(‘videophoto‘,‘spacename‘,‘spacedescription‘,‘domain‘,‘addsize‘,‘addfriend‘,‘menunum‘,‘theme‘,‘spacecss‘,‘blockposition‘,‘recentnote‘,‘spacenote‘,‘privacy‘,‘feedfriend‘,‘acceptemail‘,‘magicgift‘,‘stickblogs‘),
  186. ‘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‘),
  187. ‘verify‘=>array(‘verify1‘,‘verify2‘,‘verify3‘,‘verify4‘,‘verify5‘,‘verify6‘,‘verify7‘),
  188. );
  189. $profiletable=‘‘;
  190. foreach($tablefieldsas$table=>$fields){
  191. if(in_array($field,$fields)){
  192. $profiletable=$table;
  193. break;
  194. }
  195. }
  196. if($profiletable){
  197. $data=array();
  198. if($_G[‘uid‘]){
  199. $data=DB::fetch_first("SELECT".implode(‘,‘,$tablefields[$profiletable])."FROM".DB::table(‘common_member_‘.$profiletable)."WHEREuid=‘$_G[uid]‘");
  200. }
  201. if(!$data){
  202. foreach($tablefields[$profiletable]as$k){
  203. $data[$k]=‘‘;
  204. }
  205. }
  206. $_G[‘member‘]=array_merge(is_array($_G[‘member‘])?$_G[‘member‘]:array(),$data);
  207. return$_G[‘member‘][$field];
  208. }
  209. }
  210. /**
  211. *对字符串或者输入进行addslashes操作
  212. *@param<mix>$string
  213. *@param<int>$force
  214. *@return<mix>
  215. */
  216. functiondaddslashes($string,$force=1){
  217. if(is_array($string)){
  218. $keys=array_keys($string);
  219. foreach($keysas$key){
  220. $val=$string[$key];
  221. unset($string[$key]);
  222. $string[addslashes($key)]=daddslashes($val,$force);
  223. }
  224. }else{
  225. $string=addslashes($string);
  226. }
  227. return$string;
  228. }
  229. /**
  230. *对字符串进行加密和解密
  231. *@param<string>$string
  232. *@param<string>$operationDECODE解密|ENCODE加密
  233. *@param<string>$key当为空的时候,取全局密钥
  234. *@param<int>$expiry有效期,单位秒
  235. *@return<string>
  236. */
  237. functionauthcode($string,$operation=‘DECODE‘,$key=‘‘,$expiry=0){
  238. $ckey_length=4;
  239. $key=md5($key!=‘‘?$key:getglobal(‘authkey‘));
  240. $keya=md5(substr($key,0,16));
  241. $keyb=md5(substr($key,16,16));
  242. $keyc=$ckey_length?($operation==‘DECODE‘?substr($string,0,$ckey_length):substr(md5(microtime()),-$ckey_length)):‘‘;
  243. $cryptkey=$keya.md5($keya.$keyc);
  244. $key_length=strlen($cryptkey);
  245. $string=$operation==‘DECODE‘?base64_decode(substr($string,$ckey_length)):sprintf(‘%010d‘,$expiry?$expiry+time():0).substr(md5($string.$keyb),0,16).$string;
  246. $string_length=strlen($string);
  247. $result=‘‘;
  248. $box=range(0,255);
  249. $rndkey=array();
  250. for($i=0;$i<=255;$i++){
  251. $rndkey[$i]=ord($cryptkey[$i%$key_length]);
  252. }
  253. for($j=$i=0;$i<256;$i++){
  254. $j=($j+$box[$i]+$rndkey[$i])%256;
  255. $tmp=$box[$i];
  256. $box[$i]=$box[$j];
  257. $box[$j]=$tmp;
  258. }
  259. for($a=$j=$i=0;$i<$string_length;$i++){
  260. $a=($a+1)%256;
  261. $j=($j+$box[$a])%256;
  262. $tmp=$box[$a];
  263. $box[$a]=$box[$j];
  264. $box[$j]=$tmp;
  265. $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256]));
  266. }
  267. if($operation==‘DECODE‘){
  268. if((substr($result,0,10)==0||substr($result,0,10)-time()>0)&&substr($result,10,16)==substr(md5(substr($result,26).$keyb),0,16)){
  269. returnsubstr($result,26);
  270. }else{
  271. return‘‘;
  272. }
  273. }else{
  274. return$keyc.str_replace(‘=‘,‘‘,base64_encode($result));
  275. }
  276. }
  277. functionfsocketopen($hostname,$port=80,&$errno,&$errstr,$timeout=15){
  278. $fp=‘‘;
  279. if(function_exists(‘fsockopen‘)){
  280. $fp=@fsockopen($hostname,$port,$errno,$errstr,$timeout);
  281. }elseif(function_exists(‘pfsockopen‘)){
  282. $fp=@pfsockopen($hostname,$port,$errno,$errstr,$timeout);
  283. }elseif(function_exists(‘stream_socket_client‘)){
  284. //notephp5支持
  285. $fp=@stream_socket_client($hostname.‘:‘.$port,$errno,$errstr,$timeout);
  286. }
  287. return$fp;
  288. }
  289. /**
  290. *远程文件文件请求兼容函数
  291. */
  292. functiondfsockopen($url,$limit=0,$post=‘‘,$cookie=‘‘,$bysocket=FALSE,$ip=‘‘,$timeout=15,$block=TRUE){
  293. require_oncelibfile(‘function/filesock‘);
  294. return_dfsockopen($url,$limit,$post,$cookie,$bysocket,$ip,$timeout,$block);
  295. }
  296. /**
  297. *HTML转义字符
  298. *@param$string-字符串
  299. *@return返回转义好的字符串
  300. */
  301. functiondhtmlspecialchars($string){
  302. if(is_array($string)){
  303. foreach($stringas$key=>$val){
  304. $string[$key]=dhtmlspecialchars($val);
  305. }
  306. }else{
  307. $string=s

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved