ActionController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 type.
  20. * @var class-string
  21. */
  22. public static $viewType = 'Minz_View';
  23. public function __construct () {
  24. $this->csp_policies = self::$csp_default;
  25. $view = null;
  26. if (class_exists(self::$viewType)) {
  27. $view = new self::$viewType();
  28. if (!($view instanceof Minz_View)) {
  29. $view = null;
  30. }
  31. }
  32. $this->view = $view ?? new Minz_View();
  33. $view_path = Minz_Request::controllerName() . '/' . Minz_Request::actionName() . '.phtml';
  34. $this->view->_path($view_path);
  35. $this->view->attributeParams ();
  36. }
  37. /**
  38. * Getteur
  39. */
  40. public function view(): Minz_View {
  41. return $this->view;
  42. }
  43. /**
  44. * Set default CSP policies.
  45. * @param array<string,string> $policies An array where keys are directives and values are sources.
  46. */
  47. public static function _defaultCsp(array $policies): void {
  48. if (!isset($policies['default-src'])) {
  49. Minz_Log::warning('Default CSP policy is not declared', ADMIN_LOG);
  50. }
  51. self::$csp_default = $policies;
  52. }
  53. /**
  54. * Set CSP policies.
  55. *
  56. * A default-src directive should always be given.
  57. *
  58. * References:
  59. * - https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
  60. * - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/default-src
  61. *
  62. * @param array<string,string> $policies An array where keys are directives and values are sources.
  63. */
  64. protected function _csp(array $policies): void {
  65. if (!isset($policies['default-src'])) {
  66. $action = Minz_Request::controllerName() . '#' . Minz_Request::actionName();
  67. Minz_Log::warning(
  68. "Default CSP policy is not declared for action {$action}.",
  69. ADMIN_LOG
  70. );
  71. }
  72. $this->csp_policies = $policies;
  73. }
  74. /**
  75. * Send HTTP Content-Security-Policy header based on declared policies.
  76. */
  77. public function declareCspHeader(): void {
  78. $policies = [];
  79. foreach ($this->csp_policies as $directive => $sources) {
  80. $policies[] = $directive . ' ' . $sources;
  81. }
  82. header('Content-Security-Policy: ' . implode('; ', $policies));
  83. }
  84. /**
  85. * Méthodes à redéfinir (ou non) par héritage
  86. * firstAction est la première méthode exécutée par le Dispatcher
  87. * lastAction est la dernière
  88. */
  89. public function init(): void { }
  90. public function firstAction(): void { }
  91. public function lastAction(): void { }
  92. }