Subs-Boards.php 34 KB

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