/index.php

https://gitlab.com/xe/Carbon-Forum · PHP · 112 lines · 96 code · 9 blank · 7 comment · 18 complexity · 32f710703f3ccdf88c95681156bd2fdd MD5 · raw file

  1. <?php
  2. require(__DIR__ . '/common.php');
  3. $HTTPMethod = $_SERVER['REQUEST_METHOD'];
  4. if (!in_array($HTTPMethod, array('GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'OPTIONS'))) {
  5. exit('Unsupport HTTP method');
  6. }
  7. if ($Config['WebsitePath']) {
  8. $WebsitePathPosition = strpos($RequestURI, $Config['WebsitePath']);
  9. if ($WebsitePathPosition !== 0) {
  10. exit('WebsitePath Error!');
  11. } else {
  12. $ShortRequestURI = substr($RequestURI, strlen($Config['WebsitePath']));
  13. }
  14. } else {
  15. $ShortRequestURI = $RequestURI;
  16. }
  17. $NotFound = true;
  18. $HTTPParameters = array();
  19. if (in_array($HTTPMethod, array('PUT', 'DELETE', 'OPTIONS'))) {
  20. parse_str(file_get_contents('php://input'), $HTTPParameters);
  21. }
  22. $Routes = array();
  23. //Support HTTP Method: GET / POST / PUT / DELETE / OPTIONS
  24. //这里是Routes Start
  25. $Routes['GET']['/'] = 'home';
  26. $Routes['POST']['/'] = 'home'; //Delete later
  27. $Routes['GET']['/dashboard'] = 'dashboard';
  28. $Routes['POST']['/dashboard'] = 'dashboard';
  29. $Routes['GET']['/favorites(/page/(?<page>[0-9]+))?'] = 'favorites';
  30. $Routes['GET']['/forgot'] = 'forgot';
  31. $Routes['POST']['/forgot'] = 'forgot';
  32. $Routes['GET']['/goto/(?<topic_id>[0-9]+)-(?<post_id>[0-9]+)'] = 'goto';
  33. $Routes['POST']['/json/(?<action>[0-9a-z_\-]+)'] = 'json';
  34. $Routes['GET']['/json/(?<action>[0-9a-z_\-]+)'] = 'json';
  35. $Routes['GET']['/login'] = 'login';
  36. $Routes['POST']['/login'] = 'login';
  37. $Routes['POST']['/manage'] = 'manage';
  38. $Routes['GET']['/new'] = 'new';
  39. $Routes['POST']['/new'] = 'new';
  40. $Routes['GET']['/notifications'] = 'notifications';
  41. $Routes['GET']['/oauth-(?<app_id>[0-9]+)'] = 'oauth';
  42. $Routes['POST']['/oauth-(?<app_id>[0-9]+)'] = 'oauth';
  43. $Routes['GET']['/page/(?<page>[0-9]+)'] = 'home';
  44. $Routes['POST']['/page/(?<page>[0-9]+)'] = 'home'; //Delete later
  45. $Routes['GET']['/register'] = 'register';
  46. $Routes['POST']['/register'] = 'register';
  47. $Routes['GET']['/reply'] = 'reply';
  48. $Routes['POST']['/reply'] = 'reply';
  49. $Routes['GET']['/reset_password/(?<access_token>.*?)'] = 'reset_password';
  50. $Routes['POST']['/reset_password/(?<access_token>.*?)'] = 'reset_password';
  51. $Routes['GET']['/robots.txt'] = 'robots';
  52. $Routes['GET']['/search.xml'] = 'open_search';
  53. $Routes['GET']['/search/(?<keyword>.*?)(/page/(?<page>[0-9]*))?'] = 'search';
  54. $Routes['GET']['/settings'] = 'settings';
  55. $Routes['POST']['/settings'] = 'settings';
  56. $Routes['GET']['/sitemap-(?<action>topics|pages|tags|users|index)(-(?<page>[0-9]+))?.xml'] = 'sitemap';
  57. $Routes['GET']['/statistics'] = 'statistics';
  58. $Routes['GET']['/t/(?<id>[0-9]+)(-(?<page>[0-9]*))?'] = 'topic';
  59. $Routes['POST']['/t/(?<id>[0-9]+)(-(?<page>[0-9]*))?'] = 'topic'; //Delete later
  60. $Routes['GET']['/tag/(?<name>.*?)(/page/(?<page>[0-9]*))?'] = 'tag';
  61. $Routes['GET']['/tags/following(/page/(?<page>[0-9]*))?'] = 'favorite_tags';
  62. $Routes['GET']['/tags(/page/(?<page>[0-9]*))?'] = 'tags';
  63. $Routes['GET']['/u/(?<username>.*?)'] = 'user';
  64. $Routes['GET']['/users/following(/page/(?<page>[0-9]*))?'] = 'favorite_users';
  65. $Routes['GET']['/upload_controller'] = 'upload_controller';
  66. $Routes['POST']['/upload_controller'] = 'upload_controller';
  67. $Routes['GET']['/view-(?<view>desktop|mobile)'] = 'view';
  68. //这里是Routes End
  69. foreach ($Routes as $Method => $SubRoutes) {
  70. if ($Method === $HTTPMethod) {
  71. $ParametersVariableName = '_' . $Method;
  72. foreach ($SubRoutes as $URL => $Controller) {
  73. if (preg_match("#^" . $URL . "$#i", $ShortRequestURI, $Parameters)) {
  74. $NotFound = false;
  75. $Parameters = array_merge($Parameters, $HTTPParameters);
  76. //var_dump($Parameters);
  77. foreach ($Parameters as $Key => $Value) {
  78. if (!is_int($Key)) {
  79. $$ParametersVariableName[$Key] = urldecode($Value);
  80. $_REQUEST[$Key] = urldecode($Value);
  81. }
  82. }
  83. //$MicroTime = explode(' ', microtime());
  84. //echo number_format(($MicroTime[1] + $MicroTime[0] - $StartTime), 6) * 1000;
  85. $UrlPath = $Controller;
  86. break 2;
  87. }
  88. }
  89. break;
  90. }
  91. }
  92. if ($NotFound === true) {
  93. require(__DIR__ . '/404.php');
  94. exit();
  95. }
  96. if ($Config['MobileDomainName'] && $_SERVER['HTTP_HOST'] != $Config['MobileDomainName'] && $CurView == 'mobile' && !$IsApp && $UrlPath != 'view') {
  97. //如果是手机,则跳转到移动版
  98. header("HTTP/1.1 302 Moved Temporarily");
  99. header("Status: 302 Moved Temporarily");
  100. header('Location: ' . $CurProtocol . $Config['MobileDomainName'] . $RequestURI);
  101. exit();
  102. }
  103. require(__DIR__ . '/controller/' . $UrlPath . '.php');