--- embedaddon/php/ext/fileinfo/fileinfo.c 2012/02/21 23:47:56 1.1.1.1 +++ embedaddon/php/ext/fileinfo/fileinfo.c 2012/05/29 12:34:39 1.1.1.2 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: fileinfo.c,v 1.1.1.1 2012/02/21 23:47:56 misho Exp $ */ +/* $Id: fileinfo.c,v 1.1.1.2 2012/05/29 12:34:39 misho Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -76,9 +76,9 @@ struct finfo_object { } \ } -/* {{{ finfo_objects_dtor +/* {{{ finfo_objects_free */ -static void finfo_objects_dtor(void *object, zend_object_handle handle TSRMLS_DC) +static void finfo_objects_free(void *object TSRMLS_DC) { struct finfo_object *intern = (struct finfo_object *) object; @@ -98,17 +98,17 @@ PHP_FILEINFO_API zend_object_value finfo_objects_new(z { zend_object_value retval; struct finfo_object *intern; - zval *tmp; intern = emalloc(sizeof(struct finfo_object)); memset(intern, 0, sizeof(struct finfo_object)); zend_object_std_init(&intern->zo, class_type TSRMLS_CC); - zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref,(void *) &tmp, sizeof(zval *)); + object_properties_init(&intern->zo, class_type); intern->ptr = NULL; - retval.handle = zend_objects_store_put(intern, finfo_objects_dtor, NULL, NULL TSRMLS_CC); + retval.handle = zend_objects_store_put(intern, NULL, + finfo_objects_free, NULL TSRMLS_CC); retval.handlers = (zend_object_handlers *) &finfo_object_handlers; return retval; @@ -276,6 +276,15 @@ PHP_MINFO_FUNCTION(fileinfo) } /* }}} */ +#define FILEINFO_DESTROY_OBJECT(object) \ + do { \ + if (object) { \ + zend_object_store_ctor_failed(object TSRMLS_CC); \ + zval_dtor(object); \ + ZVAL_NULL(object); \ + } \ + } while (0) + /* {{{ proto resource finfo_open([int options [, string arg]]) Create a new fileinfo resource. */ PHP_FUNCTION(finfo_open) @@ -287,13 +296,14 @@ PHP_FUNCTION(finfo_open) FILEINFO_DECLARE_INIT_OBJECT(object) char resolved_path[MAXPATHLEN]; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ls", &options, &file, &file_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lp", &options, &file, &file_len) == FAILURE) { + FILEINFO_DESTROY_OBJECT(object); RETURN_FALSE; } - + if (object) { struct finfo_object *finfo_obj = (struct finfo_object*)zend_object_store_get_object(object TSRMLS_CC); - + if (finfo_obj->ptr) { magic_close(finfo_obj->ptr->magic); efree(finfo_obj->ptr); @@ -304,21 +314,20 @@ PHP_FUNCTION(finfo_open) if (file_len == 0) { file = NULL; } else if (file && *file) { /* user specified file, perform open_basedir checks */ - if (strlen(file) != file_len) { - RETURN_FALSE; - } - if (!VCWD_REALPATH(file, resolved_path)) { - RETURN_FALSE; - } - file = resolved_path; #if PHP_API_VERSION < 20100412 if ((PG(safe_mode) && (!php_checkuid(file, NULL, CHECKUID_CHECK_FILE_AND_DIR))) || php_check_open_basedir(file TSRMLS_CC)) { #else if (php_check_open_basedir(file TSRMLS_CC)) { #endif + FILEINFO_DESTROY_OBJECT(object); RETURN_FALSE; } + if (!expand_filepath_with_mode(file, resolved_path, NULL, 0, CWD_EXPAND TSRMLS_CC)) { + FILEINFO_DESTROY_OBJECT(object); + RETURN_FALSE; + } + file = resolved_path; } finfo = emalloc(sizeof(struct php_fileinfo)); @@ -329,21 +338,23 @@ PHP_FUNCTION(finfo_open) if (finfo->magic == NULL) { efree(finfo); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid mode '%ld'.", options); - RETURN_FALSE; + FILEINFO_DESTROY_OBJECT(object); + RETURN_FALSE; } if (magic_load(finfo->magic, file) == -1) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to load magic database at '%s'.", file); magic_close(finfo->magic); efree(finfo); + FILEINFO_DESTROY_OBJECT(object); RETURN_FALSE; - } + } if (object) { FILEINFO_REGISTER_OBJECT(object, finfo); } else { ZEND_REGISTER_RESOURCE(return_value, finfo, le_fileinfo); - } + } } /* }}} */ @@ -499,11 +510,22 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARA wrap = php_stream_locate_url_wrapper(buffer, &tmp2, 0 TSRMLS_CC); if (wrap) { + php_stream *stream; php_stream_context *context = php_stream_context_from_zval(zcontext, 0); + +#ifdef PHP_WIN32 + if (php_stream_stat_path_ex(buffer, 0, &ssb, context) == SUCCESS) { + if (ssb.sb.st_mode & S_IFDIR) { + ret_val = mime_directory; + goto common; + } + } +#endif + #if PHP_API_VERSION < 20100412 - php_stream *stream = php_stream_open_wrapper_ex(buffer, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context); + stream = php_stream_open_wrapper_ex(buffer, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context); #else - php_stream *stream = php_stream_open_wrapper_ex(buffer, "rb", REPORT_ERRORS, NULL, context); + stream = php_stream_open_wrapper_ex(buffer, "rb", REPORT_ERRORS, NULL, context); #endif if (!stream) {