1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* src/SecurityTxtHelper.php |
4
|
|
|
* |
5
|
|
|
* @package laravel-security-txt |
6
|
|
|
* @author Austin Heap <[email protected]> |
7
|
|
|
* @version v0.3.0 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace AustinHeap\Security\Txt; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* SecurityTxtHelper |
16
|
|
|
* |
17
|
|
|
* @link https://github.com/austinheap/laravel-security-txt |
18
|
|
|
* @link https://packagist.org/packages/austinheap/laravel-security-txt |
19
|
|
|
* @link https://austinheap.github.io/laravel-security-txt/classes/AustinHeap.Security.Txt.SecurityTxtHelper.html |
20
|
|
|
*/ |
21
|
|
|
class SecurityTxtHelper |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Internal version number. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
const VERSION = '0.3.0'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Internal SecurityTxt object. |
33
|
|
|
* |
34
|
|
|
* @var \AustinHeap\Security\Txt\Writer |
35
|
|
|
*/ |
36
|
|
|
protected $writer = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Internal array of log entries. |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $logEntries = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Enable built-in cache. |
47
|
|
|
* |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
protected $cache = false; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Minutes to cache output. |
54
|
|
|
* |
55
|
|
|
* @var int |
56
|
|
|
*/ |
57
|
|
|
protected $cacheTime = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Cache key to use. |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $cacheKey = 'cache:AustinHeap\Security\Txt\SecurityTxt'; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create a new SecurityTxtHelper instance. |
68
|
|
|
* |
69
|
|
|
* @return \AustinHeap\Security\Txt\SecurityTxtHelper |
70
|
|
|
*/ |
71
|
|
|
public function __construct() |
72
|
|
|
{ |
73
|
|
|
$this->writer = new Writer(); |
74
|
|
|
|
75
|
|
|
$keys = [ |
76
|
|
|
'security-txt.enabled' => ['validator' => 'is_bool', 'setter' => 'setEnabled', 'self' => true], |
77
|
|
|
'security-txt.debug' => ['validator' => 'is_bool', 'setter' => 'setDebug'], |
78
|
|
|
'security-txt.cache' => ['validator' => 'is_bool', 'setter' => 'setCache', 'self' => true], |
79
|
|
|
'security-txt.cache-time' => ['validator' => 'is_numeric', 'setter' => 'setCacheTime', 'self' => true], |
80
|
|
|
'security-txt.cache-key' => ['validator' => 'is_string', 'setter' => 'setCacheKey', 'self' => true], |
81
|
|
|
'security-txt.comments' => ['validator' => 'is_bool', 'setter' => 'setComments'], |
82
|
|
|
'security-txt.contacts' => ['validator' => 'is_array', 'setter' => 'setContacts'], |
83
|
|
|
'security-txt.encryption' => ['validator' => 'is_string', 'setter' => 'setEncryption'], |
84
|
|
|
'security-txt.disclosure' => ['validator' => 'is_string', 'setter' => 'setDisclosure'], |
85
|
|
|
'security-txt.acknowledgement' => ['validator' => 'is_string', 'setter' => 'setAcknowledgement'], |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
foreach ($keys as $key => $mapping) |
89
|
|
|
{ |
90
|
|
|
if (empty(config($key, null))) |
91
|
|
|
{ |
92
|
|
|
$this->addLogEntry('"' . __CLASS__ . '" cannot process empty value for key "' . $key . '".', 'notice'); |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (!$mapping['validator'](config($key))) |
97
|
|
|
{ |
98
|
|
|
$this->addLogEntry('"' . __CLASS__ . '" cannot find mapping "validator" method named "' . $mapping['setter'] . '".', 'warning'); |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (array_key_exists('self', $mapping) && |
103
|
|
|
is_bool($mapping['self']) && |
104
|
|
|
$mapping['self'] === true) |
105
|
|
|
{ |
106
|
|
View Code Duplication |
if (!method_exists($this, $mapping['setter'])) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$this->addLogEntry('"' . __CLASS__ . '" cannot find mapping "setter" method on object "' . get_class($this) . '" named "' . $mapping['setter'] . '".', 'error'); |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->{$mapping['setter']}(config($key)); |
113
|
|
|
} |
114
|
|
|
else |
115
|
|
|
{ |
116
|
|
View Code Duplication |
if (!method_exists($this->writer, $mapping['setter'])) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$this->addLogEntry('"' . __CLASS__ . '" cannot find mapping "setter" method on object "' . get_class($this->writer) . '" named "' . $mapping['setter'] . '".', 'error'); |
119
|
|
|
continue; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->writer->{$mapping['setter']}(config($key)); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Add log entry. |
131
|
|
|
* |
132
|
|
|
* @param string $text |
133
|
|
|
* @param string $level |
134
|
|
|
* @return \AustinHeap\Security\Txt\SecurityTxtHelper |
135
|
|
|
*/ |
136
|
|
|
public function addLogEntry(string $text, string $level = 'info'): SecurityTxtHelper |
137
|
|
|
{ |
138
|
|
|
\Log::$level($text); |
139
|
|
|
|
140
|
|
|
$this->logEntries[] = ['text' => $text, 'level' => $level]; |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Fetches the raw text of the document. |
147
|
|
|
* |
148
|
|
|
* @return \AustinHeap\Security\Txt\SecurityTxtHelper |
149
|
|
|
*/ |
150
|
|
|
public function fetch(): string |
151
|
|
|
{ |
152
|
|
|
if ($this->cache) { |
153
|
|
|
$text = cache($this->cacheKey, null); |
154
|
|
|
|
155
|
|
|
if (!is_null($text)) |
156
|
|
|
return $text; |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$text = $this->writer |
160
|
|
|
->generate() |
161
|
|
|
->getText(); |
162
|
|
|
|
163
|
|
|
if ($this->writer->getComments()) |
164
|
|
|
$text .= '# Cache is ' . ($this->cache ? 'enabled with key "' . $this->cacheKey . '"' : 'disabled') . '.' . PHP_EOL . |
165
|
|
|
'#' . PHP_EOL; |
166
|
|
|
|
167
|
|
|
if ($this->cache) |
168
|
|
|
cache([$this->cacheKey => $text], now()->addMinutes($this->cacheTime)); |
169
|
|
|
|
170
|
|
|
return empty($text) ? '' : $text; |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Enable the enabled flag. |
175
|
|
|
* |
176
|
|
|
* @return \AustinHeap\Security\Txt\SecurityTxtHelper |
177
|
|
|
*/ |
178
|
|
|
public function enable(): SecurityTxtHelper |
179
|
|
|
{ |
180
|
|
|
return $this->setEnabled(true); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Disable the enabled flag. |
185
|
|
|
* |
186
|
|
|
* @return \AustinHeap\Security\Txt\SecurityTxtHelper |
187
|
|
|
*/ |
188
|
|
|
public function disable(): SecurityTxtHelper |
189
|
|
|
{ |
190
|
|
|
return $this->setEnabled(false); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Set the enabled flag. |
195
|
|
|
* |
196
|
|
|
* @param bool $enabled |
197
|
|
|
* @return \AustinHeap\Security\Txt\SecurityTxtHelper |
198
|
|
|
*/ |
199
|
|
|
public function setEnabled(bool $enabled): SecurityTxtHelper |
200
|
|
|
{ |
201
|
|
|
$this->enabled = $enabled; |
|
|
|
|
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get the enabled flag. |
208
|
|
|
* |
209
|
|
|
* @return bool |
210
|
|
|
*/ |
211
|
|
|
public function getEnabled(): bool |
212
|
|
|
{ |
213
|
|
|
return $this->enabled; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Enable the cache flag. |
218
|
|
|
* |
219
|
|
|
* @return \AustinHeap\Security\Txt\SecurityTxtHelper |
220
|
|
|
*/ |
221
|
|
|
public function enableCache(): SecurityTxtHelper |
222
|
|
|
{ |
223
|
|
|
return $this->setCache(true); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Disable the cache flag. |
228
|
|
|
* |
229
|
|
|
* @return \AustinHeap\Security\Txt\SecurityTxtHelper |
230
|
|
|
*/ |
231
|
|
|
public function disableCache(): SecurityTxtHelper |
232
|
|
|
{ |
233
|
|
|
return $this->setCache(false); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Set the cache flag. |
238
|
|
|
* |
239
|
|
|
* @param bool $cache |
240
|
|
|
* @return \AustinHeap\Security\Txt\SecurityTxtHelper |
241
|
|
|
*/ |
242
|
|
|
public function setCache(bool $cache): SecurityTxtHelper |
243
|
|
|
{ |
244
|
|
|
$this->cache = $cache; |
245
|
|
|
|
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Get the cache flag. |
251
|
|
|
* |
252
|
|
|
* @return bool |
253
|
|
|
*/ |
254
|
|
|
public function getCache(): bool |
255
|
|
|
{ |
256
|
|
|
return $this->cache; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Set the cache key. |
261
|
|
|
* |
262
|
|
|
* @param string $cacheKey |
263
|
|
|
* @return \AustinHeap\Security\Txt\SecurityTxtHelper |
264
|
|
|
*/ |
265
|
|
|
public function setCacheKey(string $cacheKey): SecurityTxtHelper |
266
|
|
|
{ |
267
|
|
|
$this->cacheKey = $cacheKey; |
268
|
|
|
|
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Get the cache key. |
274
|
|
|
* |
275
|
|
|
* @return string |
276
|
|
|
*/ |
277
|
|
|
public function getCacheKey(): string |
278
|
|
|
{ |
279
|
|
|
return $this->cacheKey; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Set the cache time. |
284
|
|
|
* |
285
|
|
|
* @param int $cacheTime |
286
|
|
|
* @return \AustinHeap\Security\Txt\SecurityTxtHelper |
287
|
|
|
*/ |
288
|
|
|
public function setCacheTime(int $cacheTime): SecurityTxtHelper |
289
|
|
|
{ |
290
|
|
|
$this->cacheTime = $cacheTime; |
291
|
|
|
|
292
|
|
|
return $this; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Get the cache time. |
297
|
|
|
* |
298
|
|
|
* @return int |
299
|
|
|
*/ |
300
|
|
|
public function getCacheTime(): int |
301
|
|
|
{ |
302
|
|
|
return $this->cacheTime; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
} |
306
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.