Subs-Boards.php 35 KB

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