-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZoneFile.php
executable file
·384 lines (297 loc) · 10.4 KB
/
ZoneFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
namespace evan_klein\zone_file;
class ZoneFile {
private $domain = 'example.com.';
private $ttl = 60; // The default TTL, used when one is not specified for a record
private $records = [];
private $strlen_maxes = [
'name' => 0,
'ttl' => 0,
'class' => 0,
'type' => 0
];
private $spf = [
'params' => NULL,
'_includes' => []
];
public function __construct(?string $domain=NULL, ?int $ttl=NULL){
// $this->domain
if( !\is_null($domain) ){
// Throw an exception if it's not a string
if( !\is_string($domain) ){
throw new \Exception('"domain" must be a string');
}
// Trim whitespace
$domain = \trim($domain);
// Throw an exception if it's blank
if( \strlen($domain)==0 ){
throw new \Exception('"domain" cannot be blank');
}
// Throw an exception if it doesn't end with a period
if( \substr($domain, -1)!=='.' ){
throw new \Exception('"domain" must end with a period');
}
$this->domain = $domain;
}
// $this->ttl
if( !\is_null($ttl) ){
// Throw an exception if it's not an int
if( !\is_int($ttl) ) throw new \Exception('"ttl" must be an int');
$this->ttl = $ttl;
}
}
private function addRecord(string $name, ?int $ttl, string $class, string $type, string $data){
$ttl = $ttl ?? $this->ttl;
$this->records[]=[
'name' => $name,
'ttl' => $ttl,
'class' => $class,
'type' => $type,
'data' => $data
];
}
// This function searches the zone file for the record type(s) specified and returns true if at least one record with that/those type(s) exists, or false otherwise. $types can be an array with one or more types, or a string with a single type
private function hasRecordType(string|array $types): bool {
// If $types is not an array, convert it to one
if( !\is_array($types) ) $types = [$types];
// Prevents case mismatch issues
$types = \array_map(
function($type){ return \strtoupper($type); },
$types
);
// Search the records in the zone file for the type(s) specified
foreach($this->records as $record){
// If a match is found, return true
if( \in_array($record['type'], $types) ) return true;
}
// Otherwise, return false
return false;
}
private function calculateStrlenMaxes(){
foreach($this->records as $record){
foreach(['name', 'ttl', 'class', 'type'] as $key){
$len = \strlen($record[$key]);
if( $len>$this->strlen_maxes[$key] ){
$this->strlen_maxes[$key] = $len;
}
}
}
}
private function pad(string|int $input, int $max_len): string {
// The number of extra spaces padding
$pad = 8;
return \str_pad($input, ($max_len+$pad), ' ', STR_PAD_RIGHT);
}
// Add an A record
public function addA(string $name, string $ipv4_addr, ?int $ttl=NULL): self {
$this->addRecord($name, $ttl, 'IN', 'A', $ipv4_addr);
return $this;
}
// Add an AAAA record
public function addAAAA(string $name, string $ipv6_addr, ?int $ttl=NULL): self {
$this->addRecord($name, $ttl, 'IN', 'AAAA', $ipv6_addr);
return $this;
}
// Add an A and AAAA record
public function addAAAAA(string $name, string $ipv4_addr, string $ipv6_addr, ?int $ttl=NULL): self {
$this->addA($name, $ipv4_addr, $ttl);
$this->addAAAA($name, $ipv6_addr, $ttl);
return $this;
}
// Add a CNAME record
public function addCNAME(string $name, string $cname, ?int $ttl=NULL): self {
$this->addRecord($name, $ttl, 'IN', 'CNAME', $cname);
return $this;
}
// Add a TXT record
public function addTXT(string $name, string $data, ?int $ttl=NULL): self {
$this->addRecord($name, $ttl, 'IN', 'TXT', "\"$data\"");
return $this;
}
// Add a SRV record
// $target must end in a period
public function addSRV(string $service, string $protocol, int $pri, int $weight, int $port, string $target='.', ?int $ttl=NULL): self {
// Validate $target
if( \substr($target, -1)!=='.' ){
throw new \Exception('"target" must end in a period');
}
$this->addRecord(
"_$service._$protocol.{$this->domain}",
$ttl,
'IN',
'SRV',
"$pri $weight $port $target"
);
return $this;
}
// Add a MX record
public function addMX(string $name, int $pri, string $server, ?int $ttl=NULL): self {
$this->addRecord($name, $ttl, 'IN', 'MX', "$pri $server");
return $this;
}
// Add a NS record
public function addNS(string $ns, ?int $ttl=NULL): self {
$this->addRecord($this->domain, $ttl, 'IN', 'NS', $ns);
return $this;
}
public function addWWW(string|array $a=[], string|array $aaaa=[], string|array $subdomains=['www'], ?int $ttl=NULL): self {
$a = (array) $a;
$aaaa = (array) $aaaa;
$subdomains = (array) $subdomains;
foreach($a as $ipv4_addr){
$this->addA($this->domain, $ipv4_addr, $ttl);
foreach($subdomains as $subdomain) $this->addA($subdomain, $ipv4_addr, $ttl);
}
foreach($aaaa as $ipv6_addr){
$this->addAAAA($this->domain, $ipv6_addr, $ttl);
foreach($subdomains as $subdomain) $this->addAAAA($subdomain, $ipv6_addr, $ttl);
}
return $this;
}
public function addGoogle(?string $site_verification=NULL, bool $legacy_mx_servers=false, array $cnames=['mail', 'calendar', 'drive'], ?int $ttl=NULL): self {
// Google site verification
if( \is_string($site_verification) ){
$this->addTXT($this->domain, "google-site-verification=$site_verification", $ttl);
}
// CNAME records for custom URLs
foreach($cnames as $cname) $this->addCNAME($cname, 'ghs.googlehosted.com.', $ttl);
// MX records
if($legacy_mx_servers){
$this->addMX($this->domain, 1, 'aspmx.l.google.com.', $ttl);
$this->addMX($this->domain, 5, 'alt1.aspmx.l.google.com.', $ttl);
$this->addMX($this->domain, 5, 'alt2.aspmx.l.google.com.', $ttl);
$this->addMX($this->domain, 10, 'alt3.aspmx.l.google.com.', $ttl);
$this->addMX($this->domain, 10, 'alt4.aspmx.l.google.com.', $ttl);
}
else $this->addMX($this->domain, 1, 'smtp.google.com.', $ttl);
// SPF include
$this->spf['_includes'][]='_spf.google.com';
return $this;
}
public function addFastmail(?int $ttl=NULL): self {
// MX records
$this->addMX($this->domain, 10, 'in1-smtp.messagingengine.com.', $ttl);
$this->addMX($this->domain, 20, 'in2-smtp.messagingengine.com.', $ttl);
// DKIM records
foreach([1, 2, 3] as $i){
$this->addCNAME("fm$i._domainkey", "fm$i.{$this->domain}dkim.fmhosted.com.", $ttl);
}
// SPF include
$this->spf['_includes'][]='spf.messagingengine.com';
// A records
$this->addA('mail', '66.111.4.147');
$this->addA('mail', '66.111.4.148');
// SRV records
$this->addSRV('submission', 'tcp', 0, 1, 587, 'smtp.fastmail.com.', $ttl);
//$this->addSRV('imap', 'tcp', 0, 0, 0, '.', $ttl);
$this->addSRV('imaps', 'tcp', 0, 1, 993, 'imap.fastmail.com.', $ttl);
//$this->addSRV('pop3', 'tcp', 0, 0, 0, '.', $ttl);
$this->addSRV('pop3s', 'tcp', 10, 1, 995, 'pop.fastmail.com.', $ttl);
$this->addSRV('jmap', 'tcp', 0, 1, 443, 'jmap.fastmail.com.', $ttl);
//$this->addSRV('carddav', 'tcp', 0, 0, 0, '.', $ttl);
$this->addSRV('carddavs', 'tcp', 0, 1, 443, 'carddav.fastmail.com.', $ttl);
//$this->addSRV('caldav', 'tcp', 0, 0, 0, '.', $ttl);
$this->addSRV('caldavs', 'tcp', 0, 1, 443, 'caldav.fastmail.com.', $ttl);
return $this;
}
public function addMailgun(?string $dkim_hostname=NULL, ?string $dkim_key=NULL, string $subdomain='outgoing-mail', ?int $ttl=NULL): self {
// MX records
$this->addMX($subdomain, 10, 'mxa.mailgun.org.', $ttl);
$this->addMX($subdomain, 10, 'mxb.mailgun.org.', $ttl);
// CNAME record
$this->addCNAME("email.$subdomain", 'mailgun.org.', $ttl);
// DKIM record
if(
!\is_null($dkim_hostname)
&&
!\is_null($dkim_key)
) $this->addTXT("$dkim_hostname.$subdomain", $dkim_key, $ttl);
// SPF record/include
$this->addTXT($subdomain, 'v=spf1 include:mailgun.org ~all', $ttl);
$this->spf['_includes'][]='mailgun.org';
return $this;
}
/*
Add a SPF record
Unlike the other add* functions, addSPF() doesn't immediately add the SPF record to the zone file via $this->records. Instead, it simply stores the params that are sent to it in $this->spf['params']
The internal helper function addSPFIf() is what actually generates the SPF record and adds it to the zone file. It is automatically called by output()
Why break things up into two functions like this? Because the SPF record is dynamic, not static, and the other records in the zone file may affect it. So, it's important that the SPF record itself isn't actually generated until all records have been added to the zone file, which is exactly what addSPFIf() does
*/
public function addSPF(?string $mx=NULL, ?string $a=NULL, array $includes=[], ?string $mode='-all', ?int $ttl=NULL): self {
$this->spf['params'] = [
'mx' => $mx,
'a' => $a,
'includes' => $includes,
'mode' => $mode,
'ttl' => $ttl
];
return $this;
}
private function addSPFIf(){
// If SPF params are not set (because addSPF() was never called), don't add an SPF record to the zone file
if(
!isset($this->spf['params'])
||
!\is_array($this->spf['params'])
) return;
// Get SPF variables
$spf = $this->spf;
$params = $spf['params'];
// Get params
$mx = $params['mx'];
$a = $params['a'];
$includes = $params['includes'];
$mode = $params['mode'];
$ttl = $params['ttl'];
// Default values
$mx = $mx ?? $this->hasRecordType('MX');
$a = $a ?? $this->hasRecordType(['A', 'AAAA']);
$mode = $mode ?? '-all';
// Generate the includes array
$includes = \array_unique(
\array_merge($includes, $spf['_includes'])
);
// Convert params to strings for the SPF TXT record
$mx_str = $mx ? 'mx ':'';
$a_str = $a ? 'a ':'';
$includes_str = \implode(
'',
\array_map(
function($value){ return "include:$value "; },
$includes
)
);
// Add TXT record
$this->addTXT($this->domain, "v=spf1 $mx_str$a_str$includes_str$mode", $ttl);
}
// Generates the zone file
public function output(): string {
$this->addSPFIf();
if(
\in_array(
'--format=json',
$GLOBALS['argv']
)
) return \json_encode($this->records);
else{
$this->calculateStrlenMaxes();
$output = <<<OUTPUT
\$ORIGIN {$this->domain}
\$TTL {$this->ttl}
;{$this->domain}
OUTPUT;
foreach($this->records as $record){
$output.=$this->pad($record['name'], $this->strlen_maxes['name']);
$output.=$this->pad($record['ttl'], $this->strlen_maxes['ttl']);
$output.=$this->pad($record['class'], $this->strlen_maxes['class']);
$output.=$this->pad($record['type'], $this->strlen_maxes['type']);
$output.=$record['data'];
$output.="\n";
}
return $output;
}
}
}
namespace evan_klein\domain;
class Domain extends \evan_klein\zone_file\ZoneFile {}
?>