ActionController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * The Minz_ActionController class is a controller in the MVC paradigm
  8. */
  9. class Minz_ActionController {
  10. /** @var array<string,string> */
  11. private static $csp_default = [
  12. 'default-src' => "'self'",
  13. ];
  14. /** @var array<string,string> */
  15. private $csp_policies;
  16. /** @var Minz_View */
  17. protected $view;
  18. /**
  19. * Gives the possibility to override the default view model type.
  20. * @var class-string
  21. * @deprecated Use constructor with view type instead
  22. */
  23. public static $defaultViewType = Minz_View::class;
  24. /**
  25. * @phpstan-param class-string|'' $viewType
  26. * @param string $viewType Name of the class (inheriting from Minz_View) to use for the view model
  27. */
  28. public function __construct(string $viewType = '') {
  29. $this->csp_policies = self::$csp_default;
  30. $view = null;
  31. if ($viewType !== '' && class_exists($viewType)) {
  32. $view = new $viewType();
  33. if (!($view instanceof Minz_View)) {
  34. $view = null;
  35. }
  36. }
  37. if ($view === null && class_exists(self::$defaultViewType)) {
  38. $view = new self::$defaultViewType();
  39. if (!($view instanceof Minz_View)) {
  40. $view = null;
  41. }
  42. }
  43. $this->view = $view ?? new Minz_View();
  44. $view_path = Minz_Request::controllerName() . '/' . Minz_Request::actionName() . '.phtml';
  45. $this->view->_path($view_path);
  46. $this->view->attributeParams ();
  47. }
  48. /**
  49. * Getteur
  50. */
  51. public function view(): Minz_View {
  52. return $this->view;
  53. }
  54. /**
  55. * Set default CSP policies.
  56. * @param array<string,string> $policies An array where keys are directives and values are sources.
  57. */
  58. public static function _defaultCsp(array $policies): void {
  59. if (!isset($policies['default-src'])) {
  60. Minz_Log::warning('Default CSP policy is not declared', ADMIN_LOG);
  61. }
  62. self::$csp_default = $policies;
  63. }
  64. /**
  65. * Set CSP policies.
  66. *
  67. * A default-src directive should always be given.
  68. *
  69. * References:
  70. * - https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
  71. * - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/default-src
  72. *
  73. * @param array<string,string> $policies An array where keys are directives and values are sources.
  74. */
  75. protected function _csp(array $policies): void {
  76. if (!isset($policies['default-src'])) {
  77. $action = Minz_Request::controllerName() . '#' . Minz_Request::actionName();
  78. Minz_Log::warning(
  79. "Default CSP policy is not declared for action {$action}.",
  80. ADMIN_LOG
  81. );
  82. }
  83. $this->csp_policies = $policies;
  84. }
  85. /**
  86. * Send HTTP Content-Security-Policy header based on declared policies.
  87. */
  88. public function declareCspHeader(): void {
  89. $policies = [];
  90. foreach ($this->csp_policies as $directive => $sources) {
  91. $policies[] = $directive . ' ' . $sources;
  92. }
  93. header('Content-Security-Policy: ' . implode('; ', $policies));
  94. }
  95. /**
  96. * Méthodes à redéfinir (ou non) par héritage
  97. * firstAction est la première méthode exécutée par le Dispatcher
  98. * lastAction est la dernière
  99. */
  100. public function init(): void { }
  101. public function firstAction(): void { }
  102. public function lastAction(): void { }
  103. }