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: | Greg Beaver | 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't be. This only affects CRCs. The workaround -found is to sprintf("%u", 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't be. This only affects CRCs. The workaround -found is to sprintf("%u", crc32($data)) - </notes> +found is to sprintf("%u", 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¯6X¥9è'é}íC#x:ö¼ª¤©RþÆ{DV n/Ù±õj?èÑöH_úã×èÓâÔ ¿âÿÀ?S£±?QÏ~KK7Úy·d'û§öÖ 1{¾þÐÐØÉõÿ\$Ýxú ³TØ3veí(úv(6QÌä)=#OIv¯é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Ó>ÈÈó Gxc1ÁD& +^¡sÖ© ¸BȶL§âÛ0´ÚzRaJ¥¸(æù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ÉE6VÐCµ!åtþåKÙnZNt²!Cøc5BÖ*Ájø?:,ViÆcfeU¨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Ä!AAbfÓbDr¥fÙÄM0ÊCAÇf@â6ÂÍ7A#ÜBQ¸t Y 0dz70õ$ÔÅÐZK:+VßUS+¸¯ê®}n^õ¶ReSRHYN½QG"¡õSalϯÙCvÑ]&ÄqEl<Ïá)!ÌÓä|m0Î.Õ®,}?OL+hü÷öÎäÃÑþlY¿HÖb(íÃ~pÀÓìEJoÉ¿êEÑr0鮤<éIÿ7¢§?]æ°pÝýÒþ+0ï{][PI]tSYIAo0r¤sµèí;.¢èb8ø¿ÿçÓ¹Mmá\kY6¿Ä8ÕðWª*p@üáöʨ#<¹8M4 +:Få ¤·»µÝ.% ½½o÷+(«¡[ê1¯ØJK279E£±ÃHË^üP%ØÀlådûáÆzg 9ÚT5¶ +ß»ý@%I¨3x¢îóêÕÄ÷Ê¢66'ñ*~¸´?êGÝ;ô¤Î û<¸íYíoÚ8ÿÎ_a¡j °®Ú¾°]W®ÐµRïA¯ÓI"_ò+ô+rðO+¤($>ÔKãîc¯©mZº¨ïÖfY¢Øæ ÒZFµuX99åû¹·;C÷µ*H_%ÏSrO-|oÍU¥Ì§ARµ_«ÕF*ºLêÃÅé¾LõÆ>LÑ6Æ#Þ4^¶ÌOa_ <Kùÿ£?À¹P*¦Hv¤à*Vô$ä[G6¹ :ÛgIâo(¶úì¬$M2#f&Ór¢ÙùSg.Qͺ*³Dµ)Uü@ä>Sú¯1¬Z¯£Fé:K§|Mç·NVNÙÞn¬E£98pØ4%AÐ@#5¦É1sñ´« ¤úvûúÌZÍTNëâ%{-,]p°M}À/5´ÛTÞÆHò·Ù.êÎþ-¾Xfeõrpõè11]>g!'-$)#÷ªÝÊ4Í^æþä6Ïi°ñZH§ÇGÒ`µý?UÅ¡3.öõÕ½(¾oOÿZk#ìOW0ní`ýrj,UØqØ×[ç§Úø1µñ{þ.DzOÌÿvJë³wÅXÎâªGÌÂÄÙÖÑAÚc&ù{~bP0Ñ¡VÐøÎ=?ºnZs# ¡&Jâä4Á¾ QæwrÊÜ©Tñwx2©H. Wãoseº´¶u¦ÇïEÙoSn¤m§(ËL:/ +·î]öAnàì ÁÅ@iüû ҥ(º~p·«ípÀ½qßã-b"¤yÕßõït9àÆÒæ&òÝ||Ãz.~!éeYØCOKTt¼+Î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îph¶=µa²} K_áîM-w¿/DºQ5CÛÙã¼§cúv6ÓIà__5Iá_+ÎÒ]JKüÛ²NrZâÄjÜþ+c;w@Ù§©ì"¯.±ãξåØtbÏýR+öüÇñmô6&ç´8¸?¥Ñ]kÂ0à{E. +IZvíVWjeÂÆJÁ®Â±90M»Øº_¿ÓYÁï]NÂáys»a©ËÃÚ8 Íñô1"k³Á vUáúÔÅWúÁÂw}¹VÆ ZV(¤O)}Ðñò-ÈîÅOFl ÙF n¬Â/]þ¸.ãE4Úkö¾gõBPj´xÜeà|¿NF¸ÙZþ136K¼â°>M²$½dor¤QQI§¼Âuu»»ïÑ!kØ`JÙ§úeü`~Òj»4ö]xÐèó3ïÙ~Ér¼y/TõªTÃ_µZmSÛHþî_1q9\kCȧ+98%Ôe2$©;B)BÛ:ôáXþûv÷ÌHIS[ÚÍJî§{zúu¼ï?¤«´·(b?å\ä<ËìA~òDðï"ñ1b>ì=ôü L°Ý]vè_̧Cñ¼Èâ +h¿uàJk©*Je-¢vkõrIÞp%iÒ,sÖ¯Vp$ÄÒ¸9Ó÷¸?é=ö¶·Ï]¢uW^<yf[Ê-º½ÍX!¼%·½ÊWå)07]ñ0Åõ¹«Úiarí lfÉ2ö"ôi/]?"+ÔþD>D`]$)mÂÃé«5bÖ¥,1pvx0s@í*Ã!{(-»H\Ø +t¤6Þ[òü.äØë)ê#ã¡à_rc¯vÕ.Ö£*K×@ª ãUi+Æ^CÊ+|pû(¿²(ó.VQcÅæ¡mÇ.®óWùºø +Ü/=1îXnÔÅpÚLôjÄÆèq4.crj/³ó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,äðåZM¯Ê¥ï.U&U±ÃÞ¸r½º×ó³Ý«å[Ncvkïw®&á5½4ЦhÍ<÷H÷ZÐu*µ5N×+Ïu+lCkÓn£®Íw«ªÖ´¾ú}é'e©m^nÈYªb v«EߺÝè8§!mmuíª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Û¦:;4hk¸Aþøÿ×E¦´ÍÑU+K?uÞÕ¬?¢M4½áGÉ-Ô²ÊK,»º`å±ÖÈôäòûýF½þ³b~þM1ãÍÄ|mýü)ÁRÓoÝ,í£»ihSe¤tù*Np´!B°eÙ,IVô%d&¸Tz ixnF¨ªu8S.J#]ó¹æÕëXµ3an+o8p§0ÓmèΦªOð+X%ßÜoȰ¨WMbhJè±&>QÊùäÉc +ãXèOid·Jö*#¥0¨¯í9f_f'º¢ßröÂóÁ!*ß) bRBÔ±þç=úMgy{eng@70¤.ix©_¤§ku^N0ú*¨Q©¦ôß[qµ±úÍj¥OÍÅÓs:·`| +úEÈëÛ5ReÕN+ì~:QàF1¢>¡Òs{Ë´y e(}âÓçË0>8urÓûEò£0ÆeÌ}°ÙaÌñq¹Äá³Kü($[Ú8N¿Mó³/_þ8;ÿk¨'¿BÈZaIÁçnÝÁÖ '8Ý-¸2Ȫv2ø¥qà&O%Úu*c%Õ wÝQWlµûð³jÕ"îòлLâ¥meÖ¼k°þ¡´èÍß")yXpU4.0ÿãØ]`;µ¸ÖÎFc±½Àw³\`.f\¸ =i·Ê²h»0v\=,ïú EyÂâ,Áå äÜ0÷ñM²pW¹ 3#½%E½ÊÊô²!Z±\MN_P97¶S5 PªÀHE&ç=Xh<FG;º/¥ÚÆZ_IÂÆ +2[ᩦA(Ã.¤#âhÉi+´_[+°DYsár!ú¥)Y¨~|¿ÂÇ~.^Ç &XÚ\Ô³Ð!Rju¨=D½Y¯2y0Äz7äBFø1k´Çyk2ÌÖ<ÏÐÖ'ëm)^Iʸ±ó»Fê©ÒLëm©]Ä`WçsÒÀyn²Xò!ºIxÚtg²£*¬ÕEÜYBz}4¯`Ò/ÅMÿ6¯báðt\V-ÙæµÚ Lù-ûPnP +!Ùl +nnL)!IYJ¶(6R6MuR +γlùsnÿ4w¯;Í-ë*»¥NU D(î¾ýp>¨ªö(á!)Û\UÖÕ|¨ÆãKmåfF$ã¤æ]åºcJÑ|Éè©KÈÏÚöG¶iÒ:Ѭ'пçù;¢aØL׿ËÓ×4LéöÀÏ+Æ¿a§"¹¶#X#ÉA4sv"ܳÏ)õWÀļþZ¡ÖË+,]'¥xÉpÁ¾6/"9J¸áGÀ>5ÜúIWÆVÍ*±tù3[ØWÄqîUWw@C+ì÷ÔWmhmüH¬íÜÆ[[¨ßÉáÞßÔÝ=Cª[ø»{ekñÏÛ64íçèñ;* ó\ å/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%':´äÐ×4nùÀ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-ØJI1VhbT{©Æ-×aâ]ÕXÕé +DðPÄuFií](ó-±0ËþYôEû¾lo9=±±õ¯ý6nÎ^´·þ'ݹ'cHÜ»ýA¥¤©¨=Ñ{øZ)/µ©dc~£ð?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ÁÜ`1YG6§"ÙôL6ø¬ÜTDCáEd=R¸DݾI(õua/gäî»§î31ǪàA³ûmæX²Û Nôÿã¦Fùï¤Ú7NÇÐÃìZ²¬K? æ« Jè8ÓÄ50>¼`)±-vp<ð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Þ5OÊ Xd +pS¤ø8QȾél8Ï ôi§£zfÔ2+7yGLDH§!ïBâ$©Ä4{Æ9%Îþ;¹U(âp[\êÀÞ:Î-?A¾BxÊØ¢+òÿöv¶·zÂÿrýa¶YåÀô^ñ´çô¶ºÍÊôãÏÆR"ûsfGl÷°QÇÐÝÍÍ»ºÝ§[af¤+N )-Üò0]F&L¦h¨º|õTÈ ÆdIhi£0=¤É#ÙT<«[ýrLüÌ+^éÕìhìãOJKNB·é,îq¼B)b9Gr¢¨¾»Æ_¬*Uà·©þÀ.d&VWnöß#{·2<×îµ_-úøæÀÎJÞN÷êwzWÜú4é§,%+êÞ]Q+ÏX¥ÜÍɦ)""Ê)¦ØîCe-;eiØëìÁÚï§V¥f!1ÆFávIeeFk{),E0¡¼ã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_PzÆiDr"@YÝÆD[fW ¡«;éÃÔjtÚ°x;ûwo7cÞ"}+1` +Õþls¤®=iY}"9FádÎJèñîxîlÁO¶Å¤}qÞeäÛTam@þâsfiX|ïNã ib®+O{ í9DÈ+d8ÅÊÐõ0)i+"4 VÔ³2î¯wE¼²f¿ð¦ÃNÿB)q¬¨lZÕ²Ò0ÏdAdÍê\äÉ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¨di:aKäpõe·(èLmó@IJ{¢F*¯áࢠ8]ªD£*ò§Ïõ[{R<¦Àix§;â5-;]Á+%|ªÙ<§neÝy + (KQØÅ*ôÒ?(¦su ¬ÞÙÞÙí<ßÞìt¶{;|wrУö+=ñ@xüΦû7¶{&Ö3y»Âö Ò©é¤ îIÞ Î8ð'dè°;jÒ°óEô©ÎÎn¹¹ºà +QXP9¹uw©ÍÿÒéÌù:@§Ã À[Àh5Ùå¢o¨sæU27raѰ'Ñ(Açå8î"q_` RòZd54(Tz4ê%°boiÃîtíKóà¯JªìÙi±$µ¨2vwq%¬þ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ÄÎzFH2lÀµ¢arC¶¥" òDÎr)Úáyfò4q}¤Qé'+çìÌo×XïÍ=jà0+BûëÃÿ5Å|N7dh&\0bÐ2J¢pí¡-Zª¶ò ÓDqÉìÎ2jå)~ò¥8µNCOÅ`(Åx³WTìb\?§2åý·¼Ã*»q NeÆM(¹]/¶9ØH»¤+c{Î/ÎZEjuAÍ! +©es¨1@¡æÁ#±éÛs+ɳªñËxRÑcém.ÅÚCT:¸¹+æÊZÌ~6| DhZIeÝò4ÑÒåQ¥v\óS5aç¹}R©§ë¢tPÜ(]ÙUô$¶>¦w$s#çvîPf²Ý×AjØ´@ S**³.&ê~åGq`Ú&Á¢£ ¿FEõ. ¼p+rà[A)ÃBI)É8ÊË>[0æ-vÞÔÈN-Ö\HÛ²sÊææEEî+ó£F}¦¸µEBO":Ù~WÔÖWüÎÛÛÏß^ÌPÉh¯ðóJÖóZÕé#¿`ruçü =ÙÓØ1Õn¸8ï&6´MIFü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Ûä ³XTH½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 xDKù)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-£ÆJyR 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¸-AHuF¨Dª=Ö»yMÑÆÜk¾Ä[.Bfæd+w.&¤Ò-¬]ð³/À§¢óôKTÕâz:N¸÷Ÿ³pk(>ªõÚ )d®e ¿ðÃõ ãë eEh"öçâgÉô¦9ö?èTVƱ´JeR%áÇtÞ\-oçjÑÌ.çüå(èÊdì¹:ZqGNEL ´iÓ*Á(¿ÀYyþBó/¯É÷`½ýh¿³IVÁxBõîû·ÉÎÀz4zS<rdpg½·:w:äýÖðY¼[M>nW6¯7W5¬5ÿ$¾CêM}XORÓ¢¸Ègøxú3q´GÞ°^r0 uðUN|ö_,x²Õæ»>¤ÈzPðA´[ªÓÉ~£,ÿOè_M@ïý=*Kç>DLIZ¬K§aq\¨ÕÖÕúù-ºPºÍÌûÌÃÌ*jªfB©Æ['5BJ$Û1ë²=6÷_ÁÖØkýÜ¥FnjíÓrlÉ(¼Á¾'ÀÇÀÏѯµùÉúBjůèdù>HH¶y&§CqcÊâ"¶¥w.ïMþ¡my̾ò¯wüߪ¢ÍÅooÚ0ÆßçSªI +°{Ew]Á³ï (±Ý·rÍD4ǹckðR5À \±oÂ#S)×{ÝgkxIþoÆü½{+&1<F3É£%:a¨c°¸Oü«Ù§É×pê£Éi¬ä*Ô©©8yñl±ìà6hd²5êÃü¤É_$,CoW zP½d³[í%]q¦à=p|ÀMBò¤÷ÖQr 5 +IrMD|SÜwÍæ¦©Wø/þ¦Bt@h{ÎÝ˨(P*ÿae ãÕ!QeÙÖ§*ar{LÁ;!³C"'txçZYöA¬µ4ÑÍl§Ô)(0|¾qÐ9åÀ¬ì`«ÞY¨ÐvK9#z£»x¬}a4õý»Ðÿx]}ý¦DU)Û x{}ì¶ÎB÷Ê»$V.£a»Jxï/é,ÙÃ{ßZ¿ú<9/׺`Oeç(±ÛÞ9ìÞ/l\#ßÑÛ^ý¼ª¼-Á-Ò»T8¸¾µ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Â÷ð&Amfð}mxõ²@ìotùIJ»Öè£{¯²4i9÷\0êN 4b^C?Xo +wlþB¾\¾î®ðËNU(ÎrUét\JÇR@+KÒÂXÔ]¹=3CÁ¿ø]\Ï©aSvV2ßm-/,KNQJ³;t+Û§@¸7¤Ý_àÃòR1K^F"%ÅX$A®W_m-¾¬P´`EH_á8Ûå@®ä\ðÜxæ¶ÏºÎ'×ðÜúýøsR×ìiÍ5ËÌ\Kºba¹,kC>'ÉÙÉ·l¤£É!zEe3w*§êp#ãF->¤ÝÎK¾a.æðýoJ.Üà]<ùS¥u-{µð´Ö¼[;wC{ßí6bºþ¿x}û(<Ê´ÐXKndÏ&£I£rZ½f.dë»5±£hHaÚ&Ok*LHµ¦/bû1Ü!>EÔ«ÑCý~|á~í?¯Ñ F5ºtèüÒÁ+[ÛïW?õ®aõâ¦éz0)ØÔµ êÈZ©I"Ë£5/¬EÃÒÍÚÿÜUÛ'ÔòÕÎQ³$«7|ù¾G±¬Ëíy*_ÚºµEIHNÕ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Õ0e*süÜVßZ.PòÎñ¼éÇq<¾Á×å²tÄÜG®i5ÉÔºL5wßSnóXØò[>CpÉMøá+æ<w©B¼ÜN@r8Ë.àÙ¹¢äñ§¨¬ùÃFh(qÈ é»IB>IÒ¼O¢w³Ï¯É4GBd1yiJU*[Bf¨tëoÂJ.¨ÂäÏþäV¨r¥#UmÁly W0§Víjñ¶Ý>òi¼ñR ¨¡Ø ÆF +Ú$ÿq4$ñF̵zÃÉeKÂiÞ\<Ä=Þ05k=lÒ¸©Öé¸x|®tN½Áyô{]?$ªCT·ë÷}²º/¡¡kÔ¯¶WËg{gÛÛÁYçGVàÁ!BÐ9`óGvÉTìw©lrÎô2Y¹bµj¶9«õ²Å¥eÙ'5âû.Üî,µb½#jÁlý +½ÂÝ«:Ü>ü}¡²©¼¾-6ZG½g 2|ßÄÝ%¤y0+ÉqD«ë}q¤ñÂý÷5VãÿÍp²äE¡NÚ³±/È(àâJMÎÈWwóô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
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't be. This only affects CRCs. The workaround -found is to sprintf("%u", 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't be. This only affects CRCs. The workaround -found is to sprintf("%u", crc32($data)) - </notes> +found is to sprintf("%u", 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¯6X¥9è'é}íC#x:ö¼ª¤©RþÆ{DV n/Ù±õj?èÑöH_úã×èÓâÔ ¿âÿÀ?S£±?QÏ~KK7Úy·d'û§öÖ 1{¾þÐÐØÉõÿ\$Ýxú ³TØ3veí(úv(6QÌä)=#OIv¯é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Ó>ÈÈó Gxc1ÁD& +^¡sÖ© ¸BȶL§âÛ0´ÚzRaJ¥¸(æù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ÉE6VÐCµ!åtþåKÙnZNt²!Cøc5BÖ*Ájø?:,ViÆcfeU¨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Ä!AAbfÓbDr¥fÙÄM0ÊCAÇf@â6ÂÍ7A#ÜBQ¸t Y 0dz70õ$ÔÅÐZK:+VßUS+¸¯ê®}n^õ¶ReSRHYN½QG"¡õSalϯÙCvÑ]&ÄqEl<Ïá)!ÌÓä|m0Î.Õ®,}?OL+hü÷öÎäÃÑþlY¿HÖb(íÃ~pÀÓìEJoÉ¿êEÑr0鮤<éIÿ7¢§?]æ°pÝýÒþ+0ï{][PI]tSYIAo0r¤sµèí;.¢èb8ø¿ÿçÓ¹Mmá\kY6¿Ä8ÕðWª*p@üáöʨ#<¹8M4 +:Få ¤·»µÝ.% ½½o÷+(«¡[ê1¯ØJK279E£±ÃHË^üP%ØÀlådûáÆzg 9ÚT5¶ +ß»ý@%I¨3x¢îóêÕÄ÷Ê¢66'ñ*~¸´?êGÝ;ô¤Î û<¸íYíoÚ8ÿÎ_a¡j °®Ú¾°]W®ÐµRïA¯ÓI"_ò+ô+rðO+¤($>ÔKãîc¯©mZº¨ïÖfY¢Øæ ÒZFµuX99åû¹·;C÷µ*H_%ÏSrO-|oÍU¥Ì§ARµ_«ÕF*ºLêÃÅé¾LõÆ>LÑ6Æ#Þ4^¶ÌOa_ <Kùÿ£?À¹P*¦Hv¤à*Vô$ä[G6¹ :ÛgIâo(¶úì¬$M2#f&Ór¢ÙùSg.Qͺ*³Dµ)Uü@ä>Sú¯1¬Z¯£Fé:K§|Mç·NVNÙÞn¬E£98pØ4%AÐ@#5¦É1sñ´« ¤úvûúÌZÍTNëâ%{-,]p°M}À/5´ÛTÞÆHò·Ù.êÎþ-¾Xfeõrpõè11]>g!'-$)#÷ªÝÊ4Í^æþä6Ïi°ñZH§ÇGÒ`µý?UÅ¡3.öõÕ½(¾oOÿZk#ìOW0ní`ýrj,UØqØ×[ç§Úø1µñ{þ.DzOÌÿvJë³wÅXÎâªGÌÂÄÙÖÑAÚc&ù{~bP0Ñ¡VÐøÎ=?ºnZs# ¡&Jâä4Á¾ QæwrÊÜ©Tñwx2©H. Wãoseº´¶u¦ÇïEÙoSn¤m§(ËL:/ +·î]öAnàì ÁÅ@iüû ҥ(º~p·«ípÀ½qßã-b"¤yÕßõït9àÆÒæ&òÝ||Ãz.~!éeYØCOKTt¼+Î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îph¶=µa²} K_áîM-w¿/DºQ5CÛÙã¼§cúv6ÓIà__5Iá_+ÎÒ]JKüÛ²NrZâÄjÜþ+c;w@Ù§©ì"¯.±ãξåØtbÏýR+öüÇñmô6&ç´8¸?¥Ñ]kÂ0à{E. +IZvíVWjeÂÆJÁ®Â±90M»Øº_¿ÓYÁï]NÂáys»a©ËÃÚ8 Íñô1"k³Á vUáúÔÅWúÁÂw}¹VÆ ZV(¤O)}Ðñò-ÈîÅOFl ÙF n¬Â/]þ¸.ãE4Úkö¾gõBPj´xÜeà|¿NF¸ÙZþ136K¼â°>M²$½dor¤QQI§¼Âuu»»ïÑ!kØ`JÙ§úeü`~Òj»4ö]xÐèó3ïÙ~Ér¼y/TõªTÃ_µZmSÛHþî_1q9\kCȧ+98%Ôe2$©;B)BÛ:ôáXþûv÷ÌHIS[ÚÍJî§{zúu¼ï?¤«´·(b?å\ä<ËìA~òDðï"ñ1b>ì=ôü L°Ý]vè_̧Cñ¼Èâ +h¿uàJk©*Je-¢vkõrIÞp%iÒ,sÖ¯Vp$ÄÒ¸9Ó÷¸?é=ö¶·Ï]¢uW^<yf[Ê-º½ÍX!¼%·½ÊWå)07]ñ0Åõ¹«Úiarí lfÉ2ö"ôi/]?"+ÔþD>D`]$)mÂÃé«5bÖ¥,1pvx0s@í*Ã!{(-»H\Ø +t¤6Þ[òü.äØë)ê#ã¡à_rc¯vÕ.Ö£*K×@ª ãUi+Æ^CÊ+|pû(¿²(ó.VQcÅæ¡mÇ.®óWùºø +Ü/=1îXnÔÅpÚLôjÄÆèq4.crj/³ó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,äðåZM¯Ê¥ï.U&U±ÃÞ¸r½º×ó³Ý«å[Ncvkïw®&á5½4ЦhÍ<÷H÷ZÐu*µ5N×+Ïu+lCkÓn£®Íw«ªÖ´¾ú}é'e©m^nÈYªb v«EߺÝè8§!mmuíª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Û¦:;4hk¸Aþøÿ×E¦´ÍÑU+K?uÞÕ¬?¢M4½áGÉ-Ô²ÊK,»º`å±ÖÈôäòûýF½þ³b~þM1ãÍÄ|mýü)ÁRÓoÝ,í£»ihSe¤tù*Np´!B°eÙ,IVô%d&¸Tz ixnF¨ªu8S.J#]ó¹æÕëXµ3an+o8p§0ÓmèΦªOð+X%ßÜoȰ¨WMbhJè±&>QÊùäÉc +ãXèOid·Jö*#¥0¨¯í9f_f'º¢ßröÂóÁ!*ß) bRBÔ±þç=úMgy{eng@70¤.ix©_¤§ku^N0ú*¨Q©¦ôß[qµ±úÍj¥OÍÅÓs:·`| +úEÈëÛ5ReÕN+ì~:QàF1¢>¡Òs{Ë´y e(}âÓçË0>8urÓûEò£0ÆeÌ}°ÙaÌñq¹Äá³Kü($[Ú8N¿Mó³/_þ8;ÿk¨'¿BÈZaIÁçnÝÁÖ '8Ý-¸2Ȫv2ø¥qà&O%Úu*c%Õ wÝQWlµûð³jÕ"îòлLâ¥meÖ¼k°þ¡´èÍß")yXpU4.0ÿãØ]`;µ¸ÖÎFc±½Àw³\`.f\¸ =i·Ê²h»0v\=,ïú EyÂâ,Áå äÜ0÷ñM²pW¹ 3#½%E½ÊÊô²!Z±\MN_P97¶S5 PªÀHE&ç=Xh<FG;º/¥ÚÆZ_IÂÆ +2[ᩦA(Ã.¤#âhÉi+´_[+°DYsár!ú¥)Y¨~|¿ÂÇ~.^Ç &XÚ\Ô³Ð!Rju¨=D½Y¯2y0Äz7äBFø1k´Çyk2ÌÖ<ÏÐÖ'ëm)^Iʸ±ó»Fê©ÒLëm©]Ä`WçsÒÀyn²Xò!ºIxÚtg²£*¬ÕEÜYBz}4¯`Ò/ÅMÿ6¯báðt\V-ÙæµÚ Lù-ûPnP +!Ùl +nnL)!IYJ¶(6R6MuR +γlùsnÿ4w¯;Í-ë*»¥NU D(î¾ýp>¨ªö(á!)Û\UÖÕ|¨ÆãKmåfF$ã¤æ]åºcJÑ|Éè©KÈÏÚöG¶iÒ:Ѭ'пçù;¢aØL׿ËÓ×4LéöÀÏ+Æ¿a§"¹¶#X#ÉA4sv"ܳÏ)õWÀļþZ¡ÖË+,]'¥xÉpÁ¾6/"9J¸áGÀ>5ÜúIWÆVÍ*±tù3[ØWÄqîUWw@C+ì÷ÔWmhmüH¬íÜÆ[[¨ßÉáÞßÔÝ=Cª[ø»{ekñÏÛ64íçèñ;* ó\ å/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%':´äÐ×4nùÀ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-ØJI1VhbT{©Æ-×aâ]ÕXÕé +DðPÄuFií](ó-±0ËþYôEû¾lo9=±±õ¯ý6nÎ^´·þ'ݹ'cHÜ»ýA¥¤©¨=Ñ{øZ)/µ©dc~£ð?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ÁÜ`1YG6§"ÙôL6ø¬ÜTDCáEd=R¸DݾI(õua/gäî»§î31ǪàA³ûmæX²Û Nôÿã¦Fùï¤Ú7NÇÐÃìZ²¬K? æ« Jè8ÓÄ50>¼`)±-vp<ð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Þ5OÊ Xd +pS¤ø8QȾél8Ï ôi§£zfÔ2+7yGLDH§!ïBâ$©Ä4{Æ9%Îþ;¹U(âp[\êÀÞ:Î-?A¾BxÊØ¢+òÿöv¶·zÂÿrýa¶YåÀô^ñ´çô¶ºÍÊôãÏÆR"ûsfGl÷°QÇÐÝÍÍ»ºÝ§[af¤+N )-Üò0]F&L¦h¨º|õTÈ ÆdIhi£0=¤É#ÙT<«[ýrLüÌ+^éÕìhìãOJKNB·é,îq¼B)b9Gr¢¨¾»Æ_¬*Uà·©þÀ.d&VWnöß#{·2<×îµ_-úøæÀÎJÞN÷êwzWÜú4é§,%+êÞ]Q+ÏX¥ÜÍɦ)""Ê)¦ØîCe-;eiØëìÁÚï§V¥f!1ÆFávIeeFk{),E0¡¼ã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_PzÆiDr"@YÝÆD[fW ¡«;éÃÔjtÚ°x;ûwo7cÞ"}+1` +Õþls¤®=iY}"9FádÎJèñîxîlÁO¶Å¤}qÞeäÛTam@þâsfiX|ïNã ib®+O{ í9DÈ+d8ÅÊÐõ0)i+"4 VÔ³2î¯wE¼²f¿ð¦ÃNÿB)q¬¨lZÕ²Ò0ÏdAdÍê\äÉ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¨di:aKäpõe·(èLmó@IJ{¢F*¯áࢠ8]ªD£*ò§Ïõ[{R<¦Àix§;â5-;]Á+%|ªÙ<§neÝy + (KQØÅ*ôÒ?(¦su ¬ÞÙÞÙí<ßÞìt¶{;|wrУö+=ñ@xüΦû7¶{&Ö3y»Âö Ò©é¤ îIÞ Î8ð'dè°;jÒ°óEô©ÎÎn¹¹ºà +QXP9¹uw©ÍÿÒéÌù:@§Ã À[Àh5Ùå¢o¨sæU27raѰ'Ñ(Açå8î"q_` RòZd54(Tz4ê%°boiÃîtíKóà¯JªìÙi±$µ¨2vwq%¬þ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ÄÎzFH2lÀµ¢arC¶¥" òDÎr)Úáyfò4q}¤Qé'+çìÌo×XïÍ=jà0+BûëÃÿ5Å|N7dh&\0bÐ2J¢pí¡-Zª¶ò ÓDqÉìÎ2jå)~ò¥8µNCOÅ`(Åx³WTìb\?§2åý·¼Ã*»q NeÆM(¹]/¶9ØH»¤+c{Î/ÎZEjuAÍ! +©es¨1@¡æÁ#±éÛs+ɳªñËxRÑcém.ÅÚCT:¸¹+æÊZÌ~6| DhZIeÝò4ÑÒåQ¥v\óS5aç¹}R©§ë¢tPÜ(]ÙUô$¶>¦w$s#çvîPf²Ý×AjØ´@ S**³.&ê~åGq`Ú&Á¢£ ¿FEõ. ¼p+rà[A)ÃBI)É8ÊË>[0æ-vÞÔÈN-Ö\HÛ²sÊææEEî+ó£F}¦¸µEBO":Ù~WÔÖWüÎÛÛÏß^ÌPÉh¯ðóJÖóZÕé#¿`ruçü =ÙÓØ1Õn¸8ï&6´MIFü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Ûä ³XTH½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 xDKù)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-£ÆJyR 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¸-AHuF¨Dª=Ö»yMÑÆÜk¾Ä[.Bfæd+w.&¤Ò-¬]ð³/À§¢óôKTÕâz:N¸÷Ÿ³pk(>ªõÚ )d®e ¿ðÃõ ãë eEh"öçâgÉô¦9ö?èTVƱ´JeR%áÇtÞ\-oçjÑÌ.çüå(èÊdì¹:ZqGNEL ´iÓ*Á(¿ÀYyþBó/¯É÷`½ýh¿³IVÁxBõîû·ÉÎÀz4zS<rdpg½·:w:äýÖðY¼[M>nW6¯7W5¬5ÿ$¾CêM}XORÓ¢¸Ègøxú3q´GÞ°^r0 uðUN|ö_,x²Õæ»>¤ÈzPðA´[ªÓÉ~£,ÿOè_M@ïý=*Kç>DLIZ¬K§aq\¨ÕÖÕúù-ºPºÍÌûÌÃÌ*jªfB©Æ['5BJ$Û1ë²=6÷_ÁÖØkýÜ¥FnjíÓrlÉ(¼Á¾'ÀÇÀÏѯµùÉúBjůèdù>HH¶y&§CqcÊâ"¶¥w.ïMþ¡my̾ò¯wüߪ¢ÍÅooÚ0ÆßçSªI +°{Ew]Á³ï (±Ý·rÍD4ǹckðR5À \±oÂ#S)×{ÝgkxIþoÆü½{+&1<F3É£%:a¨c°¸Oü«Ù§É×pê£Éi¬ä*Ô©©8yñl±ìà6hd²5êÃü¤É_$,CoW zP½d³[í%]q¦à=p|ÀMBò¤÷ÖQr 5 +IrMD|SÜwÍæ¦©Wø/þ¦Bt@h{ÎÝ˨(P*ÿae ãÕ!QeÙÖ§*ar{LÁ;!³C"'txçZYöA¬µ4ÑÍl§Ô)(0|¾qÐ9åÀ¬ì`«ÞY¨ÐvK9#z£»x¬}a4õý»Ðÿx]}ý¦DU)Û x{}ì¶ÎB÷Ê»$V.£a»Jxï/é,ÙÃ{ßZ¿ú<9/׺`Oeç(±ÛÞ9ìÞ/l\#ßÑÛ^ý¼ª¼-Á-Ò»T8¸¾µ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Â÷ð&Amfð}mxõ²@ìotùIJ»Öè£{¯²4i9÷\0êN 4b^C?Xo +wlþB¾\¾î®ðËNU(ÎrUét\JÇR@+KÒÂXÔ]¹=3CÁ¿ø]\Ï©aSvV2ßm-/,KNQJ³;t+Û§@¸7¤Ý_àÃòR1K^F"%ÅX$A®W_m-¾¬P´`EH_á8Ûå@®ä\ðÜxæ¶ÏºÎ'×ðÜúýøsR×ìiÍ5ËÌ\Kºba¹,kC>'ÉÙÉ·l¤£É!zEe3w*§êp#ãF->¤ÝÎK¾a.æðýoJ.Üà]<ùS¥u-{µð´Ö¼[;wC{ßí6bºþ¿x}û(<Ê´ÐXKndÏ&£I£rZ½f.dë»5±£hHaÚ&Ok*LHµ¦/bû1Ü!>EÔ«ÑCý~|á~í?¯Ñ F5ºtèüÒÁ+[ÛïW?õ®aõâ¦éz0)ØÔµ êÈZ©I"Ë£5/¬EÃÒÍÚÿÜUÛ'ÔòÕÎQ³$«7|ù¾G±¬Ëíy*_ÚºµEIHNÕ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Õ0e*süÜVßZ.PòÎñ¼éÇq<¾Á×å²tÄÜG®i5ÉÔºL5wßSnóXØò[>CpÉMøá+æ<w©B¼ÜN@r8Ë.àÙ¹¢äñ§¨¬ùÃFh(qÈ é»IB>IÒ¼O¢w³Ï¯É4GBd1yiJU*[Bf¨tëoÂJ.¨ÂäÏþäV¨r¥#UmÁly W0§Víjñ¶Ý>òi¼ñR ¨¡Ø ÆF +Ú$ÿq4$ñF̵zÃÉeKÂiÞ\<Ä=Þ05k=lÒ¸©Öé¸x|®tN½Áyô{]?$ªCT·ë÷}²º/¡¡kÔ¯¶WËg{gÛÛÁYçGVàÁ!BÐ9`óGvÉTìw©lrÎô2Y¹bµj¶9«õ²Å¥eÙ'5âû.Üî,µb½#jÁlý +½ÂÝ«:Ü>ü}¡²©¼¾-6ZG½g 2|ßÄÝ%¤y0+ÉqD«ë}q¤ñÂý÷5VãÿÍp²äE¡NÚ³±/È(àâJMÎÈWwóô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