MDL-62251 Privacy: Fix get_path() and get_full_path() bug in Windows

This commit is contained in:
Shamim Rezaie 2018-05-10 19:03:36 +10:00
parent 4dfafed5d9
commit 44efefcbeb
2 changed files with 14 additions and 4 deletions

View File

@ -272,7 +272,9 @@ class moodle_content_writer implements content_writer {
// Join the directory together with the name.
$filepath = implode(DIRECTORY_SEPARATOR, $path) . DIRECTORY_SEPARATOR . $name;
return preg_replace('@' . DIRECTORY_SEPARATOR . '+@', DIRECTORY_SEPARATOR, $filepath);
// To use backslash, it must be doubled ("\\\\" PHP string).
$separator = str_replace('\\', '\\\\', DIRECTORY_SEPARATOR);
return preg_replace('@(' . $separator . '|/)+@', $separator, $filepath);
}
/**
@ -291,7 +293,9 @@ class moodle_content_writer implements content_writer {
// Join the directory together with the name.
$filepath = implode(DIRECTORY_SEPARATOR, $path);
return preg_replace('@' . DIRECTORY_SEPARATOR . '+@', DIRECTORY_SEPARATOR, $filepath);
// To use backslash, it must be doubled ("\\\\" PHP string).
$separator = str_replace('\\', '\\\\', DIRECTORY_SEPARATOR);
return preg_replace('@(' . $separator . '|/)+@', $separator, $filepath);
}
/**

View File

@ -1162,12 +1162,18 @@ class moodle_content_writer_test extends advanced_testcase {
if (null === $subcontext) {
$rcm = $rc->getMethod('get_context_path');
$rcm->setAccessible(true);
return $rcm->invoke($writer);
$path = $rcm->invoke($writer);
} else {
$rcm = $rc->getMethod('get_path');
$rcm->setAccessible(true);
return $rcm->invoke($writer, $subcontext, $name);
$path = $rcm->invoke($writer, $subcontext, $name);
}
// PHPUnit uses mikey179/vfsStream which is a stream wrapper for a virtual file system that uses '/'
// as the directory separator.
$path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
return $path;
}
/**