PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/mod.php

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