Back to index

php5  5.3.10
Defines | Functions | Variables
php_zip.c File Reference
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/file.h"
#include "ext/standard/php_string.h"
#include "ext/pcre/php_pcre.h"
#include "php_zip.h"
#include "lib/zip.h"
#include "lib/zipint.h"

Go to the source code of this file.

Defines

#define le_zip_dir_name   "Zip Directory"
#define le_zip_entry_name   "Zip Entry"
#define PHP_ZIP_STAT_INDEX(za, index, flags, sb)
#define PHP_ZIP_STAT_PATH(za, path, path_len, flags, sb)
#define PHP_ZIP_SET_FILE_COMMENT(za, index, comment, comment_len)
#define add_ascii_assoc_string   add_assoc_string
#define add_ascii_assoc_long   add_assoc_long

Functions

static PHP_NAMED_FUNCTION (zif_zip_open)
static PHP_NAMED_FUNCTION (zif_zip_read)
static PHP_NAMED_FUNCTION (zif_zip_close)
static PHP_NAMED_FUNCTION (zif_zip_entry_read)
static PHP_NAMED_FUNCTION (zif_zip_entry_filesize)
static PHP_NAMED_FUNCTION (zif_zip_entry_name)
static PHP_NAMED_FUNCTION (zif_zip_entry_compressedsize)
static PHP_NAMED_FUNCTION (zif_zip_entry_compressionmethod)
static PHP_NAMED_FUNCTION (zif_zip_entry_open)
static PHP_NAMED_FUNCTION (zif_zip_entry_close)
static char * php_zip_make_relative_path (char *path, int path_len)
static void php_zip_free_dir (zend_rsrc_list_entry *rsrc TSRMLS_DC)
static void php_zip_free_entry (zend_rsrc_list_entry *rsrc TSRMLS_DC)
static PHP_MINIT_FUNCTION (zip)
static PHP_MSHUTDOWN_FUNCTION (zip)
static PHP_MINFO_FUNCTION (zip)
static void php_zip_entry_get_info (INTERNAL_FUNCTION_PARAMETERS, int opt)

Variables

static int le_zip_dir
static int le_zip_entry
static const zend_function_entry zip_functions []
zend_module_entry zip_module_entry

Define Documentation

Definition at line 95 of file php_zip.c.

Definition at line 94 of file php_zip.c.

#define le_zip_dir_name   "Zip Directory"

Definition at line 57 of file php_zip.c.

#define le_zip_entry_name   "Zip Entry"

Definition at line 59 of file php_zip.c.

#define PHP_ZIP_SET_FILE_COMMENT (   za,
  index,
  comment,
  comment_len 
)
Value:
if (comment_len == 0) { \
              /* Passing NULL remove the existing comment */ \
              if (zip_set_file_comment(intern, index, NULL, 0) < 0) { \
                     RETURN_FALSE; \
              } \
       } else if (zip_set_file_comment(intern, index, comment, comment_len) < 0) { \
              RETURN_FALSE; \
       } \
       RETURN_TRUE;

Definition at line 81 of file php_zip.c.

#define PHP_ZIP_STAT_INDEX (   za,
  index,
  flags,
  sb 
)
Value:
if (zip_stat_index(za, index, flags, &sb) != 0) { \
              RETURN_FALSE; \
       }

Definition at line 63 of file php_zip.c.

