/
class.mailchecker.plugin.php
137 lines (124 loc) · 4.64 KB
/
class.mailchecker.plugin.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
<?php
/**
* Disallows users to register with a disposable mail address.
*
* Checks mail address during registration process and will return a validation
* error if mail provider is on the given list of disposable mail providers.
*
* @package mailchecker
* @author Robin Jurinka
* @license MIT
*/
class MailcheckerPlugin extends Gdn_Plugin {
/**
* Allow updating the list of spam providers manually.
*
* @param settingsController $sender Instance of the calling class.
* @package mailchecker
* @since 0.2
* @return void.
*/
public function settingsController_mailchecker_create($sender) {
$sender->permission('Garden.Settings.Manage');
$sender->setHighlightRoute('dashboard/settings/plugins');
$sender->setData('Title', Gdn::translate('Mailchecker Settings'));
$sender->setData(
'Description',
Gdn::translate('You can update the list from time to time but it is no needed to get started.')
);
// Fetch new list and give feedback abut the number of providers.
$sender->Form = new Gdn_Form();
if ($sender->Form->authenticatedPostBack()) {
$count = $this->updateList();
if ($count) {
Gdn::config()->saveToConfig(
'mailchecker.LastUpdate',
date(time())
);
$sender->informMessage(
sprintf(
Gdn::translate('There are currently %1s spam providers in the list'),
$count
)
);
}
}
$sender->render('settings', '', 'plugins/mailchecker');
}
/**
* Disallow users to register with disposable mails.
*
* Uses routine provided by Francois-Guillaume Ribreau to check if the email
* provider is in a list of disposable mail providers. Adds a validation
* result if there is a match.
*
* @param object $sender UserModel.
* @param mixed $args EventArguments of BeforeRegister.
* @package mailchecker
* @since 0.1
* @return void.
*/
public function userModel_beforeRegister_handler($sender, $args) {
// Get mail provider from form.
if (isset($args['RegisteringUser'])) {
$email = $args['RegisteringUser']['Email'];
} else {
$email = $args['User']['Email'];
}
// Return if no vaild mail.
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return;
}
// List of disposable mail hosts (provided by Francois-Guillaume Ribreau).
// Try to get most recent list which is held in cache folder.
if (file_exists(PATH_CACHE.'/mailchecker/providers.php')) {
include(PATH_CACHE.'/mailchecker/providers.php');
} else {
// "Fall back" to contained list.
require(__DIR__.'/providers.php');
}
// Get lowercase domain from email.
$domainStart = strrpos($email, '@') + 1;
$domain = strtolower(substr($email, $domainStart));
// Return if domain is not blacklisted.
if (!in_array($domain, $providers)) {
return;
}
// Set error message.
$sender->Validation->addValidationResult(
'Email',
'Disposable mail addresses are not allowed.'
);
$args['Valid'] = false;
}
/**
* Updates provider list which is held in cache.
*
* Plugin comes with a fallback if providers can not be loaded from cache.
* TODO: error checking!
*
* @package mailchecker
* @since 0.2
* @return integer Number of providers in list.
*/
private function updateList() {
// Create path for spam providers list file.
mkdir(PATH_CACHE.'/mailchecker');
// Get main list from GitHub.
$url = Gdn::config(
'Mailchecker.UpdateFileUrl',
'https://raw.githubusercontent.com/FGRibreau/mailchecker/master/list.txt'
);
$bareboneList = proxyRequest($url);
// Save as backup.
file_put_contents(PATH_CACHE.'/mailchecker/list.txt', $bareboneList);
$providersList = str_replace("\n", "','", $bareboneList);
file_put_contents(
PATH_CACHE.'/mailchecker/providers.php',
'<?php $providers = [\''.$providersList."'];"
);
// Return count of spam providers in the list.
include(PATH_CACHE.'/mailchecker/providers.php');
return(count($providers));
}
}