blob: f2f5f2e5dd2e8eadcc61bebe5d5f8a2e943cc5b1 [file] [log] [blame]
Andrey Andreev0ea39292011-12-25 18:48:46 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev0ea39292011-12-25 18:48:46 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev0ea39292011-12-25 18:48:46 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
Andrey Andreev1841e6b2012-01-24 15:30:01 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Zip Compression Class
30 *
31 * This class is based on a library I found at Zend:
32 * http://www.zend.com/codex.php?id=696&single=1
33 *
34 * The original library is a little rough around the edges so I
35 * refactored it and added several additional methods -- Rick Ellis
36 *
37 * @package CodeIgniter
38 * @subpackage Libraries
39 * @category Encryption
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/libraries/zip.html
42 */
Derek Jones37f4b9c2011-07-01 17:56:50 -050043class CI_Zip {
Derek Allard2067d1a2008-11-13 22:59:24 +000044
Andrey Andreev0ea39292011-12-25 18:48:46 +020045 public $zipdata = '';
46 public $directory = '';
47 public $entries = 0;
48 public $file_num = 0;
49 public $offset = 0;
50 public $now;
Derek Allard2067d1a2008-11-13 22:59:24 +000051
Greg Akera9263282010-11-10 15:26:43 -060052 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000053 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +020054 log_message('debug', 'Zip Compression Class Initialized');
Greg Aker5ed19b42010-03-19 12:13:14 -050055 $this->now = time();
Derek Allard2067d1a2008-11-13 22:59:24 +000056 }
57
58 // --------------------------------------------------------------------
59
60 /**
61 * Add Directory
62 *
63 * Lets you add a virtual directory into which you can place files.
64 *
Derek Allard2067d1a2008-11-13 22:59:24 +000065 * @param mixed the directory name. Can be string or array
66 * @return void
67 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020068 public function add_dir($directory)
Derek Allard2067d1a2008-11-13 22:59:24 +000069 {
Andrey Andreev6c308b72012-02-02 22:03:53 +020070 foreach ( (array) $directory as $dir)
Derek Allard2067d1a2008-11-13 22:59:24 +000071 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +020072 if ( ! preg_match('|.+/$|', $dir))
Derek Allard2067d1a2008-11-13 22:59:24 +000073 {
74 $dir .= '/';
75 }
76
Greg Aker5ed19b42010-03-19 12:13:14 -050077 $dir_time = $this->_get_mod_time($dir);
Greg Aker5ed19b42010-03-19 12:13:14 -050078 $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +000079 }
80 }
81
Barry Mienydd671972010-10-04 16:33:58 +020082 // --------------------------------------------------------------------
Greg Aker5ed19b42010-03-19 12:13:14 -050083
84 /**
Andrey Andreev6c308b72012-02-02 22:03:53 +020085 * Get file/directory modification time
Barry Mienydd671972010-10-04 16:33:58 +020086 *
Andrey Andreev6c308b72012-02-02 22:03:53 +020087 * If this is a newly created file/dir, we will set the time to 'now'
Greg Aker5ed19b42010-03-19 12:13:14 -050088 *
Andrey Andreev6c308b72012-02-02 22:03:53 +020089 * @param string path to file
90 * @return array filemtime/filemdate
Greg Aker5ed19b42010-03-19 12:13:14 -050091 */
Andrey Andreevb9535c82011-12-26 16:21:17 +020092 protected function _get_mod_time($dir)
Greg Aker5ed19b42010-03-19 12:13:14 -050093 {
Thomas Traub98f85b12011-12-05 14:58:29 +010094 // filemtime() may return false, but raises an error for non-existing files
Andrey Andreev6c308b72012-02-02 22:03:53 +020095 $date = file_exists($dir) ? filemtime($dir) : getdate($this->now);
Andrey Andreev0ea39292011-12-25 18:48:46 +020096
Andrey Andreevcfbd15b2012-01-08 06:41:41 +020097 return array(
Andrey Andreev0ea39292011-12-25 18:48:46 +020098 'file_mtime' => ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2,
99 'file_mdate' => (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday']
100 );
Greg Aker5ed19b42010-03-19 12:13:14 -0500101 }
102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 // --------------------------------------------------------------------
104
105 /**
106 * Add Directory
107 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 * @param string the directory name
Andrey Andreev6c308b72012-02-02 22:03:53 +0200109 * @param int
110 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 * @return void
112 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200113 protected function _add_dir($dir, $file_mtime, $file_mdate)
Barry Mienydd671972010-10-04 16:33:58 +0200114 {
Andrey Andreeve34f1a72012-01-10 22:41:52 +0200115 $dir = str_replace('\\', '/', $dir);
Derek Allard2067d1a2008-11-13 22:59:24 +0000116
117 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500118 "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00"
119 .pack('v', $file_mtime)
120 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 .pack('V', 0) // crc32
122 .pack('V', 0) // compressed filesize
123 .pack('V', 0) // uncompressed filesize
124 .pack('v', strlen($dir)) // length of pathname
125 .pack('v', 0) // extra field length
126 .$dir
127 // below is "data descriptor" segment
128 .pack('V', 0) // crc32
129 .pack('V', 0) // compressed filesize
130 .pack('V', 0); // uncompressed filesize
131
132 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500133 "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00"
134 .pack('v', $file_mtime)
135 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 .pack('V',0) // crc32
137 .pack('V',0) // compressed filesize
138 .pack('V',0) // uncompressed filesize
139 .pack('v', strlen($dir)) // length of pathname
140 .pack('v', 0) // extra field length
141 .pack('v', 0) // file comment length
142 .pack('v', 0) // disk number start
143 .pack('v', 0) // internal file attributes
144 .pack('V', 16) // external file attributes - 'directory' bit set
145 .pack('V', $this->offset) // relative offset of local header
146 .$dir;
147
148 $this->offset = strlen($this->zipdata);
149 $this->entries++;
150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // --------------------------------------------------------------------
153
154 /**
155 * Add Data to Zip
156 *
157 * Lets you add files to the archive. If the path is included
Andrey Andreev0b5a4862012-02-29 23:44:00 +0200158 * in the filename it will be placed within a directory. Make
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 * sure you use add_dir() first to create the folder.
160 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 * @param mixed
162 * @param string
163 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200164 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200165 public function add_data($filepath, $data = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200166 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 if (is_array($filepath))
168 {
169 foreach ($filepath as $path => $data)
170 {
Barry Mienydd671972010-10-04 16:33:58 +0200171 $file_data = $this->_get_mod_time($path);
Greg Aker5ed19b42010-03-19 12:13:14 -0500172 $this->_add_data($path, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
174 }
175 else
176 {
Greg Aker5ed19b42010-03-19 12:13:14 -0500177 $file_data = $this->_get_mod_time($filepath);
Greg Aker5ed19b42010-03-19 12:13:14 -0500178 $this->_add_data($filepath, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180 }
181
182 // --------------------------------------------------------------------
183
184 /**
185 * Add Data to Zip
186 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @param string the file name/path
188 * @param string the data to be encoded
Andrey Andreev6c308b72012-02-02 22:03:53 +0200189 * @param int
190 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200192 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200193 protected function _add_data($filepath, $data, $file_mtime, $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200195 $filepath = str_replace('\\', '/', $filepath);
Derek Allard2067d1a2008-11-13 22:59:24 +0000196
197 $uncompressed_size = strlen($data);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500198 $crc32 = crc32($data);
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200199 $gzdata = substr(gzcompress($data), 2, -4);
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 $compressed_size = strlen($gzdata);
201
202 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500203 "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00"
204 .pack('v', $file_mtime)
205 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 .pack('V', $crc32)
207 .pack('V', $compressed_size)
208 .pack('V', $uncompressed_size)
209 .pack('v', strlen($filepath)) // length of filename
210 .pack('v', 0) // extra field length
211 .$filepath
212 .$gzdata; // "file data" segment
213
214 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500215 "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00"
216 .pack('v', $file_mtime)
217 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 .pack('V', $crc32)
219 .pack('V', $compressed_size)
220 .pack('V', $uncompressed_size)
221 .pack('v', strlen($filepath)) // length of filename
222 .pack('v', 0) // extra field length
223 .pack('v', 0) // file comment length
224 .pack('v', 0) // disk number start
225 .pack('v', 0) // internal file attributes
226 .pack('V', 32) // external file attributes - 'archive' bit set
227 .pack('V', $this->offset) // relative offset of local header
228 .$filepath;
229
230 $this->offset = strlen($this->zipdata);
231 $this->entries++;
232 $this->file_num++;
233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 // --------------------------------------------------------------------
236
237 /**
238 * Read the contents of a file and add it to the zip
239 *
Andrey Andreev6c308b72012-02-02 22:03:53 +0200240 * @param string
241 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200243 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200244 public function read_file($path, $preserve_filepath = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
246 if ( ! file_exists($path))
247 {
248 return FALSE;
249 }
250
251 if (FALSE !== ($data = file_get_contents($path)))
252 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200253 $name = str_replace('\\', '/', $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 if ($preserve_filepath === FALSE)
255 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200256 $name = preg_replace('|.*/(.+)|', '\\1', $name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
258
259 $this->add_data($name, $data);
260 return TRUE;
261 }
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 return FALSE;
264 }
265
266 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 /**
269 * Read a directory and add it to the zip.
270 *
271 * This function recursively reads a folder and everything it contains (including
Andrey Andreev16d80662012-01-20 13:26:49 +0200272 * sub-folders) and creates a zip based on it. Whatever directory structure
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * is in the original file path will be recreated in the zip file.
274 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @param string path to source
Andrey Andreev6c308b72012-02-02 22:03:53 +0200276 * @param bool
277 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 * @return bool
Phil Sturgeon26872de2010-05-11 11:41:59 +0100279 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200280 public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
Phil Sturgeon26872de2010-05-11 11:41:59 +0100281 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200282 $path = rtrim($path, '/\\').'/';
Derek Jones2735b3e2010-05-11 08:58:21 -0500283 if ( ! $fp = @opendir($path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100285 return FALSE;
286 }
287
288 // Set the original directory root for child dir's to use as relative
289 if ($root_path === NULL)
290 {
291 $root_path = dirname($path).'/';
292 }
293
294 while (FALSE !== ($file = readdir($fp)))
295 {
Andrey Andreev0ea39292011-12-25 18:48:46 +0200296 if ($file[0] === '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100298 continue;
299 }
300
301 if (@is_dir($path.$file))
302 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200303 $this->read_dir($path.$file.'/', $preserve_filepath, $root_path);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100304 }
Andrey Andreev16d80662012-01-20 13:26:49 +0200305 elseif (FALSE !== ($data = file_get_contents($path.$file)))
Phil Sturgeon26872de2010-05-11 11:41:59 +0100306 {
Andrey Andreev16d80662012-01-20 13:26:49 +0200307 $name = str_replace('\\', '/', $path);
308 if ($preserve_filepath === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
Andrey Andreev16d80662012-01-20 13:26:49 +0200310 $name = str_replace($root_path, '', $name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
Andrey Andreev16d80662012-01-20 13:26:49 +0200312 $this->add_data($name.$file, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 }
Derek Jones2735b3e2010-05-11 08:58:21 -0500315
Andrey Andreev6c308b72012-02-02 22:03:53 +0200316 closedir($fp);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100317 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
319
320 // --------------------------------------------------------------------
321
322 /**
323 * Get the Zip file
324 *
Andrey Andreev6c308b72012-02-02 22:03:53 +0200325 * @return string (binary encoded)
Barry Mienydd671972010-10-04 16:33:58 +0200326 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200327 public function get_zip()
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 // Is there any data to return?
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200330 if ($this->entries === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 return FALSE;
333 }
334
Andrey Andreev0ea39292011-12-25 18:48:46 +0200335 return $this->zipdata
Andrey Andreev6c308b72012-02-02 22:03:53 +0200336 .$this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00"
337 .pack('v', $this->entries) // total # of entries "on this disk"
338 .pack('v', $this->entries) // total # of entries overall
339 .pack('V', strlen($this->directory)) // size of central dir
340 .pack('V', strlen($this->zipdata)) // offset to start of central dir
341 ."\x00\x00"; // .zip file comment length
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 // --------------------------------------------------------------------
345
346 /**
347 * Write File to the specified directory
348 *
349 * Lets you write a file
350 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 * @param string the file name
352 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200353 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200354 public function archive($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
356 if ( ! ($fp = @fopen($filepath, FOPEN_WRITE_CREATE_DESTRUCTIVE)))
357 {
358 return FALSE;
359 }
360
Barry Mienydd671972010-10-04 16:33:58 +0200361 flock($fp, LOCK_EX);
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 fwrite($fp, $this->get_zip());
363 flock($fp, LOCK_UN);
364 fclose($fp);
365
Barry Mienydd671972010-10-04 16:33:58 +0200366 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
368
369 // --------------------------------------------------------------------
370
371 /**
372 * Download
373 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 * @param string the file name
Andrey Andreev0b5a4862012-02-29 23:44:00 +0200375 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200377 public function download($filename = 'backup.zip')
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
Andrey Andreeve34f1a72012-01-10 22:41:52 +0200379 if ( ! preg_match('|.+?\.zip$|', $filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
381 $filename .= '.zip';
382 }
383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 $CI =& get_instance();
385 $CI->load->helper('download');
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000386 $get_zip = $this->get_zip();
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000387 $zip_content =& $get_zip;
388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 force_download($filename, $zip_content);
390 }
391
392 // --------------------------------------------------------------------
393
394 /**
395 * Initialize Data
396 *
Andrey Andreev16d80662012-01-20 13:26:49 +0200397 * Lets you clear current zip data. Useful if you need to create
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 * multiple zips with different data.
399 *
Andrey Andreev16d80662012-01-20 13:26:49 +0200400 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200401 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200402 public function clear_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
404 $this->zipdata = '';
405 $this->directory = '';
406 $this->entries = 0;
407 $this->file_num = 0;
408 $this->offset = 0;
Andrey Andreevb9535c82011-12-26 16:21:17 +0200409 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412}
413
414/* End of file Zip.php */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200415/* Location: ./system/libraries/Zip.php */