The version of vichan running on lainchan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2340 lines
75KB

  1. <?php
  2. /*
  3. * Copyright (c) 2010-2013 Tinyboard Development Group
  4. */
  5. defined('TINYBOARD') or exit;
  6. function mod_page($title, $template, $args, $subtitle = false) {
  7. global $config, $mod;
  8. echo Element('page.html', array(
  9. 'config' => $config,
  10. 'mod' => $mod,
  11. 'hide_dashboard_link' => $template == 'mod/dashboard.html',
  12. 'title' => $title,
  13. 'subtitle' => $subtitle,
  14. 'body' => Element($template,
  15. array_merge(
  16. array('config' => $config, 'mod' => $mod),
  17. $args
  18. )
  19. )
  20. )
  21. );
  22. }
  23. function mod_login($redirect = false) {
  24. global $config;
  25. $args = array();
  26. if (isset($_POST['login'])) {
  27. // Check if inputs are set and not empty
  28. if (!isset($_POST['username'], $_POST['password']) || $_POST['username'] == '' || $_POST['password'] == '') {
  29. $args['error'] = $config['error']['invalid'];
  30. } elseif (!login($_POST['username'], $_POST['password'])) {
  31. if ($config['syslog'])
  32. _syslog(LOG_WARNING, 'Unauthorized login attempt!');
  33. $args['error'] = $config['error']['invalid'];
  34. } else {
  35. modLog('Logged in');
  36. // Login successful
  37. // Set cookies
  38. setCookies();
  39. if ($redirect)
  40. header('Location: ?' . $redirect, true, $config['redirect_http']);
  41. else
  42. header('Location: ?/', true, $config['redirect_http']);
  43. }
  44. }
  45. if (isset($_POST['username']))
  46. $args['username'] = $_POST['username'];
  47. mod_page(_('Login'), 'mod/login.html', $args);
  48. }
  49. function mod_confirm($request) {
  50. mod_page(_('Confirm action'), 'mod/confirm.html', array('request' => $request, 'token' => make_secure_link_token($request)));
  51. }
  52. function mod_logout() {
  53. global $config;
  54. destroyCookies();
  55. header('Location: ?/', true, $config['redirect_http']);
  56. }
  57. function mod_dashboard() {
  58. global $config, $mod;
  59. $args = array();
  60. $args['boards'] = listBoards();
  61. if (hasPermission($config['mod']['noticeboard'])) {
  62. if (!$config['cache']['enabled'] || !$args['noticeboard'] = cache::get('noticeboard_preview')) {
  63. $query = prepare("SELECT ``noticeboard``.*, `username` FROM ``noticeboard`` LEFT JOIN ``mods`` ON ``mods``.`id` = `mod` ORDER BY `id` DESC LIMIT :limit");
  64. $query->bindValue(':limit', $config['mod']['noticeboard_dashboard'], PDO::PARAM_INT);
  65. $query->execute() or error(db_error($query));
  66. $args['noticeboard'] = $query->fetchAll(PDO::FETCH_ASSOC);
  67. if ($config['cache']['enabled'])
  68. cache::set('noticeboard_preview', $args['noticeboard']);
  69. }
  70. }
  71. if (!$config['cache']['enabled'] || ($args['unread_pms'] = cache::get('pm_unreadcount_' . $mod['id'])) === false) {
  72. $query = prepare('SELECT COUNT(*) FROM ``pms`` WHERE `to` = :id AND `unread` = 1');
  73. $query->bindValue(':id', $mod['id']);
  74. $query->execute() or error(db_error($query));
  75. $args['unread_pms'] = $query->fetchColumn();
  76. if ($config['cache']['enabled'])
  77. cache::set('pm_unreadcount_' . $mod['id'], $args['unread_pms']);
  78. }
  79. $query = query('SELECT COUNT(*) FROM ``reports``') or error(db_error($query));
  80. $args['reports'] = $query->fetchColumn();
  81. if ($mod['type'] >= ADMIN && $config['check_updates']) {
  82. if (!$config['version'])
  83. error(_('Could not find current version! (Check .installed)'));
  84. if (isset($_COOKIE['update'])) {
  85. $latest = unserialize($_COOKIE['update']);
  86. } else {
  87. $ctx = stream_context_create(array('http' => array('timeout' => 5)));
  88. if ($code = @file_get_contents('http://tinyboard.org/version.txt', 0, $ctx)) {
  89. $ver = strtok($code, "\n");
  90. if (preg_match('@^// v(\d+)\.(\d+)\.(\d+)\s*?$@', $ver, $matches)) {
  91. $latest = array(
  92. 'massive' => $matches[1],
  93. 'major' => $matches[2],
  94. 'minor' => $matches[3]
  95. );
  96. if (preg_match('/v(\d+)\.(\d)\.(\d+)(-dev.+)?$/', $config['version'], $matches)) {
  97. $current = array(
  98. 'massive' => (int) $matches[1],
  99. 'major' => (int) $matches[2],
  100. 'minor' => (int) $matches[3]
  101. );
  102. if (isset($m[4])) {
  103. // Development versions are always ahead in the versioning numbers
  104. $current['minor'] --;
  105. }
  106. // Check if it's newer
  107. if (!( $latest['massive'] > $current['massive'] ||
  108. $latest['major'] > $current['major'] ||
  109. ($latest['massive'] == $current['massive'] &&
  110. $latest['major'] == $current['major'] &&
  111. $latest['minor'] > $current['minor']
  112. )))
  113. $latest = false;
  114. } else {
  115. $latest = false;
  116. }
  117. } else {
  118. // Couldn't get latest version
  119. $latest = false;
  120. }
  121. } else {
  122. // Couldn't get latest version
  123. $latest = false;
  124. }
  125. setcookie('update', serialize($latest), time() + $config['check_updates_time'], $config['cookies']['jail'] ? $config['cookies']['path'] : '/', null, false, true);
  126. }
  127. if ($latest)
  128. $args['newer_release'] = $latest;
  129. }
  130. mod_page(_('Dashboard'), 'mod/dashboard.html', $args);
  131. }
  132. function mod_search_redirect() {
  133. global $config;
  134. if (!hasPermission($config['mod']['search']))
  135. error($config['error']['noaccess']);
  136. if (isset($_POST['query'], $_POST['type']) && in_array($_POST['type'], array('posts', 'IP_notes', 'bans', 'log'))) {
  137. $query = $_POST['query'];
  138. $query = urlencode($query);
  139. $query = str_replace('_', '%5F', $query);
  140. $query = str_replace('+', '_', $query);
  141. if ($query === '') {
  142. header('Location: ?/', true, $config['redirect_http']);
  143. return;
  144. }
  145. header('Location: ?/search/' . $_POST['type'] . '/' . $query, true, $config['redirect_http']);
  146. } else {
  147. header('Location: ?/', true, $config['redirect_http']);
  148. }
  149. }
  150. function mod_search($type, $search_query_escaped, $page_no = 1) {
  151. global $pdo, $config;
  152. if (!hasPermission($config['mod']['search']))
  153. error($config['error']['noaccess']);
  154. // Unescape query
  155. $query = str_replace('_', ' ', $search_query_escaped);
  156. $query = urldecode($query);
  157. $search_query = $query;
  158. // Form a series of LIKE clauses for the query.
  159. // This gets a little complicated.
  160. // Escape "escape" character
  161. $query = str_replace('!', '!!', $query);
  162. // Escape SQL wildcard
  163. $query = str_replace('%', '!%', $query);
  164. // Use asterisk as wildcard instead
  165. $query = str_replace('*', '%', $query);
  166. $query = str_replace('`', '!`', $query);
  167. // Array of phrases to match
  168. $match = array();
  169. // Exact phrases ("like this")
  170. if (preg_match_all('/"(.+?)"/', $query, $exact_phrases)) {
  171. $exact_phrases = $exact_phrases[1];
  172. foreach ($exact_phrases as $phrase) {
  173. $query = str_replace("\"{$phrase}\"", '', $query);
  174. $match[] = $pdo->quote($phrase);
  175. }
  176. }
  177. // Non-exact phrases (ie. plain keywords)
  178. $keywords = explode(' ', $query);
  179. foreach ($keywords as $word) {
  180. if (empty($word))
  181. continue;
  182. $match[] = $pdo->quote($word);
  183. }
  184. // Which `field` to search?
  185. if ($type == 'posts')
  186. $sql_field = array('body_nomarkup', 'filename', 'subject', 'filehash', 'ip', 'name', 'trip');
  187. if ($type == 'IP_notes')
  188. $sql_field = 'body';
  189. if ($type == 'bans')
  190. $sql_field = 'reason';
  191. if ($type == 'log')
  192. $sql_field = 'text';
  193. // Build the "LIKE 'this' AND LIKE 'that'" etc. part of the SQL query
  194. $sql_like = '';
  195. foreach ($match as $phrase) {
  196. if (!empty($sql_like))
  197. $sql_like .= ' AND ';
  198. $phrase = preg_replace('/^\'(.+)\'$/', '\'%$1%\'', $phrase);
  199. if (is_array($sql_field)) {
  200. foreach ($sql_field as $field) {
  201. $sql_like .= '`' . $field . '` LIKE ' . $phrase . ' ESCAPE \'!\' OR';
  202. }
  203. $sql_like = preg_replace('/ OR$/', '', $sql_like);
  204. } else {
  205. $sql_like .= '`' . $sql_field . '` LIKE ' . $phrase . ' ESCAPE \'!\'';
  206. }
  207. }
  208. // Compile SQL query
  209. if ($type == 'posts') {
  210. $query = '';
  211. $boards = listBoards();
  212. if (empty($boards))
  213. error(_('There are no boards to search!'));
  214. foreach ($boards as $board) {
  215. openBoard($board['uri']);
  216. if (!hasPermission($config['mod']['search_posts'], $board['uri']))
  217. continue;
  218. if (!empty($query))
  219. $query .= ' UNION ALL ';
  220. $query .= sprintf("SELECT *, '%s' AS `board` FROM ``posts_%s`` WHERE %s", $board['uri'], $board['uri'], $sql_like);
  221. }
  222. // You weren't allowed to search any boards
  223. if (empty($query))
  224. error($config['error']['noaccess']);
  225. $query .= ' ORDER BY `sticky` DESC, `id` DESC';
  226. }
  227. if ($type == 'IP_notes') {
  228. $query = 'SELECT * FROM ``ip_notes`` LEFT JOIN ``mods`` ON `mod` = ``mods``.`id` WHERE ' . $sql_like . ' ORDER BY `time` DESC';
  229. $sql_table = 'ip_notes';
  230. if (!hasPermission($config['mod']['view_notes']) || !hasPermission($config['mod']['show_ip']))
  231. error($config['error']['noaccess']);
  232. }
  233. if ($type == 'bans') {
  234. $query = 'SELECT ``bans``.*, `username` FROM ``bans`` LEFT JOIN ``mods`` ON `mod` = ``mods``.`id` WHERE ' . $sql_like . ' ORDER BY (`expires` IS NOT NULL AND `expires` < UNIX_TIMESTAMP()), `set` DESC';
  235. $sql_table = 'bans';
  236. if (!hasPermission($config['mod']['view_banlist']))
  237. error($config['error']['noaccess']);
  238. }
  239. if ($type == 'log') {
  240. $query = 'SELECT `username`, `mod`, `ip`, `board`, `time`, `text` FROM ``modlogs`` LEFT JOIN ``mods`` ON `mod` = ``mods``.`id` WHERE ' . $sql_like . ' ORDER BY `time` DESC';
  241. $sql_table = 'modlogs';
  242. if (!hasPermission($config['mod']['modlog']))
  243. error($config['error']['noaccess']);
  244. }
  245. // Execute SQL query (with pages)
  246. $q = query($query . ' LIMIT ' . (($page_no - 1) * $config['mod']['search_page']) . ', ' . $config['mod']['search_page']) or error(db_error());
  247. $results = $q->fetchAll(PDO::FETCH_ASSOC);
  248. // Get total result count
  249. if ($type == 'posts') {
  250. $q = query("SELECT COUNT(*) FROM ($query) AS `tmp_table`") or error(db_error());
  251. $result_count = $q->fetchColumn();
  252. } else {
  253. $q = query('SELECT COUNT(*) FROM `' . $sql_table . '` WHERE ' . $sql_like) or error(db_error());
  254. $result_count = $q->fetchColumn();
  255. }
  256. if ($type == 'bans') {
  257. foreach ($results as &$ban) {
  258. if (filter_var($ban['ip'], FILTER_VALIDATE_IP) !== false)
  259. $ban['real_ip'] = true;
  260. }
  261. }
  262. if ($type == 'posts') {
  263. foreach ($results as &$post) {
  264. $post['snippet'] = pm_snippet($post['body']);
  265. }
  266. }
  267. // $results now contains the search results
  268. mod_page(_('Search results'), 'mod/search_results.html', array(
  269. 'search_type' => $type,
  270. 'search_query' => $search_query,
  271. 'search_query_escaped' => $search_query_escaped,
  272. 'result_count' => $result_count,
  273. 'results' => $results
  274. ));
  275. }
  276. function mod_edit_board($boardName) {
  277. global $board, $config;
  278. if (!openBoard($boardName))
  279. error($config['error']['noboard']);
  280. if (!hasPermission($config['mod']['manageboards'], $board['uri']))
  281. error($config['error']['noaccess']);
  282. if (isset($_POST['title'], $_POST['subtitle'])) {
  283. if (isset($_POST['delete'])) {
  284. if (!hasPermission($config['mod']['manageboards'], $board['uri']))
  285. error($config['error']['deleteboard']);
  286. $query = prepare('DELETE FROM ``boards`` WHERE `uri` = :uri');
  287. $query->bindValue(':uri', $board['uri']);
  288. $query->execute() or error(db_error($query));
  289. if ($config['cache']['enabled']) {
  290. cache::delete('board_' . $board['uri']);
  291. cache::delete('all_boards');
  292. }
  293. modLog('Deleted board: ' . sprintf($config['board_abbreviation'], $board['uri']), false);
  294. // Delete posting table
  295. $query = query(sprintf('DROP TABLE IF EXISTS ``posts_%s``', $board['uri'])) or error(db_error());
  296. // Clear reports
  297. $query = prepare('DELETE FROM ``reports`` WHERE `board` = :id');
  298. $query->bindValue(':id', $board['uri'], PDO::PARAM_INT);
  299. $query->execute() or error(db_error($query));
  300. // Delete from table
  301. $query = prepare('DELETE FROM ``boards`` WHERE `uri` = :uri');
  302. $query->bindValue(':uri', $board['uri'], PDO::PARAM_INT);
  303. $query->execute() or error(db_error($query));
  304. $query = prepare("SELECT `board`, `post` FROM ``cites`` WHERE `target_board` = :board ORDER BY `board`");
  305. $query->bindValue(':board', $board['uri']);
  306. $query->execute() or error(db_error($query));
  307. while ($cite = $query->fetch(PDO::FETCH_ASSOC)) {
  308. if ($board['uri'] != $cite['board']) {
  309. if (!isset($tmp_board))
  310. $tmp_board = $board;
  311. openBoard($cite['board']);
  312. rebuildPost($cite['post']);
  313. }
  314. }
  315. if (isset($tmp_board))
  316. $board = $tmp_board;
  317. $query = prepare('DELETE FROM ``cites`` WHERE `board` = :board OR `target_board` = :board');
  318. $query->bindValue(':board', $board['uri']);
  319. $query->execute() or error(db_error($query));
  320. $query = prepare('DELETE FROM ``antispam`` WHERE `board` = :board');
  321. $query->bindValue(':board', $board['uri']);
  322. $query->execute() or error(db_error($query));
  323. // Remove board from users/permissions table
  324. $query = query('SELECT `id`,`boards` FROM ``mods``') or error(db_error());
  325. while ($user = $query->fetch(PDO::FETCH_ASSOC)) {
  326. $user_boards = explode(',', $user['boards']);
  327. if (in_array($board['uri'], $user_boards)) {
  328. unset($user_boards[array_search($board['uri'], $user_boards)]);
  329. $_query = prepare('UPDATE ``mods`` SET `boards` = :boards WHERE `id` = :id');
  330. $_query->bindValue(':boards', implode(',', $user_boards));
  331. $_query->bindValue(':id', $user['id']);
  332. $_query->execute() or error(db_error($_query));
  333. }
  334. }
  335. // Delete entire board directory
  336. rrmdir($board['uri'] . '/');
  337. } else {
  338. $query = prepare('UPDATE ``boards`` SET `title` = :title, `subtitle` = :subtitle WHERE `uri` = :uri');
  339. $query->bindValue(':uri', $board['uri']);
  340. $query->bindValue(':title', $_POST['title']);
  341. $query->bindValue(':subtitle', $_POST['subtitle']);
  342. $query->execute() or error(db_error($query));
  343. modLog('Edited board information for ' . sprintf($config['board_abbreviation'], $board['uri']), false);
  344. }
  345. if ($config['cache']['enabled']) {
  346. cache::delete('board_' . $board['uri']);
  347. cache::delete('all_boards');
  348. }
  349. rebuildThemes('boards');
  350. header('Location: ?/', true, $config['redirect_http']);
  351. } else {
  352. mod_page(sprintf('%s: ' . $config['board_abbreviation'], _('Edit board'), $board['uri']), 'mod/board.html', array('board' => $board));
  353. }
  354. }
  355. function mod_new_board() {
  356. global $config, $board;
  357. if (!hasPermission($config['mod']['newboard']))
  358. error($config['error']['noaccess']);
  359. if (isset($_POST['uri'], $_POST['title'], $_POST['subtitle'])) {
  360. if ($_POST['uri'] == '')
  361. error(sprintf($config['error']['required'], 'URI'));
  362. if ($_POST['title'] == '')
  363. error(sprintf($config['error']['required'], 'title'));
  364. if (!preg_match('/^' . $config['board_regex'] . '$/u', $_POST['uri']))
  365. error(sprintf($config['error']['invalidfield'], 'URI'));
  366. $bytes = 0;
  367. $chars = preg_split('//u', $_POST['uri'], -1, PREG_SPLIT_NO_EMPTY);
  368. foreach ($chars as $char) {
  369. $o = 0;
  370. $ord = ordutf8($char, $o);
  371. if ($ord > 0x0080)
  372. $bytes += 5; // @01ff
  373. else
  374. $bytes ++;
  375. }
  376. $bytes + strlen('posts_.frm');
  377. if ($bytes > 255) {
  378. error('Your filesystem cannot handle a board URI of that length (' . $bytes . '/255 bytes)');
  379. exit;
  380. }
  381. if (openBoard($_POST['uri'])) {
  382. error(sprintf($config['error']['boardexists'], $board['url']));
  383. }
  384. $query = prepare('INSERT INTO ``boards`` VALUES (:uri, :title, :subtitle)');
  385. $query->bindValue(':uri', $_POST['uri']);
  386. $query->bindValue(':title', $_POST['title']);
  387. $query->bindValue(':subtitle', $_POST['subtitle']);
  388. $query->execute() or error(db_error($query));
  389. modLog('Created a new board: ' . sprintf($config['board_abbreviation'], $_POST['uri']));
  390. if (!openBoard($_POST['uri']))
  391. error(_("Couldn't open board after creation."));
  392. $query = Element('posts.sql', array('board' => $board['uri']));
  393. if (mysql_version() < 50503)
  394. $query = preg_replace('/(CHARSET=|CHARACTER SET )utf8mb4/', '$1utf8', $query);
  395. query($query) or error(db_error());
  396. if ($config['cache']['enabled'])
  397. cache::delete('all_boards');
  398. // Build the board
  399. buildIndex();
  400. rebuildThemes('boards');
  401. header('Location: ?/' . $board['uri'] . '/' . $config['file_index'], true, $config['redirect_http']);
  402. }
  403. mod_page(_('New board'), 'mod/board.html', array('new' => true));
  404. }
  405. function mod_noticeboard($page_no = 1) {
  406. global $config, $pdo, $mod;
  407. if ($page_no < 1)
  408. error($config['error']['404']);
  409. if (!hasPermission($config['mod']['noticeboard']))
  410. error($config['error']['noaccess']);
  411. if (isset($_POST['subject'], $_POST['body'])) {
  412. if (!hasPermission($config['mod']['noticeboard_post']))
  413. error($config['error']['noaccess']);
  414. $_POST['body'] = escape_markup_modifiers($_POST['body']);
  415. markup($_POST['body']);
  416. $query = prepare('INSERT INTO ``noticeboard`` VALUES (NULL, :mod, :time, :subject, :body)');
  417. $query->bindValue(':mod', $mod['id']);
  418. $query->bindvalue(':time', time());
  419. $query->bindValue(':subject', $_POST['subject']);
  420. $query->bindValue(':body', $_POST['body']);
  421. $query->execute() or error(db_error($query));
  422. if ($config['cache']['enabled'])
  423. cache::delete('noticeboard_preview');
  424. modLog('Posted a noticeboard entry');
  425. header('Location: ?/noticeboard#' . $pdo->lastInsertId(), true, $config['redirect_http']);
  426. }
  427. $query = prepare("SELECT ``noticeboard``.*, `username` FROM ``noticeboard`` LEFT JOIN ``mods`` ON ``mods``.`id` = `mod` ORDER BY `id` DESC LIMIT :offset, :limit");
  428. $query->bindValue(':limit', $config['mod']['noticeboard_page'], PDO::PARAM_INT);
  429. $query->bindValue(':offset', ($page_no - 1) * $config['mod']['noticeboard_page'], PDO::PARAM_INT);
  430. $query->execute() or error(db_error($query));
  431. $noticeboard = $query->fetchAll(PDO::FETCH_ASSOC);
  432. if (empty($noticeboard) && $page_no > 1)
  433. error($config['error']['404']);
  434. $query = prepare("SELECT COUNT(*) FROM ``noticeboard``");
  435. $query->execute() or error(db_error($query));
  436. $count = $query->fetchColumn();
  437. mod_page(_('Noticeboard'), 'mod/noticeboard.html', array('noticeboard' => $noticeboard, 'count' => $count));
  438. }
  439. function mod_noticeboard_delete($id) {
  440. global $config;
  441. if (!hasPermission($config['mod']['noticeboard_delete']))
  442. error($config['error']['noaccess']);
  443. $query = prepare('DELETE FROM ``noticeboard`` WHERE `id` = :id');
  444. $query->bindValue(':id', $id);
  445. $query->execute() or error(db_error($query));
  446. modLog('Deleted a noticeboard entry');
  447. if ($config['cache']['enabled'])
  448. cache::delete('noticeboard_preview');
  449. header('Location: ?/noticeboard', true, $config['redirect_http']);
  450. }
  451. function mod_news($page_no = 1) {
  452. global $config, $pdo, $mod;
  453. if ($page_no < 1)
  454. error($config['error']['404']);
  455. if (isset($_POST['subject'], $_POST['body'])) {
  456. if (!hasPermission($config['mod']['news']))
  457. error($config['error']['noaccess']);
  458. $_POST['body'] = escape_markup_modifiers($_POST['body']);
  459. markup($_POST['body']);
  460. $query = prepare('INSERT INTO ``news`` VALUES (NULL, :name, :time, :subject, :body)');
  461. $query->bindValue(':name', isset($_POST['name']) && hasPermission($config['mod']['news_custom']) ? $_POST['name'] : $mod['username']);
  462. $query->bindvalue(':time', time());
  463. $query->bindValue(':subject', $_POST['subject']);
  464. $query->bindValue(':body', $_POST['body']);
  465. $query->execute() or error(db_error($query));
  466. modLog('Posted a news entry');
  467. rebuildThemes('news');
  468. header('Location: ?/news#' . $pdo->lastInsertId(), true, $config['redirect_http']);
  469. }
  470. $query = prepare("SELECT * FROM ``news`` ORDER BY `id` DESC LIMIT :offset, :limit");
  471. $query->bindValue(':limit', $config['mod']['news_page'], PDO::PARAM_INT);
  472. $query->bindValue(':offset', ($page_no - 1) * $config['mod']['news_page'], PDO::PARAM_INT);
  473. $query->execute() or error(db_error($query));
  474. $news = $query->fetchAll(PDO::FETCH_ASSOC);
  475. if (empty($news) && $page_no > 1)
  476. error($config['error']['404']);
  477. $query = prepare("SELECT COUNT(*) FROM ``news``");
  478. $query->execute() or error(db_error($query));
  479. $count = $query->fetchColumn();
  480. mod_page(_('News'), 'mod/news.html', array('news' => $news, 'count' => $count));
  481. }
  482. function mod_news_delete($id) {
  483. global $config;
  484. if (!hasPermission($config['mod']['news_delete']))
  485. error($config['error']['noaccess']);
  486. $query = prepare('DELETE FROM ``news`` WHERE `id` = :id');
  487. $query->bindValue(':id', $id);
  488. $query->execute() or error(db_error($query));
  489. modLog('Deleted a news entry');
  490. header('Location: ?/news', true, $config['redirect_http']);
  491. }
  492. function mod_log($page_no = 1) {
  493. global $config;
  494. if ($page_no < 1)
  495. error($config['error']['404']);
  496. if (!hasPermission($config['mod']['modlog']))
  497. error($config['error']['noaccess']);
  498. $query = prepare("SELECT `username`, `mod`, `ip`, `board`, `time`, `text` FROM ``modlogs`` LEFT JOIN ``mods`` ON `mod` = ``mods``.`id` ORDER BY `time` DESC LIMIT :offset, :limit");
  499. $query->bindValue(':limit', $config['mod']['modlog_page'], PDO::PARAM_INT);
  500. $query->bindValue(':offset', ($page_no - 1) * $config['mod']['modlog_page'], PDO::PARAM_INT);
  501. $query->execute() or error(db_error($query));
  502. $logs = $query->fetchAll(PDO::FETCH_ASSOC);
  503. if (empty($logs) && $page_no > 1)
  504. error($config['error']['404']);
  505. $query = prepare("SELECT COUNT(*) FROM ``modlogs``");
  506. $query->execute() or error(db_error($query));
  507. $count = $query->fetchColumn();
  508. mod_page(_('Moderation log'), 'mod/log.html', array('logs' => $logs, 'count' => $count));
  509. }
  510. function mod_user_log($username, $page_no = 1) {
  511. global $config;
  512. if ($page_no < 1)
  513. error($config['error']['404']);
  514. if (!hasPermission($config['mod']['modlog']))
  515. error($config['error']['noaccess']);
  516. $query = prepare("SELECT `username`, `mod`, `ip`, `board`, `time`, `text` FROM ``modlogs`` LEFT JOIN ``mods`` ON `mod` = ``mods``.`id` WHERE `username` = :username ORDER BY `time` DESC LIMIT :offset, :limit");
  517. $query->bindValue(':username', $username);
  518. $query->bindValue(':limit', $config['mod']['modlog_page'], PDO::PARAM_INT);
  519. $query->bindValue(':offset', ($page_no - 1) * $config['mod']['modlog_page'], PDO::PARAM_INT);
  520. $query->execute() or error(db_error($query));
  521. $logs = $query->fetchAll(PDO::FETCH_ASSOC);
  522. if (empty($logs) && $page_no > 1)
  523. error($config['error']['404']);
  524. $query = prepare("SELECT COUNT(*) FROM ``modlogs`` LEFT JOIN ``mods`` ON `mod` = ``mods``.`id` WHERE `username` = :username");
  525. $query->bindValue(':username', $username);
  526. $query->execute() or error(db_error($query));
  527. $count = $query->fetchColumn();
  528. mod_page(_('Moderation log'), 'mod/log.html', array('logs' => $logs, 'count' => $count, 'username' => $username));
  529. }
  530. function mod_view_board($boardName, $page_no = 1) {
  531. global $config, $mod;
  532. if (!openBoard($boardName))
  533. error($config['error']['noboard']);
  534. if (!$page = index($page_no, $mod)) {
  535. error($config['error']['404']);
  536. }
  537. $page['pages'] = getPages(true);
  538. $page['pages'][$page_no-1]['selected'] = true;
  539. $page['btn'] = getPageButtons($page['pages'], true);
  540. $page['mod'] = true;
  541. $page['config'] = $config;
  542. echo Element('index.html', $page);
  543. }
  544. function mod_view_thread($boardName, $thread) {
  545. global $config, $mod;
  546. if (!openBoard($boardName))
  547. error($config['error']['noboard']);
  548. $page = buildThread($thread, true, $mod);
  549. echo $page;
  550. }
  551. function mod_ip_remove_note($ip, $id) {
  552. global $config, $mod;
  553. if (!hasPermission($config['mod']['remove_notes']))
  554. error($config['error']['noaccess']);
  555. if (filter_var($ip, FILTER_VALIDATE_IP) === false)
  556. error("Invalid IP address.");
  557. $query = prepare('DELETE FROM ``ip_notes`` WHERE `ip` = :ip AND `id` = :id');
  558. $query->bindValue(':ip', $ip);
  559. $query->bindValue(':id', $id);
  560. $query->execute() or error(db_error($query));
  561. modLog("Removed a note for <a href=\"?/IP/{$ip}\">{$ip}</a>");
  562. header('Location: ?/IP/' . $ip . '#notes', true, $config['redirect_http']);
  563. }
  564. function mod_page_ip($ip) {
  565. global $config, $mod;
  566. if (filter_var($ip, FILTER_VALIDATE_IP) === false)
  567. error("Invalid IP address.");
  568. if (isset($_POST['ban_id'], $_POST['unban'])) {
  569. if (!hasPermission($config['mod']['unban']))
  570. error($config['error']['noaccess']);
  571. require_once 'inc/mod/ban.php';
  572. unban($_POST['ban_id']);
  573. header('Location: ?/IP/' . $ip . '#bans', true, $config['redirect_http']);
  574. return;
  575. }
  576. if (isset($_POST['note'])) {
  577. if (!hasPermission($config['mod']['create_notes']))
  578. error($config['error']['noaccess']);
  579. $_POST['note'] = escape_markup_modifiers($_POST['note']);
  580. markup($_POST['note']);
  581. $query = prepare('INSERT INTO ``ip_notes`` VALUES (NULL, :ip, :mod, :time, :body)');
  582. $query->bindValue(':ip', $ip);
  583. $query->bindValue(':mod', $mod['id']);
  584. $query->bindValue(':time', time());
  585. $query->bindValue(':body', $_POST['note']);
  586. $query->execute() or error(db_error($query));
  587. modLog("Added a note for <a href=\"?/IP/{$ip}\">{$ip}</a>");
  588. header('Location: ?/IP/' . $ip . '#notes', true, $config['redirect_http']);
  589. return;
  590. }
  591. $args = array();
  592. $args['ip'] = $ip;
  593. $args['posts'] = array();
  594. if ($config['mod']['dns_lookup'])
  595. $args['hostname'] = rDNS($ip);
  596. $boards = listBoards();
  597. foreach ($boards as $board) {
  598. openBoard($board['uri']);
  599. if (!hasPermission($config['mod']['show_ip'], $board['uri']))
  600. continue;
  601. $query = prepare(sprintf('SELECT * FROM ``posts_%s`` WHERE `ip` = :ip ORDER BY `sticky` DESC, `id` DESC LIMIT :limit', $board['uri']));
  602. $query->bindValue(':ip', $ip);
  603. $query->bindValue(':limit', $config['mod']['ip_recentposts'], PDO::PARAM_INT);
  604. $query->execute() or error(db_error($query));
  605. while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
  606. if (!$post['thread']) {
  607. $po = new Thread($post, '?/', $mod, false);
  608. } else {
  609. $po = new Post($post, '?/', $mod);
  610. }
  611. if (!isset($args['posts'][$board['uri']]))
  612. $args['posts'][$board['uri']] = array('board' => $board, 'posts' => array());
  613. $args['posts'][$board['uri']]['posts'][] = $po->build(true);
  614. }
  615. }
  616. $args['boards'] = $boards;
  617. $args['token'] = make_secure_link_token('ban');
  618. if (hasPermission($config['mod']['view_ban'])) {
  619. $query = prepare("SELECT ``bans``.*, `username` FROM ``bans`` LEFT JOIN ``mods`` ON `mod` = ``mods``.`id` WHERE `ip` = :ip ORDER BY `set` DESC");
  620. $query->bindValue(':ip', $ip);
  621. $query->execute() or error(db_error($query));
  622. $args['bans'] = $query->fetchAll(PDO::FETCH_ASSOC);
  623. }
  624. if (hasPermission($config['mod']['view_notes'])) {
  625. $query = prepare("SELECT ``ip_notes``.*, `username` FROM ``ip_notes`` LEFT JOIN ``mods`` ON `mod` = ``mods``.`id` WHERE `ip` = :ip ORDER BY `time` DESC");
  626. $query->bindValue(':ip', $ip);
  627. $query->execute() or error(db_error($query));
  628. $args['notes'] = $query->fetchAll(PDO::FETCH_ASSOC);
  629. }
  630. if (hasPermission($config['mod']['modlog_ip'])) {
  631. $query = prepare("SELECT `username`, `mod`, `ip`, `board`, `time`, `text` FROM ``modlogs`` LEFT JOIN ``mods`` ON `mod` = ``mods``.`id` WHERE `text` LIKE :search ORDER BY `time` DESC LIMIT 50");
  632. $query->bindValue(':search', '%' . $ip . '%');
  633. $query->execute() or error(db_error($query));
  634. $args['logs'] = $query->fetchAll(PDO::FETCH_ASSOC);
  635. } else {
  636. $args['logs'] = array();
  637. }
  638. mod_page(sprintf('%s: %s', _('IP'), $ip), 'mod/view_ip.html', $args, $args['hostname']);
  639. }
  640. function mod_ban() {
  641. global $config;
  642. if (!hasPermission($config['mod']['ban']))
  643. error($config['error']['noaccess']);
  644. if (!isset($_POST['ip'], $_POST['reason'], $_POST['length'], $_POST['board'])) {
  645. mod_page(_('New ban'), 'mod/ban_form.html', array('token' => make_secure_link_token('ban')));
  646. return;
  647. }
  648. require_once 'inc/mod/ban.php';
  649. ban($_POST['ip'], $_POST['reason'], parse_time($_POST['length']), $_POST['board'] == '*' ? false : $_POST['board']);
  650. if (isset($_POST['redirect']))
  651. header('Location: ' . $_POST['redirect'], true, $config['redirect_http']);
  652. else
  653. header('Location: ?/', true, $config['redirect_http']);
  654. }
  655. function mod_bans($page_no = 1) {
  656. global $config;
  657. if ($page_no < 1)
  658. error($config['error']['404']);
  659. if (!hasPermission($config['mod']['view_banlist']))
  660. error($config['error']['noaccess']);
  661. if (isset($_POST['unban'])) {
  662. if (!hasPermission($config['mod']['unban']))
  663. error($config['error']['noaccess']);
  664. $unban = array();
  665. foreach ($_POST as $name => $unused) {
  666. if (preg_match('/^ban_(\d+)$/', $name, $match))
  667. $unban[] = $match[1];
  668. }
  669. if (isset($config['mod']['unban_limit'])){
  670. if (count($unban) <= $config['mod']['unban_limit'] || $config['mod']['unban_limit'] == -1){
  671. if (!empty($unban)) {
  672. query('DELETE FROM ``bans`` WHERE `id` = ' . implode(' OR `id` = ', $unban)) or error(db_error());
  673. foreach ($unban as $id) {
  674. modLog("Removed ban #{$id}");
  675. }
  676. }
  677. } else {
  678. error(sprintf($config['error']['toomanyunban'], $config['mod']['unban_limit'], count($unban) ));
  679. }
  680. } else {
  681. if (!empty($unban)) {
  682. query('DELETE FROM ``bans`` WHERE `id` = ' . implode(' OR `id` = ', $unban)) or error(db_error());
  683. foreach ($unban as $id) {
  684. modLog("Removed ban #{$id}");
  685. }
  686. }
  687. }
  688. header('Location: ?/bans', true, $config['redirect_http']);
  689. }
  690. if ($config['mod']['view_banexpired']) {
  691. $query = prepare("SELECT ``bans``.*, `username` FROM ``bans`` LEFT JOIN ``mods`` ON `mod` = ``mods``.`id` ORDER BY (`expires` IS NOT NULL AND `expires` < :time), `set` DESC LIMIT :offset, :limit");
  692. } else {
  693. // Filter out expired bans
  694. $query = prepare("SELECT ``bans``.*, `username` FROM ``bans`` INNER JOIN ``mods`` ON `mod` = ``mods``.`id` WHERE `expires` = 0 OR `expires` > :time ORDER BY `set` DESC LIMIT :offset, :limit");
  695. }
  696. $query->bindValue(':time', time(), PDO::PARAM_INT);
  697. $query->bindValue(':limit', $config['mod']['banlist_page'], PDO::PARAM_INT);
  698. $query->bindValue(':offset', ($page_no - 1) * $config['mod']['banlist_page'], PDO::PARAM_INT);
  699. $query->execute() or error(db_error($query));
  700. $bans = $query->fetchAll(PDO::FETCH_ASSOC);
  701. if (empty($bans) && $page_no > 1)
  702. error($config['error']['404']);
  703. $query = prepare("SELECT COUNT(*) FROM ``bans``");
  704. $query->execute() or error(db_error($query));
  705. $count = $query->fetchColumn();
  706. foreach ($bans as &$ban) {
  707. if (filter_var($ban['ip'], FILTER_VALIDATE_IP) !== false)
  708. $ban['real_ip'] = true;
  709. }
  710. mod_page(_('Ban list'), 'mod/ban_list.html', array('bans' => $bans, 'count' => $count));
  711. }
  712. function mod_lock($board, $unlock, $post) {
  713. global $config;
  714. if (!openBoard($board))
  715. error($config['error']['noboard']);
  716. if (!hasPermission($config['mod']['lock'], $board))
  717. error($config['error']['noaccess']);
  718. $query = prepare(sprintf('UPDATE ``posts_%s`` SET `locked` = :locked WHERE `id` = :id AND `thread` IS NULL', $board));
  719. $query->bindValue(':id', $post);
  720. $query->bindValue(':locked', $unlock ? 0 : 1);
  721. $query->execute() or error(db_error($query));
  722. if ($query->rowCount()) {
  723. modLog(($unlock ? 'Unlocked' : 'Locked') . " thread #{$post}");
  724. buildThread($post);
  725. buildIndex();
  726. }
  727. if ($config['mod']['dismiss_reports_on_lock']) {
  728. $query = prepare('DELETE FROM ``reports`` WHERE `board` = :board AND `post` = :id');
  729. $query->bindValue(':board', $board);
  730. $query->bindValue(':id', $post);
  731. $query->execute() or error(db_error($query));
  732. }
  733. header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']);
  734. if ($unlock)
  735. event('unlock', $post);
  736. else
  737. event('lock', $post);
  738. }
  739. function mod_sticky($board, $unsticky, $post) {
  740. global $config;
  741. if (!openBoard($board))
  742. error($config['error']['noboard']);
  743. if (!hasPermission($config['mod']['sticky'], $board))
  744. error($config['error']['noaccess']);
  745. $query = prepare(sprintf('UPDATE ``posts_%s`` SET `sticky` = :sticky WHERE `id` = :id AND `thread` IS NULL', $board));
  746. $query->bindValue(':id', $post);
  747. $query->bindValue(':sticky', $unsticky ? 0 : 1);
  748. $query->execute() or error(db_error($query));
  749. if ($query->rowCount()) {
  750. modLog(($unsticky ? 'Unstickied' : 'Stickied') . " thread #{$post}");
  751. buildThread($post);
  752. buildIndex();
  753. }
  754. header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']);
  755. }
  756. function mod_bumplock($board, $unbumplock, $post) {
  757. global $config;
  758. if (!openBoard($board))
  759. error($config['error']['noboard']);
  760. if (!hasPermission($config['mod']['bumplock'], $board))
  761. error($config['error']['noaccess']);
  762. $query = prepare(sprintf('UPDATE ``posts_%s`` SET `sage` = :bumplock WHERE `id` = :id AND `thread` IS NULL', $board));
  763. $query->bindValue(':id', $post);
  764. $query->bindValue(':bumplock', $unbumplock ? 0 : 1);
  765. $query->execute() or error(db_error($query));
  766. if ($query->rowCount()) {
  767. modLog(($unbumplock ? 'Unbumplocked' : 'Bumplocked') . " thread #{$post}");
  768. buildThread($post);
  769. buildIndex();
  770. }
  771. header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']);
  772. }
  773. function mod_move($originBoard, $postID) {
  774. global $board, $config, $mod, $pdo;
  775. if (!openBoard($originBoard))
  776. error($config['error']['noboard']);
  777. if (!hasPermission($config['mod']['move'], $originBoard))
  778. error($config['error']['noaccess']);
  779. $query = prepare(sprintf('SELECT * FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL', $originBoard));
  780. $query->bindValue(':id', $postID);
  781. $query->execute() or error(db_error($query));
  782. if (!$post = $query->fetch(PDO::FETCH_ASSOC))
  783. error($config['error']['404']);
  784. if (isset($_POST['board'])) {
  785. $targetBoard = $_POST['board'];
  786. $shadow = isset($_POST['shadow']);
  787. if ($targetBoard === $originBoard)
  788. error(_('Target and source board are the same.'));
  789. // copy() if leaving a shadow thread behind; else, rename().
  790. $clone = $shadow ? 'copy' : 'rename';
  791. // indicate that the post is a thread
  792. $post['op'] = true;
  793. if ($post['file']) {
  794. $post['has_file'] = true;
  795. $post['width'] = &$post['filewidth'];
  796. $post['height'] = &$post['fileheight'];
  797. $file_src = sprintf($config['board_path'], $board['uri']) . $config['dir']['img'] . $post['file'];
  798. $file_thumb = sprintf($config['board_path'], $board['uri']) . $config['dir']['thumb'] . $post['thumb'];
  799. } else {
  800. $post['has_file'] = false;
  801. }
  802. // allow thread to keep its same traits (stickied, locked, etc.)
  803. $post['mod'] = true;
  804. if (!openBoard($targetBoard))
  805. error($config['error']['noboard']);
  806. // create the new thread
  807. $newID = post($post);
  808. if ($post['has_file']) {
  809. // copy image
  810. $clone($file_src, sprintf($config['board_path'], $board['uri']) . $config['dir']['img'] . $post['file']);
  811. if (!in_array($post['thumb'], array('spoiler', 'deleted', 'file')))
  812. $clone($file_thumb, sprintf($config['board_path'], $board['uri']) . $config['dir']['thumb'] . $post['thumb']);
  813. }
  814. // go back to the original board to fetch replies
  815. openBoard($originBoard);
  816. $query = prepare(sprintf('SELECT * FROM ``posts_%s`` WHERE `thread` = :id ORDER BY `id`', $originBoard));
  817. $query->bindValue(':id', $postID, PDO::PARAM_INT);
  818. $query->execute() or error(db_error($query));
  819. $replies = array();
  820. while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
  821. $post['mod'] = true;
  822. $post['thread'] = $newID;
  823. if ($post['file']) {
  824. $post['has_file'] = true;
  825. $post['width'] = &$post['filewidth'];
  826. $post['height'] = &$post['fileheight'];
  827. $post['file_src'] = sprintf($config['board_path'], $board['uri']) . $config['dir']['img'] . $post['file'];
  828. $post['file_thumb'] = sprintf($config['board_path'], $board['uri']) . $config['dir']['thumb'] . $post['thumb'];
  829. } else {
  830. $post['has_file'] = false;
  831. }
  832. $replies[] = $post;
  833. }
  834. $newIDs = array($postID => $newID);
  835. openBoard($targetBoard);
  836. foreach ($replies as &$post) {
  837. $query = prepare('SELECT `target` FROM ``cites`` WHERE `target_board` = :board AND `board` = :board AND `post` = :post');
  838. $query->bindValue(':board', $originBoard);
  839. $query->bindValue(':post', $post['id'], PDO::PARAM_INT);
  840. $query->execute() or error(db_error($qurey));
  841. // correct >>X links
  842. while ($cite = $query->fetch(PDO::FETCH_ASSOC)) {
  843. if (isset($newIDs[$cite['target']])) {
  844. $post['body_nomarkup'] = preg_replace(
  845. '/(>>(>\/' . preg_quote($originBoard, '/') . '\/)?)' . preg_quote($cite['target'], '/') . '/',
  846. '>>' . $newIDs[$cite['target']],
  847. $post['body_nomarkup']);
  848. $post['body'] = $post['body_nomarkup'];
  849. }
  850. }
  851. $post['body'] = $post['body_nomarkup'];
  852. $post['op'] = false;
  853. $post['tracked_cites'] = markup($post['body'], true);
  854. // insert reply
  855. $newIDs[$post['id']] = $newPostID = post($post);
  856. if ($post['has_file']) {
  857. // copy image
  858. $clone($post['file_src'], sprintf($config['board_path'], $board['uri']) . $config['dir']['img'] . $post['file']);
  859. $clone($post['file_thumb'], sprintf($config['board_path'], $board['uri']) . $config['dir']['thumb'] . $post['thumb']);
  860. }
  861. if (!empty($post['tracked_cites'])) {
  862. $insert_rows = array();
  863. foreach ($post['tracked_cites'] as $cite) {
  864. $insert_rows[] = '(' .
  865. $pdo->quote($board['uri']) . ', ' . $newPostID . ', ' .
  866. $pdo->quote($cite[0]) . ', ' . (int)$cite[1] . ')';
  867. }
  868. query('INSERT INTO ``cites`` VALUES ' . implode(', ', $insert_rows)) or error(db_error());
  869. }
  870. }
  871. modLog("Moved thread #${postID} to " . sprintf($config['board_abbreviation'], $targetBoard) . " (#${newID})", $originBoard);
  872. // build new thread
  873. buildThread($newID);
  874. clean();
  875. buildIndex();
  876. // trigger themes
  877. rebuildThemes('post', $targetBoard);
  878. // return to original board
  879. openBoard($originBoard);
  880. if ($shadow) {
  881. // lock old thread
  882. $query = prepare(sprintf('UPDATE ``posts_%s`` SET `locked` = 1 WHERE `id` = :id', $originBoard));
  883. $query->bindValue(':id', $postID, PDO::PARAM_INT);
  884. $query->execute() or error(db_error($query));
  885. // leave a reply, linking to the new thread
  886. $post = array(
  887. 'mod' => true,
  888. 'subject' => '',
  889. 'email' => '',
  890. 'name' => (!$config['mod']['shadow_name'] ? $config['anonymous'] : $config['mod']['shadow_name']),
  891. 'capcode' => $config['mod']['shadow_capcode'],
  892. 'trip' => '',
  893. 'password' => '',
  894. 'has_file' => false,
  895. // attach to original thread
  896. 'thread' => $postID,
  897. 'op' => false
  898. );
  899. $post['body'] = $post['body_nomarkup'] = sprintf($config['mod']['shadow_mesage'], '>>>/' . $targetBoard . '/' . $newID);
  900. markup($post['body']);
  901. $botID = post($post);
  902. buildThread($postID);
  903. buildIndex();
  904. header('Location: ?/' . sprintf($config['board_path'], $originBoard) . $config['dir']['res'] .sprintf($config['file_page'], $postID) .
  905. '#' . $botID, true, $config['redirect_http']);
  906. } else {
  907. deletePost($postID);
  908. buildIndex();
  909. openBoard($targetBoard);
  910. header('Location: ?/' . sprintf($config['board_path'], $board['uri']) . $config['dir']['res'] . sprintf($config['file_page'], $newID), true, $config['redirect_http']);
  911. }
  912. }
  913. $boards = listBoards();
  914. if (count($boards) <= 1)
  915. error(_('Impossible to move thread; there is only one board.'));
  916. $security_token = make_secure_link_token($originBoard . '/move/' . $postID);
  917. mod_page(_('Move thread'), 'mod/move.html', array('post' => $postID, 'board' => $originBoard, 'boards' => $boards, 'token' => $security_token));
  918. }
  919. function mod_ban_post($board, $delete, $post, $token = false) {
  920. global $config, $mod;
  921. if (!openBoard($board))
  922. error($config['error']['noboard']);
  923. if (!hasPermission($config['mod']['delete'], $board))
  924. error($config['error']['noaccess']);
  925. $security_token = make_secure_link_token($board . '/ban/' . $post);
  926. $query = prepare(sprintf('SELECT `ip`, `thread` FROM ``posts_%s`` WHERE `id` = :id', $board));
  927. $query->bindValue(':id', $post);
  928. $query->execute() or error(db_error($query));
  929. if (!$_post = $query->fetch(PDO::FETCH_ASSOC))
  930. error($config['error']['404']);
  931. $thread = $_post['thread'];
  932. $ip = $_post['ip'];
  933. if (isset($_POST['new_ban'], $_POST['reason'], $_POST['length'], $_POST['board'])) {
  934. require_once 'inc/mod/ban.php';
  935. if (isset($_POST['ip']))
  936. $ip = $_POST['ip'];
  937. ban($ip, $_POST['reason'], parse_time($_POST['length']), $_POST['board'] == '*' ? false : $_POST['board']);
  938. if (isset($_POST['public_message'], $_POST['message'])) {
  939. // public ban message
  940. $length_english = parse_time($_POST['length']) ? 'for ' . until(parse_time($_POST['length'])) : 'permanently';
  941. $_POST['message'] = preg_replace('/[\r\n]/', '', $_POST['message']);
  942. $_POST['message'] = str_replace('%length%', $length_english, $_POST['message']);
  943. $_POST['message'] = str_replace('%LENGTH%', strtoupper($length_english), $_POST['message']);
  944. $query = prepare(sprintf('UPDATE ``posts_%s`` SET `body_nomarkup` = CONCAT(`body_nomarkup`, :body_nomarkup) WHERE `id` = :id', $board));
  945. $query->bindValue(':id', $post);
  946. $query->bindValue(':body_nomarkup', sprintf("\n<tinyboard ban message>%s</tinyboard>", utf8tohtml($_POST['message'])));
  947. $query->execute() or error(db_error($query));
  948. rebuildPost($post);
  949. modLog("Attached a public ban message to post #{$post}: " . utf8tohtml($_POST['message']));
  950. buildThread($thread ? $thread : $post);
  951. buildIndex();
  952. } elseif (isset($_POST['delete']) && (int) $_POST['delete']) {
  953. // Delete post
  954. deletePost($post);
  955. modLog("Deleted post #{$post}");
  956. // Rebuild board
  957. buildIndex();
  958. // Rebuild themes
  959. rebuildThemes('post-delete', $board);
  960. }
  961. header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']);
  962. }
  963. $args = array(
  964. 'ip' => $ip,
  965. 'hide_ip' => !hasPermission($config['mod']['show_ip'], $board),
  966. 'post' => $post,
  967. 'board' => $board,
  968. 'delete' => (bool)$delete,
  969. 'boards' => listBoards(),
  970. 'token' => $security_token
  971. );
  972. mod_page(_('New ban'), 'mod/ban_form.html', $args);
  973. }
  974. function mod_edit_post($board, $edit_raw_html, $postID) {
  975. global $config, $mod;
  976. if (!openBoard($board))
  977. error($config['error']['noboard']);
  978. if (!hasPermission($config['mod']['editpost'], $board))
  979. error($config['error']['noaccess']);
  980. if ($edit_raw_html && !hasPermission($config['mod']['rawhtml'], $board))
  981. error($config['error']['noaccess']);
  982. $security_token = make_secure_link_token($board . '/edit' . ($edit_raw_html ? '_raw' : '') . '/' . $postID);
  983. $query = prepare(sprintf('SELECT * FROM ``posts_%s`` WHERE `id` = :id', $board));
  984. $query->bindValue(':id', $postID);
  985. $query->execute() or error(db_error($query));
  986. if (!$post = $query->fetch(PDO::FETCH_ASSOC))
  987. error($config['error']['404']);
  988. if (isset($_POST['name'], $_POST['email'], $_POST['subject'], $_POST['body'])) {
  989. if ($edit_raw_html)
  990. $query = prepare(sprintf('UPDATE ``posts_%s`` SET `name` = :name, `email` = :email, `subject` = :subject, `body` = :body, `body_nomarkup` = :body_nomarkup WHERE `id` = :id', $board));
  991. else
  992. $query = prepare(sprintf('UPDATE ``posts_%s`` SET `name` = :name, `email` = :email, `subject` = :subject, `body_nomarkup` = :body WHERE `id` = :id', $board));
  993. $query->bindValue(':id', $postID);
  994. $query->bindValue('name', $_POST['name']);
  995. $query->bindValue(':email', $_POST['email']);
  996. $query->bindValue(':subject', $_POST['subject']);
  997. $query->bindValue(':body', $_POST['body']);
  998. if ($edit_raw_html) {
  999. $body_nomarkup = $_POST['body'] . "\n<tinyboard raw html>1</tinyboard>";
  1000. $query->bindValue(':body_nomarkup', $body_nomarkup);
  1001. }
  1002. $query->execute() or error(db_error($query));
  1003. if ($edit_raw_html) {
  1004. modLog("Edited raw HTML of post #{$postID}");
  1005. } else {
  1006. modLog("Edited post #{$postID}");
  1007. rebuildPost($postID);
  1008. }
  1009. buildIndex();
  1010. rebuildThemes('post', $board);
  1011. header('Location: ?/' . sprintf($config['board_path'], $board) . $config['dir']['res'] . sprintf($config['file_page'], $post['thread'] ? $post['thread'] : $postID) . '#' . $postID, true, $config['redirect_http']);
  1012. } else {
  1013. if ($config['minify_html']) {
  1014. $post['body_nomarkup'] = str_replace("\n", '&#010;', utf8tohtml($post['body_nomarkup']));
  1015. $post['body'] = str_replace("\n", '&#010;', utf8tohtml($post['body']));
  1016. $post['body_nomarkup'] = str_replace("\r", '', $post['body_nomarkup']);
  1017. $post['body'] = str_replace("\r", '', $post['body']);
  1018. $post['body_nomarkup'] = str_replace("\t", '&#09;', $post['body_nomarkup']);
  1019. $post['body'] = str_replace("\t", '&#09;', $post['body']);
  1020. }
  1021. mod_page(_('Edit post'), 'mod/edit_post_form.html', array('token' => $security_token, 'board' => $board, 'raw' => $edit_raw_html, 'post' => $post));
  1022. }
  1023. }
  1024. function mod_delete($board, $post) {
  1025. global $config, $mod;
  1026. if (!openBoard($board))
  1027. error($config['error']['noboard']);
  1028. if (!hasPermission($config['mod']['delete'], $board))
  1029. error($config['error']['noaccess']);
  1030. // Delete post
  1031. deletePost($post);
  1032. // Record the action
  1033. modLog("Deleted post #{$post}");
  1034. // Rebuild board
  1035. buildIndex();
  1036. // Rebuild themes
  1037. rebuildThemes('post-delete', $board);
  1038. // Redirect
  1039. header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']);
  1040. }
  1041. function mod_deletefile($board, $post) {
  1042. global $config, $mod;
  1043. if (!openBoard($board))
  1044. error($config['error']['noboard']);
  1045. if (!hasPermission($config['mod']['deletefile'], $board))
  1046. error($config['error']['noaccess']);
  1047. // Delete file
  1048. deleteFile($post);
  1049. // Record the action
  1050. modLog("Deleted file from post #{$post}");
  1051. // Rebuild board
  1052. buildIndex();
  1053. // Rebuild themes
  1054. rebuildThemes('post-delete', $board);
  1055. // Redirect
  1056. header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']);
  1057. }
  1058. function mod_spoiler_image($board, $post) {
  1059. global $config, $mod;
  1060. if (!openBoard($board))
  1061. error($config['error']['noboard']);
  1062. if (!hasPermission($config['mod']['spoilerimage'], $board))
  1063. error($config['error']['noaccess']);
  1064. // Delete file
  1065. $query = prepare(sprintf("SELECT `thumb`, `thread` FROM ``posts_%s`` WHERE id = :id", $board));
  1066. $query->bindValue(':id', $post, PDO::PARAM_INT);
  1067. $query->execute() or error(db_error($query));
  1068. $result = $query->fetch(PDO::FETCH_ASSOC);
  1069. file_unlink($board . '/' . $config['dir']['thumb'] . $result['thumb']);
  1070. // Make thumbnail spoiler
  1071. $query = prepare(sprintf("UPDATE ``posts_%s`` SET `thumb` = :thumb, `thumbwidth` = :thumbwidth, `thumbheight` = :thumbheight WHERE `id` = :id", $board));
  1072. $query->bindValue(':thumb', "spoiler");
  1073. $query->bindValue(':thumbwidth', 128, PDO::PARAM_INT);
  1074. $query->bindValue(':thumbheight', 128, PDO::PARAM_INT);
  1075. $query->bindValue(':id', $post, PDO::PARAM_INT);
  1076. $query->execute() or error(db_error($query));
  1077. // Record the action
  1078. modLog("Spoilered file from post #{$post}");
  1079. // Rebuild thread
  1080. buildThread($result['thread'] ? $result['thread'] : $post);
  1081. // Rebuild board
  1082. buildIndex();
  1083. // Rebuild themes
  1084. rebuildThemes('post-delete', $board);
  1085. // Redirect
  1086. header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']);
  1087. }
  1088. function mod_deletebyip($boardName, $post, $global = false) {
  1089. global $config, $mod, $board;
  1090. $global = (bool)$global;
  1091. if (!openBoard($boardName))
  1092. error($config['error']['noboard']);
  1093. if (!$global && !hasPermission($config['mod']['deletebyip'], $boardName))
  1094. error($config['error']['noaccess']);
  1095. if ($global && !hasPermission($config['mod']['deletebyip_global'], $boardName))
  1096. error($config['error']['noaccess']);
  1097. // Find IP address
  1098. $query = prepare(sprintf('SELECT `ip` FROM ``posts_%s`` WHERE `id` = :id', $boardName));
  1099. $query->bindValue(':id', $post);
  1100. $query->execute() or error(db_error($query));
  1101. if (!$ip = $query->fetchColumn())
  1102. error($config['error']['invalidpost']);
  1103. $boards = $global ? listBoards() : array(array('uri' => $boardName));
  1104. $query = '';
  1105. foreach ($boards as $_board) {
  1106. $query .= sprintf("SELECT `thread`, `id`, '%s' AS `board` FROM ``posts_%s`` WHERE `ip` = :ip UNION ALL ", $_board['uri'], $_board['uri']);
  1107. }
  1108. $query = preg_replace('/UNION ALL $/', '', $query);
  1109. $query = prepare($query);
  1110. $query->bindValue(':ip', $ip);
  1111. $query->execute() or error(db_error($query));
  1112. if ($query->rowCount() < 1)
  1113. error($config['error']['invalidpost']);
  1114. @set_time_limit($config['mod']['rebuild_timelimit']);
  1115. $threads_to_rebuild = array();
  1116. $threads_deleted = array();
  1117. while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
  1118. openBoard($post['board']);
  1119. deletePost($post['id'], false, false);
  1120. rebuildThemes('post-delete', $board['uri']);
  1121. if ($post['thread'])
  1122. $threads_to_rebuild[$post['board']][$post['thread']] = true;
  1123. else
  1124. $threads_deleted[$post['board']][$post['id']] = true;
  1125. }
  1126. foreach ($threads_to_rebuild as $_board => $_threads) {
  1127. openBoard($_board);
  1128. foreach ($_threads as $_thread => $_dummy) {
  1129. if ($_dummy && !isset($threads_deleted[$_board][$_thread]))
  1130. buildThread($_thread);
  1131. }
  1132. buildIndex();
  1133. }
  1134. if ($global) {
  1135. $board = false;
  1136. }
  1137. // Record the action
  1138. modLog("Deleted all posts by IP address: <a href=\"?/IP/$ip\">$ip</a>");
  1139. // Redirect
  1140. header('Location: ?/' . sprintf($config['board_path'], $boardName) . $config['file_index'], true, $config['redirect_http']);
  1141. }
  1142. function mod_user($uid) {
  1143. global $config, $mod;
  1144. if (!hasPermission($config['mod']['editusers']) && !(hasPermission($config['mod']['change_password']) && $uid == $mod['id']))
  1145. error($config['error']['noaccess']);
  1146. $query = prepare('SELECT * FROM ``mods`` WHERE `id` = :id');
  1147. $query->bindValue(':id', $uid);
  1148. $query->execute() or error(db_error($query));
  1149. if (!$user = $query->fetch(PDO::FETCH_ASSOC))
  1150. error($config['error']['404']);
  1151. if (hasPermission($config['mod']['editusers']) && isset($_POST['username'], $_POST['password'])) {
  1152. if (isset($_POST['allboards'])) {
  1153. $boards = array('*');
  1154. } else {
  1155. $_boards = listBoards();
  1156. foreach ($_boards as &$board) {
  1157. $board = $board['uri'];
  1158. }
  1159. $boards = array();
  1160. foreach ($_POST as $name => $value) {
  1161. if (preg_match('/^board_(' . $config['board_regex'] . ')$/u', $name, $matches) && in_array($matches[1], $_boards))
  1162. $boards[] = $matches[1];
  1163. }
  1164. }
  1165. if (isset($_POST['delete'])) {
  1166. if (!hasPermission($config['mod']['deleteusers']))
  1167. error($config['error']['noaccess']);
  1168. $query = prepare('DELETE FROM ``mods`` WHERE `id` = :id');
  1169. $query->bindValue(':id', $uid);
  1170. $query->execute() or error(db_error($query));
  1171. modLog('Deleted user ' . utf8tohtml($user['username']) . ' <small>(#' . $user['id'] . ')</small>');
  1172. header('Location: ?/users', true, $config['redirect_http']);
  1173. return;
  1174. }
  1175. if ($_POST['username'] == '')
  1176. error(sprintf($config['error']['required'], 'username'));
  1177. $query = prepare('UPDATE ``mods`` SET `username` = :username, `boards` = :boards WHERE `id` = :id');
  1178. $query->bindValue(':id', $uid);
  1179. $query->bindValue(':username', $_POST['username']);
  1180. $query->bindValue(':boards', implode(',', $boards));
  1181. $query->execute() or error(db_error($query));
  1182. if ($user['username'] !== $_POST['username']) {
  1183. // account was renamed
  1184. modLog('Renamed user "' . utf8tohtml($user['username']) . '" <small>(#' . $user['id'] . ')</small> to "' . utf8tohtml($_POST['username']) . '"');
  1185. }
  1186. if ($_POST['password'] != '') {
  1187. $salt = generate_salt();
  1188. $password = hash('sha256', $salt . sha1($_POST['password']));
  1189. $query = prepare('UPDATE ``mods`` SET `password` = :password, `salt` = :salt WHERE `id` = :id');
  1190. $query->bindValue(':id', $uid);
  1191. $query->bindValue(':password', $password);
  1192. $query->bindValue(':salt', $salt);
  1193. $query->execute() or error(db_error($query));
  1194. modLog('Changed password for ' . utf8tohtml($_POST['username']) . ' <small>(#' . $user['id'] . ')</small>');
  1195. if ($uid == $mod['id']) {
  1196. login($_POST['username'], $_POST['password']);
  1197. setCookies();
  1198. }
  1199. }
  1200. if (hasPermission($config['mod']['manageusers']))
  1201. header('Location: ?/users', true, $config['redirect_http']);
  1202. else
  1203. header('Location: ?/', true, $config['redirect_http']);
  1204. return;
  1205. }
  1206. if (hasPermission($config['mod']['change_password']) && $uid == $mod['id'] && isset($_POST['password'])) {
  1207. if ($_POST['password'] != '') {
  1208. $salt = generate_salt();
  1209. $password = hash('sha256', $salt . sha1($_POST['password']));
  1210. $query = prepare('UPDATE ``mods`` SET `password` = :password, `salt` = :salt WHERE `id` = :id');
  1211. $query->bindValue(':id', $uid);
  1212. $query->bindValue(':password', $password);
  1213. $query->bindValue(':salt', $salt);
  1214. $query->execute() or error(db_error($query));
  1215. modLog('Changed own password');
  1216. login($user['username'], $_POST['password']);
  1217. setCookies();
  1218. }
  1219. if (hasPermission($config['mod']['manageusers']))
  1220. header('Location: ?/users', true, $config['redirect_http']);
  1221. else
  1222. header('Location: ?/', true, $config['redirect_http']);
  1223. return;
  1224. }
  1225. if (hasPermission($config['mod']['modlog'])) {
  1226. $query = prepare('SELECT * FROM ``modlogs`` WHERE `mod` = :id ORDER BY `time` DESC LIMIT 5');
  1227. $query->bindValue(':id', $uid);
  1228. $query->execute() or error(db_error($query));
  1229. $log = $query->fetchAll(PDO::FETCH_ASSOC);
  1230. } else {
  1231. $log = array();
  1232. }
  1233. $user['boards'] = explode(',', $user['boards']);
  1234. mod_page(_('Edit user'), 'mod/user.html', array('user' => $user, 'logs' => $log, 'boards' => listBoards()));
  1235. }
  1236. function mod_user_new() {
  1237. global $pdo, $config;
  1238. if (!hasPermission($config['mod']['createusers']))
  1239. error($config['error']['noaccess']);
  1240. if (isset($_POST['username'], $_POST['password'], $_POST['type'])) {
  1241. if ($_POST['username'] == '')
  1242. error(sprintf($config['error']['required'], 'username'));
  1243. if ($_POST['password'] == '')
  1244. error(sprintf($config['error']['required'], 'password'));
  1245. if (isset($_POST['allboards'])) {
  1246. $boards = array('*');
  1247. } else {
  1248. $_boards = listBoards();
  1249. foreach ($_boards as &$board) {
  1250. $board = $board['uri'];
  1251. }
  1252. $boards = array();
  1253. foreach ($_POST as $name => $value) {
  1254. if (preg_match('/^board_(' . $config['board_regex'] . ')$/u', $name, $matches) && in_array($matches[1], $_boards))
  1255. $boards[] = $matches[1];
  1256. }
  1257. }
  1258. $_POST['type'] = (int) $_POST['type'];
  1259. if ($_POST['type'] !== JANITOR && $_POST['type'] !== MOD && $_POST['type'] !== ADMIN)
  1260. error(sprintf($config['error']['invalidfield'], 'type'));
  1261. $salt = generate_salt();
  1262. $password = hash('sha256', $salt . sha1($_POST['password']));
  1263. $query = prepare('INSERT INTO ``mods`` VALUES (NULL, :username, :password, :salt, :type, :boards)');
  1264. $query->bindValue(':username', $_POST['username']);
  1265. $query->bindValue(':password', $password);
  1266. $query->bindValue(':salt', $salt);
  1267. $query->bindValue(':type', $_POST['type']);
  1268. $query->bindValue(':boards', implode(',', $boards));
  1269. $query->execute() or error(db_error($query));
  1270. $userID = $pdo->lastInsertId();
  1271. modLog('Created a new user: ' . utf8tohtml($_POST['username']) . ' <small>(#' . $userID . ')</small>');
  1272. header('Location: ?/users', true, $config['redirect_http']);
  1273. return;
  1274. }
  1275. mod_page(_('Edit user'), 'mod/user.html', array('new' => true, 'boards' => listBoards()));
  1276. }
  1277. function mod_users() {
  1278. global $config;
  1279. if (!hasPermission($config['mod']['manageusers']))
  1280. error($config['error']['noaccess']);
  1281. $query = query("SELECT *, (SELECT `time` FROM ``modlogs`` WHERE `mod` = `id` ORDER BY `time` DESC LIMIT 1) AS `last`, (SELECT `text` FROM ``modlogs`` WHERE `mod` = `id` ORDER BY `time` DESC LIMIT 1) AS `action` FROM ``mods`` ORDER BY `type` DESC,`id`") or error(db_error());
  1282. $users = $query->fetchAll(PDO::FETCH_ASSOC);
  1283. mod_page(sprintf('%s (%d)', _('Manage users'), count($users)), 'mod/users.html', array('users' => $users));
  1284. }
  1285. function mod_user_promote($uid, $action) {
  1286. global $config;
  1287. if (!hasPermission($config['mod']['promoteusers']))
  1288. error($config['error']['noaccess']);
  1289. $query = prepare("UPDATE ``mods`` SET `type` = `type` " . ($action == 'promote' ? "+1 WHERE `type` < " . (int)ADMIN : "-1 WHERE `type` > " . (int)JANITOR) . " AND `id` = :id");
  1290. $query->bindValue(':id', $uid);
  1291. $query->execute() or error(db_error($query));
  1292. modLog(($action == 'promote' ? 'Promoted' : 'Demoted') . " user #{$uid}");
  1293. header('Location: ?/users', true, $config['redirect_http']);
  1294. }
  1295. function mod_pm($id, $reply = false) {
  1296. global $mod, $config;
  1297. if ($reply && !hasPermission($config['mod']['create_pm']))
  1298. error($config['error']['noaccess']);
  1299. $query = prepare("SELECT ``mods``.`username`, `mods_to`.`username` AS `to_username`, ``pms``.* FROM ``pms`` LEFT JOIN ``mods`` ON ``mods``.`id` = `sender` LEFT JOIN ``mods`` AS `mods_to` ON `mods_to`.`id` = `to` WHERE ``pms``.`id` = :id");
  1300. $query->bindValue(':id', $id);
  1301. $query->execute() or error(db_error($query));
  1302. if ((!$pm = $query->fetch(PDO::FETCH_ASSOC)) || ($pm['to'] != $mod['id'] && !hasPermission($config['mod']['master_pm'])))
  1303. error($config['error']['404']);
  1304. if (isset($_POST['delete'])) {
  1305. $query = prepare("DELETE FROM ``pms`` WHERE `id` = :id");
  1306. $query->bindValue(':id', $id);
  1307. $query->execute() or error(db_error($query));
  1308. if ($config['cache']['enabled']) {
  1309. cache::delete('pm_unread_' . $mod['id']);
  1310. cache::delete('pm_unreadcount_' . $mod['id']);
  1311. }
  1312. header('Location: ?/', true, $config['redirect_http']);
  1313. return;
  1314. }
  1315. if ($pm['unread'] && $pm['to'] == $mod['id']) {
  1316. $query = prepare("UPDATE ``pms`` SET `unread` = 0 WHERE `id` = :id");
  1317. $query->bindValue(':id', $id);
  1318. $query->execute() or error(db_error($query));
  1319. if ($config['cache']['enabled']) {
  1320. cache::delete('pm_unread_' . $mod['id']);
  1321. cache::delete('pm_unreadcount_' . $mod['id']);
  1322. }
  1323. modLog('Read a PM');
  1324. }
  1325. if ($reply) {
  1326. if (!$pm['to_username'])
  1327. error($config['error']['404']); // deleted?
  1328. mod_page(sprintf('%s %s', _('New PM for'), $pm['to_username']), 'mod/new_pm.html', array(
  1329. 'username' => $pm['username'], 'id' => $pm['sender'], 'message' => quote($pm['message'])
  1330. ));
  1331. } else {
  1332. mod_page(sprintf('%s &ndash; #%d', _('Private message'), $id), 'mod/pm.html', $pm);
  1333. }
  1334. }
  1335. function mod_inbox() {
  1336. global $config, $mod;
  1337. $query = prepare('SELECT `unread`,``pms``.`id`, `time`, `sender`, `to`, `message`, `username` FROM ``pms`` LEFT JOIN ``mods`` ON ``mods``.`id` = `sender` WHERE `to` = :mod ORDER BY `unread` DESC, `time` DESC');
  1338. $query->bindValue(':mod', $mod['id']);
  1339. $query->execute() or error(db_error($query));
  1340. $messages = $query->fetchAll(PDO::FETCH_ASSOC);
  1341. $query = prepare('SELECT COUNT(*) FROM ``pms`` WHERE `to` = :mod AND `unread` = 1');
  1342. $query->bindValue(':mod', $mod['id']);
  1343. $query->execute() or error(db_error($query));
  1344. $unread = $query->fetchColumn();
  1345. foreach ($messages as &$message) {
  1346. $message['snippet'] = pm_snippet($message['message']);
  1347. }
  1348. mod_page(sprintf('%s (%s)', _('PM inbox'), count($messages) > 0 ? $unread . ' unread' : 'empty'), 'mod/inbox.html', array(
  1349. 'messages' => $messages,
  1350. 'unread' => $unread
  1351. ));
  1352. }
  1353. function mod_new_pm($username) {
  1354. global $config, $mod;
  1355. if (!hasPermission($config['mod']['create_pm']))
  1356. error($config['error']['noaccess']);
  1357. $query = prepare("SELECT `id` FROM ``mods`` WHERE `username` = :username");
  1358. $query->bindValue(':username', $username);
  1359. $query->execute() or error(db_error($query));
  1360. if (!$id = $query->fetchColumn()) {
  1361. // Old style ?/PM: by user ID
  1362. $query = prepare("SELECT `username` FROM ``mods`` WHERE `id` = :username");
  1363. $query->bindValue(':username', $username);
  1364. $query->execute() or error(db_error($query));
  1365. if ($username = $query->fetchColumn())
  1366. header('Location: ?/new_PM/' . $username, true, $config['redirect_http']);
  1367. else
  1368. error($config['error']['404']);
  1369. }
  1370. if (isset($_POST['message'])) {
  1371. $_POST['message'] = escape_markup_modifiers($_POST['message']);
  1372. markup($_POST['message']);
  1373. $query = prepare("INSERT INTO ``pms`` VALUES (NULL, :me, :id, :message, :time, 1)");
  1374. $query->bindValue(':me', $mod['id']);
  1375. $query->bindValue(':id', $id);
  1376. $query->bindValue(':message', $_POST['message']);
  1377. $query->bindValue(':time', time());
  1378. $query->execute() or error(db_error($query));
  1379. if ($config['cache']['enabled']) {
  1380. cache::delete('pm_unread_' . $id);
  1381. cache::delete('pm_unreadcount_' . $id);
  1382. }
  1383. modLog('Sent a PM to ' . utf8tohtml($username));
  1384. header('Location: ?/', true, $config['redirect_http']);
  1385. }
  1386. mod_page(sprintf('%s %s', _('New PM for'), $username), 'mod/new_pm.html', array('username' => $username, 'id' => $id));
  1387. }
  1388. function mod_rebuild() {
  1389. global $config, $twig;
  1390. if (!hasPermission($config['mod']['rebuild']))
  1391. error($config['error']['noaccess']);
  1392. if (isset($_POST['rebuild'])) {
  1393. @set_time_limit($config['mod']['rebuild_timelimit']);
  1394. $log = array();
  1395. $boards = listBoards();
  1396. $rebuilt_scripts = array();
  1397. if (isset($_POST['rebuild_cache'])) {
  1398. if ($config['cache']['enabled']) {
  1399. $log[] = 'Flushing cache';
  1400. Cache::flush();
  1401. }
  1402. $log[] = 'Clearing template cache';
  1403. load_twig();
  1404. $twig->clearCacheFiles();
  1405. }
  1406. if (isset($_POST['rebuild_themes'])) {
  1407. $log[] = 'Regenerating theme files';
  1408. rebuildThemes('all');
  1409. }
  1410. if (isset($_POST['rebuild_javascript'])) {
  1411. $log[] = 'Rebuilding <strong>' . $config['file_script'] . '</strong>';
  1412. buildJavascript();
  1413. $rebuilt_scripts[] = $config['file_script'];
  1414. }
  1415. foreach ($boards as $board) {
  1416. if (!(isset($_POST['boards_all']) || isset($_POST['board_' . $board['uri']])))
  1417. continue;
  1418. openBoard($board['uri']);
  1419. if (isset($_POST['rebuild_index'])) {
  1420. buildIndex();
  1421. $log[] = '<strong>' . sprintf($config['board_abbreviation'], $board['uri']) . '</strong>: Creating index pages';
  1422. }
  1423. if (isset($_POST['rebuild_javascript']) && !in_array($config['file_script'], $rebuilt_scripts)) {
  1424. $log[] = '<strong>' . sprintf($config['board_abbreviation'], $board['uri']) . '</strong>: Rebuilding <strong>' . $config['file_script'] . '</strong>';
  1425. buildJavascript();
  1426. $rebuilt_scripts[] = $config['file_script'];
  1427. }
  1428. if (isset($_POST['rebuild_thread'])) {
  1429. $query = query(sprintf("SELECT `id` FROM ``posts_%s`` WHERE `thread` IS NULL", $board['uri'])) or error(db_error());
  1430. while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
  1431. $log[] = '<strong>' . sprintf($config['board_abbreviation'], $board['uri']) . '</strong>: Rebuilding thread #' . $post['id'];
  1432. buildThread($post['id']);
  1433. }
  1434. }
  1435. }
  1436. mod_page(_('Rebuild'), 'mod/rebuilt.html', array('logs' => $log));
  1437. return;
  1438. }
  1439. mod_page(_('Rebuild'), 'mod/rebuild.html', array('boards' => listBoards()));
  1440. }
  1441. function mod_reports() {
  1442. global $config, $mod;
  1443. if (!hasPermission($config['mod']['reports']))
  1444. error($config['error']['noaccess']);
  1445. $query = prepare("SELECT * FROM ``reports`` ORDER BY `time` DESC LIMIT :limit");
  1446. $query->bindValue(':limit', $config['mod']['recent_reports'], PDO::PARAM_INT);
  1447. $query->execute() or error(db_error($query));
  1448. $reports = $query->fetchAll(PDO::FETCH_ASSOC);
  1449. $report_queries = array();
  1450. foreach ($reports as $report) {
  1451. if (!isset($report_queries[$report['board']]))
  1452. $report_queries[$report['board']] = array();
  1453. $report_queries[$report['board']][] = $report['post'];
  1454. }
  1455. $report_posts = array();
  1456. foreach ($report_queries as $board => $posts) {
  1457. $report_posts[$board] = array();
  1458. $query = query(sprintf('SELECT * FROM ``posts_%s`` WHERE `id` = ' . implode(' OR `id` = ', $posts), $board)) or error(db_error());
  1459. while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
  1460. $report_posts[$board][$post['id']] = $post;
  1461. }
  1462. }
  1463. $count = 0;
  1464. $body = '';
  1465. foreach ($reports as $report) {
  1466. if (!isset($report_posts[$report['board']][$report['post']])) {
  1467. // // Invalid report (post has since been deleted)
  1468. $query = prepare("DELETE FROM ``reports`` WHERE `post` = :id AND `board` = :board");
  1469. $query->bindValue(':id', $report['post'], PDO::PARAM_INT);
  1470. $query->bindValue(':board', $report['board']);
  1471. $query->execute() or error(db_error($query));
  1472. continue;
  1473. }
  1474. openBoard($report['board']);
  1475. $post = &$report_posts[$report['board']][$report['post']];
  1476. if (!$post['thread']) {
  1477. // Still need to fix this:
  1478. $po = new Thread($post, '?/', $mod, false);
  1479. } else {
  1480. $po = new Post($post, '?/', $mod);
  1481. }
  1482. // a little messy and inefficient
  1483. $append_html = Element('mod/report.html', array('report' => $report, 'config' => $config, 'mod' => $mod));
  1484. // Bug fix for https://github.com/savetheinternet/Tinyboard/issues/21
  1485. $po->body = truncate($po->body, $po->link(), $config['body_truncate'] - substr_count($append_html, '<br>'));
  1486. if (mb_strlen($po->body) + mb_strlen($append_html) > $config['body_truncate_char']) {
  1487. // still too long; temporarily increase limit in the config
  1488. $__old_body_truncate_char = $config['body_truncate_char'];
  1489. $config['body_truncate_char'] = mb_strlen($po->body) + mb_strlen($append_html);
  1490. }
  1491. $po->body .= $append_html;
  1492. $body .= $po->build(true) . '<hr>';
  1493. if (isset($__old_body_truncate_char))
  1494. $config['body_truncate_char'] = $__old_body_truncate_char;
  1495. $count++;
  1496. }
  1497. mod_page(sprintf('%s (%d)', _('Report queue'), $count), 'mod/reports.html', array('reports' => $body, 'count' => $count));
  1498. }
  1499. function mod_report_dismiss($id, $all = false) {
  1500. global $config;
  1501. $query = prepare("SELECT `post`, `board`, `ip` FROM ``reports`` WHERE `id` = :id");
  1502. $query->bindValue(':id', $id);
  1503. $query->execute() or error(db_error($query));
  1504. if ($report = $query->fetch(PDO::FETCH_ASSOC)) {
  1505. $ip = $report['ip'];
  1506. $board = $report['board'];
  1507. $post = $report['post'];
  1508. } else
  1509. error($config['error']['404']);
  1510. if (!$all && !hasPermission($config['mod']['report_dismiss'], $board))
  1511. error($config['error']['noaccess']);
  1512. if ($all && !hasPermission($config['mod']['report_dismiss_ip'], $board))
  1513. error($config['error']['noaccess']);
  1514. if ($all) {
  1515. $query = prepare("DELETE FROM ``reports`` WHERE `ip` = :ip");
  1516. $query->bindValue(':ip', $ip);
  1517. } else {
  1518. $query = prepare("DELETE FROM ``reports`` WHERE `id` = :id");
  1519. $query->bindValue(':id', $id);
  1520. }
  1521. $query->execute() or error(db_error($query));
  1522. if ($all)
  1523. modLog("Dismissed all reports by <a href=\"?/IP/$ip\">$ip</a>");
  1524. else
  1525. modLog("Dismissed a report for post #{$id}", $board);
  1526. header('Location: ?/reports', true, $config['redirect_http']);
  1527. }
  1528. function mod_config($board_config = false) {
  1529. global $config, $mod, $board;
  1530. if ($board_config && !openBoard($board_config))
  1531. error($config['error']['noboard']);
  1532. if (!hasPermission($config['mod']['edit_config'], $board_config))
  1533. error($config['error']['noaccess']);
  1534. $config_file = $board_config ? $board['dir'] . 'config.php' : 'inc/instance-config.php';
  1535. if ($config['mod']['config_editor_php']) {
  1536. $readonly = !(is_file($config_file) ? is_writable($config_file) : is_writable(dirname($config_file)));
  1537. if (!$readonly && isset($_POST['code'])) {
  1538. $code = $_POST['code'];
  1539. file_put_contents($config_file, $code);
  1540. header('Location: ?/config' . ($board_config ? '/' . $board_config : ''), true, $config['redirect_http']);
  1541. return;
  1542. }
  1543. $instance_config = @file_get_contents($config_file);
  1544. if ($instance_config === false) {
  1545. $instance_config = "<?php\n\n// This file does not exist yet. You are creating it.";
  1546. }
  1547. $instance_config = str_replace("\n", '&#010;', utf8tohtml($instance_config));
  1548. mod_page(_('Config editor'), 'mod/config-editor-php.html', array(
  1549. 'php' => $instance_config,
  1550. 'readonly' => $readonly,
  1551. 'boards' => listBoards(),
  1552. 'board' => $board_config,
  1553. 'file' => $config_file
  1554. ));
  1555. return;
  1556. }
  1557. require_once 'inc/mod/config-editor.php';
  1558. $conf = config_vars();
  1559. foreach ($conf as &$var) {
  1560. if (is_array($var['name'])) {
  1561. $c = &$config;
  1562. foreach ($var['name'] as $n)
  1563. $c = &$c[$n];
  1564. } else {
  1565. $c = @$config[$var['name']];
  1566. }
  1567. $var['value'] = $c;
  1568. }
  1569. unset($var);
  1570. if (isset($_POST['save'])) {
  1571. $config_append = '';
  1572. foreach ($conf as $var) {
  1573. $field_name = 'cf_' . (is_array($var['name']) ? implode('/', $var['name']) : $var['name']);
  1574. if ($var['type'] == 'boolean')
  1575. $value = isset($_POST[$field_name]);
  1576. elseif (isset($_POST[$field_name]))
  1577. $value = $_POST[$field_name];
  1578. else
  1579. continue; // ???
  1580. if (!settype($value, $var['type']))
  1581. continue; // invalid
  1582. if ($value != $var['value']) {
  1583. // This value has been changed.
  1584. $config_append .= '$config';
  1585. if (is_array($var['name'])) {
  1586. foreach ($var['name'] as $name)
  1587. $config_append .= '[' . var_export($name, true) . ']';
  1588. } else {
  1589. $config_append .= '[' . var_export($var['name'], true) . ']';
  1590. }
  1591. $config_append .= ' = ';
  1592. if (@$var['permissions'] && in_array($value, array(JANITOR, MOD, ADMIN, DISABLED))) {
  1593. $perm_array = array(
  1594. JANITOR => 'JANITOR',
  1595. MOD => 'MOD',
  1596. ADMIN => 'ADMIN',
  1597. DISABLED => 'DISABLED'
  1598. );
  1599. $config_append .= $perm_array[$value];
  1600. } else {
  1601. $config_append .= var_export($value, true);
  1602. }
  1603. $config_append .= ";\n";
  1604. }
  1605. }
  1606. if (!empty($config_append)) {
  1607. $config_append = "\n// Changes made via web editor by \"" . $mod['username'] . "\" @ " . date('r') . ":\n" . $config_append . "\n";
  1608. if (!is_file($config_file))
  1609. $config_append = "<?php\n\n$config_append";
  1610. if (!@file_put_contents($config_file, $config_append, FILE_APPEND)) {
  1611. $config_append = htmlentities($config_append);
  1612. if ($config['minify_html'])
  1613. $config_append = str_replace("\n", '&#010;', $config_append);
  1614. $page = array();
  1615. $page['title'] = 'Cannot write to file!';
  1616. $page['config'] = $config;
  1617. $page['body'] = '
  1618. <p style="text-align:center">Tinyboard could not write to <strong>' . $config_file . '</strong> with the ammended configuration, probably due to a permissions error.</p>
  1619. <p style="text-align:center">You may proceed with these changes manually by copying and pasting the following code to the end of <strong>' . $config_file . '</strong>:</p>
  1620. <textarea style="width:700px;height:370px;margin:auto;display:block;background:white;color:black" readonly>' . $config_append . '</textarea>
  1621. ';
  1622. echo Element('page.html', $page);
  1623. exit;
  1624. }
  1625. }
  1626. header('Location: ?/config', true, $config['redirect_http']);
  1627. exit;
  1628. }
  1629. mod_page(_('Config editor') . ($board_config ? ': ' . sprintf($config['board_abbreviation'], $board_config) : ''),
  1630. 'mod/config-editor.html', array(
  1631. 'boards' => listBoards(),
  1632. 'board' => $board_config,
  1633. 'conf' => $conf,
  1634. 'file' => $config_file
  1635. ));
  1636. }
  1637. function mod_themes_list() {
  1638. global $config;
  1639. if (!hasPermission($config['mod']['themes']))
  1640. error($config['error']['noaccess']);
  1641. if (!is_dir($config['dir']['themes']))
  1642. error(_('Themes directory doesn\'t exist!'));
  1643. if (!$dir = opendir($config['dir']['themes']))
  1644. error(_('Cannot open themes directory; check permissions.'));
  1645. $query = query('SELECT `theme` FROM ``theme_settings`` WHERE `name` IS NULL AND `value` IS NULL') or error(db_error());
  1646. $themes_in_use = $query->fetchAll(PDO::FETCH_COLUMN);
  1647. // Scan directory for themes
  1648. $themes = array();
  1649. while ($file = readdir($dir)) {
  1650. if ($file[0] != '.' && is_dir($config['dir']['themes'] . '/' . $file)) {
  1651. $themes[$file] = loadThemeConfig($file);
  1652. }
  1653. }
  1654. closedir($dir);
  1655. mod_page(_('Manage themes'), 'mod/themes.html', array(
  1656. 'themes' => $themes,
  1657. 'themes_in_use' => $themes_in_use,
  1658. ));
  1659. }
  1660. function mod_theme_configure($theme_name) {
  1661. global $config;
  1662. if (!hasPermission($config['mod']['themes']))
  1663. error($config['error']['noaccess']);
  1664. if (!$theme = loadThemeConfig($theme_name)) {
  1665. error($config['error']['invalidtheme']);
  1666. }
  1667. if (isset($_POST['install'])) {
  1668. // Check if everything is submitted
  1669. foreach ($theme['config'] as &$conf) {
  1670. if (!isset($_POST[$conf['name']]) && $conf['type'] != 'checkbox')
  1671. error(sprintf($config['error']['required'], $c['title']));
  1672. }
  1673. // Clear previous settings
  1674. $query = prepare("DELETE FROM ``theme_settings`` WHERE `theme` = :theme");
  1675. $query->bindValue(':theme', $theme_name);
  1676. $query->execute() or error(db_error($query));
  1677. foreach ($theme['config'] as &$conf) {
  1678. $query = prepare("INSERT INTO ``theme_settings`` VALUES(:theme, :name, :value)");
  1679. $query->bindValue(':theme', $theme_name);
  1680. $query->bindValue(':name', $conf['name']);
  1681. if ($conf['type'] == 'checkbox')
  1682. $query->bindValue(':value', isset($_POST[$conf['name']]) ? 1 : 0);
  1683. else
  1684. $query->bindValue(':value', $_POST[$conf['name']]);
  1685. $query->execute() or error(db_error($query));
  1686. }
  1687. $query = prepare("INSERT INTO ``theme_settings`` VALUES(:theme, NULL, NULL)");
  1688. $query->bindValue(':theme', $theme_name);
  1689. $query->execute() or error(db_error($query));
  1690. $result = true;
  1691. $message = false;
  1692. if (isset($theme['install_callback'])) {
  1693. $ret = $theme['install_callback'](themeSettings($theme_name));
  1694. if ($ret && !empty($ret)) {
  1695. if (is_array($ret) && count($ret) == 2) {
  1696. $result = $ret[0];
  1697. $message = $ret[1];
  1698. }
  1699. }
  1700. }
  1701. if (!$result) {
  1702. // Install failed
  1703. $query = prepare("DELETE FROM ``theme_settings`` WHERE `theme` = :theme");
  1704. $query->bindValue(':theme', $theme_name);
  1705. $query->execute() or error(db_error($query));
  1706. }
  1707. // Build themes
  1708. rebuildThemes('all');
  1709. mod_page(sprintf(_($result ? 'Installed theme: %s' : 'Installation failed: %s'), $theme['name']), 'mod/theme_installed.html', array(
  1710. 'theme_name' => $theme_name,
  1711. 'theme' => $theme,
  1712. 'result' => $result,
  1713. 'message' => $message,
  1714. ));
  1715. return;
  1716. }
  1717. $settings = themeSettings($theme_name);
  1718. mod_page(sprintf(_('Configuring theme: %s'), $theme['name']), 'mod/theme_config.html', array(
  1719. 'theme_name' => $theme_name,
  1720. 'theme' => $theme,
  1721. 'settings' => $settings,
  1722. ));
  1723. }
  1724. function mod_theme_uninstall($theme_name) {
  1725. global $config;
  1726. if (!hasPermission($config['mod']['themes']))
  1727. error($config['error']['noaccess']);
  1728. $query = prepare("DELETE FROM ``theme_settings`` WHERE `theme` = :theme");
  1729. $query->bindValue(':theme', $theme_name);
  1730. $query->execute() or error(db_error($query));
  1731. header('Location: ?/themes', true, $config['redirect_http']);
  1732. }
  1733. function mod_theme_rebuild($theme_name) {
  1734. global $config;
  1735. if (!hasPermission($config['mod']['themes']))
  1736. error($config['error']['noaccess']);
  1737. rebuildTheme($theme_name, 'all');
  1738. mod_page(sprintf(_('Rebuilt theme: %s'), $theme_name), 'mod/theme_rebuilt.html', array(
  1739. 'theme_name' => $theme_name,
  1740. ));
  1741. }
  1742. function mod_debug_antispam() {
  1743. global $pdo, $config;
  1744. $args = array();
  1745. if (isset($_POST['board'], $_POST['thread'])) {
  1746. $where = '`board` = ' . $pdo->quote($_POST['board']);
  1747. if ($_POST['thread'] != '')
  1748. $where .= ' AND `thread` = ' . $pdo->quote($_POST['thread']);
  1749. if (isset($_POST['purge'])) {
  1750. $query = prepare(', DATE ``antispam`` SET `expires` = UNIX_TIMESTAMP() + :expires WHERE' . $where);
  1751. $query->bindValue(':expires', $config['spam']['hidden_inputs_expire']);
  1752. $query->execute() or error(db_error());
  1753. }
  1754. $args['board'] = $_POST['board'];
  1755. $args['thread'] = $_POST['thread'];
  1756. } else {
  1757. $where = '';
  1758. }
  1759. $query = query('SELECT COUNT(*) FROM ``antispam``' . ($where ? " WHERE $where" : '')) or error(db_error());
  1760. $args['total'] = number_format($query->fetchColumn());
  1761. $query = query('SELECT COUNT(*) FROM ``antispam`` WHERE `expires` IS NOT NULL' . ($where ? " AND $where" : '')) or error(db_error());
  1762. $args['expiring'] = number_format($query->fetchColumn());
  1763. $query = query('SELECT * FROM ``antispam`` ' . ($where ? "WHERE $where" : '') . ' ORDER BY `passed` DESC LIMIT 40') or error(db_error());
  1764. $args['top'] = $query->fetchAll(PDO::FETCH_ASSOC);
  1765. $query = query('SELECT * FROM ``antispam`` ' . ($where ? "WHERE $where" : '') . ' ORDER BY `created` DESC LIMIT 20') or error(db_error());
  1766. $args['recent'] = $query->fetchAll(PDO::FETCH_ASSOC);
  1767. mod_page(_('Debug: Anti-spam'), 'mod/debug/antispam.html', $args);
  1768. }
  1769. function mod_debug_recent_posts() {
  1770. global $pdo, $config;
  1771. $limit = 500;
  1772. $boards = listBoards();
  1773. // Manually build an SQL query
  1774. $query = 'SELECT * FROM (';
  1775. foreach ($boards as $board) {
  1776. $query .= sprintf('SELECT *, %s AS `board` FROM ``posts_%s`` UNION ALL ', $pdo->quote($board['uri']), $board['uri']);
  1777. }
  1778. // Remove the last "UNION ALL" seperator and complete the query
  1779. $query = preg_replace('/UNION ALL $/', ') AS `all_posts` ORDER BY `time` DESC LIMIT ' . $limit, $query);
  1780. $query = query($query) or error(db_error());
  1781. $posts = $query->fetchAll(PDO::FETCH_ASSOC);
  1782. foreach ($posts as &$post) {
  1783. $post['snippet'] = pm_snippet($post['body']);
  1784. }
  1785. mod_page(_('Debug: Recent posts'), 'mod/debug/recent_posts.html', array('posts' => $posts));
  1786. }
  1787. function mod_debug_sql() {
  1788. global $config;
  1789. if (!hasPermission($config['mod']['debug_sql']))
  1790. error($config['error']['noaccess']);
  1791. $args['security_token'] = make_secure_link_token('debug/sql');
  1792. if (isset($_POST['query'])) {
  1793. $args['query'] = $_POST['query'];
  1794. if ($query = query($_POST['query'])) {
  1795. $args['result'] = $query->fetchAll(PDO::FETCH_ASSOC);
  1796. if (!empty($args['result']))
  1797. $args['keys'] = array_keys($args['result'][0]);
  1798. else
  1799. $args['result'] = 'empty';
  1800. } else {
  1801. $args['error'] = db_error();
  1802. }
  1803. }
  1804. mod_page(_('Debug: SQL'), 'mod/debug/sql.html', $args);
  1805. }