浏览代码

Change zip handling methods (#3470)

Before, we were using zip function but they are marked as deprecated as of
PHP 8.0. It's not safe to use them anymore since they can be removed at any
given time.
Now, we are using the ZipArchive class to handle our zip methods. It's safe
to use it since it's available for PHP 5.2 and higher.

See #3460
Alexis Degrugillier 5 年之前
父节点
当前提交
fd80387541
共有 2 个文件被更改,包括 12 次插入21 次删除
  1. 0 2
      .travis.yml
  2. 12 19
      app/Controllers/importExportController.php

+ 0 - 2
.travis.yml

@@ -4,8 +4,6 @@ jobs:
   fast_finish: true
   fast_finish: true
   allow_failures:
   allow_failures:
     - name: "Translations"
     - name: "Translations"
-    - name: "PHP 8.0 Syntax, linter and tests"
-
 
 
   include:
   include:
     - name: "PHP 8.0 Syntax, linter and tests"
     - name: "PHP 8.0 Syntax, linter and tests"

+ 12 - 19
app/Controllers/importExportController.php

@@ -64,31 +64,24 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {
 
 
 		// We try to list all files according to their type
 		// We try to list all files according to their type
 		$list = array();
 		$list = array();
-		if ($type_file === 'zip' && extension_loaded('zip')) {
-			$zip = zip_open($path);
-			if (!is_resource($zip)) {
+		if ('zip' === $type_file && extension_loaded('zip')) {
+			$zip = new ZipArchive();
+			$result = $zip->open($path);
+			if (true !== $result) {
 				// zip_open cannot open file: something is wrong
 				// zip_open cannot open file: something is wrong
-				throw new FreshRSS_Zip_Exception($zip);
+				throw new FreshRSS_Zip_Exception($result);
 			}
 			}
-			while (($zipfile = zip_read($zip)) !== false) {
-				if (!is_resource($zipfile)) {
-					// zip_entry() can also return an error code!
-					throw new FreshRSS_Zip_Exception($zipfile);
-				} else {
-					$type_zipfile = self::guessFileType(zip_entry_name($zipfile));
-					if ($type_file !== 'unknown') {
-						$list_files[$type_zipfile][] = zip_entry_read(
-							$zipfile,
-							zip_entry_filesize($zipfile)
-						);
-					}
+			for ($i = 0; $i < $zip->numFiles; $i++) {
+				$type_zipfile = self::guessFileType($zip->getNameIndex($i));
+				if ('unknown' !== $type_zipfile) {
+					$list_files[$type_zipfile][] = $zip->getFromIndex($i);
 				}
 				}
 			}
 			}
-			zip_close($zip);
-		} elseif ($type_file === 'zip') {
+			$zip->close();
+		} elseif ('zip' === $type_file) {
 			// ZIP extension is not loaded
 			// ZIP extension is not loaded
 			throw new FreshRSS_ZipMissing_Exception();
 			throw new FreshRSS_ZipMissing_Exception();
-		} elseif ($type_file !== 'unknown') {
+		} elseif ('unknown' !== $type_file) {
 			$list_files[$type_file][] = file_get_contents($path);
 			$list_files[$type_file][] = file_get_contents($path);
 		}
 		}