From 0052eff67ab498236579b945f1e1a3c368f23e89 Mon Sep 17 00:00:00 2001 From: Lai Wei Date: Tue, 27 Sep 2022 10:54:55 +0100 Subject: [PATCH] Update group mailNickname length check --- .../o365/classes/feature/coursesync/main.php | 3 ++ .../o365/classes/feature/coursesync/utils.php | 31 +++++++++---------- local/o365/lang/en/local_o365.php | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/local/o365/classes/feature/coursesync/main.php b/local/o365/classes/feature/coursesync/main.php index eaee75889..ea294e3ca 100644 --- a/local/o365/classes/feature/coursesync/main.php +++ b/local/o365/classes/feature/coursesync/main.php @@ -951,6 +951,9 @@ public function process_course_reset(stdClass $course, stdClass $o365object, boo $resetgroupnameprefix = 'disconnected-'; } $updatedmailnickname = $resetgroupnameprefix . utils::get_group_mail_alias($course); + if (strlen($updatedmailnickname) > 59) { + $updatedmailnickname = substr($updatedmailnickname, 0, 59); + } $updatedexistinggroup = [ 'id' => $existinggroup['id'], 'mailNickname' => $updatedmailnickname, diff --git a/local/o365/classes/feature/coursesync/utils.php b/local/o365/classes/feature/coursesync/utils.php index 78057eee8..7102e9c5a 100644 --- a/local/o365/classes/feature/coursesync/utils.php +++ b/local/o365/classes/feature/coursesync/utils.php @@ -332,11 +332,10 @@ public static function get_sample_team_group_names() : array { * Return the email alias of group for the given course according to configuration. * * @param stdClass $course - * @param stdClass|null $group * * @return string */ - public static function get_group_mail_alias(stdClass $course, stdClass $group = null) : string { + public static function get_group_mail_alias(stdClass $course) : string { $groupmailaliasprefix = get_config('local_o365', 'group_mail_alias_prefix'); if ($groupmailaliasprefix) { $groupmailaliasprefix = static::clean_up_group_mail_alias($groupmailaliasprefix); @@ -365,35 +364,33 @@ public static function get_group_mail_alias(stdClass $course, stdClass $group = $coursepart = $course->shortname; } - if ($group) { - $grouppart = $group->id . '_' . $group->name; - $grouppart = static::clean_up_group_mail_alias($grouppart); - if (strlen($grouppart) > 16) { - $grouppart = substr($grouppart, 0, 16); - } - $grouppart = '-' . $grouppart; - } else { - $grouppart = ''; - } - $coursepart = static::clean_up_group_mail_alias($coursepart); - $coursepartmaxlength = 64 - strlen($groupmailaliasprefix) - strlen($groupmailaliassuffix) - strlen($grouppart); + $coursepartmaxlength = 59 - strlen($groupmailaliasprefix) - strlen($groupmailaliassuffix); if (strlen($coursepart) > $coursepartmaxlength) { $coursepart = substr($coursepart, 0, $coursepartmaxlength); } - return $groupmailaliasprefix . $coursepart . $grouppart . $groupmailaliassuffix; + return $groupmailaliasprefix . $coursepart . $groupmailaliassuffix; } /** * Remove unsupported characters from the mail alias parts, and return the result. * * @param string $mailalias - * @return string|string[]|null + * @return string */ public static function clean_up_group_mail_alias(string $mailalias) { - return preg_replace('/[^a-z0-9-_]+/iu', '', $mailalias); + $notallowedbasicchars = ['@', '(', ')', "\\", '[', ']', '"', ';', ':', '.', '<', '>', ' ']; + $chars = preg_split( '//u', $mailalias, null, PREG_SPLIT_NO_EMPTY); + foreach($chars as $key => $char){ + $charorder = ord($char); + if ($charorder < 0 || $charorder > 127 || in_array($char, $notallowedbasicchars)) { + unset($chars[$key]); + } + } + + return implode($chars); } /** diff --git a/local/o365/lang/en/local_o365.php b/local/o365/lang/en/local_o365.php index 5a207041e..1c9eaeefd 100644 --- a/local/o365/lang/en/local_o365.php +++ b/local/o365/lang/en/local_o365.php @@ -219,7 +219,7 @@
  • All spaces will be removed from the group mail alias.
  • Only upper and lower case letters, numbers, - and _ are allowed in the group mail alias.
  • Group mail alias, including prefix and suffix cannot exceed 64 characters.
  • -
  • Mail alias of the group needs to be unique, otherwise a random 4 digit number will be appended to ensure uniqueness.
  • +
  • Mail alias of the group needs to be unique, otherwise a random 4-digit number will be appended to ensure uniqueness.
  • '; $string['settings_team_name_prefix'] = 'Teams name prefix'; $string['settings_team_name_prefix_desc'] = '';