Merge pull request #4759 from totten/master-civimail-status
[civicrm-core.git] / CRM / Utils / Zip.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Utilities for working with zip files
38 */
39class CRM_Utils_Zip {
40
41 /**
42 * Given a zip file which contains a single root directory, determine the root's name.
43 *
f4aaa82a
EM
44 * @param ZipArchive $zip
45 *
6a488035
TO
46 * @return mixed; FALSE if #root level items !=1; otherwise, the name of base dir
47 */
48 static public function findBaseDirName(ZipArchive $zip) {
49 $cnt = $zip->numFiles;
50
51 $base = FALSE;
52 $baselen = FALSE;
53
54 for ($i = 0; $i < $cnt; $i++) {
55 $filename = $zip->getNameIndex($i);
56 if ($base === FALSE) {
57 if (preg_match('/^[^\/]+\/$/', $filename) && $filename != './' && $filename != '../') {
58 $base = $filename;
59 $baselen = strlen($filename);
60 } else {
61 return FALSE;
62 }
63 } elseif (0 != substr_compare($base, $filename, 0, $baselen)) {
64 return FALSE;
65 }
66 }
67
68 return $base;
69 }
70
71 /**
72 * Given a zip file, find all directory names in the root
73 *
f4aaa82a
EM
74 * @param ZipArchive $zip
75 *
6a488035
TO
76 * @return array(string), no trailing /
77 */
78 static public function findBaseDirs(ZipArchive $zip) {
79 $cnt = $zip->numFiles;
80 $basedirs = array();
81
82 for ($i = 0; $i < $cnt; $i++) {
83 $filename = $zip->getNameIndex($i);
84 // hypothetically, ./ or ../ would not be legit here
85 if (preg_match('/^[^\/]+\/$/', $filename) && $filename != './' && $filename != '../') {
86 $basedirs[] = rtrim($filename, '/');
87 }
88 }
89
90 return $basedirs;
91 }
92
93 /**
94 * Determine the name of the folder within a zip
95 *
f4aaa82a
EM
96 * @param ZipArchive $zip
97 * @param $expected
98 *
6a488035
TO
99 * @return string or FALSE
100 */
101 static public function guessBasedir(ZipArchive $zip, $expected) {
102 $candidate = FALSE;
103 $basedirs = CRM_Utils_Zip::findBaseDirs($zip);
104 if (in_array($expected, $basedirs)) {
105 $candidate = $expected;
106 } elseif (count($basedirs) == 1) {
107 $candidate = array_shift($basedirs);
108 }
109 if ($candidate !== FALSE && preg_match('/^[a-zA-Z0-9]/', $candidate)) {
110 return $candidate;
111 } else {
112 return FALSE;
113 }
114 }
115
116
117 /**
118 * An inefficient helper for creating a ZIP file from data in memory.
119 * This is only intended for building temp files for unit-testing.
120 *
121 * @param $zipName string, file name
122 * @param $dirs array, list of directory paths
123 * @param $files array, keys are file names and values are file contents
124 * @return bool
125 */
126 static public function createTestZip($zipName, $dirs, $files) {
127 $zip = new ZipArchive();
128 $res = $zip->open($zipName, ZipArchive::CREATE);
129 if ($res === TRUE) {
130 foreach ($dirs as $dir) {
131 if (!$zip->addEmptyDir($dir)) {
132 return FALSE;
133 }
134 }
135 foreach ($files as $fileName => $fileData) {
136 if (!$zip->addFromString($fileName, $fileData)) {
137 return FALSE;
138 }
139 }
140 $zip->close();
141 } else {
142 return FALSE;
143 }
144 return TRUE;
145 }
146}