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

/source/module/forum/forum_guide.php

https://github.com/jinbo51/DiscuzX
PHP | 472 lines | 444 code | 22 blank | 6 comment | 117 complexity | 91e34a1f1317bc4b365aec5b699e80a9 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: forum_guide.php 32049 2012-11-02 04:07:14Z liulanbo $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. $view = $_GET['view'];
  12. loadcache('forum_guide');
  13. if(!in_array($view, array('hot', 'digest', 'new', 'my', 'newthread', 'sofa'))) {
  14. $view = 'hot';
  15. }
  16. $lang = lang('forum/template');
  17. $navtitle = $lang['guide'].'-'.$lang['guide_'.$view];
  18. $perpage = 50;
  19. $start = $perpage * ($_G['page'] - 1);
  20. $data = array();
  21. if($_GET['rss'] == 1) {
  22. if($view == 'index' || $view == 'my') {
  23. showmessage('URL_ERROR');
  24. }
  25. $ttl = 30;
  26. $charset = $_G['config']['output']['charset'];
  27. dheader("Content-type: application/xml");
  28. echo "<?xml version=\"1.0\" encoding=\"".$charset."\"?>\n".
  29. "<rss version=\"2.0\">\n".
  30. " <channel>\n".
  31. " <title>{$_G[setting][bbname]} - $lang[guide] - ".$lang['guide_'.$view]."</title>\n".
  32. " <link>{$_G[siteurl]}forum.php?mod=guide&amp;view=$view</link>\n".
  33. " <description>".$lang['guide_'.$view]."</description>\n".
  34. " <copyright>Copyright(C) {$_G[setting][bbname]}</copyright>\n".
  35. " <generator>Discuz! Board by Comsenz Inc.</generator>\n".
  36. " <lastBuildDate>".gmdate('r', TIMESTAMP)."</lastBuildDate>\n".
  37. " <ttl>$ttl</ttl>\n".
  38. " <image>\n".
  39. " <url>{$_G[siteurl]}static/image/common/logo_88_31.gif</url>\n".
  40. " <title>{$_G[setting][bbname]}</title>\n".
  41. " <link>{$_G[siteurl]}</link>\n".
  42. " </image>\n";
  43. $info = C::t('forum_rsscache')->fetch_all_by_guidetype($view, $perpage);
  44. if(empty($info) || (TIMESTAMP - $info[0]['lastupdate'] > $ttl * 60)) {
  45. update_guide_rsscache($view, $perpage);
  46. }
  47. foreach($info as $thread) {
  48. list($thread['description'], $attachremote, $attachfile, $attachsize) = explode("\t", $thread['description']);
  49. if($attachfile) {
  50. if($attachremote) {
  51. $filename = $_G['setting']['ftp']['attachurl'].'forum/'.$attachfile;
  52. } else {
  53. $filename = $_G['siteurl'].$_G['setting']['attachurl'].'forum/'.$attachfile;
  54. }
  55. }
  56. echo " <item>\n".
  57. " <title>".$thread['subject']."</title>\n".
  58. " <link>$_G[siteurl]".($trewriteflag ? rewriteoutput('forum_viewthread', 1, '', $thread['tid']) : "forum.php?mod=viewthread&amp;tid=$thread[tid]")."</link>\n".
  59. " <description><![CDATA[".dhtmlspecialchars($thread['description'])."]]></description>\n".
  60. " <category>".dhtmlspecialchars($thread['forum'])."</category>\n".
  61. " <author>".dhtmlspecialchars($thread['author'])."</author>\n".
  62. ($attachfile ? '<enclosure url="'.$filename.'" length="'.$attachsize.'" type="image/jpeg" />' : '').
  63. " <pubDate>".gmdate('r', $thread['dateline'])."</pubDate>\n".
  64. " </item>\n";
  65. }
  66. echo " </channel>\n".
  67. "</rss>";
  68. exit();
  69. }
  70. if($view != 'index') {
  71. $theurl = 'forum.php?mod=guide&view='.$view;
  72. if($view == 'my') {
  73. if(!$_G['uid']) {
  74. showmessage('to_login', '', array(), array('login' => 1));
  75. }
  76. $lang = lang('forum/template');
  77. $filter_array = array( 'common' => $lang['have_posted'], 'save' => $lang['guide_draft'], 'close' => $lang['close'], 'aduit' => $lang['pending'], 'ignored' => $lang['have_ignored'], 'recyclebin' => $lang['forum_recyclebin']);
  78. $viewtype = in_array($_GET['type'], array('reply', 'thread', 'postcomment')) ? $_GET['type'] : 'thread';
  79. if($searchkey = stripsearchkey($_GET['searchkey'])) {
  80. $searchkey = dhtmlspecialchars($searchkey);
  81. }
  82. $theurl .= '&type='.$viewtype;
  83. $filter = in_array($_GET['filter'], array_keys($filter_array)) ? $_GET['filter'] : '';
  84. $searchbody = 0;
  85. if($filter) {
  86. $theurl .= '&filter='.$filter;
  87. $searchbody = 1;
  88. }
  89. if($_GET['fid']) {
  90. $theurl .= '&fid='.intval($_GET['fid']);
  91. $searchbody = 1;
  92. }
  93. if($searchkey) {
  94. $theurl .= '&searchkey='.$searchkey;
  95. $searchbody = 1;
  96. }
  97. require_once libfile('function/forumlist');
  98. $forumlist = forumselect(FALSE, 0, intval($_GET['fid']));
  99. $data['my'] = get_my_threads($viewtype, $_GET['fid'], $filter, $searchkey, $start, $perpage, $theurl);
  100. $tids = $data['my']['tids'];
  101. $posts = $data['my']['posts'];
  102. } else {
  103. $data[$view] = get_guide_list($view, $start, $perpage);
  104. }
  105. if(empty($data['my']['multi'])) {
  106. $multipage = multi($data[$view]['threadcount'], $perpage, $_G['page'], $theurl, $_G['setting']['threadmaxpages']);
  107. } else {
  108. $multipage = $data['my']['multi'];
  109. }
  110. } else {
  111. $data['hot'] = get_guide_list('hot', 0, 30);
  112. $data['digest'] = get_guide_list('digest', 0, 30);
  113. $data['new'] = get_guide_list('new', 0, 30);
  114. $data['newthread'] = get_guide_list('newthread', 0, 30);
  115. }
  116. loadcache('stamps');
  117. $currentview[$view] = 'class="xw1 a"';
  118. $navigation = $view != 'index' ? ' <em>&rsaquo;</em> <a href="forum.php?mod=guide&view='.$view.'">'.$lang['guide_'.$view].'</a>' : '';
  119. include template('forum/guide');
  120. function get_guide_list($view, $start = 0, $num = 50, $again = 0) {
  121. global $_G;
  122. $setting_guide = unserialize($_G['setting']['guide']);
  123. if(!in_array($view, array('hot', 'digest', 'new', 'newthread', 'sofa'))) {
  124. return array();
  125. }
  126. loadcache('forums');
  127. $cachetimelimit = ($view != 'sofa') ? 900 : 60;
  128. $cache = $_G['cache']['forum_guide'][$view.($view=='sofa' && $_G['fid'] ? $_G['fid'] : '')];
  129. if($cache && (TIMESTAMP - $cache['cachetime']) < $cachetimelimit) {
  130. $tids = $cache['data'];
  131. $threadcount = count($tids);
  132. $tids = array_slice($tids, $start, $num, true);
  133. $updatecache = false;
  134. if(empty($tids)) {
  135. return array();
  136. }
  137. } else {
  138. $dateline = 0;
  139. $maxnum = 50000;
  140. if($setting_guide[$view.'dt']) {
  141. $dateline = time() - intval($setting_guide[$view.'dt']);
  142. }
  143. if($view != 'sofa') {
  144. $maxtid = C::t('forum_thread')->fetch_max_tid();
  145. $limittid = max(0,($maxtid - $maxnum));
  146. if($again) {
  147. $limittid = max(0,($limittid - $maxnum));
  148. }
  149. $tids = array();
  150. }
  151. foreach($_G['cache']['forums'] as $fid => $forum) {
  152. if($forum['type'] != 'group' && $forum['status'] > 0 && !$forum['viewperm'] && !$forum['havepassword']) {
  153. $fids[] = $fid;
  154. }
  155. }
  156. if(empty($fids)) {
  157. return array();
  158. }
  159. if($view == 'sofa') {
  160. if($_GET['fid']) {
  161. $sofa = C::t('forum_sofa')->fetch_all_by_fid($_GET['fid'], $start, $num);
  162. } else {
  163. $sofa = C::t('forum_sofa')->range($start, $num);
  164. foreach($sofa as $sofatid => $sofathread) {
  165. if(!in_array($sofathread, $fids)) {
  166. unset($sofathread[$sofatid]);
  167. }
  168. }
  169. }
  170. $tids = array_keys($sofa);
  171. }
  172. $updatecache = true;
  173. }
  174. $query = C::t('forum_thread')->fetch_all_for_guide($view, $limittid, $tids, $_G['setting']['heatthread']['guidelimit'], $dateline);
  175. $n = 0;
  176. foreach($query as $thread) {
  177. if(empty($tids) && ($thread['isgroup'] || !in_array($thread['fid'], $fids))) {
  178. continue;
  179. }
  180. if($thread['displayorder'] < 0) {
  181. continue;
  182. }
  183. $thread = guide_procthread($thread);
  184. $threadids[] = $thread['tid'];
  185. if($tids || ($n >= $start && $n < ($start + $num))) {
  186. $list[$thread[tid]] = $thread;
  187. $fids[$thread[fid]] = $thread['fid'];
  188. }
  189. $n ++;
  190. }
  191. if($limittid > $maxnum && !$again && count($list) < 50) {
  192. return get_guide_list($view, $start, $num, 1);
  193. }
  194. $forumnames = array();
  195. if($fids) {
  196. $forumnames = C::t('forum_forum')->fetch_all_name_by_fid($fids);
  197. }
  198. $threadlist = array();
  199. if($tids) {
  200. $threadids = array();
  201. foreach($tids as $key => $tid) {
  202. if($list[$tid]) {
  203. $threadlist[$key] = $list[$tid];
  204. $threadids[] = $tid;
  205. }
  206. }
  207. } else {
  208. $threadlist = $list;
  209. }
  210. unset($list);
  211. if($updatecache) {
  212. $threadcount = count($threadids);
  213. $data = array('cachetime' => TIMESTAMP, 'data' => $threadids);
  214. $_G['cache']['forum_guide'][$view.($view=='sofa' && $_G['fid'] ? $_G['fid'] : '')] = $data;
  215. savecache('forum_guide', $_G['cache']['forum_guide']);
  216. }
  217. return array('forumnames' => $forumnames, 'threadcount' => $threadcount, 'threadlist' => $threadlist);
  218. }
  219. function get_my_threads($viewtype, $fid = 0, $filter = '', $searchkey = '', $start = 0, $perpage = 20, $theurl = '') {
  220. global $_G;
  221. $fid = $fid ? intval($fid) : null;
  222. loadcache('forums');
  223. $dglue = '=';
  224. if($viewtype == 'thread') {
  225. $authorid = $_G['uid'];
  226. $displayorder = -1;
  227. $dglue = '!=';
  228. if($filter == 'recyclebin') {
  229. $displayorder = -1;
  230. } elseif($filter == 'aduit') {
  231. $displayorder = -2;
  232. } elseif($filter == 'ignored') {
  233. $displayorder = -3;
  234. } elseif($filter == 'save') {
  235. $displayorder = -4;
  236. } elseif($filter == 'close') {
  237. $closed = 1;
  238. } elseif($filter == 'common') {
  239. $closed = 0;
  240. $displayorder = 0;
  241. $dglue = '>=';
  242. }
  243. $gids = $fids = $forums = array();
  244. foreach(C::t('forum_thread')->fetch_all_by_authorid_displayorder($authorid, $displayorder, $dglue, $closed, $searchkey, $start, $perpage, null, $fid) as $tid => $value) {
  245. if(!isset($_G['cache']['forums'][$value['fid']])) {
  246. $gids[$value['fid']] = $value['fid'];
  247. } else {
  248. $forumnames[$value['fid']] = array('fid'=> $value['fid'], 'name' => $_G['cache']['forums'][$value['fid']]['name']);
  249. }
  250. $list[$value['tid']] = guide_procthread($value);
  251. }
  252. if(!empty($gids)) {
  253. $gforumnames = C::t('forum_forum')->fetch_all_name_by_fid($gids);
  254. foreach($gforumnames as $fid => $val) {
  255. $forumnames[$fid] = $val;
  256. }
  257. }
  258. $listcount = count($list);
  259. } elseif($viewtype == 'postcomment') {
  260. require_once libfile('function/post');
  261. $pids = $tids = array();
  262. $postcommentarr = C::t('forum_postcomment')->fetch_all_by_authorid($_G['uid'], $start, $perpage);
  263. foreach($postcommentarr as $value) {
  264. $pids[] = $value['pid'];
  265. $tids[] = $value['tid'];
  266. }
  267. $pids = C::t('forum_post')->fetch_all(0, $pids);
  268. $tids = C::t('forum_thread')->fetch_all($tids);
  269. $list = $fids = array();
  270. foreach($postcommentarr as $value) {
  271. $value['authorid'] = $pids[$value['pid']]['authorid'];
  272. $value['fid'] = $pids[$value['pid']]['fid'];
  273. $value['invisible'] = $pids[$value['pid']]['invisible'];
  274. $value['dateline'] = $pids[$value['pid']]['dateline'];
  275. $value['message'] = $pids[$value['pid']]['message'];
  276. $value['special'] = $tids[$value['tid']]['special'];
  277. $value['status'] = $tids[$value['tid']]['status'];
  278. $value['subject'] = $tids[$value['tid']]['subject'];
  279. $value['digest'] = $tids[$value['tid']]['digest'];
  280. $value['attachment'] = $tids[$value['tid']]['attachment'];
  281. $value['replies'] = $tids[$value['tid']]['replies'];
  282. $value['views'] = $tids[$value['tid']]['views'];
  283. $value['lastposter'] = $tids[$value['tid']]['lastposter'];
  284. $value['lastpost'] = $tids[$value['tid']]['lastpost'];
  285. $value['icon'] = $tids[$value['tid']]['icon'];
  286. $value['tid'] = $pids[$value['pid']]['tid'];
  287. $fids[] = $value['fid'];
  288. $value['comment'] = messagecutstr($value['comment'], 100);
  289. $list[] = guide_procthread($value);
  290. }
  291. unset($pids, $tids, $postcommentarr);
  292. if($fids) {
  293. $fids = array_unique($fids);
  294. $forumnames = C::t('forum_forum')->fetch_all_name_by_fid($gids);
  295. }
  296. $listcount = count($list);
  297. } else {
  298. $invisible = null;
  299. if($filter == 'recyclebin') {
  300. $invisible = -5;
  301. } elseif($filter == 'aduit') {
  302. $invisible = -2;
  303. } elseif($filter == 'save' || $filter == 'ignored') {
  304. $invisible = -3;
  305. $displayorder = -4;
  306. } elseif($filter == 'close') {
  307. $closed = 1;
  308. } elseif($filter == 'common') {
  309. $invisible = 0;
  310. $displayorder = 0;
  311. $dglue = '>=';
  312. $closed = 0;
  313. }
  314. require_once libfile('function/post');
  315. $posts = C::t('forum_post')->fetch_all_by_authorid(0, $_G['uid'], true, 'DESC', $start, $perpage, null, $invisible, $fid, $followfid);
  316. $listcount = count($posts);
  317. foreach($posts as $pid => $post) {
  318. $tids[$post['tid']][] = $pid;
  319. $post['message'] = !getstatus($post['status'], 2) || $post['authorid'] == $_G['uid'] ? messagecutstr($post['message'], 100) : '';
  320. $posts[$pid] = $post;
  321. }
  322. if(!empty($tids)) {
  323. $threads = C::t('forum_thread')->fetch_all_by_tid_displayorder(array_keys($tids), $displayorder, $dglue, array(), $closed);
  324. foreach($threads as $tid => $thread) {
  325. if(!isset($_G['cache']['forums'][$thread['fid']])) {
  326. $gids[$thread['fid']] = $thread['fid'];
  327. } else {
  328. $forumnames[$thread[fid]] = array('fid' => $thread['fid'], 'name' => $_G['cache']['forums'][$thread[fid]]['name']);
  329. }
  330. $threads[$tid] = guide_procthread($thread);
  331. }
  332. if(!empty($gids)) {
  333. $groupforums = C::t('forum_forum')->fetch_all_name_by_fid($gids);
  334. foreach($groupforums as $fid => $val) {
  335. $forumnames[$fid] = $val;
  336. }
  337. }
  338. $list = array();
  339. foreach($tids as $key => $val) {
  340. $list[$key] = $threads[$key];
  341. }
  342. unset($threads);
  343. }
  344. }
  345. $multi = simplepage($listcount, $perpage, $_G['page'], $theurl);
  346. return array('forumnames' => $forumnames, 'threadcount' => $listcount, 'threadlist' => $list, 'multi' => $multi, 'tids' => $tids, 'posts' => $posts);
  347. }
  348. function guide_procthread($thread) {
  349. global $_G;
  350. $todaytime = strtotime(dgmdate(TIMESTAMP, 'Ymd'));
  351. $thread['lastposterenc'] = rawurlencode($thread['lastposter']);
  352. $thread['multipage'] = '';
  353. $topicposts = $thread['special'] ? $thread['replies'] : $thread['replies'] + 1;
  354. if($topicposts > $_G['ppp']) {
  355. $pagelinks = '';
  356. $thread['pages'] = ceil($topicposts / $_G['ppp']);
  357. for($i = 2; $i <= 6 && $i <= $thread['pages']; $i++) {
  358. $pagelinks .= "<a href=\"forum.php?mod=viewthread&tid=$thread[tid]&amp;extra=$extra&amp;page=$i\">$i</a>";
  359. }
  360. if($thread['pages'] > 6) {
  361. $pagelinks .= "..<a href=\"forum.php?mod=viewthread&tid=$thread[tid]&amp;extra=$extra&amp;page=$thread[pages]\">$thread[pages]</a>";
  362. }
  363. $thread['multipage'] = '&nbsp;...'.$pagelinks;
  364. }
  365. if($thread['highlight']) {
  366. $string = sprintf('%02d', $thread['highlight']);
  367. $stylestr = sprintf('%03b', $string[0]);
  368. $thread['highlight'] = ' style="';
  369. $thread['highlight'] .= $stylestr[0] ? 'font-weight: bold;' : '';
  370. $thread['highlight'] .= $stylestr[1] ? 'font-style: italic;' : '';
  371. $thread['highlight'] .= $stylestr[2] ? 'text-decoration: underline;' : '';
  372. $thread['highlight'] .= $string[1] ? 'color: '.$_G['forum_colorarray'][$string[1]] : '';
  373. $thread['highlight'] .= '"';
  374. } else {
  375. $thread['highlight'] = '';
  376. }
  377. $thread['recommendicon'] = '';
  378. if(!empty($_G['setting']['recommendthread']['status']) && $thread['recommends']) {
  379. foreach($_G['setting']['recommendthread']['iconlevels'] as $k => $i) {
  380. if($thread['recommends'] > $i) {
  381. $thread['recommendicon'] = $k+1;
  382. break;
  383. }
  384. }
  385. }
  386. $thread['moved'] = $thread['heatlevel'] = $thread['new'] = 0;
  387. $thread['icontid'] = $thread['forumstick'] || !$thread['moved'] && $thread['isgroup'] != 1 ? $thread['tid'] : $thread['closed'];
  388. $thread['folder'] = 'common';
  389. $thread['weeknew'] = TIMESTAMP - 604800 <= $thread['dbdateline'];
  390. if($thread['replies'] > $thread['views']) {
  391. $thread['views'] = $thread['replies'];
  392. }
  393. if($_G['setting']['heatthread']['iconlevels']) {
  394. foreach($_G['setting']['heatthread']['iconlevels'] as $k => $i) {
  395. if($thread['heats'] > $i) {
  396. $thread['heatlevel'] = $k + 1;
  397. break;
  398. }
  399. }
  400. }
  401. $thread['istoday'] = $thread['dateline'] > $todaytime ? 1 : 0;
  402. $thread['dbdateline'] = $thread['dateline'];
  403. $thread['dateline'] = dgmdate($thread['dateline'], 'u', '9999', getglobal('setting/dateformat'));
  404. $thread['dblastpost'] = $thread['lastpost'];
  405. $thread['lastpost'] = dgmdate($thread['lastpost'], 'u');
  406. if(in_array($thread['displayorder'], array(1, 2, 3, 4))) {
  407. $thread['id'] = 'stickthread_'.$thread['tid'];
  408. } else {
  409. $thread['id'] = 'normalthread_'.$thread['tid'];
  410. }
  411. $thread['rushreply'] = getstatus($thread['status'], 3);
  412. return $thread;
  413. }
  414. function update_guide_rsscache($type, $perpage) {
  415. global $_G;
  416. $processname = 'guide_rss_cache';
  417. if(discuz_process::islocked($processname, 600)) {
  418. return false;
  419. }
  420. C::t('forum_rsscache')->delete_by_guidetype($type);
  421. require_once libfile('function/post');
  422. $data = get_guide_list($type, 0, $perpage);
  423. foreach($data['threadlist'] as $thread) {
  424. $thread['author'] = $thread['author'] != '' ? addslashes($thread['author']) : 'Anonymous';
  425. $thread['subject'] = addslashes($thread['subject']);
  426. $post = C::t('forum_post')->fetch_threadpost_by_tid_invisible($thread['tid']);
  427. $attachdata = '';
  428. if($post['attachment'] == 2) {
  429. $attach = C::t('forum_attachment_n')->fetch_max_image('tid:'.$thread['tid'], 'pid', $post['pid']);
  430. $attachdata = "\t".$attach['remote']."\t".$attach['attachment']."\t".$attach['filesize'];
  431. }
  432. $thread['message'] = $post['message'];
  433. $thread['status'] = $post['status'];
  434. $thread['description'] = $thread['readperm'] > 0 || $thread['price'] > 0 || $thread['status'] & 1 ? '' : addslashes(messagecutstr($thread['message'], 250 - strlen($attachdata)).$attachdata);
  435. C::t('forum_rsscache')->insert(array(
  436. 'lastupdate'=>$_G['timestamp'],
  437. 'fid'=>$thread['fid'],
  438. 'tid'=>$thread['tid'],
  439. 'dateline'=>$thread['dbdateline'],
  440. 'forum'=>strip_tags($data['forumnames'][$thread[fid]]['name']),
  441. 'author'=>$thread['author'],
  442. 'subject'=>$thread['subject'],
  443. 'description'=>$thread['description'],
  444. 'guidetype'=>$type
  445. ), false, true);
  446. }
  447. discuz_process::unlock($processname);
  448. return true;
  449. }
  450. ?>