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

LTP GCOV extension - code coverage report
Current view: directory - fileinfo - fileinfo.c
Test: PHP Code Coverage
Date: 2009-11-21 Instrumented lines: 179
Code covered: 92.2 % Executed lines: 165
Legend: not executed executed

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

Generated by: LTP GCOV extension version 1.5

Generated at Sat, 21 Nov 2009 12:26:59 +0000 (3 days ago)

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