Răsfoiți Sursa

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.
El RIDO 3 ani în urmă
părinte
comite
a799351db3
1 a modificat fișierele cu 33 adăugiri și 24 ștergeri
  1. 33 24
      lib/Data/Filesystem.php

+ 33 - 24
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);
-
-        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;
+        $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'
+                );
 
-                $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;
     }