cvs: pear /PHP_Archive Archive.php package.php package.xml /PHP_Archive/Archive Creator.php Manager.php /PHP_Archive/tests eof.phpt
filenotfound_phar.phpt filenotfoundtest.php normalstat.phpt normalstat_phar.phpt seek.phpt streamstat.phpt streamstat_phar.phpt
/PHP_Archive/tests/filenotfound filenotfound.phar makefilenotfoundphar.php.inc /PHP_Archive/tests/longfilename longphar.phar makelongphar.php.inc
/PHP_Archive/tests/opendir makeopendirphar.php.inc opendir.phar /PHP_Archive/tests/require_once makerequire_oncephar.php.inc require_once.phar

From: Date: Tue, 06 Feb 2007 03:34:23 +0000
Subject: cvs: pear /PHP_Archive Archive.php package.php package.xml /PHP_Archive/Archive Creator.php Manager.php /PHP_Archive/tests eof.phpt
filenotfound_phar.phpt filenotfoundtest.php normalstat.phpt normalstat_phar.phpt seek.phpt streamstat.phpt streamstat_phar.phpt
/PHP_Archive/tests/filenotfound filenotfound.phar makefilenotfoundphar.php.inc /PHP_Archive/tests/longfilename longphar.phar makelongphar.php.inc
/PHP_Archive/tests/opendir makeopendirphar.php.inc opendir.phar /PHP_Archive/tests/require_once makerequire_oncephar.php.inc require_once.phar
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-44547@lists.php.net to get a copy of this message
cellog Tue Feb 6 03:34:23 2007 UTC Modified files: /pear/PHP_Archive Archive.php package.php package.xml /pear/PHP_Archive/Archive Creator.php Manager.php /pear/PHP_Archive/tests eof.phpt filenotfound_phar.phpt filenotfoundtest.php normalstat.phpt normalstat_phar.phpt seek.phpt streamstat.phpt streamstat_phar.phpt /pear/PHP_Archive/tests/filenotfound filenotfound.phar makefilenotfoundphar.php.inc /pear/PHP_Archive/tests/longfilename longphar.phar makelongphar.php.inc /pear/PHP_Archive/tests/opendir makeopendirphar.php.inc opendir.phar /pear/PHP_Archive/tests/require_once makerequire_oncephar.php.inc require_once.phar Log: sync with pecl/phar

