Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * Utilities for working with zip files | |
36 | */ | |
37 | class CRM_Utils_Zip { | |
38 | ||
39 | /** | |
40 | * Given a zip file which contains a single root directory, determine the root's name. | |
41 | * | |
f4aaa82a EM |
42 | * @param ZipArchive $zip |
43 | * | |
50bfb460 SB |
44 | * @return mixed |
45 | * FALSE if #root level items !=1; otherwise, the name of base dir | |
6a488035 | 46 | */ |
6714d8d2 | 47 | public static function findBaseDirName(ZipArchive $zip) { |
6a488035 TO |
48 | $cnt = $zip->numFiles; |
49 | ||
50 | $base = FALSE; | |
51 | $baselen = FALSE; | |
52 | ||
53 | for ($i = 0; $i < $cnt; $i++) { | |
54 | $filename = $zip->getNameIndex($i); | |
55 | if ($base === FALSE) { | |
56 | if (preg_match('/^[^\/]+\/$/', $filename) && $filename != './' && $filename != '../') { | |
57 | $base = $filename; | |
58 | $baselen = strlen($filename); | |
0db6c3e1 TO |
59 | } |
60 | else { | |
6a488035 TO |
61 | return FALSE; |
62 | } | |
d3e86119 TO |
63 | } |
64 | elseif (0 != substr_compare($base, $filename, 0, $baselen)) { | |
6a488035 TO |
65 | return FALSE; |
66 | } | |
67 | } | |
68 | ||
69 | return $base; | |
70 | } | |
71 | ||
72 | /** | |
73 | * Given a zip file, find all directory names in the root | |
74 | * | |
f4aaa82a EM |
75 | * @param ZipArchive $zip |
76 | * | |
50bfb460 SB |
77 | * @return array(string) |
78 | * no trailing / | |
6a488035 | 79 | */ |
6714d8d2 | 80 | public static function findBaseDirs(ZipArchive $zip) { |
6a488035 | 81 | $cnt = $zip->numFiles; |
be2fb01f | 82 | $basedirs = []; |
6a488035 TO |
83 | |
84 | for ($i = 0; $i < $cnt; $i++) { | |
85 | $filename = $zip->getNameIndex($i); | |
86 | // hypothetically, ./ or ../ would not be legit here | |
87 | if (preg_match('/^[^\/]+\/$/', $filename) && $filename != './' && $filename != '../') { | |
88 | $basedirs[] = rtrim($filename, '/'); | |
89 | } | |
90 | } | |
91 | ||
92 | return $basedirs; | |
93 | } | |
94 | ||
95 | /** | |
fe482240 | 96 | * Determine the name of the folder within a zip. |
6a488035 | 97 | * |
f4aaa82a EM |
98 | * @param ZipArchive $zip |
99 | * @param $expected | |
100 | * | |
50bfb460 SB |
101 | * @return string|bool |
102 | * Return string or FALSE | |
6a488035 | 103 | */ |
6714d8d2 | 104 | public static function guessBasedir(ZipArchive $zip, $expected) { |
6a488035 TO |
105 | $candidate = FALSE; |
106 | $basedirs = CRM_Utils_Zip::findBaseDirs($zip); | |
107 | if (in_array($expected, $basedirs)) { | |
108 | $candidate = $expected; | |
0db6c3e1 TO |
109 | } |
110 | elseif (count($basedirs) == 1) { | |
6a488035 TO |
111 | $candidate = array_shift($basedirs); |
112 | } | |
113 | if ($candidate !== FALSE && preg_match('/^[a-zA-Z0-9]/', $candidate)) { | |
114 | return $candidate; | |
0db6c3e1 TO |
115 | } |
116 | else { | |
6a488035 TO |
117 | return FALSE; |
118 | } | |
119 | } | |
120 | ||
6a488035 TO |
121 | /** |
122 | * An inefficient helper for creating a ZIP file from data in memory. | |
123 | * This is only intended for building temp files for unit-testing. | |
124 | * | |
5a4f6742 CW |
125 | * @param string $zipName |
126 | * file name. | |
127 | * @param array $dirs | |
77855840 | 128 | * Array, list of directory paths. |
5a4f6742 | 129 | * @param array $files |
77855840 | 130 | * Array, keys are file names and values are file contents. |
6a488035 TO |
131 | * @return bool |
132 | */ | |
6714d8d2 | 133 | public static function createTestZip($zipName, $dirs, $files) { |
6a488035 TO |
134 | $zip = new ZipArchive(); |
135 | $res = $zip->open($zipName, ZipArchive::CREATE); | |
136 | if ($res === TRUE) { | |
137 | foreach ($dirs as $dir) { | |
138 | if (!$zip->addEmptyDir($dir)) { | |
139 | return FALSE; | |
140 | } | |
141 | } | |
142 | foreach ($files as $fileName => $fileData) { | |
143 | if (!$zip->addFromString($fileName, $fileData)) { | |
144 | return FALSE; | |
145 | } | |
146 | } | |
147 | $zip->close(); | |
0db6c3e1 TO |
148 | } |
149 | else { | |
e7292422 | 150 | return FALSE; |
6a488035 TO |
151 | } |
152 | return TRUE; | |
153 | } | |
96025800 | 154 | |
6a488035 | 155 | } |