Skip to content

Commit 7ce7cc6

Browse files
committed
Improve default dkim selector #34
1 parent 22020e6 commit 7ce7cc6

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

classes/dns_util.php

+43
Original file line numberDiff line numberDiff line change
@@ -249,5 +249,48 @@ public function get_matching_dns_record($domain, $match) {
249249
}
250250
return '';
251251
}
252+
253+
/**
254+
* Gets the selector suffix
255+
* @param string $domain check specific domain
256+
* @return string suffix
257+
*/
258+
public function get_selector_suffix($domain = '') {
259+
GLOBAL $CFG;
260+
261+
if (empty($domain)) {
262+
$url = new \moodle_url($CFG->wwwroot);
263+
$domain = $url->get_host();
264+
}
265+
266+
// Determine the suffix based on the LMS domain and noreply domain.
267+
$primarydomain = $this->get_primary_domain($domain);
268+
$noreplydomain = $this->get_noreply_domain();
269+
if ($primarydomain == $noreplydomain) {
270+
// Noreply domain is same as primary domain, add all LMS subdomains.
271+
$suffix = $this->get_subdomains($domain);
272+
} else if (str_contains($domain, '.' . $noreplydomain)) {
273+
// Noreply domain includes part of the LMS subdomain, only add different subdomains.
274+
$suffix = str_replace('.' . $noreplydomain, '', $domain);
275+
} else if (str_contains($noreplydomain, '.' . $domain)) {
276+
// Noreply domain is a subdomain of LMS, domain already has all info.
277+
$suffix = '';
278+
} else if (str_contains($noreplydomain, '.' . $primarydomain)) {
279+
// Noreply domain is a different subdomain of primary domain, add all LMS subdomains.
280+
$suffix = $this->get_subdomains($domain);
281+
} else {
282+
// Noreply domain shares nothing in common with LMS, add entire LMS domain.
283+
$suffix = $domain;
284+
}
285+
286+
// Clean the suffix to remove www and foreign language chars, and convert '.' to '-'.
287+
// Email filter is enough because domains don't contain the other allowed chars.
288+
$suffix = ltrim($suffix, 'www.');
289+
$suffix = trim(filter_var($suffix, FILTER_SANITIZE_EMAIL), '.');
290+
$suffix = str_replace('.', '-', $suffix);
291+
292+
return $suffix;
293+
}
294+
252295
}
253296

classes/form/create_dkim.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function definition() {
5353

5454
$group[] =& $mform->createElement('text', 'selector', array("size" => 20));
5555

56-
$selector = \userdate(time(), get_string('selectordefault', 'tool_emailutils'));
56+
$selector = $this->get_default_selector();
5757
$mform->setDefault("selector", $selector);
5858
$mform->setType('selector', PARAM_HOST);
5959

@@ -74,4 +74,22 @@ public function validation($data, $files) {
7474
$errors = parent::validation($data, $files);
7575
return $errors;
7676
}
77+
78+
/**
79+
* Gets a selector value to use as a default
80+
*
81+
* @return string default selector
82+
*/
83+
private function get_default_selector() {
84+
// Add date to default.
85+
$selector = \userdate(time(), get_string('selectordefault', 'tool_emailutils'));
86+
87+
// Add suffix.
88+
$dns = new \tool_emailutils\dns_util();
89+
if ($suffix = $dns->get_selector_suffix()) {
90+
$selector .= '-' . $suffix;
91+
}
92+
return $selector;
93+
}
94+
7795
}

0 commit comments

Comments
 (0)