00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 class ArchiveTar
00067 {
00068
00069 var $filename;
00070 var $isGzipped;
00071 var $tar_file;
00072
00073
00074 var $files;
00075 var $directories;
00076 var $numFiles;
00077 var $numDirectories;
00078
00079
00080
00081 function tar() {
00082 return true;
00083 }
00084
00085
00086
00087
00088
00089 function __computeUnsignedChecksum($bytestring)
00090 {
00091 $unsigned_chksum=0;
00092 for($i=0; $i<512; $i++)
00093 $unsigned_chksum += ord($bytestring[$i]);
00094 for($i=0; $i<8; $i++)
00095 $unsigned_chksum -= ord($bytestring[148 + $i]);
00096 $unsigned_chksum += ord(" ") * 8;
00097
00098 return $unsigned_chksum;
00099 }
00100
00101
00102
00103
00104 function __parseNullPaddedString($string)
00105 {
00106 $position = strpos($string,chr(0));
00107 return substr($string,0,$position);
00108 }
00109
00110
00111
00112
00113 function __parseTar()
00114 {
00115
00116 $this->numFiles=0;
00117 $tar_length = strlen($this->tar_file);
00118 $main_offset = 0;
00119 while($main_offset < $tar_length) {
00120
00121 if(substr($this->tar_file,$main_offset,512) == str_repeat(chr(0),512))
00122 break;
00123
00124
00125 $file_name = $this->__parseNullPaddedString(substr($this->tar_file,$main_offset,100));
00126
00127
00128 $file_mode = substr($this->tar_file,$main_offset + 100,8);
00129
00130
00131 $file_uid = octdec(substr($this->tar_file,$main_offset + 108,8));
00132
00133
00134 $file_gid = octdec(substr($this->tar_file,$main_offset + 116,8));
00135
00136
00137 $file_size = octdec(substr($this->tar_file,$main_offset + 124,12));
00138
00139
00140 $file_time = octdec(substr($this->tar_file,$main_offset + 136,12));
00141
00142
00143 $file_chksum = octdec(substr($this->tar_file,$main_offset + 148,6));
00144
00145
00146 $file_uname = $this->__parseNullPaddedString(substr($this->tar_file,$main_offset + 265,32));
00147
00148
00149 $file_gname = $this->__parseNullPaddedString(substr($this->tar_file,$main_offset + 297,32));
00150
00151
00152 if($this->__computeUnsignedChecksum(substr($this->tar_file,$main_offset,512)) != $file_chksum)
00153 return false;
00154
00155
00156 $file_contents = substr($this->tar_file,$main_offset + 512,$file_size);
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 if($file_size > 0) {
00170
00171 $this->numFiles++;
00172
00173
00174 $activeFile = &$this->files[];
00175
00176
00177 $activeFile["name"] = $file_name;
00178 $activeFile["mode"] = $file_mode;
00179 $activeFile["size"] = $file_size;
00180 $activeFile["time"] = $file_time;
00181 $activeFile["user_id"] = $file_uid;
00182 $activeFile["group_id"] = $file_gid;
00183 $activeFile["user_name"] = $file_uname;
00184 $activeFile["group_name"] = $file_gname;
00185 $activeFile["checksum"] = $file_chksum;
00186 $activeFile["file"] = $file_contents;
00187
00188 } else {
00189
00190 $this->numDirectories++;
00191
00192
00193 $activeDir = &$this->directories[];
00194
00195
00196 $activeDir["name"] = $file_name;
00197 $activeDir["mode"] = $file_mode;
00198 $activeDir["time"] = $file_time;
00199 $activeDir["user_id"] = $file_uid;
00200 $activeDir["group_id"] = $file_gid;
00201 $activeDir["user_name"] = $file_uname;
00202 $activeDir["group_name"] = $file_gname;
00203 $activeDir["checksum"] = $file_chksum;
00204 }
00205
00206
00207 $main_offset += 512 + (ceil($file_size / 512) * 512);
00208 }
00209
00210 return true;
00211 }
00212
00213
00214
00215
00216 function __readTar($filename='')
00217 {
00218
00219
00220
00221 if($this->tar_file[0] == chr(31) && $this->tar_file[1] == chr(139) && $this->tar_file[2] == chr(8)) {
00222 if(!function_exists("gzinflate"))
00223 return false;
00224
00225 $this->isGzipped = TRUE;
00226
00227 $this->tar_file = gzinflate(substr($this->tar_file,10,-4));
00228 }
00229
00230
00231 $this->__parseTar();
00232
00233 return true;
00234 }
00235
00236
00237
00238
00239 function __generateTAR()
00240 {
00241
00242 unset($this->tar_file);
00243
00244
00245 if($this->numDirectories > 0) {
00246 foreach($this->directories as $key => $information) {
00247 unset($header);
00248
00249
00250
00251 $header .= str_pad($information["name"],100,chr(0));
00252 $header .= str_pad(decoct($information["mode"]),7,"0",STR_PAD_LEFT) . chr(0);
00253 $header .= str_pad(decoct($information["user_id"]),7,"0",STR_PAD_LEFT) . chr(0);
00254 $header .= str_pad(decoct($information["group_id"]),7,"0",STR_PAD_LEFT) . chr(0);
00255 $header .= str_pad(decoct(0),11,"0",STR_PAD_LEFT) . chr(0);
00256 $header .= str_pad(decoct($information["time"]),11,"0",STR_PAD_LEFT) . chr(0);
00257 $header .= str_repeat(" ",8);
00258 $header .= "5";
00259 $header .= str_repeat(chr(0),100);
00260 $header .= str_pad("ustar",6,chr(32));
00261 $header .= chr(32) . chr(0);
00262 $header .= str_pad("",32,chr(0));
00263 $header .= str_pad("",32,chr(0));
00264 $header .= str_repeat(chr(0),8);
00265 $header .= str_repeat(chr(0),8);
00266 $header .= str_repeat(chr(0),155);
00267 $header .= str_repeat(chr(0),12);
00268
00269
00270 $checksum = str_pad(decoct($this->__computeUnsignedChecksum($header)),6,"0",STR_PAD_LEFT);
00271 for($i=0; $i<6; $i++) {
00272 $header[(148 + $i)] = substr($checksum,$i,1);
00273 }
00274 $header[154] = chr(0);
00275 $header[155] = chr(32);
00276
00277
00278 $this->tar_file .= $header;
00279 }
00280 }
00281
00282
00283 if($this->numFiles > 0)
00284 {
00285 foreach($this->files as $key => $information)
00286 {
00287 unset($header);
00288
00289
00290
00291 $header .= str_pad($information["name"],100,chr(0));
00292 $header .= str_pad(decoct($information["mode"]),7,"0",STR_PAD_LEFT) . chr(0);
00293 $header .= str_pad(decoct($information["user_id"]),7,"0",STR_PAD_LEFT) . chr(0);
00294 $header .= str_pad(decoct($information["group_id"]),7,"0",STR_PAD_LEFT) . chr(0);
00295 $header .= str_pad(decoct($information["size"]),11,"0",STR_PAD_LEFT) . chr(0);
00296 $header .= str_pad(decoct($information["time"]),11,"0",STR_PAD_LEFT) . chr(0);
00297 $header .= str_repeat(" ",8);
00298 $header .= "0";
00299 $header .= str_repeat(chr(0),100);
00300 $header .= str_pad("ustar",6,chr(32));
00301 $header .= chr(32) . chr(0);
00302 $header .= str_pad($information["user_name"],32,chr(0));
00303 $header .= str_pad($information["group_name"],32,chr(0));
00304 $header .= str_repeat(chr(0),8);
00305 $header .= str_repeat(chr(0),8);
00306 $header .= str_repeat(chr(0),155);
00307 $header .= str_repeat(chr(0),12);
00308
00309
00310 $checksum = str_pad(decoct($this->__computeUnsignedChecksum($header)),6,"0",STR_PAD_LEFT);
00311 for($i=0; $i<6; $i++)
00312 {
00313 $header[(148 + $i)] = substr($checksum,$i,1);
00314 }
00315 $header[154] = chr(0);
00316 $header[155] = chr(32);
00317
00318
00319 $file_contents = str_pad($information["file"],(ceil($information["size"] / 512) * 512),chr(0));
00320
00321
00322 $this->tar_file .= $header . $file_contents;
00323 }
00324 }
00325
00326
00327 $this->tar_file .= str_repeat(chr(0),512);
00328
00329 return true;
00330 }
00331
00332
00333
00334 function openTAR($value)
00335 {
00336
00337 unset($this->filename);
00338 unset($this->isGzipped);
00339 unset($this->tar_file);
00340 unset($this->files);
00341 unset($this->directories);
00342 unset($this->numFiles);
00343 unset($this->numDirectories);
00344
00345 $this->filename = 'none';
00346 $this->tar_file = $value;
00347
00348 $this->__readTar();
00349
00350 return true;
00351 }
00352
00353
00354
00355 function saveTar()
00356 {
00357 if(!$this->filename)
00358 return false;
00359
00360
00361 $this->toTar($this->filename,$this->isGzipped);
00362
00363 return true;
00364 }
00365
00366
00367
00368 function toTar($filename,$useGzip)
00369 {
00370 if(!$filename)
00371 return false;
00372
00373
00374 $this->__generateTar();
00375
00376
00377 if($useGzip) {
00378
00379 if(!function_exists("gzencode"))
00380 return false;
00381
00382 $file = gzencode($this->tar_file);
00383 } else {
00384 $file = $this->tar_file;
00385 }
00386
00387
00388 $fp = fopen($filename,"wb");
00389 fwrite($fp,$file);
00390 fclose($fp);
00391
00392 return true;
00393 }
00394 }
00395
00396 ?>