| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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 |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Utilities for working with zip files |
| 38 | */ |
| 39 | class CRM_Utils_Zip { |
| 40 | |
| 41 | /** |
| 42 | * Given a zip file which contains a single root directory, determine the root's name. |
| 43 | * |
| 44 | * @param ZipArchive $zip |
| 45 | * |
| 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 | } |
| 61 | else { |
| 62 | return FALSE; |
| 63 | } |
| 64 | } elseif (0 != substr_compare($base, $filename, 0, $baselen)) { |
| 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 | * |
| 75 | * @param ZipArchive $zip |
| 76 | * |
| 77 | * @return array(string), no trailing / |
| 78 | */ |
| 79 | static public function findBaseDirs(ZipArchive $zip) { |
| 80 | $cnt = $zip->numFiles; |
| 81 | $basedirs = array(); |
| 82 | |
| 83 | for ($i = 0; $i < $cnt; $i++) { |
| 84 | $filename = $zip->getNameIndex($i); |
| 85 | // hypothetically, ./ or ../ would not be legit here |
| 86 | if (preg_match('/^[^\/]+\/$/', $filename) && $filename != './' && $filename != '../') { |
| 87 | $basedirs[] = rtrim($filename, '/'); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return $basedirs; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Determine the name of the folder within a zip |
| 96 | * |
| 97 | * @param ZipArchive $zip |
| 98 | * @param $expected |
| 99 | * |
| 100 | * @return string or FALSE |
| 101 | */ |
| 102 | static public function guessBasedir(ZipArchive $zip, $expected) { |
| 103 | $candidate = FALSE; |
| 104 | $basedirs = CRM_Utils_Zip::findBaseDirs($zip); |
| 105 | if (in_array($expected, $basedirs)) { |
| 106 | $candidate = $expected; |
| 107 | } |
| 108 | elseif (count($basedirs) == 1) { |
| 109 | $candidate = array_shift($basedirs); |
| 110 | } |
| 111 | if ($candidate !== FALSE && preg_match('/^[a-zA-Z0-9]/', $candidate)) { |
| 112 | return $candidate; |
| 113 | } |
| 114 | else { |
| 115 | return FALSE; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * An inefficient helper for creating a ZIP file from data in memory. |
| 122 | * This is only intended for building temp files for unit-testing. |
| 123 | * |
| 124 | * @param $zipName |
| 125 | * String, file name. |
| 126 | * @param $dirs |
| 127 | * Array, list of directory paths. |
| 128 | * @param $files |
| 129 | * Array, keys are file names and values are file contents. |
| 130 | * @return bool |
| 131 | */ |
| 132 | static public function createTestZip($zipName, $dirs, $files) { |
| 133 | $zip = new ZipArchive(); |
| 134 | $res = $zip->open($zipName, ZipArchive::CREATE); |
| 135 | if ($res === TRUE) { |
| 136 | foreach ($dirs as $dir) { |
| 137 | if (!$zip->addEmptyDir($dir)) { |
| 138 | return FALSE; |
| 139 | } |
| 140 | } |
| 141 | foreach ($files as $fileName => $fileData) { |
| 142 | if (!$zip->addFromString($fileName, $fileData)) { |
| 143 | return FALSE; |
| 144 | } |
| 145 | } |
| 146 | $zip->close(); |
| 147 | } |
| 148 | else { |
| 149 | return FALSE; |
| 150 | } |
| 151 | return TRUE; |
| 152 | } |
| 153 | } |