diff --git a/backup/util/dbops/restore_dbops.class.php b/backup/util/dbops/restore_dbops.class.php index f56de9be243..9042c4f4fde 100644 --- a/backup/util/dbops/restore_dbops.class.php +++ b/backup/util/dbops/restore_dbops.class.php @@ -1811,10 +1811,9 @@ abstract class restore_dbops { public static function calculate_course_names($courseid, $fullname, $shortname) { global $CFG, $DB; - $currentfullname = ''; - $currentshortname = ''; $counter = 0; - // Iteratere while the name exists + + // Iterate while fullname or shortname exist. do { if ($counter) { $suffixfull = ' ' . get_string('copyasnoun') . ' ' . $counter; @@ -1823,8 +1822,11 @@ abstract class restore_dbops { $suffixfull = ''; $suffixshort = ''; } - $currentfullname = $fullname.$suffixfull; - $currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort; // < 100cc + + // Ensure we don't overflow maximum length of name fields, in multi-byte safe manner. + $currentfullname = core_text::substr($fullname, 0, 254 - strlen($suffixfull)) . $suffixfull; + $currentshortname = core_text::substr($shortname, 0, 100 - strlen($suffixshort)) . $suffixshort; + $coursefull = $DB->get_record_select('course', 'fullname = ? AND id != ?', array($currentfullname, $courseid), '*', IGNORE_MULTIPLE); $courseshort = $DB->get_record_select('course', 'shortname = ? AND id != ?', array($currentshortname, $courseid));