PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/source/function/function_misc.php

https://github.com/jinbo51/DiscuzX
PHP | 510 lines | 430 code | 74 blank | 6 comment | 134 complexity | 8d917bba69a2effc0a7fecdbd7af615b MD5 | raw file
Possible License(s): BSD-3-Clause
  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_misc.php 30988 2012-07-06 02:24:35Z chenmengshu $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. function convertip($ip) {
  12. $return = '';
  13. if(preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip)) {
  14. $iparray = explode('.', $ip);
  15. if($iparray[0] == 10 || $iparray[0] == 127 || ($iparray[0] == 192 && $iparray[1] == 168) || ($iparray[0] == 172 && ($iparray[1] >= 16 && $iparray[1] <= 31))) {
  16. $return = '- LAN';
  17. } elseif($iparray[0] > 255 || $iparray[1] > 255 || $iparray[2] > 255 || $iparray[3] > 255) {
  18. $return = '- Invalid IP Address';
  19. } else {
  20. $tinyipfile = DISCUZ_ROOT.'./data/ipdata/tinyipdata.dat';
  21. $fullipfile = DISCUZ_ROOT.'./data/ipdata/wry.dat';
  22. if(@file_exists($tinyipfile)) {
  23. $return = convertip_tiny($ip, $tinyipfile);
  24. } elseif(@file_exists($fullipfile)) {
  25. $return = convertip_full($ip, $fullipfile);
  26. }
  27. }
  28. }
  29. return $return;
  30. }
  31. function convertip_tiny($ip, $ipdatafile) {
  32. static $fp = NULL, $offset = array(), $index = NULL;
  33. $ipdot = explode('.', $ip);
  34. $ip = pack('N', ip2long($ip));
  35. $ipdot[0] = (int)$ipdot[0];
  36. $ipdot[1] = (int)$ipdot[1];
  37. if($fp === NULL && $fp = @fopen($ipdatafile, 'rb')) {
  38. $offset = @unpack('Nlen', @fread($fp, 4));
  39. $index = @fread($fp, $offset['len'] - 4);
  40. } elseif($fp == FALSE) {
  41. return '- Invalid IP data file';
  42. }
  43. $length = $offset['len'] - 1028;
  44. $start = @unpack('Vlen', $index[$ipdot[0] * 4] . $index[$ipdot[0] * 4 + 1] . $index[$ipdot[0] * 4 + 2] . $index[$ipdot[0] * 4 + 3]);
  45. for ($start = $start['len'] * 8 + 1024; $start < $length; $start += 8) {
  46. if ($index{$start} . $index{$start + 1} . $index{$start + 2} . $index{$start + 3} >= $ip) {
  47. $index_offset = @unpack('Vlen', $index{$start + 4} . $index{$start + 5} . $index{$start + 6} . "\x0");
  48. $index_length = @unpack('Clen', $index{$start + 7});
  49. break;
  50. }
  51. }
  52. @fseek($fp, $offset['len'] + $index_offset['len'] - 1024);
  53. if($index_length['len']) {
  54. return '- '.@fread($fp, $index_length['len']);
  55. } else {
  56. return '- Unknown';
  57. }
  58. }
  59. function convertip_full($ip, $ipdatafile) {
  60. if(!$fd = @fopen($ipdatafile, 'rb')) {
  61. return '- Invalid IP data file';
  62. }
  63. $ip = explode('.', $ip);
  64. $ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];
  65. if(!($DataBegin = fread($fd, 4)) || !($DataEnd = fread($fd, 4)) ) return;
  66. @$ipbegin = implode('', unpack('L', $DataBegin));
  67. if($ipbegin < 0) $ipbegin += pow(2, 32);
  68. @$ipend = implode('', unpack('L', $DataEnd));
  69. if($ipend < 0) $ipend += pow(2, 32);
  70. $ipAllNum = ($ipend - $ipbegin) / 7 + 1;
  71. $BeginNum = $ip2num = $ip1num = 0;
  72. $ipAddr1 = $ipAddr2 = '';
  73. $EndNum = $ipAllNum;
  74. while($ip1num > $ipNum || $ip2num < $ipNum) {
  75. $Middle= intval(($EndNum + $BeginNum) / 2);
  76. fseek($fd, $ipbegin + 7 * $Middle);
  77. $ipData1 = fread($fd, 4);
  78. if(strlen($ipData1) < 4) {
  79. fclose($fd);
  80. return '- System Error';
  81. }
  82. $ip1num = implode('', unpack('L', $ipData1));
  83. if($ip1num < 0) $ip1num += pow(2, 32);
  84. if($ip1num > $ipNum) {
  85. $EndNum = $Middle;
  86. continue;
  87. }
  88. $DataSeek = fread($fd, 3);
  89. if(strlen($DataSeek) < 3) {
  90. fclose($fd);
  91. return '- System Error';
  92. }
  93. $DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
  94. fseek($fd, $DataSeek);
  95. $ipData2 = fread($fd, 4);
  96. if(strlen($ipData2) < 4) {
  97. fclose($fd);
  98. return '- System Error';
  99. }
  100. $ip2num = implode('', unpack('L', $ipData2));
  101. if($ip2num < 0) $ip2num += pow(2, 32);
  102. if($ip2num < $ipNum) {
  103. if($Middle == $BeginNum) {
  104. fclose($fd);
  105. return '- Unknown';
  106. }
  107. $BeginNum = $Middle;
  108. }
  109. }
  110. $ipFlag = fread($fd, 1);
  111. if($ipFlag == chr(1)) {
  112. $ipSeek = fread($fd, 3);
  113. if(strlen($ipSeek) < 3) {
  114. fclose($fd);
  115. return '- System Error';
  116. }
  117. $ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
  118. fseek($fd, $ipSeek);
  119. $ipFlag = fread($fd, 1);
  120. }
  121. if($ipFlag == chr(2)) {
  122. $AddrSeek = fread($fd, 3);
  123. if(strlen($AddrSeek) < 3) {
  124. fclose($fd);
  125. return '- System Error';
  126. }
  127. $ipFlag = fread($fd, 1);
  128. if($ipFlag == chr(2)) {
  129. $AddrSeek2 = fread($fd, 3);
  130. if(strlen($AddrSeek2) < 3) {
  131. fclose($fd);
  132. return '- System Error';
  133. }
  134. $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
  135. fseek($fd, $AddrSeek2);
  136. } else {
  137. fseek($fd, -1, SEEK_CUR);
  138. }
  139. while(($char = fread($fd, 1)) != chr(0))
  140. $ipAddr2 .= $char;
  141. $AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
  142. fseek($fd, $AddrSeek);
  143. while(($char = fread($fd, 1)) != chr(0))
  144. $ipAddr1 .= $char;
  145. } else {
  146. fseek($fd, -1, SEEK_CUR);
  147. while(($char = fread($fd, 1)) != chr(0))
  148. $ipAddr1 .= $char;
  149. $ipFlag = fread($fd, 1);
  150. if($ipFlag == chr(2)) {
  151. $AddrSeek2 = fread($fd, 3);
  152. if(strlen($AddrSeek2) < 3) {
  153. fclose($fd);
  154. return '- System Error';
  155. }
  156. $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
  157. fseek($fd, $AddrSeek2);
  158. } else {
  159. fseek($fd, -1, SEEK_CUR);
  160. }
  161. while(($char = fread($fd, 1)) != chr(0))
  162. $ipAddr2 .= $char;
  163. }
  164. fclose($fd);
  165. if(preg_match('/http/i', $ipAddr2)) {
  166. $ipAddr2 = '';
  167. }
  168. $ipaddr = "$ipAddr1 $ipAddr2";
  169. $ipaddr = preg_replace('/CZ88\.NET/is', '', $ipaddr);
  170. $ipaddr = preg_replace('/^\s*/is', '', $ipaddr);
  171. $ipaddr = preg_replace('/\s*$/is', '', $ipaddr);
  172. if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
  173. $ipaddr = '- Unknown';
  174. }
  175. return '- '.$ipaddr;
  176. }
  177. function procthread($thread, $timeformat = 'd') {
  178. global $_G;
  179. $lastvisit = $_G['member']['lastvisit'];
  180. if(empty($_G['forum_colorarray'])) {
  181. $_G['forum_colorarray'] = array('', '#EE1B2E', '#EE5023', '#996600', '#3C9D40', '#2897C5', '#2B65B7', '#8F2A90', '#EC1282');
  182. }
  183. if($thread['closed']) {
  184. $thread['new'] = 0;
  185. if($thread['isgroup'] && $thread['closed'] > 1) {
  186. $thread['folder'] = 'common';
  187. } else {
  188. $thread['folder'] = 'lock';
  189. }
  190. } else {
  191. $thread['folder'] = 'common';
  192. if($lastvisit < $thread['lastpost'] && (empty($_G['cookie']['oldtopics']) || strpos($_G['cookie']['oldtopics'], 'D'.$thread['tid'].'D') === FALSE)) {
  193. $thread['new'] = 1;
  194. $thread['folder'] = 'new';
  195. } else {
  196. $thread['new'] = 0;
  197. }
  198. }
  199. $thread['icon'] = '';
  200. $thread['id'] = random(6, 1);
  201. if(!$thread['forumname']) {
  202. $thread['forumname'] = empty($_G['cache']['forums'][$thread['fid']]['name']) ? 'Forum' : $_G['cache']['forums'][$thread['fid']]['name'];
  203. }
  204. $thread['dateline'] = dgmdate($thread['dateline'], $timeformat);
  205. $thread['lastpost'] = dgmdate($thread['lastpost'], 'u');
  206. $thread['lastposterenc'] = rawurlencode($thread['lastposter']);
  207. if($thread['replies'] > $thread['views']) {
  208. $thread['views'] = $thread['replies'];
  209. }
  210. $postsnum = $thread['special'] ? $thread['replies'] : $thread['replies'] + 1;
  211. $pagelinks = '';
  212. if($postsnum > $_G['ppp']) {
  213. if($_G['setting']['domain']['app']['forum'] || $_G['setting']['domain']['app']['default']) {
  214. $domain = 'http://'.($_G['setting']['domain']['app']['forum'] ? $_G['setting']['domain']['app']['forum'] : ($_G['setting']['domain']['app']['default'] ? $_G['setting']['domain']['app']['default'] : '')).'/';
  215. } else {
  216. $domain = $_G['siteurl'];
  217. }
  218. $posts = $postsnum;
  219. $topicpages = ceil($posts / $_G['ppp']);
  220. for($i = 1; $i <= $topicpages; $i++) {
  221. if(!in_array('forum_viewthread', $_G['setting']['rewritestatus'])) {
  222. $pagelinks .= '<a href="forum.php?mod=viewthread&tid='.$thread['tid'].'&page='.$i.($_GET['from'] ? '&from='.$_GET['from'] : '').'" target="_blank">'.$i.'</a> ';
  223. } else {
  224. $pagelinks .= '<a href="'.rewriteoutput('forum_viewthread', 1, $domain, $thread['tid'], $i, '', '').'" target="_blank">'.$i.'</a> ';
  225. }
  226. if($i == 6) {
  227. $i = $topicpages + 1;
  228. }
  229. }
  230. if($topicpages > 6) {
  231. if(!in_array('forum_viewthread', $_G['setting']['rewritestatus'])) {
  232. $pagelinks .= ' .. <a href="forum.php?mod=viewthread&tid='.$thread['tid'].'&page='.$topicpages.'" target="_blank">'.$topicpages.'</a> ';
  233. } else {
  234. $pagelinks .= ' .. <a href="'.rewriteoutput('forum_viewthread', 1, $domain, $thread['tid'], $topicpages, '', '').'" target="_blank">'.$topicpages.'</a> ';
  235. }
  236. }
  237. $thread['multipage'] = '... '.$pagelinks;
  238. } else {
  239. $thread['multipage'] = '';
  240. }
  241. if($thread['highlight']) {
  242. $string = sprintf('%02d', $thread['highlight']);
  243. $stylestr = sprintf('%03b', $string[0]);
  244. $thread['highlight'] = 'style="';
  245. $thread['highlight'] .= $stylestr[0] ? 'font-weight: bold;' : '';
  246. $thread['highlight'] .= $stylestr[1] ? 'font-style: italic;' : '';
  247. $thread['highlight'] .= $stylestr[2] ? 'text-decoration: underline;' : '';
  248. $thread['highlight'] .= $string[1] ? 'color: '.$_G['forum_colorarray'][$string[1]] : '';
  249. $thread['highlight'] .= '"';
  250. } else {
  251. $thread['highlight'] = '';
  252. }
  253. return $thread;
  254. }
  255. function modlog($thread, $action) {
  256. global $_G;
  257. $reason = $_GET['reason'];
  258. writelog('modslog', dhtmlspecialchars("$_G[timestamp]\t$_G[username]\t$_G[adminid]\t$_G[clientip]\t".$_G['forum']['fid']."\t".$_G['forum']['name']."\t$thread[tid]\t$thread[subject]\t$action\t$reason\t".$_G['toforum']['fid']."\t".$_G['toforum']['name']));
  259. }
  260. function checkreasonpm() {
  261. global $_G;
  262. $reason = trim(strip_tags($_GET['reason']));
  263. if(($_G['group']['reasonpm'] == 1 || $_G['group']['reasonpm'] == 3) && !$reason) {
  264. showmessage('admin_reason_invalid');
  265. }
  266. return $reason;
  267. }
  268. function sendreasonpm($var, $item, $notevar, $notictype = '') {
  269. global $_G;
  270. if(!empty($var['authorid']) && $var['authorid'] != $_G['uid']) {
  271. if(!empty($notevar['modaction'])) {
  272. $notevar['from_id'] = 0;
  273. $notevar['from_idtype'] = 'moderate_'.$notevar['modaction'];
  274. $notevar['modaction'] = lang('forum/modaction', $notevar['modaction']);
  275. }
  276. empty($notictype) && $notictype = 'system';
  277. notification_add($var['authorid'], $notictype, $item, $notevar, 1);
  278. }
  279. }
  280. function modreasonselect($isadmincp = 0, $reasionkey = 'modreasons') {
  281. global $_G;
  282. if(!isset($_G['cache'][$reasionkey]) || !is_array($_G['cache'][$reasionkey])) {
  283. loadcache(array($reasionkey, 'stamptypeid'));
  284. }
  285. $select = '';
  286. if(!empty($_G['cache'][$reasionkey])) {
  287. foreach($_G['cache'][$reasionkey] as $reason) {
  288. $select .= !$isadmincp ? ($reason ? '<li>'.$reason.'</li>' : '<li>--------</li>') : ($reason ? '<option value="'.dhtmlspecialchars($reason).'">'.$reason.'</option>' : '<option></option>');
  289. }
  290. }
  291. if($select) {
  292. return $select;
  293. } else {
  294. return false;
  295. }
  296. }
  297. function acpmsg($message, $url = '', $type = '', $extra = '') {
  298. if(defined('IN_ADMINCP')) {
  299. !defined('CPHEADER_SHOWN') && cpheader();
  300. cpmsg($message, $url, $type, $extra);
  301. } else {
  302. showmessage($message, $url, $extra);
  303. }
  304. }
  305. function savebanlog($username, $origgroupid, $newgroupid, $expiration, $reason) {
  306. global $_G;
  307. if($_G['setting']['plugins']['func'][HOOKTYPE]['savebanlog']) {
  308. $param = func_get_args();
  309. hookscript('savebanlog', 'global', 'funcs', array('param' => $param), 'savebanlog');
  310. }
  311. writelog('banlog', dhtmlspecialchars("$_G[timestamp]\t{$_G[member][username]}\t$_G[groupid]\t$_G[clientip]\t$username\t$origgroupid\t$newgroupid\t$expiration\t$reason"));
  312. }
  313. function clearlogstring($str) {
  314. if(!empty($str)) {
  315. if(!is_array($str)) {
  316. $str = dhtmlspecialchars(trim($str));
  317. $str = str_replace(array("\t", "\r\n", "\n", " ", " "), ' ', $str);
  318. } else {
  319. foreach ($str as $key => $val) {
  320. $str[$key] = clearlogstring($val);
  321. }
  322. }
  323. }
  324. return $str;
  325. }
  326. function implodearray($array, $skip = array()) {
  327. $return = '';
  328. if(is_array($array) && !empty($array)) {
  329. foreach ($array as $key => $value) {
  330. if(empty($skip) || !in_array($key, $skip, true)) {
  331. if(is_array($value)) {
  332. $return .= "$key={".implodearray($value, $skip)."}; ";
  333. } elseif(!empty($value)) {
  334. $return .= "$key=$value; ";
  335. } else {
  336. $return .= '';
  337. }
  338. }
  339. }
  340. }
  341. return $return;
  342. }
  343. function undeletethreads($tids) {
  344. global $_G;
  345. if($_G['setting']['plugins']['func'][HOOKTYPE]['undeletethreads']) {
  346. $param = func_get_args();
  347. hookscript('undeletethreads', 'global', 'funcs', array('param' => $param), 'undeletethreads');
  348. }
  349. $threadsundel = 0;
  350. if($tids && is_array($tids)) {
  351. $arrtids = $tids;
  352. $tids = '\''.implode('\',\'', $tids).'\'';
  353. $tuidarray = $ruidarray = $fidarray = $posttabletids = array();
  354. foreach(C::t('forum_thread')->fetch_all_by_tid($arrtids) as $thread) {
  355. $posttabletids[$thread['posttableid'] ? $thread['posttableid'] : 0][] = $thread['tid'];
  356. }
  357. foreach($posttabletids as $posttableid => $ptids) {
  358. foreach(C::t('forum_post')->fetch_all_by_tid($posttableid, $ptids, false) as $post) {
  359. if($post['first']) {
  360. $tuidarray[$post['fid']][] = $post['authorid'];
  361. } else {
  362. $ruidarray[$post['fid']][] = $post['authorid'];
  363. }
  364. if(!in_array($post['fid'], $fidarray)) {
  365. $fidarray[] = $post['fid'];
  366. }
  367. }
  368. C::t('forum_post')->update_by_tid($posttableid, $ptids, array('invisible' => '0'), true);
  369. }
  370. if($tuidarray) {
  371. foreach($tuidarray as $fid => $tuids) {
  372. updatepostcredits('+', $tuids, 'post', $fid);
  373. }
  374. }
  375. if($ruidarray) {
  376. foreach($ruidarray as $fid => $ruids) {
  377. updatepostcredits('+', $ruids, 'reply', $fid);
  378. }
  379. }
  380. $threadsundel = C::t('forum_thread')->update($arrtids, array('displayorder'=>0, 'moderated'=>1));
  381. updatemodlog($tids, 'UDL');
  382. updatemodworks('UDL', $threadsundel);
  383. foreach($fidarray as $fid) {
  384. updateforumcount($fid);
  385. }
  386. }
  387. return $threadsundel;
  388. }
  389. function recyclebinpostdelete($deletepids, $posttableid = false) {
  390. if(empty($deletepids)) {
  391. return 0;
  392. }
  393. require_once libfile('function/delete');
  394. return deletepost($deletepids, 'pid', true, $posttableid);
  395. }
  396. function recyclebinpostundelete($undeletepids, $posttableid = false) {
  397. global $_G;
  398. if($_G['setting']['plugins']['func'][HOOKTYPE]['recyclebinpostundelete']) {
  399. $param = func_get_args();
  400. hookscript('recyclebinpostundelete', 'global', 'funcs', array('param' => $param), 'recyclebinpostundelete');
  401. }
  402. $postsundel = 0;
  403. if(empty($undeletepids)) {
  404. return $postsundel;
  405. }
  406. loadcache('posttableids');
  407. $posttableids = !empty($_G['cache']['posttableids']) ? ($posttableid !== false && in_array($posttableid, $_G['cache']['posttableids']) ? array($posttableid) : $_G['cache']['posttableids']): array('0');
  408. $postarray = $ruidarray = $fidarray = $tidarray = array();
  409. foreach($posttableids as $ptid) {
  410. foreach(C::t('forum_post')->fetch_all($ptid, $undeletepids, false) as $post) {
  411. if(!$post['first']) {
  412. $ruidarray[$post['fid']][] = $post['authorid'];
  413. }
  414. $fidarray[$post['fid']] = $post['fid'];
  415. $tidarray[$post['tid']] = $post['tid'];
  416. }
  417. }
  418. if(empty($fidarray)) {
  419. return $postsundel;
  420. }
  421. C::t('forum_post')->update($posttableid, $undeletepids, array('invisible' => '0'), true);
  422. include_once libfile('function/post');
  423. if($ruidarray) {
  424. foreach($ruidarray as $fid => $ruids) {
  425. updatepostcredits('+', $ruids, 'reply', $fid);
  426. }
  427. }
  428. foreach($tidarray as $tid) {
  429. updatethreadcount($tid, 1);
  430. }
  431. foreach($fidarray as $fid) {
  432. updateforumcount($fid);
  433. }
  434. return count($undeletepids);
  435. }
  436. ?>