Subs-Boards.php 35 KB

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