Subs-Boards.php 34 KB

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