http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/Archive.php?r1=1.35&r2=1.36&diff_format=u Index: pear/PHP_Archive/Archive.php diff -u pear/PHP_Archive/Archive.php:1.35 pear/PHP_Archive/Archive.php:1.36 --- pear/PHP_Archive/Archive.php:1.35 Sat Jan 6 21:58:24 2007 +++ pear/PHP_Archive/Archive.php Tue Feb 6 03:34:22 2007 @@ -6,10 +6,6 @@ * @category PHP */ /** - * Flag for GZ compression - */ -define('PHP_ARCHIVE_COMPRESSED', 1); -/** * PHP_Archive Class (implements .phar) * * PHAR files a singular archive from which an entire application can run. @@ -23,13 +19,15 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version $Id: Archive.php,v 1.35 2007/01/06 21:58:24 cellog Exp $ + * @version $Id: Archive.php,v 1.36 2007/02/06 03:34:22 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + const GZ = 0x00001000; + const BZ2 = 0x00002000; /** * Whether this archive is compressed with zlib * @@ -74,13 +72,20 @@ * </code> * then the alias is "PEAR.phar" * - * Information stored is the actual file name, a boolean indicating whether - * this .phar is compressed with zlib, and the precise offset of internal files + * Information stored is a boolean indicating whether this .phar is compressed + * with zlib, another for bzip2, phar-specific meta-data, and + * the precise offset of internal files * within the .phar, used with the {@link $_manifest} to load actual file contents * @var array */ private static $_pharMapping = array(); /** + * Map real file paths to alias used + * + * @var array + */ + private static $_pharFiles = array(); + /** * File listing for the .phar * * The manifest is indexed per phar. @@ -110,6 +115,30 @@ private $_basename; /** + * Allows loading an external Phar archive without include()ing it + * + * @param string $file + * @throws Exception + */ + public static final function loadPhar($file) + { + $file = realpath($file); + if ($file) { + $fp = fopen($file, 'rb'); + $buffer = ''; + while (!$found && !feof($fp)) { + $buffer .= fread($fp, 8192); + // don't break phars + if ($pos = strpos($buffer, '__HALT_COMPI' . 'LER();')) { + fclose($fp); + return self::_mapPhar($file, $pos); + } + } + fclose($fp); + } + } + + /** * Map a full real file path to an alias used to refer to the .phar * * This function can only be called from the initialization of the .phar itself. @@ -124,55 +153,157 @@ */ public static final function mapPhar($file, $dataoffset) { + try { + // this ensures that this is safe + if (!in_array($file, get_included_files())) { + die('SECURITY ERROR: PHP_Archive::mapPhar can only be called from within ' . + 'the phar that initiates it'); + } + self::_mapPhar($file, $dataoffset); + } catch (Exception $e) { + die($e->getMessage()); + } + } + + /** + * Sub-function, allows recovery from errors + * + * @param unknown_type $file + * @param unknown_type $dataoffset + */ + private static function _mapPhar($file, $dataoffset) + { $file = realpath($file); - // this ensures that this is safe - if (!in_array($file, get_included_files())) { - die('SECURITY ERROR: PHP_Archive::mapPhar can only be called from within ' . - 'the phar that initiates it'); + if (isset(self::$_manifest[$file])) { + return; } if (!is_array(self::$_pharMapping)) { self::$_pharMapping = array(); } - if (!isset(self::$_manifest[$file])) { - $fp = fopen($file, 'rb'); - // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, $dataoffset); - $manifest_length = unpack('Vlen', fread($fp, 4)); - $manifest = ''; - $last = '1'; - while (strlen($last) && strlen($manifest) < $manifest_length['len']) { - $read = 8192; - if ($manifest_length['len'] - strlen($manifest) < 8192) { - $read = $manifest_length['len'] - strlen($manifest); - } - $last = fread($fp, $read); - $manifest .= $last; - } - if (strlen($manifest) < $manifest_length['len']) { - die('ERROR: manifest length read was "' . strlen($manifest) .'" should be "' . - $manifest_length['len'] . '"'); - } - $info = self::_unserializeManifest($manifest); - if (!$info) { - die; // error declared in unserializeManifest - } + $fp = fopen($file, 'rb'); + // seek to __HALT_COMPILER_OFFSET__ + fseek($fp, $dataoffset); + $manifest_length = unpack('Vlen', fread($fp, 4)); + $manifest = ''; + $last = '1'; + while (strlen($last) && strlen($manifest) < $manifest_length['len']) { + $read = 8192; + if ($manifest_length['len'] - strlen($manifest) < 8192) { + $read = $manifest_length['len'] - strlen($manifest); + } + $last = fread($fp, $read); + $manifest .= $last; + } + if (strlen($manifest) < $manifest_length['len']) { + throw new Exception('ERROR: manifest length read was "' . + strlen($manifest) .'" should be "' . + $manifest_length['len'] . '"'); + } + $info = self::_unserializeManifest($manifest); + if ($info['alias']) { $alias = $info['alias']; - self::$_manifest[$file] = $info['manifest']; - $compressed = $info['compressed']; - self::$_fileStart[$file] = ftell($fp); - fclose($fp); + $explicit = true; + } else { + $alias = $file; + $explicit = false; } - if ($compressed) { + self::$_manifest[$file] = $info['manifest']; + $compressed = $info['compressed']; + self::$_fileStart[$file] = ftell($fp); + fclose($fp); + if ($compressed & 0x00001000) { if (!function_exists('gzinflate')) { - die('Error: zlib extension is not enabled - gzinflate() function needed' . + throw new Exception('Error: zlib extension is not enabled - gzinflate() function needed' . + ' for compressed .phars'); + } + } + if ($compressed & 0x00002000) { + if (!function_exists('bzdecompress')) { + throw new Exception('Error: bzip2 extension is not enabled - bzdecompress() function needed' . ' for compressed .phars'); } } if (isset(self::$_pharMapping[$alias])) { - die('ERROR: PHP_Archive::mapPhar has already been called for alias "' . + throw new Exception('ERROR: PHP_Archive::mapPhar has already been called for alias "' . $alias . '" cannot re-alias to "' . $file . '"'); } - self::$_pharMapping[$alias] = array($file, $compressed, $dataoffset); + self::$_pharMapping[$alias] = array($file, $compressed, $dataoffset, $explicit); + self::$_pharFiles[$file] = $alias; + } + + /** + * extract the manifest into an internal array + * + * @param string $manifest + * @return false|array + */ + private static function _unserializeManifest($manifest) + { + // retrieve the number of files in the manifest + $info = unpack('V', substr($manifest, 0, 4)); + $apiver = substr($manifest, 4, 2); + $apiver = bin2hex($apiver); + $apiver_dots = hexdec($apiver[0]) . '.' . hexdec($apiver[1]) . '.' . hexdec($apiver[2]); + $majorcompat = hexdec($apiver[0]); + $calcapi = explode('.', '@API-VER@'); + if ($calcapi[0] != $majorcompat) { + throw new Exception('Phar is incompatible API version ' . $apiver_dots . ', but ' . + 'PHP_Archive is API version @API-VER@'); + } + if ($calcapi[0] === '0') { + if ('@API-VER@' != $apiver_dots) { + throw new Exception('Phar is API version ' . $apiver_dots . + ', but PHP_Archive is API version @API-VER@', E_USER_ERROR); + } + } + $flags = unpack('V', substr($manifest, 6, 4)); + $ret = array('compressed' => $flags & 0x00003000); + // signature is not verified by default in PHP_Archive, phar is better + $ret['hassignature'] = $flags & 0x00010000; + $aliaslen = unpack('V', substr($manifest, 10, 4)); + if ($aliaslen) { + $ret['alias'] = substr($manifest, 14, $aliaslen[1]); + } else { + $ret['alias'] = false; + } + $manifest = substr($manifest, 14 + $aliaslen[1]); + $metadatalen = unpack('V', substr($manifest, 0, 4)); + if ($metadatalen[1]) { + $ret['metadata'] = unserialize(substr($manifest, 4, $metadatalen[1])); + $manifest = substr($manifest, 4 + $metadatalen[1]); + } else { + $ret['metadata'] = null; + $manifest = substr($manifest, 4); + } + $offset = 0; + $start = 0; + for ($i = 0; $i < $info[1]; $i++) { + // length of the file name + $len = unpack('V', substr($manifest, $start, 4)); + $start += 4; + // file name + $savepath = substr($manifest, $start, $len[1]); + $start += $len[1]; + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = compressed filesize + // 3 = crc32 + // 4 = flags + // 5 = metadata length + $ret['manifest'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($manifest, $start, 24))); + $ret['manifest'][$savepath][3] = sprintf('%u', $ret['manifest'][$savepath][3]); + if ($ret['manifest'][$savepath][5]) { + $ret['manifest'][$savepath][6] = unserialize(substr($manifest, $start + 24, + $ret['manifest'][$savepath][5])); + } else { + $ret['manifest'][$savepath][6] = null; + } + $ret['manifest'][$savepath][7] = $offset; + $offset += $ret['manifest'][$savepath][2]; + $start += 24 + $ret['manifest'][$savepath][5]; + } + return $ret; } /** @@ -234,7 +365,7 @@ $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][2]; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][5]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][7]); } } @@ -263,8 +394,10 @@ } } @fclose($this->fp); - if (self::$_manifest[$this->_archiveName][$path][4] & PHP_ARCHIVE_COMPRESSED) { + if (self::$_manifest[$this->_archiveName][$path][4] & self::GZ) { $data = gzinflate($data); + } elseif (self::$_manifest[$this->_archiveName][$path][4] & self::BZ2) { + $data = bzdecompress($data); } if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if (strlen($data) != $this->currentStat[7]) { @@ -284,27 +417,79 @@ } /** + * Parse urls like phar:///fullpath/to/my.phar/file.txt + * + * @param string $file + * @return false|array + */ + static protected function parseUrl($file) + { + if (substr($file, 0, 7) != 'phar://') { + return false; + } + $file = substr($file, 7); + + $ret = array('scheme' => 'phar'); + $pos_p = strpos($file, '.phar.php'); + $pos_z = strpos($file, '.phar.gz'); + $pos_b = strpos($file, '.phar.bz2'); + if ($pos_p) { + if ($pos_z) { + return false; + } + $ret['host'] = substr($file, 0, $pos_p + strlen('.phar.php')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif ($pos_z) { + $ret['host'] = substr($file, 0, $pos_z + strlen('.phar.gz')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif ($pos_b) { + $ret['host'] = substr($file, 0, $pos_z + strlen('.phar.bz2')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif (($pos_p = strpos($file, ".phar")) !== false) { + $ret['host'] = substr($file, 0, $pos_p + strlen('.phar')); + $ret['path'] = substr($file, strlen($ret['host'])); + } else { + return false; + } + if (!$ret['path']) { + $ret['path'] = '/'; + } + return $ret; + } + + /** * Locate the .phar archive in the include_path and detect the file to open within * the archive. * - * Possible parameters are phar://filename_within_phar.ext or - * phar://pharname.phar/filename_within_phar.ext - * - * phar://filename_within_phar.ext will simply use the last .phar opened. + * Possible parameters are phar://pharname.phar/filename_within_phar.ext * @param string a file within the archive * @return string the filename within the .phar to retrieve */ public function initializeStream($file) { - $info = parse_url($file); - if (!isset($info['host']) || !count(self::$_pharMapping)) { + $info = @parse_url($file); + if (!$info) { + $info = self::parseUrl($file); + } + if (!$info) { + return false; + } + if (!isset($info['host'])) { // malformed internal file return false; } + if (!isset(self::$_pharFiles[$info['host']]) && + !isset(self::$_pharMapping[$info['host']])) { + try { + self::loadPhar($info['host']); + // use alias from here out + $info['host'] = self::$_pharFiles[$info['host']]; + } catch (Exception $e) { + return false; + } + } if (!isset($info['path'])) { - // last opened phar is requested - $info['path'] = $info['host']; - $info['host'] = ''; + return false; } elseif (strlen($info['path']) > 1) { $info['path'] = substr($info['path'], 1); } @@ -312,73 +497,16 @@ $this->_basename = $info['host']; $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; - } else { - // no such phar has been included, or last opened phar is requested - $pharinfo = end(self::$_pharMapping); - $this->_basename = key(self::$_pharMapping); - $this->_archiveName = $pharinfo[0]; - $this->_compressed = $pharinfo[1]; + } elseif (isset(self::$_pharFiles[$info['host']])) { + $this->_archiveName = $info['host']; + $this->_basename = self::$_pharFiles[$info['host']]; + $this->_compressed = self::$_pharMapping[$this->_basename][1]; } $file = $info['path']; return $file; } /** - * extract the manifest into an internal array - * - * @param string $manifest - * @return false|array - */ - private static function _unserializeManifest($manifest) - { - // retrieve the number of files in the manifest - $info = unpack('V', substr($manifest, 0, 4)); - $apiver = substr($manifest, 4, 2); - $apiver = bin2hex($apiver); - $apiver_dots = hexdec($apiver[0]) . '.' . hexdec($apiver[1]) . '.' . hexdec($apiver[2]); - $majorcompat = hexdec($apiver[0]); - $calcapi = explode('.', '@API-VER@'); - if ($calcapi[0] != $majorcompat) { - trigger_error('Phar is incompatible API version ' . $apiver_dots . ', but ' . - 'PHP_Archive is API version @API-VER@'); - return false; - } - if ($calcapi[0] === '0') { - if ('@API-VER@' != $apiver_dots) { - trigger_error('Phar is API version ' . $apiver_dots . - ', but PHP_Archive is API version @API-VER@', E_USER_ERROR); - return false; - } - } - $ret = array('compressed' => $apiver[3]); - $aliaslen = unpack('V', substr($manifest, 6, 4)); - $ret['alias'] = substr($manifest, 10, $aliaslen[1]); - $manifest = substr($manifest, 10 + $aliaslen[1]); - $offset = 0; - $start = 0; - for ($i = 0; $i < $info[1]; $i++) { - // length of the file name - $len = unpack('V', substr($manifest, $start, 4)); - $start += 4; - // file name - $savepath = substr($manifest, $start, $len[1]); - $start += $len[1]; - // retrieve manifest data: - // 0 = uncompressed file size - // 1 = timestamp of when file was added to phar - // 2 = compressed filesize - // 3 = crc32 - // 4 = flags - $ret['manifest'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ce', substr($manifest, $start, 17))); - $ret['manifest'][$savepath][3] = sprintf('%u', $ret['manifest'][$savepath][3]); - $ret['manifest'][$savepath][5] = $offset; - $offset += $ret['manifest'][$savepath][2]; - $start += 17; - } - return $ret; - } - - /** * Open the requested file - PHP streams API * * @param string $file String provided by the Stream wrapper @@ -550,7 +678,15 @@ */ public function dir_opendir($path) { - $info = parse_url($path); + $info = @parse_url($path); + if (!$info) { + $info = self::parseUrl($path); + if (!$info) { + trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', + E_USER_ERROR); + return false; + } + } $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/package.php?r1=1.22&r2=1.23&diff_format=u Index: pear/PHP_Archive/package.php diff -u pear/PHP_Archive/package.php:1.22 pear/PHP_Archive/package.php:1.23 --- pear/PHP_Archive/package.php:1.22 Sat Jan 6 21:58:24 2007 +++ pear/PHP_Archive/package.php Tue Feb 6 03:34:22 2007 @@ -2,15 +2,12 @@ require_once 'PEAR/PackageFileManager.php'; require_once 'PEAR/PackageFileManager2.php'; PEAR::setErrorHAndling(PEAR_ERROR_DIE); -$version = '0.9.2'; -$apiversion = '0.8.0'; +$version = '0.10.0'; +$apiversion = '1.0.0'; $notes = ' -another major 32-bit/64-bit issue in PHP 5.1 where crc32() returns different values -was causing some phars to fail. +Update to match phar extension -This is *not* fixed in PHP 5.2, and won\'t be. This only affects CRCs. The workaround -found is to sprintf("%u", crc32($data)) -'; +Final API for 1.0.0 is set'; $package = PEAR_PackageFileManager2::importOptions(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'package.xml', @@ -28,7 +25,7 @@ $package->setReleaseVersion($version); $package->setAPIVersion($apiversion); $package->setReleaseStability('alpha'); -$package->setAPIStability('alpha'); +$package->setAPIStability('stable'); $package->setNotes($notes); $package->clearDeps(); $package->addPackageDepWithChannel('required', 'PEAR', 'pear.php.net', '1.4.3'); http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/package.xml?r1=1.25&r2=1.26&diff_format=u Index: pear/PHP_Archive/package.xml diff -u pear/PHP_Archive/package.xml:1.25 pear/PHP_Archive/package.xml:1.26 --- pear/PHP_Archive/package.xml:1.25 Sat Jan 6 21:58:24 2007 +++ pear/PHP_Archive/package.xml Tue Feb 6 03:34:22 2007 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="UTF-8"?> -<package packagerversion="1.5.0RC2" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> +<package packagerversion="1.5.0" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> <name>PHP_Archive</name> <channel>pear.php.net</channel> <summary>Create and Use PHP Archive files</summary> @@ -16,24 +16,21 @@ <email>cellog@php.net</email> <active>yes</active> </lead> - <date>2007-01-06</date> - <time>15:58:01</time> + <date>2007-02-05</date> + <time>12:40:40</time> <version> - <release>0.9.2</release> - <api>0.8.0</api> + <release>0.10.0</release> + <api>1.0.0</api> </version> <stability> <release>alpha</release> - <api>alpha</api> + <api>stable</api> </stability> <license uri="http://www.php.net/license">PHP License</license> <notes> -another major 32-bit/64-bit issue in PHP 5.1 where crc32() returns different values -was causing some phars to fail. +Update to match phar extension -This is *not* fixed in PHP 5.2, and won&apos;t be. This only affects CRCs. The workaround -found is to sprintf(&quot;%u&quot;, crc32($data)) - </notes> +Final API for 1.0.0 is set</notes> <contents> <dir baseinstalldir="PHP" name="/"> <dir name="Archive"> @@ -116,6 +113,22 @@ <changelog> <release> <version> + <release>0.10.0</release> + <api>1.0.0</api> + </version> + <stability> + <release>alpha</release> + <api>stable</api> + </stability> + <date>2007-02-05</date> + <license uri="http://www.php.net/license">PHP License</license> + <notes> +Update to match phar extension + +Final API for 1.0.0 is set</notes> + </release> + <release> + <version> <release>0.9.2</release> <api>0.8.0</api> </version> @@ -125,13 +138,11 @@ </stability> <date>2007-01-06</date> <license uri="http://www.php.net/license">PHP License</license> - <notes> -another major 32-bit/64-bit issue in PHP 5.1 where crc32() returns different values + <notes>another major 32-bit/64-bit issue in PHP 5.1 where crc32() returns different values was causing some phars to fail. This is *not* fixed in PHP 5.2, and won&apos;t be. This only affects CRCs. The workaround -found is to sprintf(&quot;%u&quot;, crc32($data)) - </notes> +found is to sprintf(&quot;%u&quot;, crc32($data))</notes> </release> <release> <version> http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/Archive/Creator.php?r1=1.34&r2=1.35&diff_format=u Index: pear/PHP_Archive/Archive/Creator.php diff -u pear/PHP_Archive/Archive/Creator.php:1.34 pear/PHP_Archive/Archive/Creator.php:1.35 --- pear/PHP_Archive/Archive/Creator.php:1.34 Sat Dec 16 04:22:20 2006 +++ pear/PHP_Archive/Archive/Creator.php Tue Feb 6 03:34:22 2007 @@ -9,6 +9,7 @@ * Needed for file manipulation */ require_once 'System.php'; +require_once 'PHP/Archive.php'; /** * PHP_Archive Class creator (implements .phar) * @@ -23,11 +24,11 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version $Id: Creator.php,v 1.34 2006/12/16 04:22:20 cellog Exp $ + * @version $Id: Creator.php,v 1.35 2007/02/06 03:34:22 cellog Exp $ * @package PHP_Archive * @category PHP */ -class PHP_Archive_Creator implements ArrayAccess +class PHP_Archive_Creator { /** * @var string The Archive Filename @@ -55,6 +56,12 @@ protected $compress = false; /** + * Phar bitmapped flags + * + * @var int + */ + protected $flags = 0; + /** * @var boolean Whether or not to collapse (remove whitespace/comments) */ protected $collapse = false; @@ -72,6 +79,12 @@ protected $manifest = array(); /** + * Used to save Phar-specific metadata + * @var mixed + */ + protected $metadata = null; + + /** * A list of custom callbacks that should be used for manipulating file contents * prior to adding to the phar. * @@ -79,36 +92,6 @@ */ private $_magicRequireCallbacks = array(); - /**#@+ - * ArrayAccess methods - */ - public function offsetExists($offset) - { - return is_string($offset) && isset($this->manifest[$offset]); - } - - public function offsetGet($offset) - { - if (is_string($offset) && isset($this->manifest[$offset])) { - return file_get_contents($this->manifest[$offset]['tempfile']); - } - } - - public function offsetSet($offset, $value) - { - if (is_string($offset)) { - $this->addString($value, $offset, false); - } - } - - public function offsetUnset($offset) - { - if (is_string($offset) && isset($this->manifest[$offset])) { - unset($this->manifest[$offset]); - } - } - - /**#@-*/ /** * @param string */ @@ -138,7 +121,8 @@ * use this option for libraries * @param string $alias alias name like "go-pear.phar" to be used for opening * files from this phar - * @param boolean $compress Whether to compress the files or not (will cause slowdown!) + * @param string|false $compress Whether to compress the files or not (will cause slowdown!) + * use 'gz' for zlib compression, 'bz2' for bzip2 compression * @param bool $relyOnPhar if true, then a slim, phar extension-dependent .phar will be * created * @param bool $collapse Remove whitespace and comments from PHP_Archive class @@ -157,7 +141,7 @@ $contents = trim(str_replace(array('<?php', '?>'), array('', ''), $contents)); // make sure .phars added to CVS don't get checksum errors because of CVS tags $contents = str_replace('* @version $Id', '* @version Id', $contents); - $unpack_code = "<?php #PHP_ARCHIVE_HEADER-@API-VER@ + $unpack_code = "<?php error_reporting(E_ALL); if (function_exists('mb_internal_encoding')) { mb_internal_encoding('ASCII'); @@ -169,12 +153,13 @@ $unpack_code .= $contents; $unpack_code .= "} if (!class_exists('Phar')) { - if (!in_array('phar', stream_get_wrappers())) { - stream_wrapper_register('phar', 'PHP_Archive'); - } PHP_Archive::mapPhar(__FILE__, __COMPILER_HALT_OFFSET__); } else { - Phar::mapPhar(); + try { + Phar::mapPhar(); + } catch (Exception \$e) { + echo \$e->getMessage(); + } } if (class_exists('PHP_Archive') && !in_array('phar', stream_get_wrappers())) { stream_wrapper_register('phar', 'PHP_Archive'); @@ -184,7 +169,12 @@ $unpack_code .= "if (!extension_loaded('phar')) {"; $unpack_code .= 'die("Error - phar extension not loaded"); } -Phar::mapPhar();'; +try { + Phar::mapPhar(); +} catch (Exception \$e) { + echo \$e->getMessage(); +} +'; } $unpack_code .= <<<PHP @ini_set('memory_limit', -1); @@ -201,6 +191,11 @@ file_put_contents($this->temp_path . DIRECTORY_SEPARATOR . 'loader.php', $unpack_code); } + public function setPharMetadata($metadata) + { + $this->_metadata = $metadata; + } + /** * Specify a custom "magic require" callback for processing file contents. * @@ -221,12 +216,14 @@ * * @param string $file Path of the File to add * @param string $save_path The save location of the file in the archive + * @param false $magicrequire unused, set this to false + * @param mixed $metadata Any file-specific metadata to save * @return boolean */ - public function addFile($file, $save_path, $magicrequire = false) + public function addFile($file, $save_path, $magicrequire = false, $metadata = null) { - return $this->addString(file_get_contents($file), $save_path, $magicrequire); + return $this->addString(file_get_contents($file), $save_path, $magicrequire, $metadata); } /** @@ -237,7 +234,8 @@ * @return boolean */ - public function addString($file_contents, $save_path, $magicrequire = false) + public function addString($file_contents, $save_path, $magicrequire = false, + $metadata = null) { $save_path = self::processFile($save_path); if (count($this->_magicRequireCallbacks)) { @@ -256,8 +254,15 @@ $crc32 = crc32($file_contents); // save crc32 of file and the uncompressed file size, so we // can do a sanity check on the file when opening it from the phar - $file_contents = - ($this->compress ? gzdeflate($file_contents, 9) : $file_contents); + if ($this->compress) { + if ($this->compress == 'gz') { + $this->flags |= PHP_Archive::GZ; + $file_contents = gzdeflate($file_contents, 9); + } elseif ($this->compress == 'bz2') { + $this->flags |= PHP_Archive::BZ2; + $file_contents = bzcompress($file_contents, 9); + } + } System::mkdir(array('-p', dirname($this->temp_path . DIRECTORY_SEPARATOR . 'contents' . DIRECTORY_SEPARATOR . $save_path))); if (file_exists($this->temp_path . DIRECTORY_SEPARATOR . 'contents' . @@ -266,6 +271,10 @@ } file_put_contents($this->temp_path . DIRECTORY_SEPARATOR . 'contents' . DIRECTORY_SEPARATOR . $save_path, $file_contents); + $flags = 0; + if ($this->compress) { + $flags |= ($this->compress == 'gz') ? 0x00001000 : 0x00002000; + } $this->manifest[$save_path] = array( 'tempfile' => $this->temp_path . DIRECTORY_SEPARATOR . 'contents' . @@ -273,7 +282,8 @@ 'originalsize' => $size, 'actualsize' => strlen($file_contents), 'crc32' => $crc32, - 'flags' => $this->compress ? 1 : 0); // gz compression = 0x1 + 'flags' => $flags, + 'metadata' => $metadata); } /** @@ -609,7 +619,8 @@ time(), // save time = 1 $info['actualsize'], // actual size in the .phar = 2 $info['crc32'], // crc32 = 3 - $info['flags']); // flags = 4 + $info['flags'], // flags = 4 + $info['metadata']); // metadata = 5 $offset += $info['actualsize']; } $manifest = $this->serializeManifest($manifest); @@ -645,7 +656,14 @@ $apiver = chr(((int) ((int) $apiver[0]) << 4) + ((int) $apiver[1])) . chr((int)($apiver[2] << 4) + ($this->compress ? 0x1 : 0)); $ret = $apiver; + $ret .= pack('V', $this->flags); $ret .= pack('V', strlen($this->alias)) . $this->alias; + if ($this->metadata === null) { + $ret .= pack('V', 0); + } else { + $metadata = serialize($this->metadata); + $ret .= pack('V', strlen($metadata)) . $metadata; + } foreach ($manifest as $savepath => $info) { // save the string length, then the string, then this info // uncompressed file size @@ -653,8 +671,16 @@ // compressed file size // crc32 // flags + // metadata + $metadata = array_pop($info); $ret .= pack('V', strlen($savepath)) . $savepath . call_user_func_array('pack', - array_merge(array('VVVVC'), $info)); + array_merge(array('VVVVV'), $info)); + if ($metadata === null) { + $ret .= pack('V', 0); + } else { + $metadata = serialize($metadata); + $ret .= pack('V', strlen($metadata)) . $metadata; + } } // save the size of the manifest return pack('VV', strlen($ret) + 4, count($manifest)) . $ret; http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/Archive/Manager.php?r1=1.11&r2=1.12&diff_format=u Index: pear/PHP_Archive/Archive/Manager.php diff -u pear/PHP_Archive/Archive/Manager.php:1.11 pear/PHP_Archive/Archive/Manager.php:1.12 --- pear/PHP_Archive/Archive/Manager.php:1.11 Wed Apr 19 21:57:31 2006 +++ pear/PHP_Archive/Archive/Manager.php Tue Feb 6 03:34:22 2007 @@ -9,29 +9,31 @@ * Needed for file manipulation */ require_once 'System.php'; +require_once 'PHP/Archive/Exception.php'; -define('PHP_ARCHIVE_MANAGER_COMPRESSED_GZ', 0x1); - /** * * @copyright Copyright ? Gregory Beaver * @author Greg Beaver <cellog@php.net> - * @version $Id: Manager.php,v 1.11 2006/04/19 21:57:31 cellog Exp $ + * @version $Id: Manager.php,v 1.12 2007/02/06 03:34:22 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive_Manager { + const GZ = 0x00001000; + const BZ2 = 0x00002000; private $_alias; private $_archiveName; private $_apiVersion; - private $_compressed; - private $_knownAPIVersions = array('0.8.0'); + private $_flags; + private $_knownAPIVersions = array('1.0.0'); private $_manifest; private $_fileStart; private $_manifestSize; private $_html; + private $_metadata; /** * Locate the .phar archive in the include_path and detect the file to open within * the archive. @@ -55,7 +57,6 @@ */ public function validate($strict = false) { - require_once 'PHP/Archive/Exception.php'; $errors = array(); $warnings = array(); $fp = fopen($this->_archiveName, 'rb'); @@ -79,16 +80,13 @@ $this->_version = $version; } // seek to __HALT_COMPILER_OFFSET__ - $prev = ''; $found = false; - while (!feof($fp) && false != ($next = fread($fp, 8092))) { - $prev .= $next; - if (false != ($t = strpos($prev, '__HALT_COMPILER();'))) { - fseek($fp, $t - strlen($prev) + strlen('__HALT_COMPILER();'), SEEK_CUR); + while (!feof($fp) && false != ($next = fread($fp, 8192))) { + if (false != ($t = strpos($next, '__HALT_COMPILER();'))) { + fseek($fp, $t - strlen($next) + strlen('__HALT_COMPILER();'), SEEK_CUR); $found = true; break; } - $prev = $next; } if (!$found) { throw new PHP_Archive_ExceptionExtended(PHP_Archive_ExceptionExtended::NOTPHAR, @@ -110,7 +108,7 @@ $manifest = fread($fp, $manifest_length); // retrieve the number of files in the manifest $info = unpack('V', substr($manifest, 0, 4)); - if ($info[1] * 17 > $manifest_length) { + if ($info[1] * 24 > $manifest_length) { $errors[] = new PHP_Archive_ExceptionExtended( PHP_Archive_ExceptionExtended::MANIFESTENTRIESOVERFLOW,array( 'archive' => $this->_archiveName)); @@ -141,6 +139,16 @@ 'archive' => $this->_archiveName)); throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); } + // get flags + $flags = unpack('V', substr($manifest, 0, 4)); + $this->_flags = $flags[1]; + $manifest = substr($manifest, 4); + if (strlen($manifest) < 4) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESUNDERFLOW, array( + 'archive' => $this->_archiveName)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } // get alias $aliaslen = unpack('V', substr($manifest, 0, 4)); $aliaslen = $aliaslen[1]; @@ -153,6 +161,26 @@ } $this->_alias = substr($manifest, 0, $aliaslen); $manifest = substr($manifest, $aliaslen); + // phar metadata + if (strlen($manifest) < 4) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESUNDERFLOW, array( + 'archive' => $this->_archiveName)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } + $metadatalen = unpack('V', substr($manifest, 0, 4)); + $metadatalen = $metadatalen[1]; + $manifest = substr($manifest, 4); + if ($metadatalen) { + if (strlen($manifest) < $metadatalen) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESUNDERFLOW, array( + 'archive' => $this->_archiveName)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } + $this->_metadata = unserialize(substr($manifest, 0, $metadatalen)); + $manifest = substr($manifest, $metadatalen); + } $ret = array(); $offset = 0; for ($i = 0; $i < $info[1]; $i++) { @@ -196,7 +224,7 @@ } $savepath = substr($manifest, 4, $len[1]); $manifest = substr($manifest, $len[1] + 4); - if (strlen($manifest) < 17) { + if (strlen($manifest) < 24) { if (isset($savepath)) { $errors[] = new PHP_Archive_ExceptionExtended( PHP_Archive_ExceptionExtended::MANIFESTENTRIESTRUNCATEDENTRY, array( @@ -217,11 +245,22 @@ // 2 = compressed file size // 3 = crc32 // 4 = flags - $ret[$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ce', substr($manifest, 0, 17))); - $ret[$savepath][5] = $offset; - $this->_compressed = $ret[$savepath][4] & PHP_ARCHIVE_MANAGER_COMPRESSED_GZ; + // 5 = metadata length + $ret[$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($manifest, 0, 24))); + $manifest = substr($manifest, 24); + if ($ret[$savepath][5]) { + if (strlen($manifest) < $ret[$savepath][5]) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESTRUNCATEDENTRY, + array('archive' => $this->_archiveName, 'last' => $last, + 'current' => $savepath, 'size' => $info[1], 'cur' => $i)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . + '"', $errors); + } + $ret[$savepath][6] = unserialize(fread($fp, $ret[$savepath][5])); + } + $ret[$savepath][7] = $offset; $offset += $ret[$savepath][2]; - $manifest = substr($manifest, 17); } $this->_manifest = $ret; $this->_fileStart = ftell($fp); @@ -229,10 +268,10 @@ $currentFilename = $path; $internalFileLength = $info[2]; // seek to offset of file header within the .phar - if (fseek($fp, $this->_fileStart + $info[5])) { + if (fseek($fp, $this->_fileStart + $info[7])) { $errors[] = new PHP_Archive_ExceptionExtended( PHP_Archive_ExceptionExtended::FILELOCATIONINVALID, - array('archive' => $this->_archiveName, 'file' => $path, 'loc' => $this->_fileStart + $info[5], + array('archive' => $this->_archiveName, 'file' => $path, 'loc' => $this->_fileStart + $info[7], 'size' => filesize($this->_archiveName))); continue; } @@ -248,7 +287,7 @@ $data .= @fread($fp, 8192); } } - if ($info[4] & PHP_ARCHIVE_MANAGER_COMPRESSED_GZ) { + if ($info[4] & self::GZ) { $data = @gzinflate($data); if ($data === false) { $errors[] = new PHP_Archive_ExceptionExtended( @@ -256,6 +295,14 @@ array('archive' => $this->_archiveName, 'file' => $path, 'loc' => $this->_fileStart + $info[2])); } } + if ($info[4] & self::BZ2) { + $data = @bzdecompress($data, true); + if ($data === false) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::FILECORRUPTEDBZ2, + array('archive' => $this->_archiveName, 'file' => $path, 'loc' => $this->_fileStart + $info[2])); + } + } if ($temp['isize'] != strlen($data)) { $errors[] = new PHP_Archive_ExceptionExtended( PHP_Archive_ExceptionExtended::FILECORRUPTEDSIZE, @@ -294,13 +341,16 @@ 'Manifest size (bytes)' => $this->_manifestSize, 'Manifest entries' => count($this->_manifest), 'Alias' => $this->_alias, - 'Global compressed flag' => $this->_compressed, + 'Phar Metadata' => var_export($this->_metadata, true), + 'Global compressed flag' => bin2hex($this->_flags), ); // 0 = uncompressed file size // 1 = save timestamp // 2 = compressed file size // 3 = crc32 // 4 = flags + // 5 = meta-data length + // 6 = meta-data $offset = 0; foreach ($this->_manifest as $file => $info) { $ret['File phar://' . $this->_alias . '/' . $file . ' size'] = $info[0]; @@ -309,8 +359,13 @@ $ret['File phar://' . $this->_alias . '/' . $file . ' crc'] = $info[3]; $ret['File phar://' . $this->_alias . '/' . $file . ' size in archive'] = $info[2]; $ret['File phar://' . $this->_alias . '/' . $file . ' offset in archive'] = $offset; + $ret['File phar://' . $this->_alias . '/' . $file . ' meta-data length'] = $info[5]; + $ret['File phar://' . $this->_alias . '/' . $file . ' meta-data'] = + var_export($info[6], true); $ret['File phar://' . $this->_alias . '/' . $file . ' GZ compressed'] = - $info[4] & PHP_ARCHIVE_MANAGER_COMPRESSED_GZ ? 'yes' : 'no'; + $info[4] & self::GZ ? 'yes' : 'no'; + $ret['File phar://' . $this->_alias . '/' . $file . ' BZ2 compressed'] = + $info[4] & self::BZ2 ? 'yes' : 'no'; $offset += $info[2]; } return $ret; http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/eof.phpt?r1=1.7&r2=1.8&diff_format=u Index: pear/PHP_Archive/tests/eof.phpt diff -u pear/PHP_Archive/tests/eof.phpt:1.7 pear/PHP_Archive/tests/eof.phpt:1.8 --- pear/PHP_Archive/tests/eof.phpt:1.7 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/eof.phpt Tue Feb 6 03:34:22 2007 @@ -29,5 +29,5 @@ bool(false) int(0) bool(true) -int(47) +int(43) tests done \ No newline at end of file http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/filenotfound_phar.phpt?r1=1.2&r2=1.3&diff_format=u Index: pear/PHP_Archive/tests/filenotfound_phar.phpt diff -u pear/PHP_Archive/tests/filenotfound_phar.phpt:1.2 pear/PHP_Archive/tests/filenotfound_phar.phpt:1.3 --- pear/PHP_Archive/tests/filenotfound_phar.phpt:1.2 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/filenotfound_phar.phpt Tue Feb 6 03:34:22 2007 @@ -19,5 +19,5 @@ echo 'tests done'; ?> --EXPECT-- -fopen(phar://cachestat.phar/test2.php): failed to open stream: phar error: "test2.php" is not a file in phar "cachestat.phar" +Error: "test2.php" is not a file in phar "filenotfound.phar" tests done \ No newline at end of file http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/filenotfoundtest.php?r1=1.1&r2=1.2&diff_format=u Index: pear/PHP_Archive/tests/filenotfoundtest.php diff -u pear/PHP_Archive/tests/filenotfoundtest.php:1.1 pear/PHP_Archive/tests/filenotfoundtest.php:1.2 --- pear/PHP_Archive/tests/filenotfoundtest.php:1.1 Thu Apr 14 03:19:44 2005 +++ pear/PHP_Archive/tests/filenotfoundtest.php Tue Feb 6 03:34:22 2007 @@ -1,3 +1,3 @@ <?php -@fopen('phar://cachestat.phar/test2.php', 'r'); +fopen('phar://filenotfound.phar/test2.php', 'r'); ?> \ No newline at end of file http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/normalstat.phpt?r1=1.5&r2=1.6&diff_format=u Index: pear/PHP_Archive/tests/normalstat.phpt diff -u pear/PHP_Archive/tests/normalstat.phpt:1.5 pear/PHP_Archive/tests/normalstat.phpt:1.6 --- pear/PHP_Archive/tests/normalstat.phpt:1.5 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/normalstat.phpt Tue Feb 6 03:34:22 2007 @@ -23,12 +23,12 @@ 4 => 0, 5 => 0, 6 => 0, - 7 => 47, + 7 => 43, 8 => $x[8], 9 => $x[8], 10 => $x[8], - 11 => -1, - 12 => -1, + 11 => 0, + 12 => 0, 'dev' => 0, 'ino' => 0, 'mode' => 33060, @@ -36,12 +36,12 @@ 'uid' => 0, 'gid' => 0, 'rdev' => 0, - 'size' => 47, + 'size' => 43, 'atime' => $x[8], 'mtime' => $x[8], 'ctime' => $x[8], - 'blksize' => -1, - 'blocks' => -1, + 'blksize' => 0, + 'blocks' => 0, ), $x, 'stat'); echo 'tests done'; ?> http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/normalstat_phar.phpt?r1=1.2&r2=1.3&diff_format=u Index: pear/PHP_Archive/tests/normalstat_phar.phpt diff -u pear/PHP_Archive/tests/normalstat_phar.phpt:1.2 pear/PHP_Archive/tests/normalstat_phar.phpt:1.3 --- pear/PHP_Archive/tests/normalstat_phar.phpt:1.2 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/normalstat_phar.phpt Tue Feb 6 03:34:22 2007 @@ -22,12 +22,12 @@ 4 => 0, 5 => 0, 6 => -1, - 7 => 47, + 7 => 43, 8 => $x[8], 9 => $x[8], 10 => $x[8], - 11 => -1, - 12 => -1, + 11 => 0, + 12 => 0, 'dev' => 12, 'ino' => -895164305, 'mode' => 33060, @@ -35,12 +35,12 @@ 'uid' => 0, 'gid' => 0, 'rdev' => -1, - 'size' => 47, + 'size' => 43, 'atime' => $x[8], 'mtime' => $x[8], 'ctime' => $x[8], - 'blksize' => -1, - 'blocks' => -1, + 'blksize' => 0, + 'blocks' => 0, ), $x, 'stat'); echo 'tests done'; ?> http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/seek.phpt?r1=1.5&r2=1.6&diff_format=u Index: pear/PHP_Archive/tests/seek.phpt diff -u pear/PHP_Archive/tests/seek.phpt:1.5 pear/PHP_Archive/tests/seek.phpt:1.6 --- pear/PHP_Archive/tests/seek.phpt:1.5 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/seek.phpt Tue Feb 6 03:34:22 2007 @@ -22,17 +22,17 @@ $phpunit->assertEquals('hp', fread($fp, 2), 'second read'); $phpunit->assertEquals(5, ftell($fp), 'after second read'); fseek($fp, 0, SEEK_END); -$phpunit->assertEquals(47, ftell($fp), 'third seek 0 SEEK_END'); +$phpunit->assertEquals(43, ftell($fp), 'third seek 0 SEEK_END'); fseek($fp, -1, SEEK_END); -$phpunit->assertEquals(46, ftell($fp), 'fourth seek -1 SEEK_END'); +$phpunit->assertEquals(42, ftell($fp), 'fourth seek -1 SEEK_END'); fseek($fp, -61, SEEK_END); -$phpunit->assertEquals(46, ftell($fp), 'fifth seek -61 SEEK_END'); +$phpunit->assertEquals(42, ftell($fp), 'fifth seek -61 SEEK_END'); fseek($fp, -1, SEEK_CUR); -$phpunit->assertEquals(45, ftell($fp), 'sixth seek -1 SEEK_CUR'); +$phpunit->assertEquals(41, ftell($fp), 'sixth seek -1 SEEK_CUR'); fseek($fp, 20, SEEK_CUR); -$phpunit->assertEquals(65, ftell($fp), 'seventh seek 20 SEEK_CUR'); +$phpunit->assertEquals(61, ftell($fp), 'seventh seek 20 SEEK_CUR'); fseek($fp, 1, SEEK_END); -$phpunit->assertEquals(48, ftell($fp), 'eighth seek 1 SEEK_END'); +$phpunit->assertEquals(44, ftell($fp), 'eighth seek 1 SEEK_END'); fclose($fp); echo 'tests done'; ?> http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/streamstat.phpt?r1=1.5&r2=1.6&diff_format=u Index: pear/PHP_Archive/tests/streamstat.phpt diff -u pear/PHP_Archive/tests/streamstat.phpt:1.5 pear/PHP_Archive/tests/streamstat.phpt:1.6 --- pear/PHP_Archive/tests/streamstat.phpt:1.5 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/streamstat.phpt Tue Feb 6 03:34:22 2007 @@ -24,12 +24,12 @@ 4 => 0, 5 => 0, 6 => 0, - 7 => 47, + 7 => 43, 8 => $x[8], 9 => $x[8], 10 => $x[8], - 11 => -1, - 12 => -1, + 11 => 0, + 12 => 0, 'dev' => 0, 'ino' => 0, 'mode' => 33060, @@ -37,12 +37,12 @@ 'uid' => 0, 'gid' => 0, 'rdev' => 0, - 'size' => 47, + 'size' => 43, 'atime' => $x[8], 'mtime' => $x[8], 'ctime' => $x[8], - 'blksize' => -1, - 'blocks' => -1, + 'blksize' => 0, + 'blocks' => 0, ), $x, 'stat'); fclose($fp); echo 'tests done'; http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/streamstat_phar.phpt?r1=1.2&r2=1.3&diff_format=u Index: pear/PHP_Archive/tests/streamstat_phar.phpt diff -u pear/PHP_Archive/tests/streamstat_phar.phpt:1.2 pear/PHP_Archive/tests/streamstat_phar.phpt:1.3 --- pear/PHP_Archive/tests/streamstat_phar.phpt:1.2 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/streamstat_phar.phpt Tue Feb 6 03:34:22 2007 @@ -23,12 +23,12 @@ 4 => 0, 5 => 0, 6 => -1, - 7 => 47, + 7 => 43, 8 => $x[8], 9 => $x[8], 10 => $x[8], - 11 => -1, - 12 => -1, + 11 => 0, + 12 => 0, 'dev' => 12, 'ino' => -895164305, 'mode' => 33060, @@ -36,12 +36,12 @@ 'uid' => 0, 'gid' => 0, 'rdev' => -1, - 'size' => 47, + 'size' => 43, 'atime' => $x[8], 'mtime' => $x[8], 'ctime' => $x[8], - 'blksize' => -1, - 'blocks' => -1, + 'blksize' => 0, + 'blocks' => 0, ), $x, 'stat'); fclose($fp); echo 'tests done'; http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/filenotfound/filenotfound.phar?r1=1.9&r2=1.10&diff_format=u Index: pear/PHP_Archive/tests/filenotfound/filenotfound.phar diff -u pear/PHP_Archive/tests/filenotfound/filenotfound.phar:1.9 pear/PHP_Archive/tests/filenotfound/filenotfound.phar:1.10 --- pear/PHP_Archive/tests/filenotfound/filenotfound.phar:1.9 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/filenotfound/filenotfound.phar Tue Feb 6 03:34:23 2007 @@ -1,4 +1,4 @@ -<?php #PHP_ARCHIVE_HEADER-0.8.0 +<?php error_reporting(E_ALL); if (function_exists('mb_internal_encoding')) { mb_internal_encoding('ASCII'); @@ -23,13 +23,15 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version Id: Archive.php,v 1.26 2006/01/09 04:48:42 cellog Exp $ + * @version Id: Archive.php,v 1.35 2007/01/06 21:58:24 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + const GZ = 0x00001000; + const BZ2 = 0x00002000; /** * Whether this archive is compressed with zlib * @@ -74,13 +76,20 @@ * </code> * then the alias is "PEAR.phar" * - * Information stored is the actual file name, a boolean indicating whether - * this .phar is compressed with zlib, and the precise offset of internal files + * Information stored is a boolean indicating whether this .phar is compressed + * with zlib, another for bzip2, phar-specific meta-data, and + * the precise offset of internal files * within the .phar, used with the {@link $_manifest} to load actual file contents * @var array */ private static $_pharMapping = array(); /** + * Map real file paths to alias used + * + * @var array + */ + private static $_pharFiles = array(); + /** * File listing for the .phar * * The manifest is indexed per phar. @@ -110,6 +119,30 @@ private $_basename; /** + * Allows loading an external Phar archive without include()ing it + * + * @param string $file + * @throws Exception + */ + public static final function loadPhar($file) + { + $file = realpath($file); + if ($file) { + $fp = fopen($file, 'rb'); + $buffer = ''; + while (!$found && !feof($fp)) { + $buffer .= fread($fp, 8192); + // don't break phars + if ($pos = strpos($buffer, '__HALT_COMPI' . 'LER();')) { + fclose($fp); + return self::_mapPhar($file, $pos); + } + } + fclose($fp); + } + } + + /** * Map a full real file path to an alias used to refer to the .phar * * This function can only be called from the initialization of the .phar itself. @@ -124,40 +157,157 @@ */ public static final function mapPhar($file, $dataoffset) { - // this ensures that this is safe - if (!in_array($file, get_included_files())) { - die('SECURITY ERROR: PHP_Archive::mapPhar can only be called from within ' . - 'the phar that initiates it'); + try { + // this ensures that this is safe + if (!in_array($file, get_included_files())) { + die('SECURITY ERROR: PHP_Archive::mapPhar can only be called from within ' . + 'the phar that initiates it'); + } + self::_mapPhar($file, $dataoffset); + } catch (Exception $e) { + die($e->getMessage()); + } + } + + /** + * Sub-function, allows recovery from errors + * + * @param unknown_type $file + * @param unknown_type $dataoffset + */ + private static function _mapPhar($file, $dataoffset) + { + $file = realpath($file); + if (isset(self::$_manifest[$file])) { + return; } if (!is_array(self::$_pharMapping)) { self::$_pharMapping = array(); } - if (!isset(self::$_manifest[$file])) { - $fp = fopen($file, 'rb'); - // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, $dataoffset); - $manifest_length = unpack('Vlen', fread($fp, 4)); - $info = self::_unserializeManifest(fread($fp, $manifest_length['len'])); - if (!$info) { - die; // error declared in unserializeManifest + $fp = fopen($file, 'rb'); + // seek to __HALT_COMPILER_OFFSET__ + fseek($fp, $dataoffset); + $manifest_length = unpack('Vlen', fread($fp, 4)); + $manifest = ''; + $last = '1'; + while (strlen($last) && strlen($manifest) < $manifest_length['len']) { + $read = 8192; + if ($manifest_length['len'] - strlen($manifest) < 8192) { + $read = $manifest_length['len'] - strlen($manifest); } + $last = fread($fp, $read); + $manifest .= $last; + } + if (strlen($manifest) < $manifest_length['len']) { + throw new Exception('ERROR: manifest length read was "' . + strlen($manifest) .'" should be "' . + $manifest_length['len'] . '"'); + } + $info = self::_unserializeManifest($manifest); + if ($info['alias']) { $alias = $info['alias']; - self::$_manifest[$file] = $info['manifest']; - $compressed = $info['compressed']; - self::$_fileStart[$file] = ftell($fp); - fclose($fp); + $explicit = true; + } else { + $alias = $file; + $explicit = false; } - if ($compressed) { + self::$_manifest[$file] = $info['manifest']; + $compressed = $info['compressed']; + self::$_fileStart[$file] = ftell($fp); + fclose($fp); + if ($compressed & 0x00001000) { if (!function_exists('gzinflate')) { - die('Error: zlib extension is not enabled - gzinflate() function needed' . + throw new Exception('Error: zlib extension is not enabled - gzinflate() function needed' . + ' for compressed .phars'); + } + } + if ($compressed & 0x00002000) { + if (!function_exists('bzdecompress')) { + throw new Exception('Error: bzip2 extension is not enabled - bzdecompress() function needed' . ' for compressed .phars'); } } if (isset(self::$_pharMapping[$alias])) { - die('ERROR: PHP_Archive::mapPhar has already been called for alias "' . + throw new Exception('ERROR: PHP_Archive::mapPhar has already been called for alias "' . $alias . '" cannot re-alias to "' . $file . '"'); } - self::$_pharMapping[$alias] = array($file, $compressed, $dataoffset); + self::$_pharMapping[$alias] = array($file, $compressed, $dataoffset, $explicit); + self::$_pharFiles[$file] = $alias; + } + + /** + * extract the manifest into an internal array + * + * @param string $manifest + * @return false|array + */ + private static function _unserializeManifest($manifest) + { + // retrieve the number of files in the manifest + $info = unpack('V', substr($manifest, 0, 4)); + $apiver = substr($manifest, 4, 2); + $apiver = bin2hex($apiver); + $apiver_dots = hexdec($apiver[0]) . '.' . hexdec($apiver[1]) . '.' . hexdec($apiver[2]); + $majorcompat = hexdec($apiver[0]); + $calcapi = explode('.', '1.0.0'); + if ($calcapi[0] != $majorcompat) { + throw new Exception('Phar is incompatible API version ' . $apiver_dots . ', but ' . + 'PHP_Archive is API version 1.0.0'); + } + if ($calcapi[0] === '0') { + if ('1.0.0' != $apiver_dots) { + throw new Exception('Phar is API version ' . $apiver_dots . + ', but PHP_Archive is API version 1.0.0', E_USER_ERROR); + } + } + $flags = unpack('V', substr($manifest, 6, 4)); + $ret = array('compressed' => $flags & 0x00003000); + // signature is not verified by default in PHP_Archive, phar is better + $ret['hassignature'] = $flags & 0x00010000; + $aliaslen = unpack('V', substr($manifest, 10, 4)); + if ($aliaslen) { + $ret['alias'] = substr($manifest, 14, $aliaslen[1]); + } else { + $ret['alias'] = false; + } + $manifest = substr($manifest, 14 + $aliaslen[1]); + $metadatalen = unpack('V', substr($manifest, 0, 4)); + if ($metadatalen[1]) { + $ret['metadata'] = unserialize(substr($manifest, 4, $metadatalen[1])); + $manifest = substr($manifest, 4 + $metadatalen[1]); + } else { + $ret['metadata'] = null; + $manifest = substr($manifest, 4); + } + $offset = 0; + $start = 0; + for ($i = 0; $i < $info[1]; $i++) { + // length of the file name + $len = unpack('V', substr($manifest, $start, 4)); + $start += 4; + // file name + $savepath = substr($manifest, $start, $len[1]); + $start += $len[1]; + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = compressed filesize + // 3 = crc32 + // 4 = flags + // 5 = metadata length + $ret['manifest'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($manifest, $start, 24))); + $ret['manifest'][$savepath][3] = sprintf('%u', $ret['manifest'][$savepath][3]); + if ($ret['manifest'][$savepath][5]) { + $ret['manifest'][$savepath][6] = unserialize(substr($manifest, $start + 24, + $ret['manifest'][$savepath][5])); + } else { + $ret['manifest'][$savepath][6] = null; + } + $ret['manifest'][$savepath][7] = $offset; + $offset += $ret['manifest'][$savepath][2]; + $start += 24 + $ret['manifest'][$savepath][5]; + } + return $ret; } /** @@ -219,7 +369,7 @@ $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][2]; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][5]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][7]); } } @@ -248,15 +398,17 @@ } } @fclose($this->fp); - if ($this->_compressed) { + if (self::$_manifest[$this->_archiveName][$path][4] & self::GZ) { $data = gzinflate($data); + } elseif (self::$_manifest[$this->_archiveName][$path][4] & self::BZ2) { + $data = bzdecompress($data); } if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if (strlen($data) != $this->currentStat[7]) { return array("Not valid internal .phar file (size error {$size} != " . $this->currentStat[7] . ")"); } - if (self::$_manifest[$this->_archiveName][$path][3] != crc32($data)) { + if (self::$_manifest[$this->_archiveName][$path][3] != sprintf("%u", crc32($data))) { return array("Not valid internal .phar file (checksum error)"); } self::$_manifest[$this->_archiveName][$path]['ok'] = true; @@ -269,27 +421,79 @@ } /** + * Parse urls like phar:///fullpath/to/my.phar/file.txt + * + * @param string $file + * @return false|array + */ + static protected function parseUrl($file) + { + if (substr($file, 0, 7) != 'phar://') { + return false; + } + $file = substr($file, 7); + + $ret = array('scheme' => 'phar'); + $pos_p = strpos($file, '.phar.php'); + $pos_z = strpos($file, '.phar.gz'); + $pos_b = strpos($file, '.phar.bz2'); + if ($pos_p) { + if ($pos_z) { + return false; + } + $ret['host'] = substr($file, 0, $pos_p + strlen('.phar.php')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif ($pos_z) { + $ret['host'] = substr($file, 0, $pos_z + strlen('.phar.gz')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif ($pos_b) { + $ret['host'] = substr($file, 0, $pos_z + strlen('.phar.bz2')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif (($pos_p = strpos($file, ".phar")) !== false) { + $ret['host'] = substr($file, 0, $pos_p + strlen('.phar')); + $ret['path'] = substr($file, strlen($ret['host'])); + } else { + return false; + } + if (!$ret['path']) { + $ret['path'] = '/'; + } + return $ret; + } + + /** * Locate the .phar archive in the include_path and detect the file to open within * the archive. * - * Possible parameters are phar://filename_within_phar.ext or - * phar://pharname.phar/filename_within_phar.ext - * - * phar://filename_within_phar.ext will simply use the last .phar opened. + * Possible parameters are phar://pharname.phar/filename_within_phar.ext * @param string a file within the archive * @return string the filename within the .phar to retrieve */ public function initializeStream($file) { - $info = parse_url($file); - if (!isset($info['host']) || !count(self::$_pharMapping)) { + $info = @parse_url($file); + if (!$info) { + $info = self::parseUrl($file); + } + if (!$info) { + return false; + } + if (!isset($info['host'])) { // malformed internal file return false; } + if (!isset(self::$_pharFiles[$info['host']]) && + !isset(self::$_pharMapping[$info['host']])) { + try { + self::loadPhar($info['host']); + // use alias from here out + $info['host'] = self::$_pharFiles[$info['host']]; + } catch (Exception $e) { + return false; + } + } if (!isset($info['path'])) { - // last opened phar is requested - $info['path'] = $info['host']; - $info['host'] = ''; + return false; } elseif (strlen($info['path']) > 1) { $info['path'] = substr($info['path'], 1); } @@ -297,72 +501,16 @@ $this->_basename = $info['host']; $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; - } else { - // no such phar has been included, or last opened phar is requested - $pharinfo = end(self::$_pharMapping); - $this->_basename = key(self::$_pharMapping); - $this->_archiveName = $pharinfo[0]; - $this->_compressed = $pharinfo[1]; + } elseif (isset(self::$_pharFiles[$info['host']])) { + $this->_archiveName = $info['host']; + $this->_basename = self::$_pharFiles[$info['host']]; + $this->_compressed = self::$_pharMapping[$this->_basename][1]; } $file = $info['path']; return $file; } /** - * extract the manifest into an internal array - * - * @param string $manifest - * @return false|array - */ - private static function _unserializeManifest($manifest) - { - // retrieve the number of files in the manifest - $info = unpack('V', substr($manifest, 0, 4)); - $apiver = substr($manifest, 4, 2); - $apiver = bin2hex($apiver); - $apiver_dots = hexdec($apiver[0]) . '.' . hexdec($apiver[1]) . '.' . hexdec($apiver[2]); - $majorcompat = hexdec($apiver[0]); - $calcapi = explode('.', '0.8.0'); - if ($calcapi[0] != $majorcompat) { - trigger_error('Phar is incompatible API version ' . $apivers_dots . ', but ' . - 'PHP_Archive is API version 0.8.0'); - return false; - } - if ($calcapi[0] === '0') { - if ('0.8.0' != $apiver_dots) { - trigger_error('Phar is API version ' . $apiver_dots . - ', but PHP_Archive is API version 0.8.0', E_USER_ERROR); - return false; - } - } - $ret = array('compressed' => $apiver[3]); - $aliaslen = unpack('V', substr($manifest, 6, 4)); - $ret['alias'] = substr($manifest, 10, $aliaslen[1]); - $manifest = substr($manifest, 10 + $aliaslen[1]); - $offset = 0; - $start = 0; - for ($i = 0; $i < $info[1]; $i++) { - // length of the file name - $len = unpack('V', substr($manifest, $start, 4)); - $start += 4; - // file name - $savepath = substr($manifest, $start, $len[1]); - $start += $len[1]; - // retrieve manifest data: - // 0 = uncompressed file size - // 1 = timestamp of when file was added to phar - // 2 = compressed filesize - // 3 = crc32 - // 4 = flags - $ret['manifest'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ce', substr($manifest, $start, 17))); - $ret['manifest'][$savepath][5] = $offset; - $offset += $ret['manifest'][$savepath][2]; - $start += 17; - } - return $ret; - } - - /** * Open the requested file - PHP streams API * * @param string $file String provided by the Stream wrapper @@ -534,7 +682,15 @@ */ public function dir_opendir($path) { - $info = parse_url($path); + $info = @parse_url($path); + if (!$info) { + $info = self::parseUrl($path); + if (!$info) { + trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', + E_USER_ERROR); + return false; + } + } $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); @@ -556,15 +712,19 @@ foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if ($path == '/') { if (strpos($file, '/')) { - $this->_dirFiles[array_shift($a = explode('/', $file))] = true; + $a = explode('/', $file); + $this->_dirFiles[array_shift($a)] = true; } else { $this->_dirFiles[$file] = true; } } elseif (strpos($file, $path) === 0) { $fname = substr($file, strlen($path) + 1); if (strpos($fname, '/')) { - $this->_dirFiles[array_unshift($a = explode('/', $fname))] = true; - } else { + $a = explode('/', $fname); + $this->_dirFiles[array_shift($a)] = true; + } elseif (strlen($file) != strlen($path)) { + // if the two match exactly, the path searched for was + // not a directory, but was a file. $this->_dirFiles[$fname] = true; } } @@ -615,20 +775,24 @@ */ public final function APIVersion() { - return '0.8.0'; + return '1.0.0'; } }} if (!class_exists('Phar')) { - if (!in_array('phar', stream_get_wrappers())) { - stream_wrapper_register('phar', 'PHP_Archive'); - } PHP_Archive::mapPhar(__FILE__, __COMPILER_HALT_OFFSET__); } else { - Phar::mapPhar(); + try { + Phar::mapPhar(); + } catch (Exception $e) { + echo $e->getMessage(); + } +} +if (class_exists('PHP_Archive') && !in_array('phar', stream_get_wrappers())) { + stream_wrapper_register('phar', 'PHP_Archive'); } @ini_set('memory_limit', -1); require_once 'phar://filenotfound.phar/index.php'; -__HALT_COMPILER();9-@fopen('phar://cachestat.phar/test2.php', 'r'); +__HALT_COMPILER();H+fopen('phar://filenotfound.phar/test2.php', 'r'); ?> \ No newline at end of file http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/filenotfound/makefilenotfoundphar.php.inc?r1=1.3&r2=1.4&diff_format=u Index: pear/PHP_Archive/tests/filenotfound/makefilenotfoundphar.php.inc diff -u pear/PHP_Archive/tests/filenotfound/makefilenotfoundphar.php.inc:1.3 pear/PHP_Archive/tests/filenotfound/makefilenotfoundphar.php.inc:1.4 --- pear/PHP_Archive/tests/filenotfound/makefilenotfoundphar.php.inc:1.3 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/filenotfound/makefilenotfoundphar.php.inc Tue Feb 6 03:34:23 2007 @@ -2,7 +2,7 @@ require_once 'PHP/Archive/Creator.php'; require_once 'PHP/Archive/Manager.php'; chdir(dirname(__FILE__)); -$creator = new PHP_Archive_Creator('index.php', false, 'filenotfound.phar'); +$creator = new PHP_Archive_Creator('index.php', 'filenotfound.phar'); $creator->addFile(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'filenotfoundtest.php', 'index.php'); $creator->savePhar(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'filenotfound.phar'); @unlink($name); http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/longfilename/longphar.phar?r1=1.11&r2=1.12&diff_format=u Index: pear/PHP_Archive/tests/longfilename/longphar.phar diff -u pear/PHP_Archive/tests/longfilename/longphar.phar:1.11 pear/PHP_Archive/tests/longfilename/longphar.phar:1.12 --- pear/PHP_Archive/tests/longfilename/longphar.phar:1.11 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/longfilename/longphar.phar Tue Feb 6 03:34:23 2007 @@ -1,4 +1,4 @@ -<?php #PHP_ARCHIVE_HEADER-0.8.0 +<?php error_reporting(E_ALL); if (function_exists('mb_internal_encoding')) { mb_internal_encoding('ASCII'); @@ -23,13 +23,15 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version Id: Archive.php,v 1.26 2006/01/09 04:48:42 cellog Exp $ + * @version Id: Archive.php,v 1.35 2007/01/06 21:58:24 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + const GZ = 0x00001000; + const BZ2 = 0x00002000; /** * Whether this archive is compressed with zlib * @@ -74,13 +76,20 @@ * </code> * then the alias is "PEAR.phar" * - * Information stored is the actual file name, a boolean indicating whether - * this .phar is compressed with zlib, and the precise offset of internal files + * Information stored is a boolean indicating whether this .phar is compressed + * with zlib, another for bzip2, phar-specific meta-data, and + * the precise offset of internal files * within the .phar, used with the {@link $_manifest} to load actual file contents * @var array */ private static $_pharMapping = array(); /** + * Map real file paths to alias used + * + * @var array + */ + private static $_pharFiles = array(); + /** * File listing for the .phar * * The manifest is indexed per phar. @@ -110,6 +119,30 @@ private $_basename; /** + * Allows loading an external Phar archive without include()ing it + * + * @param string $file + * @throws Exception + */ + public static final function loadPhar($file) + { + $file = realpath($file); + if ($file) { + $fp = fopen($file, 'rb'); + $buffer = ''; + while (!$found && !feof($fp)) { + $buffer .= fread($fp, 8192); + // don't break phars + if ($pos = strpos($buffer, '__HALT_COMPI' . 'LER();')) { + fclose($fp); + return self::_mapPhar($file, $pos); + } + } + fclose($fp); + } + } + + /** * Map a full real file path to an alias used to refer to the .phar * * This function can only be called from the initialization of the .phar itself. @@ -124,40 +157,157 @@ */ public static final function mapPhar($file, $dataoffset) { - // this ensures that this is safe - if (!in_array($file, get_included_files())) { - die('SECURITY ERROR: PHP_Archive::mapPhar can only be called from within ' . - 'the phar that initiates it'); + try { + // this ensures that this is safe + if (!in_array($file, get_included_files())) { + die('SECURITY ERROR: PHP_Archive::mapPhar can only be called from within ' . + 'the phar that initiates it'); + } + self::_mapPhar($file, $dataoffset); + } catch (Exception $e) { + die($e->getMessage()); + } + } + + /** + * Sub-function, allows recovery from errors + * + * @param unknown_type $file + * @param unknown_type $dataoffset + */ + private static function _mapPhar($file, $dataoffset) + { + $file = realpath($file); + if (isset(self::$_manifest[$file])) { + return; } if (!is_array(self::$_pharMapping)) { self::$_pharMapping = array(); } - if (!isset(self::$_manifest[$file])) { - $fp = fopen($file, 'rb'); - // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, $dataoffset); - $manifest_length = unpack('Vlen', fread($fp, 4)); - $info = self::_unserializeManifest(fread($fp, $manifest_length['len'])); - if (!$info) { - die; // error declared in unserializeManifest + $fp = fopen($file, 'rb'); + // seek to __HALT_COMPILER_OFFSET__ + fseek($fp, $dataoffset); + $manifest_length = unpack('Vlen', fread($fp, 4)); + $manifest = ''; + $last = '1'; + while (strlen($last) && strlen($manifest) < $manifest_length['len']) { + $read = 8192; + if ($manifest_length['len'] - strlen($manifest) < 8192) { + $read = $manifest_length['len'] - strlen($manifest); } + $last = fread($fp, $read); + $manifest .= $last; + } + if (strlen($manifest) < $manifest_length['len']) { + throw new Exception('ERROR: manifest length read was "' . + strlen($manifest) .'" should be "' . + $manifest_length['len'] . '"'); + } + $info = self::_unserializeManifest($manifest); + if ($info['alias']) { $alias = $info['alias']; - self::$_manifest[$file] = $info['manifest']; - $compressed = $info['compressed']; - self::$_fileStart[$file] = ftell($fp); - fclose($fp); + $explicit = true; + } else { + $alias = $file; + $explicit = false; } - if ($compressed) { + self::$_manifest[$file] = $info['manifest']; + $compressed = $info['compressed']; + self::$_fileStart[$file] = ftell($fp); + fclose($fp); + if ($compressed & 0x00001000) { if (!function_exists('gzinflate')) { - die('Error: zlib extension is not enabled - gzinflate() function needed' . + throw new Exception('Error: zlib extension is not enabled - gzinflate() function needed' . + ' for compressed .phars'); + } + } + if ($compressed & 0x00002000) { + if (!function_exists('bzdecompress')) { + throw new Exception('Error: bzip2 extension is not enabled - bzdecompress() function needed' . ' for compressed .phars'); } } if (isset(self::$_pharMapping[$alias])) { - die('ERROR: PHP_Archive::mapPhar has already been called for alias "' . + throw new Exception('ERROR: PHP_Archive::mapPhar has already been called for alias "' . $alias . '" cannot re-alias to "' . $file . '"'); } - self::$_pharMapping[$alias] = array($file, $compressed, $dataoffset); + self::$_pharMapping[$alias] = array($file, $compressed, $dataoffset, $explicit); + self::$_pharFiles[$file] = $alias; + } + + /** + * extract the manifest into an internal array + * + * @param string $manifest + * @return false|array + */ + private static function _unserializeManifest($manifest) + { + // retrieve the number of files in the manifest + $info = unpack('V', substr($manifest, 0, 4)); + $apiver = substr($manifest, 4, 2); + $apiver = bin2hex($apiver); + $apiver_dots = hexdec($apiver[0]) . '.' . hexdec($apiver[1]) . '.' . hexdec($apiver[2]); + $majorcompat = hexdec($apiver[0]); + $calcapi = explode('.', '1.0.0'); + if ($calcapi[0] != $majorcompat) { + throw new Exception('Phar is incompatible API version ' . $apiver_dots . ', but ' . + 'PHP_Archive is API version 1.0.0'); + } + if ($calcapi[0] === '0') { + if ('1.0.0' != $apiver_dots) { + throw new Exception('Phar is API version ' . $apiver_dots . + ', but PHP_Archive is API version 1.0.0', E_USER_ERROR); + } + } + $flags = unpack('V', substr($manifest, 6, 4)); + $ret = array('compressed' => $flags & 0x00003000); + // signature is not verified by default in PHP_Archive, phar is better + $ret['hassignature'] = $flags & 0x00010000; + $aliaslen = unpack('V', substr($manifest, 10, 4)); + if ($aliaslen) { + $ret['alias'] = substr($manifest, 14, $aliaslen[1]); + } else { + $ret['alias'] = false; + } + $manifest = substr($manifest, 14 + $aliaslen[1]); + $metadatalen = unpack('V', substr($manifest, 0, 4)); + if ($metadatalen[1]) { + $ret['metadata'] = unserialize(substr($manifest, 4, $metadatalen[1])); + $manifest = substr($manifest, 4 + $metadatalen[1]); + } else { + $ret['metadata'] = null; + $manifest = substr($manifest, 4); + } + $offset = 0; + $start = 0; + for ($i = 0; $i < $info[1]; $i++) { + // length of the file name + $len = unpack('V', substr($manifest, $start, 4)); + $start += 4; + // file name + $savepath = substr($manifest, $start, $len[1]); + $start += $len[1]; + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = compressed filesize + // 3 = crc32 + // 4 = flags + // 5 = metadata length + $ret['manifest'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($manifest, $start, 24))); + $ret['manifest'][$savepath][3] = sprintf('%u', $ret['manifest'][$savepath][3]); + if ($ret['manifest'][$savepath][5]) { + $ret['manifest'][$savepath][6] = unserialize(substr($manifest, $start + 24, + $ret['manifest'][$savepath][5])); + } else { + $ret['manifest'][$savepath][6] = null; + } + $ret['manifest'][$savepath][7] = $offset; + $offset += $ret['manifest'][$savepath][2]; + $start += 24 + $ret['manifest'][$savepath][5]; + } + return $ret; } /** @@ -219,7 +369,7 @@ $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][2]; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][5]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][7]); } } @@ -248,15 +398,17 @@ } } @fclose($this->fp); - if ($this->_compressed) { + if (self::$_manifest[$this->_archiveName][$path][4] & self::GZ) { $data = gzinflate($data); + } elseif (self::$_manifest[$this->_archiveName][$path][4] & self::BZ2) { + $data = bzdecompress($data); } if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if (strlen($data) != $this->currentStat[7]) { return array("Not valid internal .phar file (size error {$size} != " . $this->currentStat[7] . ")"); } - if (self::$_manifest[$this->_archiveName][$path][3] != crc32($data)) { + if (self::$_manifest[$this->_archiveName][$path][3] != sprintf("%u", crc32($data))) { return array("Not valid internal .phar file (checksum error)"); } self::$_manifest[$this->_archiveName][$path]['ok'] = true; @@ -269,27 +421,79 @@ } /** + * Parse urls like phar:///fullpath/to/my.phar/file.txt + * + * @param string $file + * @return false|array + */ + static protected function parseUrl($file) + { + if (substr($file, 0, 7) != 'phar://') { + return false; + } + $file = substr($file, 7); + + $ret = array('scheme' => 'phar'); + $pos_p = strpos($file, '.phar.php'); + $pos_z = strpos($file, '.phar.gz'); + $pos_b = strpos($file, '.phar.bz2'); + if ($pos_p) { + if ($pos_z) { + return false; + } + $ret['host'] = substr($file, 0, $pos_p + strlen('.phar.php')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif ($pos_z) { + $ret['host'] = substr($file, 0, $pos_z + strlen('.phar.gz')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif ($pos_b) { + $ret['host'] = substr($file, 0, $pos_z + strlen('.phar.bz2')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif (($pos_p = strpos($file, ".phar")) !== false) { + $ret['host'] = substr($file, 0, $pos_p + strlen('.phar')); + $ret['path'] = substr($file, strlen($ret['host'])); + } else { + return false; + } + if (!$ret['path']) { + $ret['path'] = '/'; + } + return $ret; + } + + /** * Locate the .phar archive in the include_path and detect the file to open within * the archive. * - * Possible parameters are phar://filename_within_phar.ext or - * phar://pharname.phar/filename_within_phar.ext - * - * phar://filename_within_phar.ext will simply use the last .phar opened. + * Possible parameters are phar://pharname.phar/filename_within_phar.ext * @param string a file within the archive * @return string the filename within the .phar to retrieve */ public function initializeStream($file) { - $info = parse_url($file); - if (!isset($info['host']) || !count(self::$_pharMapping)) { + $info = @parse_url($file); + if (!$info) { + $info = self::parseUrl($file); + } + if (!$info) { + return false; + } + if (!isset($info['host'])) { // malformed internal file return false; } + if (!isset(self::$_pharFiles[$info['host']]) && + !isset(self::$_pharMapping[$info['host']])) { + try { + self::loadPhar($info['host']); + // use alias from here out + $info['host'] = self::$_pharFiles[$info['host']]; + } catch (Exception $e) { + return false; + } + } if (!isset($info['path'])) { - // last opened phar is requested - $info['path'] = $info['host']; - $info['host'] = ''; + return false; } elseif (strlen($info['path']) > 1) { $info['path'] = substr($info['path'], 1); } @@ -297,72 +501,16 @@ $this->_basename = $info['host']; $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; - } else { - // no such phar has been included, or last opened phar is requested - $pharinfo = end(self::$_pharMapping); - $this->_basename = key(self::$_pharMapping); - $this->_archiveName = $pharinfo[0]; - $this->_compressed = $pharinfo[1]; + } elseif (isset(self::$_pharFiles[$info['host']])) { + $this->_archiveName = $info['host']; + $this->_basename = self::$_pharFiles[$info['host']]; + $this->_compressed = self::$_pharMapping[$this->_basename][1]; } $file = $info['path']; return $file; } /** - * extract the manifest into an internal array - * - * @param string $manifest - * @return false|array - */ - private static function _unserializeManifest($manifest) - { - // retrieve the number of files in the manifest - $info = unpack('V', substr($manifest, 0, 4)); - $apiver = substr($manifest, 4, 2); - $apiver = bin2hex($apiver); - $apiver_dots = hexdec($apiver[0]) . '.' . hexdec($apiver[1]) . '.' . hexdec($apiver[2]); - $majorcompat = hexdec($apiver[0]); - $calcapi = explode('.', '0.8.0'); - if ($calcapi[0] != $majorcompat) { - trigger_error('Phar is incompatible API version ' . $apivers_dots . ', but ' . - 'PHP_Archive is API version 0.8.0'); - return false; - } - if ($calcapi[0] === '0') { - if ('0.8.0' != $apiver_dots) { - trigger_error('Phar is API version ' . $apiver_dots . - ', but PHP_Archive is API version 0.8.0', E_USER_ERROR); - return false; - } - } - $ret = array('compressed' => $apiver[3]); - $aliaslen = unpack('V', substr($manifest, 6, 4)); - $ret['alias'] = substr($manifest, 10, $aliaslen[1]); - $manifest = substr($manifest, 10 + $aliaslen[1]); - $offset = 0; - $start = 0; - for ($i = 0; $i < $info[1]; $i++) { - // length of the file name - $len = unpack('V', substr($manifest, $start, 4)); - $start += 4; - // file name - $savepath = substr($manifest, $start, $len[1]); - $start += $len[1]; - // retrieve manifest data: - // 0 = uncompressed file size - // 1 = timestamp of when file was added to phar - // 2 = compressed filesize - // 3 = crc32 - // 4 = flags - $ret['manifest'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ce', substr($manifest, $start, 17))); - $ret['manifest'][$savepath][5] = $offset; - $offset += $ret['manifest'][$savepath][2]; - $start += 17; - } - return $ret; - } - - /** * Open the requested file - PHP streams API * * @param string $file String provided by the Stream wrapper @@ -534,7 +682,15 @@ */ public function dir_opendir($path) { - $info = parse_url($path); + $info = @parse_url($path); + if (!$info) { + $info = self::parseUrl($path); + if (!$info) { + trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', + E_USER_ERROR); + return false; + } + } $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); @@ -556,15 +712,19 @@ foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if ($path == '/') { if (strpos($file, '/')) { - $this->_dirFiles[array_shift($a = explode('/', $file))] = true; + $a = explode('/', $file); + $this->_dirFiles[array_shift($a)] = true; } else { $this->_dirFiles[$file] = true; } } elseif (strpos($file, $path) === 0) { $fname = substr($file, strlen($path) + 1); if (strpos($fname, '/')) { - $this->_dirFiles[array_unshift($a = explode('/', $fname))] = true; - } else { + $a = explode('/', $fname); + $this->_dirFiles[array_shift($a)] = true; + } elseif (strlen($file) != strlen($path)) { + // if the two match exactly, the path searched for was + // not a directory, but was a file. $this->_dirFiles[$fname] = true; } } @@ -615,21 +775,26 @@ */ public final function APIVersion() { - return '0.8.0'; + return '1.0.0'; } }} if (!class_exists('Phar')) { - if (!in_array('phar', stream_get_wrappers())) { - stream_wrapper_register('phar', 'PHP_Archive'); - } PHP_Archive::mapPhar(__FILE__, __COMPILER_HALT_OFFSET__); } else { - Phar::mapPhar(); + try { + Phar::mapPhar(); + } catch (Exception $e) { + echo $e->getMessage(); + } +} +PHP_Archive::mapPhar(__FILE__, __COMPILER_HALT_OFFSET__ + strlen('__HALT'.'_COMPILER();')); +if (class_exists('PHP_Archive') && !in_array('phar', stream_get_wrappers())) { + stream_wrapper_register('phar', 'PHP_Archive'); } @ini_set('memory_limit', -1); require_once 'phar://longphar.phar/testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest.php'; -__HALT_COMPILER();˜+__HALT_COMPILER();§ echo __FILE__; var_dump('hello'); http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/longfilename/makelongphar.php.inc?r1=1.3&r2=1.4&diff_format=u Index: pear/PHP_Archive/tests/longfilename/makelongphar.php.inc diff -u pear/PHP_Archive/tests/longfilename/makelongphar.php.inc:1.3 pear/PHP_Archive/tests/longfilename/makelongphar.php.inc:1.4 --- pear/PHP_Archive/tests/longfilename/makelongphar.php.inc:1.3 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/longfilename/makelongphar.php.inc Tue Feb 6 03:34:23 2007 @@ -2,11 +2,11 @@ require_once 'PHP/Archive/Creator.php'; require_once 'PHP/Archive/Manager.php'; chdir(dirname(__FILE__)); -copy('../test1.php', $name = str_repeat('test', 26) . '.php'); -$creator = new PHP_Archive_Creator($name, false, 'longphar.phar'); -$creator->addFile(dirname(__FILE__) . DIRECTORY_SEPARATOR . $name, $name); -$creator->savePhar(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'longphar.phar'); +copy(dirname(dirname(__FILE__)) . '/test1.php', $name = str_repeat('test', 26) . '.php'); +$creator = new PHP_Archive_Creator($name, 'longphar.phar'); +$creator->addFile($name, $name); +$creator->savePhar('longphar.phar'); @unlink($name); -$b = new PHP_Archive_Manager(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'longphar.phar'); +$b = new PHP_Archive_Manager('longphar.phar'); $b->dump(); ?> \ No newline at end of file http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/opendir/makeopendirphar.php.inc?r1=1.3&r2=1.4&diff_format=u Index: pear/PHP_Archive/tests/opendir/makeopendirphar.php.inc diff -u pear/PHP_Archive/tests/opendir/makeopendirphar.php.inc:1.3 pear/PHP_Archive/tests/opendir/makeopendirphar.php.inc:1.4 --- pear/PHP_Archive/tests/opendir/makeopendirphar.php.inc:1.3 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/opendir/makeopendirphar.php.inc Tue Feb 6 03:34:23 2007 @@ -2,7 +2,7 @@ require_once 'PHP/Archive/Creator.php'; require_once 'PHP/Archive/Manager.php'; chdir(dirname(__FILE__)); -$creator = new PHP_Archive_Creator('indexhooha.php', true, 'opendir.phar'); +$creator = new PHP_Archive_Creator('indexhooha.php', 'opendir.phar', 'gz'); $creator->addDir(dirname(dirname(__FILE__)), array('CVS/', '*.phar')); $creator->addFile(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'test1.php', 'indexhooha.php'); $creator->savePhar(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'opendir.phar'); http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/opendir/opendir.phar?r1=1.10&r2=1.11&diff_format=u Index: pear/PHP_Archive/tests/opendir/opendir.phar diff -u pear/PHP_Archive/tests/opendir/opendir.phar:1.10 pear/PHP_Archive/tests/opendir/opendir.phar:1.11 --- pear/PHP_Archive/tests/opendir/opendir.phar:1.10 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/opendir/opendir.phar Tue Feb 6 03:34:23 2007 @@ -1,4 +1,4 @@ -<?php #PHP_ARCHIVE_HEADER-0.8.0 +<?php error_reporting(E_ALL); if (function_exists('mb_internal_encoding')) { mb_internal_encoding('ASCII'); @@ -23,13 +23,15 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version Id: Archive.php,v 1.26 2006/01/09 04:48:42 cellog Exp $ + * @version Id: Archive.php,v 1.35 2007/01/06 21:58:24 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + const GZ = 0x00001000; + const BZ2 = 0x00002000; /** * Whether this archive is compressed with zlib * @@ -74,13 +76,20 @@ * </code> * then the alias is "PEAR.phar" * - * Information stored is the actual file name, a boolean indicating whether - * this .phar is compressed with zlib, and the precise offset of internal files + * Information stored is a boolean indicating whether this .phar is compressed + * with zlib, another for bzip2, phar-specific meta-data, and + * the precise offset of internal files * within the .phar, used with the {@link $_manifest} to load actual file contents * @var array */ private static $_pharMapping = array(); /** + * Map real file paths to alias used + * + * @var array + */ + private static $_pharFiles = array(); + /** * File listing for the .phar * * The manifest is indexed per phar. @@ -110,6 +119,30 @@ private $_basename; /** + * Allows loading an external Phar archive without include()ing it + * + * @param string $file + * @throws Exception + */ + public static final function loadPhar($file) + { + $file = realpath($file); + if ($file) { + $fp = fopen($file, 'rb'); + $buffer = ''; + while (!$found && !feof($fp)) { + $buffer .= fread($fp, 8192); + // don't break phars + if ($pos = strpos($buffer, '__HALT_COMPI' . 'LER();')) { + fclose($fp); + return self::_mapPhar($file, $pos); + } + } + fclose($fp); + } + } + + /** * Map a full real file path to an alias used to refer to the .phar * * This function can only be called from the initialization of the .phar itself. @@ -124,40 +157,157 @@ */ public static final function mapPhar($file, $dataoffset) { - // this ensures that this is safe - if (!in_array($file, get_included_files())) { - die('SECURITY ERROR: PHP_Archive::mapPhar can only be called from within ' . - 'the phar that initiates it'); + try { + // this ensures that this is safe + if (!in_array($file, get_included_files())) { + die('SECURITY ERROR: PHP_Archive::mapPhar can only be called from within ' . + 'the phar that initiates it'); + } + self::_mapPhar($file, $dataoffset); + } catch (Exception $e) { + die($e->getMessage()); + } + } + + /** + * Sub-function, allows recovery from errors + * + * @param unknown_type $file + * @param unknown_type $dataoffset + */ + private static function _mapPhar($file, $dataoffset) + { + $file = realpath($file); + if (isset(self::$_manifest[$file])) { + return; } if (!is_array(self::$_pharMapping)) { self::$_pharMapping = array(); } - if (!isset(self::$_manifest[$file])) { - $fp = fopen($file, 'rb'); - // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, $dataoffset); - $manifest_length = unpack('Vlen', fread($fp, 4)); - $info = self::_unserializeManifest(fread($fp, $manifest_length['len'])); - if (!$info) { - die; // error declared in unserializeManifest + $fp = fopen($file, 'rb'); + // seek to __HALT_COMPILER_OFFSET__ + fseek($fp, $dataoffset); + $manifest_length = unpack('Vlen', fread($fp, 4)); + $manifest = ''; + $last = '1'; + while (strlen($last) && strlen($manifest) < $manifest_length['len']) { + $read = 8192; + if ($manifest_length['len'] - strlen($manifest) < 8192) { + $read = $manifest_length['len'] - strlen($manifest); } + $last = fread($fp, $read); + $manifest .= $last; + } + if (strlen($manifest) < $manifest_length['len']) { + throw new Exception('ERROR: manifest length read was "' . + strlen($manifest) .'" should be "' . + $manifest_length['len'] . '"'); + } + $info = self::_unserializeManifest($manifest); + if ($info['alias']) { $alias = $info['alias']; - self::$_manifest[$file] = $info['manifest']; - $compressed = $info['compressed']; - self::$_fileStart[$file] = ftell($fp); - fclose($fp); + $explicit = true; + } else { + $alias = $file; + $explicit = false; } - if ($compressed) { + self::$_manifest[$file] = $info['manifest']; + $compressed = $info['compressed']; + self::$_fileStart[$file] = ftell($fp); + fclose($fp); + if ($compressed & 0x00001000) { if (!function_exists('gzinflate')) { - die('Error: zlib extension is not enabled - gzinflate() function needed' . + throw new Exception('Error: zlib extension is not enabled - gzinflate() function needed' . + ' for compressed .phars'); + } + } + if ($compressed & 0x00002000) { + if (!function_exists('bzdecompress')) { + throw new Exception('Error: bzip2 extension is not enabled - bzdecompress() function needed' . ' for compressed .phars'); } } if (isset(self::$_pharMapping[$alias])) { - die('ERROR: PHP_Archive::mapPhar has already been called for alias "' . + throw new Exception('ERROR: PHP_Archive::mapPhar has already been called for alias "' . $alias . '" cannot re-alias to "' . $file . '"'); } - self::$_pharMapping[$alias] = array($file, $compressed, $dataoffset); + self::$_pharMapping[$alias] = array($file, $compressed, $dataoffset, $explicit); + self::$_pharFiles[$file] = $alias; + } + + /** + * extract the manifest into an internal array + * + * @param string $manifest + * @return false|array + */ + private static function _unserializeManifest($manifest) + { + // retrieve the number of files in the manifest + $info = unpack('V', substr($manifest, 0, 4)); + $apiver = substr($manifest, 4, 2); + $apiver = bin2hex($apiver); + $apiver_dots = hexdec($apiver[0]) . '.' . hexdec($apiver[1]) . '.' . hexdec($apiver[2]); + $majorcompat = hexdec($apiver[0]); + $calcapi = explode('.', '1.0.0'); + if ($calcapi[0] != $majorcompat) { + throw new Exception('Phar is incompatible API version ' . $apiver_dots . ', but ' . + 'PHP_Archive is API version 1.0.0'); + } + if ($calcapi[0] === '0') { + if ('1.0.0' != $apiver_dots) { + throw new Exception('Phar is API version ' . $apiver_dots . + ', but PHP_Archive is API version 1.0.0', E_USER_ERROR); + } + } + $flags = unpack('V', substr($manifest, 6, 4)); + $ret = array('compressed' => $flags & 0x00003000); + // signature is not verified by default in PHP_Archive, phar is better + $ret['hassignature'] = $flags & 0x00010000; + $aliaslen = unpack('V', substr($manifest, 10, 4)); + if ($aliaslen) { + $ret['alias'] = substr($manifest, 14, $aliaslen[1]); + } else { + $ret['alias'] = false; + } + $manifest = substr($manifest, 14 + $aliaslen[1]); + $metadatalen = unpack('V', substr($manifest, 0, 4)); + if ($metadatalen[1]) { + $ret['metadata'] = unserialize(substr($manifest, 4, $metadatalen[1])); + $manifest = substr($manifest, 4 + $metadatalen[1]); + } else { + $ret['metadata'] = null; + $manifest = substr($manifest, 4); + } + $offset = 0; + $start = 0; + for ($i = 0; $i < $info[1]; $i++) { + // length of the file name + $len = unpack('V', substr($manifest, $start, 4)); + $start += 4; + // file name + $savepath = substr($manifest, $start, $len[1]); + $start += $len[1]; + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = compressed filesize + // 3 = crc32 + // 4 = flags + // 5 = metadata length + $ret['manifest'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($manifest, $start, 24))); + $ret['manifest'][$savepath][3] = sprintf('%u', $ret['manifest'][$savepath][3]); + if ($ret['manifest'][$savepath][5]) { + $ret['manifest'][$savepath][6] = unserialize(substr($manifest, $start + 24, + $ret['manifest'][$savepath][5])); + } else { + $ret['manifest'][$savepath][6] = null; + } + $ret['manifest'][$savepath][7] = $offset; + $offset += $ret['manifest'][$savepath][2]; + $start += 24 + $ret['manifest'][$savepath][5]; + } + return $ret; } /** @@ -219,7 +369,7 @@ $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][2]; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][5]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][7]); } } @@ -248,15 +398,17 @@ } } @fclose($this->fp); - if ($this->_compressed) { + if (self::$_manifest[$this->_archiveName][$path][4] & self::GZ) { $data = gzinflate($data); + } elseif (self::$_manifest[$this->_archiveName][$path][4] & self::BZ2) { + $data = bzdecompress($data); } if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if (strlen($data) != $this->currentStat[7]) { return array("Not valid internal .phar file (size error {$size} != " . $this->currentStat[7] . ")"); } - if (self::$_manifest[$this->_archiveName][$path][3] != crc32($data)) { + if (self::$_manifest[$this->_archiveName][$path][3] != sprintf("%u", crc32($data))) { return array("Not valid internal .phar file (checksum error)"); } self::$_manifest[$this->_archiveName][$path]['ok'] = true; @@ -269,27 +421,79 @@ } /** + * Parse urls like phar:///fullpath/to/my.phar/file.txt + * + * @param string $file + * @return false|array + */ + static protected function parseUrl($file) + { + if (substr($file, 0, 7) != 'phar://') { + return false; + } + $file = substr($file, 7); + + $ret = array('scheme' => 'phar'); + $pos_p = strpos($file, '.phar.php'); + $pos_z = strpos($file, '.phar.gz'); + $pos_b = strpos($file, '.phar.bz2'); + if ($pos_p) { + if ($pos_z) { + return false; + } + $ret['host'] = substr($file, 0, $pos_p + strlen('.phar.php')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif ($pos_z) { + $ret['host'] = substr($file, 0, $pos_z + strlen('.phar.gz')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif ($pos_b) { + $ret['host'] = substr($file, 0, $pos_z + strlen('.phar.bz2')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif (($pos_p = strpos($file, ".phar")) !== false) { + $ret['host'] = substr($file, 0, $pos_p + strlen('.phar')); + $ret['path'] = substr($file, strlen($ret['host'])); + } else { + return false; + } + if (!$ret['path']) { + $ret['path'] = '/'; + } + return $ret; + } + + /** * Locate the .phar archive in the include_path and detect the file to open within * the archive. * - * Possible parameters are phar://filename_within_phar.ext or - * phar://pharname.phar/filename_within_phar.ext - * - * phar://filename_within_phar.ext will simply use the last .phar opened. + * Possible parameters are phar://pharname.phar/filename_within_phar.ext * @param string a file within the archive * @return string the filename within the .phar to retrieve */ public function initializeStream($file) { - $info = parse_url($file); - if (!isset($info['host']) || !count(self::$_pharMapping)) { + $info = @parse_url($file); + if (!$info) { + $info = self::parseUrl($file); + } + if (!$info) { + return false; + } + if (!isset($info['host'])) { // malformed internal file return false; } + if (!isset(self::$_pharFiles[$info['host']]) && + !isset(self::$_pharMapping[$info['host']])) { + try { + self::loadPhar($info['host']); + // use alias from here out + $info['host'] = self::$_pharFiles[$info['host']]; + } catch (Exception $e) { + return false; + } + } if (!isset($info['path'])) { - // last opened phar is requested - $info['path'] = $info['host']; - $info['host'] = ''; + return false; } elseif (strlen($info['path']) > 1) { $info['path'] = substr($info['path'], 1); } @@ -297,72 +501,16 @@ $this->_basename = $info['host']; $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; - } else { - // no such phar has been included, or last opened phar is requested - $pharinfo = end(self::$_pharMapping); - $this->_basename = key(self::$_pharMapping); - $this->_archiveName = $pharinfo[0]; - $this->_compressed = $pharinfo[1]; + } elseif (isset(self::$_pharFiles[$info['host']])) { + $this->_archiveName = $info['host']; + $this->_basename = self::$_pharFiles[$info['host']]; + $this->_compressed = self::$_pharMapping[$this->_basename][1]; } $file = $info['path']; return $file; } /** - * extract the manifest into an internal array - * - * @param string $manifest - * @return false|array - */ - private static function _unserializeManifest($manifest) - { - // retrieve the number of files in the manifest - $info = unpack('V', substr($manifest, 0, 4)); - $apiver = substr($manifest, 4, 2); - $apiver = bin2hex($apiver); - $apiver_dots = hexdec($apiver[0]) . '.' . hexdec($apiver[1]) . '.' . hexdec($apiver[2]); - $majorcompat = hexdec($apiver[0]); - $calcapi = explode('.', '0.8.0'); - if ($calcapi[0] != $majorcompat) { - trigger_error('Phar is incompatible API version ' . $apivers_dots . ', but ' . - 'PHP_Archive is API version 0.8.0'); - return false; - } - if ($calcapi[0] === '0') { - if ('0.8.0' != $apiver_dots) { - trigger_error('Phar is API version ' . $apiver_dots . - ', but PHP_Archive is API version 0.8.0', E_USER_ERROR); - return false; - } - } - $ret = array('compressed' => $apiver[3]); - $aliaslen = unpack('V', substr($manifest, 6, 4)); - $ret['alias'] = substr($manifest, 10, $aliaslen[1]); - $manifest = substr($manifest, 10 + $aliaslen[1]); - $offset = 0; - $start = 0; - for ($i = 0; $i < $info[1]; $i++) { - // length of the file name - $len = unpack('V', substr($manifest, $start, 4)); - $start += 4; - // file name - $savepath = substr($manifest, $start, $len[1]); - $start += $len[1]; - // retrieve manifest data: - // 0 = uncompressed file size - // 1 = timestamp of when file was added to phar - // 2 = compressed filesize - // 3 = crc32 - // 4 = flags - $ret['manifest'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ce', substr($manifest, $start, 17))); - $ret['manifest'][$savepath][5] = $offset; - $offset += $ret['manifest'][$savepath][2]; - $start += 17; - } - return $ret; - } - - /** * Open the requested file - PHP streams API * * @param string $file String provided by the Stream wrapper @@ -534,7 +682,15 @@ */ public function dir_opendir($path) { - $info = parse_url($path); + $info = @parse_url($path); + if (!$info) { + $info = self::parseUrl($path); + if (!$info) { + trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', + E_USER_ERROR); + return false; + } + } $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); @@ -556,15 +712,19 @@ foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if ($path == '/') { if (strpos($file, '/')) { - $this->_dirFiles[array_shift($a = explode('/', $file))] = true; + $a = explode('/', $file); + $this->_dirFiles[array_shift($a)] = true; } else { $this->_dirFiles[$file] = true; } } elseif (strpos($file, $path) === 0) { $fname = substr($file, strlen($path) + 1); if (strpos($fname, '/')) { - $this->_dirFiles[array_unshift($a = explode('/', $fname))] = true; - } else { + $a = explode('/', $fname); + $this->_dirFiles[array_shift($a)] = true; + } elseif (strlen($file) != strlen($path)) { + // if the two match exactly, the path searched for was + // not a directory, but was a file. $this->_dirFiles[$fname] = true; } } @@ -615,18 +775,107 @@ */ public final function APIVersion() { - return '0.8.0'; + return '1.0.0'; } }} if (!class_exists('Phar')) { - if (!in_array('phar', stream_get_wrappers())) { - stream_wrapper_register('phar', 'PHP_Archive'); - } PHP_Archive::mapPhar(__FILE__, __COMPILER_HALT_OFFSET__); } else { - Phar::mapPhar(); + try { + Phar::mapPhar(); + } catch (Exception $e) { + echo $e->getMessage(); + } +} +PHP_Archive::mapPhar(__FILE__, __COMPILER_HALT_OFFSET__ + strlen('__HALT'.'_COMPILER();')); +if (class_exists('PHP_Archive') && !in_array('phar', stream_get_wrappers())) { + stream_wrapper_register('phar', 'PHP_Archive'); } @ini_set('memory_limit', -1); require_once 'phar://opendir.phar/indexhooha.php'; -__HALT_COMPILER();ò\ No newline at end of file +__HALT_COMPILER();ã+¶Ã! Þhs-”nh=„è±=°¢Ø¾]7ëK‚_¸Á1݃8¢ÚÕكk= +Šß…„||QÎËÙ՜ÓòœK ßЃÝ`‡í€ð‹Âšñ D*͗ì;»xI¥.×ïꟅ<~š´GeM‡°×Þ´J%ŒRJx½ÞÔ¯vï7Õ¶nV›-)ÊélT)/ép¥6åO¤uc‚Â[b¼!‘÷äª +Âäœõ²z¼h’ üõÞ¼iª•ï}ÄT"Ó¿Cü Ïњë^˜ðüW°\>C­<ª÷˜ÊNFGx?CS¯6ŠX¥9Šè'”é}í­C#x:ö¼ª¤©R‹þƛ{DV n/Ù±õj?œèÑö‚H’_úˆã˜×è“ӆâԜ ¿âÿÀ?S£ ±?Q›Ï~KK7ڀy·d'û§„öÖ 1{¾þÐÐØÉõÿ\$Ýxúˆ…„³TØ3veí(úv (™6QÌä)”=–#OŸIv¯éu’ÛJÄ0†ïóc)¤낗®­X,»½PBigmp7©™Ôâ»;éêÊ"æ*Lf¾ÿŸ™dY•/ª,’7£Í#ÔpÐwµƒWí;Xk¢\êŠ,[\eqÉÇg}× ½„ß<ÒÖ¨•­[lªešÂ`ÓYô¤{9…OqvʈËâ&ßì5«šHá›&O‰,ªð™L€†¾·ÎÃhj«6¾:|´CeMƒ Ë«r2sM§_;`Iñ)–ƒi<§Ãú½«M›Ä¸1¥bCò1ÂÉ äjQ͋ó*e¦œ™nßÉ»ÞRÓ>ÈÈó GxšŽc‡1ÁD& +Š„^¡sÖ© ¸B—ȍ¶L§âÛ0´Ú™z‰RaJ¥ ¸(æùyu;¿W‹¼œÍg|å¨ “7Ö/íÀˆÿÒF/;©ãy›ß­5È¡qù]É^D¼ÁokhÆðG@Ðf3úè>¿Ü/¥ÝŠÂ0Fï}Š\ÁŸp­·¢°Ë–êW!6S¶NkÚºûøŽZÅ¢ˆàE`È|œó1£<É[v•± 3Œ€ñ`ô=%fý‰Uf¶G)>x’ûV¨6pÉE‰6VÐCµ!åtþåKÙnZNt²!Cøc„5BÖ*Ájø?‘:,ViÆc“feœU¨i¥,¿u]¥õ”W彚õØç<ô'˟p%~à…ôÛ@—P”µù¦FCU¨=TàÞñšâÚ~\ajðW8GÊQ±~p”ú®o˜u×ÕÕ64Ü³±/È(àrHË/HÍÓP/ÈH,²Ò×ONLÎH-.I,Ñ è—+­Ëåcò„}<HË1SÌ8ðý*È%˜Cì#>Ì­!u7h­‘È'±¦Ž«Ü!ÿ Õ8¡·—— áµ9Á†)@;”+2ï?ž|äÇ”Ì +¡&ȝ5òo‹§e“ÿåTȳ8)᝞re(‹®¦º'¸0yñVaÄ!‹A•AbfÓbDr¥fÙćM0ÊC‚Aǀƒf@â6ÂÍ7A#ÜBQ¸t Y 0dz70õ˜$ÔÅÐZK:+ƒV߁U•S+¸¯›ê®}n^õ¶ReSRHYN½QG"¡õSalϯ™ÙCvя]&ĐqEl<Ïá‹)!ÌÓä|„m0Î.Õ®”,}?˜OL+hü÷öÎäÃÑþ—lY¿HÖb(íÃ~pÀÓìœEJ­oÉ¿êEÑr0鮤<é“Iÿ7™¢§?]æ°p݊ýÒþ+‚0†ï{Š][PI]tSYIAo0–r¤sµèí;.‹¢èb8ø¿ÿç›Ó¹Mmá\kY˜6‘¿Ä8ÕðWª*p@Ÿüáöʨ#<¹8M4 +:Få ¤ ·»µ”Ý.%…½½‚o€÷+(«¡[ê1¯،•JK2‚79E£±ÃHË^üP%ØÀ•‘œlådûáÆzŒg…9ÚT5¶ +ß»ý@%I¨3x¢îó”êÕÄ÷Ê¢6™6'ñ*~¸´?êGÝ;ôƒ¤Î­ û<¸íYíoÚ8ÿÎ_a¡j °®Ú¾°]W®ÐµRïA¯ÓI“"_ò+ô+rð˜O+¤ž€($>ԜKãîc¯©mZº¨ïÖfY¢Øæ ‚ÒZFµuX99åû¹·;C÷µ*H_%€ÏSrO—-‚|‹oÍU¥Ì§ARµ_«ÕF*ºLêÛÅé¾L„õÆ>Lјˆ6Æ#Þ4^¶ÌOœa_…<Kùÿ£?À¹P*¦Hv¤à*Vôœ$ä[G6¹ :ېgIâo(¶ úì¬$M2Ÿ#f&ӝr¢ÙùSg.‡Qͺ *­³Dµ)U›ü@ä>Sú¯1¬Z¯£Fé:K§|Mç·NVNÙÞn¬E£98„pØ4%”AÐ@’#5¦É1Ÿ†šsñ´«…¤úvûúÌZÍT Nëâˆ%{-,]p°–žM‘}À/5´ÛTÞÆHò·Ù.êÎþ-¾Xf‘eõ™rp™žõè11]>g!'ˆ-­$)Ÿ#÷“ªÝÊ4Í^æþä6Ïi°ñZH§ÇšGÒ`µý?UÅ¡š3.öõՆ˜½(¾oOÿZk#ìOWŸ0ní`›ýrj,ŠUØqØ×[ç§Úø1µñ{þ.DzO ÌÿvJë³wŽÅXžÎâªGÌ Ä­ٟÖÑAÚc&“™ù{~bP0Ñ¡VВøÎ’=?ºnZ—Šs# ¡&’Jâä4Á‘¾ QæwrÊÜ ©Tñw†xœ2©H. Wãoseº´¶u¦ŠÇïžEÙoSn¤m§(˞–L:/ +·î]öAnàì ÁÅ@iüû…‘Ò­¥(º~p·«ípÀ½qßã-b"¤yÕßõït9àÆÒ„æŽ&ò‰Ý||‘Øz.Ž~!‚éeYØCO—K•Tt¼+ž˜ÎAçLuἚà®gŸÂÚ¾¤;&i,xJ!c’'[jDZɉcºða6ß/?ϿƋ0šÎ§‘E+¥Ž.ÞSÙe<EßG­¿‘)_¯XAM>ùSZuaUª9kÕ¬ÖÁÂ;Î4\§O…ÓyÝD¦¶–;ê`ÊMOW÷¶×k‰ôŒÿñ5wDœÆ²;I”¢R‡»¤Pv"eò 6ÓƒË øýS„¾îø<ðGÁªo¨ÁÀy&T¹ª¸ F£j{µ|fàðÌÀ±ýíøÎDç­È÷Ú¡ß(øý“Œ>’Æ&a\ß­’­È(iù%¼`|Cj×dÇ2Rû%ëV$V9UìÛAîp’h¶=µa²}…K_áî‹M-w”¿/DºQ5ƒCÛÙ㼚§cúv˜6ÓI™à__5Iá—_+ÎÒ]JKüÛ²NrZâÄjÜþ+c;w@و§©ì"¯.±ãξåØtbÏýR+öü›‹Ç‚ñmô6&ç´­8¸?¥Ñ]kÂ0à{E. +I‡ZvíVWjeÂÆJ•Á®Â±9˜0M»Øº_¿ÓYÁï›]NÂáys’»a©ËŽÃÚ8”…Í‘ñô1"—k³Á vUáúÔÅWúžÁÂw}¹VÆ ZV(¤Ož)}Ðñò-Èî™ÅOF„l ÙF n¬Â/]þ¸.ãE‰–4ڂkö‹¾gõBPj´xÜeà| ¿NƒF¸ÙZþ136K¼â°>M²$ž½dorš¤QQI§¼Âuu»»ïÑ!kØ`JÙ§úeü`~Òj»4ö]xÐèó3ïÙ~Ér¼y/TõªTÃ_µZmSÛHþî_1q9‘\kCȧ+98‡%Ôe2$©;B)BÛ:ôáXþûv÷ÌHIS[—ÚÍJšî§{zúu¼ï?¤«´·(b?’˜å\ä<ËìA~ŸòDðï"ñ1 b>ì=ôü L’°Ý]vèž_̎§C–ñ¼È⠑ +›h¿žuà™J„k•©*Je-¢vkõrIÞp%iÒ,ˆsÖ¯Vp$Äҕ¸9Ó÷¸?é=ö¶·Ï]¢uW^<yf[Ê-º½ÍX!¼%·‡½ÊWå)”‰‹07]ñ0Åõ¹Ÿ«Úi—arí…lfÉ2ö"ôi/ ]?‰"+ÔþD>D`]$)m ÂÃé«5b֝¥,1pvx0sœ@í•*Ã!{(-»H‹\Ø +t¤ 6Þ[òü.äŽØëƒ)ê#ã¡à_rc¯vÕ.Ö£*K×@ª ­›ãUi+Æ^ Cʍ+|pû(¿²(™ó.VQcÅæ¡mÇ.®óW—ùºø +Ü/=1îXnÔÅpÚLôjÄÆèqˆ4„.cr›j/³óU X$+Y§Û}-5ò”e&•òZE¯5e1âf |¨ ­ì{®‰íz ÃR.šª*€‰%AȂÐ*S+rs¯Çÿ²AøS +èLø2Î cÒ{j»ÊBIú[Ø.û* ¶¯_gûäŠvÿŸ}:«Hû[ÐÀ(d{ØEþòã%tdu¶ÿÁg÷IÆYcÉJ1n©íßÜ&åñŽC3sm}ãÈê-ˆ+\ÕÁRù]¶êF,äðåZ“M¯ÊŽ¥ï€.U&U±Ãޓ¸ r½º×ó³Ý«å[Ncvkw®&á5ˆ½™4Цh͍<‡÷H–÷ZÐ u *µ5N×+Ïu+lCkÓn£®Íw«ªÖ´¾—ú}é'e©m ^nÈ Yªb…v«EߺÝè8š§!­m‹muíª­6@,FûYîìšh=t)á4¾Ô¡”]̃SÙ¥Æ{I‘ØúÑË=»OzŽ J¹'‹y]+0ª'èÝ/²ŒÇ0ÒõÍcØAg<…™AÈFH® ÕÍ1@÷a#Ñ=|¦›<¨<ؒiDš±ADéªòjÂõlX÷»¯¾÷)z£¹ÊãÒÆÃgóæ€î‡€áÒüÞV)kÈ>èìÅês&›¥Ùy#1ⵏ=¸á÷&ÀI¡_YüW¹ØÖ®5R Í$YnÛ¦:;4Œhk¸A’þøÿ×E¦‡´͒ÑU+K?‰uލլ?¢M4½áŒGÉ-Ô²ÊK,»‘º`å±ÖÈôäòûýF½þ³b~þM1ãÍÄ|mˆ‘ý“ü)ÁRÓoÝ,톣»ihSe¤tù*Np´‚!ŽB°e‚Ù,‘™IVô†%d&¸Tz…iƒxŸnF¨ªu8S™.J #]󓹿ÕëXµ3an+o8pž§0ÓmèΦŽªO•ð+X%ߐÜoȰ¨WMbhJ豀&>Q‹ÊùäÉcŸ +ãXèOid·J‹ö*#¥0¨¯í9f‡_f'º—¢ßr­öÂóÁ!*ß)“…bRB’Ô”±þç=úMgy{eng@7™0¤.i “x©_¤§kuŒ^N‡0‹ú*¨Q©¦ôߝ†[qµ±úÍj¥OÍÅÓs:·`| +úEÈëÛ5ReÕN+ì~:QàF1¢>¡Òžs—{Ë´y e(}âÓçË0>8urÓûE ò£”0ÆeÌ}°ÙaÌñqŒ¹Äá³Kü($ [Ú8™N¿Mó³/_þ8;ÿk¨'¿‘B‡ÈZa—IÁçn݀ÁÖ '8 Ý-¸2șŸªv2ø¥qà&O%Úu*c%Õ wÝQ‹˜Wlµûð³ jÕ"îòлLâ¥meÖ¼k°þ¡Œ´èÍߟ")yXpU4œ.0ÿŠãØ]`;µ¸Ö΂ƒFc‹±½ž Àw³\`.f\¸ =i·Êœ²h»0v\=,ïú E†yÂâ,Áå ä­ Ü0’„÷ñM²p„Wœ¹ ‘3#€½%E½ ʑ›‹ÊŒô²!Z±\MN_P97¶S5 PªÀHE&ç=šX›„h<FG;º/„¥ÚÆZ_IÂÆ˜ +2[ᩦA (Ã.¤’#€‹âhÉi+´_[+°DYsár!ú¥)Y¨~|¿ÂÇ~.^Dž€&XÚ\Ô³Ð!Rju¨=D½Y¯2y0Äz7äBFø1k´Çyk2ÌÖ<ϕ†ÐÖ'ëm)Ÿ^Iʸ±ó»Fê©ÒLëm©]Ä`WŠçsÒÀyn²Xò!ºIxÚtg”²£Œ*¬ÕEÜYžBz}4¯`Ò/ÅMÿ6¯báðt\Vš-ÙæµÚ L†ù-ûPnP +!Ùl +›ŒnnL)!”–‡I‚YJ¶(6R6MuR +γlùsnÿ4w¯;Í-ë*»¥N“U ‘D(î¾ýp>¨ªö(á!)Û\UÖÕ|¨ÆãKm—åfF$ã¤æ]—åºcJÑ|Éè©KÈÏڂšöG¶iÒ:Ñ¬'Ð¿çù;¢aØL׿ËÓ׏Ÿ4LéöÀ‘Ï+Ɲ¿a§"¹¶#X#ɞA4sv"ܳÏ)õšŽWÀˆļþZ¡ÖË+”,]'¥xÉpÁ¾6/"9‚J„¸áG‰À>5ÜúIWÆV͈•*±žt›ù3†[™ØWÄqîUWw@­C+ì÷ÔWmhmü H¬í‘Ü”Æ[[¨ßÉáÞßÔÝ=Cª[ø»{›ekñÏÛ6­4íçèñ;* …ó\ åˆ/1Š ø’E)/GåöÁîþÇÀnèOb@{ÉY=Êï/;KýûKÏm÷ºÊüý`Q +øîñ  ·G齂¼O*O~%0¤½u̙Ãð*}êºÕ›±ÞPxÒl*IÖYV©„”Çbc­¼ ߣ—¬œWR«uŠ'}óÐ!ª§ (ŒylËJ­#*µÕ‚R]яh naç1ìáð7öÍ÷oY+ ø‰µo9TôëriêmÙh+p\*uƸʭ6–øl©^ÿÐ÷E¨&FèPD¡€è* èmÐQ%':´äÐח–4ˆnùÀm÷f¸7—!{àG#Ð>ÃTНՍøæ_N²Ô7Ê´»Ý+»Ì–¹ëÔ\u;ÎՍ†·ˆs%¯<9u¬¨ D#ÅH!F’«Ý®³íô:ÍuÑÙÝë.Ö€ú¦E=澻С«0k¨&4Ó¬‹‰‚âŽ2_&þTŒ°Ò‚q(B‰:ˆ“èFð)Â0¨t¬V%ѽ“a&“©x±&~T*+*€ +t8ÚcÚ‡ÃÐc•Àà¢@ x-؞†›J”I1V ’hbT{”©Æ-׈aâ]ÕXÕé +DðPĎuFi‰íš](ó-±0ËþYôEû¾lo9=±±õ¯ý6nΞ^œž´·þ'ݹ'c•Hܟ»ýA¥¤©¨=ћ{øZ)/µ©d‘c~­£ð?™J"~¸9÷ð±b2÷`¿RïïU´÷ða¤IYÆ÷—l-ַʇƒõZĜÀ8ЉÂÚ^'ÒM³Dyⓒ>B¥2_cqõKä¬hƒêÎÖFNé‘4ªuÅÑùF‰ úðôðJ&‰œ®®Ý³©ÎÞFï®MY™ä²§¡øŖԐì5ÀÙuqSŠ>…°¤ëFbµ‡ûÔ1p€»„M™%Íþ¾ÌŒx“ÁÜ`1„YG6§"‹ÙôL6ø¬ÜTDCáEd=R¸DݾI“(õuˆa/gä§î31ǪàA³—ûmžæX²Û…Nôÿã ¦Fùï¤Ú7NÇÐÃìZ²¬ K? æ« Jè8ӉÄ50>¼›`)±-v‡p<ðf8C±ŠØ²’;%³T3ÿåd æ‚ÝÚ8â骄Jž^…‰çj BÄu<1ÍþϾ˜ee <*D‰_–©<.Éµ pK$̳?7”ïÄ% \…š|@ñ ¨RœÒ`rÿwx"áØæ–ˆrw*ö õ]îX"‡¾†‘ήYqz­ÎQr…£x´;Vî5ô9_¸ô•#v쵚^©[ÄS̞Ýy3Y }Ážñ—ð£(.ÜìÓæ+éŽ]< çaÑAìØ™ 2/(\(ê‡ÃƒÄ‘%ŸHøË™Hÿº`/]τãJßH÷ZXVMT{!¿v#ýL™ù÷ۅrÞ5„OÊ Xd +pS¤øˆ8QȆ¾él8ϙ ô i§£zfÔ2+Œ7 Œ”yGƒ„œLDH›§!ïBâ$©Ä4{Æ9%΂þ;¹U(âp[\êÀÞ:Î-?A¾BxʖØ¢+òÿöv¶·zˆšÿrýa¶YåÀô^ñ´çô¶ºÍÊôãÏÆR"ûsf––Gl÷€°QŒÇÐÝÍÍ­»ºÝ§[af¤+N‹ …„)-Üò0 ]F“&L¦h¨º|õTÈ  ÆdIœhiŒ£‰0=¤É‹#ٝT<«[ýrLüÌ+^é›ÕìhìãOJKNB·ˆé,Ž„îq¼B)b9ƒGr¢¨†¾»Æ_¬*Uà·©þÀ.d &VŠWnö‘ßž#{·2<×îµ_-ú–­øæÀÎJÞN÷êwzWÜú4镧,%š˜+êÞ]Q+ςX¥Ü™Íɦ)"Ÿ"Ê)‚¦ØîCeŠ-;eiØëìÁÚ§V¥f!“1’ÆFávIeeF™k{)ƒ,E’0¡¼ã1Já¨R.r²Ä?o?uÜãèæýÆ«X½ùÙüöë'ÿ÷£îµü<üz–ýÖÛMß÷ޙAϽùí×wŸå¿÷ûÅÎäìøpòáò0Ãütpxpð½ â@$™^iï+¦0Á¸"Ħ”5Áå1bu \ S®4&ò0ÅA\5fP‘…6}­t1 )3«´fk™õ<Iá5áÕP­@j_PzÆiDr"@YÝÆD[fW ¡‡«”;‚éÑÔj tÚ°x;ûƒ„wo7cÞ"}+1„` +Õþls¤®=iY}"9FˆádÎJèñîxîlÁO¶Å¤}qÞeäÛTa‹–m@þâsfˆiX|ïN‡œŸã ib®+€O{ í 9DÈ+d8ÅÊЋ õ0)­€i+"“4„ V Ô³2žî“ ¯wEœ¼²f¿ð¦ÃNÿB)q¬‚¨lZղҍ0Ïd™Ad͛ê\äÉE½bAÛ"'äþÀü">PïˆsK8¬¥MÑÇ/fÆ,æYÇIP-IJ_ùòîÄMò<ä¸T§6P$Êé~½¸xÄa™Þ÷¹±øÓõ™®†YDë݅(6¦Ái6ˆ>ƒÃ5ÙhDzfóŒ‚_CˆáXrÇ.×çÜ,»B8¼žsZìÀÏ[É ÑÝÑË=w¥9òPvƒÝyÄ+]VˆÉcd)ސ¢–C°^º ©ÿŒïEÀå°3 ²$'?0ò×>Ù2Û&)i"<üíö2â’Ð@Q•ŽõHsq£´†DÅQ‚ç¨cë,"W~ަucZ‚óËoQõþ»“·nµÒzMÝ(Mó«%ΑS‹ÓÓùd­{#{®1´µ1¥ÆÍûE!2rr…ºf­ÛTPLÅW¢ŸW6ý¢á€DÓîT¦éëkEJGlÈbVÊÉU>e¨‘d”i:aKäpõe·‚(èLmó–@IJ{¢F*¯á”ࢆ 8]ªD£*ò§Ï€”õ[ {R<¦Ài†x§;Šâ5-;]Á+%|ª€Ù<‡’ž–§neŽ“Ýy + (†K˜Q‚ØÅ*ôÒ?(¦su ¬ÞÙÞÙí<ßÞìt¶{;|wrУö+š=ñ@xü†Î¦û7¶{&ŠÖ3y»‡Âö ҩ餠îIޅÎ8ð'dè°;jÒ°óE–ôœ“©ÎÎn¹¹Ÿºà +QXP9¹uw©ÍÿÒéÌù:@§ÃŒ À[À‹h5œÙ倢o“¨sæU27raѰ'Ñ(‘Açå8î"q_` RòZd54(Tz4†ê%°boiÃtí’Kóà¯Jªìٛi±$µ¨2vŸ›wqœˆ%¬þ‹Xõ£<ÌSn¿¶.FÔk(7¼°¼?m@ürN¸*u+©̵ 5Há8#µÚýö +J°êBi?)ŸKŒÊóòAÜSNÔïL=f§D)H)À©p~k:«nMÓ®¹Jäd¹«$üᆠ+ݶNOÒÈpÄΕzFH•2lÀµ¢a‹ rC¶¥" òDÎr)Úáyfò4q}¤Qé'+çìÌo’×™XïÍ=jà0+BûëÃÿ™5Å|N 7dh&\0bÐ2J­¢pí¡-Zª¶ò†Š ÓD›qÉìÎ2jå)~ ’ò¥8†µNCOÅ`(Åx³WTìb\?§2åý·¼Ã*›»q NeÆM(¹]/¶­9ØH»¤+c{‹Î/ÎZEjuA–Í! +©es¨1@¡æÁ#±éÛs+ɳª‰ñËxŠRÑcém.ÅÚCT:¸†¹+æÊZÌ~6•|…DhZIeÝò4ÑҀåQ¥†v\óS5aç¹}R©–§ë¢tP܋(]ÙUô$¶>¦w$s#çvîPfŸ²Ý×AjØ´@…S**•³.&ê~åGq`Ú&Á¢£ ‡ž—¿FEŽõ. ¼p+rà[™A)ÃBI)˜É8ÊË>[0æ -vÞÔÈN-Ö\HÛ²sÊææEEî+ó£‰F}¦¸µEBO":Ù~WÔÖWüÎۜÛÏߟ^œÌŸP Éh¯ðóJÖóZÕé#¿`ruçü =—ÙÓØ1Õn¸8ï&6´MI„Füzöþê2¡³U²#Ôk5¤Þ$½Ê”âÙË(#­?]Xy3¨(Î ë2òí†y´0H}$©±ö×12}¹ßƽor.¨ó7ew”¬@DÚ½Fº8fœÐG :æ[ÈêøÞWu‹îAÕº·œ »”©u¶»îv§÷êfÿâәø‰[¶÷ûD›{E/¼ÖlºÖµ±Ç"Oz òn7~Ë$A”± =€=‚8âð~gQ}qÂ# $3¢¨Ä.:G:,¦òl翋8øt4Ûä…³XŒTH½FÞ½Ö_lh ˋì $äË{ÖnèRJ±™:Ú;m˜´ HZ°$¶[ìTÉrÚßʱs +•~„e‘„îiwz›;¹²ÁÑÞ®Çì±[ŒÇ´“3àŒÎø+¸h^ ½xúD5A¡qŽ«^–Ï!»-‹ðòvûŠºç‹MµöS + ÑXôy…åû¾‹å\Ó ~ìE„…Àj_NxlŸ{1µoωƒsÛßKUB á-¨ŽL]YœÎ,€C‡;x °”@ëF« ç¼ô Ñd·Æ>0óœf¢, Q“úì‰ã) mÓ¡b¢ÏÓx‹&8Xú^cŒä¯â,^äèΠZXòzp=¦“'õ¹l=•FQq(˜ƒÿ}ˆ:Ÿ•qèEóXo©X¢c½¼HµÆXÑÉ+0R–µÙÎï¿·?^\<€ÚB!aÀª’( +0Ÿ•/uéÑ8dšŠà‡W³AtóÑh?ç(Ó1ò¨º wIÅ"Z¦H7 W~– Ìò%ÁZ>£LŒ)¾K6ÒäXÞqv‘ö.u:»={"?¿²Ó©kÙ*>Pö}80ñ‹ep󆵸¿F¿<fÝ%ð²ÛKà%Sî^-D쐎zÑ窖č`q^n?óö•Œ«Xe¾;¦’¶˜vÅQùò"#ÌXv;‰s~vç„ÑrŸ¦³0Ç÷†Þý"Þ<ù혾Q²4)XØ _Íȹý·ìÇ ŸÌ&€[¥%»<¼¦Lt”¾ÀÔi°Œ©úµ©_ÿäNëQ ù·QÀ!…#Mc8D=÷ž~?Œ§‡9£ùDz†Ê‹éX,¼ìZÁ¿fù]{À‡N1ñÏ'"5L`¶Ê\§Qœ³hîÎáB*Ž,¤7>ž<m¾tÆÚÄÒJ¹Dÿmù{YH$úkO•>Á0:4¾ÙÿñøD—–…xDKù)xÍ èk˜Ýf¬¸Ðÿƒø‘G]bÔ²p}z‹¡¸c}›sÁá÷9\&.A˜ø)ÜE-$D6c¢ ÎH£Îp½,œÈ`6¯Ÿ¾ /Ž>ž=‘'/Öúç¸| +-¢ÙÔ@Qj{ZӁ/G£â˜SÔJáñAP&¡ç+;5¡b¹¸S&+-û +b«ë|Ž}B¶]Âo¿d¯vÉ<)øöKnÔ.¹œ¨ /Ýt‰¸Jï,¥ÑÞDýp71löïÝZ^ .ì7 +ø“>ˆvsVs?>hÔæc +n{†H;M²ðšcpcwħòLÒc( +>D%¦‚k~'G´2! ù~åwo#>_7àóÇ:¤#þ¯rzØ70þ2ÑIÊõe¿ò«ü Þ€UkÕ¢.哒•ƒEï£QDßÛÒ£ðIÈÞé"Aw0Tüœß+OcÎxzwUì·ù[Òûüáðþÿ•RÁJÃ@½ç+†€$Óx®b)šbP0´9èi ÉÔLgcv«ñߝM¥XŠ÷²ËìÛ÷ÞÌÛ4­òU•¦A…ÖÁ°e&~&½®pº–"¾mi@+U6œâެCv@ìp຃5u¤éê¾(‹…P]ÍzÝ´†w‚´dXu¦n±#O% | m ²¯ÔƒóúáuG³ðANÃèáÀ]_ÁìZ”ÅC~Ðù±§ 7-‰£ÆJyŒR Là¶Xæ7ÕãòY­òr¾œËQªâ¤wÊ«J«ý„¸þ#®¨¼+³ùÐhzG‘{bR]i·éƒltÉáéüV‹þ‚s:‚ŽÑˆ™ýð|ZÃ(¥q:ùS)4>œfÙÉ㌸ŝ6F×¾±`Q;‰qlg +ù~ ÙØm£}¶Y‰ßÉ'ð% ŸNxBú»3 †¡#F9~¥Ë +Â0E÷~E…DðñjµhEA±T7®†Ø &¨i­úùFâ{ã"0d.ç\¦ÙÎdV2¸-”AHu‚„Fƒ¨˜Dª=Ö»yžššMÑÆÜ˜k¾Ä[.‘BfŸæd+w.&¤ŽÒ-¬]‹ð³/À§¢óôKTÕâz:›N¸÷Ÿ³pk(>ªõÚ )d®“e ¿ðÕõ ã“ë eE„h"œö–çâg†Éô¦9ö?èTžVƱ´JeŒR% áÇtÞ\-oçjÑÌ.ç—üå( è’Êdì¹:ZqGNEL…´iÓ* Á‡(¿ÀYyþBó/ ¯•É÷`½ýh¿³ŸIVÁxBõî›û·ÉÎÀz4zS<rdpg½·:›w:äýÖðY¼[M>nW6¯7W—5¬5ÿ$¾C‚˜êM}XORƒÓ¢¸Èg“øxú3q´GÞ°^r0ž u„ðUˆ‰N|ƒö_,x²œÕæ»>¤Èz–PðA´[ƒªÓÉ~“Ÿ£,ÿOè_•M ‚@†ïýŠ=*”Kç>DLIZ¬K§aq\¨ÕÖÕúù-ºPºÍÌûÌÃÌ*jªfB©Æ['5B­J$Û1벒=†6÷–_„ÁÖØˆk›ýÜ¥‰FnjíÓrlɚ(¼ ‚Á¾'•ÀÇÀψѯµù† ‘É úBjůèdù>HH¶y‘&§Cq†cÊâ"¶¥w.œïMþ¡myÌ¾ò¯wüߪ¢ÍŕooÚ0ÆßçSœªI +°‹{–Ew]Á³ï (±Ý·žŸrƒÍD4ǹck†ðR­5À…“\±oÂ#S)×{ˆÝ‡gkxIþoÆü½{‰+&1<F˜3É£%:a¨c°¸Oü«Ù§É×pê£Éˆi”¬ä*Ô©©8yƒñ˜l±ìà6hŽdœ²5ê˜ÃüŸ¤É_$,CoW…™zšP½d³[”í%]q¦à=p|„ÀMBò¤÷ÖQr…5 +IršMDŽ|SÜwÍæ¦©Wø/þ¦Bt@h{ÎÝ˨(P*ÿae…ãÕ!Q˜eÙÖ§*ar{LÁ;!³C"'txçZY‡öA¬µ4єÍl§šÔ)(0|¾qÐ9åÀ¬­ì`«ÞY¨ÐvK9#z£»x¬}aŸŠ4õý»Ðÿx]}ý¦DU)“Û…x{}‰ì¶ÎB÷Ê»$V’.£a»­Jxï/é,ÙÃ{­ßZ¿ú<9/׺`Oeç(±ÛÞ9ìÞ/l\#ßÑÛ^ý¼ª¼-Á‘-Ò»T”8š¸¾µi¢úú0©i˜é ¨ÉP‹üï-¾9ô rº5¸Hi}âÂ:¸ý ÅTÿ‹Ó0ÿ=ÅóšÀÚu;w¢›Žq×Ãq¢¥› ˆ„Ú¾ÞÂuIl»S‘ûß}iÇy…Í´P’|Þç}‰ï¯£ÕÚ÷ÙëjÄ¥¯!…ÀnÒ +ê¦Ât Û~kP×ÊèOÌ÷WWËxyIz³¹ÝX¦ +à·X9©ÌÌÖ¦rÂ÷ð&A„mfžðƒ}˜m xõ²@ì‰otùŠÄ›²»Öè£{¯²4iŽ9÷\0­‰‡êN 4b^C?Xo +wlþ’B¾\¾Žî®ðËNU(ÎrUét‹\JǑR@+KÒÂXÔ]¹ž‡=3C—Á¿ø]•\Ï©aSv›V2ßm-/,KNQŠJ³–;t+Û§@¸7¤Ý†_àÃòR1K^F"%Å X$A®ƒW_”m-“¾¬P´`EH_á8Ûå@®ä\ðÜxæ¶ϺÎ'×ðܑúýøsR×ìiÍ5˔Ì\Kºba–¹˜,kC>Ž'ɇٗɷlš¤£É!z‘Ee3w*§êp™#ãF-’>¤Ý‘ÎK¾a.æðýoŽJ.Üà]<ùS˜¥u-{µð´Ö¼‚[˜;wC{ßí6bºŽþ¿x}û(<ʆ´ÐXKn‘›dϐ&£I†£r›Z½f.d눻5 ±‰£œhHaÚ&Ok*LHµ¦/bû1Ü!>EԫљCý~|á­~í?¯Ñ F5ºtèüÒÁ+[ÛïW?œõ®aõâ¦ézŸ0)؆Ե —êÈZ©‚‘I"—Ë£5/ެEÃÒÍچÿÜUۑ'ÔòÕÎQ³$«7|ù¾G±¬Ëíy*_šÚƒºµEIHNÕy.”a{¹v÷Ò)o P’á2ú‹—|Mqyñêý÷m4Vã.´á¤dB¨“àÀö7ÅTQoÚ0~ϯ¸UHR Jז +m©†6i𰩪¢41`ìÔ6ÝÔÿ¾»ÀBѺ‡½l‘¢|÷ù|÷ùîϛEәç93n,›Z+äR ªäZå2Õ0‡e*süÜV ßZ.PòÎñ¼éÇq<¾Á—×å²tÄÜG®i5ÉÔºL5w‘ßSnóXØò[>CpɚMøá+•æ<w‰©B¼ÜN›@rž8Ë.àÙ¹¢ä›ñ§¨¬ùÃFhž(™qȅ–éš»IB>I҄¼O¢w³Ï“¯É4ŠG“Bd1yiJ†U*[Bf¨tëoÂJ.¨ÂäÏþäV•¨r¥#UmÁly W0§Víjñ¶Ý>òi“¼ñR ¨¡Ø”…ÆF +‹Ú$ÿq4š$ñ‡˜F̵zÃÉeKÂiÞ\<ċ=Þ05†k=lÒ¸©Öé¸x|®†tN½Áyô{]?$ªCT·ë÷}²º•/¡¡Š kÔ¯¶WËg{gÛÛÁYçGVà›Á!BÐ9`–óGvÉ„Tìw©l­rΎô2Y¹bµj¶9«õ²Å‘¥eÙ'5âû.Üî,µb½#jÁlý +—½ÂÝ«:Ü>ü}¡²•©¼¾-6šZG½žg…2|ßÄÝ%¤y0+ÉqD«ë}‰q¤ñÂý÷5Vãÿ͛p²äE¡NœƒÚŸ³±/È(àâJMÎÈWˆwóôq·æâ*J-,Í,JUP/ÈH,²Ò×/I-.‰±õ@„[”Ÿ_ä¨[ôûxúµ+è)(Åä)a1¥¸4)%ªÂƦ¨ÑÞ+@À@;&&F +`  `h`b`¬À0 +h`¡ÎE!ƒ«(µ°4³(5>?/9UA˜žô‹’32ËRAIHݚ«¸¤(517¾(5=³¸$µ(¾¼(±  µHC½ #±H]¬%ªE]Ӛ+3MAC1%5-3/5EC9}:ûû¹»º¨kj*TƒÝ+†/\ No newline at end of file http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/require_once/makerequire_oncephar.php.inc?r1=1.3&r2=1.4&diff_format=u Index: pear/PHP_Archive/tests/require_once/makerequire_oncephar.php.inc diff -u pear/PHP_Archive/tests/require_once/makerequire_oncephar.php.inc:1.3 pear/PHP_Archive/tests/require_once/makerequire_oncephar.php.inc:1.4 --- pear/PHP_Archive/tests/require_once/makerequire_oncephar.php.inc:1.3 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/require_once/makerequire_oncephar.php.inc Tue Feb 6 03:34:23 2007 @@ -2,7 +2,7 @@ require_once 'PHP/Archive/Creator.php'; require_once 'PHP/Archive/Manager.php'; chdir(dirname(__FILE__)); -$creator = new PHP_Archive_Creator('indexhooha.php', true, 'require_once.phar'); +$creator = new PHP_Archive_Creator('indexhooha.php', 'require_once.phar', 'gz'); $creator->addFile(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'test_require.php', 'indexhooha.php'); $creator->savePhar(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'require_once.phar'); @unlink($name); http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/tests/require_once/require_once.phar?r1=1.9&r2=1.10&diff_format=u Index: pear/PHP_Archive/tests/require_once/require_once.phar diff -u pear/PHP_Archive/tests/require_once/require_once.phar:1.9 pear/PHP_Archive/tests/require_once/require_once.phar:1.10 --- pear/PHP_Archive/tests/require_once/require_once.phar:1.9 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/tests/require_once/require_once.phar Tue Feb 6 03:34:23 2007 @@ -1,4 +1,4 @@ -<?php #PHP_ARCHIVE_HEADER-0.8.0 +<?php error_reporting(E_ALL); if (function_exists('mb_internal_encoding')) { mb_internal_encoding('ASCII'); @@ -23,13 +23,15 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version Id: Archive.php,v 1.26 2006/01/09 04:48:42 cellog Exp $ + * @version Id: Archive.php,v 1.35 2007/01/06 21:58:24 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + const GZ = 0x00001000; + const BZ2 = 0x00002000; /** * Whether this archive is compressed with zlib * @@ -74,13 +76,20 @@ * </code> * then the alias is "PEAR.phar" * - * Information stored is the actual file name, a boolean indicating whether - * this .phar is compressed with zlib, and the precise offset of internal files + * Information stored is a boolean indicating whether this .phar is compressed + * with zlib, another for bzip2, phar-specific meta-data, and + * the precise offset of internal files * within the .phar, used with the {@link $_manifest} to load actual file contents * @var array */ private static $_pharMapping = array(); /** + * Map real file paths to alias used + * + * @var array + */ + private static $_pharFiles = array(); + /** * File listing for the .phar * * The manifest is indexed per phar. @@ -110,6 +119,30 @@ private $_basename; /** + * Allows loading an external Phar archive without include()ing it + * + * @param string $file + * @throws Exception + */ + public static final function loadPhar($file) + { + $file = realpath($file); + if ($file) { + $fp = fopen($file, 'rb'); + $buffer = ''; + while (!$found && !feof($fp)) { + $buffer .= fread($fp, 8192); + // don't break phars + if ($pos = strpos($buffer, '__HALT_COMPI' . 'LER();')) { + fclose($fp); + return self::_mapPhar($file, $pos); + } + } + fclose($fp); + } + } + + /** * Map a full real file path to an alias used to refer to the .phar * * This function can only be called from the initialization of the .phar itself. @@ -124,40 +157,157 @@ */ public static final function mapPhar($file, $dataoffset) { - // this ensures that this is safe - if (!in_array($file, get_included_files())) { - die('SECURITY ERROR: PHP_Archive::mapPhar can only be called from within ' . - 'the phar that initiates it'); + try { + // this ensures that this is safe + if (!in_array($file, get_included_files())) { + die('SECURITY ERROR: PHP_Archive::mapPhar can only be called from within ' . + 'the phar that initiates it'); + } + self::_mapPhar($file, $dataoffset); + } catch (Exception $e) { + die($e->getMessage()); + } + } + + /** + * Sub-function, allows recovery from errors + * + * @param unknown_type $file + * @param unknown_type $dataoffset + */ + private static function _mapPhar($file, $dataoffset) + { + $file = realpath($file); + if (isset(self::$_manifest[$file])) { + return; } if (!is_array(self::$_pharMapping)) { self::$_pharMapping = array(); } - if (!isset(self::$_manifest[$file])) { - $fp = fopen($file, 'rb'); - // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, $dataoffset); - $manifest_length = unpack('Vlen', fread($fp, 4)); - $info = self::_unserializeManifest(fread($fp, $manifest_length['len'])); - if (!$info) { - die; // error declared in unserializeManifest + $fp = fopen($file, 'rb'); + // seek to __HALT_COMPILER_OFFSET__ + fseek($fp, $dataoffset); + $manifest_length = unpack('Vlen', fread($fp, 4)); + $manifest = ''; + $last = '1'; + while (strlen($last) && strlen($manifest) < $manifest_length['len']) { + $read = 8192; + if ($manifest_length['len'] - strlen($manifest) < 8192) { + $read = $manifest_length['len'] - strlen($manifest); } + $last = fread($fp, $read); + $manifest .= $last; + } + if (strlen($manifest) < $manifest_length['len']) { + throw new Exception('ERROR: manifest length read was "' . + strlen($manifest) .'" should be "' . + $manifest_length['len'] . '"'); + } + $info = self::_unserializeManifest($manifest); + if ($info['alias']) { $alias = $info['alias']; - self::$_manifest[$file] = $info['manifest']; - $compressed = $info['compressed']; - self::$_fileStart[$file] = ftell($fp); - fclose($fp); + $explicit = true; + } else { + $alias = $file; + $explicit = false; } - if ($compressed) { + self::$_manifest[$file] = $info['manifest']; + $compressed = $info['compressed']; + self::$_fileStart[$file] = ftell($fp); + fclose($fp); + if ($compressed & 0x00001000) { if (!function_exists('gzinflate')) { - die('Error: zlib extension is not enabled - gzinflate() function needed' . + throw new Exception('Error: zlib extension is not enabled - gzinflate() function needed' . + ' for compressed .phars'); + } + } + if ($compressed & 0x00002000) { + if (!function_exists('bzdecompress')) { + throw new Exception('Error: bzip2 extension is not enabled - bzdecompress() function needed' . ' for compressed .phars'); } } if (isset(self::$_pharMapping[$alias])) { - die('ERROR: PHP_Archive::mapPhar has already been called for alias "' . + throw new Exception('ERROR: PHP_Archive::mapPhar has already been called for alias "' . $alias . '" cannot re-alias to "' . $file . '"'); } - self::$_pharMapping[$alias] = array($file, $compressed, $dataoffset); + self::$_pharMapping[$alias] = array($file, $compressed, $dataoffset, $explicit); + self::$_pharFiles[$file] = $alias; + } + + /** + * extract the manifest into an internal array + * + * @param string $manifest + * @return false|array + */ + private static function _unserializeManifest($manifest) + { + // retrieve the number of files in the manifest + $info = unpack('V', substr($manifest, 0, 4)); + $apiver = substr($manifest, 4, 2); + $apiver = bin2hex($apiver); + $apiver_dots = hexdec($apiver[0]) . '.' . hexdec($apiver[1]) . '.' . hexdec($apiver[2]); + $majorcompat = hexdec($apiver[0]); + $calcapi = explode('.', '1.0.0'); + if ($calcapi[0] != $majorcompat) { + throw new Exception('Phar is incompatible API version ' . $apiver_dots . ', but ' . + 'PHP_Archive is API version 1.0.0'); + } + if ($calcapi[0] === '0') { + if ('1.0.0' != $apiver_dots) { + throw new Exception('Phar is API version ' . $apiver_dots . + ', but PHP_Archive is API version 1.0.0', E_USER_ERROR); + } + } + $flags = unpack('V', substr($manifest, 6, 4)); + $ret = array('compressed' => $flags & 0x00003000); + // signature is not verified by default in PHP_Archive, phar is better + $ret['hassignature'] = $flags & 0x00010000; + $aliaslen = unpack('V', substr($manifest, 10, 4)); + if ($aliaslen) { + $ret['alias'] = substr($manifest, 14, $aliaslen[1]); + } else { + $ret['alias'] = false; + } + $manifest = substr($manifest, 14 + $aliaslen[1]); + $metadatalen = unpack('V', substr($manifest, 0, 4)); + if ($metadatalen[1]) { + $ret['metadata'] = unserialize(substr($manifest, 4, $metadatalen[1])); + $manifest = substr($manifest, 4 + $metadatalen[1]); + } else { + $ret['metadata'] = null; + $manifest = substr($manifest, 4); + } + $offset = 0; + $start = 0; + for ($i = 0; $i < $info[1]; $i++) { + // length of the file name + $len = unpack('V', substr($manifest, $start, 4)); + $start += 4; + // file name + $savepath = substr($manifest, $start, $len[1]); + $start += $len[1]; + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = compressed filesize + // 3 = crc32 + // 4 = flags + // 5 = metadata length + $ret['manifest'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($manifest, $start, 24))); + $ret['manifest'][$savepath][3] = sprintf('%u', $ret['manifest'][$savepath][3]); + if ($ret['manifest'][$savepath][5]) { + $ret['manifest'][$savepath][6] = unserialize(substr($manifest, $start + 24, + $ret['manifest'][$savepath][5])); + } else { + $ret['manifest'][$savepath][6] = null; + } + $ret['manifest'][$savepath][7] = $offset; + $offset += $ret['manifest'][$savepath][2]; + $start += 24 + $ret['manifest'][$savepath][5]; + } + return $ret; } /** @@ -219,7 +369,7 @@ $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][2]; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][5]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][7]); } } @@ -248,15 +398,17 @@ } } @fclose($this->fp); - if ($this->_compressed) { + if (self::$_manifest[$this->_archiveName][$path][4] & self::GZ) { $data = gzinflate($data); + } elseif (self::$_manifest[$this->_archiveName][$path][4] & self::BZ2) { + $data = bzdecompress($data); } if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if (strlen($data) != $this->currentStat[7]) { return array("Not valid internal .phar file (size error {$size} != " . $this->currentStat[7] . ")"); } - if (self::$_manifest[$this->_archiveName][$path][3] != crc32($data)) { + if (self::$_manifest[$this->_archiveName][$path][3] != sprintf("%u", crc32($data))) { return array("Not valid internal .phar file (checksum error)"); } self::$_manifest[$this->_archiveName][$path]['ok'] = true; @@ -269,27 +421,79 @@ } /** + * Parse urls like phar:///fullpath/to/my.phar/file.txt + * + * @param string $file + * @return false|array + */ + static protected function parseUrl($file) + { + if (substr($file, 0, 7) != 'phar://') { + return false; + } + $file = substr($file, 7); + + $ret = array('scheme' => 'phar'); + $pos_p = strpos($file, '.phar.php'); + $pos_z = strpos($file, '.phar.gz'); + $pos_b = strpos($file, '.phar.bz2'); + if ($pos_p) { + if ($pos_z) { + return false; + } + $ret['host'] = substr($file, 0, $pos_p + strlen('.phar.php')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif ($pos_z) { + $ret['host'] = substr($file, 0, $pos_z + strlen('.phar.gz')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif ($pos_b) { + $ret['host'] = substr($file, 0, $pos_z + strlen('.phar.bz2')); + $ret['path'] = substr($file, strlen($ret['host'])); + } elseif (($pos_p = strpos($file, ".phar")) !== false) { + $ret['host'] = substr($file, 0, $pos_p + strlen('.phar')); + $ret['path'] = substr($file, strlen($ret['host'])); + } else { + return false; + } + if (!$ret['path']) { + $ret['path'] = '/'; + } + return $ret; + } + + /** * Locate the .phar archive in the include_path and detect the file to open within * the archive. * - * Possible parameters are phar://filename_within_phar.ext or - * phar://pharname.phar/filename_within_phar.ext - * - * phar://filename_within_phar.ext will simply use the last .phar opened. + * Possible parameters are phar://pharname.phar/filename_within_phar.ext * @param string a file within the archive * @return string the filename within the .phar to retrieve */ public function initializeStream($file) { - $info = parse_url($file); - if (!isset($info['host']) || !count(self::$_pharMapping)) { + $info = @parse_url($file); + if (!$info) { + $info = self::parseUrl($file); + } + if (!$info) { + return false; + } + if (!isset($info['host'])) { // malformed internal file return false; } + if (!isset(self::$_pharFiles[$info['host']]) && + !isset(self::$_pharMapping[$info['host']])) { + try { + self::loadPhar($info['host']); + // use alias from here out + $info['host'] = self::$_pharFiles[$info['host']]; + } catch (Exception $e) { + return false; + } + } if (!isset($info['path'])) { - // last opened phar is requested - $info['path'] = $info['host']; - $info['host'] = ''; + return false; } elseif (strlen($info['path']) > 1) { $info['path'] = substr($info['path'], 1); } @@ -297,72 +501,16 @@ $this->_basename = $info['host']; $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; - } else { - // no such phar has been included, or last opened phar is requested - $pharinfo = end(self::$_pharMapping); - $this->_basename = key(self::$_pharMapping); - $this->_archiveName = $pharinfo[0]; - $this->_compressed = $pharinfo[1]; + } elseif (isset(self::$_pharFiles[$info['host']])) { + $this->_archiveName = $info['host']; + $this->_basename = self::$_pharFiles[$info['host']]; + $this->_compressed = self::$_pharMapping[$this->_basename][1]; } $file = $info['path']; return $file; } /** - * extract the manifest into an internal array - * - * @param string $manifest - * @return false|array - */ - private static function _unserializeManifest($manifest) - { - // retrieve the number of files in the manifest - $info = unpack('V', substr($manifest, 0, 4)); - $apiver = substr($manifest, 4, 2); - $apiver = bin2hex($apiver); - $apiver_dots = hexdec($apiver[0]) . '.' . hexdec($apiver[1]) . '.' . hexdec($apiver[2]); - $majorcompat = hexdec($apiver[0]); - $calcapi = explode('.', '0.8.0'); - if ($calcapi[0] != $majorcompat) { - trigger_error('Phar is incompatible API version ' . $apivers_dots . ', but ' . - 'PHP_Archive is API version 0.8.0'); - return false; - } - if ($calcapi[0] === '0') { - if ('0.8.0' != $apiver_dots) { - trigger_error('Phar is API version ' . $apiver_dots . - ', but PHP_Archive is API version 0.8.0', E_USER_ERROR); - return false; - } - } - $ret = array('compressed' => $apiver[3]); - $aliaslen = unpack('V', substr($manifest, 6, 4)); - $ret['alias'] = substr($manifest, 10, $aliaslen[1]); - $manifest = substr($manifest, 10 + $aliaslen[1]); - $offset = 0; - $start = 0; - for ($i = 0; $i < $info[1]; $i++) { - // length of the file name - $len = unpack('V', substr($manifest, $start, 4)); - $start += 4; - // file name - $savepath = substr($manifest, $start, $len[1]); - $start += $len[1]; - // retrieve manifest data: - // 0 = uncompressed file size - // 1 = timestamp of when file was added to phar - // 2 = compressed filesize - // 3 = crc32 - // 4 = flags - $ret['manifest'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ce', substr($manifest, $start, 17))); - $ret['manifest'][$savepath][5] = $offset; - $offset += $ret['manifest'][$savepath][2]; - $start += 17; - } - return $ret; - } - - /** * Open the requested file - PHP streams API * * @param string $file String provided by the Stream wrapper @@ -534,7 +682,15 @@ */ public function dir_opendir($path) { - $info = parse_url($path); + $info = @parse_url($path); + if (!$info) { + $info = self::parseUrl($path); + if (!$info) { + trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', + E_USER_ERROR); + return false; + } + } $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); @@ -556,15 +712,19 @@ foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if ($path == '/') { if (strpos($file, '/')) { - $this->_dirFiles[array_shift($a = explode('/', $file))] = true; + $a = explode('/', $file); + $this->_dirFiles[array_shift($a)] = true; } else { $this->_dirFiles[$file] = true; } } elseif (strpos($file, $path) === 0) { $fname = substr($file, strlen($path) + 1); if (strpos($fname, '/')) { - $this->_dirFiles[array_unshift($a = explode('/', $fname))] = true; - } else { + $a = explode('/', $fname); + $this->_dirFiles[array_shift($a)] = true; + } elseif (strlen($file) != strlen($path)) { + // if the two match exactly, the path searched for was + // not a directory, but was a file. $this->_dirFiles[$fname] = true; } } @@ -615,18 +775,23 @@ */ public final function APIVersion() { - return '0.8.0'; + return '1.0.0'; } }} if (!class_exists('Phar')) { - if (!in_array('phar', stream_get_wrappers())) { - stream_wrapper_register('phar', 'PHP_Archive'); - } PHP_Archive::mapPhar(__FILE__, __COMPILER_HALT_OFFSET__); } else { - Phar::mapPhar(); + try { + Phar::mapPhar(); + } catch (Exception $e) { + echo $e->getMessage(); + } +} +PHP_Archive::mapPhar(__FILE__, __COMPILER_HALT_OFFSET__ + strlen('__HALT'.'_COMPILER();')); +if (class_exists('PHP_Archive') && !in_array('phar', stream_get_wrappers())) { + stream_wrapper_register('phar', 'PHP_Archive'); } @ini_set('memory_limit', -1); require_once 'phar://require_once.phar/indexhooha.php'; -__HALT_COMPILER();>\ No newline at end of file +__HALT_COMPILER();M\ No newline at end of file
« previous php.pear.cvs (#44547) next »