Subs-Boards.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. <?php
  2. /**
  3. * This file is mainly concerned with minor tasks relating to boards, such as
  4. * marking them read, collapsing categories, or quick moderation.
  5. *
  6. * Simple Machines Forum (SMF)
  7. *
  8. * @package SMF
  9. * @author Simple Machines http://www.simplemachines.org
  10. * @copyright 2012 Simple Machines
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version 2.1 Alpha 1
  14. */
  15. if (!defined('SMF'))
  16. die('Hacking attempt...');
  17. /**
  18. * Mark a board or multiple boards read.
  19. *
  20. * @param array $boards
  21. * @param bool $unread
  22. */
  23. function markBoardsRead($boards, $unread = false)
  24. {
  25. global $user_info, $modSettings, $smcFunc;
  26. // Force $boards to be an array.
  27. if (!is_array($boards))
  28. $boards = array($boards);
  29. else
  30. $boards = array_unique($boards);
  31. // No boards, nothing to mark as read.
  32. if (empty($boards))
  33. return;
  34. // Allow the user to mark a board as unread.
  35. if ($unread)
  36. {
  37. // Clear out all the places where this lovely info is stored.
  38. // @todo Maybe not log_mark_read?
  39. $smcFunc['db_query']('', '
  40. DELETE FROM {db_prefix}log_mark_read
  41. WHERE id_board IN ({array_int:board_list})
  42. AND id_member = {int:current_member}',
  43. array(
  44. 'current_member' => $user_info['id'],
  45. 'board_list' => $boards,
  46. )
  47. );
  48. $smcFunc['db_query']('', '
  49. DELETE FROM {db_prefix}log_boards
  50. WHERE id_board IN ({array_int:board_list})
  51. AND id_member = {int:current_member}',
  52. array(
  53. 'current_member' => $user_info['id'],
  54. 'board_list' => $boards,
  55. )
  56. );
  57. }
  58. // Otherwise mark the board as read.
  59. else
  60. {
  61. $markRead = array();
  62. foreach ($boards as $board)
  63. $markRead[] = array($modSettings['maxMsgID'], $user_info['id'], $board);
  64. // Update log_mark_read and log_boards.
  65. $smcFunc['db_insert']('replace',
  66. '{db_prefix}log_mark_read',
  67. array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'),
  68. $markRead,
  69. array('id_board', 'id_member')
  70. );
  71. $smcFunc['db_insert']('replace',
  72. '{db_prefix}log_boards',
  73. array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'),
  74. $markRead,
  75. array('id_board', 'id_member')
  76. );
  77. }
  78. // Get rid of useless log_topics data, because log_mark_read is better for it - even if marking unread - I think so...
  79. // @todo look at this...
  80. // The call to markBoardsRead() in Display() used to be simply
  81. // marking log_boards (the previous query only)
  82. $result = $smcFunc['db_query']('', '
  83. SELECT MIN(id_topic)
  84. FROM {db_prefix}log_topics
  85. WHERE id_member = {int:current_member}',
  86. array(
  87. 'current_member' => $user_info['id'],
  88. )
  89. );
  90. list ($lowest_topic) = $smcFunc['db_fetch_row']($result);
  91. $smcFunc['db_free_result']($result);
  92. if (empty($lowest_topic))
  93. return;
  94. // @todo SLOW This query seems to eat it sometimes.
  95. $result = $smcFunc['db_query']('', '
  96. SELECT lt.id_topic
  97. FROM {db_prefix}log_topics AS lt
  98. INNER JOIN {db_prefix}topics AS t /*!40000 USE INDEX (PRIMARY) */ ON (t.id_topic = lt.id_topic
  99. AND t.id_board IN ({array_int:board_list}))
  100. WHERE lt.id_member = {int:current_member}
  101. AND lt.id_topic >= {int:lowest_topic}',
  102. array(
  103. 'current_member' => $user_info['id'],
  104. 'board_list' => $boards,
  105. 'lowest_topic' => $lowest_topic,
  106. )
  107. );
  108. $topics = array();
  109. while ($row = $smcFunc['db_fetch_assoc']($result))
  110. $topics[] = $row['id_topic'];
  111. $smcFunc['db_free_result']($result);
  112. if (!empty($topics))
  113. $smcFunc['db_query']('', '
  114. DELETE FROM {db_prefix}log_topics
  115. WHERE id_member = {int:current_member}
  116. AND id_topic IN ({array_int:topic_list})',
  117. array(
  118. 'current_member' => $user_info['id'],
  119. 'topic_list' => $topics,
  120. )
  121. );
  122. }
  123. /**
  124. * Mark one or more boards as read.
  125. */
  126. function MarkRead()
  127. {
  128. global $board, $topic, $user_info, $board_info, $modSettings, $smcFunc;
  129. // No Guests allowed!
  130. is_not_guest();
  131. checkSession('get');
  132. if (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'all')
  133. {
  134. // Find all the boards this user can see.
  135. $result = $smcFunc['db_query']('', '
  136. SELECT b.id_board
  137. FROM {db_prefix}boards AS b
  138. WHERE {query_see_board}',
  139. array(
  140. )
  141. );
  142. $boards = array();
  143. while ($row = $smcFunc['db_fetch_assoc']($result))
  144. $boards[] = $row['id_board'];
  145. $smcFunc['db_free_result']($result);
  146. if (!empty($boards))
  147. markBoardsRead($boards, isset($_REQUEST['unread']));
  148. $_SESSION['id_msg_last_visit'] = $modSettings['maxMsgID'];
  149. if (!empty($_SESSION['old_url']) && strpos($_SESSION['old_url'], 'action=unread') !== false)
  150. redirectexit('action=unread');
  151. if (isset($_SESSION['topicseen_cache']))
  152. $_SESSION['topicseen_cache'] = array();
  153. redirectexit();
  154. }
  155. elseif (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'unreadreplies')
  156. {
  157. // Make sure all the boards are integers!
  158. $topics = explode('-', $_REQUEST['topics']);
  159. foreach ($topics as &$id_topic)
  160. $id_topic = (int) $id_topic;
  161. $smcFunc['db_query']('', '
  162. SELECT id_topic, disregarded
  163. FROM {db_prefix}log_topics
  164. WHERE id_topic IN ({array_int:selected_topics}
  165. AND id_member = {int:current_user}',
  166. array(
  167. 'selected_topics' => $topics,
  168. 'current_user' => $user_info['id'],
  169. )
  170. );
  171. $logged_topics = array();
  172. while ($row = $smcFunc['db_fetch_assoc']($request))
  173. $logged_topics[$row['id_topic']] = $row['disregarded'];
  174. $smcFunc['db_free_result']($request);
  175. $markRead = array();
  176. foreach ($topics as $id_topic)
  177. $markRead[] = array($modSettings['maxMsgID'], $user_info['id'], $id_topic, (isset($logged_topics[$topic]) ? $logged_topics[$topic] : 0));
  178. $smcFunc['db_insert']('replace',
  179. '{db_prefix}log_topics',
  180. array('id_msg' => 'int', 'id_member' => 'int', 'id_topic' => 'int', 'disregarded' => 'int'),
  181. $markRead,
  182. array('id_member', 'id_topic')
  183. );
  184. if (isset($_SESSION['topicseen_cache']))
  185. $_SESSION['topicseen_cache'] = array();
  186. redirectexit('action=unreadreplies');
  187. }
  188. // Special case: mark a topic unread!
  189. elseif (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'topic')
  190. {
  191. // First, let's figure out what the latest message is.
  192. $result = $smcFunc['db_query']('', '
  193. SELECT t.id_first_msg, t.id_last_msg, IFNULL(lt.disregarded, 0) as disregarded
  194. FROM {db_prefix}topics as t
  195. LEFT JOIN {db_prefix}log_topics as lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  196. WHERE t.id_topic = {int:current_topic}',
  197. array(
  198. 'current_topic' => $topic,
  199. 'current_member' => $user_info['id'],
  200. )
  201. );
  202. $topicinfo = $smcFunc['db_fetch_assoc']($result);
  203. $smcFunc['db_free_result']($result);
  204. if (!empty($_GET['t']))
  205. {
  206. // If they read the whole topic, go back to the beginning.
  207. if ($_GET['t'] >= $topicinfo['id_last_msg'])
  208. $earlyMsg = 0;
  209. // If they want to mark the whole thing read, same.
  210. elseif ($_GET['t'] <= $topicinfo['id_first_msg'])
  211. $earlyMsg = 0;
  212. // Otherwise, get the latest message before the named one.
  213. else
  214. {
  215. $result = $smcFunc['db_query']('', '
  216. SELECT MAX(id_msg)
  217. FROM {db_prefix}messages
  218. WHERE id_topic = {int:current_topic}
  219. AND id_msg >= {int:id_first_msg}
  220. AND id_msg < {int:topic_msg_id}',
  221. array(
  222. 'current_topic' => $topic,
  223. 'topic_msg_id' => (int) $_GET['t'],
  224. 'id_first_msg' => $topicinfo['id_first_msg'],
  225. )
  226. );
  227. list ($earlyMsg) = $smcFunc['db_fetch_row']($result);
  228. $smcFunc['db_free_result']($result);
  229. }
  230. }
  231. // Marking read from first page? That's the whole topic.
  232. elseif ($_REQUEST['start'] == 0)
  233. $earlyMsg = 0;
  234. else
  235. {
  236. $result = $smcFunc['db_query']('', '
  237. SELECT id_msg
  238. FROM {db_prefix}messages
  239. WHERE id_topic = {int:current_topic}
  240. ORDER BY id_msg
  241. LIMIT {int:start}, 1',
  242. array(
  243. 'current_topic' => $topic,
  244. 'start' => (int) $_REQUEST['start'],
  245. )
  246. );
  247. list ($earlyMsg) = $smcFunc['db_fetch_row']($result);
  248. $smcFunc['db_free_result']($result);
  249. $earlyMsg--;
  250. }
  251. // Blam, unread!
  252. $smcFunc['db_insert']('replace',
  253. '{db_prefix}log_topics',
  254. array('id_msg' => 'int', 'id_member' => 'int', 'id_topic' => 'int', 'disregarded' => 'int'),
  255. array($earlyMsg, $user_info['id'], $topic, $topicinfo['disregarded']),
  256. array('id_member', 'id_topic')
  257. );
  258. redirectexit('board=' . $board . '.0');
  259. }
  260. else
  261. {
  262. $categories = array();
  263. $boards = array();
  264. if (isset($_REQUEST['c']))
  265. {
  266. $_REQUEST['c'] = explode(',', $_REQUEST['c']);
  267. foreach ($_REQUEST['c'] as $c)
  268. $categories[] = (int) $c;
  269. }
  270. if (isset($_REQUEST['boards']))
  271. {
  272. $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
  273. foreach ($_REQUEST['boards'] as $b)
  274. $boards[] = (int) $b;
  275. }
  276. if (!empty($board))
  277. $boards[] = (int) $board;
  278. if (isset($_REQUEST['children']) && !empty($boards))
  279. {
  280. // They want to mark the entire tree starting with the boards specified
  281. // The easist thing is to just get all the boards they can see, but since we've specified the top of tree we ignore some of them
  282. $request = $smcFunc['db_query']('', '
  283. SELECT b.id_board, b.id_parent
  284. FROM {db_prefix}boards AS b
  285. WHERE {query_see_board}
  286. AND b.child_level > {int:no_parents}
  287. AND b.id_board NOT IN ({array_int:board_list})
  288. ORDER BY child_level ASC
  289. ',
  290. array(
  291. 'no_parents' => 0,
  292. 'board_list' => $boards,
  293. )
  294. );
  295. while ($row = $smcFunc['db_fetch_assoc']($request))
  296. if (in_array($row['id_parent'], $boards))
  297. $boards[] = $row['id_board'];
  298. $smcFunc['db_free_result']($request);
  299. }
  300. $clauses = array();
  301. $clauseParameters = array();
  302. if (!empty($categories))
  303. {
  304. $clauses[] = 'id_cat IN ({array_int:category_list})';
  305. $clauseParameters['category_list'] = $categories;
  306. }
  307. if (!empty($boards))
  308. {
  309. $clauses[] = 'id_board IN ({array_int:board_list})';
  310. $clauseParameters['board_list'] = $boards;
  311. }
  312. if (empty($clauses))
  313. redirectexit();
  314. $request = $smcFunc['db_query']('', '
  315. SELECT b.id_board
  316. FROM {db_prefix}boards AS b
  317. WHERE {query_see_board}
  318. AND b.' . implode(' OR b.', $clauses),
  319. array_merge($clauseParameters, array(
  320. ))
  321. );
  322. $boards = array();
  323. while ($row = $smcFunc['db_fetch_assoc']($request))
  324. $boards[] = $row['id_board'];
  325. $smcFunc['db_free_result']($request);
  326. if (empty($boards))
  327. redirectexit();
  328. markBoardsRead($boards, isset($_REQUEST['unread']));
  329. foreach ($boards as $b)
  330. {
  331. if (isset($_SESSION['topicseen_cache'][$b]))
  332. $_SESSION['topicseen_cache'][$b] = array();
  333. }
  334. if (!isset($_REQUEST['unread']))
  335. {
  336. // Find all the boards this user can see.
  337. $result = $smcFunc['db_query']('', '
  338. SELECT b.id_board
  339. FROM {db_prefix}boards AS b
  340. WHERE b.id_parent IN ({array_int:parent_list})
  341. AND {query_see_board}',
  342. array(
  343. 'parent_list' => $boards,
  344. )
  345. );
  346. if ($smcFunc['db_num_rows']($result) > 0)
  347. {
  348. $logBoardInserts = '';
  349. while ($row = $smcFunc['db_fetch_assoc']($result))
  350. $logBoardInserts[] = array($modSettings['maxMsgID'], $user_info['id'], $row['id_board']);
  351. $smcFunc['db_insert']('replace',
  352. '{db_prefix}log_boards',
  353. array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'),
  354. $logBoardInserts,
  355. array('id_member', 'id_board')
  356. );
  357. }
  358. $smcFunc['db_free_result']($result);
  359. if (empty($board))
  360. redirectexit();
  361. else
  362. redirectexit('board=' . $board . '.0');
  363. }
  364. else
  365. {
  366. if (empty($board_info['parent']))
  367. redirectexit();
  368. else
  369. redirectexit('board=' . $board_info['parent'] . '.0');
  370. }
  371. }
  372. }
  373. /**
  374. * Get the id_member associated with the specified message.
  375. * @param int $messageID
  376. * @return int the member id
  377. */
  378. function getMsgMemberID($messageID)
  379. {
  380. global $smcFunc;
  381. // Find the topic and make sure the member still exists.
  382. $result = $smcFunc['db_query']('', '
  383. SELECT IFNULL(mem.id_member, 0)
  384. FROM {db_prefix}messages AS m
  385. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  386. WHERE m.id_msg = {int:selected_message}
  387. LIMIT 1',
  388. array(
  389. 'selected_message' => (int) $messageID,
  390. )
  391. );
  392. if ($smcFunc['db_num_rows']($result) > 0)
  393. list ($memberID) = $smcFunc['db_fetch_row']($result);
  394. // The message doesn't even exist.
  395. else
  396. $memberID = 0;
  397. $smcFunc['db_free_result']($result);
  398. return (int) $memberID;
  399. }
  400. /**
  401. * Modify the settings and position of a board.
  402. * Used by ManageBoards.php to change the settings of a board.
  403. *
  404. * @param int $board_id
  405. * @param array &$boardOptions
  406. */
  407. function modifyBoard($board_id, &$boardOptions)
  408. {
  409. global $sourcedir, $cat_tree, $boards, $boardList, $modSettings, $smcFunc;
  410. // Get some basic information about all boards and categories.
  411. getBoardTree();
  412. // Make sure given boards and categories exist.
  413. if (!isset($boards[$board_id]) || (isset($boardOptions['target_board']) && !isset($boards[$boardOptions['target_board']])) || (isset($boardOptions['target_category']) && !isset($cat_tree[$boardOptions['target_category']])))
  414. fatal_lang_error('no_board');
  415. $id = $board_id;
  416. call_integration_hook('integrate_pre_modify_board', array($id, &$boardOptions));
  417. // All things that will be updated in the database will be in $boardUpdates.
  418. $boardUpdates = array();
  419. $boardUpdateParameters = array();
  420. // In case the board has to be moved
  421. if (isset($boardOptions['move_to']))
  422. {
  423. // Move the board to the top of a given category.
  424. if ($boardOptions['move_to'] == 'top')
  425. {
  426. $id_cat = $boardOptions['target_category'];
  427. $child_level = 0;
  428. $id_parent = 0;
  429. $after = $cat_tree[$id_cat]['last_board_order'];
  430. }
  431. // Move the board to the bottom of a given category.
  432. elseif ($boardOptions['move_to'] == 'bottom')
  433. {
  434. $id_cat = $boardOptions['target_category'];
  435. $child_level = 0;
  436. $id_parent = 0;
  437. $after = 0;
  438. foreach ($cat_tree[$id_cat]['children'] as $id_board => $dummy)
  439. $after = max($after, $boards[$id_board]['order']);
  440. }
  441. // Make the board a child of a given board.
  442. elseif ($boardOptions['move_to'] == 'child')
  443. {
  444. $id_cat = $boards[$boardOptions['target_board']]['category'];
  445. $child_level = $boards[$boardOptions['target_board']]['level'] + 1;
  446. $id_parent = $boardOptions['target_board'];
  447. // People can be creative, in many ways...
  448. if (isChildOf($id_parent, $board_id))
  449. fatal_lang_error('mboards_parent_own_child_error', false);
  450. elseif ($id_parent == $board_id)
  451. fatal_lang_error('mboards_board_own_child_error', false);
  452. $after = $boards[$boardOptions['target_board']]['order'];
  453. // Check if there are already children and (if so) get the max board order.
  454. if (!empty($boards[$id_parent]['tree']['children']) && empty($boardOptions['move_first_child']))
  455. foreach ($boards[$id_parent]['tree']['children'] as $childBoard_id => $dummy)
  456. $after = max($after, $boards[$childBoard_id]['order']);
  457. }
  458. // Place a board before or after another board, on the same child level.
  459. elseif (in_array($boardOptions['move_to'], array('before', 'after')))
  460. {
  461. $id_cat = $boards[$boardOptions['target_board']]['category'];
  462. $child_level = $boards[$boardOptions['target_board']]['level'];
  463. $id_parent = $boards[$boardOptions['target_board']]['parent'];
  464. $after = $boards[$boardOptions['target_board']]['order'] - ($boardOptions['move_to'] == 'before' ? 1 : 0);
  465. }
  466. // Oops...?
  467. else
  468. trigger_error('modifyBoard(): The move_to value \'' . $boardOptions['move_to'] . '\' is incorrect', E_USER_ERROR);
  469. // Get a list of children of this board.
  470. $childList = array();
  471. recursiveBoards($childList, $boards[$board_id]['tree']);
  472. // See if there are changes that affect children.
  473. $childUpdates = array();
  474. $levelDiff = $child_level - $boards[$board_id]['level'];
  475. if ($levelDiff != 0)
  476. $childUpdates[] = 'child_level = child_level ' . ($levelDiff > 0 ? '+ ' : '') . '{int:level_diff}';
  477. if ($id_cat != $boards[$board_id]['category'])
  478. $childUpdates[] = 'id_cat = {int:category}';
  479. // Fix the children of this board.
  480. if (!empty($childList) && !empty($childUpdates))
  481. $smcFunc['db_query']('', '
  482. UPDATE {db_prefix}boards
  483. SET ' . implode(',
  484. ', $childUpdates) . '
  485. WHERE id_board IN ({array_int:board_list})',
  486. array(
  487. 'board_list' => $childList,
  488. 'category' => $id_cat,
  489. 'level_diff' => $levelDiff,
  490. )
  491. );
  492. // Make some room for this spot.
  493. $smcFunc['db_query']('', '
  494. UPDATE {db_prefix}boards
  495. SET board_order = board_order + {int:new_order}
  496. WHERE board_order > {int:insert_after}
  497. AND id_board != {int:selected_board}',
  498. array(
  499. 'insert_after' => $after,
  500. 'selected_board' => $board_id,
  501. 'new_order' => 1 + count($childList),
  502. )
  503. );
  504. $boardUpdates[] = 'id_cat = {int:id_cat}';
  505. $boardUpdates[] = 'id_parent = {int:id_parent}';
  506. $boardUpdates[] = 'child_level = {int:child_level}';
  507. $boardUpdates[] = 'board_order = {int:board_order}';
  508. $boardUpdateParameters += array(
  509. 'id_cat' => $id_cat,
  510. 'id_parent' => $id_parent,
  511. 'child_level' => $child_level,
  512. 'board_order' => $after + 1,
  513. );
  514. }
  515. // This setting is a little twisted in the database...
  516. if (isset($boardOptions['posts_count']))
  517. {
  518. $boardUpdates[] = 'count_posts = {int:count_posts}';
  519. $boardUpdateParameters['count_posts'] = $boardOptions['posts_count'] ? 0 : 1;
  520. }
  521. // Set the theme for this board.
  522. if (isset($boardOptions['board_theme']))
  523. {
  524. $boardUpdates[] = 'id_theme = {int:id_theme}';
  525. $boardUpdateParameters['id_theme'] = (int) $boardOptions['board_theme'];
  526. }
  527. // Should the board theme override the user preferred theme?
  528. if (isset($boardOptions['override_theme']))
  529. {
  530. $boardUpdates[] = 'override_theme = {int:override_theme}';
  531. $boardUpdateParameters['override_theme'] = $boardOptions['override_theme'] ? 1 : 0;
  532. }
  533. // Who's allowed to access this board.
  534. if (isset($boardOptions['access_groups']))
  535. {
  536. $boardUpdates[] = 'member_groups = {string:member_groups}';
  537. $boardUpdateParameters['member_groups'] = implode(',', $boardOptions['access_groups']);
  538. }
  539. // And who isn't.
  540. if (isset($boardOptions['deny_groups']))
  541. {
  542. $boardUpdates[] = 'deny_member_groups = {string:deny_groups}';
  543. $boardUpdateParameters['deny_groups'] = implode(',', $boardOptions['deny_groups']);
  544. }
  545. if (isset($boardOptions['board_name']))
  546. {
  547. $boardUpdates[] = 'name = {string:board_name}';
  548. $boardUpdateParameters['board_name'] = $boardOptions['board_name'];
  549. }
  550. if (isset($boardOptions['board_description']))
  551. {
  552. $boardUpdates[] = 'description = {string:board_description}';
  553. $boardUpdateParameters['board_description'] = $boardOptions['board_description'];
  554. }
  555. if (isset($boardOptions['profile']))
  556. {
  557. $boardUpdates[] = 'id_profile = {int:profile}';
  558. $boardUpdateParameters['profile'] = (int) $boardOptions['profile'];
  559. }
  560. if (isset($boardOptions['redirect']))
  561. {
  562. $boardUpdates[] = 'redirect = {string:redirect}';
  563. $boardUpdateParameters['redirect'] = $boardOptions['redirect'];
  564. }
  565. if (isset($boardOptions['num_posts']))
  566. {
  567. $boardUpdates[] = 'num_posts = {int:num_posts}';
  568. $boardUpdateParameters['num_posts'] = (int) $boardOptions['num_posts'];
  569. }
  570. $id = $board_id;
  571. call_integration_hook('integrate_modify_board', array($id, &$boardUpdates, &$boardUpdateParameters));
  572. // Do the updates (if any).
  573. if (!empty($boardUpdates))
  574. $request = $smcFunc['db_query']('', '
  575. UPDATE {db_prefix}boards
  576. SET
  577. ' . implode(',
  578. ', $boardUpdates) . '
  579. WHERE id_board = {int:selected_board}',
  580. array_merge($boardUpdateParameters, array(
  581. 'selected_board' => $board_id,
  582. ))
  583. );
  584. // Set moderators of this board.
  585. if (isset($boardOptions['moderators']) || isset($boardOptions['moderator_string']))
  586. {
  587. // Reset current moderators for this board - if there are any!
  588. $smcFunc['db_query']('', '
  589. DELETE FROM {db_prefix}moderators
  590. WHERE id_board = {int:board_list}',
  591. array(
  592. 'board_list' => $board_id,
  593. )
  594. );
  595. // Validate and get the IDs of the new moderators.
  596. if (isset($boardOptions['moderator_string']) && trim($boardOptions['moderator_string']) != '')
  597. {
  598. // Divvy out the usernames, remove extra space.
  599. $moderator_string = strtr($smcFunc['htmlspecialchars']($boardOptions['moderator_string'], ENT_QUOTES), array('&quot;' => '"'));
  600. preg_match_all('~"([^"]+)"~', $moderator_string, $matches);
  601. $moderators = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $moderator_string)));
  602. for ($k = 0, $n = count($moderators); $k < $n; $k++)
  603. {
  604. $moderators[$k] = trim($moderators[$k]);
  605. if (strlen($moderators[$k]) == 0)
  606. unset($moderators[$k]);
  607. }
  608. // Find all the id_member's for the member_name's in the list.
  609. if (empty($boardOptions['moderators']))
  610. $boardOptions['moderators'] = array();
  611. if (!empty($moderators))
  612. {
  613. $request = $smcFunc['db_query']('', '
  614. SELECT id_member
  615. FROM {db_prefix}members
  616. WHERE member_name IN ({array_string:moderator_list}) OR real_name IN ({array_string:moderator_list})
  617. LIMIT ' . count($moderators),
  618. array(
  619. 'moderator_list' => $moderators,
  620. )
  621. );
  622. while ($row = $smcFunc['db_fetch_assoc']($request))
  623. $boardOptions['moderators'][] = $row['id_member'];
  624. $smcFunc['db_free_result']($request);
  625. }
  626. }
  627. // Add the moderators to the board.
  628. if (!empty($boardOptions['moderators']))
  629. {
  630. $inserts = array();
  631. foreach ($boardOptions['moderators'] as $moderator)
  632. $inserts[] = array($board_id, $moderator);
  633. $smcFunc['db_insert']('insert',
  634. '{db_prefix}moderators',
  635. array('id_board' => 'int', 'id_member' => 'int'),
  636. $inserts,
  637. array('id_board', 'id_member')
  638. );
  639. }
  640. // Note that caches can now be wrong!
  641. updateSettings(array('settings_updated' => time()));
  642. }
  643. if (isset($boardOptions['move_to']))
  644. reorderBoards();
  645. clean_cache('data');
  646. if (empty($boardOptions['dont_log']))
  647. logAction('edit_board', array('board' => $board_id), 'admin');
  648. }
  649. /**
  650. * Create a new board and set its properties and position.
  651. * Allows (almost) the same options as the modifyBoard() function.
  652. * With the option inherit_permissions set, the parent board permissions
  653. * will be inherited.
  654. *
  655. * @param array $boardOptions
  656. * @return int The new board id
  657. */
  658. function createBoard($boardOptions)
  659. {
  660. global $boards, $modSettings, $smcFunc;
  661. // Trigger an error if one of the required values is not set.
  662. if (!isset($boardOptions['board_name']) || trim($boardOptions['board_name']) == '' || !isset($boardOptions['move_to']) || !isset($boardOptions['target_category']))
  663. trigger_error('createBoard(): One or more of the required options is not set', E_USER_ERROR);
  664. if (in_array($boardOptions['move_to'], array('child', 'before', 'after')) && !isset($boardOptions['target_board']))
  665. trigger_error('createBoard(): Target board is not set', E_USER_ERROR);
  666. // Set every optional value to its default value.
  667. $boardOptions += array(
  668. 'posts_count' => true,
  669. 'override_theme' => false,
  670. 'board_theme' => 0,
  671. 'access_groups' => array(),
  672. 'board_description' => '',
  673. 'profile' => 1,
  674. 'moderators' => '',
  675. 'inherit_permissions' => true,
  676. 'dont_log' => true,
  677. );
  678. $board_columns = array(
  679. 'id_cat' => 'int', 'name' => 'string-255', 'description' => 'string', 'board_order' => 'int',
  680. 'member_groups' => 'string', 'redirect' => 'string',
  681. );
  682. $board_parameters = array(
  683. $boardOptions['target_category'], $boardOptions['board_name'] , '', 0,
  684. '-1,0', '',
  685. );
  686. call_integration_hook('integrate_create_board', array(&$boardOptions, &$board_columns, &$board_parameters));
  687. // Insert a board, the settings are dealt with later.
  688. $smcFunc['db_insert']('',
  689. '{db_prefix}boards',
  690. $board_columns,
  691. $board_parameters,
  692. array('id_board')
  693. );
  694. $board_id = $smcFunc['db_insert_id']('{db_prefix}boards', 'id_board');
  695. if (empty($board_id))
  696. return 0;
  697. // Change the board according to the given specifications.
  698. modifyBoard($board_id, $boardOptions);
  699. // Do we want the parent permissions to be inherited?
  700. if ($boardOptions['inherit_permissions'])
  701. {
  702. getBoardTree();
  703. if (!empty($boards[$board_id]['parent']))
  704. {
  705. $request = $smcFunc['db_query']('', '
  706. SELECT id_profile
  707. FROM {db_prefix}boards
  708. WHERE id_board = {int:board_parent}
  709. LIMIT 1',
  710. array(
  711. 'board_parent' => (int) $boards[$board_id]['parent'],
  712. )
  713. );
  714. list ($boardOptions['profile']) = $smcFunc['db_fetch_row']($request);
  715. $smcFunc['db_free_result']($request);
  716. $smcFunc['db_query']('', '
  717. UPDATE {db_prefix}boards
  718. SET id_profile = {int:new_profile}
  719. WHERE id_board = {int:current_board}',
  720. array(
  721. 'new_profile' => $boardOptions['profile'],
  722. 'current_board' => $board_id,
  723. )
  724. );
  725. }
  726. }
  727. // Clean the data cache.
  728. clean_cache('data');
  729. // Created it.
  730. logAction('add_board', array('board' => $board_id), 'admin');
  731. // Here you are, a new board, ready to be spammed.
  732. return $board_id;
  733. }
  734. /**
  735. * Remove one or more boards.
  736. * Allows to move the children of the board before deleting it
  737. * if moveChildrenTo is set to null, the child boards will be deleted.
  738. * Deletes:
  739. * - all topics that are on the given boards;
  740. * - all information that's associated with the given boards;
  741. * updates the statistics to reflect the new situation.
  742. *
  743. * @param array $boards_to_remove
  744. * @param array $moveChildrenTo = null
  745. */
  746. function deleteBoards($boards_to_remove, $moveChildrenTo = null)
  747. {
  748. global $sourcedir, $boards, $smcFunc;
  749. // No boards to delete? Return!
  750. if (empty($boards_to_remove))
  751. return;
  752. getBoardTree();
  753. call_integration_hook('integrate_delete_board', array($boards_to_remove, &$moveChildrenTo));
  754. // If $moveChildrenTo is set to null, include the children in the removal.
  755. if ($moveChildrenTo === null)
  756. {
  757. // Get a list of the child boards that will also be removed.
  758. $child_boards_to_remove = array();
  759. foreach ($boards_to_remove as $board_to_remove)
  760. recursiveBoards($child_boards_to_remove, $boards[$board_to_remove]['tree']);
  761. // Merge the children with their parents.
  762. if (!empty($child_boards_to_remove))
  763. $boards_to_remove = array_unique(array_merge($boards_to_remove, $child_boards_to_remove));
  764. }
  765. // Move the children to a safe home.
  766. else
  767. {
  768. foreach ($boards_to_remove as $id_board)
  769. {
  770. // @todo Separate category?
  771. if ($moveChildrenTo === 0)
  772. fixChildren($id_board, 0, 0);
  773. else
  774. fixChildren($id_board, $boards[$moveChildrenTo]['level'] + 1, $moveChildrenTo);
  775. }
  776. }
  777. // Delete ALL topics in the selected boards (done first so topics can't be marooned.)
  778. $request = $smcFunc['db_query']('', '
  779. SELECT id_topic
  780. FROM {db_prefix}topics
  781. WHERE id_board IN ({array_int:boards_to_remove})',
  782. array(
  783. 'boards_to_remove' => $boards_to_remove,
  784. )
  785. );
  786. $topics = array();
  787. while ($row = $smcFunc['db_fetch_assoc']($request))
  788. $topics[] = $row['id_topic'];
  789. $smcFunc['db_free_result']($request);
  790. require_once($sourcedir . '/RemoveTopic.php');
  791. removeTopics($topics, false);
  792. // Delete the board's logs.
  793. $smcFunc['db_query']('', '
  794. DELETE FROM {db_prefix}log_mark_read
  795. WHERE id_board IN ({array_int:boards_to_remove})',
  796. array(
  797. 'boards_to_remove' => $boards_to_remove,
  798. )
  799. );
  800. $smcFunc['db_query']('', '
  801. DELETE FROM {db_prefix}log_boards
  802. WHERE id_board IN ({array_int:boards_to_remove})',
  803. array(
  804. 'boards_to_remove' => $boards_to_remove,
  805. )
  806. );
  807. $smcFunc['db_query']('', '
  808. DELETE FROM {db_prefix}log_notify
  809. WHERE id_board IN ({array_int:boards_to_remove})',
  810. array(
  811. 'boards_to_remove' => $boards_to_remove,
  812. )
  813. );
  814. // Delete this board's moderators.
  815. $smcFunc['db_query']('', '
  816. DELETE FROM {db_prefix}moderators
  817. WHERE id_board IN ({array_int:boards_to_remove})',
  818. array(
  819. 'boards_to_remove' => $boards_to_remove,
  820. )
  821. );
  822. // Delete any extra events in the calendar.
  823. $smcFunc['db_query']('', '
  824. DELETE FROM {db_prefix}calendar
  825. WHERE id_board IN ({array_int:boards_to_remove})',
  826. array(
  827. 'boards_to_remove' => $boards_to_remove,
  828. )
  829. );
  830. // Delete any message icons that only appear on these boards.
  831. $smcFunc['db_query']('', '
  832. DELETE FROM {db_prefix}message_icons
  833. WHERE id_board IN ({array_int:boards_to_remove})',
  834. array(
  835. 'boards_to_remove' => $boards_to_remove,
  836. )
  837. );
  838. // Delete the boards.
  839. $smcFunc['db_query']('', '
  840. DELETE FROM {db_prefix}boards
  841. WHERE id_board IN ({array_int:boards_to_remove})',
  842. array(
  843. 'boards_to_remove' => $boards_to_remove,
  844. )
  845. );
  846. // Latest message/topic might not be there anymore.
  847. updateStats('message');
  848. updateStats('topic');
  849. updateSettings(array(
  850. 'calendar_updated' => time(),
  851. ));
  852. // Plus reset the cache to stop people getting odd results.
  853. updateSettings(array('settings_updated' => time()));
  854. // Clean the cache as well.
  855. clean_cache('data');
  856. // Let's do some serious logging.
  857. foreach ($boards_to_remove as $id_board)
  858. logAction('delete_board', array('boardname' => $boards[$id_board]['name']), 'admin');
  859. reorderBoards();
  860. }
  861. /**
  862. * Put all boards in the right order and sorts the records of the boards table.
  863. * Used by modifyBoard(), deleteBoards(), modifyCategory(), and deleteCategories() functions
  864. */
  865. function reorderBoards()
  866. {
  867. global $cat_tree, $boardList, $boards, $smcFunc;
  868. getBoardTree();
  869. // Set the board order for each category.
  870. $board_order = 0;
  871. foreach ($cat_tree as $catID => $dummy)
  872. {
  873. foreach ($boardList[$catID] as $boardID)
  874. if ($boards[$boardID]['order'] != ++$board_order)
  875. $smcFunc['db_query']('', '
  876. UPDATE {db_prefix}boards
  877. SET board_order = {int:new_order}
  878. WHERE id_board = {int:selected_board}',
  879. array(
  880. 'new_order' => $board_order,
  881. 'selected_board' => $boardID,
  882. )
  883. );
  884. }
  885. // Sort the records of the boards table on the board_order value.
  886. $smcFunc['db_query']('alter_table_boards', '
  887. ALTER TABLE {db_prefix}boards
  888. ORDER BY board_order',
  889. array(
  890. 'db_error_skip' => true,
  891. )
  892. );
  893. }
  894. /**
  895. * Fixes the children of a board by setting their child_levels to new values.
  896. * Used when a board is deleted or moved, to affect its children.
  897. *
  898. * @param int $parent
  899. * @param int $newLevel
  900. * @param int $newParent
  901. */
  902. function fixChildren($parent, $newLevel, $newParent)
  903. {
  904. global $smcFunc;
  905. // Grab all children of $parent...
  906. $result = $smcFunc['db_query']('', '
  907. SELECT id_board
  908. FROM {db_prefix}boards
  909. WHERE id_parent = {int:parent_board}',
  910. array(
  911. 'parent_board' => $parent,
  912. )
  913. );
  914. $children = array();
  915. while ($row = $smcFunc['db_fetch_assoc']($result))
  916. $children[] = $row['id_board'];
  917. $smcFunc['db_free_result']($result);
  918. // ...and set it to a new parent and child_level.
  919. $smcFunc['db_query']('', '
  920. UPDATE {db_prefix}boards
  921. SET id_parent = {int:new_parent}, child_level = {int:new_child_level}
  922. WHERE id_parent = {int:parent_board}',
  923. array(
  924. 'new_parent' => $newParent,
  925. 'new_child_level' => $newLevel,
  926. 'parent_board' => $parent,
  927. )
  928. );
  929. // Recursively fix the children of the children.
  930. foreach ($children as $child)
  931. fixChildren($child, $newLevel + 1, $child);
  932. }
  933. /**
  934. * Load a lot of useful information regarding the boards and categories.
  935. * The information retrieved is stored in globals:
  936. * $boards properties of each board.
  937. * $boardList a list of boards grouped by category ID.
  938. * $cat_tree properties of each category.
  939. */
  940. function getBoardTree()
  941. {
  942. global $cat_tree, $boards, $boardList, $txt, $modSettings, $smcFunc;
  943. // Getting all the board and category information you'd ever wanted.
  944. $request = $smcFunc['db_query']('', '
  945. SELECT
  946. IFNULL(b.id_board, 0) AS id_board, b.id_parent, b.name AS board_name, b.description, b.child_level,
  947. b.board_order, b.count_posts, b.member_groups, b.id_theme, b.override_theme, b.id_profile, b.redirect,
  948. b.num_posts, b.num_topics, b.deny_member_groups, c.id_cat, c.name AS cat_name, c.cat_order, c.can_collapse
  949. FROM {db_prefix}categories AS c
  950. LEFT JOIN {db_prefix}boards AS b ON (b.id_cat = c.id_cat)
  951. ORDER BY c.cat_order, b.child_level, b.board_order',
  952. array(
  953. )
  954. );
  955. $cat_tree = array();
  956. $boards = array();
  957. $last_board_order = 0;
  958. while ($row = $smcFunc['db_fetch_assoc']($request))
  959. {
  960. if (!isset($cat_tree[$row['id_cat']]))
  961. {
  962. $cat_tree[$row['id_cat']] = array(
  963. 'node' => array(
  964. 'id' => $row['id_cat'],
  965. 'name' => $row['cat_name'],
  966. 'order' => $row['cat_order'],
  967. 'can_collapse' => $row['can_collapse']
  968. ),
  969. 'is_first' => empty($cat_tree),
  970. 'last_board_order' => $last_board_order,
  971. 'children' => array()
  972. );
  973. $prevBoard = 0;
  974. $curLevel = 0;
  975. }
  976. if (!empty($row['id_board']))
  977. {
  978. if ($row['child_level'] != $curLevel)
  979. $prevBoard = 0;
  980. $boards[$row['id_board']] = array(
  981. 'id' => $row['id_board'],
  982. 'category' => $row['id_cat'],
  983. 'parent' => $row['id_parent'],
  984. 'level' => $row['child_level'],
  985. 'order' => $row['board_order'],
  986. 'name' => $row['board_name'],
  987. 'member_groups' => explode(',', $row['member_groups']),
  988. 'deny_groups' => explode(',', $row['deny_member_groups']),
  989. 'description' => $row['description'],
  990. 'count_posts' => empty($row['count_posts']),
  991. 'posts' => $row['num_posts'],
  992. 'topics' => $row['num_topics'],
  993. 'theme' => $row['id_theme'],
  994. 'override_theme' => $row['override_theme'],
  995. 'profile' => $row['id_profile'],
  996. 'redirect' => $row['redirect'],
  997. 'prev_board' => $prevBoard
  998. );
  999. $prevBoard = $row['id_board'];
  1000. $last_board_order = $row['board_order'];
  1001. if (empty($row['child_level']))
  1002. {
  1003. $cat_tree[$row['id_cat']]['children'][$row['id_board']] = array(
  1004. 'node' => &$boards[$row['id_board']],
  1005. 'is_first' => empty($cat_tree[$row['id_cat']]['children']),
  1006. 'children' => array()
  1007. );
  1008. $boards[$row['id_board']]['tree'] = &$cat_tree[$row['id_cat']]['children'][$row['id_board']];
  1009. }
  1010. else
  1011. {
  1012. // Parent doesn't exist!
  1013. if (!isset($boards[$row['id_parent']]['tree']))
  1014. fatal_lang_error('no_valid_parent', false, array($row['board_name']));
  1015. // Wrong childlevel...we can silently fix this...
  1016. if ($boards[$row['id_parent']]['tree']['node']['level'] != $row['child_level'] - 1)
  1017. $smcFunc['db_query']('', '
  1018. UPDATE {db_prefix}boards
  1019. SET child_level = {int:new_child_level}
  1020. WHERE id_board = {int:selected_board}',
  1021. array(
  1022. 'new_child_level' => $boards[$row['id_parent']]['tree']['node']['level'] + 1,
  1023. 'selected_board' => $row['id_board'],
  1024. )
  1025. );
  1026. $boards[$row['id_parent']]['tree']['children'][$row['id_board']] = array(
  1027. 'node' => &$boards[$row['id_board']],
  1028. 'is_first' => empty($boards[$row['id_parent']]['tree']['children']),
  1029. 'children' => array()
  1030. );
  1031. $boards[$row['id_board']]['tree'] = &$boards[$row['id_parent']]['tree']['children'][$row['id_board']];
  1032. }
  1033. }
  1034. }
  1035. $smcFunc['db_free_result']($request);
  1036. // Get a list of all the boards in each category (using recursion).
  1037. $boardList = array();
  1038. foreach ($cat_tree as $catID => $node)
  1039. {
  1040. $boardList[$catID] = array();
  1041. recursiveBoards($boardList[$catID], $node);
  1042. }
  1043. }
  1044. /**
  1045. * Recursively get a list of boards.
  1046. * Used by getBoardTree
  1047. *
  1048. * @param array &$_boardList
  1049. * @param array &$_tree
  1050. */
  1051. function recursiveBoards(&$_boardList, &$_tree)
  1052. {
  1053. if (empty($_tree['children']))
  1054. return;
  1055. foreach ($_tree['children'] as $id => $node)
  1056. {
  1057. $_boardList[] = $id;
  1058. recursiveBoards($_boardList, $node);
  1059. }
  1060. }
  1061. /**
  1062. * Returns whether the child board id is actually a child of the parent (recursive).
  1063. * @param int $child
  1064. * @param int $parent
  1065. * @return bool
  1066. */
  1067. function isChildOf($child, $parent)
  1068. {
  1069. global $boards;
  1070. if (empty($boards[$child]['parent']))
  1071. return false;
  1072. if ($boards[$child]['parent'] == $parent)
  1073. return true;
  1074. return isChildOf($boards[$child]['parent'], $parent);
  1075. }
  1076. ?>