ActionController.php 2.6 KB

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