FrontController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. Minz_Request::forward($url, redirect: Minz_Request::pathInfo() !== '');
  44. } catch (Minz_Exception $e) {
  45. Minz_Log::error($e->getMessage());
  46. self::killApp($e->getMessage());
  47. }
  48. $this->dispatcher = Minz_Dispatcher::getInstance();
  49. }
  50. /**
  51. * Démarre l'application (lance le dispatcher et renvoie la réponse)
  52. */
  53. public function run(): void {
  54. try {
  55. $this->dispatcher->run();
  56. } catch (Minz_BadRequestException $e) {
  57. Minz_Error::error(400, ['error' => [$e->getMessage()]], redirect: true);
  58. } catch (Minz_Exception $e) {
  59. try {
  60. Minz_Log::error($e->getMessage());
  61. } catch (Minz_PermissionDeniedException $e) {
  62. self::killApp($e->getMessage());
  63. }
  64. if ($e instanceof Minz_FileNotExistException ||
  65. $e instanceof Minz_ControllerNotExistException ||
  66. $e instanceof Minz_ControllerNotActionControllerException ||
  67. $e instanceof Minz_ActionException) {
  68. Minz_Error::error(404, ['error' => [$e->getMessage()]], true);
  69. } else {
  70. self::killApp($e->getMessage());
  71. }
  72. }
  73. }
  74. /**
  75. * Kills the programme
  76. */
  77. public static function killApp(string $txt = ''): never {
  78. header('HTTP/1.1 500 Internal Server Error', true, 500);
  79. if (function_exists('errorMessageInfo')) {
  80. //If the application has defined a custom error message function
  81. die(errorMessageInfo('Application problem', $txt));
  82. }
  83. die('### Application problem ###<br />' . "\n" . $txt);
  84. }
  85. private function setReporting(): void {
  86. $envType = getenv('FRESHRSS_ENV');
  87. if ($envType == '') {
  88. $conf = Minz_Configuration::get('system');
  89. $envType = $conf->environment;
  90. }
  91. switch ($envType) {
  92. case 'development':
  93. error_reporting(E_ALL);
  94. ini_set('display_errors', 'On');
  95. ini_set('log_errors', 'On');
  96. break;
  97. case 'silent':
  98. error_reporting(0);
  99. break;
  100. case 'production':
  101. default:
  102. error_reporting(E_ALL);
  103. ini_set('display_errors', 'Off');
  104. ini_set('log_errors', 'On');
  105. break;
  106. }
  107. }
  108. }