| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * CodeIgniter | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  |  * An open source application development framework for PHP | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  |  * This content is released under the MIT License (MIT) | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-11-07 03:05:55 +08:00
										 |  |  |  * Copyright (c) 2014 - 2019, British Columbia Institute of Technology | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
					
						
							|  |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
					
						
							|  |  |  |  * in the Software without restriction, including without limitation the rights | 
					
						
							|  |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
					
						
							|  |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
					
						
							|  |  |  |  * furnished to do so, subject to the following conditions: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The above copyright notice and this permission notice shall be included in | 
					
						
							|  |  |  |  * all copies or substantial portions of the Software. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
					
						
							|  |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
					
						
							|  |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
					
						
							|  |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
					
						
							|  |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
					
						
							|  |  |  |  * THE SOFTWARE. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package	CodeIgniter | 
					
						
							|  |  |  |  * @author	EllisLab Dev Team | 
					
						
							|  |  |  |  * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) | 
					
						
							| 
									
										
										
										
											2021-11-07 03:05:55 +08:00
										 |  |  |  * @copyright	Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/) | 
					
						
							|  |  |  |  * @license	https://opensource.org/licenses/MIT	MIT License | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  |  * @link	https://codeigniter.com | 
					
						
							|  |  |  |  * @since	Version 1.0.0 | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  |  * @filesource | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | defined('BASEPATH') OR exit('No direct script access allowed'); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Zip Compression Class | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This class is based on a library I found at Zend: | 
					
						
							| 
									
										
										
										
											2021-11-07 03:05:55 +08:00
										 |  |  |  * https://www.zend.com/codex.php?id=696&single=1 | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * The original library is a little rough around the edges so I | 
					
						
							|  |  |  |  * refactored it and added several additional methods -- Rick Ellis | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package		CodeIgniter | 
					
						
							|  |  |  |  * @subpackage	Libraries | 
					
						
							|  |  |  |  * @category	Encryption | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  |  * @author		EllisLab Dev Team | 
					
						
							| 
									
										
										
										
											2021-11-07 03:05:55 +08:00
										 |  |  |  * @link		https://codeigniter.com/userguide3/libraries/zip.html | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | class CI_Zip { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Zip data in string form | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var string | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public $zipdata = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Zip data for a directory in string form | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var string | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public $directory = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Number of files/folder in zip file | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var int | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public $entries = 0; | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Number of files in zip | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var int | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public $file_num = 0; | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * relative offset of local header | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var int | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public $offset = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Reference to time at init | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var int | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public $now; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * The level of compression | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * Ranges from 0 to 9, with 9 being the highest level. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var	int | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public $compression_level = 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * mbstring.func_overload flag | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @var	bool | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	protected static $func_overload; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Initialize zip compression class | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @return	void | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	public function __construct() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		isset(self::$func_overload) OR self::$func_overload = (extension_loaded('mbstring') && ini_get('mbstring.func_overload')); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		$this->now = time(); | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		log_message('info', 'Zip Compression Class Initialized'); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Add Directory | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * Lets you add a virtual directory into which you can place files. | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @param	mixed	$directory	the directory name. Can be string or array | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 * @return	void | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	public function add_dir($directory) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		foreach ((array) $directory as $dir) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			if ( ! preg_match('|.+/$|', $dir)) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				$dir .= '/'; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			$dir_time = $this->_get_mod_time($dir); | 
					
						
							|  |  |  | 			$this->_add_dir($dir, $dir_time['file_mtime'], $dir_time['file_mdate']); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * Get file/directory modification time | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * If this is a newly created file/dir, we will set the time to 'now' | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @param	string	$dir	path to file | 
					
						
							|  |  |  | 	 * @return	array	filemtime/filemdate | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	protected function _get_mod_time($dir) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		// filemtime() may return false, but raises an error for non-existing files
 | 
					
						
							|  |  |  | 		$date = file_exists($dir) ? getdate(filemtime($dir)) : getdate($this->now); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		return array( | 
					
						
							|  |  |  | 			'file_mtime' => ($date['hours'] << 11) + ($date['minutes'] << 5) + $date['seconds'] / 2, | 
					
						
							|  |  |  | 			'file_mdate' => (($date['year'] - 1980) << 9) + ($date['mon'] << 5) + $date['mday'] | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Add Directory | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @param	string	$dir	the directory name | 
					
						
							|  |  |  | 	 * @param	int	$file_mtime | 
					
						
							|  |  |  | 	 * @param	int	$file_mdate | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 * @return	void | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	protected function _add_dir($dir, $file_mtime, $file_mdate) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		$dir = str_replace('\\', '/', $dir); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		$this->zipdata .= | 
					
						
							|  |  |  | 			"\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00" | 
					
						
							|  |  |  | 			.pack('v', $file_mtime) | 
					
						
							|  |  |  | 			.pack('v', $file_mdate) | 
					
						
							|  |  |  | 			.pack('V', 0) // crc32
 | 
					
						
							|  |  |  | 			.pack('V', 0) // compressed filesize
 | 
					
						
							|  |  |  | 			.pack('V', 0) // uncompressed filesize
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			.pack('v', self::strlen($dir)) // length of pathname
 | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 			.pack('v', 0) // extra field length
 | 
					
						
							|  |  |  | 			.$dir | 
					
						
							|  |  |  | 			// below is "data descriptor" segment
 | 
					
						
							|  |  |  | 			.pack('V', 0) // crc32
 | 
					
						
							|  |  |  | 			.pack('V', 0) // compressed filesize
 | 
					
						
							|  |  |  | 			.pack('V', 0); // uncompressed filesize
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$this->directory .= | 
					
						
							|  |  |  | 			"\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00" | 
					
						
							|  |  |  | 			.pack('v', $file_mtime) | 
					
						
							|  |  |  | 			.pack('v', $file_mdate) | 
					
						
							|  |  |  | 			.pack('V',0) // crc32
 | 
					
						
							|  |  |  | 			.pack('V',0) // compressed filesize
 | 
					
						
							|  |  |  | 			.pack('V',0) // uncompressed filesize
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			.pack('v', self::strlen($dir)) // length of pathname
 | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 			.pack('v', 0) // extra field length
 | 
					
						
							|  |  |  | 			.pack('v', 0) // file comment length
 | 
					
						
							|  |  |  | 			.pack('v', 0) // disk number start
 | 
					
						
							|  |  |  | 			.pack('v', 0) // internal file attributes
 | 
					
						
							|  |  |  | 			.pack('V', 16) // external file attributes - 'directory' bit set
 | 
					
						
							|  |  |  | 			.pack('V', $this->offset) // relative offset of local header
 | 
					
						
							|  |  |  | 			.$dir; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		$this->offset = self::strlen($this->zipdata); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 		$this->entries++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Add Data to Zip | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * Lets you add files to the archive. If the path is included | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * in the filename it will be placed within a directory. Make | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 * sure you use add_dir() first to create the folder. | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @param	mixed	$filepath	A single filepath or an array of file => data pairs | 
					
						
							|  |  |  | 	 * @param	string	$data		Single file contents | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 * @return	void | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	public function add_data($filepath, $data = NULL) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (is_array($filepath)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			foreach ($filepath as $path => $data) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				$file_data = $this->_get_mod_time($path); | 
					
						
							|  |  |  | 				$this->_add_data($path, $data, $file_data['file_mtime'], $file_data['file_mdate']); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			$file_data = $this->_get_mod_time($filepath); | 
					
						
							|  |  |  | 			$this->_add_data($filepath, $data, $file_data['file_mtime'], $file_data['file_mdate']); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Add Data to Zip | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @param	string	$filepath	the file name/path | 
					
						
							|  |  |  | 	 * @param	string	$data	the data to be encoded | 
					
						
							|  |  |  | 	 * @param	int	$file_mtime | 
					
						
							|  |  |  | 	 * @param	int	$file_mdate | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 * @return	void | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	protected function _add_data($filepath, $data, $file_mtime, $file_mdate) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		$filepath = str_replace('\\', '/', $filepath); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		$uncompressed_size = self::strlen($data); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 		$crc32  = crc32($data); | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		$gzdata = self::substr(gzcompress($data, $this->compression_level), 2, -4); | 
					
						
							|  |  |  | 		$compressed_size = self::strlen($gzdata); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		$this->zipdata .= | 
					
						
							|  |  |  | 			"\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00" | 
					
						
							|  |  |  | 			.pack('v', $file_mtime) | 
					
						
							|  |  |  | 			.pack('v', $file_mdate) | 
					
						
							|  |  |  | 			.pack('V', $crc32) | 
					
						
							|  |  |  | 			.pack('V', $compressed_size) | 
					
						
							|  |  |  | 			.pack('V', $uncompressed_size) | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			.pack('v', self::strlen($filepath)) // length of filename
 | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 			.pack('v', 0) // extra field length
 | 
					
						
							|  |  |  | 			.$filepath | 
					
						
							|  |  |  | 			.$gzdata; // "file data" segment
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$this->directory .= | 
					
						
							|  |  |  | 			"\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00" | 
					
						
							|  |  |  | 			.pack('v', $file_mtime) | 
					
						
							|  |  |  | 			.pack('v', $file_mdate) | 
					
						
							|  |  |  | 			.pack('V', $crc32) | 
					
						
							|  |  |  | 			.pack('V', $compressed_size) | 
					
						
							|  |  |  | 			.pack('V', $uncompressed_size) | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			.pack('v', self::strlen($filepath)) // length of filename
 | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 			.pack('v', 0) // extra field length
 | 
					
						
							|  |  |  | 			.pack('v', 0) // file comment length
 | 
					
						
							|  |  |  | 			.pack('v', 0) // disk number start
 | 
					
						
							|  |  |  | 			.pack('v', 0) // internal file attributes
 | 
					
						
							|  |  |  | 			.pack('V', 32) // external file attributes - 'archive' bit set
 | 
					
						
							|  |  |  | 			.pack('V', $this->offset) // relative offset of local header
 | 
					
						
							|  |  |  | 			.$filepath; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		$this->offset = self::strlen($this->zipdata); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 		$this->entries++; | 
					
						
							|  |  |  | 		$this->file_num++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Read the contents of a file and add it to the zip | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @param	string	$path | 
					
						
							|  |  |  | 	 * @param	bool	$archive_filepath | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 * @return	bool | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	public function read_file($path, $archive_filepath = FALSE) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		if (file_exists($path) && FALSE !== ($data = file_get_contents($path))) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			if (is_string($archive_filepath)) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				$name = str_replace('\\', '/', $archive_filepath); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 				$name = str_replace('\\', '/', $path); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if ($archive_filepath === FALSE) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					$name = preg_replace('|.*/(.+)|', '\\1', $name); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			$this->add_data($name, $data); | 
					
						
							|  |  |  | 			return TRUE; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 		return FALSE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// ------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Read a directory and add it to the zip. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * This function recursively reads a folder and everything it contains (including | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * sub-folders) and creates a zip based on it. Whatever directory structure | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 * is in the original file path will be recreated in the zip file. | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @param	string	$path	path to source directory | 
					
						
							|  |  |  | 	 * @param	bool	$preserve_filepath | 
					
						
							|  |  |  | 	 * @param	string	$root_path | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 * @return	bool | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	public function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		$path = rtrim($path, '/\\').DIRECTORY_SEPARATOR; | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 		if ( ! $fp = @opendir($path)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return FALSE; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Set the original directory root for child dir's to use as relative
 | 
					
						
							|  |  |  | 		if ($root_path === NULL) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			$root_path = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, dirname($path)).DIRECTORY_SEPARATOR; | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		while (FALSE !== ($file = readdir($fp))) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2021-11-07 03:05:55 +08:00
										 |  |  | 			if ($file === '.' OR $file === '..') | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			if (is_dir($path.$file)) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 				$this->read_dir($path.$file.DIRECTORY_SEPARATOR, $preserve_filepath, $root_path); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			elseif (FALSE !== ($data = file_get_contents($path.$file))) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 				$name = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $path); | 
					
						
							|  |  |  | 				if ($preserve_filepath === FALSE) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 					$name = str_replace($root_path, '', $name); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				$this->add_data($name.$file, $data); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		closedir($fp); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 		return TRUE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Get the Zip file | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @return	string	(binary encoded) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	public function get_zip() | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		// Is there any data to return?
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		if ($this->entries === 0) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return FALSE; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-07 03:05:55 +08:00
										 |  |  | 		// @see https://github.com/bcit-ci/CodeIgniter/issues/5864
 | 
					
						
							|  |  |  | 		$footer = $this->directory."\x50\x4b\x05\x06\x00\x00\x00\x00" | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 			.pack('v', $this->entries) // total # of entries "on this disk"
 | 
					
						
							|  |  |  | 			.pack('v', $this->entries) // total # of entries overall
 | 
					
						
							|  |  |  | 			.pack('V', self::strlen($this->directory)) // size of central dir
 | 
					
						
							|  |  |  | 			.pack('V', self::strlen($this->zipdata)) // offset to start of central dir
 | 
					
						
							|  |  |  | 			."\x00\x00"; // .zip file comment length
 | 
					
						
							| 
									
										
										
										
											2021-11-07 03:05:55 +08:00
										 |  |  | 		return $this->zipdata.$footer; | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Write File to the specified directory | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * Lets you write a file | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @param	string	$filepath	the file name | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 * @return	bool | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	public function archive($filepath) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		if ( ! ($fp = @fopen($filepath, 'w+b'))) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return FALSE; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		flock($fp, LOCK_EX); | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for ($result = $written = 0, $data = $this->get_zip(), $length = self::strlen($data); $written < $length; $written += $result) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (($result = fwrite($fp, self::substr($data, $written))) === FALSE) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 		flock($fp, LOCK_UN); | 
					
						
							|  |  |  | 		fclose($fp); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		return is_int($result); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Download | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @param	string	$filename	the file name | 
					
						
							|  |  |  | 	 * @return	void | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	public function download($filename = 'backup.zip') | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		if ( ! preg_match('|.+?\.zip$|', $filename)) | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			$filename .= '.zip'; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		get_instance()->load->helper('download'); | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 		$get_zip = $this->get_zip(); | 
					
						
							|  |  |  | 		$zip_content =& $get_zip; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		force_download($filename, $zip_content); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Initialize Data | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * Lets you clear current zip data. Useful if you need to create | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 * multiple zips with different data. | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	 * @return	CI_Zip | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	public function clear_data() | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 		$this->zipdata = ''; | 
					
						
							|  |  |  | 		$this->directory = ''; | 
					
						
							|  |  |  | 		$this->entries = 0; | 
					
						
							|  |  |  | 		$this->file_num = 0; | 
					
						
							|  |  |  | 		$this->offset = 0; | 
					
						
							|  |  |  | 		return $this; | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Byte-safe strlen() | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @param	string	$str | 
					
						
							|  |  |  | 	 * @return	int | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	protected static function strlen($str) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return (self::$func_overload) | 
					
						
							|  |  |  | 			? mb_strlen($str, '8bit') | 
					
						
							|  |  |  | 			: strlen($str); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-04-25 23:24:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-07 08:45:06 +08:00
										 |  |  | 	// --------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Byte-safe substr() | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @param	string	$str | 
					
						
							|  |  |  | 	 * @param	int	$start | 
					
						
							|  |  |  | 	 * @param	int	$length | 
					
						
							|  |  |  | 	 * @return	string | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	protected static function substr($str, $start, $length = NULL) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (self::$func_overload) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return mb_substr($str, $start, $length, '8bit'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return isset($length) | 
					
						
							|  |  |  | 			? substr($str, $start, $length) | 
					
						
							|  |  |  | 			: substr($str, $start); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |