1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FMUP\Response\Header; |
4
|
|
|
|
5
|
|
|
use FMUP\Response\Header; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class XFrameOptions |
9
|
|
|
* |
10
|
|
|
* @package FMUP\Response\Header |
11
|
|
|
*/ |
12
|
|
|
class XFrameOptions extends Header |
13
|
|
|
{ |
14
|
|
|
const TYPE = 'X-Frame-Options'; |
15
|
|
|
const OPTIONS_DENY = 'Deny'; |
16
|
|
|
const OPTIONS_SAME_ORIGIN = 'Sameorigin'; |
17
|
|
|
const OPTIONS_ALLOW_FROM = 'ALLOW_FROM'; |
18
|
|
|
const OPTIONS_ALLOW_FROM_URI_DEFAULT = '*'; |
19
|
|
|
|
20
|
|
|
private $options = self::OPTIONS_DENY; |
21
|
|
|
private $uri = [self::OPTIONS_ALLOW_FROM_URI_DEFAULT]; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* XFrameOptions constructor. |
26
|
|
|
* @param $options |
27
|
|
|
* @param array $uri |
28
|
|
|
*/ |
29
|
1 |
|
public function __construct($options = self::OPTIONS_DENY, array $uri = [self::OPTIONS_ALLOW_FROM_URI_DEFAULT]) |
30
|
|
|
{ |
31
|
1 |
|
$this->setOptions($options)->setUri($uri); |
32
|
1 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Value returned in the header |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
4 |
|
public function getValue() |
39
|
|
|
{ |
40
|
4 |
|
$options = $this->getOptions(); |
41
|
4 |
|
$return = $options; |
42
|
4 |
|
if ($options == self::OPTIONS_ALLOW_FROM) { |
43
|
3 |
|
$return = ''; |
44
|
3 |
|
foreach ($this->getUri() as $url) { |
45
|
3 |
|
$return .= $options . ' ' . $url . ';'; |
46
|
|
|
} |
47
|
|
|
} |
48
|
4 |
|
return $return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
2 |
|
public function getOptions() |
55
|
|
|
{ |
56
|
2 |
|
return $this->options; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $options |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
2 |
|
public function setOptions($options = self::OPTIONS_DENY) |
64
|
|
|
{ |
65
|
2 |
|
$this->options = is_string($options) ? $options : self::OPTIONS_DENY; |
66
|
2 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns allowed Uri |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
3 |
|
public function getUri() |
74
|
|
|
{ |
75
|
3 |
|
return $this->uri; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Define list of url to be allowed |
80
|
|
|
* @param string[] $uri |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
2 |
|
public function setUri(array $uri = []) |
84
|
|
|
{ |
85
|
2 |
|
$this->uri = $uri ?: [self::OPTIONS_ALLOW_FROM_URI_DEFAULT]; |
86
|
2 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Type for the header. Can be used to determine header to send |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
2 |
|
public function getType() |
94
|
|
|
{ |
95
|
2 |
|
return self::TYPE; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Displays the header |
100
|
|
|
* @return Header |
101
|
|
|
*/ |
102
|
2 |
|
public function render() |
103
|
|
|
{ |
104
|
2 |
|
return ($this->getUri() != [self::OPTIONS_ALLOW_FROM_URI_DEFAULT] ? parent::render() : $this); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|