3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * Utilities for working with zip files
42 * Given a zip file which contains a single root directory, determine the root's name.
44 * @return mixed; FALSE if #root level items !=1; otherwise, the name of base dir
46 static public function findBaseDirName(ZipArchive
$zip) {
47 $cnt = $zip->numFiles
;
52 for ($i = 0; $i < $cnt; $i++
) {
53 $filename = $zip->getNameIndex($i);
54 if ($base === FALSE) {
55 if (preg_match('/^[^\/]+\/$/', $filename) && $filename != './' && $filename != '../') {
57 $baselen = strlen($filename);
61 } elseif (0 != substr_compare($base, $filename, 0, $baselen)) {
70 * Given a zip file, find all directory names in the root
72 * @return array(string), no trailing /
74 static public function findBaseDirs(ZipArchive
$zip) {
75 $cnt = $zip->numFiles
;
78 for ($i = 0; $i < $cnt; $i++
) {
79 $filename = $zip->getNameIndex($i);
80 // hypothetically, ./ or ../ would not be legit here
81 if (preg_match('/^[^\/]+\/$/', $filename) && $filename != './' && $filename != '../') {
82 $basedirs[] = rtrim($filename, '/');
90 * Determine the name of the folder within a zip
92 * @return string or FALSE
94 static public function guessBasedir(ZipArchive
$zip, $expected) {
96 $basedirs = CRM_Utils_Zip
::findBaseDirs($zip);
97 if (in_array($expected, $basedirs)) {
98 $candidate = $expected;
99 } elseif (count($basedirs) == 1) {
100 $candidate = array_shift($basedirs);
102 if ($candidate !== FALSE && preg_match('/^[a-zA-Z0-9]/', $candidate)) {
111 * An inefficient helper for creating a ZIP file from data in memory.
112 * This is only intended for building temp files for unit-testing.
114 * @param $zipName string, file name
115 * @param $dirs array, list of directory paths
116 * @param $files array, keys are file names and values are file contents
119 static public function createTestZip($zipName, $dirs, $files) {
120 $zip = new ZipArchive();
121 $res = $zip->open($zipName, ZipArchive
::CREATE
);
123 foreach ($dirs as $dir) {
124 if (!$zip->addEmptyDir($dir)) {
128 foreach ($files as $fileName => $fileData) {
129 if (!$zip->addFromString($fileName, $fileData)) {