1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* src/Writer.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; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Writer |
18
|
|
|
* |
19
|
|
|
* @link https://github.com/austinheap/php-security-txt |
20
|
|
|
* @link https://packagist.org/packages/austinheap/php-security-txt |
21
|
|
|
* @link https://austinheap.github.io/php-security-txt/classes/AustinHeap.Security.Txt.Writer.html |
22
|
|
|
* @link https://securitytext.org/ |
23
|
|
|
*/ |
24
|
|
|
class Writer extends SecurityTxt implements SecurityTxtInterface |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Internal lines cache. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $lines = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create a new Writer instance. |
36
|
|
|
* |
37
|
|
|
* @return Writer|SecurityTxt |
38
|
|
|
*/ |
39
|
36 |
|
public function __construct() |
40
|
|
|
{ |
41
|
36 |
|
return parent::__construct($this); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Add a comment to the output buffer. |
46
|
|
|
* |
47
|
|
|
* @param string $comment |
48
|
|
|
* |
49
|
|
|
* @return Writer |
50
|
|
|
*/ |
51
|
14 |
|
public function comment(string $comment = ''): Writer |
52
|
|
|
{ |
53
|
14 |
|
$comment = trim($comment); |
54
|
|
|
|
55
|
14 |
|
if (!empty($comment)) { |
56
|
14 |
|
$comment = ' ' . $comment; |
57
|
|
|
} |
58
|
|
|
|
59
|
14 |
|
return $this->line(trim('#' . $comment)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add a spacer to the output buffer. |
64
|
|
|
* |
65
|
|
|
* @return Writer |
66
|
|
|
*/ |
67
|
8 |
|
public function spacer(): Writer |
68
|
|
|
{ |
69
|
8 |
|
return $this->line(''); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Add multiple spacers to the output buffer. |
74
|
|
|
* |
75
|
|
|
* @param int $count |
76
|
|
|
* |
77
|
|
|
* @return Writer |
78
|
|
|
*/ |
79
|
1 |
|
public function spacers(int $count = 1): Writer |
80
|
|
|
{ |
81
|
1 |
|
for ($x = 0; $x < $count; $x++) { |
82
|
1 |
|
$this->spacer(); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Add a line. |
90
|
|
|
* |
91
|
|
|
* @param string $line |
92
|
|
|
* |
93
|
|
|
* @return Writer |
94
|
|
|
*/ |
95
|
14 |
|
public function line(string $line): Writer |
96
|
|
|
{ |
97
|
14 |
|
$this->lines[] = $line; |
98
|
|
|
|
99
|
14 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Add multiple lines. |
104
|
|
|
* |
105
|
|
|
* @param array $lines |
106
|
|
|
* |
107
|
|
|
* @return Writer |
108
|
|
|
*/ |
109
|
1 |
|
public function lines(array $lines): Writer |
110
|
|
|
{ |
111
|
1 |
|
foreach ($lines as $line) { |
112
|
1 |
|
$this->line($line); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Reset the output buffer. |
120
|
|
|
* |
121
|
|
|
* @param bool $test_case |
122
|
|
|
* |
123
|
|
|
* @return Writer |
124
|
|
|
*/ |
125
|
1 |
|
public function reset(bool $test_case = false): Writer |
126
|
|
|
{ |
127
|
1 |
|
$this->lines = []; |
128
|
|
|
|
129
|
1 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Execute the SecurityTxtInterface. |
134
|
|
|
* |
135
|
|
|
* @param bool $test_case |
136
|
|
|
* |
137
|
|
|
* @return Writer |
138
|
|
|
* @throws Exception |
139
|
|
|
*/ |
140
|
14 |
|
public function execute(bool $test_case = false): Writer |
141
|
|
|
{ |
142
|
14 |
|
$time = microtime(true); |
143
|
|
|
|
144
|
14 |
|
if ($this->comments) { |
145
|
14 |
|
$this->comment('Our security address'); |
146
|
|
|
} |
147
|
|
|
|
148
|
14 |
|
if (empty($this->contacts)) { |
149
|
1 |
|
throw new Exception('One (or more) contacts must be defined.'); |
150
|
|
|
} |
151
|
|
|
|
152
|
13 |
|
foreach (array_keys($this->contacts) as $contact) { |
153
|
13 |
|
$this->line('Contact: ' . trim($contact)); |
154
|
|
|
} |
155
|
|
|
|
156
|
13 |
|
if (!empty($this->encryption)) { |
157
|
1 |
|
if ($this->comments) { |
158
|
1 |
|
$this->spacer() |
159
|
1 |
|
->comment('Our PGP key'); |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
$this->line('Encryption: ' . trim($this->encryption)); |
163
|
|
|
} |
164
|
|
|
|
165
|
13 |
|
if (!empty($this->disclosure)) { |
166
|
1 |
|
if ($this->comments) { |
167
|
1 |
|
$this->spacer() |
168
|
1 |
|
->comment('Our disclosure policy'); |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
$this->line('Disclosure: ' . trim(ucfirst($this->disclosure))); |
172
|
|
|
} |
173
|
|
|
|
174
|
13 |
|
if (!empty($this->acknowledgement)) { |
175
|
1 |
|
if ($this->comments) { |
176
|
1 |
|
$this->spacer() |
177
|
1 |
|
->comment('Our public acknowledgement'); |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
$this->line('Acknowledgement: ' . trim($this->acknowledgement)); |
181
|
|
|
} |
182
|
|
|
|
183
|
13 |
|
if ($this->debug) { |
184
|
3 |
|
$this->spacer() |
185
|
3 |
|
->comment() |
186
|
3 |
|
->comment( |
187
|
3 |
|
'Generated by "' . (defined('LARAVEL_SECURITY_TXT_VERSION') ? 'laravel' : (defined('WORDPRESS_SECURITY_TXT_VERSION') ? 'wordpress' : 'php')) . '-security-txt"' . |
188
|
3 |
|
(defined('LARAVEL_SECURITY_TXT_VERSION') ? ' v' . LARAVEL_SECURITY_TXT_VERSION : (defined('WORDPRESS_SECURITY_TXT_VERSION') ? ' v' . WORDPRESS_SECURITY_TXT_VERSION : (defined('PHP_SECURITY_TXT_VERSION') ? ' v' . PHP_SECURITY_TXT_VERSION : ''))) . |
|
|
|
|
189
|
3 |
|
' (https://github.com/austinheap/' . (defined('LARAVEL_SECURITY_TXT_VERSION') ? 'laravel' : (defined('WORDPRESS_SECURITY_TXT_VERSION') ? 'wordpress' : 'php')) . '-security-txt' . (defined('LARAVEL_SECURITY_TXT_VERSION') ? '/releases/tag/v' . LARAVEL_SECURITY_TXT_VERSION : (defined('WORDPRESS_SECURITY_TXT_VERSION') ? '/releases/tag/v' . WORDPRESS_SECURITY_TXT_VERSION : (defined('PHP_SECURITY_TXT_VERSION') ? '/releases/tag/v' . PHP_SECURITY_TXT_VERSION : ''))) . ')'); |
190
|
3 |
|
if (defined('LARAVEL_SECURITY_TXT_VERSION') || defined('WORDPRESS_SECURITY_TXT_VERSION')) { |
191
|
2 |
|
$this->comment( |
192
|
2 |
|
'using "php-security-txt"' . (defined('PHP_SECURITY_TXT_VERSION') ? ' v' . PHP_SECURITY_TXT_VERSION : '') . |
193
|
2 |
|
' (https://github.com/austinheap/php-security-txt' . (defined('PHP_SECURITY_TXT_VERSION') ? '/releases/tag/v' . PHP_SECURITY_TXT_VERSION : '') . ')'); |
194
|
|
|
} |
195
|
3 |
|
$this->comment('in ' . round((microtime(true) - $time) * 1000, 6) . ' seconds on ' . date('c') . '.') |
196
|
3 |
|
->comment() |
197
|
3 |
|
->spacer(); |
198
|
|
|
} |
199
|
|
|
|
200
|
13 |
|
$output = implode(PHP_EOL, $this->lines); |
201
|
|
|
|
202
|
13 |
|
return $this->setText($output); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|