Subs-Boards.php 33 KB

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