Subs-Boards.php 33 KB

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