4
0

FrontController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <http://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_Exception $e) {
  58. try {
  59. Minz_Log::error($e->getMessage());
  60. } catch (Minz_PermissionDeniedException $e) {
  61. self::killApp($e->getMessage());
  62. }
  63. if ($e instanceof Minz_FileNotExistException ||
  64. $e instanceof Minz_ControllerNotExistException ||
  65. $e instanceof Minz_ControllerNotActionControllerException ||
  66. $e instanceof Minz_ActionException) {
  67. Minz_Error::error(404, ['error' => [$e->getMessage()]], true);
  68. } else {
  69. self::killApp($e->getMessage());
  70. }
  71. }
  72. }
  73. /**
  74. * Kills the programme
  75. */
  76. public static function killApp(string $txt = ''): never {
  77. header('HTTP/1.1 500 Internal Server Error', true, 500);
  78. if (function_exists('errorMessageInfo')) {
  79. //If the application has defined a custom error message function
  80. die(errorMessageInfo('Application problem', $txt));
  81. }
  82. die('### Application problem ###<br />' . "\n" . $txt);
  83. }
  84. private function setReporting(): void {
  85. $envType = getenv('FRESHRSS_ENV');
  86. if ($envType == '') {
  87. $conf = Minz_Configuration::get('system');
  88. $envType = $conf->environment;
  89. }
  90. switch ($envType) {
  91. case 'development':
  92. error_reporting(E_ALL);
  93. ini_set('display_errors', 'On');
  94. ini_set('log_errors', 'On');
  95. break;
  96. case 'silent':
  97. error_reporting(0);
  98. break;
  99. case 'production':
  100. default:
  101. error_reporting(E_ALL);
  102. ini_set('display_errors', 'Off');
  103. ini_set('log_errors', 'On');
  104. break;
  105. }
  106. }
  107. }