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 |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, 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 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | * @param mixed the directory name. Can be string or array |
| 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 | 6c308b7 | 2012-02-02 22:03:53 +0200 | [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 | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 130 | * @param string path to file |
| 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 |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 136 | $date = file_exists($dir) ? 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 | 0ea3929 | 2011-12-25 18:48:46 +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 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | * @param string the directory name |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 150 | * @param int |
| 151 | * @param int |
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 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | * @param mixed |
| 203 | * @param string |
| 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 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | * @param string the file name/path |
| 229 | * @param string the data to be encoded |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 230 | * @param int |
| 231 | * @param int |
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 | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 281 | * @param string |
| 282 | * @param bool |
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 | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 285 | public function read_file($path, $preserve_filepath = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | { |
| 287 | if ( ! file_exists($path)) |
| 288 | { |
| 289 | return FALSE; |
| 290 | } |
| 291 | |
| 292 | if (FALSE !== ($data = file_get_contents($path))) |
| 293 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 294 | $name = str_replace('\\', '/', $path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 295 | if ($preserve_filepath === FALSE) |
| 296 | { |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 297 | $name = preg_replace('|.*/(.+)|', '\\1', $name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | $this->add_data($name, $data); |
| 301 | return TRUE; |
| 302 | } |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 303 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | return FALSE; |
| 305 | } |
| 306 | |
| 307 | // ------------------------------------------------------------------------ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 308 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | /** |
| 310 | * Read a directory and add it to the zip. |
| 311 | * |
| 312 | * This function recursively reads a folder and everything it contains (including |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 313 | * sub-folders) and creates a zip based on it. Whatever directory structure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | * is in the original file path will be recreated in the zip file. |
| 315 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | * @param string path to source |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 317 | * @param bool |
| 318 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | * @return bool |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 320 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 321 | public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL) |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 322 | { |
tschechniker | c8175be | 2012-04-04 14:47:30 +0300 | [diff] [blame] | 323 | $path = rtrim($path, '/\\').DIRECTORY_SEPARATOR; |
Derek Jones | 2735b3e | 2010-05-11 08:58:21 -0500 | [diff] [blame] | 324 | if ( ! $fp = @opendir($path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 326 | return FALSE; |
| 327 | } |
| 328 | |
| 329 | // Set the original directory root for child dir's to use as relative |
| 330 | if ($root_path === NULL) |
| 331 | { |
tschechniker | c8175be | 2012-04-04 14:47:30 +0300 | [diff] [blame] | 332 | $root_path = dirname($path).DIRECTORY_SEPARATOR; |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | while (FALSE !== ($file = readdir($fp))) |
| 336 | { |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 337 | if ($file[0] === '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | { |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 339 | continue; |
| 340 | } |
| 341 | |
| 342 | if (@is_dir($path.$file)) |
| 343 | { |
tschechniker | c8175be | 2012-04-04 14:47:30 +0300 | [diff] [blame] | 344 | $this->read_dir($path.$file.DIRECTORY_SEPARATOR, $preserve_filepath, $root_path); |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 345 | } |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 346 | elseif (FALSE !== ($data = file_get_contents($path.$file))) |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 347 | { |
Tobias Tschech | 9664cc9 | 2012-04-04 15:22:33 +0300 | [diff] [blame] | 348 | $name = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $path); |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 349 | if ($preserve_filepath === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | { |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 351 | $name = str_replace($root_path, '', $name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | } |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 353 | $this->add_data($name.$file, $data); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | } |
Derek Jones | 2735b3e | 2010-05-11 08:58:21 -0500 | [diff] [blame] | 356 | |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 357 | closedir($fp); |
Phil Sturgeon | 26872de | 2010-05-11 11:41:59 +0100 | [diff] [blame] | 358 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // -------------------------------------------------------------------- |
| 362 | |
| 363 | /** |
| 364 | * Get the Zip file |
| 365 | * |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 366 | * @return string (binary encoded) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 367 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 368 | public function get_zip() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | { |
| 370 | // Is there any data to return? |
Andrey Andreev | cfbd15b | 2012-01-08 06:41:41 +0200 | [diff] [blame] | 371 | if ($this->entries === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | { |
| 373 | return FALSE; |
| 374 | } |
| 375 | |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 376 | return $this->zipdata |
Andrey Andreev | 6c308b7 | 2012-02-02 22:03:53 +0200 | [diff] [blame] | 377 | .$this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00" |
| 378 | .pack('v', $this->entries) // total # of entries "on this disk" |
| 379 | .pack('v', $this->entries) // total # of entries overall |
| 380 | .pack('V', strlen($this->directory)) // size of central dir |
| 381 | .pack('V', strlen($this->zipdata)) // offset to start of central dir |
| 382 | ."\x00\x00"; // .zip file comment length |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 384 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | // -------------------------------------------------------------------- |
| 386 | |
| 387 | /** |
| 388 | * Write File to the specified directory |
| 389 | * |
| 390 | * Lets you write a file |
| 391 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | * @param string the file name |
| 393 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 394 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 395 | public function archive($filepath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | { |
| 397 | if ( ! ($fp = @fopen($filepath, FOPEN_WRITE_CREATE_DESTRUCTIVE))) |
| 398 | { |
| 399 | return FALSE; |
| 400 | } |
| 401 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 402 | flock($fp, LOCK_EX); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | fwrite($fp, $this->get_zip()); |
| 404 | flock($fp, LOCK_UN); |
| 405 | fclose($fp); |
| 406 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 407 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | // -------------------------------------------------------------------- |
| 411 | |
| 412 | /** |
| 413 | * Download |
| 414 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | * @param string the file name |
Andrey Andreev | 0b5a486 | 2012-02-29 23:44:00 +0200 | [diff] [blame] | 416 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 418 | public function download($filename = 'backup.zip') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | { |
Andrey Andreev | e34f1a7 | 2012-01-10 22:41:52 +0200 | [diff] [blame] | 420 | if ( ! preg_match('|.+?\.zip$|', $filename)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | { |
| 422 | $filename .= '.zip'; |
| 423 | } |
| 424 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | $CI =& get_instance(); |
| 426 | $CI->load->helper('download'); |
Derek Allard | 8dc2c7c | 2009-12-07 16:07:15 +0000 | [diff] [blame] | 427 | $get_zip = $this->get_zip(); |
Derek Allard | 8dc2c7c | 2009-12-07 16:07:15 +0000 | [diff] [blame] | 428 | $zip_content =& $get_zip; |
| 429 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | force_download($filename, $zip_content); |
| 431 | } |
| 432 | |
| 433 | // -------------------------------------------------------------------- |
| 434 | |
| 435 | /** |
| 436 | * Initialize Data |
| 437 | * |
Andrey Andreev | 16d8066 | 2012-01-20 13:26:49 +0200 | [diff] [blame] | 438 | * Lets you clear current zip data. Useful if you need to create |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 439 | * multiple zips with different data. |
| 440 | * |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame^] | 441 | * @return CI_Zip |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 442 | */ |
Andrey Andreev | 0ea3929 | 2011-12-25 18:48:46 +0200 | [diff] [blame] | 443 | public function clear_data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | { |
| 445 | $this->zipdata = ''; |
| 446 | $this->directory = ''; |
| 447 | $this->entries = 0; |
| 448 | $this->file_num = 0; |
| 449 | $this->offset = 0; |
Andrey Andreev | b9535c8 | 2011-12-26 16:21:17 +0200 | [diff] [blame] | 450 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 452 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* End of file Zip.php */ |
Andrey Andreev | 0336dc2 | 2012-03-20 15:34:28 +0200 | [diff] [blame] | 456 | /* Location: ./system/libraries/Zip.php */ |