ActionController.php 2.5 KB

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