FrontController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare(strict_types=1);
  3. # ***** BEGIN LICENSE BLOCK *****
  4. # MINZ - a free PHP Framework like Zend Framework
  5. # Copyright (C) 2011 Marien Fressinaud
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. # ***** END LICENSE BLOCK *****
  21. /**
  22. * The Minz_FrontController class is the framework Dispatcher.
  23. * It runs the application.
  24. * It is generally invoqued by an index.php file at the root.
  25. */
  26. class Minz_FrontController {
  27. protected Minz_Dispatcher $dispatcher;
  28. /**
  29. * Constructeur
  30. * Initialise le dispatcher, met à jour la Request
  31. */
  32. public function __construct() {
  33. try {
  34. $this->setReporting();
  35. Minz_Request::init();
  36. // Build the current request's URL and forward to it directly.
  37. // If needed, a redirect is issued instead to correct the public relative path.
  38. $url = Minz_Url::build();
  39. $url['params'] = array_merge(
  40. empty($url['params']) || !is_array($url['params']) ? [] : $url['params'],
  41. array_filter($_POST, 'is_string', ARRAY_FILTER_USE_KEY)
  42. );
  43. $pathInfo = $_SERVER['PATH_INFO'] ?? $_SERVER['ORIG_PATH_INFO'] ?? '';
  44. Minz_Request::forward($url, redirect: $pathInfo !== '');
  45. } catch (Minz_Exception $e) {
  46. Minz_Log::error($e->getMessage());
  47. self::killApp($e->getMessage());
  48. }
  49. $this->dispatcher = Minz_Dispatcher::getInstance();
  50. }
  51. /**
  52. * Démarre l'application (lance le dispatcher et renvoie la réponse)
  53. */
  54. public function run(): void {
  55. try {
  56. $this->dispatcher->run();
  57. } catch (Minz_BadRequestException $e) {
  58. Minz_Error::error(400, ['error' => [$e->getMessage()]], redirect: true);
  59. } catch (Minz_Exception $e) {
  60. try {
  61. Minz_Log::error($e->getMessage());
  62. } catch (Minz_PermissionDeniedException $e) {
  63. self::killApp($e->getMessage());
  64. }
  65. if ($e instanceof Minz_FileNotExistException ||
  66. $e instanceof Minz_ControllerNotExistException ||
  67. $e instanceof Minz_ControllerNotActionControllerException ||
  68. $e instanceof Minz_ActionException) {
  69. Minz_Error::error(404, ['error' => [$e->getMessage()]], true);
  70. } else {
  71. self::killApp($e->getMessage());
  72. }
  73. }
  74. }
  75. /**
  76. * Kills the programme
  77. */
  78. public static function killApp(string $txt = ''): never {
  79. header('HTTP/1.1 500 Internal Server Error', true, 500);
  80. if (function_exists('errorMessageInfo')) {
  81. //If the application has defined a custom error message function
  82. die(errorMessageInfo('Application problem', $txt));
  83. }
  84. die('### Application problem ###<br />' . "\n" . $txt);
  85. }
  86. private function setReporting(): void {
  87. $envType = getenv('FRESHRSS_ENV');
  88. if ($envType == '') {
  89. $conf = Minz_Configuration::get('system');
  90. $envType = $conf->environment;
  91. }
  92. switch ($envType) {
  93. case 'development':
  94. error_reporting(E_ALL);
  95. ini_set('display_errors', 'On');
  96. ini_set('log_errors', 'On');
  97. break;
  98. case 'silent':
  99. error_reporting(0);
  100. break;
  101. case 'production':
  102. default:
  103. error_reporting(E_ALL);
  104. ini_set('display_errors', 'Off');
  105. ini_set('log_errors', 'On');
  106. break;
  107. }
  108. }
  109. }