admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Zip Compression Class |
| 20 | * |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 21 | * This class is based on a library I found at Zend: |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 22 | * http://www.zend.com/codex.php?id=696&single=1 |
| 23 | * |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 24 | * The original library is a little rough around the edges so I |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 25 | * refactored it and added several additional methods -- Rick Ellis |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 26 | * |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 27 | * @package CodeIgniter |
| 28 | * @subpackage Libraries |
| 29 | * @category Encryption |
| 30 | * @author Rick Ellis |
| 31 | * @link http://www.codeigniter.com/user_guide/general/encryption.html |
| 32 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 33 | class CI_Zip { |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 34 | |
| 35 | var $zipfile = ''; |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 36 | var $zipdata = array(); |
| 37 | var $directory = array(); |
| 38 | var $offset = 0; |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 39 | |
| 40 | function CI_Zip() |
| 41 | { |
| 42 | log_message('debug', "Zip Compression Class Initialized"); |
| 43 | } |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 44 | |
| 45 | // -------------------------------------------------------------------- |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 46 | |
| 47 | /** |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 48 | * Add Directory |
| 49 | * |
| 50 | * Lets you add a virtual directory into which you can place files. |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 51 | * |
| 52 | * @access public |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 53 | * @param mixed the directory name. Can be string or array |
| 54 | * @return void |
| 55 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 56 | function add_dir($directory) |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 57 | { |
| 58 | foreach ((array)$directory as $dir) |
| 59 | { |
| 60 | if ( ! preg_match("|.+/$|", $dir)) |
| 61 | { |
| 62 | $dir .= '/'; |
| 63 | } |
| 64 | |
| 65 | $this->_add_dir($dir); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // -------------------------------------------------------------------- |
| 70 | |
| 71 | /** |
| 72 | * Add Directory |
| 73 | * |
| 74 | * @access private |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 75 | * @param string the directory name |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 76 | * @return void |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 77 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 78 | function _add_dir($dir) |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 79 | { |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 80 | $dir = str_replace("\\", "/", $dir); |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 81 | |
| 82 | $this->zipdata[] = "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 83 | .pack('V', 0) |
| 84 | .pack('V', 0) |
| 85 | .pack('V', 0) |
| 86 | .pack('v', strlen($dir)) |
| 87 | .pack('v', 0) |
| 88 | .$dir |
| 89 | .pack('V', 0) |
| 90 | .pack('V', 0) |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 91 | .pack('V', 0); |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 92 | |
| 93 | $newoffset = strlen(implode('', $this->zipdata)); |
| 94 | |
| 95 | $record = "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 96 | .pack('V',0) |
| 97 | .pack('V',0) |
| 98 | .pack('V',0) |
| 99 | .pack('v', strlen($dir)) |
| 100 | .pack('v', 0) |
| 101 | .pack('v', 0) |
| 102 | .pack('v', 0) |
| 103 | .pack('v', 0) |
| 104 | .pack('V', 16) |
| 105 | .pack('V', $this->offset) |
| 106 | .$dir; |
| 107 | |
| 108 | $this->offset = $newoffset; |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 109 | $this->directory[] = $record; |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 110 | } |
| 111 | |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 112 | // -------------------------------------------------------------------- |
| 113 | |
| 114 | /** |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 115 | * Add Data to Zip |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 116 | * |
| 117 | * Lets you add files to the archive. If the path is included |
| 118 | * in the filename it will be placed within a directory. Make |
| 119 | * sure you use add_dir() first to create the folder. |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 120 | * |
| 121 | * @access public |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 122 | * @param mixed |
| 123 | * @param string |
| 124 | * @return void |
| 125 | */ |
| 126 | function add_data($filepath, $data = NULL) |
| 127 | { |
| 128 | if (is_array($filepath)) |
| 129 | { |
| 130 | foreach ($filepath as $path => $data) |
| 131 | { |
| 132 | $this->_add_data($path, $data); |
| 133 | } |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | $this->_add_data($filepath, $data); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // -------------------------------------------------------------------- |
| 142 | |
| 143 | /** |
| 144 | * Add Data to Zip |
| 145 | * |
| 146 | * @access private |
| 147 | * @param string the file name/path |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 148 | * @param string the data to be encoded |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 149 | * @return void |
| 150 | */ |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 151 | function _add_data($filepath, $data) |
| 152 | { |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 153 | $filepath = str_replace("\\", "/", $filepath); |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 154 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 155 | $oldlen = strlen($data); |
| 156 | $crc32 = crc32($data); |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 157 | |
| 158 | $gzdata = gzcompress($data); |
| 159 | $gzdata = substr(substr($gzdata, 0, strlen($gzdata) - 4), 2); |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 160 | $newlen = strlen($gzdata); |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 161 | |
| 162 | $this->zipdata[] = "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00\x00\x00\x00\x00" |
| 163 | .pack('V', $crc32) |
| 164 | .pack('V', $newlen) |
| 165 | .pack('V', $oldlen) |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 166 | .pack('v', strlen($filepath)) |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 167 | .pack('v', 0) |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 168 | .$filepath |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 169 | .$gzdata |
| 170 | .pack('V', $crc32) |
| 171 | .pack('V', $newlen) |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 172 | .pack('V', $oldlen); |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 173 | |
| 174 | $newoffset = strlen(implode("", $this->zipdata)); |
| 175 | |
| 176 | $record = "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00\x00\x00\x00\x00" |
| 177 | .pack('V', $crc32) |
| 178 | .pack('V', $newlen) |
| 179 | .pack('V', $oldlen) |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 180 | .pack('v', strlen($filepath)) |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 181 | .pack('v', 0) |
| 182 | .pack('v', 0) |
| 183 | .pack('v', 0) |
| 184 | .pack('v', 0) |
| 185 | .pack('V', 32) |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 186 | .pack('V', $this->offset); |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 187 | |
| 188 | $this->offset = $newoffset; |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 189 | $this->directory[] = $record.$filepath; |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // -------------------------------------------------------------------- |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 193 | |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 194 | /** |
admin | 5725a40 | 2006-10-23 20:02:04 +0000 | [diff] [blame] | 195 | * Read the contents of a file and add it to the zip |
| 196 | * |
| 197 | * @access public |
| 198 | * @return bool |
| 199 | */ |
| 200 | function read_file($path, $preserve_filepath = FALSE) |
| 201 | { |
| 202 | if ( ! file_exists($path)) |
| 203 | { |
| 204 | return FALSE; |
| 205 | } |
| 206 | |
| 207 | if (FALSE !== ($data = file_get_contents($path))) |
| 208 | { |
| 209 | $name = str_replace("\\", "/", $path); |
| 210 | |
| 211 | if ($preserve_filepath === FALSE) |
| 212 | { |
| 213 | $name = preg_replace("|.*/(.+)|", "\\1", $name); |
| 214 | } |
| 215 | |
| 216 | $this->add_data($name, $data); |
| 217 | return TRUE; |
| 218 | } |
| 219 | return FALSE; |
| 220 | } |
| 221 | |
| 222 | // ------------------------------------------------------------------------ |
| 223 | |
| 224 | /** |
| 225 | * Read a directory and add it to the zip. |
| 226 | * |
| 227 | * This function recursively reads a folder and everything it contains (including |
| 228 | * sub-folders) and creates a zip based on it. Whatever directory structure |
| 229 | * is in the original file path will be recreated in the zip file. |
| 230 | * |
| 231 | * @access public |
| 232 | * @param string path to source |
| 233 | * @return bool |
| 234 | */ |
| 235 | function read_dir($path) |
| 236 | { |
| 237 | if ($fp = @opendir($path)) |
| 238 | { |
| 239 | while (FALSE !== ($file = readdir($fp))) |
| 240 | { |
| 241 | if (@is_dir($path.$file) && substr($file, 0, 1) != '.') |
| 242 | { |
| 243 | $this->read_dir($path.$file."/"); |
| 244 | } |
| 245 | elseif (substr($file, 0, 1) != ".") |
| 246 | { |
| 247 | if (FALSE !== ($data = file_get_contents($path.$file))) |
| 248 | { |
| 249 | $this->add_data(str_replace("\\", "/", $path).$file, $data); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | return TRUE; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // -------------------------------------------------------------------- |
| 258 | |
| 259 | /** |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 260 | * Get the Zip file |
| 261 | * |
| 262 | * @access public |
| 263 | * @return binary string |
| 264 | */ |
| 265 | function get_zip() |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 266 | { |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 267 | // We cache the zip data so multiple calls |
| 268 | // do not require recompiling |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 269 | if ($this->zipfile != '') |
| 270 | { |
| 271 | return $this->zipfile; |
| 272 | } |
| 273 | |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 274 | // Is there any data to return? |
| 275 | if (count($this->zipdata) == 0) |
| 276 | { |
| 277 | return FALSE; |
| 278 | } |
| 279 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 280 | $data = implode('', $this->zipdata); |
| 281 | $dir = implode('', $this->directory); |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 282 | |
| 283 | $this->zipfile = $data.$dir."\x50\x4b\x05\x06\x00\x00\x00\x00" |
| 284 | .pack('v', sizeof($this->directory)) |
| 285 | .pack('v', sizeof($this->directory)) |
| 286 | .pack('V', strlen($dir)) |
| 287 | .pack('V', strlen($data)) |
| 288 | ."\x00\x00"; |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 289 | |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 290 | return $this->zipfile; |
| 291 | } |
| 292 | |
| 293 | // -------------------------------------------------------------------- |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 294 | |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 295 | /** |
admin | e7e1dcd | 2006-10-21 18:04:01 +0000 | [diff] [blame] | 296 | * Write File to the specified directory |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 297 | * |
| 298 | * Lets you write a file |
| 299 | * |
| 300 | * @access public |
| 301 | * @param string the file name |
| 302 | * @param string the data to be encoded |
| 303 | * @return bool |
| 304 | */ |
admin | 0f8015f | 2006-10-01 22:08:34 +0000 | [diff] [blame] | 305 | function archive($filepath) |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 306 | { |
admin | 0f8015f | 2006-10-01 22:08:34 +0000 | [diff] [blame] | 307 | if ( ! ($fp = @fopen($filepath, "wb"))) |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 308 | { |
| 309 | return FALSE; |
| 310 | } |
| 311 | |
| 312 | flock($fp, LOCK_EX); |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 313 | fwrite($fp, $this->get_zip()); |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 314 | flock($fp, LOCK_UN); |
| 315 | fclose($fp); |
| 316 | |
| 317 | return TRUE; |
| 318 | } |
| 319 | |
| 320 | // -------------------------------------------------------------------- |
| 321 | |
| 322 | /** |
| 323 | * Download |
| 324 | * |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 325 | * @access public |
| 326 | * @param string the file name |
| 327 | * @param string the data to be encoded |
| 328 | * @return bool |
| 329 | */ |
admin | 33dd95d | 2006-10-01 19:20:42 +0000 | [diff] [blame] | 330 | function download($filename = 'backup.zip') |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 331 | { |
admin | 33dd95d | 2006-10-01 19:20:42 +0000 | [diff] [blame] | 332 | if ( ! preg_match("|.+?\.zip$|", $filename)) |
| 333 | { |
| 334 | $filename .= '.zip'; |
| 335 | } |
| 336 | |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 337 | if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) |
| 338 | { |
| 339 | header('Content-Type: application/x-zip'); |
| 340 | header('Content-Disposition: inline; filename="'.$filename.'"'); |
| 341 | header('Expires: 0'); |
| 342 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
| 343 | header("Content-Transfer-Encoding: binary"); |
| 344 | header('Pragma: public'); |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 345 | header("Content-Length: ".strlen($this->get_zip())); |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 346 | } |
| 347 | else |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 348 | { |
| 349 | header('Content-Type: application/x-zip'); |
| 350 | header('Content-Disposition: attachment; filename="'.$filename.'"'); |
| 351 | header("Content-Transfer-Encoding: binary"); |
| 352 | header('Expires: 0'); |
| 353 | header('Pragma: no-cache'); |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 354 | header("Content-Length: ".strlen($this->get_zip())); |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 355 | } |
| 356 | |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 357 | echo $this->get_zip(); |
| 358 | } |
| 359 | |
| 360 | // -------------------------------------------------------------------- |
| 361 | |
| 362 | /** |
| 363 | * Initialize Data |
| 364 | * |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 365 | * Lets you clear current zip data. Useful if you need to create |
admin | 24dd7e7 | 2006-10-01 19:02:29 +0000 | [diff] [blame] | 366 | * multiple zips with different data. |
| 367 | * |
| 368 | * @access public |
| 369 | * @return void |
| 370 | */ |
| 371 | function clear_data() |
| 372 | { |
| 373 | $this->zipfile = ''; |
| 374 | $this->zipdata = array(); |
| 375 | $this->directory = array(); |
| 376 | $this->offset = array(); |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 377 | } |
| 378 | |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 379 | } |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 380 | ?> |