Merge pull request #15986 from civicrm/5.20
[civicrm-core.git] / CRM / Utils / Zip.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Utilities for working with zip files
20 */
21 class CRM_Utils_Zip {
22
23 /**
24 * Given a zip file which contains a single root directory, determine the root's name.
25 *
26 * @param ZipArchive $zip
27 *
28 * @return mixed
29 * FALSE if #root level items !=1; otherwise, the name of base dir
30 */
31 public static function findBaseDirName(ZipArchive $zip) {
32 $cnt = $zip->numFiles;
33
34 $base = FALSE;
35 $baselen = FALSE;
36
37 for ($i = 0; $i < $cnt; $i++) {
38 $filename = $zip->getNameIndex($i);
39 if ($base === FALSE) {
40 if (preg_match('/^[^\/]+\/$/', $filename) && $filename != './' && $filename != '../') {
41 $base = $filename;
42 $baselen = strlen($filename);
43 }
44 else {
45 return FALSE;
46 }
47 }
48 elseif (0 != substr_compare($base, $filename, 0, $baselen)) {
49 return FALSE;
50 }
51 }
52
53 return $base;
54 }
55
56 /**
57 * Given a zip file, find all directory names in the root
58 *
59 * @param ZipArchive $zip
60 *
61 * @return array(string)
62 * no trailing /
63 */
64 public static function findBaseDirs(ZipArchive $zip) {
65 $cnt = $zip->numFiles;
66 $basedirs = [];
67
68 for ($i = 0; $i < $cnt; $i++) {
69 $filename = $zip->getNameIndex($i);
70 // hypothetically, ./ or ../ would not be legit here
71 if (preg_match('/^[^\/]+\/$/', $filename) && $filename != './' && $filename != '../') {
72 $basedirs[] = rtrim($filename, '/');
73 }
74 }
75
76 return $basedirs;
77 }
78
79 /**
80 * Determine the name of the folder within a zip.
81 *
82 * @param ZipArchive $zip
83 * @param $expected
84 *
85 * @return string|bool
86 * Return string or FALSE
87 */
88 public static function guessBasedir(ZipArchive $zip, $expected) {
89 $candidate = FALSE;
90 $basedirs = CRM_Utils_Zip::findBaseDirs($zip);
91 if (in_array($expected, $basedirs)) {
92 $candidate = $expected;
93 }
94 elseif (count($basedirs) == 1) {
95 $candidate = array_shift($basedirs);
96 }
97 if ($candidate !== FALSE && preg_match('/^[a-zA-Z0-9]/', $candidate)) {
98 return $candidate;
99 }
100 else {
101 return FALSE;
102 }
103 }
104
105 /**
106 * An inefficient helper for creating a ZIP file from data in memory.
107 * This is only intended for building temp files for unit-testing.
108 *
109 * @param string $zipName
110 * file name.
111 * @param array $dirs
112 * Array, list of directory paths.
113 * @param array $files
114 * Array, keys are file names and values are file contents.
115 * @return bool
116 */
117 public static function createTestZip($zipName, $dirs, $files) {
118 $zip = new ZipArchive();
119 $res = $zip->open($zipName, ZipArchive::CREATE);
120 if ($res === TRUE) {
121 foreach ($dirs as $dir) {
122 if (!$zip->addEmptyDir($dir)) {
123 return FALSE;
124 }
125 }
126 foreach ($files as $fileName => $fileData) {
127 if (!$zip->addFromString($fileName, $fileData)) {
128 return FALSE;
129 }
130 }
131 $zip->close();
132 }
133 else {
134 return FALSE;
135 }
136 return TRUE;
137 }
138
139 }