PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/modcp/threads.inc.php

https://github.com/AlexChien/EY_BBS
PHP | 399 lines | 315 code | 78 blank | 6 comment | 108 complexity | 04c9b4347097256e3fc287eb61a83b49 MD5 | raw file
Possible License(s): Apache-2.0
  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 16886 2008-11-26 05:08:30Z cnteacher $
  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 * 7) : $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}attachments WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  180. $db->query("DELETE FROM {$tablepre}threadsmod WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  181. $db->query("DELETE FROM {$tablepre}threadsmod WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  182. $db->query("DELETE FROM {$tablepre}threads WHERE tid IN ($tidsdelete)");
  183. $deletedthreads = $db->affected_rows();
  184. $db->query("DELETE FROM {$tablepre}posts WHERE pid IN ($pidsdelete)");
  185. $deletedposts = $db->affected_rows();
  186. $db->query("DELETE FROM {$tablepre}posts WHERE tid IN ($tidsdelete)");
  187. $deletedposts += $db->affected_rows();
  188. $db->query("DELETE FROM {$tablepre}polloptions WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  189. $db->query("DELETE FROM {$tablepre}polls WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  190. $db->query("DELETE FROM {$tablepre}rewardlog WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  191. $db->query("DELETE FROM {$tablepre}trades WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  192. $db->query("DELETE FROM {$tablepre}rewardlog WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  193. $db->query("DELETE FROM {$tablepre}activities WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  194. $db->query("DELETE FROM {$tablepre}activityapplies WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  195. $db->query("DELETE FROM {$tablepre}typeoptionvars WHERE tid IN ($tidsdelete)", 'UNBUFFERED');
  196. if(count($prunt['thread']) < 50) {
  197. foreach($prune['thread'] as $tid => $decrease) {
  198. updatethreadcount($tid);
  199. }
  200. } else {
  201. $repliesarray = array();
  202. foreach($prune['thread'] as $tid => $decrease) {
  203. $repliesarray[$decrease][] = $tid;
  204. }
  205. foreach($repliesarray as $decrease => $tidarray) {
  206. $db->query("UPDATE {$tablepre}threads SET replies=replies-$decrease WHERE tid IN (".implode(',', $tidarray).")");
  207. }
  208. }
  209. foreach(array_unique($prune['forums']) as $id) {
  210. updateforumcount($id);
  211. }
  212. }
  213. $do = 'list';
  214. }
  215. if($do == 'search' && submitcheck('searchsubmit')) {
  216. if(($starttime == '0' && $endtime == '0') || ($keywords == '' && $useip == '' && $users == '')) {
  217. $error = 1;
  218. return ;
  219. }
  220. $sql = '';
  221. if($threadoption == 1) {
  222. $sql .= " AND first='1'";
  223. } elseif($threadoption == 2) {
  224. $sql .= " AND first='0'";
  225. }
  226. if($starttime != '0') {
  227. $starttime = strtotime($starttime);
  228. $sql .= " AND dateline>'$starttime'";
  229. }
  230. if($adminid == 1 && $endtime != gmdate('Y-n-j', $timestamp + $timeoffset * 3600)) {
  231. if($endtime != '0') {
  232. $endtime = strtotime($endtime);
  233. $sql .= " AND dateline<'$endtime'";
  234. }
  235. } else {
  236. $endtime = $timestamp;
  237. }
  238. if(($adminid == 2 && $endtime - $starttime > 86400 * 16) || ($adminid == 3 && $endtime - $starttime > 86400 * 8)) {
  239. $error = '2';
  240. return;
  241. }
  242. if($users != '') {
  243. $uids = $comma = '';
  244. $query = $db->query("SELECT uid FROM {$tablepre}members WHERE username IN ('".str_replace(',', '\',\'', str_replace(' ', '', $users))."')");
  245. while($member = $db->fetch_array($query)) {
  246. $uids .= $comma.$member[uid]; $comma = ',';
  247. }
  248. if($uids) {
  249. $sql .= " AND authorid IN ($uids)";
  250. }
  251. }
  252. if(trim($keywords)) {
  253. $sqlkeywords = '';
  254. $or = '';
  255. $keywords = explode(',', str_replace(' ', '', $keywords));
  256. for($i = 0; $i < count($keywords); $i++) {
  257. if(strlen($keywords[$i]) > 3) {
  258. $sqlkeywords .= " $or message LIKE '%".$keywords[$i]."%'";
  259. $or = 'OR';
  260. } else {
  261. $error = 3;
  262. return ;
  263. }
  264. }
  265. $sql .= " AND ($sqlkeywords)";
  266. }
  267. $useip = trim($useip);
  268. if($useip != '') {
  269. $sql .= " AND useip LIKE '".str_replace('*', '%', $useip)."'";
  270. }
  271. if($sql) {
  272. $query = $db->query("SELECT pid FROM {$tablepre}posts WHERE 1 $fidadd $sql ORDER BY dateline DESC LIMIT 1000");
  273. $pids = $comma = '';
  274. $count = 0;
  275. while ($pid = $db->fetch_array($query)) {
  276. $pids .= $comma.$pid['pid'];
  277. $comma = ',';
  278. $count ++;
  279. }
  280. $result['pids'] = $pids;
  281. $result['count'] = $count;
  282. $result['fid'] = $fid;
  283. $modsession->set('srchresult_p'.$fid, $result, true);
  284. $db->free_result($query);
  285. unset($result, $pids);
  286. $do = 'list';
  287. $page = 1;
  288. } else {
  289. $do = '';
  290. }
  291. }
  292. $page = max(1, intval($page));
  293. $total = 0;
  294. $query = $multipage = '';
  295. if($do == 'list') {
  296. $result = $modsession->get('srchresult_p'.$fid);
  297. $threadoptionselect = array($result['threadoption'] => 'selected');
  298. if($result['fid'] == $fid) {
  299. $total = $result['count'];
  300. $tpage = ceil($total / $tpp);
  301. $page = min($tpage, $page);
  302. $multipage = multi($total, $tpp, $page, "$cpscript?action=$action&amp;op=$op&amp;fid=$fid&amp;do=$do");
  303. if($total && $result[pids]) {
  304. $start = ($page - 1) * $tpp;
  305. $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");
  306. }
  307. }
  308. }
  309. $postlist = array();
  310. if($query) {
  311. while ($post = $db->fetch_array($query)) {
  312. $post['dateline'] = gmdate("$dateformat $timeformat", $post['dateline'] + $timeoffset * 3600);
  313. $post['message'] = dhtmlspecialchars(cutstr($post['message'], 200), 200);
  314. $post['forum'] = $modforums['list'][$post[fid]];
  315. $post['modthreadkey'] = modthreadkey($post['tid']);
  316. $postlist[] = $post;
  317. }
  318. }
  319. }
  320. ?>