#define PHP_ZIP_STAT_PATH (   za,
  path,
  path_len,
  flags,
  sb 
)
Value:
if (path_len < 1) { \
              php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string as entry name"); \
              RETURN_FALSE; \
       } \
       if (zip_stat(za, path, flags, &sb) != 0) { \
              RETURN_FALSE; \
       }

Definition at line 70 of file php_zip.c.


Function Documentation

static PHP_MINFO_FUNCTION ( zip  ) [static]

Definition at line 2870 of file php_zip.c.

{
       php_info_print_table_start();

       php_info_print_table_row(2, "Zip", "enabled");
       php_info_print_table_row(2, "Extension Version","$Id: php_zip.c 321634 2012-01-01 13:15:04Z felipe $");
       php_info_print_table_row(2, "Zip version", PHP_ZIP_VERSION_STRING);
       php_info_print_table_row(2, "Libzip version", "0.9.0");

       php_info_print_table_end();
}

Here is the call graph for this function:

static PHP_MINIT_FUNCTION ( zip  ) [static]

Definition at line 2769 of file php_zip.c.

{
#ifdef PHP_ZIP_USE_OO 
       zend_class_entry ce;

       memcpy(&zip_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
       zip_object_handlers.clone_obj             = NULL;
       zip_object_handlers.get_property_ptr_ptr = php_zip_get_property_ptr_ptr;

       zip_object_handlers.get_properties = php_zip_get_properties;
       zip_object_handlers.read_property  = php_zip_read_property;
       zip_object_handlers.has_property   = php_zip_has_property;

       INIT_CLASS_ENTRY(ce, "ZipArchive", zip_class_functions);
       ce.create_object = php_zip_object_new;
       zip_class_entry = zend_register_internal_class(&ce TSRMLS_CC);

       zend_hash_init(&zip_prop_handlers, 0, NULL, NULL, 1);
       php_zip_register_prop_handler(&zip_prop_handlers, "status",    php_zip_status, NULL, NULL, IS_LONG TSRMLS_CC);
       php_zip_register_prop_handler(&zip_prop_handlers, "statusSys", php_zip_status_sys, NULL, NULL, IS_LONG TSRMLS_CC);
       php_zip_register_prop_handler(&zip_prop_handlers, "numFiles",  php_zip_get_num_files, NULL, NULL, IS_LONG TSRMLS_CC);
       php_zip_register_prop_handler(&zip_prop_handlers, "filename", NULL, NULL, php_zipobj_get_filename, IS_STRING TSRMLS_CC);
       php_zip_register_prop_handler(&zip_prop_handlers, "comment", NULL, php_zipobj_get_zip_comment, NULL, IS_STRING TSRMLS_CC);

       REGISTER_ZIP_CLASS_CONST_LONG("CREATE", ZIP_CREATE);
       REGISTER_ZIP_CLASS_CONST_LONG("EXCL", ZIP_EXCL);
       REGISTER_ZIP_CLASS_CONST_LONG("CHECKCONS", ZIP_CHECKCONS);
       REGISTER_ZIP_CLASS_CONST_LONG("OVERWRITE", ZIP_OVERWRITE);

       REGISTER_ZIP_CLASS_CONST_LONG("FL_NOCASE", ZIP_FL_NOCASE);
       REGISTER_ZIP_CLASS_CONST_LONG("FL_NODIR", ZIP_FL_NODIR);
       REGISTER_ZIP_CLASS_CONST_LONG("FL_COMPRESSED", ZIP_FL_COMPRESSED);
       REGISTER_ZIP_CLASS_CONST_LONG("FL_UNCHANGED", ZIP_FL_UNCHANGED);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_DEFAULT", ZIP_CM_DEFAULT);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_STORE", ZIP_CM_STORE);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_SHRINK", ZIP_CM_SHRINK);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_REDUCE_1", ZIP_CM_REDUCE_1);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_REDUCE_2", ZIP_CM_REDUCE_2);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_REDUCE_3", ZIP_CM_REDUCE_3);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_REDUCE_4", ZIP_CM_REDUCE_4);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_IMPLODE", ZIP_CM_IMPLODE);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_DEFLATE", ZIP_CM_DEFLATE);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_DEFLATE64", ZIP_CM_DEFLATE64);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_PKWARE_IMPLODE", ZIP_CM_PKWARE_IMPLODE);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_BZIP2", ZIP_CM_BZIP2);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_LZMA", ZIP_CM_LZMA);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_TERSE", ZIP_CM_TERSE);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_LZ77", ZIP_CM_LZ77);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_WAVPACK", ZIP_CM_WAVPACK);
       REGISTER_ZIP_CLASS_CONST_LONG("CM_PPMD", ZIP_CM_PPMD);

       /* Error code */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_OK",                  ZIP_ER_OK);                 /* N No error */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_MULTIDISK",    ZIP_ER_MULTIDISK);   /* N Multi-disk zip archives not supported */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_RENAME",              ZIP_ER_RENAME);             /* S Renaming temporary file failed */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_CLOSE",        ZIP_ER_CLOSE);              /* S Closing zip archive failed */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_SEEK",         ZIP_ER_SEEK);        /* S Seek error */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_READ",         ZIP_ER_READ);        /* S Read error */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_WRITE",        ZIP_ER_WRITE);              /* S Write error */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_CRC",                 ZIP_ER_CRC);         /* N CRC error */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_ZIPCLOSED",    ZIP_ER_ZIPCLOSED);   /* N Containing zip archive was closed */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_NOENT",        ZIP_ER_NOENT);              /* N No such file */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_EXISTS",              ZIP_ER_EXISTS);             /* N File already exists */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_OPEN",         ZIP_ER_OPEN);        /* S Can't open file */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_TMPOPEN",             ZIP_ER_TMPOPEN);     /* S Failure to create temporary file */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_ZLIB",         ZIP_ER_ZLIB);        /* Z Zlib error */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_MEMORY",              ZIP_ER_MEMORY);             /* N Malloc failure */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_CHANGED",             ZIP_ER_CHANGED);     /* N Entry has been changed */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_COMPNOTSUPP",  ZIP_ER_COMPNOTSUPP);/* N Compression method not supported */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_EOF",                 ZIP_ER_EOF);         /* N Premature EOF */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_INVAL",        ZIP_ER_INVAL);              /* N Invalid argument */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_NOZIP",        ZIP_ER_NOZIP);              /* N Not a zip archive */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_INTERNAL",     ZIP_ER_INTERNAL);    /* N Internal error */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_INCONS",              ZIP_ER_INCONS);             /* N Zip archive inconsistent */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_REMOVE",              ZIP_ER_REMOVE);             /* S Can't remove file */
       REGISTER_ZIP_CLASS_CONST_LONG("ER_DELETED",      ZIP_ER_DELETED);     /* N Entry has been deleted */

       php_register_url_stream_wrapper("zip", &php_stream_zip_wrapper TSRMLS_CC);
