Passed
Push — master ( 572343...b02c13 )
by Jean-Christophe
01:22
created

ContentSecurityManager::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 3
c 2
b 0
f 2
dl 0
loc 4
rs 10
cc 1
nc 1
nop 3
1
<?php
2
namespace Ubiquity\security\csp;
3
4
/**
5
 * Manage Content Security Policies.
6
 * Ubiquity\security\csp$ContentSecurityManager
7
 * This class is part of Ubiquity
8
 *
9
 * @author jc
10
 * @version 1.0.0
11
 *
12
 */
13
class ContentSecurityManager {
14
15
	private static NonceGenerator $nonceGenerator;
16
17
	private static array $csp = [];
18
19
	private static bool $reportOnly;
20
21
	/**
22
	 * Starts the Content Security Policies manager.
23
	 *
24
	 * @param string|null $nonceGeneratorClass
25
	 *        	The class used for generating nonces.
26
	 * @param bool $reportOnly
27
	 * @param callable|null $onNonce
28
	 */
29
	public static function start(string $nonceGeneratorClass = null, bool $reportOnly = false, ?callable $onNonce = null): void {
30
		$nonceGeneratorClass ??= NonceGenerator::class;
31
		self::$nonceGenerator = new $nonceGeneratorClass($onNonce);
32
		self::$reportOnly = $reportOnly;
33
	}
34
35
	/**
36
	 * Returns a new or an existing nonce.
37
	 *
38
	 * @param string $name
39
	 *        	The nonce to create
40
	 * @return string
41
	 */
42
	public static function getNonce(string $name): string {
43
		return self::$nonceGenerator->getNonce($name);
44
	}
45
46
	/**
47
	 *
48
	 * @param string $name
49
	 * @return bool
50
	 */
51
	public static function hasNonce(string $name): bool {
52
		if (isset(self::$nonceGenerator)) {
53
			return self::$nonceGenerator->hasNonce($name);
54
		}
55
		return false;
56
	}
57
58
	/**
59
	 * Checks if the manager is started.
60
	 *
61
	 * @return bool
62
	 */
63
	public static function isStarted(): bool {
64
		return isset(self::$nonceGenerator);
65
	}
66
67
	/**
68
	 * Creates and returns a new ContentSecurity object.
69
	 *
70
	 * @param bool|null $reportOnly
71
	 * @return ContentSecurity
72
	 */
73
	public static function addCsp(?bool $reportOnly = null): ContentSecurity {
74
		return self::$csp[] = new ContentSecurity($reportOnly ?? self::$reportOnly);
75
	}
76
77
	/**
78
	 * Removes all CSP objects.
79
	 */
80
	public static function clearCsp(): void {
81
		self::$csp = [];
82
	}
83
84
	/**
85
	 * Creates a new ContentSecurity object for Ubiquity Webtools.
86
	 *
87
	 * @param bool|null $reportOnly
88
	 * @return ContentSecurity
89
	 */
90
	public static function defaultUbiquity(?bool $reportOnly = null): ContentSecurity {
91
		return self::$csp[] = ContentSecurity::defaultUbiquity()->reportOnly($reportOnly);
92
	}
93
94
	/**
95
	 * Creates a new ContentSecurity object for Ubiquity Webtools in debug mode.
96
	 *
97
	 * @param bool|null $reportOnly
98
	 * @param string $livereloadServer
99
	 * @return ContentSecurity
100
	 */
101
	public static function defaultUbiquityDebug(?bool $reportOnly = null, string $livereloadServer = '127.0.0.1:35729'): ContentSecurity {
102
		return self::$csp[] = ContentSecurity::defaultUbiquityDebug($livereloadServer)->reportOnly($reportOnly);
103
	}
104
105
	/**
106
	 * Adds all Content security policies to headers.
107
	 *
108
	 * @param bool|null $reportOnly
109
	 */
110
	public static function addHeadersToResponse(?bool $reportOnly = null): void {
111
		$reportOnly ??= self::$reportOnly;
112
		foreach (self::$csp as $csp) {
113
			$csp->addHeaderToResponse($reportOnly);
114
		}
115
	}
116
117
	/**
118
	 * Returns the NonceGenerator instance.
119
	 *
120
	 * @return NonceGenerator
121
	 */
122
	public static function getNonceGenerator(): NonceGenerator {
123
		return self::$nonceGenerator;
124
	}
125
126
	/**
127
	 *
128
	 * @return array
129
	 */
130
	public static function getCsp(): array {
131
		return self::$csp;
132
	}
133
134
	/**
135
	 * Returns true if reportOnly header is activated.
136
	 *
137
	 * @return bool
138
	 */
139
	public static function isReportOnly(): bool {
140
		return self::$reportOnly;
141
	}
142
}
143