From dfdfb3bd997fc48a4f579e22b94138ca6124461a Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Mon, 28 Aug 2017 10:45:54 +0200 Subject: [PATCH] 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. --- h5p.classes.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/h5p.classes.php b/h5p.classes.php index 7c0e546..aae261d 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -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); }