#endif

       le_zip_dir   = zend_register_list_destructors_ex(php_zip_free_dir,   NULL, le_zip_dir_name,   module_number);
       le_zip_entry = zend_register_list_destructors_ex(php_zip_free_entry, NULL, le_zip_entry_name, module_number);

       return SUCCESS;
}

Here is the call graph for this function:

static PHP_MSHUTDOWN_FUNCTION ( zip  ) [static]

Definition at line 2858 of file php_zip.c.

{
#ifdef PHP_ZIP_USE_OO 
       zend_hash_destroy(&zip_prop_handlers);
       php_unregister_url_stream_wrapper("zip" TSRMLS_CC);
#endif
       return SUCCESS;
}

Here is the call graph for this function:

static PHP_NAMED_FUNCTION ( zif_zip_open  ) [static]

Definition at line 1193 of file php_zip.c.

{
       char     *filename;
       int       filename_len;
       char resolved_path[MAXPATHLEN + 1];
       zip_rsrc *rsrc_int;
       int err = 0;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
              return;
       }

       if (filename_len == 0) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty string as source");
              RETURN_FALSE;
       }

       if (strlen(filename) != filename_len) {
              RETURN_FALSE;
       }

       if (ZIP_OPENBASEDIR_CHECKPATH(filename)) {
              RETURN_FALSE;
       }

       if(!expand_filepath(filename, resolved_path TSRMLS_CC)) {
              RETURN_FALSE;
       }

       rsrc_int = (zip_rsrc *)emalloc(sizeof(zip_rsrc));

       rsrc_int->za = zip_open(resolved_path, 0, &err);
       if (rsrc_int->za == NULL) {
              efree(rsrc_int);
              RETURN_LONG((long)err);
       }

       rsrc_int->index_current = 0;
       rsrc_int->num_files = zip_get_num_files(rsrc_int->za);

       ZEND_REGISTER_RESOURCE(return_value, rsrc_int, le_zip_dir);
}

Here is the call graph for this function:

static PHP_NAMED_FUNCTION ( zif_zip_read  ) [static]

Definition at line 1256 of file php_zip.c.

