blob: 2ed79f0e36b4abd73e84a02855792b237411aa64 [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
12 * bundled with this package in the files license.txt / license.rst. It is
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 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
28// ------------------------------------------------------------------------
29
30/**
31 * Zip Compression Class
32 *
33 * This class is based on a library I found at Zend:
34 * http://www.zend.com/codex.php?id=696&single=1
35 *
36 * The original library is a little rough around the edges so I
37 * refactored it and added several additional methods -- Rick Ellis
38 *
39 * @package CodeIgniter
40 * @subpackage Libraries
41 * @category Encryption
Derek Jonesf4a4bd82011-10-20 12:18:42 -050042 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000043 * @link http://codeigniter.com/user_guide/libraries/zip.html
44 */
Derek Jones37f4b9c2011-07-01 17:56:50 -050045class CI_Zip {
Derek Allard2067d1a2008-11-13 22:59:24 +000046
Andrey Andreev0ea39292011-12-25 18:48:46 +020047 public $zipdata = '';
48 public $directory = '';
49 public $entries = 0;
50 public $file_num = 0;
51 public $offset = 0;
52 public $now;
Derek Allard2067d1a2008-11-13 22:59:24 +000053
Greg Akera9263282010-11-10 15:26:43 -060054 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000055 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +020056 log_message('debug', 'Zip Compression Class Initialized');
Greg Aker5ed19b42010-03-19 12:13:14 -050057 $this->now = time();
Derek Allard2067d1a2008-11-13 22:59:24 +000058 }
59
60 // --------------------------------------------------------------------
61
62 /**
63 * Add Directory
64 *
65 * Lets you add a virtual directory into which you can place files.
66 *
Derek Allard2067d1a2008-11-13 22:59:24 +000067 * @param mixed the directory name. Can be string or array
68 * @return void
69 */
Andrey Andreev0ea39292011-12-25 18:48:46 +020070 public function add_dir($directory)
Derek Allard2067d1a2008-11-13 22:59:24 +000071 {
72 foreach ((array)$directory as $dir)
73 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +020074 if ( ! preg_match('|.+/$|', $dir))
Derek Allard2067d1a2008-11-13 22:59:24 +000075 {
76 $dir .= '/';
77 }
78
Greg Aker5ed19b42010-03-19 12:13:14 -050079 $dir_time = $this->_get_mod_time($dir);
Greg Aker5ed19b42010-03-19 12:13:14 -050080 $this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +000081 }
82 }
83
Barry Mienydd671972010-10-04 16:33:58 +020084 // --------------------------------------------------------------------
Greg Aker5ed19b42010-03-19 12:13:14 -050085
86 /**
87 * Get file/directory modification time
Barry Mienydd671972010-10-04 16:33:58 +020088 *
Greg Aker5ed19b42010-03-19 12:13:14 -050089 * If this is a newly created file/dir, we will set the time to 'now'
90 *
91 * @param string path to file
Barry Mienydd671972010-10-04 16:33:58 +020092 * @return array filemtime/filemdate
Greg Aker5ed19b42010-03-19 12:13:14 -050093 */
Andrey Andreevb9535c82011-12-26 16:21:17 +020094 protected function _get_mod_time($dir)
Greg Aker5ed19b42010-03-19 12:13:14 -050095 {
Thomas Traub98f85b12011-12-05 14:58:29 +010096 // filemtime() may return false, but raises an error for non-existing files
Thomas Traubdba657e2011-12-06 06:30:37 +010097 $date = (file_exists($dir)) ? filemtime($dir): getdate($this->now);
Andrey Andreev0ea39292011-12-25 18:48:46 +020098
Andrey Andreevcfbd15b2012-01-08 06:41:41 +020099 return array(
Andrey Andreev0ea39292011-12-25 18:48:46 +0200100 'file_mtime' => ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2,
101 'file_mdate' => (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday']
102 );
Greg Aker5ed19b42010-03-19 12:13:14 -0500103 }
104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 // --------------------------------------------------------------------
106
107 /**
108 * Add Directory
109 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 * @param string the directory name
111 * @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 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 $dir = str_replace("\\", "/", $dir);
116
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
Derek Jones37f4b9c2011-07-01 17:56:50 -0500158 * 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
189 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200190 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200191 protected function _add_data($filepath, $data, $file_mtime, $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200193 $filepath = str_replace('\\', '/', $filepath);
Derek Allard2067d1a2008-11-13 22:59:24 +0000194
195 $uncompressed_size = strlen($data);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500196 $crc32 = crc32($data);
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200197 $gzdata = substr(gzcompress($data), 2, -4);
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 $compressed_size = strlen($gzdata);
199
200 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500201 "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00"
202 .pack('v', $file_mtime)
203 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 .pack('V', $crc32)
205 .pack('V', $compressed_size)
206 .pack('V', $uncompressed_size)
207 .pack('v', strlen($filepath)) // length of filename
208 .pack('v', 0) // extra field length
209 .$filepath
210 .$gzdata; // "file data" segment
211
212 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500213 "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00"
214 .pack('v', $file_mtime)
215 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 .pack('V', $crc32)
217 .pack('V', $compressed_size)
218 .pack('V', $uncompressed_size)
219 .pack('v', strlen($filepath)) // length of filename
220 .pack('v', 0) // extra field length
221 .pack('v', 0) // file comment length
222 .pack('v', 0) // disk number start
223 .pack('v', 0) // internal file attributes
224 .pack('V', 32) // external file attributes - 'archive' bit set
225 .pack('V', $this->offset) // relative offset of local header
226 .$filepath;
227
228 $this->offset = strlen($this->zipdata);
229 $this->entries++;
230 $this->file_num++;
231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 // --------------------------------------------------------------------
234
235 /**
236 * Read the contents of a file and add it to the zip
237 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200239 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200240 public function read_file($path, $preserve_filepath = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
242 if ( ! file_exists($path))
243 {
244 return FALSE;
245 }
246
247 if (FALSE !== ($data = file_get_contents($path)))
248 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200249 $name = str_replace('\\', '/', $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 if ($preserve_filepath === FALSE)
251 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200252 $name = preg_replace('|.*/(.+)|', '\\1', $name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
254
255 $this->add_data($name, $data);
256 return TRUE;
257 }
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200258
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 return FALSE;
260 }
261
262 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 /**
265 * Read a directory and add it to the zip.
266 *
267 * This function recursively reads a folder and everything it contains (including
Derek Jones37f4b9c2011-07-01 17:56:50 -0500268 * sub-folders) and creates a zip based on it. Whatever directory structure
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 * is in the original file path will be recreated in the zip file.
270 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 * @param string path to source
272 * @return bool
Phil Sturgeon26872de2010-05-11 11:41:59 +0100273 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200274 public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
Phil Sturgeon26872de2010-05-11 11:41:59 +0100275 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200276 $path = rtrim($path, '/\\').'/';
Andrey Andreevb9535c82011-12-26 16:21:17 +0200277
Derek Jones2735b3e2010-05-11 08:58:21 -0500278 if ( ! $fp = @opendir($path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100280 return FALSE;
281 }
282
283 // Set the original directory root for child dir's to use as relative
284 if ($root_path === NULL)
285 {
286 $root_path = dirname($path).'/';
287 }
288
289 while (FALSE !== ($file = readdir($fp)))
290 {
Andrey Andreev0ea39292011-12-25 18:48:46 +0200291 if ($file[0] === '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100293 continue;
294 }
295
296 if (@is_dir($path.$file))
297 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200298 $this->read_dir($path.$file.'/', $preserve_filepath, $root_path);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100299 }
Phil Sturgeon26872de2010-05-11 11:41:59 +0100300 else
301 {
302 if (FALSE !== ($data = file_get_contents($path.$file)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200304 $name = str_replace('\\', '/', $path);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100305 if ($preserve_filepath === FALSE)
306 {
307 $name = str_replace($root_path, '', $name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
Phil Sturgeon26872de2010-05-11 11:41:59 +0100309
310 $this->add_data($name.$file, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
312 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
Derek Jones2735b3e2010-05-11 08:58:21 -0500314
Phil Sturgeon26872de2010-05-11 11:41:59 +0100315 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * Get the Zip file
322 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 * @return binary string
Barry Mienydd671972010-10-04 16:33:58 +0200324 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200325 public function get_zip()
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
327 // Is there any data to return?
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200328 if ($this->entries === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 return FALSE;
331 }
332
Andrey Andreev0ea39292011-12-25 18:48:46 +0200333 return $this->zipdata
334 . $this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00"
335 . pack('v', $this->entries) // total # of entries "on this disk"
336 . pack('v', $this->entries) // total # of entries overall
337 . pack('V', strlen($this->directory)) // size of central dir
338 . pack('V', strlen($this->zipdata)) // offset to start of central dir
339 . "\x00\x00"; // .zip file comment length
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // --------------------------------------------------------------------
343
344 /**
345 * Write File to the specified directory
346 *
347 * Lets you write a file
348 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 * @param string the file name
350 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200351 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200352 public function archive($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
354 if ( ! ($fp = @fopen($filepath, FOPEN_WRITE_CREATE_DESTRUCTIVE)))
355 {
356 return FALSE;
357 }
358
Barry Mienydd671972010-10-04 16:33:58 +0200359 flock($fp, LOCK_EX);
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 fwrite($fp, $this->get_zip());
361 flock($fp, LOCK_UN);
362 fclose($fp);
363
Barry Mienydd671972010-10-04 16:33:58 +0200364 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
366
367 // --------------------------------------------------------------------
368
369 /**
370 * Download
371 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 * @param string the file name
373 * @param string the data to be encoded
374 * @return bool
375 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200376 public function download($filename = 'backup.zip')
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 if ( ! preg_match("|.+?\.zip$|", $filename))
379 {
380 $filename .= '.zip';
381 }
382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 $CI =& get_instance();
384 $CI->load->helper('download');
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000385 $get_zip = $this->get_zip();
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000386 $zip_content =& $get_zip;
387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 force_download($filename, $zip_content);
389 }
390
391 // --------------------------------------------------------------------
392
393 /**
394 * Initialize Data
395 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500396 * Lets you clear current zip data. Useful if you need to create
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * multiple zips with different data.
398 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200400 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200401 public function clear_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
403 $this->zipdata = '';
404 $this->directory = '';
405 $this->entries = 0;
406 $this->file_num = 0;
407 $this->offset = 0;
Andrey Andreevb9535c82011-12-26 16:21:17 +0200408 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411}
412
413/* End of file Zip.php */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200414/* Location: ./system/libraries/Zip.php */