PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/forum/includes/functions_admin.php

https://code.google.com/p/torrentpier/
PHP | 945 lines | 754 code | 148 blank | 43 comment | 66 complexity | 9dd1d75ec21d01cbf43374ad5d8b5860 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. if (!defined('BB_ROOT')) die(basename(__FILE__));
  3. function sync ($type, $id)
  4. {
  5. global $db;
  6. switch ($type)
  7. {
  8. case 'forum':
  9. $all_forums = ($id === 'all');
  10. if (!$all_forums AND !$forum_csv = get_id_csv($id))
  11. {
  12. break;
  13. }
  14. $tmp_sync_forums = 'tmp_sync_forums';
  15. $db->query("
  16. CREATE TEMPORARY TABLE $tmp_sync_forums (
  17. forum_id SMALLINT UNSIGNED NOT NULL DEFAULT '0',
  18. forum_last_post_id MEDIUMINT UNSIGNED NOT NULL DEFAULT '0',
  19. forum_posts MEDIUMINT UNSIGNED NOT NULL DEFAULT '0',
  20. forum_topics MEDIUMINT UNSIGNED NOT NULL DEFAULT '0',
  21. PRIMARY KEY (forum_id)
  22. ) ENGINE = MEMORY
  23. ");
  24. $where_sql = (!$all_forums) ? "WHERE f.forum_id IN($forum_csv)" : '';
  25. $db->query("
  26. INSERT INTO $tmp_sync_forums
  27. SELECT
  28. f.forum_id,
  29. MAX(p.post_id),
  30. COUNT(p.post_id),
  31. COUNT(DISTINCT p.topic_id)
  32. FROM ". FORUMS_TABLE ." f
  33. LEFT JOIN ". POSTS_TABLE ." p USING(forum_id)
  34. $where_sql
  35. GROUP BY f.forum_id
  36. ");
  37. $db->query("
  38. UPDATE
  39. $tmp_sync_forums tmp, ". FORUMS_TABLE ." f
  40. SET
  41. f.forum_last_post_id = tmp.forum_last_post_id,
  42. f.forum_posts = tmp.forum_posts,
  43. f.forum_topics = tmp.forum_topics
  44. WHERE
  45. f.forum_id = tmp.forum_id
  46. ");
  47. $db->query("DROP TEMPORARY TABLE $tmp_sync_forums");
  48. break;
  49. case 'topic':
  50. $all_topics = ($id === 'all');
  51. if (!$all_topics AND !$topic_csv = get_id_csv($id))
  52. {
  53. break;
  54. }
  55. $tmp_sync_topics = 'tmp_sync_topics';
  56. $db->query("
  57. CREATE TEMPORARY TABLE $tmp_sync_topics (
  58. topic_id MEDIUMINT UNSIGNED NOT NULL DEFAULT '0',
  59. total_posts MEDIUMINT UNSIGNED NOT NULL DEFAULT '0',
  60. topic_first_post_id MEDIUMINT UNSIGNED NOT NULL DEFAULT '0',
  61. topic_last_post_id MEDIUMINT UNSIGNED NOT NULL DEFAULT '0',
  62. topic_last_post_time INT UNSIGNED NOT NULL DEFAULT '0',
  63. topic_attachment TINYINT UNSIGNED NOT NULL DEFAULT '0',
  64. PRIMARY KEY (topic_id)
  65. ) ENGINE = MEMORY
  66. ");
  67. $where_sql = (!$all_topics) ? "AND t.topic_id IN($topic_csv)" : '';
  68. $db->query("
  69. INSERT INTO $tmp_sync_topics
  70. SELECT
  71. t.topic_id,
  72. COUNT(p.post_id) AS total_posts,
  73. MIN(p.post_id) AS topic_first_post_id,
  74. MAX(p.post_id) AS topic_last_post_id,
  75. MAX(p.post_time) AS topic_last_post_time,
  76. IF(MAX(a.attach_id), 1, 0) AS topic_attachment
  77. FROM ". TOPICS_TABLE ." t
  78. LEFT JOIN ". POSTS_TABLE ." p ON(p.topic_id = t.topic_id)
  79. LEFT JOIN ". ATTACHMENTS_TABLE ." a ON(a.post_id = p.post_id)
  80. WHERE t.topic_status != ". TOPIC_MOVED ."
  81. $where_sql
  82. GROUP BY t.topic_id
  83. ");
  84. $db->query("
  85. UPDATE
  86. $tmp_sync_topics tmp, ". TOPICS_TABLE ." t
  87. SET
  88. t.topic_replies = tmp.total_posts - 1,
  89. t.topic_first_post_id = tmp.topic_first_post_id,
  90. t.topic_last_post_id = tmp.topic_last_post_id,
  91. t.topic_last_post_time = tmp.topic_last_post_time,
  92. t.topic_attachment = tmp.topic_attachment
  93. WHERE
  94. t.topic_id = tmp.topic_id
  95. ");
  96. $sql = "SELECT topic_id FROM ". $tmp_sync_topics ." WHERE total_posts = 0";
  97. if ($rowset = $db->fetch_rowset($sql))
  98. {
  99. $topics = array();
  100. foreach ($rowset as $row)
  101. {
  102. $topics[] = $row['topic_id'];
  103. }
  104. topic_delete($topics);
  105. }
  106. $db->query("DROP TEMPORARY TABLE $tmp_sync_topics");
  107. break;
  108. case 'user_posts':
  109. $all_users = ($id === 'all');
  110. if (!$all_users AND !$user_csv = get_id_csv($id))
  111. {
  112. break;
  113. }
  114. $tmp_user_posts = 'tmp_sync_user_posts';
  115. $db->query("
  116. CREATE TEMPORARY TABLE $tmp_user_posts (
  117. user_id MEDIUMINT NOT NULL DEFAULT '0',
  118. user_posts MEDIUMINT UNSIGNED NOT NULL DEFAULT '0',
  119. PRIMARY KEY (user_id)
  120. ) ENGINE = MEMORY
  121. ");
  122. // Set posts count = 0 and then update to real count
  123. $where_user_sql = (!$all_users) ? "AND user_id IN($user_csv)" : "AND user_posts != 0";
  124. $where_post_sql = (!$all_users) ? "AND poster_id IN($user_csv)" : '';
  125. $db->query("
  126. REPLACE INTO $tmp_user_posts
  127. SELECT user_id, 0
  128. FROM ". USERS_TABLE ."
  129. WHERE user_id != ". ANONYMOUS ."
  130. $where_user_sql
  131. UNION
  132. SELECT poster_id, COUNT(*)
  133. FROM ". POSTS_TABLE ."
  134. WHERE poster_id != ". ANONYMOUS ."
  135. $where_post_sql
  136. GROUP BY poster_id
  137. ");
  138. $db->query("
  139. UPDATE
  140. $tmp_user_posts tmp, ". USERS_TABLE ." u
  141. SET
  142. u.user_posts = tmp.user_posts
  143. WHERE
  144. u.user_id = tmp.user_id
  145. ");
  146. $db->query("DROP TEMPORARY TABLE $tmp_user_posts");
  147. break;
  148. }
  149. }
  150. function topic_delete ($mode_or_topic_id, $forum_id = null, $prune_time = 0, $prune_all = false)
  151. {
  152. global $db, $lang, $bb_cfg, $log_action;
  153. $prune = ($mode_or_topic_id === 'prune');
  154. if (!$prune AND !$topic_csv = get_id_csv($mode_or_topic_id))
  155. {
  156. return false;
  157. }
  158. $log_topics = $sync_forums = array();
  159. if ($prune)
  160. {
  161. $sync_forums[$forum_id] = true;
  162. }
  163. else
  164. {
  165. $where_sql = ($forum_csv = get_id_csv($forum_id)) ? "AND forum_id IN($forum_csv)" : '';
  166. $sql = "
  167. SELECT topic_id, forum_id, topic_title, topic_status
  168. FROM ". TOPICS_TABLE ."
  169. WHERE topic_id IN($topic_csv)
  170. $where_sql
  171. ";
  172. $topic_csv = array();
  173. foreach ($db->fetch_rowset($sql) as $row)
  174. {
  175. $topic_csv[] = $row['topic_id'];
  176. $log_topics[] = $row;
  177. $sync_forums[$row['forum_id']] = true;
  178. }
  179. if (!$topic_csv = get_id_csv($topic_csv))
  180. {
  181. return false;
  182. }
  183. }
  184. // Get topics to delete
  185. $tmp_delete_topics = 'tmp_delete_topics';
  186. $db->query("
  187. CREATE TEMPORARY TABLE $tmp_delete_topics (
  188. topic_id MEDIUMINT UNSIGNED NOT NULL DEFAULT '0',
  189. PRIMARY KEY (topic_id)
  190. ) ENGINE = MEMORY
  191. ");
  192. $where_sql = ($prune) ? "forum_id = $forum_id" : "topic_id IN($topic_csv)";
  193. $where_sql .= ($prune && $prune_time) ? " AND topic_last_post_time < $prune_time" : '';
  194. $where_sql .= ($prune && !$prune_all) ? " AND topic_type NOT IN(". POST_ANNOUNCE .",". POST_STICKY .")": '';
  195. $db->query("
  196. INSERT INTO $tmp_delete_topics
  197. SELECT topic_id
  198. FROM ". TOPICS_TABLE ."
  199. WHERE $where_sql
  200. ");
  201. // Get topics count
  202. $row = $db->fetch_row("SELECT COUNT(*) AS topics_count FROM $tmp_delete_topics");
  203. if (!$deleted_topics_count = $row['topics_count'])
  204. {
  205. $db->query("DROP TEMPORARY TABLE $tmp_delete_topics");
  206. return 0;
  207. }
  208. // Update user posts count
  209. $tmp_user_posts = 'tmp_user_posts';
  210. $db->query("
  211. CREATE TEMPORARY TABLE $tmp_user_posts (
  212. user_id MEDIUMINT NOT NULL DEFAULT '0',
  213. user_posts MEDIUMINT UNSIGNED NOT NULL DEFAULT '0',
  214. PRIMARY KEY (user_id)
  215. ) ENGINE = MEMORY
  216. ");
  217. $db->query("
  218. INSERT INTO $tmp_user_posts
  219. SELECT p.poster_id, COUNT(p.post_id)
  220. FROM ". $tmp_delete_topics ." del, ". POSTS_TABLE ." p
  221. WHERE p.topic_id = del.topic_id
  222. AND p.poster_id != ". ANONYMOUS ."
  223. GROUP BY p.poster_id
  224. ");
  225. $db->query("
  226. UPDATE
  227. $tmp_user_posts tmp, ". USERS_TABLE ." u
  228. SET
  229. u.user_posts = u.user_posts - tmp.user_posts
  230. WHERE
  231. u.user_id = tmp.user_id
  232. ");
  233. $db->query("DROP TEMPORARY TABLE $tmp_user_posts");
  234. // Delete votes
  235. $db->query("
  236. DELETE vd, vr, vu
  237. FROM ". $tmp_delete_topics ." del
  238. LEFT JOIN ". VOTE_DESC_TABLE ." vd USING(topic_id)
  239. LEFT JOIN ". VOTE_RESULTS_TABLE ." vr USING(vote_id)
  240. LEFT JOIN ". VOTE_USERS_TABLE ." vu USING(vote_id)
  241. ");
  242. if ($bb_cfg['auto_delete_posted_pics'])
  243. {
  244. $result = $db->sql_query("
  245. SELECT ph.post_id, ph.post_html
  246. FROM $tmp_delete_topics tmp
  247. LEFT JOIN ". POSTS_TABLE ." p USING(topic_id)
  248. LEFT JOIN ". POSTS_HTML_TABLE ." ph ON(p.post_id = ph.post_id)
  249. ");
  250. while ( $post = $db->sql_fetchrow($result) )
  251. {
  252. preg_match_all('#<var.*?title="(.*?)"#', $post['post_html'], $matches, PREG_SET_ORDER);
  253. foreach($matches as $match)
  254. {
  255. $have = $db->fetch_row("
  256. SELECT post_id
  257. FROM ". POSTS_HTML_TABLE ."
  258. WHERE post_html LIKE '%". $db->escape($match[1]). "%'
  259. AND post_id != {$post['post_id']}
  260. ");
  261. if(empty($have))
  262. {
  263. @unlink(BB_ROOT . $bb_cfg['pic_dir'] . end(explode('/', $match[1])));
  264. }
  265. }
  266. }
  267. }
  268. // Delete attachments (from disk)
  269. $attach_dir = get_attachments_dir();
  270. $result = $db->query("
  271. SELECT
  272. d.physical_filename
  273. FROM
  274. ". $tmp_delete_topics ." del,
  275. ". POSTS_TABLE ." p,
  276. ". ATTACHMENTS_TABLE ." a,
  277. ". ATTACHMENTS_DESC_TABLE ." d
  278. WHERE
  279. p.topic_id = del.topic_id
  280. AND a.post_id = p.post_id
  281. AND d.attach_id = a.attach_id
  282. ");
  283. while ($row = $db->fetch_next($result))
  284. {
  285. if ($filename = basename($row['physical_filename']))
  286. {
  287. @unlink("$attach_dir/". $filename);
  288. @unlink("$attach_dir/". THUMB_DIR .'/t_'. $filename);
  289. }
  290. }
  291. unset($row, $result);
  292. // Delete posts, posts_text, attachments (from DB)
  293. $db->query("
  294. DELETE p, pt, ps, a, d
  295. FROM ". $tmp_delete_topics ." del
  296. LEFT JOIN ". POSTS_TABLE ." p ON(p.topic_id = del.topic_id)
  297. LEFT JOIN ". POSTS_TEXT_TABLE ." pt ON(pt.post_id = p.post_id)
  298. LEFT JOIN ". POSTS_SEARCH_TABLE ." ps ON(ps.post_id = p.post_id)
  299. LEFT JOIN ". ATTACHMENTS_TABLE ." a ON(a.post_id = p.post_id)
  300. LEFT JOIN ". ATTACHMENTS_DESC_TABLE ." d ON(d.attach_id = a.attach_id)
  301. ");
  302. // Delete topics, topics watch
  303. $db->query("
  304. DELETE t, tw
  305. FROM ". $tmp_delete_topics ." del
  306. LEFT JOIN ". TOPICS_TABLE ." t USING(topic_id)
  307. LEFT JOIN ". TOPICS_WATCH_TABLE ." tw USING(topic_id)
  308. ");
  309. // Delete topic moved stubs
  310. $db->query("
  311. DELETE t
  312. FROM ". $tmp_delete_topics ." del, ". TOPICS_TABLE ." t
  313. WHERE t.topic_moved_id = del.topic_id
  314. ");
  315. // Delete torrents
  316. $db->query("
  317. DELETE tor, tr
  318. FROM ". $tmp_delete_topics ." del
  319. LEFT JOIN ". BT_TORRENTS_TABLE ." tor USING(topic_id)
  320. LEFT JOIN ". BT_TRACKER_TABLE ." tr USING(topic_id)
  321. ");
  322. /*
  323. // Delete dlstat
  324. $db->query("
  325. DELETE dl
  326. FROM ". $tmp_delete_topics ." del
  327. LEFT JOIN ". BT_DLSTATUS_TABLE ." dl USING(topic_id)
  328. ");
  329. */
  330. // Log action
  331. if ($prune)
  332. {
  333. // TODO
  334. }
  335. else
  336. {
  337. foreach ($log_topics as $row)
  338. {
  339. if ($row['topic_status'] == TOPIC_MOVED)
  340. {
  341. $row['topic_title'] = '<i>'. $lang['TOPIC_MOVED'] .'</i> '. $row['topic_title'];
  342. }
  343. $log_action->mod('mod_topic_delete', array(
  344. 'forum_id' => $row['forum_id'],
  345. 'topic_id' => $row['topic_id'],
  346. 'topic_title' => $row['topic_title'],
  347. ));
  348. }
  349. }
  350. // Sync
  351. sync('forum', array_keys($sync_forums));
  352. $db->query("DROP TEMPORARY TABLE $tmp_delete_topics");
  353. return $deleted_topics_count;
  354. }
  355. function topic_move ($topic_id, $to_forum_id, $from_forum_id = null, $leave_shadow = false, $insert_bot_msg = false)
  356. {
  357. global $db, $log_action;
  358. $to_forum_id = (int) $to_forum_id;
  359. // Verify input params
  360. if (!$topic_csv = get_id_csv($topic_id))
  361. {
  362. return false;
  363. }
  364. if (!forum_exists($to_forum_id))
  365. {
  366. return false;
  367. }
  368. if ($from_forum_id && (!forum_exists($from_forum_id) || $to_forum_id == $from_forum_id))
  369. {
  370. return false;
  371. }
  372. // Get topics info
  373. $where_sql = ($forum_csv = get_id_csv($from_forum_id)) ? "AND forum_id IN($forum_csv)" : '';
  374. $sql = "
  375. SELECT *
  376. FROM ". TOPICS_TABLE ."
  377. WHERE topic_id IN($topic_csv)
  378. AND topic_status != ". TOPIC_MOVED ."
  379. $where_sql
  380. ";
  381. $topics = array();
  382. $sync_forums = array($to_forum_id => true);
  383. foreach ($db->fetch_rowset($sql) as $row)
  384. {
  385. if ($row['forum_id'] != $to_forum_id)
  386. {
  387. $topics[$row['topic_id']] = $row;
  388. $sync_forums[$row['forum_id']] = true;
  389. }
  390. }
  391. if (!$topics OR !$topic_csv = get_id_csv(array_keys($topics)))
  392. {
  393. return false;
  394. }
  395. // Insert topic in the old forum that indicates that the topic has moved
  396. if ($leave_shadow)
  397. {
  398. $shadows = array();
  399. foreach ($topics as $topic_id => $row)
  400. {
  401. $shadows[] = array(
  402. 'forum_id' => $row['forum_id'],
  403. 'topic_title' => $row['topic_title'],
  404. 'topic_poster' => $row['topic_poster'],
  405. 'topic_time' => TIMENOW,
  406. 'topic_status' => TOPIC_MOVED,
  407. 'topic_type' => POST_NORMAL,
  408. 'topic_vote' => $row['topic_vote'],
  409. 'topic_views' => $row['topic_views'],
  410. 'topic_replies' => $row['topic_replies'],
  411. 'topic_first_post_id' => $row['topic_first_post_id'],
  412. 'topic_last_post_id' => $row['topic_last_post_id'],
  413. 'topic_moved_id' => $topic_id,
  414. 'topic_last_post_time' => $row['topic_last_post_time'],
  415. );
  416. }
  417. if ($sql_args = $db->build_array('MULTI_INSERT', $shadows))
  418. {
  419. $db->query("INSERT INTO ". TOPICS_TABLE . $sql_args);
  420. }
  421. }
  422. // Update topics
  423. $db->query("
  424. UPDATE ". TOPICS_TABLE ." SET
  425. forum_id = $to_forum_id
  426. WHERE topic_id IN($topic_csv)
  427. ");
  428. // Update posts
  429. $db->query("
  430. UPDATE ". POSTS_TABLE ." SET
  431. forum_id = $to_forum_id
  432. WHERE topic_id IN($topic_csv)
  433. ");
  434. // Update torrents
  435. $db->query("
  436. UPDATE ". BT_TORRENTS_TABLE ." SET
  437. forum_id = $to_forum_id
  438. WHERE topic_id IN($topic_csv)
  439. ");
  440. // Bot
  441. if ($insert_bot_msg)
  442. {
  443. foreach ($topics as $topic_id => $row)
  444. {
  445. insert_post('after_move', $topic_id, $to_forum_id, $row['forum_id']);
  446. }
  447. sync('topic', array_keys($topics));
  448. }
  449. // Sync
  450. sync('forum', array_keys($sync_forums));
  451. // Log action
  452. foreach ($topics as $topic_id => $row)
  453. {
  454. $log_action->mod('mod_topic_move', array(
  455. 'forum_id' => $row['forum_id'],
  456. 'forum_id_new' => $to_forum_id,
  457. 'topic_id' => $topic_id,
  458. 'topic_title' => $row['topic_title'],
  459. ));
  460. }
  461. return true;
  462. }
  463. function post_delete ($mode_or_post_id, $user_id = null, $exclude_first = true)
  464. {
  465. global $db, $bb_cfg, $log_action;
  466. $del_user_posts = ($mode_or_post_id === 'user'); // Delete all user posts
  467. // Get required params
  468. if ($del_user_posts)
  469. {
  470. if (!$user_csv = get_id_csv($user_id)) return false;
  471. }
  472. else
  473. {
  474. if (!$post_csv = get_id_csv($mode_or_post_id)) return false;
  475. // exclude first post of topic
  476. if ($exclude_first)
  477. {
  478. $sql = "SELECT topic_first_post_id FROM ". TOPICS_TABLE ." WHERE topic_first_post_id IN($post_csv)";
  479. if ($first_posts = $db->fetch_rowset($sql, 'topic_first_post_id'))
  480. {
  481. $posts_without_first = array_diff(explode(',', $post_csv), $first_posts);
  482. if (!$post_csv = get_id_csv($posts_without_first))
  483. {
  484. return false;
  485. }
  486. }
  487. }
  488. }
  489. // Collect data for logs, sync..
  490. $log_topics = $sync_forums = $sync_topics = $sync_users = array();
  491. if ($del_user_posts)
  492. {
  493. $sql = "SELECT DISTINCT topic_id FROM ". POSTS_TABLE ." WHERE poster_id IN($user_csv)";
  494. foreach ($db->fetch_rowset($sql) as $row)
  495. {
  496. $sync_topics[] = $row['topic_id'];
  497. }
  498. if ($topic_csv = get_id_csv($sync_topics))
  499. {
  500. $sql = "SELECT DISTINCT forum_id FROM ". TOPICS_TABLE ." WHERE topic_id IN($topic_csv)";
  501. foreach ($db->fetch_rowset($sql) as $row)
  502. {
  503. $sync_forums[$row['forum_id']] = true;
  504. }
  505. }
  506. $sync_users = explode(',', $user_csv);
  507. }
  508. else
  509. {
  510. $sql = "
  511. SELECT p.topic_id, p.forum_id, t.topic_title
  512. FROM ". POSTS_TABLE ." p, ". TOPICS_TABLE ." t
  513. WHERE p.post_id IN($post_csv)
  514. AND t.topic_id = p.topic_id
  515. GROUP BY t.topic_id
  516. ";
  517. foreach ($db->fetch_rowset($sql) as $row)
  518. {
  519. $log_topics[] = $row;
  520. $sync_topics[] = $row['topic_id'];
  521. $sync_forums[$row['forum_id']] = true;
  522. }
  523. $sql = "SELECT DISTINCT poster_id FROM ". POSTS_TABLE ." WHERE post_id IN($post_csv)";
  524. foreach ($db->fetch_rowset($sql) as $row)
  525. {
  526. $sync_users[] = $row['poster_id'];
  527. }
  528. }
  529. // Get all post_id for deleting
  530. $tmp_delete_posts = 'tmp_delete_posts';
  531. $db->query("
  532. CREATE TEMPORARY TABLE $tmp_delete_posts (
  533. post_id MEDIUMINT UNSIGNED NOT NULL DEFAULT '0',
  534. PRIMARY KEY (post_id)
  535. ) ENGINE = MEMORY
  536. ");
  537. $db->add_shutdown_query("DROP TEMPORARY TABLE IF EXISTS $tmp_delete_posts");
  538. if ($del_user_posts)
  539. {
  540. $where_sql = "poster_id IN($user_csv)";
  541. $exclude_posts_ary = array();
  542. foreach ($db->fetch_rowset("SELECT topic_first_post_id FROM ". TOPICS_TABLE ." WHERE topic_poster IN($user_csv)") as $row)
  543. {
  544. $exclude_posts_ary[] = $row['topic_first_post_id'];
  545. }
  546. if ($exclude_posts_csv = get_id_csv($exclude_posts_ary))
  547. {
  548. $where_sql .= " AND post_id NOT IN($exclude_posts_csv)";
  549. }
  550. }
  551. else
  552. {
  553. $where_sql = "post_id IN($post_csv)";
  554. }
  555. $db->query("
  556. INSERT INTO $tmp_delete_posts
  557. SELECT post_id
  558. FROM ". POSTS_TABLE ."
  559. WHERE $where_sql
  560. ");
  561. // Deleted posts count
  562. $row = $db->fetch_row("SELECT COUNT(*) AS posts_count FROM $tmp_delete_posts");
  563. if (!$deleted_posts_count = $row['posts_count'])
  564. {
  565. $db->query("DROP TEMPORARY TABLE $tmp_delete_posts");
  566. return 0;
  567. }
  568. if ($bb_cfg['auto_delete_posted_pics'])
  569. {
  570. $result = $db->sql_query("
  571. SELECT ph.post_id, ph.post_html
  572. FROM $tmp_delete_posts tmp
  573. LEFT JOIN ". POSTS_HTML_TABLE ." ph USING(post_id)
  574. ");
  575. while ( $post = $db->sql_fetchrow($result) )
  576. {
  577. preg_match_all('#<var.*?title="(.*?)"#', $post['post_html'], $matches, PREG_SET_ORDER);
  578. foreach($matches as $match)
  579. {
  580. $have = $db->fetch_row("
  581. SELECT post_id
  582. FROM ". POSTS_HTML_TABLE ."
  583. WHERE post_html LIKE '%". $db->escape($match[1]). "%'
  584. AND post_id != {$post['post_id']}
  585. ");
  586. if(empty($have))
  587. {
  588. @unlink(BB_ROOT . $bb_cfg['pic_dir']. end(explode('/', $match[1])));
  589. }
  590. }
  591. }
  592. }
  593. // Delete attachments (from disk)
  594. $attach_dir = get_attachments_dir();
  595. $result = $db->query("
  596. SELECT
  597. d.physical_filename
  598. FROM
  599. ". $tmp_delete_posts ." del,
  600. ". ATTACHMENTS_TABLE ." a,
  601. ". ATTACHMENTS_DESC_TABLE ." d
  602. WHERE
  603. a.post_id = del.post_id
  604. AND d.attach_id = a.attach_id
  605. ");
  606. while ($row = $db->fetch_next($result))
  607. {
  608. if ($filename = basename($row['physical_filename']))
  609. {
  610. @unlink("$attach_dir/". $filename);
  611. @unlink("$attach_dir/". THUMB_DIR .'/t_'. $filename);
  612. }
  613. }
  614. unset($row, $result);
  615. // Delete posts, posts_text, attachments (from DB)
  616. $db->query("
  617. DELETE p, pt, ps, tor, a, d
  618. FROM ". $tmp_delete_posts ." del
  619. LEFT JOIN ". POSTS_TABLE ." p ON(p.post_id = del.post_id)
  620. LEFT JOIN ". POSTS_TEXT_TABLE ." pt ON(pt.post_id = del.post_id)
  621. LEFT JOIN ". POSTS_SEARCH_TABLE ." ps ON(ps.post_id = del.post_id)
  622. LEFT JOIN ". BT_TORRENTS_TABLE ." tor ON(tor.post_id = del.post_id)
  623. LEFT JOIN ". ATTACHMENTS_TABLE ." a ON(a.post_id = del.post_id)
  624. LEFT JOIN ". ATTACHMENTS_DESC_TABLE ." d ON(d.attach_id = a.attach_id)
  625. ");
  626. // Log action
  627. if ($del_user_posts)
  628. {
  629. $log_action->admin('mod_post_delete', array(
  630. 'log_msg' => 'user: '. get_usernames_for_log($user_id) ."<br />posts: $deleted_posts_count",
  631. ));
  632. }
  633. else if (!defined('IN_CRON'))
  634. {
  635. foreach ($log_topics as $row)
  636. {
  637. $log_action->mod('mod_post_delete', array(
  638. 'forum_id' => $row['forum_id'],
  639. 'topic_id' => $row['topic_id'],
  640. 'topic_title' => $row['topic_title'],
  641. ));
  642. }
  643. }
  644. // Sync
  645. sync('topic', $sync_topics);
  646. sync('forum', array_keys($sync_forums));
  647. sync('user_posts', $sync_users);
  648. $db->query("DROP TEMPORARY TABLE $tmp_delete_posts");
  649. return $deleted_posts_count;
  650. }
  651. function poll_delete ($topic_id)
  652. {
  653. global $db;
  654. if (!$topic_csv = get_id_csv($topic_id))
  655. {
  656. return false;
  657. }
  658. $db->query("
  659. DELETE vd, vr, vu
  660. FROM ". VOTE_DESC_TABLE ." vd
  661. LEFT JOIN ". VOTE_RESULTS_TABLE ." vr USING(vote_id)
  662. LEFT JOIN ". VOTE_USERS_TABLE ." vu USING(vote_id)
  663. WHERE vd.topic_id IN($topic_csv)
  664. ");
  665. $db->query("
  666. UPDATE ". TOPICS_TABLE ." SET topic_vote = 0 WHERE topic_id IN($topic_csv)
  667. ");
  668. }
  669. function user_delete ($user_id, $delete_posts = false)
  670. {
  671. global $db, $bb_cfg, $log_action;
  672. $default_group_moderator_id = 2;
  673. if (!$user_csv = get_id_csv($user_id))
  674. {
  675. return false;
  676. }
  677. // LOG
  678. $log_action->admin('adm_user_delete', array(
  679. 'log_msg' => get_usernames_for_log($user_id),
  680. ));
  681. // Avatar
  682. $result = $db->query("
  683. SELECT user_avatar
  684. FROM ". USERS_TABLE ."
  685. WHERE user_avatar_type = ". USER_AVATAR_UPLOAD ."
  686. AND user_avatar != ''
  687. AND user_id IN($user_csv)
  688. ");
  689. while ($row = $db->fetch_next($result))
  690. {
  691. if ($filename = basename($row['user_avatar']))
  692. {
  693. @unlink(BB_ROOT . $bb_cfg['avatar_path'] .'/'. $filename);
  694. }
  695. }
  696. unset($row, $result);
  697. // Group
  698. $db->query("
  699. UPDATE ". GROUPS_TABLE ." SET
  700. group_moderator = $default_group_moderator_id
  701. WHERE group_single_user = 0
  702. AND group_moderator IN($user_csv)
  703. ");
  704. if ($delete_posts)
  705. {
  706. post_delete('user', $user_id);
  707. }
  708. else
  709. {
  710. $db->query("
  711. UPDATE ". POSTS_TABLE ." p, ". USERS_TABLE ." u SET
  712. p.post_username = u.username,
  713. p.poster_id = ". DELETED ."
  714. WHERE u.user_id IN($user_csv)
  715. AND p.poster_id = u.user_id
  716. ");
  717. }
  718. $db->query("
  719. UPDATE ". TOPICS_TABLE ." SET
  720. topic_poster = ". DELETED ."
  721. WHERE topic_poster IN($user_csv)
  722. ");
  723. $db->query("
  724. UPDATE ". VOTE_USERS_TABLE ." SET
  725. vote_user_id = ". DELETED ."
  726. WHERE vote_user_id IN($user_csv)
  727. ");
  728. $db->query("
  729. UPDATE ". BT_TORRENTS_TABLE ." SET
  730. poster_id = ". DELETED ."
  731. WHERE poster_id IN($user_csv)
  732. ");
  733. $db->query("
  734. DELETE ug, g, a, qt1, qt2
  735. FROM ". USER_GROUP_TABLE ." ug
  736. LEFT JOIN ". GROUPS_TABLE ." g ON(g.group_id = ug.group_id AND g.group_single_user = 1)
  737. LEFT JOIN ". AUTH_ACCESS_TABLE ." a ON(a.group_id = g.group_id)
  738. LEFT JOIN ". QUOTA_TABLE ." qt1 ON(qt1.user_id = ug.user_id)
  739. LEFT JOIN ". QUOTA_TABLE ." qt2 ON(qt2.group_id = g.group_id)
  740. WHERE ug.user_id IN($user_csv)
  741. ");
  742. $db->query("
  743. DELETE u, ban, s, tw, asn
  744. FROM ". USERS_TABLE ." u
  745. LEFT JOIN ". BANLIST_TABLE ." ban ON(ban.ban_userid = u.user_id)
  746. LEFT JOIN ". SESSIONS_TABLE ." s ON(s.session_user_id = u.user_id)
  747. LEFT JOIN ". TOPICS_WATCH_TABLE ." tw ON(tw.user_id = u.user_id)
  748. LEFT JOIN ". AUTH_ACCESS_SNAP_TABLE ." asn ON(asn.user_id = u.user_id)
  749. WHERE u.user_id IN($user_csv)
  750. ");
  751. $db->query("
  752. DELETE btu, tr, ust
  753. FROM ". BT_USERS_TABLE ." btu
  754. LEFT JOIN ". BT_TRACKER_TABLE ." tr ON(tr.user_id = btu.user_id)
  755. LEFT JOIN ". BT_USER_SETTINGS_TABLE ." ust ON(ust.user_id = btu.user_id)
  756. WHERE btu.user_id IN($user_csv)
  757. ");
  758. // PM
  759. $db->query("
  760. DELETE pm, pmt
  761. FROM ". PRIVMSGS_TABLE ." pm
  762. LEFT JOIN ". PRIVMSGS_TEXT_TABLE ." pmt ON(pmt.privmsgs_text_id = pm.privmsgs_id)
  763. WHERE pm.privmsgs_from_userid IN($user_csv)
  764. AND pm.privmsgs_type IN(". PRIVMSGS_SENT_MAIL .','. PRIVMSGS_SAVED_OUT_MAIL .")
  765. ");
  766. $db->query("
  767. DELETE pm, pmt
  768. FROM ". PRIVMSGS_TABLE ." pm
  769. LEFT JOIN ". PRIVMSGS_TEXT_TABLE ." pmt ON(pmt.privmsgs_text_id = pm.privmsgs_id)
  770. WHERE pm.privmsgs_to_userid IN($user_csv)
  771. AND pm.privmsgs_type IN(". PRIVMSGS_READ_MAIL .','. PRIVMSGS_SAVED_IN_MAIL .")
  772. ");
  773. $db->query("
  774. UPDATE ". PRIVMSGS_TABLE ." SET
  775. privmsgs_from_userid = ". DELETED ."
  776. WHERE privmsgs_from_userid IN($user_csv)
  777. ");
  778. $db->query("
  779. UPDATE ". PRIVMSGS_TABLE ." SET
  780. privmsgs_to_userid = ". DELETED ."
  781. WHERE privmsgs_to_userid IN($user_csv)
  782. ");
  783. }
  784. function get_usernames_for_log ($user_id)
  785. {
  786. global $db;
  787. $users_log_msg = array();
  788. if ($user_csv = get_id_csv($user_id))
  789. {
  790. $sql = "SELECT user_id, username FROM ". USERS_TABLE ." WHERE user_id IN($user_csv)";
  791. foreach ($db->fetch_rowset($sql) as $row)
  792. {
  793. $users_log_msg[] = "<b>$row[username]</b> [$row[user_id]]";
  794. }
  795. }
  796. return join(', ', $users_log_msg);
  797. }