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: 174 190 91.6 %
Date: 2014-04-08 Functions: 12 12 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 08 Apr 2014 11:59:37 +0000 (8 days ago)

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