Add prevent deleting sub content of linked directories

Sometimes devs may link to library folders elsewhere, then we
should prevent traversing and deleting the linked content.
Default behavior now is to remove the link and not its content.
pull/42/head
Frode Petterson 2017-08-28 10:45:54 +02:00
parent e3d7cf2562
commit dfdfb3bd99
1 changed files with 15 additions and 1 deletions

View File

@ -2246,9 +2246,23 @@ class H5PCore {
if (!is_dir($dir)) {
return false;
}
if (is_link($dir)) {
// Do not traverse and delete linked content, simply unlink.
unlink($dir);
return;
}
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? self::deleteFileTree("$dir/$file") : unlink("$dir/$file");
$filepath = "$dir/$file";
// Note that links may resolve as directories
if (!is_dir($filepath) || is_link($filepath)) {
// Unlink files and links
unlink($filepath);
}
else {
// Traverse subdir and delete files
self::deleteFileTree($filepath);
}
}
return rmdir($dir);
}