{
       zval *zip_dp;
       zip_read_rsrc *zr_rsrc;
       int ret;
       zip_rsrc *rsrc_int;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zip_dp) == FAILURE) {
              return;
       }
       ZEND_FETCH_RESOURCE(rsrc_int, zip_rsrc *, &zip_dp, -1, le_zip_dir_name, le_zip_dir);

       if (rsrc_int && rsrc_int->za) {
              if (rsrc_int->index_current >= rsrc_int->num_files) {
                     RETURN_FALSE;
              }

              zr_rsrc = emalloc(sizeof(zip_read_rsrc));

              ret = zip_stat_index(rsrc_int->za, rsrc_int->index_current, 0, &zr_rsrc->sb);

              if (ret != 0) {
                     efree(zr_rsrc);
                     RETURN_FALSE;
              }

              zr_rsrc->zf = zip_fopen_index(rsrc_int->za, rsrc_int->index_current, 0);
              if (zr_rsrc->zf) {
                     rsrc_int->index_current++;
                     ZEND_REGISTER_RESOURCE(return_value, zr_rsrc, le_zip_entry);
              } else {
                     efree(zr_rsrc);
                     RETURN_FALSE;
              }

       } else {
              RETURN_FALSE;
       }
}

Here is the call graph for this function:

static PHP_NAMED_FUNCTION ( zif_zip_close  ) [static]

Definition at line 1239 of file php_zip.c.

{
       zval * zip;
       zip_rsrc *z_rsrc = NULL;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zip) == FAILURE) {
              return;
       }
       ZEND_FETCH_RESOURCE(z_rsrc, zip_rsrc *, &zip, -1, le_zip_dir_name, le_zip_dir);

       /* really close the zip will break BC :-D */
       zend_list_delete(Z_LVAL_P(zip));
}

Here is the call graph for this function:

static PHP_NAMED_FUNCTION ( zif_zip_entry_read  ) [static]

Definition at line 1344 of file php_zip.c.

{
       zval * zip_entry;
       long len = 0;
       zip_read_rsrc * zr_rsrc;
       char *buffer;
       int n = 0;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zip_entry, &len) == FAILURE) {
              return;
       }

       ZEND_FETCH_RESOURCE(zr_rsrc, zip_read_rsrc *, &zip_entry, -1, le_zip_entry_name, le_zip_entry);

       if (len <= 0) {
              len = 1024;
       }

       if (zr_rsrc->zf) {
              buffer = safe_emalloc(len, 1, 1);
              n = zip_fread(zr_rsrc->zf, buffer, len);
              if (n > 0) {
                     buffer[n] = 0;
                     RETURN_STRINGL(buffer, n, 0);
              } else {
                     efree(buffer);
                     RETURN_EMPTY_STRING()
              }
       } else {
              RETURN_FALSE;
       }
}

Here is the call graph for this function:

static PHP_NAMED_FUNCTION ( zif_zip_entry_filesize  ) [static]

Definition at line 1460 of file php_zip.c.

Here is the call graph for this function:

static PHP_NAMED_FUNCTION ( zif_zip_entry_name  ) [static]

Definition at line 1444 of file php_zip.c.

Here is the call graph for this function:

static PHP_NAMED_FUNCTION ( zif_zip_entry_compressedsize  ) [static]

Definition at line 1452 of file php_zip.c.

Here is the call graph for this function:

static PHP_NAMED_FUNCTION ( zif_zip_entry_compressionmethod  ) [static]

Definition at line 1468 of file php_zip.c.

Here is the call graph for this function:

static PHP_NAMED_FUNCTION ( zif_zip_entry_open  ) [static]

Definition at line 1300 of file php_zip.c.

{
       zval * zip;
       zval * zip_entry;
       char *mode = NULL;
       int mode_len = 0;
       zip_read_rsrc * zr_rsrc;
       zip_rsrc *z_rsrc;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|s", &zip, &zip_entry, &mode, &mode_len) == FAILURE) {
              return;
       }

       ZEND_FETCH_RESOURCE(zr_rsrc, zip_read_rsrc *, &zip_entry, -1, le_zip_entry_name, le_zip_entry);
       ZEND_FETCH_RESOURCE(z_rsrc, zip_rsrc *, &zip, -1, le_zip_dir_name, le_zip_dir);

       if (zr_rsrc->zf != NULL) {
              RETURN_TRUE;
       } else {
              RETURN_FALSE;
       }
}

Here is the call graph for this function:

static PHP_NAMED_FUNCTION ( zif_zip_entry_close  ) [static]

Definition at line 1327 of file php_zip.c.

