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.

196 lines
7.2KB

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