blob: 85a0b5ce9ba2ac784fd20138fe01e74990be1e76 [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 Andreev16d80662012-01-20 13:26:49 +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 {
70 foreach ((array)$directory as $dir)
71 {
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 /**
85 * Get file/directory modification time
Barry Mienydd671972010-10-04 16:33:58 +020086 *
Greg Aker5ed19b42010-03-19 12:13:14 -050087 * If this is a newly created file/dir, we will set the time to 'now'
88 *
89 * @param string path to file
Barry Mienydd671972010-10-04 16:33:58 +020090 * @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
Thomas Traubdba657e2011-12-06 06:30:37 +010095 $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
109 * @return void
110 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200111 protected function _add_dir($dir, $file_mtime, $file_mdate)
Barry Mienydd671972010-10-04 16:33:58 +0200112 {
Andrey Andreeve34f1a72012-01-10 22:41:52 +0200113 $dir = str_replace('\\', '/', $dir);
Derek Allard2067d1a2008-11-13 22:59:24 +0000114
115 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500116 "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00"
117 .pack('v', $file_mtime)
118 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 .pack('V', 0) // crc32
120 .pack('V', 0) // compressed filesize
121 .pack('V', 0) // uncompressed filesize
122 .pack('v', strlen($dir)) // length of pathname
123 .pack('v', 0) // extra field length
124 .$dir
125 // below is "data descriptor" segment
126 .pack('V', 0) // crc32
127 .pack('V', 0) // compressed filesize
128 .pack('V', 0); // uncompressed filesize
129
130 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500131 "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00"
132 .pack('v', $file_mtime)
133 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 .pack('V',0) // crc32
135 .pack('V',0) // compressed filesize
136 .pack('V',0) // uncompressed filesize
137 .pack('v', strlen($dir)) // length of pathname
138 .pack('v', 0) // extra field length
139 .pack('v', 0) // file comment length
140 .pack('v', 0) // disk number start
141 .pack('v', 0) // internal file attributes
142 .pack('V', 16) // external file attributes - 'directory' bit set
143 .pack('V', $this->offset) // relative offset of local header
144 .$dir;
145
146 $this->offset = strlen($this->zipdata);
147 $this->entries++;
148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 // --------------------------------------------------------------------
151
152 /**
153 * Add Data to Zip
154 *
155 * Lets you add files to the archive. If the path is included
Derek Jones37f4b9c2011-07-01 17:56:50 -0500156 * in the filename it will be placed within a directory. Make
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * sure you use add_dir() first to create the folder.
158 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 * @param mixed
160 * @param string
161 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200162 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200163 public function add_data($filepath, $data = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200164 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 if (is_array($filepath))
166 {
167 foreach ($filepath as $path => $data)
168 {
Barry Mienydd671972010-10-04 16:33:58 +0200169 $file_data = $this->_get_mod_time($path);
Greg Aker5ed19b42010-03-19 12:13:14 -0500170 $this->_add_data($path, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
172 }
173 else
174 {
Greg Aker5ed19b42010-03-19 12:13:14 -0500175 $file_data = $this->_get_mod_time($filepath);
Greg Aker5ed19b42010-03-19 12:13:14 -0500176 $this->_add_data($filepath, $data, $file_data['file_mtime'], $file_data['file_mdate']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 }
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Add Data to Zip
184 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 * @param string the file name/path
186 * @param string the data to be encoded
187 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200188 */
Andrey Andreevb9535c82011-12-26 16:21:17 +0200189 protected function _add_data($filepath, $data, $file_mtime, $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200191 $filepath = str_replace('\\', '/', $filepath);
Derek Allard2067d1a2008-11-13 22:59:24 +0000192
193 $uncompressed_size = strlen($data);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500194 $crc32 = crc32($data);
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200195 $gzdata = substr(gzcompress($data), 2, -4);
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 $compressed_size = strlen($gzdata);
197
198 $this->zipdata .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500199 "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00"
200 .pack('v', $file_mtime)
201 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 .pack('V', $crc32)
203 .pack('V', $compressed_size)
204 .pack('V', $uncompressed_size)
205 .pack('v', strlen($filepath)) // length of filename
206 .pack('v', 0) // extra field length
207 .$filepath
208 .$gzdata; // "file data" segment
209
210 $this->directory .=
Greg Aker5ed19b42010-03-19 12:13:14 -0500211 "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00"
212 .pack('v', $file_mtime)
213 .pack('v', $file_mdate)
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 .pack('V', $crc32)
215 .pack('V', $compressed_size)
216 .pack('V', $uncompressed_size)
217 .pack('v', strlen($filepath)) // length of filename
218 .pack('v', 0) // extra field length
219 .pack('v', 0) // file comment length
220 .pack('v', 0) // disk number start
221 .pack('v', 0) // internal file attributes
222 .pack('V', 32) // external file attributes - 'archive' bit set
223 .pack('V', $this->offset) // relative offset of local header
224 .$filepath;
225
226 $this->offset = strlen($this->zipdata);
227 $this->entries++;
228 $this->file_num++;
229 }
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 // --------------------------------------------------------------------
232
233 /**
234 * Read the contents of a file and add it to the zip
235 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200237 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200238 public function read_file($path, $preserve_filepath = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
240 if ( ! file_exists($path))
241 {
242 return FALSE;
243 }
244
245 if (FALSE !== ($data = file_get_contents($path)))
246 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200247 $name = str_replace('\\', '/', $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 if ($preserve_filepath === FALSE)
249 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200250 $name = preg_replace('|.*/(.+)|', '\\1', $name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 }
252
253 $this->add_data($name, $data);
254 return TRUE;
255 }
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 return FALSE;
258 }
259
260 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 /**
263 * Read a directory and add it to the zip.
264 *
265 * This function recursively reads a folder and everything it contains (including
Andrey Andreev16d80662012-01-20 13:26:49 +0200266 * sub-folders) and creates a zip based on it. Whatever directory structure
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 * is in the original file path will be recreated in the zip file.
268 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 * @param string path to source
270 * @return bool
Phil Sturgeon26872de2010-05-11 11:41:59 +0100271 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200272 public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
Phil Sturgeon26872de2010-05-11 11:41:59 +0100273 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200274 $path = rtrim($path, '/\\').'/';
Andrey Andreevb9535c82011-12-26 16:21:17 +0200275
Derek Jones2735b3e2010-05-11 08:58:21 -0500276 if ( ! $fp = @opendir($path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100278 return FALSE;
279 }
280
281 // Set the original directory root for child dir's to use as relative
282 if ($root_path === NULL)
283 {
284 $root_path = dirname($path).'/';
285 }
286
287 while (FALSE !== ($file = readdir($fp)))
288 {
Andrey Andreev0ea39292011-12-25 18:48:46 +0200289 if ($file[0] === '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Phil Sturgeon26872de2010-05-11 11:41:59 +0100291 continue;
292 }
293
294 if (@is_dir($path.$file))
295 {
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200296 $this->read_dir($path.$file.'/', $preserve_filepath, $root_path);
Phil Sturgeon26872de2010-05-11 11:41:59 +0100297 }
Andrey Andreev16d80662012-01-20 13:26:49 +0200298 elseif (FALSE !== ($data = file_get_contents($path.$file)))
Phil Sturgeon26872de2010-05-11 11:41:59 +0100299 {
Andrey Andreev16d80662012-01-20 13:26:49 +0200300 $name = str_replace('\\', '/', $path);
301 if ($preserve_filepath === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
Andrey Andreev16d80662012-01-20 13:26:49 +0200303 $name = str_replace($root_path, '', $name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 }
Andrey Andreev16d80662012-01-20 13:26:49 +0200305 $this->add_data($name.$file, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
Derek Jones2735b3e2010-05-11 08:58:21 -0500308
Phil Sturgeon26872de2010-05-11 11:41:59 +0100309 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311
312 // --------------------------------------------------------------------
313
314 /**
315 * Get the Zip file
316 *
Andrey Andreev16d80662012-01-20 13:26:49 +0200317 * @return string (binary encoded)
Barry Mienydd671972010-10-04 16:33:58 +0200318 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200319 public function get_zip()
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 // Is there any data to return?
Andrey Andreevcfbd15b2012-01-08 06:41:41 +0200322 if ($this->entries === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
324 return FALSE;
325 }
326
Andrey Andreev0ea39292011-12-25 18:48:46 +0200327 return $this->zipdata
328 . $this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00"
329 . pack('v', $this->entries) // total # of entries "on this disk"
330 . pack('v', $this->entries) // total # of entries overall
331 . pack('V', strlen($this->directory)) // size of central dir
332 . pack('V', strlen($this->zipdata)) // offset to start of central dir
333 . "\x00\x00"; // .zip file comment length
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 // --------------------------------------------------------------------
337
338 /**
339 * Write File to the specified directory
340 *
341 * Lets you write a file
342 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 * @param string the file name
344 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200345 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200346 public function archive($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
348 if ( ! ($fp = @fopen($filepath, FOPEN_WRITE_CREATE_DESTRUCTIVE)))
349 {
350 return FALSE;
351 }
352
Barry Mienydd671972010-10-04 16:33:58 +0200353 flock($fp, LOCK_EX);
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 fwrite($fp, $this->get_zip());
355 flock($fp, LOCK_UN);
356 fclose($fp);
357
Barry Mienydd671972010-10-04 16:33:58 +0200358 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
360
361 // --------------------------------------------------------------------
362
363 /**
364 * Download
365 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 * @param string the file name
367 * @param string the data to be encoded
368 * @return bool
369 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200370 public function download($filename = 'backup.zip')
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
Andrey Andreeve34f1a72012-01-10 22:41:52 +0200372 if ( ! preg_match('|.+?\.zip$|', $filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
374 $filename .= '.zip';
375 }
376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 $CI =& get_instance();
378 $CI->load->helper('download');
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000379 $get_zip = $this->get_zip();
Derek Allard8dc2c7c2009-12-07 16:07:15 +0000380 $zip_content =& $get_zip;
381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 force_download($filename, $zip_content);
383 }
384
385 // --------------------------------------------------------------------
386
387 /**
388 * Initialize Data
389 *
Andrey Andreev16d80662012-01-20 13:26:49 +0200390 * Lets you clear current zip data. Useful if you need to create
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 * multiple zips with different data.
392 *
Andrey Andreev16d80662012-01-20 13:26:49 +0200393 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200394 */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200395 public function clear_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
397 $this->zipdata = '';
398 $this->directory = '';
399 $this->entries = 0;
400 $this->file_num = 0;
401 $this->offset = 0;
Andrey Andreevb9535c82011-12-26 16:21:17 +0200402 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405}
406
407/* End of file Zip.php */
Andrey Andreev0ea39292011-12-25 18:48:46 +0200408/* Location: ./system/libraries/Zip.php */