Subs-Boards.php 35 KB

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