From a799351db33861e0b7aeb2172ed8108f8729ef78 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 6 Nov 2022 09:09:50 +0100 Subject: [PATCH] re-use logic from _getExpiredPastes() Scrutinizer pointed out that the dieerr() function isn't available in this class. Code does work when invoked by migrate script, but this way it would also work in other contexts. --- lib/Data/Filesystem.php | 57 ++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/lib/Data/Filesystem.php b/lib/Data/Filesystem.php index 2d082144..abaa51db 100644 --- a/lib/Data/Filesystem.php +++ b/lib/Data/Filesystem.php @@ -410,37 +410,46 @@ class Filesystem extends AbstractData public function getAllPastes() { $pastes = array(); - $subdirs = scandir($this->_path); - if ($subdirs === false) { - dieerr('Unable to list directory ' . $this->_path); - } - $subdirs = preg_grep('/^[^.].$/', $subdirs); + $firstLevel = array_filter( + scandir($this->_path), + 'PrivateBin\Data\Filesystem::_isFirstLevelDir' + ); + if (count($firstLevel) > 0) { + foreach ($firstLevel as $firstKey) { + $secondLevel = array_filter( + scandir($this->_path . DIRECTORY_SEPARATOR . $firstKey), + 'PrivateBin\Data\Filesystem::_isSecondLevelDir' + ); - foreach ($subdirs as $subdir) { - $subpath = $this->_path . DIRECTORY_SEPARATOR . $subdir; - - $subsubdirs = scandir($subpath); - if ($subsubdirs === false) { - dieerr('Unable to list directory ' . $subpath); - } - $subsubdirs = preg_grep('/^[^.].$/', $subsubdirs); - foreach ($subsubdirs as $subsubdir) { - $subsubpath = $subpath . DIRECTORY_SEPARATOR . $subsubdir; - - $files = scandir($subsubpath); - if ($files === false) { - dieerr('Unable to list directory ' . $subsubpath); + // skip this folder + if (count($secondLevel) == 0) { + continue; } - $files = preg_grep('/\.php$/', $files); - foreach ($files as $file) { - if (substr($file, 0, 4) === $subdir . $subsubdir) { - $pastes[] = substr($file, 0, strlen($file) - 4); + foreach ($secondLevel as $secondKey) { + $path = $this->_path . DIRECTORY_SEPARATOR . $firstKey . + DIRECTORY_SEPARATOR . $secondKey; + if (!is_dir($path)) { + continue; } + $thirdLevel = array_filter( + array_map( + function ($filename) { + return strlen($filename) >= 20 ? + substr($filename, 0, -4) : + $filename; + }, + scandir($path) + ), + 'PrivateBin\\Model\\Paste::isValidId' + ); + if (count($thirdLevel) == 0) { + continue; + } + $pastes += $thirdLevel; } } } - return $pastes; }