Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 8 | * |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2019, British Columbia Institute of Technology |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/) |
| 33 | * @license https://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Zip Compression Class |
| 42 | * |
| 43 | * This class is based on a library I found at Zend: |
| 44 | * http://www.zend.com/codex.php?id=696&single=1 |
| 45 | * |
| 46 | * The original library is a little rough around the edges so I |
| 47 | * refactored it and added several additional methods -- Rick Ellis |
| 48 | * |
| 49 | * @package CodeIgniter |
| 50 | * @subpackage Libraries |
| 51 | * @category Encryption |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 52 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 53 | * @link https://codeigniter.com/user_guide/libraries/zip.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | */ |
vkeranov | 95b2f4f | 2012-06-16 20:37:11 +0300 | [diff] [blame] | 55 | class CI_Zip { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 57 | /** |
| 58 | * Zip data in string form |
| 59 | * |
| 60 | * @var string |
| 61 | */ |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 62 | public $zipdata = ''; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 63 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 64 | /** |
| 65 | * Zip data for a directory in string form |
| 66 | * |
| 67 | * @var string |
| 68 | */ |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 69 | public $directory = ''; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 70 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 71 | /** |
| 72 | * Number of files/folder in zip file |
| 73 | * |
| 74 | * @var int |
| 75 | */ |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 76 | public $entries = 0; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 77 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 78 | /** |
| 79 | * Number of files in zip |
| 80 | * |
| 81 | * @var int |
| 82 | */ |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 83 | public $file_num = 0; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 84 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 85 | /** |
| 86 | * relative offset of local header |
| 87 | * |
| 88 | * @var int |
| 89 | */ |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 90 | public $offset = 0; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 91 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 92 | /** |
| 93 | * Reference to time at init |
| 94 | * |
| 95 | * @var int |
| 96 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 97 | public $now; |
Andrey Andreev | 309d701 | 2014-12-04 11:47:26 +0200 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * The level of compression |
| 101 | * |
| 102 | * Ranges from 0 to 9, with 9 being the highest level. |
| 103 | * |
| 104 | * @var int |
| 105 | */ |
| 106 | public $compression_level = 2; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 108 | /** |
Andrey Andreev | c0c74d5 | 2017-01-19 15:26:35 +0200 | [diff] [blame] | 109 | * mbstring.func_overload flag |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 110 | * |
| 111 | * @var bool |
| 112 | */ |
Andrey Andreev | c0c74d5 | 2017-01-19 15:26:35 +0200 | [diff] [blame] | 113 | protected static $func_overload; |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 114 | |
| 115 | /** |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 116 | * Initialize zip compression class |
| 117 | * |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 118 | * @return void |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 119 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 120 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | { |
Andrey Andreev | c0c74d5 | 2017-01-19 15:26:35 +0200 | [diff] [blame] | 122 | isset(self::$func_overload) OR self::$func_overload = (extension_loaded('mbstring') && ini_get('mbstring.func_overload')); |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 123 | |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 124 | $this->now = time(); |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 125 | log_message('info', 'Zip Compression Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // -------------------------------------------------------------------- |
| 129 | |
| 130 | /** |
| 131 | * Add Directory |
| 132 | * |
| 133 | * Lets you add a virtual directory into which you can place files. |
| 134 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 135 | * @param mixed $directory the directory name. Can be string or array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | * @return void |
| 137 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 138 | public function add_dir($directory) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | { |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 140 | foreach ((array) $directory as $dir) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 142 | if ( ! preg_match('|.+/$|', $dir)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | { |
| 144 | $dir .= '/'; |
| 145 | } |
| 146 | |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 147 | $dir_time = $this->_get_mod_time($dir); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 148 | $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 152 | // -------------------------------------------------------------------- |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 153 | |
| 154 | /** |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 155 | * Get file/directory modification time |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | * |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 157 | * If this is a newly created file/dir, we will set the time to 'now' |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 158 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 159 | * @param string $dir path to file |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 160 | * @return array filemtime/filemdate |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 161 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 162 | protected function _get_mod_time($dir) |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 163 | { |
Thomas Traub | 98f85b1 | 2011-12-05 14:58:29 +0100 | [diff] [blame] | 164 | // filemtime() may return false, but raises an error for non-existing files |
Ahmad Anbar | 00421bf | 2014-03-14 16:49:47 +0200 | [diff] [blame] | 165 | $date = file_exists($dir) ? getdate(filemtime($dir)) : getdate($this->now); |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 166 | |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 167 | return array( |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame] | 168 | 'file_mtime' => ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2, |
| 169 | 'file_mdate' => (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday'] |
| 170 | ); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 171 | } |
| 172 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | // -------------------------------------------------------------------- |
| 174 | |
| 175 | /** |
| 176 | * Add Directory |
| 177 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 178 | * @param string $dir the directory name |
| 179 | * @param int $file_mtime |
| 180 | * @param int $file_mdate |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | * @return void |
| 182 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 183 | protected function _add_dir($dir, $file_mtime, $file_mdate) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 184 | { |
Andrey Andreev | e34f1a7 | 2012-01-10 22:41:52 +0200 | [diff] [blame] | 185 | $dir = str_replace('\\', '/', $dir); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | |
| 187 | $this->zipdata .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 188 | "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00" |
| 189 | .pack('v', $file_mtime) |
| 190 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | .pack('V', 0) // crc32 |
| 192 | .pack('V', 0) // compressed filesize |
| 193 | .pack('V', 0) // uncompressed filesize |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 194 | .pack('v', self::strlen($dir)) // length of pathname |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | .pack('v', 0) // extra field length |
| 196 | .$dir |
| 197 | // below is "data descriptor" segment |
| 198 | .pack('V', 0) // crc32 |
| 199 | .pack('V', 0) // compressed filesize |
| 200 | .pack('V', 0); // uncompressed filesize |
| 201 | |
| 202 | $this->directory .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 203 | "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00" |
| 204 | .pack('v', $file_mtime) |
| 205 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | .pack('V',0) // crc32 |
| 207 | .pack('V',0) // compressed filesize |
| 208 | .pack('V',0) // uncompressed filesize |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 209 | .pack('v', self::strlen($dir)) // length of pathname |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | .pack('v', 0) // extra field length |
| 211 | .pack('v', 0) // file comment length |
| 212 | .pack('v', 0) // disk number start |
| 213 | .pack('v', 0) // internal file attributes |
| 214 | .pack('V', 16) // external file attributes - 'directory' bit set |
| 215 | .pack('V', $this->offset) // relative offset of local header |
| 216 | .$dir; |
| 217 | |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 218 | $this->offset = self::strlen($this->zipdata); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | $this->entries++; |
| 220 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 221 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | // -------------------------------------------------------------------- |
| 223 | |
| 224 | /** |
| 225 | * Add Data to Zip |
| 226 | * |
| 227 | * Lets you add files to the archive. If the path is included |
Andrey Andreev | 0b5a486 | 2012-02-29 23:44:00 +0200 | [diff] [blame] | 228 | * in the filename it will be placed within a directory. Make |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | * sure you use add_dir() first to create the folder. |
| 230 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 231 | * @param mixed $filepath A single filepath or an array of file => data pairs |
| 232 | * @param string $data Single file contents |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 234 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 235 | public function add_data($filepath, $data = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 236 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | if (is_array($filepath)) |
| 238 | { |
| 239 | foreach ($filepath as $path => $data) |
| 240 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 241 | $file_data = $this->_get_mod_time($path); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 242 | $this->_add_data($path, $data, $file_data['file_mtime'], $file_data['file_mdate']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | else |
| 246 | { |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 247 | $file_data = $this->_get_mod_time($filepath); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 248 | $this->_add_data($filepath, $data, $file_data['file_mtime'], $file_data['file_mdate']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | // -------------------------------------------------------------------- |
| 253 | |
| 254 | /** |
| 255 | * Add Data to Zip |
| 256 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 257 | * @param string $filepath the file name/path |
| 258 | * @param string $data the data to be encoded |
| 259 | * @param int $file_mtime |
| 260 | * @param int $file_mdate |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 262 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 263 | protected function _add_data($filepath, $data, $file_mtime, $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 265 | $filepath = str_replace('\\', '/', $filepath); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 267 | $uncompressed_size = self::strlen($data); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 268 | $crc32 = crc32($data); |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 269 | $gzdata = self::substr(gzcompress($data, $this->compression_level), 2, -4); |
| 270 | $compressed_size = self::strlen($gzdata); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | |
| 272 | $this->zipdata .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 273 | "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00" |
| 274 | .pack('v', $file_mtime) |
| 275 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | .pack('V', $crc32) |
| 277 | .pack('V', $compressed_size) |
| 278 | .pack('V', $uncompressed_size) |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 279 | .pack('v', self::strlen($filepath)) // length of filename |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | .pack('v', 0) // extra field length |
| 281 | .$filepath |
| 282 | .$gzdata; // "file data" segment |
| 283 | |
| 284 | $this->directory .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 285 | "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00" |
| 286 | .pack('v', $file_mtime) |
| 287 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | .pack('V', $crc32) |
| 289 | .pack('V', $compressed_size) |
| 290 | .pack('V', $uncompressed_size) |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 291 | .pack('v', self::strlen($filepath)) // length of filename |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | .pack('v', 0) // extra field length |
| 293 | .pack('v', 0) // file comment length |
| 294 | .pack('v', 0) // disk number start |
| 295 | .pack('v', 0) // internal file attributes |
| 296 | .pack('V', 32) // external file attributes - 'archive' bit set |
| 297 | .pack('V', $this->offset) // relative offset of local header |
| 298 | .$filepath; |
| 299 | |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 300 | $this->offset = self::strlen($this->zipdata); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | $this->entries++; |
| 302 | $this->file_num++; |
| 303 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 304 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | // -------------------------------------------------------------------- |
| 306 | |
| 307 | /** |
| 308 | * Read the contents of a file and add it to the zip |
| 309 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 310 | * @param string $path |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 311 | * @param bool $archive_filepath |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 313 | */ |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 314 | public function read_file($path, $archive_filepath = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | { |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 316 | if (file_exists($path) && FALSE !== ($data = file_get_contents($path))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 317 | { |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 318 | if (is_string($archive_filepath)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | { |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 320 | $name = str_replace('\\', '/', $archive_filepath); |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | $name = str_replace('\\', '/', $path); |
| 325 | |
Andrey Andreev | aa9a4f7 | 2014-01-28 12:05:51 +0200 | [diff] [blame] | 326 | if ($archive_filepath === FALSE) |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 327 | { |
| 328 | $name = preg_replace('|.*/(.+)|', '\\1', $name); |
| 329 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | $this->add_data($name, $data); |
| 333 | return TRUE; |
| 334 | } |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 335 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 336 | return FALSE; |
| 337 | } |
| 338 | |
| 339 | // ------------------------------------------------------------------------ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 340 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | /** |
| 342 | * Read a directory and add it to the zip. |
| 343 | * |
| 344 | * This function recursively reads a folder and everything it contains (including |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 345 | * sub-folders) and creates a zip based on it. Whatever directory structure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 346 | * is in the original file path will be recreated in the zip file. |
| 347 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 348 | * @param string $path path to source directory |
| 349 | * @param bool $preserve_filepath |
| 350 | * @param string $root_path |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | * @return bool |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 352 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 353 | public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL) |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 354 | { |
tschechniker | c8175be | 2012-04-04 14:47:30 +0300 | [diff] [blame] | 355 | $path = rtrim($path, '/\\').DIRECTORY_SEPARATOR; |
Derek Jones | 2735b3e | 2010-05-11 08:58:21 -0500 | [diff] [blame] | 356 | if ( ! $fp = @opendir($path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 358 | return FALSE; |
| 359 | } |
| 360 | |
| 361 | // Set the original directory root for child dir's to use as relative |
| 362 | if ($root_path === NULL) |
| 363 | { |
Kyle Gadd | b7a8fbb | 2015-04-03 17:37:44 -0600 | [diff] [blame] | 364 | $root_path = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, dirname($path)).DIRECTORY_SEPARATOR; |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | while (FALSE !== ($file = readdir($fp))) |
| 368 | { |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 369 | if ($file[0] === '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 371 | continue; |
| 372 | } |
| 373 | |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame] | 374 | if (is_dir($path.$file)) |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 375 | { |
tschechniker | c8175be | 2012-04-04 14:47:30 +0300 | [diff] [blame] | 376 | $this->read_dir($path.$file.DIRECTORY_SEPARATOR, $preserve_filepath, $root_path); |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 377 | } |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 378 | elseif (FALSE !== ($data = file_get_contents($path.$file))) |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 379 | { |
Tobias Tschech | 9664cc9 | 2012-04-04 15:22:33 +0300 | [diff] [blame] | 380 | $name = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $path); |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 381 | if ($preserve_filepath === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | { |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 383 | $name = str_replace($root_path, '', $name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | } |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame] | 385 | |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 386 | $this->add_data($name.$file, $data); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | } |
Derek Jones | 2735b3e | 2010-05-11 08:58:21 -0500 | [diff] [blame] | 389 | |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 390 | closedir($fp); |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 391 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | // -------------------------------------------------------------------- |
| 395 | |
| 396 | /** |
| 397 | * Get the Zip file |
| 398 | * |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 399 | * @return string (binary encoded) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 400 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 401 | public function get_zip() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | { |
| 403 | // Is there any data to return? |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 404 | if ($this->entries === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | { |
| 406 | return FALSE; |
| 407 | } |
| 408 | |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 409 | return $this->zipdata |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 410 | .$this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00" |
| 411 | .pack('v', $this->entries) // total # of entries "on this disk" |
| 412 | .pack('v', $this->entries) // total # of entries overall |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 413 | .pack('V', self::strlen($this->directory)) // size of central dir |
| 414 | .pack('V', self::strlen($this->zipdata)) // offset to start of central dir |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 415 | ."\x00\x00"; // .zip file comment length |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 417 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | // -------------------------------------------------------------------- |
| 419 | |
| 420 | /** |
| 421 | * Write File to the specified directory |
| 422 | * |
| 423 | * Lets you write a file |
| 424 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 425 | * @param string $filepath the file name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 427 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 428 | public function archive($filepath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | { |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 430 | if ( ! ($fp = @fopen($filepath, 'w+b'))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | { |
| 432 | return FALSE; |
| 433 | } |
| 434 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 435 | flock($fp, LOCK_EX); |
Andrey Andreev | d8b1ad3 | 2014-01-15 17:42:52 +0200 | [diff] [blame] | 436 | |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 437 | for ($result = $written = 0, $data = $this->get_zip(), $length = self::strlen($data); $written < $length; $written += $result) |
Andrey Andreev | d8b1ad3 | 2014-01-15 17:42:52 +0200 | [diff] [blame] | 438 | { |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 439 | if (($result = fwrite($fp, self::substr($data, $written))) === FALSE) |
Andrey Andreev | d8b1ad3 | 2014-01-15 17:42:52 +0200 | [diff] [blame] | 440 | { |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | flock($fp, LOCK_UN); |
| 446 | fclose($fp); |
| 447 | |
Andrey Andreev | d8b1ad3 | 2014-01-15 17:42:52 +0200 | [diff] [blame] | 448 | return is_int($result); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // -------------------------------------------------------------------- |
| 452 | |
| 453 | /** |
| 454 | * Download |
| 455 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 456 | * @param string $filename the file name |
Andrey Andreev | 0b5a486 | 2012-02-29 23:44:00 +0200 | [diff] [blame] | 457 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 459 | public function download($filename = 'backup.zip') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | { |
Andrey Andreev | e34f1a7 | 2012-01-10 22:41:52 +0200 | [diff] [blame] | 461 | if ( ! preg_match('|.+?\.zip$|', $filename)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | { |
| 463 | $filename .= '.zip'; |
| 464 | } |
| 465 | |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 466 | get_instance()->load->helper('download'); |
Derek Allard | 8dc2c7c | 2009-12-07 16:07:15 +0000 | [diff] [blame] | 467 | $get_zip = $this->get_zip(); |
Derek Allard | 8dc2c7c | 2009-12-07 16:07:15 +0000 | [diff] [blame] | 468 | $zip_content =& $get_zip; |
| 469 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | force_download($filename, $zip_content); |
| 471 | } |
| 472 | |
| 473 | // -------------------------------------------------------------------- |
| 474 | |
| 475 | /** |
| 476 | * Initialize Data |
| 477 | * |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 478 | * Lets you clear current zip data. Useful if you need to create |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 479 | * multiple zips with different data. |
| 480 | * |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 481 | * @return CI_Zip |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 482 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 483 | public function clear_data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | { |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 485 | $this->zipdata = ''; |
| 486 | $this->directory = ''; |
| 487 | $this->entries = 0; |
| 488 | $this->file_num = 0; |
| 489 | $this->offset = 0; |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 490 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 492 | |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 493 | // -------------------------------------------------------------------- |
| 494 | |
| 495 | /** |
| 496 | * Byte-safe strlen() |
| 497 | * |
| 498 | * @param string $str |
| 499 | * @return int |
| 500 | */ |
| 501 | protected static function strlen($str) |
| 502 | { |
Andrey Andreev | c0c74d5 | 2017-01-19 15:26:35 +0200 | [diff] [blame] | 503 | return (self::$func_overload) |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 504 | ? mb_strlen($str, '8bit') |
| 505 | : strlen($str); |
| 506 | } |
| 507 | |
| 508 | // -------------------------------------------------------------------- |
| 509 | |
| 510 | /** |
| 511 | * Byte-safe substr() |
| 512 | * |
| 513 | * @param string $str |
| 514 | * @param int $start |
| 515 | * @param int $length |
| 516 | * @return string |
| 517 | */ |
| 518 | protected static function substr($str, $start, $length = NULL) |
| 519 | { |
Andrey Andreev | c0c74d5 | 2017-01-19 15:26:35 +0200 | [diff] [blame] | 520 | if (self::$func_overload) |
Andrey Andreev | 4e2cdec | 2016-10-28 14:19:08 +0300 | [diff] [blame] | 521 | { |
| 522 | // mb_substr($str, $start, null, '8bit') returns an empty |
| 523 | // string on PHP 5.3 |
| 524 | isset($length) OR $length = ($start >= 0 ? self::strlen($str) - $start : -$start); |
| 525 | return mb_substr($str, $start, $length, '8bit'); |
| 526 | } |
| 527 | |
| 528 | return isset($length) |
| 529 | ? substr($str, $start, $length) |
| 530 | : substr($str, $start); |
| 531 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 532 | } |