]> source.dussan.org Git - nextcloud-server.git/commitdiff
Always check variable type before using readdir to avoid surprises
authorArthur Schiwon <blizzz@owncloud.com>
Wed, 4 Sep 2013 11:06:04 +0000 (13:06 +0200)
committerArthur Schiwon <blizzz@owncloud.com>
Wed, 4 Sep 2013 11:06:04 +0000 (13:06 +0200)
14 files changed:
apps/files_external/lib/amazons3.php
apps/files_external/lib/google.php
apps/files_external/lib/irods.php
apps/files_external/lib/smb.php
apps/files_trashbin/index.php
lib/app.php
lib/archive.php
lib/cache/file.php
lib/cache/fileglobal.php
lib/connector/sabre/objecttree.php
lib/files/cache/scanner.php
lib/files/storage/common.php
lib/files/view.php
lib/installer.php

index 2d7bcd4ac376b5d69f15f0899824e7d38fe6fee3..c08a266b48c23d36e897cae68b319af36d4963df 100644 (file)
@@ -183,17 +183,20 @@ class AmazonS3 extends \OC\Files\Storage\Common {
                }
 
                $dh = $this->opendir($path);
-               while (($file = readdir($dh)) !== false) {
-                       if ($file === '.' || $file === '..') {
-                               continue;
-                       }
 
-                       if ($this->is_dir($path . '/' . $file)) {
-                               $this->rmdir($path . '/' . $file);
-                       } else {
-                               $this->unlink($path . '/' . $file);
+               if(is_resource($dh)) {
+                       while (($file = readdir($dh)) !== false) {
+                               if ($file === '.' || $file === '..') {
+                                       continue;
+                               }
+
+                               if ($this->is_dir($path . '/' . $file)) {
+                                       $this->rmdir($path . '/' . $file);
+                               } else {
+                                       $this->unlink($path . '/' . $file);
+                               }
                        }
-                       }
+               }
 
                try {
                        $result = $this->connection->deleteObject(array(
@@ -464,15 +467,17 @@ class AmazonS3 extends \OC\Files\Storage\Common {
                        }
 
                        $dh = $this->opendir($path1);
-                       while (($file = readdir($dh)) !== false) {
-                               if ($file === '.' || $file === '..') {
-                                       continue;
+                       if(is_resource($dh)) {
+                               while (($file = readdir($dh)) !== false) {
+                                       if ($file === '.' || $file === '..') {
+                                               continue;
+                                       }
+
+                                       $source = $path1 . '/' . $file;
+                                       $target = $path2 . '/' . $file;
+                                       $this->copy($source, $target);
                                }
-
-                               $source = $path1 . '/' . $file;
-                               $target = $path2 . '/' . $file;
-                               $this->copy($source, $target);
-                       }
+                       }
                }
 
                return true;
index 215bdcda6c2356a645f631eeef2ab10fed746e16..b63b5885de1ac80287c334e3e821b8c8523e7390 100644 (file)
@@ -206,14 +206,16 @@ class Google extends \OC\Files\Storage\Common {
        public function rmdir($path) {
                if (trim($path, '/') === '') {
                        $dir = $this->opendir($path);
-                       while (($file = readdir($dh)) !== false) {
-                               if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
-                                       if (!$this->unlink($path.'/'.$file)) {
-                                               return false;
+                       if(is_resource($dir)) {
+                               while (($file = readdir($dir)) !== false) {
+                                       if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
+                                               if (!$this->unlink($path.'/'.$file)) {
+                                                       return false;
+                                               }
                                        }
                                }
+                               closedir($dir);
                        }
-                       closedir($dir);
                        $this->driveFiles = array();
                        return true;
                } else {
index 7ec3b3a0cfc09f32cfcb02aa289a5f73b7386386..f7279a6c5d221defc7d90c427d5b489e8e54d9c5 100644 (file)
@@ -55,7 +55,7 @@ class iRODS extends \OC\Files\Storage\StreamWrapper{
                } else {
                        throw new \Exception();
                }
-               
+
        }
 
        public static function login( $params ) {
@@ -137,11 +137,13 @@ class iRODS extends \OC\Files\Storage\StreamWrapper{
        private function collectionMTime($path) {
                $dh = $this->opendir($path);
                $lastCTime = $this->filemtime($path);
-               while (($file = readdir($dh)) !== false) {
-                       if ($file != '.' and $file != '..') {
-                               $time = $this->filemtime($file);
-                               if ($time > $lastCTime) {
-                                       $lastCTime = $time;
+               if(is_resource($dh)) {
+                       while (($file = readdir($dh)) !== false) {
+                               if ($file != '.' and $file != '..') {
+                                       $time = $this->filemtime($file);
+                                       if ($time > $lastCTime) {
+                                               $lastCTime = $time;
+                                       }
                                }
                        }
                }
index 8e7a28fba1ad0a46cc3702c2916363d8ff6c3767..ecd4dae04849a19dccd316bb0f11d0e1f9411b78 100644 (file)
@@ -99,11 +99,13 @@ class SMB extends \OC\Files\Storage\StreamWrapper{
        private function shareMTime() {
                $dh=$this->opendir('');
                $lastCtime=0;
-               while (($file = readdir($dh)) !== false) {
-                       if ($file!='.' and $file!='..') {
-                               $ctime=$this->filemtime($file);
-                               if ($ctime>$lastCtime) {
-                                       $lastCtime=$ctime;
+               if(is_resource($dh)) {
+                       while (($file = readdir($dh)) !== false) {
+                               if ($file!='.' and $file!='..') {
+                                       $ctime=$this->filemtime($file);
+                                       if ($ctime>$lastCtime) {
+                                               $lastCtime=$ctime;
+                                       }
                                }
                        }
                }
index 0baeab1de9734ad9e821ecfb6333e6573f534793..0dd6944281ca232025b68644a0bd49a4e52aefc6 100644 (file)
@@ -23,23 +23,24 @@ if ($dir) {
        $dirlisting = true;
        $dirContent = $view->opendir($dir);
        $i = 0;
-       while(($entryName = readdir($dirContent)) !== false) {
-               if (!\OC\Files\Filesystem::isIgnoredDir($entryName)) {
-                       $pos = strpos($dir.'/', '/', 1);
-                       $tmp = substr($dir, 0, $pos);
-                       $pos = strrpos($tmp, '.d');
-                       $timestamp = substr($tmp, $pos+2);
-                       $result[] = array(
-                                       'id' => $entryName,
-                                       'timestamp' => $timestamp,
-                                       'mime' =>  $view->getMimeType($dir.'/'.$entryName),
-                                       'type' => $view->is_dir($dir.'/'.$entryName) ? 'dir' : 'file',
-                                       'location' => $dir,
-                                       );
+       if(is_resource($dirContent)) {
+               while(($entryName = readdir($dirContent)) !== false) {
+                       if (!\OC\Files\Filesystem::isIgnoredDir($entryName)) {
+                               $pos = strpos($dir.'/', '/', 1);
+                               $tmp = substr($dir, 0, $pos);
+                               $pos = strrpos($tmp, '.d');
+                               $timestamp = substr($tmp, $pos+2);
+                               $result[] = array(
+                                               'id' => $entryName,
+                                               'timestamp' => $timestamp,
+                                               'mime' =>  $view->getMimeType($dir.'/'.$entryName),
+                                               'type' => $view->is_dir($dir.'/'.$entryName) ? 'dir' : 'file',
+                                               'location' => $dir,
+                                               );
+                       }
                }
+               closedir($dirContent);
        }
-       closedir($dirContent);
-
 } else {
        $dirlisting = false;
        $query = \OC_DB::prepare('SELECT `id`,`location`,`timestamp`,`type`,`mime` FROM `*PREFIX*files_trash` WHERE `user` = ?');
index 1a0a7e6f9a9b26b43373738167d3f8ce64eb9806..d98af2dc29688956d2a531a5851973b2ccd65299 100644 (file)
@@ -667,14 +667,16 @@ class OC_App{
                        }
                        $dh = opendir( $apps_dir['path'] );
 
-                       while (($file = readdir($dh)) !== false) {
+                       if(is_resource($dh)) {
+                               while (($file = readdir($dh)) !== false) {
 
-                               if ($file[0] != '.' and is_file($apps_dir['path'].'/'.$file.'/appinfo/app.php')) {
+                                       if ($file[0] != '.' and is_file($apps_dir['path'].'/'.$file.'/appinfo/app.php')) {
 
-                                       $apps[] = $file;
+                                               $apps[] = $file;
 
-                               }
+                                       }
 
+                               }
                        }
 
                }
@@ -868,10 +870,10 @@ class OC_App{
 
 
        /**
-        * Compares the app version with the owncloud version to see if the app 
+        * Compares the app version with the owncloud version to see if the app
         * requires a newer version than the currently active one
         * @param array $owncloudVersions array with 3 entries: major minor bugfix
-        * @param string $appRequired the required version from the xml 
+        * @param string $appRequired the required version from the xml
         * major.minor.bugfix
         * @return boolean true if compatible, otherwise false
         */
index 364cd5a74a1c9a880cef6400da70c0cc124c1ce5..85bfae57295d64e6bb49731519e360ae744db1cf 100644 (file)
@@ -119,7 +119,8 @@ abstract class OC_Archive{
         * @return bool
         */
        function addRecursive($path, $source) {
-               if($dh=opendir($source)) {
+               $dh = opendir($source);
+               if(is_resource($dh)) {
                        $this->addFolder($path);
                        while (($file = readdir($dh)) !== false) {
                                if($file=='.' or $file=='..') {
index 9fee6034a714d34a250727af701a9b4fdf331098..361138e47362138295c67873ca611aa26c12fd55 100644 (file)
@@ -80,9 +80,11 @@ class OC_Cache_File{
                $storage = $this->getStorage();
                if($storage and $storage->is_dir('/')) {
                        $dh=$storage->opendir('/');
-                       while (($file = readdir($dh)) !== false) {
-                               if($file!='.' and $file!='..' and ($prefix==='' || strpos($file, $prefix) === 0)) {
-                                       $storage->unlink('/'.$file);
+                       if(is_resource($dh)) {
+                               while (($file = readdir($dh)) !== false) {
+                                       if($file!='.' and $file!='..' and ($prefix==='' || strpos($file, $prefix) === 0)) {
+                                               $storage->unlink('/'.$file);
+                                       }
                                }
                        }
                }
@@ -94,6 +96,9 @@ class OC_Cache_File{
                if($storage and $storage->is_dir('/')) {
                        $now = time();
                        $dh=$storage->opendir('/');
+                       if(!is_resource($dh)) {
+                               return null;
+                       }
                        while (($file = readdir($dh)) !== false) {
                                if($file!='.' and $file!='..') {
                                        $mtime = $storage->filemtime('/'.$file);
index 2fbd8ca3edbd0b5ae30cecda282ceecbf65ee207..c0bd8e45f39a67729a37baf60ee7073599baa076 100644 (file)
@@ -69,9 +69,11 @@ class OC_Cache_FileGlobal{
                $prefix = $this->fixKey($prefix);
                if($cache_dir and is_dir($cache_dir)) {
                        $dh=opendir($cache_dir);
-                       while (($file = readdir($dh)) !== false) {
-                               if($file!='.' and $file!='..' and ($prefix==='' || strpos($file, $prefix) === 0)) {
-                                       unlink($cache_dir.$file);
+                       if(is_resource($dh)) {
+                               while (($file = readdir($dh)) !== false) {
+                                       if($file!='.' and $file!='..' and ($prefix==='' || strpos($file, $prefix) === 0)) {
+                                               unlink($cache_dir.$file);
+                                       }
                                }
                        }
                }
@@ -88,11 +90,13 @@ class OC_Cache_FileGlobal{
                $cache_dir = self::getCacheDir();
                if($cache_dir and is_dir($cache_dir)) {
                        $dh=opendir($cache_dir);
-                       while (($file = readdir($dh)) !== false) {
-                               if($file!='.' and $file!='..') {
-                                       $mtime = filemtime($cache_dir.$file);
-                                       if ($mtime < $now) {
-                                               unlink($cache_dir.$file);
+                       if(is_resource($dh)) {
+                               while (($file = readdir($dh)) !== false) {
+                                       if($file!='.' and $file!='..') {
+                                               $mtime = filemtime($cache_dir.$file);
+                                               if ($mtime < $now) {
+                                                       unlink($cache_dir.$file);
+                                               }
                                        }
                                }
                        }
index b298813a202c6a5e62b5419f12ba22fb5048af9b..acff45ed5e2c0daa814843f78a5669445bb012c9 100644 (file)
@@ -88,11 +88,13 @@ class ObjectTree extends \Sabre_DAV_ObjectTree {
                } else {
                        Filesystem::mkdir($destination);
                        $dh = Filesystem::opendir($source);
-                       while (($subnode = readdir($dh)) !== false) {
+                       if(is_resource($dh)) {
+                               while (($subnode = readdir($dh)) !== false) {
 
-                               if ($subnode == '.' || $subnode == '..') continue;
-                               $this->copy($source . '/' . $subnode, $destination . '/' . $subnode);
+                                       if ($subnode == '.' || $subnode == '..') continue;
+                                       $this->copy($source . '/' . $subnode, $destination . '/' . $subnode);
 
+                               }
                        }
                }
 
index 87fa7c1365a2ecd3fa2a805d28a83d5f1ead3006..9d180820e9d28f7fd1f501110f145b7a39694663 100644 (file)
@@ -159,20 +159,22 @@ class Scanner extends BasicEmitter {
                $newChildren = array();
                if ($this->storage->is_dir($path) && ($dh = $this->storage->opendir($path))) {
                        \OC_DB::beginTransaction();
-                       while (($file = readdir($dh)) !== false) {
-                               $child = ($path) ? $path . '/' . $file : $file;
-                               if (!Filesystem::isIgnoredDir($file)) {
-                                       $newChildren[] = $file;
-                                       $data = $this->scanFile($child, $reuse, true);
-                                       if ($data) {
-                                               if ($data['size'] === -1) {
-                                                       if ($recursive === self::SCAN_RECURSIVE) {
-                                                               $childQueue[] = $child;
-                                                       } else {
-                                                               $size = -1;
+                       if(is_resource($dh)) {
+                               while (($file = readdir($dh)) !== false) {
+                                       $child = ($path) ? $path . '/' . $file : $file;
+                                       if (!Filesystem::isIgnoredDir($file)) {
+                                               $newChildren[] = $file;
+                                               $data = $this->scanFile($child, $reuse, true);
+                                               if ($data) {
+                                                       if ($data['size'] === -1) {
+                                                               if ($recursive === self::SCAN_RECURSIVE) {
+                                                                       $childQueue[] = $child;
+                                                               } else {
+                                                                       $size = -1;
+                                                               }
+                                                       } else if ($size !== -1) {
+                                                               $size += $data['size'];
                                                        }
-                                               } else if ($size !== -1) {
-                                                       $size += $data['size'];
                                                }
                                        }
                                }
index 01560f34fdef0d30149399c86e9e08c061db0984..a5b79f0e96763df55e01cf3f70aa45ec56f39f4f 100644 (file)
@@ -142,13 +142,15 @@ abstract class Common implements \OC\Files\Storage\Storage {
                        return false;
                } else {
                        $directoryHandle = $this->opendir($directory);
-                       while (($contents = readdir($directoryHandle)) !== false) {
-                               if (!\OC\Files\Filesystem::isIgnoredDir($contents)) {
-                                       $path = $directory . '/' . $contents;
-                                       if ($this->is_dir($path)) {
-                                               $this->deleteAll($path);
-                                       } else {
-                                               $this->unlink($path);
+                       if(is_resource($directoryHandle)) {
+                               while (($contents = readdir($directoryHandle)) !== false) {
+                                       if (!\OC\Files\Filesystem::isIgnoredDir($contents)) {
+                                               $path = $directory . '/' . $contents;
+                                               if ($this->is_dir($path)) {
+                                                       $this->deleteAll($path);
+                                               } else {
+                                                       $this->unlink($path);
+                                               }
                                        }
                                }
                        }
@@ -224,7 +226,8 @@ abstract class Common implements \OC\Files\Storage\Storage {
        }
 
        private function addLocalFolder($path, $target) {
-               if ($dh = $this->opendir($path)) {
+               $dh = $this->opendir($path);
+               if(is_resource($dh)) {
                        while (($file = readdir($dh)) !== false) {
                                if ($file !== '.' and $file !== '..') {
                                        if ($this->is_dir($path . '/' . $file)) {
@@ -242,7 +245,7 @@ abstract class Common implements \OC\Files\Storage\Storage {
        protected function searchInDir($query, $dir = '') {
                $files = array();
                $dh = $this->opendir($dir);
-               if ($dh) {
+               if (is_resource($dh)) {
                        while (($item = readdir($dh)) !== false) {
                                if ($item == '.' || $item == '..') continue;
                                if (strstr(strtolower($item), strtolower($query)) !== false) {
index 8aee12bf6fe2b157e490a40beb2697383032e6ae..14de92c20050df7489fffc0ae133394d809dc807 100644 (file)
@@ -500,9 +500,11 @@ class View {
                                } else {
                                        if ($this->is_dir($path1) && ($dh = $this->opendir($path1))) {
                                                $result = $this->mkdir($path2);
-                                               while (($file = readdir($dh)) !== false) {
-                                                       if (!Filesystem::isIgnoredDir($file)) {
-                                                               $result = $this->copy($path1 . '/' . $file, $path2 . '/' . $file);
+                                               if(is_resource($dh)) {
+                                                       while (($file = readdir($dh)) !== false) {
+                                                               if (!Filesystem::isIgnoredDir($file)) {
+                                                                       $result = $this->copy($path1 . '/' . $file, $path2 . '/' . $file);
+                                                               }
                                                        }
                                                }
                                        } else {
index b9684eaeea051967af9b09dfcdd84b41c38fe5b6..607e6da726550ec4cacefeb6ec12f8fe6d387ab3 100644 (file)
@@ -107,10 +107,12 @@ class OC_Installer{
                if(!is_file($extractDir.'/appinfo/info.xml')) {
                        //try to find it in a subdir
                        $dh=opendir($extractDir);
-                       while (($folder = readdir($dh)) !== false) {
-                               if($folder[0]!='.' and is_dir($extractDir.'/'.$folder)) {
-                                       if(is_file($extractDir.'/'.$folder.'/appinfo/info.xml')) {
-                                               $extractDir.='/'.$folder;
+                       if(is_resource($dh)) {
+                               while (($folder = readdir($dh)) !== false) {
+                                       if($folder[0]!='.' and is_dir($extractDir.'/'.$folder)) {
+                                               if(is_file($extractDir.'/'.$folder.'/appinfo/info.xml')) {
+                                                       $extractDir.='/'.$folder;
+                                               }
                                        }
                                }
                        }