annotate e2gallerypro/e2upload/Backend/Assets/getid3/module.archive.tar.php @ 8:1b77e8657088 judyates

[svn r9] saving for transference to cd
author rlm
date Fri, 19 Mar 2010 07:17:36 -0400
parents 3f6b44aa6b35
children
rev   line source
rlm@3 1 <?php
rlm@3 2 // +----------------------------------------------------------------------+
rlm@3 3 // | PHP version 5 |
rlm@3 4 // +----------------------------------------------------------------------+
rlm@3 5 // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
rlm@3 6 // +----------------------------------------------------------------------+
rlm@3 7 // | This source file is subject to version 2 of the GPL license, |
rlm@3 8 // | that is bundled with this package in the file license.txt and is |
rlm@3 9 // | available through the world-wide-web at the following url: |
rlm@3 10 // | http://www.gnu.org/copyleft/gpl.html |
rlm@3 11 // +----------------------------------------------------------------------+
rlm@3 12 // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
rlm@3 13 // +----------------------------------------------------------------------+
rlm@3 14 // | Authors: James Heinrich <infoØgetid3*org> |
rlm@3 15 // | Allan Hansen <ahØartemis*dk> |
rlm@3 16 // +----------------------------------------------------------------------+
rlm@3 17 // | module.archive.tar.php |
rlm@3 18 // | module for analyzing TAR files |
rlm@3 19 // | dependencies: NONE |
rlm@3 20 // +----------------------------------------------------------------------+
rlm@3 21 // | Module originally written by Mike Mozolin <teddybearØmail*ru> |
rlm@3 22 // +----------------------------------------------------------------------+
rlm@3 23 //
rlm@3 24 // $Id: module.archive.tar.php,v 1.2 2006/11/02 10:48:00 ah Exp $
rlm@3 25
rlm@3 26
rlm@3 27
rlm@3 28 class getid3_tar extends getid3_handler
rlm@3 29 {
rlm@3 30
rlm@3 31 function Analyze() {
rlm@3 32
rlm@3 33 $info = &$this->getid3->info;
rlm@3 34
rlm@3 35 $info['fileformat'] = 'tar';
rlm@3 36
rlm@3 37 $fp = $this->getid3->fp;
rlm@3 38
rlm@3 39 fseek($fp, 0);
rlm@3 40
rlm@3 41 $unpack_header = 'a100fname/a8mode/a8uid/a8gid/a12size/a12mtime/a8chksum/a1typflag/a100lnkname/a6magic/a2ver/a32uname/a32gname/a8devmaj/a8devmin/a155/prefix';
rlm@3 42
rlm@3 43 $null_512k = str_repeat("\0", 512); // end-of-file marker
rlm@3 44
rlm@3 45 $already_warned = false;
rlm@3 46
rlm@3 47 while (!feof($fp)) {
rlm@3 48
rlm@3 49 $buffer = fread($fp, 512);
rlm@3 50
rlm@3 51 // check the block
rlm@3 52 $checksum = 0;
rlm@3 53 for ($i = 0; $i < 148; $i++) {
rlm@3 54 $checksum += ord(substr($buffer, $i, 1));
rlm@3 55 }
rlm@3 56 for ($i = 148; $i < 156; $i++) {
rlm@3 57 $checksum += ord(' ');
rlm@3 58 }
rlm@3 59 for ($i = 156; $i < 512; $i++) {
rlm@3 60 $checksum += ord(substr($buffer, $i, 1));
rlm@3 61 }
rlm@3 62 $attr = unpack($unpack_header, $buffer);
rlm@3 63 $name = trim(@$attr['fname']);
rlm@3 64 $mode = octdec(trim(@$attr['mode']));
rlm@3 65 $uid = octdec(trim(@$attr['uid']));
rlm@3 66 $gid = octdec(trim(@$attr['gid']));
rlm@3 67 $size = octdec(trim(@$attr['size']));
rlm@3 68 $mtime = octdec(trim(@$attr['mtime']));
rlm@3 69 $chksum = octdec(trim(@$attr['chksum']));
rlm@3 70 $typflag = trim(@$attr['typflag']);
rlm@3 71 $lnkname = trim(@$attr['lnkname']);
rlm@3 72 $magic = trim(@$attr['magic']);
rlm@3 73 $ver = trim(@$attr['ver']);
rlm@3 74 $uname = trim(@$attr['uname']);
rlm@3 75 $gname = trim(@$attr['gname']);
rlm@3 76 $devmaj = octdec(trim(@$attr['devmaj']));
rlm@3 77 $devmin = octdec(trim(@$attr['devmin']));
rlm@3 78 $prefix = trim(@$attr['prefix']);
rlm@3 79
rlm@3 80 // EOF Found
rlm@3 81 if (($checksum == 256) && ($chksum == 0)) {
rlm@3 82 break;
rlm@3 83 }
rlm@3 84
rlm@3 85 // Check if filename if 7bit as spec requires
rlm@3 86 if (!$already_warned) {
rlm@3 87 for ($i = 0; $i < strlen($name); $i++) {
rlm@3 88 if ($name{$i} < chr(32) || $name{$i} > chr(127)) {
rlm@3 89 $this->getid3->warning('Some filenames contains extended characters, which breaks the tar specifation. This is not uncommon, but you will have to handle the character encoding for filenames yourself.');
rlm@3 90 $already_warned = true;
rlm@3 91 break;
rlm@3 92 }
rlm@3 93 }
rlm@3 94 }
rlm@3 95
rlm@3 96 if ($prefix) {
rlm@3 97 $name = $prefix.'/'.$name;
rlm@3 98 }
rlm@3 99 if ((preg_match('#/$#', $name)) && !$name) {
rlm@3 100 $typeflag = 5;
rlm@3 101 }
rlm@3 102
rlm@3 103 // If it's the end of the tar-file...
rlm@3 104 if ($buffer == $null_512k) {
rlm@3 105 break;
rlm@3 106 }
rlm@3 107
rlm@3 108 // Protect against tar-files with garbage at the end
rlm@3 109 if ($name == '') {
rlm@3 110 break;
rlm@3 111 }
rlm@3 112
rlm@3 113 $info['tar']['file_details'][$name] = array (
rlm@3 114 'name' => $name,
rlm@3 115 'mode_raw' => $mode,
rlm@3 116 'mode' => getid3_tar::display_perms($mode),
rlm@3 117 'uid' => $uid,
rlm@3 118 'gid' => $gid,
rlm@3 119 'size' => $size,
rlm@3 120 'mtime' => $mtime,
rlm@3 121 'chksum' => $chksum,
rlm@3 122 'typeflag' => getid3_tar::get_flag_type($typflag),
rlm@3 123 'linkname' => $lnkname,
rlm@3 124 'magic' => $magic,
rlm@3 125 'version' => $ver,
rlm@3 126 'uname' => $uname,
rlm@3 127 'gname' => $gname,
rlm@3 128 'devmajor' => $devmaj,
rlm@3 129 'devminor' => $devmin
rlm@3 130 );
rlm@3 131
rlm@3 132 // Skip the next chunk
rlm@3 133 fseek($fp, $size, SEEK_CUR);
rlm@3 134
rlm@3 135 // Throw away padding
rlm@3 136 if ($size % 512) {
rlm@3 137 fseek($fp, 512 - $diff, SEEK_CUR);
rlm@3 138 }
rlm@3 139
rlm@3 140 }
rlm@3 141 return true;
rlm@3 142 }
rlm@3 143
rlm@3 144
rlm@3 145 // Parses the file mode to file permissions
rlm@3 146 public static function display_perms($mode) {
rlm@3 147
rlm@3 148 // Determine Type
rlm@3 149 if ($mode & 0x1000) {
rlm@3 150 $type='p'; // FIFO pipe
rlm@3 151 }
rlm@3 152 elseif ($mode & 0x2000) {
rlm@3 153 $type='c'; // Character special
rlm@3 154 }
rlm@3 155 elseif ($mode & 0x4000) {
rlm@3 156 $type='d'; // Directory
rlm@3 157 }
rlm@3 158 elseif ($mode & 0x6000) {
rlm@3 159 $type='b'; // Block special
rlm@3 160 }
rlm@3 161 elseif ($mode & 0x8000) {
rlm@3 162 $type='-'; // Regular
rlm@3 163 }
rlm@3 164 elseif ($mode & 0xA000) {
rlm@3 165 $type='l'; // Symbolic Link
rlm@3 166 }
rlm@3 167 elseif ($mode & 0xC000) {
rlm@3 168 $type='s'; // Socket
rlm@3 169 }
rlm@3 170 else {
rlm@3 171 $type='u'; // UNKNOWN
rlm@3 172 }
rlm@3 173
rlm@3 174 // Determine permissions
rlm@3 175 $owner['read'] = (($mode & 00400) ? 'r' : '-');
rlm@3 176 $owner['write'] = (($mode & 00200) ? 'w' : '-');
rlm@3 177 $owner['execute'] = (($mode & 00100) ? 'x' : '-');
rlm@3 178 $group['read'] = (($mode & 00040) ? 'r' : '-');
rlm@3 179 $group['write'] = (($mode & 00020) ? 'w' : '-');
rlm@3 180 $group['execute'] = (($mode & 00010) ? 'x' : '-');
rlm@3 181 $world['read'] = (($mode & 00004) ? 'r' : '-');
rlm@3 182 $world['write'] = (($mode & 00002) ? 'w' : '-');
rlm@3 183 $world['execute'] = (($mode & 00001) ? 'x' : '-');
rlm@3 184
rlm@3 185 // Adjust for SUID, SGID and sticky bit
rlm@3 186 if ($mode & 0x800) {
rlm@3 187 $owner['execute'] = ($owner['execute'] == 'x') ? 's' : 'S';
rlm@3 188 }
rlm@3 189 if ($mode & 0x400) {
rlm@3 190 $group['execute'] = ($group['execute'] == 'x') ? 's' : 'S';
rlm@3 191 }
rlm@3 192 if ($mode & 0x200) {
rlm@3 193 $world['execute'] = ($world['execute'] == 'x') ? 't' : 'T';
rlm@3 194 }
rlm@3 195
rlm@3 196 $s = sprintf('%1s', $type);
rlm@3 197 $s .= sprintf('%1s%1s%1s', $owner['read'], $owner['write'], $owner['execute']);
rlm@3 198 $s .= sprintf('%1s%1s%1s', $group['read'], $group['write'], $group['execute']);
rlm@3 199 $s .= sprintf('%1s%1s%1s'."\n", $world['read'], $world['write'], $world['execute']);
rlm@3 200
rlm@3 201 return $s;
rlm@3 202 }
rlm@3 203
rlm@3 204
rlm@3 205 // Converts the file type
rlm@3 206 public static function get_flag_type($typflag) {
rlm@3 207
rlm@3 208 static $flag_types = array (
rlm@3 209 '0' => 'LF_NORMAL',
rlm@3 210 '1' => 'LF_LINK',
rlm@3 211 '2' => 'LF_SYNLINK',
rlm@3 212 '3' => 'LF_CHR',
rlm@3 213 '4' => 'LF_BLK',
rlm@3 214 '5' => 'LF_DIR',
rlm@3 215 '6' => 'LF_FIFO',
rlm@3 216 '7' => 'LF_CONFIG',
rlm@3 217 'D' => 'LF_DUMPDIR',
rlm@3 218 'K' => 'LF_LONGLINK',
rlm@3 219 'L' => 'LF_LONGNAME',
rlm@3 220 'M' => 'LF_MULTIVOL',
rlm@3 221 'N' => 'LF_NAMES',
rlm@3 222 'S' => 'LF_SPARSE',
rlm@3 223 'V' => 'LF_VOLHDR'
rlm@3 224 );
rlm@3 225
rlm@3 226 return @$flag_types[$typflag];
rlm@3 227 }
rlm@3 228
rlm@3 229 }
rlm@3 230
rlm@3 231 ?>