Skip to content

Commit

Permalink
Improve default dkim selector #34
Browse files Browse the repository at this point in the history
  • Loading branch information
bwalkerl committed Jan 30, 2024
1 parent d738734 commit e332956
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion classes/form/create_dkim.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

namespace tool_emailutils\form;
use tool_emailutils\dns_util;

defined('MOODLE_INTERNAL') || die;

Expand Down Expand Up @@ -53,7 +54,7 @@ public function definition() {

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

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

Expand All @@ -74,4 +75,38 @@ public function validation($data, $files) {
$errors = parent::validation($data, $files);
return $errors;
}

/**
* Gets a selector value to use as a default
*
* @return string default selector
*/
private function get_default_selector() {
GLOBAL $CFG;
// Add date to default.
$selector = \userdate(time(), get_string('selectordefault', 'tool_emailutils'));

// Add subdomain to default.
$dns = new dns_util();
$url = new \moodle_url($CFG->wwwroot);
$domain = $url->get_host();
$subdomains = $dns->get_subdomains($domain);

// Clean the subdomains to remove foreign language chars.
// Email filter is enough because domains don't contain the other allowed chars.
$cleanedsubdomains = trim(filter_var($subdomains, FILTER_SANITIZE_EMAIL), '.');

if (!empty($cleanedsubdomains)) {
$formatteddomain = str_replace('.', '-', $cleanedsubdomains);
} else {
$partdomain = explode('.', $domain)[0];
$formatteddomain = filter_var($partdomain, FILTER_SANITIZE_EMAIL);
}

if ($formatteddomain && $formatteddomain != 'www') {
$selector .= '-' . $formatteddomain;
}
return $selector;
}

}

0 comments on commit e332956

Please sign in to comment.