‪TYPO3CMS  9.5
ZipService.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
20 
27 {
36  public function ‪extract(string $fileName, string $directory): bool
37  {
38  $this->‪assertDirectoryIsWritable($directory);
39 
40  $zip = new \ZipArchive();
41  $state = $zip->open($fileName);
42  if ($state !== true) {
43  throw new ‪ExtractException(
44  sprintf('Unable to open zip file %s, error code %d', $fileName, $state),
45  1565709712
46  );
47  }
48 
49  $result = $zip->extractTo($directory);
50  $zip->close();
51  if ($result) {
52  GeneralUtility::fixPermissions(rtrim($directory, '/'), true);
53  }
54  return $result;
55  }
56 
62  public function ‪verify(string $fileName): bool
63  {
64  $zip = new \ZipArchive();
65  $state = $zip->open($fileName);
66  if ($state !== true) {
67  throw new ‪ExtractException(
68  sprintf('Unable to open zip file %s, error code %d', $fileName, $state),
69  1565709713
70  );
71  }
72 
73  for ($i = 0; $i < $zip->numFiles; $i++) {
74  $entryName = $zip->getNameIndex($i);
75  if (preg_match('#/(?:\.{2,})+#', $entryName) // Contains any traversal sequence starting with a slash, e.g. /../, /.., /.../
76  || preg_match('#^(?:\.{2,})+/#', $entryName) // Starts with a traversal sequence, e.g. ../, .../
77  ) {
78  throw new ‪ExtractException(
79  sprintf('Suspicious sequence in zip file %s: %s', $fileName, $entryName),
80  1565709714
81  );
82  }
83  }
84 
85  $zip->close();
86  return true;
87  }
88 
92  private function ‪assertDirectoryIsWritable(string $directory): void
93  {
94  if (!is_dir($directory)) {
95  throw new \RuntimeException(
96  sprintf('Directory %s does not exist', $directory),
97  1565773005
98  );
99  }
100  if (!is_writable($directory)) {
101  throw new \RuntimeException(
102  sprintf('Directory %s is not writable', $directory),
103  1565773006
104  );
105  }
106  }
107 }
‪TYPO3\CMS\Core\Service\Archive
Definition: ZipService.php:3
‪TYPO3\CMS\Core\Exception\Archive\ExtractException
Definition: ExtractException.php:24
‪TYPO3\CMS\Core\Service\Archive\ZipService\assertDirectoryIsWritable
‪assertDirectoryIsWritable(string $directory)
Definition: ZipService.php:92
‪TYPO3\CMS\Core\Service\Archive\ZipService\verify
‪bool verify(string $fileName)
Definition: ZipService.php:62
‪TYPO3\CMS\Core\Service\Archive\ZipService\extract
‪bool extract(string $fileName, string $directory)
Definition: ZipService.php:36
‪TYPO3\CMS\Core\Service\Archive\ZipService
Definition: ZipService.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45