2005-12-05 04:35:38 +08:00
/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2005-12-05 06:50:54 +08:00
| phar php single - file executable PHP extension |
2005-12-05 04:35:38 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2005-12-05 06:50:54 +08:00
| Copyright ( c ) 2005 The PHP Group |
2005-12-05 04:35:38 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2005-12-05 06:50:54 +08:00
| This source file is subject to version 3.01 of the PHP license , |
2005-12-05 04:35:38 +08:00
| that is bundled with this package in the file LICENSE , and is |
| available through the world - wide - web at the following url : |
2005-12-05 06:50:54 +08:00
| http : //www.php.net/license/3_01.txt. |
2005-12-05 04:35:38 +08:00
| 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 . |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2006-01-04 21:18:31 +08:00
| Authors : Gregory Beaver < cellog @ php . net > |
| Marcus Boerger < helly @ php . net > |
2005-12-05 04:35:38 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
/* $Id$ */
# ifdef HAVE_CONFIG_H
# include "config.h"
# endif
# include <time.h>
# include "php.h"
# include "php_ini.h"
# include "ext/standard/info.h"
# include "ext/standard/url.h"
# include "ext/standard/crc32.h"
# include "zend_execute.h"
2006-01-02 04:16:09 +08:00
# include "zend_qsort.h"
2005-12-05 04:35:38 +08:00
# include "zend_constants.h"
2006-01-02 04:16:09 +08:00
# include "zend_operators.h"
2005-12-05 04:35:38 +08:00
# include "php_phar.h"
2005-12-05 12:47:29 +08:00
# include "main/php_streams.h"
2005-12-05 04:35:38 +08:00
# ifndef TRUE
# define TRUE 1
# define FALSE 0
# endif
ZEND_DECLARE_MODULE_GLOBALS ( phar )
2006-01-04 23:50:45 +08:00
/* entry for one file in a phar file */
typedef struct _phar_manifest_entry {
php_uint32 filename_len ;
char * filename ;
php_uint32 uncompressed_filesize ;
php_uint32 timestamp ;
php_uint32 offset_within_phar ;
php_uint32 compressed_filesize ;
zend_bool crc_checked ;
2006-01-06 06:24:41 +08:00
php_stream * fp ;
2006-01-04 23:50:45 +08:00
} phar_manifest_entry ;
/* information about a phar file */
typedef struct _phar_file_data {
char * filename ;
int filename_len ;
char * alias ;
int alias_len ;
size_t internal_file_start ;
zend_bool is_compressed ;
2006-01-05 19:32:05 +08:00
HashTable manifest ;
2006-01-04 23:50:45 +08:00
php_stream * fp ;
} phar_file_data ;
/* stream access data for one file entry in a phar file */
2006-01-04 23:27:18 +08:00
typedef struct _phar_internal_file_data {
2006-01-06 06:24:41 +08:00
phar_file_data * phar ;
php_stream * fp ;
2005-12-05 04:35:38 +08:00
phar_manifest_entry * internal_file ;
} phar_internal_file_data ;
2006-01-04 23:50:45 +08:00
/* {{{ forward declarations */
static php_stream * php_stream_phar_url_wrapper ( php_stream_wrapper * wrapper , char * path , char * mode , int options , char * * opened_path , php_stream_context * context STREAMS_DC TSRMLS_DC ) ;
static int phar_close ( php_stream * stream , int close_handle TSRMLS_DC ) ;
static int phar_closedir ( php_stream * stream , int close_handle TSRMLS_DC ) ;
static int phar_seekdir ( php_stream * stream , off_t offset , int whence , off_t * newoffset TSRMLS_DC ) ;
static size_t phar_read ( php_stream * stream , char * buf , size_t count TSRMLS_DC ) ;
static size_t phar_readdir ( php_stream * stream , char * buf , size_t count TSRMLS_DC ) ;
static int phar_seek ( php_stream * stream , off_t offset , int whence , off_t * newoffset TSRMLS_DC ) ;
static size_t phar_write ( php_stream * stream , const char * buf , size_t count TSRMLS_DC ) ;
static int phar_flush ( php_stream * stream TSRMLS_DC ) ;
static int phar_stat ( php_stream * stream , php_stream_statbuf * ssb TSRMLS_DC ) ;
static int phar_stream_stat ( php_stream_wrapper * wrapper , char * url , int flags , php_stream_statbuf * ssb , php_stream_context * context TSRMLS_DC ) ;
static php_stream * phar_opendir ( php_stream_wrapper * wrapper , char * filename , char * mode ,
int options , char * * opened_path , php_stream_context * context STREAMS_DC TSRMLS_DC ) ;
/* }}} */
2005-12-05 04:35:38 +08:00
static zend_class_entry * php_archive_entry_ptr ;
2006-01-04 23:50:45 +08:00
static void destroy_phar_data ( void * pDest ) /* { { { */
2005-12-05 04:35:38 +08:00
{
2006-01-05 00:18:31 +08:00
TSRMLS_FETCH ( ) ;
2005-12-05 04:35:38 +08:00
phar_file_data * data = ( phar_file_data * ) pDest ;
2005-12-10 07:37:03 +08:00
efree ( data - > alias ) ;
2006-01-05 19:32:05 +08:00
zend_hash_destroy ( & data - > manifest ) ;
2006-01-04 23:27:18 +08:00
php_stream_close ( data - > fp ) ;
2005-12-05 04:35:38 +08:00
}
2006-01-04 23:50:45 +08:00
/* }}}*/
2005-12-05 04:35:38 +08:00
2006-01-04 23:50:45 +08:00
static void destroy_phar_manifest ( void * pDest ) /* { { { */
2005-12-05 04:35:38 +08:00
{
2006-01-04 23:27:18 +08:00
phar_manifest_entry * entry = ( phar_manifest_entry * ) pDest ;
efree ( entry - > filename ) ;
2005-12-05 04:35:38 +08:00
}
2006-01-04 23:50:45 +08:00
/* }}} */
2005-12-05 04:35:38 +08:00
2006-01-04 23:50:45 +08:00
static phar_internal_file_data * phar_get_filedata ( char * alias , char * path TSRMLS_DC ) /* { { { */
2005-12-05 04:35:38 +08:00
{
2006-01-04 23:27:18 +08:00
phar_file_data * phar ;
2005-12-05 04:35:38 +08:00
phar_internal_file_data * ret ;
2006-01-04 23:27:18 +08:00
phar_manifest_entry * internal_file ;
2005-12-05 04:35:38 +08:00
ret = NULL ;
2006-01-04 23:27:18 +08:00
if ( SUCCESS = = zend_hash_find ( & ( PHAR_GLOBALS - > phar_data ) , alias , strlen ( alias ) , ( void * * ) & phar ) ) {
2006-01-05 19:32:05 +08:00
if ( SUCCESS = = zend_hash_find ( & phar - > manifest , path , strlen ( path ) , ( void * * ) & internal_file ) ) {
2005-12-05 04:35:38 +08:00
ret = ( phar_internal_file_data * ) emalloc ( sizeof ( phar_internal_file_data ) ) ;
2006-01-04 23:27:18 +08:00
ret - > phar = phar ;
ret - > internal_file = internal_file ;
2006-01-06 06:24:41 +08:00
if ( internal_file - > fp ) {
/* transfer ownership */
ret - > fp = internal_file - > fp ;
2006-01-07 01:23:42 +08:00
php_stream_seek ( ret - > fp , 0 , SEEK_SET ) ;
2006-01-06 06:24:41 +08:00
internal_file - > fp = 0 ;
} else {
ret - > fp = 0 ;
}
2005-12-05 04:35:38 +08:00
}
}
return ret ;
}
2006-01-04 23:50:45 +08:00
/* }}} */
2005-12-05 04:35:38 +08:00
2006-01-03 23:50:46 +08:00
/* {{{ proto string apiVersion()
* Returns the api version */
PHP_METHOD ( Phar , apiVersion )
{
2006-01-07 01:32:34 +08:00
RETURN_STRINGL ( " 0.7.1 " , sizeof ( " 0.7.1 " ) - 1 , 1 ) ;
2006-01-03 23:50:46 +08:00
}
/* }}}*/
2005-12-05 04:35:38 +08:00
2006-01-03 23:50:46 +08:00
/* {{{ proto bool canCompress()
* Returns whether phar extension supports compression using zlib */
2006-01-02 06:49:10 +08:00
PHP_METHOD ( Phar , canCompress )
2005-12-07 14:39:03 +08:00
{
# ifdef HAVE_PHAR_ZLIB
RETURN_TRUE ;
# else
RETURN_FALSE ;
# endif
}
2006-01-03 23:50:46 +08:00
/* }}} */
2005-12-05 04:35:38 +08:00
2006-01-04 10:26:15 +08:00
# define MAPPHAR_ALLOC_FAIL(msg) \
php_stream_close ( fp ) ; \
php_error_docref ( NULL TSRMLS_CC , E_ERROR , msg , fname ) ; \
return FAILURE ;
# define MAPPHAR_FAIL(msg) \
efree ( savebuf ) ; \
MAPPHAR_ALLOC_FAIL ( msg )
2006-01-05 08:39:31 +08:00
# ifdef WORDS_BIGENDIAN
# define PHAR_GET_VAL(buffer, var) \
var = ( ( unsigned char ) buffer [ 3 ] ) < < 12 \
+ ( ( unsigned char ) buffer [ 2 ] ) < < 8 \
+ ( ( unsigned char ) buffer [ 1 ] ) < < 4 \
+ ( ( unsigned char ) buffer [ 0 ] ) ; \
buffer + = 4
# else
# define PHAR_GET_VAL(buffer, var) \
2006-01-06 06:24:41 +08:00
var = * ( php_uint32 * ) ( buffer ) ; \
2006-01-05 08:39:31 +08:00
buffer + = 4
# endif
2006-01-04 23:27:18 +08:00
static int phar_open_file ( php_stream * fp , char * fname , int fname_len , char * alias , int alias_len , zend_bool compressed , long halt_offset TSRMLS_DC ) /* { { { */
2005-12-05 04:35:38 +08:00
{
2006-01-05 08:39:31 +08:00
char * buffer , * endbuffer , * savebuf ;
2005-12-05 12:21:36 +08:00
phar_file_data mydata ;
phar_manifest_entry entry ;
2005-12-05 04:35:38 +08:00
php_uint32 manifest_len , manifest_count , manifest_index ;
2005-12-08 15:38:44 +08:00
/* check for ?>\n and increment accordingly */
2005-12-08 15:08:49 +08:00
if ( - 1 = = php_stream_seek ( fp , halt_offset , SEEK_SET ) ) {
2005-12-08 15:34:16 +08:00
MAPPHAR_ALLOC_FAIL ( " cannot seek to __HALT_COMPILER(); location in phar \" %s \" " )
2005-12-08 14:46:02 +08:00
}
2005-12-07 14:39:03 +08:00
if ( FALSE = = ( buffer = ( char * ) emalloc ( 4 ) ) ) {
MAPPHAR_ALLOC_FAIL ( " memory allocation failed in phar \" %s \" " )
}
2005-12-08 15:34:16 +08:00
savebuf = buffer ;
2005-12-07 14:39:03 +08:00
if ( 3 ! = php_stream_read ( fp , buffer , 3 ) ) {
MAPPHAR_FAIL ( " internal corruption of phar \" %s \" (truncated manifest) " )
2005-12-05 04:35:38 +08:00
}
if ( * buffer = = ' ' & & * ( buffer + 1 ) = = ' ? ' & & * ( buffer + 2 ) = = ' > ' ) {
int nextchar ;
2005-12-07 09:18:54 +08:00
halt_offset + = 3 ;
2005-12-07 14:39:03 +08:00
if ( EOF = = ( nextchar = php_stream_getc ( fp ) ) ) {
MAPPHAR_FAIL ( " internal corruption of phar \" %s \" (truncated manifest) " )
}
2005-12-05 04:35:38 +08:00
if ( ( char ) nextchar = = ' \r ' ) {
2005-12-07 14:39:03 +08:00
if ( EOF = = ( nextchar = php_stream_getc ( fp ) ) ) {
MAPPHAR_FAIL ( " internal corruption of phar \" %s \" (truncated manifest) " )
}
2005-12-05 04:35:38 +08:00
halt_offset + + ;
}
if ( ( char ) nextchar = = ' \n ' ) {
halt_offset + + ;
}
}
2005-12-08 15:38:44 +08:00
/* make sure we are at the right location to read the manifest */
2005-12-08 15:08:49 +08:00
if ( - 1 = = php_stream_seek ( fp , halt_offset , SEEK_SET ) ) {
MAPPHAR_FAIL ( " cannot seek to __HALT_COMPILER(); location in phar \" %s \" " )
}
2005-12-05 04:35:38 +08:00
2005-12-08 15:38:44 +08:00
/* read in manifest */
2005-12-07 14:39:03 +08:00
if ( 4 ! = php_stream_read ( fp , buffer , 4 ) ) {
MAPPHAR_FAIL ( " internal corruption of phar \" %s \" (truncated manifest) " )
}
2005-12-08 15:59:18 +08:00
endbuffer = buffer + 5 ;
2006-01-05 08:39:31 +08:00
PHAR_GET_VAL ( buffer , manifest_len ) ;
2005-12-05 04:35:38 +08:00
buffer - = 4 ;
2005-12-08 15:34:16 +08:00
if ( manifest_len > 1048576 ) {
/* prevent serious memory issues by limiting manifest to at most 1 MB in length */
MAPPHAR_FAIL ( " manifest cannot be larger than 1 MB in phar \" %s \" " )
}
2005-12-07 14:39:03 +08:00
if ( FALSE = = ( buffer = ( char * ) erealloc ( buffer , manifest_len ) ) ) {
2005-12-08 15:34:16 +08:00
MAPPHAR_FAIL ( " memory allocation failed in phar \" %s \" " )
2005-12-07 14:39:03 +08:00
}
2005-12-05 04:35:38 +08:00
savebuf = buffer ;
2005-12-08 15:38:44 +08:00
/* set the test pointer */
2005-12-05 04:35:38 +08:00
endbuffer = buffer + manifest_len ;
2005-12-08 15:38:44 +08:00
/* retrieve manifest */
2005-12-07 14:39:03 +08:00
if ( manifest_len ! = php_stream_read ( fp , buffer , manifest_len ) ) {
MAPPHAR_FAIL ( " internal corruption of phar \" %s \" (truncated manifest) " )
}
2005-12-08 15:38:44 +08:00
/* extract the number of entries */
2006-01-05 08:39:31 +08:00
if ( buffer + 4 > endbuffer ) {
MAPPHAR_FAIL ( " internal corruption of phar \" %s \" (buffer overrun) " ) ;
}
PHAR_GET_VAL ( buffer , manifest_count ) ;
2005-12-10 07:37:03 +08:00
/* we have 5 32-bit items at least */
if ( manifest_count > ( manifest_len / ( 4 * 5 ) ) ) {
2005-12-10 03:55:29 +08:00
/* prevent serious memory issues */
MAPPHAR_FAIL ( " too many manifest entries for size of manifest in phar \" %s \" " )
}
2005-12-08 15:38:44 +08:00
/* set up our manifest */
2006-01-05 19:32:05 +08:00
zend_hash_init ( & mydata . manifest , sizeof ( phar_manifest_entry ) ,
2005-12-05 04:35:38 +08:00
zend_get_hash_value , destroy_phar_manifest , 0 ) ;
for ( manifest_index = 0 ; manifest_index < manifest_count ; manifest_index + + ) {
2006-01-05 08:39:31 +08:00
if ( buffer + 4 > endbuffer ) {
2005-12-07 14:39:03 +08:00
MAPPHAR_FAIL ( " internal corruption of phar \" %s \" (truncated manifest) " )
}
2006-01-05 08:39:31 +08:00
PHAR_GET_VAL ( buffer , entry . filename_len ) ;
if ( buffer + entry . filename_len + 16 > endbuffer ) {
MAPPHAR_FAIL ( " internal corruption of phar \" %s \" (buffer overrun) " ) ;
}
2006-01-04 23:27:18 +08:00
entry . filename = estrndup ( buffer , entry . filename_len ) ;
2005-12-05 12:21:36 +08:00
buffer + = entry . filename_len ;
2006-01-05 08:39:31 +08:00
PHAR_GET_VAL ( buffer , entry . uncompressed_filesize ) ;
PHAR_GET_VAL ( buffer , entry . timestamp ) ;
PHAR_GET_VAL ( buffer , entry . offset_within_phar ) ;
PHAR_GET_VAL ( buffer , entry . compressed_filesize ) ;
2006-01-06 06:24:41 +08:00
if ( entry . uncompressed_filesize + 8 ! = entry . compressed_filesize ) {
2006-01-07 01:23:42 +08:00
# ifndef HAVE_PHAR_ZLIB
if ( ! compressed ) {
MAPPHAR_FAIL ( " zlib extension is required for compressed .phar file \" %s \" " ) ;
}
# endif
2006-01-05 08:55:55 +08:00
compressed = 1 ;
2006-01-06 06:24:41 +08:00
}
2005-12-10 08:00:00 +08:00
entry . crc_checked = 0 ;
2006-01-06 06:24:41 +08:00
entry . fp = NULL ;
2005-12-10 07:55:52 +08:00
if ( entry . compressed_filesize < 9 ) {
MAPPHAR_FAIL ( " internal corruption of phar \" %s \" (file size in phar is not large enough) " )
}
2006-01-05 19:32:05 +08:00
zend_hash_add ( & mydata . manifest , entry . filename , entry . filename_len , & entry ,
2005-12-05 04:35:38 +08:00
sizeof ( phar_manifest_entry ) , NULL ) ;
}
2006-01-04 23:27:18 +08:00
mydata . filename = estrndup ( fname , fname_len ) ;
mydata . filename_len = fname_len ;
2005-12-10 07:37:03 +08:00
/* alias is auto-efreed after returning, so we must dupe it */
mydata . alias = estrndup ( alias , alias_len ) ;
2005-12-05 12:21:36 +08:00
mydata . alias_len = alias_len ;
mydata . internal_file_start = manifest_len + halt_offset + 4 ;
mydata . is_compressed = compressed ;
2006-01-04 23:27:18 +08:00
mydata . fp = fp ;
2005-12-05 16:31:05 +08:00
zend_hash_add ( & ( PHAR_GLOBALS - > phar_data ) , alias , alias_len , & mydata ,
2005-12-05 04:35:38 +08:00
sizeof ( phar_file_data ) , NULL ) ;
efree ( savebuf ) ;
2006-01-04 10:26:15 +08:00
return SUCCESS ;
}
/* }}} */
2006-01-04 23:27:18 +08:00
static int phar_open_filename ( char * fname , int fname_len , char * alias , int alias_len , zend_bool compressed TSRMLS_DC ) /* { { { */
2006-01-04 10:26:15 +08:00
{
const char token [ ] = " __HALT_COMPILER(); " ;
char * pos , buffer [ 1024 + sizeof ( token ) ] ;
const long readsize = sizeof ( buffer ) - sizeof ( token ) ;
const long tokenlen = sizeof ( token ) - 1 ;
int result ;
long halt_offset ;
php_stream * fp ;
2006-01-05 19:32:05 +08:00
if ( PG ( safe_mode ) & & ( ! php_checkuid ( fname , NULL , CHECKUID_ALLOW_ONLY_FILE ) ) ) {
return FAILURE ;
}
if ( php_check_open_basedir ( fname TSRMLS_CC ) ) {
return FAILURE ;
}
2006-01-04 10:26:15 +08:00
fp = php_stream_open_wrapper ( fname , " rb " , IGNORE_URL | STREAM_MUST_SEEK | REPORT_ERRORS , NULL ) ;
if ( ! fp ) {
php_error_docref ( NULL TSRMLS_CC , E_ERROR , " unable to open phar for reading \" %s \" " , fname ) ;
return FAILURE ;
}
/* Maybe it's better to compile the file instead of just searching, */
/* but we only want the offset. So we want a .re scanner to find it. */
if ( - 1 = = php_stream_seek ( fp , 0 , SEEK_SET ) ) {
2006-01-05 08:39:31 +08:00
MAPPHAR_ALLOC_FAIL ( " cannot rewind phar \" %s \" " )
2006-01-04 10:26:15 +08:00
}
buffer [ sizeof ( buffer ) - 1 ] = ' \0 ' ;
memset ( buffer , 32 , sizeof ( token ) ) ;
halt_offset = 0 ;
while ( ! php_stream_eof ( fp ) ) {
if ( php_stream_read ( fp , buffer + tokenlen , readsize ) < 0 ) {
MAPPHAR_ALLOC_FAIL ( " internal corruption of phar \" %s \" (truncated manifest) " )
}
if ( ( pos = strstr ( buffer , token ) ) ! = NULL ) {
2006-01-05 08:39:31 +08:00
halt_offset + = ( pos - buffer ) ; /* no -tokenlen+tokenlen here */
2006-01-04 23:27:18 +08:00
result = phar_open_file ( fp , fname , fname_len , alias , alias_len , compressed , halt_offset TSRMLS_CC ) ;
if ( result = = FAILURE ) {
php_stream_close ( fp ) ;
}
2006-01-04 10:26:15 +08:00
return result ;
}
halt_offset + = readsize ;
memmove ( buffer , buffer + tokenlen , readsize + 1 ) ;
}
MAPPHAR_ALLOC_FAIL ( " internal corruption of phar \" %s \" (__HALT_COMPILER(); not found) " )
2005-12-05 12:47:29 +08:00
php_stream_close ( fp ) ;
2006-01-04 10:26:15 +08:00
return FAILURE ;
}
/* }}} */
2006-01-04 17:26:32 +08:00
static php_url * phar_open_url ( php_stream_wrapper * wrapper , char * filename , char * mode , int options TSRMLS_DC ) /* { { { */
2006-01-04 10:26:15 +08:00
{
php_url * resource ;
char * pos_p , * pos_z , * ext_str , * alias ;
int len , ext_len ;
if ( ! strncasecmp ( filename , " phar:// " , 7 ) ) {
filename + = 7 ;
pos_p = strstr ( filename , " .phar.php " ) ;
pos_z = strstr ( filename , " .phar.gz " ) ;
if ( pos_p ) {
if ( pos_z ) {
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: invalid url \" %s \" (cannot contain .phar.php and .phar.gz) " , filename ) ;
return NULL ;
}
ext_str = pos_p ;
ext_len = 9 ;
} else if ( pos_z ) {
ext_str = pos_z ;
ext_len = 8 ;
2006-01-05 08:53:41 +08:00
} else if ( ( pos_p = strstr ( filename , " .phar " ) ) ! = NULL ) {
ext_str = pos_p ;
ext_len = 5 ;
2006-01-04 10:26:15 +08:00
} else {
2006-01-05 08:53:41 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: invalid url \" %s \" (filename extension must be .phar.php, .phar.gz or .phar) " , filename ) ;
2006-01-04 10:26:15 +08:00
return NULL ;
}
resource = emalloc ( sizeof ( php_url ) ) ;
resource - > scheme = estrndup ( " phar " , 4 ) ;
len = ext_str - filename + ext_len ;
alias = resource - > host = estrndup ( filename , len ) ;
if ( ext_str [ ext_len ] ) {
resource - > path = estrdup ( ext_str + ext_len ) ;
} else {
resource - > path = estrndup ( " / " , 1 ) ;
}
# if MBO_0
if ( resource ) {
fprintf ( stderr , " Alias: %s \n " , alias ) ;
fprintf ( stderr , " Compressed:%s \n " , pos_z ? " yes " : " no " ) ;
fprintf ( stderr , " Scheme: %s \n " , resource - > scheme ) ;
/* fprintf(stderr, "User: %s\n", resource->user);*/
/* fprintf(stderr, "Pass: %s\n", resource->pass ? "***" : NULL);*/
fprintf ( stderr , " Host: %s \n " , resource - > host ) ;
/* fprintf(stderr, "Port: %d\n", resource->port);*/
fprintf ( stderr , " Path: %s \n " , resource - > path ) ;
/* fprintf(stderr, "Query: %s\n", resource->query);*/
/* fprintf(stderr, "Fragment: %s\n", resource->fragment);*/
}
# endif
2006-01-04 23:27:18 +08:00
if ( phar_open_filename ( resource - > host , len , alias , len , pos_z ? 1 : 0 TSRMLS_CC ) = = FAILURE )
2006-01-04 10:26:15 +08:00
{
php_url_free ( resource ) ;
return NULL ;
}
return resource ;
}
return NULL ;
2005-12-05 04:35:38 +08:00
}
/* }}} */
2006-01-04 17:26:32 +08:00
static int phar_open_compiled_file ( char * alias , int alias_len , zend_bool compressed TSRMLS_DC ) /* { { { */
2006-01-04 10:26:15 +08:00
{
char * fname ;
int result ;
long halt_offset ;
zval * halt_constant ;
php_stream * fp ;
fname = zend_get_executed_filename ( TSRMLS_C ) ;
if ( ! strcmp ( fname , " [no active file] " ) ) {
php_error_docref ( NULL TSRMLS_CC , E_ERROR , " cannot initialize a phar outside of PHP execution " ) ;
return FAILURE ;
}
MAKE_STD_ZVAL ( halt_constant ) ;
if ( 0 = = zend_get_constant ( " __COMPILER_HALT_OFFSET__ " , 24 , halt_constant TSRMLS_CC ) ) {
zval_dtor ( halt_constant ) ;
FREE_ZVAL ( halt_constant ) ;
php_error_docref ( NULL TSRMLS_CC , E_ERROR , " __HALT_COMPILER(); must be declared in a phar " ) ;
return FAILURE ;
}
halt_offset = Z_LVAL ( * halt_constant ) ;
zval_dtor ( halt_constant ) ;
FREE_ZVAL ( halt_constant ) ;
fp = php_stream_open_wrapper ( fname , " rb " , IGNORE_URL | STREAM_MUST_SEEK | REPORT_ERRORS , NULL ) ;
if ( ! fp ) {
php_error_docref ( NULL TSRMLS_CC , E_ERROR , " unable to open phar for reading \" %s \" " , fname ) ;
return FAILURE ;
}
2006-01-04 23:27:18 +08:00
result = phar_open_file ( fp , fname , strlen ( fname ) , alias , alias_len , compressed , halt_offset TSRMLS_CC ) ;
if ( result = = FAILURE ) {
php_stream_close ( fp ) ;
}
2006-01-04 10:26:15 +08:00
return result ;
}
/* }}} */
/* {{{ proto mixed Phar::mapPhar(string alias, mixed compressed, [mixed unused [, mixed unused]])
2006-01-06 23:31:42 +08:00
* Maps the currently executed file ( a phar ) with the given alias */
2006-01-04 10:26:15 +08:00
PHP_METHOD ( Phar , mapPhar )
{
char * alias ;
zend_bool compressed ;
int alias_len ;
zval * * unused1 , * * unused2 ;
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " sb|zz " , & alias , & alias_len , & compressed , & unused1 , & unused2 ) = = FAILURE ) {
return ;
}
# ifndef HAVE_PHAR_ZLIB
if ( compressed ) {
php_error_docref ( NULL TSRMLS_CC , E_ERROR , " zlib extension is required for compressed .phar files " ) ;
return ;
}
# endif
2006-01-04 17:26:32 +08:00
RETURN_BOOL ( phar_open_compiled_file ( alias , alias_len , compressed TSRMLS_CC ) = = SUCCESS ) ;
2006-01-04 10:26:15 +08:00
} /* }}} */
2006-01-07 01:23:42 +08:00
/* {{{ proto mixed Phar::loadPhar(string url, string alias)
* Loads a phar archive with an alias */
PHP_METHOD ( Phar , loadPhar )
{
char * fname , * alias ;
int fname_len , alias_len ;
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " ss " , & fname , & fname_len , & alias , & alias_len ) = = FAILURE ) {
return ;
}
RETURN_BOOL ( phar_open_filename ( fname , fname_len , alias , alias_len , 0 TSRMLS_CC ) = = SUCCESS ) ;
} /* }}} */
2005-12-05 04:35:38 +08:00
static php_stream_ops phar_ops = {
2005-12-08 15:38:44 +08:00
phar_write , /* write (does nothing) */
phar_read , /* read */
phar_close , /* close */
phar_flush , /* flush (does nothing) */
2005-12-05 04:35:38 +08:00
" phar stream " ,
2005-12-12 14:17:16 +08:00
phar_seek , /* seek */
2005-12-08 15:38:44 +08:00
NULL , /* cast */
phar_stat , /* stat */
NULL , /* set option */
2005-12-05 04:35:38 +08:00
} ;
static php_stream_ops phar_dir_ops = {
2005-12-08 15:38:44 +08:00
phar_write , /* write (does nothing) */
phar_readdir , /* read */
phar_closedir , /* close */
phar_flush , /* flush (does nothing) */
2005-12-05 04:35:38 +08:00
" phar stream " ,
2006-01-04 10:26:15 +08:00
phar_seekdir , /* seek */
2005-12-08 15:38:44 +08:00
NULL , /* cast */
2005-12-11 15:25:30 +08:00
phar_stat , /* stat */
2005-12-08 15:38:44 +08:00
NULL , /* set option */
2005-12-05 04:35:38 +08:00
} ;
static php_stream_wrapper_ops phar_stream_wops = {
php_stream_phar_url_wrapper ,
NULL , /* stream_close */
2005-12-08 15:38:44 +08:00
NULL , /* php_stream_phar_stat, */
2005-12-05 04:35:38 +08:00
phar_stream_stat , /* stat_url */
phar_opendir , /* opendir */
" phar " ,
NULL , /* unlink */
NULL , /* rename */
NULL , /* create directory */
NULL /* remove directory */
} ;
php_stream_wrapper php_stream_phar_wrapper = {
& phar_stream_wops ,
NULL ,
0 /* is_url */
} ;
2006-01-06 06:24:41 +08:00
static int phar_postprocess_file ( php_stream_wrapper * wrapper , int options , phar_internal_file_data * idata , unsigned long crc32 TSRMLS_DC ) /* { { { */
2005-12-05 04:35:38 +08:00
{
unsigned int crc = ~ 0 ;
2006-01-06 06:24:41 +08:00
int len = idata - > internal_file - > uncompressed_filesize ;
char c ;
2006-01-05 08:39:31 +08:00
2006-01-06 06:24:41 +08:00
php_stream_seek ( idata - > fp , 0 , SEEK_SET ) ;
while ( len - - ) {
php_stream_read ( idata - > fp , & c , 1 ) ;
CRC32 ( crc , c ) ;
2005-12-05 04:35:38 +08:00
}
2006-01-06 06:24:41 +08:00
php_stream_seek ( idata - > fp , 0 , SEEK_SET ) ;
2005-12-05 04:35:38 +08:00
if ( ~ crc = = crc32 ) {
2006-01-06 06:24:41 +08:00
return SUCCESS ;
2005-12-05 04:35:38 +08:00
} else {
2006-01-06 06:24:41 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: internal corruption of phar \" %s \" (crc32 mismatch on file \" %s \" ) " , idata - > phar - > filename , idata - > internal_file - > filename ) ;
return FAILURE ;
2005-12-05 04:35:38 +08:00
}
}
2006-01-03 23:59:07 +08:00
/* }}} */
2005-12-05 04:35:38 +08:00
2006-01-04 23:50:45 +08:00
static php_stream * php_stream_phar_url_wrapper ( php_stream_wrapper * wrapper , char * path , char * mode , int options , char * * opened_path , php_stream_context * context STREAMS_DC TSRMLS_DC ) /* { { { */
2005-12-05 04:35:38 +08:00
{
phar_internal_file_data * idata ;
php_stream * stream = NULL ;
char * internal_file ;
char * buffer ;
2006-01-04 23:27:18 +08:00
char * filedata ;
2006-01-06 06:24:41 +08:00
char tmpbuf [ 8 ] ;
2005-12-05 04:35:38 +08:00
php_url * resource = NULL ;
2005-12-05 12:47:29 +08:00
php_stream * fp ;
2005-12-10 07:55:52 +08:00
int status ;
2005-12-05 09:07:02 +08:00
# ifdef HAVE_PHAR_ZLIB
2005-12-10 07:55:52 +08:00
unsigned long crc32 ;
2006-01-05 08:39:31 +08:00
php_uint32 actual_length ;
char * savebuf ;
2005-12-05 04:35:38 +08:00
/* borrowed from zlib.c gzinflate() function */
2005-12-10 07:37:03 +08:00
php_uint32 offset ;
2005-12-05 04:35:38 +08:00
unsigned long length ;
char * s1 = NULL ;
z_stream zstream ;
# endif
2005-12-07 14:39:03 +08:00
resource = php_url_parse ( path ) ;
2006-01-04 10:26:15 +08:00
2006-01-04 17:26:32 +08:00
if ( ! resource & & ( resource = phar_open_url ( wrapper , path , mode , options TSRMLS_CC ) ) = = NULL ) {
2006-01-04 10:26:15 +08:00
return NULL ;
}
2006-01-02 04:40:43 +08:00
/* we must have at the very least phar://alias.phar/internalfile.php */
2005-12-05 04:35:38 +08:00
if ( ! resource | | ! resource - > scheme | | ! resource - > host | | ! resource - > path ) {
2005-12-07 14:39:03 +08:00
if ( resource ) {
php_url_free ( resource ) ;
}
2005-12-11 08:00:31 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: invalid url \" %s \" " , path ) ;
2005-12-07 14:39:03 +08:00
return NULL ;
2005-12-05 04:35:38 +08:00
}
if ( strcasecmp ( " phar " , resource - > scheme ) ) {
2005-12-07 14:39:03 +08:00
if ( resource ) {
php_url_free ( resource ) ;
}
2005-12-11 08:00:31 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: not a phar stream url \" %s \" " , path ) ;
2005-12-07 14:39:03 +08:00
return NULL ;
2005-12-05 04:35:38 +08:00
}
2005-12-10 07:37:03 +08:00
/* strip leading "/" */
2006-01-04 23:27:18 +08:00
internal_file = estrdup ( resource - > path + 1 ) ;
2005-12-07 09:18:54 +08:00
if ( NULL = = ( idata = phar_get_filedata ( resource - > host , internal_file TSRMLS_CC ) ) ) {
2006-01-01 15:40:36 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: \" %s \" is not a file in phar \" %s \" " , internal_file , resource - > host ) ;
2005-12-10 07:37:03 +08:00
efree ( internal_file ) ;
2006-01-04 23:27:18 +08:00
php_url_free ( resource ) ;
2005-12-05 04:35:38 +08:00
return NULL ;
}
2006-01-04 23:27:18 +08:00
# if MBO_0
2006-01-06 06:24:41 +08:00
fprintf ( stderr , " Pharname: %s \n " , idata - > phar - > filename ) ;
fprintf ( stderr , " Filename: %s \n " , internal_file ) ;
fprintf ( stderr , " Entry: %s \n " , idata - > internal_file - > filename ) ;
2006-01-04 23:27:18 +08:00
fprintf ( stderr , " Size: %u \n " , idata - > internal_file - > uncompressed_filesize ) ;
fprintf ( stderr , " Offset: %u \n " , idata - > internal_file - > offset_within_phar ) ;
2006-01-06 06:24:41 +08:00
fprintf ( stderr , " Cached: %s \n " , idata - > internal_file - > filedata ? " yes " : " no " ) ;
2006-01-04 23:27:18 +08:00
# endif
2005-12-05 04:35:38 +08:00
2006-01-04 23:27:18 +08:00
/* do we have the data already? */
2006-01-06 06:24:41 +08:00
if ( idata - > fp ) {
2006-01-04 23:27:18 +08:00
stream = php_stream_alloc ( & phar_ops , idata , NULL , mode ) ;
efree ( internal_file ) ;
return stream ;
}
2005-12-05 04:35:38 +08:00
2006-01-04 23:27:18 +08:00
if ( PG ( safe_mode ) & & ( ! php_checkuid ( idata - > phar - > filename , NULL , CHECKUID_ALLOW_ONLY_FILE ) ) ) {
efree ( idata ) ;
2005-12-10 07:37:03 +08:00
efree ( internal_file ) ;
2005-12-05 12:47:29 +08:00
return NULL ;
}
2006-01-04 23:27:18 +08:00
if ( php_check_open_basedir ( idata - > phar - > filename TSRMLS_CC ) ) {
efree ( idata ) ;
2005-12-10 07:37:03 +08:00
efree ( internal_file ) ;
2005-12-05 12:47:29 +08:00
return NULL ;
2005-12-05 04:35:38 +08:00
}
2005-12-05 12:47:29 +08:00
2006-01-04 23:27:18 +08:00
fp = idata - > phar - > fp ;
2005-12-05 12:47:29 +08:00
2005-12-05 04:35:38 +08:00
if ( ! fp ) {
2006-01-04 23:27:18 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: cannot open phar \" %s \" " , idata - > phar - > filename ) ;
2005-12-05 04:35:38 +08:00
efree ( idata ) ;
2005-12-10 07:37:03 +08:00
efree ( internal_file ) ;
2005-12-05 04:35:38 +08:00
return NULL ;
}
/* seek to start of internal file and read it */
2006-01-04 23:27:18 +08:00
offset = idata - > phar - > internal_file_start + idata - > internal_file - > offset_within_phar ;
if ( - 1 = = php_stream_seek ( fp , offset , SEEK_SET ) ) {
2005-12-11 08:00:31 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: internal corruption of phar \" %s \" (cannot seek to start of file \" %s \" at offset \" %d \" ) " ,
2006-01-04 23:27:18 +08:00
idata - > phar - > filename , internal_file , offset ) ;
efree ( idata ) ;
2005-12-11 08:00:31 +08:00
efree ( internal_file ) ;
2005-12-10 07:37:03 +08:00
return NULL ;
}
2006-01-04 23:27:18 +08:00
if ( idata - > phar - > is_compressed ) {
2005-12-05 09:07:02 +08:00
# ifdef HAVE_PHAR_ZLIB
2005-12-05 04:35:38 +08:00
buffer = ( char * ) emalloc ( idata - > internal_file - > compressed_filesize ) ;
if ( idata - > internal_file - > compressed_filesize ! =
2005-12-05 12:47:29 +08:00
php_stream_read ( fp , buffer , idata - > internal_file - > compressed_filesize ) ) {
2006-01-04 23:27:18 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: internal corruption of phar \" %s \" (filesize mismatch on file \" %s \" ) " , idata - > phar - > filename , internal_file ) ;
2005-12-05 04:35:38 +08:00
efree ( idata ) ;
2005-12-11 08:00:31 +08:00
efree ( internal_file ) ;
2006-01-04 23:27:18 +08:00
efree ( buffer ) ;
2005-12-05 04:35:38 +08:00
return NULL ;
}
savebuf = buffer ;
2006-01-05 08:39:31 +08:00
PHAR_GET_VAL ( buffer , crc32 ) ;
PHAR_GET_VAL ( buffer , actual_length ) ;
2005-12-05 04:35:38 +08:00
/* borrowed from zlib.c gzinflate() function */
zstream . zalloc = ( alloc_func ) Z_NULL ;
zstream . zfree = ( free_func ) Z_NULL ;
length = idata - > internal_file - > uncompressed_filesize ;
do {
2006-01-04 23:27:18 +08:00
filedata = ( char * ) erealloc ( s1 , length ) ;
2005-12-05 04:35:38 +08:00
2006-01-04 23:27:18 +08:00
if ( ! filedata & & s1 ) {
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: internal corruption of phar \" %s \" (corrupted zlib compression of file \" %s \" ) " , idata - > phar - > filename , internal_file ) ;
2005-12-05 04:35:38 +08:00
efree ( s1 ) ;
efree ( savebuf ) ;
efree ( idata ) ;
2005-12-11 08:00:31 +08:00
efree ( internal_file ) ;
2005-12-05 04:35:38 +08:00
return NULL ;
}
zstream . next_in = ( Bytef * ) buffer ;
zstream . avail_in = ( uInt ) idata - > internal_file - > compressed_filesize ;
2006-01-04 23:27:18 +08:00
zstream . next_out = filedata ;
2005-12-05 04:35:38 +08:00
zstream . avail_out = ( uInt ) length ;
/* init with -MAX_WBITS disables the zlib internal headers */
status = inflateInit2 ( & zstream , - MAX_WBITS ) ;
if ( status = = Z_OK ) {
status = inflate ( & zstream , Z_FINISH ) ;
if ( status ! = Z_STREAM_END ) {
inflateEnd ( & zstream ) ;
if ( status = = Z_OK ) {
status = Z_BUF_ERROR ;
}
} else {
status = inflateEnd ( & zstream ) ;
}
}
2006-01-04 23:27:18 +08:00
s1 = filedata ;
2005-12-05 04:35:38 +08:00
} while ( status = = Z_BUF_ERROR ) ;
if ( status ! = Z_OK ) {
efree ( savebuf ) ;
2006-01-04 23:27:18 +08:00
efree ( filedata ) ;
2005-12-05 04:35:38 +08:00
efree ( idata ) ;
2005-12-11 07:19:32 +08:00
efree ( internal_file ) ;
2005-12-12 05:44:44 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: decompression failed " ) ;
2005-12-05 04:35:38 +08:00
return NULL ;
}
2005-12-07 14:39:03 +08:00
2005-12-05 12:21:36 +08:00
efree ( savebuf ) ;
2005-12-08 15:38:44 +08:00
/* check length */
2005-12-05 04:35:38 +08:00
if ( actual_length ! = idata - > internal_file - > uncompressed_filesize ) {
2006-01-04 23:27:18 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: internal corruption of phar \" %s \" (filesize mismatch on file \" %s \" ) " , idata - > phar - > filename , internal_file ) ;
2005-12-11 08:00:31 +08:00
efree ( internal_file ) ;
2006-01-04 23:27:18 +08:00
efree ( filedata ) ;
2006-01-02 06:11:03 +08:00
efree ( idata ) ;
2005-12-11 08:00:31 +08:00
return NULL ;
2005-12-05 04:35:38 +08:00
}
2006-01-06 06:24:41 +08:00
/* need to copy filedata to stream now */
idata - > fp = php_stream_temp_open ( 0 , PHP_STREAM_MAX_MEM , filedata , actual_length ) ;
efree ( filedata ) ;
2005-12-10 07:55:52 +08:00
/* check crc32/filesize */
2006-01-06 06:24:41 +08:00
if ( ! idata - > internal_file - > crc_checked
& & phar_postprocess_file ( wrapper , options , idata , crc32 TSRMLS_CC ) ! = SUCCESS ) {
efree ( idata ) ;
efree ( internal_file ) ;
2005-12-05 04:35:38 +08:00
}
2006-01-06 06:24:41 +08:00
idata - > internal_file - > crc_checked = 1 ;
2005-12-05 04:35:38 +08:00
# else
2005-12-11 08:00:31 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " zlib extension must be enabled for compressed .phar files " ) ;
efree ( internal_file ) ;
2005-12-07 14:39:03 +08:00
return NULL ;
2005-12-05 04:35:38 +08:00
# endif
2006-01-02 06:11:03 +08:00
} else { /* from here is for non-compressed */
2006-01-06 06:24:41 +08:00
buffer = & tmpbuf [ 0 ] ;
php_stream_read ( fp , buffer , 8 ) ;
PHAR_GET_VAL ( buffer , crc32 ) ;
PHAR_GET_VAL ( buffer , actual_length ) ;
if ( actual_length ! = idata - > internal_file - > uncompressed_filesize ) {
2006-01-04 23:27:18 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: internal corruption of phar \" %s \" (actual filesize mismatch on file \" %s \" ) " , idata - > phar - > filename , internal_file ) ;
2006-01-06 06:24:41 +08:00
efree ( idata ) ;
efree ( internal_file ) ;
return NULL ;
}
/* bypass to temp stream */
idata - > fp = php_stream_temp_new ( ) ;
if ( php_stream_copy_to_stream ( fp , idata - > fp , actual_length ) ! = actual_length ) {
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: internal corruption of phar \" %s \" (actual filesize mismatch on file \" %s \" ) " , idata - > phar - > filename , internal_file ) ;
php_stream_close ( idata - > fp ) ;
2005-12-05 04:35:38 +08:00
efree ( idata ) ;
2005-12-11 08:00:31 +08:00
efree ( internal_file ) ;
2005-12-05 04:35:38 +08:00
return NULL ;
}
2005-12-08 15:38:44 +08:00
/* check length, crc32 */
2006-01-06 06:24:41 +08:00
if ( ! idata - > internal_file - > crc_checked
& & phar_postprocess_file ( wrapper , options , idata , crc32 TSRMLS_CC ) ! = SUCCESS ) {
php_stream_close ( idata - > fp ) ;
efree ( idata ) ;
efree ( internal_file ) ;
return NULL ;
2005-12-10 07:55:52 +08:00
}
2006-01-06 06:24:41 +08:00
idata - > internal_file - > crc_checked = 1 ;
2005-12-05 04:35:38 +08:00
}
stream = php_stream_alloc ( & phar_ops , idata , NULL , mode ) ;
2005-12-10 07:37:03 +08:00
efree ( internal_file ) ;
2005-12-05 04:35:38 +08:00
return stream ;
}
/* }}} */
2006-01-04 23:50:45 +08:00
static int phar_close ( php_stream * stream , int close_handle TSRMLS_DC ) /* { { { */
2005-12-05 04:35:38 +08:00
{
phar_internal_file_data * data = ( phar_internal_file_data * ) stream - > abstract ;
2006-01-06 06:24:41 +08:00
if ( data - > fp ) {
if ( data - > internal_file - > fp ) {
php_stream_close ( data - > fp ) ;
} else {
data - > internal_file - > fp = data - > fp ;
}
}
2005-12-05 04:35:38 +08:00
efree ( data ) ;
return 0 ;
}
2006-01-03 23:59:07 +08:00
/* }}} */
2005-12-05 04:35:38 +08:00
2006-01-04 23:50:45 +08:00
static int phar_closedir ( php_stream * stream , int close_handle TSRMLS_DC ) /* { { { */
2005-12-05 16:31:05 +08:00
{
HashTable * data = ( HashTable * ) stream - > abstract ;
2006-01-02 06:19:51 +08:00
if ( data )
{
zend_hash_destroy ( data ) ;
FREE_HASHTABLE ( data ) ;
stream - > abstract = NULL ;
}
2005-12-05 16:31:05 +08:00
return 0 ;
}
2006-01-03 23:59:07 +08:00
/* }}} */
2005-12-05 16:31:05 +08:00
2006-01-04 23:50:45 +08:00
static int phar_seekdir ( php_stream * stream , off_t offset , int whence , off_t * newoffset TSRMLS_DC ) /* { { { */
2006-01-04 10:26:15 +08:00
{
HashTable * data = ( HashTable * ) stream - > abstract ;
if ( data )
{
if ( whence = = SEEK_END ) {
whence = SEEK_SET ;
offset = zend_hash_num_elements ( data ) + offset ;
}
if ( whence = = SEEK_SET ) {
zend_hash_internal_pointer_reset ( data ) ;
}
if ( offset < 0 ) {
php_stream_wrapper_log_error ( stream - > wrapper , stream - > flags TSRMLS_CC , " phar error: cannot seek because the resulting seek is negative " ) ;
return - 1 ;
} else {
* newoffset = 0 ;
while ( * newoffset < offset & & zend_hash_move_forward ( data ) = = SUCCESS ) {
( * newoffset ) + + ;
}
return 0 ;
}
}
return - 1 ;
}
/* }}} */
2006-01-04 23:50:45 +08:00
static size_t phar_read ( php_stream * stream , char * buf , size_t count TSRMLS_DC ) /* { { { */
2005-12-05 04:35:38 +08:00
{
phar_internal_file_data * data = ( phar_internal_file_data * ) stream - > abstract ;
2006-01-06 06:24:41 +08:00
return php_stream_read ( data - > fp , buf , count ) ;
2005-12-05 04:35:38 +08:00
}
2006-01-03 23:59:07 +08:00
/* }}} */
2005-12-05 04:35:38 +08:00
2006-01-04 23:50:45 +08:00
static size_t phar_readdir ( php_stream * stream , char * buf , size_t count TSRMLS_DC ) /* { { { */
2005-12-05 04:35:38 +08:00
{
size_t to_read ;
2005-12-05 16:31:05 +08:00
HashTable * data = ( HashTable * ) stream - > abstract ;
char * key ;
uint keylen ;
ulong unused ;
2005-12-05 04:35:38 +08:00
2005-12-05 16:31:05 +08:00
if ( FAILURE = = zend_hash_has_more_elements ( data ) ) {
return 0 ;
}
if ( HASH_KEY_NON_EXISTANT = = zend_hash_get_current_key_ex ( data , & key , & keylen , & unused , 0 , NULL ) ) {
2005-12-05 04:35:38 +08:00
return 0 ;
}
2005-12-05 16:31:05 +08:00
zend_hash_move_forward ( data ) ;
to_read = MIN ( keylen , count ) ;
if ( to_read = = 0 | | count < keylen ) {
return 0 ;
}
memset ( buf , 0 , sizeof ( php_stream_dirent ) ) ;
memcpy ( ( ( php_stream_dirent * ) buf ) - > d_name , key , to_read ) ;
( ( php_stream_dirent * ) buf ) - > d_name [ to_read + 1 ] = ' \0 ' ;
2005-12-05 04:35:38 +08:00
2005-12-05 16:31:05 +08:00
return sizeof ( php_stream_dirent ) ;
2005-12-05 04:35:38 +08:00
}
2006-01-03 23:59:07 +08:00
/* }}} */
2005-12-05 04:35:38 +08:00
2006-01-04 23:50:45 +08:00
static int phar_seek ( php_stream * stream , off_t offset , int whence , off_t * newoffset TSRMLS_DC ) /* { { { */
2005-12-05 04:35:38 +08:00
{
phar_internal_file_data * data = ( phar_internal_file_data * ) stream - > abstract ;
2006-01-06 06:24:41 +08:00
int res = php_stream_seek ( data - > fp , offset , whence ) ;
* newoffset = php_stream_tell ( data - > fp ) ;
return res ;
2005-12-05 04:35:38 +08:00
}
2006-01-03 23:59:07 +08:00
/* }}} */
2005-12-05 04:35:38 +08:00
2006-01-04 23:50:45 +08:00
static size_t phar_write ( php_stream * stream , const char * buf , size_t count TSRMLS_DC ) /* { { { */
2005-12-05 04:35:38 +08:00
{
return 0 ;
}
2006-01-03 23:59:07 +08:00
/* }}} */
2005-12-05 04:35:38 +08:00
2006-01-04 23:50:45 +08:00
static int phar_flush ( php_stream * stream TSRMLS_DC ) /* { { { */
2005-12-05 04:35:38 +08:00
{
return EOF ;
}
2006-01-03 23:59:07 +08:00
/* }}} */
2005-12-05 04:35:38 +08:00
2006-01-01 15:40:36 +08:00
static void phar_dostat ( phar_manifest_entry * data , php_stream_statbuf * ssb , zend_bool is_dir , char * alias ,
int alias_len TSRMLS_DC ) ;
2005-12-05 04:35:38 +08:00
2006-01-04 23:50:45 +08:00
static int phar_stat ( php_stream * stream , php_stream_statbuf * ssb TSRMLS_DC ) /* { { { */
2005-12-05 04:35:38 +08:00
{
2005-12-07 09:18:54 +08:00
phar_internal_file_data * data ;
2005-12-05 04:35:38 +08:00
/* If ssb is NULL then someone is misbehaving */
if ( ! ssb ) return - 1 ;
2005-12-07 09:18:54 +08:00
data = ( phar_internal_file_data * ) stream - > abstract ;
2006-01-04 23:27:18 +08:00
phar_dostat ( data - > internal_file , ssb , 0 , data - > phar - > alias , data - > phar - > alias_len TSRMLS_CC ) ;
2005-12-05 04:35:38 +08:00
return 0 ;
}
2006-01-03 23:59:07 +08:00
/* }}} */
2005-12-05 04:35:38 +08:00
2006-01-01 15:40:36 +08:00
static void phar_dostat ( phar_manifest_entry * data , php_stream_statbuf * ssb , zend_bool is_dir , char * alias ,
2006-01-03 23:59:07 +08:00
int alias_len TSRMLS_DC ) /* {{{ */
2005-12-05 04:35:38 +08:00
{
2006-01-01 15:40:36 +08:00
char * tmp ;
int tmp_len ;
2005-12-05 04:35:38 +08:00
memset ( ssb , 0 , sizeof ( php_stream_statbuf ) ) ;
2005-12-11 15:25:30 +08:00
/* read-only across the board */
2005-12-05 04:35:38 +08:00
ssb - > sb . st_mode = 0444 ;
if ( ! is_dir ) {
ssb - > sb . st_size = data - > uncompressed_filesize ;
2005-12-11 15:25:30 +08:00
ssb - > sb . st_mode | = S_IFREG ; /* regular file */
/* timestamp is just the timestamp when this was added to the phar */
2005-12-05 04:35:38 +08:00
# ifdef NETWARE
ssb - > sb . st_mtime . tv_sec = data - > timestamp ;
ssb - > sb . st_atime . tv_sec = data - > timestamp ;
ssb - > sb . st_ctime . tv_sec = data - > timestamp ;
# else
ssb - > sb . st_mtime = data - > timestamp ;
ssb - > sb . st_atime = data - > timestamp ;
ssb - > sb . st_ctime = data - > timestamp ;
# endif
} else {
ssb - > sb . st_size = 0 ;
2005-12-11 15:25:30 +08:00
ssb - > sb . st_mode | = S_IFDIR ; /* regular directory */
2005-12-05 04:35:38 +08:00
# ifdef NETWARE
ssb - > sb . st_mtime . tv_sec = 0 ;
ssb - > sb . st_atime . tv_sec = 0 ;
ssb - > sb . st_ctime . tv_sec = 0 ;
# else
ssb - > sb . st_mtime = 0 ;
ssb - > sb . st_atime = 0 ;
ssb - > sb . st_ctime = 0 ;
# endif
}
ssb - > sb . st_nlink = 1 ;
ssb - > sb . st_rdev = - 1 ;
2006-01-01 15:40:36 +08:00
if ( data ) {
tmp_len = data - > filename_len + alias_len ;
} else {
tmp_len = alias_len + 1 ;
}
tmp = ( char * ) emalloc ( tmp_len ) ;
memcpy ( tmp , alias , alias_len ) ;
if ( data ) {
memcpy ( tmp + alias_len , data - > filename , data - > filename_len ) ;
} else {
* ( tmp + alias_len ) = ' / ' ;
}
/* this is only for APC, so use /dev/null device - no chance of conflict there! */
ssb - > sb . st_dev = 0xc ;
/* generate unique inode number for alias/filename, so no phars will conflict */
ssb - > sb . st_ino = zend_get_hash_value ( tmp , tmp_len ) ;
efree ( tmp ) ;
2005-12-07 09:18:54 +08:00
# ifndef PHP_WIN32
2005-12-05 04:35:38 +08:00
ssb - > sb . st_blksize = - 1 ;
ssb - > sb . st_blocks = - 1 ;
2005-12-07 09:18:54 +08:00
# endif
2005-12-05 04:35:38 +08:00
}
2006-01-03 23:59:07 +08:00
/* }}}*/
2005-12-05 04:35:38 +08:00
2006-01-04 23:50:45 +08:00
static int phar_stream_stat ( php_stream_wrapper * wrapper , char * url , int flags ,
2006-01-03 23:59:07 +08:00
php_stream_statbuf * ssb , php_stream_context * context TSRMLS_DC ) /* {{{ */
2005-12-05 04:35:38 +08:00
{
php_url * resource = NULL ;
char * internal_file , * key ;
uint keylen ;
ulong unused ;
phar_file_data * data ;
phar_manifest_entry * file_data ;
resource = php_url_parse ( url ) ;
2006-01-04 10:26:15 +08:00
2006-01-04 17:26:32 +08:00
if ( ! resource & & ( resource = phar_open_url ( wrapper , url , " r " , 0 TSRMLS_CC ) ) = = NULL ) {
2006-01-04 10:26:15 +08:00
return - 1 ;
}
2006-01-02 04:40:43 +08:00
/* we must have at the very least phar://alias.phar/internalfile.php */
2005-12-05 04:35:38 +08:00
if ( ! resource | | ! resource - > scheme | | ! resource - > host | | ! resource - > path ) {
2006-01-04 10:26:15 +08:00
if ( resource ) {
php_url_free ( resource ) ;
}
2005-12-11 08:00:31 +08:00
php_stream_wrapper_log_error ( wrapper , flags TSRMLS_CC , " phar error: invalid url \" %s \" " , url ) ;
2005-12-05 04:35:38 +08:00
return - 1 ;
}
if ( strcasecmp ( " phar " , resource - > scheme ) ) {
2005-12-07 14:39:03 +08:00
php_url_free ( resource ) ;
2005-12-11 08:00:31 +08:00
php_stream_wrapper_log_error ( wrapper , flags TSRMLS_CC , " phar error: not a phar url \" %s \" " , url ) ;
2005-12-07 14:39:03 +08:00
return - 1 ;
2005-12-05 04:35:38 +08:00
}
internal_file = resource - > path + 1 ; /* strip leading "/" */
2006-01-02 04:40:43 +08:00
/* find the phar in our trusty global hash indexed by alias (host of phar://blah.phar/file.whatever) */
2005-12-05 16:31:05 +08:00
if ( SUCCESS = = zend_hash_find ( & ( PHAR_GLOBALS - > phar_data ) , resource - > host , strlen ( resource - > host ) , ( void * * ) & data ) ) {
2005-12-05 04:35:38 +08:00
if ( * internal_file = = ' \0 ' ) {
2005-12-08 15:38:44 +08:00
/* root directory requested */
2006-01-01 15:40:36 +08:00
phar_dostat ( NULL , ssb , 1 , data - > alias , data - > alias_len TSRMLS_CC ) ;
2005-12-05 04:35:38 +08:00
php_url_free ( resource ) ;
return 0 ;
}
2005-12-11 15:25:30 +08:00
/* search through the manifest of files, and if we have an exact match, it's a file */
2006-01-05 19:32:05 +08:00
if ( SUCCESS = = zend_hash_find ( & data - > manifest , internal_file , strlen ( internal_file ) , ( void * * ) & file_data ) ) {
2006-01-01 15:40:36 +08:00
phar_dostat ( file_data , ssb , 0 , data - > alias , data - > alias_len TSRMLS_CC ) ;
2005-12-05 04:35:38 +08:00
} else {
2005-12-11 15:25:30 +08:00
/* search for directory (partial match of a file) */
2006-01-05 19:32:05 +08:00
zend_hash_internal_pointer_reset ( & data - > manifest ) ;
while ( HASH_KEY_NON_EXISTANT ! = zend_hash_has_more_elements ( & data - > manifest ) ) {
2005-12-05 16:31:05 +08:00
if ( HASH_KEY_NON_EXISTANT ! =
zend_hash_get_current_key_ex (
2006-01-05 19:32:05 +08:00
& data - > manifest , & key , & keylen , & unused , 0 , NULL ) ) {
2005-12-11 15:25:30 +08:00
if ( 0 = = memcmp ( internal_file , key , strlen ( internal_file ) ) ) {
/* directory found, all dirs have the same stat */
2005-12-11 16:14:44 +08:00
if ( key [ strlen ( internal_file ) ] = = ' / ' ) {
2006-01-01 15:40:36 +08:00
phar_dostat ( NULL , ssb , 1 , data - > alias , data - > alias_len TSRMLS_CC ) ;
2005-12-11 16:14:44 +08:00
break ;
}
2005-12-05 04:35:38 +08:00
}
}
2006-01-05 19:32:05 +08:00
if ( SUCCESS ! = zend_hash_move_forward ( & data - > manifest ) ) {
2005-12-11 15:25:30 +08:00
break ;
}
2005-12-05 04:35:38 +08:00
}
}
}
php_url_free ( resource ) ;
return 0 ;
}
2006-01-03 23:59:07 +08:00
/* }}} */
2005-12-05 04:35:38 +08:00
2005-12-11 15:25:30 +08:00
/* add an empty element with a char * key to a hash table, avoiding duplicates */
2006-01-03 23:59:07 +08:00
static int phar_add_empty ( HashTable * ht , char * arKey , uint nKeyLength ) /* { { { */
2005-12-05 16:31:05 +08:00
{
void * dummy = ( void * ) 1 ;
return zend_hash_update ( ht , arKey , nKeyLength , & dummy , sizeof ( void * ) , NULL ) ;
}
2006-01-03 23:59:07 +08:00
/* }}} */
2005-12-05 16:31:05 +08:00
2006-01-03 23:59:07 +08:00
static int compare_dir_name ( const void * a , const void * b TSRMLS_DC ) /* { { { */
2006-01-02 04:16:09 +08:00
{
Bucket * f ;
Bucket * s ;
int result ;
f = * ( ( Bucket * * ) a ) ;
s = * ( ( Bucket * * ) b ) ;
2006-01-02 06:20:26 +08:00
result = zend_binary_strcmp ( f - > arKey , f - > nKeyLength ,
s - > arKey , s - > nKeyLength ) ;
2006-01-02 04:16:09 +08:00
if ( result = = 0 ) {
return 0 ;
}
if ( result < 0 ) {
return - 1 ;
} else if ( result > 0 ) {
return 1 ;
} else {
return 0 ;
}
return 0 ;
}
2006-01-03 23:59:07 +08:00
/* }}} */
2006-01-02 04:16:09 +08:00
2006-01-03 23:59:07 +08:00
static php_stream * phar_make_dirstream ( char * dir , HashTable * manifest TSRMLS_DC ) /* { { { */
2005-12-05 04:35:38 +08:00
{
2005-12-05 16:31:05 +08:00
HashTable * data ;
2005-12-05 04:35:38 +08:00
int dirlen = strlen ( dir ) ;
char * save , * found , * key ;
uint keylen ;
ulong unused ;
2005-12-05 16:31:05 +08:00
char * entry ;
ALLOC_HASHTABLE ( data ) ;
zend_hash_init ( data , 64 , zend_get_hash_value , NULL , 0 ) ;
2005-12-05 04:35:38 +08:00
zend_hash_internal_pointer_reset ( manifest ) ;
2005-12-11 16:14:44 +08:00
while ( HASH_KEY_NON_EXISTANT ! = zend_hash_has_more_elements ( manifest ) ) {
2005-12-05 16:31:05 +08:00
if ( HASH_KEY_NON_EXISTANT = = zend_hash_get_current_key_ex ( manifest , & key , & keylen , & unused , 0 , NULL ) ) {
2005-12-11 16:14:44 +08:00
break ;
2005-12-05 04:35:38 +08:00
}
if ( * dir = = ' / ' ) {
2005-12-11 16:14:44 +08:00
/* root directory */
2005-12-05 16:31:05 +08:00
if ( NULL ! = ( found = ( char * ) memchr ( key , ' / ' , keylen ) ) ) {
2005-12-08 15:38:44 +08:00
/* the entry has a path separator and is a subdirectory */
2005-12-11 16:14:44 +08:00
entry = ( char * ) emalloc ( found - key + 1 ) ;
memcpy ( entry , key , found - key ) ;
keylen = found - key ;
entry [ keylen ] = ' \0 ' ;
} else {
entry = ( char * ) emalloc ( keylen + 1 ) ;
memcpy ( entry , key , keylen ) ;
entry [ keylen ] = ' \0 ' ;
2005-12-05 04:35:38 +08:00
}
2005-12-11 16:14:44 +08:00
goto PHAR_ADD_ENTRY ;
2005-12-05 04:35:38 +08:00
} else {
if ( 0 ! = memcmp ( key , dir , dirlen ) ) {
2005-12-08 15:38:44 +08:00
/* entry in directory not found */
2005-12-11 16:14:44 +08:00
if ( SUCCESS ! = zend_hash_move_forward ( manifest ) ) {
break ;
}
2005-12-05 04:35:38 +08:00
continue ;
2005-12-11 16:14:44 +08:00
} else {
if ( key [ dirlen ] ! = ' / ' ) {
if ( SUCCESS ! = zend_hash_move_forward ( manifest ) ) {
break ;
}
continue ;
}
2005-12-05 04:35:38 +08:00
}
}
save = key ;
2005-12-08 15:38:44 +08:00
save + = dirlen + 1 ; /* seek to just past the path separator */
2005-12-05 04:35:38 +08:00
if ( NULL ! = ( found = ( char * ) memchr ( save , ' / ' , keylen - dirlen - 1 ) ) ) {
2005-12-08 15:38:44 +08:00
/* is subdirectory */
2005-12-05 16:31:05 +08:00
save - = dirlen + 1 ;
2005-12-11 16:14:44 +08:00
entry = ( char * ) emalloc ( found - save + dirlen + 1 ) ;
memcpy ( entry , save + dirlen + 1 , found - save - dirlen - 1 ) ;
keylen = found - save - dirlen - 1 ;
entry [ keylen ] = ' \0 ' ;
2005-12-05 04:35:38 +08:00
} else {
2005-12-08 15:38:44 +08:00
/* is file */
2005-12-05 16:31:05 +08:00
save - = dirlen + 1 ;
entry = ( char * ) emalloc ( keylen - dirlen + 1 ) ;
2005-12-11 16:14:44 +08:00
memcpy ( entry , save + dirlen + 1 , keylen - dirlen - 1 ) ;
entry [ keylen - dirlen - 1 ] = ' \0 ' ;
keylen = keylen - dirlen - 1 ;
2005-12-05 04:35:38 +08:00
}
2005-12-11 16:14:44 +08:00
PHAR_ADD_ENTRY :
2005-12-05 16:31:05 +08:00
phar_add_empty ( data , entry , keylen ) ;
efree ( entry ) ;
2005-12-11 16:14:44 +08:00
if ( SUCCESS ! = zend_hash_move_forward ( manifest ) ) {
break ;
}
}
if ( HASH_KEY_NON_EXISTANT ! = zend_hash_has_more_elements ( data ) ) {
efree ( dir ) ;
2006-01-02 04:16:09 +08:00
if ( zend_hash_sort ( data , zend_qsort , compare_dir_name , 0 TSRMLS_CC ) = = FAILURE ) {
FREE_HASHTABLE ( data ) ;
return NULL ;
}
2005-12-11 16:14:44 +08:00
return php_stream_alloc ( & phar_dir_ops , data , NULL , " r " ) ;
} else {
efree ( dir ) ;
FREE_HASHTABLE ( data ) ;
return NULL ;
2005-12-05 04:35:38 +08:00
}
}
2006-01-03 23:59:07 +08:00
/* }}}*/
2005-12-05 04:35:38 +08:00
2006-01-04 23:50:45 +08:00
static php_stream * phar_opendir ( php_stream_wrapper * wrapper , char * filename , char * mode ,
2006-01-03 23:59:07 +08:00
int options , char * * opened_path , php_stream_context * context STREAMS_DC TSRMLS_DC ) /* {{{ */
2005-12-05 04:35:38 +08:00
{
php_url * resource = NULL ;
php_stream * ret ;
char * internal_file , * key ;
uint keylen ;
ulong unused ;
phar_file_data * data ;
phar_manifest_entry * file_data ;
resource = php_url_parse ( filename ) ;
2006-01-04 10:26:15 +08:00
2006-01-04 17:26:32 +08:00
if ( ! resource & & ( resource = phar_open_url ( wrapper , filename , mode , options TSRMLS_CC ) ) = = NULL ) {
2006-01-04 10:26:15 +08:00
return NULL ;
}
2006-01-02 04:40:43 +08:00
/* we must have at the very least phar://alias.phar/ */
2005-12-05 04:35:38 +08:00
if ( ! resource | | ! resource - > scheme | | ! resource - > host | | ! resource - > path ) {
2006-01-04 00:22:23 +08:00
if ( resource & & resource - > host & & ! resource - > path ) {
2006-01-02 04:40:43 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: no directory in \" %s \" , must have at least phar://%s/ for root directory " , filename , resource - > host ) ;
2005-12-11 15:25:30 +08:00
php_url_free ( resource ) ;
return NULL ;
}
2006-01-04 00:22:23 +08:00
if ( resource ) {
php_url_free ( resource ) ;
}
2006-01-02 04:40:43 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: invalid url \" %s \" , must have at least phar://%s/ " , filename , filename ) ;
2005-12-07 14:39:03 +08:00
return NULL ;
2005-12-05 04:35:38 +08:00
}
if ( strcasecmp ( " phar " , resource - > scheme ) ) {
2005-12-07 14:39:03 +08:00
php_url_free ( resource ) ;
2005-12-11 08:00:31 +08:00
php_stream_wrapper_log_error ( wrapper , options TSRMLS_CC , " phar error: not a phar url \" %s \" " , filename ) ;
2005-12-07 14:39:03 +08:00
return NULL ;
2005-12-05 04:35:38 +08:00
}
internal_file = resource - > path + 1 ; /* strip leading "/" */
2005-12-05 16:31:05 +08:00
if ( SUCCESS = = zend_hash_find ( & ( PHAR_GLOBALS - > phar_data ) , resource - > host , strlen ( resource - > host ) , ( void * * ) & data ) ) {
2005-12-05 04:35:38 +08:00
if ( * internal_file = = ' \0 ' ) {
2005-12-08 15:38:44 +08:00
/* root directory requested */
2005-12-11 16:14:44 +08:00
internal_file = estrndup ( internal_file - 1 , 1 ) ;
2006-01-05 19:32:05 +08:00
ret = phar_make_dirstream ( internal_file , & data - > manifest TSRMLS_CC ) ;
2005-12-05 04:35:38 +08:00
php_url_free ( resource ) ;
return ret ;
}
2006-01-05 19:32:05 +08:00
if ( SUCCESS = = zend_hash_find ( & data - > manifest , internal_file , strlen ( internal_file ) , ( void * * ) & file_data ) ) {
2005-12-05 04:35:38 +08:00
php_url_free ( resource ) ;
return NULL ;
} else {
2005-12-08 15:38:44 +08:00
/* search for directory */
2006-01-05 19:32:05 +08:00
zend_hash_internal_pointer_reset ( & data - > manifest ) ;
while ( HASH_KEY_NON_EXISTANT ! = zend_hash_has_more_elements ( & data - > manifest ) ) {
2005-12-05 16:31:05 +08:00
if ( HASH_KEY_NON_EXISTANT ! =
zend_hash_get_current_key_ex (
2006-01-05 19:32:05 +08:00
& data - > manifest , & key , & keylen , & unused , 0 , NULL ) ) {
2005-12-11 16:14:44 +08:00
if ( 0 = = memcmp ( key , internal_file , strlen ( internal_file ) ) ) {
2005-12-08 15:38:44 +08:00
/* directory found */
2005-12-11 16:14:44 +08:00
internal_file = estrndup ( internal_file ,
strlen ( internal_file ) ) ;
2005-12-05 04:35:38 +08:00
php_url_free ( resource ) ;
2006-01-05 19:32:05 +08:00
return phar_make_dirstream ( internal_file , & data - > manifest TSRMLS_CC ) ;
2005-12-05 04:35:38 +08:00
}
}
2006-01-05 19:32:05 +08:00
if ( SUCCESS ! = zend_hash_move_forward ( & data - > manifest ) ) {
2005-12-11 16:14:44 +08:00
break ;
}
2005-12-05 04:35:38 +08:00
}
}
}
php_url_free ( resource ) ;
return NULL ;
}
2006-01-03 23:59:07 +08:00
/* }}} */
2006-01-03 23:50:46 +08:00
# ifdef COMPILE_DL_PHAR
ZEND_GET_MODULE ( phar )
# endif
/* {{{ phar_functions[]
*
* Every user visible function must have an entry in phar_functions [ ] .
*/
function_entry phar_functions [ ] = {
{ NULL , NULL , NULL } /* Must be the last line in phar_functions[] */
} ;
2006-01-03 23:59:07 +08:00
static
ZEND_BEGIN_ARG_INFO ( arginfo_phar_mapPhar , 0 )
ZEND_ARG_INFO ( 0 , alias )
ZEND_ARG_INFO ( 0 , compressed )
ZEND_END_ARG_INFO ( ) ;
2006-01-07 01:23:42 +08:00
static
ZEND_BEGIN_ARG_INFO ( arginfo_phar_loadPhar , 0 )
ZEND_ARG_INFO ( 0 , fname )
ZEND_ARG_INFO ( 0 , alias )
ZEND_END_ARG_INFO ( ) ;
2006-01-03 23:50:46 +08:00
zend_function_entry php_archive_methods [ ] = {
2006-01-07 01:23:42 +08:00
PHP_ME ( Phar , apiVersion , NULL , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
PHP_ME ( Phar , canCompress , NULL , 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 )
PHP_ME ( Phar , loadPhar , arginfo_phar_loadPhar , ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_FINAL )
2006-01-03 23:50:46 +08:00
{ NULL , NULL , NULL }
} ;
/* }}} */
/* {{{ php_phar_init_globals
*/
static void php_phar_init_globals_module ( zend_phar_globals * phar_globals )
{
memset ( phar_globals , 0 , sizeof ( zend_phar_globals ) ) ;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION ( phar )
{
zend_class_entry php_archive_entry ;
ZEND_INIT_MODULE_GLOBALS ( phar , php_phar_init_globals_module , NULL ) ;
INIT_CLASS_ENTRY ( php_archive_entry , " Phar " , php_archive_methods ) ;
php_archive_entry_ptr = zend_register_internal_class ( & php_archive_entry TSRMLS_CC ) ;
2006-01-04 10:26:15 +08:00
2006-01-03 23:50:46 +08:00
return php_register_url_stream_wrapper ( " phar " , & php_stream_phar_wrapper TSRMLS_CC ) ;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION ( phar )
{
return php_unregister_url_stream_wrapper ( " phar " TSRMLS_CC ) ;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION ( phar )
{
zend_hash_init ( & ( PHAR_GLOBALS - > phar_data ) , sizeof ( phar_file_data ) ,
zend_get_hash_value , destroy_phar_data , 0 ) ;
return SUCCESS ;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION ( phar )
{
zend_hash_destroy ( & ( PHAR_GLOBALS - > phar_data ) ) ;
return SUCCESS ;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION ( phar )
{
php_info_print_table_start ( ) ;
php_info_print_table_header ( 2 , " phar PHP Archive support " , " enabled " ) ;
php_info_print_table_row ( 2 , " phar API version " , " 0.7.1 " ) ;
php_info_print_table_row ( 2 , " CVS revision " , " $Revision$ " ) ;
php_info_print_table_row ( 2 , " compressed phar support " ,
# ifdef HAVE_PHAR_ZLIB
" enabled " ) ;
# else
" disabled " ) ;
# endif
php_info_print_table_end ( ) ;
}
/* }}} */
/* {{{ phar_module_entry
*/
zend_module_entry phar_module_entry = {
# if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER ,
# endif
2006-01-04 10:26:15 +08:00
" Phar " ,
2006-01-03 23:50:46 +08:00
phar_functions ,
PHP_MINIT ( phar ) ,
PHP_MSHUTDOWN ( phar ) ,
PHP_RINIT ( phar ) ,
PHP_RSHUTDOWN ( phar ) ,
PHP_MINFO ( phar ) ,
# if ZEND_MODULE_API_NO >= 20010901
" 0.1.0 " , /* Replace with version number for your extension */
# endif
STANDARD_MODULE_PROPERTIES
} ;
/* }}} */
2005-12-05 04:35:38 +08: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
*/