Passed
Push — master ( cd7372...773ce9 )
by Roeland
08:46
created

EmptyFeaturePolicy::buildPolicy()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 46
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 32
nc 64
nop 0
dl 0
loc 46
rs 8.4746
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]>
5
 *
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCP\AppFramework\Http;
26
27
/**
28
 * Class EmptyFeaturePolicy is a simple helper which allows applications
29
 * to modify the FeaturePolicy sent by Nextcloud. Per default the policy
30
 * is forbidding everything.
31
 *
32
 * As alternative with sane exemptions look at FeaturePolicy
33
 *
34
 * @see \OCP\AppFramework\Http\FeaturePolicy
35
 * @package OCP\AppFramework\Http
36
 * @since 17.0.0
37
 */
38
class EmptyFeaturePolicy {
39
40
	/** @var string[] of allowed domains to autoplay media */
41
	protected $autoplayDomains = null;
42
43
	/** @var string[] of allowed domains that can access the camera */
44
	protected $cameraDomains = null;
45
46
	/** @var string[] of allowed domains that can use fullscreen */
47
	protected $fullscreenDomains = null;
48
49
	/** @var string[] of allowed domains that can use the geolocation of the device */
50
	protected $geolocationDomains = null;
51
52
	/** @var string[] of allowed domains that can use the microphone */
53
	protected $microphoneDomains = null;
54
55
	/** @var string[] of allowed domains that can use the payment API */
56
	protected $paymentDomains = null;
57
58
	/**
59
	 * Allows to use autoplay from a specific domain. Use * to allow from all domains.
60
	 *
61
	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
62
	 * @return $this
63
	 * @since 17.0.0
64
	 */
65
	public function addAllowedAutoplayDomain(string $domain): self {
66
		$this->autoplayDomains[] = $domain;
67
		return $this;
68
	}
69
70
	/**
71
	 * Allows to use the camera on a specific domain. Use * to allow from all domains
72
	 *
73
	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
74
	 * @return $this
75
	 * @since 17.0.0
76
	 */
77
	public function addAllowedCameraDomain(string $domain): self {
78
		$this->cameraDomains[] = $domain;
79
		return $this;
80
	}
81
82
	/**
83
	 * Allows the full screen functionality to be used on a specific domain. Use * to allow from all domains
84
	 *
85
	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
86
	 * @return $this
87
	 * @since 17.0.0
88
	 */
89
	public function addAllowedFullScreenDomain(string $domain): self {
90
		$this->fullscreenDomains[] = $domain;
91
		return $this;
92
	}
93
94
	/**
95
	 * Allows to use the geolocation on a specific domain. Use * to allow from all domains
96
	 *
97
	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
98
	 * @return $this
99
	 * @since 17.0.0
100
	 */
101
	public function addAllowedGeoLocationDomain(string $domain): self {
102
		$this->geolocationDomains[] = $domain;
103
		return $this;
104
	}
105
106
	/**
107
	 * Allows to use the microphone on a specific domain. Use * to allow from all domains
108
	 *
109
	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
110
	 * @return $this
111
	 * @since 17.0.0
112
	 */
113
	public function addAllowedMicrophoneDomain(string $domain): self {
114
		$this->microphoneDomains[] = $domain;
115
		return $this;
116
	}
117
118
	/**
119
	 * Allows to use the payment API on a specific domain. Use * to allow from all domains
120
	 *
121
	 * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
122
	 * @return $this
123
	 * @since 17.0.0
124
	 */
125
	public function addAllowedPaymentDomain(string $domain): self {
126
		$this->paymentDomains[] = $domain;
127
		return $this;
128
	}
129
130
	/**
131
	 * Get the generated Feature-Policy as a string
132
	 *
133
	 * @return string
134
	 * @since 17.0.0
135
	 */
136
	public function buildPolicy(): string {
137
		$policy = '';
138
139
		if (empty($this->autoplayDomains)) {
140
			$policy .= "autoplay 'none';";
141
		} else {
142
			$policy .= 'autoplay ' . implode(' ', $this->autoplayDomains);
143
			$policy .= ';';
144
		}
145
146
		if (empty($this->cameraDomains)) {
147
			$policy .= "camera 'none';";
148
		} else {
149
			$policy .= 'camera ' . implode(' ', $this->cameraDomains);
150
			$policy .= ';';
151
		}
152
153
		if (empty($this->fullscreenDomains)) {
154
			$policy .= "fullscreen 'none';";
155
		} else {
156
			$policy .= 'fullscreen ' . implode(' ', $this->fullscreenDomains);
157
			$policy .= ';';
158
		}
159
160
		if (empty($this->geolocationDomains)) {
161
			$policy .= "geolocation 'none';";
162
		} else {
163
			$policy .= 'geolocation ' . implode(' ', $this->geolocationDomains);
164
			$policy .= ';';
165
		}
166
167
		if (empty($this->microphoneDomains)) {
168
			$policy .= "microphone 'none';";
169
		} else {
170
			$policy .= 'microphone ' . implode(' ', $this->microphoneDomains);
171
			$policy .= ';';
172
		}
173
174
		if (empty($this->paymentDomains)) {
175
			$policy .= "payment 'none';";
176
		} else {
177
			$policy .= 'payment ' . implode(' ', $this->paymentDomains);
178
			$policy .= ';';
179
		}
180
181
		return rtrim($policy, ';');
182
	}
183
}
184