cvs: pear /PHP_Archive Archive.php /PHP_Archive/Archive Creator.php Exception.php Manager.php
| From: | Greg Beaver | Date: | Tue, 06 Feb 2007 04:11:57 +0000 |
| Subject: | cvs: pear /PHP_Archive Archive.php /PHP_Archive/Archive Creator.php Exception.php Manager.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-44548@lists.php.net to get a copy of this message | ||
cellog Tue Feb 6 04:11:57 2007 UTC
Modified files:
/pear/PHP_Archive Archive.php
/pear/PHP_Archive/Archive Creator.php Exception.php Manager.php
Log:
add support for creating phars with hashes
http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/Archive.php?r1=1.36&r2=1.37&diff_format=u Index: pear/PHP_Archive/Archive.php diff -u pear/PHP_Archive/Archive.php:1.36 pear/PHP_Archive/Archive.php:1.37 --- pear/PHP_Archive/Archive.php:1.36 Tue Feb 6 03:34:22 2007 +++ pear/PHP_Archive/Archive.php Tue Feb 6 04:11:56 2007 @@ -19,7 +19,7 @@ * @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.36 2007/02/06 03:34:22 cellog Exp $ + * @version $Id: Archive.php,v 1.37 2007/02/06 04:11:56 cellog Exp $ * @package PHP_Archive * @category PHP */ @@ -28,6 +28,9 @@ { const GZ = 0x00001000; const BZ2 = 0x00002000; + const SIG = 0x00010000; + const SHA1 = 0x0002; + const MD5 = 0x0001; /** * Whether this archive is compressed with zlib * http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/Archive/Creator.php?r1=1.35&r2=1.36&diff_format=u Index: pear/PHP_Archive/Archive/Creator.php diff -u pear/PHP_Archive/Archive/Creator.php:1.35 pear/PHP_Archive/Archive/Creator.php:1.36 --- pear/PHP_Archive/Archive/Creator.php:1.35 Tue Feb 6 03:34:22 2007 +++ pear/PHP_Archive/Archive/Creator.php Tue Feb 6 04:11:56 2007 @@ -24,7 +24,7 @@ * @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.35 2007/02/06 03:34:22 cellog Exp $ + * @version $Id: Creator.php,v 1.36 2007/02/06 04:11:56 cellog Exp $ * @package PHP_Archive * @category PHP */ @@ -83,7 +83,14 @@ * @var mixed */ protected $metadata = null; - + + /** + * Signature type, either PHP_Archive::SHA1 or PHP_Archive::MD5 + * + * @var int + */ + protected $sig; + /** * A list of custom callbacks that should be used for manipulating file contents * prior to adding to the phar. @@ -191,12 +198,35 @@ file_put_contents($this->temp_path . DIRECTORY_SEPARATOR . 'loader.php', $unpack_code); } + /** + * Set meta-data for entire Phar archive + * + * @param mixed $metadata + */ public function setPharMetadata($metadata) { $this->_metadata = $metadata; } /** + * Append a signature to this phar when it is created + */ + public function useSHA1Signature() + { + $this->flags |= PHP_Archive::SIG; + $this->sig = PHP_Archive::SHA1; + } + + /** + * Append a signature to this phar when it is created + */ + public function useMD5Signature() + { + $this->flags |= PHP_Archive::SIG; + $this->sig = PHP_Archive::MD5; + } + + /** * Specify a custom "magic require" callback for processing file contents. * * This will be called regardless of the magicrequire parameter's @@ -632,6 +662,19 @@ fclose($file); } fclose($newfile); + if ($this->flags & PHP_Archive::SIG) { + if ($this->sig == PHP_Archive::SHA1) { + $sig = sha1_file($file_path, true); + } elseif ($this->sig == PHP_Archive::MD5) { + $sig = md5_file($file_path, true); + } + $fp = fopen($file_path, 'ab'); + fwrite($fp, $sig); + // add signature indicator plus the magic indicator + // ah to be immortalized in file format + fwrite($fp, pack('V', $this->sig) . 'GBMB'); + fclose($fp); + } return true; } http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/Archive/Exception.php?r1=1.6&r2=1.7&diff_format=u Index: pear/PHP_Archive/Archive/Exception.php diff -u pear/PHP_Archive/Archive/Exception.php:1.6 pear/PHP_Archive/Archive/Exception.php:1.7 --- pear/PHP_Archive/Archive/Exception.php:1.6 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/Archive/Exception.php Tue Feb 6 04:11:56 2007 @@ -31,6 +31,8 @@ const FILECORRUPTEDGZ = 11; const FILECORRUPTEDSIZE = 12; const FILECORRUPTEDCRC = 13; + const NOSIGNATUREMAGIC = 14; + const BADSIGNATURE = 15; private static $_messages = array( 'en' => array( self::NOOPEN => 'Cannot open "%archive%"', @@ -46,6 +48,9 @@ self::FILECORRUPTEDSIZE => '"%archive%" file "%file%" is %actual% bytes, but size indicator at file start says it should be %expected% bytes', self::FILECORRUPTEDCRC => '"%archive%" file "%file%" has a crc32 of "%actual%" but was expecting "%expected%"', self::UNKNOWNAPI => '"%archive%" has unknown API version "%ver%"', + self::NOSIGNATUREMAGIC => '%archive% has a signature, but does not have the magic "GBMB" flags', + self::UNKNOWNSIGTYPE => '%archive% has unknown signature type "%type%"', + self::BADSIGNATURE => '%archive% is corrupted: signature does not match', ) ); private static $_lang = 'en'; http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/Archive/Manager.php?r1=1.12&r2=1.13&diff_format=u Index: pear/PHP_Archive/Archive/Manager.php diff -u pear/PHP_Archive/Archive/Manager.php:1.12 pear/PHP_Archive/Archive/Manager.php:1.13 --- pear/PHP_Archive/Archive/Manager.php:1.12 Tue Feb 6 03:34:22 2007 +++ pear/PHP_Archive/Archive/Manager.php Tue Feb 6 04:11:56 2007 @@ -16,7 +16,7 @@ * * @copyright Copyright ? Gregory Beaver * @author Greg Beaver <cellog@php.net> - * @version $Id: Manager.php,v 1.12 2007/02/06 03:34:22 cellog Exp $ + * @version $Id: Manager.php,v 1.13 2007/02/06 04:11:56 cellog Exp $ * @package PHP_Archive * @category PHP */ @@ -24,6 +24,9 @@ { const GZ = 0x00001000; const BZ2 = 0x00002000; + const SIG = 0x00010000; + const SHA1 = 0x0002; + const MD5 = 0x0001; private $_alias; private $_archiveName; private $_apiVersion; @@ -34,6 +37,8 @@ private $_manifestSize; private $_html; private $_metadata; + private $_sigtype; + private $_signature = false; /** * Locate the .phar archive in the include_path and detect the file to open within * the archive. @@ -316,6 +321,51 @@ 'actual' => crc32($data))); } } + if ($this->_flags & self::SIG) { + do { + $end = ftell($fp); + $data = fread($fp, 28); + if (substr($data, strlen($data) - 4) != 'GBMB') { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::NOSIGNATUREMAGIC, + array('archive' => $this->_archiveName)); + break; + } + $type = unpack('V', substr($data, strlen($data) - 8, 4)); + $all = file_get_contents($this->_archiveName); + switch ($type[1]) { + case self::MD5 : + $hash = substr($all, strlen($all) - 16 - 8, 16); + $all = substr($all, 0, strlen($all) - 16 - 8); + if (md5(substr($all, 0, $end), true) != $hash) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::BADSIGNATURE, + array('archive' => $this->_archiveName)); + } else { + $this->_sigtype = 'MD5'; + $this->_signature = md5($all); + } + break; + case self::SHA1 : + $hash = substr($all, strlen($all) - 20 - 8, 20); + if (sha1(substr($all, 0, $end), true) != $hash) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::BADSIGNATURE, + array('archive' => $this->_archiveName)); + } else { + $this->_sigtype = 'SHA1'; + $this->_signature = sha1($all); + } + break; + default : + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::UNKNOWNSIGTYPE, + array('archive' => $this->_archiveName, + 'type' => bin2hex(substr($data, strlen($data) - 8, 4)))); + break; + } + } while (false); + } @fclose($fp); if (count($errors)) { throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); @@ -344,6 +394,10 @@ 'Phar Metadata' => var_export($this->_metadata, true), 'Global compressed flag' => bin2hex($this->_flags), ); + if ($this->_signature) { + $ret['Signature Type'] = $this->_sigtype; + $ret['Signature'] = $this->_signature; + } // 0 = uncompressed file size // 1 = save timestamp // 2 = compressed file size
http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/Archive.php?r1=1.36&r2=1.37&diff_format=u Index: pear/PHP_Archive/Archive.php diff -u pear/PHP_Archive/Archive.php:1.36 pear/PHP_Archive/Archive.php:1.37 --- pear/PHP_Archive/Archive.php:1.36 Tue Feb 6 03:34:22 2007 +++ pear/PHP_Archive/Archive.php Tue Feb 6 04:11:56 2007 @@ -19,7 +19,7 @@ * @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.36 2007/02/06 03:34:22 cellog Exp $ + * @version $Id: Archive.php,v 1.37 2007/02/06 04:11:56 cellog Exp $ * @package PHP_Archive * @category PHP */ @@ -28,6 +28,9 @@ { const GZ = 0x00001000; const BZ2 = 0x00002000; + const SIG = 0x00010000; + const SHA1 = 0x0002; + const MD5 = 0x0001; /** * Whether this archive is compressed with zlib * http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/Archive/Creator.php?r1=1.35&r2=1.36&diff_format=u Index: pear/PHP_Archive/Archive/Creator.php diff -u pear/PHP_Archive/Archive/Creator.php:1.35 pear/PHP_Archive/Archive/Creator.php:1.36 --- pear/PHP_Archive/Archive/Creator.php:1.35 Tue Feb 6 03:34:22 2007 +++ pear/PHP_Archive/Archive/Creator.php Tue Feb 6 04:11:56 2007 @@ -24,7 +24,7 @@ * @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.35 2007/02/06 03:34:22 cellog Exp $ + * @version $Id: Creator.php,v 1.36 2007/02/06 04:11:56 cellog Exp $ * @package PHP_Archive * @category PHP */ @@ -83,7 +83,14 @@ * @var mixed */ protected $metadata = null; - + + /** + * Signature type, either PHP_Archive::SHA1 or PHP_Archive::MD5 + * + * @var int + */ + protected $sig; + /** * A list of custom callbacks that should be used for manipulating file contents * prior to adding to the phar. @@ -191,12 +198,35 @@ file_put_contents($this->temp_path . DIRECTORY_SEPARATOR . 'loader.php', $unpack_code); } + /** + * Set meta-data for entire Phar archive + * + * @param mixed $metadata + */ public function setPharMetadata($metadata) { $this->_metadata = $metadata; } /** + * Append a signature to this phar when it is created + */ + public function useSHA1Signature() + { + $this->flags |= PHP_Archive::SIG; + $this->sig = PHP_Archive::SHA1; + } + + /** + * Append a signature to this phar when it is created + */ + public function useMD5Signature() + { + $this->flags |= PHP_Archive::SIG; + $this->sig = PHP_Archive::MD5; + } + + /** * Specify a custom "magic require" callback for processing file contents. * * This will be called regardless of the magicrequire parameter's @@ -632,6 +662,19 @@ fclose($file); } fclose($newfile); + if ($this->flags & PHP_Archive::SIG) { + if ($this->sig == PHP_Archive::SHA1) { + $sig = sha1_file($file_path, true); + } elseif ($this->sig == PHP_Archive::MD5) { + $sig = md5_file($file_path, true); + } + $fp = fopen($file_path, 'ab'); + fwrite($fp, $sig); + // add signature indicator plus the magic indicator + // ah to be immortalized in file format + fwrite($fp, pack('V', $this->sig) . 'GBMB'); + fclose($fp); + } return true; } http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/Archive/Exception.php?r1=1.6&r2=1.7&diff_format=u Index: pear/PHP_Archive/Archive/Exception.php diff -u pear/PHP_Archive/Archive/Exception.php:1.6 pear/PHP_Archive/Archive/Exception.php:1.7 --- pear/PHP_Archive/Archive/Exception.php:1.6 Fri Mar 3 21:35:29 2006 +++ pear/PHP_Archive/Archive/Exception.php Tue Feb 6 04:11:56 2007 @@ -31,6 +31,8 @@ const FILECORRUPTEDGZ = 11; const FILECORRUPTEDSIZE = 12; const FILECORRUPTEDCRC = 13; + const NOSIGNATUREMAGIC = 14; + const BADSIGNATURE = 15; private static $_messages = array( 'en' => array( self::NOOPEN => 'Cannot open "%archive%"', @@ -46,6 +48,9 @@ self::FILECORRUPTEDSIZE => '"%archive%" file "%file%" is %actual% bytes, but size indicator at file start says it should be %expected% bytes', self::FILECORRUPTEDCRC => '"%archive%" file "%file%" has a crc32 of "%actual%" but was expecting "%expected%"', self::UNKNOWNAPI => '"%archive%" has unknown API version "%ver%"', + self::NOSIGNATUREMAGIC => '%archive% has a signature, but does not have the magic "GBMB" flags', + self::UNKNOWNSIGTYPE => '%archive% has unknown signature type "%type%"', + self::BADSIGNATURE => '%archive% is corrupted: signature does not match', ) ); private static $_lang = 'en'; http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/Archive/Manager.php?r1=1.12&r2=1.13&diff_format=u Index: pear/PHP_Archive/Archive/Manager.php diff -u pear/PHP_Archive/Archive/Manager.php:1.12 pear/PHP_Archive/Archive/Manager.php:1.13 --- pear/PHP_Archive/Archive/Manager.php:1.12 Tue Feb 6 03:34:22 2007 +++ pear/PHP_Archive/Archive/Manager.php Tue Feb 6 04:11:56 2007 @@ -16,7 +16,7 @@ * * @copyright Copyright ? Gregory Beaver * @author Greg Beaver <cellog@php.net> - * @version $Id: Manager.php,v 1.12 2007/02/06 03:34:22 cellog Exp $ + * @version $Id: Manager.php,v 1.13 2007/02/06 04:11:56 cellog Exp $ * @package PHP_Archive * @category PHP */ @@ -24,6 +24,9 @@ { const GZ = 0x00001000; const BZ2 = 0x00002000; + const SIG = 0x00010000; + const SHA1 = 0x0002; + const MD5 = 0x0001; private $_alias; private $_archiveName; private $_apiVersion; @@ -34,6 +37,8 @@ private $_manifestSize; private $_html; private $_metadata; + private $_sigtype; + private $_signature = false; /** * Locate the .phar archive in the include_path and detect the file to open within * the archive. @@ -316,6 +321,51 @@ 'actual' => crc32($data))); } } + if ($this->_flags & self::SIG) { + do { + $end = ftell($fp); + $data = fread($fp, 28); + if (substr($data, strlen($data) - 4) != 'GBMB') { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::NOSIGNATUREMAGIC, + array('archive' => $this->_archiveName)); + break; + } + $type = unpack('V', substr($data, strlen($data) - 8, 4)); + $all = file_get_contents($this->_archiveName); + switch ($type[1]) { + case self::MD5 : + $hash = substr($all, strlen($all) - 16 - 8, 16); + $all = substr($all, 0, strlen($all) - 16 - 8); + if (md5(substr($all, 0, $end), true) != $hash) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::BADSIGNATURE, + array('archive' => $this->_archiveName)); + } else { + $this->_sigtype = 'MD5'; + $this->_signature = md5($all); + } + break; + case self::SHA1 : + $hash = substr($all, strlen($all) - 20 - 8, 20); + if (sha1(substr($all, 0, $end), true) != $hash) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::BADSIGNATURE, + array('archive' => $this->_archiveName)); + } else { + $this->_sigtype = 'SHA1'; + $this->_signature = sha1($all); + } + break; + default : + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::UNKNOWNSIGTYPE, + array('archive' => $this->_archiveName, + 'type' => bin2hex(substr($data, strlen($data) - 8, 4)))); + break; + } + } while (false); + } @fclose($fp); if (count($errors)) { throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); @@ -344,6 +394,10 @@ 'Phar Metadata' => var_export($this->_metadata, true), 'Global compressed flag' => bin2hex($this->_flags), ); + if ($this->_signature) { + $ret['Signature Type'] = $this->_sigtype; + $ret['Signature'] = $this->_signature; + } // 0 = uncompressed file size // 1 = save timestamp // 2 = compressed file size