{
       zval * zip_entry;
       zip_read_rsrc * zr_rsrc;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zip_entry) == FAILURE) {
              return;
       }

       ZEND_FETCH_RESOURCE(zr_rsrc, zip_read_rsrc *, &zip_entry, -1, le_zip_entry_name, le_zip_entry);
       /*  we got a zip_entry resource, be happy */
       RETURN_TRUE;
}

Here is the call graph for this function:

static void php_zip_entry_get_info ( INTERNAL_FUNCTION_PARAMETERS  ,
int  opt 
) [static]

Definition at line 1378 of file php_zip.c.

{
       zval * zip_entry;
       zip_read_rsrc * zr_rsrc;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zip_entry) == FAILURE) {
              return;
       }

       ZEND_FETCH_RESOURCE(zr_rsrc, zip_read_rsrc *, &zip_entry, -1, le_zip_entry_name, le_zip_entry);

       if (!zr_rsrc->zf) {
              RETURN_FALSE;
       }

       switch (opt) {
              case 0:
                     RETURN_STRING((char *)zr_rsrc->sb.name, 1);
                     break;
              case 1:
                     RETURN_LONG((long) (zr_rsrc->sb.comp_size));
                     break;
              case 2:
                     RETURN_LONG((long) (zr_rsrc->sb.size));
                     break;
              case 3:
                     switch (zr_rsrc->sb.comp_method) {
                            case 0:
                                   RETURN_STRING("stored", 1);
                                   break;
                            case 1:
                                   RETURN_STRING("shrunk", 1);
                                   break;
                            case 2:
                            case 3:
                            case 4:
                            case 5:
                                   RETURN_STRING("reduced", 1);
                                   break;
                            case 6:
                                   RETURN_STRING("imploded", 1);
                                   break;
                            case 7:
                                   RETURN_STRING("tokenized", 1);
                                   break;
                            case 8:
                                   RETURN_STRING("deflated", 1);
                                   break;
                            case 9:
                                   RETURN_STRING("deflatedX", 1);
                                   break;
                            case 10:
                                   RETURN_STRING("implodedX", 1);
                                   break;
                            default:
                                   RETURN_FALSE;
                     }
                     RETURN_LONG((long) (zr_rsrc->sb.comp_method));
                     break;
       }

}

Here is the call graph for this function:

Here is the caller graph for this function:

static void php_zip_free_dir ( zend_rsrc_list_entry *rsrc  TSRMLS_DC) [static]

Definition at line 1125 of file php_zip.c.

{
       zip_rsrc * zip_int = (zip_rsrc *) rsrc->ptr;

       if (zip_int) {
              if (zip_int->za) {
                     if (zip_close(zip_int->za) != 0) {
                            _zip_free(zip_int->za);
                     }
                     zip_int->za = NULL;
              }

              efree(rsrc->ptr);

              rsrc->ptr = NULL;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void php_zip_free_entry ( zend_rsrc_list_entry *rsrc  TSRMLS_DC) [static]

Definition at line 1145 of file php_zip.c.

{
       zip_read_rsrc *zr_rsrc = (zip_read_rsrc *) rsrc->ptr;

       if (zr_rsrc) {
              if (zr_rsrc->zf) {
                     zip_fclose(zr_rsrc->zf);
                     zr_rsrc->zf = NULL;
              }
              efree(zr_rsrc);
              rsrc->ptr = NULL;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static char* php_zip_make_relative_path ( char *  path,
int  path_len 
) [static]

Definition at line 99 of file php_zip.c.

{
       char *path_begin = path;
       size_t i;

       if (IS_SLASH(path[0])) {
              return path + 1;
       }

       if (path_len < 1 || path == NULL) {
              return NULL;
       }

       i = path_len;

       while (1) {
              while (i > 0 && !IS_SLASH(path[i])) {
                     i--;
              }

              if (!i) {
                     return path;
              }

              if (i >= 2 && (path[i -1] == '.' || path[i -1] == ':')) {
                     /* i is the position of . or :, add 1 for / */
                     path_begin = path + i + 1;
                     break;
              }
              i--;
       }

       return path_begin;
}

Variable Documentation

int le_zip_dir [static]

Definition at line 56 of file php_zip.c.

int le_zip_entry [static]

Definition at line 58 of file php_zip.c.

Initial value:

Definition at line 774 of file php_zip.c.

zend_module_entry zip_module_entry