PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/modcp/threads.inc.php

https://github.com/gcao/bbs
PHP | 402 lines | 318 code | 78 blank | 6 comment | 110 complexity | 9a24cbf8486cdb9094c96b6782210817 MD5 | raw file
  1. <?php
  2. /*
  3. [Discuz!] (C)2001-2009 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: threads.inc.php 21059 2009-11-10 01:28:17Z monkey $
  6. */
  7. if(!defined('IN_DISCUZ') || !defined('IN_MODCP')) {
  8. exit('Access Denied');
  9. }
  10. $op = !in_array($op , array('threads', 'posts')) ? 'threads' : $op;
  11. $do = !empty($do) ? dhtmlspecialchars($do) : '';
  12. $modtpl = $op == 'posts' ? 'modcp_posts' : 'modcp_threads';
  13. if($op == 'threads') {
  14. if($fid && $forum['ismoderator']) {
  15. $result = array();
  16. foreach (array('threadoption', 'viewsless', 'viewsmore', 'repliesless', 'repliesmore', 'noreplydays') as $key) {
  17. $$key = isset($$key) && is_numeric($$key) ? intval($$key) : '';
  18. $result[$key] = $$key;
  19. }
  20. foreach (array('starttime', 'endtime', 'keywords', 'users') as $key) {
  21. $result[$key] = isset($$key) ? dhtmlspecialchars($$key) : '';
  22. }
  23. $threadoptionselect = array($threadoption => 'selected');
  24. if($do == 'search' && submitcheck('submit')) {
  25. $sql = '';
  26. if($threadoption > 0 && $threadoption < 255) {
  27. $sql .= " AND special='$threadoption'";
  28. } elseif($threadoption == 999) {
  29. $sql .= " AND digest in(1,2,3)";
  30. } elseif($threadoption == 888) {
  31. $sql .= " AND displayorder IN(1,2,3)";
  32. }
  33. $sql .= $viewsless !== ''? " AND views<='$viewsless'" : '';
  34. $sql .= $viewsmore !== ''? " AND views>='$viewsmore'" : '';
  35. $sql .= $repliesless !== ''? " AND replies<='$repliesless'" : '';
  36. $sql .= $repliesmore !== ''? " AND replies>='$repliesmore'" : '';
  37. $sql .= $noreplydays !== ''? " AND lastpost<='$timestamp'-'$noreplydays'*86400" : '';
  38. $sql .= $starttime != '' ? " AND dateline>='".strtotime($starttime)."'" : '';
  39. $sql .= $endtime != '' ? " AND dateline<='".strtotime($endtime)."'" : '';
  40. if(trim($keywords)) {
  41. $sqlkeywords = '';
  42. $or = '';
  43. $keywords = explode(',', str_replace(' ', '', $keywords));
  44. for($i = 0; $i < count($keywords); $i++) {
  45. $sqlkeywords .= " $or subject LIKE '%".$keywords[$i]."%'";
  46. $or = 'OR';
  47. }
  48. $sql .= " AND ($sqlkeywords)";
  49. $keywords = implode(', ', $keywords);
  50. }
  51. if(trim($users)) {
  52. $sql .= " AND author IN ('".str_replace(',', '\',\'', str_replace(' ', '', trim($users)))."')";
  53. }
  54. if($sql) {
  55. $query = $db->query("SELECT tid FROM {$tablepre}threads WHERE fid='$fid' AND displayorder>=0 $sql ORDER BY displayorder DESC, lastpost DESC LIMIT 1000");
  56. $tids = $comma = '';
  57. $count = 0;
  58. while ($tid = $db->fetch_array($query)) {
  59. $tids .= $comma.$tid['tid'];
  60. $comma = ',';
  61. $count ++;
  62. }
  63. $result['tids'] = $tids;
  64. $result['count'] = $count;
  65. $result['fid'] = $fid;
  66. $modsession->set('srchresult', $result, true);
  67. $db->free_result($query);
  68. unset($result, $tids);
  69. $do = 'list';
  70. $page = 1;
  71. } else {
  72. $do = '';
  73. }
  74. }
  75. $page = max(1, intval($page));
  76. $total = 0;
  77. $query = $multipage = '';
  78. if(empty($do)) {
  79. $total = $db->result_first("SELECT count(*) FROM {$tablepre}threads WHERE fid='$fid' AND displayorder>=0");
  80. $tpage = ceil($total / $tpp);
  81. $page = min($tpage, $page);
  82. $multipage = multi($total, $tpp, $page, "$cpscript?action=$action&amp;op=$op&amp;fid=$fid&amp;do=$do");
  83. if($total) {
  84. $start = ($page - 1) * $tpp;
  85. $query = $db->query("SELECT * FROM {$tablepre}threads WHERE fid='$fid' AND displayorder>=0 ORDER BY displayorder DESC, lastpost DESC LIMIT $start, $tpp");
  86. }
  87. } else {
  88. $result = $modsession->get('srchresult');
  89. $threadoptionselect = array($result['threadoption'] => 'selected');
  90. if($result['fid'] == $fid) {
  91. $total = $result['count'];
  92. $tpage = ceil($total / $tpp);
  93. $page = min($tpage, $page);
  94. $multipage = multi($total, $tpp, $page, "$cpscript?action=$action&amp;op=$op&amp;fid=$fid&amp;do=$do");
  95. if($total) {
  96. $start = ($page - 1) * $tpp;
  97. $query = $db->query("SELECT * FROM {$tablepre}threads WHERE tid in($result[tids]) ORDER BY lastpost DESC LIMIT $start, $tpp");
  98. }
  99. }
  100. }
  101. $postlist = array();
  102. if($query) {
  103. require_once DISCUZ_ROOT.'./include/misc.func.php';
  104. while ($thread = $db->fetch_array($query)) {
  105. $postlist[] = procthread($thread);
  106. }
  107. }
  108. }
  109. return;
  110. }
  111. if($op == 'posts') {
  112. $error = 0;
  113. $result = array();
  114. $result['threadoption'] = intval($threadoption);
  115. $starttime = !preg_match("/^(0|\d{4}\-\d{1,2}\-\d{1,2})$/", $starttime) ? gmdate('Y-n-j', $timestamp + $timeoffset * 3600 - 86400 * ($adminid == 2 ? 13 : ($adminid == 3 ? 6 : 60))) : $starttime;
  116. $endtime = $adminid == 3 || !preg_match("/^(0|\d{4}\-\d{1,2}\-\d{1,2})$/", $endtime) ? gmdate('Y-n-j', $timestamp + $timeoffset * 3600) : $endtime;
  117. foreach (array('starttime', 'endtime', 'keywords', 'users', 'useip') as $key) {
  118. $$key = isset($$key) ? trim($$key) : '';
  119. $result[$key] = dhtmlspecialchars($$key);
  120. }
  121. $threadoptionselect = array($threadoption => 'selected');
  122. $fidadd = '';
  123. if($fid && $modforums['list'][$fid]) {
  124. $fidadd = "AND fid='$fid'";
  125. } else {
  126. if($adminid == 1 && $adminid == $groupid) {
  127. $fidadd = '';
  128. } elseif(!$modforums['fids']) {
  129. $fidadd = 'AND 0 ';
  130. } else {
  131. $fidadd = "AND fid in($modforums[fids])";
  132. }
  133. }
  134. if($do == 'delete' && submitcheck('deletesubmit')) {
  135. if(!$allowmassprune) {
  136. $error = 4;
  137. return;
  138. }
  139. $tidsdelete = $pidsdelete = '0';
  140. $prune = array();
  141. if($pids = implodeids($delete)) {
  142. $tidsdelete = $pidsdelete = '0';
  143. $query = $db->query("SELECT fid, tid, pid, first, authorid FROM {$tablepre}posts WHERE pid IN ($pids) $fidadd");
  144. while($post = $db->fetch_array($query)) {
  145. $prune['forums'][] = $post['fid'];
  146. $prune['thread'][$post['tid']]++;
  147. $pidsdelete .= ",$post[pid]";
  148. $tidsdelete .= $post['first'] ? ",$post[tid]" : '';
  149. }
  150. }
  151. if($pidsdelete) {
  152. require_once DISCUZ_ROOT.'./include/post.func.php';
  153. $query = $db->query("SELECT attachment, thumb, remote FROM {$tablepre}attachments WHERE pid IN ($pidsdelete) OR tid IN ($tidsdelete)");
  154. while($attach = $db->fetch_array($query)) {
  155. dunlink($attach['attachment'], $attach['thumb'], $attach['remote']);
  156. }
  157. if(!$nocredit) {
  158. $postsarray = $tuidarray = $ruidarray = array();
  159. $query1 = $db->query("SELECT pid, first, authorid FROM {$tablepre}posts WHERE pid IN ($pidsdelete)");
  160. $query2 = $db->query("SELECT pid, first, authorid FROM {$tablepre}posts WHERE tid IN ($tidsdelete)");
  161. while(($post = $db->fetch_array($query1)) || ($post = $db->fetch_array($query2))) {
  162. $postsarray[$post['pid']] = $post;
  163. }
  164. foreach($postsarray as $post) {
  165. if($post['first']) {
  166. $tuidarray[] = $post['authorid'];
  167. } else {
  168. $ruidarray[] = $post['authorid'];
  169. }
  170. }
  171. if($tuidarray) {
  172. updatepostcredits('-', $tuidarray, $creditspolicy['post']);
  173. }
  174. if($ruidarray) {
  175. updatepostcredits('-', $ruidarray, $creditspolicy['reply']);
  176. }
  177. }
  178. $db->query("DELETE FROM {$tablepre}attachments WHERE pid IN ($pidsdelete)", 'UNBUFFERED');
  179. $db->query("DELETE FROM {$tablepre}attachmentfields WHERE pid IN ($pidsdelete)", 'UNBUFFERED');
  180. $db->query("DELETE FROM {$tablepre}attachments WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  181. $db->query("DELETE FROM {$tablepre}attachmentfields WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  182. $db->query("DELETE FROM {$tablepre}threadsmod WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  183. $db->query("DELETE FROM {$tablepre}threadsmod WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  184. $db->query("DELETE FROM {$tablepre}threads WHERE tid IN ($tidsdelete)");
  185. $deletedthreads = $db->affected_rows();
  186. $db->query("DELETE FROM {$tablepre}posts WHERE pid IN ($pidsdelete)");
  187. $deletedposts = $db->affected_rows();
  188. $db->query("DELETE FROM {$tablepre}posts WHERE tid IN ($tidsdelete)");
  189. $deletedposts += $db->affected_rows();
  190. $db->query("DELETE FROM {$tablepre}polloptions WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  191. $db->query("DELETE FROM {$tablepre}polls WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  192. $db->query("DELETE FROM {$tablepre}rewardlog WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  193. $db->query("DELETE FROM {$tablepre}trades WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  194. $db->query("DELETE FROM {$tablepre}rewardlog WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  195. $db->query("DELETE FROM {$tablepre}activities WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  196. $db->query("DELETE FROM {$tablepre}activityapplies WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  197. $db->query("DELETE FROM {$tablepre}typeoptionvars WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  198. if(count($prunt['thread']) < 50) {
  199. foreach($prune['thread'] as $tid => $decrease) {
  200. updatethreadcount($tid);
  201. }
  202. } else {
  203. $repliesarray = array();
  204. foreach($prune['thread'] as $tid => $decrease) {
  205. $repliesarray[$decrease][] = $tid;
  206. }
  207. foreach($repliesarray as $decrease => $tidarray) {
  208. $db->query("UPDATE {$tablepre}threads SET replies=replies-$decrease WHERE tid IN (".implode(',', $tidarray).")");
  209. }
  210. }
  211. foreach(array_unique($prune['forums']) as $id) {
  212. updateforumcount($id);
  213. }
  214. }
  215. $do = 'list';
  216. }
  217. if($do == 'search' && submitcheck('searchsubmit', 1)) {
  218. if(($starttime == '0' && $endtime == '0') || ($keywords == '' && $useip == '' && $users == '')) {
  219. $error = 1;
  220. return ;
  221. }
  222. $sql = '';
  223. if($threadoption == 1) {
  224. $sql .= " AND first='1'";
  225. } elseif($threadoption == 2) {
  226. $sql .= " AND first='0'";
  227. }
  228. if($starttime != '0') {
  229. $starttime = strtotime($starttime);
  230. $sql .= " AND dateline>'$starttime'";
  231. }
  232. if($adminid == 1 && $endtime != gmdate('Y-n-j', $timestamp + $timeoffset * 3600)) {
  233. if($endtime != '0') {
  234. $endtime = strtotime($endtime);
  235. $sql .= " AND dateline<'$endtime'";
  236. }
  237. } else {
  238. $endtime = $timestamp;
  239. }
  240. if(($adminid == 2 && $endtime - $starttime > 86400 * 14) || ($adminid == 3 && $endtime - $starttime > 86400 * 7)) {
  241. $error = '2';
  242. return;
  243. }
  244. if($users != '') {
  245. $uids = $comma = '';
  246. $query = $db->query("SELECT uid FROM {$tablepre}members WHERE username IN ('".str_replace(',', '\',\'', str_replace(' ', '', $users))."')");
  247. while($member = $db->fetch_array($query)) {
  248. $uids .= $comma.$member[uid]; $comma = ',';
  249. }
  250. if($uids) {
  251. $sql .= " AND authorid IN ($uids)";
  252. }
  253. }
  254. if(trim($keywords)) {
  255. $sqlkeywords = '';
  256. $or = '';
  257. $keywords = explode(',', str_replace(' ', '', $keywords));
  258. for($i = 0; $i < count($keywords); $i++) {
  259. if(strlen($keywords[$i]) > 3) {
  260. $sqlkeywords .= " $or message LIKE '%".$keywords[$i]."%'";
  261. $or = 'OR';
  262. } else {
  263. $error = 3;
  264. return ;
  265. }
  266. }
  267. $sql .= " AND ($sqlkeywords)";
  268. }
  269. $useip = trim($useip);
  270. if($useip != '') {
  271. $sql .= " AND useip LIKE '".str_replace('*', '%', $useip)."'";
  272. }
  273. if($sql) {
  274. $query = $db->query("SELECT pid FROM {$tablepre}posts WHERE 1 $fidadd $sql ORDER BY dateline DESC LIMIT 1000");
  275. $pids = $comma = '';
  276. $count = 0;
  277. while ($pid = $db->fetch_array($query)) {
  278. $pids .= $comma.$pid['pid'];
  279. $comma = ',';
  280. $count ++;
  281. }
  282. $result['pids'] = $pids;
  283. $result['count'] = $count;
  284. $result['fid'] = $fid;
  285. $modsession->set('srchresult_p'.$fid, $result, true);
  286. $db->free_result($query);
  287. unset($result, $pids);
  288. $do = 'list';
  289. $page = 1;
  290. } else {
  291. $do = '';
  292. }
  293. }
  294. $page = max(1, intval($page));
  295. $total = 0;
  296. $query = $multipage = '';
  297. if($do == 'list') {
  298. $result = $modsession->get('srchresult_p'.$fid);
  299. $threadoptionselect = array($result['threadoption'] => 'selected');
  300. if($result['fid'] == $fid) {
  301. $total = $result['count'];
  302. $tpage = ceil($total / $tpp);
  303. $page = min($tpage, $page);
  304. $multipage = multi($total, $tpp, $page, "$cpscript?action=$action&amp;op=$op&amp;fid=$fid&amp;do=$do");
  305. if($total && $result[pids]) {
  306. $start = ($page - 1) * $tpp;
  307. $query = $db->query("SELECT p.*, t.subject as tsubject FROM {$tablepre}posts p LEFT JOIN {$tablepre}threads t USING(tid) WHERE pid in($result[pids]) ORDER BY dateline DESC LIMIT $start, $tpp");
  308. }
  309. }
  310. }
  311. $postlist = array();
  312. if($query) {
  313. require_once DISCUZ_ROOT.'./include/post.func.php';
  314. while ($post = $db->fetch_array($query)) {
  315. $post['dateline'] = gmdate("$dateformat $timeformat", $post['dateline'] + $timeoffset * 3600);
  316. $post['message'] = messagecutstr($post['message'], 200);
  317. $post['forum'] = $modforums['list'][$post[fid]];
  318. $post['modthreadkey'] = modthreadkey($post['tid']);
  319. $postlist[] = $post;
  320. }
  321. }
  322. }
  323. ?>