2007-01-21 15:25:50 +00:00
/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| phar php single - file executable PHP extension |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2016-01-01 17:19:27 +00:00
| Copyright ( c ) 2005 - 2016 The PHP Group |
2007-01-21 15:25:50 +00:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| This source file is subject to version 3.01 of the PHP license , |
| that is bundled with this package in the file LICENSE , and is |
| available through the world - wide - web at the following url : |
| http : //www.php.net/license/3_01.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world - wide - web , please send a note to |
| license @ php . net so we can mail you a copy immediately . |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Authors : Gregory Beaver < cellog @ php . net > |
| Marcus Boerger < helly @ php . net > |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
/* $Id$ */
# include "phar_internal.h"
2008-01-11 07:30:03 +00:00
# include "func_interceptors.h"
2007-01-21 15:25:50 +00:00
static zend_class_entry * phar_ce_archive ;
2008-02-27 21:34:26 +00:00
static zend_class_entry * phar_ce_data ;
2007-01-29 06:02:19 +00:00
static zend_class_entry * phar_ce_PharException ;
2007-01-21 15:25:50 +00:00
static zend_class_entry * phar_ce_entry ;
2014-12-13 22:06:14 +00:00
static int phar_file_type ( HashTable * mimes , char * file , char * * mime_type ) /* { { { */
2007-12-23 21:12:42 +00:00
{
char * ext ;
phar_mime_type * mime ;
ext = strrchr ( file , ' . ' ) ;
if ( ! ext ) {
* mime_type = " text/plain " ;
/* no file extension = assume text/plain */
return PHAR_MIME_OTHER ;
}
2008-03-22 22:11:49 +00:00
+ + ext ;
2014-05-08 14:30:07 +00:00
if ( NULL = = ( mime = zend_hash_str_find_ptr ( mimes , ext , strlen ( ext ) ) ) ) {
2007-12-23 21:12:42 +00:00
* mime_type = " application/octet-stream " ;
return PHAR_MIME_OTHER ;
}
* mime_type = mime - > mime ;
return mime - > type ;
}
/* }}} */
2014-12-13 22:06:14 +00:00
static void phar_mung_server_vars ( char * fname , char * entry , int entry_len , char * basename , int request_uri_len ) /* { { { */
2008-01-04 04:57:11 +00:00
{
2008-08-01 13:45:06 +00:00
HashTable * _SERVER ;
2014-05-08 14:30:07 +00:00
zval * stuff ;
2008-01-04 04:57:11 +00:00
char * path_info ;
2008-08-01 13:45:06 +00:00
int basename_len = strlen ( basename ) ;
2009-07-25 23:45:45 +00:00
int code ;
2014-05-08 14:30:07 +00:00
zval temp ;
2008-01-04 04:57:11 +00:00
/* "tweak" $_SERVER variables requested in earlier call to Phar::mungServer() */
2014-05-08 14:30:07 +00:00
if ( Z_TYPE ( PG ( http_globals ) [ TRACK_VARS_SERVER ] ) = = IS_UNDEF ) {
2008-01-04 04:57:11 +00:00
return ;
}
2014-05-08 14:30:07 +00:00
_SERVER = Z_ARRVAL ( PG ( http_globals ) [ TRACK_VARS_SERVER ] ) ;
2008-08-01 13:45:06 +00:00
2008-01-11 02:52:13 +00:00
/* PATH_INFO and PATH_TRANSLATED should always be munged */
2014-05-08 14:30:07 +00:00
if ( NULL ! = ( stuff = zend_hash_str_find ( _SERVER , " PATH_INFO " , sizeof ( " PATH_INFO " ) - 1 ) ) ) {
path_info = Z_STRVAL_P ( stuff ) ;
2014-08-25 17:24:55 +00:00
code = Z_STRLEN_P ( stuff ) ;
2014-05-08 14:30:07 +00:00
if ( code > entry_len & & ! memcmp ( path_info , entry , entry_len ) ) {
ZVAL_STR ( & temp , Z_STR_P ( stuff ) ) ;
ZVAL_STRINGL ( stuff , path_info + entry_len , request_uri_len ) ;
zend_hash_str_update ( _SERVER , " PHAR_PATH_INFO " , sizeof ( " PHAR_PATH_INFO " ) - 1 , & temp ) ;
2008-03-22 22:56:04 +00:00
}
2008-01-11 02:52:13 +00:00
}
2008-02-07 23:42:03 +00:00
2014-05-08 14:30:07 +00:00
if ( NULL ! = ( stuff = zend_hash_str_find ( _SERVER , " PATH_TRANSLATED " , sizeof ( " PATH_TRANSLATED " ) - 1 ) ) ) {
2014-08-13 18:18:05 +00:00
zend_string * str = strpprintf ( 4096 , " phar://%s%s " , fname , entry ) ;
2012-08-21 23:15:34 +00:00
2014-05-08 14:30:07 +00:00
ZVAL_STR ( & temp , Z_STR_P ( stuff ) ) ;
2014-09-19 11:41:01 +00:00
ZVAL_NEW_STR ( stuff , str ) ;
2014-05-08 14:30:07 +00:00
2014-08-13 18:18:05 +00:00
zend_hash_str_update ( _SERVER , " PHAR_PATH_TRANSLATED " , sizeof ( " PHAR_PATH_TRANSLATED " ) - 1 , & temp ) ;
2008-01-11 02:52:13 +00:00
}
2008-08-01 13:45:06 +00:00
2015-03-09 14:58:53 +00:00
if ( ! PHAR_G ( phar_SERVER_mung_list ) ) {
2008-01-11 02:52:13 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2015-03-09 14:58:53 +00:00
if ( PHAR_G ( phar_SERVER_mung_list ) & PHAR_MUNG_REQUEST_URI ) {
2014-05-08 14:30:07 +00:00
if ( NULL ! = ( stuff = zend_hash_str_find ( _SERVER , " REQUEST_URI " , sizeof ( " REQUEST_URI " ) - 1 ) ) ) {
path_info = Z_STRVAL_P ( stuff ) ;
2014-08-25 17:24:55 +00:00
code = Z_STRLEN_P ( stuff ) ;
2014-05-08 14:30:07 +00:00
if ( code > basename_len & & ! memcmp ( path_info , basename , basename_len ) ) {
ZVAL_STR ( & temp , Z_STR_P ( stuff ) ) ;
ZVAL_STRINGL ( stuff , path_info + basename_len , code - basename_len ) ;
zend_hash_str_update ( _SERVER , " PHAR_REQUEST_URI " , sizeof ( " PHAR_REQUEST_URI " ) - 1 , & temp ) ;
2008-03-22 22:56:04 +00:00
}
2008-02-07 23:42:03 +00:00
}
}
2008-08-01 13:45:06 +00:00
2015-03-09 14:58:53 +00:00
if ( PHAR_G ( phar_SERVER_mung_list ) & PHAR_MUNG_PHP_SELF ) {
2014-05-08 14:30:07 +00:00
if ( NULL ! = ( stuff = zend_hash_str_find ( _SERVER , " PHP_SELF " , sizeof ( " PHP_SELF " ) - 1 ) ) ) {
path_info = Z_STRVAL_P ( stuff ) ;
2014-08-25 17:24:55 +00:00
code = Z_STRLEN_P ( stuff ) ;
2014-05-08 14:30:07 +00:00
if ( code > basename_len & & ! memcmp ( path_info , basename , basename_len ) ) {
ZVAL_STR ( & temp , Z_STR_P ( stuff ) ) ;
ZVAL_STRINGL ( stuff , path_info + basename_len , code - basename_len ) ;
zend_hash_str_update ( _SERVER , " PHAR_PHP_SELF " , sizeof ( " PHAR_PHP_SELF " ) - 1 , & temp ) ;
2008-03-22 22:56:04 +00:00
}
2008-02-07 23:42:03 +00:00
}
}
2008-01-04 04:57:11 +00:00
2015-03-09 14:58:53 +00:00
if ( PHAR_G ( phar_SERVER_mung_list ) & PHAR_MUNG_SCRIPT_NAME ) {
2014-05-08 14:30:07 +00:00
if ( NULL ! = ( stuff = zend_hash_str_find ( _SERVER , " SCRIPT_NAME " , sizeof ( " SCRIPT_NAME " ) - 1 ) ) ) {
ZVAL_STR ( & temp , Z_STR_P ( stuff ) ) ;
ZVAL_STRINGL ( stuff , entry , entry_len ) ;
zend_hash_str_update ( _SERVER , " PHAR_SCRIPT_NAME " , sizeof ( " PHAR_SCRIPT_NAME " ) - 1 , & temp ) ;
2008-08-01 13:45:06 +00:00
}
2008-01-04 04:57:11 +00:00
}
2015-03-09 14:58:53 +00:00
if ( PHAR_G ( phar_SERVER_mung_list ) & PHAR_MUNG_SCRIPT_FILENAME ) {
2014-05-08 14:30:07 +00:00
if ( NULL ! = ( stuff = zend_hash_str_find ( _SERVER , " SCRIPT_FILENAME " , sizeof ( " SCRIPT_FILENAME " ) - 1 ) ) ) {
2014-08-13 18:18:05 +00:00
zend_string * str = strpprintf ( 4096 , " phar://%s%s " , fname , entry ) ;
2008-02-07 23:42:03 +00:00
2014-05-08 14:30:07 +00:00
ZVAL_STR ( & temp , Z_STR_P ( stuff ) ) ;
2014-09-19 11:41:01 +00:00
ZVAL_NEW_STR ( stuff , str ) ;
2012-08-21 23:15:34 +00:00
2014-05-08 14:30:07 +00:00
zend_hash_str_update ( _SERVER , " PHAR_SCRIPT_FILENAME " , sizeof ( " PHAR_SCRIPT_FILENAME " ) - 1 , & temp ) ;
2008-01-04 04:57:11 +00:00
}
}
}
2008-02-08 09:25:02 +00:00
/* }}} */
2008-01-04 04:57:11 +00:00
2014-12-13 22:06:14 +00:00
static int phar_file_action ( phar_archive_data * phar , phar_entry_info * info , char * mime_type , int code , char * entry , int entry_len , char * arch , char * basename , char * ru , int ru_len ) /* { { { */
2007-12-23 21:12:42 +00:00
{
2010-11-19 16:03:00 +00:00
char * name = NULL , buf [ 8192 ] ;
const char * cwd ;
2007-12-23 21:12:42 +00:00
zend_syntax_highlighter_ini syntax_highlighter_ini ;
sapi_header_line ctr = { 0 } ;
size_t got ;
2014-05-08 14:30:07 +00:00
zval dummy ;
int name_len ;
2007-12-23 21:12:42 +00:00
zend_file_handle file_handle ;
zend_op_array * new_op_array ;
2014-05-08 14:30:07 +00:00
zval result ;
2008-08-01 13:45:06 +00:00
php_stream * fp ;
2014-08-25 18:22:49 +00:00
zend_off_t position ;
2007-12-23 21:12:42 +00:00
switch ( code ) {
case PHAR_MIME_PHPS :
2008-01-04 04:57:11 +00:00
efree ( basename ) ;
2007-12-23 21:12:42 +00:00
/* highlight source */
2007-12-29 02:01:12 +00:00
if ( entry [ 0 ] = = ' / ' ) {
name_len = spprintf ( & name , 4096 , " phar://%s%s " , arch , entry ) ;
} else {
name_len = spprintf ( & name , 4096 , " phar://%s/%s " , arch , entry ) ;
}
2007-12-23 21:12:42 +00:00
php_get_highlight_struct ( & syntax_highlighter_ini ) ;
2014-12-13 22:06:14 +00:00
highlight_file ( name , & syntax_highlighter_ini ) ;
2007-12-23 21:12:42 +00:00
efree ( name ) ;
2007-12-26 16:59:01 +00:00
# ifdef PHP_WIN32
efree ( arch ) ;
# endif
2007-12-23 21:12:42 +00:00
zend_bailout ( ) ;
case PHAR_MIME_OTHER :
/* send headers, output file contents */
2008-01-04 04:57:11 +00:00
efree ( basename ) ;
2007-12-23 21:12:42 +00:00
ctr . line_len = spprintf ( & ( ctr . line ) , 0 , " Content-type: %s " , mime_type ) ;
2014-12-13 22:06:14 +00:00
sapi_header_op ( SAPI_HEADER_REPLACE , & ctr ) ;
2007-12-23 21:12:42 +00:00
efree ( ctr . line ) ;
2008-08-31 20:54:28 +00:00
ctr . line_len = spprintf ( & ( ctr . line ) , 0 , " Content-length: %u " , info - > uncompressed_filesize ) ;
2014-12-13 22:06:14 +00:00
sapi_header_op ( SAPI_HEADER_REPLACE , & ctr ) ;
2007-12-23 21:12:42 +00:00
efree ( ctr . line ) ;
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( FAILURE = = sapi_send_headers ( ) ) {
2007-12-23 21:12:42 +00:00
zend_bailout ( ) ;
}
/* prepare to output */
2014-12-13 22:06:14 +00:00
fp = phar_get_efp ( info , 1 ) ;
2008-08-01 13:45:06 +00:00
if ( ! fp ) {
2007-12-31 22:42:40 +00:00
char * error ;
2014-12-13 22:06:14 +00:00
if ( ! phar_open_jit ( phar , info , & error ) ) {
2007-12-31 22:42:40 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-12-31 22:42:40 +00:00
efree ( error ) ;
}
return - 1 ;
}
2014-12-13 22:06:14 +00:00
fp = phar_get_efp ( info , 1 ) ;
2007-12-23 21:12:42 +00:00
}
2008-08-01 13:45:06 +00:00
position = 0 ;
2014-12-13 22:06:14 +00:00
phar_seek_efp ( info , 0 , SEEK_SET , 0 , 1 ) ;
2008-08-01 13:45:06 +00:00
2007-12-23 21:12:42 +00:00
do {
2008-08-01 13:45:06 +00:00
got = php_stream_read ( fp , buf , MIN ( 8192 , info - > uncompressed_filesize - position ) ) ;
if ( got > 0 ) {
PHPWRITE ( buf , got ) ;
position + = got ;
2014-08-25 18:22:49 +00:00
if ( position = = ( zend_off_t ) info - > uncompressed_filesize ) {
2008-08-01 13:45:06 +00:00
break ;
}
2007-12-23 21:12:42 +00:00
}
} while ( 1 ) ;
2008-01-04 01:45:37 +00:00
zend_bailout ( ) ;
2007-12-23 21:12:42 +00:00
case PHAR_MIME_PHP :
2008-01-04 04:57:11 +00:00
if ( basename ) {
2014-12-13 22:06:14 +00:00
phar_mung_server_vars ( arch , entry , entry_len , basename , ru_len ) ;
2008-01-04 04:57:11 +00:00
efree ( basename ) ;
}
2008-08-01 13:45:06 +00:00
2007-12-29 02:01:12 +00:00
if ( entry [ 0 ] = = ' / ' ) {
name_len = spprintf ( & name , 4096 , " phar://%s%s " , arch , entry ) ;
} else {
name_len = spprintf ( & name , 4096 , " phar://%s/%s " , arch , entry ) ;
}
2007-12-23 21:12:42 +00:00
2008-08-01 13:45:06 +00:00
file_handle . type = ZEND_HANDLE_FILENAME ;
file_handle . handle . fd = 0 ;
file_handle . filename = name ;
file_handle . opened_path = NULL ;
file_handle . free_filename = 0 ;
2008-01-10 15:13:00 +00:00
PHAR_G ( cwd ) = NULL ;
PHAR_G ( cwd_len ) = 0 ;
2008-08-01 13:45:06 +00:00
2014-05-08 14:30:07 +00:00
ZVAL_NULL ( & dummy ) ;
if ( zend_hash_str_add ( & EG ( included_files ) , name , name_len , & dummy ) ! = NULL ) {
2008-08-01 13:45:06 +00:00
if ( ( cwd = zend_memrchr ( entry , ' / ' , entry_len ) ) ) {
2008-04-26 05:31:00 +00:00
PHAR_G ( cwd_init ) = 1 ;
2008-01-12 22:16:00 +00:00
if ( entry = = cwd ) {
/* root directory */
PHAR_G ( cwd_len ) = 0 ;
PHAR_G ( cwd ) = NULL ;
} else if ( entry [ 0 ] = = ' / ' ) {
2008-01-10 15:13:00 +00:00
PHAR_G ( cwd_len ) = cwd - ( entry + 1 ) ;
PHAR_G ( cwd ) = estrndup ( entry + 1 , PHAR_G ( cwd_len ) ) ;
} else {
PHAR_G ( cwd_len ) = cwd - entry ;
PHAR_G ( cwd ) = estrndup ( entry , PHAR_G ( cwd_len ) ) ;
}
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
new_op_array = zend_compile_file ( & file_handle , ZEND_REQUIRE ) ;
2008-08-01 13:45:06 +00:00
if ( ! new_op_array ) {
2014-05-08 14:30:07 +00:00
zend_hash_str_del ( & EG ( included_files ) , name , name_len ) ;
2008-08-01 13:45:06 +00:00
}
2014-12-13 22:06:14 +00:00
zend_destroy_file_handle ( & file_handle ) ;
2008-08-01 13:45:06 +00:00
2007-12-23 21:12:42 +00:00
} else {
2008-08-01 13:45:06 +00:00
efree ( name ) ;
2007-12-23 21:12:42 +00:00
new_op_array = NULL ;
}
2007-12-26 16:59:01 +00:00
# ifdef PHP_WIN32
efree ( arch ) ;
# endif
2007-12-23 21:12:42 +00:00
if ( new_op_array ) {
2014-05-08 14:30:07 +00:00
ZVAL_UNDEF ( & result ) ;
2008-01-10 15:13:00 +00:00
zend_try {
2014-12-13 22:06:14 +00:00
zend_execute ( new_op_array , & result ) ;
2009-04-29 03:24:09 +00:00
if ( PHAR_G ( cwd ) ) {
efree ( PHAR_G ( cwd ) ) ;
PHAR_G ( cwd ) = NULL ;
PHAR_G ( cwd_len ) = 0 ;
}
2008-08-01 13:45:06 +00:00
2009-04-29 03:24:09 +00:00
PHAR_G ( cwd_init ) = 0 ;
efree ( name ) ;
2014-12-13 22:06:14 +00:00
destroy_op_array ( new_op_array ) ;
2009-04-29 03:24:09 +00:00
efree ( new_op_array ) ;
2014-05-08 14:30:07 +00:00
zval_ptr_dtor ( & result ) ;
2009-04-29 03:24:09 +00:00
} zend_catch {
if ( PHAR_G ( cwd ) ) {
efree ( PHAR_G ( cwd ) ) ;
PHAR_G ( cwd ) = NULL ;
PHAR_G ( cwd_len ) = 0 ;
}
PHAR_G ( cwd_init ) = 0 ;
efree ( name ) ;
} zend_end_try ( ) ;
2008-08-01 13:45:06 +00:00
2007-12-23 21:12:42 +00:00
zend_bailout ( ) ;
}
2008-08-01 13:45:06 +00:00
2007-12-23 21:12:42 +00:00
return PHAR_MIME_PHP ;
}
return - 1 ;
}
2008-02-08 09:25:02 +00:00
/* }}} */
2007-12-23 21:12:42 +00:00
2014-12-13 22:06:14 +00:00
static void phar_do_403 ( char * entry , int entry_len ) /* { { { */
2008-02-07 23:42:03 +00:00
{
sapi_header_line ctr = { 0 } ;
ctr . response_code = 403 ;
2012-03-02 08:21:54 +00:00
ctr . line_len = sizeof ( " HTTP/1.0 403 Access Denied " ) - 1 ;
2008-02-07 23:42:03 +00:00
ctr . line = " HTTP/1.0 403 Access Denied " ;
2014-12-13 22:06:14 +00:00
sapi_header_op ( SAPI_HEADER_REPLACE , & ctr ) ;
sapi_send_headers ( ) ;
2008-02-07 23:42:03 +00:00
PHPWRITE ( " <html> \n <head> \n <title>Access Denied</title> \n </head> \n <body> \n <h1>403 - File " , sizeof ( " <html> \n <head> \n <title>Access Denied</title> \n </head> \n <body> \n <h1>403 - File " ) - 1 ) ;
PHPWRITE ( entry , entry_len ) ;
PHPWRITE ( " Access Denied</h1> \n </body> \n </html> " , sizeof ( " Access Denied</h1> \n </body> \n </html> " ) - 1 ) ;
}
2008-02-08 09:25:02 +00:00
/* }}} */
2008-02-07 23:42:03 +00:00
2014-12-13 22:06:14 +00:00
static void phar_do_404 ( phar_archive_data * phar , char * fname , int fname_len , char * f404 , size_t f404_len , char * entry , size_t entry_len ) /* { { { */
2007-12-29 01:35:46 +00:00
{
2008-08-01 13:45:06 +00:00
sapi_header_line ctr = { 0 } ;
phar_entry_info * info ;
if ( phar & & f404_len ) {
2014-12-13 22:06:14 +00:00
info = phar_get_entry_info ( phar , f404 , f404_len , NULL , 1 ) ;
2008-08-01 13:45:06 +00:00
if ( info ) {
2014-12-13 22:06:14 +00:00
phar_file_action ( phar , info , " text/html " , PHAR_MIME_PHP , f404 , f404_len , fname , NULL , NULL , 0 ) ;
2008-08-01 13:45:06 +00:00
return ;
2007-12-29 01:35:46 +00:00
}
}
2008-08-01 13:45:06 +00:00
ctr . response_code = 404 ;
2012-03-02 08:21:54 +00:00
ctr . line_len = sizeof ( " HTTP/1.0 404 Not Found " ) - 1 ;
2008-08-01 13:45:06 +00:00
ctr . line = " HTTP/1.0 404 Not Found " ;
2014-12-13 22:06:14 +00:00
sapi_header_op ( SAPI_HEADER_REPLACE , & ctr ) ;
sapi_send_headers ( ) ;
2008-08-01 13:45:06 +00:00
PHPWRITE ( " <html> \n <head> \n <title>File Not Found</title> \n </head> \n <body> \n <h1>404 - File " , sizeof ( " <html> \n <head> \n <title>File Not Found</title> \n </head> \n <body> \n <h1>404 - File " ) - 1 ) ;
PHPWRITE ( entry , entry_len ) ;
PHPWRITE ( " Not Found</h1> \n </body> \n </html> " , sizeof ( " Not Found</h1> \n </body> \n </html> " ) - 1 ) ;
2007-12-29 01:35:46 +00:00
}
2008-02-08 09:25:02 +00:00
/* }}} */
2007-12-29 01:35:46 +00:00
2008-04-23 16:00:06 +00:00
/* post-process REQUEST_URI and retrieve the actual request URI. This is for
cases like http : //localhost/blah.phar/path/to/file.php/extra/stuff
which calls " blah.phar " file " path/to/file.php " with PATH_INFO " /extra/stuff " */
2014-12-13 22:06:14 +00:00
static void phar_postprocess_ru_web ( char * fname , int fname_len , char * * entry , int * entry_len , char * * ru , int * ru_len ) /* { { { */
2008-02-07 23:42:03 +00:00
{
2008-04-21 17:59:28 +00:00
char * e = * entry + 1 , * u = NULL , * u1 = NULL , * saveu = NULL ;
2008-02-07 23:42:03 +00:00
int e_len = * entry_len - 1 , u_len = 0 ;
2014-05-08 14:30:07 +00:00
phar_archive_data * pphar ;
2008-02-07 23:42:03 +00:00
/* we already know we can retrieve the phar if we reach here */
2015-03-09 14:58:53 +00:00
pphar = zend_hash_str_find_ptr ( & ( PHAR_G ( phar_fname_map ) ) , fname , fname_len ) ;
2008-02-07 23:42:03 +00:00
2008-08-01 13:45:06 +00:00
if ( ! pphar & & PHAR_G ( manifest_cached ) ) {
2014-05-08 14:30:07 +00:00
pphar = zend_hash_str_find_ptr ( & cached_phars , fname , fname_len ) ;
2008-08-01 13:45:06 +00:00
}
2008-02-07 23:42:03 +00:00
do {
2014-05-08 14:30:07 +00:00
if ( zend_hash_str_exists ( & ( pphar - > manifest ) , e , e_len ) ) {
2008-02-07 23:42:03 +00:00
if ( u ) {
u [ 0 ] = ' / ' ;
* ru = estrndup ( u , u_len + 1 ) ;
2008-03-22 22:11:49 +00:00
+ + u_len ;
2008-02-07 23:42:03 +00:00
u [ 0 ] = ' \0 ' ;
} else {
* ru = NULL ;
}
* ru_len = u_len ;
* entry_len = e_len + 1 ;
return ;
}
2008-08-01 13:45:06 +00:00
2008-02-07 23:42:03 +00:00
if ( u ) {
2008-04-21 17:59:28 +00:00
u1 = strrchr ( e , ' / ' ) ;
2008-02-07 23:42:03 +00:00
u [ 0 ] = ' / ' ;
saveu = u ;
2008-04-21 17:59:28 +00:00
e_len + = u_len + 1 ;
u = u1 ;
if ( ! u ) {
return ;
}
} else {
u = strrchr ( e , ' / ' ) ;
if ( ! u ) {
if ( saveu ) {
saveu [ 0 ] = ' / ' ;
}
return ;
2008-02-07 23:42:03 +00:00
}
}
2008-08-01 13:45:06 +00:00
2008-02-07 23:42:03 +00:00
u [ 0 ] = ' \0 ' ;
u_len = strlen ( u + 1 ) ;
e_len - = u_len + 1 ;
2008-08-01 13:45:06 +00:00
2008-04-21 17:46:24 +00:00
if ( e_len < 0 ) {
if ( saveu ) {
saveu [ 0 ] = ' / ' ;
}
return ;
}
2008-02-07 23:42:03 +00:00
} while ( 1 ) ;
}
2008-02-08 09:25:02 +00:00
/* }}} */
2008-02-07 23:42:03 +00:00
2008-03-23 06:00:31 +00:00
/* {{{ proto void Phar::running([bool retphar = true])
* return the name of the currently running phar archive . If the optional parameter
* is set to true , return the phar : // URL to the currently running phar
2008-02-25 04:23:36 +00:00
*/
2008-03-23 06:00:31 +00:00
PHP_METHOD ( Phar , running )
2008-02-25 04:23:36 +00:00
{
char * fname , * arch , * entry ;
int fname_len , arch_len , entry_len ;
2008-03-23 06:00:31 +00:00
zend_bool retphar = 1 ;
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " |b " , & retphar ) = = FAILURE ) {
2008-03-23 06:00:31 +00:00
return ;
}
2008-02-25 04:23:36 +00:00
2014-12-13 22:06:14 +00:00
fname = ( char * ) zend_get_executed_filename ( ) ;
2008-02-25 04:23:36 +00:00
fname_len = strlen ( fname ) ;
2014-12-13 22:06:14 +00:00
if ( fname_len > 7 & & ! memcmp ( fname , " phar:// " , 7 ) & & SUCCESS = = phar_split_fname ( fname , fname_len , & arch , & arch_len , & entry , & entry_len , 2 , 0 ) ) {
2008-02-25 04:23:36 +00:00
efree ( entry ) ;
2008-03-23 06:00:31 +00:00
if ( retphar ) {
2014-05-08 14:30:07 +00:00
RETVAL_STRINGL ( fname , arch_len + 7 ) ;
2008-03-23 06:00:31 +00:00
efree ( arch ) ;
return ;
} else {
2014-05-08 14:30:07 +00:00
// TODO: avoid reallocation ???
RETVAL_STRINGL ( arch , arch_len ) ;
efree ( arch ) ;
return ;
2008-03-23 06:00:31 +00:00
}
2008-02-25 04:23:36 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 14:30:07 +00:00
RETURN_EMPTY_STRING ( ) ;
2008-02-25 04:23:36 +00:00
}
/* }}} */
2008-02-18 04:32:50 +00:00
/* {{{ proto void Phar::mount(string pharpath, string externalfile)
* mount an external file or path to a location within the phar . This maps
* an external file or directory to a location within the phar archive , allowing
* reference to an external location as if it were within the phar archive . This
* is useful for writable temp files like databases
*/
PHP_METHOD ( Phar , mount )
{
2008-05-18 23:12:47 +00:00
char * fname , * arch = NULL , * entry = NULL , * path , * actual ;
2014-08-27 19:53:06 +00:00
int fname_len , arch_len , entry_len ;
size_t path_len , actual_len ;
2014-05-08 14:30:07 +00:00
phar_archive_data * pphar ;
2008-02-18 04:32:50 +00:00
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " pp " , & path , & path_len , & actual , & actual_len ) = = FAILURE ) {
2008-02-18 04:32:50 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
fname = ( char * ) zend_get_executed_filename ( ) ;
2008-02-18 04:32:50 +00:00
fname_len = strlen ( fname ) ;
2008-08-01 13:45:06 +00:00
# ifdef PHP_WIN32
phar_unixify_path_separators ( fname , fname_len ) ;
# endif
2014-12-13 22:06:14 +00:00
if ( fname_len > 7 & & ! memcmp ( fname , " phar:// " , 7 ) & & SUCCESS = = phar_split_fname ( fname , fname_len , & arch , & arch_len , & entry , & entry_len , 2 , 0 ) ) {
2008-02-25 04:23:36 +00:00
efree ( entry ) ;
2008-04-21 18:21:54 +00:00
entry = NULL ;
2008-08-01 13:45:06 +00:00
2008-04-21 18:21:54 +00:00
if ( path_len > 7 & & ! memcmp ( path , " phar:// " , 7 ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " Can only mount internal paths within a phar archive, use a relative path instead of \" %s \" " , path ) ;
2008-04-21 18:21:54 +00:00
efree ( arch ) ;
return ;
}
carry_on2 :
2015-03-09 14:58:53 +00:00
if ( NULL = = ( pphar = zend_hash_str_find_ptr ( & ( PHAR_G ( phar_fname_map ) ) , arch , arch_len ) ) ) {
2014-05-08 14:30:07 +00:00
if ( PHAR_G ( manifest_cached ) & & NULL ! = ( pphar = zend_hash_str_find_ptr ( & cached_phars , arch , arch_len ) ) ) {
2014-12-13 22:06:14 +00:00
if ( SUCCESS = = phar_copy_on_write ( & pphar ) ) {
2008-08-01 13:45:06 +00:00
goto carry_on ;
}
}
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s is not a phar archive, cannot mount " , arch ) ;
2008-08-01 13:45:06 +00:00
2008-05-18 23:12:47 +00:00
if ( arch ) {
efree ( arch ) ;
}
2008-02-18 04:32:50 +00:00
return ;
}
2008-02-25 04:23:36 +00:00
carry_on :
2014-12-13 22:06:14 +00:00
if ( SUCCESS ! = phar_mount_entry ( pphar , actual , actual_len , path , path_len ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " Mounting of %s to %s within phar %s failed " , path , actual , arch ) ;
2008-04-21 18:21:54 +00:00
if ( path & & path = = entry ) {
efree ( entry ) ;
}
2008-08-01 13:45:06 +00:00
2008-05-18 23:12:47 +00:00
if ( arch ) {
efree ( arch ) ;
}
2008-08-01 13:45:06 +00:00
2008-04-26 05:31:00 +00:00
return ;
2008-02-18 04:32:50 +00:00
}
2008-08-01 13:45:06 +00:00
2008-05-18 23:12:47 +00:00
if ( entry & & path & & path = = entry ) {
2008-04-21 18:21:54 +00:00
efree ( entry ) ;
}
2008-08-01 13:45:06 +00:00
2008-05-18 23:12:47 +00:00
if ( arch ) {
efree ( arch ) ;
}
2008-08-01 13:45:06 +00:00
2008-02-25 04:23:36 +00:00
return ;
2015-03-09 14:58:53 +00:00
} else if ( PHAR_G ( phar_fname_map . u . flags ) & & NULL ! = ( pphar = zend_hash_str_find_ptr ( & ( PHAR_G ( phar_fname_map ) ) , fname , fname_len ) ) ) {
2008-08-01 13:45:06 +00:00
goto carry_on ;
2014-05-08 14:30:07 +00:00
} else if ( PHAR_G ( manifest_cached ) & & NULL ! = ( pphar = zend_hash_str_find_ptr ( & cached_phars , fname , fname_len ) ) ) {
2014-12-13 22:06:14 +00:00
if ( SUCCESS = = phar_copy_on_write ( & pphar ) ) {
2008-08-01 13:45:06 +00:00
goto carry_on ;
}
2008-02-25 04:23:36 +00:00
goto carry_on ;
2014-12-13 22:06:14 +00:00
} else if ( SUCCESS = = phar_split_fname ( path , path_len , & arch , & arch_len , & entry , & entry_len , 2 , 0 ) ) {
2008-04-21 18:21:54 +00:00
path = entry ;
path_len = entry_len ;
goto carry_on2 ;
2008-02-18 04:32:50 +00:00
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " Mounting of %s to %s failed " , path , actual ) ;
2008-02-18 04:32:50 +00:00
}
/* }}} */
2008-02-07 23:42:03 +00:00
/* {{{ proto void Phar::webPhar([string alias, [string index, [string f404, [array mimetypes, [callback rewrites]]]]])
2007-12-23 21:12:42 +00:00
* mapPhar for web - based phars . Reads the currently executed file ( a phar )
2007-12-24 20:30:44 +00:00
* and registers its manifest . When executed in the CLI or CGI command - line sapi ,
* this works exactly like mapPhar ( ) . When executed by a web - based sapi , this
* reads $ _SERVER [ ' REQUEST_URI ' ] ( the actual original value ) and parses out the
* intended internal file .
2007-12-23 21:12:42 +00:00
*/
PHP_METHOD ( Phar , webPhar )
{
2008-02-07 23:42:03 +00:00
zval * mimeoverride = NULL , * rewrite = NULL ;
2008-05-07 05:38:31 +00:00
char * alias = NULL , * error , * index_php = NULL , * f404 = NULL , * ru = NULL ;
2014-12-16 08:31:20 +00:00
size_t alias_len = 0 , f404_len = 0 , free_pathinfo = 0 ;
int ru_len = 0 ;
2010-11-19 16:03:00 +00:00
char * fname , * path_info , * mime_type = NULL , * entry , * pt ;
const char * basename ;
2014-08-27 17:25:28 +00:00
size_t fname_len , index_php_len = 0 ;
int entry_len , code , not_cgi ;
2008-08-01 13:45:06 +00:00
phar_archive_data * phar = NULL ;
2011-08-07 11:13:27 +00:00
phar_entry_info * info = NULL ;
2014-09-18 19:28:02 +00:00
size_t sapi_mod_name_len = strlen ( sapi_module . name ) ;
2007-12-23 21:12:42 +00:00
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " |s!s!saz " , & alias , & alias_len , & index_php , & index_php_len , & f404 , & f404_len , & mimeoverride , & rewrite ) = = FAILURE ) {
2007-12-23 21:12:42 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
phar_request_initialize ( ) ;
fname = ( char * ) zend_get_executed_filename ( ) ;
2008-02-11 06:49:22 +00:00
fname_len = strlen ( fname ) ;
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( phar_open_executed_filename ( alias , alias_len , & error ) ! = SUCCESS ) {
2007-12-23 21:12:42 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-12-23 21:12:42 +00:00
efree ( error ) ;
}
return ;
}
/* retrieve requested file within phar */
if ( ! ( SG ( request_info ) . request_method & & SG ( request_info ) . request_uri & & ( ! strcmp ( SG ( request_info ) . request_method , " GET " ) | | ! strcmp ( SG ( request_info ) . request_method , " POST " ) ) ) ) {
return ;
}
2008-08-01 13:45:06 +00:00
2007-12-26 16:59:01 +00:00
# ifdef PHP_WIN32
fname = estrndup ( fname , fname_len ) ;
phar_unixify_path_separators ( fname , fname_len ) ;
# endif
2008-08-01 13:45:06 +00:00
basename = zend_memrchr ( fname , ' / ' , fname_len ) ;
2007-12-23 21:12:42 +00:00
if ( ! basename ) {
basename = fname ;
} else {
2008-03-22 22:11:49 +00:00
+ + basename ;
2007-12-23 21:12:42 +00:00
}
2008-01-04 04:57:11 +00:00
2014-09-18 19:28:02 +00:00
if ( ( sapi_mod_name_len = = sizeof ( " cgi-fcgi " ) - 1 & & ! strncmp ( sapi_module . name , " cgi-fcgi " , sizeof ( " cgi-fcgi " ) - 1 ) )
| | ( sapi_mod_name_len = = sizeof ( " fpm-fcgi " ) - 1 & & ! strncmp ( sapi_module . name , " fpm-fcgi " , sizeof ( " fpm-fcgi " ) - 1 ) )
| | ( sapi_mod_name_len = = sizeof ( " cgi " ) - 1 & & ! strncmp ( sapi_module . name , " cgi " , sizeof ( " cgi " ) - 1 ) ) ) {
2008-02-07 04:24:58 +00:00
2014-05-08 14:30:07 +00:00
if ( Z_TYPE ( PG ( http_globals ) [ TRACK_VARS_SERVER ] ) ! = IS_UNDEF ) {
HashTable * _server = Z_ARRVAL ( PG ( http_globals ) [ TRACK_VARS_SERVER ] ) ;
zval * z_script_name , * z_path_info ;
2008-08-01 13:45:06 +00:00
2014-05-08 14:30:07 +00:00
if ( NULL = = ( z_script_name = zend_hash_str_find ( _server , " SCRIPT_NAME " , sizeof ( " SCRIPT_NAME " ) - 1 ) ) | |
IS_STRING ! = Z_TYPE_P ( z_script_name ) | |
! strstr ( Z_STRVAL_P ( z_script_name ) , basename ) ) {
2008-08-01 13:45:06 +00:00
return ;
}
2014-05-08 14:30:07 +00:00
if ( NULL ! = ( z_path_info = zend_hash_str_find ( _server , " PATH_INFO " , sizeof ( " PATH_INFO " ) - 1 ) ) & &
IS_STRING = = Z_TYPE_P ( z_path_info ) ) {
2014-08-25 17:24:55 +00:00
entry_len = Z_STRLEN_P ( z_path_info ) ;
2014-05-08 14:30:07 +00:00
entry = estrndup ( Z_STRVAL_P ( z_path_info ) , entry_len ) ;
2014-08-25 17:24:55 +00:00
path_info = emalloc ( Z_STRLEN_P ( z_script_name ) + entry_len + 1 ) ;
memcpy ( path_info , Z_STRVAL_P ( z_script_name ) , Z_STRLEN_P ( z_script_name ) ) ;
memcpy ( path_info + Z_STRLEN_P ( z_script_name ) , entry , entry_len + 1 ) ;
2008-08-01 13:45:06 +00:00
free_pathinfo = 1 ;
} else {
entry_len = 0 ;
entry = estrndup ( " " , 0 ) ;
2014-05-08 14:30:07 +00:00
path_info = Z_STRVAL_P ( z_script_name ) ;
2008-08-01 13:45:06 +00:00
}
2014-08-25 17:24:55 +00:00
pt = estrndup ( Z_STRVAL_P ( z_script_name ) , Z_STRLEN_P ( z_script_name ) ) ;
2008-08-01 13:45:06 +00:00
2008-02-07 04:24:58 +00:00
} else {
2008-08-01 13:45:06 +00:00
char * testit ;
2014-12-13 22:06:14 +00:00
testit = sapi_getenv ( " SCRIPT_NAME " , sizeof ( " SCRIPT_NAME " ) - 1 ) ;
2008-08-01 13:45:06 +00:00
if ( ! ( pt = strstr ( testit , basename ) ) ) {
efree ( testit ) ;
return ;
}
2014-12-13 22:06:14 +00:00
path_info = sapi_getenv ( " PATH_INFO " , sizeof ( " PATH_INFO " ) - 1 ) ;
2008-08-01 13:45:06 +00:00
if ( path_info ) {
entry = path_info ;
entry_len = strlen ( entry ) ;
spprintf ( & path_info , 0 , " %s%s " , testit , path_info ) ;
free_pathinfo = 1 ;
} else {
path_info = testit ;
free_pathinfo = 1 ;
entry = estrndup ( " " , 0 ) ;
entry_len = 0 ;
}
pt = estrndup ( testit , ( pt - testit ) + ( fname_len - ( basename - fname ) ) ) ;
2008-02-07 04:24:58 +00:00
}
2008-02-08 01:08:02 +00:00
not_cgi = 0 ;
2008-02-07 04:24:58 +00:00
} else {
path_info = SG ( request_info ) . request_uri ;
2008-02-07 23:42:03 +00:00
if ( ! ( pt = strstr ( path_info , basename ) ) ) {
/* this can happen with rewrite rules - and we have no idea what to do then, so return */
return ;
}
2008-08-01 13:45:06 +00:00
entry_len = strlen ( path_info ) ;
2008-02-07 23:42:03 +00:00
entry_len - = ( pt - path_info ) + ( fname_len - ( basename - fname ) ) ;
entry = estrndup ( pt + ( fname_len - ( basename - fname ) ) , entry_len ) ;
pt = estrndup ( path_info , ( pt - path_info ) + ( fname_len - ( basename - fname ) ) ) ;
2008-02-08 01:08:02 +00:00
not_cgi = 1 ;
2008-01-04 04:57:11 +00:00
}
2008-02-07 23:42:03 +00:00
if ( rewrite ) {
zend_fcall_info fci ;
zend_fcall_info_cache fcc ;
2014-05-08 14:30:07 +00:00
zval params , retval ;
2008-02-07 23:42:03 +00:00
2014-05-08 14:30:07 +00:00
ZVAL_STRINGL ( & params , entry , entry_len ) ;
2008-02-07 23:42:03 +00:00
2014-12-13 22:06:14 +00:00
if ( FAILURE = = zend_fcall_info_init ( rewrite , 0 , & fci , & fcc , NULL , NULL ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar error: invalid rewrite callback " ) ;
2008-08-01 13:45:06 +00:00
2008-02-07 23:42:03 +00:00
if ( free_pathinfo ) {
efree ( path_info ) ;
}
2008-08-01 13:45:06 +00:00
2008-02-07 23:42:03 +00:00
return ;
}
fci . param_count = 1 ;
2014-05-08 14:30:07 +00:00
fci . params = & params ;
Z_ADDREF ( params ) ;
fci . retval = & retval ;
2008-01-04 04:57:11 +00:00
2014-12-13 22:06:14 +00:00
if ( FAILURE = = zend_call_function ( & fci , & fcc ) ) {
2008-02-07 23:42:03 +00:00
if ( ! EG ( exception ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar error: failed to call rewrite callback " ) ;
2008-02-07 23:42:03 +00:00
}
2008-08-01 13:45:06 +00:00
2008-02-07 23:42:03 +00:00
if ( free_pathinfo ) {
efree ( path_info ) ;
}
2008-08-01 13:45:06 +00:00
2008-02-07 23:42:03 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 14:30:07 +00:00
if ( Z_TYPE_P ( fci . retval ) = = IS_UNDEF | | Z_TYPE ( retval ) = = IS_UNDEF ) {
2008-02-07 23:42:03 +00:00
if ( free_pathinfo ) {
efree ( path_info ) ;
}
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar error: rewrite callback must return a string or false " ) ;
2008-02-07 23:42:03 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 14:30:07 +00:00
switch ( Z_TYPE ( retval ) ) {
2008-05-19 19:05:42 +00:00
case IS_STRING :
2008-02-07 23:42:03 +00:00
efree ( entry ) ;
2014-08-25 17:24:55 +00:00
entry = estrndup ( Z_STRVAL_P ( fci . retval ) , Z_STRLEN_P ( fci . retval ) ) ;
entry_len = Z_STRLEN_P ( fci . retval ) ;
2008-02-07 23:42:03 +00:00
break ;
2014-05-08 14:30:07 +00:00
case IS_TRUE :
case IS_FALSE :
2014-12-13 22:06:14 +00:00
phar_do_403 ( entry , entry_len ) ;
2008-08-01 13:45:06 +00:00
2008-02-07 23:42:03 +00:00
if ( free_pathinfo ) {
efree ( path_info ) ;
}
2008-08-01 13:45:06 +00:00
2008-02-07 23:42:03 +00:00
zend_bailout ( ) ;
return ;
default :
if ( free_pathinfo ) {
efree ( path_info ) ;
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar error: rewrite callback must return a string or false " ) ;
2008-02-07 23:42:03 +00:00
return ;
}
}
2008-02-07 04:24:58 +00:00
2008-02-07 23:42:03 +00:00
if ( entry_len ) {
2014-12-16 09:21:05 +00:00
phar_postprocess_ru_web ( fname , fname_len , & entry , & entry_len , & ru , & ru_len ) ;
2008-02-07 23:42:03 +00:00
}
2008-08-01 13:45:06 +00:00
2008-01-04 04:57:11 +00:00
if ( ! entry_len | | ( entry_len = = 1 & & entry [ 0 ] = = ' / ' ) ) {
efree ( entry ) ;
/* direct request */
if ( index_php_len ) {
entry = index_php ;
entry_len = index_php_len ;
2008-02-02 05:20:37 +00:00
if ( entry [ 0 ] ! = ' / ' ) {
spprintf ( & entry , 0 , " /%s " , index_php ) ;
2008-03-22 22:11:49 +00:00
+ + entry_len ;
2008-02-02 05:20:37 +00:00
}
2008-01-04 04:57:11 +00:00
} else {
/* assume "index.php" is starting point */
entry = estrndup ( " /index.php " , sizeof ( " /index.php " ) ) ;
entry_len = sizeof ( " /index.php " ) - 1 ;
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_get_archive ( & phar , fname , fname_len , NULL , 0 , NULL ) | |
( info = phar_get_entry_info ( phar , entry , entry_len , NULL , 0 ) ) = = NULL ) {
phar_do_404 ( phar , fname , fname_len , f404 , f404_len , entry , entry_len ) ;
2008-08-01 13:45:06 +00:00
2008-02-07 04:24:58 +00:00
if ( free_pathinfo ) {
efree ( path_info ) ;
}
2008-08-01 13:45:06 +00:00
2008-01-04 04:57:11 +00:00
zend_bailout ( ) ;
} else {
2011-08-07 11:13:27 +00:00
char * tmp = NULL , sa = ' \0 ' ;
2008-01-04 04:57:11 +00:00
sapi_header_line ctr = { 0 } ;
ctr . response_code = 301 ;
2012-03-02 08:21:54 +00:00
ctr . line_len = sizeof ( " HTTP/1.1 301 Moved Permanently " ) - 1 ;
2008-01-04 04:57:11 +00:00
ctr . line = " HTTP/1.1 301 Moved Permanently " ;
2014-12-13 22:06:14 +00:00
sapi_header_op ( SAPI_HEADER_REPLACE , & ctr ) ;
2008-01-04 04:57:11 +00:00
2008-02-08 00:58:39 +00:00
if ( not_cgi ) {
tmp = strstr ( path_info , basename ) + fname_len ;
2008-02-08 00:55:38 +00:00
sa = * tmp ;
* tmp = ' \0 ' ;
}
2008-08-01 13:45:06 +00:00
2008-01-04 04:57:11 +00:00
ctr . response_code = 0 ;
2008-08-01 13:45:06 +00:00
2008-02-02 05:20:37 +00:00
if ( path_info [ strlen ( path_info ) - 1 ] = = ' / ' ) {
ctr . line_len = spprintf ( & ( ctr . line ) , 4096 , " Location: %s%s " , path_info , entry + 1 ) ;
2008-01-05 03:49:01 +00:00
} else {
2008-02-02 05:20:37 +00:00
ctr . line_len = spprintf ( & ( ctr . line ) , 4096 , " Location: %s%s " , path_info , entry ) ;
2008-01-05 03:49:01 +00:00
}
2008-08-01 13:45:06 +00:00
2008-02-08 00:58:39 +00:00
if ( not_cgi ) {
2008-02-08 00:55:38 +00:00
* tmp = sa ;
}
2008-08-01 13:45:06 +00:00
2008-02-07 04:24:58 +00:00
if ( free_pathinfo ) {
efree ( path_info ) ;
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
sapi_header_op ( SAPI_HEADER_REPLACE , & ctr ) ;
sapi_send_headers ( ) ;
2008-01-04 04:57:11 +00:00
efree ( ctr . line ) ;
zend_bailout ( ) ;
2007-12-24 20:30:44 +00:00
}
2007-12-23 21:12:42 +00:00
}
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_get_archive ( & phar , fname , fname_len , NULL , 0 , NULL ) | |
( info = phar_get_entry_info ( phar , entry , entry_len , NULL , 0 ) ) = = NULL ) {
phar_do_404 ( phar , fname , fname_len , f404 , f404_len , entry , entry_len ) ;
2007-12-29 01:35:46 +00:00
# ifdef PHP_WIN32
efree ( fname ) ;
# endif
zend_bailout ( ) ;
}
2008-08-01 13:45:06 +00:00
if ( mimeoverride & & zend_hash_num_elements ( Z_ARRVAL_P ( mimeoverride ) ) ) {
2010-11-19 16:03:00 +00:00
const char * ext = zend_memrchr ( entry , ' . ' , entry_len ) ;
2014-05-08 14:30:07 +00:00
zval * val ;
2008-05-14 21:27:31 +00:00
2008-08-01 13:45:06 +00:00
if ( ext ) {
+ + ext ;
2008-05-14 21:27:31 +00:00
2014-05-08 14:30:07 +00:00
if ( NULL ! = ( val = zend_hash_str_find ( Z_ARRVAL_P ( mimeoverride ) , ext , strlen ( ext ) ) ) ) {
switch ( Z_TYPE_P ( val ) ) {
2014-08-25 17:24:55 +00:00
case IS_LONG :
if ( Z_LVAL_P ( val ) = = PHAR_MIME_PHP | | Z_LVAL_P ( val ) = = PHAR_MIME_PHPS ) {
2008-08-01 13:45:06 +00:00
mime_type = " " ;
2014-08-25 17:24:55 +00:00
code = Z_LVAL_P ( val ) ;
2008-08-01 13:45:06 +00:00
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " Unknown mime type specifier used, only Phar::PHP, Phar::PHPS and a mime type string are allowed " ) ;
2015-03-11 14:26:33 +00:00
if ( free_pathinfo ) {
efree ( path_info ) ;
}
efree ( pt ) ;
efree ( entry ) ;
2007-12-26 16:59:01 +00:00
# ifdef PHP_WIN32
2008-08-01 13:45:06 +00:00
efree ( fname ) ;
2007-12-26 16:59:01 +00:00
# endif
2008-08-01 13:45:06 +00:00
RETURN_FALSE ;
}
break ;
case IS_STRING :
2014-05-08 14:30:07 +00:00
mime_type = Z_STRVAL_P ( val ) ;
2008-08-01 13:45:06 +00:00
code = PHAR_MIME_OTHER ;
break ;
default :
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " Unknown mime type specifier used (not a string or int), only Phar::PHP, Phar::PHPS and a mime type string are allowed " ) ;
2015-03-11 14:26:33 +00:00
if ( free_pathinfo ) {
efree ( path_info ) ;
}
efree ( pt ) ;
efree ( entry ) ;
2008-01-05 05:47:47 +00:00
# ifdef PHP_WIN32
efree ( fname ) ;
# endif
2007-12-23 21:12:42 +00:00
RETURN_FALSE ;
2008-08-01 13:45:06 +00:00
}
2007-12-23 21:12:42 +00:00
}
}
}
2007-12-29 02:38:29 +00:00
2008-08-01 13:45:06 +00:00
if ( ! mime_type ) {
2014-12-13 22:06:14 +00:00
code = phar_file_type ( & PHAR_G ( mime_types ) , entry , & mime_type ) ;
2008-08-01 13:45:06 +00:00
}
2014-12-13 22:06:14 +00:00
phar_file_action ( phar , info , mime_type , code , entry , entry_len , fname , pt , ru , ru_len ) ;
2007-12-23 21:12:42 +00:00
}
2008-02-08 09:25:02 +00:00
/* }}} */
2007-12-23 21:12:42 +00:00
2008-01-04 04:57:11 +00:00
/* {{{ proto void Phar::mungServer(array munglist)
* Defines a list of up to 4 $ _SERVER variables that should be modified for execution
* to mask the presence of the phar archive . This should be used in conjunction with
* Phar : : webPhar ( ) , and has no effect otherwise
* SCRIPT_NAME , PHP_SELF , REQUEST_URI and SCRIPT_FILENAME
*/
PHP_METHOD ( Phar , mungServer )
{
2014-05-28 10:09:42 +00:00
zval * mungvalues , * data ;
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " a " , & mungvalues ) = = FAILURE ) {
2008-01-04 04:57:11 +00:00
return ;
}
if ( ! zend_hash_num_elements ( Z_ARRVAL_P ( mungvalues ) ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " No values passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME " ) ;
2008-01-04 04:57:11 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2008-01-04 04:57:11 +00:00
if ( zend_hash_num_elements ( Z_ARRVAL_P ( mungvalues ) ) > 4 ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " Too many values passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME " ) ;
2008-01-04 04:57:11 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
phar_request_initialize ( ) ;
2008-01-05 22:02:33 +00:00
2014-05-28 10:09:42 +00:00
ZEND_HASH_FOREACH_VAL ( Z_ARRVAL_P ( mungvalues ) , data ) {
2008-05-19 19:05:42 +00:00
2014-05-08 14:30:07 +00:00
if ( Z_TYPE_P ( data ) ! = IS_STRING ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " Non-string value passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME " ) ;
2008-01-04 04:57:11 +00:00
return ;
}
2008-05-19 19:05:42 +00:00
2014-08-25 17:24:55 +00:00
if ( Z_STRLEN_P ( data ) = = sizeof ( " PHP_SELF " ) - 1 & & ! strncmp ( Z_STRVAL_P ( data ) , " PHP_SELF " , sizeof ( " PHP_SELF " ) - 1 ) ) {
2015-03-09 14:58:53 +00:00
PHAR_G ( phar_SERVER_mung_list ) | = PHAR_MUNG_PHP_SELF ;
2008-01-04 04:57:11 +00:00
}
2008-08-01 13:45:06 +00:00
2014-08-25 17:24:55 +00:00
if ( Z_STRLEN_P ( data ) = = sizeof ( " REQUEST_URI " ) - 1 ) {
2014-05-08 14:30:07 +00:00
if ( ! strncmp ( Z_STRVAL_P ( data ) , " REQUEST_URI " , sizeof ( " REQUEST_URI " ) - 1 ) ) {
2015-03-09 14:58:53 +00:00
PHAR_G ( phar_SERVER_mung_list ) | = PHAR_MUNG_REQUEST_URI ;
2008-01-04 04:57:11 +00:00
}
2014-05-08 14:30:07 +00:00
if ( ! strncmp ( Z_STRVAL_P ( data ) , " SCRIPT_NAME " , sizeof ( " SCRIPT_NAME " ) - 1 ) ) {
2015-03-09 14:58:53 +00:00
PHAR_G ( phar_SERVER_mung_list ) | = PHAR_MUNG_SCRIPT_NAME ;
2008-01-04 04:57:11 +00:00
}
}
2008-08-01 13:45:06 +00:00
2014-08-25 17:24:55 +00:00
if ( Z_STRLEN_P ( data ) = = sizeof ( " SCRIPT_FILENAME " ) - 1 & & ! strncmp ( Z_STRVAL_P ( data ) , " SCRIPT_FILENAME " , sizeof ( " SCRIPT_FILENAME " ) - 1 ) ) {
2015-03-09 14:58:53 +00:00
PHAR_G ( phar_SERVER_mung_list ) | = PHAR_MUNG_SCRIPT_FILENAME ;
2008-01-04 04:57:11 +00:00
}
2014-05-28 10:09:42 +00:00
} ZEND_HASH_FOREACH_END ( ) ;
2008-01-04 04:57:11 +00:00
}
/* }}} */
2008-01-11 07:30:03 +00:00
/* {{{ proto void Phar::interceptFileFuncs()
* instructs phar to intercept fopen , file_get_contents , opendir , and all of the stat - related functions
* and return stat on files within the phar for relative paths
*
* Once called , this cannot be reversed , and continue until the end of the request .
*
* This allows legacy scripts to be pharred unmodified
*/
PHP_METHOD ( Phar , interceptFileFuncs )
{
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2014-12-13 22:06:14 +00:00
phar_intercept_functions ( ) ;
2008-01-11 07:30:03 +00:00
}
/* }}} */
2008-02-08 09:25:02 +00:00
/* {{{ proto array Phar::createDefaultStub([string indexfile[, string webindexfile]])
2008-01-19 04:26:22 +00:00
* Return a stub that can be used to run a phar - based archive without the phar extension
2008-01-20 00:49:45 +00:00
* indexfile is the CLI startup filename , which defaults to " index.php " , webindexfile
* is the web startup filename , and also defaults to " index.php "
2008-01-19 04:26:22 +00:00
*/
PHP_METHOD ( Phar , createDefaultStub )
{
2015-07-02 17:29:06 +00:00
char * index = NULL , * webindex = NULL , * error ;
zend_string * stub ;
2014-08-27 13:31:48 +00:00
size_t index_len = 0 , webindex_len = 0 ;
2008-02-18 21:45:02 +00:00
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " |pp " , & index , & index_len , & webindex , & webindex_len ) = = FAILURE ) {
2008-01-19 04:26:22 +00:00
return ;
}
2015-07-02 17:29:06 +00:00
stub = phar_create_default_stub ( index , webindex , & error ) ;
2008-02-18 21:45:02 +00:00
2008-01-19 04:26:22 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2008-01-19 04:26:22 +00:00
efree ( error ) ;
return ;
}
2015-07-02 17:29:06 +00:00
RETURN_NEW_STR ( stub ) ;
2008-01-19 04:26:22 +00:00
}
2008-02-08 09:25:02 +00:00
/* }}} */
2008-01-19 04:26:22 +00:00
2007-05-28 18:07:49 +00:00
/* {{{ proto mixed Phar::mapPhar([string alias, [int dataoffset]])
2007-01-21 15:25:50 +00:00
* Reads the currently executed file ( a phar ) and registers its manifest */
PHP_METHOD ( Phar , mapPhar )
{
2008-05-07 05:38:31 +00:00
char * alias = NULL , * error ;
2014-08-27 17:25:28 +00:00
size_t alias_len = 0 ;
2014-08-25 17:24:55 +00:00
zend_long dataoffset = 0 ;
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " |s!l " , & alias , & alias_len , & dataoffset ) = = FAILURE ) {
2007-01-21 15:25:50 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
phar_request_initialize ( ) ;
2007-05-21 16:43:53 +00:00
2014-12-13 22:06:14 +00:00
RETVAL_BOOL ( phar_open_executed_filename ( alias , alias_len , & error ) = = SUCCESS ) ;
2008-08-01 13:45:06 +00:00
2007-01-29 06:02:19 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-01-29 06:02:19 +00:00
efree ( error ) ;
}
2007-01-21 15:25:50 +00:00
} /* }}} */
/* {{{ proto mixed Phar::loadPhar(string filename [, string alias])
* Loads any phar archive with an alias */
PHP_METHOD ( Phar , loadPhar )
{
2008-05-07 05:38:31 +00:00
char * fname , * alias = NULL , * error ;
2014-08-27 17:25:28 +00:00
size_t fname_len , alias_len = 0 ;
2007-01-21 15:25:50 +00:00
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " p|s! " , & fname , & fname_len , & alias , & alias_len ) = = FAILURE ) {
2007-01-21 15:25:50 +00:00
return ;
}
2007-05-21 16:43:53 +00:00
2014-12-13 22:06:14 +00:00
phar_request_initialize ( ) ;
2007-05-21 16:43:53 +00:00
2014-12-13 22:06:14 +00:00
RETVAL_BOOL ( phar_open_from_filename ( fname , fname_len , alias , alias_len , REPORT_ERRORS , NULL , & error ) = = SUCCESS ) ;
2008-08-01 13:45:06 +00:00
2007-01-29 06:02:19 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-01-29 06:02:19 +00:00
efree ( error ) ;
}
2007-01-21 15:25:50 +00:00
} /* }}} */
2007-02-03 13:42:10 +00:00
/* {{{ proto string Phar::apiVersion()
2007-01-21 15:25:50 +00:00
* Returns the api version */
PHP_METHOD ( Phar , apiVersion )
{
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2014-05-08 14:30:07 +00:00
RETURN_STRINGL ( PHP_PHAR_API_VERSION , sizeof ( PHP_PHAR_API_VERSION ) - 1 ) ;
2007-01-21 15:25:50 +00:00
}
/* }}}*/
2007-02-06 23:56:39 +00:00
/* {{{ proto bool Phar::canCompress([int method])
2007-01-21 15:25:50 +00:00
* Returns whether phar extension supports compression using zlib / bzip2 */
PHP_METHOD ( Phar , canCompress )
{
2014-08-25 17:24:55 +00:00
zend_long method = 0 ;
2007-02-06 23:56:39 +00:00
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " |l " , & method ) = = FAILURE ) {
2007-02-06 23:56:39 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
phar_request_initialize ( ) ;
2007-02-06 23:56:39 +00:00
switch ( method ) {
case PHAR_ENT_COMPRESSED_GZ :
2008-05-17 20:07:38 +00:00
if ( PHAR_G ( has_zlib ) ) {
2007-11-25 05:04:40 +00:00
RETURN_TRUE ;
} else {
RETURN_FALSE ;
}
2007-02-06 23:56:39 +00:00
case PHAR_ENT_COMPRESSED_BZ2 :
2008-05-17 20:07:38 +00:00
if ( PHAR_G ( has_bz2 ) ) {
2007-11-25 05:04:40 +00:00
RETURN_TRUE ;
} else {
RETURN_FALSE ;
}
2007-02-06 23:56:39 +00:00
default :
2008-05-17 20:07:38 +00:00
if ( PHAR_G ( has_zlib ) | | PHAR_G ( has_bz2 ) ) {
2008-01-17 03:48:29 +00:00
RETURN_TRUE ;
} else {
RETURN_FALSE ;
}
2007-02-06 23:56:39 +00:00
}
2007-01-21 15:25:50 +00:00
}
/* }}} */
2007-02-03 13:42:10 +00:00
/* {{{ proto bool Phar::canWrite()
2007-01-21 15:25:50 +00:00
* Returns whether phar extension supports writing and creating phars */
PHP_METHOD ( Phar , canWrite )
{
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2007-05-09 18:09:41 +00:00
RETURN_BOOL ( ! PHAR_G ( readonly ) ) ;
2007-01-21 15:25:50 +00:00
}
/* }}} */
2008-04-18 04:13:13 +00:00
/* {{{ proto bool Phar::isValidPharFilename(string filename[, bool executable = true])
2007-11-16 04:17:37 +00:00
* Returns whether the given filename is a valid phar filename */
2007-05-15 20:42:38 +00:00
PHP_METHOD ( Phar , isValidPharFilename )
{
2008-03-22 00:11:43 +00:00
char * fname ;
const char * ext_str ;
2014-08-27 17:25:28 +00:00
size_t fname_len ;
int ext_len , is_executable ;
2008-04-18 04:13:13 +00:00
zend_bool executable = 1 ;
2007-05-15 20:42:38 +00:00
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " p|b " , & fname , & fname_len , & executable ) = = FAILURE ) {
2007-05-15 20:42:38 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
is_executable = executable ;
2014-12-13 22:06:14 +00:00
RETVAL_BOOL ( phar_detect_phar_fname_ext ( fname , fname_len , & ext_str , & ext_len , is_executable , 2 , 1 ) = = SUCCESS ) ;
2007-05-15 20:42:38 +00:00
}
/* }}} */
2007-01-21 15:25:50 +00:00
/**
* from spl_directory
*/
2014-12-13 22:06:14 +00:00
static void phar_spl_foreign_dtor ( spl_filesystem_object * object ) /* { { { */
2007-01-21 15:25:50 +00:00
{
2008-08-01 13:45:06 +00:00
phar_archive_data * phar = ( phar_archive_data * ) object - > oth ;
if ( ! phar - > is_persistent ) {
2014-12-13 22:06:14 +00:00
phar_archive_delref ( phar ) ;
2008-08-01 13:45:06 +00:00
}
2007-01-26 19:58:22 +00:00
object - > oth = NULL ;
2007-01-21 15:25:50 +00:00
}
/* }}} */
/**
* from spl_directory
*/
2014-12-13 22:06:14 +00:00
static void phar_spl_foreign_clone ( spl_filesystem_object * src , spl_filesystem_object * dst ) /* { { { */
2007-01-21 15:25:50 +00:00
{
phar_archive_data * phar_data = ( phar_archive_data * ) dst - > oth ;
2008-08-01 13:45:06 +00:00
if ( ! phar_data - > is_persistent ) {
+ + ( phar_data - > refcount ) ;
}
2007-01-21 15:25:50 +00:00
}
/* }}} */
static spl_other_handler phar_spl_foreign_handler = {
2008-08-01 13:45:06 +00:00
phar_spl_foreign_dtor ,
phar_spl_foreign_clone
2007-01-21 15:25:50 +00:00
} ;
/* {{{ proto void Phar::__construct(string fname [, int flags [, string alias]])
* Construct a Phar archive object
2010-09-23 04:41:14 +00:00
*
* proto void PharData : : __construct ( string fname [ [ , int flags [ , string alias ] ] , int file format = Phar : : TAR ] )
2008-02-27 21:34:26 +00:00
* Construct a PharData archive object
2010-09-23 04:41:14 +00:00
*
* This function is used as the constructor for both the Phar and PharData
* classes , hence the two prototypes above .
2007-01-21 15:25:50 +00:00
*/
PHP_METHOD ( Phar , __construct )
{
2008-08-01 13:45:06 +00:00
char * fname , * alias = NULL , * error , * arch = NULL , * entry = NULL , * save_fname ;
2014-08-27 17:25:28 +00:00
size_t fname_len , alias_len = 0 ;
int arch_len , entry_len , is_data ;
2014-08-25 17:24:55 +00:00
zend_long flags = SPL_FILE_DIR_SKIPDOTS | SPL_FILE_DIR_UNIXPATHS ;
zend_long format = 0 ;
2007-01-21 15:25:50 +00:00
phar_archive_object * phar_obj ;
phar_archive_data * phar_data ;
zval * zobj = getThis ( ) , arg1 , arg2 ;
2014-05-08 17:18:59 +00:00
phar_obj = ( phar_archive_object * ) ( ( char * ) Z_OBJ_P ( zobj ) - Z_OBJ_P ( zobj ) - > handlers - > offset ) ;
2007-01-21 15:25:50 +00:00
2014-12-13 22:06:14 +00:00
is_data = instanceof_function ( Z_OBJCE_P ( zobj ) , phar_ce_data ) ;
2008-04-18 04:13:13 +00:00
2008-05-24 14:18:31 +00:00
if ( is_data ) {
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters_throw ( ZEND_NUM_ARGS ( ) , " p|ls!l " , & fname , & fname_len , & flags , & alias , & alias_len , & format ) = = FAILURE ) {
2008-05-24 14:18:31 +00:00
return ;
}
} else {
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters_throw ( ZEND_NUM_ARGS ( ) , " p|ls! " , & fname , & fname_len , & flags , & alias , & alias_len ) = = FAILURE ) {
2008-05-24 14:18:31 +00:00
return ;
}
}
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Cannot call constructor twice " ) ;
2008-05-24 14:18:31 +00:00
return ;
}
2008-09-11 03:29:42 +00:00
save_fname = fname ;
2014-12-13 22:06:14 +00:00
if ( SUCCESS = = phar_split_fname ( fname , ( int ) fname_len , & arch , & arch_len , & entry , & entry_len , ! is_data , 2 ) ) {
2008-02-27 21:34:26 +00:00
/* use arch (the basename for the archive) for fname instead of fname */
2008-02-11 06:46:44 +00:00
/* this allows support for RecursiveDirectoryIterator of subdirectories */
2008-02-11 07:33:20 +00:00
# ifdef PHP_WIN32
phar_unixify_path_separators ( arch , arch_len ) ;
# endif
2008-02-11 06:46:44 +00:00
fname = arch ;
fname_len = arch_len ;
2008-02-11 07:33:20 +00:00
# ifdef PHP_WIN32
} else {
arch = estrndup ( fname , fname_len ) ;
2008-04-25 04:34:03 +00:00
arch_len = fname_len ;
2008-02-11 07:33:20 +00:00
fname = arch ;
phar_unixify_path_separators ( arch , arch_len ) ;
# endif
2008-02-11 06:46:44 +00:00
}
2008-02-27 21:34:26 +00:00
2014-12-13 22:06:14 +00:00
if ( phar_open_or_create_filename ( fname , fname_len , alias , alias_len , is_data , REPORT_ERRORS , & phar_data , & error ) = = FAILURE ) {
2008-02-27 21:34:26 +00:00
2008-09-11 03:29:42 +00:00
if ( fname = = arch & & fname ! = save_fname ) {
2008-02-11 06:46:44 +00:00
efree ( arch ) ;
fname = save_fname ;
}
2008-08-01 13:45:06 +00:00
2008-03-03 08:41:15 +00:00
if ( entry ) {
2008-02-11 06:46:44 +00:00
efree ( entry ) ;
2008-03-03 08:41:15 +00:00
}
2008-08-01 13:45:06 +00:00
2008-03-03 08:41:15 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-03-03 08:41:15 +00:00
" %s " , error ) ;
2007-01-29 06:02:19 +00:00
efree ( error ) ;
2008-05-12 20:42:07 +00:00
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-05-12 20:42:07 +00:00
" Phar creation or opening failed " ) ;
2007-01-29 06:02:19 +00:00
}
2008-08-01 13:45:06 +00:00
2007-01-21 15:25:50 +00:00
return ;
}
2008-05-24 14:18:31 +00:00
if ( is_data & & phar_data - > is_tar & & phar_data - > is_brandnew & & format = = PHAR_FORMAT_ZIP ) {
phar_data - > is_zip = 1 ;
phar_data - > is_tar = 0 ;
}
2008-02-11 06:46:44 +00:00
if ( fname = = arch ) {
efree ( arch ) ;
fname = save_fname ;
}
2008-02-27 21:34:26 +00:00
2008-05-24 14:18:31 +00:00
if ( ( is_data & & ! phar_data - > is_data ) | | ( ! is_data & & phar_data - > is_data ) ) {
if ( is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-05-24 14:18:31 +00:00
" PharData class can only be used for non-executable tar and zip archives " ) ;
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-05-24 14:18:31 +00:00
" Phar class can only be used for executable tar and zip archives " ) ;
}
efree ( entry ) ;
return ;
}
2008-02-27 21:34:26 +00:00
is_data = phar_data - > is_data ;
2008-08-01 13:45:06 +00:00
if ( ! phar_data - > is_persistent ) {
+ + ( phar_data - > refcount ) ;
}
2014-05-08 17:18:59 +00:00
phar_obj - > archive = phar_data ;
2007-01-21 15:25:50 +00:00
phar_obj - > spl . oth_handler = & phar_spl_foreign_handler ;
2008-02-11 06:46:44 +00:00
if ( entry ) {
fname_len = spprintf ( & fname , 0 , " phar://%s%s " , phar_data - > fname , entry ) ;
efree ( entry ) ;
} else {
fname_len = spprintf ( & fname , 0 , " phar://%s " , phar_data - > fname ) ;
}
2008-02-27 21:34:26 +00:00
2014-05-08 14:30:07 +00:00
ZVAL_STRINGL ( & arg1 , fname , fname_len ) ;
2014-08-25 17:24:55 +00:00
ZVAL_LONG ( & arg2 , flags ) ;
2007-01-21 15:25:50 +00:00
2015-01-03 09:22:58 +00:00
zend_call_method_with_2_params ( zobj , Z_OBJCE_P ( zobj ) ,
2008-08-01 13:45:06 +00:00
& spl_ce_RecursiveDirectoryIterator - > constructor , " __construct " , NULL , & arg1 , & arg2 ) ;
2014-05-08 14:30:07 +00:00
zval_ptr_dtor ( & arg1 ) ;
2008-08-01 13:45:06 +00:00
if ( ! phar_data - > is_persistent ) {
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > is_data = is_data ;
2008-10-12 19:40:30 +00:00
} else if ( ! EG ( exception ) ) {
/* register this guy so we can modify if necessary */
2015-03-09 14:58:53 +00:00
zend_hash_str_add_ptr ( & PHAR_G ( phar_persist_map ) , ( const char * ) phar_obj - > archive , sizeof ( phar_obj - > archive ) , phar_obj ) ;
2007-01-21 15:25:50 +00:00
}
phar_obj - > spl . info_class = phar_ce_entry ;
efree ( fname ) ;
}
/* }}} */
2007-12-18 17:01:24 +00:00
/* {{{ proto array Phar::getSupportedSignatures()
* Return array of supported signature types
*/
PHP_METHOD ( Phar , getSupportedSignatures )
{
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2015-01-03 09:22:58 +00:00
2007-12-18 17:01:24 +00:00
array_init ( return_value ) ;
2014-04-15 11:40:40 +00:00
add_next_index_stringl ( return_value , " MD5 " , 3 ) ;
add_next_index_stringl ( return_value , " SHA-1 " , 5 ) ;
2008-10-09 00:51:27 +00:00
# ifdef PHAR_HASH_OK
2014-04-15 11:40:40 +00:00
add_next_index_stringl ( return_value , " SHA-256 " , 7 ) ;
add_next_index_stringl ( return_value , " SHA-512 " , 7 ) ;
2007-12-18 17:01:24 +00:00
# endif
2008-08-01 13:45:06 +00:00
# if PHAR_HAVE_OPENSSL
2014-04-15 11:40:40 +00:00
add_next_index_stringl ( return_value , " OpenSSL " , 7 ) ;
2008-08-01 13:45:06 +00:00
# else
2014-05-08 14:30:07 +00:00
if ( zend_hash_str_exists ( & module_registry , " openssl " , sizeof ( " openssl " ) - 1 ) ) {
2014-04-15 11:40:40 +00:00
add_next_index_stringl ( return_value , " OpenSSL " , 7 ) ;
2008-08-01 13:45:06 +00:00
}
# endif
2007-12-18 17:01:24 +00:00
}
/* }}} */
/* {{{ proto array Phar::getSupportedCompression()
* Return array of supported comparession algorithms
*/
PHP_METHOD ( Phar , getSupportedCompression )
{
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2015-01-03 09:22:58 +00:00
2007-12-18 17:01:24 +00:00
array_init ( return_value ) ;
2014-12-13 22:06:14 +00:00
phar_request_initialize ( ) ;
2008-08-01 13:45:06 +00:00
2008-05-17 20:07:38 +00:00
if ( PHAR_G ( has_zlib ) ) {
2014-04-15 11:40:40 +00:00
add_next_index_stringl ( return_value , " GZ " , 2 ) ;
2007-12-18 17:01:24 +00:00
}
2008-08-01 13:45:06 +00:00
2008-05-17 20:07:38 +00:00
if ( PHAR_G ( has_bz2 ) ) {
2014-04-15 11:40:40 +00:00
add_next_index_stringl ( return_value , " BZIP2 " , 5 ) ;
2007-12-18 17:01:24 +00:00
}
}
/* }}} */
2008-05-03 02:04:24 +00:00
/* {{{ proto array Phar::unlinkArchive(string archive)
* Completely remove a phar archive from memory and disk
*/
PHP_METHOD ( Phar , unlinkArchive )
{
2008-05-11 21:15:47 +00:00
char * fname , * error , * zname , * arch , * entry ;
2014-08-27 19:53:06 +00:00
size_t fname_len ;
int zname_len , arch_len , entry_len ;
2008-05-03 02:04:24 +00:00
phar_archive_data * phar ;
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " p " , & fname , & fname_len ) = = FAILURE ) {
2008-05-03 02:04:24 +00:00
RETURN_FALSE ;
}
if ( ! fname_len ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " Unknown phar archive \" \" " ) ;
2008-05-03 02:04:24 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_open_from_filename ( fname , fname_len , NULL , 0 , REPORT_ERRORS , & phar , & error ) ) {
2008-05-03 02:04:24 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " Unknown phar archive \" %s \" : %s " , fname , error ) ;
2008-05-03 02:04:24 +00:00
efree ( error ) ;
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " Unknown phar archive \" %s \" " , fname ) ;
2008-05-03 02:04:24 +00:00
}
return ;
}
2014-12-13 22:06:14 +00:00
zname = ( char * ) zend_get_executed_filename ( ) ;
2008-05-11 21:15:47 +00:00
zname_len = strlen ( zname ) ;
2014-12-13 22:06:14 +00:00
if ( zname_len > 7 & & ! memcmp ( zname , " phar:// " , 7 ) & & SUCCESS = = phar_split_fname ( zname , zname_len , & arch , & arch_len , & entry , & entry_len , 2 , 0 ) ) {
2016-06-21 21:40:50 +00:00
if ( ( size_t ) arch_len = = fname_len & & ! memcmp ( arch , fname , arch_len ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar archive \" %s \" cannot be unlinked from within itself " , fname ) ;
2008-05-11 21:15:47 +00:00
efree ( arch ) ;
efree ( entry ) ;
return ;
}
efree ( arch ) ;
efree ( entry ) ;
}
2008-08-01 13:45:06 +00:00
if ( phar - > is_persistent ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar archive \" %s \" is in phar.cache_list, cannot unlinkArchive() " , fname ) ;
2008-08-01 13:45:06 +00:00
return ;
}
2008-05-03 02:04:24 +00:00
if ( phar - > refcount ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar archive \" %s \" has open file handles or objects. fclose() all file handles, and unset() all objects prior to calling unlinkArchive() " , fname ) ;
2008-05-03 02:04:24 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2008-05-03 02:04:24 +00:00
fname = estrndup ( phar - > fname , phar - > fname_len ) ;
2008-08-01 13:45:06 +00:00
/* invalidate phar cache */
PHAR_G ( last_phar ) = NULL ;
PHAR_G ( last_phar_name ) = PHAR_G ( last_alias ) = NULL ;
2014-12-13 22:06:14 +00:00
phar_archive_delref ( phar ) ;
2008-05-03 02:04:24 +00:00
unlink ( fname ) ;
efree ( fname ) ;
RETURN_TRUE ;
}
/* }}} */
2007-01-21 15:25:50 +00:00
# define PHAR_ARCHIVE_OBJECT() \
2014-05-08 17:18:59 +00:00
zval * zobj = getThis ( ) ; \
phar_archive_object * phar_obj = ( phar_archive_object * ) ( ( char * ) Z_OBJ_P ( zobj ) - Z_OBJ_P ( zobj ) - > handlers - > offset ) ; \
if ( ! phar_obj - > archive ) { \
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , \
2007-01-23 23:31:14 +00:00
" Cannot call method on an uninitialized Phar object " ) ; \
2007-01-21 15:25:50 +00:00
return ; \
}
2008-10-12 19:40:30 +00:00
/* {{{ proto void Phar::__destruct()
* if persistent , remove from the cache
*/
PHP_METHOD ( Phar , __destruct )
{
2014-05-08 17:18:59 +00:00
zval * zobj = getThis ( ) ;
phar_archive_object * phar_obj = ( phar_archive_object * ) ( ( char * ) Z_OBJ_P ( zobj ) - Z_OBJ_P ( zobj ) - > handlers - > offset ) ;
2008-10-12 19:40:30 +00:00
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive & & phar_obj - > archive - > is_persistent ) {
2015-03-09 14:58:53 +00:00
zend_hash_str_del ( & PHAR_G ( phar_persist_map ) , ( const char * ) phar_obj - > archive , sizeof ( phar_obj - > archive ) ) ;
2008-10-12 19:40:30 +00:00
}
}
/* }}} */
2008-08-01 13:45:06 +00:00
struct _phar_t {
phar_archive_object * p ;
zend_class_entry * c ;
char * b ;
zval * ret ;
php_stream * fp ;
2014-09-14 09:36:11 +00:00
uint l ;
int count ;
2008-08-01 13:45:06 +00:00
} ;
2014-12-13 22:06:14 +00:00
static int phar_build ( zend_object_iterator * iter , void * puser ) /* { { { */
2007-12-13 00:54:15 +00:00
{
2014-05-08 14:30:07 +00:00
zval * value ;
2008-08-31 20:54:28 +00:00
zend_bool close_fp = 1 ;
2008-08-01 13:45:06 +00:00
struct _phar_t * p_obj = ( struct _phar_t * ) puser ;
2007-12-13 05:22:06 +00:00
uint str_key_len , base_len = p_obj - > l , fname_len ;
2007-12-13 00:54:15 +00:00
phar_entry_data * data ;
php_stream * fp ;
2009-04-29 03:24:09 +00:00
size_t contents_len ;
2015-03-03 23:05:28 +00:00
char * fname , * error = NULL , * base = p_obj - > b , * save = NULL , * temp = NULL ;
zend_string * opened ;
2008-05-14 21:27:31 +00:00
char * str_key ;
2007-12-13 00:54:15 +00:00
zend_class_entry * ce = p_obj - > c ;
phar_archive_object * phar_obj = p_obj - > p ;
2014-12-13 22:06:14 +00:00
value = iter - > funcs - > get_current_data ( iter ) ;
2008-08-01 13:45:06 +00:00
2007-12-13 00:54:15 +00:00
if ( EG ( exception ) ) {
return ZEND_HASH_APPLY_STOP ;
}
2008-08-01 13:45:06 +00:00
2007-12-13 00:54:15 +00:00
if ( ! value ) {
/* failure in get_current_data */
2016-06-21 13:00:37 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " Iterator %s returned no value " , ZSTR_VAL ( ce - > name ) ) ;
2008-01-14 05:12:46 +00:00
return ZEND_HASH_APPLY_STOP ;
2007-12-13 00:54:15 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 14:30:07 +00:00
switch ( Z_TYPE_P ( value ) ) {
2008-05-19 19:05:42 +00:00
case IS_STRING :
2007-12-13 05:22:06 +00:00
break ;
2008-05-19 19:05:42 +00:00
case IS_RESOURCE :
2007-12-14 19:45:22 +00:00
php_stream_from_zval_no_verify ( fp , value ) ;
2008-08-01 13:45:06 +00:00
2007-12-14 19:45:22 +00:00
if ( ! fp ) {
2016-06-21 13:00:37 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Iterator %s returned an invalid stream handle " , ZSTR_VAL ( ce - > name ) ) ;
2007-12-14 19:45:22 +00:00
return ZEND_HASH_APPLY_STOP ;
}
2008-08-01 13:45:06 +00:00
2007-12-14 19:45:22 +00:00
if ( iter - > funcs - > get_current_key ) {
2013-02-16 18:13:36 +00:00
zval key ;
2014-12-13 22:06:14 +00:00
iter - > funcs - > get_current_key ( iter , & key ) ;
2008-05-14 21:27:31 +00:00
2007-12-14 19:45:22 +00:00
if ( EG ( exception ) ) {
return ZEND_HASH_APPLY_STOP ;
}
2008-05-14 21:27:31 +00:00
2013-02-16 18:13:36 +00:00
if ( Z_TYPE ( key ) ! = IS_STRING ) {
zval_dtor ( & key ) ;
2016-06-21 13:00:37 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " Iterator %s returned an invalid key (must return a string) " , ZSTR_VAL ( ce - > name ) ) ;
2007-12-14 19:45:22 +00:00
return ZEND_HASH_APPLY_STOP ;
}
2008-08-01 13:45:06 +00:00
2014-08-25 17:24:55 +00:00
str_key_len = Z_STRLEN ( key ) ;
2013-02-16 18:13:36 +00:00
str_key = estrndup ( Z_STRVAL ( key ) , str_key_len ) ;
2008-08-01 13:45:06 +00:00
2008-05-14 21:27:31 +00:00
save = str_key ;
2013-02-16 18:13:36 +00:00
zval_dtor ( & key ) ;
2007-12-14 19:45:22 +00:00
} else {
2016-06-21 13:00:37 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " Iterator %s returned an invalid key (must return a string) " , ZSTR_VAL ( ce - > name ) ) ;
2007-12-14 19:45:22 +00:00
return ZEND_HASH_APPLY_STOP ;
}
2008-08-01 13:45:06 +00:00
2007-12-14 19:45:22 +00:00
close_fp = 0 ;
2015-03-03 23:05:28 +00:00
opened = zend_string_init ( " [stream] " , sizeof ( " [stream] " ) - 1 , 0 ) ;
2007-12-14 19:45:22 +00:00
goto after_open_fp ;
2008-05-19 19:05:42 +00:00
case IS_OBJECT :
2014-12-13 22:06:14 +00:00
if ( instanceof_function ( Z_OBJCE_P ( value ) , spl_ce_SplFileInfo ) ) {
2007-12-14 04:10:14 +00:00
char * test = NULL ;
2007-12-13 05:22:06 +00:00
zval dummy ;
2014-05-08 17:18:59 +00:00
spl_filesystem_object * intern = ( spl_filesystem_object * ) ( ( char * ) Z_OBJ_P ( value ) - Z_OBJ_P ( value ) - > handlers - > offset ) ;
2007-12-13 05:22:06 +00:00
if ( ! base_len ) {
2016-06-21 13:00:37 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Iterator %s returns an SplFileInfo object, so base directory must be specified " , ZSTR_VAL ( ce - > name ) ) ;
2007-12-13 05:22:06 +00:00
return ZEND_HASH_APPLY_STOP ;
}
2008-08-01 13:45:06 +00:00
2007-12-13 05:22:06 +00:00
switch ( intern - > type ) {
case SPL_FS_DIR :
2014-12-13 22:06:14 +00:00
test = spl_filesystem_object_get_path ( intern , NULL ) ;
2008-01-30 20:31:09 +00:00
fname_len = spprintf ( & fname , 0 , " %s%c%s " , test , DEFAULT_SLASH , intern - > u . dir . entry . d_name ) ;
2014-12-13 22:06:14 +00:00
php_stat ( fname , fname_len , FS_IS_DIR , & dummy ) ;
2008-08-01 13:45:06 +00:00
2014-05-08 14:30:07 +00:00
if ( Z_TYPE ( dummy ) = = IS_TRUE ) {
2007-12-13 05:22:06 +00:00
/* ignore directories */
efree ( fname ) ;
return ZEND_HASH_APPLY_KEEP ;
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
test = expand_filepath ( fname , NULL ) ;
2011-08-09 14:11:56 +00:00
efree ( fname ) ;
2008-08-01 13:45:06 +00:00
2007-12-13 05:22:06 +00:00
if ( test ) {
fname = test ;
fname_len = strlen ( fname ) ;
2011-08-09 14:11:56 +00:00
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " Could not resolve file path " ) ;
2011-08-09 14:11:56 +00:00
return ZEND_HASH_APPLY_STOP ;
2007-12-13 05:22:06 +00:00
}
2008-08-01 13:45:06 +00:00
2007-12-13 05:22:06 +00:00
save = fname ;
goto phar_spl_fileinfo ;
case SPL_FS_INFO :
case SPL_FS_FILE :
2014-12-13 22:06:14 +00:00
fname = expand_filepath ( intern - > file_name , NULL ) ;
2011-08-09 14:11:56 +00:00
if ( ! fname ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " Could not resolve file path " ) ;
2011-08-09 14:11:56 +00:00
return ZEND_HASH_APPLY_STOP ;
}
2008-04-25 21:37:41 +00:00
fname_len = strlen ( fname ) ;
2008-04-26 01:52:22 +00:00
save = fname ;
2008-04-25 21:37:41 +00:00
goto phar_spl_fileinfo ;
2007-12-13 05:22:06 +00:00
}
}
/* fall-through */
2008-08-01 13:45:06 +00:00
default :
2016-06-21 13:00:37 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " Iterator %s returned an invalid value (must return a string) " , ZSTR_VAL ( ce - > name ) ) ;
2007-12-13 05:22:06 +00:00
return ZEND_HASH_APPLY_STOP ;
2007-12-13 00:54:15 +00:00
}
2014-05-08 14:30:07 +00:00
fname = Z_STRVAL_P ( value ) ;
2014-08-25 17:24:55 +00:00
fname_len = Z_STRLEN_P ( value ) ;
2007-12-13 01:16:56 +00:00
2007-12-13 05:22:06 +00:00
phar_spl_fileinfo :
2007-12-13 00:54:15 +00:00
if ( base_len ) {
2014-12-13 22:06:14 +00:00
temp = expand_filepath ( base , NULL ) ;
2011-08-09 14:11:56 +00:00
if ( ! temp ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " Could not resolve file path " ) ;
2011-08-09 14:11:56 +00:00
if ( save ) {
efree ( save ) ;
}
return ZEND_HASH_APPLY_STOP ;
}
2015-01-03 09:22:58 +00:00
2008-04-26 01:52:22 +00:00
base = temp ;
2008-04-25 21:37:41 +00:00
base_len = strlen ( base ) ;
2008-08-01 13:45:06 +00:00
2007-12-13 00:54:15 +00:00
if ( strstr ( fname , base ) ) {
2007-12-13 05:22:06 +00:00
str_key_len = fname_len - base_len ;
2008-08-01 13:45:06 +00:00
2007-12-13 00:54:15 +00:00
if ( str_key_len < = 0 ) {
2007-12-13 05:22:06 +00:00
if ( save ) {
efree ( save ) ;
2008-04-26 01:52:22 +00:00
efree ( temp ) ;
2007-12-13 05:22:06 +00:00
}
2007-12-13 00:54:15 +00:00
return ZEND_HASH_APPLY_KEEP ;
}
2008-08-01 13:45:06 +00:00
2008-05-14 21:27:31 +00:00
str_key = fname + base_len ;
2008-08-01 13:45:06 +00:00
2008-05-14 21:27:31 +00:00
if ( * str_key = = ' / ' | | * str_key = = ' \\ ' ) {
str_key + + ;
2008-04-26 17:09:58 +00:00
str_key_len - - ;
}
2008-08-01 13:45:06 +00:00
2007-12-13 00:54:15 +00:00
} else {
2016-06-21 13:00:37 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " Iterator %s returned a path \" %s \" that is not in the base directory \" %s \" " , ZSTR_VAL ( ce - > name ) , fname , base ) ;
2008-08-01 13:45:06 +00:00
2007-12-13 05:22:06 +00:00
if ( save ) {
efree ( save ) ;
2008-04-26 01:52:22 +00:00
efree ( temp ) ;
2007-12-13 05:22:06 +00:00
}
2008-08-01 13:45:06 +00:00
2007-12-13 00:54:15 +00:00
return ZEND_HASH_APPLY_STOP ;
}
2007-12-13 05:22:06 +00:00
} else {
2007-12-13 01:16:56 +00:00
if ( iter - > funcs - > get_current_key ) {
2013-02-16 18:13:36 +00:00
zval key ;
2014-12-13 22:06:14 +00:00
iter - > funcs - > get_current_key ( iter , & key ) ;
2008-05-14 21:27:31 +00:00
2007-12-13 01:16:56 +00:00
if ( EG ( exception ) ) {
return ZEND_HASH_APPLY_STOP ;
}
2008-05-14 21:27:31 +00:00
2013-02-16 18:13:36 +00:00
if ( Z_TYPE ( key ) ! = IS_STRING ) {
zval_dtor ( & key ) ;
2016-06-21 13:00:37 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " Iterator %s returned an invalid key (must return a string) " , ZSTR_VAL ( ce - > name ) ) ;
2007-12-13 01:16:56 +00:00
return ZEND_HASH_APPLY_STOP ;
}
2008-08-01 13:45:06 +00:00
2014-08-25 17:24:55 +00:00
str_key_len = Z_STRLEN ( key ) ;
2013-02-16 18:13:36 +00:00
str_key = estrndup ( Z_STRVAL ( key ) , str_key_len ) ;
2008-08-01 13:45:06 +00:00
2008-05-14 21:27:31 +00:00
save = str_key ;
2013-02-16 18:13:36 +00:00
zval_dtor ( & key ) ;
2007-12-13 01:16:56 +00:00
} else {
2016-06-21 13:00:37 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " Iterator %s returned an invalid key (must return a string) " , ZSTR_VAL ( ce - > name ) ) ;
2007-12-13 01:16:56 +00:00
return ZEND_HASH_APPLY_STOP ;
}
2007-12-13 00:54:15 +00:00
}
2014-12-13 22:06:14 +00:00
if ( php_check_open_basedir ( fname ) ) {
2016-06-21 13:00:37 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " Iterator %s returned a path \" %s \" that open_basedir prevents opening " , ZSTR_VAL ( ce - > name ) , fname ) ;
2008-08-01 13:45:06 +00:00
2007-12-13 05:22:06 +00:00
if ( save ) {
efree ( save ) ;
}
2008-08-01 13:45:06 +00:00
2008-04-26 01:52:22 +00:00
if ( temp ) {
efree ( temp ) ;
}
2008-08-01 13:45:06 +00:00
2007-12-13 00:54:15 +00:00
return ZEND_HASH_APPLY_STOP ;
}
/* try to open source file, then create internal phar file and copy contents */
fp = php_stream_open_wrapper ( fname , " rb " , STREAM_MUST_SEEK | 0 , & opened ) ;
2008-08-01 13:45:06 +00:00
2007-12-13 00:54:15 +00:00
if ( ! fp ) {
2016-06-21 13:00:37 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " Iterator %s returned a file that could not be opened \" %s \" " , ZSTR_VAL ( ce - > name ) , fname ) ;
2008-08-01 13:45:06 +00:00
2007-12-13 05:22:06 +00:00
if ( save ) {
efree ( save ) ;
}
2008-08-01 13:45:06 +00:00
2008-04-26 01:52:22 +00:00
if ( temp ) {
efree ( temp ) ;
}
2008-08-01 13:45:06 +00:00
2007-12-13 00:54:15 +00:00
return ZEND_HASH_APPLY_STOP ;
}
2007-12-14 19:45:22 +00:00
after_open_fp :
2008-05-15 16:09:21 +00:00
if ( str_key_len > = sizeof ( " .phar " ) - 1 & & ! memcmp ( str_key , " .phar " , sizeof ( " .phar " ) - 1 ) ) {
/* silently skip any files that would be added to the magic .phar directory */
if ( save ) {
efree ( save ) ;
}
2008-08-01 13:45:06 +00:00
2008-05-15 16:09:21 +00:00
if ( temp ) {
efree ( temp ) ;
}
2008-08-01 13:45:06 +00:00
2008-05-15 16:09:21 +00:00
if ( opened ) {
2015-03-03 23:05:28 +00:00
zend_string_release ( opened ) ;
2008-05-15 16:09:21 +00:00
}
2008-08-01 13:45:06 +00:00
2008-05-15 16:09:21 +00:00
if ( close_fp ) {
php_stream_close ( fp ) ;
}
2008-08-01 13:45:06 +00:00
2008-05-15 16:09:21 +00:00
return ZEND_HASH_APPLY_KEEP ;
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( ! ( data = phar_get_or_create_entry_data ( phar_obj - > archive - > fname , phar_obj - > archive - > fname_len , str_key , str_key_len , " w+b " , 0 , & error , 1 ) ) ) {
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Entry %s cannot be created: %s " , str_key , error ) ;
2007-12-13 00:54:15 +00:00
efree ( error ) ;
2008-08-01 13:45:06 +00:00
2007-12-13 05:22:06 +00:00
if ( save ) {
efree ( save ) ;
}
2008-08-01 13:45:06 +00:00
2008-05-15 16:09:21 +00:00
if ( opened ) {
2015-03-03 23:05:28 +00:00
zend_string_release ( opened ) ;
2008-05-15 16:09:21 +00:00
}
2008-08-01 13:45:06 +00:00
2008-04-26 01:52:22 +00:00
if ( temp ) {
efree ( temp ) ;
}
2008-08-01 13:45:06 +00:00
2007-12-14 19:45:22 +00:00
if ( close_fp ) {
php_stream_close ( fp ) ;
}
2008-08-01 13:45:06 +00:00
2007-12-13 00:54:15 +00:00
return ZEND_HASH_APPLY_STOP ;
2008-08-01 13:45:06 +00:00
2007-12-13 00:54:15 +00:00
} else {
if ( error ) {
efree ( error ) ;
}
2008-08-01 13:45:06 +00:00
/* convert to PHAR_UFP */
if ( data - > internal_file - > fp_type = = PHAR_MOD ) {
php_stream_close ( data - > internal_file - > fp ) ;
}
data - > internal_file - > fp = NULL ;
data - > internal_file - > fp_type = PHAR_UFP ;
data - > internal_file - > offset_abs = data - > internal_file - > offset = php_stream_tell ( p_obj - > fp ) ;
data - > fp = NULL ;
2013-09-13 19:40:32 +00:00
php_stream_copy_to_stream_ex ( fp , p_obj - > fp , PHP_STREAM_COPY_ALL , & contents_len ) ;
2008-08-01 13:45:06 +00:00
data - > internal_file - > uncompressed_filesize = data - > internal_file - > compressed_filesize =
php_stream_tell ( p_obj - > fp ) - data - > internal_file - > offset ;
2007-12-13 00:54:15 +00:00
}
2008-08-01 13:45:06 +00:00
2007-12-14 19:45:22 +00:00
if ( close_fp ) {
php_stream_close ( fp ) ;
}
2007-12-13 00:54:15 +00:00
2015-03-03 23:05:28 +00:00
add_assoc_str ( p_obj - > ret , str_key , opened ) ;
2008-04-26 01:52:22 +00:00
2007-12-13 05:22:06 +00:00
if ( save ) {
efree ( save ) ;
}
2008-08-01 13:45:06 +00:00
2008-04-26 01:52:22 +00:00
if ( temp ) {
efree ( temp ) ;
}
2007-12-13 00:54:15 +00:00
data - > internal_file - > compressed_filesize = data - > internal_file - > uncompressed_filesize = contents_len ;
2014-12-13 22:06:14 +00:00
phar_entry_delref ( data ) ;
2008-08-01 13:45:06 +00:00
2007-12-13 00:54:15 +00:00
return ZEND_HASH_APPLY_KEEP ;
}
2008-02-08 09:25:02 +00:00
/* }}} */
2007-12-13 00:54:15 +00:00
2008-04-26 13:22:38 +00:00
/* {{{ proto array Phar::buildFromDirectory(string base_dir[, string regex])
2008-04-25 21:37:41 +00:00
* Construct a phar archive from an existing directory , recursively .
* Optional second parameter is a regular expression for filtering directory contents .
2015-01-03 09:22:58 +00:00
*
2008-04-25 21:37:41 +00:00
* Return value is an array mapping phar index to actual files added .
*/
PHP_METHOD ( Phar , buildFromDirectory )
{
2008-04-27 10:14:00 +00:00
char * dir , * error , * regex = NULL ;
2014-08-27 17:25:28 +00:00
size_t dir_len , regex_len = 0 ;
2008-04-25 21:37:41 +00:00
zend_bool apply_reg = 0 ;
2014-05-08 14:30:07 +00:00
zval arg , arg2 , iter , iteriter , regexiter ;
2008-08-01 13:45:06 +00:00
struct _phar_t pass ;
2008-04-25 21:37:41 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-04-25 21:37:41 +00:00
" Cannot write to archive - write operations restricted by INI setting " ) ;
return ;
}
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " p|s " , & dir , & dir_len , & regex , & regex_len ) = = FAILURE ) {
2008-04-25 21:37:41 +00:00
RETURN_FALSE ;
}
2014-05-08 14:30:07 +00:00
if ( SUCCESS ! = object_init_ex ( & iter , spl_ce_RecursiveDirectoryIterator ) ) {
2008-05-06 21:23:48 +00:00
zval_ptr_dtor ( & iter ) ;
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Unable to instantiate directory iterator for %s " , phar_obj - > archive - > fname ) ;
2008-04-25 21:37:41 +00:00
RETURN_FALSE ;
}
2014-05-08 14:30:07 +00:00
ZVAL_STRINGL ( & arg , dir , dir_len ) ;
2014-08-25 17:24:55 +00:00
ZVAL_LONG ( & arg2 , SPL_FILE_DIR_SKIPDOTS | SPL_FILE_DIR_UNIXPATHS ) ;
2008-04-25 21:37:41 +00:00
2015-01-03 09:22:58 +00:00
zend_call_method_with_2_params ( & iter , spl_ce_RecursiveDirectoryIterator ,
2008-08-01 13:45:06 +00:00
& spl_ce_RecursiveDirectoryIterator - > constructor , " __construct " , NULL , & arg , & arg2 ) ;
2008-04-25 21:37:41 +00:00
2014-05-08 14:30:07 +00:00
zval_ptr_dtor ( & arg ) ;
2008-05-06 21:23:48 +00:00
if ( EG ( exception ) ) {
zval_ptr_dtor ( & iter ) ;
RETURN_FALSE ;
}
2014-05-08 14:30:07 +00:00
if ( SUCCESS ! = object_init_ex ( & iteriter , spl_ce_RecursiveIteratorIterator ) ) {
2008-05-06 21:23:48 +00:00
zval_ptr_dtor ( & iter ) ;
2008-08-01 13:45:06 +00:00
zval_ptr_dtor ( & iteriter ) ;
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Unable to instantiate directory iterator for %s " , phar_obj - > archive - > fname ) ;
2008-04-25 21:37:41 +00:00
RETURN_FALSE ;
}
2015-01-03 09:22:58 +00:00
zend_call_method_with_1_params ( & iteriter , spl_ce_RecursiveIteratorIterator ,
2014-05-08 14:30:07 +00:00
& spl_ce_RecursiveIteratorIterator - > constructor , " __construct " , NULL , & iter ) ;
2008-04-25 21:37:41 +00:00
2008-05-06 21:23:48 +00:00
if ( EG ( exception ) ) {
zval_ptr_dtor ( & iter ) ;
zval_ptr_dtor ( & iteriter ) ;
RETURN_FALSE ;
}
2008-04-25 21:37:41 +00:00
zval_ptr_dtor ( & iter ) ;
if ( regex_len > 0 ) {
apply_reg = 1 ;
2014-05-08 14:30:07 +00:00
if ( SUCCESS ! = object_init_ex ( & regexiter , spl_ce_RegexIterator ) ) {
2008-08-01 13:45:06 +00:00
zval_ptr_dtor ( & iteriter ) ;
2014-05-08 14:30:07 +00:00
zval_dtor ( & regexiter ) ;
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Unable to instantiate regex iterator for %s " , phar_obj - > archive - > fname ) ;
2008-04-25 21:37:41 +00:00
RETURN_FALSE ;
}
2014-05-08 14:30:07 +00:00
ZVAL_STRINGL ( & arg2 , regex , regex_len ) ;
2008-04-25 21:37:41 +00:00
2015-01-03 09:22:58 +00:00
zend_call_method_with_2_params ( & regexiter , spl_ce_RegexIterator ,
2014-05-08 14:30:07 +00:00
& spl_ce_RegexIterator - > constructor , " __construct " , NULL , & iteriter , & arg2 ) ;
zval_ptr_dtor ( & arg2 ) ;
2008-04-25 21:37:41 +00:00
}
array_init ( return_value ) ;
2014-05-08 14:30:07 +00:00
pass . c = apply_reg ? Z_OBJCE ( regexiter ) : Z_OBJCE ( iteriter ) ;
2008-04-25 21:37:41 +00:00
pass . p = phar_obj ;
pass . b = dir ;
pass . l = dir_len ;
2008-08-01 13:45:06 +00:00
pass . count = 0 ;
2008-04-25 21:37:41 +00:00
pass . ret = return_value ;
2008-08-01 13:45:06 +00:00
pass . fp = php_stream_fopen_tmpfile ( ) ;
2013-05-14 14:45:11 +00:00
if ( pass . fp = = NULL ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" unable to create temporary file " , phar_obj - > archive - > fname ) ;
2013-05-14 14:45:11 +00:00
return ;
}
2008-04-25 21:37:41 +00:00
2014-12-13 22:06:14 +00:00
if ( phar_obj - > archive - > is_persistent & & FAILURE = = phar_copy_on_write ( & ( phar_obj - > archive ) ) ) {
2008-10-12 06:53:52 +00:00
zval_ptr_dtor ( & iteriter ) ;
if ( apply_reg ) {
zval_ptr_dtor ( & regexiter ) ;
}
php_stream_close ( pass . fp ) ;
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar_obj - > archive - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
if ( SUCCESS = = spl_iterator_apply ( ( apply_reg ? & regexiter : & iteriter ) , ( spl_iterator_apply_func_t ) phar_build , ( void * ) & pass ) ) {
2008-04-25 21:37:41 +00:00
zval_ptr_dtor ( & iteriter ) ;
2008-08-01 13:45:06 +00:00
2008-04-26 02:04:09 +00:00
if ( apply_reg ) {
zval_ptr_dtor ( & regexiter ) ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > ufp = pass . fp ;
2014-12-13 22:06:14 +00:00
phar_flush ( phar_obj - > archive , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2008-04-25 21:37:41 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2008-04-25 21:37:41 +00:00
efree ( error ) ;
}
2008-08-01 13:45:06 +00:00
} else {
zval_ptr_dtor ( & iteriter ) ;
2008-10-12 06:53:52 +00:00
if ( apply_reg ) {
zval_ptr_dtor ( & regexiter ) ;
}
2008-08-01 13:45:06 +00:00
php_stream_close ( pass . fp ) ;
2008-04-25 21:37:41 +00:00
}
}
2008-08-01 13:45:06 +00:00
/* }}} */
2008-04-25 21:37:41 +00:00
2007-12-13 00:54:15 +00:00
/* {{{ proto array Phar::buildFromIterator(Iterator iter[, string base_directory])
2007-12-12 18:01:40 +00:00
* Construct a phar archive from an iterator . The iterator must return a series of strings
* that are full paths to files that should be added to the phar . The iterator key should
* be the path that the file will have within the phar archive .
*
* If base directory is specified , then the key will be ignored , and instead the portion of
* the current value minus the base directory will be used
2007-12-13 00:54:15 +00:00
*
* Returned is an array mapping phar index to actual file added
2007-12-12 18:01:40 +00:00
*/
PHP_METHOD ( Phar , buildFromIterator )
{
2007-12-13 00:54:15 +00:00
zval * obj ;
char * error ;
2014-08-27 13:31:48 +00:00
size_t base_len = 0 ;
2008-04-27 10:14:00 +00:00
char * base = NULL ;
2008-08-01 13:45:06 +00:00
struct _phar_t pass ;
2007-12-12 18:01:40 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2007-12-12 18:01:40 +00:00
" Cannot write out phar archive, phar is read-only " ) ;
return ;
}
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " O|s " , & obj , zend_ce_traversable , & base , & base_len ) = = FAILURE ) {
2007-12-12 18:01:40 +00:00
RETURN_FALSE ;
}
2014-12-13 22:06:14 +00:00
if ( phar_obj - > archive - > is_persistent & & FAILURE = = phar_copy_on_write ( & ( phar_obj - > archive ) ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar_obj - > archive - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
2007-12-13 00:54:15 +00:00
array_init ( return_value ) ;
2007-12-12 18:01:40 +00:00
2007-12-13 00:54:15 +00:00
pass . c = Z_OBJCE_P ( obj ) ;
pass . p = phar_obj ;
pass . b = base ;
pass . l = base_len ;
pass . ret = return_value ;
2008-08-01 13:45:06 +00:00
pass . count = 0 ;
pass . fp = php_stream_fopen_tmpfile ( ) ;
2013-05-14 14:45:11 +00:00
if ( pass . fp = = NULL ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" : unable to create temporary file " , phar_obj - > archive - > fname ) ;
2013-05-14 14:45:11 +00:00
return ;
}
2007-12-12 18:01:40 +00:00
2014-12-13 22:06:14 +00:00
if ( SUCCESS = = spl_iterator_apply ( obj , ( spl_iterator_apply_func_t ) phar_build , ( void * ) & pass ) ) {
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > ufp = pass . fp ;
2014-12-13 22:06:14 +00:00
phar_flush ( phar_obj - > archive , 0 , 0 , 0 , & error ) ;
2007-12-13 00:54:15 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-12-13 00:54:15 +00:00
efree ( error ) ;
2007-12-12 18:01:40 +00:00
}
2008-08-01 13:45:06 +00:00
} else {
php_stream_close ( pass . fp ) ;
2007-12-12 18:01:40 +00:00
}
}
/* }}} */
2007-01-21 15:25:50 +00:00
/* {{{ proto int Phar::count()
* Returns the number of entries in the Phar archive
*/
PHP_METHOD ( Phar , count )
{
2014-04-13 22:08:36 +00:00
/* mode can be ignored, maximum depth is 1 */
2014-08-25 17:24:55 +00:00
zend_long mode ;
2007-01-21 15:25:50 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " |l " , & mode ) = = FAILURE ) {
2014-04-13 22:08:36 +00:00
RETURN_FALSE ;
2011-06-18 22:48:31 +00:00
}
2008-08-01 13:45:06 +00:00
2014-08-25 17:24:55 +00:00
RETURN_LONG ( zend_hash_num_elements ( & phar_obj - > archive - > manifest ) ) ;
2007-01-21 15:25:50 +00:00
}
/* }}} */
2008-05-02 05:05:56 +00:00
/* {{{ proto bool Phar::isFileFormat(int format)
* Returns true if the phar archive is based on the tar / zip / phar file format depending
* on whether Phar : : TAR , Phar : : ZIP or Phar : : PHAR was passed in
2008-01-07 05:41:09 +00:00
*/
2008-05-02 05:05:56 +00:00
PHP_METHOD ( Phar , isFileFormat )
2008-01-07 05:41:09 +00:00
{
2014-08-25 17:24:55 +00:00
zend_long type ;
2008-01-07 05:41:09 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " l " , & type ) = = FAILURE ) {
2008-05-02 05:05:56 +00:00
RETURN_FALSE ;
}
2008-08-01 13:45:06 +00:00
2008-05-02 05:05:56 +00:00
switch ( type ) {
case PHAR_FORMAT_TAR :
2014-05-08 17:18:59 +00:00
RETURN_BOOL ( phar_obj - > archive - > is_tar ) ;
2008-05-02 05:05:56 +00:00
case PHAR_FORMAT_ZIP :
2014-05-08 17:18:59 +00:00
RETURN_BOOL ( phar_obj - > archive - > is_zip ) ;
2008-05-02 05:05:56 +00:00
case PHAR_FORMAT_PHAR :
2014-05-08 17:18:59 +00:00
RETURN_BOOL ( ! phar_obj - > archive - > is_tar & & ! phar_obj - > archive - > is_zip ) ;
2008-05-02 05:05:56 +00:00
default :
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " Unknown file format specified " ) ;
2008-05-02 05:05:56 +00:00
}
2008-01-07 05:41:09 +00:00
}
/* }}} */
2014-12-13 22:06:14 +00:00
static int phar_copy_file_contents ( phar_entry_info * entry , php_stream * fp ) /* { { { */
2008-01-16 07:24:39 +00:00
{
2008-01-28 08:52:08 +00:00
char * error ;
2014-08-25 18:22:49 +00:00
zend_off_t offset ;
2008-04-21 06:17:51 +00:00
phar_entry_info * link ;
2008-01-28 08:52:08 +00:00
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_open_entry_fp ( entry , & error , 1 ) ) {
2008-01-28 08:52:08 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-01-28 08:52:08 +00:00
" Cannot convert phar archive \" %s \" , unable to open entry \" %s \" contents: %s " , entry - > phar - > fname , entry - > filename , error ) ;
efree ( error ) ;
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-01-28 08:52:08 +00:00
" Cannot convert phar archive \" %s \" , unable to open entry \" %s \" contents " , entry - > phar - > fname , entry - > filename ) ;
2008-01-16 07:24:39 +00:00
}
2008-01-28 08:52:08 +00:00
return FAILURE ;
2008-01-16 07:24:39 +00:00
}
2008-08-01 13:45:06 +00:00
2008-01-16 07:24:39 +00:00
/* copy old contents in entirety */
2014-12-13 22:06:14 +00:00
phar_seek_efp ( entry , 0 , SEEK_SET , 0 , 1 ) ;
2008-01-28 08:52:08 +00:00
offset = php_stream_tell ( fp ) ;
2014-12-13 22:06:14 +00:00
link = phar_get_link_source ( entry ) ;
2008-08-01 13:45:06 +00:00
2008-04-21 06:17:51 +00:00
if ( ! link ) {
link = entry ;
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( SUCCESS ! = php_stream_copy_to_stream_ex ( phar_get_efp ( link , 0 ) , fp , link - > uncompressed_filesize , NULL ) ) {
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-01-16 07:24:39 +00:00
" Cannot convert phar archive \" %s \" , unable to copy entry \" %s \" contents " , entry - > phar - > fname , entry - > filename ) ;
return FAILURE ;
}
2008-08-01 13:45:06 +00:00
2008-01-28 08:52:08 +00:00
if ( entry - > fp_type = = PHAR_MOD ) {
/* save for potential restore on error */
entry - > cfp = entry - > fp ;
2008-01-16 07:24:39 +00:00
entry - > fp = NULL ;
}
2008-08-01 13:45:06 +00:00
2008-01-28 08:52:08 +00:00
/* set new location of file contents */
entry - > fp_type = PHAR_FP ;
entry - > offset = offset ;
2008-01-16 07:24:39 +00:00
return SUCCESS ;
}
/* }}} */
2015-07-07 05:58:28 +00:00
static zend_object * phar_rename_archive ( phar_archive_data * * sphar , char * ext , zend_bool compress ) /* { { { */
2008-01-28 08:52:08 +00:00
{
2010-11-19 16:03:00 +00:00
const char * oldname = NULL ;
2015-07-07 05:58:28 +00:00
phar_archive_data * phar = * sphar ;
2010-11-19 16:03:00 +00:00
char * oldpath = NULL ;
2008-03-23 22:42:45 +00:00
char * basename = NULL , * basepath = NULL ;
char * newname = NULL , * newpath = NULL ;
2014-05-08 14:30:07 +00:00
zval ret , arg1 ;
2008-03-23 22:42:45 +00:00
zend_class_entry * ce ;
char * error ;
2008-05-07 06:36:24 +00:00
const char * pcr_error ;
2014-12-16 08:31:20 +00:00
int ext_len = ext ? strlen ( ext ) : 0 ;
2008-08-31 20:54:28 +00:00
int oldname_len ;
2014-05-08 14:30:07 +00:00
phar_archive_data * pphar = NULL ;
2009-07-23 17:04:24 +00:00
php_stream_statbuf ssb ;
2008-03-23 22:42:45 +00:00
if ( ! ext ) {
if ( phar - > is_zip ) {
2008-08-01 13:45:06 +00:00
2008-03-23 22:42:45 +00:00
if ( phar - > is_data ) {
ext = " zip " ;
} else {
ext = " phar.zip " ;
}
2008-08-01 13:45:06 +00:00
2008-03-23 22:42:45 +00:00
} else if ( phar - > is_tar ) {
2008-08-01 13:45:06 +00:00
2008-03-23 22:42:45 +00:00
switch ( phar - > flags ) {
case PHAR_FILE_COMPRESSED_GZ :
if ( phar - > is_data ) {
ext = " tar.gz " ;
} else {
ext = " phar.tar.gz " ;
}
break ;
case PHAR_FILE_COMPRESSED_BZ2 :
if ( phar - > is_data ) {
ext = " tar.bz2 " ;
} else {
ext = " phar.tar.bz2 " ;
}
break ;
default :
if ( phar - > is_data ) {
ext = " tar " ;
} else {
ext = " phar.tar " ;
}
}
} else {
2008-08-01 13:45:06 +00:00
2008-03-23 22:42:45 +00:00
switch ( phar - > flags ) {
case PHAR_FILE_COMPRESSED_GZ :
ext = " phar.gz " ;
break ;
case PHAR_FILE_COMPRESSED_BZ2 :
ext = " phar.bz2 " ;
break ;
default :
ext = " phar " ;
}
}
2014-12-16 08:31:20 +00:00
} else if ( phar_path_check ( & ext , & ext_len , & pcr_error ) > pcr_is_ok ) {
2008-08-01 13:45:06 +00:00
2008-05-07 06:36:24 +00:00
if ( phar - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " data phar converted from \" %s \" has invalid extension %s " , phar - > fname , ext ) ;
2008-05-07 06:36:24 +00:00
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " phar converted from \" %s \" has invalid extension %s " , phar - > fname , ext ) ;
2008-05-07 06:36:24 +00:00
}
return NULL ;
2008-03-23 22:42:45 +00:00
}
if ( ext [ 0 ] = = ' . ' ) {
+ + ext ;
}
oldpath = estrndup ( phar - > fname , phar - > fname_len ) ;
2015-01-18 03:58:16 +00:00
if ( ( oldname = zend_memrchr ( phar - > fname , ' / ' , phar - > fname_len ) ) ) {
+ + oldname ;
} else {
oldname = phar - > fname ;
}
2008-08-31 20:54:28 +00:00
oldname_len = strlen ( oldname ) ;
2008-01-28 08:52:08 +00:00
2008-08-31 20:54:28 +00:00
basename = estrndup ( oldname , oldname_len ) ;
2008-03-23 22:42:45 +00:00
spprintf ( & newname , 0 , " %s.%s " , strtok ( basename , " . " ) , ext ) ;
efree ( basename ) ;
2015-01-03 09:22:58 +00:00
2009-07-23 17:04:24 +00:00
2008-08-31 20:54:28 +00:00
basepath = estrndup ( oldpath , ( strlen ( oldpath ) - oldname_len ) ) ;
2008-03-23 22:42:45 +00:00
phar - > fname_len = spprintf ( & newpath , 0 , " %s%s " , basepath , newname ) ;
phar - > fname = newpath ;
2008-05-24 14:18:31 +00:00
phar - > ext = newpath + phar - > fname_len - strlen ( ext ) - 1 ;
2008-03-23 22:42:45 +00:00
efree ( basepath ) ;
efree ( newname ) ;
2014-05-08 14:30:07 +00:00
if ( PHAR_G ( manifest_cached ) & & NULL ! = ( pphar = zend_hash_str_find_ptr ( & cached_phars , newpath , phar - > fname_len ) ) ) {
2008-08-01 13:45:06 +00:00
efree ( oldpath ) ;
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Unable to add newly converted phar \" %s \" to the list of phars, new phar name is in phar.cache_list " , phar - > fname ) ;
2008-08-01 13:45:06 +00:00
return NULL ;
}
2015-03-09 14:58:53 +00:00
if ( NULL ! = ( pphar = zend_hash_str_find_ptr ( & ( PHAR_G ( phar_fname_map ) ) , newpath , phar - > fname_len ) ) ) {
2014-05-08 14:30:07 +00:00
if ( pphar - > fname_len = = phar - > fname_len & & ! memcmp ( pphar - > fname , phar - > fname , phar - > fname_len ) ) {
2008-05-20 04:09:41 +00:00
if ( ! zend_hash_num_elements ( & phar - > manifest ) ) {
2014-05-08 14:30:07 +00:00
pphar - > is_tar = phar - > is_tar ;
pphar - > is_zip = phar - > is_zip ;
pphar - > is_data = phar - > is_data ;
pphar - > flags = phar - > flags ;
pphar - > fp = phar - > fp ;
2008-05-24 13:59:56 +00:00
phar - > fp = NULL ;
2014-12-13 22:06:14 +00:00
phar_destroy_phar_data ( phar ) ;
2015-07-07 05:58:28 +00:00
* sphar = NULL ;
2014-05-08 14:30:07 +00:00
phar = pphar ;
2008-05-24 14:18:31 +00:00
phar - > refcount + + ;
2008-05-24 13:59:56 +00:00
newpath = oldpath ;
2008-05-20 04:09:41 +00:00
goto its_ok ;
}
}
2008-08-01 13:45:06 +00:00
2008-04-23 18:30:53 +00:00
efree ( oldpath ) ;
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Unable to add newly converted phar \" %s \" to the list of phars, a phar with that name already exists " , phar - > fname ) ;
2008-03-23 22:42:45 +00:00
return NULL ;
}
2008-05-20 04:09:41 +00:00
its_ok :
2009-07-23 17:04:24 +00:00
if ( SUCCESS = = php_stream_stat_path ( newpath , & ssb ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " phar \" %s \" exists and must be unlinked prior to conversion " , newpath ) ;
2015-01-28 16:00:09 +00:00
efree ( oldpath ) ;
2009-07-23 17:04:24 +00:00
return NULL ;
}
2008-03-23 22:42:45 +00:00
if ( ! phar - > is_data ) {
2014-12-13 22:06:14 +00:00
if ( SUCCESS ! = phar_detect_phar_fname_ext ( newpath , phar - > fname_len , ( const char * * ) & ( phar - > ext ) , & ( phar - > ext_len ) , 1 , 1 , 1 ) ) {
2008-05-07 06:36:24 +00:00
efree ( oldpath ) ;
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " phar \" %s \" has invalid extension %s " , phar - > fname , ext ) ;
2008-05-07 06:36:24 +00:00
return NULL ;
}
2008-08-01 13:45:06 +00:00
2008-03-23 22:42:45 +00:00
if ( phar - > alias ) {
2008-03-24 03:01:31 +00:00
if ( phar - > is_temporary_alias ) {
phar - > alias = NULL ;
phar - > alias_len = 0 ;
} else {
phar - > alias = estrndup ( newpath , strlen ( newpath ) ) ;
phar - > alias_len = strlen ( newpath ) ;
phar - > is_temporary_alias = 1 ;
2015-03-09 14:58:53 +00:00
zend_hash_str_update_ptr ( & ( PHAR_G ( phar_alias_map ) ) , newpath , phar - > fname_len , phar ) ;
2008-03-24 03:01:31 +00:00
}
2008-03-23 22:42:45 +00:00
}
2008-08-01 13:45:06 +00:00
2008-03-24 03:01:31 +00:00
} else {
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( SUCCESS ! = phar_detect_phar_fname_ext ( newpath , phar - > fname_len , ( const char * * ) & ( phar - > ext ) , & ( phar - > ext_len ) , 0 , 1 , 1 ) ) {
2008-05-07 06:36:24 +00:00
efree ( oldpath ) ;
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " data phar \" %s \" has invalid extension %s " , phar - > fname , ext ) ;
2008-05-07 06:36:24 +00:00
return NULL ;
}
2008-08-01 13:45:06 +00:00
2008-03-24 03:01:31 +00:00
phar - > alias = NULL ;
phar - > alias_len = 0 ;
2008-03-23 22:42:45 +00:00
}
2015-03-09 14:58:53 +00:00
if ( ( ! pphar | | phar = = pphar ) & & NULL = = zend_hash_str_update_ptr ( & ( PHAR_G ( phar_fname_map ) ) , newpath , phar - > fname_len , phar ) ) {
2008-05-07 06:36:24 +00:00
efree ( oldpath ) ;
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Unable to add newly converted phar \" %s \" to the list of phars " , phar - > fname ) ;
2008-05-07 06:36:24 +00:00
return NULL ;
}
2014-12-13 22:06:14 +00:00
phar_flush ( phar , 0 , 0 , 1 , & error ) ;
2008-08-01 13:45:06 +00:00
2008-03-23 22:42:45 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " %s " , error ) ;
2008-03-23 22:42:45 +00:00
efree ( error ) ;
2008-04-23 18:30:53 +00:00
efree ( oldpath ) ;
2008-03-23 22:42:45 +00:00
return NULL ;
}
efree ( oldpath ) ;
if ( phar - > is_data ) {
ce = phar_ce_data ;
2008-01-28 08:52:08 +00:00
} else {
2008-03-23 22:42:45 +00:00
ce = phar_ce_archive ;
2008-01-28 08:52:08 +00:00
}
2008-03-23 22:42:45 +00:00
2014-05-08 14:30:07 +00:00
ZVAL_NULL ( & ret ) ;
if ( SUCCESS ! = object_init_ex ( & ret , ce ) ) {
zval_dtor ( & ret ) ;
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Unable to instantiate phar object when converting archive \" %s \" " , phar - > fname ) ;
2008-03-23 22:42:45 +00:00
return NULL ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 14:30:07 +00:00
ZVAL_STRINGL ( & arg1 , phar - > fname , phar - > fname_len ) ;
2008-03-23 22:42:45 +00:00
zend_call_method_with_1_params ( & ret , ce , & ce - > constructor , " __construct " , NULL , & arg1 ) ;
2014-05-08 14:30:07 +00:00
zval_ptr_dtor ( & arg1 ) ;
return Z_OBJ ( ret ) ;
2008-01-28 08:52:08 +00:00
}
2008-08-01 13:45:06 +00:00
/* }}} */
2008-01-28 08:52:08 +00:00
2016-01-28 16:11:53 +00:00
static zend_object * phar_convert_to_other ( phar_archive_data * source , int convert , char * ext , uint32_t flags ) /* { { { */
2008-01-16 07:24:39 +00:00
{
2008-03-23 22:42:45 +00:00
phar_archive_data * phar ;
2008-01-28 08:52:08 +00:00
phar_entry_info * entry , newentry ;
2014-05-08 14:30:07 +00:00
zend_object * ret ;
2008-01-16 07:24:39 +00:00
2008-08-01 13:45:06 +00:00
/* invalidate phar cache */
PHAR_G ( last_phar ) = NULL ;
PHAR_G ( last_phar_name ) = PHAR_G ( last_alias ) = NULL ;
2008-03-23 22:42:45 +00:00
phar = ( phar_archive_data * ) ecalloc ( 1 , sizeof ( phar_archive_data ) ) ;
2008-02-27 21:34:26 +00:00
/* set whole-archive compression and type from parameter */
2008-03-23 22:42:45 +00:00
phar - > flags = flags ;
phar - > is_data = source - > is_data ;
2008-08-01 13:45:06 +00:00
2008-01-16 07:24:39 +00:00
switch ( convert ) {
2008-08-01 13:45:06 +00:00
case PHAR_FORMAT_TAR :
2008-03-23 22:42:45 +00:00
phar - > is_tar = 1 ;
2008-01-16 07:24:39 +00:00
break ;
2008-08-01 13:45:06 +00:00
case PHAR_FORMAT_ZIP :
2008-03-23 22:42:45 +00:00
phar - > is_zip = 1 ;
break ;
2008-08-01 13:45:06 +00:00
default :
2008-03-23 22:42:45 +00:00
phar - > is_data = 0 ;
2008-01-16 07:24:39 +00:00
break ;
}
2008-03-23 22:42:45 +00:00
zend_hash_init ( & ( phar - > manifest ) , sizeof ( phar_entry_info ) ,
2008-01-16 07:24:39 +00:00
zend_get_hash_value , destroy_phar_manifest_entry , 0 ) ;
2008-08-01 13:45:06 +00:00
zend_hash_init ( & phar - > mounted_dirs , sizeof ( char * ) ,
zend_get_hash_value , NULL , 0 ) ;
zend_hash_init ( & phar - > virtual_dirs , sizeof ( char * ) ,
zend_get_hash_value , NULL , 0 ) ;
2008-01-16 07:24:39 +00:00
2008-03-23 22:42:45 +00:00
phar - > fp = php_stream_fopen_tmpfile ( ) ;
2013-05-14 14:45:11 +00:00
if ( phar - > fp = = NULL ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " unable to create temporary file " ) ;
2013-05-14 14:45:11 +00:00
return NULL ;
}
2008-03-23 22:42:45 +00:00
phar - > fname = source - > fname ;
phar - > fname_len = source - > fname_len ;
phar - > is_temporary_alias = source - > is_temporary_alias ;
2008-03-24 03:01:31 +00:00
phar - > alias = source - > alias ;
2008-08-01 13:45:06 +00:00
2014-05-08 14:30:07 +00:00
if ( Z_TYPE ( source - > metadata ) ! = IS_UNDEF ) {
2014-05-12 12:43:18 +00:00
ZVAL_DUP ( & phar - > metadata , & source - > metadata ) ;
2009-05-24 18:50:37 +00:00
phar - > metadata_len = 0 ;
}
2008-01-16 07:24:39 +00:00
/* first copy each file's uncompressed contents to a temporary file and set per-file flags */
2014-05-28 10:09:42 +00:00
ZEND_HASH_FOREACH_PTR ( & source - > manifest , entry ) {
2008-08-01 13:45:06 +00:00
2008-01-16 07:24:39 +00:00
newentry = * entry ;
2008-08-01 13:45:06 +00:00
2008-05-11 19:17:51 +00:00
if ( newentry . link ) {
newentry . link = estrdup ( newentry . link ) ;
2008-05-11 22:45:03 +00:00
goto no_copy ;
}
2008-08-01 13:45:06 +00:00
2008-05-11 22:45:03 +00:00
if ( newentry . tmp ) {
newentry . tmp = estrdup ( newentry . tmp ) ;
goto no_copy ;
2008-05-11 19:17:51 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 14:30:07 +00:00
newentry . metadata_str . s = NULL ;
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_copy_file_contents ( & newentry , phar - > fp ) ) {
2008-03-23 22:42:45 +00:00
zend_hash_destroy ( & ( phar - > manifest ) ) ;
php_stream_close ( phar - > fp ) ;
efree ( phar ) ;
2008-01-16 07:24:39 +00:00
/* exception already thrown */
2008-03-23 22:42:45 +00:00
return NULL ;
2008-01-16 07:24:39 +00:00
}
2008-05-11 22:45:03 +00:00
no_copy :
2008-01-16 07:24:39 +00:00
newentry . filename = estrndup ( newentry . filename , newentry . filename_len ) ;
2008-08-01 13:45:06 +00:00
2014-05-08 14:30:07 +00:00
if ( Z_TYPE ( newentry . metadata ) ! = IS_UNDEF ) {
zval_copy_ctor ( & newentry . metadata ) ;
newentry . metadata_str . s = NULL ;
2008-01-16 07:24:39 +00:00
}
2008-08-01 13:45:06 +00:00
2008-03-23 22:42:45 +00:00
newentry . is_zip = phar - > is_zip ;
newentry . is_tar = phar - > is_tar ;
2008-08-01 13:45:06 +00:00
2008-01-16 07:24:39 +00:00
if ( newentry . is_tar ) {
newentry . tar_type = ( entry - > is_dir ? TAR_DIR : TAR_FILE ) ;
}
2008-08-01 13:45:06 +00:00
2008-01-16 07:24:39 +00:00
newentry . is_modified = 1 ;
2008-03-23 22:42:45 +00:00
newentry . phar = phar ;
2008-08-01 13:45:06 +00:00
newentry . old_flags = newentry . flags & ~ PHAR_ENT_COMPRESSION_MASK ; /* remove compression from old_flags */
2014-12-13 22:06:14 +00:00
phar_set_inode ( & newentry ) ;
2014-05-08 14:30:07 +00:00
zend_hash_str_add_mem ( & ( phar - > manifest ) , newentry . filename , newentry . filename_len , ( void * ) & newentry , sizeof ( phar_entry_info ) ) ;
2014-12-13 22:06:14 +00:00
phar_add_virtual_dirs ( phar , newentry . filename , newentry . filename_len ) ;
2014-05-28 10:09:42 +00:00
} ZEND_HASH_FOREACH_END ( ) ;
2008-02-27 21:34:26 +00:00
2015-07-07 05:58:28 +00:00
if ( ( ret = phar_rename_archive ( & phar , ext , 0 ) ) ) {
2008-03-23 22:42:45 +00:00
return ret ;
2008-01-28 08:52:08 +00:00
} else {
2015-07-07 05:58:28 +00:00
if ( phar ! = NULL ) {
zend_hash_destroy ( & ( phar - > manifest ) ) ;
zend_hash_destroy ( & ( phar - > mounted_dirs ) ) ;
zend_hash_destroy ( & ( phar - > virtual_dirs ) ) ;
if ( phar - > fp ) {
php_stream_close ( phar - > fp ) ;
}
efree ( phar - > fname ) ;
efree ( phar ) ;
2015-07-05 04:01:50 +00:00
}
2008-03-23 22:42:45 +00:00
return NULL ;
2008-02-24 22:29:06 +00:00
}
2008-01-16 07:24:39 +00:00
}
2008-02-08 09:25:02 +00:00
/* }}} */
2008-01-16 07:24:39 +00:00
2008-04-11 04:20:15 +00:00
/* {{{ proto object Phar::convertToExecutable([int format[, int compression [, string file_ext]]])
2008-08-01 13:45:06 +00:00
* Convert a phar . tar or phar . zip archive to the phar file format . The
2008-04-11 04:20:15 +00:00
* optional parameter allows the user to determine the new
* filename extension ( default is phar ) .
2008-01-16 07:24:39 +00:00
*/
2008-04-11 04:20:15 +00:00
PHP_METHOD ( Phar , convertToExecutable )
2008-01-16 07:24:39 +00:00
{
2008-03-01 21:31:03 +00:00
char * ext = NULL ;
2014-08-27 17:25:28 +00:00
int is_data ;
size_t ext_len = 0 ;
2016-01-28 16:11:53 +00:00
uint32_t flags ;
2014-05-08 14:30:07 +00:00
zend_object * ret ;
2008-04-11 04:20:15 +00:00
/* a number that is not 0, 1 or 2 (Which is also Greg's birthday, so there) */
2014-08-25 17:24:55 +00:00
zend_long format = 9021976 , method = 9021976 ;
2008-01-16 07:24:39 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2008-02-24 22:29:06 +00:00
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " |lls " , & format , & method , & ext , & ext_len ) = = FAILURE ) {
2008-02-24 22:29:06 +00:00
return ;
}
2008-02-28 01:08:34 +00:00
2008-01-16 07:24:39 +00:00
if ( PHAR_G ( readonly ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-04-11 04:20:15 +00:00
" Cannot write out executable phar archive, phar is read-only " ) ;
2008-03-23 22:42:45 +00:00
return ;
2008-01-16 07:24:39 +00:00
}
2008-01-16 21:09:32 +00:00
2008-04-11 04:20:15 +00:00
switch ( format ) {
case 9021976 :
2008-04-23 18:44:43 +00:00
case PHAR_FORMAT_SAME : /* null is converted to 0 */
2008-04-11 04:20:15 +00:00
/* by default, use the existing format */
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > is_tar ) {
2008-04-11 04:20:15 +00:00
format = PHAR_FORMAT_TAR ;
2014-05-08 17:18:59 +00:00
} else if ( phar_obj - > archive - > is_zip ) {
2008-04-11 04:20:15 +00:00
format = PHAR_FORMAT_ZIP ;
} else {
format = PHAR_FORMAT_PHAR ;
}
break ;
case PHAR_FORMAT_PHAR :
case PHAR_FORMAT_TAR :
case PHAR_FORMAT_ZIP :
break ;
default :
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 04:20:15 +00:00
" Unknown file format specified, please pass one of Phar::PHAR, Phar::TAR or Phar::ZIP " ) ;
return ;
2008-03-03 08:41:15 +00:00
}
2008-01-16 21:09:32 +00:00
switch ( method ) {
2008-04-11 04:20:15 +00:00
case 9021976 :
2014-05-08 17:18:59 +00:00
flags = phar_obj - > archive - > flags & PHAR_FILE_COMPRESSION_MASK ;
2008-04-11 04:20:15 +00:00
break ;
2008-01-16 21:09:32 +00:00
case 0 :
flags = PHAR_FILE_COMPRESSED_NONE ;
break ;
case PHAR_ENT_COMPRESSED_GZ :
2008-04-11 04:20:15 +00:00
if ( format = = PHAR_FORMAT_ZIP ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 04:20:15 +00:00
" Cannot compress entire archive with gzip, zip archives do not support whole-archive compression " ) ;
return ;
}
2008-08-01 13:45:06 +00:00
2008-05-17 20:07:38 +00:00
if ( ! PHAR_G ( has_zlib ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-01-16 21:09:32 +00:00
" Cannot compress entire archive with gzip, enable ext/zlib in php.ini " ) ;
return ;
}
2008-08-01 13:45:06 +00:00
2008-01-16 21:09:32 +00:00
flags = PHAR_FILE_COMPRESSED_GZ ;
break ;
case PHAR_ENT_COMPRESSED_BZ2 :
2008-04-11 04:20:15 +00:00
if ( format = = PHAR_FORMAT_ZIP ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 04:20:15 +00:00
" Cannot compress entire archive with bz2, zip archives do not support whole-archive compression " ) ;
return ;
}
2008-08-01 13:45:06 +00:00
2008-05-17 20:07:38 +00:00
if ( ! PHAR_G ( has_bz2 ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-01-16 21:09:32 +00:00
" Cannot compress entire archive with bz2, enable ext/bz2 in php.ini " ) ;
return ;
}
2008-08-01 13:45:06 +00:00
2008-01-16 21:09:32 +00:00
flags = PHAR_FILE_COMPRESSED_BZ2 ;
break ;
default :
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-01-16 21:09:32 +00:00
" Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2 " ) ;
return ;
}
2008-01-16 07:24:39 +00:00
2014-05-08 17:18:59 +00:00
is_data = phar_obj - > archive - > is_data ;
phar_obj - > archive - > is_data = 0 ;
2014-12-13 22:06:14 +00:00
ret = phar_convert_to_other ( phar_obj - > archive , format , ext , flags ) ;
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > is_data = is_data ;
2008-08-01 13:45:06 +00:00
2008-03-23 22:42:45 +00:00
if ( ret ) {
2014-05-08 14:30:07 +00:00
ZVAL_OBJ ( return_value , ret ) ;
2008-03-23 22:42:45 +00:00
} else {
RETURN_NULL ( ) ;
}
}
/* }}} */
2008-04-11 04:20:15 +00:00
/* {{{ proto object Phar::convertToData([int format[, int compression [, string file_ext]]])
* Convert an archive to a non - executable . tar or . zip .
2008-03-23 22:42:45 +00:00
* The optional parameter allows the user to determine the new
* filename extension ( default is . zip or . tar ) .
*/
PHP_METHOD ( Phar , convertToData )
{
char * ext = NULL ;
2014-08-27 17:25:28 +00:00
int is_data ;
size_t ext_len = 0 ;
2016-01-28 16:11:53 +00:00
uint32_t flags ;
2014-05-08 14:30:07 +00:00
zend_object * ret ;
2008-04-11 04:20:15 +00:00
/* a number that is not 0, 1 or 2 (Which is also Greg's birthday so there) */
2014-08-25 17:24:55 +00:00
zend_long format = 9021976 , method = 9021976 ;
2008-03-23 22:42:45 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " |lls " , & format , & method , & ext , & ext_len ) = = FAILURE ) {
2008-03-23 22:42:45 +00:00
return ;
}
2008-04-11 04:20:15 +00:00
switch ( format ) {
case 9021976 :
2008-04-23 18:44:43 +00:00
case PHAR_FORMAT_SAME : /* null is converted to 0 */
2008-04-11 04:20:15 +00:00
/* by default, use the existing format */
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > is_tar ) {
2008-04-11 04:20:15 +00:00
format = PHAR_FORMAT_TAR ;
2014-05-08 17:18:59 +00:00
} else if ( phar_obj - > archive - > is_zip ) {
2008-04-11 04:20:15 +00:00
format = PHAR_FORMAT_ZIP ;
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-04-11 04:20:15 +00:00
" Cannot write out data phar archive, use Phar::TAR or Phar::ZIP " ) ;
2008-04-23 18:54:09 +00:00
return ;
2008-04-11 04:20:15 +00:00
}
break ;
case PHAR_FORMAT_PHAR :
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-04-11 04:20:15 +00:00
" Cannot write out data phar archive, use Phar::TAR or Phar::ZIP " ) ;
return ;
case PHAR_FORMAT_TAR :
case PHAR_FORMAT_ZIP :
break ;
default :
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 04:20:15 +00:00
" Unknown file format specified, please pass one of Phar::TAR or Phar::ZIP " ) ;
return ;
2008-03-23 22:42:45 +00:00
}
2008-04-11 04:20:15 +00:00
switch ( method ) {
case 9021976 :
2014-05-08 17:18:59 +00:00
flags = phar_obj - > archive - > flags & PHAR_FILE_COMPRESSION_MASK ;
2008-04-11 04:20:15 +00:00
break ;
case 0 :
flags = PHAR_FILE_COMPRESSED_NONE ;
break ;
case PHAR_ENT_COMPRESSED_GZ :
if ( format = = PHAR_FORMAT_ZIP ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 04:20:15 +00:00
" Cannot compress entire archive with gzip, zip archives do not support whole-archive compression " ) ;
return ;
}
2008-08-01 13:45:06 +00:00
2008-05-17 20:07:38 +00:00
if ( ! PHAR_G ( has_zlib ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 04:20:15 +00:00
" Cannot compress entire archive with gzip, enable ext/zlib in php.ini " ) ;
return ;
}
2008-08-01 13:45:06 +00:00
2008-04-11 04:20:15 +00:00
flags = PHAR_FILE_COMPRESSED_GZ ;
break ;
case PHAR_ENT_COMPRESSED_BZ2 :
if ( format = = PHAR_FORMAT_ZIP ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 04:20:15 +00:00
" Cannot compress entire archive with bz2, zip archives do not support whole-archive compression " ) ;
return ;
}
2008-08-01 13:45:06 +00:00
2008-05-17 20:07:38 +00:00
if ( ! PHAR_G ( has_bz2 ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 04:20:15 +00:00
" Cannot compress entire archive with bz2, enable ext/bz2 in php.ini " ) ;
return ;
}
2008-08-01 13:45:06 +00:00
2008-04-11 04:20:15 +00:00
flags = PHAR_FILE_COMPRESSED_BZ2 ;
break ;
default :
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 04:20:15 +00:00
" Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2 " ) ;
return ;
2008-03-23 22:42:45 +00:00
}
2014-05-08 17:18:59 +00:00
is_data = phar_obj - > archive - > is_data ;
phar_obj - > archive - > is_data = 1 ;
2014-12-13 22:06:14 +00:00
ret = phar_convert_to_other ( phar_obj - > archive , format , ext , flags ) ;
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > is_data = is_data ;
2008-08-01 13:45:06 +00:00
2008-03-23 22:42:45 +00:00
if ( ret ) {
2014-05-08 14:30:07 +00:00
ZVAL_OBJ ( return_value , ret ) ;
2008-03-23 22:42:45 +00:00
} else {
RETURN_NULL ( ) ;
}
2008-01-16 07:24:39 +00:00
}
/* }}} */
2008-01-21 05:28:09 +00:00
/* {{{ proto int|false Phar::isCompressed()
2008-02-25 16:27:23 +00:00
* Returns Phar : : GZ or PHAR : : BZ2 if the entire archive is compressed
* ( . tar . gz / tar . bz2 and so on ) , or FALSE otherwise .
2008-01-09 07:09:04 +00:00
*/
PHP_METHOD ( Phar , isCompressed )
{
PHAR_ARCHIVE_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > flags & PHAR_FILE_COMPRESSED_GZ ) {
2014-08-25 17:24:55 +00:00
RETURN_LONG ( PHAR_ENT_COMPRESSED_GZ ) ;
2008-01-09 07:09:04 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > flags & PHAR_FILE_COMPRESSED_BZ2 ) {
2014-08-25 17:24:55 +00:00
RETURN_LONG ( PHAR_ENT_COMPRESSED_BZ2 ) ;
2008-01-09 07:09:04 +00:00
}
2008-08-01 13:45:06 +00:00
2008-01-21 05:28:09 +00:00
RETURN_FALSE ;
2008-01-09 07:09:04 +00:00
}
/* }}} */
2008-04-10 03:32:10 +00:00
/* {{{ proto bool Phar::isWritable()
* Returns true if phar . readonly = 0 or phar is a PharData AND the actual file is writable .
*/
PHP_METHOD ( Phar , isWritable )
{
php_stream_statbuf ssb ;
PHAR_ARCHIVE_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( ! phar_obj - > archive - > is_writeable ) {
2008-04-10 03:32:10 +00:00
RETURN_FALSE ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( SUCCESS ! = php_stream_stat_path ( phar_obj - > archive - > fname , & ssb ) ) {
if ( phar_obj - > archive - > is_brandnew ) {
2008-04-10 03:32:10 +00:00
/* assume it works if the file doesn't exist yet */
RETURN_TRUE ;
}
RETURN_FALSE ;
}
2008-08-01 13:45:06 +00:00
2008-04-10 03:32:10 +00:00
RETURN_BOOL ( ( ssb . sb . st_mode & ( S_IWOTH | S_IWGRP | S_IWUSR ) ) ! = 0 ) ;
}
/* }}} */
2008-02-20 17:58:56 +00:00
/* {{{ proto bool Phar::delete(string entry)
2008-02-25 16:27:23 +00:00
* Deletes a named file within the archive .
2007-08-25 01:32:35 +00:00
*/
PHP_METHOD ( Phar , delete )
{
char * fname ;
2014-08-27 17:25:28 +00:00
size_t fname_len ;
2007-08-25 01:32:35 +00:00
char * error ;
phar_entry_info * entry ;
PHAR_ARCHIVE_OBJECT ( ) ;
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2007-08-25 01:32:35 +00:00
" Cannot write out phar archive, phar is read-only " ) ;
2008-01-14 05:12:46 +00:00
return ;
2007-08-25 01:32:35 +00:00
}
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " p " , & fname , & fname_len ) = = FAILURE ) {
2007-08-25 01:32:35 +00:00
RETURN_FALSE ;
}
2014-12-13 22:06:14 +00:00
if ( phar_obj - > archive - > is_persistent & & FAILURE = = phar_copy_on_write ( & ( phar_obj - > archive ) ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar_obj - > archive - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
2014-05-08 17:18:59 +00:00
if ( zend_hash_str_exists ( & phar_obj - > archive - > manifest , fname , ( uint ) fname_len ) ) {
if ( NULL ! = ( entry = zend_hash_str_find_ptr ( & phar_obj - > archive - > manifest , fname , ( uint ) fname_len ) ) ) {
2007-08-25 01:32:35 +00:00
if ( entry - > is_deleted ) {
/* entry is deleted, but has not been flushed to disk yet */
RETURN_TRUE ;
} else {
entry - > is_deleted = 1 ;
entry - > is_modified = 1 ;
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > is_modified = 1 ;
2007-08-25 01:32:35 +00:00
}
}
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Entry %s does not exist and cannot be deleted " , fname ) ;
2007-08-25 01:32:35 +00:00
RETURN_FALSE ;
}
2014-12-13 22:06:14 +00:00
phar_flush ( phar_obj - > archive , NULL , 0 , 0 , & error ) ;
2007-08-25 01:32:35 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-08-25 01:32:35 +00:00
efree ( error ) ;
}
2008-08-01 13:45:06 +00:00
2007-08-25 01:32:35 +00:00
RETURN_TRUE ;
}
2007-08-25 01:36:21 +00:00
/* }}} */
2007-08-25 01:32:35 +00:00
2007-05-18 17:50:39 +00:00
/* {{{ proto int Phar::getAlias()
2008-02-25 16:27:23 +00:00
* Returns the alias for the Phar or NULL .
2007-05-18 17:50:39 +00:00
*/
PHP_METHOD ( Phar , getAlias )
{
PHAR_ARCHIVE_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2007-05-18 17:50:39 +00:00
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > alias & & phar_obj - > archive - > alias ! = phar_obj - > archive - > fname ) {
RETURN_STRINGL ( phar_obj - > archive - > alias , phar_obj - > archive - > alias_len ) ;
2007-05-18 17:50:39 +00:00
}
}
/* }}} */
2008-03-23 19:03:15 +00:00
/* {{{ proto int Phar::getPath()
* Returns the real path to the phar archive on disk
*/
PHP_METHOD ( Phar , getPath )
{
PHAR_ARCHIVE_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2008-03-23 19:03:15 +00:00
2014-05-08 17:18:59 +00:00
RETURN_STRINGL ( phar_obj - > archive - > fname , phar_obj - > archive - > fname_len ) ;
2008-03-23 19:03:15 +00:00
}
/* }}} */
2007-08-23 04:49:39 +00:00
/* {{{ proto bool Phar::setAlias(string alias)
2008-02-25 16:27:23 +00:00
* Sets the alias for a Phar archive . The default value is the full path
* to the archive .
2007-08-23 04:49:39 +00:00
*/
PHP_METHOD ( Phar , setAlias )
{
2008-02-11 06:46:44 +00:00
char * alias , * error , * oldalias ;
2014-05-08 14:30:07 +00:00
phar_archive_data * fd_ptr ;
2014-08-27 17:25:28 +00:00
size_t alias_len , oldalias_len ;
int old_temp , readd = 0 ;
2008-08-01 13:45:06 +00:00
2007-08-23 04:49:39 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2007-08-25 01:32:35 +00:00
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2007-08-25 01:32:35 +00:00
" Cannot write out phar archive, phar is read-only " ) ;
2008-03-24 01:33:30 +00:00
RETURN_FALSE ;
2007-08-25 01:32:35 +00:00
}
2008-08-01 13:45:06 +00:00
/* invalidate phar cache */
PHAR_G ( last_phar ) = NULL ;
PHAR_G ( last_phar_name ) = PHAR_G ( last_alias ) = NULL ;
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > is_data ) {
if ( phar_obj - > archive - > is_tar ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-08-01 13:45:06 +00:00
" A Phar alias cannot be set in a plain tar archive " ) ;
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-08-01 13:45:06 +00:00
" A Phar alias cannot be set in a plain zip archive " ) ;
}
2008-03-24 01:33:30 +00:00
RETURN_FALSE ;
2008-02-27 21:34:26 +00:00
}
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " s " , & alias , & alias_len ) = = SUCCESS ) {
2016-06-21 21:40:50 +00:00
if ( alias_len = = ( size_t ) phar_obj - > archive - > alias_len & & memcmp ( phar_obj - > archive - > alias , alias , alias_len ) = = 0 ) {
2007-08-23 23:23:26 +00:00
RETURN_TRUE ;
}
2015-03-09 14:58:53 +00:00
if ( alias_len & & NULL ! = ( fd_ptr = zend_hash_str_find_ptr ( & ( PHAR_G ( phar_alias_map ) ) , alias , alias_len ) ) ) {
2014-05-08 14:30:07 +00:00
spprintf ( & error , 0 , " alias \" %s \" is already used for archive \" %s \" and cannot be used for other archives " , alias , fd_ptr - > fname ) ;
2014-12-13 22:06:14 +00:00
if ( SUCCESS = = phar_free_alias ( fd_ptr , alias , alias_len ) ) {
2008-05-06 21:14:53 +00:00
efree ( error ) ;
goto valid_alias ;
}
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-08-23 23:23:26 +00:00
efree ( error ) ;
RETURN_FALSE ;
}
2008-03-24 01:33:30 +00:00
if ( ! phar_validate_alias ( alias , alias_len ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2014-05-08 17:18:59 +00:00
" Invalid alias \" %s \" specified for phar \" %s \" " , alias , phar_obj - > archive - > fname ) ;
2008-03-24 01:33:30 +00:00
RETURN_FALSE ;
}
2008-05-06 21:14:53 +00:00
valid_alias :
2014-12-13 22:06:14 +00:00
if ( phar_obj - > archive - > is_persistent & & FAILURE = = phar_copy_on_write ( & ( phar_obj - > archive ) ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar_obj - > archive - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
2015-03-09 14:58:53 +00:00
if ( phar_obj - > archive - > alias_len & & NULL ! = ( fd_ptr = zend_hash_str_find_ptr ( & ( PHAR_G ( phar_alias_map ) ) , phar_obj - > archive - > alias , phar_obj - > archive - > alias_len ) ) ) {
zend_hash_str_del ( & ( PHAR_G ( phar_alias_map ) ) , phar_obj - > archive - > alias , phar_obj - > archive - > alias_len ) ;
2008-02-11 06:46:44 +00:00
readd = 1 ;
2007-08-23 04:49:39 +00:00
}
2014-05-08 17:18:59 +00:00
oldalias = phar_obj - > archive - > alias ;
oldalias_len = phar_obj - > archive - > alias_len ;
old_temp = phar_obj - > archive - > is_temporary_alias ;
2008-08-01 13:45:06 +00:00
2007-12-31 22:42:40 +00:00
if ( alias_len ) {
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > alias = estrndup ( alias , alias_len ) ;
2007-12-31 22:42:40 +00:00
} else {
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > alias = NULL ;
2007-12-31 22:42:40 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > alias_len = alias_len ;
phar_obj - > archive - > is_temporary_alias = 0 ;
2014-12-13 22:06:14 +00:00
phar_flush ( phar_obj - > archive , NULL , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-08-23 04:49:39 +00:00
if ( error ) {
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > alias = oldalias ;
phar_obj - > archive - > alias_len = oldalias_len ;
phar_obj - > archive - > is_temporary_alias = old_temp ;
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2008-02-11 06:46:44 +00:00
if ( readd ) {
2015-03-09 14:58:53 +00:00
zend_hash_str_add_ptr ( & ( PHAR_G ( phar_alias_map ) ) , oldalias , oldalias_len , phar_obj - > archive ) ;
2008-02-11 06:46:44 +00:00
}
2007-08-23 04:49:39 +00:00
efree ( error ) ;
2008-03-24 01:33:30 +00:00
RETURN_FALSE ;
2008-02-11 06:46:44 +00:00
}
2008-08-01 13:45:06 +00:00
2015-03-09 14:58:53 +00:00
zend_hash_str_add_ptr ( & ( PHAR_G ( phar_alias_map ) ) , alias , alias_len , phar_obj - > archive ) ;
2008-08-01 13:45:06 +00:00
2008-02-11 06:46:44 +00:00
if ( oldalias ) {
efree ( oldalias ) ;
2007-08-23 04:49:39 +00:00
}
2008-08-01 13:45:06 +00:00
2007-08-23 04:49:39 +00:00
RETURN_TRUE ;
}
RETURN_FALSE ;
}
2008-02-08 09:25:02 +00:00
/* }}} */
2007-08-23 04:49:39 +00:00
2007-01-21 15:25:50 +00:00
/* {{{ proto string Phar::getVersion()
* Return version info of Phar archive
*/
PHP_METHOD ( Phar , getVersion )
{
PHAR_ARCHIVE_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
RETURN_STRING ( phar_obj - > archive - > version ) ;
2007-01-21 15:25:50 +00:00
}
/* }}} */
2007-03-22 16:31:46 +00:00
/* {{{ proto void Phar::startBuffering()
2007-01-26 14:52:10 +00:00
* Do not flush a writeable phar ( save its contents ) until explicitly requested
*/
2007-03-22 16:31:46 +00:00
PHP_METHOD ( Phar , startBuffering )
2007-01-26 14:52:10 +00:00
{
PHAR_ARCHIVE_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > donotflush = 1 ;
2007-01-26 14:52:10 +00:00
}
/* }}} */
2007-03-22 16:31:46 +00:00
/* {{{ proto bool Phar::isBuffering()
2008-02-25 16:27:23 +00:00
* Returns whether write operations are flushing to disk immediately .
2007-01-30 20:58:14 +00:00
*/
2007-03-22 16:31:46 +00:00
PHP_METHOD ( Phar , isBuffering )
2007-01-30 20:58:14 +00:00
{
PHAR_ARCHIVE_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
RETURN_BOOL ( phar_obj - > archive - > donotflush ) ;
2007-01-30 20:58:14 +00:00
}
/* }}} */
2007-03-22 16:31:46 +00:00
/* {{{ proto bool Phar::stopBuffering()
2008-02-25 16:27:23 +00:00
* Saves the contents of a modified archive to disk .
2007-01-26 14:52:10 +00:00
*/
2007-03-22 16:31:46 +00:00
PHP_METHOD ( Phar , stopBuffering )
2007-01-26 14:52:10 +00:00
{
2007-01-29 06:02:19 +00:00
char * error ;
2008-08-01 13:45:06 +00:00
2007-01-26 14:52:10 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2007-01-26 14:52:10 +00:00
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2007-01-26 14:52:10 +00:00
" Cannot write out phar archive, phar is read-only " ) ;
2008-01-14 05:12:46 +00:00
return ;
2007-01-26 14:52:10 +00:00
}
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > donotflush = 0 ;
2014-12-13 22:06:14 +00:00
phar_flush ( phar_obj - > archive , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-01-29 06:02:19 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-01-29 06:02:19 +00:00
efree ( error ) ;
}
2007-01-28 22:11:28 +00:00
}
/* }}} */
/* {{{ proto bool Phar::setStub(string|stream stub [, int len])
2008-02-25 16:27:23 +00:00
* Change the stub in a phar , phar . tar or phar . zip archive to something other
* than the default . The stub * must * end with a call to __HALT_COMPILER ( ) .
2007-01-28 22:11:28 +00:00
*/
PHP_METHOD ( Phar , setStub )
{
zval * zstub ;
2007-01-29 06:02:19 +00:00
char * stub , * error ;
2014-08-27 17:25:28 +00:00
size_t stub_len ;
2014-08-25 17:24:55 +00:00
zend_long len = - 1 ;
2007-01-28 22:11:28 +00:00
php_stream * stream ;
PHAR_ARCHIVE_OBJECT ( ) ;
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2007-01-28 22:11:28 +00:00
" Cannot change stub, phar is read-only " ) ;
2008-01-14 05:12:46 +00:00
return ;
2007-01-28 22:11:28 +00:00
}
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > is_data ) {
if ( phar_obj - > archive - > is_tar ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-08-01 13:45:06 +00:00
" A Phar stub cannot be set in a plain tar archive " ) ;
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-08-01 13:45:06 +00:00
" A Phar stub cannot be set in a plain zip archive " ) ;
}
2008-02-27 21:34:26 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters_ex ( ZEND_PARSE_PARAMS_QUIET , ZEND_NUM_ARGS ( ) , " r|l " , & zstub , & len ) = = SUCCESS ) {
2014-05-08 14:30:07 +00:00
if ( ( php_stream_from_zval_no_verify ( stream , zstub ) ) ! = NULL ) {
2007-01-28 22:11:28 +00:00
if ( len > 0 ) {
len = - len ;
} else {
len = - 1 ;
}
2014-12-13 22:06:14 +00:00
if ( phar_obj - > archive - > is_persistent & & FAILURE = = phar_copy_on_write ( & ( phar_obj - > archive ) ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar_obj - > archive - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
phar_flush ( phar_obj - > archive , ( char * ) zstub , len , 0 , & error ) ;
2007-01-29 06:02:19 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-01-29 06:02:19 +00:00
efree ( error ) ;
}
2007-01-28 22:11:28 +00:00
RETURN_TRUE ;
2007-01-27 15:31:24 +00:00
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2007-01-28 22:11:28 +00:00
" Cannot change stub, unable to read from input stream " ) ;
2007-01-26 14:52:10 +00:00
}
2014-12-13 22:06:14 +00:00
} else if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " s " , & stub , & stub_len ) = = SUCCESS ) {
if ( phar_obj - > archive - > is_persistent & & FAILURE = = phar_copy_on_write ( & ( phar_obj - > archive ) ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar_obj - > archive - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
phar_flush ( phar_obj - > archive , stub , stub_len , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-01-29 06:02:19 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-01-29 06:02:19 +00:00
efree ( error ) ;
}
2008-08-01 13:45:06 +00:00
2007-01-28 22:11:28 +00:00
RETURN_TRUE ;
2007-01-26 14:52:10 +00:00
}
2007-01-28 22:11:28 +00:00
RETURN_FALSE ;
2007-01-26 14:52:10 +00:00
}
/* }}} */
2008-02-20 19:22:12 +00:00
/* {{{ proto bool Phar::setDefaultStub([string index[, string webindex]])
2008-02-25 16:27:23 +00:00
* In a pure phar archive , sets a stub that can be used to run the archive
* regardless of whether the phar extension is available . The first parameter
* is the CLI startup filename , which defaults to " index.php " . The second
* parameter is the web startup filename and also defaults to " index.php "
* ( falling back to CLI behaviour ) .
* Both parameters are optional .
* In a phar . zip or phar . tar archive , the default stub is used only to
* identify the archive to the extension as a Phar object . This allows the
* extension to treat phar . zip and phar . tar types as honorary phars . Since
* files cannot be loaded via this kind of stub , no parameters are accepted
* when the Phar object is zip - or tar - based .
2008-02-20 19:22:12 +00:00
*/
2008-08-01 13:45:06 +00:00
PHP_METHOD ( Phar , setDefaultStub )
2008-02-20 19:22:12 +00:00
{
2015-07-02 17:29:06 +00:00
char * index = NULL , * webindex = NULL , * error = NULL ;
zend_string * stub = NULL ;
2014-08-27 17:25:28 +00:00
size_t index_len = 0 , webindex_len = 0 ;
int created_stub = 0 ;
2008-02-20 19:22:12 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > is_data ) {
if ( phar_obj - > archive - > is_tar ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-08-01 13:45:06 +00:00
" A Phar stub cannot be set in a plain tar archive " ) ;
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-08-01 13:45:06 +00:00
" A Phar stub cannot be set in a plain zip archive " ) ;
}
2008-02-27 21:34:26 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " |s!s " , & index , & index_len , & webindex , & webindex_len ) = = FAILURE ) {
2008-02-20 19:22:12 +00:00
RETURN_FALSE ;
}
2014-05-08 17:18:59 +00:00
if ( ZEND_NUM_ARGS ( ) > 0 & & ( phar_obj - > archive - > is_tar | | phar_obj - > archive - > is_zip ) ) {
2014-12-13 22:06:14 +00:00
php_error_docref ( NULL , E_WARNING , " method accepts no arguments for a tar- or zip-based phar stub, %d given " , ZEND_NUM_ARGS ( ) ) ;
2008-02-20 19:22:12 +00:00
RETURN_FALSE ;
}
if ( PHAR_G ( readonly ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-02-20 19:22:12 +00:00
" Cannot change stub: phar.readonly=1 " ) ;
RETURN_FALSE ;
}
2014-05-08 17:18:59 +00:00
if ( ! phar_obj - > archive - > is_tar & & ! phar_obj - > archive - > is_zip ) {
2015-07-02 17:29:06 +00:00
stub = phar_create_default_stub ( index , webindex , & error ) ;
2008-02-20 19:22:12 +00:00
2008-02-21 00:24:38 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " %s " , error ) ;
2008-02-21 00:24:38 +00:00
efree ( error ) ;
2008-02-23 14:59:52 +00:00
if ( stub ) {
2015-07-02 17:29:06 +00:00
zend_string_free ( stub ) ;
2008-02-23 14:59:52 +00:00
}
2008-02-21 00:24:38 +00:00
RETURN_FALSE ;
}
2008-08-01 13:45:06 +00:00
2008-02-23 14:59:52 +00:00
created_stub = 1 ;
2008-02-20 19:22:12 +00:00
}
2014-12-13 22:06:14 +00:00
if ( phar_obj - > archive - > is_persistent & & FAILURE = = phar_copy_on_write ( & ( phar_obj - > archive ) ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar_obj - > archive - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
2015-07-02 17:29:06 +00:00
phar_flush ( phar_obj - > archive , stub ? ZSTR_VAL ( stub ) : 0 , stub ? ZSTR_LEN ( stub ) : 0 , 1 , & error ) ;
2008-02-24 22:29:06 +00:00
2008-02-23 14:59:52 +00:00
if ( created_stub ) {
2015-07-02 17:29:06 +00:00
zend_string_free ( stub ) ;
2008-02-23 14:59:52 +00:00
}
2008-02-20 19:22:12 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2008-02-20 19:22:12 +00:00
efree ( error ) ;
RETURN_FALSE ;
}
RETURN_TRUE ;
}
/* }}} */
2008-08-01 13:45:06 +00:00
/* {{{ proto array Phar::setSignatureAlgorithm(int sigtype[, string privatekey])
2008-02-25 16:27:23 +00:00
* Sets the signature algorithm for a phar and applies it . The signature
* algorithm must be one of Phar : : MD5 , Phar : : SHA1 , Phar : : SHA256 ,
2008-08-01 13:45:06 +00:00
* Phar : : SHA512 , or Phar : : OPENSSL . Note that zip - based phar archives
* cannot support signatures .
2007-03-26 00:00:56 +00:00
*/
PHP_METHOD ( Phar , setSignatureAlgorithm )
{
2014-08-25 17:24:55 +00:00
zend_long algo ;
2008-08-01 13:45:06 +00:00
char * error , * key = NULL ;
2014-08-27 13:31:48 +00:00
size_t key_len = 0 ;
2008-08-01 13:45:06 +00:00
2007-03-26 00:00:56 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-01-03 04:45:00 +00:00
" Cannot set signature algorithm, phar is read-only " ) ;
2008-01-14 05:12:46 +00:00
return ;
2008-01-03 04:45:00 +00:00
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters_ex ( ZEND_PARSE_PARAMS_QUIET , ZEND_NUM_ARGS ( ) , " l|s " , & algo , & key , & key_len ) ! = SUCCESS ) {
2007-03-26 00:00:56 +00:00
return ;
}
switch ( algo ) {
2008-08-01 13:45:06 +00:00
case PHAR_SIG_SHA256 :
case PHAR_SIG_SHA512 :
2008-10-09 00:51:27 +00:00
# ifndef PHAR_HASH_OK
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-10-09 00:51:27 +00:00
" SHA-256 and SHA-512 signatures are only supported if the hash extension is enabled and built non-shared " ) ;
2008-01-14 05:12:46 +00:00
return ;
2007-03-26 00:00:56 +00:00
# endif
2008-08-01 13:45:06 +00:00
case PHAR_SIG_MD5 :
case PHAR_SIG_SHA1 :
case PHAR_SIG_OPENSSL :
2014-12-13 22:06:14 +00:00
if ( phar_obj - > archive - > is_persistent & & FAILURE = = phar_copy_on_write ( & ( phar_obj - > archive ) ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar_obj - > archive - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > sig_flags = algo ;
phar_obj - > archive - > is_modified = 1 ;
2008-08-01 13:45:06 +00:00
PHAR_G ( openssl_privatekey ) = key ;
PHAR_G ( openssl_privatekey_len ) = key_len ;
2007-03-26 00:00:56 +00:00
2014-12-13 22:06:14 +00:00
phar_flush ( phar_obj - > archive , 0 , 0 , 0 , & error ) ;
2007-03-26 00:00:56 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-03-26 00:00:56 +00:00
efree ( error ) ;
}
2007-03-26 00:29:22 +00:00
break ;
2008-08-01 13:45:06 +00:00
default :
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2007-03-26 00:00:56 +00:00
" Unknown signature algorithm specified " ) ;
}
}
/* }}} */
2007-01-21 15:25:50 +00:00
/* {{{ proto array|false Phar::getSignature()
2008-02-25 16:27:23 +00:00
* Returns a hash signature , or FALSE if the archive is unsigned .
2007-01-21 15:25:50 +00:00
*/
PHP_METHOD ( Phar , getSignature )
{
PHAR_ARCHIVE_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2007-01-21 15:25:50 +00:00
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > signature ) {
2014-08-13 18:18:05 +00:00
zend_string * unknown ;
2008-08-01 13:45:06 +00:00
2007-01-21 15:25:50 +00:00
array_init ( return_value ) ;
2014-05-08 17:18:59 +00:00
add_assoc_stringl ( return_value , " hash " , phar_obj - > archive - > signature , phar_obj - > archive - > sig_len ) ;
switch ( phar_obj - > archive - > sig_flags ) {
2008-08-01 13:45:06 +00:00
case PHAR_SIG_MD5 :
2014-04-15 11:40:40 +00:00
add_assoc_stringl ( return_value , " hash_type " , " MD5 " , 3 ) ;
2008-08-01 13:45:06 +00:00
break ;
case PHAR_SIG_SHA1 :
2014-04-15 11:40:40 +00:00
add_assoc_stringl ( return_value , " hash_type " , " SHA-1 " , 5 ) ;
2008-08-01 13:45:06 +00:00
break ;
case PHAR_SIG_SHA256 :
2014-04-15 11:40:40 +00:00
add_assoc_stringl ( return_value , " hash_type " , " SHA-256 " , 7 ) ;
2008-08-01 13:45:06 +00:00
break ;
case PHAR_SIG_SHA512 :
2014-04-15 11:40:40 +00:00
add_assoc_stringl ( return_value , " hash_type " , " SHA-512 " , 7 ) ;
2008-08-01 13:45:06 +00:00
break ;
case PHAR_SIG_OPENSSL :
2014-04-15 11:40:40 +00:00
add_assoc_stringl ( return_value , " hash_type " , " OpenSSL " , 7 ) ;
2008-08-01 13:45:06 +00:00
break ;
default :
2014-08-13 18:18:05 +00:00
unknown = strpprintf ( 0 , " Unknown (%u) " , phar_obj - > archive - > sig_flags ) ;
add_assoc_str ( return_value , " hash_type " , unknown ) ;
2008-08-01 13:45:06 +00:00
break ;
2007-01-21 15:25:50 +00:00
}
} else {
RETURN_FALSE ;
}
}
/* }}} */
/* {{{ proto bool Phar::getModified()
* Return whether phar was modified
*/
PHP_METHOD ( Phar , getModified )
{
PHAR_ARCHIVE_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2007-01-21 15:25:50 +00:00
2014-05-08 17:18:59 +00:00
RETURN_BOOL ( phar_obj - > archive - > is_modified ) ;
2007-01-21 15:25:50 +00:00
}
/* }}} */
2014-12-13 22:06:14 +00:00
static int phar_set_compression ( zval * zv , void * argument ) /* { { { */
2007-01-27 19:26:58 +00:00
{
2014-05-08 14:30:07 +00:00
phar_entry_info * entry = ( phar_entry_info * ) Z_PTR_P ( zv ) ;
2016-01-28 16:11:53 +00:00
uint32_t compress = * ( uint32_t * ) argument ;
2007-01-27 19:26:58 +00:00
if ( entry - > is_deleted ) {
return ZEND_HASH_APPLY_KEEP ;
}
2008-08-01 13:45:06 +00:00
2007-08-17 04:47:50 +00:00
entry - > old_flags = entry - > flags ;
2007-01-27 19:26:58 +00:00
entry - > flags & = ~ PHAR_ENT_COMPRESSION_MASK ;
entry - > flags | = compress ;
entry - > is_modified = 1 ;
return ZEND_HASH_APPLY_KEEP ;
}
/* }}} */
2014-12-13 22:06:14 +00:00
static int phar_test_compression ( zval * zv , void * argument ) /* { { { */
2007-01-27 19:26:58 +00:00
{
2014-05-08 14:30:07 +00:00
phar_entry_info * entry = ( phar_entry_info * ) Z_PTR_P ( zv ) ;
2007-01-27 19:26:58 +00:00
if ( entry - > is_deleted ) {
return ZEND_HASH_APPLY_KEEP ;
}
2008-08-01 13:45:06 +00:00
2008-05-17 20:07:38 +00:00
if ( ! PHAR_G ( has_bz2 ) ) {
2007-11-24 04:06:44 +00:00
if ( entry - > flags & PHAR_ENT_COMPRESSED_BZ2 ) {
* ( int * ) argument = 0 ;
}
2007-01-27 19:26:58 +00:00
}
2008-08-01 13:45:06 +00:00
2008-05-17 20:07:38 +00:00
if ( ! PHAR_G ( has_zlib ) ) {
2007-11-25 05:04:40 +00:00
if ( entry - > flags & PHAR_ENT_COMPRESSED_GZ ) {
* ( int * ) argument = 0 ;
}
}
2008-08-01 13:45:06 +00:00
2007-01-27 19:26:58 +00:00
return ZEND_HASH_APPLY_KEEP ;
}
/* }}} */
2016-01-28 16:11:53 +00:00
static void pharobj_set_compression ( HashTable * manifest , uint32_t compress ) /* { { { */
2007-01-27 19:26:58 +00:00
{
2014-12-13 22:06:14 +00:00
zend_hash_apply_with_argument ( manifest , phar_set_compression , & compress ) ;
2007-01-27 19:26:58 +00:00
}
/* }}} */
2014-12-13 22:06:14 +00:00
static int pharobj_cancompress ( HashTable * manifest ) /* { { { */
2007-01-27 19:26:58 +00:00
{
int test ;
2008-08-01 13:45:06 +00:00
2007-01-27 19:26:58 +00:00
test = 1 ;
2014-12-13 22:06:14 +00:00
zend_hash_apply_with_argument ( manifest , phar_test_compression , & test ) ;
2007-01-27 19:26:58 +00:00
return test ;
}
/* }}} */
2008-04-11 13:41:59 +00:00
/* {{{ proto object Phar::compress(int method[, string extension])
* Compress a . tar , or . phar . tar with whole - file compression
* The parameter can be one of Phar : : GZ or Phar : : BZ2 to specify
* the kind of compression desired
*/
PHP_METHOD ( Phar , compress )
{
2014-08-25 17:24:55 +00:00
zend_long method ;
2008-04-11 13:41:59 +00:00
char * ext = NULL ;
2014-08-27 17:25:28 +00:00
size_t ext_len = 0 ;
2016-01-28 16:11:53 +00:00
uint32_t flags ;
2014-05-08 14:30:07 +00:00
zend_object * ret ;
2008-04-11 13:41:59 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " l|s " , & method , & ext , & ext_len ) = = FAILURE ) {
2008-04-11 13:41:59 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-04-11 13:41:59 +00:00
" Cannot compress phar archive, phar is read-only " ) ;
return ;
}
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > is_zip ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-04-24 04:05:20 +00:00
" Cannot compress zip-based archives with whole-archive compression " ) ;
2008-04-11 13:41:59 +00:00
return ;
}
switch ( method ) {
case 0 :
flags = PHAR_FILE_COMPRESSED_NONE ;
break ;
case PHAR_ENT_COMPRESSED_GZ :
2008-05-17 20:07:38 +00:00
if ( ! PHAR_G ( has_zlib ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 13:41:59 +00:00
" Cannot compress entire archive with gzip, enable ext/zlib in php.ini " ) ;
return ;
}
flags = PHAR_FILE_COMPRESSED_GZ ;
break ;
2008-08-01 13:45:06 +00:00
2008-04-11 13:41:59 +00:00
case PHAR_ENT_COMPRESSED_BZ2 :
2008-05-17 20:07:38 +00:00
if ( ! PHAR_G ( has_bz2 ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 13:41:59 +00:00
" Cannot compress entire archive with bz2, enable ext/bz2 in php.ini " ) ;
return ;
}
flags = PHAR_FILE_COMPRESSED_BZ2 ;
break ;
default :
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 13:41:59 +00:00
" Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2 " ) ;
return ;
}
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > is_tar ) {
2014-12-13 22:06:14 +00:00
ret = phar_convert_to_other ( phar_obj - > archive , PHAR_FORMAT_TAR , ext , flags ) ;
2008-04-11 13:41:59 +00:00
} else {
2014-12-13 22:06:14 +00:00
ret = phar_convert_to_other ( phar_obj - > archive , PHAR_FORMAT_PHAR , ext , flags ) ;
2008-04-11 13:41:59 +00:00
}
2008-08-01 13:45:06 +00:00
2008-04-11 13:41:59 +00:00
if ( ret ) {
2014-05-08 14:30:07 +00:00
ZVAL_OBJ ( return_value , ret ) ;
2008-04-11 13:41:59 +00:00
} else {
RETURN_NULL ( ) ;
}
}
/* }}} */
/* {{{ proto object Phar::decompress([string extension])
* Decompress a . tar , or . phar . tar with whole - file compression
*/
PHP_METHOD ( Phar , decompress )
{
char * ext = NULL ;
2014-08-27 17:25:28 +00:00
size_t ext_len = 0 ;
2014-05-08 14:30:07 +00:00
zend_object * ret ;
2008-04-11 13:41:59 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " |s " , & ext , & ext_len ) = = FAILURE ) {
2008-04-11 13:41:59 +00:00
return ;
}
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-04-24 04:05:20 +00:00
" Cannot decompress phar archive, phar is read-only " ) ;
2008-04-11 13:41:59 +00:00
return ;
}
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > is_zip ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2008-04-24 04:05:20 +00:00
" Cannot decompress zip-based archives with whole-archive compression " ) ;
2008-04-11 13:41:59 +00:00
return ;
}
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > is_tar ) {
2014-12-13 22:06:14 +00:00
ret = phar_convert_to_other ( phar_obj - > archive , PHAR_FORMAT_TAR , ext , PHAR_FILE_COMPRESSED_NONE ) ;
2008-04-11 13:41:59 +00:00
} else {
2014-12-13 22:06:14 +00:00
ret = phar_convert_to_other ( phar_obj - > archive , PHAR_FORMAT_PHAR , ext , PHAR_FILE_COMPRESSED_NONE ) ;
2008-04-11 13:41:59 +00:00
}
2008-08-01 13:45:06 +00:00
2008-04-11 13:41:59 +00:00
if ( ret ) {
2014-05-08 14:30:07 +00:00
ZVAL_OBJ ( return_value , ret ) ;
2008-04-11 13:41:59 +00:00
} else {
RETURN_NULL ( ) ;
}
}
/* }}} */
2008-04-10 13:40:26 +00:00
/* {{{ proto object Phar::compressFiles(int method)
* Compress all files within a phar or zip archive using the specified compression
* The parameter can be one of Phar : : GZ or Phar : : BZ2 to specify
* the kind of compression desired
2007-01-27 19:26:58 +00:00
*/
2008-04-10 13:40:26 +00:00
PHP_METHOD ( Phar , compressFiles )
2007-01-27 19:26:58 +00:00
{
2007-01-29 06:02:19 +00:00
char * error ;
2016-01-28 16:11:53 +00:00
uint32_t flags ;
2014-08-25 17:24:55 +00:00
zend_long method ;
2007-01-27 19:26:58 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " l " , & method ) = = FAILURE ) {
2008-01-14 05:12:46 +00:00
return ;
2007-01-27 19:26:58 +00:00
}
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2007-01-27 19:26:58 +00:00
" Phar is readonly, cannot change compression " ) ;
2008-01-14 05:12:46 +00:00
return ;
2007-01-27 19:26:58 +00:00
}
2008-04-10 13:40:26 +00:00
switch ( method ) {
case PHAR_ENT_COMPRESSED_GZ :
2008-05-17 20:07:38 +00:00
if ( ! PHAR_G ( has_zlib ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-10 13:40:26 +00:00
" Cannot compress files within archive with gzip, enable ext/zlib in php.ini " ) ;
return ;
}
flags = PHAR_ENT_COMPRESSED_GZ ;
break ;
case PHAR_ENT_COMPRESSED_BZ2 :
2008-05-17 20:07:38 +00:00
if ( ! PHAR_G ( has_bz2 ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-10 13:40:26 +00:00
" Cannot compress files within archive with bz2, enable ext/bz2 in php.ini " ) ;
return ;
}
flags = PHAR_ENT_COMPRESSED_BZ2 ;
break ;
default :
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-10 13:40:26 +00:00
" Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2 " ) ;
return ;
2007-11-24 04:06:44 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > is_tar ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-10 13:40:26 +00:00
" Cannot compress with Gzip compression, tar archives cannot compress individual files, use compress() to compress the whole archive " ) ;
2008-03-23 22:42:45 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( ! pharobj_cancompress ( & phar_obj - > archive - > manifest ) ) {
2008-04-10 13:40:26 +00:00
if ( flags = = PHAR_FILE_COMPRESSED_GZ ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-10 13:40:26 +00:00
" Cannot compress all files as Gzip, some are compressed as bzip2 and cannot be decompressed " ) ;
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-10 13:40:26 +00:00
" Cannot compress all files as Bzip2, some are compressed as gzip and cannot be decompressed " ) ;
}
2008-01-14 05:12:46 +00:00
return ;
2007-01-27 19:26:58 +00:00
}
2008-03-09 04:46:16 +00:00
2014-12-13 22:06:14 +00:00
if ( phar_obj - > archive - > is_persistent & & FAILURE = = phar_copy_on_write ( & ( phar_obj - > archive ) ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar_obj - > archive - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
pharobj_set_compression ( & phar_obj - > archive - > manifest , flags ) ;
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > is_modified = 1 ;
2014-12-13 22:06:14 +00:00
phar_flush ( phar_obj - > archive , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-01-29 06:02:19 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " %s " , error ) ;
2007-01-29 06:02:19 +00:00
efree ( error ) ;
}
2007-01-27 19:26:58 +00:00
}
/* }}} */
2008-04-10 13:40:26 +00:00
/* {{{ proto bool Phar::decompressFiles()
* decompress every file
2007-01-27 19:26:58 +00:00
*/
2008-04-10 13:40:26 +00:00
PHP_METHOD ( Phar , decompressFiles )
2007-01-27 19:26:58 +00:00
{
2007-01-29 06:02:19 +00:00
char * error ;
2007-01-27 19:26:58 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2007-01-27 19:26:58 +00:00
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2007-01-27 19:26:58 +00:00
" Phar is readonly, cannot change compression " ) ;
2008-01-14 05:12:46 +00:00
return ;
2007-01-27 19:26:58 +00:00
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( ! pharobj_cancompress ( & phar_obj - > archive - > manifest ) ) {
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-10 13:40:26 +00:00
" Cannot decompress all files, some are compressed as bzip2 or gzip and cannot be decompressed " ) ;
2008-01-14 05:12:46 +00:00
return ;
2007-01-27 19:26:58 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > is_tar ) {
2008-03-23 22:42:45 +00:00
RETURN_TRUE ;
2008-01-21 05:28:09 +00:00
} else {
2014-12-13 22:06:14 +00:00
if ( phar_obj - > archive - > is_persistent & & FAILURE = = phar_copy_on_write ( & ( phar_obj - > archive ) ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar_obj - > archive - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
pharobj_set_compression ( & phar_obj - > archive - > manifest , PHAR_ENT_COMPRESSED_NONE ) ;
2008-01-21 05:28:09 +00:00
}
2008-03-09 04:46:16 +00:00
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > is_modified = 1 ;
2014-12-13 22:06:14 +00:00
phar_flush ( phar_obj - > archive , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-01-29 06:02:19 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " %s " , error ) ;
2007-01-29 06:02:19 +00:00
efree ( error ) ;
}
2008-08-01 13:45:06 +00:00
2008-03-23 22:42:45 +00:00
RETURN_TRUE ;
2007-01-27 19:26:58 +00:00
}
/* }}} */
2007-12-16 00:57:39 +00:00
/* {{{ proto bool Phar::copy(string oldfile, string newfile)
* copy a file internal to the phar archive to another new file within the phar
*/
PHP_METHOD ( Phar , copy )
{
char * oldfile , * newfile , * error ;
const char * pcr_error ;
2014-08-27 17:25:28 +00:00
size_t oldfile_len , newfile_len ;
2007-12-16 00:57:39 +00:00
phar_entry_info * oldentry , newentry = { 0 } , * temp ;
2014-08-28 15:02:53 +00:00
int tmp_len = 0 ;
2008-08-01 13:45:06 +00:00
2007-12-16 00:57:39 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " pp " , & oldfile , & oldfile_len , & newfile , & newfile_len ) = = FAILURE ) {
2007-12-16 00:57:39 +00:00
return ;
}
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2007-12-16 00:57:39 +00:00
" Cannot copy \" %s \" to \" %s \" , phar is read-only " , oldfile , newfile ) ;
RETURN_FALSE ;
}
2008-05-15 16:09:21 +00:00
if ( oldfile_len > = sizeof ( " .phar " ) - 1 & & ! memcmp ( oldfile , " .phar " , sizeof ( " .phar " ) - 1 ) ) {
/* can't copy a meta file */
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2014-05-08 17:18:59 +00:00
" file \" %s \" cannot be copied to file \" %s \" , cannot copy Phar meta-file in %s " , oldfile , newfile , phar_obj - > archive - > fname ) ;
2008-05-15 16:09:21 +00:00
RETURN_FALSE ;
}
if ( newfile_len > = sizeof ( " .phar " ) - 1 & & ! memcmp ( newfile , " .phar " , sizeof ( " .phar " ) - 1 ) ) {
/* can't copy a meta file */
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2014-05-08 17:18:59 +00:00
" file \" %s \" cannot be copied to file \" %s \" , cannot copy to Phar meta-file in %s " , oldfile , newfile , phar_obj - > archive - > fname ) ;
2008-05-15 16:09:21 +00:00
RETURN_FALSE ;
}
2014-05-08 17:18:59 +00:00
if ( ! zend_hash_str_exists ( & phar_obj - > archive - > manifest , oldfile , ( uint ) oldfile_len ) | | NULL = = ( oldentry = zend_hash_str_find_ptr ( & phar_obj - > archive - > manifest , oldfile , ( uint ) oldfile_len ) ) | | oldentry - > is_deleted ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2014-05-08 17:18:59 +00:00
" file \" %s \" cannot be copied to file \" %s \" , file does not exist in %s " , oldfile , newfile , phar_obj - > archive - > fname ) ;
2007-12-16 00:57:39 +00:00
RETURN_FALSE ;
}
2014-05-08 17:18:59 +00:00
if ( zend_hash_str_exists ( & phar_obj - > archive - > manifest , newfile , ( uint ) newfile_len ) ) {
if ( NULL ! = ( temp = zend_hash_str_find_ptr ( & phar_obj - > archive - > manifest , newfile , ( uint ) newfile_len ) ) | | ! temp - > is_deleted ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2014-05-08 17:18:59 +00:00
" file \" %s \" cannot be copied to file \" %s \" , file must not already exist in phar %s " , oldfile , newfile , phar_obj - > archive - > fname ) ;
2007-12-16 00:57:39 +00:00
RETURN_FALSE ;
}
}
2014-08-28 15:35:54 +00:00
tmp_len = ( int ) newfile_len ;
2014-08-28 15:02:53 +00:00
if ( phar_path_check ( & newfile , & tmp_len , & pcr_error ) > pcr_is_ok ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 ,
2014-05-08 17:18:59 +00:00
" file \" %s \" contains invalid characters %s, cannot be copied from \" %s \" in phar %s " , newfile , pcr_error , oldfile , phar_obj - > archive - > fname ) ;
2007-12-16 00:57:39 +00:00
RETURN_FALSE ;
}
2014-08-28 15:02:53 +00:00
newfile_len = tmp_len ;
2007-12-16 00:57:39 +00:00
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > is_persistent ) {
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_copy_on_write ( & ( phar_obj - > archive ) ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar_obj - > archive - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
/* re-populate with copied-on-write entry */
2014-05-08 17:18:59 +00:00
oldentry = zend_hash_str_find_ptr ( & phar_obj - > archive - > manifest , oldfile , ( uint ) oldfile_len ) ;
2008-10-12 06:53:52 +00:00
}
2007-12-16 00:57:39 +00:00
memcpy ( ( void * ) & newentry , oldentry , sizeof ( phar_entry_info ) ) ;
2008-08-01 13:45:06 +00:00
2014-05-08 14:30:07 +00:00
if ( Z_TYPE ( newentry . metadata ) ! = IS_UNDEF ) {
zval_copy_ctor ( & newentry . metadata ) ;
newentry . metadata_str . s = NULL ;
2007-12-16 00:57:39 +00:00
}
2008-08-01 13:45:06 +00:00
2007-12-16 06:31:00 +00:00
newentry . filename = estrndup ( newfile , newfile_len ) ;
newentry . filename_len = newfile_len ;
2008-01-08 07:08:46 +00:00
newentry . fp_refcount = 0 ;
2008-01-28 08:52:08 +00:00
if ( oldentry - > fp_type ! = PHAR_FP ) {
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_copy_entry_fp ( oldentry , & newentry , & error ) ) {
2008-01-28 08:52:08 +00:00
efree ( newentry . filename ) ;
php_stream_close ( newentry . fp ) ;
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2008-01-28 08:52:08 +00:00
efree ( error ) ;
return ;
}
}
2014-05-08 14:30:07 +00:00
zend_hash_str_add_mem ( & oldentry - > phar - > manifest , newfile , newfile_len , & newentry , sizeof ( phar_entry_info ) ) ;
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > is_modified = 1 ;
2014-12-13 22:06:14 +00:00
phar_flush ( phar_obj - > archive , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-12-16 00:57:39 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-12-16 00:57:39 +00:00
efree ( error ) ;
}
2007-12-31 22:42:40 +00:00
2007-12-16 00:57:39 +00:00
RETURN_TRUE ;
}
2008-02-08 09:25:02 +00:00
/* }}} */
2007-12-16 00:57:39 +00:00
2008-02-20 17:58:56 +00:00
/* {{{ proto int Phar::offsetExists(string entry)
2007-01-21 15:25:50 +00:00
* determines whether a file exists in the phar
*/
PHP_METHOD ( Phar , offsetExists )
{
char * fname ;
2014-08-27 17:25:28 +00:00
size_t fname_len ;
2007-01-21 15:25:50 +00:00
phar_entry_info * entry ;
2008-08-01 13:45:06 +00:00
2007-01-21 15:25:50 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " p " , & fname , & fname_len ) = = FAILURE ) {
2007-01-21 15:25:50 +00:00
return ;
}
2014-05-08 17:18:59 +00:00
if ( zend_hash_str_exists ( & phar_obj - > archive - > manifest , fname , ( uint ) fname_len ) ) {
if ( NULL ! = ( entry = zend_hash_str_find_ptr ( & phar_obj - > archive - > manifest , fname , ( uint ) fname_len ) ) ) {
2007-01-21 15:25:50 +00:00
if ( entry - > is_deleted ) {
/* entry is deleted, but has not been flushed to disk yet */
RETURN_FALSE ;
}
}
2008-05-15 16:09:21 +00:00
if ( fname_len > = sizeof ( " .phar " ) - 1 & & ! memcmp ( fname , " .phar " , sizeof ( " .phar " ) - 1 ) ) {
/* none of these are real files, so they don't exist */
RETURN_FALSE ;
}
2007-01-21 15:25:50 +00:00
RETURN_TRUE ;
} else {
2014-05-08 17:18:59 +00:00
if ( zend_hash_str_exists ( & phar_obj - > archive - > virtual_dirs , fname , ( uint ) fname_len ) ) {
2009-06-05 04:44:55 +00:00
RETURN_TRUE ;
}
2007-01-21 15:25:50 +00:00
RETURN_FALSE ;
}
}
/* }}} */
2008-02-20 17:58:56 +00:00
/* {{{ proto int Phar::offsetGet(string entry)
2007-01-21 15:25:50 +00:00
* get a PharFileInfo object for a specific file
*/
PHP_METHOD ( Phar , offsetGet )
{
2007-02-04 20:10:03 +00:00
char * fname , * error ;
2014-08-27 17:25:28 +00:00
size_t fname_len ;
2014-05-08 14:30:07 +00:00
zval zfname ;
2008-04-25 04:34:03 +00:00
phar_entry_info * entry ;
2014-08-13 18:18:05 +00:00
zend_string * sfname ;
2014-08-14 19:31:01 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2007-01-21 15:25:50 +00:00
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " p " , & fname , & fname_len ) = = FAILURE ) {
2007-01-21 15:25:50 +00:00
return ;
}
2008-05-15 16:09:21 +00:00
/* security is 0 here so that we can get a better error message than "entry doesn't exist" */
2014-12-13 22:06:14 +00:00
if ( ! ( entry = phar_get_entry_info_dir ( phar_obj - > archive , fname , fname_len , 1 , & error , 0 ) ) ) {
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Entry %s does not exist%s%s " , fname , error ? " , " : " " , error ? error : " " ) ;
2007-01-21 15:25:50 +00:00
} else {
2008-05-15 16:09:21 +00:00
if ( fname_len = = sizeof ( " .phar/stub.php " ) - 1 & & ! memcmp ( fname , " .phar/stub.php " , sizeof ( " .phar/stub.php " ) - 1 ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Cannot get stub \" .phar/stub.php \" directly in phar \" %s \" , use getStub " , phar_obj - > archive - > fname ) ;
2008-05-15 16:09:21 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2008-05-15 16:09:21 +00:00
if ( fname_len = = sizeof ( " .phar/alias.txt " ) - 1 & & ! memcmp ( fname , " .phar/alias.txt " , sizeof ( " .phar/alias.txt " ) - 1 ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Cannot get alias \" .phar/alias.txt \" directly in phar \" %s \" , use getAlias " , phar_obj - > archive - > fname ) ;
2008-05-15 16:09:21 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2008-05-15 16:09:21 +00:00
if ( fname_len > = sizeof ( " .phar " ) - 1 & & ! memcmp ( fname , " .phar " , sizeof ( " .phar " ) - 1 ) ) {
2016-06-21 13:00:37 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Cannot directly get any files or directories in magic \" .phar \" directory " ) ;
2008-05-15 16:09:21 +00:00
return ;
}
2008-04-25 04:34:03 +00:00
if ( entry - > is_temp_dir ) {
efree ( entry - > filename ) ;
efree ( entry ) ;
}
2008-08-01 13:45:06 +00:00
2014-08-13 18:18:05 +00:00
sfname = strpprintf ( 0 , " phar://%s/%s " , phar_obj - > archive - > fname , fname ) ;
2014-09-19 11:41:01 +00:00
ZVAL_NEW_STR ( & zfname , sfname ) ;
2014-12-13 22:06:14 +00:00
spl_instantiate_arg_ex1 ( phar_obj - > spl . info_class , return_value , & zfname ) ;
2007-01-21 15:25:50 +00:00
zval_ptr_dtor ( & zfname ) ;
}
}
/* }}} */
2008-04-09 19:23:31 +00:00
/* {{{ add a file within the phar archive from a string or resource
2007-01-21 15:25:50 +00:00
*/
2014-12-13 22:06:14 +00:00
static void phar_add_file ( phar_archive_data * * pphar , char * filename , int filename_len , char * cont_str , size_t cont_len , zval * zresource )
2007-01-21 15:25:50 +00:00
{
2008-04-09 19:23:31 +00:00
char * error ;
2009-04-29 03:24:09 +00:00
size_t contents_len ;
2007-01-21 15:25:50 +00:00
phar_entry_data * data ;
2007-01-27 15:31:24 +00:00
php_stream * contents_file ;
2007-01-21 15:25:50 +00:00
2016-06-21 21:40:50 +00:00
if ( filename_len > = ( int ) sizeof ( " .phar " ) - 1 & & ! memcmp ( filename , " .phar " , sizeof ( " .phar " ) - 1 ) & & ( filename [ 5 ] = = ' / ' | | filename [ 5 ] = = ' \\ ' | | filename [ 5 ] = = ' \0 ' ) ) {
2016-06-21 13:00:37 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Cannot create any files in magic \" .phar \" directory " ) ;
2008-05-15 16:09:21 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
if ( ! ( data = phar_get_or_create_entry_data ( ( * pphar ) - > fname , ( * pphar ) - > fname_len , filename , filename_len , " w+b " , 0 , & error , 1 ) ) ) {
2007-01-27 21:23:02 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Entry %s does not exist and cannot be created: %s " , filename , error ) ;
2007-01-27 21:23:02 +00:00
efree ( error ) ;
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Entry %s does not exist and cannot be created " , filename ) ;
2007-01-27 21:23:02 +00:00
}
2008-01-14 05:12:46 +00:00
return ;
2007-01-21 15:25:50 +00:00
} else {
2007-01-28 03:25:53 +00:00
if ( error ) {
efree ( error ) ;
}
2008-08-01 13:45:06 +00:00
2008-01-09 00:58:37 +00:00
if ( ! data - > internal_file - > is_dir ) {
if ( cont_str ) {
contents_len = php_stream_write ( data - > fp , cont_str , cont_len ) ;
if ( contents_len ! = cont_len ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Entry %s could not be written to " , filename ) ;
2008-01-14 05:12:46 +00:00
return ;
2008-01-09 00:58:37 +00:00
}
} else {
2014-05-08 14:30:07 +00:00
if ( ! ( php_stream_from_zval_no_verify ( contents_file , zresource ) ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Entry %s could not be written to " , filename ) ;
2008-01-14 05:12:46 +00:00
return ;
2008-01-09 00:58:37 +00:00
}
2013-09-13 19:40:32 +00:00
php_stream_copy_to_stream_ex ( contents_file , data - > fp , PHP_STREAM_COPY_ALL , & contents_len ) ;
2007-01-26 14:52:10 +00:00
}
2008-08-01 13:45:06 +00:00
2008-01-09 00:58:37 +00:00
data - > internal_file - > compressed_filesize = data - > internal_file - > uncompressed_filesize = contents_len ;
2007-01-21 15:25:50 +00:00
}
2008-08-01 13:45:06 +00:00
2008-10-12 06:53:52 +00:00
/* check for copy-on-write */
if ( pphar [ 0 ] ! = data - > phar ) {
* pphar = data - > phar ;
}
2014-12-13 22:06:14 +00:00
phar_entry_delref ( data ) ;
phar_flush ( * pphar , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-01-29 06:02:19 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-01-29 06:02:19 +00:00
efree ( error ) ;
}
2007-01-21 15:25:50 +00:00
}
}
/* }}} */
2008-04-09 19:23:31 +00:00
/* {{{ create a directory within the phar archive
*/
2014-12-13 22:06:14 +00:00
static void phar_mkdir ( phar_archive_data * * pphar , char * dirname , int dirname_len )
2008-04-09 19:23:31 +00:00
{
char * error ;
phar_entry_data * data ;
2014-12-13 22:06:14 +00:00
if ( ! ( data = phar_get_or_create_entry_data ( ( * pphar ) - > fname , ( * pphar ) - > fname_len , dirname , dirname_len , " w+b " , 2 , & error , 1 ) ) ) {
2008-04-09 19:23:31 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Directory %s does not exist and cannot be created: %s " , dirname , error ) ;
2008-04-09 19:23:31 +00:00
efree ( error ) ;
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Directory %s does not exist and cannot be created " , dirname ) ;
2008-04-09 19:23:31 +00:00
}
2008-08-01 13:45:06 +00:00
2008-04-09 19:23:31 +00:00
return ;
} else {
if ( error ) {
efree ( error ) ;
}
2008-08-01 13:45:06 +00:00
2008-10-12 06:53:52 +00:00
/* check for copy on write */
if ( data - > phar ! = * pphar ) {
* pphar = data - > phar ;
}
2014-12-13 22:06:14 +00:00
phar_entry_delref ( data ) ;
phar_flush ( * pphar , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2008-04-09 19:23:31 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2008-04-09 19:23:31 +00:00
efree ( error ) ;
}
}
}
/* }}} */
/* {{{ proto int Phar::offsetSet(string entry, string value)
* set the contents of an internal file to those of an external file
*/
PHP_METHOD ( Phar , offsetSet )
{
char * fname , * cont_str = NULL ;
2014-08-27 17:25:28 +00:00
size_t fname_len , cont_len ;
2008-04-09 19:23:31 +00:00
zval * zresource ;
PHAR_ARCHIVE_OBJECT ( ) ;
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Write operations disabled by the php.ini setting phar.readonly " ) ;
2008-04-09 19:23:31 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters_ex ( ZEND_PARSE_PARAMS_QUIET , ZEND_NUM_ARGS ( ) , " pr " , & fname , & fname_len , & zresource ) = = FAILURE
& & zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " ps " , & fname , & fname_len , & cont_str , & cont_len ) = = FAILURE ) {
2008-04-09 19:23:31 +00:00
return ;
}
2008-05-15 16:09:21 +00:00
if ( fname_len = = sizeof ( " .phar/stub.php " ) - 1 & & ! memcmp ( fname , " .phar/stub.php " , sizeof ( " .phar/stub.php " ) - 1 ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Cannot set stub \" .phar/stub.php \" directly in phar \" %s \" , use setStub " , phar_obj - > archive - > fname ) ;
2008-04-09 19:23:31 +00:00
return ;
}
2008-05-15 16:09:21 +00:00
if ( fname_len = = sizeof ( " .phar/alias.txt " ) - 1 & & ! memcmp ( fname , " .phar/alias.txt " , sizeof ( " .phar/alias.txt " ) - 1 ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Cannot set alias \" .phar/alias.txt \" directly in phar \" %s \" , use setAlias " , phar_obj - > archive - > fname ) ;
2008-04-09 19:23:31 +00:00
return ;
}
2008-05-15 16:09:21 +00:00
if ( fname_len > = sizeof ( " .phar " ) - 1 & & ! memcmp ( fname , " .phar " , sizeof ( " .phar " ) - 1 ) ) {
2016-06-21 13:00:37 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Cannot set any files or directories in magic \" .phar \" directory " ) ;
2008-05-15 16:09:21 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
phar_add_file ( & ( phar_obj - > archive ) , fname , fname_len , cont_str , cont_len , zresource ) ;
2008-04-09 19:23:31 +00:00
}
/* }}} */
2008-02-20 17:58:56 +00:00
/* {{{ proto int Phar::offsetUnset(string entry)
2007-01-21 15:25:50 +00:00
* remove a file from a phar
*/
PHP_METHOD ( Phar , offsetUnset )
{
2007-01-29 06:02:19 +00:00
char * fname , * error ;
2014-08-27 17:25:28 +00:00
size_t fname_len ;
2007-01-21 15:25:50 +00:00
phar_entry_info * entry ;
PHAR_ARCHIVE_OBJECT ( ) ;
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Write operations disabled by the php.ini setting phar.readonly " ) ;
2007-01-21 15:25:50 +00:00
return ;
}
2008-04-09 19:23:31 +00:00
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " p " , & fname , & fname_len ) = = FAILURE ) {
2007-01-21 15:25:50 +00:00
return ;
}
2014-05-08 17:18:59 +00:00
if ( zend_hash_str_exists ( & phar_obj - > archive - > manifest , fname , ( uint ) fname_len ) ) {
if ( NULL ! = ( entry = zend_hash_str_find_ptr ( & phar_obj - > archive - > manifest , fname , ( uint ) fname_len ) ) ) {
2007-01-21 15:25:50 +00:00
if ( entry - > is_deleted ) {
/* entry is deleted, but has not been flushed to disk yet */
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > is_persistent ) {
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_copy_on_write ( & ( phar_obj - > archive ) ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar_obj - > archive - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
/* re-populate entry after copy on write */
2014-05-08 17:18:59 +00:00
entry = zend_hash_str_find_ptr ( & phar_obj - > archive - > manifest , fname , ( uint ) fname_len ) ;
2008-10-12 06:53:52 +00:00
}
2007-01-21 15:25:50 +00:00
entry - > is_modified = 0 ;
entry - > is_deleted = 1 ;
/* we need to "flush" the stream to save the newly deleted file on disk */
2014-12-13 22:06:14 +00:00
phar_flush ( phar_obj - > archive , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-01-29 06:02:19 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-01-29 06:02:19 +00:00
efree ( error ) ;
}
2008-08-01 13:45:06 +00:00
2007-01-21 15:25:50 +00:00
RETURN_TRUE ;
}
} else {
RETURN_FALSE ;
}
}
/* }}} */
2008-04-09 19:23:31 +00:00
/* {{{ proto string Phar::addEmptyDir(string dirname)
* Adds an empty directory to the phar archive
*/
PHP_METHOD ( Phar , addEmptyDir )
{
char * dirname ;
2014-08-27 17:25:28 +00:00
size_t dirname_len ;
2008-08-01 13:45:06 +00:00
2008-04-09 19:23:31 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " p " , & dirname , & dirname_len ) = = FAILURE ) {
2008-04-09 19:23:31 +00:00
return ;
}
2008-05-15 16:09:21 +00:00
if ( dirname_len > = sizeof ( " .phar " ) - 1 & & ! memcmp ( dirname , " .phar " , sizeof ( " .phar " ) - 1 ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Cannot create a directory in magic \" .phar \" directory " ) ;
2008-05-15 16:09:21 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
phar_mkdir ( & phar_obj - > archive , dirname , dirname_len ) ;
2008-04-09 19:23:31 +00:00
}
/* }}} */
/* {{{ proto string Phar::addFile(string filename[, string localname])
* Adds a file to the archive using the filename , or the second parameter as the name within the archive
*/
PHP_METHOD ( Phar , addFile )
{
char * fname , * localname = NULL ;
2014-08-27 17:25:28 +00:00
size_t fname_len , localname_len = 0 ;
2008-04-09 19:23:31 +00:00
php_stream * resource ;
2014-05-08 14:30:07 +00:00
zval zresource ;
2008-08-01 13:45:06 +00:00
2008-04-09 19:23:31 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " p|s " , & fname , & fname_len , & localname , & localname_len ) = = FAILURE ) {
2008-04-09 19:23:31 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
if ( ! strstr ( fname , " :// " ) & & php_check_open_basedir ( fname ) ) {
zend_throw_exception_ex ( spl_ce_RuntimeException , 0 , " phar error: unable to open file \" %s \" to add to phar archive, open_basedir restrictions prevent this " , fname ) ;
2008-04-27 23:59:41 +00:00
return ;
}
2008-04-09 19:23:31 +00:00
if ( ! ( resource = php_stream_open_wrapper ( fname , " rb " , 0 , NULL ) ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_RuntimeException , 0 , " phar error: unable to open file \" %s \" to add to phar archive " , fname ) ;
2008-04-09 19:23:31 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2008-04-09 19:23:31 +00:00
if ( localname ) {
fname = localname ;
fname_len = localname_len ;
}
2014-05-08 14:30:07 +00:00
php_stream_to_zval ( resource , & zresource ) ;
2014-12-13 22:06:14 +00:00
phar_add_file ( & ( phar_obj - > archive ) , fname , fname_len , NULL , 0 , & zresource ) ;
2014-05-08 14:30:07 +00:00
zval_ptr_dtor ( & zresource ) ;
2008-04-09 19:23:31 +00:00
}
/* }}} */
/* {{{ proto string Phar::addFromString(string localname, string contents)
* Adds a file to the archive using its contents as a string
*/
PHP_METHOD ( Phar , addFromString )
{
char * localname , * cont_str ;
2014-08-27 13:31:48 +00:00
size_t localname_len , cont_len ;
2008-08-01 13:45:06 +00:00
2008-04-09 19:23:31 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " ps " , & localname , & localname_len , & cont_str , & cont_len ) = = FAILURE ) {
2008-04-09 19:23:31 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
phar_add_file ( & ( phar_obj - > archive ) , localname , localname_len , cont_str , cont_len , NULL ) ;
2008-04-09 19:23:31 +00:00
}
/* }}} */
2007-01-22 00:17:14 +00:00
/* {{{ proto string Phar::getStub()
2008-02-25 16:27:23 +00:00
* Returns the stub at the head of a phar archive as a string .
2007-01-22 00:17:14 +00:00
*/
PHP_METHOD ( Phar , getStub )
{
2007-02-05 20:34:23 +00:00
size_t len ;
2014-08-13 18:18:05 +00:00
zend_string * buf ;
2007-01-22 00:59:02 +00:00
php_stream * fp ;
2008-01-28 08:52:08 +00:00
php_stream_filter * filter = NULL ;
phar_entry_info * stub ;
2008-08-01 13:45:06 +00:00
2007-01-22 00:17:14 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2007-01-22 00:17:14 +00:00
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > is_tar | | phar_obj - > archive - > is_zip ) {
2008-01-03 04:45:00 +00:00
2014-05-08 17:18:59 +00:00
if ( NULL ! = ( stub = zend_hash_str_find_ptr ( & ( phar_obj - > archive - > manifest ) , " .phar/stub.php " , sizeof ( " .phar/stub.php " ) - 1 ) ) ) {
if ( phar_obj - > archive - > fp & & ! phar_obj - > archive - > is_brandnew & & ! ( stub - > flags & PHAR_ENT_COMPRESSION_MASK ) ) {
fp = phar_obj - > archive - > fp ;
2008-01-03 04:45:00 +00:00
} else {
2014-05-08 17:18:59 +00:00
if ( ! ( fp = php_stream_open_wrapper ( phar_obj - > archive - > fname , " rb " , 0 , NULL ) ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " phar error: unable to open phar \" %s \" " , phar_obj - > archive - > fname ) ;
2011-08-09 14:11:56 +00:00
return ;
}
2008-01-28 08:52:08 +00:00
if ( stub - > flags & PHAR_ENT_COMPRESSION_MASK ) {
char * filter_name ;
if ( ( filter_name = phar_decompress_filter ( stub , 0 ) ) ! = NULL ) {
2014-12-13 22:06:14 +00:00
filter = php_stream_filter_create ( filter_name , NULL , php_stream_is_persistent ( fp ) ) ;
2008-01-28 08:52:08 +00:00
} else {
filter = NULL ;
}
if ( ! filter ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_UnexpectedValueException , 0 , " phar error: unable to read stub of phar \" %s \" (cannot create %s filter) " , phar_obj - > archive - > fname , phar_decompress_filter ( stub , 1 ) ) ;
2008-01-28 08:52:08 +00:00
return ;
}
php_stream_filter_append ( & fp - > readfilters , filter ) ;
}
2008-01-03 04:45:00 +00:00
}
if ( ! fp ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_RuntimeException , 0 ,
2008-01-03 04:45:00 +00:00
" Unable to read stub " ) ;
return ;
}
2008-01-28 08:52:08 +00:00
php_stream_seek ( fp , stub - > offset_abs , SEEK_SET ) ;
2008-01-03 04:45:00 +00:00
len = stub - > uncompressed_filesize ;
goto carry_on ;
} else {
2014-05-08 14:30:07 +00:00
RETURN_EMPTY_STRING ( ) ;
2008-01-03 04:45:00 +00:00
}
}
2014-05-08 17:18:59 +00:00
len = phar_obj - > archive - > halt_offset ;
2007-01-22 00:59:02 +00:00
2014-05-08 17:18:59 +00:00
if ( phar_obj - > archive - > fp & & ! phar_obj - > archive - > is_brandnew ) {
fp = phar_obj - > archive - > fp ;
2007-01-28 21:26:54 +00:00
} else {
2014-05-08 17:18:59 +00:00
fp = php_stream_open_wrapper ( phar_obj - > archive - > fname , " rb " , 0 , NULL ) ;
2007-01-22 00:59:02 +00:00
}
2007-01-22 00:17:14 +00:00
2007-01-22 00:59:02 +00:00
if ( ! fp ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_RuntimeException , 0 ,
2007-01-22 00:17:14 +00:00
" Unable to read stub " ) ;
return ;
}
2007-01-22 00:59:02 +00:00
php_stream_rewind ( fp ) ;
2008-01-03 04:45:00 +00:00
carry_on :
2014-08-25 17:24:55 +00:00
buf = zend_string_alloc ( len , 0 ) ;
2008-08-01 13:45:06 +00:00
2015-06-30 01:05:24 +00:00
if ( len ! = php_stream_read ( fp , ZSTR_VAL ( buf ) , len ) ) {
2014-05-08 17:18:59 +00:00
if ( fp ! = phar_obj - > archive - > fp ) {
2007-01-22 00:59:02 +00:00
php_stream_close ( fp ) ;
}
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_RuntimeException , 0 ,
2007-01-22 00:17:14 +00:00
" Unable to read stub " ) ;
2014-08-25 17:24:55 +00:00
zend_string_release ( buf ) ;
2007-01-22 00:17:14 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2008-01-28 08:52:08 +00:00
if ( filter ) {
php_stream_filter_flush ( filter , 1 ) ;
2014-12-13 22:06:14 +00:00
php_stream_filter_remove ( filter , 1 ) ;
2008-01-28 08:52:08 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( fp ! = phar_obj - > archive - > fp ) {
2007-01-22 00:59:02 +00:00
php_stream_close ( fp ) ;
}
2007-12-31 22:42:40 +00:00
2015-06-30 01:05:24 +00:00
ZSTR_VAL ( buf ) [ len ] = ' \0 ' ;
ZSTR_LEN ( buf ) = len ;
2014-08-13 18:18:05 +00:00
RETVAL_STR ( buf ) ;
2007-01-22 00:17:14 +00:00
}
/* }}}*/
2007-05-15 20:42:38 +00:00
/* {{{ proto int Phar::hasMetaData()
2008-02-25 16:27:23 +00:00
* Returns TRUE if the phar has global metadata , FALSE otherwise .
2007-05-15 20:42:38 +00:00
*/
PHP_METHOD ( Phar , hasMetadata )
{
PHAR_ARCHIVE_OBJECT ( ) ;
2014-05-08 17:18:59 +00:00
RETURN_BOOL ( Z_TYPE ( phar_obj - > archive - > metadata ) ! = IS_UNDEF ) ;
2007-05-15 20:42:38 +00:00
}
/* }}} */
2007-01-29 03:59:55 +00:00
/* {{{ proto int Phar::getMetaData()
2007-05-15 20:42:38 +00:00
* Returns the global metadata of the phar
2007-01-29 03:59:55 +00:00
*/
PHP_METHOD ( Phar , getMetadata )
{
PHAR_ARCHIVE_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2007-01-29 03:59:55 +00:00
2014-05-08 17:18:59 +00:00
if ( Z_TYPE ( phar_obj - > archive - > metadata ) ! = IS_UNDEF ) {
if ( phar_obj - > archive - > is_persistent ) {
char * buf = estrndup ( ( char * ) Z_PTR ( phar_obj - > archive - > metadata ) , phar_obj - > archive - > metadata_len ) ;
2008-10-12 19:40:30 +00:00
/* assume success, we would have failed before */
2015-06-12 10:33:14 +00:00
phar_parse_metadata ( & buf , return_value , phar_obj - > archive - > metadata_len ) ;
2008-10-12 19:40:30 +00:00
efree ( buf ) ;
2015-06-12 10:33:14 +00:00
} else {
ZVAL_COPY ( return_value , & phar_obj - > archive - > metadata ) ;
2008-10-12 19:40:30 +00:00
}
2007-01-29 03:59:55 +00:00
}
}
/* }}} */
/* {{{ proto int Phar::setMetaData(mixed $metadata)
2007-05-15 20:42:38 +00:00
* Sets the global metadata of the phar
2007-01-29 03:59:55 +00:00
*/
PHP_METHOD ( Phar , setMetadata )
{
2007-05-15 20:42:38 +00:00
char * error ;
2007-01-29 03:59:55 +00:00
zval * metadata ;
2008-08-01 13:45:06 +00:00
2007-01-29 03:59:55 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Write operations disabled by the php.ini setting phar.readonly " ) ;
2008-01-21 05:28:09 +00:00
return ;
}
2008-05-15 23:46:54 +00:00
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " z " , & metadata ) = = FAILURE ) {
2007-01-29 03:59:55 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
if ( phar_obj - > archive - > is_persistent & & FAILURE = = phar_copy_on_write ( & ( phar_obj - > archive ) ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar_obj - > archive - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
2014-05-08 17:18:59 +00:00
if ( Z_TYPE ( phar_obj - > archive - > metadata ) ! = IS_UNDEF ) {
zval_ptr_dtor ( & phar_obj - > archive - > metadata ) ;
ZVAL_UNDEF ( & phar_obj - > archive - > metadata ) ;
2007-01-29 03:59:55 +00:00
}
2015-06-12 09:33:23 +00:00
ZVAL_COPY ( & phar_obj - > archive - > metadata , metadata ) ;
2014-05-08 17:18:59 +00:00
phar_obj - > archive - > is_modified = 1 ;
2014-12-13 22:06:14 +00:00
phar_flush ( phar_obj - > archive , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-05-15 20:42:38 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-05-15 20:42:38 +00:00
efree ( error ) ;
}
}
/* }}} */
2008-01-21 05:28:09 +00:00
/* {{{ proto int Phar::delMetadata()
2007-05-15 20:42:38 +00:00
* Deletes the global metadata of the phar
*/
PHP_METHOD ( Phar , delMetadata )
{
char * error ;
2008-08-01 13:45:06 +00:00
2007-05-15 20:42:38 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! phar_obj - > archive - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Write operations disabled by the php.ini setting phar.readonly " ) ;
2008-01-21 05:28:09 +00:00
return ;
}
2008-05-15 23:46:54 +00:00
2014-05-08 17:18:59 +00:00
if ( Z_TYPE ( phar_obj - > archive - > metadata ) ! = IS_UNDEF ) {
zval_ptr_dtor ( & phar_obj - > archive - > metadata ) ;
ZVAL_UNDEF ( & phar_obj - > archive - > metadata ) ;
phar_obj - > archive - > is_modified = 1 ;
2014-12-13 22:06:14 +00:00
phar_flush ( phar_obj - > archive , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-05-15 20:42:38 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-05-15 20:42:38 +00:00
efree ( error ) ;
RETURN_FALSE ;
} else {
RETURN_TRUE ;
}
2008-08-01 13:45:06 +00:00
2007-05-15 20:42:38 +00:00
} else {
2007-11-25 05:04:40 +00:00
RETURN_TRUE ;
2007-05-15 20:42:38 +00:00
}
2007-01-29 03:59:55 +00:00
}
/* }}} */
2008-04-26 05:31:00 +00:00
2014-12-13 22:06:14 +00:00
static int phar_extract_file ( zend_bool overwrite , phar_entry_info * entry , char * dest , int dest_len , char * * error ) /* { { { */
2008-04-26 05:31:00 +00:00
{
php_stream_statbuf ssb ;
int len ;
php_stream * fp ;
2010-11-19 16:03:00 +00:00
char * fullpath ;
const char * slash ;
2008-05-03 21:09:56 +00:00
mode_t mode ;
2015-08-04 21:00:29 +00:00
cwd_state new_state ;
char * filename ;
size_t filename_len ;
2008-04-26 05:31:00 +00:00
2008-05-08 02:58:45 +00:00
if ( entry - > is_mounted ) {
/* silently ignore mounted entries */
return SUCCESS ;
}
2008-08-01 13:45:06 +00:00
2008-05-15 16:09:21 +00:00
if ( entry - > filename_len > = sizeof ( " .phar " ) - 1 & & ! memcmp ( entry - > filename , " .phar " , sizeof ( " .phar " ) - 1 ) ) {
return SUCCESS ;
}
2015-08-04 21:00:29 +00:00
/* strip .. from path and restrict it to be under dest directory */
2015-08-04 23:31:57 +00:00
new_state . cwd = ( char * ) emalloc ( 2 ) ;
2015-08-04 21:00:29 +00:00
new_state . cwd [ 0 ] = DEFAULT_SLASH ;
new_state . cwd [ 1 ] = ' \0 ' ;
new_state . cwd_length = 1 ;
2015-12-25 10:13:39 +00:00
if ( virtual_file_ex ( & new_state , entry - > filename , NULL , CWD_EXPAND ) ! = 0 | |
2015-08-04 21:00:29 +00:00
new_state . cwd_length < = 1 ) {
if ( EINVAL = = errno & & entry - > filename_len > 50 ) {
char * tmp = estrndup ( entry - > filename , 50 ) ;
spprintf ( error , 4096 , " Cannot extract \" %s... \" to \" %s... \" , extracted filename is too long for filesystem " , tmp , dest ) ;
efree ( tmp ) ;
} else {
spprintf ( error , 4096 , " Cannot extract \" %s \" , internal error " , entry - > filename ) ;
}
2015-08-04 23:31:57 +00:00
efree ( new_state . cwd ) ;
2015-08-04 21:00:29 +00:00
return FAILURE ;
}
filename = new_state . cwd + 1 ;
filename_len = new_state . cwd_length - 1 ;
# ifdef PHP_WIN32
/* unixify the path back, otherwise non zip formats might be broken */
{
int cnt = filename_len ;
do {
if ( ' \\ ' = = filename [ cnt ] ) {
filename [ cnt ] = ' / ' ;
}
} while ( cnt - - > = 0 ) ;
}
# endif
2008-08-01 13:45:06 +00:00
2015-08-04 21:00:29 +00:00
len = spprintf ( & fullpath , 0 , " %s/%s " , dest , filename ) ;
2008-08-01 13:45:06 +00:00
2008-04-26 05:31:00 +00:00
if ( len > = MAXPATHLEN ) {
char * tmp ;
/* truncate for error message */
fullpath [ 50 ] = ' \0 ' ;
if ( entry - > filename_len > 50 ) {
tmp = estrndup ( entry - > filename , 50 ) ;
spprintf ( error , 4096 , " Cannot extract \" %s... \" to \" %s... \" , extracted filename is too long for filesystem " , tmp , fullpath ) ;
efree ( tmp ) ;
} else {
spprintf ( error , 4096 , " Cannot extract \" %s \" to \" %s... \" , extracted filename is too long for filesystem " , entry - > filename , fullpath ) ;
}
efree ( fullpath ) ;
2015-08-04 23:31:57 +00:00
efree ( new_state . cwd ) ;
2008-04-26 05:31:00 +00:00
return FAILURE ;
}
2008-08-01 13:45:06 +00:00
2008-04-26 05:31:00 +00:00
if ( ! len ) {
spprintf ( error , 4096 , " Cannot extract \" %s \" , internal error " , entry - > filename ) ;
efree ( fullpath ) ;
2015-08-04 23:31:57 +00:00
efree ( new_state . cwd ) ;
2008-04-26 05:31:00 +00:00
return FAILURE ;
}
2016-01-30 13:56:17 +00:00
if ( php_check_open_basedir ( fullpath ) ) {
2008-04-26 05:31:00 +00:00
spprintf ( error , 4096 , " Cannot extract \" %s \" to \" %s \" , openbasedir/safe mode restrictions in effect " , entry - > filename , fullpath ) ;
efree ( fullpath ) ;
2015-08-04 23:31:57 +00:00
efree ( new_state . cwd ) ;
2008-04-26 05:31:00 +00:00
return FAILURE ;
}
/* let see if the path already exists */
if ( ! overwrite & & SUCCESS = = php_stream_stat_path ( fullpath , & ssb ) ) {
spprintf ( error , 4096 , " Cannot extract \" %s \" to \" %s \" , path already exists " , entry - > filename , fullpath ) ;
efree ( fullpath ) ;
2015-08-04 23:31:57 +00:00
efree ( new_state . cwd ) ;
2008-04-26 05:31:00 +00:00
return FAILURE ;
}
2008-08-01 13:45:06 +00:00
2008-04-26 05:31:00 +00:00
/* perform dirname */
2015-08-04 21:00:29 +00:00
slash = zend_memrchr ( filename , ' / ' , filename_len ) ;
2008-08-01 13:45:06 +00:00
2008-04-26 05:31:00 +00:00
if ( slash ) {
2015-08-04 21:00:29 +00:00
fullpath [ dest_len + ( slash - filename ) + 1 ] = ' \0 ' ;
2008-04-26 05:31:00 +00:00
} else {
fullpath [ dest_len ] = ' \0 ' ;
}
2008-08-01 13:45:06 +00:00
2008-04-26 05:31:00 +00:00
if ( FAILURE = = php_stream_stat_path ( fullpath , & ssb ) ) {
2008-05-02 04:44:39 +00:00
if ( entry - > is_dir ) {
if ( ! php_stream_mkdir ( fullpath , entry - > flags & PHAR_ENT_PERM_MASK , PHP_STREAM_MKDIR_RECURSIVE , NULL ) ) {
spprintf ( error , 4096 , " Cannot extract \" %s \" , could not create directory \" %s \" " , entry - > filename , fullpath ) ;
efree ( fullpath ) ;
2016-06-12 23:43:12 +00:00
efree ( new_state . cwd ) ;
2008-05-02 04:44:39 +00:00
return FAILURE ;
}
} else {
if ( ! php_stream_mkdir ( fullpath , 0777 , PHP_STREAM_MKDIR_RECURSIVE , NULL ) ) {
spprintf ( error , 4096 , " Cannot extract \" %s \" , could not create directory \" %s \" " , entry - > filename , fullpath ) ;
efree ( fullpath ) ;
2016-06-12 23:43:12 +00:00
efree ( new_state . cwd ) ;
2008-05-02 04:44:39 +00:00
return FAILURE ;
}
2008-04-26 05:31:00 +00:00
}
}
2008-08-01 13:45:06 +00:00
2008-04-26 05:31:00 +00:00
if ( slash ) {
2015-08-04 21:00:29 +00:00
fullpath [ dest_len + ( slash - filename ) + 1 ] = ' / ' ;
2008-04-26 05:31:00 +00:00
} else {
fullpath [ dest_len ] = ' / ' ;
}
2015-08-04 21:00:29 +00:00
filename = NULL ;
2015-08-04 23:31:57 +00:00
efree ( new_state . cwd ) ;
2008-04-26 05:31:00 +00:00
/* it is a standalone directory, job done */
if ( entry - > is_dir ) {
efree ( fullpath ) ;
return SUCCESS ;
}
2010-04-26 23:53:30 +00:00
fp = php_stream_open_wrapper ( fullpath , " w+b " , REPORT_ERRORS , NULL ) ;
2008-05-03 21:09:56 +00:00
2008-04-26 05:31:00 +00:00
if ( ! fp ) {
spprintf ( error , 4096 , " Cannot extract \" %s \" , could not open for writing \" %s \" " , entry - > filename , fullpath ) ;
efree ( fullpath ) ;
return FAILURE ;
}
2008-05-03 21:09:56 +00:00
2014-12-13 22:06:14 +00:00
if ( ! phar_get_efp ( entry , 0 ) ) {
if ( FAILURE = = phar_open_entry_fp ( entry , error , 1 ) ) {
2008-04-26 05:31:00 +00:00
if ( error ) {
spprintf ( error , 4096 , " Cannot extract \" %s \" to \" %s \" , unable to open internal file pointer: %s " , entry - > filename , fullpath , * error ) ;
} else {
spprintf ( error , 4096 , " Cannot extract \" %s \" to \" %s \" , unable to open internal file pointer " , entry - > filename , fullpath ) ;
}
efree ( fullpath ) ;
php_stream_close ( fp ) ;
return FAILURE ;
}
}
2008-05-03 21:09:56 +00:00
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_seek_efp ( entry , 0 , SEEK_SET , 0 , 0 ) ) {
2008-04-26 05:31:00 +00:00
spprintf ( error , 4096 , " Cannot extract \" %s \" to \" %s \" , unable to seek internal file pointer " , entry - > filename , fullpath ) ;
efree ( fullpath ) ;
php_stream_close ( fp ) ;
return FAILURE ;
}
2008-05-03 21:09:56 +00:00
2014-12-13 22:06:14 +00:00
if ( SUCCESS ! = php_stream_copy_to_stream_ex ( phar_get_efp ( entry , 0 ) , fp , entry - > uncompressed_filesize , NULL ) ) {
2008-04-26 05:31:00 +00:00
spprintf ( error , 4096 , " Cannot extract \" %s \" to \" %s \" , copying contents failed " , entry - > filename , fullpath ) ;
efree ( fullpath ) ;
php_stream_close ( fp ) ;
return FAILURE ;
}
2008-05-03 21:09:56 +00:00
2008-04-26 05:31:00 +00:00
php_stream_close ( fp ) ;
2008-05-03 21:09:56 +00:00
mode = ( mode_t ) entry - > flags & PHAR_ENT_PERM_MASK ;
if ( FAILURE = = VCWD_CHMOD ( fullpath , mode ) ) {
2008-05-02 04:44:39 +00:00
spprintf ( error , 4096 , " Cannot extract \" %s \" to \" %s \" , setting file permissions failed " , entry - > filename , fullpath ) ;
efree ( fullpath ) ;
return FAILURE ;
}
2008-05-03 21:09:56 +00:00
2008-05-02 04:44:39 +00:00
efree ( fullpath ) ;
2008-04-26 05:31:00 +00:00
return SUCCESS ;
}
2008-08-01 13:45:06 +00:00
/* }}} */
2008-04-26 05:31:00 +00:00
/* {{{ proto bool Phar::extractTo(string pathto[[, mixed files], bool overwrite])
* Extract one or more file from a phar archive , optionally overwriting existing files
*/
PHP_METHOD ( Phar , extractTo )
{
char * error = NULL ;
2008-08-01 13:45:06 +00:00
php_stream * fp ;
2008-04-26 05:31:00 +00:00
php_stream_statbuf ssb ;
phar_entry_info * entry ;
2015-03-03 23:05:28 +00:00
char * pathto , * filename ;
2014-08-27 13:31:48 +00:00
size_t pathto_len , filename_len ;
2008-04-26 05:31:00 +00:00
int ret , i ;
int nelems ;
zval * zval_files = NULL ;
zend_bool overwrite = 0 ;
2008-08-01 13:45:06 +00:00
2008-04-26 05:31:00 +00:00
PHAR_ARCHIVE_OBJECT ( ) ;
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " p|z!b " , & pathto , & pathto_len , & zval_files , & overwrite ) = = FAILURE ) {
2008-04-26 05:31:00 +00:00
return ;
}
2015-03-03 23:05:28 +00:00
fp = php_stream_open_wrapper ( phar_obj - > archive - > fname , " rb " , IGNORE_URL | STREAM_MUST_SEEK , NULL ) ;
2008-08-01 13:45:06 +00:00
if ( ! fp ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_InvalidArgumentException , 0 ,
2014-05-08 17:18:59 +00:00
" Invalid argument, %s cannot be found " , phar_obj - > archive - > fname ) ;
2008-08-01 13:45:06 +00:00
return ;
}
php_stream_close ( fp ) ;
2008-04-26 05:31:00 +00:00
if ( pathto_len < 1 ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_InvalidArgumentException , 0 ,
2008-04-26 05:31:00 +00:00
" Invalid argument, extraction path must be non-zero length " ) ;
return ;
}
2008-08-01 13:45:06 +00:00
2008-04-26 05:31:00 +00:00
if ( pathto_len > = MAXPATHLEN ) {
char * tmp = estrndup ( pathto , 50 ) ;
/* truncate for error message */
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_InvalidArgumentException , 0 , " Cannot extract to \" %s... \" , destination directory is too long for filesystem " , tmp ) ;
2008-04-26 05:31:00 +00:00
efree ( tmp ) ;
return ;
}
if ( php_stream_stat_path ( pathto , & ssb ) < 0 ) {
ret = php_stream_mkdir ( pathto , 0777 , PHP_STREAM_MKDIR_RECURSIVE , NULL ) ;
if ( ! ret ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_RuntimeException , 0 ,
2008-04-26 05:31:00 +00:00
" Unable to create path \" %s \" for extraction " , pathto ) ;
return ;
}
} else if ( ! ( ssb . sb . st_mode & S_IFDIR ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_RuntimeException , 0 ,
2008-04-26 05:31:00 +00:00
" Unable to use path \" %s \" for extraction, it is a file, must be a directory " , pathto ) ;
return ;
}
if ( zval_files ) {
switch ( Z_TYPE_P ( zval_files ) ) {
2008-05-02 04:44:39 +00:00
case IS_NULL :
goto all_files ;
2008-04-26 05:31:00 +00:00
case IS_STRING :
filename = Z_STRVAL_P ( zval_files ) ;
2014-08-25 17:24:55 +00:00
filename_len = Z_STRLEN_P ( zval_files ) ;
2008-04-26 05:31:00 +00:00
break ;
case IS_ARRAY :
nelems = zend_hash_num_elements ( Z_ARRVAL_P ( zval_files ) ) ;
if ( nelems = = 0 ) {
RETURN_FALSE ;
}
for ( i = 0 ; i < nelems ; i + + ) {
2014-05-08 14:30:07 +00:00
zval * zval_file ;
if ( ( zval_file = zend_hash_index_find ( Z_ARRVAL_P ( zval_files ) , i ) ) ! = NULL ) {
switch ( Z_TYPE_P ( zval_file ) ) {
2008-04-26 05:31:00 +00:00
case IS_STRING :
break ;
default :
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_InvalidArgumentException , 0 ,
2008-04-26 05:31:00 +00:00
" Invalid argument, array of filenames to extract contains non-string value " ) ;
return ;
}
2014-05-08 17:18:59 +00:00
if ( NULL = = ( entry = zend_hash_find_ptr ( & phar_obj - > archive - > manifest , Z_STR_P ( zval_file ) ) ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 ,
2014-05-08 17:18:59 +00:00
" Phar Error: attempted to extract non-existent file \" %s \" from phar \" %s \" " , Z_STRVAL_P ( zval_file ) , phar_obj - > archive - > fname ) ;
2008-04-26 05:31:00 +00:00
}
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_extract_file ( overwrite , entry , pathto , pathto_len , & error ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 ,
2014-05-08 17:18:59 +00:00
" Extraction from phar \" %s \" failed: %s " , phar_obj - > archive - > fname , error ) ;
2008-04-26 05:31:00 +00:00
efree ( error ) ;
return ;
}
}
}
RETURN_TRUE ;
default :
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_InvalidArgumentException , 0 ,
2008-04-26 05:31:00 +00:00
" Invalid argument, expected a filename (string) or array of filenames " ) ;
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( NULL = = ( entry = zend_hash_str_find_ptr ( & phar_obj - > archive - > manifest , filename , filename_len ) ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 ,
2014-05-08 17:18:59 +00:00
" Phar Error: attempted to extract non-existent file \" %s \" from phar \" %s \" " , filename , phar_obj - > archive - > fname ) ;
2008-04-26 05:31:00 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_extract_file ( overwrite , entry , pathto , pathto_len , & error ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 ,
2014-05-08 17:18:59 +00:00
" Extraction from phar \" %s \" failed: %s " , phar_obj - > archive - > fname , error ) ;
2008-04-26 05:31:00 +00:00
efree ( error ) ;
return ;
}
} else {
2008-09-13 22:31:18 +00:00
phar_archive_data * phar ;
2008-05-02 04:44:39 +00:00
all_files :
2014-05-08 17:18:59 +00:00
phar = phar_obj - > archive ;
2008-04-26 05:31:00 +00:00
/* Extract all files */
if ( ! zend_hash_num_elements ( & ( phar - > manifest ) ) ) {
RETURN_TRUE ;
}
2014-05-28 10:09:42 +00:00
ZEND_HASH_FOREACH_PTR ( & phar - > manifest , entry ) {
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_extract_file ( overwrite , entry , pathto , pathto_len , & error ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 ,
2008-04-26 05:31:00 +00:00
" Extraction from phar \" %s \" failed: %s " , phar - > fname , error ) ;
efree ( error ) ;
return ;
}
2014-05-28 10:09:42 +00:00
} ZEND_HASH_FOREACH_END ( ) ;
2008-04-26 05:31:00 +00:00
}
RETURN_TRUE ;
}
/* }}} */
2007-01-29 03:59:55 +00:00
2007-01-21 15:25:50 +00:00
/* {{{ proto void PharFileInfo::__construct(string entry)
* Construct a Phar entry object
*/
PHP_METHOD ( PharFileInfo , __construct )
{
2007-01-29 06:02:19 +00:00
char * fname , * arch , * entry , * error ;
2014-08-27 19:53:06 +00:00
size_t fname_len ;
int arch_len , entry_len ;
2008-08-01 13:45:06 +00:00
phar_entry_object * entry_obj ;
phar_entry_info * entry_info ;
2007-01-21 15:25:50 +00:00
phar_archive_data * phar_data ;
zval * zobj = getThis ( ) , arg1 ;
2016-03-21 03:54:09 +00:00
if ( zend_parse_parameters_throw ( ZEND_NUM_ARGS ( ) , " p " , & fname , & fname_len ) = = FAILURE ) {
2007-01-21 15:25:50 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
entry_obj = ( phar_entry_object * ) ( ( char * ) Z_OBJ_P ( zobj ) - Z_OBJ_P ( zobj ) - > handlers - > offset ) ;
2007-01-21 15:25:50 +00:00
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Cannot call constructor twice " ) ;
2007-01-21 15:25:50 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
if ( fname_len < 7 | | memcmp ( fname , " phar:// " , 7 ) | | phar_split_fname ( fname , ( int ) fname_len , & arch , & arch_len , & entry , & entry_len , 2 , 0 ) = = FAILURE ) {
zend_throw_exception_ex ( spl_ce_RuntimeException , 0 ,
2008-04-24 05:18:50 +00:00
" '%s' is not a valid phar archive URL (must have at least phar://filename.phar) " , fname ) ;
2007-01-21 15:25:50 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
if ( phar_open_from_filename ( arch , arch_len , NULL , 0 , REPORT_ERRORS , & phar_data , & error ) = = FAILURE ) {
2007-01-21 15:25:50 +00:00
efree ( arch ) ;
efree ( entry ) ;
2007-01-29 06:02:19 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_RuntimeException , 0 ,
2007-01-29 06:02:19 +00:00
" Cannot open phar file '%s': %s " , fname , error ) ;
efree ( error ) ;
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_RuntimeException , 0 ,
2007-01-29 06:02:19 +00:00
" Cannot open phar file '%s' " , fname ) ;
}
2007-01-21 15:25:50 +00:00
return ;
}
2014-12-13 22:06:14 +00:00
if ( ( entry_info = phar_get_entry_info_dir ( phar_data , entry , entry_len , 1 , & error , 1 ) ) = = NULL ) {
zend_throw_exception_ex ( spl_ce_RuntimeException , 0 ,
2008-08-01 13:45:06 +00:00
" Cannot access phar file entry '%s' in archive '%s'%s%s " , entry , arch , error ? " , " : " " , error ? error : " " ) ;
2007-10-04 03:33:21 +00:00
efree ( arch ) ;
2007-01-21 15:25:50 +00:00
efree ( entry ) ;
return ;
}
efree ( arch ) ;
efree ( entry ) ;
2014-05-08 17:18:59 +00:00
entry_obj - > entry = entry_info ;
2007-01-21 15:25:50 +00:00
2014-05-08 14:30:07 +00:00
ZVAL_STRINGL ( & arg1 , fname , fname_len ) ;
2007-01-21 15:25:50 +00:00
2015-01-03 09:22:58 +00:00
zend_call_method_with_1_params ( zobj , Z_OBJCE_P ( zobj ) ,
2007-01-21 15:25:50 +00:00
& spl_ce_SplFileInfo - > constructor , " __construct " , NULL , & arg1 ) ;
2014-05-08 14:30:07 +00:00
zval_ptr_dtor ( & arg1 ) ;
2007-01-21 15:25:50 +00:00
}
/* }}} */
# define PHAR_ENTRY_OBJECT() \
2014-05-08 17:18:59 +00:00
zval * zobj = getThis ( ) ; \
phar_entry_object * entry_obj = ( phar_entry_object * ) ( ( char * ) Z_OBJ_P ( zobj ) - Z_OBJ_P ( zobj ) - > handlers - > offset ) ; \
if ( ! entry_obj - > entry ) { \
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , \
2007-01-23 23:31:14 +00:00
" Cannot call method on an uninitialized PharFileInfo object " ) ; \
2007-01-21 15:25:50 +00:00
return ; \
}
2007-10-04 03:33:21 +00:00
/* {{{ proto void PharFileInfo::__destruct()
* clean up directory - based entry objects
*/
PHP_METHOD ( PharFileInfo , __destruct )
{
2014-05-08 17:18:59 +00:00
zval * zobj = getThis ( ) ;
phar_entry_object * entry_obj = ( phar_entry_object * ) ( ( char * ) Z_OBJ_P ( zobj ) - Z_OBJ_P ( zobj ) - > handlers - > offset ) ;
2007-10-04 03:33:21 +00:00
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry & & entry_obj - > entry - > is_temp_dir ) {
if ( entry_obj - > entry - > filename ) {
efree ( entry_obj - > entry - > filename ) ;
entry_obj - > entry - > filename = NULL ;
2007-10-04 03:33:21 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
efree ( entry_obj - > entry ) ;
entry_obj - > entry = NULL ;
2007-10-04 03:33:21 +00:00
}
}
/* }}} */
2007-01-21 15:25:50 +00:00
/* {{{ proto int PharFileInfo::getCompressedSize()
* Returns the compressed size
*/
PHP_METHOD ( PharFileInfo , getCompressedSize )
{
PHAR_ENTRY_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2007-10-04 03:33:21 +00:00
2014-08-25 17:24:55 +00:00
RETURN_LONG ( entry_obj - > entry - > compressed_filesize ) ;
2007-01-21 15:25:50 +00:00
}
/* }}} */
2008-04-11 13:07:14 +00:00
/* {{{ proto bool PharFileInfo::isCompressed([int compression_type])
* Returns whether the entry is compressed , and whether it is compressed with Phar : : GZ or Phar : : BZ2 if specified
2007-01-21 15:25:50 +00:00
*/
PHP_METHOD ( PharFileInfo , isCompressed )
{
2008-04-11 13:07:14 +00:00
/* a number that is not Phar::GZ or Phar::BZ2 */
2014-08-25 17:24:55 +00:00
zend_long method = 9021976 ;
2007-01-21 15:25:50 +00:00
PHAR_ENTRY_OBJECT ( ) ;
2008-04-11 13:26:03 +00:00
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " |l " , & method ) = = FAILURE ) {
2008-04-11 13:07:14 +00:00
return ;
}
switch ( method ) {
case 9021976 :
2014-05-08 17:18:59 +00:00
RETURN_BOOL ( entry_obj - > entry - > flags & PHAR_ENT_COMPRESSION_MASK ) ;
2008-04-11 13:07:14 +00:00
case PHAR_ENT_COMPRESSED_GZ :
2014-05-08 17:18:59 +00:00
RETURN_BOOL ( entry_obj - > entry - > flags & PHAR_ENT_COMPRESSED_GZ ) ;
2008-04-11 13:07:14 +00:00
case PHAR_ENT_COMPRESSED_BZ2 :
2014-05-08 17:18:59 +00:00
RETURN_BOOL ( entry_obj - > entry - > flags & PHAR_ENT_COMPRESSED_BZ2 ) ;
2008-04-11 13:07:14 +00:00
default :
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , \
2008-04-11 13:07:14 +00:00
" Unknown compression type specified " ) ; \
}
2007-01-21 15:25:50 +00:00
}
/* }}} */
/* {{{ proto int PharFileInfo::getCRC32()
* Returns CRC32 code or throws an exception if not CRC checked
*/
PHP_METHOD ( PharFileInfo , getCRC32 )
{
PHAR_ENTRY_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2007-01-21 15:25:50 +00:00
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_dir ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , \
2007-10-04 03:33:21 +00:00
" Phar entry is a directory, does not have a CRC " ) ; \
2008-01-14 05:12:46 +00:00
return ;
2007-10-04 03:33:21 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_crc_checked ) {
2014-08-25 17:24:55 +00:00
RETURN_LONG ( entry_obj - > entry - > crc32 ) ;
2007-01-21 15:25:50 +00:00
} else {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , \
2007-01-21 15:25:50 +00:00
" Phar entry was not CRC checked " ) ; \
}
}
/* }}} */
/* {{{ proto int PharFileInfo::isCRCChecked()
* Returns whether file entry is CRC checked
*/
PHP_METHOD ( PharFileInfo , isCRCChecked )
{
PHAR_ENTRY_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
RETURN_BOOL ( entry_obj - > entry - > is_crc_checked ) ;
2007-01-21 15:25:50 +00:00
}
/* }}} */
/* {{{ proto int PharFileInfo::getPharFlags()
* Returns the Phar file entry flags
*/
PHP_METHOD ( PharFileInfo , getPharFlags )
{
PHAR_ENTRY_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2008-08-01 13:45:06 +00:00
2014-08-25 17:24:55 +00:00
RETURN_LONG ( entry_obj - > entry - > flags & ~ ( PHAR_ENT_PERM_MASK | PHAR_ENT_COMPRESSION_MASK ) ) ;
2007-01-21 15:25:50 +00:00
}
/* }}} */
2007-02-06 05:41:18 +00:00
/* {{{ proto int PharFileInfo::chmod()
* set the file permissions for the Phar . This only allows setting execution bit , read / write
*/
PHP_METHOD ( PharFileInfo , chmod )
{
2007-02-06 17:09:37 +00:00
char * error ;
2014-08-25 17:24:55 +00:00
zend_long perms ;
2007-02-06 05:41:18 +00:00
PHAR_ENTRY_OBJECT ( ) ;
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_temp_dir ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , \
2014-05-08 17:18:59 +00:00
" Phar entry \" %s \" is a temporary directory (not an actual entry in the archive), cannot chmod " , entry_obj - > entry - > filename ) ; \
2008-01-08 07:08:46 +00:00
return ;
2007-10-04 03:33:21 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! entry_obj - > entry - > phar - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " Cannot modify permissions for file \" %s \" in phar \" %s \" , write operations are prohibited " , entry_obj - > entry - > filename , entry_obj - > entry - > phar - > fname ) ;
2008-01-08 07:08:46 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " l " , & perms ) = = FAILURE ) {
2007-02-06 05:41:18 +00:00
return ;
2008-08-01 13:45:06 +00:00
}
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_persistent ) {
phar_archive_data * phar = entry_obj - > entry - > phar ;
2008-10-12 06:53:52 +00:00
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_copy_on_write ( & phar ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
/* re-populate after copy-on-write */
2014-05-08 17:18:59 +00:00
entry_obj - > entry = zend_hash_str_find_ptr ( & phar - > manifest , entry_obj - > entry - > filename , entry_obj - > entry - > filename_len ) ;
2008-10-12 06:53:52 +00:00
}
2008-08-01 13:45:06 +00:00
/* clear permissions */
2014-05-08 17:18:59 +00:00
entry_obj - > entry - > flags & = ~ PHAR_ENT_PERM_MASK ;
2007-02-06 05:41:18 +00:00
perms & = 0777 ;
2014-05-08 17:18:59 +00:00
entry_obj - > entry - > flags | = perms ;
entry_obj - > entry - > old_flags = entry_obj - > entry - > flags ;
entry_obj - > entry - > phar - > is_modified = 1 ;
entry_obj - > entry - > is_modified = 1 ;
2008-08-01 13:45:06 +00:00
2007-02-06 17:09:37 +00:00
/* hackish cache in php_stat needs to be cleared */
/* if this code fails to work, check main/streams/streams.c, _php_stream_stat_path */
if ( BG ( CurrentLStatFile ) ) {
efree ( BG ( CurrentLStatFile ) ) ;
}
2008-08-01 13:45:06 +00:00
2007-02-06 17:09:37 +00:00
if ( BG ( CurrentStatFile ) ) {
efree ( BG ( CurrentStatFile ) ) ;
}
2008-08-01 13:45:06 +00:00
2007-02-06 17:09:37 +00:00
BG ( CurrentLStatFile ) = NULL ;
BG ( CurrentStatFile ) = NULL ;
2014-12-13 22:06:14 +00:00
phar_flush ( entry_obj - > entry - > phar , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-02-06 17:09:37 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-02-06 17:09:37 +00:00
efree ( error ) ;
}
2007-02-06 05:41:18 +00:00
}
/* }}} */
2007-05-14 17:13:07 +00:00
/* {{{ proto int PharFileInfo::hasMetaData()
* Returns the metadata of the entry
*/
PHP_METHOD ( PharFileInfo , hasMetadata )
{
PHAR_ENTRY_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
RETURN_BOOL ( Z_TYPE ( entry_obj - > entry - > metadata ) ! = IS_UNDEF ) ;
2007-05-14 17:13:07 +00:00
}
/* }}} */
2007-01-21 20:12:50 +00:00
/* {{{ proto int PharFileInfo::getMetaData()
* Returns the metadata of the entry
*/
PHP_METHOD ( PharFileInfo , getMetadata )
{
PHAR_ENTRY_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( Z_TYPE ( entry_obj - > entry - > metadata ) ! = IS_UNDEF ) {
if ( entry_obj - > entry - > is_persistent ) {
char * buf = estrndup ( ( char * ) Z_PTR ( entry_obj - > entry - > metadata ) , entry_obj - > entry - > metadata_len ) ;
2008-10-26 05:49:24 +00:00
/* assume success, we would have failed before */
2015-06-12 10:33:14 +00:00
phar_parse_metadata ( & buf , return_value , entry_obj - > entry - > metadata_len ) ;
2008-10-26 05:49:24 +00:00
efree ( buf ) ;
2015-06-12 10:33:14 +00:00
} else {
ZVAL_COPY ( return_value , & entry_obj - > entry - > metadata ) ;
2008-10-26 05:49:24 +00:00
}
2007-01-21 20:12:50 +00:00
}
}
/* }}} */
/* {{{ proto int PharFileInfo::setMetaData(mixed $metadata)
2007-05-14 17:13:07 +00:00
* Sets the metadata of the entry
2007-01-21 20:12:50 +00:00
*/
PHP_METHOD ( PharFileInfo , setMetadata )
{
2007-05-14 18:11:53 +00:00
char * error ;
2007-01-21 20:12:50 +00:00
zval * metadata ;
2008-08-01 13:45:06 +00:00
2007-01-21 20:12:50 +00:00
PHAR_ENTRY_OBJECT ( ) ;
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! entry_obj - > entry - > phar - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Write operations disabled by the php.ini setting phar.readonly " ) ;
2008-01-21 05:28:09 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_temp_dir ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , \
2008-04-25 16:05:05 +00:00
" Phar entry is a temporary directory (not an actual entry in the archive), cannot set metadata " ) ; \
2008-01-14 05:12:46 +00:00
return ;
2007-10-04 03:33:21 +00:00
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " z " , & metadata ) = = FAILURE ) {
2007-01-21 20:12:50 +00:00
return ;
}
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_persistent ) {
phar_archive_data * phar = entry_obj - > entry - > phar ;
2008-10-12 06:53:52 +00:00
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_copy_on_write ( & phar ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
/* re-populate after copy-on-write */
2014-05-08 17:18:59 +00:00
entry_obj - > entry = zend_hash_str_find_ptr ( & phar - > manifest , entry_obj - > entry - > filename , entry_obj - > entry - > filename_len ) ;
2008-10-12 06:53:52 +00:00
}
2014-05-08 17:18:59 +00:00
if ( Z_TYPE ( entry_obj - > entry - > metadata ) ! = IS_UNDEF ) {
zval_ptr_dtor ( & entry_obj - > entry - > metadata ) ;
ZVAL_UNDEF ( & entry_obj - > entry - > metadata ) ;
2007-01-21 20:12:50 +00:00
}
2015-06-12 09:33:23 +00:00
ZVAL_COPY ( & entry_obj - > entry - > metadata , metadata ) ;
2007-05-14 18:11:53 +00:00
2014-05-08 17:18:59 +00:00
entry_obj - > entry - > is_modified = 1 ;
entry_obj - > entry - > phar - > is_modified = 1 ;
2014-12-13 22:06:14 +00:00
phar_flush ( entry_obj - > entry - > phar , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-05-14 18:11:53 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-05-14 18:11:53 +00:00
efree ( error ) ;
}
2007-01-21 20:12:50 +00:00
}
/* }}} */
2007-05-14 17:13:07 +00:00
/* {{{ proto bool PharFileInfo::delMetaData()
* Deletes the metadata of the entry
*/
PHP_METHOD ( PharFileInfo , delMetadata )
{
2007-05-14 18:11:53 +00:00
char * error ;
2008-08-01 13:45:06 +00:00
2007-05-14 17:13:07 +00:00
PHAR_ENTRY_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2007-05-14 17:13:07 +00:00
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! entry_obj - > entry - > phar - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Write operations disabled by the php.ini setting phar.readonly " ) ;
2008-01-14 05:12:46 +00:00
return ;
2007-10-04 03:33:21 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_temp_dir ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , \
2008-04-25 16:05:05 +00:00
" Phar entry is a temporary directory (not an actual entry in the archive), cannot delete metadata " ) ; \
return ;
2008-01-21 05:28:09 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( Z_TYPE ( entry_obj - > entry - > metadata ) ! = IS_UNDEF ) {
if ( entry_obj - > entry - > is_persistent ) {
phar_archive_data * phar = entry_obj - > entry - > phar ;
2008-10-12 06:53:52 +00:00
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_copy_on_write ( & phar ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
/* re-populate after copy-on-write */
2014-05-08 17:18:59 +00:00
entry_obj - > entry = zend_hash_str_find_ptr ( & phar - > manifest , entry_obj - > entry - > filename , entry_obj - > entry - > filename_len ) ;
2008-10-12 06:53:52 +00:00
}
2014-05-08 17:18:59 +00:00
zval_ptr_dtor ( & entry_obj - > entry - > metadata ) ;
ZVAL_UNDEF ( & entry_obj - > entry - > metadata ) ;
entry_obj - > entry - > is_modified = 1 ;
entry_obj - > entry - > phar - > is_modified = 1 ;
2007-05-14 18:11:53 +00:00
2014-12-13 22:06:14 +00:00
phar_flush ( entry_obj - > entry - > phar , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-05-14 18:11:53 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-05-14 18:11:53 +00:00
efree ( error ) ;
RETURN_FALSE ;
} else {
RETURN_TRUE ;
}
2008-08-01 13:45:06 +00:00
2007-05-14 17:13:07 +00:00
} else {
2007-11-25 05:04:40 +00:00
RETURN_TRUE ;
2007-05-14 17:13:07 +00:00
}
}
/* }}} */
2008-04-10 13:41:29 +00:00
/* {{{ proto string PharFileInfo::getContent()
2008-04-09 18:12:41 +00:00
* return the complete file contents of the entry ( like file_get_contents )
*/
2008-04-10 13:41:29 +00:00
PHP_METHOD ( PharFileInfo , getContent )
2008-04-09 18:12:41 +00:00
{
char * error ;
php_stream * fp ;
2008-04-21 06:17:51 +00:00
phar_entry_info * link ;
2014-05-08 14:30:07 +00:00
zend_string * str ;
2008-08-01 13:45:06 +00:00
2008-04-09 18:12:41 +00:00
PHAR_ENTRY_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2008-04-09 18:12:41 +00:00
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_dir ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2014-05-08 17:18:59 +00:00
" Phar error: Cannot retrieve contents, \" %s \" in phar \" %s \" is a directory " , entry_obj - > entry - > filename , entry_obj - > entry - > phar - > fname ) ;
2008-04-09 18:12:41 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
link = phar_get_link_source ( entry_obj - > entry ) ;
2008-08-01 13:45:06 +00:00
2008-04-21 06:17:51 +00:00
if ( ! link ) {
2014-05-08 17:18:59 +00:00
link = entry_obj - > entry ;
2008-04-21 06:17:51 +00:00
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( SUCCESS ! = phar_open_entry_fp ( link , & error , 0 ) ) {
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2014-05-08 17:18:59 +00:00
" Phar error: Cannot retrieve contents, \" %s \" in phar \" %s \" : %s " , entry_obj - > entry - > filename , entry_obj - > entry - > phar - > fname , error ) ;
2008-04-09 18:12:41 +00:00
efree ( error ) ;
return ;
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
if ( ! ( fp = phar_get_efp ( link , 0 ) ) ) {
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2014-05-08 17:18:59 +00:00
" Phar error: Cannot retrieve contents of \" %s \" in phar \" %s \" " , entry_obj - > entry - > filename , entry_obj - > entry - > phar - > fname ) ;
2008-04-09 18:12:41 +00:00
return ;
}
2008-08-01 13:45:06 +00:00
2014-12-13 22:06:14 +00:00
phar_seek_efp ( link , 0 , SEEK_SET , 0 , 0 ) ;
2014-05-08 14:30:07 +00:00
str = php_stream_copy_to_mem ( fp , link - > uncompressed_filesize , 0 ) ;
if ( str ) {
RETURN_STR ( str ) ;
} else {
RETURN_EMPTY_STRING ( ) ;
2008-04-21 06:17:51 +00:00
}
2008-04-09 18:12:41 +00:00
}
/* }}} */
2008-04-11 13:26:03 +00:00
/* {{{ proto int PharFileInfo::compress(int compression_type)
* Instructs the Phar class to compress the current file using zlib or bzip2 compression
2007-01-22 00:13:20 +00:00
*/
2008-04-11 13:26:03 +00:00
PHP_METHOD ( PharFileInfo , compress )
2007-01-22 00:13:20 +00:00
{
2014-08-25 17:24:55 +00:00
zend_long method ;
2007-01-29 06:02:19 +00:00
char * error ;
2007-01-22 00:13:20 +00:00
PHAR_ENTRY_OBJECT ( ) ;
2014-12-13 22:06:14 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) , " l " , & method ) = = FAILURE ) {
2008-04-11 13:26:03 +00:00
return ;
}
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_tar ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-01-03 04:45:00 +00:00
" Cannot compress with Gzip compression, not possible with tar-based phar archives " ) ;
2008-01-14 05:12:46 +00:00
return ;
2008-01-03 04:45:00 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_dir ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , \
2007-10-04 03:33:21 +00:00
" Phar entry is a directory, cannot set compression " ) ; \
2008-01-14 05:12:46 +00:00
return ;
2007-10-04 03:33:21 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! entry_obj - > entry - > phar - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2007-01-22 00:13:20 +00:00
" Phar is readonly, cannot change compression " ) ;
2008-01-14 05:12:46 +00:00
return ;
2007-01-22 00:13:20 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_deleted ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2007-01-22 00:13:20 +00:00
" Cannot compress deleted file " ) ;
2008-01-14 05:12:46 +00:00
return ;
2007-01-22 00:13:20 +00:00
}
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_persistent ) {
phar_archive_data * phar = entry_obj - > entry - > phar ;
2008-10-12 06:53:52 +00:00
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_copy_on_write ( & phar ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
/* re-populate after copy-on-write */
2014-05-08 17:18:59 +00:00
entry_obj - > entry = zend_hash_str_find_ptr ( & phar - > manifest , entry_obj - > entry - > filename , entry_obj - > entry - > filename_len ) ;
2008-10-12 06:53:52 +00:00
}
2008-04-11 13:26:03 +00:00
switch ( method ) {
case PHAR_ENT_COMPRESSED_GZ :
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > flags & PHAR_ENT_COMPRESSED_GZ ) {
2008-04-11 13:26:03 +00:00
RETURN_TRUE ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( ( entry_obj - > entry - > flags & PHAR_ENT_COMPRESSED_BZ2 ) ! = 0 ) {
2008-05-17 20:07:38 +00:00
if ( ! PHAR_G ( has_bz2 ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 13:26:03 +00:00
" Cannot compress with gzip compression, file is already compressed with bzip2 compression and bz2 extension is not enabled, cannot decompress " ) ;
return ;
}
2008-08-01 13:45:06 +00:00
2008-04-11 13:26:03 +00:00
/* decompress this file indirectly */
2014-12-13 22:06:14 +00:00
if ( SUCCESS ! = phar_open_entry_fp ( entry_obj - > entry , & error , 1 ) ) {
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2014-05-08 17:18:59 +00:00
" Phar error: Cannot decompress bzip2-compressed file \" %s \" in phar \" %s \" in order to compress with gzip: %s " , entry_obj - > entry - > filename , entry_obj - > entry - > phar - > fname , error ) ;
2008-04-11 13:26:03 +00:00
efree ( error ) ;
return ;
}
}
2008-08-01 13:45:06 +00:00
2008-05-17 20:07:38 +00:00
if ( ! PHAR_G ( has_zlib ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 13:26:03 +00:00
" Cannot compress with gzip compression, zlib extension is not enabled " ) ;
return ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
entry_obj - > entry - > old_flags = entry_obj - > entry - > flags ;
entry_obj - > entry - > flags & = ~ PHAR_ENT_COMPRESSION_MASK ;
entry_obj - > entry - > flags | = PHAR_ENT_COMPRESSED_GZ ;
2008-04-11 13:26:03 +00:00
break ;
case PHAR_ENT_COMPRESSED_BZ2 :
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > flags & PHAR_ENT_COMPRESSED_BZ2 ) {
2008-04-11 13:26:03 +00:00
RETURN_TRUE ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( ( entry_obj - > entry - > flags & PHAR_ENT_COMPRESSED_GZ ) ! = 0 ) {
2008-05-17 20:07:38 +00:00
if ( ! PHAR_G ( has_zlib ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 13:26:03 +00:00
" Cannot compress with bzip2 compression, file is already compressed with gzip compression and zlib extension is not enabled, cannot decompress " ) ;
return ;
}
2008-08-01 13:45:06 +00:00
2008-04-11 13:26:03 +00:00
/* decompress this file indirectly */
2014-12-13 22:06:14 +00:00
if ( SUCCESS ! = phar_open_entry_fp ( entry_obj - > entry , & error , 1 ) ) {
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2014-05-08 17:18:59 +00:00
" Phar error: Cannot decompress gzip-compressed file \" %s \" in phar \" %s \" in order to compress with bzip2: %s " , entry_obj - > entry - > filename , entry_obj - > entry - > phar - > fname , error ) ;
2008-04-11 13:26:03 +00:00
efree ( error ) ;
return ;
}
}
2008-08-01 13:45:06 +00:00
2008-05-17 20:07:38 +00:00
if ( ! PHAR_G ( has_bz2 ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-24 19:57:22 +00:00
" Cannot compress with bzip2 compression, bz2 extension is not enabled " ) ;
return ;
}
2014-05-08 17:18:59 +00:00
entry_obj - > entry - > old_flags = entry_obj - > entry - > flags ;
entry_obj - > entry - > flags & = ~ PHAR_ENT_COMPRESSION_MASK ;
entry_obj - > entry - > flags | = PHAR_ENT_COMPRESSED_BZ2 ;
2008-04-11 13:26:03 +00:00
break ;
default :
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , \
2008-04-11 13:26:03 +00:00
" Unknown compression type specified " ) ; \
2007-01-29 06:02:19 +00:00
}
2007-01-22 00:13:20 +00:00
2014-05-08 17:18:59 +00:00
entry_obj - > entry - > phar - > is_modified = 1 ;
entry_obj - > entry - > is_modified = 1 ;
2014-12-13 22:06:14 +00:00
phar_flush ( entry_obj - > entry - > phar , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-01-29 06:02:19 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-01-29 06:02:19 +00:00
efree ( error ) ;
}
2008-08-01 13:45:06 +00:00
2007-01-22 00:13:20 +00:00
RETURN_TRUE ;
}
/* }}} */
2008-04-11 12:56:53 +00:00
/* {{{ proto int PharFileInfo::decompress()
* Instructs the Phar class to decompress the current file
2007-01-22 00:13:20 +00:00
*/
2008-04-11 12:56:53 +00:00
PHP_METHOD ( PharFileInfo , decompress )
2007-01-22 00:13:20 +00:00
{
2008-04-24 19:57:22 +00:00
char * error ;
2007-01-22 00:13:20 +00:00
PHAR_ENTRY_OBJECT ( ) ;
2015-01-03 09:22:58 +00:00
2011-06-18 22:48:31 +00:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
2007-01-22 00:13:20 +00:00
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_dir ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , \
2007-10-04 03:33:21 +00:00
" Phar entry is a directory, cannot set compression " ) ; \
2008-01-14 05:12:46 +00:00
return ;
2007-10-04 03:33:21 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( ( entry_obj - > entry - > flags & PHAR_ENT_COMPRESSION_MASK ) = = 0 ) {
2007-01-22 00:13:20 +00:00
RETURN_TRUE ;
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( PHAR_G ( readonly ) & & ! entry_obj - > entry - > phar - > is_data ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-24 19:57:22 +00:00
" Phar is readonly, cannot decompress " ) ;
2008-01-14 05:12:46 +00:00
return ;
2007-01-22 00:13:20 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_deleted ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2007-01-22 00:13:20 +00:00
" Cannot compress deleted file " ) ;
2008-01-14 05:12:46 +00:00
return ;
2007-01-22 00:13:20 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( ( entry_obj - > entry - > flags & PHAR_ENT_COMPRESSED_GZ ) ! = 0 & & ! PHAR_G ( has_zlib ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 12:56:53 +00:00
" Cannot decompress Gzip-compressed file, zlib extension is not enabled " ) ;
2008-01-14 05:12:46 +00:00
return ;
2007-11-25 05:04:40 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( ( entry_obj - > entry - > flags & PHAR_ENT_COMPRESSED_BZ2 ) ! = 0 & & ! PHAR_G ( has_bz2 ) ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 ,
2008-04-11 12:56:53 +00:00
" Cannot decompress Bzip2-compressed file, bz2 extension is not enabled " ) ;
2008-01-14 05:12:46 +00:00
return ;
2007-11-24 04:06:44 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
if ( entry_obj - > entry - > is_persistent ) {
phar_archive_data * phar = entry_obj - > entry - > phar ;
2008-10-12 06:53:52 +00:00
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_copy_on_write ( & phar ) ) {
zend_throw_exception_ex ( phar_ce_PharException , 0 , " phar \" %s \" is persistent, unable to copy on write " , phar - > fname ) ;
2008-10-12 06:53:52 +00:00
return ;
}
/* re-populate after copy-on-write */
2014-05-08 17:18:59 +00:00
entry_obj - > entry = zend_hash_str_find_ptr ( & phar - > manifest , entry_obj - > entry - > filename , entry_obj - > entry - > filename_len ) ;
2008-10-12 06:53:52 +00:00
}
2014-05-08 17:18:59 +00:00
if ( ! entry_obj - > entry - > fp ) {
2014-12-13 22:06:14 +00:00
if ( FAILURE = = phar_open_archive_fp ( entry_obj - > entry - > phar ) ) {
zend_throw_exception_ex ( spl_ce_BadMethodCallException , 0 , " Cannot decompress entry \" %s \" , phar error: Cannot open phar archive \" %s \" for reading " , entry_obj - > entry - > filename , entry_obj - > entry - > phar - > fname ) ;
2008-04-24 19:57:22 +00:00
return ;
}
2014-05-08 17:18:59 +00:00
entry_obj - > entry - > fp_type = PHAR_FP ;
2007-01-22 00:13:20 +00:00
}
2008-08-01 13:45:06 +00:00
2014-05-08 17:18:59 +00:00
entry_obj - > entry - > old_flags = entry_obj - > entry - > flags ;
entry_obj - > entry - > flags & = ~ PHAR_ENT_COMPRESSION_MASK ;
entry_obj - > entry - > phar - > is_modified = 1 ;
entry_obj - > entry - > is_modified = 1 ;
2014-12-13 22:06:14 +00:00
phar_flush ( entry_obj - > entry - > phar , 0 , 0 , 0 , & error ) ;
2008-08-01 13:45:06 +00:00
2007-01-29 06:02:19 +00:00
if ( error ) {
2014-12-13 22:06:14 +00:00
zend_throw_exception_ex ( phar_ce_PharException , 0 , " %s " , error ) ;
2007-01-29 06:02:19 +00:00
efree ( error ) ;
}
2007-01-22 00:13:20 +00:00
RETURN_TRUE ;
}
/* }}} */
2007-01-21 15:25:50 +00:00
/* {{{ phar methods */
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar___construct , 0 , 0 , 1 )
2008-02-20 17:58:56 +00:00
ZEND_ARG_INFO ( 0 , filename )
2007-01-21 15:25:50 +00:00
ZEND_ARG_INFO ( 0 , flags )
ZEND_ARG_INFO ( 0 , alias )
2008-05-24 14:18:31 +00:00
ZEND_ARG_INFO ( 0 , fileformat )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2007-01-21 15:25:50 +00:00
2008-02-20 17:58:56 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_createDS , 0 , 0 , 0 )
ZEND_ARG_INFO ( 0 , index )
ZEND_ARG_INFO ( 0 , webindex )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-02-20 17:58:56 +00:00
2011-06-18 22:48:31 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_cancompress , 0 , 0 , 0 )
ZEND_ARG_INFO ( 0 , method )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_isvalidpharfilename , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , filename )
ZEND_ARG_INFO ( 0 , executable )
ZEND_END_ARG_INFO ( )
2008-02-20 17:58:56 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_loadPhar , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , filename )
ZEND_ARG_INFO ( 0 , alias )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-02-20 17:58:56 +00:00
2007-01-21 15:25:50 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_mapPhar , 0 , 0 , 0 )
ZEND_ARG_INFO ( 0 , alias )
2007-12-23 21:12:42 +00:00
ZEND_ARG_INFO ( 0 , offset )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2007-12-23 21:12:42 +00:00
2008-02-20 17:58:56 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_mount , 0 , 0 , 2 )
ZEND_ARG_INFO ( 0 , inphar )
ZEND_ARG_INFO ( 0 , externalfile )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-02-20 17:58:56 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_mungServer , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , munglist )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-02-20 17:58:56 +00:00
2007-12-23 21:12:42 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_webPhar , 0 , 0 , 0 )
ZEND_ARG_INFO ( 0 , alias )
2007-12-24 20:30:44 +00:00
ZEND_ARG_INFO ( 0 , index )
2007-12-29 01:37:12 +00:00
ZEND_ARG_INFO ( 0 , f404 )
2007-12-23 21:12:42 +00:00
ZEND_ARG_INFO ( 0 , mimetypes )
2007-12-29 02:38:29 +00:00
ZEND_ARG_INFO ( 0 , rewrites )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2007-01-21 15:25:50 +00:00
2008-04-12 21:43:39 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_running , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , retphar )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-04-12 21:43:39 +00:00
2008-05-03 02:04:24 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_ua , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , archive )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-05-03 02:04:24 +00:00
2008-02-20 17:58:56 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_build , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , iterator )
ZEND_ARG_INFO ( 0 , base_directory )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-01-04 04:57:11 +00:00
2008-02-20 17:58:56 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_conv , 0 , 0 , 0 )
2008-04-11 04:20:15 +00:00
ZEND_ARG_INFO ( 0 , format )
ZEND_ARG_INFO ( 0 , compression_type )
2008-02-25 12:21:19 +00:00
ZEND_ARG_INFO ( 0 , file_ext )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-02-25 12:21:19 +00:00
2008-04-11 13:41:59 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_comps , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , compression_type )
ZEND_ARG_INFO ( 0 , file_ext )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-04-11 13:41:59 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_decomp , 0 , 0 , 0 )
ZEND_ARG_INFO ( 0 , file_ext )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-04-11 13:41:59 +00:00
2008-03-23 22:42:45 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_comp , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , compression_type )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-01-19 04:26:22 +00:00
2008-04-11 13:26:03 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_compo , 0 , 0 , 0 )
ZEND_ARG_INFO ( 0 , compression_type )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-04-11 13:26:03 +00:00
2008-02-20 17:58:56 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_copy , 0 , 0 , 2 )
ZEND_ARG_INFO ( 0 , newfile )
ZEND_ARG_INFO ( 0 , oldfile )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2007-01-21 23:22:57 +00:00
2008-02-20 17:58:56 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_delete , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , entry )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2007-01-21 15:25:50 +00:00
2008-04-25 21:37:41 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_fromdir , 0 , 0 , 1 )
2008-04-26 13:22:38 +00:00
ZEND_ARG_INFO ( 0 , base_dir )
2008-04-25 21:37:41 +00:00
ZEND_ARG_INFO ( 0 , regex )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-04-25 21:37:41 +00:00
2007-01-21 15:25:50 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_offsetExists , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , entry )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2007-01-21 15:25:50 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_offsetSet , 0 , 0 , 2 )
ZEND_ARG_INFO ( 0 , entry )
ZEND_ARG_INFO ( 0 , value )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2007-12-12 18:01:40 +00:00
2008-02-20 17:58:56 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_setAlias , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , alias )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2007-12-16 00:57:39 +00:00
2008-02-20 17:58:56 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_setMetadata , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , metadata )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-01-16 21:09:32 +00:00
2008-02-20 17:58:56 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_setSigAlgo , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , algorithm )
2008-08-01 13:45:06 +00:00
ZEND_ARG_INFO ( 0 , privatekey )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-02-18 04:32:50 +00:00
2008-02-20 17:58:56 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_setStub , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , newstub )
ZEND_ARG_INFO ( 0 , maxlen )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2007-01-21 15:25:50 +00:00
2008-04-09 19:23:31 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_emptydir , 0 , 0 , 0 )
ZEND_ARG_INFO ( 0 , dirname )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-04-09 19:23:31 +00:00
2008-04-26 05:31:00 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_extract , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , pathto )
ZEND_ARG_INFO ( 0 , files )
ZEND_ARG_INFO ( 0 , overwrite )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-04-26 05:31:00 +00:00
2008-04-09 19:23:31 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_addfile , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , filename )
ZEND_ARG_INFO ( 0 , localname )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-04-09 19:23:31 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_fromstring , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , localname )
ZEND_ARG_INFO ( 0 , contents )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-04-09 19:23:31 +00:00
2008-05-02 05:05:56 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_phar_isff , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , fileformat )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-05-02 05:05:56 +00:00
2011-06-18 15:56:14 +00:00
ZEND_BEGIN_ARG_INFO ( arginfo_phar__void , 0 )
ZEND_END_ARG_INFO ( )
2007-01-21 15:25:50 +00:00
zend_function_entry php_archive_methods [ ] = {
2007-01-27 19:26:58 +00:00
PHP_ME ( Phar , __construct , arginfo_phar___construct , ZEND_ACC_PUBLIC )
2011-06-18 15:56:14 +00:00
PHP_ME ( Phar , __destruct , arginfo_phar__void , ZEND_ACC_PUBLIC )
2008-04-09 19:23:31 +00:00
PHP_ME ( Phar , addEmptyDir , arginfo_phar_emptydir , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , addFile , arginfo_phar_addfile , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , addFromString , arginfo_phar_fromstring , ZEND_ACC_PUBLIC )
2008-04-25 21:37:41 +00:00
PHP_ME ( Phar , buildFromDirectory , arginfo_phar_fromdir , ZEND_ACC_PUBLIC )
2008-02-20 17:58:56 +00:00
PHP_ME ( Phar , buildFromIterator , arginfo_phar_build , ZEND_ACC_PUBLIC )
2008-04-10 13:40:26 +00:00
PHP_ME ( Phar , compressFiles , arginfo_phar_comp , ZEND_ACC_PUBLIC )
2011-06-18 15:56:14 +00:00
PHP_ME ( Phar , decompressFiles , arginfo_phar__void , ZEND_ACC_PUBLIC )
2008-04-11 13:41:59 +00:00
PHP_ME ( Phar , compress , arginfo_phar_comps , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , decompress , arginfo_phar_decomp , ZEND_ACC_PUBLIC )
2008-04-11 04:20:15 +00:00
PHP_ME ( Phar , convertToExecutable , arginfo_phar_conv , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , convertToData , arginfo_phar_conv , ZEND_ACC_PUBLIC )
2007-12-16 00:57:39 +00:00
PHP_ME ( Phar , copy , arginfo_phar_copy , ZEND_ACC_PUBLIC )
2011-06-18 15:56:14 +00:00
PHP_ME ( Phar , count , arginfo_phar__void , ZEND_ACC_PUBLIC )
2008-02-20 17:58:56 +00:00
PHP_ME ( Phar , delete , arginfo_phar_delete , ZEND_ACC_PUBLIC )
2011-06-18 15:56:14 +00:00
PHP_ME ( Phar , delMetadata , arginfo_phar__void , ZEND_ACC_PUBLIC )
2008-04-26 05:31:00 +00:00
PHP_ME ( Phar , extractTo , arginfo_phar_extract , ZEND_ACC_PUBLIC )
2011-06-18 15:56:14 +00:00
PHP_ME ( Phar , getAlias , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , getPath , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , getMetadata , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , getModified , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , getSignature , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , getStub , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , getVersion , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , hasMetadata , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , isBuffering , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , isCompressed , arginfo_phar__void , ZEND_ACC_PUBLIC )
2008-05-02 05:05:56 +00:00
PHP_ME ( Phar , isFileFormat , arginfo_phar_isff , ZEND_ACC_PUBLIC )
2011-06-18 15:56:14 +00:00
PHP_ME ( Phar , isWritable , arginfo_phar__void , ZEND_ACC_PUBLIC )
2007-01-27 19:26:58 +00:00
PHP_ME ( Phar , offsetExists , arginfo_phar_offsetExists , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , offsetGet , arginfo_phar_offsetExists , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , offsetSet , arginfo_phar_offsetSet , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , offsetUnset , arginfo_phar_offsetExists , ZEND_ACC_PUBLIC )
2008-02-20 17:58:56 +00:00
PHP_ME ( Phar , setAlias , arginfo_phar_setAlias , ZEND_ACC_PUBLIC )
2008-02-20 19:22:12 +00:00
PHP_ME ( Phar , setDefaultStub , arginfo_phar_createDS , ZEND_ACC_PUBLIC )
2008-02-20 17:58:56 +00:00
PHP_ME ( Phar , setMetadata , arginfo_phar_setMetadata , ZEND_ACC_PUBLIC )
2008-04-11 13:26:36 +00:00
PHP_ME ( Phar , setSignatureAlgorithm , arginfo_phar_setSigAlgo , ZEND_ACC_PUBLIC )
2008-02-20 17:58:56 +00:00
PHP_ME ( Phar , setStub , arginfo_phar_setStub , ZEND_ACC_PUBLIC )
2011-06-18 15:56:14 +00:00
PHP_ME ( Phar , startBuffering , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( Phar , stopBuffering , arginfo_phar__void , ZEND_ACC_PUBLIC )
2007-01-21 15:25:50 +00:00
/* static member functions */
2011-06-18 15:56:14 +00:00
PHP_ME ( Phar , apiVersion , arginfo_phar__void , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
2011-06-18 22:48:31 +00:00
PHP_ME ( Phar , canCompress , arginfo_phar_cancompress , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
2011-06-18 15:56:14 +00:00
PHP_ME ( Phar , canWrite , arginfo_phar__void , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
2008-02-20 17:58:56 +00:00
PHP_ME ( Phar , createDefaultStub , arginfo_phar_createDS , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
2011-06-18 15:56:14 +00:00
PHP_ME ( Phar , getSupportedCompression , arginfo_phar__void , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
PHP_ME ( Phar , getSupportedSignatures , arginfo_phar__void , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
PHP_ME ( Phar , interceptFileFuncs , arginfo_phar__void , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
2011-06-18 22:48:31 +00:00
PHP_ME ( Phar , isValidPharFilename , arginfo_phar_isvalidpharfilename , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
2007-01-27 19:26:58 +00:00
PHP_ME ( Phar , loadPhar , arginfo_phar_loadPhar , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
PHP_ME ( Phar , mapPhar , arginfo_phar_mapPhar , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
2008-03-23 06:00:31 +00:00
PHP_ME ( Phar , running , arginfo_phar_running , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
2008-02-18 04:32:50 +00:00
PHP_ME ( Phar , mount , arginfo_phar_mount , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
2008-01-04 04:57:11 +00:00
PHP_ME ( Phar , mungServer , arginfo_phar_mungServer , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
2008-05-03 02:04:24 +00:00
PHP_ME ( Phar , unlinkArchive , arginfo_phar_ua , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
2008-02-20 17:58:56 +00:00
PHP_ME ( Phar , webPhar , arginfo_phar_webPhar , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
2011-07-25 11:35:02 +00:00
PHP_FE_END
2007-01-21 15:25:50 +00:00
} ;
ZEND_BEGIN_ARG_INFO_EX ( arginfo_entry___construct , 0 , 0 , 1 )
2008-02-20 17:58:56 +00:00
ZEND_ARG_INFO ( 0 , filename )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2008-02-20 17:58:56 +00:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_entry_chmod , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , perms )
2008-08-31 20:54:28 +00:00
ZEND_END_ARG_INFO ( )
2007-01-21 15:25:50 +00:00
zend_function_entry php_entry_methods [ ] = {
2008-04-11 13:26:03 +00:00
PHP_ME ( PharFileInfo , __construct , arginfo_entry___construct , ZEND_ACC_PUBLIC )
2011-06-18 15:56:14 +00:00
PHP_ME ( PharFileInfo , __destruct , arginfo_phar__void , ZEND_ACC_PUBLIC )
2008-04-11 13:26:03 +00:00
PHP_ME ( PharFileInfo , chmod , arginfo_entry_chmod , ZEND_ACC_PUBLIC )
2008-05-02 04:44:39 +00:00
PHP_ME ( PharFileInfo , compress , arginfo_phar_comp , ZEND_ACC_PUBLIC )
2011-06-18 15:56:14 +00:00
PHP_ME ( PharFileInfo , decompress , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( PharFileInfo , delMetadata , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( PharFileInfo , getCompressedSize , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( PharFileInfo , getCRC32 , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( PharFileInfo , getContent , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( PharFileInfo , getMetadata , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( PharFileInfo , getPharFlags , arginfo_phar__void , ZEND_ACC_PUBLIC )
PHP_ME ( PharFileInfo , hasMetadata , arginfo_phar__void , ZEND_ACC_PUBLIC )
2008-04-11 13:26:03 +00:00
PHP_ME ( PharFileInfo , isCompressed , arginfo_phar_compo , ZEND_ACC_PUBLIC )
2011-06-18 15:56:14 +00:00
PHP_ME ( PharFileInfo , isCRCChecked , arginfo_phar__void , ZEND_ACC_PUBLIC )
2008-04-11 13:26:03 +00:00
PHP_ME ( PharFileInfo , setMetadata , arginfo_phar_setMetadata , ZEND_ACC_PUBLIC )
2011-07-25 11:35:02 +00:00
PHP_FE_END
2007-01-21 15:25:50 +00:00
} ;
2007-01-29 06:02:19 +00:00
zend_function_entry phar_exception_methods [ ] = {
2011-07-25 11:35:02 +00:00
PHP_FE_END
2007-01-29 06:02:19 +00:00
} ;
2007-01-21 15:25:50 +00:00
/* }}} */
2014-08-25 17:24:55 +00:00
# define REGISTER_PHAR_CLASS_CONST_LONG(class_name, const_name, value) \
2014-12-13 22:06:14 +00:00
zend_declare_class_constant_long ( class_name , const_name , sizeof ( const_name ) - 1 , ( zend_long ) value ) ;
2007-01-21 15:25:50 +00:00
2014-12-13 22:06:14 +00:00
void phar_object_init ( void ) /* { { { */
2007-01-21 15:25:50 +00:00
{
zend_class_entry ce ;
2007-01-29 06:02:19 +00:00
INIT_CLASS_ENTRY ( ce , " PharException " , phar_exception_methods ) ;
2015-07-03 14:45:03 +00:00
phar_ce_PharException = zend_register_internal_class_ex ( & ce , zend_ce_exception ) ;
2007-01-29 06:02:19 +00:00
2007-01-21 15:25:50 +00:00
INIT_CLASS_ENTRY ( ce , " Phar " , php_archive_methods ) ;
2014-12-13 22:06:14 +00:00
phar_ce_archive = zend_register_internal_class_ex ( & ce , spl_ce_RecursiveDirectoryIterator ) ;
2007-01-21 15:25:50 +00:00
2014-12-13 22:06:14 +00:00
zend_class_implements ( phar_ce_archive , 2 , spl_ce_Countable , zend_ce_arrayaccess ) ;
2007-01-21 15:25:50 +00:00
2008-02-27 21:34:26 +00:00
INIT_CLASS_ENTRY ( ce , " PharData " , php_archive_methods ) ;
2014-12-13 22:06:14 +00:00
phar_ce_data = zend_register_internal_class_ex ( & ce , spl_ce_RecursiveDirectoryIterator ) ;
2008-02-27 21:34:26 +00:00
2014-12-13 22:06:14 +00:00
zend_class_implements ( phar_ce_data , 2 , spl_ce_Countable , zend_ce_arrayaccess ) ;
2008-02-27 21:34:26 +00:00
2007-01-21 15:25:50 +00:00
INIT_CLASS_ENTRY ( ce , " PharFileInfo " , php_entry_methods ) ;
2014-12-13 22:06:14 +00:00
phar_ce_entry = zend_register_internal_class_ex ( & ce , spl_ce_SplFileInfo ) ;
2007-01-21 15:25:50 +00:00
2014-08-25 17:24:55 +00:00
REGISTER_PHAR_CLASS_CONST_LONG ( phar_ce_archive , " BZ2 " , PHAR_ENT_COMPRESSED_BZ2 )
REGISTER_PHAR_CLASS_CONST_LONG ( phar_ce_archive , " GZ " , PHAR_ENT_COMPRESSED_GZ )
REGISTER_PHAR_CLASS_CONST_LONG ( phar_ce_archive , " NONE " , PHAR_ENT_COMPRESSED_NONE )
REGISTER_PHAR_CLASS_CONST_LONG ( phar_ce_archive , " PHAR " , PHAR_FORMAT_PHAR )
REGISTER_PHAR_CLASS_CONST_LONG ( phar_ce_archive , " TAR " , PHAR_FORMAT_TAR )
REGISTER_PHAR_CLASS_CONST_LONG ( phar_ce_archive , " ZIP " , PHAR_FORMAT_ZIP )
REGISTER_PHAR_CLASS_CONST_LONG ( phar_ce_archive , " COMPRESSED " , PHAR_ENT_COMPRESSION_MASK )
REGISTER_PHAR_CLASS_CONST_LONG ( phar_ce_archive , " PHP " , PHAR_MIME_PHP )
REGISTER_PHAR_CLASS_CONST_LONG ( phar_ce_archive , " PHPS " , PHAR_MIME_PHPS )
REGISTER_PHAR_CLASS_CONST_LONG ( phar_ce_archive , " MD5 " , PHAR_SIG_MD5 )
REGISTER_PHAR_CLASS_CONST_LONG ( phar_ce_archive , " OPENSSL " , PHAR_SIG_OPENSSL )
REGISTER_PHAR_CLASS_CONST_LONG ( phar_ce_archive , " SHA1 " , PHAR_SIG_SHA1 )
REGISTER_PHAR_CLASS_CONST_LONG ( phar_ce_archive , " SHA256 " , PHAR_SIG_SHA256 )
REGISTER_PHAR_CLASS_CONST_LONG ( phar_ce_archive , " SHA512 " , PHAR_SIG_SHA512 )
2007-01-21 15:25:50 +00:00
}
/* }}} */
/*
* Local variables :
* tab - width : 4
* c - basic - offset : 4
* End :
* vim600 : noet sw = 4 ts = 4 fdm = marker
* vim < 600 : noet sw = 4 ts = 4
*/