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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
Andrey Andreev | 1841e6b | 2012-01-24 15:30:01 +0200 | [diff] [blame] | 12 | * bundled with this package in the files license.txt / license.rst. It is |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Zip Compression Class |
| 31 | * |
| 32 | * This class is based on a library I found at Zend: |
| 33 | * http://www.zend.com/codex.php?id=696&single=1 |
| 34 | * |
| 35 | * The original library is a little rough around the edges so I |
| 36 | * refactored it and added several additional methods -- Rick Ellis |
| 37 | * |
| 38 | * @package CodeIgniter |
| 39 | * @subpackage Libraries |
| 40 | * @category Encryption |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 41 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | * @link http://codeigniter.com/user_guide/libraries/zip.html |
| 43 | */ |
vkeranov | 95b2f4f | 2012-06-16 20:37:11 +0300 | [diff] [blame] | 44 | class CI_Zip { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 46 | /** |
| 47 | * Zip data in string form |
| 48 | * |
| 49 | * @var string |
| 50 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 51 | public $zipdata = ''; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 52 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 53 | /** |
| 54 | * Zip data for a directory in string form |
| 55 | * |
| 56 | * @var string |
| 57 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 58 | public $directory = ''; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 59 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 60 | /** |
| 61 | * Number of files/folder in zip file |
| 62 | * |
| 63 | * @var int |
| 64 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 65 | public $entries = 0; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 66 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 67 | /** |
| 68 | * Number of files in zip |
| 69 | * |
| 70 | * @var int |
| 71 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 72 | public $file_num = 0; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 73 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 74 | /** |
| 75 | * relative offset of local header |
| 76 | * |
| 77 | * @var int |
| 78 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 79 | public $offset = 0; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 80 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 81 | /** |
| 82 | * Reference to time at init |
| 83 | * |
| 84 | * @var int |
| 85 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 86 | public $now; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 88 | /** |
| 89 | * Initialize zip compression class |
| 90 | * |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 91 | * @return void |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 92 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 93 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | { |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 95 | $this->now = time(); |
Andrey Andreev | bb2e518 | 2012-03-26 13:58:12 +0300 | [diff] [blame] | 96 | log_message('debug', 'Zip Compression Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // -------------------------------------------------------------------- |
| 100 | |
| 101 | /** |
| 102 | * Add Directory |
| 103 | * |
| 104 | * Lets you add a virtual directory into which you can place files. |
| 105 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 106 | * @param mixed $directory the directory name. Can be string or array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | * @return void |
| 108 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 109 | public function add_dir($directory) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | { |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 111 | foreach ((array) $directory as $dir) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 113 | if ( ! preg_match('|.+/$|', $dir)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | { |
| 115 | $dir .= '/'; |
| 116 | } |
| 117 | |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 118 | $dir_time = $this->_get_mod_time($dir); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 119 | $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 123 | // -------------------------------------------------------------------- |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 124 | |
| 125 | /** |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 126 | * Get file/directory modification time |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 127 | * |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 128 | * 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] | 129 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 130 | * @param string $dir path to file |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 131 | * @return array filemtime/filemdate |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 132 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 133 | protected function _get_mod_time($dir) |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 134 | { |
Thomas Traub | 98f85b1 | 2011-12-05 14:58:29 +0100 | [diff] [blame] | 135 | // filemtime() may return false, but raises an error for non-existing files |
Ahmad Anbar | 00421bf | 2014-03-14 16:49:47 +0200 | [diff] [blame^] | 136 | $date = file_exists($dir) ? getdate(filemtime($dir)) : getdate($this->now); |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 137 | |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 138 | return array( |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame] | 139 | 'file_mtime' => ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2, |
| 140 | 'file_mdate' => (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday'] |
| 141 | ); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 142 | } |
| 143 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | // -------------------------------------------------------------------- |
| 145 | |
| 146 | /** |
| 147 | * Add Directory |
| 148 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 149 | * @param string $dir the directory name |
| 150 | * @param int $file_mtime |
| 151 | * @param int $file_mdate |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | * @return void |
| 153 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 154 | protected function _add_dir($dir, $file_mtime, $file_mdate) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | { |
Andrey Andreev | e34f1a7 | 2012-01-10 22:41:52 +0200 | [diff] [blame] | 156 | $dir = str_replace('\\', '/', $dir); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | |
| 158 | $this->zipdata .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 159 | "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00" |
| 160 | .pack('v', $file_mtime) |
| 161 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | .pack('V', 0) // crc32 |
| 163 | .pack('V', 0) // compressed filesize |
| 164 | .pack('V', 0) // uncompressed filesize |
| 165 | .pack('v', strlen($dir)) // length of pathname |
| 166 | .pack('v', 0) // extra field length |
| 167 | .$dir |
| 168 | // below is "data descriptor" segment |
| 169 | .pack('V', 0) // crc32 |
| 170 | .pack('V', 0) // compressed filesize |
| 171 | .pack('V', 0); // uncompressed filesize |
| 172 | |
| 173 | $this->directory .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 174 | "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00" |
| 175 | .pack('v', $file_mtime) |
| 176 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | .pack('V',0) // crc32 |
| 178 | .pack('V',0) // compressed filesize |
| 179 | .pack('V',0) // uncompressed filesize |
| 180 | .pack('v', strlen($dir)) // length of pathname |
| 181 | .pack('v', 0) // extra field length |
| 182 | .pack('v', 0) // file comment length |
| 183 | .pack('v', 0) // disk number start |
| 184 | .pack('v', 0) // internal file attributes |
| 185 | .pack('V', 16) // external file attributes - 'directory' bit set |
| 186 | .pack('V', $this->offset) // relative offset of local header |
| 187 | .$dir; |
| 188 | |
| 189 | $this->offset = strlen($this->zipdata); |
| 190 | $this->entries++; |
| 191 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 192 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | // -------------------------------------------------------------------- |
| 194 | |
| 195 | /** |
| 196 | * Add Data to Zip |
| 197 | * |
| 198 | * Lets you add files to the archive. If the path is included |
Andrey Andreev | 0b5a486 | 2012-02-29 23:44:00 +0200 | [diff] [blame] | 199 | * in the filename it will be placed within a directory. Make |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | * sure you use add_dir() first to create the folder. |
| 201 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 202 | * @param mixed $filepath A single filepath or an array of file => data pairs |
| 203 | * @param string $data Single file contents |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 206 | public function add_data($filepath, $data = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 207 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | if (is_array($filepath)) |
| 209 | { |
| 210 | foreach ($filepath as $path => $data) |
| 211 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 212 | $file_data = $this->_get_mod_time($path); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 213 | $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] | 214 | } |
| 215 | } |
| 216 | else |
| 217 | { |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 218 | $file_data = $this->_get_mod_time($filepath); |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 219 | $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] | 220 | } |
| 221 | } |
| 222 | |
| 223 | // -------------------------------------------------------------------- |
| 224 | |
| 225 | /** |
| 226 | * Add Data to Zip |
| 227 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 228 | * @param string $filepath the file name/path |
| 229 | * @param string $data the data to be encoded |
| 230 | * @param int $file_mtime |
| 231 | * @param int $file_mdate |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 233 | */ |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 234 | protected function _add_data($filepath, $data, $file_mtime, $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 236 | $filepath = str_replace('\\', '/', $filepath); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | |
| 238 | $uncompressed_size = strlen($data); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 239 | $crc32 = crc32($data); |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 240 | $gzdata = substr(gzcompress($data), 2, -4); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 241 | $compressed_size = strlen($gzdata); |
| 242 | |
| 243 | $this->zipdata .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 244 | "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00" |
| 245 | .pack('v', $file_mtime) |
| 246 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | .pack('V', $crc32) |
| 248 | .pack('V', $compressed_size) |
| 249 | .pack('V', $uncompressed_size) |
| 250 | .pack('v', strlen($filepath)) // length of filename |
| 251 | .pack('v', 0) // extra field length |
| 252 | .$filepath |
| 253 | .$gzdata; // "file data" segment |
| 254 | |
| 255 | $this->directory .= |
Greg Aker | 5ed19b4 | 2010-03-19 12:13:14 -0500 | [diff] [blame] | 256 | "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00" |
| 257 | .pack('v', $file_mtime) |
| 258 | .pack('v', $file_mdate) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | .pack('V', $crc32) |
| 260 | .pack('V', $compressed_size) |
| 261 | .pack('V', $uncompressed_size) |
| 262 | .pack('v', strlen($filepath)) // length of filename |
| 263 | .pack('v', 0) // extra field length |
| 264 | .pack('v', 0) // file comment length |
| 265 | .pack('v', 0) // disk number start |
| 266 | .pack('v', 0) // internal file attributes |
| 267 | .pack('V', 32) // external file attributes - 'archive' bit set |
| 268 | .pack('V', $this->offset) // relative offset of local header |
| 269 | .$filepath; |
| 270 | |
| 271 | $this->offset = strlen($this->zipdata); |
| 272 | $this->entries++; |
| 273 | $this->file_num++; |
| 274 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 275 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | // -------------------------------------------------------------------- |
| 277 | |
| 278 | /** |
| 279 | * Read the contents of a file and add it to the zip |
| 280 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 281 | * @param string $path |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 282 | * @param bool $archive_filepath |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | */ |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 285 | public function read_file($path, $archive_filepath = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | { |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 287 | if (file_exists($path) && FALSE !== ($data = file_get_contents($path))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | { |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 289 | if (is_string($archive_filepath)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | { |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 291 | $name = str_replace('\\', '/', $archive_filepath); |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | $name = str_replace('\\', '/', $path); |
| 296 | |
Andrey Andreev | aa9a4f7 | 2014-01-28 12:05:51 +0200 | [diff] [blame] | 297 | if ($archive_filepath === FALSE) |
Andrey Andreev | a20ec97 | 2014-01-07 15:23:10 +0200 | [diff] [blame] | 298 | { |
| 299 | $name = preg_replace('|.*/(.+)|', '\\1', $name); |
| 300 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | $this->add_data($name, $data); |
| 304 | return TRUE; |
| 305 | } |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 306 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | return FALSE; |
| 308 | } |
| 309 | |
| 310 | // ------------------------------------------------------------------------ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 311 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | /** |
| 313 | * Read a directory and add it to the zip. |
| 314 | * |
| 315 | * This function recursively reads a folder and everything it contains (including |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 316 | * sub-folders) and creates a zip based on it. Whatever directory structure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 317 | * is in the original file path will be recreated in the zip file. |
| 318 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 319 | * @param string $path path to source directory |
| 320 | * @param bool $preserve_filepath |
| 321 | * @param string $root_path |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | * @return bool |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 323 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 324 | public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL) |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 325 | { |
tschechniker | c8175be | 2012-04-04 14:47:30 +0300 | [diff] [blame] | 326 | $path = rtrim($path, '/\\').DIRECTORY_SEPARATOR; |
Derek Jones | 2735b3e | 2010-05-11 08:58:21 -0500 | [diff] [blame] | 327 | if ( ! $fp = @opendir($path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 329 | return FALSE; |
| 330 | } |
| 331 | |
| 332 | // Set the original directory root for child dir's to use as relative |
| 333 | if ($root_path === NULL) |
| 334 | { |
tschechniker | c8175be | 2012-04-04 14:47:30 +0300 | [diff] [blame] | 335 | $root_path = dirname($path).DIRECTORY_SEPARATOR; |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | while (FALSE !== ($file = readdir($fp))) |
| 339 | { |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 340 | if ($file[0] === '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 342 | continue; |
| 343 | } |
| 344 | |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame] | 345 | if (is_dir($path.$file)) |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 346 | { |
tschechniker | c8175be | 2012-04-04 14:47:30 +0300 | [diff] [blame] | 347 | $this->read_dir($path.$file.DIRECTORY_SEPARATOR, $preserve_filepath, $root_path); |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 348 | } |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 349 | elseif (FALSE !== ($data = file_get_contents($path.$file))) |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 350 | { |
Tobias Tschech | 9664cc9 | 2012-04-04 15:22:33 +0300 | [diff] [blame] | 351 | $name = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $path); |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 352 | if ($preserve_filepath === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | { |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 354 | $name = str_replace($root_path, '', $name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | } |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame] | 356 | |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 357 | $this->add_data($name.$file, $data); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | } |
Derek Jones | 2735b3e | 2010-05-11 08:58:21 -0500 | [diff] [blame] | 360 | |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 361 | closedir($fp); |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 362 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | // -------------------------------------------------------------------- |
| 366 | |
| 367 | /** |
| 368 | * Get the Zip file |
| 369 | * |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 370 | * @return string (binary encoded) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 371 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 372 | public function get_zip() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 373 | { |
| 374 | // Is there any data to return? |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 375 | if ($this->entries === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | { |
| 377 | return FALSE; |
| 378 | } |
| 379 | |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 380 | return $this->zipdata |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 381 | .$this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00" |
| 382 | .pack('v', $this->entries) // total # of entries "on this disk" |
| 383 | .pack('v', $this->entries) // total # of entries overall |
| 384 | .pack('V', strlen($this->directory)) // size of central dir |
| 385 | .pack('V', strlen($this->zipdata)) // offset to start of central dir |
| 386 | ."\x00\x00"; // .zip file comment length |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 388 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | // -------------------------------------------------------------------- |
| 390 | |
| 391 | /** |
| 392 | * Write File to the specified directory |
| 393 | * |
| 394 | * Lets you write a file |
| 395 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 396 | * @param string $filepath the file name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 397 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 398 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 399 | public function archive($filepath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | { |
Andrey Andreev | 7cf682a | 2014-03-13 14:55:45 +0200 | [diff] [blame] | 401 | if ( ! ($fp = @fopen($filepath, 'w+b'))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | { |
| 403 | return FALSE; |
| 404 | } |
| 405 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 406 | flock($fp, LOCK_EX); |
Andrey Andreev | d8b1ad3 | 2014-01-15 17:42:52 +0200 | [diff] [blame] | 407 | |
| 408 | for ($written = 0, $data = $this->get_zip(), $length = strlen($data); $written < $length; $written += $result) |
| 409 | { |
| 410 | if (($result = fwrite($fp, substr($data, $written))) === FALSE) |
| 411 | { |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | flock($fp, LOCK_UN); |
| 417 | fclose($fp); |
| 418 | |
Andrey Andreev | d8b1ad3 | 2014-01-15 17:42:52 +0200 | [diff] [blame] | 419 | return is_int($result); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | // -------------------------------------------------------------------- |
| 423 | |
| 424 | /** |
| 425 | * Download |
| 426 | * |
Andrey Andreev | 88f41df | 2013-09-23 15:24:43 +0300 | [diff] [blame] | 427 | * @param string $filename the file name |
Andrey Andreev | 0b5a486 | 2012-02-29 23:44:00 +0200 | [diff] [blame] | 428 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 430 | public function download($filename = 'backup.zip') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | { |
Andrey Andreev | e34f1a7 | 2012-01-10 22:41:52 +0200 | [diff] [blame] | 432 | if ( ! preg_match('|.+?\.zip$|', $filename)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | { |
| 434 | $filename .= '.zip'; |
| 435 | } |
| 436 | |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 437 | get_instance()->load->helper('download'); |
Derek Allard | 8dc2c7c | 2009-12-07 16:07:15 +0000 | [diff] [blame] | 438 | $get_zip = $this->get_zip(); |
Derek Allard | 8dc2c7c | 2009-12-07 16:07:15 +0000 | [diff] [blame] | 439 | $zip_content =& $get_zip; |
| 440 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | force_download($filename, $zip_content); |
| 442 | } |
| 443 | |
| 444 | // -------------------------------------------------------------------- |
| 445 | |
| 446 | /** |
| 447 | * Initialize Data |
| 448 | * |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 449 | * Lets you clear current zip data. Useful if you need to create |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 450 | * multiple zips with different data. |
| 451 | * |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 452 | * @return CI_Zip |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 453 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 454 | public function clear_data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | { |
| 456 | $this->zipdata = ''; |
| 457 | $this->directory = ''; |
| 458 | $this->entries = 0; |
| 459 | $this->file_num = 0; |
| 460 | $this->offset = 0; |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 461 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 463 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* End of file Zip.php */ |
Andrey Andreev | 0336dc2 | 2012-03-20 15:34:28 +0200 | [diff] [blame] | 467 | /* Location: ./system/libraries/Zip.php */ |