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.

204 lines
7.5KB

  1. <?php
  2. /*
  3. * Copyright (c) 2010-2014 Tinyboard Development Group
  4. */
  5. require 'inc/functions.php';
  6. require 'inc/mod/pages.php';
  7. require 'inc/mod/auth.php';
  8. if ($config['debug'])
  9. $parse_start_time = microtime(true);
  10. // Fix for magic quotes
  11. if (get_magic_quotes_gpc()) {
  12. function strip_array($var) {
  13. return is_array($var) ? array_map('strip_array', $var) : stripslashes($var);
  14. }
  15. $_GET = strip_array($_GET);
  16. $_POST = strip_array($_POST);
  17. }
  18. $query = isset($_SERVER['QUERY_STRING']) ? rawurldecode($_SERVER['QUERY_STRING']) : '';
  19. // If there's a thread parameter (eg, for returning to after enacting a ban)
  20. // drop it from the $query.
  21. if(isset($_GET['thread'])) {
  22. $query = explode("&thread=", $query)[0];
  23. }
  24. $pages = array(
  25. '' => ':?/', // redirect to dashboard
  26. '/' => 'dashboard', // dashboard
  27. '/confirm/(.+)' => 'confirm', // confirm action (if javascript didn't work)
  28. '/logout' => 'secure logout', // logout
  29. '/users' => 'users', // manage users
  30. '/users/(\d+)/(promote|demote)' => 'secure user_promote', // prmote/demote user
  31. '/users/(\d+)' => 'secure_POST user', // edit user
  32. '/users/new' => 'secure_POST user_new', // create a new user
  33. '/new_PM/([^/]+)' => 'secure_POST new_pm', // create a new pm
  34. '/PM/(\d+)(/reply)?' => 'pm', // read a pm
  35. '/inbox' => 'inbox', // pm inbox
  36. '/log' => 'log', // modlog
  37. '/log/(\d+)' => 'log', // modlog
  38. '/log:([^/]+)' => 'user_log', // modlog
  39. '/log:([^/]+)/(\d+)' => 'user_log', // modlog
  40. '/news' => 'secure_POST news', // view news
  41. '/news/(\d+)' => 'secure_POST news', // view news
  42. '/news/delete/(\d+)' => 'secure news_delete', // delete from news
  43. '/noticeboard' => 'secure_POST noticeboard', // view noticeboard
  44. '/noticeboard/(\d+)' => 'secure_POST noticeboard', // view noticeboard
  45. '/noticeboard/delete/(\d+)' => 'secure noticeboard_delete', // delete from noticeboard
  46. '/edit/(\%b)' => 'secure_POST edit_board', // edit board details
  47. '/new-board' => 'secure_POST new_board', // create a new board
  48. '/rebuild' => 'secure_POST rebuild', // rebuild static files
  49. '/reports' => 'reports', // report queue
  50. '/reports/(\d+)/dismiss(all)?' => 'secure report_dismiss', // dismiss a report
  51. '/IP/([\w.:]+)' => 'secure_POST ip', // view ip address
  52. '/IP/([\w.:]+)/remove_note/(\d+)' => 'secure ip_remove_note', // remove note from ip address
  53. '/ban' => 'secure_POST ban', // new ban
  54. '/bans' => 'secure_POST bans', // ban list
  55. '/bans/(\d+)' => 'secure_POST bans', // ban list
  56. '/ban-appeals' => 'secure_POST ban_appeals', // view ban appeals
  57. '/recent/(\d+)' => 'recent_posts', // view recent posts
  58. '/search' => 'search_redirect', // search
  59. '/search/(posts|IP_notes|bans|log)/(.+)/(\d+)' => 'search', // search
  60. '/search/(posts|IP_notes|bans|log)/(.+)' => 'search', // search
  61. '/(\%b)/ban(&delete)?/(\d+)' => 'secure_POST ban_post', // ban poster
  62. '/(\%b)/move/(\d+)' => 'secure_POST move', // move thread
  63. '/(\%b)/move_reply/(\d+)' => 'secure_POST move_reply', // move reply
  64. '/(\%b)/edit(_raw)?/(\d+)' => 'secure_POST edit_post', // edit post
  65. '/(\%b)/delete/(\d+)' => 'secure delete', // delete post
  66. '/(\%b)/deletefile/(\d+)/(\d+)' => 'secure deletefile', // delete file from post
  67. '/(\%b+)/spoiler/(\d+)/(\d+)' => 'secure spoiler_image', // spoiler file
  68. '/(\%b)/deletebyip/(\d+)(/global)?' => 'secure deletebyip', // delete all posts by IP address
  69. '/(\%b)/(un)?lock/(\d+)' => 'secure lock', // lock thread
  70. '/(\%b)/(un)?sticky/(\d+)' => 'secure sticky', // sticky thread
  71. '/(\%b)/bump(un)?lock/(\d+)' => 'secure bumplock', // "bumplock" thread
  72. '/themes' => 'themes_list', // manage themes
  73. '/themes/(\w+)' => 'secure_POST theme_configure', // configure/reconfigure theme
  74. '/themes/(\w+)/rebuild' => 'secure theme_rebuild', // rebuild theme
  75. '/themes/(\w+)/uninstall' => 'secure theme_uninstall', // uninstall theme
  76. '/config' => 'secure_POST config', // config editor
  77. '/config/(\%b)' => 'secure_POST config', // config editor
  78. // these pages aren't listed in the dashboard without $config['debug']
  79. '/debug/antispam' => 'debug_antispam',
  80. '/debug/recent' => 'debug_recent_posts',
  81. '/debug/apc' => 'debug_apc',
  82. '/debug/sql' => 'secure_POST debug_sql',
  83. // This should always be at the end:
  84. '/(\%b)/' => 'view_board',
  85. '/(\%b)/' . preg_quote($config['file_index'], '!') => 'view_board',
  86. '/(\%b)/' . str_replace('%d', '(\d+)', preg_quote($config['file_page'], '!')) => 'view_board',
  87. '/(\%b)/' . preg_quote($config['dir']['res'], '!') .
  88. str_replace('%d', '(\d+)', preg_quote($config['file_page50'], '!')) => 'view_thread50',
  89. '/(\%b)/' . preg_quote($config['dir']['res'], '!') .
  90. str_replace('%d', '(\d+)', preg_quote($config['file_page'], '!')) => 'view_thread',
  91. );
  92. if (!$mod) {
  93. $pages = array('!^(.+)?$!' => 'login');
  94. } elseif (isset($_GET['status'], $_GET['r'])) {
  95. header('Location: ' . $_GET['r'], true, (int)$_GET['status']);
  96. exit;
  97. }
  98. if (isset($config['mod']['custom_pages'])) {
  99. $pages = array_merge($pages, $config['mod']['custom_pages']);
  100. }
  101. $new_pages = array();
  102. foreach ($pages as $key => $callback) {
  103. if (is_string($callback) && preg_match('/^secure /', $callback))
  104. $key .= '(/(?P<token>[a-f0-9]{8}))?';
  105. $key = str_replace('\%b', '?P<board>' . sprintf(substr($config['board_path'], 0, -1), $config['board_regex']), $key);
  106. $new_pages[@$key[0] == '!' ? $key : '!^' . $key . '(?:&[^&=]+=[^&]*)*$!u'] = $callback;
  107. }
  108. $pages = $new_pages;
  109. foreach ($pages as $uri => $handler) {
  110. if (preg_match($uri, $query, $matches)) {
  111. $matches = array_slice($matches, 1);
  112. if (isset($matches['board'])) {
  113. $board_match = $matches['board'];
  114. unset($matches['board']);
  115. $key = array_search($board_match, $matches);
  116. if (preg_match('/^' . sprintf(substr($config['board_path'], 0, -1), '(' . $config['board_regex'] . ')') . '$/u', $matches[$key], $board_match)) {
  117. $matches[$key] = $board_match[1];
  118. }
  119. }
  120. if (is_string($handler) && preg_match('/^secure(_POST)? /', $handler, $m)) {
  121. $secure_post_only = isset($m[1]);
  122. if (!$secure_post_only || $_SERVER['REQUEST_METHOD'] == 'POST') {
  123. $token = isset($matches['token']) ? $matches['token'] : (isset($_POST['token']) ? $_POST['token'] : false);
  124. if ($token === false) {
  125. if ($secure_post_only)
  126. error($config['error']['csrf']);
  127. else {
  128. mod_confirm(substr($query, 1));
  129. exit;
  130. }
  131. }
  132. // CSRF-protected page; validate security token
  133. $actual_query = preg_replace('!/([a-f0-9]{8})$!', '', $query);
  134. if ($token != make_secure_link_token(substr($actual_query, 1))) {
  135. error($config['error']['csrf']);
  136. }
  137. }
  138. $handler = preg_replace('/^secure(_POST)? /', '', $handler);
  139. }
  140. if ($config['debug']) {
  141. $debug['mod_page'] = array(
  142. 'req' => $query,
  143. 'match' => $uri,
  144. 'handler' => $handler,
  145. );
  146. $debug['time']['parse_mod_req'] = '~' . round((microtime(true) - $parse_start_time) * 1000, 2) . 'ms';
  147. }
  148. if (is_string($handler)) {
  149. if ($handler[0] == ':') {
  150. header('Location: ' . substr($handler, 1), true, $config['redirect_http']);
  151. } elseif (is_callable("mod_page_$handler")) {
  152. call_user_func_array("mod_page_$handler", $matches);
  153. } elseif (is_callable("mod_$handler")) {
  154. call_user_func_array("mod_$handler", $matches);
  155. } else {
  156. error("Mod page '$handler' not found!");
  157. }
  158. } elseif (is_callable($handler)) {
  159. call_user_func_array($handler, $matches);
  160. } else {
  161. error("Mod page '$handler' not a string, and not callable!");
  162. }
  163. exit;
  164. }
  165. }
  166. error($config['error']['404']);