1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* src/Directives/Contact.php |
4
|
|
|
* |
5
|
|
|
* @package php-security-txt |
6
|
|
|
* @author Austin Heap <[email protected]> |
7
|
|
|
* @version v0.4.0 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types = 1); |
11
|
|
|
|
12
|
|
|
namespace AustinHeap\Security\Txt\Directives; |
13
|
|
|
|
14
|
|
|
use AustinHeap\Security\Txt\SecurityTxt; |
15
|
|
|
use Exception; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Contact |
19
|
|
|
* |
20
|
|
|
* @link https://github.com/austinheap/php-security-txt |
21
|
|
|
* @link https://packagist.org/packages/austinheap/php-security-txt |
22
|
|
|
* @link https://austinheap.github.io/php-security-txt/classes/AustinHeap.Security.Txt.SecurityTxt.html |
23
|
|
|
*/ |
24
|
|
|
trait Contact |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The security contact(s). |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $contacts = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set the contacts. |
36
|
|
|
* |
37
|
|
|
* @param array $contacts |
38
|
|
|
* |
39
|
|
|
* @return SecurityTxt |
40
|
|
|
*/ |
41
|
|
|
public function setContacts(array $contacts): SecurityTxt |
42
|
|
|
{ |
43
|
|
|
if (!$this->validContacts($contacts, true)) { |
44
|
|
|
throw new Exception('Contacts array must contain well-formed e-mails and/or URLs.'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->contacts = $contacts; |
48
|
|
|
|
49
|
|
|
return $this; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the contacts. |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function getContacts(): array |
58
|
|
|
{ |
59
|
|
|
return is_null($this->contacts) ? [] : array_keys($this->contacts); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add a contact. |
64
|
|
|
* |
65
|
|
|
* @param string $contact |
66
|
|
|
* |
67
|
|
|
* @return SecurityTxt |
68
|
|
|
*/ |
69
|
|
|
public function addContact(string $contact): SecurityTxt |
70
|
|
|
{ |
71
|
|
|
return $this->addContacts([$contact]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Add contacts. |
76
|
|
|
* |
77
|
|
|
* @param array $contacts |
78
|
|
|
* |
79
|
|
|
* @return SecurityTxt |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function addContacts(array $contacts): SecurityTxt |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
if (!$this->validContacts($contacts)) { |
84
|
|
|
throw new Exception('Contacts must be well-formed e-mails and/or URLs.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
foreach ($contacts as $contact) { |
88
|
|
|
$this->contacts[$contact] = true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this; |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Validates a contact. |
96
|
|
|
* |
97
|
|
|
* @param string $contact |
98
|
|
|
* |
99
|
|
|
* @string string $contact |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function validContact(string $contact): bool |
103
|
|
|
{ |
104
|
|
|
return filter_var($contact, FILTER_VALIDATE_EMAIL) !== false || |
105
|
|
|
filter_var($contact, FILTER_VALIDATE_URL) !== false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Validates an array of contacts. |
110
|
|
|
* |
111
|
|
|
* @param array $contacts |
112
|
|
|
* @param bool $use_keys |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function validContacts(array $contacts, bool $use_keys = false): bool |
117
|
|
|
{ |
118
|
|
|
if ($use_keys) { |
119
|
|
|
$contacts = array_keys($contacts); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
foreach ($contacts as $contact) { |
123
|
|
|
if (!$this->validContact($contact)) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return true; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Remove a contact. |
133
|
|
|
* |
134
|
|
|
* @param string $contact |
135
|
|
|
* |
136
|
|
|
* @return SecurityTxt |
137
|
|
|
*/ |
138
|
|
|
public function removeContact(string $contact): SecurityTxt |
139
|
|
|
{ |
140
|
|
|
$this->removeContacts([$contact]); |
141
|
|
|
|
142
|
|
|
return $this; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Remove contacts. |
147
|
|
|
* |
148
|
|
|
* @param array $contacts |
149
|
|
|
* |
150
|
|
|
* @return SecurityTxt |
151
|
|
|
*/ |
152
|
|
View Code Duplication |
public function removeContacts(array $contacts): SecurityTxt |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
if (!$this->hasContacts($contacts)) { |
155
|
|
|
throw new Exception('Cannot remove contacts that do not exist.'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
foreach ($contacts as $contact) { |
159
|
|
|
unset($this->contacts[$contact]); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $this; |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Determines if a contact exists. |
167
|
|
|
* |
168
|
|
|
* @param string $contact |
169
|
|
|
* |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
|
|
public function hasContact(string $contact): bool |
173
|
|
|
{ |
174
|
|
|
return array_key_exists($contact, $this->contacts); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Determines if an array of contacts exists. |
179
|
|
|
* |
180
|
|
|
* @param array $contacts |
181
|
|
|
* |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
|
|
public function hasContacts(array $contacts): bool |
185
|
|
|
{ |
186
|
|
|
foreach ($contacts as $contact) { |
187
|
|
|
if (!$this->hasContact($contact)) { |
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return true; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
} |
196
|
|
|
|