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. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Zip Compression Class |
| 20 | * |
| 21 | * This class is based on a library aquired at Zend: |
| 22 | * http://www.zend.com/codex.php?id=696&single=1 |
| 23 | * |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 24 | * |
| 25 | * @package CodeIgniter |
| 26 | * @subpackage Libraries |
| 27 | * @category Encryption |
| 28 | * @author Rick Ellis |
| 29 | * @link http://www.codeigniter.com/user_guide/general/encryption.html |
| 30 | */ |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 31 | class Zip { |
| 32 | |
| 33 | var $zipdata = array(); |
| 34 | var $directory = array(); |
| 35 | var $offset = 0; |
| 36 | var $zipfile = ''; |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 37 | |
| 38 | /** |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 39 | * Add Directory |
| 40 | * |
| 41 | * Lets you add a virtual directory into which you can place files. |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 42 | * |
| 43 | * @access public |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 44 | * @param string the directory name |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 45 | * @return void |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 46 | */ |
| 47 | function add_dir($dir) |
| 48 | { |
| 49 | $this->zipfile = ''; |
| 50 | |
| 51 | $dir = str_replace("\\", "/", $dir); |
| 52 | |
| 53 | $this->zipdata[] = "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 54 | .pack('V', 0) |
| 55 | .pack('V', 0) |
| 56 | .pack('V', 0) |
| 57 | .pack('v', strlen($dir)) |
| 58 | .pack('v', 0) |
| 59 | .$dir |
| 60 | .pack('V', 0) |
| 61 | .pack('V', 0) |
| 62 | .pack('V', 0); |
| 63 | |
| 64 | $newoffset = strlen(implode('', $this->zipdata)); |
| 65 | |
| 66 | $record = "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 67 | .pack('V',0) |
| 68 | .pack('V',0) |
| 69 | .pack('V',0) |
| 70 | .pack('v', strlen($dir)) |
| 71 | .pack('v', 0) |
| 72 | .pack('v', 0) |
| 73 | .pack('v', 0) |
| 74 | .pack('v', 0) |
| 75 | .pack('V', 16) |
| 76 | .pack('V', $this->offset) |
| 77 | .$dir; |
| 78 | |
| 79 | $this->offset = $newoffset; |
| 80 | $this->directory[] = $record; |
| 81 | } |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 82 | |
| 83 | // -------------------------------------------------------------------- |
| 84 | |
| 85 | /** |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 86 | * Add File |
| 87 | * |
| 88 | * Lets you add files to the archive. If the path is included |
| 89 | * in the filename it will be placed within a directory. Make |
| 90 | * sure you use add_dir() first to create the folder. |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 91 | * |
| 92 | * @access public |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 93 | * @param string the file name |
| 94 | * @param string the data to be encoded |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 95 | * @return void |
| 96 | */ |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 97 | function add_file($filename, $data) |
| 98 | { |
| 99 | $this->zipfile = ''; |
| 100 | |
| 101 | $filename = str_replace("\\", "/", $filename); |
| 102 | |
| 103 | $oldlen = strlen($data); |
| 104 | $crc32 = crc32($data); |
| 105 | |
| 106 | $gzdata = gzcompress($data); |
| 107 | $gzdata = substr(substr($gzdata, 0, strlen($gzdata) - 4), 2); |
| 108 | $newlen = strlen($gzdata); |
| 109 | |
| 110 | $this->zipdata[] = "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00\x00\x00\x00\x00" |
| 111 | .pack('V', $crc32) |
| 112 | .pack('V', $newlen) |
| 113 | .pack('V', $oldlen) |
| 114 | .pack('v', strlen($filename)) |
| 115 | .pack('v', 0) |
| 116 | .$filename |
| 117 | .$gzdata |
| 118 | .pack('V', $crc32) |
| 119 | .pack('V', $newlen) |
| 120 | .pack('V', $oldlen); |
| 121 | |
| 122 | $newoffset = strlen(implode("", $this->zipdata)); |
| 123 | |
| 124 | $record = "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00\x00\x00\x00\x00" |
| 125 | .pack('V', $crc32) |
| 126 | .pack('V', $newlen) |
| 127 | .pack('V', $oldlen) |
| 128 | .pack('v', strlen($filename)) |
| 129 | .pack('v', 0) |
| 130 | .pack('v', 0) |
| 131 | .pack('v', 0) |
| 132 | .pack('v', 0) |
| 133 | .pack('V', 32) |
| 134 | .pack('V', $this->offset); |
| 135 | |
| 136 | $this->offset = $newoffset; |
| 137 | $this->directory[] = $record.$filename; |
| 138 | } |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 139 | |
| 140 | // -------------------------------------------------------------------- |
| 141 | |
| 142 | /** |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 143 | * Read the content of a file |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 144 | * |
| 145 | * @access public |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 146 | * @param string the file path |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 147 | * @return string |
| 148 | */ |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 149 | function read_file($filepath) |
| 150 | { |
| 151 | if ( ! file_exists($filepath)) |
| 152 | { |
| 153 | return FALSE; |
| 154 | } |
| 155 | |
| 156 | return file_get_contents($filepath); |
| 157 | } |
| 158 | |
| 159 | // -------------------------------------------------------------------- |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 160 | |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 161 | /** |
| 162 | * Get the Zip file |
| 163 | * |
| 164 | * @access public |
| 165 | * @return binary string |
| 166 | */ |
| 167 | function get_zip() |
| 168 | { |
| 169 | if ($this->zipfile != '') |
| 170 | { |
| 171 | return $this->zipfile; |
| 172 | } |
| 173 | |
| 174 | $data = implode('', $this->zipdata); |
| 175 | $dir = implode('', $this->directory); |
| 176 | |
| 177 | $this->zipfile = $data.$dir."\x50\x4b\x05\x06\x00\x00\x00\x00" |
| 178 | .pack('v', sizeof($this->directory)) |
| 179 | .pack('v', sizeof($this->directory)) |
| 180 | .pack('V', strlen($dir)) |
| 181 | .pack('V', strlen($data)) |
| 182 | ."\x00\x00"; |
| 183 | |
| 184 | return $this->zipfile; |
| 185 | } |
| 186 | |
| 187 | // -------------------------------------------------------------------- |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 188 | |
admin | 6ae57e0 | 2006-10-01 07:59:45 +0000 | [diff] [blame] | 189 | /** |
| 190 | * Write File |
| 191 | * |
| 192 | * Lets you write a file |
| 193 | * |
| 194 | * @access public |
| 195 | * @param string the file name |
| 196 | * @param string the data to be encoded |
| 197 | * @return bool |
| 198 | */ |
| 199 | function write_file($filename, $data) |
| 200 | { |
| 201 | if ( ! ($fp = fopen($filename, "wb"))) |
| 202 | { |
| 203 | return FALSE; |
| 204 | } |
| 205 | |
| 206 | flock($fp, LOCK_EX); |
| 207 | fwrite($fp, $data); |
| 208 | flock($fp, LOCK_UN); |
| 209 | fclose($fp); |
| 210 | |
| 211 | return TRUE; |
| 212 | } |
| 213 | |
| 214 | // -------------------------------------------------------------------- |
| 215 | |
| 216 | /** |
| 217 | * Download |
| 218 | * |
| 219 | * |
| 220 | * @access public |
| 221 | * @param string the file name |
| 222 | * @param string the data to be encoded |
| 223 | * @return bool |
| 224 | */ |
| 225 | function download($filename, $data) |
| 226 | { |
| 227 | if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) |
| 228 | { |
| 229 | header('Content-Type: application/x-zip'); |
| 230 | header('Content-Disposition: inline; filename="'.$filename.'"'); |
| 231 | header('Expires: 0'); |
| 232 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
| 233 | header("Content-Transfer-Encoding: binary"); |
| 234 | header('Pragma: public'); |
| 235 | header("Content-Length: ".strlen($data)); |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | header('Content-Type: application/x-zip'); |
| 240 | header('Content-Disposition: attachment; filename="'.$filename.'"'); |
| 241 | header("Content-Transfer-Encoding: binary"); |
| 242 | header('Expires: 0'); |
| 243 | header('Pragma: no-cache'); |
| 244 | header("Content-Length: ".strlen($data)); |
| 245 | } |
| 246 | |
| 247 | echo $data; |
| 248 | } |
| 249 | |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 250 | } |
admin | ae58a5d | 2006-09-30 19:25:07 +0000 | [diff] [blame] | 251 | ?> |