Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ContentSecurityManager.php
Go to the documentation of this file.
1<?php
3
5
16
18
19 private static array $csp = [];
20
21 private static bool $reportOnly;
22
23 private static string $hashAlgo = 'sha256';
24
25 private static $onGenerate;
26
35 public static function start(string $nonceGeneratorClass = null, bool $reportOnly = false, ?callable $onGenerate = null): void {
36 $nonceGeneratorClass ??= NonceGenerator::class;
37 self::$onGenerate = $onGenerate;
38 self::$nonceGenerator = new $nonceGeneratorClass($onGenerate);
39 self::$reportOnly = $reportOnly;
40 }
41
49 public static function getNonce(string $name): string {
50 return self::$nonceGenerator->getNonce($name);
51 }
52
62 public static function getHash(string $name, string $code, string $algo = 'sha256'): string {
63 $code = \preg_replace('/\r\n/', '\n', $code);
64 $hash = \base64_encode(\hash($algo, $code, true));
65 $hash = "$algo-$hash";
66 if (isset(self::$onGenerate) && ! URequest::isAjax()) {
67 $onG = self::$onGenerate;
68 $onG($name, $hash, $algo);
69 }
70 return $hash;
71 }
72
78 public static function hasNonce(string $name): bool {
79 if (isset(self::$nonceGenerator)) {
80 return self::$nonceGenerator->hasNonce($name);
81 }
82 return false;
83 }
84
90 public static function isStarted(): bool {
91 return isset(self::$nonceGenerator);
92 }
93
100 public static function addCsp(?bool $reportOnly = null): ContentSecurity {
101 return self::$csp[] = new ContentSecurity($reportOnly ?? self::$reportOnly);
102 }
103
110 public static function defaultCsp(?bool $reportOnly = null): ContentSecurity {
111 return self::$csp['default'] ??= new ContentSecurity($reportOnly ?? self::$reportOnly);
112 }
113
117 public static function clearCsp(): void {
118 self::$csp = [];
119 }
120
127 public static function defaultUbiquity(?bool $reportOnly = null): ContentSecurity {
128 return self::$csp['defaultUbiquity'] ??= ContentSecurity::defaultUbiquity()->reportOnly($reportOnly);
129 }
130
138 public static function defaultUbiquityDebug(?bool $reportOnly = null, string $livereloadServer = '127.0.0.1:35729'): ContentSecurity {
139 return self::$csp['defaultUbiquity'] ??= ContentSecurity::defaultUbiquityDebug($livereloadServer)->reportOnly($reportOnly);
140 }
141
147 public static function addHeadersToResponse(?bool $reportOnly = null): void {
148 $reportOnly ??= self::$reportOnly;
149 foreach (self::$csp as $csp) {
150 $csp->addHeaderToResponse($reportOnly);
151 }
152 }
153
159 public static function getNonceGenerator(): NonceGenerator {
160 return self::$nonceGenerator;
161 }
162
167 public static function getCsp(): array {
168 return self::$csp;
169 }
170
176 public static function isReportOnly(): bool {
177 return self::$reportOnly;
178 }
179
184 public static function getHashAlgo(): string {
185 return ContentSecurityManager::$hashAlgo;
186 }
187
192 public static function setHashAlgo(string $hashAlgo) {
193 ContentSecurityManager::$hashAlgo = $hashAlgo;
194 }
195
200 public static function setOnGenerate(callable $onGenerate) {
201 ContentSecurityManager::$onGenerate = $onGenerate;
202 }
203}
Creates a Content Security Policy object.
static addCsp(?bool $reportOnly=null)
Creates and returns a new ContentSecurity object.
static getHash(string $name, string $code, string $algo='sha256')
Generates a hash and add it to a directive.
static isReportOnly()
Returns true if reportOnly header is activated.
static getNonceGenerator()
Returns the NonceGenerator instance.
static addHeadersToResponse(?bool $reportOnly=null)
Adds all Content security policies to headers.
static defaultUbiquityDebug(?bool $reportOnly=null, string $livereloadServer='127.0.0.1:35729')
Creates a new ContentSecurity object for Ubiquity Webtools in debug mode.
static defaultUbiquity(?bool $reportOnly=null)
Creates a new ContentSecurity object for Ubiquity Webtools.
static getNonce(string $name)
Returns a new or an existing nonce.
static start(string $nonceGeneratorClass=null, bool $reportOnly=false, ?callable $onGenerate=null)
Starts the Content Security Policies manager.
static defaultCsp(?bool $reportOnly=null)
Returns a default ContentSecurity object.
static isStarted()
Checks if the manager is started.
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.
Definition URequest.php:18