blob: c0a14023d7b910b11959f291160d17870e898d8b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev0ea39292011-12-25 18:48:46 +02008 *
Instructor, BCIT0e59db62019-01-01 08:34:36 -08009 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
Andrey Andreev0ea39292011-12-25 18:48:46 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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 Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Instructor, BCIT0e59db62019-01-01 08:34:36 -080032 * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33 * @license https://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
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 Jonesf4a4bd82011-10-20 12:18:42 -050052 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020053 * @link https://codeigniter.com/user_guide/libraries/zip.html
Derek Allard2067d1a2008-11-13 22:59:24 +000054 */
vkeranov95b2f4f2012-06-16 20:37:11 +030055class CI_Zip {
Derek Allard2067d1a2008-11-13 22:59:24 +000056
Timothy Warren86611db2012-04-27 10:06:25 -040057 /**
58 * Zip data in string form
59 *
60 * @var string
61 */
Andrey Andreevfe9309d2015-01-09 17:48:58 +020062 public $zipdata = '';
Andrey Andreev56454792012-05-17 14:32:19 +030063
Timothy Warren86611db2012-04-27 10:06:25 -040064 /**
65 * Zip data for a directory in string form
66 *
67 * @var string
68 */
Andrey Andreevfe9309d2015-01-09 17:48:58 +020069 public $directory = '';
Andrey Andreev56454792012-05-17 14:32:19 +030070
Timothy Warren86611db2012-04-27 10:06:25 -040071 /**
72 * Number of files/folder in zip file
73 *
74 * @var int
75 */
Andrey Andreevfe9309d2015-01-09 17:48:58 +020076 public $entries = 0;
Andrey Andreev56454792012-05-17 14:32:19 +030077
Timothy Warren86611db2012-04-27 10:06:25 -040078 /**
79 * Number of files in zip
80 *
81 * @var int
82 */
Andrey Andreevfe9309d2015-01-09 17:48:58 +020083 public $file_num = 0;
Andrey Andreev56454792012-05-17 14:32:19 +030084
Timothy Warren86611db2012-04-27 10:06:25 -040085 /**
86 * relative offset of local header
87 *
88 * @var int
89 */
Andrey Andreevfe9309d2015-01-09 17:48:58 +020090 public $offset = 0;
Andrey Andreev56454792012-05-17 14:32:19 +030091
Timothy Warren86611db2012-04-27 10:06:25 -040092 /**
93 * Reference to time at init
94 *
95 * @var int
96 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020097 public $now;
Andrey Andreev309d7012014-12-04 11:47:26 +020098
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 Allard2067d1a2008-11-13 22:59:24 +0000107
Timothy Warren86611db2012-04-27 10:06:25 -0400108 /**
Andrey Andreevc0c74d52017-01-19 15:26:35 +0200109 * mbstring.func_overload flag
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300110 *
111 * @var bool
112 */
Andrey Andreevc0c74d52017-01-19 15:26:35 +0200113 protected static $func_overload;
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300114
115 /**
Timothy Warren86611db2012-04-27 10:06:25 -0400116 * Initialize zip compression class
117 *
Andrey Andreev56454792012-05-17 14:32:19 +0300118 * @return void
Timothy Warren86611db2012-04-27 10:06:25 -0400119 */
Greg Akera9263282010-11-10 15:26:43 -0600120 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
Andrey Andreevc0c74d52017-01-19 15:26:35 +0200122 isset(self::$func_overload) OR self::$func_overload = (extension_loaded('mbstring') && ini_get('mbstring.func_overload'));
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300123
Greg Aker5ed19b42010-03-19 12:13:14 -0500124 $this->now = time();
Andrey Andreev90726b82015-01-20 12:39:22 +0200125 log_message('info', 'Zip Compression Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 }
127
128 // --------------------------------------------------------------------
129
130 /**
131 * Add Directory
132 *
133 * Lets you add a virtual directory into which you can place files.
134 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300135 * @param mixed $directory the directory name. Can be string or array
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 * @return void
137 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200138 public function add_dir($directory)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Andrey Andreev88f41df2013-09-23 15:24:43 +0300140 foreach ((array) $directory as $dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200142 if ( ! preg_match('|.+/$|', $dir))
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
144 $dir .= '/';
145 }
146
Greg Aker5ed19b42010-03-19 12:13:14 -0500147 $dir_time = $this->_get_mod_time($dir);
Greg Aker5ed19b42010-03-19 12:13:14 -0500148 $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
150 }
151
Barry Mienydd671972010-10-04 16:33:58 +0200152 // --------------------------------------------------------------------
Greg Aker5ed19b42010-03-19 12:13:14 -0500153
154 /**
Andrey Andreev6c308b72012-02-02 22:03:53 +0200155 * Get file/directory modification time
Barry Mienydd671972010-10-04 16:33:58 +0200156 *
Andrey Andreev6c308b72012-02-02 22:03:53 +0200157 * If this is a newly created file/dir, we will set the time to 'now'
Greg Aker5ed19b42010-03-19 12:13:14 -0500158 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300159 * @param string $dir path to file
Andrey Andreev6c308b72012-02-02 22:03:53 +0200160 * @return array filemtime/filemdate
Greg Aker5ed19b42010-03-19 12:13:14 -0500161 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200162 protected function _get_mod_time($dir)
Greg Aker5ed19b42010-03-19 12:13:14 -0500163 {
Thomas Traub98f85b12011-12-05 14:58:29 +0100164 // filemtime() may return false, but raises an error for non-existing files
Ahmad Anbar00421bf2014-03-14 16:49:47 +0200165 $date = file_exists($dir) ? getdate(filemtime($dir)) : getdate($this->now);
Andrey Andreev0ea39292011-12-25 18:48:46 +0200166
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200167 return array(
Andrey Andreev382b5132014-02-26 18:41:59 +0200168 '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 Aker5ed19b42010-03-19 12:13:14 -0500171 }
172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 // --------------------------------------------------------------------
174
175 /**
176 * Add Directory
177 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300178 * @param string $dir the directory name
179 * @param int $file_mtime
180 * @param int $file_mdate
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 * @return void
182 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200183 protected function _add_dir($dir, $file_mtime, $file_mdate)
Barry Mienydd671972010-10-04 16:33:58 +0200184 {
Andrey Andreeve34f1a72012-01-10 22:41:52 +0200185 $dir = str_replace('\\', '/', $dir);
Derek Allard2067d1a2008-11-13 22:59:24 +0000186
187 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500188 "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00"
189 .pack('v', $file_mtime)
190 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 .pack('V', 0) // crc32
192 .pack('V', 0) // compressed filesize
193 .pack('V', 0) // uncompressed filesize
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300194 .pack('v', self::strlen($dir)) // length of pathname
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 .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 Aker5ed19b42010-03-19 12:13:14 -0500203 "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00"
204 .pack('v', $file_mtime)
205 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 .pack('V',0) // crc32
207 .pack('V',0) // compressed filesize
208 .pack('V',0) // uncompressed filesize
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300209 .pack('v', self::strlen($dir)) // length of pathname
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 .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 Andreev4e2cdec2016-10-28 14:19:08 +0300218 $this->offset = self::strlen($this->zipdata);
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 $this->entries++;
220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 // --------------------------------------------------------------------
223
224 /**
225 * Add Data to Zip
226 *
227 * Lets you add files to the archive. If the path is included
Andrey Andreev0b5a4862012-02-29 23:44:00 +0200228 * in the filename it will be placed within a directory. Make
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 * sure you use add_dir() first to create the folder.
230 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300231 * @param mixed $filepath A single filepath or an array of file => data pairs
232 * @param string $data Single file contents
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200234 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200235 public function add_data($filepath, $data = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200236 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 if (is_array($filepath))
238 {
239 foreach ($filepath as $path => $data)
240 {
Barry Mienydd671972010-10-04 16:33:58 +0200241 $file_data = $this->_get_mod_time($path);
Greg Aker5ed19b42010-03-19 12:13:14 -0500242 $this->_add_data($path, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
244 }
245 else
246 {
Greg Aker5ed19b42010-03-19 12:13:14 -0500247 $file_data = $this->_get_mod_time($filepath);
Greg Aker5ed19b42010-03-19 12:13:14 -0500248 $this->_add_data($filepath, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 }
250 }
251
252 // --------------------------------------------------------------------
253
254 /**
255 * Add Data to Zip
256 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300257 * @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 Allard2067d1a2008-11-13 22:59:24 +0000261 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200262 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200263 protected function _add_data($filepath, $data, $file_mtime, $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200265 $filepath = str_replace('\\', '/', $filepath);
Derek Allard2067d1a2008-11-13 22:59:24 +0000266
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300267 $uncompressed_size = self::strlen($data);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500268 $crc32 = crc32($data);
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300269 $gzdata = self::substr(gzcompress($data, $this->compression_level), 2, -4);
270 $compressed_size = self::strlen($gzdata);
Derek Allard2067d1a2008-11-13 22:59:24 +0000271
272 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500273 "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00"
274 .pack('v', $file_mtime)
275 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 .pack('V', $crc32)
277 .pack('V', $compressed_size)
278 .pack('V', $uncompressed_size)
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300279 .pack('v', self::strlen($filepath)) // length of filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 .pack('v', 0) // extra field length
281 .$filepath
282 .$gzdata; // "file data" segment
283
284 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500285 "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00"
286 .pack('v', $file_mtime)
287 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 .pack('V', $crc32)
289 .pack('V', $compressed_size)
290 .pack('V', $uncompressed_size)
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300291 .pack('v', self::strlen($filepath)) // length of filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 .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 Andreev4e2cdec2016-10-28 14:19:08 +0300300 $this->offset = self::strlen($this->zipdata);
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 $this->entries++;
302 $this->file_num++;
303 }
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 // --------------------------------------------------------------------
306
307 /**
308 * Read the contents of a file and add it to the zip
309 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300310 * @param string $path
Andrey Andreeva20ec972014-01-07 15:23:10 +0200311 * @param bool $archive_filepath
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200313 */
Andrey Andreeva20ec972014-01-07 15:23:10 +0200314 public function read_file($path, $archive_filepath = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200316 if (file_exists($path) && FALSE !== ($data = file_get_contents($path)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200318 if (is_string($archive_filepath))
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
Andrey Andreeva20ec972014-01-07 15:23:10 +0200320 $name = str_replace('\\', '/', $archive_filepath);
321 }
322 else
323 {
324 $name = str_replace('\\', '/', $path);
325
Andrey Andreevaa9a4f72014-01-28 12:05:51 +0200326 if ($archive_filepath === FALSE)
Andrey Andreeva20ec972014-01-07 15:23:10 +0200327 {
328 $name = preg_replace('|.*/(.+)|', '\\1', $name);
329 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
331
332 $this->add_data($name, $data);
333 return TRUE;
334 }
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 return FALSE;
337 }
338
339 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 /**
342 * Read a directory and add it to the zip.
343 *
344 * This function recursively reads a folder and everything it contains (including
Andrey Andreev16d80662012-01-20 13:26:49 +0200345 * sub-folders) and creates a zip based on it. Whatever directory structure
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * is in the original file path will be recreated in the zip file.
347 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300348 * @param string $path path to source directory
349 * @param bool $preserve_filepath
350 * @param string $root_path
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 * @return bool
Phil Sturgeon26872de2010-05-11 11:41:59 +0100352 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200353 public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
Phil Sturgeon26872de2010-05-11 11:41:59 +0100354 {
tschechnikerc8175be2012-04-04 14:47:30 +0300355 $path = rtrim($path, '/\\').DIRECTORY_SEPARATOR;
Derek Jones2735b3e2010-05-11 08:58:21 -0500356 if ( ! $fp = @opendir($path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100358 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 Gaddb7a8fbb2015-04-03 17:37:44 -0600364 $root_path = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, dirname($path)).DIRECTORY_SEPARATOR;
Phil Sturgeon26872de2010-05-11 11:41:59 +0100365 }
366
367 while (FALSE !== ($file = readdir($fp)))
368 {
Andrey Andreev0ea39292011-12-25 18:48:46 +0200369 if ($file[0] === '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100371 continue;
372 }
373
Andrey Andreev382b5132014-02-26 18:41:59 +0200374 if (is_dir($path.$file))
Phil Sturgeon26872de2010-05-11 11:41:59 +0100375 {
tschechnikerc8175be2012-04-04 14:47:30 +0300376 $this->read_dir($path.$file.DIRECTORY_SEPARATOR, $preserve_filepath, $root_path);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100377 }
Andrey Andreev16d80662012-01-20 13:26:49 +0200378 elseif (FALSE !== ($data = file_get_contents($path.$file)))
Phil Sturgeon26872de2010-05-11 11:41:59 +0100379 {
Tobias Tschech9664cc92012-04-04 15:22:33 +0300380 $name = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $path);
Andrey Andreev16d80662012-01-20 13:26:49 +0200381 if ($preserve_filepath === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
Andrey Andreev16d80662012-01-20 13:26:49 +0200383 $name = str_replace($root_path, '', $name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 }
Andrey Andreev382b5132014-02-26 18:41:59 +0200385
Andrey Andreev16d80662012-01-20 13:26:49 +0200386 $this->add_data($name.$file, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 }
Derek Jones2735b3e2010-05-11 08:58:21 -0500389
Andrey Andreev6c308b72012-02-02 22:03:53 +0200390 closedir($fp);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100391 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
393
394 // --------------------------------------------------------------------
395
396 /**
397 * Get the Zip file
398 *
Andrey Andreev6c308b72012-02-02 22:03:53 +0200399 * @return string (binary encoded)
Barry Mienydd671972010-10-04 16:33:58 +0200400 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200401 public function get_zip()
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
403 // Is there any data to return?
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200404 if ($this->entries === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
406 return FALSE;
407 }
408
Andrey Andreev0ea39292011-12-25 18:48:46 +0200409 return $this->zipdata
Andrey Andreev6c308b72012-02-02 22:03:53 +0200410 .$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 Andreev4e2cdec2016-10-28 14:19:08 +0300413 .pack('V', self::strlen($this->directory)) // size of central dir
414 .pack('V', self::strlen($this->zipdata)) // offset to start of central dir
Andrey Andreev6c308b72012-02-02 22:03:53 +0200415 ."\x00\x00"; // .zip file comment length
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // --------------------------------------------------------------------
419
420 /**
421 * Write File to the specified directory
422 *
423 * Lets you write a file
424 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300425 * @param string $filepath the file name
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200427 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200428 public function archive($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
Andrey Andreev7cf682a2014-03-13 14:55:45 +0200430 if ( ! ($fp = @fopen($filepath, 'w+b')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
432 return FALSE;
433 }
434
Barry Mienydd671972010-10-04 16:33:58 +0200435 flock($fp, LOCK_EX);
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200436
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300437 for ($result = $written = 0, $data = $this->get_zip(), $length = self::strlen($data); $written < $length; $written += $result)
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200438 {
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300439 if (($result = fwrite($fp, self::substr($data, $written))) === FALSE)
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200440 {
441 break;
442 }
443 }
444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 flock($fp, LOCK_UN);
446 fclose($fp);
447
Andrey Andreevd8b1ad32014-01-15 17:42:52 +0200448 return is_int($result);
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450
451 // --------------------------------------------------------------------
452
453 /**
454 * Download
455 *
Andrey Andreev88f41df2013-09-23 15:24:43 +0300456 * @param string $filename the file name
Andrey Andreev0b5a4862012-02-29 23:44:00 +0200457 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200459 public function download($filename = 'backup.zip')
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
Andrey Andreeve34f1a72012-01-10 22:41:52 +0200461 if ( ! preg_match('|.+?\.zip$|', $filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
463 $filename .= '.zip';
464 }
465
Andrey Andreev119d8a72014-01-08 15:27:53 +0200466 get_instance()->load->helper('download');
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000467 $get_zip = $this->get_zip();
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000468 $zip_content =& $get_zip;
469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 force_download($filename, $zip_content);
471 }
472
473 // --------------------------------------------------------------------
474
475 /**
476 * Initialize Data
477 *
Andrey Andreev16d80662012-01-20 13:26:49 +0200478 * Lets you clear current zip data. Useful if you need to create
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 * multiple zips with different data.
480 *
Andrew Podner4296a652012-12-17 07:51:15 -0500481 * @return CI_Zip
Barry Mienydd671972010-10-04 16:33:58 +0200482 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200483 public function clear_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
Andrey Andreevfe9309d2015-01-09 17:48:58 +0200485 $this->zipdata = '';
486 $this->directory = '';
487 $this->entries = 0;
488 $this->file_num = 0;
489 $this->offset = 0;
Andrey Andreevb9535c82011-12-26 16:21:17 +0200490 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
Barry Mienydd671972010-10-04 16:33:58 +0200492
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300493 // --------------------------------------------------------------------
494
495 /**
496 * Byte-safe strlen()
497 *
498 * @param string $str
499 * @return int
500 */
501 protected static function strlen($str)
502 {
Andrey Andreevc0c74d52017-01-19 15:26:35 +0200503 return (self::$func_overload)
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300504 ? 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 Andreevc0c74d52017-01-19 15:26:35 +0200520 if (self::$func_overload)
Andrey Andreev4e2cdec2016-10-28 14:19:08 +0300521 {
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 Allard2067d1a2008-11-13 22:59:24 +0000532}