forked from anonaddy/anonaddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Domain.php
229 lines (195 loc) · 5.7 KB
/
Domain.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
<?php
namespace App;
use App\Http\Resources\DomainResource;
use App\Traits\HasEncryptedAttributes;
use App\Traits\HasUuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
class Domain extends Model
{
use HasUuid, HasEncryptedAttributes;
public $incrementing = false;
protected $keyType = 'string';
protected $encrypted = [
'description'
];
protected $fillable = [
'domain',
'description',
'active'
];
protected $dates = [
'created_at',
'updated_at',
'domain_verified_at',
'domain_sending_verified_at'
];
protected $casts = [
'id' => 'string',
'user_id' => 'string',
'active' => 'boolean',
'default_recipient_id' => 'string',
];
public static function boot()
{
parent::boot();
Domain::deleting(function ($domain) {
$domain->aliases()->delete();
});
}
/**
* Set the domain's name.
*/
public function setDomainAttribute($value)
{
$this->attributes['domain'] = strtolower($value);
}
/**
* Get the user for the custom domain.
*/
public function user()
{
return $this->belongsTo(User::class);
}
/**
* Get all of the domains's aliases.
*/
public function aliases()
{
return $this->morphMany(Alias::class, 'aliasable');
}
/**
* Get the domains's default recipient.
*/
public function defaultRecipient()
{
return $this->hasOne(Recipient::class, 'id', 'default_recipient_id');
}
/**
* Set the domains's default recipient.
*/
public function setDefaultRecipientAttribute($recipient)
{
$this->attributes['default_recipient_id'] = $recipient->id;
$this->setRelation('defaultRecipient', $recipient);
}
/**
* Deactivate the domain.
*/
public function deactivate()
{
$this->update(['active' => false]);
}
/**
* Activate the domain.
*/
public function activate()
{
$this->update(['active' => true]);
}
/**
* Determine if the domain is verified.
*
* @return bool
*/
public function isVerified()
{
return ! is_null($this->domain_verified_at);
}
/**
* Determine if the domain is verified for sending.
*
* @return bool
*/
public function isVerifiedForSending()
{
return ! is_null($this->domain_sending_verified_at);
}
/**
* Mark this domain as verified.
*
* @return bool
*/
public function markDomainAsVerified()
{
return $this->forceFill([
'domain_verified_at' => $this->freshTimestamp(),
])->save();
}
/**
* Mark this domain as verified for sending.
*
* @return bool
*/
public function markDomainAsVerifiedForSending()
{
return $this->forceFill([
'domain_sending_verified_at' => $this->freshTimestamp(),
])->save();
}
/**
* Checks if the domain has the correct records.
*/
public function checkVerification()
{
if (App::environment('testing')) {
return true;
}
return collect(dns_get_record($this->domain . '.', DNS_TXT))
->contains(function ($r) {
return trim($r['txt']) === 'aa-verify=' . sha1(config('anonaddy.secret') . user()->id);
});
}
/**
* Checks if the domain has the correct records for sending.
*/
public function checkVerificationForSending()
{
$spf = collect(dns_get_record($this->domain . '.', DNS_TXT))
->contains(function ($r) {
return preg_match("/^(v=spf1).*(include:spf\." . config('anonaddy.domain') . ").*(-|~)all$/", $r['txt']);
});
if (!$spf) {
return response()->json([
'success' => false,
'message' => 'SPF record not found. This could be due to DNS caching, please try again later.'
]);
}
$dmarc = collect(dns_get_record('_dmarc.' . $this->domain . '.', DNS_TXT))
->contains(function ($r) {
return preg_match("/^(v=DMARC1).*(p=quarantine|reject).*/", $r['txt']);
});
if (!$dmarc) {
return response()->json([
'success' => false,
'message' => 'DMARC record not found. This could be due to DNS caching, please try again later.'
]);
}
$dk1 = collect(dns_get_record('dk1._domainkey.' . $this->domain . '.', DNS_CNAME))
->contains(function ($r) {
return $r['target'] === 'dk1._domainkey.' . config('anonaddy.domain');
});
if (!$dk1) {
return response()->json([
'success' => false,
'message' => 'CNAME dk1._domainkey record not found. This could be due to DNS caching, please try again later.'
]);
}
$dk2 = collect(dns_get_record('dk2._domainkey.' . $this->domain . '.', DNS_CNAME))
->contains(function ($r) {
return $r['target'] === 'dk2._domainkey.' . config('anonaddy.domain');
});
if (!$dk2) {
return response()->json([
'success' => false,
'message' => 'CNAME dk2._domainkey record not found. This could be due to DNS caching, please try again later.'
]);
}
$this->markDomainAsVerifiedForSending();
return response()->json([
'success' => true,
'message' => 'Records successfully verified for sending.',
'data' => new DomainResource($this->fresh())
]);
}
}