Skip to content

Commit 2191b0e

Browse files
committed
Improve default dkim selector #34
1 parent 31db203 commit 2191b0e

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

classes/form/create_dkim.php

+36-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525

2626
namespace tool_emailutils\form;
27+
use tool_emailutils\dns_util;
2728

2829
defined('MOODLE_INTERNAL') || die;
2930

@@ -53,7 +54,7 @@ public function definition() {
5354

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

56-
$selector = \userdate(time(), get_string('selectordefault', 'tool_emailutils'));
57+
$selector = $this->get_default_selector();
5758
$mform->setDefault("selector", $selector);
5859
$mform->setType('selector', PARAM_HOST);
5960

@@ -74,4 +75,38 @@ public function validation($data, $files) {
7475
$errors = parent::validation($data, $files);
7576
return $errors;
7677
}
78+
79+
/**
80+
* Gets a selector value to use as a default
81+
*
82+
* @return string default selector
83+
*/
84+
private function get_default_selector() {
85+
GLOBAL $CFG;
86+
// Add date to default.
87+
$selector = \userdate(time(), get_string('selectordefault', 'tool_emailutils'));
88+
89+
// Add subdomain to default.
90+
$dns = new dns_util();
91+
$url = new \moodle_url($CFG->wwwroot);
92+
$domain = $url->get_host();
93+
$subdomains = $dns->get_subdomains($domain);
94+
95+
// Clean the subdomains to remove foreign language chars.
96+
// Email filter is enough because domains don't contain the other allowed chars.
97+
$cleanedsubdomains = trim(filter_var($subdomains, FILTER_SANITIZE_EMAIL), '.');
98+
99+
if (!empty($cleanedsubdomains)) {
100+
$formatteddomain = str_replace('.', '-', $cleanedsubdomains);
101+
} else {
102+
$partdomain = explode('.', $domain)[0];
103+
$formatteddomain = filter_var($partdomain, FILTER_SANITIZE_EMAIL);
104+
}
105+
106+
if ($formatteddomain && $formatteddomain != 'www') {
107+
$selector .= '-' . $formatteddomain;
108+
}
109+
return $selector;
110+
}
111+
77112
}

0 commit comments

Comments
 (0)