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

Generated by: LCOV version 1.10

Generated at Fri, 20 Mar 2015 23:40:26 +0000 (6 days ago)

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