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 | * |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2016, 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/) |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://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 | /** |
| 109 | * Initialize zip compression class |
| 110 | * |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 111 | * @return void |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 112 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 113 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | { |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 115 | $this->now = time(); |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 116 | log_message('info', 'Zip Compression Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // -------------------------------------------------------------------- |
| 120 | |
| 121 | /** |
| 122 | * Add Directory |
| 123 | * |
| 124 | * Lets you add a virtual directory into which you can place files. |
| 125 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 126 | * @param mixed $directory the directory name. Can be string or array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | * @return void |
| 128 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 129 | public function add_dir($directory) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | { |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 131 | foreach ((array) $directory as $dir) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 133 | if ( ! preg_match('|.+/$|', $dir)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | { |
| 135 | $dir .= '/'; |
| 136 | } |
| 137 | |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 138 | $dir_time = $this->_get_mod_time($dir); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 139 | $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 143 | // -------------------------------------------------------------------- |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 144 | |
| 145 | /** |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 146 | * Get file/directory modification time |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 147 | * |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 148 | * 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] | 149 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 150 | * @param string $dir path to file |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 151 | * @return array filemtime/filemdate |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 152 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 153 | protected function _get_mod_time($dir) |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 154 | { |
Thomas Traub | 98f85b1 | 2011-12-05 14:58:29 +0100 | [diff] [blame] | 155 | // filemtime() may return false, but raises an error for non-existing files |
Ahmad Anbar | 00421bf | 2014-03-14 16:49:47 +0200 | [diff] [blame] | 156 | $date = file_exists($dir) ? getdate(filemtime($dir)) : getdate($this->now); |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 157 | |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 158 | return array( |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame] | 159 | 'file_mtime' => ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2, |
| 160 | 'file_mdate' => (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday'] |
| 161 | ); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 162 | } |
| 163 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | // -------------------------------------------------------------------- |
| 165 | |
| 166 | /** |
| 167 | * Add Directory |
| 168 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 169 | * @param string $dir the directory name |
| 170 | * @param int $file_mtime |
| 171 | * @param int $file_mdate |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | * @return void |
| 173 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 174 | protected function _add_dir($dir, $file_mtime, $file_mdate) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | { |
Andrey Andreev | e34f1a7 | 2012-01-10 22:41:52 +0200 | [diff] [blame] | 176 | $dir = str_replace('\\', '/', $dir); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | |
| 178 | $this->zipdata .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 179 | "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00" |
| 180 | .pack('v', $file_mtime) |
| 181 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | .pack('V', 0) // crc32 |
| 183 | .pack('V', 0) // compressed filesize |
| 184 | .pack('V', 0) // uncompressed filesize |
| 185 | .pack('v', strlen($dir)) // length of pathname |
| 186 | .pack('v', 0) // extra field length |
| 187 | .$dir |
| 188 | // below is "data descriptor" segment |
| 189 | .pack('V', 0) // crc32 |
| 190 | .pack('V', 0) // compressed filesize |
| 191 | .pack('V', 0); // uncompressed filesize |
| 192 | |
| 193 | $this->directory .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 194 | "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00" |
| 195 | .pack('v', $file_mtime) |
| 196 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | .pack('V',0) // crc32 |
| 198 | .pack('V',0) // compressed filesize |
| 199 | .pack('V',0) // uncompressed filesize |
| 200 | .pack('v', strlen($dir)) // length of pathname |
| 201 | .pack('v', 0) // extra field length |
| 202 | .pack('v', 0) // file comment length |
| 203 | .pack('v', 0) // disk number start |
| 204 | .pack('v', 0) // internal file attributes |
| 205 | .pack('V', 16) // external file attributes - 'directory' bit set |
| 206 | .pack('V', $this->offset) // relative offset of local header |
| 207 | .$dir; |
| 208 | |
| 209 | $this->offset = strlen($this->zipdata); |
| 210 | $this->entries++; |
| 211 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 212 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | // -------------------------------------------------------------------- |
| 214 | |
| 215 | /** |
| 216 | * Add Data to Zip |
| 217 | * |
| 218 | * Lets you add files to the archive. If the path is included |
Andrey Andreev | 0b5a486 | 2012-02-29 23:44:00 +0200 | [diff] [blame] | 219 | * in the filename it will be placed within a directory. Make |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | * sure you use add_dir() first to create the folder. |
| 221 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 222 | * @param mixed $filepath A single filepath or an array of file => data pairs |
| 223 | * @param string $data Single file contents |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 226 | public function add_data($filepath, $data = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 227 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | if (is_array($filepath)) |
| 229 | { |
| 230 | foreach ($filepath as $path => $data) |
| 231 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | $file_data = $this->_get_mod_time($path); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 233 | $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] | 234 | } |
| 235 | } |
| 236 | else |
| 237 | { |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 238 | $file_data = $this->_get_mod_time($filepath); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 239 | $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] | 240 | } |
| 241 | } |
| 242 | |
| 243 | // -------------------------------------------------------------------- |
| 244 | |
| 245 | /** |
| 246 | * Add Data to Zip |
| 247 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 248 | * @param string $filepath the file name/path |
| 249 | * @param string $data the data to be encoded |
| 250 | * @param int $file_mtime |
| 251 | * @param int $file_mdate |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 253 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 254 | protected function _add_data($filepath, $data, $file_mtime, $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 256 | $filepath = str_replace('\\', '/', $filepath); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | |
| 258 | $uncompressed_size = strlen($data); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 259 | $crc32 = crc32($data); |
Andrey Andreev | 309d701 | 2014-12-04 11:47:26 +0200 | [diff] [blame] | 260 | $gzdata = substr(gzcompress($data, $this->compression_level), 2, -4); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | $compressed_size = strlen($gzdata); |
| 262 | |
| 263 | $this->zipdata .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 264 | "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00" |
| 265 | .pack('v', $file_mtime) |
| 266 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | .pack('V', $crc32) |
| 268 | .pack('V', $compressed_size) |
| 269 | .pack('V', $uncompressed_size) |
| 270 | .pack('v', strlen($filepath)) // length of filename |
| 271 | .pack('v', 0) // extra field length |
| 272 | .$filepath |
| 273 | .$gzdata; // "file data" segment |
| 274 | |
| 275 | $this->directory .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 276 | "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00" |
| 277 | .pack('v', $file_mtime) |
| 278 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 279 | .pack('V', $crc32) |
| 280 | .pack('V', $compressed_size) |
| 281 | .pack('V', $uncompressed_size) |
| 282 | .pack('v', strlen($filepath)) // length of filename |
| 283 | .pack('v', 0) // extra field length |
| 284 | .pack('v', 0) // file comment length |
| 285 | .pack('v', 0) // disk number start |
| 286 | .pack('v', 0) // internal file attributes |
| 287 | .pack('V', 32) // external file attributes - 'archive' bit set |
| 288 | .pack('V', $this->offset) // relative offset of local header |
| 289 | .$filepath; |
| 290 | |
| 291 | $this->offset = strlen($this->zipdata); |
| 292 | $this->entries++; |
| 293 | $this->file_num++; |
| 294 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 295 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | // -------------------------------------------------------------------- |
| 297 | |
| 298 | /** |
| 299 | * Read the contents of a file and add it to the zip |
| 300 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 301 | * @param string $path |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 302 | * @param bool $archive_filepath |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 304 | */ |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 305 | public function read_file($path, $archive_filepath = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 306 | { |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 307 | if (file_exists($path) && FALSE !== ($data = file_get_contents($path))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | { |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 309 | if (is_string($archive_filepath)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | { |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 311 | $name = str_replace('\\', '/', $archive_filepath); |
| 312 | } |
| 313 | else |
| 314 | { |
| 315 | $name = str_replace('\\', '/', $path); |
| 316 | |
Andrey Andreev | aa9a4f7 | 2014-01-28 12:05:51 +0200 | [diff] [blame] | 317 | if ($archive_filepath === FALSE) |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 318 | { |
| 319 | $name = preg_replace('|.*/(.+)|', '\\1', $name); |
| 320 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | $this->add_data($name, $data); |
| 324 | return TRUE; |
| 325 | } |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 326 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | return FALSE; |
| 328 | } |
| 329 | |
| 330 | // ------------------------------------------------------------------------ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 331 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | /** |
| 333 | * Read a directory and add it to the zip. |
| 334 | * |
| 335 | * This function recursively reads a folder and everything it contains (including |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 336 | * sub-folders) and creates a zip based on it. Whatever directory structure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 337 | * is in the original file path will be recreated in the zip file. |
| 338 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 339 | * @param string $path path to source directory |
| 340 | * @param bool $preserve_filepath |
| 341 | * @param string $root_path |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | * @return bool |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 343 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 344 | public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL) |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 345 | { |
tschechniker | c8175be | 2012-04-04 14:47:30 +0300 | [diff] [blame] | 346 | $path = rtrim($path, '/\\').DIRECTORY_SEPARATOR; |
Derek Jones | 2735b3e | 2010-05-11 08:58:21 -0500 | [diff] [blame] | 347 | if ( ! $fp = @opendir($path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 349 | return FALSE; |
| 350 | } |
| 351 | |
| 352 | // Set the original directory root for child dir's to use as relative |
| 353 | if ($root_path === NULL) |
| 354 | { |
Kyle Gadd | b7a8fbb | 2015-04-03 17:37:44 -0600 | [diff] [blame] | 355 | $root_path = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, dirname($path)).DIRECTORY_SEPARATOR; |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | while (FALSE !== ($file = readdir($fp))) |
| 359 | { |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 360 | if ($file[0] === '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 362 | continue; |
| 363 | } |
| 364 | |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame] | 365 | if (is_dir($path.$file)) |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 366 | { |
tschechniker | c8175be | 2012-04-04 14:47:30 +0300 | [diff] [blame] | 367 | $this->read_dir($path.$file.DIRECTORY_SEPARATOR, $preserve_filepath, $root_path); |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 368 | } |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 369 | elseif (FALSE !== ($data = file_get_contents($path.$file))) |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 370 | { |
Tobias Tschech | 9664cc9 | 2012-04-04 15:22:33 +0300 | [diff] [blame] | 371 | $name = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $path); |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 372 | if ($preserve_filepath === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 373 | { |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 374 | $name = str_replace($root_path, '', $name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | } |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame] | 376 | |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 377 | $this->add_data($name.$file, $data); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | } |
Derek Jones | 2735b3e | 2010-05-11 08:58:21 -0500 | [diff] [blame] | 380 | |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 381 | closedir($fp); |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 382 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | // -------------------------------------------------------------------- |
| 386 | |
| 387 | /** |
| 388 | * Get the Zip file |
| 389 | * |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 390 | * @return string (binary encoded) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 391 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 392 | public function get_zip() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | { |
| 394 | // Is there any data to return? |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 395 | if ($this->entries === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | { |
| 397 | return FALSE; |
| 398 | } |
| 399 | |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 400 | return $this->zipdata |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 401 | .$this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00" |
| 402 | .pack('v', $this->entries) // total # of entries "on this disk" |
| 403 | .pack('v', $this->entries) // total # of entries overall |
| 404 | .pack('V', strlen($this->directory)) // size of central dir |
| 405 | .pack('V', strlen($this->zipdata)) // offset to start of central dir |
| 406 | ."\x00\x00"; // .zip file comment length |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 408 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | // -------------------------------------------------------------------- |
| 410 | |
| 411 | /** |
| 412 | * Write File to the specified directory |
| 413 | * |
| 414 | * Lets you write a file |
| 415 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 416 | * @param string $filepath the file name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 418 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 419 | public function archive($filepath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | { |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 421 | if ( ! ($fp = @fopen($filepath, 'w+b'))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | { |
| 423 | return FALSE; |
| 424 | } |
| 425 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 426 | flock($fp, LOCK_EX); |
Andrey Andreev | d8b1ad3 | 2014-01-15 17:42:52 +0200 | [diff] [blame] | 427 | |
Andrey Andreev | 1f5090a | 2014-06-03 15:40:30 +0300 | [diff] [blame] | 428 | for ($result = $written = 0, $data = $this->get_zip(), $length = strlen($data); $written < $length; $written += $result) |
Andrey Andreev | d8b1ad3 | 2014-01-15 17:42:52 +0200 | [diff] [blame] | 429 | { |
| 430 | if (($result = fwrite($fp, substr($data, $written))) === FALSE) |
| 431 | { |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | flock($fp, LOCK_UN); |
| 437 | fclose($fp); |
| 438 | |
Andrey Andreev | d8b1ad3 | 2014-01-15 17:42:52 +0200 | [diff] [blame] | 439 | return is_int($result); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | // -------------------------------------------------------------------- |
| 443 | |
| 444 | /** |
| 445 | * Download |
| 446 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 447 | * @param string $filename the file name |
Andrey Andreev | 0b5a486 | 2012-02-29 23:44:00 +0200 | [diff] [blame] | 448 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 450 | public function download($filename = 'backup.zip') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | { |
Andrey Andreev | e34f1a7 | 2012-01-10 22:41:52 +0200 | [diff] [blame] | 452 | if ( ! preg_match('|.+?\.zip$|', $filename)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | { |
| 454 | $filename .= '.zip'; |
| 455 | } |
| 456 | |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 457 | get_instance()->load->helper('download'); |
Derek Allard | 8dc2c7c | 2009-12-07 16:07:15 +0000 | [diff] [blame] | 458 | $get_zip = $this->get_zip(); |
Derek Allard | 8dc2c7c | 2009-12-07 16:07:15 +0000 | [diff] [blame] | 459 | $zip_content =& $get_zip; |
| 460 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | force_download($filename, $zip_content); |
| 462 | } |
| 463 | |
| 464 | // -------------------------------------------------------------------- |
| 465 | |
| 466 | /** |
| 467 | * Initialize Data |
| 468 | * |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 469 | * Lets you clear current zip data. Useful if you need to create |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | * multiple zips with different data. |
| 471 | * |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 472 | * @return CI_Zip |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 473 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 474 | public function clear_data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 475 | { |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 476 | $this->zipdata = ''; |
| 477 | $this->directory = ''; |
| 478 | $this->entries = 0; |
| 479 | $this->file_num = 0; |
| 480 | $this->offset = 0; |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 481 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 483 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | } |