PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/source/function/function_core.php

https://github.com/jinbo51/DiscuzX
PHP | 2070 lines | 1833 code | 231 blank | 6 comment | 609 complexity | 67120abb31739bbbf3f90e0d4abda70b MD5 | raw file
Possible License(s): BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: function_core.php 33364 2013-06-03 02:30:46Z andyzheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. define('DISCUZ_CORE_FUNCTION', true);
  12. function system_error($message, $show = true, $save = true, $halt = true) {
  13. discuz_error::system_error($message, $show, $save, $halt);
  14. }
  15. function updatesession() {
  16. return C::app()->session->updatesession();
  17. }
  18. function setglobal($key , $value, $group = null) {
  19. global $_G;
  20. $key = explode('/', $group === null ? $key : $group.'/'.$key);
  21. $p = &$_G;
  22. foreach ($key as $k) {
  23. if(!isset($p[$k]) || !is_array($p[$k])) {
  24. $p[$k] = array();
  25. }
  26. $p = &$p[$k];
  27. }
  28. $p = $value;
  29. return true;
  30. }
  31. function getglobal($key, $group = null) {
  32. global $_G;
  33. $key = explode('/', $group === null ? $key : $group.'/'.$key);
  34. $v = &$_G;
  35. foreach ($key as $k) {
  36. if (!isset($v[$k])) {
  37. return null;
  38. }
  39. $v = &$v[$k];
  40. }
  41. return $v;
  42. }
  43. function getgpc($k, $type='GP') {
  44. $type = strtoupper($type);
  45. switch($type) {
  46. case 'G': $var = &$_GET; break;
  47. case 'P': $var = &$_POST; break;
  48. case 'C': $var = &$_COOKIE; break;
  49. default:
  50. if(isset($_GET[$k])) {
  51. $var = &$_GET;
  52. } else {
  53. $var = &$_POST;
  54. }
  55. break;
  56. }
  57. return isset($var[$k]) ? $var[$k] : NULL;
  58. }
  59. function getuserbyuid($uid, $fetch_archive = 0) {
  60. static $users = array();
  61. if(empty($users[$uid])) {
  62. $users[$uid] = C::t('common_member'.($fetch_archive === 2 ? '_archive' : ''))->fetch($uid);
  63. if($fetch_archive === 1 && empty($users[$uid])) {
  64. $users[$uid] = C::t('common_member_archive')->fetch($uid);
  65. }
  66. }
  67. if(!isset($users[$uid]['self']) && $uid == getglobal('uid') && getglobal('uid')) {
  68. $users[$uid]['self'] = 1;
  69. }
  70. return $users[$uid];
  71. }
  72. function getuserprofile($field) {
  73. global $_G;
  74. if(isset($_G['member'][$field])) {
  75. return $_G['member'][$field];
  76. }
  77. static $tablefields = array(
  78. 'count' => array('extcredits1','extcredits2','extcredits3','extcredits4','extcredits5','extcredits6','extcredits7','extcredits8','friends','posts','threads','digestposts','doings','blogs','albums','sharings','attachsize','views','oltime','todayattachs','todayattachsize', 'follower', 'following', 'newfollower', 'blacklist'),
  79. 'status' => array('regip','lastip','lastvisit','lastactivity','lastpost','lastsendmail','invisible','buyercredit','sellercredit','favtimes','sharetimes','profileprogress'),
  80. 'field_forum' => array('publishfeed','customshow','customstatus','medals','sightml','groupterms','authstr','groups','attentiongroup'),
  81. 'field_home' => array('videophoto','spacename','spacedescription','domain','addsize','addfriend','menunum','theme','spacecss','blockposition','recentnote','spacenote','privacy','feedfriend','acceptemail','magicgift','stickblogs'),
  82. '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'),
  83. 'verify' => array('verify1', 'verify2', 'verify3', 'verify4', 'verify5', 'verify6', 'verify7'),
  84. );
  85. $profiletable = '';
  86. foreach($tablefields as $table => $fields) {
  87. if(in_array($field, $fields)) {
  88. $profiletable = $table;
  89. break;
  90. }
  91. }
  92. if($profiletable) {
  93. if(is_array($_G['member']) && $_G['member']['uid']) {
  94. space_merge($_G['member'], $profiletable);
  95. } else {
  96. foreach($tablefields[$profiletable] as $k) {
  97. $_G['member'][$k] = '';
  98. }
  99. }
  100. return $_G['member'][$field];
  101. }
  102. return null;
  103. }
  104. function daddslashes($string, $force = 1) {
  105. if(is_array($string)) {
  106. $keys = array_keys($string);
  107. foreach($keys as $key) {
  108. $val = $string[$key];
  109. unset($string[$key]);
  110. $string[addslashes($key)] = daddslashes($val, $force);
  111. }
  112. } else {
  113. $string = addslashes($string);
  114. }
  115. return $string;
  116. }
  117. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  118. $ckey_length = 4;
  119. $key = md5($key != '' ? $key : getglobal('authkey'));
  120. $keya = md5(substr($key, 0, 16));
  121. $keyb = md5(substr($key, 16, 16));
  122. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  123. $cryptkey = $keya.md5($keya.$keyc);
  124. $key_length = strlen($cryptkey);
  125. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  126. $string_length = strlen($string);
  127. $result = '';
  128. $box = range(0, 255);
  129. $rndkey = array();
  130. for($i = 0; $i <= 255; $i++) {
  131. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  132. }
  133. for($j = $i = 0; $i < 256; $i++) {
  134. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  135. $tmp = $box[$i];
  136. $box[$i] = $box[$j];
  137. $box[$j] = $tmp;
  138. }
  139. for($a = $j = $i = 0; $i < $string_length; $i++) {
  140. $a = ($a + 1) % 256;
  141. $j = ($j + $box[$a]) % 256;
  142. $tmp = $box[$a];
  143. $box[$a] = $box[$j];
  144. $box[$j] = $tmp;
  145. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  146. }
  147. if($operation == 'DECODE') {
  148. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  149. return substr($result, 26);
  150. } else {
  151. return '';
  152. }
  153. } else {
  154. return $keyc.str_replace('=', '', base64_encode($result));
  155. }
  156. }
  157. function fsocketopen($hostname, $port = 80, &$errno, &$errstr, $timeout = 15) {
  158. $fp = '';
  159. if(function_exists('fsockopen')) {
  160. $fp = @fsockopen($hostname, $port, $errno, $errstr, $timeout);
  161. } elseif(function_exists('pfsockopen')) {
  162. $fp = @pfsockopen($hostname, $port, $errno, $errstr, $timeout);
  163. } elseif(function_exists('stream_socket_client')) {
  164. $fp = @stream_socket_client($hostname.':'.$port, $errno, $errstr, $timeout);
  165. }
  166. return $fp;
  167. }
  168. function dfsockopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE', $allowcurl = TRUE, $position = 0) {
  169. require_once libfile('function/filesock');
  170. return _dfsockopen($url, $limit, $post, $cookie, $bysocket, $ip, $timeout, $block, $encodetype, $allowcurl, $position);
  171. }
  172. function dhtmlspecialchars($string, $flags = null) {
  173. if(is_array($string)) {
  174. foreach($string as $key => $val) {
  175. $string[$key] = dhtmlspecialchars($val, $flags);
  176. }
  177. } else {
  178. if($flags === null) {
  179. $string = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string);
  180. if(strpos($string, '&amp;#') !== false) {
  181. $string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $string);
  182. }
  183. } else {
  184. if(PHP_VERSION < '5.4.0') {
  185. $string = htmlspecialchars($string, $flags);
  186. } else {
  187. if(strtolower(CHARSET) == 'utf-8') {
  188. $charset = 'UTF-8';
  189. } else {
  190. $charset = 'ISO-8859-1';
  191. }
  192. $string = htmlspecialchars($string, $flags, $charset);
  193. }
  194. }
  195. }
  196. return $string;
  197. }
  198. function dexit($message = '') {
  199. echo $message;
  200. output();
  201. exit();
  202. }
  203. function dheader($string, $replace = true, $http_response_code = 0) {
  204. $islocation = substr(strtolower(trim($string)), 0, 8) == 'location';
  205. if(defined('IN_MOBILE') && strpos($string, 'mobile') === false && $islocation) {
  206. if (strpos($string, '?') === false) {
  207. $string = $string.'?mobile='.IN_MOBILE;
  208. } else {
  209. if(strpos($string, '#') === false) {
  210. $string = $string.'&mobile='.IN_MOBILE;
  211. } else {
  212. $str_arr = explode('#', $string);
  213. $str_arr[0] = $str_arr[0].'&mobile='.IN_MOBILE;
  214. $string = implode('#', $str_arr);
  215. }
  216. }
  217. }
  218. $string = str_replace(array("\r", "\n"), array('', ''), $string);
  219. if(empty($http_response_code) || PHP_VERSION < '4.3' ) {
  220. @header($string, $replace);
  221. } else {
  222. @header($string, $replace, $http_response_code);
  223. }
  224. if($islocation) {
  225. exit();
  226. }
  227. }
  228. function dsetcookie($var, $value = '', $life = 0, $prefix = 1, $httponly = false) {
  229. global $_G;
  230. $config = $_G['config']['cookie'];
  231. $_G['cookie'][$var] = $value;
  232. $var = ($prefix ? $config['cookiepre'] : '').$var;
  233. $_COOKIE[$var] = $value;
  234. if($value == '' || $life < 0) {
  235. $value = '';
  236. $life = -1;
  237. }
  238. if(defined('IN_MOBILE')) {
  239. $httponly = false;
  240. }
  241. $life = $life > 0 ? getglobal('timestamp') + $life : ($life < 0 ? getglobal('timestamp') - 31536000 : 0);
  242. $path = $httponly && PHP_VERSION < '5.2.0' ? $config['cookiepath'].'; HttpOnly' : $config['cookiepath'];
  243. $secure = $_SERVER['SERVER_PORT'] == 443 ? 1 : 0;
  244. if(PHP_VERSION < '5.2.0') {
  245. setcookie($var, $value, $life, $path, $config['cookiedomain'], $secure);
  246. } else {
  247. setcookie($var, $value, $life, $path, $config['cookiedomain'], $secure, $httponly);
  248. }
  249. }
  250. function getcookie($key) {
  251. global $_G;
  252. return isset($_G['cookie'][$key]) ? $_G['cookie'][$key] : '';
  253. }
  254. function fileext($filename) {
  255. return addslashes(strtolower(substr(strrchr($filename, '.'), 1, 10)));
  256. }
  257. function formhash($specialadd = '') {
  258. global $_G;
  259. $hashadd = defined('IN_ADMINCP') ? 'Only For Discuz! Admin Control Panel' : '';
  260. return substr(md5(substr($_G['timestamp'], 0, -7).$_G['username'].$_G['uid'].$_G['authkey'].$hashadd.$specialadd), 8, 8);
  261. }
  262. function checkrobot($useragent = '') {
  263. static $kw_spiders = array('bot', 'crawl', 'spider' ,'slurp', 'sohu-search', 'lycos', 'robozilla');
  264. static $kw_browsers = array('msie', 'netscape', 'opera', 'konqueror', 'mozilla');
  265. $useragent = strtolower(empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent);
  266. if(strpos($useragent, 'http://') === false && dstrpos($useragent, $kw_browsers)) return false;
  267. if(dstrpos($useragent, $kw_spiders)) return true;
  268. return false;
  269. }
  270. function checkmobile() {
  271. global $_G;
  272. $mobile = array();
  273. static $mobilebrowser_list =array('iphone', 'android', 'phone', 'mobile', 'wap', 'netfront', 'java', 'opera mobi', 'opera mini',
  274. 'ucweb', 'windows ce', 'symbian', 'series', 'webos', 'sony', 'blackberry', 'dopod', 'nokia', 'samsung',
  275. 'palmsource', 'xda', 'pieplus', 'meizu', 'midp', 'cldc', 'motorola', 'foma', 'docomo', 'up.browser',
  276. 'up.link', 'blazer', 'helio', 'hosin', 'huawei', 'novarra', 'coolpad', 'webos', 'techfaith', 'palmsource',
  277. 'alcatel', 'amoi', 'ktouch', 'nexian', 'ericsson', 'philips', 'sagem', 'wellcom', 'bunjalloo', 'maui', 'smartphone',
  278. 'iemobile', 'spice', 'bird', 'zte-', 'longcos', 'pantech', 'gionee', 'portalmmm', 'jig browser', 'hiptop',
  279. 'benq', 'haier', '^lct', '320x320', '240x320', '176x220');
  280. static $wmlbrowser_list = array('cect', 'compal', 'ctl', 'lg', 'nec', 'tcl', 'alcatel', 'ericsson', 'bird', 'daxian', 'dbtel', 'eastcom',
  281. 'pantech', 'dopod', 'philips', 'haier', 'konka', 'kejian', 'lenovo', 'benq', 'mot', 'soutec', 'nokia', 'sagem', 'sgh',
  282. 'sed', 'capitel', 'panasonic', 'sonyericsson', 'sharp', 'amoi', 'panda', 'zte');
  283. $pad_list = array('pad', 'gt-p1000');
  284. $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
  285. if(dstrpos($useragent, $pad_list)) {
  286. return false;
  287. }
  288. if(($v = dstrpos($useragent, $mobilebrowser_list, true))){
  289. $_G['mobile'] = $v;
  290. return '2';
  291. }
  292. if(($v = dstrpos($useragent, $wmlbrowser_list))) {
  293. $_G['mobile'] = $v;
  294. return '3'; //wml版
  295. }
  296. $brower = array('mozilla', 'chrome', 'safari', 'opera', 'm3gate', 'winwap', 'openwave', 'myop');
  297. if(dstrpos($useragent, $brower)) return false;
  298. $_G['mobile'] = 'unknown';
  299. if(isset($_G['mobiletpl'][$_GET['mobile']])) {
  300. return true;
  301. } else {
  302. return false;
  303. }
  304. }
  305. function dstrpos($string, $arr, $returnvalue = false) {
  306. if(empty($string)) return false;
  307. foreach((array)$arr as $v) {
  308. if(strpos($string, $v) !== false) {
  309. $return = $returnvalue ? $v : true;
  310. return $return;
  311. }
  312. }
  313. return false;
  314. }
  315. function isemail($email) {
  316. return strlen($email) > 6 && strlen($email) <= 32 && preg_match("/^([A-Za-z0-9\-_.+]+)@([A-Za-z0-9\-]+[.][A-Za-z0-9\-.]+)$/", $email);
  317. }
  318. function quescrypt($questionid, $answer) {
  319. return $questionid > 0 && $answer != '' ? substr(md5($answer.md5($questionid)), 16, 8) : '';
  320. }
  321. function random($length, $numeric = 0) {
  322. $seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  323. $seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
  324. if($numeric) {
  325. $hash = '';
  326. } else {
  327. $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
  328. $length--;
  329. }
  330. $max = strlen($seed) - 1;
  331. for($i = 0; $i < $length; $i++) {
  332. $hash .= $seed{mt_rand(0, $max)};
  333. }
  334. return $hash;
  335. }
  336. function strexists($string, $find) {
  337. return !(strpos($string, $find) === FALSE);
  338. }
  339. function avatar($uid, $size = 'middle', $returnsrc = FALSE, $real = FALSE, $static = FALSE, $ucenterurl = '') {
  340. global $_G;
  341. if($_G['setting']['plugins']['func'][HOOKTYPE]['avatar']) {
  342. $_G['hookavatar'] = '';
  343. $param = func_get_args();
  344. hookscript('avatar', 'global', 'funcs', array('param' => $param), 'avatar');
  345. if($_G['hookavatar']) {
  346. return $_G['hookavatar'];
  347. }
  348. }
  349. static $staticavatar;
  350. if($staticavatar === null) {
  351. $staticavatar = $_G['setting']['avatarmethod'];
  352. }
  353. $ucenterurl = empty($ucenterurl) ? $_G['setting']['ucenterurl'] : $ucenterurl;
  354. $size = in_array($size, array('big', 'middle', 'small')) ? $size : 'middle';
  355. $uid = abs(intval($uid));
  356. if(!$staticavatar && !$static) {
  357. return $returnsrc ? $ucenterurl.'/avatar.php?uid='.$uid.'&size='.$size : '<img src="'.$ucenterurl.'/avatar.php?uid='.$uid.'&size='.$size.($real ? '&type=real' : '').'" />';
  358. } else {
  359. $uid = sprintf("%09d", $uid);
  360. $dir1 = substr($uid, 0, 3);
  361. $dir2 = substr($uid, 3, 2);
  362. $dir3 = substr($uid, 5, 2);
  363. $file = $ucenterurl.'/data/avatar/'.$dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).($real ? '_real' : '').'_avatar_'.$size.'.jpg';
  364. return $returnsrc ? $file : '<img src="'.$file.'" onerror="this.onerror=null;this.src=\''.$ucenterurl.'/images/noavatar_'.$size.'.gif\'" />';
  365. }
  366. }
  367. function lang($file, $langvar = null, $vars = array(), $default = null) {
  368. global $_G;
  369. $fileinput = $file;
  370. list($path, $file) = explode('/', $file);
  371. if(!$file) {
  372. $file = $path;
  373. $path = '';
  374. }
  375. if(strpos($file, ':') !== false) {
  376. $path = 'plugin';
  377. list($file) = explode(':', $file);
  378. }
  379. if($path != 'plugin') {
  380. $key = $path == '' ? $file : $path.'_'.$file;
  381. if(!isset($_G['lang'][$key])) {
  382. include DISCUZ_ROOT.'./source/language/'.($path == '' ? '' : $path.'/').'lang_'.$file.'.php';
  383. $_G['lang'][$key] = $lang;
  384. }
  385. if(defined('IN_MOBILE') && !defined('TPL_DEFAULT')) {
  386. include DISCUZ_ROOT.'./source/language/mobile/lang_template.php';
  387. $_G['lang'][$key] = array_merge($_G['lang'][$key], $lang);
  388. }
  389. if($file != 'error' && !isset($_G['cache']['pluginlanguage_system'])) {
  390. loadcache('pluginlanguage_system');
  391. }
  392. if(!isset($_G['hooklang'][$fileinput])) {
  393. if(isset($_G['cache']['pluginlanguage_system'][$fileinput])) {
  394. $_G['lang'][$key] = array_merge($_G['lang'][$key], $_G['cache']['pluginlanguage_system'][$fileinput]);
  395. }
  396. $_G['hooklang'][$fileinput] = true;
  397. }
  398. $returnvalue = &$_G['lang'];
  399. } else {
  400. if(empty($_G['config']['plugindeveloper'])) {
  401. loadcache('pluginlanguage_script');
  402. } elseif(!isset($_G['cache']['pluginlanguage_script'][$file]) && preg_match("/^[a-z]+[a-z0-9_]*$/i", $file)) {
  403. if(@include(DISCUZ_ROOT.'./data/plugindata/'.$file.'.lang.php')) {
  404. $_G['cache']['pluginlanguage_script'][$file] = $scriptlang[$file];
  405. } else {
  406. loadcache('pluginlanguage_script');
  407. }
  408. }
  409. $returnvalue = & $_G['cache']['pluginlanguage_script'];
  410. $key = &$file;
  411. }
  412. $return = $langvar !== null ? (isset($returnvalue[$key][$langvar]) ? $returnvalue[$key][$langvar] : null) : $returnvalue[$key];
  413. $return = $return === null ? ($default !== null ? $default : $langvar) : $return;
  414. $searchs = $replaces = array();
  415. if($vars && is_array($vars)) {
  416. foreach($vars as $k => $v) {
  417. $searchs[] = '{'.$k.'}';
  418. $replaces[] = $v;
  419. }
  420. }
  421. if(is_string($return) && strpos($return, '{_G/') !== false) {
  422. preg_match_all('/\{_G\/(.+?)\}/', $return, $gvar);
  423. foreach($gvar[0] as $k => $v) {
  424. $searchs[] = $v;
  425. $replaces[] = getglobal($gvar[1][$k]);
  426. }
  427. }
  428. $return = str_replace($searchs, $replaces, $return);
  429. return $return;
  430. }
  431. function checktplrefresh($maintpl, $subtpl, $timecompare, $templateid, $cachefile, $tpldir, $file) {
  432. static $tplrefresh, $timestamp, $targettplname;
  433. if($tplrefresh === null) {
  434. $tplrefresh = getglobal('config/output/tplrefresh');
  435. $timestamp = getglobal('timestamp');
  436. }
  437. if(empty($timecompare) || $tplrefresh == 1 || ($tplrefresh > 1 && !($timestamp % $tplrefresh))) {
  438. if(empty($timecompare) || @filemtime(DISCUZ_ROOT.$subtpl) > $timecompare) {
  439. require_once DISCUZ_ROOT.'/source/class/class_template.php';
  440. $template = new template();
  441. $template->parse_template($maintpl, $templateid, $tpldir, $file, $cachefile);
  442. if($targettplname === null) {
  443. $targettplname = getglobal('style/tplfile');
  444. if(!empty($targettplname)) {
  445. include_once libfile('function/block');
  446. $targettplname = strtr($targettplname, ':', '_');
  447. update_template_block($targettplname, getglobal('style/tpldirectory'), $template->blocks);
  448. }
  449. $targettplname = true;
  450. }
  451. return TRUE;
  452. }
  453. }
  454. return FALSE;
  455. }
  456. function template($file, $templateid = 0, $tpldir = '', $gettplfile = 0, $primaltpl='') {
  457. global $_G;
  458. static $_init_style = false;
  459. if($_init_style === false) {
  460. C::app()->_init_style();
  461. $_init_style = true;
  462. }
  463. $oldfile = $file;
  464. if(strpos($file, ':') !== false) {
  465. $clonefile = '';
  466. list($templateid, $file, $clonefile) = explode(':', $file);
  467. $oldfile = $file;
  468. $file = empty($clonefile) ? $file : $file.'_'.$clonefile;
  469. if($templateid == 'diy') {
  470. $indiy = false;
  471. $_G['style']['tpldirectory'] = $tpldir ? $tpldir : (defined('TPLDIR') ? TPLDIR : '');
  472. $_G['style']['prefile'] = '';
  473. $diypath = DISCUZ_ROOT.'./data/diy/'.$_G['style']['tpldirectory'].'/'; //DIY模板文件目录
  474. $preend = '_diy_preview';
  475. $_GET['preview'] = !empty($_GET['preview']) ? $_GET['preview'] : '';
  476. $curtplname = $oldfile;
  477. $basescript = $_G['mod'] == 'viewthread' && !empty($_G['thread']) ? 'forum' : $_G['basescript'];
  478. if(isset($_G['cache']['diytemplatename'.$basescript])) {
  479. $diytemplatename = &$_G['cache']['diytemplatename'.$basescript];
  480. } else {
  481. if(!isset($_G['cache']['diytemplatename'])) {
  482. loadcache('diytemplatename');
  483. }
  484. $diytemplatename = &$_G['cache']['diytemplatename'];
  485. }
  486. $tplsavemod = 0;
  487. if(isset($diytemplatename[$file]) && file_exists($diypath.$file.'.htm') && ($tplsavemod = 1) || empty($_G['forum']['styleid']) && ($file = $primaltpl ? $primaltpl : $oldfile) && isset($diytemplatename[$file]) && file_exists($diypath.$file.'.htm')) {
  488. $tpldir = 'data/diy/'.$_G['style']['tpldirectory'].'/';
  489. !$gettplfile && $_G['style']['tplsavemod'] = $tplsavemod;
  490. $curtplname = $file;
  491. if(isset($_GET['diy']) && $_GET['diy'] == 'yes' || isset($_GET['diy']) && $_GET['preview'] == 'yes') { //DIY模式或预览模式下做以下判断
  492. $flag = file_exists($diypath.$file.$preend.'.htm');
  493. if($_GET['preview'] == 'yes') {
  494. $file .= $flag ? $preend : '';
  495. } else {
  496. $_G['style']['prefile'] = $flag ? 1 : '';
  497. }
  498. }
  499. $indiy = true;
  500. } else {
  501. $file = $primaltpl ? $primaltpl : $oldfile;
  502. }
  503. $tplrefresh = $_G['config']['output']['tplrefresh'];
  504. if($indiy && ($tplrefresh ==1 || ($tplrefresh > 1 && !($_G['timestamp'] % $tplrefresh))) && filemtime($diypath.$file.'.htm') < filemtime(DISCUZ_ROOT.$_G['style']['tpldirectory'].'/'.($primaltpl ? $primaltpl : $oldfile).'.htm')) {
  505. if (!updatediytemplate($file, $_G['style']['tpldirectory'])) {
  506. unlink($diypath.$file.'.htm');
  507. $tpldir = '';
  508. }
  509. }
  510. if (!$gettplfile && empty($_G['style']['tplfile'])) {
  511. $_G['style']['tplfile'] = empty($clonefile) ? $curtplname : $oldfile.':'.$clonefile;
  512. }
  513. $_G['style']['prefile'] = !empty($_GET['preview']) && $_GET['preview'] == 'yes' ? '' : $_G['style']['prefile'];
  514. } else {
  515. $tpldir = './source/plugin/'.$templateid.'/template';
  516. }
  517. }
  518. $file .= !empty($_G['inajax']) && ($file == 'common/header' || $file == 'common/footer') ? '_ajax' : '';
  519. $tpldir = $tpldir ? $tpldir : (defined('TPLDIR') ? TPLDIR : '');
  520. $templateid = $templateid ? $templateid : (defined('TEMPLATEID') ? TEMPLATEID : '');
  521. $filebak = $file;
  522. if(defined('IN_MOBILE') && !defined('TPL_DEFAULT') && strpos($file, $_G['mobiletpl'][IN_MOBILE].'/') === false || (isset($_G['forcemobilemessage']) && $_G['forcemobilemessage'])) {
  523. if(IN_MOBILE == 2) {
  524. $oldfile .= !empty($_G['inajax']) && ($oldfile == 'common/header' || $oldfile == 'common/footer') ? '_ajax' : '';
  525. }
  526. $file = $_G['mobiletpl'][IN_MOBILE].'/'.$oldfile;
  527. }
  528. if(!$tpldir) {
  529. $tpldir = './template/default';
  530. }
  531. $tplfile = $tpldir.'/'.$file.'.htm';
  532. $file == 'common/header' && defined('CURMODULE') && CURMODULE && $file = 'common/header_'.$_G['basescript'].'_'.CURMODULE;
  533. if(defined('IN_MOBILE') && !defined('TPL_DEFAULT')) {
  534. if(strpos($tpldir, 'plugin')) {
  535. if(!file_exists(DISCUZ_ROOT.$tpldir.'/'.$file.'.htm') && !file_exists(DISCUZ_ROOT.$tpldir.'/'.$file.'.php')) {
  536. discuz_error::template_error('template_notfound', $tpldir.'/'.$file.'.htm');
  537. } else {
  538. $mobiletplfile = $tpldir.'/'.$file.'.htm';
  539. }
  540. }
  541. !$mobiletplfile && $mobiletplfile = $file.'.htm';
  542. if(strpos($tpldir, 'plugin') && (file_exists(DISCUZ_ROOT.$mobiletplfile) || file_exists(substr(DISCUZ_ROOT.$mobiletplfile, 0, -4).'.php'))) {
  543. $tplfile = $mobiletplfile;
  544. } elseif(!file_exists(DISCUZ_ROOT.TPLDIR.'/'.$mobiletplfile) && !file_exists(substr(DISCUZ_ROOT.TPLDIR.'/'.$mobiletplfile, 0, -4).'.php')) {
  545. $mobiletplfile = './template/default/'.$mobiletplfile;
  546. if(!file_exists(DISCUZ_ROOT.$mobiletplfile) && !$_G['forcemobilemessage']) {
  547. $tplfile = str_replace($_G['mobiletpl'][IN_MOBILE].'/', '', $tplfile);
  548. $file = str_replace($_G['mobiletpl'][IN_MOBILE].'/', '', $file);
  549. define('TPL_DEFAULT', true);
  550. } else {
  551. $tplfile = $mobiletplfile;
  552. }
  553. } else {
  554. $tplfile = TPLDIR.'/'.$mobiletplfile;
  555. }
  556. }
  557. $cachefile = './data/template/'.(defined('STYLEID') ? STYLEID.'_' : '_').$templateid.'_'.str_replace('/', '_', $file).'.tpl.php';
  558. if($templateid != 1 && !file_exists(DISCUZ_ROOT.$tplfile) && !file_exists(substr(DISCUZ_ROOT.$tplfile, 0, -4).'.php')
  559. && !file_exists(DISCUZ_ROOT.($tplfile = $tpldir.$filebak.'.htm'))) {
  560. $tplfile = './template/default/'.$filebak.'.htm';
  561. }
  562. if($gettplfile) {
  563. return $tplfile;
  564. }
  565. checktplrefresh($tplfile, $tplfile, @filemtime(DISCUZ_ROOT.$cachefile), $templateid, $cachefile, $tpldir, $file);
  566. return DISCUZ_ROOT.$cachefile;
  567. }
  568. function dsign($str, $length = 16){
  569. return substr(md5($str.getglobal('security/authkey')), 0, ($length ? max(8, $length) : 16));
  570. }
  571. function modauthkey($id) {
  572. return md5(getglobal('username').getglobal('uid').getglobal('authkey').substr(TIMESTAMP, 0, -7).$id);
  573. }
  574. function getcurrentnav() {
  575. global $_G;
  576. if(!empty($_G['mnid'])) {
  577. return $_G['mnid'];
  578. }
  579. $mnid = '';
  580. $_G['basefilename'] = $_G['basefilename'] == $_G['basescript'] ? $_G['basefilename'] : $_G['basescript'].'.php';
  581. if(isset($_G['setting']['navmns'][$_G['basefilename']])) {
  582. if($_G['basefilename'] == 'home.php' && $_GET['mod'] == 'space' && (empty($_GET['do']) || in_array($_GET['do'], array('follow', 'view')))) {
  583. $_GET['mod'] = 'follow';
  584. }
  585. foreach($_G['setting']['navmns'][$_G['basefilename']] as $navmn) {
  586. if($navmn[0] == array_intersect_assoc($navmn[0], $_GET) || ($navmn[0]['mod'] == 'space' && $_GET['mod'] == 'spacecp' && ($navmn[0]['do'] == $_GET['ac'] || $navmn[0]['do'] == 'album' && $_GET['ac'] == 'upload'))) {
  587. $mnid = $navmn[1];
  588. }
  589. }
  590. }
  591. if(!$mnid && isset($_G['setting']['navdms'])) {
  592. foreach($_G['setting']['navdms'] as $navdm => $navid) {
  593. if(strpos(strtolower($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']), $navdm) !== false && strpos(strtolower($_SERVER['HTTP_HOST']), $navdm) === false) {
  594. $mnid = $navid;
  595. break;
  596. }
  597. }
  598. }
  599. if(!$mnid && isset($_G['setting']['navmn'][$_G['basefilename']])) {
  600. $mnid = $_G['setting']['navmn'][$_G['basefilename']];
  601. }
  602. return $mnid;
  603. }
  604. function loaducenter() {
  605. require_once DISCUZ_ROOT.'./config/config_ucenter.php';
  606. require_once DISCUZ_ROOT.'./uc_client/client.php';
  607. }
  608. function loadcache($cachenames, $force = false) {
  609. global $_G;
  610. static $loadedcache = array();
  611. $cachenames = is_array($cachenames) ? $cachenames : array($cachenames);
  612. $caches = array();
  613. foreach ($cachenames as $k) {
  614. if(!isset($loadedcache[$k]) || $force) {
  615. $caches[] = $k;
  616. $loadedcache[$k] = true;
  617. }
  618. }
  619. if(!empty($caches)) {
  620. $cachedata = C::t('common_syscache')->fetch_all($caches);
  621. foreach($cachedata as $cname => $data) {
  622. if($cname == 'setting') {
  623. $_G['setting'] = $data;
  624. } elseif($cname == 'usergroup_'.$_G['groupid']) {
  625. $_G['cache'][$cname] = $_G['group'] = $data;
  626. } elseif($cname == 'style_default') {
  627. $_G['cache'][$cname] = $_G['style'] = $data;
  628. } elseif($cname == 'grouplevels') {
  629. $_G['grouplevels'] = $data;
  630. } else {
  631. $_G['cache'][$cname] = $data;
  632. }
  633. }
  634. }
  635. return true;
  636. }
  637. function dgmdate($timestamp, $format = 'dt', $timeoffset = '9999', $uformat = '') {
  638. global $_G;
  639. $format == 'u' && !$_G['setting']['dateconvert'] && $format = 'dt';
  640. static $dformat, $tformat, $dtformat, $offset, $lang;
  641. if($dformat === null) {
  642. $dformat = getglobal('setting/dateformat');
  643. $tformat = getglobal('setting/timeformat');
  644. $dtformat = $dformat.' '.$tformat;
  645. $offset = getglobal('member/timeoffset');
  646. $lang = lang('core', 'date');
  647. }
  648. $timeoffset = $timeoffset == 9999 ? $offset : $timeoffset;
  649. $timestamp += $timeoffset * 3600;
  650. $format = empty($format) || $format == 'dt' ? $dtformat : ($format == 'd' ? $dformat : ($format == 't' ? $tformat : $format));
  651. if($format == 'u') {
  652. $todaytimestamp = TIMESTAMP - (TIMESTAMP + $timeoffset * 3600) % 86400 + $timeoffset * 3600;
  653. $s = gmdate(!$uformat ? $dtformat : $uformat, $timestamp);
  654. $time = TIMESTAMP + $timeoffset * 3600 - $timestamp;
  655. if($timestamp >= $todaytimestamp) {
  656. if($time > 3600) {
  657. $return = intval($time / 3600).'&nbsp;'.$lang['hour'].$lang['before'];
  658. } elseif($time > 1800) {
  659. $return = $lang['half'].$lang['hour'].$lang['before'];
  660. } elseif($time > 60) {
  661. $return = intval($time / 60).'&nbsp;'.$lang['min'].$lang['before'];
  662. } elseif($time > 0) {
  663. $return = $time.'&nbsp;'.$lang['sec'].$lang['before'];
  664. } elseif($time == 0) {
  665. $return = $lang['now'];
  666. } else {
  667. $return = $s;
  668. }
  669. if($time >=0 && !defined('IN_MOBILE')) {
  670. $return = '<span title="'.$s.'">'.$return.'</span>';
  671. }
  672. } elseif(($days = intval(($todaytimestamp - $timestamp) / 86400)) >= 0 && $days < 7) {
  673. if($days == 0) {
  674. $return = $lang['yday'].'&nbsp;'.gmdate($tformat, $timestamp);
  675. } elseif($days == 1) {
  676. $return = $lang['byday'].'&nbsp;'.gmdate($tformat, $timestamp);
  677. } else {
  678. $return = ($days + 1).'&nbsp;'.$lang['day'].$lang['before'];
  679. }
  680. if(!defined('IN_MOBILE')) {
  681. $return = '<span title="'.$s.'">'.$return.'</span>';
  682. }
  683. } else {
  684. $return = $s;
  685. }
  686. return $return;
  687. } else {
  688. return gmdate($format, $timestamp);
  689. }
  690. }
  691. function dmktime($date) {
  692. if(strpos($date, '-')) {
  693. $time = explode('-', $date);
  694. return mktime(0, 0, 0, $time[1], $time[2], $time[0]);
  695. }
  696. return 0;
  697. }
  698. function dnumber($number) {
  699. return abs($number) > 10000 ? '<span title="'.$number.'">'.intval($number / 10000).lang('core', '10k').'</span>' : $number;
  700. }
  701. function savecache($cachename, $data) {
  702. C::t('common_syscache')->insert($cachename, $data);
  703. }
  704. function save_syscache($cachename, $data) {
  705. savecache($cachename, $data);
  706. }
  707. function block_get($parameter) {
  708. include_once libfile('function/block');
  709. block_get_batch($parameter);
  710. }
  711. function block_display($bid) {
  712. include_once libfile('function/block');
  713. block_display_batch($bid);
  714. }
  715. function dimplode($array) {
  716. if(!empty($array)) {
  717. $array = array_map('addslashes', $array);
  718. return "'".implode("','", is_array($array) ? $array : array($array))."'";
  719. } else {
  720. return 0;
  721. }
  722. }
  723. function libfile($libname, $folder = '') {
  724. $libpath = '/source/'.$folder;
  725. if(strstr($libname, '/')) {
  726. list($pre, $name) = explode('/', $libname);
  727. $path = "{$libpath}/{$pre}/{$pre}_{$name}";
  728. } else {
  729. $path = "{$libpath}/{$libname}";
  730. }
  731. return preg_match('/^[\w\d\/_]+$/i', $path) ? realpath(DISCUZ_ROOT.$path.'.php') : false;
  732. }
  733. function dstrlen($str) {
  734. if(strtolower(CHARSET) != 'utf-8') {
  735. return strlen($str);
  736. }
  737. $count = 0;
  738. for($i = 0; $i < strlen($str); $i++){
  739. $value = ord($str[$i]);
  740. if($value > 127) {
  741. $count++;
  742. if($value >= 192 && $value <= 223) $i++;
  743. elseif($value >= 224 && $value <= 239) $i = $i + 2;
  744. elseif($value >= 240 && $value <= 247) $i = $i + 3;
  745. }
  746. $count++;
  747. }
  748. return $count;
  749. }
  750. function cutstr($string, $length, $dot = ' ...') {
  751. if(strlen($string) <= $length) {
  752. return $string;
  753. }
  754. $pre = chr(1);
  755. $end = chr(1);
  756. $string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), $string);
  757. $strcut = '';
  758. if(strtolower(CHARSET) == 'utf-8') {
  759. $n = $tn = $noc = 0;
  760. while($n < strlen($string)) {
  761. $t = ord($string[$n]);
  762. if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
  763. $tn = 1; $n++; $noc++;
  764. } elseif(194 <= $t && $t <= 223) {
  765. $tn = 2; $n += 2; $noc += 2;
  766. } elseif(224 <= $t && $t <= 239) {
  767. $tn = 3; $n += 3; $noc += 2;
  768. } elseif(240 <= $t && $t <= 247) {
  769. $tn = 4; $n += 4; $noc += 2;
  770. } elseif(248 <= $t && $t <= 251) {
  771. $tn = 5; $n += 5; $noc += 2;
  772. } elseif($t == 252 || $t == 253) {
  773. $tn = 6; $n += 6; $noc += 2;
  774. } else {
  775. $n++;
  776. }
  777. if($noc >= $length) {
  778. break;
  779. }
  780. }
  781. if($noc > $length) {
  782. $n -= $tn;
  783. }
  784. $strcut = substr($string, 0, $n);
  785. } else {
  786. $_length = $length - 1;
  787. for($i = 0; $i < $length; $i++) {
  788. if(ord($string[$i]) <= 127) {
  789. $strcut .= $string[$i];
  790. } else if($i < $_length) {
  791. $strcut .= $string[$i].$string[++$i];
  792. }
  793. }
  794. }
  795. $strcut = str_replace(array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
  796. $pos = strrpos($strcut, chr(1));
  797. if($pos !== false) {
  798. $strcut = substr($strcut,0,$pos);
  799. }
  800. return $strcut.$dot;
  801. }
  802. function dstripslashes($string) {
  803. if(empty($string)) return $string;
  804. if(is_array($string)) {
  805. foreach($string as $key => $val) {
  806. $string[$key] = dstripslashes($val);
  807. }
  808. } else {
  809. $string = stripslashes($string);
  810. }
  811. return $string;
  812. }
  813. function aidencode($aid, $type = 0, $tid = 0) {
  814. global $_G;
  815. $s = !$type ? $aid.'|'.substr(md5($aid.md5($_G['config']['security']['authkey']).TIMESTAMP.$_G['uid']), 0, 8).'|'.TIMESTAMP.'|'.$_G['uid'].'|'.$tid : $aid.'|'.md5($aid.md5($_G['config']['security']['authkey']).TIMESTAMP).'|'.TIMESTAMP;
  816. return rawurlencode(base64_encode($s));
  817. }
  818. function getforumimg($aid, $nocache = 0, $w = 140, $h = 140, $type = '') {
  819. global $_G;
  820. $key = dsign($aid.'|'.$w.'|'.$h);
  821. return 'forum.php?mod=image&aid='.$aid.'&size='.$w.'x'.$h.'&key='.rawurlencode($key).($nocache ? '&nocache=yes' : '').($type ? '&type='.$type : '');
  822. }
  823. function rewriteoutput($type, $returntype, $host) {
  824. global $_G;
  825. $fextra = '';
  826. if($type == 'forum_forumdisplay') {
  827. list(,,, $fid, $page, $extra) = func_get_args();
  828. $r = array(
  829. '{fid}' => empty($_G['setting']['forumkeys'][$fid]) ? $fid : $_G['setting']['forumkeys'][$fid],
  830. '{page}' => $page ? $page : 1,
  831. );
  832. } elseif($type == 'forum_viewthread') {
  833. list(,,, $tid, $page, $prevpage, $extra) = func_get_args();
  834. $r = array(
  835. '{tid}' => $tid,
  836. '{page}' => $page ? $page : 1,
  837. '{prevpage}' => $prevpage && !IS_ROBOT ? $prevpage : 1,
  838. );
  839. } elseif($type == 'home_space') {
  840. list(,,, $uid, $username, $extra) = func_get_args();
  841. $_G['setting']['rewritecompatible'] && $username = rawurlencode($username);
  842. $r = array(
  843. '{user}' => $uid ? 'uid' : 'username',
  844. '{value}' => $uid ? $uid : $username,
  845. );
  846. } elseif($type == 'home_blog') {
  847. list(,,, $uid, $blogid, $extra) = func_get_args();
  848. $r = array(
  849. '{uid}' => $uid,
  850. '{blogid}' => $blogid,
  851. );
  852. } elseif($type == 'group_group') {
  853. list(,,, $fid, $page, $extra) = func_get_args();
  854. $r = array(
  855. '{fid}' => $fid,
  856. '{page}' => $page ? $page : 1,
  857. );
  858. } elseif($type == 'portal_topic') {
  859. list(,,, $name, $extra) = func_get_args();
  860. $r = array(
  861. '{name}' => $name,
  862. );
  863. } elseif($type == 'portal_article') {
  864. list(,,, $id, $page, $extra) = func_get_args();
  865. $r = array(
  866. '{id}' => $id,
  867. '{page}' => $page ? $page : 1,
  868. );
  869. } elseif($type == 'forum_archiver') {
  870. list(,, $action, $value, $page, $extra) = func_get_args();
  871. $host = '';
  872. $r = array(
  873. '{action}' => $action,
  874. '{value}' => $value,
  875. );
  876. if($page) {
  877. $fextra = '?page='.$page;
  878. }
  879. } elseif($type == 'plugin') {
  880. list(,, $pluginid, $module,, $param, $extra) = func_get_args();
  881. $host = '';
  882. $r = array(
  883. '{pluginid}' => $pluginid,
  884. '{module}' => $module,
  885. );
  886. if($param) {
  887. $fextra = '?'.$param;
  888. }
  889. }
  890. $href = str_replace(array_keys($r), $r, $_G['setting']['rewriterule'][$type]).$fextra;
  891. if(!$returntype) {
  892. return '<a href="'.$host.$href.'"'.(!empty($extra) ? stripslashes($extra) : '').'>';
  893. } else {
  894. return $host.$href;
  895. }
  896. }
  897. function mobilereplace($file, $replace) {
  898. return helper_mobile::mobilereplace($file, $replace);
  899. }
  900. function mobileoutput() {
  901. helper_mobile::mobileoutput();
  902. }
  903. function output() {
  904. global $_G;
  905. if(defined('DISCUZ_OUTPUTED')) {
  906. return;
  907. } else {
  908. define('DISCUZ_OUTPUTED', 1);
  909. }
  910. if(!empty($_G['blockupdate'])) {
  911. block_updatecache($_G['blockupdate']['bid']);
  912. }
  913. if(defined('IN_MOBILE')) {
  914. mobileoutput();
  915. }
  916. if(!defined('IN_MOBILE') && !defined('IN_ARCHIVER')) {
  917. $tipsService = Cloud::loadClass('Service_DiscuzTips');
  918. $tipsService->show();
  919. }
  920. $havedomain = implode('', $_G['setting']['domain']['app']);
  921. if($_G['setting']['rewritestatus'] || !empty($havedomain)) {
  922. $content = ob_get_contents();
  923. $content = output_replace($content);
  924. ob_end_clean();
  925. $_G['gzipcompress'] ? ob_start('ob_gzhandler') : ob_start();
  926. echo $content;
  927. }
  928. if(isset($_G['makehtml'])) {
  929. helper_makehtml::make_html();
  930. }
  931. if($_G['setting']['ftp']['connid']) {
  932. @ftp_close($_G['setting']['ftp']['connid']);
  933. }
  934. $_G['setting']['ftp'] = array();
  935. if(defined('CACHE_FILE') && CACHE_FILE && !defined('CACHE_FORBIDDEN') && !defined('IN_MOBILE') && !checkmobile()) {
  936. if(diskfreespace(DISCUZ_ROOT.'./'.$_G['setting']['cachethreaddir']) > 1000000) {
  937. if($fp = @fopen(CACHE_FILE, 'w')) {
  938. flock($fp, LOCK_EX);
  939. fwrite($fp, empty($content) ? ob_get_contents() : $content);
  940. }
  941. @fclose($fp);
  942. chmod(CACHE_FILE, 0777);
  943. }
  944. }
  945. if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG && @include(libfile('function/debug'))) {
  946. function_exists('debugmessage') && debugmessage();
  947. }
  948. }
  949. function output_replace($content) {
  950. global $_G;
  951. if(defined('IN_MODCP') || defined('IN_ADMINCP')) return $content;
  952. if(!empty($_G['setting']['output']['str']['search'])) {
  953. if(empty($_G['setting']['domain']['app']['default'])) {
  954. $_G['setting']['output']['str']['replace'] = str_replace('{CURHOST}', $_G['siteurl'], $_G['setting']['output']['str']['replace']);
  955. }
  956. $content = str_replace($_G['setting']['output']['str']['search'], $_G['setting']['output']['str']['replace'], $content);
  957. }
  958. if(!empty($_G['setting']['output']['preg']['search']) && (empty($_G['setting']['rewriteguest']) || empty($_G['uid']))) {
  959. if(empty($_G['setting']['domain']['app']['default'])) {
  960. $_G['setting']['output']['preg']['search'] = str_replace('\{CURHOST\}', preg_quote($_G['siteurl'], '/'), $_G['setting']['output']['preg']['search']);
  961. $_G['setting']['output']['preg']['replace'] = str_replace('{CURHOST}', $_G['siteurl'], $_G['setting']['output']['preg']['replace']);
  962. }
  963. $content = preg_replace($_G['setting']['output']['preg']['search'], $_G['setting']['output']['preg']['replace'], $content);
  964. }
  965. return $content;
  966. }
  967. function output_ajax() {
  968. global $_G;
  969. $s = ob_get_contents();
  970. ob_end_clean();
  971. $s = preg_replace("/([\\x01-\\x08\\x0b-\\x0c\\x0e-\\x1f])+/", ' ', $s);
  972. $s = str_replace(array(chr(0), ']]>'), array(' ', ']]&gt;'), $s);
  973. if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG && @include(libfile('function/debug'))) {
  974. function_exists('debugmessage') && $s .= debugmessage(1);
  975. }
  976. $havedomain = implode('', $_G['setting']['domain']['app']);
  977. if($_G['setting']['rewritestatus'] || !empty($havedomain)) {
  978. $s = output_replace($s);
  979. }
  980. return $s;
  981. }
  982. function runhooks($scriptextra = '') {
  983. if(!defined('HOOKTYPE')) {
  984. define('HOOKTYPE', !defined('IN_MOBILE') ? 'hookscript' : 'hookscriptmobile');
  985. }
  986. if(defined('CURMODULE')) {
  987. global $_G;
  988. if($_G['setting']['plugins']['func'][HOOKTYPE]['common']) {
  989. hookscript('common', 'global', 'funcs', array(), 'common');
  990. }
  991. hookscript(CURMODULE, $_G['basescript'], 'funcs', array(), '', $scriptextra);
  992. }
  993. }
  994. function hookscript($script, $hscript, $type = 'funcs', $param = array(), $func = '', $scriptextra = '') {
  995. global $_G;
  996. static $pluginclasses;
  997. if($hscript == 'home') {
  998. if($script == 'space') {
  999. $scriptextra = !$scriptextra ? $_GET['do'] : $scriptextra;
  1000. $script = 'space'.(!empty($scriptextra) ? '_'.$scriptextra : '');
  1001. } elseif($script == 'spacecp') {
  1002. $scriptextra = !$scriptextra ? $_GET['ac'] : $scriptextra;
  1003. $script .= !empty($scriptextra) ? '_'.$scriptextra : '';
  1004. }
  1005. }
  1006. if(!isset($_G['setting'][HOOKTYPE][$hscript][$script][$type])) {
  1007. return;
  1008. }
  1009. if(!isset($_G['cache']['plugin'])) {
  1010. loadcache('plugin');
  1011. }
  1012. foreach((array)$_G['setting'][HOOKTYPE][$hscript][$script]['module'] as $identifier => $include) {
  1013. if($_G['pluginrunlist'] && !in_array($identifier, $_G['pluginrunlist'])) {
  1014. continue;
  1015. }
  1016. $hooksadminid[$identifier] = !$_G['setting'][HOOKTYPE][$hscript][$script]['adminid'][$identifier] || ($_G['setting'][HOOKTYPE][$hscript][$script]['adminid'][$identifier] && $_G['adminid'] > 0 && $_G['setting']['hookscript'][$hscript][$script]['adminid'][$identifier] >= $_G['adminid']);
  1017. if($hooksadminid[$identifier]) {
  1018. @include_once DISCUZ_ROOT.'./source/plugin/'.$include.'.class.php';
  1019. }
  1020. }
  1021. if(@is_array($_G['setting'][HOOKTYPE][$hscript][$script][$type])) {
  1022. $_G['inhookscript'] = true;
  1023. $funcs = !$func ? $_G['setting'][HOOKTYPE][$hscript][$script][$type] : array($func => $_G['setting'][HOOKTYPE][$hscript][$script][$type][$func]);
  1024. foreach($funcs as $hookkey => $hookfuncs) {
  1025. foreach($hookfuncs as $hookfunc) {
  1026. if($hooksadminid[$hookfunc[0]]) {
  1027. $classkey = (HOOKTYPE != 'hookscriptmobile' ? '' : 'mobile').'plugin_'.($hookfunc[0].($hscript != 'global' ? '_'.$hscript : ''));
  1028. if(!class_exists($classkey, false)) {
  1029. continue;
  1030. }
  1031. if(!isset($pluginclasses[$classkey])) {
  1032. $pluginclasses[$classkey] = new $classkey;
  1033. }
  1034. if(!method_exists($pluginclasses[$classkey], $hookfunc[1])) {
  1035. continue;
  1036. }
  1037. $return = $pluginclasses[$classkey]->$hookfunc[1]($param);
  1038. if(substr($hookkey, -7) == '_extend' && !empty($_G['setting']['pluginhooks'][$hookkey])) {
  1039. continue;
  1040. }
  1041. if(is_array($return)) {
  1042. if(!isset($_G['setting']['pluginhooks'][$hookkey]) || is_array($_G['setting']['pluginhooks'][$hookkey])) {
  1043. foreach($return as $k => $v) {
  1044. $_G['setting']['pluginhooks'][$hookkey][$k] .= $v;
  1045. }
  1046. } else {
  1047. foreach($return as $k => $v) {
  1048. $_G['setting']['pluginhooks'][$hookkey][$k] = $v;
  1049. }
  1050. }
  1051. } else {
  1052. if(!is_array($_G['setting']['pluginhooks'][$hookkey])) {
  1053. $_G['setting']['pluginhooks'][$hookkey] .= $return;
  1054. } else {
  1055. foreach($_G['setting']['pluginhooks'][$hookkey] as $k => $v) {
  1056. $_G['setting']['pluginhooks'][$hookkey][$k] .= $return;
  1057. }
  1058. }
  1059. }
  1060. }
  1061. }
  1062. }
  1063. }
  1064. $_G['inhookscript'] = false;
  1065. }
  1066. function hookscriptoutput($tplfile) {
  1067. global $_G;
  1068. if(!empty($_G['hookscriptoutput'])) {
  1069. return;
  1070. }
  1071. hookscript('global', 'global');
  1072. if(defined('CURMODULE')) {
  1073. $param = array('template' => $tplfile, 'message' => $_G['hookscriptmessage'], 'values' => $_G['hookscriptvalues']);
  1074. hookscript(CURMODULE, $_G['basescript'], 'outputfuncs', $param);
  1075. }
  1076. $_G['hookscriptoutput'] = true;
  1077. }
  1078. function pluginmodule($pluginid, $type) {
  1079. global $_G;
  1080. $pluginid = $pluginid ? preg_replace("/[^A-Za-z0-9_:]/", '', $pluginid) : '';
  1081. if(!isset($_G['cache']['plugin'])) {
  1082. loadcache('plugin');
  1083. }
  1084. list($identifier, $module) = explode(':', $pluginid);
  1085. if(!is_array($_G['setting']['plugins'][$type]) || !array_key_exists($pluginid, $_G['setting']['plugins'][$type])) {
  1086. showmessage('plugin_nonexistence');
  1087. }
  1088. if(!empty($_G['setting']['plugins'][$type][$pluginid]['url'])) {
  1089. dheader('location: '.$_G['setting']['plugins'][$type][$pluginid]['url']);
  1090. }
  1091. $directory = $_G['setting']['plugins'][$type][$pluginid]['directory'];
  1092. if(empty($identifier) || !preg_match("/^[a-z]+[a-z0-9_]*\/$/i", $directory) || !preg_match("/^[a-z0-9_\-]+$/i", $module)) {
  1093. showmessage('undefined_action');
  1094. }
  1095. if(@!file_exists(DISCUZ_ROOT.($modfile = './source/plugin/'.$directory.$module.'.inc.php'))) {
  1096. showmessage('plugin_module_nonexistence', '', array('mod' => $modfile));
  1097. }
  1098. return DISCUZ_ROOT.$modfile;
  1099. }
  1100. function updatecreditbyaction($action, $uid = 0, $extrasql = array(), $needle = '', $coef = 1, $update = 1, $fid = 0) {
  1101. $credit = credit::instance();
  1102. if($extrasql) {
  1103. $credit->extrasql = $extrasql;
  1104. }
  1105. return $credit->execrule($action, $uid, $needle, $coef, $update, $fid);
  1106. }
  1107. function checklowerlimit($action, $uid = 0, $coef = 1, $fid = 0, $returnonly = 0) {
  1108. require_once libfile('function/credit');
  1109. return _checklowerlimit($action, $uid, $coef, $fid, $returnonly);
  1110. }
  1111. function batchupdatecredit($action, $uids = 0, $extrasql = array(), $coef = 1, $fid = 0) {
  1112. $credit = & credit::instance();
  1113. if($extrasql) {
  1114. $credit->extrasql = $extrasql;
  1115. }
  1116. return $credit->updatecreditbyrule($action, $uids, $coef, $fid);
  1117. }
  1118. function updatemembercount($uids, $dataarr = array(), $checkgroup = true, $operation = '', $relatedid = 0, $ruletxt = '', $customtitle = '', $custommemo = '') {
  1119. if(!empty($uids) && (is_array($dataarr) && $dataarr)) {
  1120. require_once libfile('function/credit');
  1121. return _updatemembercount($uids, $dataarr, $checkgroup, $operation, $relatedid, $ruletxt, $customtitle, $custommemo);
  1122. }
  1123. return true;
  1124. }
  1125. function checkusergroup($uid = 0) {
  1126. $credit = & credit::instance();
  1127. $credit->checkusergroup($uid);
  1128. }
  1129. function checkformulasyntax($formula, $operators, $tokens) {
  1130. $var = implode('|', $tokens);
  1131. $operator = implode('', $operators);
  1132. $operator = str_replace(
  1133. array('+', '-', '*', '/', '(', ')', '{', '}', '\''),
  1134. array('\+', '\-', '\*', '\/', '\(', '\)', '\{', '\}', '\\\''),
  1135. $operator
  1136. );
  1137. if(!empty($formula)) {
  1138. if(!preg_match("/^([$operator\.\d\(\)]|(($var)([$operator\(\)]|$)+))+$/", $formula) || !is_null(eval(preg_replace("/($var)/", "\$\\1", $formula).';'))){
  1139. return false;
  1140. }
  1141. }
  1142. return true;
  1143. }
  1144. function checkformulacredits($formula) {
  1145. return checkformulasyntax(
  1146. $formula,
  1147. array('+', '-', '*', '/', ' '),
  1148. array('extcredits[1-8]', 'digestposts', 'posts', 'threads', 'oltime', 'friends', 'doings', 'polls', 'blogs', 'albums', 'sharings')
  1149. );
  1150. }
  1151. function debug($var = null, $vardump = false) {
  1152. echo '<pre>';
  1153. $vardump = empty($var) ? true : $vardump;
  1154. if($vardump) {
  1155. var_dump($var);
  1156. } else {
  1157. print_r($var);
  1158. }
  1159. exit();
  1160. }
  1161. function debuginfo() {
  1162. global $_G;
  1163. if(getglobal('setting/debug')) {
  1164. $db = & DB::object();
  1165. $_G['debuginfo'] = array(
  1166. 'time' => number_format((microtime(true) - $_G['starttime']), 6),
  1167. 'queries' => $db->querynum,
  1168. 'memory' => ucwords(C::memory()->type)
  1169. );
  1170. if($db->slaveid) {
  1171. $_G['debuginfo']['queries'] = 'Total '.$db->querynum.', Slave '.$db->slavequery;
  1172. }
  1173. return TRUE;
  1174. } else {
  1175. return FALSE;
  1176. }
  1177. }
  1178. function getfocus_rand($module) {
  1179. global $_G;
  1180. if(empty($_G['setting']['focus']) || !array_key_exists($module, $_G['setting']['focus']) || !empty($_G['cookie']['nofocus_'.$module]) || !$_G['setting']['focus'][$module]) {
  1181. return null;
  1182. }
  1183. loadcache('focus');
  1184. if(empty($_G['cache']['focus']['data']) || !is_array($_G['cache']['focus']['data'])) {
  1185. return null;
  1186. }
  1187. $focusid = $_G['setting']['focus'][$module][array_rand($_G['setting']['focus'][$module])];
  1188. return $focusid;
  1189. }
  1190. function check_seccode($value, $idhash) {
  1191. return helper_form::check_seccode($value, $idhash);
  1192. }
  1193. function check_secqaa($value, $idhash) {
  1194. return helper_form::check_secqaa($value, $idhash);
  1195. }
  1196. function adshow($parameter) {
  1197. global $_G;
  1198. if($_G['inajax'] || $_G['group']['closead']) {
  1199. return;
  1200. }
  1201. if(isset($_G['config']['plugindeveloper']) && $_G['config']['plugindeveloper'] == 2) {
  1202. return '<hook>[ad '.$parameter.']</hook>';
  1203. }
  1204. $params = explode('/', $parameter);
  1205. $customid = 0;
  1206. $customc = explode('_', $params[0]);
  1207. if($customc[0] == 'custom') {
  1208. $params[0] = $customc[0];
  1209. $customid = $customc[1];
  1210. }
  1211. $adcontent = null;
  1212. if(empty($_G['setting']['advtype']) || !in_array($params[0], $_G['setting']['advtype'])) {
  1213. $adcontent = '';
  1214. }
  1215. if($adcontent === null) {
  1216. loadcache('advs');
  1217. $adids = array();
  1218. $evalcode = &$_G['cache']['advs']['evalcode'][$params[0]];
  1219. $parameters = &$_G['cache']['advs']['parameters'][$params[0]];
  1220. $codes = &$_G['cache']['advs']['code'][$_G['basescript']][$params[0]];
  1221. if(!empty($codes)) {
  1222. foreach($codes as $adid => $code) {
  1223. $parameter = &$parameters[$adid];
  1224. $checked = true;
  1225. @eval($evalcode['check']);
  1226. if($checked) {
  1227. $adids[] = $adid;
  1228. }
  1229. }
  1230. if(!empty($adids)) {
  1231. $adcode = $extra = '';
  1232. @eval($evalcode['create']);
  1233. if(empty($notag)) {
  1234. $adcontent = '<div'.($params[1] != '' ? ' class="'.$params[1].'"' : '').$extra.'>'.$adcode.'</div>';
  1235. } else {
  1236. $adcontent = $adcode;
  1237. }
  1238. }
  1239. }
  1240. }
  1241. $adfunc = 'ad_'.$params[0];
  1242. $_G['setting']['pluginhooks'][$adfunc] = null;
  1243. hookscript('ad', 'global', 'funcs', array('params' => $params, 'content' => $adcontent), $adfunc);
  1244. if(!$_G['setting']['hookscript']['global']['ad']['funcs'][$adfunc]) {
  1245. hookscript('ad', $_G['basescript'], 'funcs', array('params' => $params, 'content' => $adcontent), $adfunc);
  1246. }
  1247. return $_G['setting']['pluginhooks'][$adfunc] === null ? $adcontent : $_G['setting']['pluginhooks'][$adfunc];
  1248. }
  1249. function showmessage($message, $url_forward = '', $values = array(), $extraparam = array(), $custom = 0) {
  1250. require_once libfile('function/message');
  1251. return dshowmessage($message, $url_forward, $values, $extraparam, $custom);
  1252. }
  1253. function submitcheck($var, $allowget = 0, $seccodecheck = 0, $secqaacheck = 0) {
  1254. if(!getgpc($var)) {
  1255. return FALSE;
  1256. } else {
  1257. return helper_form::submitcheck($var, $allowget, $seccodecheck, $secqaacheck);
  1258. }
  1259. }
  1260. function multi($num, $perpage, $curpage, $mpurl, $maxpages = 0, $page = 10, $autogoto = FALSE, $simple = FALSE, $jsfunc = FALSE) {
  1261. return $num > $perpage ? helper_page::multi($num, $perpage, $curpage, $mpurl, $maxpages, $page, $autogoto, $simple, $jsfunc) : '';
  1262. }
  1263. function simplepage($num, $perpage, $curpage, $mpurl) {
  1264. return helper_page::simplepage($num, $perpage, $curpage, $mpurl);
  1265. }
  1266. function censor($message, $modword = NULL, $return = FALSE) {
  1267. return helper_form::censor($message, $modword, $return);
  1268. }
  1269. function censormod($message) {
  1270. return getglobal('group/ignorecensor') || !$message ? false :helper_form::censormod($message);
  1271. }
  1272. function space_merge(&$values, $tablename, $isarchive = false) {
  1273. global $_G;
  1274. $uid = empty($values['uid'])?$_G['uid']:$values['uid'];
  1275. $var = "member_{$uid}_{$tablename}";
  1276. if($uid) {
  1277. if(!isset($_G[$var])) {
  1278. $ext = $isarchive ? '_archive' : '';
  1279. if(($_G[$var] = C::t('common_member_'.$tablename.$ext)->fetch($uid)) !== false) {
  1280. if($tablename == 'field_home') {
  1281. $_G['setting']['privacy'] = empty($_G['setting']['privacy']) ? array() : (is_array($_G['setting']['privacy']) ? $_G['setting']['privacy'] : dunserialize($_G['setting']['privacy']));
  1282. $_G[$var]['privacy'] = empty($_G[$var]['privacy'])? array() : is_array($_G[$var]['privacy']) ? $_G[$var]['privacy'] : dunserialize($_G[$var]['privacy']);
  1283. foreach (array('feed','view','profile') as $pkey) {
  1284. if(empty($_G[$var]['privacy'][$pkey]) && !isset($_G[$var]['privacy'][$pkey])) {
  1285. $_G[$var]['privacy'][$pkey] = isset($_G['setting']['privacy'][$pkey]) ? $_G['setting']['privacy'][$pkey] : array();
  1286. }
  1287. }
  1288. $_G[$var]['acceptemail'] = empty($_G[$var]['acceptemail'])? array() : dunserialize($_G[$var]['acceptemail']);
  1289. if(empty($_G[$var]['acceptemail'])) {
  1290. $_G[$var]['acceptemail'] = empty($_G['setting']['acceptemail'])?array():dunserialize($_G['setting']['acceptemail']);
  1291. }
  1292. }
  1293. } else {
  1294. C::t('common_member_'.$tablename.$ext)->insert(array('uid'=>$uid));
  1295. $_G[$var] = array();
  1296. }
  1297. }
  1298. $values = array_merge($values, $_G[$var]);
  1299. }
  1300. }
  1301. function runlog($file, $message, $halt=0) {
  1302. helper_log::runlog($file, $message, $halt);
  1303. }
  1304. function stripsearchkey($string) {
  1305. $string = trim($string);
  1306. $string = str_replace('*', '%', addcslashes($string, '%_'));
  1307. return $string;
  1308. }
  1309. function dmkdir($dir, $mode = 0777, $makeindex = TRUE){
  1310. if(!is_dir($dir)) {
  1311. dmkdir(dirname($dir), $mode, $makeindex);
  1312. @mkdir($dir, $mode);
  1313. if(!empty($makeindex)) {
  1314. @touch($dir.'/index.html'); @chmod($dir.'/index.html', 0777);
  1315. }
  1316. }
  1317. return true;
  1318. }
  1319. function dreferer($default = '') {
  1320. global $_G;
  1321. $default = empty($default) ? $GLOBALS['_t_curapp'] : '';
  1322. $_G['referer'] = !empty($_GET['referer']) ? $_GET['referer'] : $_SERVER['HTTP_REFERER'];
  1323. $_G['referer'] = substr($_G['referer'], -1) == '?' ? substr($_G['referer'], 0, -1) : $_G['referer'];
  1324. if(strpos($_G['referer'], 'member.php?mod=logging')) {
  1325. $_G['referer'] = $default;
  1326. }
  1327. $_G['referer'] = dhtmlspecialchars($_G['referer'], ENT_QUOTES);
  1328. $_G['referer'

Large files files are truncated, but you can click here to view the full file