MDL-75789 restore: multi-byte safe substring calculating course names.

Co-authored-by: Leon Stringer <leon.stringer@ntlworld.com>
This commit is contained in:
Paul Holden 2022-11-02 22:33:10 +00:00
parent 0ea3d45e04
commit 530e06da6d

View File

@ -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));