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