PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/fileinfo - fileinfo.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 183 211 86.7 %
Date: 2015-05-21 Functions: 13 13 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2015 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.0 of the PHP license,       |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_0.txt.                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Author: Ilia Alshanetsky <ilia@php.net>                              |
      16             :   +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #ifdef HAVE_CONFIG_H
      22             : #include "config.h"
      23             : #endif
      24             : #include "php.h"
      25             : 
      26             : #include <magic.h>
      27             : /*
      28             :  * HOWMANY specifies the maximum offset libmagic will look at
      29             :  * this is currently hardcoded in the libmagic source but not exported
      30             :  */
      31             : #ifndef HOWMANY
      32             : #define HOWMANY 65536
      33             : #endif
      34             : 
      35             : #include "php_ini.h"
      36             : #include "ext/standard/info.h"
      37             : #include "ext/standard/file.h" /* needed for context stuff */
      38             : #include "php_fileinfo.h"
      39             : #include "fopen_wrappers.h" /* needed for is_url */
      40             : #include "Zend/zend_exceptions.h"
      41             : 
      42             : #ifndef _S_IFDIR
      43             : # define _S_IFDIR               S_IFDIR
      44             : #endif
      45             : 
      46             : /* {{{ macros and type definitions */
      47             : typedef struct _php_fileinfo {
      48             :         zend_long options;
      49             :         struct magic_set *magic;
      50             : } php_fileinfo;
      51             : 
      52             : static zend_object_handlers finfo_object_handlers;
      53             : zend_class_entry *finfo_class_entry;
      54             : 
      55             : typedef struct _finfo_object {
      56             :         php_fileinfo *ptr;
      57             :         zend_object zo;
      58             : } finfo_object;
      59             : 
      60             : #define FILEINFO_DECLARE_INIT_OBJECT(object) \
      61             :         zval *object = ZEND_IS_METHOD_CALL() ? getThis() : NULL;
      62             : 
      63          58 : static inline finfo_object *php_finfo_fetch_object(zend_object *obj) {
      64          58 :         return (finfo_object *)((char*)(obj) - XtOffsetOf(finfo_object, zo));
      65             : }
      66             : 
      67             : #define Z_FINFO_P(zv) php_finfo_fetch_object(Z_OBJ_P((zv)))
      68             : 
      69             : #define FILEINFO_REGISTER_OBJECT(_object, _ptr) \
      70             : { \
      71             :         finfo_object *obj; \
      72             :     obj = Z_FINFO_P(_object); \
      73             :     obj->ptr = _ptr; \
      74             : }
      75             : 
      76             : #define FILEINFO_FROM_OBJECT(finfo, object) \
      77             : { \
      78             :         finfo_object *obj = Z_FINFO_P(object); \
      79             :         finfo = obj->ptr; \
      80             :         if (!finfo) { \
      81             :                 php_error_docref(NULL, E_WARNING, "The invalid fileinfo object."); \
      82             :                 RETURN_FALSE; \
      83             :         } \
      84             : }
      85             : 
      86             : /* {{{ finfo_objects_free
      87             :  */
      88          14 : static void finfo_objects_free(zend_object *object)
      89             : {
      90          14 :         finfo_object *intern = php_finfo_fetch_object(object);
      91             : 
      92          14 :         if (intern->ptr) {
      93          12 :                 magic_close(intern->ptr->magic);
      94          12 :                 efree(intern->ptr);
      95             :         }
      96             : 
      97          14 :         zend_object_std_dtor(&intern->zo);
      98          14 : }
      99             : /* }}} */
     100             : 
     101             : /* {{{ finfo_objects_new
     102             :  */
     103          14 : PHP_FILEINFO_API zend_object *finfo_objects_new(zend_class_entry *class_type)
     104             : {
     105             :         finfo_object *intern;
     106             : 
     107          14 :         intern = ecalloc(1, sizeof(finfo_object) + zend_object_properties_size(class_type));
     108             : 
     109          14 :         zend_object_std_init(&intern->zo, class_type);
     110          14 :         object_properties_init(&intern->zo, class_type);
     111          14 :         intern->zo.handlers = &finfo_object_handlers;
     112             : 
     113          14 :         return &intern->zo;
     114             : }
     115             : /* }}} */
     116             : 
     117             : /* {{{ arginfo */
     118             : ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_open, 0, 0, 0)
     119             :         ZEND_ARG_INFO(0, options)
     120             :         ZEND_ARG_INFO(0, arg)
     121             : ZEND_END_ARG_INFO()
     122             : 
     123             : ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_close, 0, 0, 1)
     124             :         ZEND_ARG_INFO(0, finfo)
     125             : ZEND_END_ARG_INFO()
     126             : 
     127             : ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_set_flags, 0, 0, 2)
     128             :         ZEND_ARG_INFO(0, finfo)
     129             :         ZEND_ARG_INFO(0, options)
     130             : ZEND_END_ARG_INFO()
     131             : 
     132             : ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_method_set_flags, 0, 0, 1)
     133             :         ZEND_ARG_INFO(0, options)
     134             : ZEND_END_ARG_INFO()
     135             : 
     136             : ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_file, 0, 0, 2)
     137             :         ZEND_ARG_INFO(0, finfo)
     138             :         ZEND_ARG_INFO(0, filename)
     139             :         ZEND_ARG_INFO(0, options)
     140             :         ZEND_ARG_INFO(0, context)
     141             : ZEND_END_ARG_INFO()
     142             : 
     143             : ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_method_file, 0, 0, 1)
     144             :         ZEND_ARG_INFO(0, filename)
     145             :         ZEND_ARG_INFO(0, options)
     146             :         ZEND_ARG_INFO(0, context)
     147             : ZEND_END_ARG_INFO()
     148             : 
     149             : ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_buffer, 0, 0, 2)
     150             :         ZEND_ARG_INFO(0, finfo)
     151             :         ZEND_ARG_INFO(0, string)
     152             :         ZEND_ARG_INFO(0, options)
     153             :         ZEND_ARG_INFO(0, context)
     154             : ZEND_END_ARG_INFO()
     155             : 
     156             : ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_method_buffer, 0, 0, 1)
     157             :         ZEND_ARG_INFO(0, string)
     158             :         ZEND_ARG_INFO(0, options)
     159             :         ZEND_ARG_INFO(0, context)
     160             : ZEND_END_ARG_INFO()
     161             : 
     162             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mime_content_type, 0, 0, 1)
     163             :         ZEND_ARG_INFO(0, string)
     164             : ZEND_END_ARG_INFO()
     165             : /* }}} */
     166             : 
     167             : /* {{{ finfo_class_functions
     168             :  */
     169             : zend_function_entry finfo_class_functions[] = {
     170             :         ZEND_ME_MAPPING(finfo,          finfo_open,     arginfo_finfo_open, ZEND_ACC_PUBLIC)
     171             :         ZEND_ME_MAPPING(set_flags,      finfo_set_flags,arginfo_finfo_method_set_flags, ZEND_ACC_PUBLIC)
     172             :         ZEND_ME_MAPPING(file,           finfo_file,     arginfo_finfo_method_file, ZEND_ACC_PUBLIC)
     173             :         ZEND_ME_MAPPING(buffer,         finfo_buffer,   arginfo_finfo_method_buffer, ZEND_ACC_PUBLIC)
     174             :         PHP_FE_END
     175             : };
     176             : /* }}} */
     177             : 
     178             : #define FINFO_SET_OPTION(magic, options) \
     179             :         if (magic_setflags(magic, options) == -1) { \
     180             :                 php_error_docref(NULL, E_WARNING, "Failed to set option '%pd' %d:%s", \
     181             :                                 options, magic_errno(magic), magic_error(magic)); \
     182             :                 RETURN_FALSE; \
     183             :         }
     184             : 
     185             : /* True global resources - no need for thread safety here */
     186             : static int le_fileinfo;
     187             : /* }}} */
     188             : 
     189          31 : void finfo_resource_destructor(zend_resource *rsrc) /* {{{ */
     190             : {
     191          31 :         if (rsrc->ptr) {
     192          31 :                 php_fileinfo *finfo = (php_fileinfo *) rsrc->ptr;
     193          31 :                 magic_close(finfo->magic);
     194          31 :                 efree(rsrc->ptr);
     195          31 :                 rsrc->ptr = NULL;
     196             :         }
     197          31 : }
     198             : /* }}} */
     199             : 
     200             : 
     201             : /* {{{ fileinfo_functions[]
     202             :  */
     203             : zend_function_entry fileinfo_functions[] = {
     204             :         PHP_FE(finfo_open,              arginfo_finfo_open)
     205             :         PHP_FE(finfo_close,             arginfo_finfo_close)
     206             :         PHP_FE(finfo_set_flags, arginfo_finfo_set_flags)
     207             :         PHP_FE(finfo_file,              arginfo_finfo_file)
     208             :         PHP_FE(finfo_buffer,    arginfo_finfo_buffer)
     209             :         PHP_FE(mime_content_type, arginfo_mime_content_type)
     210             :         {NULL, NULL, NULL}
     211             : };
     212             : /* }}} */
     213             : 
     214             : /* {{{ PHP_MINIT_FUNCTION
     215             :  */
     216       20916 : PHP_MINIT_FUNCTION(finfo)
     217             : {
     218             :         zend_class_entry _finfo_class_entry;
     219       20916 :         INIT_CLASS_ENTRY(_finfo_class_entry, "finfo", finfo_class_functions);
     220       20916 :         _finfo_class_entry.create_object = finfo_objects_new;
     221       20916 :         finfo_class_entry = zend_register_internal_class(&_finfo_class_entry);
     222             : 
     223             :         /* copy the standard object handlers to you handler table */
     224       20916 :         memcpy(&finfo_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
     225       20916 :         finfo_object_handlers.offset = XtOffsetOf(finfo_object, zo);
     226       20916 :         finfo_object_handlers.free_obj = finfo_objects_free;
     227             : 
     228       20916 :         le_fileinfo = zend_register_list_destructors_ex(finfo_resource_destructor, NULL, "file_info", module_number);
     229             : 
     230       20916 :         REGISTER_LONG_CONSTANT("FILEINFO_NONE",                       MAGIC_NONE, CONST_CS|CONST_PERSISTENT);
     231       20916 :         REGISTER_LONG_CONSTANT("FILEINFO_SYMLINK",            MAGIC_SYMLINK, CONST_CS|CONST_PERSISTENT);
     232       20916 :         REGISTER_LONG_CONSTANT("FILEINFO_MIME",                       MAGIC_MIME, CONST_CS|CONST_PERSISTENT);
     233       20916 :         REGISTER_LONG_CONSTANT("FILEINFO_MIME_TYPE",  MAGIC_MIME_TYPE, CONST_CS|CONST_PERSISTENT);
     234       20916 :         REGISTER_LONG_CONSTANT("FILEINFO_MIME_ENCODING",MAGIC_MIME_ENCODING, CONST_CS|CONST_PERSISTENT);
     235             : /*      REGISTER_LONG_CONSTANT("FILEINFO_COMPRESS",           MAGIC_COMPRESS, CONST_CS|CONST_PERSISTENT); disabled, as it does fork now */
     236       20916 :         REGISTER_LONG_CONSTANT("FILEINFO_DEVICES",            MAGIC_DEVICES, CONST_CS|CONST_PERSISTENT);
     237       20916 :         REGISTER_LONG_CONSTANT("FILEINFO_CONTINUE",           MAGIC_CONTINUE, CONST_CS|CONST_PERSISTENT);
     238             : #ifdef MAGIC_PRESERVE_ATIME
     239       20916 :         REGISTER_LONG_CONSTANT("FILEINFO_PRESERVE_ATIME",     MAGIC_PRESERVE_ATIME, CONST_CS|CONST_PERSISTENT);
     240             : #endif
     241             : #ifdef MAGIC_RAW
     242       20916 :         REGISTER_LONG_CONSTANT("FILEINFO_RAW",                        MAGIC_RAW, CONST_CS|CONST_PERSISTENT);
     243             : #endif
     244             : 
     245       20916 :         return SUCCESS;
     246             : }
     247             : /* }}} */
     248             : 
     249             : /* {{{ fileinfo_module_entry
     250             :  */
     251             : zend_module_entry fileinfo_module_entry = {
     252             :         STANDARD_MODULE_HEADER,
     253             :         "fileinfo",
     254             :         fileinfo_functions,
     255             :         PHP_MINIT(finfo),
     256             :         NULL,
     257             :         NULL,
     258             :         NULL,
     259             :         PHP_MINFO(fileinfo),
     260             :         PHP_FILEINFO_VERSION,
     261             :         STANDARD_MODULE_PROPERTIES
     262             : };
     263             : /* }}} */
     264             : 
     265             : #ifdef COMPILE_DL_FILEINFO
     266             : ZEND_GET_MODULE(fileinfo)
     267             : #endif
     268             : 
     269             : /* {{{ PHP_MINFO_FUNCTION
     270             :  */
     271         142 : PHP_MINFO_FUNCTION(fileinfo)
     272             : {
     273             :         char magic_ver[5];
     274             : 
     275         142 :         (void)snprintf(magic_ver, 4, "%d", magic_version());
     276         142 :         magic_ver[4] = '\0';
     277             : 
     278         142 :         php_info_print_table_start();
     279         142 :         php_info_print_table_row(2, "fileinfo support", "enabled");
     280         142 :         php_info_print_table_row(2, "version", PHP_FILEINFO_VERSION);
     281         142 :         php_info_print_table_row(2, "libmagic", magic_ver);
     282         142 :         php_info_print_table_end();
     283         142 : }
     284             : /* }}} */
     285             : 
     286             : /* {{{ proto resource finfo_open([int options [, string arg]])
     287             :    Create a new fileinfo resource. */
     288          58 : PHP_FUNCTION(finfo_open)
     289             : {
     290          58 :         zend_long options = MAGIC_NONE;
     291          58 :         char *file = NULL;
     292          58 :         size_t file_len = 0;
     293             :         php_fileinfo *finfo;
     294          58 :         FILEINFO_DECLARE_INIT_OBJECT(object)
     295             :         char resolved_path[MAXPATHLEN];
     296             :         zend_error_handling zeh;
     297          58 :         int flags = object ? ZEND_PARSE_PARAMS_THROW : 0;
     298             : 
     299          58 :         if (zend_parse_parameters_ex(flags, ZEND_NUM_ARGS(), "|lp", &options, &file, &file_len) == FAILURE) {
     300           6 :                 RETURN_FALSE;
     301             :         }
     302             : 
     303          52 :         if (object) {
     304          13 :                 finfo_object *finfo_obj = Z_FINFO_P(object);
     305             : 
     306          13 :                 zend_replace_error_handling(EH_THROW, NULL, &zeh);
     307             : 
     308          13 :                 if (finfo_obj->ptr) {
     309           1 :                         magic_close(finfo_obj->ptr->magic);
     310           1 :                         efree(finfo_obj->ptr);
     311           1 :                         finfo_obj->ptr = NULL;
     312             :                 }
     313             :         }
     314             : 
     315          52 :         if (file_len == 0) {
     316          19 :                 file = NULL;
     317          33 :         } else if (file && *file) { /* user specified file, perform open_basedir checks */
     318             : 
     319          33 :                 if (php_check_open_basedir(file)) {
     320           0 :                         if (object) {
     321           0 :                                 zend_restore_error_handling(&zeh);
     322           0 :                                 if (!EG(exception)) {
     323           0 :                                         zend_throw_exception(NULL, "Constructor failed", 0);
     324             :                                 }
     325             :                         }
     326           0 :                         RETURN_FALSE;
     327             :                 }
     328          33 :                 if (!expand_filepath_with_mode(file, resolved_path, NULL, 0, CWD_EXPAND)) {
     329           0 :                         if (object) {
     330           0 :                                 zend_restore_error_handling(&zeh);
     331           0 :                                 if (!EG(exception)) {
     332           0 :                                         zend_throw_exception(NULL, "Constructor failed", 0);
     333             :                                 }
     334             :                         }
     335           0 :                         RETURN_FALSE;
     336             :                 }
     337          33 :                 file = resolved_path;
     338             :         }
     339             : 
     340          52 :         finfo = emalloc(sizeof(php_fileinfo));
     341             : 
     342          52 :         finfo->options = options;
     343          52 :         finfo->magic = magic_open(options);
     344             : 
     345          52 :         if (finfo->magic == NULL) {
     346           0 :                 efree(finfo);
     347           0 :                 php_error_docref(NULL, E_WARNING, "Invalid mode '%pd'.", options);
     348           0 :                 if (object) {
     349           0 :                         zend_restore_error_handling(&zeh);
     350           0 :                         if (!EG(exception)) {
     351           0 :                                 zend_throw_exception(NULL, "Constructor failed", 0);
     352             :                         }
     353             :                 }
     354           0 :                 RETURN_FALSE;
     355             :         }
     356             : 
     357          52 :         if (magic_load(finfo->magic, file) == -1) {
     358           8 :                 php_error_docref(NULL, E_WARNING, "Failed to load magic database at '%s'.", file);
     359           8 :                 magic_close(finfo->magic);
     360           8 :                 efree(finfo);
     361           8 :                 if (object) {
     362           0 :                         zend_restore_error_handling(&zeh);
     363           0 :                         if (!EG(exception)) {
     364           0 :                                 zend_throw_exception(NULL, "Constructor failed", 0);
     365             :                         }
     366             :                 }
     367           8 :                 RETURN_FALSE;
     368             :         }
     369             : 
     370          44 :         if (object) {
     371          13 :                 zend_restore_error_handling(&zeh);
     372          13 :                 FILEINFO_REGISTER_OBJECT(object, finfo);
     373             :         } else {
     374          31 :                 RETURN_RES(zend_register_resource(finfo, le_fileinfo));
     375             :         }
     376             : }
     377             : /* }}} */
     378             : 
     379             : /* {{{ proto resource finfo_close(resource finfo)
     380             :    Close fileinfo resource. */
     381          10 : PHP_FUNCTION(finfo_close)
     382             : {
     383             :         php_fileinfo *finfo;
     384             :         zval *zfinfo;
     385             : 
     386          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zfinfo) == FAILURE) {
     387           2 :                 RETURN_FALSE;
     388             :         }
     389             : 
     390           8 :         if ((finfo = (php_fileinfo *)zend_fetch_resource(Z_RES_P(zfinfo), "file_info", le_fileinfo)) == NULL) {
     391           1 :                 RETURN_FALSE;
     392             :         }
     393             : 
     394           7 :         zend_list_close(Z_RES_P(zfinfo));
     395             : 
     396           7 :         RETURN_TRUE;
     397             : }
     398             : /* }}} */
     399             : 
     400             : /* {{{ proto bool finfo_set_flags(resource finfo, int options)
     401             :    Set libmagic configuration options. */
     402           5 : PHP_FUNCTION(finfo_set_flags)
     403             : {
     404             :         zend_long options;
     405             :         php_fileinfo *finfo;
     406             :         zval *zfinfo;
     407           5 :         FILEINFO_DECLARE_INIT_OBJECT(object)
     408             : 
     409           5 :         if (object) {
     410           2 :                 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &options) == FAILURE) {
     411           1 :                         RETURN_FALSE;
     412             :                 }
     413           1 :                 FILEINFO_FROM_OBJECT(finfo, object);
     414             :         } else {
     415           3 :                 if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zfinfo, &options) == FAILURE) {
     416           1 :                         RETURN_FALSE;
     417             :                 }
     418           2 :                 if ((finfo = (php_fileinfo *)zend_fetch_resource(Z_RES_P(zfinfo), "file_info", le_fileinfo)) == NULL) {
     419           0 :                         RETURN_FALSE;
     420             :                 }
     421             :         }
     422             : 
     423           3 :         FINFO_SET_OPTION(finfo->magic, options)
     424           3 :         finfo->options = options;
     425             : 
     426           3 :         RETURN_TRUE;
     427             : }
     428             : /* }}} */
     429             : 
     430             : #define FILEINFO_MODE_BUFFER 0
     431             : #define FILEINFO_MODE_STREAM 1
     432             : #define FILEINFO_MODE_FILE 2
     433             : 
     434          71 : static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mimetype_emu) /* {{{ */
     435             : {
     436          71 :         zend_long options = 0;
     437          71 :         char *ret_val = NULL, *buffer = NULL;
     438             :         size_t buffer_len;
     439          71 :         php_fileinfo *finfo = NULL;
     440          71 :         zval *zfinfo, *zcontext = NULL;
     441             :         zval *what;
     442          71 :         char mime_directory[] = "directory";
     443             : 
     444          71 :         struct magic_set *magic = NULL;
     445          71 :         FILEINFO_DECLARE_INIT_OBJECT(object)
     446             : 
     447          71 :         if (mimetype_emu) {
     448             : 
     449             :                 /* mime_content_type(..) emulation */
     450          11 :                 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &what) == FAILURE) {
     451           0 :                         return;
     452             :                 }
     453             : 
     454          22 :                 switch (Z_TYPE_P(what)) {
     455             :                         case IS_STRING:
     456           6 :                                 buffer = Z_STRVAL_P(what);
     457           6 :                                 buffer_len = Z_STRLEN_P(what);
     458           6 :                                 mode = FILEINFO_MODE_FILE;
     459           6 :                                 break;
     460             : 
     461             :                         case IS_RESOURCE:
     462           1 :                                 mode = FILEINFO_MODE_STREAM;
     463           1 :                                 break;
     464             : 
     465             :                         default:
     466           4 :                                 php_error_docref(NULL, E_WARNING, "Can only process string or stream arguments");
     467           4 :                                 RETURN_FALSE;
     468             :                 }
     469             : 
     470           7 :                 magic = magic_open(MAGIC_MIME_TYPE);
     471           7 :                 if (magic_load(magic, NULL) == -1) {
     472           0 :                         php_error_docref(NULL, E_WARNING, "Failed to load magic database.");
     473           0 :                         goto common;
     474             :                 }
     475          60 :         } else if (object) {
     476          17 :                 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|lr", &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
     477           0 :                         RETURN_FALSE;
     478             :                 }
     479          17 :                 FILEINFO_FROM_OBJECT(finfo, object);
     480          17 :                 magic = finfo->magic;
     481             :         } else {
     482          43 :                 if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|lr", &zfinfo, &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
     483           2 :                         RETURN_FALSE;
     484             :                 }
     485          41 :                 if ((finfo = (php_fileinfo *)zend_fetch_resource(Z_RES_P(zfinfo), "file_info", le_fileinfo)) == NULL) {
     486           0 :                         RETURN_FALSE;
     487             :                 }
     488          41 :                 magic = finfo->magic;
     489             :         }
     490             : 
     491             :         /* Set options for the current file/buffer. */
     492          65 :         if (options) {
     493          14 :                 FINFO_SET_OPTION(magic, options)
     494             :         }
     495             : 
     496          65 :         switch (mode) {
     497             :                 case FILEINFO_MODE_BUFFER:
     498             :                 {
     499          27 :                         ret_val = (char *) magic_buffer(magic, buffer, buffer_len);
     500          27 :                         break;
     501             :                 }
     502             : 
     503             :                 case FILEINFO_MODE_STREAM:
     504             :                 {
     505             :                                 php_stream *stream;
     506             :                                 zend_off_t streampos;
     507             : 
     508           1 :                                 php_stream_from_zval_no_verify(stream, what);
     509           1 :                                 if (!stream) {
     510           0 :                                         goto common;
     511             :                                 }
     512             : 
     513           1 :                                 streampos = php_stream_tell(stream); /* remember stream position for restoration */
     514           1 :                                 php_stream_seek(stream, 0, SEEK_SET);
     515             : 
     516           1 :                                 ret_val = (char *) magic_stream(magic, stream);
     517             : 
     518           1 :                                 php_stream_seek(stream, streampos, SEEK_SET);
     519           1 :                                 break;
     520             :                 }
     521             : 
     522             :                 case FILEINFO_MODE_FILE:
     523             :                 {
     524             :                         /* determine if the file is a local file or remote URL */
     525             :                         const char *tmp2;
     526             :                         php_stream_wrapper *wrap;
     527             :                         php_stream_statbuf ssb;
     528             : 
     529          37 :                         if (buffer == NULL || !*buffer) {
     530           5 :                                 php_error_docref(NULL, E_WARNING, "Empty filename or path");
     531           5 :                                 RETVAL_FALSE;
     532           5 :                                 goto clean;
     533             :                         }
     534          32 :                         if (CHECK_NULL_PATH(buffer, buffer_len)) {
     535           1 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid path");
     536           1 :                                 RETVAL_FALSE;
     537           1 :                                 goto clean;
     538             :                         }
     539             : 
     540          31 :                         wrap = php_stream_locate_url_wrapper(buffer, &tmp2, 0);
     541             : 
     542          31 :                         if (wrap) {
     543             :                                 php_stream *stream;
     544          31 :                                 php_stream_context *context = php_stream_context_from_zval(zcontext, 0);
     545             : 
     546             : #ifdef PHP_WIN32
     547             :                                 if (php_stream_stat_path_ex(buffer, 0, &ssb, context) == SUCCESS) {
     548             :                                         if (ssb.sb.st_mode & S_IFDIR) {
     549             :                                                 ret_val = mime_directory;
     550             :                                                 goto common;
     551             :                                         }
     552             :                                 }
     553             : #endif
     554             : 
     555             : #if PHP_API_VERSION < 20100412
     556             :                                 stream = php_stream_open_wrapper_ex(buffer, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context);
     557             : #else
     558          31 :                                 stream = php_stream_open_wrapper_ex(buffer, "rb", REPORT_ERRORS, NULL, context);
     559             : #endif
     560             : 
     561          31 :                                 if (!stream) {
     562           2 :                                         RETVAL_FALSE;
     563           2 :                                         goto clean;
     564             :                                 }
     565             : 
     566          29 :                                 if (php_stream_stat(stream, &ssb) == SUCCESS) {
     567          29 :                                         if (ssb.sb.st_mode & S_IFDIR) {
     568           4 :                                                 ret_val = mime_directory;
     569             :                                         } else {
     570          25 :                                                 ret_val = (char *)magic_stream(magic, stream);
     571             :                                         }
     572             :                                 }
     573             : 
     574          29 :                                 php_stream_close(stream);
     575             :                         }
     576          29 :                         break;
     577             :                 }
     578             : 
     579             :                 default:
     580           0 :                         php_error_docref(NULL, E_WARNING, "Can only process string or stream arguments");
     581             :         }
     582             : 
     583             : common:
     584          57 :         if (ret_val) {
     585         112 :                 RETVAL_STRING(ret_val);
     586             :         } else {
     587           1 :                 php_error_docref(NULL, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic));
     588           1 :                 RETVAL_FALSE;
     589             :         }
     590             : 
     591             : clean:
     592          65 :         if (mimetype_emu) {
     593           7 :                 magic_close(magic);
     594             :         }
     595             : 
     596             :         /* Restore options */
     597          65 :         if (options) {
     598          14 :                 FINFO_SET_OPTION(magic, finfo->options)
     599             :         }
     600          65 :         return;
     601             : }
     602             : /* }}} */
     603             : 
     604             : /* {{{ proto string finfo_file(resource finfo, char *file_name [, int options [, resource context]])
     605             :    Return information about a file. */
     606          31 : PHP_FUNCTION(finfo_file)
     607             : {
     608          31 :         _php_finfo_get_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, FILEINFO_MODE_FILE, 0);
     609          31 : }
     610             : /* }}} */
     611             : 
     612             : /* {{{ proto string finfo_buffer(resource finfo, char *string [, int options [, resource context]])
     613             :    Return infromation about a string buffer. */
     614          29 : PHP_FUNCTION(finfo_buffer)
     615             : {
     616          29 :         _php_finfo_get_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, FILEINFO_MODE_BUFFER, 0);
     617          29 : }
     618             : /* }}} */
     619             : 
     620             : /* {{{ proto string mime_content_type(string filename|resource stream)
     621             :    Return content-type for file */
     622          11 : PHP_FUNCTION(mime_content_type)
     623             : {
     624          11 :         _php_finfo_get_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, -1, 1);
     625          11 : }
     626             : /* }}} */
     627             : 
     628             : 
     629             : /*
     630             :  * Local variables:
     631             :  * tab-width: 4
     632             :  * c-basic-offset: 4
     633             :  * End:
     634             :  * vim600: noet sw=4 ts=4 fdm=marker
     635             :  * vim<600: noet sw=4 ts=4
     636             :  */

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:58:55 +0000 (8 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.