function_core.php 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  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 36342 2017-01-09 01:15:30Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. define('DISCUZ_CORE_FUNCTION', true);
  12. function durlencode($url) {
  13. static $fix = array('%21', '%2A','%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
  14. static $replacements = array('!', '*', ';', ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");
  15. return str_replace($fix, $replacements, urlencode($url));
  16. }
  17. function system_error($message, $show = true, $save = true, $halt = true) {
  18. discuz_error::system_error($message, $show, $save, $halt);
  19. }
  20. function updatesession() {
  21. return C::app()->session->updatesession();
  22. }
  23. function setglobal($key , $value, $group = null) {
  24. global $_G;
  25. $key = explode('/', $group === null ? $key : $group.'/'.$key);
  26. $p = &$_G;
  27. foreach ($key as $k) {
  28. if(!isset($p[$k]) || !is_array($p[$k])) {
  29. $p[$k] = array();
  30. }
  31. $p = &$p[$k];
  32. }
  33. $p = $value;
  34. return true;
  35. }
  36. function getglobal($key, $group = null) {
  37. global $_G;
  38. $key = explode('/', $group === null ? $key : $group.'/'.$key);
  39. $v = &$_G;
  40. foreach ($key as $k) {
  41. if (!isset($v[$k])) {
  42. return null;
  43. }
  44. $v = &$v[$k];
  45. }
  46. return $v;
  47. }
  48. function getgpc($k, $type='GP') {
  49. $type = strtoupper($type);
  50. switch($type) {
  51. case 'G': $var = &$_GET; break;
  52. case 'P': $var = &$_POST; break;
  53. case 'C': $var = &$_COOKIE; break;
  54. default:
  55. if(isset($_GET[$k])) {
  56. $var = &$_GET;
  57. } else {
  58. $var = &$_POST;
  59. }
  60. break;
  61. }
  62. return isset($var[$k]) ? $var[$k] : NULL;
  63. }
  64. function getuserbyuid($uid, $fetch_archive = 0) {
  65. static $users = array();
  66. if(empty($users[$uid])) {
  67. $users[$uid] = C::t('common_member'.($fetch_archive === 2 ? '_archive' : ''))->fetch($uid);
  68. if($fetch_archive === 1 && empty($users[$uid])) {
  69. $users[$uid] = C::t('common_member_archive')->fetch($uid);
  70. }
  71. }
  72. if(!isset($users[$uid]['self']) && $uid == getglobal('uid') && getglobal('uid')) {
  73. $users[$uid]['self'] = 1;
  74. }
  75. return $users[$uid];
  76. }
  77. function getuserprofile($field) {
  78. global $_G;
  79. if(isset($_G['member'][$field])) {
  80. return $_G['member'][$field];
  81. }
  82. static $tablefields = array(
  83. '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'),
  84. 'status' => array('regip','lastip','lastvisit','lastactivity','lastpost','lastsendmail','invisible','buyercredit','sellercredit','favtimes','sharetimes','profileprogress'),
  85. 'field_forum' => array('publishfeed','customshow','customstatus','medals','sightml','groupterms','authstr','groups','attentiongroup'),
  86. 'field_home' => array('videophoto','spacename','spacedescription','domain','addsize','addfriend','menunum','theme','spacecss','blockposition','recentnote','spacenote','privacy','feedfriend','acceptemail','magicgift','stickblogs'),
  87. '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'),
  88. 'verify' => array('verify1', 'verify2', 'verify3', 'verify4', 'verify5', 'verify6', 'verify7'),
  89. );
  90. $profiletable = '';
  91. foreach($tablefields as $table => $fields) {
  92. if(in_array($field, $fields)) {
  93. $profiletable = $table;
  94. break;
  95. }
  96. }
  97. if($profiletable) {
  98. if(is_array($_G['member']) && $_G['member']['uid']) {
  99. space_merge($_G['member'], $profiletable);
  100. } else {
  101. foreach($tablefields[$profiletable] as $k) {
  102. $_G['member'][$k] = '';
  103. }
  104. }
  105. return $_G['member'][$field];
  106. }
  107. return null;
  108. }
  109. function daddslashes($string, $force = 1) {
  110. if(is_array($string)) {
  111. $keys = array_keys($string);
  112. foreach($keys as $key) {
  113. $val = $string[$key];
  114. unset($string[$key]);
  115. $string[addslashes($key)] = daddslashes($val, $force);
  116. }
  117. } else {
  118. $string = addslashes($string);
  119. }
  120. return $string;
  121. }
  122. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  123. $ckey_length = 4;
  124. $key = md5($key != '' ? $key : getglobal('authkey'));
  125. $keya = md5(substr($key, 0, 16));
  126. $keyb = md5(substr($key, 16, 16));
  127. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  128. $cryptkey = $keya.md5($keya.$keyc);
  129. $key_length = strlen($cryptkey);
  130. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  131. $string_length = strlen($string);
  132. $result = '';
  133. $box = range(0, 255);
  134. $rndkey = array();
  135. for($i = 0; $i <= 255; $i++) {
  136. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  137. }
  138. for($j = $i = 0; $i < 256; $i++) {
  139. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  140. $tmp = $box[$i];
  141. $box[$i] = $box[$j];
  142. $box[$j] = $tmp;
  143. }
  144. for($a = $j = $i = 0; $i < $string_length; $i++) {
  145. $a = ($a + 1) % 256;
  146. $j = ($j + $box[$a]) % 256;
  147. $tmp = $box[$a];
  148. $box[$a] = $box[$j];
  149. $box[$j] = $tmp;
  150. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  151. }
  152. if($operation == 'DECODE') {
  153. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  154. return substr($result, 26);
  155. } else {
  156. return '';
  157. }
  158. } else {
  159. return $keyc.str_replace('=', '', base64_encode($result));
  160. }
  161. }
  162. function fsocketopen($hostname, $port = 80, &$errno, &$errstr, $timeout = 15) {
  163. $fp = '';
  164. if(function_exists('fsockopen')) {
  165. $fp = @fsockopen($hostname, $port, $errno, $errstr, $timeout);
  166. } elseif(function_exists('pfsockopen')) {
  167. $fp = @pfsockopen($hostname, $port, $errno, $errstr, $timeout);
  168. } elseif(function_exists('stream_socket_client')) {
  169. $fp = @stream_socket_client($hostname.':'.$port, $errno, $errstr, $timeout);
  170. }
  171. return $fp;
  172. }
  173. function dfsockopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE', $allowcurl = TRUE, $position = 0, $files = array()) {
  174. require_once libfile('function/filesock');
  175. return _dfsockopen($url, $limit, $post, $cookie, $bysocket, $ip, $timeout, $block, $encodetype, $allowcurl, $position, $files);
  176. }
  177. function dhtmlspecialchars($string, $flags = null) {
  178. if(is_array($string)) {
  179. foreach($string as $key => $val) {
  180. $string[$key] = dhtmlspecialchars($val, $flags);
  181. }
  182. } else {
  183. if($flags === null) {
  184. $string = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string);
  185. if(strpos($string, '&amp;#') !== false) {
  186. $string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $string);
  187. }
  188. } else {
  189. if(PHP_VERSION < '5.4.0') {
  190. $string = htmlspecialchars($string, $flags);
  191. } else {
  192. if(strtolower(CHARSET) == 'utf-8') {
  193. $charset = 'UTF-8';
  194. } else {
  195. $charset = 'ISO-8859-1';
  196. }
  197. $string = htmlspecialchars($string, $flags, $charset);
  198. }
  199. }
  200. }
  201. return $string;
  202. }
  203. function dexit($message = '') {
  204. echo $message;
  205. output();
  206. exit();
  207. }
  208. function dheader($string, $replace = true, $http_response_code = 0) {
  209. $islocation = substr(strtolower(trim($string)), 0, 8) == 'location';
  210. if(defined('IN_MOBILE') && strpos($string, 'mobile') === false && $islocation) {
  211. if (strpos($string, '?') === false) {
  212. $string = $string.'?mobile='.IN_MOBILE;
  213. } else {
  214. if(strpos($string, '#') === false) {
  215. $string = $string.'&mobile='.IN_MOBILE;
  216. } else {
  217. $str_arr = explode('#', $string);
  218. $str_arr[0] = $str_arr[0].'&mobile='.IN_MOBILE;
  219. $string = implode('#', $str_arr);
  220. }
  221. }
  222. }
  223. $string = str_replace(array("\r", "\n"), array('', ''), $string);
  224. if(empty($http_response_code) || PHP_VERSION < '4.3' ) {
  225. @header($string, $replace);
  226. } else {
  227. @header($string, $replace, $http_response_code);
  228. }
  229. if($islocation) {
  230. exit();
  231. }
  232. }
  233. function dsetcookie($var, $value = '', $life = 0, $prefix = 1, $httponly = false) {
  234. global $_G;
  235. $config = $_G['config']['cookie'];
  236. $_G['cookie'][$var] = $value;
  237. $var = ($prefix ? $config['cookiepre'] : '').$var;
  238. $_COOKIE[$var] = $value;
  239. if($value == '' || $life < 0) {
  240. $value = '';
  241. $life = -1;
  242. }
  243. if(defined('IN_MOBILE')) {
  244. $httponly = false;
  245. }
  246. $life = $life > 0 ? getglobal('timestamp') + $life : ($life < 0 ? getglobal('timestamp') - 31536000 : 0);
  247. $path = $httponly && PHP_VERSION < '5.2.0' ? $config['cookiepath'].'; HttpOnly' : $config['cookiepath'];
  248. $secure = $_SERVER['SERVER_PORT'] == 443 ? 1 : 0;
  249. if(PHP_VERSION < '5.2.0') {
  250. setcookie($var, $value, $life, $path, $config['cookiedomain'], $secure);
  251. } else {
  252. setcookie($var, $value, $life, $path, $config['cookiedomain'], $secure, $httponly);
  253. }
  254. }
  255. function getcookie($key) {
  256. global $_G;
  257. return isset($_G['cookie'][$key]) ? $_G['cookie'][$key] : '';
  258. }
  259. function fileext($filename) {
  260. return addslashes(strtolower(substr(strrchr($filename, '.'), 1, 10)));
  261. }
  262. function formhash($specialadd = '') {
  263. global $_G;
  264. $hashadd = defined('IN_ADMINCP') ? 'Only For Discuz! Admin Control Panel' : '';
  265. return substr(md5(substr($_G['timestamp'], 0, -7).$_G['username'].$_G['uid'].$_G['authkey'].$hashadd.$specialadd), 8, 8);
  266. }
  267. function checkrobot($useragent = '') {
  268. static $kw_spiders = array('bot', 'crawl', 'spider' ,'slurp', 'sohu-search', 'lycos', 'robozilla');
  269. static $kw_browsers = array('msie', 'netscape', 'opera', 'konqueror', 'mozilla');
  270. $useragent = strtolower(empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent);
  271. if(strpos($useragent, 'http://') === false && dstrpos($useragent, $kw_browsers)) return false;
  272. if(dstrpos($useragent, $kw_spiders)) return true;
  273. return false;
  274. }
  275. function checkmobile() {
  276. global $_G;
  277. $mobile = array();
  278. static $touchbrowser_list =array('iphone', 'android', 'phone', 'mobile', 'wap', 'netfront', 'java', 'opera mobi', 'opera mini',
  279. 'ucweb', 'windows ce', 'symbian', 'series', 'webos', 'sony', 'blackberry', 'dopod', 'nokia', 'samsung',
  280. 'palmsource', 'xda', 'pieplus', 'meizu', 'midp', 'cldc', 'motorola', 'foma', 'docomo', 'up.browser',
  281. 'up.link', 'blazer', 'helio', 'hosin', 'huawei', 'novarra', 'coolpad', 'webos', 'techfaith', 'palmsource',
  282. 'alcatel', 'amoi', 'ktouch', 'nexian', 'ericsson', 'philips', 'sagem', 'wellcom', 'bunjalloo', 'maui', 'smartphone',
  283. 'iemobile', 'spice', 'bird', 'zte-', 'longcos', 'pantech', 'gionee', 'portalmmm', 'jig browser', 'hiptop',
  284. 'benq', 'haier', '^lct', '320x320', '240x320', '176x220', 'windows phone');
  285. static $wmlbrowser_list = array('cect', 'compal', 'ctl', 'lg', 'nec', 'tcl', 'alcatel', 'ericsson', 'bird', 'daxian', 'dbtel', 'eastcom',
  286. 'pantech', 'dopod', 'philips', 'haier', 'konka', 'kejian', 'lenovo', 'benq', 'mot', 'soutec', 'nokia', 'sagem', 'sgh',
  287. 'sed', 'capitel', 'panasonic', 'sonyericsson', 'sharp', 'amoi', 'panda', 'zte');
  288. static $pad_list = array('ipad');
  289. $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
  290. if(dstrpos($useragent, $pad_list)) {
  291. return false;
  292. }
  293. if(($v = dstrpos($useragent, $touchbrowser_list, true))){
  294. $_G['mobile'] = $v;
  295. return '2';
  296. }
  297. if(($v = dstrpos($useragent, $wmlbrowser_list))) {
  298. $_G['mobile'] = $v;
  299. return '3'; //wml版
  300. }
  301. $brower = array('mozilla', 'chrome', 'safari', 'opera', 'm3gate', 'winwap', 'openwave', 'myop');
  302. if(dstrpos($useragent, $brower)) return false;
  303. $_G['mobile'] = 'unknown';
  304. if(isset($_G['mobiletpl'][$_GET['mobile']])) {
  305. return true;
  306. } else {
  307. return false;
  308. }
  309. }
  310. function dstrpos($string, $arr, $returnvalue = false) {
  311. if(empty($string)) return false;
  312. foreach((array)$arr as $v) {
  313. if(strpos($string, $v) !== false) {
  314. $return = $returnvalue ? $v : true;
  315. return $return;
  316. }
  317. }
  318. return false;
  319. }
  320. function isemail($email) {
  321. return strlen($email) > 6 && strlen($email) <= 32 && preg_match("/^([A-Za-z0-9\-_.+]+)@([A-Za-z0-9\-]+[.][A-Za-z0-9\-.]+)$/", $email);
  322. }
  323. function quescrypt($questionid, $answer) {
  324. return $questionid > 0 && $answer != '' ? substr(md5($answer.md5($questionid)), 16, 8) : '';
  325. }
  326. function random($length, $numeric = 0) {
  327. $seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  328. $seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
  329. if($numeric) {
  330. $hash = '';
  331. } else {
  332. $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
  333. $length--;
  334. }
  335. $max = strlen($seed) - 1;
  336. for($i = 0; $i < $length; $i++) {
  337. $hash .= $seed{mt_rand(0, $max)};
  338. }
  339. return $hash;
  340. }
  341. function strexists($string, $find) {
  342. return !(strpos($string, $find) === FALSE);
  343. }
  344. function avatar($uid, $size = 'middle', $returnsrc = FALSE, $real = FALSE, $static = FALSE, $ucenterurl = '') {
  345. global $_G;
  346. if($_G['setting']['plugins']['func'][HOOKTYPE]['avatar']) {
  347. $_G['hookavatar'] = '';
  348. $param = func_get_args();
  349. hookscript('avatar', 'global', 'funcs', array('param' => $param), 'avatar');
  350. if($_G['hookavatar']) {
  351. return $_G['hookavatar'];
  352. }
  353. }
  354. static $staticavatar;
  355. if($staticavatar === null) {
  356. $staticavatar = $_G['setting']['avatarmethod'];
  357. }
  358. $ucenterurl = empty($ucenterurl) ? $_G['setting']['ucenterurl'] : $ucenterurl;
  359. $size = in_array($size, array('big', 'middle', 'small')) ? $size : 'middle';
  360. $uid = abs(intval($uid));
  361. if(!$staticavatar && !$static) {
  362. return $returnsrc ? $ucenterurl.'/avatar.php?uid='.$uid.'&size='.$size.($real ? '&type=real' : '') : '<img src="'.$ucenterurl.'/avatar.php?uid='.$uid.'&size='.$size.($real ? '&type=real' : '').'" />';
  363. } else {
  364. $uid = sprintf("%09d", $uid);
  365. $dir1 = substr($uid, 0, 3);
  366. $dir2 = substr($uid, 3, 2);
  367. $dir3 = substr($uid, 5, 2);
  368. $file = $ucenterurl.'/data/avatar/'.$dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).($real ? '_real' : '').'_avatar_'.$size.'.jpg';
  369. return $returnsrc ? $file : '<img src="'.$file.'" onerror="this.onerror=null;this.src=\''.$ucenterurl.'/images/noavatar_'.$size.'.gif\'" />';
  370. }
  371. }
  372. function lang($file, $langvar = null, $vars = array(), $default = null) {
  373. global $_G;
  374. $fileinput = $file;
  375. list($path, $file) = explode('/', $file);
  376. if(!$file) {
  377. $file = $path;
  378. $path = '';
  379. }
  380. if(strpos($file, ':') !== false) {
  381. $path = 'plugin';
  382. list($file) = explode(':', $file);
  383. }
  384. if($path != 'plugin') {
  385. $key = $path == '' ? $file : $path.'_'.$file;
  386. if(!isset($_G['lang'][$key])) {
  387. include DISCUZ_ROOT.'./source/language/'.($path == '' ? '' : $path.'/').'lang_'.$file.'.php';
  388. $_G['lang'][$key] = $lang;
  389. }
  390. if(defined('IN_MOBILE') && !defined('TPL_DEFAULT')) {
  391. include DISCUZ_ROOT.'./source/language/mobile/lang_template.php';
  392. $_G['lang'][$key] = array_merge($_G['lang'][$key], $lang);
  393. }
  394. if($file != 'error' && !isset($_G['cache']['pluginlanguage_system'])) {
  395. loadcache('pluginlanguage_system');
  396. }
  397. if(!isset($_G['hooklang'][$fileinput])) {
  398. if(isset($_G['cache']['pluginlanguage_system'][$fileinput]) && is_array($_G['cache']['pluginlanguage_system'][$fileinput])) {
  399. $_G['lang'][$key] = array_merge($_G['lang'][$key], $_G['cache']['pluginlanguage_system'][$fileinput]);
  400. }
  401. $_G['hooklang'][$fileinput] = true;
  402. }
  403. $returnvalue = &$_G['lang'];
  404. } else {
  405. if(empty($_G['config']['plugindeveloper'])) {
  406. loadcache('pluginlanguage_script');
  407. } elseif(!isset($_G['cache']['pluginlanguage_script'][$file]) && preg_match("/^[a-z]+[a-z0-9_]*$/i", $file)) {
  408. if(@include(DISCUZ_ROOT.'./data/plugindata/'.$file.'.lang.php')) {
  409. $_G['cache']['pluginlanguage_script'][$file] = $scriptlang[$file];
  410. } else {
  411. loadcache('pluginlanguage_script');
  412. }
  413. }
  414. $returnvalue = & $_G['cache']['pluginlanguage_script'];
  415. $key = &$file;
  416. }
  417. $return = $langvar !== null ? (isset($returnvalue[$key][$langvar]) ? $returnvalue[$key][$langvar] : null) : $returnvalue[$key];
  418. $return = $return === null ? ($default !== null ? $default : $langvar) : $return;
  419. $searchs = $replaces = array();
  420. if($vars && is_array($vars)) {
  421. foreach($vars as $k => $v) {
  422. $searchs[] = '{'.$k.'}';
  423. $replaces[] = $v;
  424. }
  425. }
  426. if(is_string($return) && strpos($return, '{_G/') !== false) {
  427. preg_match_all('/\{_G\/(.+?)\}/', $return, $gvar);
  428. foreach($gvar[0] as $k => $v) {
  429. $searchs[] = $v;
  430. $replaces[] = getglobal($gvar[1][$k]);
  431. }
  432. }
  433. $return = str_replace($searchs, $replaces, $return);
  434. return $return;
  435. }
  436. function checktplrefresh($maintpl, $subtpl, $timecompare, $templateid, $cachefile, $tpldir, $file) {
  437. static $tplrefresh, $timestamp, $targettplname;
  438. if($tplrefresh === null) {
  439. $tplrefresh = getglobal('config/output/tplrefresh');
  440. $timestamp = getglobal('timestamp');
  441. }
  442. if(empty($timecompare) || $tplrefresh == 1 || ($tplrefresh > 1 && !($timestamp % $tplrefresh))) {
  443. if(empty($timecompare) || @filemtime(DISCUZ_ROOT.$subtpl) > $timecompare) {
  444. require_once DISCUZ_ROOT.'/source/class/class_template.php';
  445. $template = new template();
  446. $template->parse_template($maintpl, $templateid, $tpldir, $file, $cachefile);
  447. if($targettplname === null) {
  448. $targettplname = getglobal('style/tplfile');
  449. if(!empty($targettplname)) {
  450. include_once libfile('function/block');
  451. $targettplname = strtr($targettplname, ':', '_');
  452. update_template_block($targettplname, getglobal('style/tpldirectory'), $template->blocks);
  453. }
  454. $targettplname = true;
  455. }
  456. return TRUE;
  457. }
  458. }
  459. return FALSE;
  460. }
  461. function template($file, $templateid = 0, $tpldir = '', $gettplfile = 0, $primaltpl='') {
  462. global $_G;
  463. if($_G['setting']['plugins']['func'][HOOKTYPE]['template']) {
  464. $param = func_get_args();
  465. $hookreturn = hookscript('template', 'global', 'funcs', array('param' => $param, 'caller' => 'template'), 'template');
  466. if($hookreturn) {
  467. return $hookreturn;
  468. }
  469. }
  470. static $_init_style = false;
  471. if($_init_style === false) {
  472. C::app()->_init_style();
  473. $_init_style = true;
  474. }
  475. $oldfile = $file;
  476. if(strpos($file, ':') !== false) {
  477. $clonefile = '';
  478. list($templateid, $file, $clonefile) = explode(':', $file);
  479. $oldfile = $file;
  480. $file = empty($clonefile) ? $file : $file.'_'.$clonefile;
  481. if($templateid == 'diy') {
  482. $indiy = false;
  483. $_G['style']['tpldirectory'] = $tpldir ? $tpldir : (defined('TPLDIR') ? TPLDIR : '');
  484. $_G['style']['prefile'] = '';
  485. $diypath = DISCUZ_ROOT.'./data/diy/'.$_G['style']['tpldirectory'].'/'; //DIY模板文件目录
  486. $preend = '_diy_preview';
  487. $_GET['preview'] = !empty($_GET['preview']) ? $_GET['preview'] : '';
  488. $curtplname = $oldfile;
  489. $basescript = $_G['mod'] == 'viewthread' && !empty($_G['thread']) ? 'forum' : $_G['basescript'];
  490. if(isset($_G['cache']['diytemplatename'.$basescript])) {
  491. $diytemplatename = &$_G['cache']['diytemplatename'.$basescript];
  492. } else {
  493. if(!isset($_G['cache']['diytemplatename'])) {
  494. loadcache('diytemplatename');
  495. }
  496. $diytemplatename = &$_G['cache']['diytemplatename'];
  497. }
  498. $tplsavemod = 0;
  499. 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')) {
  500. $tpldir = 'data/diy/'.$_G['style']['tpldirectory'].'/';
  501. !$gettplfile && $_G['style']['tplsavemod'] = $tplsavemod;
  502. $curtplname = $file;
  503. if(isset($_GET['diy']) && $_GET['diy'] == 'yes' || isset($_GET['diy']) && $_GET['preview'] == 'yes') { //DIY模式或预览模式下做以下判断
  504. $flag = file_exists($diypath.$file.$preend.'.htm');
  505. if($_GET['preview'] == 'yes') {
  506. $file .= $flag ? $preend : '';
  507. } else {
  508. $_G['style']['prefile'] = $flag ? 1 : '';
  509. }
  510. }
  511. $indiy = true;
  512. } else {
  513. $file = $primaltpl ? $primaltpl : $oldfile;
  514. }
  515. $tplrefresh = $_G['config']['output']['tplrefresh'];
  516. if($indiy && ($tplrefresh ==1 || ($tplrefresh > 1 && !($_G['timestamp'] % $tplrefresh))) && filemtime($diypath.$file.'.htm') < filemtime(DISCUZ_ROOT.$_G['style']['tpldirectory'].'/'.($primaltpl ? $primaltpl : $oldfile).'.htm')) {
  517. if (!updatediytemplate($file, $_G['style']['tpldirectory'])) {
  518. unlink($diypath.$file.'.htm');
  519. $tpldir = '';
  520. }
  521. }
  522. if (!$gettplfile && empty($_G['style']['tplfile'])) {
  523. $_G['style']['tplfile'] = empty($clonefile) ? $curtplname : $oldfile.':'.$clonefile;
  524. }
  525. $_G['style']['prefile'] = !empty($_GET['preview']) && $_GET['preview'] == 'yes' ? '' : $_G['style']['prefile'];
  526. } else {
  527. $tpldir = './source/plugin/'.$templateid.'/template';
  528. }
  529. }
  530. $file .= !empty($_G['inajax']) && ($file == 'common/header' || $file == 'common/footer') ? '_ajax' : '';
  531. $tpldir = $tpldir ? $tpldir : (defined('TPLDIR') ? TPLDIR : '');
  532. $templateid = $templateid ? $templateid : (defined('TEMPLATEID') ? TEMPLATEID : '');
  533. $filebak = $file;
  534. if(defined('IN_MOBILE') && !defined('TPL_DEFAULT') && strpos($file, $_G['mobiletpl'][IN_MOBILE].'/') === false || (isset($_G['forcemobilemessage']) && $_G['forcemobilemessage'])) {
  535. if(IN_MOBILE == 2) {
  536. $oldfile .= !empty($_G['inajax']) && ($oldfile == 'common/header' || $oldfile == 'common/footer') ? '_ajax' : '';
  537. }
  538. $file = $_G['mobiletpl'][IN_MOBILE].'/'.$oldfile;
  539. }
  540. if(!$tpldir) {
  541. $tpldir = './template/default';
  542. }
  543. $tplfile = $tpldir.'/'.$file.'.htm';
  544. $file == 'common/header' && defined('CURMODULE') && CURMODULE && $file = 'common/header_'.$_G['basescript'].'_'.CURMODULE;
  545. if(defined('IN_MOBILE') && !defined('TPL_DEFAULT')) {
  546. if(strpos($tpldir, 'plugin')) {
  547. if(!file_exists(DISCUZ_ROOT.$tpldir.'/'.$file.'.htm') && !file_exists(DISCUZ_ROOT.$tpldir.'/'.$file.'.php')) {
  548. $url = $_SERVER['REQUEST_URI'].(strexists($_SERVER['REQUEST_URI'], '?') ? '&' : '?').'mobile=no';
  549. showmessage('mobile_template_no_found', '', array('url' => $url));
  550. } else {
  551. $mobiletplfile = $tpldir.'/'.$file.'.htm';
  552. }
  553. }
  554. !$mobiletplfile && $mobiletplfile = $file.'.htm';
  555. if(strpos($tpldir, 'plugin') && (file_exists(DISCUZ_ROOT.$mobiletplfile) || file_exists(substr(DISCUZ_ROOT.$mobiletplfile, 0, -4).'.php'))) {
  556. $tplfile = $mobiletplfile;
  557. } elseif(!file_exists(DISCUZ_ROOT.TPLDIR.'/'.$mobiletplfile) && !file_exists(substr(DISCUZ_ROOT.TPLDIR.'/'.$mobiletplfile, 0, -4).'.php')) {
  558. $mobiletplfile = './template/default/'.$file.'.htm';
  559. if(!file_exists(DISCUZ_ROOT.$mobiletplfile) && !$_G['forcemobilemessage']) {
  560. $tplfile = str_replace($_G['mobiletpl'][IN_MOBILE].'/', '', $tplfile);
  561. $file = str_replace($_G['mobiletpl'][IN_MOBILE].'/', '', $file);
  562. define('TPL_DEFAULT', true);
  563. } else {
  564. $tplfile = $mobiletplfile;
  565. }
  566. } else {
  567. $tplfile = TPLDIR.'/'.$mobiletplfile;
  568. }
  569. }
  570. $cachefile = './data/template/'.(defined('STYLEID') ? STYLEID.'_' : '_').$templateid.'_'.str_replace('/', '_', $file).'.tpl.php';
  571. if($templateid != 1 && !file_exists(DISCUZ_ROOT.$tplfile) && !file_exists(substr(DISCUZ_ROOT.$tplfile, 0, -4).'.php')
  572. && !file_exists(DISCUZ_ROOT.($tplfile = $tpldir.$filebak.'.htm'))) {
  573. $tplfile = './template/default/'.$filebak.'.htm';
  574. }
  575. if($gettplfile) {
  576. return $tplfile;
  577. }
  578. checktplrefresh($tplfile, $tplfile, @filemtime(DISCUZ_ROOT.$cachefile), $templateid, $cachefile, $tpldir, $file);
  579. return DISCUZ_ROOT.$cachefile;
  580. }
  581. function dsign($str, $length = 16){
  582. return substr(md5($str.getglobal('config/security/authkey')), 0, ($length ? max(8, $length) : 16));
  583. }
  584. function modauthkey($id) {
  585. return md5(getglobal('username').getglobal('uid').getglobal('authkey').substr(TIMESTAMP, 0, -7).$id);
  586. }
  587. function getcurrentnav() {
  588. global $_G;
  589. if(!empty($_G['mnid'])) {
  590. return $_G['mnid'];
  591. }
  592. $mnid = '';
  593. $_G['basefilename'] = $_G['basefilename'] == $_G['basescript'] ? $_G['basefilename'] : $_G['basescript'].'.php';
  594. if(isset($_G['setting']['navmns'][$_G['basefilename']])) {
  595. if($_G['basefilename'] == 'home.php' && $_GET['mod'] == 'space' && (empty($_GET['do']) || in_array($_GET['do'], array('follow', 'view')))) {
  596. $_GET['mod'] = 'follow';
  597. }
  598. foreach($_G['setting']['navmns'][$_G['basefilename']] as $navmn) {
  599. 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'))) {
  600. $mnid = $navmn[1];
  601. }
  602. }
  603. }
  604. if(!$mnid && isset($_G['setting']['navdms'])) {
  605. foreach($_G['setting']['navdms'] as $navdm => $navid) {
  606. if(strpos(strtolower($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']), $navdm) !== false && strpos(strtolower($_SERVER['HTTP_HOST']), $navdm) === false) {
  607. $mnid = $navid;
  608. break;
  609. }
  610. }
  611. }
  612. if(!$mnid && isset($_G['setting']['navmn'][$_G['basefilename']])) {
  613. $mnid = $_G['setting']['navmn'][$_G['basefilename']];
  614. }
  615. return $mnid;
  616. }
  617. function loaducenter() {
  618. require_once DISCUZ_ROOT.'./config/config_ucenter.php';
  619. require_once DISCUZ_ROOT.'./uc_client/client.php';
  620. }
  621. function loadcache($cachenames, $force = false) {
  622. global $_G;
  623. static $loadedcache = array();
  624. $cachenames = is_array($cachenames) ? $cachenames : array($cachenames);
  625. $caches = array();
  626. foreach ($cachenames as $k) {
  627. if(!isset($loadedcache[$k]) || $force) {
  628. $caches[] = $k;
  629. $loadedcache[$k] = true;
  630. }
  631. }
  632. if(!empty($caches)) {
  633. $cachedata = C::t('common_syscache')->fetch_all($caches);
  634. foreach($cachedata as $cname => $data) {
  635. if($cname == 'setting') {
  636. $_G['setting'] = $data;
  637. } elseif($cname == 'usergroup_'.$_G['groupid']) {
  638. $_G['cache'][$cname] = $_G['group'] = $data;
  639. } elseif($cname == 'style_default') {
  640. $_G['cache'][$cname] = $_G['style'] = $data;
  641. } elseif($cname == 'grouplevels') {
  642. $_G['grouplevels'] = $data;
  643. } else {
  644. $_G['cache'][$cname] = $data;
  645. }
  646. }
  647. }
  648. return true;
  649. }
  650. function dgmdate($timestamp, $format = 'dt', $timeoffset = '9999', $uformat = '') {
  651. global $_G;
  652. $format == 'u' && !$_G['setting']['dateconvert'] && $format = 'dt';
  653. static $dformat, $tformat, $dtformat, $offset, $lang;
  654. if($dformat === null) {
  655. $dformat = getglobal('setting/dateformat');
  656. $tformat = getglobal('setting/timeformat');
  657. $dtformat = $dformat.' '.$tformat;
  658. $offset = getglobal('member/timeoffset');
  659. $sysoffset = getglobal('setting/timeoffset');
  660. $offset = $offset == 9999 ? ($sysoffset ? $sysoffset : 0) : $offset;
  661. $lang = lang('core', 'date');
  662. }
  663. $timeoffset = $timeoffset == 9999 ? $offset : $timeoffset;
  664. $timestamp += $timeoffset * 3600;
  665. $format = empty($format) || $format == 'dt' ? $dtformat : ($format == 'd' ? $dformat : ($format == 't' ? $tformat : $format));
  666. if($format == 'u') {
  667. $todaytimestamp = TIMESTAMP - (TIMESTAMP + $timeoffset * 3600) % 86400 + $timeoffset * 3600;
  668. $s = gmdate(!$uformat ? $dtformat : $uformat, $timestamp);
  669. $time = TIMESTAMP + $timeoffset * 3600 - $timestamp;
  670. if($timestamp >= $todaytimestamp) {
  671. if($time > 3600) {
  672. $return = intval($time / 3600).'&nbsp;'.$lang['hour'].$lang['before'];
  673. } elseif($time > 1800) {
  674. $return = $lang['half'].$lang['hour'].$lang['before'];
  675. } elseif($time > 60) {
  676. $return = intval($time / 60).'&nbsp;'.$lang['min'].$lang['before'];
  677. } elseif($time > 0) {
  678. $return = $time.'&nbsp;'.$lang['sec'].$lang['before'];
  679. } elseif($time == 0) {
  680. $return = $lang['now'];
  681. } else {
  682. $return = $s;
  683. }
  684. if($time >=0 && !defined('IN_MOBILE')) {
  685. $return = '<span title="'.$s.'">'.$return.'</span>';
  686. }
  687. } elseif(($days = intval(($todaytimestamp - $timestamp) / 86400)) >= 0 && $days < 7) {
  688. if($days == 0) {
  689. $return = $lang['yday'].'&nbsp;'.gmdate($tformat, $timestamp);
  690. } elseif($days == 1) {
  691. $return = $lang['byday'].'&nbsp;'.gmdate($tformat, $timestamp);
  692. } else {
  693. $return = ($days + 1).'&nbsp;'.$lang['day'].$lang['before'];
  694. }
  695. if(!defined('IN_MOBILE')) {
  696. $return = '<span title="'.$s.'">'.$return.'</span>';
  697. }
  698. } else {
  699. $return = $s;
  700. }
  701. return $return;
  702. } else {
  703. return gmdate($format, $timestamp);
  704. }
  705. }
  706. function dmktime($date) {
  707. if(strpos($date, '-')) {
  708. $time = explode('-', $date);
  709. return mktime(0, 0, 0, $time[1], $time[2], $time[0]);
  710. }
  711. return 0;
  712. }
  713. function dnumber($number) {
  714. return abs($number) > 10000 ? '<span title="'.$number.'">'.intval($number / 10000).lang('core', '10k').'</span>' : $number;
  715. }
  716. function savecache($cachename, $data) {
  717. C::t('common_syscache')->insert($cachename, $data);
  718. }
  719. function save_syscache($cachename, $data) {
  720. savecache($cachename, $data);
  721. }
  722. function block_get($parameter) {
  723. include_once libfile('function/block');
  724. block_get_batch($parameter);
  725. }
  726. function block_display($bid) {
  727. include_once libfile('function/block');
  728. block_display_batch($bid);
  729. }
  730. function dimplode($array) {
  731. if(!empty($array)) {
  732. $array = array_map('addslashes', $array);
  733. return "'".implode("','", is_array($array) ? $array : array($array))."'";
  734. } else {
  735. return 0;
  736. }
  737. }
  738. function libfile($libname, $folder = '') {
  739. $libpath = '/source/'.$folder;
  740. if(strstr($libname, '/')) {
  741. list($pre, $name) = explode('/', $libname);
  742. $path = "{$libpath}/{$pre}/{$pre}_{$name}";
  743. } else {
  744. $path = "{$libpath}/{$libname}";
  745. }
  746. return preg_match('/^[\w\d\/_]+$/i', $path) ? realpath(DISCUZ_ROOT.$path.'.php') : false;
  747. }
  748. function dstrlen($str) {
  749. if(strtolower(CHARSET) != 'utf-8') {
  750. return strlen($str);
  751. }
  752. $count = 0;
  753. for($i = 0; $i < strlen($str); $i++){
  754. $value = ord($str[$i]);
  755. if($value > 127) {
  756. $count++;
  757. if($value >= 192 && $value <= 223) $i++;
  758. elseif($value >= 224 && $value <= 239) $i = $i + 2;
  759. elseif($value >= 240 && $value <= 247) $i = $i + 3;
  760. }
  761. $count++;
  762. }
  763. return $count;
  764. }
  765. function cutstr($string, $length, $dot = ' ...') {
  766. if(strlen($string) <= $length) {
  767. return $string;
  768. }
  769. $pre = chr(1);
  770. $end = chr(1);
  771. $string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), $string);
  772. $strcut = '';
  773. if(strtolower(CHARSET) == 'utf-8') {
  774. $n = $tn = $noc = 0;
  775. while($n < strlen($string)) {
  776. $t = ord($string[$n]);
  777. if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
  778. $tn = 1; $n++; $noc++;
  779. } elseif(194 <= $t && $t <= 223) {
  780. $tn = 2; $n += 2; $noc += 2;
  781. } elseif(224 <= $t && $t <= 239) {
  782. $tn = 3; $n += 3; $noc += 2;
  783. } elseif(240 <= $t && $t <= 247) {
  784. $tn = 4; $n += 4; $noc += 2;
  785. } elseif(248 <= $t && $t <= 251) {
  786. $tn = 5; $n += 5; $noc += 2;
  787. } elseif($t == 252 || $t == 253) {
  788. $tn = 6; $n += 6; $noc += 2;
  789. } else {
  790. $n++;
  791. }
  792. if($noc >= $length) {
  793. break;
  794. }
  795. }
  796. if($noc > $length) {
  797. $n -= $tn;
  798. }
  799. $strcut = substr($string, 0, $n);
  800. } else {
  801. $_length = $length - 1;
  802. for($i = 0; $i < $length; $i++) {
  803. if(ord($string[$i]) <= 127) {
  804. $strcut .= $string[$i];
  805. } else if($i < $_length) {
  806. $strcut .= $string[$i].$string[++$i];
  807. }
  808. }
  809. }
  810. $strcut = str_replace(array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
  811. $pos = strrpos($strcut, chr(1));
  812. if($pos !== false) {
  813. $strcut = substr($strcut,0,$pos);
  814. }
  815. return $strcut.$dot;
  816. }
  817. function dstripslashes($string) {
  818. if(empty($string)) return $string;
  819. if(is_array($string)) {
  820. foreach($string as $key => $val) {
  821. $string[$key] = dstripslashes($val);
  822. }
  823. } else {
  824. $string = stripslashes($string);
  825. }
  826. return $string;
  827. }
  828. function aidencode($aid, $type = 0, $tid = 0) {
  829. global $_G;
  830. $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;
  831. return rawurlencode(base64_encode($s));
  832. }
  833. function getforumimg($aid, $nocache = 0, $w = 140, $h = 140, $type = '') {
  834. global $_G;
  835. $key = dsign($aid.'|'.$w.'|'.$h);
  836. return 'forum.php?mod=image&aid='.$aid.'&size='.$w.'x'.$h.'&key='.rawurlencode($key).($nocache ? '&nocache=yes' : '').($type ? '&type='.$type : '');
  837. }
  838. function rewriteoutput($type, $returntype, $host) {
  839. global $_G;
  840. $fextra = '';
  841. if($type == 'forum_forumdisplay') {
  842. list(,,, $fid, $page, $extra) = func_get_args();
  843. $r = array(
  844. '{fid}' => empty($_G['setting']['forumkeys'][$fid]) ? $fid : $_G['setting']['forumkeys'][$fid],
  845. '{page}' => $page ? $page : 1,
  846. );
  847. } elseif($type == 'forum_viewthread') {
  848. list(,,, $tid, $page, $prevpage, $extra) = func_get_args();
  849. $r = array(
  850. '{tid}' => $tid,
  851. '{page}' => $page ? $page : 1,
  852. '{prevpage}' => $prevpage && !IS_ROBOT ? $prevpage : 1,
  853. );
  854. } elseif($type == 'home_space') {
  855. list(,,, $uid, $username, $extra) = func_get_args();
  856. $_G['setting']['rewritecompatible'] && $username = rawurlencode($username);
  857. $r = array(
  858. '{user}' => $uid ? 'uid' : 'username',
  859. '{value}' => $uid ? $uid : $username,
  860. );
  861. } elseif($type == 'home_blog') {
  862. list(,,, $uid, $blogid, $extra) = func_get_args();
  863. $r = array(
  864. '{uid}' => $uid,
  865. '{blogid}' => $blogid,
  866. );
  867. } elseif($type == 'group_group') {
  868. list(,,, $fid, $page, $extra) = func_get_args();
  869. $r = array(
  870. '{fid}' => $fid,
  871. '{page}' => $page ? $page : 1,
  872. );
  873. } elseif($type == 'portal_topic') {
  874. list(,,, $name, $extra) = func_get_args();
  875. $r = array(
  876. '{name}' => $name,
  877. );
  878. } elseif($type == 'portal_article') {
  879. list(,,, $id, $page, $extra) = func_get_args();
  880. $r = array(
  881. '{id}' => $id,
  882. '{page}' => $page ? $page : 1,
  883. );
  884. } elseif($type == 'forum_archiver') {
  885. list(,, $action, $value, $page, $extra) = func_get_args();
  886. $host = '';
  887. $r = array(
  888. '{action}' => $action,
  889. '{value}' => $value,
  890. );
  891. if($page) {
  892. $fextra = '?page='.$page;
  893. }
  894. } elseif($type == 'plugin') {
  895. list(,, $pluginid, $module,, $param, $extra) = func_get_args();
  896. $host = '';
  897. $r = array(
  898. '{pluginid}' => $pluginid,
  899. '{module}' => $module,
  900. );
  901. if($param) {
  902. $fextra = '?'.$param;
  903. }
  904. }
  905. $href = str_replace(array_keys($r), $r, $_G['setting']['rewriterule'][$type]).$fextra;
  906. if(!$returntype) {
  907. return '<a href="'.$host.$href.'"'.(!empty($extra) ? stripslashes($extra) : '').'>';
  908. } else {
  909. return $host.$href;
  910. }
  911. }
  912. function mobilereplace($file, $replace) {
  913. return helper_mobile::mobilereplace($file, $replace);
  914. }
  915. function mobileoutput() {
  916. helper_mobile::mobileoutput();
  917. }
  918. function output() {
  919. global $_G;
  920. if(defined('DISCUZ_OUTPUTED')) {
  921. return;
  922. } else {
  923. define('DISCUZ_OUTPUTED', 1);
  924. }
  925. if(!empty($_G['blockupdate'])) {
  926. block_updatecache($_G['blockupdate']['bid']);
  927. }
  928. if(defined('IN_MOBILE')) {
  929. mobileoutput();
  930. }
  931. if(!defined('IN_MOBILE') && !defined('IN_ARCHIVER')) {
  932. $tipsService = Cloud::loadClass('Service_DiscuzTips');
  933. $tipsService->show();
  934. }
  935. $havedomain = implode('', $_G['setting']['domain']['app']);
  936. if($_G['setting']['rewritestatus'] || !empty($havedomain)) {
  937. $content = ob_get_contents();
  938. $content = output_replace($content);
  939. ob_end_clean();
  940. $_G['gzipcompress'] ? ob_start('ob_gzhandler') : ob_start();
  941. echo $content;
  942. }
  943. if(isset($_G['makehtml'])) {
  944. helper_makehtml::make_html();
  945. }
  946. if($_G['setting']['ftp']['connid']) {
  947. @ftp_close($_G['setting']['ftp']['connid']);
  948. }
  949. $_G['setting']['ftp'] = array();
  950. if(defined('CACHE_FILE') && CACHE_FILE && !defined('CACHE_FORBIDDEN') && !defined('IN_MOBILE') && !checkmobile()) {
  951. if(diskfreespace(DISCUZ_ROOT.'./'.$_G['setting']['cachethreaddir']) > 1000000) {
  952. if($fp = @fopen(CACHE_FILE, 'w')) {
  953. flock($fp, LOCK_EX);
  954. fwrite($fp, empty($content) ? ob_get_contents() : $content);
  955. }
  956. @fclose($fp);
  957. chmod(CACHE_FILE, 0777);
  958. }
  959. }
  960. if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG && @include(libfile('function/debug'))) {
  961. function_exists('debugmessage') && debugmessage();
  962. }
  963. }
  964. function output_replace($content) {
  965. global $_G;
  966. if(defined('IN_MODCP') || defined('IN_ADMINCP')) return $content;
  967. if(!empty($_G['setting']['output']['str']['search'])) {
  968. if(empty($_G['setting']['domain']['app']['default'])) {
  969. $_G['setting']['output']['str']['replace'] = str_replace('{CURHOST}', $_G['siteurl'], $_G['setting']['output']['str']['replace']);
  970. }
  971. $content = str_replace($_G['setting']['output']['str']['search'], $_G['setting']['output']['str']['replace'], $content);
  972. }
  973. if(!empty($_G['setting']['output']['preg']['search']) && (empty($_G['setting']['rewriteguest']) || empty($_G['uid']))) {
  974. if(empty($_G['setting']['domain']['app']['default'])) {
  975. $_G['setting']['output']['preg']['search'] = str_replace('\{CURHOST\}', preg_quote($_G['siteurl'], '/'), $_G['setting']['output']['preg']['search']);
  976. $_G['setting']['output']['preg']['replace'] = str_replace('{CURHOST}', $_G['siteurl'], $_G['setting']['output']['preg']['replace']);
  977. }
  978. foreach($_G['setting']['output']['preg']['search'] as $key => $value) {
  979. $content = preg_replace_callback($value, create_function('$matches', 'return '.$_G['setting']['output']['preg']['replace'][$key].';'), $content);
  980. }
  981. }
  982. return $content;
  983. }
  984. function output_ajax() {
  985. global $_G;
  986. $s = ob_get_contents();
  987. ob_end_clean();
  988. $s = preg_replace("/([\\x01-\\x08\\x0b-\\x0c\\x0e-\\x1f])+/", ' ', $s);
  989. $s = str_replace(array(chr(0), ']]>'), array(' ', ']]&gt;'), $s);
  990. if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG && @include(libfile('function/debug'))) {
  991. function_exists('debugmessage') && $s .= debugmessage(1);
  992. }
  993. $havedomain = implode('', $_G['setting']['domain']['app']);
  994. if($_G['setting']['rewritestatus'] || !empty($havedomain)) {
  995. $s = output_replace($s);
  996. }
  997. return $s;
  998. }
  999. function runhooks($scriptextra = '') {
  1000. if(!defined('HOOKTYPE')) {
  1001. define('HOOKTYPE', !defined('IN_MOBILE') ? 'hookscript' : 'hookscriptmobile');
  1002. }
  1003. if(defined('CURMODULE')) {
  1004. global $_G;
  1005. if($_G['setting']['plugins']['func'][HOOKTYPE]['common']) {
  1006. hookscript('common', 'global', 'funcs', array(), 'common');
  1007. }
  1008. hookscript(CURMODULE, $_G['basescript'], 'funcs', array(), '', $scriptextra);
  1009. }
  1010. }
  1011. function hookscript($script, $hscript, $type = 'funcs', $param = array(), $func = '', $scriptextra = '') {
  1012. global $_G;
  1013. static $pluginclasses;
  1014. if($hscript == 'home') {
  1015. if($script == 'space') {
  1016. $scriptextra = !$scriptextra ? $_GET['do'] : $scriptextra;
  1017. $script = 'space'.(!empty($scriptextra) ? '_'.$scriptextra : '');
  1018. } elseif($script == 'spacecp') {
  1019. $scriptextra = !$scriptextra ? $_GET['ac'] : $scriptextra;
  1020. $script .= !empty($scriptextra) ? '_'.$scriptextra : '';
  1021. }
  1022. }
  1023. if(!isset($_G['setting'][HOOKTYPE][$hscript][$script][$type])) {
  1024. return;
  1025. }
  1026. if(!isset($_G['cache']['plugin'])) {
  1027. loadcache('plugin');
  1028. }
  1029. foreach((array)$_G['setting'][HOOKTYPE][$hscript][$script]['module'] as $identifier => $include) {
  1030. if($_G['pluginrunlist'] && !in_array($identifier, $_G['pluginrunlist'])) {
  1031. continue;
  1032. }
  1033. $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']);
  1034. if($hooksadminid[$identifier]) {
  1035. @include_once DISCUZ_ROOT.'./source/plugin/'.$include.'.class.php';
  1036. }
  1037. }
  1038. if(@is_array($_G['setting'][HOOKTYPE][$hscript][$script][$type])) {
  1039. $_G['inhookscript'] = true;
  1040. $funcs = !$func ? $_G['setting'][HOOKTYPE][$hscript][$script][$type] : array($func => $_G['setting'][HOOKTYPE][$hscript][$script][$type][$func]);
  1041. foreach($funcs as $hookkey => $hookfuncs) {
  1042. foreach($hookfuncs as $hookfunc) {
  1043. if($hooksadminid[$hookfunc[0]]) {
  1044. $classkey = (HOOKTYPE != 'hookscriptmobile' ? '' : 'mobile').'plugin_'.($hookfunc[0].($hscript != 'global' ? '_'.$hscript : ''));
  1045. if(!class_exists($classkey, false)) {
  1046. continue;
  1047. }
  1048. if(!isset($pluginclasses[$classkey])) {
  1049. $pluginclasses[$classkey] = new $classkey;
  1050. }
  1051. if(!method_exists($pluginclasses[$classkey], $hookfunc[1])) {
  1052. continue;
  1053. }
  1054. $return = call_user_func(array($pluginclasses[$classkey], $hookfunc[1]), $param);
  1055. if(substr($hookkey, -7) == '_extend' && !empty($_G['setting']['pluginhooks'][$hookkey])) {
  1056. continue;
  1057. }
  1058. if(is_array($return)) {
  1059. if(!isset($_G['setting']['pluginhooks'][$hookkey]) || is_array($_G['setting']['pluginhooks'][$hookkey])) {
  1060. foreach($return as $k => $v) {
  1061. $_G['setting']['pluginhooks'][$hookkey][$k] .= $v;
  1062. }
  1063. } else {
  1064. foreach($return as $k => $v) {
  1065. $_G['setting']['pluginhooks'][$hookkey][$k] = $v;
  1066. }
  1067. }
  1068. } else {
  1069. if(!is_array($_G['setting']['pluginhooks'][$hookkey])) {
  1070. $_G['setting']['pluginhooks'][$hookkey] .= $return;
  1071. } else {
  1072. foreach($_G['setting']['pluginhooks'][$hookkey] as $k => $v) {
  1073. $_G['setting']['pluginhooks'][$hookkey][$k] .= $return;
  1074. }
  1075. }
  1076. }
  1077. }
  1078. }
  1079. }
  1080. }
  1081. $_G['inhookscript'] = false;
  1082. }
  1083. function hookscriptoutput($tplfile) {
  1084. global $_G;
  1085. if(!empty($_G['hookscriptoutput'])) {
  1086. return;
  1087. }
  1088. hookscript('global', 'global');
  1089. $_G['hookscriptoutput'] = true;
  1090. if(defined('CURMODULE')) {
  1091. $param = array('template' => $tplfile, 'message' => $_G['hookscriptmessage'], 'values' => $_G['hookscriptvalues']);
  1092. hookscript(CURMODULE, $_G['basescript'], 'outputfuncs', $param);
  1093. }
  1094. }
  1095. function pluginmodule($pluginid, $type) {
  1096. global $_G;
  1097. $pluginid = $pluginid ? preg_replace("/[^A-Za-z0-9_:]/", '', $pluginid) : '';
  1098. if(!isset($_G['cache']['plugin'])) {
  1099. loadcache('plugin');
  1100. }
  1101. list($identifier, $module) = explode(':', $pluginid);
  1102. if(!is_array($_G['setting']['plugins'][$type]) || !array_key_exists($pluginid, $_G['setting']['plugins'][$type])) {
  1103. showmessage('plugin_nonexistence');
  1104. }
  1105. if(!empty($_G['setting']['plugins'][$type][$pluginid]['url'])) {
  1106. dheader('location: '.$_G['setting']['plugins'][$type][$pluginid]['url']);
  1107. }
  1108. $directory = $_G['setting']['plugins'][$type][$pluginid]['directory'];
  1109. if(empty($identifier) || !preg_match("/^[a-z]+[a-z0-9_]*\/$/i", $directory) || !preg_match("/^[a-z0-9_\-]+$/i", $module)) {
  1110. showmessage('undefined_action');
  1111. }
  1112. if(@!file_exists(DISCUZ_ROOT.($modfile = './source/plugin/'.$directory.$module.'.inc.php'))) {
  1113. showmessage('plugin_module_nonexistence', '', array('mod' => $modfile));
  1114. }
  1115. return DISCUZ_ROOT.$modfile;
  1116. }
  1117. function updatecreditbyaction($action, $uid = 0, $extrasql = array(), $needle = '', $coef = 1, $update = 1, $fid = 0) {
  1118. $credit = credit::instance();
  1119. if($extrasql) {
  1120. $credit->extrasql = $extrasql;
  1121. }
  1122. return $credit->execrule($action, $uid, $needle, $coef, $update, $fid);
  1123. }
  1124. function checklowerlimit($action, $uid = 0, $coef = 1, $fid = 0, $returnonly = 0) {
  1125. require_once libfile('function/credit');
  1126. return _checklowerlimit($action, $uid, $coef, $fid, $returnonly);
  1127. }
  1128. function batchupdatecredit($action, $uids = 0, $extrasql = array(), $coef = 1, $fid = 0) {
  1129. $credit = & credit::instance();
  1130. if($extrasql) {
  1131. $credit->extrasql = $extrasql;
  1132. }
  1133. return $credit->updatecreditbyrule($action, $uids, $coef, $fid);
  1134. }
  1135. function updatemembercount($uids, $dataarr = array(), $checkgroup = true, $operation = '', $relatedid = 0, $ruletxt = '', $customtitle = '', $custommemo = '') {
  1136. if(!empty($uids) && (is_array($dataarr) && $dataarr)) {
  1137. require_once libfile('function/credit');
  1138. return _updatemembercount($uids, $dataarr, $checkgroup, $operation, $relatedid, $ruletxt, $customtitle, $custommemo);
  1139. }
  1140. return true;
  1141. }
  1142. function checkusergroup($uid = 0) {
  1143. $credit = & credit::instance();
  1144. $credit->checkusergroup($uid);
  1145. }
  1146. function checkformulasyntax($formula, $operators, $tokens) {
  1147. $var = implode('|', $tokens);
  1148. $operator = implode('', $operators);
  1149. $operator = str_replace(
  1150. array('+', '-', '*', '/', '(', ')', '{', '}', '\''),
  1151. array('\+', '\-', '\*', '\/', '\(', '\)', '\{', '\}', '\\\''),
  1152. $operator
  1153. );
  1154. if(!empty($formula)) {
  1155. if(!preg_match("/^([$operator\.\d\(\)]|(($var)([$operator\(\)]|$)+))+$/", $formula) || !is_null(eval(preg_replace("/($var)/", "\$\\1", $formula).';'))){
  1156. return false;
  1157. }
  1158. }
  1159. return true;
  1160. }
  1161. function checkformulacredits($formula) {
  1162. return checkformulasyntax(
  1163. $formula,
  1164. array('+', '-', '*', '/', ' '),
  1165. array('extcredits[1-8]', 'digestposts', 'posts', 'threads', 'oltime', 'friends', 'doings', 'polls', 'blogs', 'albums', 'sharings')
  1166. );
  1167. }
  1168. function debug($var = null, $vardump = false) {
  1169. echo '<pre>';
  1170. $vardump = empty($var) ? true : $vardump;
  1171. if($vardump) {
  1172. var_dump($var);
  1173. } else {
  1174. print_r($var);
  1175. }
  1176. exit();
  1177. }
  1178. function debuginfo() {
  1179. global $_G;
  1180. if(getglobal('setting/debug')) {
  1181. $db = & DB::object();
  1182. $_G['debuginfo'] = array(
  1183. 'time' => number_format((microtime(true) - $_G['starttime']), 6),
  1184. 'queries' => $db->querynum,
  1185. 'memory' => ucwords(C::memory()->type)
  1186. );
  1187. if($db->slaveid) {
  1188. $_G['debuginfo']['queries'] = 'Total '.$db->querynum.', Slave '.$db->slavequery;
  1189. }
  1190. return TRUE;
  1191. } else {
  1192. return FALSE;
  1193. }
  1194. }
  1195. function getfocus_rand($module) {
  1196. global $_G;
  1197. if(empty($_G['setting']['focus']) || !array_key_exists($module, $_G['setting']['focus']) || !empty($_G['cookie']['nofocus_'.$module]) || !$_G['setting']['focus'][$module]) {
  1198. return null;
  1199. }
  1200. loadcache('focus');
  1201. if(empty($_G['cache']['focus']['data']) || !is_array($_G['cache']['focus']['data'])) {
  1202. return null;
  1203. }
  1204. $focusid = $_G['setting']['focus'][$module][array_rand($_G['setting']['focus'][$module])];
  1205. return $focusid;
  1206. }
  1207. function check_seccode($value, $idhash, $fromjs = 0, $modid = '') {
  1208. return helper_seccheck::check_seccode($value, $idhash, $fromjs, $modid);
  1209. }
  1210. function check_secqaa($value, $idhash) {
  1211. return helper_seccheck::check_secqaa($value, $idhash);
  1212. }
  1213. function seccheck($rule, $param = array()) {
  1214. return helper_seccheck::seccheck($rule, $param);
  1215. }
  1216. function make_seccode($seccode = '') {
  1217. return helper_seccheck::make_seccode($seccode);
  1218. }
  1219. function make_secqaa() {
  1220. return helper_seccheck::make_secqaa();
  1221. }
  1222. function adshow($parameter) {
  1223. global $_G;
  1224. if($_G['inajax'] || $_G['group']['closead']) {
  1225. return;
  1226. }
  1227. if(isset($_G['config']['plugindeveloper']) && $_G['config']['plugindeveloper'] == 2) {
  1228. return '<hook>[ad '.$parameter.']</hook>';
  1229. }
  1230. $params = explode('/', $parameter);
  1231. $customid = 0;
  1232. $customc = explode('_', $params[0]);
  1233. if($customc[0] == 'custom') {
  1234. $params[0] = $customc[0];
  1235. $customid = $customc[1];
  1236. }
  1237. $adcontent = null;
  1238. if(empty($_G['setting']['advtype']) || !in_array($params[0], $_G['setting']['advtype'])) {
  1239. $adcontent = '';
  1240. }
  1241. if($adcontent === null) {
  1242. loadcache('advs');
  1243. $adids = array();
  1244. $evalcode = &$_G['cache']['advs']['evalcode'][$params[0]];
  1245. $parameters = &$_G['cache']['advs']['parameters'][$params[0]];
  1246. $codes = &$_G['cache']['advs']['code'][$_G['basescript']][$params[0]];
  1247. if(!empty($codes)) {
  1248. foreach($codes as $adid => $code) {
  1249. $parameter = &$parameters[$adid];
  1250. $checked = true;
  1251. @eval($evalcode['check']);
  1252. if($checked) {
  1253. $adids[] = $adid;
  1254. }
  1255. }
  1256. if(!empty($adids)) {
  1257. $adcode = $extra = '';
  1258. @eval($evalcode['create']);
  1259. if(empty($notag)) {
  1260. $adcontent = '<div'.($params[1] != '' ? ' class="'.$params[1].'"' : '').$extra.'>'.$adcode.'</div>';
  1261. } else {
  1262. $adcontent = $adcode;
  1263. }
  1264. }
  1265. }
  1266. }
  1267. $adfunc = 'ad_'.$params[0];
  1268. $_G['setting']['pluginhooks'][$adfunc] = null;
  1269. hookscript('ad', 'global', 'funcs', array('params' => $params, 'content' => $adcontent), $adfunc);
  1270. if(!$_G['setting']['hookscript']['global']['ad']['funcs'][$adfunc]) {
  1271. hookscript('ad', $_G['basescript'], 'funcs', array('params' => $params, 'content' => $adcontent), $adfunc);
  1272. }
  1273. return $_G['setting']['pluginhooks'][$adfunc] === null ? $adcontent : $_G['setting']['pluginhooks'][$adfunc];
  1274. }
  1275. function showmessage($message, $url_forward = '', $values = array(), $extraparam = array(), $custom = 0) {
  1276. require_once libfile('function/message');
  1277. return dshowmessage($message, $url_forward, $values, $extraparam, $custom);
  1278. }
  1279. function submitcheck($var, $allowget = 0, $seccodecheck = 0, $secqaacheck = 0) {
  1280. if(!getgpc($var)) {
  1281. return FALSE;
  1282. } else {
  1283. return helper_form::submitcheck($var, $allowget, $seccodecheck, $secqaacheck);
  1284. }
  1285. }
  1286. function multi($num, $perpage, $curpage, $mpurl, $maxpages = 0, $page = 10, $autogoto = FALSE, $simple = FALSE, $jsfunc = FALSE) {
  1287. return $num > $perpage ? helper_page::multi($num, $perpage, $curpage, $mpurl, $maxpages, $page, $autogoto, $simple, $jsfunc) : '';
  1288. }
  1289. function simplepage($num, $perpage, $curpage, $mpurl) {
  1290. return helper_page::simplepage($num, $perpage, $curpage, $mpurl);
  1291. }
  1292. function censor($message, $modword = NULL, $return = FALSE) {
  1293. return helper_form::censor($message, $modword, $return);
  1294. }
  1295. function censormod($message) {
  1296. return getglobal('group/ignorecensor') || !$message ? false :helper_form::censormod($message);
  1297. }
  1298. function space_merge(&$values, $tablename, $isarchive = false) {
  1299. global $_G;
  1300. $uid = empty($values['uid'])?$_G['uid']:$values['uid'];
  1301. $var = "member_{$uid}_{$tablename}";
  1302. if($uid) {
  1303. if(!isset($_G[$var])) {
  1304. $ext = $isarchive ? '_archive' : '';
  1305. if(($_G[$var] = C::t('common_member_'.$tablename.$ext)->fetch($uid)) !== false) {
  1306. if($tablename == 'field_home') {
  1307. $_G['setting']['privacy'] = empty($_G['setting']['privacy']) ? array() : (is_array($_G['setting']['privacy']) ? $_G['setting']['privacy'] : dunserialize($_G['setting']['privacy']));
  1308. $_G[$var]['privacy'] = empty($_G[$var]['privacy'])? array() : is_array($_G[$var]['privacy']) ? $_G[$var]['privacy'] : dunserialize($_G[$var]['privacy']);
  1309. foreach (array('feed','view','profile') as $pkey) {
  1310. if(empty($_G[$var]['privacy'][$pkey]) && !isset($_G[$var]['privacy'][$pkey])) {
  1311. $_G[$var]['privacy'][$pkey] = isset($_G['setting']['privacy'][$pkey]) ? $_G['setting']['privacy'][$pkey] : array();
  1312. }
  1313. }
  1314. $_G[$var]['acceptemail'] = empty($_G[$var]['acceptemail'])? array() : dunserialize($_G[$var]['acceptemail']);
  1315. if(empty($_G[$var]['acceptemail'])) {
  1316. $_G[$var]['acceptemail'] = empty($_G['setting']['acceptemail'])?array():dunserialize($_G['setting']['acceptemail']);
  1317. }
  1318. }
  1319. } else {
  1320. C::t('common_member_'.$tablename.$ext)->insert(array('uid'=>$uid));
  1321. $_G[$var] = array();
  1322. }
  1323. }
  1324. $values = array_merge($values, $_G[$var]);
  1325. }
  1326. }
  1327. function runlog($file, $message, $halt=0) {
  1328. helper_log::runlog($file, $message, $halt);
  1329. }
  1330. function stripsearchkey($string) {
  1331. $string = trim($string);
  1332. $string = str_replace('*', '%', addcslashes($string, '%_'));
  1333. return $string;
  1334. }
  1335. function dmkdir($dir, $mode = 0777, $makeindex = TRUE){
  1336. if(!is_dir($dir)) {
  1337. dmkdir(dirname($dir), $mode, $makeindex);
  1338. @mkdir($dir, $mode);
  1339. if(!empty($makeindex)) {
  1340. @touch($dir.'/index.html'); @chmod($dir.'/index.html', 0777);
  1341. }
  1342. }
  1343. return true;
  1344. }
  1345. function dreferer($default = '') {
  1346. global $_G;
  1347. $default = empty($default) && $_ENV['curapp'] ? $_ENV['curapp'].'.php' : '';
  1348. $_G['referer'] = !empty($_GET['referer']) ? $_GET['referer'] : $_SERVER['HTTP_REFERER'];
  1349. $_G['referer'] = substr($_G['referer'], -1) == '?' ? substr($_G['referer'], 0, -1) : $_G['referer'];
  1350. if(strpos($_G['referer'], 'member.php?mod=logging')) {
  1351. $_G['referer'] = $default;
  1352. }
  1353. $reurl = parse_url($_G['referer']);
  1354. if(!$reurl || (isset($reurl['scheme']) && !in_array(strtolower($reurl['scheme']), array('http', 'https')))) {
  1355. $_G['referer'] = '';
  1356. }
  1357. if(!empty($reurl['host']) && !in_array($reurl['host'], array($_SERVER['HTTP_HOST'], 'www.'.$_SERVER['HTTP_HOST'])) && !in_array($_SERVER['HTTP_HOST'], array($reurl['host'], 'www.'.$reurl['host']))) {
  1358. if(!in_array($reurl['host'], $_G['setting']['domain']['app']) && !isset($_G['setting']['domain']['list'][$reurl['host']])) {
  1359. $domainroot = substr($reurl['host'], strpos($reurl['host'], '.')+1);
  1360. if(empty($_G['setting']['domain']['root']) || (is_array($_G['setting']['domain']['root']) && !in_array($domainroot, $_G['setting']['domain']['root']))) {
  1361. $_G['referer'] = $_G['setting']['domain']['defaultindex'] ? $_G['setting']['domain']['defaultindex'] : 'index.php';
  1362. }
  1363. }
  1364. } elseif(empty($reurl['host'])) {
  1365. $_G['referer'] = $_G['siteurl'].'./'.$_G['referer'];
  1366. }
  1367. $_G['referer'] = durlencode($_G['referer']);
  1368. return $_G['referer'];
  1369. }
  1370. function ftpcmd($cmd, $arg1 = '') {
  1371. static $ftp;
  1372. $ftpon = getglobal('setting/ftp/on');
  1373. if(!$ftpon) {
  1374. return $cmd == 'error' ? -101 : 0;
  1375. } elseif($ftp == null) {
  1376. $ftp = & discuz_ftp::instance();
  1377. }
  1378. if(!$ftp->enabled) {
  1379. return $ftp->error();
  1380. } elseif($ftp->enabled && !$ftp->connectid) {
  1381. $ftp->connect();
  1382. }
  1383. switch ($cmd) {
  1384. case 'upload' : return $ftp->upload(getglobal('setting/attachdir').'/'.$arg1, $arg1); break;
  1385. case 'delete' : return $ftp->ftp_delete($arg1); break;
  1386. case 'close' : return $ftp->ftp_close(); break;
  1387. case 'error' : return $ftp->error(); break;
  1388. case 'object' : return $ftp; break;
  1389. default : return false;
  1390. }
  1391. }
  1392. function diconv($str, $in_charset, $out_charset = CHARSET, $ForceTable = FALSE) {
  1393. global $_G;
  1394. $in_charset = strtoupper($in_charset);
  1395. $out_charset = strtoupper($out_charset);
  1396. if(empty($str) || $in_charset == $out_charset) {
  1397. return $str;
  1398. }
  1399. $out = '';
  1400. if(!$ForceTable) {
  1401. if(function_exists('iconv')) {
  1402. $out = iconv($in_charset, $out_charset.'//IGNORE', $str);
  1403. } elseif(function_exists('mb_convert_encoding')) {
  1404. $out = mb_convert_encoding($str, $out_charset, $in_charset);
  1405. }
  1406. }
  1407. if($out == '') {
  1408. $chinese = new Chinese($in_charset, $out_charset, true);
  1409. $out = $chinese->Convert($str);
  1410. }
  1411. return $out;
  1412. }
  1413. function widthauto() {
  1414. global $_G;
  1415. if($_G['disabledwidthauto']) {
  1416. return 0;
  1417. }
  1418. if(!empty($_G['widthauto'])) {
  1419. return $_G['widthauto'] > 0 ? 1 : 0;
  1420. }
  1421. if($_G['setting']['switchwidthauto'] && !empty($_G['cookie']['widthauto'])) {
  1422. return $_G['cookie']['widthauto'] > 0 ? 1 : 0;
  1423. } else {
  1424. return $_G['setting']['allowwidthauto'] ? 0 : 1;
  1425. }
  1426. }
  1427. function renum($array) {
  1428. $newnums = $nums = array();
  1429. foreach ($array as $id => $num) {
  1430. $newnums[$num][] = $id;
  1431. $nums[$num] = $num;
  1432. }
  1433. return array($nums, $newnums);
  1434. }
  1435. function sizecount($size) {
  1436. if($size >= 1073741824) {
  1437. $size = round($size / 1073741824 * 100) / 100 . ' GB';
  1438. } elseif($size >= 1048576) {
  1439. $size = round($size / 1048576 * 100) / 100 . ' MB';
  1440. } elseif($size >= 1024) {
  1441. $size = round($size / 1024 * 100) / 100 . ' KB';
  1442. } else {
  1443. $size = intval($size) . ' Bytes';
  1444. }
  1445. return $size;
  1446. }
  1447. function swapclass($class1, $class2 = '') {
  1448. static $swapc = null;
  1449. $swapc = isset($swapc) && $swapc != $class1 ? $class1 : $class2;
  1450. return $swapc;
  1451. }
  1452. function writelog($file, $log) {
  1453. helper_log::writelog($file, $log);
  1454. }
  1455. function getstatus($status, $position) {
  1456. $t = $status & pow(2, $position - 1) ? 1 : 0;
  1457. return $t;
  1458. }
  1459. function setstatus($position, $value, $baseon = null) {
  1460. $t = pow(2, $position - 1);
  1461. if($value) {
  1462. $t = $baseon | $t;
  1463. } elseif ($baseon !== null) {
  1464. $t = $baseon & ~$t;
  1465. } else {
  1466. $t = ~$t;
  1467. }
  1468. return $t & 0xFFFF;
  1469. }
  1470. function notification_add($touid, $type, $note, $notevars = array(), $system = 0) {
  1471. return helper_notification::notification_add($touid, $type, $note, $notevars, $system);
  1472. }
  1473. function manage_addnotify($type, $from_num = 0, $langvar = array()) {
  1474. helper_notification::manage_addnotify($type, $from_num, $langvar);
  1475. }
  1476. function sendpm($toid, $subject, $message, $fromid = '', $replypmid = 0, $isusername = 0, $type = 0) {
  1477. return helper_pm::sendpm($toid, $subject, $message, $fromid, $replypmid, $isusername, $type);
  1478. }
  1479. function g_icon($groupid, $return = 0) {
  1480. global $_G;
  1481. if(empty($_G['cache']['usergroups'][$groupid]['icon'])) {
  1482. $s = '';
  1483. } else {
  1484. if(substr($_G['cache']['usergroups'][$groupid]['icon'], 0, 5) == 'http:') {
  1485. $s = '<img src="'.$_G['cache']['usergroups'][$groupid]['icon'].'" alt="" class="vm" />';
  1486. } else {
  1487. $s = '<img src="'.$_G['setting']['attachurl'].'common/'.$_G['cache']['usergroups'][$groupid]['icon'].'" alt="" class="vm" />';
  1488. }
  1489. }
  1490. if($return) {
  1491. return $s;
  1492. } else {
  1493. echo $s;
  1494. }
  1495. }
  1496. function updatediytemplate($targettplname = '', $tpldirectory = '') {
  1497. $r = false;
  1498. $alldata = !empty($targettplname) ? array( C::t('common_diy_data')->fetch($targettplname, $tpldirectory)) : C::t('common_diy_data')->range();
  1499. require_once libfile('function/portalcp');
  1500. foreach($alldata as $value) {
  1501. $r = save_diy_data($value['tpldirectory'], $value['primaltplname'], $value['targettplname'], dunserialize($value['diycontent']));
  1502. }
  1503. return $r;
  1504. }
  1505. function space_key($uid, $appid=0) {
  1506. global $_G;
  1507. return substr(md5($_G['setting']['siteuniqueid'].'|'.$uid.(empty($appid)?'':'|'.$appid)), 8, 16);
  1508. }
  1509. function getposttablebytid($tids, $primary = 0) {
  1510. return table_forum_post::getposttablebytid($tids, $primary);
  1511. }
  1512. function getposttable($tableid = 0, $prefix = false) {
  1513. return table_forum_post::getposttable($tableid, $prefix);
  1514. }
  1515. function memory($cmd, $key='', $value='', $ttl = 0, $prefix = '') {
  1516. if($cmd == 'check') {
  1517. return C::memory()->enable ? C::memory()->type : '';
  1518. } elseif(C::memory()->enable && in_array($cmd, array('set', 'get', 'rm', 'inc', 'dec'))) {
  1519. if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
  1520. if(is_array($key)) {
  1521. foreach($key as $k) {
  1522. C::memory()->debug[$cmd][] = ($cmd == 'get' || $cmd == 'rm' ? $value : '').$prefix.$k;
  1523. }
  1524. } else {
  1525. C::memory()->debug[$cmd][] = ($cmd == 'get' || $cmd == 'rm' ? $value : '').$prefix.$key;
  1526. }
  1527. }
  1528. switch ($cmd) {
  1529. case 'set': return C::memory()->set($key, $value, $ttl, $prefix); break;
  1530. case 'get': return C::memory()->get($key, $value); break;
  1531. case 'rm': return C::memory()->rm($key, $value); break;
  1532. case 'inc': return C::memory()->inc($key, $value ? $value : 1); break;
  1533. case 'dec': return C::memory()->dec($key, $value ? $value : -1); break;
  1534. }
  1535. }
  1536. return null;
  1537. }
  1538. function ipaccess($ip, $accesslist) {
  1539. return preg_match("/^(".str_replace(array("\r\n", ' '), array('|', ''), preg_quote($accesslist, '/')).")/", $ip);
  1540. }
  1541. function ipbanned($onlineip) {
  1542. global $_G;
  1543. if($_G['setting']['ipaccess'] && !ipaccess($onlineip, $_G['setting']['ipaccess'])) {
  1544. return TRUE;
  1545. }
  1546. loadcache('ipbanned');
  1547. if(empty($_G['cache']['ipbanned'])) {
  1548. return FALSE;
  1549. } else {
  1550. if($_G['cache']['ipbanned']['expiration'] < TIMESTAMP) {
  1551. require_once libfile('function/cache');
  1552. updatecache('ipbanned');
  1553. }
  1554. return preg_match("/^(".$_G['cache']['ipbanned']['regexp'].")$/", $onlineip);
  1555. }
  1556. }
  1557. function getcount($tablename, $condition) {
  1558. if(empty($condition)) {
  1559. $where = '1';
  1560. } elseif(is_array($condition)) {
  1561. $where = DB::implode_field_value($condition, ' AND ');
  1562. } else {
  1563. $where = $condition;
  1564. }
  1565. $ret = intval(DB::result_first("SELECT COUNT(*) AS num FROM ".DB::table($tablename)." WHERE $where"));
  1566. return $ret;
  1567. }
  1568. function sysmessage($message) {
  1569. helper_sysmessage::show($message);
  1570. }
  1571. function forumperm($permstr, $groupid = 0) {
  1572. global $_G;
  1573. $groupidarray = array($_G['groupid']);
  1574. if($groupid) {
  1575. return preg_match("/(^|\t)(".$groupid.")(\t|$)/", $permstr);
  1576. }
  1577. foreach(explode("\t", $_G['member']['extgroupids']) as $extgroupid) {
  1578. if($extgroupid = intval(trim($extgroupid))) {
  1579. $groupidarray[] = $extgroupid;
  1580. }
  1581. }
  1582. if($_G['setting']['verify']['enabled']) {
  1583. getuserprofile('verify1');
  1584. foreach($_G['setting']['verify'] as $vid => $verify) {
  1585. if($verify['available'] && $_G['member']['verify'.$vid] == 1) {
  1586. $groupidarray[] = 'v'.$vid;
  1587. }
  1588. }
  1589. }
  1590. return preg_match("/(^|\t)(".implode('|', $groupidarray).")(\t|$)/", $permstr);
  1591. }
  1592. function checkperm($perm) {
  1593. global $_G;
  1594. return defined('IN_ADMINCP') ? true : (empty($_G['group'][$perm])?'':$_G['group'][$perm]);
  1595. }
  1596. function periodscheck($periods, $showmessage = 1) {
  1597. global $_G;
  1598. if(($periods == 'postmodperiods' || $periods == 'postbanperiods') && ($_G['setting']['postignorearea'] || $_G['setting']['postignoreip'])) {
  1599. if($_G['setting']['postignoreip']) {
  1600. foreach(explode("\n", $_G['setting']['postignoreip']) as $ctrlip) {
  1601. if(preg_match("/^(".preg_quote(($ctrlip = trim($ctrlip)), '/').")/", $_G['clientip'])) {
  1602. return false;
  1603. break;
  1604. }
  1605. }
  1606. }
  1607. if($_G['setting']['postignorearea']) {
  1608. $location = $whitearea = '';
  1609. require_once libfile('function/misc');
  1610. $location = trim(convertip($_G['clientip'], "./"));
  1611. if($location) {
  1612. $whitearea = preg_quote(trim($_G['setting']['postignorearea']), '/');
  1613. $whitearea = str_replace(array("\\*"), array('.*'), $whitearea);
  1614. $whitearea = '.*'.$whitearea.'.*';
  1615. $whitearea = '/^('.str_replace(array("\r\n", ' '), array('.*|.*', ''), $whitearea).')$/i';
  1616. if(@preg_match($whitearea, $location)) {
  1617. return false;
  1618. }
  1619. }
  1620. }
  1621. }
  1622. if(!$_G['group']['disableperiodctrl'] && $_G['setting'][$periods]) {
  1623. $now = dgmdate(TIMESTAMP, 'G.i', $_G['setting']['timeoffset']);
  1624. foreach(explode("\r\n", str_replace(':', '.', $_G['setting'][$periods])) as $period) {
  1625. list($periodbegin, $periodend) = explode('-', $period);
  1626. if(($periodbegin > $periodend && ($now >= $periodbegin || $now < $periodend)) || ($periodbegin < $periodend && $now >= $periodbegin && $now < $periodend)) {
  1627. $banperiods = str_replace("\r\n", ', ', $_G['setting'][$periods]);
  1628. if($showmessage) {
  1629. showmessage('period_nopermission', NULL, array('banperiods' => $banperiods), array('login' => 1));
  1630. } else {
  1631. return TRUE;
  1632. }
  1633. }
  1634. }
  1635. }
  1636. return FALSE;
  1637. }
  1638. function cknewuser($return=0) {
  1639. global $_G;
  1640. $result = true;
  1641. if(!$_G['uid']) return true;
  1642. if(checkperm('disablepostctrl')) {
  1643. return $result;
  1644. }
  1645. $ckuser = $_G['member'];
  1646. if($_G['setting']['newbiespan'] && $_G['timestamp']-$ckuser['regdate']<$_G['setting']['newbiespan']*60) {
  1647. if(empty($return)) showmessage('no_privilege_newbiespan', '', array('newbiespan' => $_G['setting']['newbiespan']), array());
  1648. $result = false;
  1649. }
  1650. if($_G['setting']['need_avatar'] && empty($ckuser['avatarstatus'])) {
  1651. if(empty($return)) showmessage('no_privilege_avatar', '', array(), array());
  1652. $result = false;
  1653. }
  1654. if($_G['setting']['need_email'] && empty($ckuser['emailstatus'])) {
  1655. if(empty($return)) showmessage('no_privilege_email', '', array(), array());
  1656. $result = false;
  1657. }
  1658. if($_G['setting']['need_friendnum']) {
  1659. space_merge($ckuser, 'count');
  1660. if($ckuser['friends'] < $_G['setting']['need_friendnum']) {
  1661. if(empty($return)) showmessage('no_privilege_friendnum', '', array('friendnum' => $_G['setting']['need_friendnum']), array());
  1662. $result = false;
  1663. }
  1664. }
  1665. return $result;
  1666. }
  1667. function manyoulog($logtype, $uids, $action, $fid = '') {
  1668. helper_manyou::manyoulog($logtype, $uids, $action, $fid);
  1669. }
  1670. function useractionlog($uid, $action) {
  1671. return helper_log::useractionlog($uid, $action);
  1672. }
  1673. function getuseraction($var) {
  1674. return helper_log::getuseraction($var);
  1675. }
  1676. function getuserapp($panel = 0) {
  1677. return helper_manyou::getuserapp($panel);
  1678. }
  1679. function getmyappiconpath($appid, $iconstatus=0) {
  1680. return helper_manyou::getmyappiconpath($appid, $iconstatus);
  1681. }
  1682. function getexpiration() {
  1683. global $_G;
  1684. $date = getdate($_G['timestamp']);
  1685. return mktime(0, 0, 0, $date['mon'], $date['mday'], $date['year']) + 86400;
  1686. }
  1687. function return_bytes($val) {
  1688. $val = trim($val);
  1689. $last = strtolower($val{strlen($val)-1});
  1690. switch($last) {
  1691. case 'g': $val *= 1024;
  1692. case 'm': $val *= 1024;
  1693. case 'k': $val *= 1024;
  1694. }
  1695. return $val;
  1696. }
  1697. function iswhitelist($host) {
  1698. global $_G;
  1699. static $iswhitelist = array();
  1700. if(isset($iswhitelist[$host])) {
  1701. return $iswhitelist[$host];
  1702. }
  1703. $hostlen = strlen($host);
  1704. $iswhitelist[$host] = false;
  1705. if(!$_G['cache']['domainwhitelist']) {
  1706. loadcache('domainwhitelist');
  1707. }
  1708. if(is_array($_G['cache']['domainwhitelist'])) foreach($_G['cache']['domainwhitelist'] as $val) {
  1709. $domainlen = strlen($val);
  1710. if($domainlen > $hostlen) {
  1711. continue;
  1712. }
  1713. if(substr($host, -$domainlen) == $val) {
  1714. $iswhitelist[$host] = true;
  1715. break;
  1716. }
  1717. }
  1718. if($iswhitelist[$host] == false) {
  1719. $iswhitelist[$host] = $host == $_SERVER['HTTP_HOST'];
  1720. }
  1721. return $iswhitelist[$host];
  1722. }
  1723. function getattachtablebyaid($aid) {
  1724. $attach = C::t('forum_attachment')->fetch($aid);
  1725. $tableid = $attach['tableid'];
  1726. return 'forum_attachment_'.($tableid >= 0 && $tableid < 10 ? intval($tableid) : 'unused');
  1727. }
  1728. function getattachtableid($tid) {
  1729. $tid = (string)$tid;
  1730. return intval($tid{strlen($tid)-1});
  1731. }
  1732. function getattachtablebytid($tid) {
  1733. return 'forum_attachment_'.getattachtableid($tid);
  1734. }
  1735. function getattachtablebypid($pid) {
  1736. $tableid = DB::result_first("SELECT tableid FROM ".DB::table('forum_attachment')." WHERE pid='$pid' LIMIT 1");
  1737. return 'forum_attachment_'.($tableid >= 0 && $tableid < 10 ? intval($tableid) : 'unused');
  1738. }
  1739. function getattachnewaid($uid = 0) {
  1740. global $_G;
  1741. $uid = !$uid ? $_G['uid'] : $uid;
  1742. return C::t('forum_attachment')->insert(array('tid' => 0, 'pid' => 0, 'uid' => $uid, 'tableid' => 127), true);
  1743. }
  1744. function get_seosetting($page, $data = array(), $defset = array()) {
  1745. return helper_seo::get_seosetting($page, $data, $defset);
  1746. }
  1747. function getimgthumbname($fileStr, $extend='.thumb.jpg', $holdOldExt=true) {
  1748. if(empty($fileStr)) {
  1749. return '';
  1750. }
  1751. if(!$holdOldExt) {
  1752. $fileStr = substr($fileStr, 0, strrpos($fileStr, '.'));
  1753. }
  1754. $extend = strstr($extend, '.') ? $extend : '.'.$extend;
  1755. return $fileStr.$extend;
  1756. }
  1757. function updatemoderate($idtype, $ids, $status = 0) {
  1758. helper_form::updatemoderate($idtype, $ids, $status);
  1759. }
  1760. function userappprompt() {
  1761. global $_G;
  1762. if($_G['setting']['my_app_status'] && $_G['setting']['my_openappprompt'] && empty($_G['cookie']['userappprompt'])) {
  1763. $sid = $_G['setting']['my_siteid'];
  1764. $ts = $_G['timestamp'];
  1765. $key = md5($sid.$ts.$_G['setting']['my_sitekey']);
  1766. $uchId = $_G['uid'] ? $_G['uid'] : 0;
  1767. echo '<script type="text/javascript" src="http://notice.uchome.manyou.com/notice/userNotice?sId='.$sid.'&ts='.$ts.'&key='.$key.'&uchId='.$uchId.'" charset="UTF-8"></script>';
  1768. }
  1769. }
  1770. function dintval($int, $allowarray = false) {
  1771. $ret = floatval($int);
  1772. if($int == $ret || !$allowarray && is_array($int)) return $ret;
  1773. if($allowarray && is_array($int)) {
  1774. foreach($int as &$v) {
  1775. $v = dintval($v, true);
  1776. }
  1777. return $int;
  1778. } elseif($int <= 0xffffffff) {
  1779. $l = strlen($int);
  1780. $m = substr($int, 0, 1) == '-' ? 1 : 0;
  1781. if(($l - $m) === strspn($int,'0987654321', $m)) {
  1782. return $int;
  1783. }
  1784. }
  1785. return $ret;
  1786. }
  1787. function makeSearchSignUrl() {
  1788. return getglobal('setting/my_search_data/status') ? helper_manyou::makeSearchSignUrl() : array();
  1789. }
  1790. function get_related_link($extent) {
  1791. return helper_seo::get_related_link($extent);
  1792. }
  1793. function parse_related_link($content, $extent) {
  1794. return helper_seo::parse_related_link($content, $extent);
  1795. }
  1796. function check_diy_perm($topic = array(), $flag = '') {
  1797. static $ret;
  1798. if(!isset($ret)) {
  1799. global $_G;
  1800. $common = !empty($_G['style']['tplfile']) || $_GET['inajax'];
  1801. $blockallow = getstatus($_G['member']['allowadmincp'], 4) || getstatus($_G['member']['allowadmincp'], 5) || getstatus($_G['member']['allowadmincp'], 6);
  1802. $ret['data'] = $common && $blockallow;
  1803. $ret['layout'] = $common && ($_G['group']['allowdiy'] || (
  1804. CURMODULE === 'topic' && ($_G['group']['allowmanagetopic'] || $_G['group']['allowaddtopic'] && $topic && $topic['uid'] == $_G['uid'])
  1805. ));
  1806. }
  1807. return empty($flag) ? $ret['data'] || $ret['layout'] : $ret[$flag];
  1808. }
  1809. function strhash($string, $operation = 'DECODE', $key = '') {
  1810. $key = md5($key != '' ? $key : getglobal('authkey'));
  1811. if($operation == 'DECODE') {
  1812. $hashcode = gzuncompress(base64_decode(($string)));
  1813. $string = substr($hashcode, 0, -16);
  1814. $hash = substr($hashcode, -16);
  1815. unset($hashcode);
  1816. }
  1817. $vkey = substr(md5($string.substr($key, 0, 16)), 4, 8).substr(md5($string.substr($key, 16, 16)), 18, 8);
  1818. if($operation == 'DECODE') {
  1819. return $hash == $vkey ? $string : '';
  1820. }
  1821. return base64_encode(gzcompress($string.$vkey));
  1822. }
  1823. function dunserialize($data) {
  1824. if(($ret = unserialize($data)) === false) {
  1825. $ret = unserialize(stripslashes($data));
  1826. }
  1827. return $ret;
  1828. }
  1829. function browserversion($type) {
  1830. static $return = array();
  1831. static $types = array('ie' => 'msie', 'firefox' => '', 'chrome' => '', 'opera' => '', 'safari' => '', 'mozilla' => '', 'webkit' => '', 'maxthon' => '', 'qq' => 'qqbrowser');
  1832. if(!$return) {
  1833. $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
  1834. $other = 1;
  1835. foreach($types as $i => $v) {
  1836. $v = $v ? $v : $i;
  1837. if(strpos($useragent, $v) !== false) {
  1838. preg_match('/'.$v.'(\/|\s)([\d\.]+)/i', $useragent, $matches);
  1839. $ver = $matches[2];
  1840. $other = $ver !== 0 && $v != 'mozilla' ? 0 : $other;
  1841. } else {
  1842. $ver = 0;
  1843. }
  1844. $return[$i] = $ver;
  1845. }
  1846. $return['other'] = $other;
  1847. }
  1848. return $return[$type];
  1849. }
  1850. function currentlang() {
  1851. $charset = strtoupper(CHARSET);
  1852. if($charset == 'GBK') {
  1853. return 'SC_GBK';
  1854. } elseif($charset == 'BIG5') {
  1855. return 'TC_BIG5';
  1856. } elseif($charset == 'UTF-8') {
  1857. global $_G;
  1858. if($_G['config']['output']['language'] == 'zh_cn') {
  1859. return 'SC_UTF8';
  1860. } elseif ($_G['config']['output']['language'] == 'zh_tw') {
  1861. return 'TC_UTF8';
  1862. }
  1863. } else {
  1864. return '';
  1865. }
  1866. }
  1867. if(PHP_VERSION < '7.0.0') {
  1868. function dpreg_replace($pattern, $replacement, $subject, $limit = -1, &$count) {
  1869. return preg_replace($pattern, $replacement, $subject, $limit, $count);
  1870. }
  1871. } else {
  1872. function dpreg_replace($pattern, $replacement, $subject, $limit = -1, &$count) {
  1873. require_once libfile('function/preg');
  1874. return _dpreg_replace($pattern, $replacement, $subject, $limit, $count);
  1875. }
  1876. }
  1877. ?>