-
Notifications
You must be signed in to change notification settings - Fork 2
/
dkim.php
239 lines (203 loc) · 9.03 KB
/
dkim.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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* DKIM manager admin page
*
* @package tool_emailutils
* @copyright 2023 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Brendan Heywood <[email protected]>
*/
use tool_emailutils\dkim_manager;
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
$baseurl = new moodle_url('/admin/tool/emailutils/dkim.php');
$PAGE->set_url($baseurl);
admin_externalpage_setup('tool_emailutils_dkim');
$action = optional_param('action', '', PARAM_ALPHA);
if ($action == 'delete') {
require_sesskey();
$domain = required_param('domain', PARAM_TEXT);
$selector = required_param('selector', PARAM_TEXT);
$manager = new dkim_manager($domain, $selector);
$manager->delete_selector();
redirect($baseurl, get_string('selectordeleted', 'tool_emailutils'), null, \core\output\notification::NOTIFY_WARNING);
}
if ($action == 'activate') {
require_sesskey();
$selector = required_param('selector', PARAM_TEXT);
add_to_config_log('emaildkimselector', $CFG->emaildkimselector, $selector, '');
set_config('emaildkimselector', $selector);
redirect($baseurl, get_string('selectoractivated', 'tool_emailutils'), null, \core\output\notification::NOTIFY_SUCCESS);
}
if ($action == 'deactivate') {
require_sesskey();
$selector = required_param('selector', PARAM_TEXT);
add_to_config_log('emaildkimselector', $CFG->emaildkimselector, '', '');
set_config('emaildkimselector', '');
redirect($baseurl, get_string('selectordeactivated', 'tool_emailutils'), null, \core\output\notification::NOTIFY_WARNING);
}
$form = new \tool_emailutils\form\create_dkim();
if ($form->is_cancelled()) {
redirect($prevurl);
} else if ($fromform = $form->get_data()) {
$domain = $fromform->domain;
$selector = $fromform->selector;
$manager = new dkim_manager($domain, $selector, true);
redirect($baseurl, get_string('selectorcreated', 'tool_emailutils'), null, \core\output\notification::NOTIFY_SUCCESS);
}
$dkimdir = $CFG->dataroot . '/dkim/';
$domains = [];
if (is_dir($dkimdir)) {
$domains = scandir($dkimdir, SCANDIR_SORT_DESCENDING);
}
$domaincount = 0;
$noreplydomain = substr($CFG->noreplyaddress, strpos($CFG->noreplyaddress, '@') + 1);
// Always make sure the noreply domain is included even if nothing has been setup yet.
$domains = array_unique(array_merge($domains, [$noreplydomain]));
print $OUTPUT->header();
print $OUTPUT->heading(get_string('dkimmanager', 'tool_emailutils'));
print "<table class='table table-sm w-auto table-bordered'>";
print '<tr><th colspan=2>Domains / selectors</th><th>Actions</th></tr>';
foreach ($domains as $domain) {
if (substr($domain, 0, 1) == '.') {
continue;
}
if (!is_dir($dkimdir . $domain) && $domain != $noreplydomain) {
continue;
}
$domaincount ++;
print '<tr><td colspan=2>';
print '<h3>';
print html_writer::tag('span', "@$domain ");
if ($domain == $noreplydomain) {
print ' ' . html_writer::tag('span', get_string('domaindefaultnoreply', 'tool_emailutils'),
['class' => 'badge badge-secondary']);
}
print '</h3>';
print '</td>';
print '<td>';
$url = new moodle_url('https://mxtoolbox.com/SuperTool.aspx', ['action' => "spf:$domain", 'run' => 'toolpage']);
print get_string('mxtoolbox', 'tool_emailutils');
print '<ul>';
print "<li><a href='$url' target='_blank'>SPF</a>";
$url = new moodle_url('https://mxtoolbox.com/SuperTool.aspx', ['action' => "txt:$domain"]);
print "<li><a href='$url' target='_blank'>Raw TXT</a>";
$url = new moodle_url('https://mxtoolbox.com/SuperTool.aspx', ['action' => "dmarc:$domain", 'run' => 'toolpage']);
print "<li><a href='$url' target='_blank'>DMARC</a>";
$url = new moodle_url('https://mxtoolbox.com/SuperTool.aspx', ['action' => "txt:_dmarc.$domain"]);
print "<li><a href='$url' target='_blank'>DMARC TXT</a>";
print '</th></tr>';
$selectors = [];
$selectordir = $dkimdir . $domain;
if (is_dir($selectordir)) {
$selectors = scandir($selectordir);
}
// We want newer date based selectors to be at the top.
natsort($selectors);
$selectors = array_reverse($selectors);
$selectorcount = 0;
foreach ($selectors as $file) {
if (substr($file, -8, 8) !== '.private') {
continue;
}
$selector = substr($file, 0, -8);
$manager = new dkim_manager($domain, $selector);
$context = [
'domain' => $domain,
'selector' => $selector,
'dkimurl' => new moodle_url('https://mxtoolbox.com/SuperTool.aspx',
['action' => "dkim:$domain:$selector", 'run' => 'toolpage']),
'dkimrawurl' => new moodle_url('https://mxtoolbox.com/SuperTool.aspx',
['action' => "txt:$selector._domainkey.$domain"]),
'dnskey' => $manager->get_dns_key(),
'dnsvalue' => $manager->get_dns_value(),
'dnsvaluechunked' => $manager->get_dns_value_chunked(),
'dnsvalueescaped' => $manager->get_dns_value_escaped(),
'id' => uniqid(),
];
if ($CFG->emaildkimselector == $selector) {
$context['selectoractive'] = true;
}
$isactive = $CFG->emaildkimselector == $selector;
// Only give the option to delete if it is not being used.
$confirmation = new \confirm_action(
get_string('selectordeleteconfirm', 'tool_emailutils'),
null,
get_string('selectordelete', 'tool_emailutils')
);
$context['selectordelete'] = $OUTPUT->action_link(
new moodle_url(
'/admin/tool/emailutils/dkim.php',
[
'domain' => $domain,
'selector' => $selector,
'action' => 'delete',
'sesskey' => sesskey(),
]
),
get_string('selectordelete', 'tool_emailutils'),
$confirmation,
['class' => 'btn btn-sm ' . ($isactive ? 'btn-secondary disabled' : 'btn-outline-danger')],
new pix_icon('i/delete', '')
);
if ($isactive) {
// Only give the option to make it the active select if it is not being used.
$confirmation = new \confirm_action(
get_string('selectordeactivateconfirm', 'tool_emailutils'),
null,
get_string('selectordeactivate', 'tool_emailutils')
);
$context['selectordeactivate'] = $OUTPUT->action_link(
new moodle_url('/admin/tool/emailutils/dkim.php', [
'selector' => $selector,
'action' => 'deactivate',
'sesskey' => sesskey(),
]),
get_string('selectordeactivate', 'tool_emailutils'),
$confirmation,
['class' => 'btn btn-sm btn-secondary'],
new pix_icon('i/show', ''));
} else {
// Only give the option to make it the active select if it is not being used.
$confirmation = new \confirm_action(
get_string('selectoractivateconfirm', 'tool_emailutils'),
null,
get_string('selectoractivate', 'tool_emailutils')
);
$context['selectoractivate'] = $OUTPUT->action_link(
new moodle_url('/admin/tool/emailutils/dkim.php', [
'selector' => $selector,
'action' => 'activate',
'sesskey' => sesskey(),
]),
get_string('selectoractivate', 'tool_emailutils'),
$confirmation,
['class' => 'btn btn-sm btn-primary'],
new pix_icon('t/hide', ''));
}
print $OUTPUT->render_from_template('tool_emailutils/dkimselector', $context);
}
}
print "</table>";
if ($domaincount == 0) {
echo $OUTPUT->notification(get_string('selectormissing', 'tool_emailutils'), \core\notification::ERROR);
}
print html_writer::tag('div',
get_string('dkimmanagerhelp', 'tool_emailutils', ['emailtest' => (new moodle_url('/admin/testoutgoingmailconf.php'))->out()]),
['class' => 'crap', 'style' => 'max-width: 40em']);
$form->display();
echo $OUTPUT->footer();