PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/source/function/function_misc.php

https://github.com/kuaileshike/upload
PHP | 509 lines | 429 code | 74 blank | 6 comment | 133 complexity | 81342be511311519921d4b2c1603c143 MD5 | raw file
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: function_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) {
  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. notification_add($var['authorid'], 'system', $item, $notevar, 1);
  277. }
  278. }
  279. function modreasonselect($isadmincp = 0, $reasionkey = 'modreasons') {
  280. global $_G;
  281. if(!isset($_G['cache'][$reasionkey]) || !is_array($_G['cache'][$reasionkey])) {
  282. loadcache(array($reasionkey, 'stamptypeid'));
  283. }
  284. $select = '';
  285. if(!empty($_G['cache'][$reasionkey])) {
  286. foreach($_G['cache'][$reasionkey] as $reason) {
  287. $select .= !$isadmincp ? ($reason ? '<li>'.$reason.'</li>' : '<li>--------</li>') : ($reason ? '<option value="'.dhtmlspecialchars($reason).'">'.$reason.'</option>' : '<option></option>');
  288. }
  289. }
  290. if($select) {
  291. return $select;
  292. } else {
  293. return false;
  294. }
  295. }
  296. function acpmsg($message, $url = '', $type = '', $extra = '') {
  297. if(defined('IN_ADMINCP')) {
  298. !defined('CPHEADER_SHOWN') && cpheader();
  299. cpmsg($message, $url, $type, $extra);
  300. } else {
  301. showmessage($message, $url, $extra);
  302. }
  303. }
  304. function savebanlog($username, $origgroupid, $newgroupid, $expiration, $reason) {
  305. global $_G;
  306. if($_G['setting']['plugins']['func'][HOOKTYPE]['savebanlog']) {
  307. $param = func_get_args();
  308. hookscript('savebanlog', 'global', 'funcs', array('param' => $param), 'savebanlog');
  309. }
  310. 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"));
  311. }
  312. function clearlogstring($str) {
  313. if(!empty($str)) {
  314. if(!is_array($str)) {
  315. $str = dhtmlspecialchars(trim($str));
  316. $str = str_replace(array("\t", "\r\n", "\n", " ", " "), ' ', $str);
  317. } else {
  318. foreach ($str as $key => $val) {
  319. $str[$key] = clearlogstring($val);
  320. }
  321. }
  322. }
  323. return $str;
  324. }
  325. function implodearray($array, $skip = array()) {
  326. $return = '';
  327. if(is_array($array) && !empty($array)) {
  328. foreach ($array as $key => $value) {
  329. if(empty($skip) || !in_array($key, $skip, true)) {
  330. if(is_array($value)) {
  331. $return .= "$key={".implodearray($value, $skip)."}; ";
  332. } elseif(!empty($value)) {
  333. $return .= "$key=$value; ";
  334. } else {
  335. $return .= '';
  336. }
  337. }
  338. }
  339. }
  340. return $return;
  341. }
  342. function undeletethreads($tids) {
  343. global $_G;
  344. if($_G['setting']['plugins']['func'][HOOKTYPE]['undeletethreads']) {
  345. $param = func_get_args();
  346. hookscript('undeletethreads', 'global', 'funcs', array('param' => $param), 'undeletethreads');
  347. }
  348. $threadsundel = 0;
  349. if($tids && is_array($tids)) {
  350. $arrtids = $tids;
  351. $tids = '\''.implode('\',\'', $tids).'\'';
  352. $tuidarray = $ruidarray = $fidarray = $posttabletids = array();
  353. foreach(C::t('forum_thread')->fetch_all_by_tid($arrtids) as $thread) {
  354. $posttabletids[$thread['posttableid'] ? $thread['posttableid'] : 0][] = $thread['tid'];
  355. }
  356. foreach($posttabletids as $posttableid => $ptids) {
  357. foreach(C::t('forum_post')->fetch_all_by_tid($posttableid, $ptids, false) as $post) {
  358. if($post['first']) {
  359. $tuidarray[$post['fid']][] = $post['authorid'];
  360. } else {
  361. $ruidarray[$post['fid']][] = $post['authorid'];
  362. }
  363. if(!in_array($post['fid'], $fidarray)) {
  364. $fidarray[] = $post['fid'];
  365. }
  366. }
  367. C::t('forum_post')->update_by_tid($posttableid, $ptids, array('invisible' => '0'), true);
  368. }
  369. if($tuidarray) {
  370. foreach($tuidarray as $fid => $tuids) {
  371. updatepostcredits('+', $tuids, 'post', $fid);
  372. }
  373. }
  374. if($ruidarray) {
  375. foreach($ruidarray as $fid => $ruids) {
  376. updatepostcredits('+', $ruids, 'reply', $fid);
  377. }
  378. }
  379. $threadsundel = C::t('forum_thread')->update($arrtids, array('displayorder'=>0, 'moderated'=>1));
  380. updatemodlog($tids, 'UDL');
  381. updatemodworks('UDL', $threadsundel);
  382. foreach($fidarray as $fid) {
  383. updateforumcount($fid);
  384. }
  385. }
  386. return $threadsundel;
  387. }
  388. function recyclebinpostdelete($deletepids, $posttableid = false) {
  389. if(empty($deletepids)) {
  390. return 0;
  391. }
  392. require_once libfile('function/delete');
  393. return deletepost($deletepids, 'pid', true, $posttableid);
  394. }
  395. function recyclebinpostundelete($undeletepids, $posttableid = false) {
  396. global $_G;
  397. if($_G['setting']['plugins']['func'][HOOKTYPE]['recyclebinpostundelete']) {
  398. $param = func_get_args();
  399. hookscript('recyclebinpostundelete', 'global', 'funcs', array('param' => $param), 'recyclebinpostundelete');
  400. }
  401. $postsundel = 0;
  402. if(empty($undeletepids)) {
  403. return $postsundel;
  404. }
  405. loadcache('posttableids');
  406. $posttableids = !empty($_G['cache']['posttableids']) ? ($posttableid !== false && in_array($posttableid, $_G['cache']['posttableids']) ? array($posttableid) : $_G['cache']['posttableids']): array('0');
  407. $postarray = $ruidarray = $fidarray = $tidarray = array();
  408. foreach($posttableids as $ptid) {
  409. foreach(C::t('forum_post')->fetch_all($ptid, $undeletepids, false) as $post) {
  410. if(!$post['first']) {
  411. $ruidarray[$post['fid']][] = $post['authorid'];
  412. }
  413. $fidarray[$post['fid']] = $post['fid'];
  414. $tidarray[$post['tid']] = $post['tid'];
  415. }
  416. }
  417. if(empty($fidarray)) {
  418. return $postsundel;
  419. }
  420. C::t('forum_post')->update($posttableid, $undeletepids, array('invisible' => '0'), true);
  421. include_once libfile('function/post');
  422. if($ruidarray) {
  423. foreach($ruidarray as $fid => $ruids) {
  424. updatepostcredits('+', $ruids, 'reply', $fid);
  425. }
  426. }
  427. foreach($tidarray as $tid) {
  428. updatethreadcount($tid, 1);
  429. }
  430. foreach($fidarray as $fid) {
  431. updateforumcount($fid);
  432. }
  433. return count($undeletepids);
  434. }
  435. ?>