Subs-Boards.php 34 KB

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