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. 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. call_integration_hook('integrate_modify_board', array($board_id, &$boardOptions));
  431. // All things that will be updated in the database will be in $boardUpdates.
  432. $boardUpdates = array();
  433. $boardUpdateParameters = array();
  434. // In case the board has to be moved
  435. if (isset($boardOptions['move_to']))
  436. {
  437. // Move the board to the top of a given category.
  438. if ($boardOptions['move_to'] == 'top')
  439. {
  440. $id_cat = $boardOptions['target_category'];
  441. $child_level = 0;
  442. $id_parent = 0;
  443. $after = $cat_tree[$id_cat]['last_board_order'];
  444. }
  445. // Move the board to the bottom of a given category.
  446. elseif ($boardOptions['move_to'] == 'bottom')
  447. {
  448. $id_cat = $boardOptions['target_category'];
  449. $child_level = 0;
  450. $id_parent = 0;
  451. $after = 0;
  452. foreach ($cat_tree[$id_cat]['children'] as $id_board => $dummy)
  453. $after = max($after, $boards[$id_board]['order']);
  454. }
  455. // Make the board a child of a given board.
  456. elseif ($boardOptions['move_to'] == 'child')
  457. {
  458. $id_cat = $boards[$boardOptions['target_board']]['category'];
  459. $child_level = $boards[$boardOptions['target_board']]['level'] + 1;
  460. $id_parent = $boardOptions['target_board'];
  461. // People can be creative, in many ways...
  462. if (isChildOf($id_parent, $board_id))
  463. fatal_lang_error('mboards_parent_own_child_error', false);
  464. elseif ($id_parent == $board_id)
  465. fatal_lang_error('mboards_board_own_child_error', false);
  466. $after = $boards[$boardOptions['target_board']]['order'];
  467. // Check if there are already children and (if so) get the max board order.
  468. if (!empty($boards[$id_parent]['tree']['children']) && empty($boardOptions['move_first_child']))
  469. foreach ($boards[$id_parent]['tree']['children'] as $childBoard_id => $dummy)
  470. $after = max($after, $boards[$childBoard_id]['order']);
  471. }
  472. // Place a board before or after another board, on the same child level.
  473. elseif (in_array($boardOptions['move_to'], array('before', 'after')))
  474. {
  475. $id_cat = $boards[$boardOptions['target_board']]['category'];
  476. $child_level = $boards[$boardOptions['target_board']]['level'];
  477. $id_parent = $boards[$boardOptions['target_board']]['parent'];
  478. $after = $boards[$boardOptions['target_board']]['order'] - ($boardOptions['move_to'] == 'before' ? 1 : 0);
  479. }
  480. // Oops...?
  481. else
  482. trigger_error('modifyBoard(): The move_to value \'' . $boardOptions['move_to'] . '\' is incorrect', E_USER_ERROR);
  483. // Get a list of children of this board.
  484. $childList = array();
  485. recursiveBoards($childList, $boards[$board_id]['tree']);
  486. // See if there are changes that affect children.
  487. $childUpdates = array();
  488. $levelDiff = $child_level - $boards[$board_id]['level'];
  489. if ($levelDiff != 0)
  490. $childUpdates[] = 'child_level = child_level ' . ($levelDiff > 0 ? '+ ' : '') . '{int:level_diff}';
  491. if ($id_cat != $boards[$board_id]['category'])
  492. $childUpdates[] = 'id_cat = {int:category}';
  493. // Fix the children of this board.
  494. if (!empty($childList) && !empty($childUpdates))
  495. $smcFunc['db_query']('', '
  496. UPDATE {db_prefix}boards
  497. SET ' . implode(',
  498. ', $childUpdates) . '
  499. WHERE id_board IN ({array_int:board_list})',
  500. array(
  501. 'board_list' => $childList,
  502. 'category' => $id_cat,
  503. 'level_diff' => $levelDiff,
  504. )
  505. );
  506. // Make some room for this spot.
  507. $smcFunc['db_query']('', '
  508. UPDATE {db_prefix}boards
  509. SET board_order = board_order + {int:new_order}
  510. WHERE board_order > {int:insert_after}
  511. AND id_board != {int:selected_board}',
  512. array(
  513. 'insert_after' => $after,
  514. 'selected_board' => $board_id,
  515. 'new_order' => 1 + count($childList),
  516. )
  517. );
  518. $boardUpdates[] = 'id_cat = {int:id_cat}';
  519. $boardUpdates[] = 'id_parent = {int:id_parent}';
  520. $boardUpdates[] = 'child_level = {int:child_level}';
  521. $boardUpdates[] = 'board_order = {int:board_order}';
  522. $boardUpdateParameters += array(
  523. 'id_cat' => $id_cat,
  524. 'id_parent' => $id_parent,
  525. 'child_level' => $child_level,
  526. 'board_order' => $after + 1,
  527. );
  528. }
  529. // This setting is a little twisted in the database...
  530. if (isset($boardOptions['posts_count']))
  531. {
  532. $boardUpdates[] = 'count_posts = {int:count_posts}';
  533. $boardUpdateParameters['count_posts'] = $boardOptions['posts_count'] ? 0 : 1;
  534. }
  535. // Set the theme for this board.
  536. if (isset($boardOptions['board_theme']))
  537. {
  538. $boardUpdates[] = 'id_theme = {int:id_theme}';
  539. $boardUpdateParameters['id_theme'] = (int) $boardOptions['board_theme'];
  540. }
  541. // Should the board theme override the user preferred theme?
  542. if (isset($boardOptions['override_theme']))
  543. {
  544. $boardUpdates[] = 'override_theme = {int:override_theme}';
  545. $boardUpdateParameters['override_theme'] = $boardOptions['override_theme'] ? 1 : 0;
  546. }
  547. // Who's allowed to access this board.
  548. if (isset($boardOptions['access_groups']))
  549. {
  550. $boardUpdates[] = 'member_groups = {string:member_groups}';
  551. $boardUpdateParameters['member_groups'] = implode(',', $boardOptions['access_groups']);
  552. }
  553. // And who isn't.
  554. if (isset($boardOptions['deny_groups']))
  555. {
  556. $boardUpdates[] = 'deny_member_groups = {string:deny_groups}';
  557. $boardUpdateParameters['deny_groups'] = implode(',', $boardOptions['deny_groups']);
  558. }
  559. if (isset($boardOptions['board_name']))
  560. {
  561. $boardUpdates[] = 'name = {string:board_name}';
  562. $boardUpdateParameters['board_name'] = $boardOptions['board_name'];
  563. }
  564. if (isset($boardOptions['board_description']))
  565. {
  566. $boardUpdates[] = 'description = {string:board_description}';
  567. $boardUpdateParameters['board_description'] = $boardOptions['board_description'];
  568. }
  569. if (isset($boardOptions['profile']))
  570. {
  571. $boardUpdates[] = 'id_profile = {int:profile}';
  572. $boardUpdateParameters['profile'] = (int) $boardOptions['profile'];
  573. }
  574. if (isset($boardOptions['redirect']))
  575. {
  576. $boardUpdates[] = 'redirect = {string:redirect}';
  577. $boardUpdateParameters['redirect'] = $boardOptions['redirect'];
  578. }
  579. if (isset($boardOptions['num_posts']))
  580. {
  581. $boardUpdates[] = 'num_posts = {int:num_posts}';
  582. $boardUpdateParameters['num_posts'] = (int) $boardOptions['num_posts'];
  583. }
  584. // Do the updates (if any).
  585. if (!empty($boardUpdates))
  586. $request = $smcFunc['db_query']('', '
  587. UPDATE {db_prefix}boards
  588. SET
  589. ' . implode(',
  590. ', $boardUpdates) . '
  591. WHERE id_board = {int:selected_board}',
  592. array_merge($boardUpdateParameters, array(
  593. 'selected_board' => $board_id,
  594. ))
  595. );
  596. // Set moderators of this board.
  597. if (isset($boardOptions['moderators']) || isset($boardOptions['moderator_string']))
  598. {
  599. // Reset current moderators for this board - if there are any!
  600. $smcFunc['db_query']('', '
  601. DELETE FROM {db_prefix}moderators
  602. WHERE id_board = {int:board_list}',
  603. array(
  604. 'board_list' => $board_id,
  605. )
  606. );
  607. // Validate and get the IDs of the new moderators.
  608. if (isset($boardOptions['moderator_string']) && trim($boardOptions['moderator_string']) != '')
  609. {
  610. // Divvy out the usernames, remove extra space.
  611. $moderator_string = strtr($smcFunc['htmlspecialchars']($boardOptions['moderator_string'], ENT_QUOTES), array('&quot;' => '"'));
  612. preg_match_all('~"([^"]+)"~', $moderator_string, $matches);
  613. $moderators = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $moderator_string)));
  614. for ($k = 0, $n = count($moderators); $k < $n; $k++)
  615. {
  616. $moderators[$k] = trim($moderators[$k]);
  617. if (strlen($moderators[$k]) == 0)
  618. unset($moderators[$k]);
  619. }
  620. // Find all the id_member's for the member_name's in the list.
  621. if (empty($boardOptions['moderators']))
  622. $boardOptions['moderators'] = array();
  623. if (!empty($moderators))
  624. {
  625. $request = $smcFunc['db_query']('', '
  626. SELECT id_member
  627. FROM {db_prefix}members
  628. WHERE member_name IN ({array_string:moderator_list}) OR real_name IN ({array_string:moderator_list})
  629. LIMIT ' . count($moderators),
  630. array(
  631. 'moderator_list' => $moderators,
  632. )
  633. );
  634. while ($row = $smcFunc['db_fetch_assoc']($request))
  635. $boardOptions['moderators'][] = $row['id_member'];
  636. $smcFunc['db_free_result']($request);
  637. }
  638. }
  639. // Add the moderators to the board.
  640. if (!empty($boardOptions['moderators']))
  641. {
  642. $inserts = array();
  643. foreach ($boardOptions['moderators'] as $moderator)
  644. $inserts[] = array($board_id, $moderator);
  645. $smcFunc['db_insert']('insert',
  646. '{db_prefix}moderators',
  647. array('id_board' => 'int', 'id_member' => 'int'),
  648. $inserts,
  649. array('id_board', 'id_member')
  650. );
  651. }
  652. // Note that caches can now be wrong!
  653. updateSettings(array('settings_updated' => time()));
  654. }
  655. if (isset($boardOptions['move_to']))
  656. reorderBoards();
  657. clean_cache('data');
  658. if (empty($boardOptions['dont_log']))
  659. logAction('edit_board', array('board' => $board_id), 'admin');
  660. }
  661. /**
  662. * Create a new board and set its properties and position.
  663. * @param array $boardOptions
  664. * @return int The new board id
  665. */
  666. function createBoard($boardOptions)
  667. {
  668. global $boards, $modSettings, $smcFunc;
  669. // Trigger an error if one of the required values is not set.
  670. if (!isset($boardOptions['board_name']) || trim($boardOptions['board_name']) == '' || !isset($boardOptions['move_to']) || !isset($boardOptions['target_category']))
  671. trigger_error('createBoard(): One or more of the required options is not set', E_USER_ERROR);
  672. if (in_array($boardOptions['move_to'], array('child', 'before', 'after')) && !isset($boardOptions['target_board']))
  673. trigger_error('createBoard(): Target board is not set', E_USER_ERROR);
  674. call_integration_hook('integrate_create_board', array(&$boardOptions));
  675. // Set every optional value to its default value.
  676. $boardOptions += array(
  677. 'posts_count' => true,
  678. 'override_theme' => false,
  679. 'board_theme' => 0,
  680. 'access_groups' => array(),
  681. 'board_description' => '',
  682. 'profile' => 1,
  683. 'moderators' => '',
  684. 'inherit_permissions' => true,
  685. 'dont_log' => true,
  686. );
  687. // Insert a board, the settings are dealt with later.
  688. $smcFunc['db_insert']('',
  689. '{db_prefix}boards',
  690. array(
  691. 'id_cat' => 'int', 'name' => 'string-255', 'description' => 'string', 'board_order' => 'int',
  692. 'member_groups' => 'string', 'redirect' => 'string',
  693. ),
  694. array(
  695. $boardOptions['target_category'], $boardOptions['board_name'] , '', 0,
  696. '-1,0', '',
  697. ),
  698. array('id_board')
  699. );
  700. $board_id = $smcFunc['db_insert_id']('{db_prefix}boards', 'id_board');
  701. if (empty($board_id))
  702. return 0;
  703. // Change the board according to the given specifications.
  704. modifyBoard($board_id, $boardOptions);
  705. // Do we want the parent permissions to be inherited?
  706. if ($boardOptions['inherit_permissions'])
  707. {
  708. getBoardTree();
  709. if (!empty($boards[$board_id]['parent']))
  710. {
  711. $request = $smcFunc['db_query']('', '
  712. SELECT id_profile
  713. FROM {db_prefix}boards
  714. WHERE id_board = {int:board_parent}
  715. LIMIT 1',
  716. array(
  717. 'board_parent' => (int) $boards[$board_id]['parent'],
  718. )
  719. );
  720. list ($boardOptions['profile']) = $smcFunc['db_fetch_row']($request);
  721. $smcFunc['db_free_result']($request);
  722. $smcFunc['db_query']('', '
  723. UPDATE {db_prefix}boards
  724. SET id_profile = {int:new_profile}
  725. WHERE id_board = {int:current_board}',
  726. array(
  727. 'new_profile' => $boardOptions['profile'],
  728. 'current_board' => $board_id,
  729. )
  730. );
  731. }
  732. }
  733. // Clean the data cache.
  734. clean_cache('data');
  735. // Created it.
  736. logAction('add_board', array('board' => $board_id), 'admin');
  737. // Here you are, a new board, ready to be spammed.
  738. return $board_id;
  739. }
  740. /**
  741. * Remove one or more boards.
  742. * @param array $boards_to_remove
  743. * @param array $moveChildrenTo = null
  744. */
  745. function deleteBoards($boards_to_remove, $moveChildrenTo = null)
  746. {
  747. global $sourcedir, $boards, $smcFunc;
  748. // No boards to delete? Return!
  749. if (empty($boards_to_remove))
  750. return;
  751. getBoardTree();
  752. call_integration_hook('integrate_delete_board', array($boards_to_remove, &$moveChildrenTo));
  753. // If $moveChildrenTo is set to null, include the children in the removal.
  754. if ($moveChildrenTo === null)
  755. {
  756. // Get a list of the child boards that will also be removed.
  757. $child_boards_to_remove = array();
  758. foreach ($boards_to_remove as $board_to_remove)
  759. recursiveBoards($child_boards_to_remove, $boards[$board_to_remove]['tree']);
  760. // Merge the children with their parents.
  761. if (!empty($child_boards_to_remove))
  762. $boards_to_remove = array_unique(array_merge($boards_to_remove, $child_boards_to_remove));
  763. }
  764. // Move the children to a safe home.
  765. else
  766. {
  767. foreach ($boards_to_remove as $id_board)
  768. {
  769. // @todo Separate category?
  770. if ($moveChildrenTo === 0)
  771. fixChildren($id_board, 0, 0);
  772. else
  773. fixChildren($id_board, $boards[$moveChildrenTo]['level'] + 1, $moveChildrenTo);
  774. }
  775. }
  776. // Delete ALL topics in the selected boards (done first so topics can't be marooned.)
  777. $request = $smcFunc['db_query']('', '
  778. SELECT id_topic
  779. FROM {db_prefix}topics
  780. WHERE id_board IN ({array_int:boards_to_remove})',
  781. array(
  782. 'boards_to_remove' => $boards_to_remove,
  783. )
  784. );
  785. $topics = array();
  786. while ($row = $smcFunc['db_fetch_assoc']($request))
  787. $topics[] = $row['id_topic'];
  788. $smcFunc['db_free_result']($request);
  789. require_once($sourcedir . '/RemoveTopic.php');
  790. removeTopics($topics, false);
  791. // Delete the board's logs.
  792. $smcFunc['db_query']('', '
  793. DELETE FROM {db_prefix}log_mark_read
  794. WHERE id_board IN ({array_int:boards_to_remove})',
  795. array(
  796. 'boards_to_remove' => $boards_to_remove,
  797. )
  798. );
  799. $smcFunc['db_query']('', '
  800. DELETE FROM {db_prefix}log_boards
  801. WHERE id_board IN ({array_int:boards_to_remove})',
  802. array(
  803. 'boards_to_remove' => $boards_to_remove,
  804. )
  805. );
  806. $smcFunc['db_query']('', '
  807. DELETE FROM {db_prefix}log_notify
  808. WHERE id_board IN ({array_int:boards_to_remove})',
  809. array(
  810. 'boards_to_remove' => $boards_to_remove,
  811. )
  812. );
  813. // Delete this board's moderators.
  814. $smcFunc['db_query']('', '
  815. DELETE FROM {db_prefix}moderators
  816. WHERE id_board IN ({array_int:boards_to_remove})',
  817. array(
  818. 'boards_to_remove' => $boards_to_remove,
  819. )
  820. );
  821. // Delete any extra events in the calendar.
  822. $smcFunc['db_query']('', '
  823. DELETE FROM {db_prefix}calendar
  824. WHERE id_board IN ({array_int:boards_to_remove})',
  825. array(
  826. 'boards_to_remove' => $boards_to_remove,
  827. )
  828. );
  829. // Delete any message icons that only appear on these boards.
  830. $smcFunc['db_query']('', '
  831. DELETE FROM {db_prefix}message_icons
  832. WHERE id_board IN ({array_int:boards_to_remove})',
  833. array(
  834. 'boards_to_remove' => $boards_to_remove,
  835. )
  836. );
  837. // Delete the boards.
  838. $smcFunc['db_query']('', '
  839. DELETE FROM {db_prefix}boards
  840. WHERE id_board IN ({array_int:boards_to_remove})',
  841. array(
  842. 'boards_to_remove' => $boards_to_remove,
  843. )
  844. );
  845. // Latest message/topic might not be there anymore.
  846. updateStats('message');
  847. updateStats('topic');
  848. updateSettings(array(
  849. 'calendar_updated' => time(),
  850. ));
  851. // Plus reset the cache to stop people getting odd results.
  852. updateSettings(array('settings_updated' => time()));
  853. // Clean the cache as well.
  854. clean_cache('data');
  855. // Let's do some serious logging.
  856. foreach ($boards_to_remove as $id_board)
  857. logAction('delete_board', array('boardname' => $boards[$id_board]['name']), 'admin');
  858. reorderBoards();
  859. }
  860. /**
  861. * Put all boards in the right order.
  862. */
  863. function reorderBoards()
  864. {
  865. global $cat_tree, $boardList, $boards, $smcFunc;
  866. getBoardTree();
  867. // Set the board order for each category.
  868. $board_order = 0;
  869. foreach ($cat_tree as $catID => $dummy)
  870. {
  871. foreach ($boardList[$catID] as $boardID)
  872. if ($boards[$boardID]['order'] != ++$board_order)
  873. $smcFunc['db_query']('', '
  874. UPDATE {db_prefix}boards
  875. SET board_order = {int:new_order}
  876. WHERE id_board = {int:selected_board}',
  877. array(
  878. 'new_order' => $board_order,
  879. 'selected_board' => $boardID,
  880. )
  881. );
  882. }
  883. // Sort the records of the boards table on the board_order value.
  884. $smcFunc['db_query']('alter_table_boards', '
  885. ALTER TABLE {db_prefix}boards
  886. ORDER BY board_order',
  887. array(
  888. 'db_error_skip' => true,
  889. )
  890. );
  891. }
  892. /**
  893. * Fixes the children of a board by setting their child_levels to new values.
  894. * @param int $parent
  895. * @param int $newLevel
  896. * @param int $newParent
  897. */
  898. function fixChildren($parent, $newLevel, $newParent)
  899. {
  900. global $smcFunc;
  901. // Grab all children of $parent...
  902. $result = $smcFunc['db_query']('', '
  903. SELECT id_board
  904. FROM {db_prefix}boards
  905. WHERE id_parent = {int:parent_board}',
  906. array(
  907. 'parent_board' => $parent,
  908. )
  909. );
  910. $children = array();
  911. while ($row = $smcFunc['db_fetch_assoc']($result))
  912. $children[] = $row['id_board'];
  913. $smcFunc['db_free_result']($result);
  914. // ...and set it to a new parent and child_level.
  915. $smcFunc['db_query']('', '
  916. UPDATE {db_prefix}boards
  917. SET id_parent = {int:new_parent}, child_level = {int:new_child_level}
  918. WHERE id_parent = {int:parent_board}',
  919. array(
  920. 'new_parent' => $newParent,
  921. 'new_child_level' => $newLevel,
  922. 'parent_board' => $parent,
  923. )
  924. );
  925. // Recursively fix the children of the children.
  926. foreach ($children as $child)
  927. fixChildren($child, $newLevel + 1, $child);
  928. }
  929. /**
  930. * Load a lot of useful information regarding the boards and categories.
  931. * The information retrieved is stored in globals:
  932. * $boards properties of each board.
  933. * $boardList a list of boards grouped by category ID.
  934. * $cat_tree properties of each category.
  935. */
  936. function getBoardTree()
  937. {
  938. global $cat_tree, $boards, $boardList, $txt, $modSettings, $smcFunc;
  939. // Getting all the board and category information you'd ever wanted.
  940. $request = $smcFunc['db_query']('', '
  941. SELECT
  942. IFNULL(b.id_board, 0) AS id_board, b.id_parent, b.name AS board_name, b.description, b.child_level,
  943. b.board_order, b.count_posts, b.member_groups, b.id_theme, b.override_theme, b.id_profile, b.redirect,
  944. b.num_posts, b.num_topics, b.deny_member_groups, c.id_cat, c.name AS cat_name, c.cat_order, c.can_collapse
  945. FROM {db_prefix}categories AS c
  946. LEFT JOIN {db_prefix}boards AS b ON (b.id_cat = c.id_cat)
  947. ORDER BY c.cat_order, b.child_level, b.board_order',
  948. array(
  949. )
  950. );
  951. $cat_tree = array();
  952. $boards = array();
  953. $last_board_order = 0;
  954. while ($row = $smcFunc['db_fetch_assoc']($request))
  955. {
  956. if (!isset($cat_tree[$row['id_cat']]))
  957. {
  958. $cat_tree[$row['id_cat']] = array(
  959. 'node' => array(
  960. 'id' => $row['id_cat'],
  961. 'name' => $row['cat_name'],
  962. 'order' => $row['cat_order'],
  963. 'can_collapse' => $row['can_collapse']
  964. ),
  965. 'is_first' => empty($cat_tree),
  966. 'last_board_order' => $last_board_order,
  967. 'children' => array()
  968. );
  969. $prevBoard = 0;
  970. $curLevel = 0;
  971. }
  972. if (!empty($row['id_board']))
  973. {
  974. if ($row['child_level'] != $curLevel)
  975. $prevBoard = 0;
  976. $boards[$row['id_board']] = array(
  977. 'id' => $row['id_board'],
  978. 'category' => $row['id_cat'],
  979. 'parent' => $row['id_parent'],
  980. 'level' => $row['child_level'],
  981. 'order' => $row['board_order'],
  982. 'name' => $row['board_name'],
  983. 'member_groups' => explode(',', $row['member_groups']),
  984. 'deny_groups' => explode(',', $row['deny_member_groups']),
  985. 'description' => $row['description'],
  986. 'count_posts' => empty($row['count_posts']),
  987. 'posts' => $row['num_posts'],
  988. 'topics' => $row['num_topics'],
  989. 'theme' => $row['id_theme'],
  990. 'override_theme' => $row['override_theme'],
  991. 'profile' => $row['id_profile'],
  992. 'redirect' => $row['redirect'],
  993. 'prev_board' => $prevBoard
  994. );
  995. $prevBoard = $row['id_board'];
  996. $last_board_order = $row['board_order'];
  997. if (empty($row['child_level']))
  998. {
  999. $cat_tree[$row['id_cat']]['children'][$row['id_board']] = array(
  1000. 'node' => &$boards[$row['id_board']],
  1001. 'is_first' => empty($cat_tree[$row['id_cat']]['children']),
  1002. 'children' => array()
  1003. );
  1004. $boards[$row['id_board']]['tree'] = &$cat_tree[$row['id_cat']]['children'][$row['id_board']];
  1005. }
  1006. else
  1007. {
  1008. // Parent doesn't exist!
  1009. if (!isset($boards[$row['id_parent']]['tree']))
  1010. fatal_lang_error('no_valid_parent', false, array($row['board_name']));
  1011. // Wrong childlevel...we can silently fix this...
  1012. if ($boards[$row['id_parent']]['tree']['node']['level'] != $row['child_level'] - 1)
  1013. $smcFunc['db_query']('', '
  1014. UPDATE {db_prefix}boards
  1015. SET child_level = {int:new_child_level}
  1016. WHERE id_board = {int:selected_board}',
  1017. array(
  1018. 'new_child_level' => $boards[$row['id_parent']]['tree']['node']['level'] + 1,
  1019. 'selected_board' => $row['id_board'],
  1020. )
  1021. );
  1022. $boards[$row['id_parent']]['tree']['children'][$row['id_board']] = array(
  1023. 'node' => &$boards[$row['id_board']],
  1024. 'is_first' => empty($boards[$row['id_parent']]['tree']['children']),
  1025. 'children' => array()
  1026. );
  1027. $boards[$row['id_board']]['tree'] = &$boards[$row['id_parent']]['tree']['children'][$row['id_board']];
  1028. }
  1029. }
  1030. }
  1031. $smcFunc['db_free_result']($request);
  1032. // Get a list of all the boards in each category (using recursion).
  1033. $boardList = array();
  1034. foreach ($cat_tree as $catID => $node)
  1035. {
  1036. $boardList[$catID] = array();
  1037. recursiveBoards($boardList[$catID], $node);
  1038. }
  1039. }
  1040. /**
  1041. * Recursively get a list of boards.
  1042. * @param array &$_boardList
  1043. * @param array &$_tree
  1044. */
  1045. function recursiveBoards(&$_boardList, &$_tree)
  1046. {
  1047. if (empty($_tree['children']))
  1048. return;
  1049. foreach ($_tree['children'] as $id => $node)
  1050. {
  1051. $_boardList[] = $id;
  1052. recursiveBoards($_boardList, $node);
  1053. }
  1054. }
  1055. /**
  1056. * Returns whether the child board id is actually a child of the parent (recursive).
  1057. * @param int $child
  1058. * @param int $parent
  1059. * @return bool
  1060. */
  1061. function isChildOf($child, $parent)
  1062. {
  1063. global $boards;
  1064. if (empty($boards[$child]['parent']))
  1065. return false;
  1066. if ($boards[$child]['parent'] == $parent)
  1067. return true;
  1068. return isChildOf($boards[$child]['parent'], $parent);
  1069. }
  1070. ?>