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/phar - func_interceptors.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 522 604 86.4 %
Date: 2015-05-21 Functions: 30 30 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | phar php single-file executable PHP extension                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 2005-2015 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 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_01.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             :   | Authors: Gregory Beaver <cellog@php.net>                             |
      16             :   +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #include "phar_internal.h"
      22             : 
      23             : #define PHAR_FUNC(name) \
      24             :         static PHP_NAMED_FUNCTION(name)
      25             : 
      26         810 : PHAR_FUNC(phar_opendir) /* {{{ */
      27             : {
      28             :         char *filename;
      29             :         size_t filename_len;
      30         810 :         zval *zcontext = NULL;
      31             : 
      32         810 :         if (!PHAR_G(intercepted)) {
      33         802 :                 goto skip_phar;
      34             :         }
      35             : 
      36           9 :         if ((PHAR_G(phar_fname_map.u.flags) && !zend_hash_num_elements(&(PHAR_G(phar_fname_map))))
      37           1 :                 && !cached_phars.u.flags) {
      38           1 :                 goto skip_phar;
      39             :         }
      40             : 
      41           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|z", &filename, &filename_len, &zcontext) == FAILURE) {
      42           1 :                 return;
      43             :         }
      44             : 
      45           6 :         if (!IS_ABSOLUTE_PATH(filename, filename_len) && !strstr(filename, "://")) {
      46             :                 char *arch, *entry, *fname;
      47             :                 int arch_len, entry_len, fname_len;
      48           4 :                 fname = (char*)zend_get_executed_filename();
      49             : 
      50             :                 /* we are checking for existence of a file within the relative path.  Chances are good that this is
      51             :                    retrieving something from within the phar archive */
      52             : 
      53           4 :                 if (strncasecmp(fname, "phar://", 7)) {
      54           1 :                         goto skip_phar;
      55             :                 }
      56           3 :                 fname_len = strlen(fname);
      57           3 :                 if (SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
      58           3 :                         php_stream_context *context = NULL;
      59             :                         php_stream *stream;
      60             :                         char *name;
      61             : 
      62           3 :                         efree(entry);
      63           3 :                         entry = estrndup(filename, filename_len);
      64             :                         /* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
      65           3 :                         entry_len = filename_len;
      66             :                         /* retrieving a file within the current directory, so use this if possible */
      67           3 :                         entry = phar_fix_filepath(entry, &entry_len, 1);
      68             : 
      69           3 :                         if (entry[0] == '/') {
      70           1 :                                 spprintf(&name, 4096, "phar://%s%s", arch, entry);
      71             :                         } else {
      72           2 :                                 spprintf(&name, 4096, "phar://%s/%s", arch, entry);
      73             :                         }
      74           3 :                         efree(entry);
      75           3 :                         efree(arch);
      76           3 :                         if (zcontext) {
      77           1 :                                 context = php_stream_context_from_zval(zcontext, 0);
      78             :                         }
      79           3 :                         stream = php_stream_opendir(name, REPORT_ERRORS, context);
      80           3 :                         efree(name);
      81           3 :                         if (!stream) {
      82           1 :                                 RETURN_FALSE;
      83             :                         }
      84           2 :                         php_stream_to_zval(stream, return_value);
      85           2 :                         return;
      86             :                 }
      87             :         }
      88             : skip_phar:
      89         806 :         PHAR_G(orig_opendir)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
      90         806 :         return;
      91             : }
      92             : /* }}} */
      93             : 
      94        3220 : PHAR_FUNC(phar_file_get_contents) /* {{{ */
      95             : {
      96             :         char *filename;
      97             :         size_t filename_len;
      98             :         zend_string *contents;
      99        3220 :         zend_bool use_include_path = 0;
     100             :         php_stream *stream;
     101        3220 :         zend_long offset = -1;
     102        3220 :         zend_long maxlen = PHP_STREAM_COPY_ALL;
     103        3220 :         zval *zcontext = NULL;
     104             : 
     105        3220 :         if (!PHAR_G(intercepted)) {
     106        3205 :                 goto skip_phar;
     107             :         }
     108             : 
     109          16 :         if ((PHAR_G(phar_fname_map.u.flags) && !zend_hash_num_elements(&(PHAR_G(phar_fname_map))))
     110           1 :                 && !cached_phars.u.flags) {
     111           1 :                 goto skip_phar;
     112             :         }
     113             : 
     114             :         /* Parse arguments */
     115          14 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "p|br!ll", &filename, &filename_len, &use_include_path, &zcontext, &offset, &maxlen) == FAILURE) {
     116           1 :                 goto skip_phar;
     117             :         }
     118             : 
     119          13 :         if (use_include_path || (!IS_ABSOLUTE_PATH(filename, filename_len) && !strstr(filename, "://"))) {
     120             :                 char *arch, *entry, *fname;
     121          12 :                 zend_string *entry_str = NULL;
     122             :                 int arch_len, entry_len, fname_len;
     123          12 :                 php_stream_context *context = NULL;
     124             : 
     125          12 :                 fname = (char*)zend_get_executed_filename();
     126             : 
     127          12 :                 if (strncasecmp(fname, "phar://", 7)) {
     128           0 :                         goto skip_phar;
     129             :                 }
     130          12 :                 fname_len = strlen(fname);
     131          12 :                 if (SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
     132             :                         char *name;
     133             :                         phar_archive_data *phar;
     134             : 
     135          12 :                         efree(entry);
     136          12 :                         entry = filename;
     137             :                         /* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
     138          12 :                         entry_len = filename_len;
     139             : 
     140          12 :                         if (ZEND_NUM_ARGS() == 5 && maxlen < 0) {
     141           1 :                                 efree(arch);
     142           1 :                                 php_error_docref(NULL, E_WARNING, "length must be greater than or equal to zero");
     143           1 :                                 RETURN_FALSE;
     144             :                         }
     145             : 
     146             :                         /* retrieving a file defaults to within the current directory, so use this if possible */
     147          11 :                         if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
     148           0 :                                 efree(arch);
     149           0 :                                 goto skip_phar;
     150             :                         }
     151          11 :                         if (use_include_path) {
     152           2 :                                 if ((entry_str = phar_find_in_include_path(entry, entry_len, NULL))) {
     153           2 :                                         name = entry_str->val;
     154           2 :                                         goto phar_it;
     155             :                                 } else {
     156             :                                         /* this file is not in the phar, use the original path */
     157           0 :                                         efree(arch);
     158           0 :                                         goto skip_phar;
     159             :                                 }
     160             :                         } else {
     161           9 :                                 entry = phar_fix_filepath(estrndup(entry, entry_len), &entry_len, 1);
     162           9 :                                 if (entry[0] == '/') {
     163           3 :                                         if (!zend_hash_str_exists(&(phar->manifest), entry + 1, entry_len - 1)) {
     164             :                                                 /* this file is not in the phar, use the original path */
     165             : notfound:
     166           1 :                                                 efree(arch);
     167           1 :                                                 efree(entry);
     168           1 :                                                 goto skip_phar;
     169             :                                         }
     170             :                                 } else {
     171           6 :                                         if (!zend_hash_str_exists(&(phar->manifest), entry, entry_len)) {
     172           1 :                                                 goto notfound;
     173             :                                         }
     174             :                                 }
     175             :                                 /* auto-convert to phar:// */
     176           8 :                                 if (entry[0] == '/') {
     177           3 :                                         spprintf(&name, 4096, "phar://%s%s", arch, entry);
     178             :                                 } else {
     179           5 :                                         spprintf(&name, 4096, "phar://%s/%s", arch, entry);
     180             :                                 }
     181           8 :                                 if (entry != filename) {
     182           8 :                                         efree(entry);
     183             :                                 }
     184             :                         }
     185             : 
     186             : phar_it:
     187          10 :                         efree(arch);
     188          10 :                         if (zcontext) {
     189           3 :                                 context = php_stream_context_from_zval(zcontext, 0);
     190             :                         }
     191          10 :                         stream = php_stream_open_wrapper_ex(name, "rb", 0 | REPORT_ERRORS, NULL, context);
     192          10 :                         if (entry_str) {
     193             :                                 zend_string_release(entry_str);
     194             :                         } else {
     195           8 :                                 efree(name);
     196             :                         }
     197             : 
     198          10 :                         if (!stream) {
     199           1 :                                 RETURN_FALSE;
     200             :                         }
     201             : 
     202           9 :                         if (offset > 0 && php_stream_seek(stream, offset, SEEK_SET) < 0) {
     203           1 :                                 php_error_docref(NULL, E_WARNING, "Failed to seek to position %pd in the stream", offset);
     204           1 :                                 php_stream_close(stream);
     205           1 :                                 RETURN_FALSE;
     206             :                         }
     207             : 
     208             :                         /* uses mmap if possible */
     209           8 :                         contents = php_stream_copy_to_mem(stream, maxlen, 0);
     210          15 :                         if (contents && contents->len > 0) {
     211           7 :                                 RETVAL_STR(contents);
     212           1 :                         } else if (contents) {
     213             :                                 zend_string_release(contents);
     214           1 :                                 RETVAL_EMPTY_STRING();
     215             :                         } else {
     216           0 :                                 RETVAL_FALSE;
     217             :                         }
     218             : 
     219           8 :                         php_stream_close(stream);
     220           8 :                         return;
     221             :                 }
     222             :         }
     223             : skip_phar:
     224        3209 :         PHAR_G(orig_file_get_contents)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     225        3209 :         return;
     226             : }
     227             : /* }}} */
     228             : 
     229         460 : PHAR_FUNC(phar_readfile) /* {{{ */
     230             : {
     231             :         char *filename;
     232             :         size_t filename_len;
     233         460 :         int size = 0;
     234         460 :         zend_bool use_include_path = 0;
     235         460 :         zval *zcontext = NULL;
     236             :         php_stream *stream;
     237             : 
     238         460 :         if (!PHAR_G(intercepted)) {
     239         457 :                 goto skip_phar;
     240             :         }
     241             : 
     242           4 :         if ((PHAR_G(phar_fname_map.u.flags) && !zend_hash_num_elements(&(PHAR_G(phar_fname_map))))
     243           1 :                 && !cached_phars.u.flags) {
     244           1 :                 goto skip_phar;
     245             :         }
     246           2 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "p|br!", &filename, &filename_len, &use_include_path, &zcontext) == FAILURE) {
     247           0 :                 goto skip_phar;
     248             :         }
     249           2 :         if (use_include_path || (!IS_ABSOLUTE_PATH(filename, filename_len) && !strstr(filename, "://"))) {
     250             :                 char *arch, *entry, *fname;
     251           2 :                 zend_string *entry_str = NULL;
     252             :                 int arch_len, entry_len, fname_len;
     253           2 :                 php_stream_context *context = NULL;
     254             :                 char *name;
     255             :                 phar_archive_data *phar;
     256           2 :                 fname = (char*)zend_get_executed_filename();
     257             : 
     258           2 :                 if (strncasecmp(fname, "phar://", 7)) {
     259           0 :                         goto skip_phar;
     260             :                 }
     261           2 :                 fname_len = strlen(fname);
     262           2 :                 if (FAILURE == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
     263           0 :                         goto skip_phar;
     264             :                 }
     265             : 
     266           2 :                 efree(entry);
     267           2 :                 entry = filename;
     268             :                 /* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
     269           2 :                 entry_len = filename_len;
     270             :                 /* retrieving a file defaults to within the current directory, so use this if possible */
     271           2 :                 if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
     272           0 :                         efree(arch);
     273           0 :                         goto skip_phar;
     274             :                 }
     275           2 :                 if (use_include_path) {
     276           1 :                         if (!(entry_str = phar_find_in_include_path(entry, entry_len, NULL))) {
     277             :                                 /* this file is not in the phar, use the original path */
     278           0 :                                 efree(arch);
     279           0 :                                 goto skip_phar;
     280             :                         } else {
     281           1 :                                 name = entry_str->val;
     282             :                         }
     283             :                 } else {
     284           1 :                         entry = phar_fix_filepath(estrndup(entry, entry_len), &entry_len, 1);
     285           1 :                         if (entry[0] == '/') {
     286           1 :                                 if (!zend_hash_str_exists(&(phar->manifest), entry + 1, entry_len - 1)) {
     287             :                                         /* this file is not in the phar, use the original path */
     288             : notfound:
     289           0 :                                         efree(entry);
     290           0 :                                         efree(arch);
     291           0 :                                         goto skip_phar;
     292             :                                 }
     293             :                         } else {
     294           0 :                                 if (!zend_hash_str_exists(&(phar->manifest), entry, entry_len)) {
     295           0 :                                         goto notfound;
     296             :                                 }
     297             :                         }
     298             :                         /* auto-convert to phar:// */
     299           1 :                         if (entry[0] == '/') {
     300           1 :                                 spprintf(&name, 4096, "phar://%s%s", arch, entry);
     301             :                         } else {
     302           0 :                                 spprintf(&name, 4096, "phar://%s/%s", arch, entry);
     303             :                         }
     304           1 :                         efree(entry);
     305             :                 }
     306             : 
     307           2 :                 efree(arch);
     308           2 :                 context = php_stream_context_from_zval(zcontext, 0);
     309           2 :                 stream = php_stream_open_wrapper_ex(name, "rb", 0 | REPORT_ERRORS, NULL, context);
     310           2 :                 if (entry_str) {
     311             :                         zend_string_release(entry_str);
     312             :                 } else {
     313           1 :                         efree(name);
     314             :                 }
     315           2 :                 if (stream == NULL) {
     316           0 :                         RETURN_FALSE;
     317             :                 }
     318           2 :                 size = php_stream_passthru(stream);
     319           2 :                 php_stream_close(stream);
     320           2 :                 RETURN_LONG(size);
     321             :         }
     322             : 
     323             : skip_phar:
     324         458 :         PHAR_G(orig_readfile)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     325         458 :         return;
     326             : 
     327             : }
     328             : /* }}} */
     329             : 
     330       24513 : PHAR_FUNC(phar_fopen) /* {{{ */
     331             : {
     332             :         char *filename, *mode;
     333             :         size_t filename_len, mode_len;
     334       24513 :         zend_bool use_include_path = 0;
     335       24513 :         zval *zcontext = NULL;
     336             :         php_stream *stream;
     337             : 
     338       24513 :         if (!PHAR_G(intercepted)) {
     339       24506 :                 goto skip_phar;
     340             :         }
     341             : 
     342           8 :         if ((PHAR_G(phar_fname_map.u.flags) && !zend_hash_num_elements(&(PHAR_G(phar_fname_map))))
     343           1 :                 && !cached_phars.u.flags) {
     344             :                 /* no need to check, include_path not even specified in fopen/ no active phars */
     345           1 :                 goto skip_phar;
     346             :         }
     347           6 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "ps|br", &filename, &filename_len, &mode, &mode_len, &use_include_path, &zcontext) == FAILURE) {
     348           1 :                 goto skip_phar;
     349             :         }
     350           5 :         if (use_include_path || (!IS_ABSOLUTE_PATH(filename, filename_len) && !strstr(filename, "://"))) {
     351             :                 char *arch, *entry, *fname;
     352           4 :                 zend_string *entry_str = NULL;
     353             :                 int arch_len, entry_len, fname_len;
     354           4 :                 php_stream_context *context = NULL;
     355             :                 char *name;
     356             :                 phar_archive_data *phar;
     357           4 :                 fname = (char*)zend_get_executed_filename();
     358             : 
     359           4 :                 if (strncasecmp(fname, "phar://", 7)) {
     360           0 :                         goto skip_phar;
     361             :                 }
     362           4 :                 fname_len = strlen(fname);
     363           4 :                 if (FAILURE == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
     364           0 :                         goto skip_phar;
     365             :                 }
     366             : 
     367           4 :                 efree(entry);
     368           4 :                 entry = filename;
     369             :                 /* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
     370           4 :                 entry_len = filename_len;
     371             :                 /* retrieving a file defaults to within the current directory, so use this if possible */
     372           4 :                 if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
     373           0 :                         efree(arch);
     374           0 :                         goto skip_phar;
     375             :                 }
     376           4 :                 if (use_include_path) {
     377           3 :                         if (!(entry_str = phar_find_in_include_path(entry, entry_len, NULL))) {
     378             :                                 /* this file is not in the phar, use the original path */
     379           1 :                                 efree(arch);
     380           1 :                                 goto skip_phar;
     381             :                         } else {
     382           2 :                                 name = entry_str->val;
     383             :                         }
     384             :                 } else {
     385           1 :                         entry = phar_fix_filepath(estrndup(entry, entry_len), &entry_len, 1);
     386           1 :                         if (entry[0] == '/') {
     387           1 :                                 if (!zend_hash_str_exists(&(phar->manifest), entry + 1, entry_len - 1)) {
     388             :                                         /* this file is not in the phar, use the original path */
     389             : notfound:
     390           0 :                                         efree(entry);
     391           0 :                                         efree(arch);
     392           0 :                                         goto skip_phar;
     393             :                                 }
     394             :                         } else {
     395           0 :                                 if (!zend_hash_str_exists(&(phar->manifest), entry, entry_len)) {
     396             :                                         /* this file is not in the phar, use the original path */
     397           0 :                                         goto notfound;
     398             :                                 }
     399             :                         }
     400             :                         /* auto-convert to phar:// */
     401           1 :                         if (entry[0] == '/') {
     402           1 :                                 spprintf(&name, 4096, "phar://%s%s", arch, entry);
     403             :                         } else {
     404           0 :                                 spprintf(&name, 4096, "phar://%s/%s", arch, entry);
     405             :                         }
     406           1 :                         efree(entry);
     407             :                 }
     408             : 
     409           3 :                 efree(arch);
     410           3 :                 context = php_stream_context_from_zval(zcontext, 0);
     411           3 :                 stream = php_stream_open_wrapper_ex(name, mode, 0 | REPORT_ERRORS, NULL, context);
     412           3 :                 if (entry_str) {
     413             :                         zend_string_release(entry_str);
     414             :                 } else {
     415           1 :                         efree(name);
     416             :                 }
     417           3 :                 if (stream == NULL) {
     418           0 :                         RETURN_FALSE;
     419             :                 }
     420           3 :                 php_stream_to_zval(stream, return_value);
     421           3 :                 if (zcontext) {
     422           0 :                         Z_ADDREF_P(zcontext);
     423             :                 }
     424           3 :                 return;
     425             :         }
     426             : skip_phar:
     427       24510 :         PHAR_G(orig_fopen)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     428       24510 :         return;
     429             : }
     430             : /* }}} */
     431             : 
     432             : #ifndef S_ISDIR
     433             : #define S_ISDIR(mode)   (((mode)&S_IFMT) == S_IFDIR)
     434             : #endif
     435             : #ifndef S_ISREG
     436             : #define S_ISREG(mode)   (((mode)&S_IFMT) == S_IFREG)
     437             : #endif
     438             : #ifndef S_ISLNK
     439             : #define S_ISLNK(mode)   (((mode)&S_IFMT) == S_IFLNK)
     440             : #endif
     441             : 
     442             : #define S_IXROOT ( S_IXUSR | S_IXGRP | S_IXOTH )
     443             : 
     444             : #define IS_LINK_OPERATION(__t) ((__t) == FS_TYPE || (__t) == FS_IS_LINK || (__t) == FS_LSTAT)
     445             : #define IS_EXISTS_CHECK(__t) ((__t) == FS_EXISTS  || (__t) == FS_IS_W || (__t) == FS_IS_R || (__t) == FS_IS_X || (__t) == FS_IS_FILE || (__t) == FS_IS_DIR || (__t) == FS_IS_LINK)
     446             : #define IS_ABLE_CHECK(__t) ((__t) == FS_IS_R || (__t) == FS_IS_W || (__t) == FS_IS_X)
     447             : #define IS_ACCESS_CHECK(__t) (IS_ABLE_CHECK(type) || (__t) == FS_EXISTS)
     448             : 
     449             : /* {{{ php_stat
     450             :  */
     451          19 : static void phar_fancy_stat(zend_stat_t *stat_sb, int type, zval *return_value)
     452             : {
     453             :         zval stat_dev, stat_ino, stat_mode, stat_nlink, stat_uid, stat_gid, stat_rdev,
     454             :                  stat_size, stat_atime, stat_mtime, stat_ctime, stat_blksize, stat_blocks;
     455          19 :         int rmask=S_IROTH, wmask=S_IWOTH, xmask=S_IXOTH; /* access rights defaults to other */
     456             :         char *stat_sb_names[13] = {
     457             :                 "dev", "ino", "mode", "nlink", "uid", "gid", "rdev",
     458             :                 "size", "atime", "mtime", "ctime", "blksize", "blocks"
     459          19 :         };
     460             : 
     461             : #ifndef NETWARE
     462          19 :         if (type >= FS_IS_W && type <= FS_IS_X) {
     463           3 :                 if(stat_sb->st_uid==getuid()) {
     464           0 :                         rmask=S_IRUSR;
     465           0 :                         wmask=S_IWUSR;
     466           0 :                         xmask=S_IXUSR;
     467           3 :                 } else if(stat_sb->st_gid==getgid()) {
     468           0 :                         rmask=S_IRGRP;
     469           0 :                         wmask=S_IWGRP;
     470           0 :                         xmask=S_IXGRP;
     471             :                 } else {
     472             :                         int   groups, n, i;
     473             :                         gid_t *gids;
     474             : 
     475           3 :                         groups = getgroups(0, NULL);
     476           3 :                         if(groups > 0) {
     477           3 :                                 gids=(gid_t *)safe_emalloc(groups, sizeof(gid_t), 0);
     478           3 :                                 n=getgroups(groups, gids);
     479           9 :                                 for(i=0;i<n;++i){
     480           6 :                                         if(stat_sb->st_gid==gids[i]) {
     481           0 :                                                 rmask=S_IRGRP;
     482           0 :                                                 wmask=S_IWGRP;
     483           0 :                                                 xmask=S_IXGRP;
     484           0 :                                                 break;
     485             :                                         }
     486             :                                 }
     487           3 :                                 efree(gids);
     488             :                         }
     489             :                 }
     490             :         }
     491             : #endif
     492             : 
     493          19 :         switch (type) {
     494             :         case FS_PERMS:
     495           1 :                 RETURN_LONG((zend_long)stat_sb->st_mode);
     496             :         case FS_INODE:
     497           1 :                 RETURN_LONG((zend_long)stat_sb->st_ino);
     498             :         case FS_SIZE:
     499           1 :                 RETURN_LONG((zend_long)stat_sb->st_size);
     500             :         case FS_OWNER:
     501           1 :                 RETURN_LONG((zend_long)stat_sb->st_uid);
     502             :         case FS_GROUP:
     503           1 :                 RETURN_LONG((zend_long)stat_sb->st_gid);
     504             :         case FS_ATIME:
     505             : #ifdef NETWARE
     506             :                 RETURN_LONG((zend_long)stat_sb->st_atime.tv_sec);
     507             : #else
     508           1 :                 RETURN_LONG((zend_long)stat_sb->st_atime);
     509             : #endif
     510             :         case FS_MTIME:
     511             : #ifdef NETWARE
     512             :                 RETURN_LONG((zend_long)stat_sb->st_mtime.tv_sec);
     513             : #else
     514           1 :                 RETURN_LONG((zend_long)stat_sb->st_mtime);
     515             : #endif
     516             :         case FS_CTIME:
     517             : #ifdef NETWARE
     518             :                 RETURN_LONG((zend_long)stat_sb->st_ctime.tv_sec);
     519             : #else
     520           1 :                 RETURN_LONG((zend_long)stat_sb->st_ctime);
     521             : #endif
     522             :         case FS_TYPE:
     523           3 :                 if (S_ISLNK(stat_sb->st_mode)) {
     524           2 :                         RETURN_STRING("link");
     525             :                 }
     526           2 :                 switch(stat_sb->st_mode & S_IFMT) {
     527           2 :                 case S_IFDIR: RETURN_STRING("dir");
     528           2 :                 case S_IFREG: RETURN_STRING("file");
     529             :                 }
     530           0 :                 php_error_docref(NULL, E_NOTICE, "Unknown file type (%u)", stat_sb->st_mode & S_IFMT);
     531           0 :                 RETURN_STRING("unknown");
     532             :         case FS_IS_W:
     533           1 :                 RETURN_BOOL((stat_sb->st_mode & wmask) != 0);
     534             :         case FS_IS_R:
     535           1 :                 RETURN_BOOL((stat_sb->st_mode&rmask)!=0);
     536             :         case FS_IS_X:
     537           1 :                 RETURN_BOOL((stat_sb->st_mode&xmask)!=0 && !S_ISDIR(stat_sb->st_mode));
     538             :         case FS_IS_FILE:
     539           0 :                 RETURN_BOOL(S_ISREG(stat_sb->st_mode));
     540             :         case FS_IS_DIR:
     541           1 :                 RETURN_BOOL(S_ISDIR(stat_sb->st_mode));
     542             :         case FS_IS_LINK:
     543           0 :                 RETURN_BOOL(S_ISLNK(stat_sb->st_mode));
     544             :         case FS_EXISTS:
     545           2 :                 RETURN_TRUE; /* the false case was done earlier */
     546             :         case FS_LSTAT:
     547             :                 /* FALLTHROUGH */
     548             :         case FS_STAT:
     549           2 :                 array_init(return_value);
     550             : 
     551           2 :                 ZVAL_LONG(&stat_dev, stat_sb->st_dev);
     552           2 :                 ZVAL_LONG(&stat_ino, stat_sb->st_ino);
     553           2 :                 ZVAL_LONG(&stat_mode, stat_sb->st_mode);
     554           2 :                 ZVAL_LONG(&stat_nlink, stat_sb->st_nlink);
     555           2 :                 ZVAL_LONG(&stat_uid, stat_sb->st_uid);
     556           2 :                 ZVAL_LONG(&stat_gid, stat_sb->st_gid);
     557             : #ifdef HAVE_ST_RDEV
     558           2 :                 ZVAL_LONG(&stat_rdev, stat_sb->st_rdev);
     559             : #else
     560             :                 ZVAL_LONG(&stat_rdev, -1);
     561             : #endif
     562           2 :                 ZVAL_LONG(&stat_size, stat_sb->st_size);
     563             : #ifdef NETWARE
     564             :                 ZVAL_LONG(&stat_atime, (stat_sb->st_atime).tv_sec);
     565             :                 ZVAL_LONG(&stat_mtime, (stat_sb->st_mtime).tv_sec);
     566             :                 ZVAL_LONG(&stat_ctime, (stat_sb->st_ctime).tv_sec);
     567             : #else
     568           2 :                 ZVAL_LONG(&stat_atime, stat_sb->st_atime);
     569           2 :                 ZVAL_LONG(&stat_mtime, stat_sb->st_mtime);
     570           2 :                 ZVAL_LONG(&stat_ctime, stat_sb->st_ctime);
     571             : #endif
     572             : #ifdef HAVE_ST_BLKSIZE
     573           2 :                 ZVAL_LONG(&stat_blksize, stat_sb->st_blksize);
     574             : #else
     575             :                 ZVAL_LONG(&stat_blksize,-1);
     576             : #endif
     577             : #ifdef HAVE_ST_BLOCKS
     578           2 :                 ZVAL_LONG(&stat_blocks, stat_sb->st_blocks);
     579             : #else
     580             :                 ZVAL_LONG(&stat_blocks,-1);
     581             : #endif
     582             :                 /* Store numeric indexes in proper order */
     583           2 :                 zend_hash_next_index_insert(HASH_OF(return_value), &stat_dev);
     584           2 :                 zend_hash_next_index_insert(HASH_OF(return_value), &stat_ino);
     585           2 :                 zend_hash_next_index_insert(HASH_OF(return_value), &stat_mode);
     586           2 :                 zend_hash_next_index_insert(HASH_OF(return_value), &stat_nlink);
     587           2 :                 zend_hash_next_index_insert(HASH_OF(return_value), &stat_uid);
     588           2 :                 zend_hash_next_index_insert(HASH_OF(return_value), &stat_gid);
     589             : 
     590           2 :                 zend_hash_next_index_insert(HASH_OF(return_value), &stat_rdev);
     591           2 :                 zend_hash_next_index_insert(HASH_OF(return_value), &stat_size);
     592           2 :                 zend_hash_next_index_insert(HASH_OF(return_value), &stat_atime);
     593           2 :                 zend_hash_next_index_insert(HASH_OF(return_value), &stat_mtime);
     594           2 :                 zend_hash_next_index_insert(HASH_OF(return_value), &stat_ctime);
     595           2 :                 zend_hash_next_index_insert(HASH_OF(return_value), &stat_blksize);
     596           2 :                 zend_hash_next_index_insert(HASH_OF(return_value), &stat_blocks);
     597             : 
     598             :                 /* Store string indexes referencing the same zval*/
     599           4 :                 zend_hash_str_update(HASH_OF(return_value), stat_sb_names[0], strlen(stat_sb_names[0]), &stat_dev);
     600           4 :                 zend_hash_str_update(HASH_OF(return_value), stat_sb_names[1], strlen(stat_sb_names[1]), &stat_ino);
     601           4 :                 zend_hash_str_update(HASH_OF(return_value), stat_sb_names[2], strlen(stat_sb_names[2]), &stat_mode);
     602           4 :                 zend_hash_str_update(HASH_OF(return_value), stat_sb_names[3], strlen(stat_sb_names[3]), &stat_nlink);
     603           4 :                 zend_hash_str_update(HASH_OF(return_value), stat_sb_names[4], strlen(stat_sb_names[4]), &stat_uid);
     604           4 :                 zend_hash_str_update(HASH_OF(return_value), stat_sb_names[5], strlen(stat_sb_names[5]), &stat_gid);
     605           4 :                 zend_hash_str_update(HASH_OF(return_value), stat_sb_names[6], strlen(stat_sb_names[6]), &stat_rdev);
     606           4 :                 zend_hash_str_update(HASH_OF(return_value), stat_sb_names[7], strlen(stat_sb_names[7]), &stat_size);
     607           4 :                 zend_hash_str_update(HASH_OF(return_value), stat_sb_names[8], strlen(stat_sb_names[8]), &stat_atime);
     608           4 :                 zend_hash_str_update(HASH_OF(return_value), stat_sb_names[9], strlen(stat_sb_names[9]), &stat_mtime);
     609           4 :                 zend_hash_str_update(HASH_OF(return_value), stat_sb_names[10], strlen(stat_sb_names[10]), &stat_ctime);
     610           4 :                 zend_hash_str_update(HASH_OF(return_value), stat_sb_names[11], strlen(stat_sb_names[11]), &stat_blksize);
     611           4 :                 zend_hash_str_update(HASH_OF(return_value), stat_sb_names[12], strlen(stat_sb_names[12]), &stat_blocks);
     612             : 
     613           2 :                 return;
     614             :         }
     615           0 :         php_error_docref(NULL, E_WARNING, "Didn't understand stat call");
     616           0 :         RETURN_FALSE;
     617             : }
     618             : /* }}} */
     619             : 
     620          27 : static void phar_file_stat(const char *filename, php_stat_len filename_length, int type, void (*orig_stat_func)(INTERNAL_FUNCTION_PARAMETERS), INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
     621             : {
     622          27 :         if (!filename_length) {
     623           1 :                 RETURN_FALSE;
     624             :         }
     625             : 
     626          26 :         if (!IS_ABSOLUTE_PATH(filename, filename_length) && !strstr(filename, "://")) {
     627             :                 char *arch, *entry, *fname;
     628             :                 int arch_len, entry_len, fname_len;
     629          25 :                 zend_stat_t sb = {0};
     630          25 :                 phar_entry_info *data = NULL;
     631             :                 phar_archive_data *phar;
     632             : 
     633          25 :                 fname = (char*)zend_get_executed_filename();
     634             : 
     635             :                 /* we are checking for existence of a file within the relative path.  Chances are good that this is
     636             :                    retrieving something from within the phar archive */
     637             : 
     638          25 :                 if (strncasecmp(fname, "phar://", 7)) {
     639           0 :                         goto skip_phar;
     640             :                 }
     641          25 :                 fname_len = strlen(fname);
     642          25 :                 if (PHAR_G(last_phar) && fname_len - 7 >= PHAR_G(last_phar_name_len) && !memcmp(fname + 7, PHAR_G(last_phar_name), PHAR_G(last_phar_name_len))) {
     643          21 :                         arch = estrndup(PHAR_G(last_phar_name), PHAR_G(last_phar_name_len));
     644          21 :                         arch_len = PHAR_G(last_phar_name_len);
     645          21 :                         entry = estrndup(filename, filename_length);
     646             :                         /* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
     647          21 :                         entry_len = (int) filename_length;
     648          21 :                         phar = PHAR_G(last_phar);
     649          21 :                         goto splitted;
     650             :                 }
     651           4 :                 if (SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
     652             : 
     653           1 :                         efree(entry);
     654           1 :                         entry = estrndup(filename, filename_length);
     655             :                         /* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
     656           1 :                         entry_len = (int) filename_length;
     657           1 :                         if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
     658           0 :                                 efree(arch);
     659           0 :                                 efree(entry);
     660           0 :                                 goto skip_phar;
     661             :                         }
     662             : splitted:
     663          22 :                         entry = phar_fix_filepath(entry, &entry_len, 1);
     664          22 :                         if (entry[0] == '/') {
     665          38 :                                 if (NULL != (data = zend_hash_str_find_ptr(&(phar->manifest), entry + 1, entry_len - 1))) {
     666          17 :                                         efree(entry);
     667          17 :                                         goto stat_entry;
     668             :                                 }
     669           2 :                                 goto notfound;
     670             :                         }
     671           6 :                         if (NULL != (data = zend_hash_str_find_ptr(&(phar->manifest), entry, entry_len))) {
     672           2 :                                 efree(entry);
     673           2 :                                 goto stat_entry;
     674             :                         }
     675           1 :                         if (zend_hash_str_exists(&(phar->virtual_dirs), entry, entry_len)) {
     676           1 :                                 efree(entry);
     677           1 :                                 efree(arch);
     678           1 :                                 if (IS_EXISTS_CHECK(type)) {
     679           1 :                                         RETURN_TRUE;
     680             :                                 }
     681           0 :                                 sb.st_size = 0;
     682           0 :                                 sb.st_mode = 0777;
     683           0 :                                 sb.st_mode |= S_IFDIR; /* regular directory */
     684             : #ifdef NETWARE
     685             :                                 sb.st_mtime.tv_sec = phar->max_timestamp;
     686             :                                 sb.st_atime.tv_sec = phar->max_timestamp;
     687             :                                 sb.st_ctime.tv_sec = phar->max_timestamp;
     688             : #else
     689           0 :                                 sb.st_mtime = phar->max_timestamp;
     690           0 :                                 sb.st_atime = phar->max_timestamp;
     691           0 :                                 sb.st_ctime = phar->max_timestamp;
     692             : #endif
     693           0 :                                 goto statme_baby;
     694             :                         } else {
     695             :                                 char *save;
     696             :                                 int save_len;
     697             : 
     698             : notfound:
     699           2 :                                 efree(entry);
     700           2 :                                 save = PHAR_G(cwd);
     701           2 :                                 save_len = PHAR_G(cwd_len);
     702             :                                 /* this file is not in the current directory, use the original path */
     703           2 :                                 entry = estrndup(filename, filename_length);
     704           2 :                                 entry_len = filename_length;
     705           2 :                                 PHAR_G(cwd) = "/";
     706           2 :                                 PHAR_G(cwd_len) = 0;
     707             :                                 /* clean path without cwd */
     708           2 :                                 entry = phar_fix_filepath(entry, &entry_len, 1);
     709           4 :                                 if (NULL != (data = zend_hash_str_find_ptr(&(phar->manifest), entry + 1, entry_len - 1))) {
     710           0 :                                         PHAR_G(cwd) = save;
     711           0 :                                         PHAR_G(cwd_len) = save_len;
     712           0 :                                         efree(entry);
     713           0 :                                         if (IS_EXISTS_CHECK(type)) {
     714           0 :                                                 efree(arch);
     715           0 :                                                 RETURN_TRUE;
     716             :                                         }
     717           0 :                                         goto stat_entry;
     718             :                                 }
     719           2 :                                 if (zend_hash_str_exists(&(phar->virtual_dirs), entry + 1, entry_len - 1)) {
     720           0 :                                         PHAR_G(cwd) = save;
     721           0 :                                         PHAR_G(cwd_len) = save_len;
     722           0 :                                         efree(entry);
     723           0 :                                         efree(arch);
     724           0 :                                         if (IS_EXISTS_CHECK(type)) {
     725           0 :                                                 RETURN_TRUE;
     726             :                                         }
     727           0 :                                         sb.st_size = 0;
     728           0 :                                         sb.st_mode = 0777;
     729           0 :                                         sb.st_mode |= S_IFDIR; /* regular directory */
     730             : #ifdef NETWARE
     731             :                                         sb.st_mtime.tv_sec = phar->max_timestamp;
     732             :                                         sb.st_atime.tv_sec = phar->max_timestamp;
     733             :                                         sb.st_ctime.tv_sec = phar->max_timestamp;
     734             : #else
     735           0 :                                         sb.st_mtime = phar->max_timestamp;
     736           0 :                                         sb.st_atime = phar->max_timestamp;
     737           0 :                                         sb.st_ctime = phar->max_timestamp;
     738             : #endif
     739           0 :                                         goto statme_baby;
     740             :                                 }
     741           2 :                                 PHAR_G(cwd) = save;
     742           2 :                                 PHAR_G(cwd_len) = save_len;
     743           2 :                                 efree(entry);
     744           2 :                                 efree(arch);
     745             :                                 /* Error Occurred */
     746           2 :                                 if (!IS_EXISTS_CHECK(type)) {
     747           1 :                                         php_error_docref(NULL, E_WARNING, "%sstat failed for %s", IS_LINK_OPERATION(type) ? "L" : "", filename);
     748             :                                 }
     749           2 :                                 RETURN_FALSE;
     750             :                         }
     751             : stat_entry:
     752          19 :                         efree(arch);
     753          19 :                         if (!data->is_dir) {
     754          18 :                                 sb.st_size = data->uncompressed_filesize;
     755          18 :                                 sb.st_mode = data->flags & PHAR_ENT_PERM_MASK;
     756          18 :                                 if (data->link) {
     757           1 :                                         sb.st_mode |= S_IFREG|S_IFLNK; /* regular file */
     758             :                                 } else {
     759          17 :                                         sb.st_mode |= S_IFREG; /* regular file */
     760             :                                 }
     761             :                                 /* timestamp is just the timestamp when this was added to the phar */
     762             : #ifdef NETWARE
     763             :                                 sb.st_mtime.tv_sec = data->timestamp;
     764             :                                 sb.st_atime.tv_sec = data->timestamp;
     765             :                                 sb.st_ctime.tv_sec = data->timestamp;
     766             : #else
     767          18 :                                 sb.st_mtime = data->timestamp;
     768          18 :                                 sb.st_atime = data->timestamp;
     769          18 :                                 sb.st_ctime = data->timestamp;
     770             : #endif
     771             :                         } else {
     772           1 :                                 sb.st_size = 0;
     773           1 :                                 sb.st_mode = data->flags & PHAR_ENT_PERM_MASK;
     774           1 :                                 sb.st_mode |= S_IFDIR; /* regular directory */
     775           1 :                                 if (data->link) {
     776           0 :                                         sb.st_mode |= S_IFLNK;
     777             :                                 }
     778             :                                 /* timestamp is just the timestamp when this was added to the phar */
     779             : #ifdef NETWARE
     780             :                                 sb.st_mtime.tv_sec = data->timestamp;
     781             :                                 sb.st_atime.tv_sec = data->timestamp;
     782             :                                 sb.st_ctime.tv_sec = data->timestamp;
     783             : #else
     784           1 :                                 sb.st_mtime = data->timestamp;
     785           1 :                                 sb.st_atime = data->timestamp;
     786           1 :                                 sb.st_ctime = data->timestamp;
     787             : #endif
     788             :                         }
     789             : 
     790             : statme_baby:
     791          19 :                         if (!phar->is_writeable) {
     792           1 :                                 sb.st_mode = (sb.st_mode & 0555) | (sb.st_mode & ~0777);
     793             :                         }
     794             : 
     795          19 :                         sb.st_nlink = 1;
     796          19 :                         sb.st_rdev = -1;
     797             :                         /* this is only for APC, so use /dev/null device - no chance of conflict there! */
     798          19 :                         sb.st_dev = 0xc;
     799             :                         /* generate unique inode number for alias/filename, so no phars will conflict */
     800          19 :                         if (data) {
     801          19 :                                 sb.st_ino = data->inode;
     802             :                         }
     803             : #ifndef PHP_WIN32
     804          19 :                         sb.st_blksize = -1;
     805          19 :                         sb.st_blocks = -1;
     806             : #endif
     807          19 :                         phar_fancy_stat(&sb, type, return_value);
     808          19 :                         return;
     809             :                 }
     810             :         }
     811             : skip_phar:
     812           4 :         orig_stat_func(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     813           4 :         return;
     814             : }
     815             : /* }}} */
     816             : 
     817             : #define PharFileFunction(fname, funcnum, orig) \
     818             : void fname(INTERNAL_FUNCTION_PARAMETERS) { \
     819             :         if (!PHAR_G(intercepted)) { \
     820             :                 PHAR_G(orig)(INTERNAL_FUNCTION_PARAM_PASSTHRU); \
     821             :         } else { \
     822             :                 char *filename; \
     823             :                 size_t filename_len; \
     824             :                 \
     825             :                 if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) { \
     826             :                         return; \
     827             :                 } \
     828             :                 \
     829             :                 phar_file_stat(filename, (php_stat_len) filename_len, funcnum, PHAR_G(orig), INTERNAL_FUNCTION_PARAM_PASSTHRU); \
     830             :         } \
     831             : }
     832             : /* }}} */
     833             : 
     834             : /* {{{ proto int fileperms(string filename)
     835             :    Get file permissions */
     836        6784 : PharFileFunction(phar_fileperms, FS_PERMS, orig_fileperms)
     837             : /* }}} */
     838             : 
     839             : /* {{{ proto int fileinode(string filename)
     840             :    Get file inode */
     841          71 : PharFileFunction(phar_fileinode, FS_INODE, orig_fileinode)
     842             : /* }}} */
     843             : 
     844             : /* {{{ proto int filesize(string filename)
     845             :    Get file size */
     846       14177 : PharFileFunction(phar_filesize, FS_SIZE, orig_filesize)
     847             : /* }}} */
     848             : 
     849             : /* {{{ proto int fileowner(string filename)
     850             :    Get file owner */
     851          82 : PharFileFunction(phar_fileowner, FS_OWNER, orig_fileowner)
     852             : /* }}} */
     853             : 
     854             : /* {{{ proto int filegroup(string filename)
     855             :    Get file group */
     856          54 : PharFileFunction(phar_filegroup, FS_GROUP, orig_filegroup)
     857             : /* }}} */
     858             : 
     859             : /* {{{ proto int fileatime(string filename)
     860             :    Get last access time of file */
     861          53 : PharFileFunction(phar_fileatime, FS_ATIME, orig_fileatime)
     862             : /* }}} */
     863             : 
     864             : /* {{{ proto int filemtime(string filename)
     865             :    Get last modification time of file */
     866          53 : PharFileFunction(phar_filemtime, FS_MTIME, orig_filemtime)
     867             : /* }}} */
     868             : 
     869             : /* {{{ proto int filectime(string filename)
     870             :    Get inode modification time of file */
     871          48 : PharFileFunction(phar_filectime, FS_CTIME, orig_filectime)
     872             : /* }}} */
     873             : 
     874             : /* {{{ proto string filetype(string filename)
     875             :    Get file type */
     876          42 : PharFileFunction(phar_filetype, FS_TYPE, orig_filetype)
     877             : /* }}} */
     878             : 
     879             : /* {{{ proto bool is_writable(string filename)
     880             :    Returns true if file can be written */
     881         568 : PharFileFunction(phar_is_writable, FS_IS_W, orig_is_writable)
     882             : /* }}} */
     883             : 
     884             : /* {{{ proto bool is_readable(string filename)
     885             :    Returns true if file can be read */
     886         698 : PharFileFunction(phar_is_readable, FS_IS_R, orig_is_readable)
     887             : /* }}} */
     888             : 
     889             : /* {{{ proto bool is_executable(string filename)
     890             :    Returns true if file is executable */
     891         617 : PharFileFunction(phar_is_executable, FS_IS_X, orig_is_executable)
     892             : /* }}} */
     893             : 
     894             : /* {{{ proto bool file_exists(string filename)
     895             :    Returns true if filename exists */
     896        3135 : PharFileFunction(phar_file_exists, FS_EXISTS, orig_file_exists)
     897             : /* }}} */
     898             : 
     899             : /* {{{ proto bool is_dir(string filename)
     900             :    Returns true if file is directory */
     901       28802 : PharFileFunction(phar_is_dir, FS_IS_DIR, orig_is_dir)
     902             : /* }}} */
     903             : 
     904        3211 : PHAR_FUNC(phar_is_file) /* {{{ */
     905             : {
     906             :         char *filename;
     907             :         size_t filename_len;
     908             : 
     909        3211 :         if (!PHAR_G(intercepted)) {
     910        3203 :                 goto skip_phar;
     911             :         }
     912             : 
     913           9 :         if ((PHAR_G(phar_fname_map.u.flags) && !zend_hash_num_elements(&(PHAR_G(phar_fname_map))))
     914           1 :                 && !cached_phars.u.flags) {
     915           1 :                 goto skip_phar;
     916             :         }
     917           7 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
     918           1 :                 goto skip_phar;
     919             :         }
     920           6 :         if (!IS_ABSOLUTE_PATH(filename, filename_len) && !strstr(filename, "://")) {
     921             :                 char *arch, *entry, *fname;
     922             :                 int arch_len, entry_len, fname_len;
     923           5 :                 fname = (char*)zend_get_executed_filename();
     924             : 
     925             :                 /* we are checking for existence of a file within the relative path.  Chances are good that this is
     926             :                    retrieving something from within the phar archive */
     927             : 
     928           5 :                 if (strncasecmp(fname, "phar://", 7)) {
     929           0 :                         goto skip_phar;
     930             :                 }
     931           5 :                 fname_len = strlen(fname);
     932           5 :                 if (SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
     933             :                         phar_archive_data *phar;
     934             : 
     935           5 :                         efree(entry);
     936           5 :                         entry = filename;
     937             :                         /* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
     938           5 :                         entry_len = filename_len;
     939             :                         /* retrieving a file within the current directory, so use this if possible */
     940           5 :                         if (SUCCESS == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
     941             :                                 phar_entry_info *etemp;
     942             : 
     943           5 :                                 entry = phar_fix_filepath(estrndup(entry, entry_len), &entry_len, 1);
     944           5 :                                 if (entry[0] == '/') {
     945           6 :                                         if (NULL != (etemp = zend_hash_str_find_ptr(&(phar->manifest), entry + 1, entry_len - 1))) {
     946             :                                                 /* this file is not in the current directory, use the original path */
     947             : found_it:
     948           4 :                                                 efree(entry);
     949           4 :                                                 efree(arch);
     950           4 :                                                 RETURN_BOOL(!etemp->is_dir);
     951             :                                         }
     952             :                                 } else {
     953           4 :                                         if (NULL != (etemp = zend_hash_str_find_ptr(&(phar->manifest), entry, entry_len))) {
     954           1 :                                                 goto found_it;
     955             :                                         }
     956             :                                 }
     957             :                         }
     958           1 :                         if (entry != filename) {
     959           1 :                                 efree(entry);
     960             :                         }
     961           1 :                         efree(arch);
     962           1 :                         RETURN_FALSE;
     963             :                 }
     964             :         }
     965             : skip_phar:
     966        3206 :         PHAR_G(orig_is_file)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     967        3206 :         return;
     968             : }
     969             : /* }}} */
     970             : 
     971          63 : PHAR_FUNC(phar_is_link) /* {{{ */
     972             : {
     973             :         char *filename;
     974             :         size_t filename_len;
     975             : 
     976          63 :         if (!PHAR_G(intercepted)) {
     977          56 :                 goto skip_phar;
     978             :         }
     979             : 
     980           8 :         if ((PHAR_G(phar_fname_map.u.flags) && !zend_hash_num_elements(&(PHAR_G(phar_fname_map))))
     981           1 :                 && !cached_phars.u.flags) {
     982           1 :                 goto skip_phar;
     983             :         }
     984           6 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
     985           1 :                 goto skip_phar;
     986             :         }
     987           5 :         if (!IS_ABSOLUTE_PATH(filename, filename_len) && !strstr(filename, "://")) {
     988             :                 char *arch, *entry, *fname;
     989             :                 int arch_len, entry_len, fname_len;
     990           5 :                 fname = (char*)zend_get_executed_filename();
     991             : 
     992             :                 /* we are checking for existence of a file within the relative path.  Chances are good that this is
     993             :                    retrieving something from within the phar archive */
     994             : 
     995           5 :                 if (strncasecmp(fname, "phar://", 7)) {
     996           0 :                         goto skip_phar;
     997             :                 }
     998           5 :                 fname_len = strlen(fname);
     999           5 :                 if (SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
    1000             :                         phar_archive_data *phar;
    1001             : 
    1002           5 :                         efree(entry);
    1003           5 :                         entry = filename;
    1004             :                         /* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
    1005           5 :                         entry_len = filename_len;
    1006             :                         /* retrieving a file within the current directory, so use this if possible */
    1007           5 :                         if (SUCCESS == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
    1008             :                                 phar_entry_info *etemp;
    1009             : 
    1010           5 :                                 entry = phar_fix_filepath(estrndup(entry, entry_len), &entry_len, 1);
    1011           5 :                                 if (entry[0] == '/') {
    1012           4 :                                         if (NULL != (etemp = zend_hash_str_find_ptr(&(phar->manifest), entry + 1, entry_len - 1))) {
    1013             :                                                 /* this file is not in the current directory, use the original path */
    1014             : found_it:
    1015           3 :                                                 efree(entry);
    1016           3 :                                                 efree(arch);
    1017           3 :                                                 RETURN_BOOL(etemp->link);
    1018             :                                         }
    1019             :                                 } else {
    1020           6 :                                         if (NULL != (etemp = zend_hash_str_find_ptr(&(phar->manifest), entry, entry_len))) {
    1021           1 :                                                 goto found_it;
    1022             :                                         }
    1023             :                                 }
    1024             :                         }
    1025           2 :                         efree(entry);
    1026           2 :                         efree(arch);
    1027           2 :                         RETURN_FALSE;
    1028             :                 }
    1029             :         }
    1030             : skip_phar:
    1031          58 :         PHAR_G(orig_is_link)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1032          58 :         return;
    1033             : }
    1034             : /* }}} */
    1035             : 
    1036             : /* {{{ proto array lstat(string filename)
    1037             :    Give information about a file or symbolic link */
    1038          46 : PharFileFunction(phar_lstat, FS_LSTAT, orig_lstat)
    1039             : /* }}} */
    1040             : 
    1041             : /* {{{ proto array stat(string filename)
    1042             :    Give information about a file */
    1043         128 : PharFileFunction(phar_stat, FS_STAT, orig_stat)
    1044             : /* }}} */
    1045             : 
    1046             : /* {{{ void phar_intercept_functions(void) */
    1047          15 : void phar_intercept_functions(void)
    1048             : {
    1049          15 :         if (!PHAR_G(request_init)) {
    1050          10 :                 PHAR_G(cwd) = NULL;
    1051          10 :                 PHAR_G(cwd_len) = 0;
    1052             :         }
    1053          15 :         PHAR_G(intercepted) = 1;
    1054          15 : }
    1055             : /* }}} */
    1056             : 
    1057             : /* {{{ void phar_release_functions(void) */
    1058         559 : void phar_release_functions(void)
    1059             : {
    1060         559 :         PHAR_G(intercepted) = 0;
    1061         559 : }
    1062             : /* }}} */
    1063             : 
    1064             : /* {{{ void phar_intercept_functions_init(void) */
    1065             : #define PHAR_INTERCEPT(func) \
    1066             :         PHAR_G(orig_##func) = NULL; \
    1067             :         if (NULL != (orig = zend_hash_str_find_ptr(CG(function_table), #func, sizeof(#func)-1))) { \
    1068             :                 PHAR_G(orig_##func) = orig->internal_function.handler; \
    1069             :                 orig->internal_function.handler = phar_##func; \
    1070             :         }
    1071             : 
    1072       20916 : void phar_intercept_functions_init(void)
    1073             : {
    1074             :         zend_function *orig;
    1075             : 
    1076       41832 :         PHAR_INTERCEPT(fopen);
    1077       41832 :         PHAR_INTERCEPT(file_get_contents);
    1078       41832 :         PHAR_INTERCEPT(is_file);
    1079       41832 :         PHAR_INTERCEPT(is_link);
    1080       41832 :         PHAR_INTERCEPT(is_dir);
    1081       41832 :         PHAR_INTERCEPT(opendir);
    1082       41832 :         PHAR_INTERCEPT(file_exists);
    1083       41832 :         PHAR_INTERCEPT(fileperms);
    1084       41832 :         PHAR_INTERCEPT(fileinode);
    1085       41832 :         PHAR_INTERCEPT(filesize);
    1086       41832 :         PHAR_INTERCEPT(fileowner);
    1087       41832 :         PHAR_INTERCEPT(filegroup);
    1088       41832 :         PHAR_INTERCEPT(fileatime);
    1089       41832 :         PHAR_INTERCEPT(filemtime);
    1090       41832 :         PHAR_INTERCEPT(filectime);
    1091       41832 :         PHAR_INTERCEPT(filetype);
    1092       41832 :         PHAR_INTERCEPT(is_writable);
    1093       41832 :         PHAR_INTERCEPT(is_readable);
    1094       41832 :         PHAR_INTERCEPT(is_executable);
    1095       41832 :         PHAR_INTERCEPT(lstat);
    1096       41832 :         PHAR_INTERCEPT(stat);
    1097       41832 :         PHAR_INTERCEPT(readfile);
    1098       20916 :         PHAR_G(intercepted) = 0;
    1099       20916 : }
    1100             : /* }}} */
    1101             : 
    1102             : /* {{{ void phar_intercept_functions_shutdown(void) */
    1103             : #define PHAR_RELEASE(func) \
    1104             :         if (PHAR_G(orig_##func) && NULL != (orig = zend_hash_str_find_ptr(CG(function_table), #func, sizeof(#func)-1))) { \
    1105             :                 orig->internal_function.handler = PHAR_G(orig_##func); \
    1106             :         } \
    1107             :         PHAR_G(orig_##func) = NULL;
    1108             : 
    1109       20952 : void phar_intercept_functions_shutdown(void)
    1110             : {
    1111             :         zend_function *orig;
    1112             : 
    1113       41904 :         PHAR_RELEASE(fopen);
    1114       41904 :         PHAR_RELEASE(file_get_contents);
    1115       41904 :         PHAR_RELEASE(is_file);
    1116       41904 :         PHAR_RELEASE(is_dir);
    1117       41904 :         PHAR_RELEASE(opendir);
    1118       41904 :         PHAR_RELEASE(file_exists);
    1119       41904 :         PHAR_RELEASE(fileperms);
    1120       41904 :         PHAR_RELEASE(fileinode);
    1121       41904 :         PHAR_RELEASE(filesize);
    1122       41904 :         PHAR_RELEASE(fileowner);
    1123       41904 :         PHAR_RELEASE(filegroup);
    1124       41904 :         PHAR_RELEASE(fileatime);
    1125       41904 :         PHAR_RELEASE(filemtime);
    1126       41904 :         PHAR_RELEASE(filectime);
    1127       41904 :         PHAR_RELEASE(filetype);
    1128       41904 :         PHAR_RELEASE(is_writable);
    1129       41904 :         PHAR_RELEASE(is_readable);
    1130       41904 :         PHAR_RELEASE(is_executable);
    1131       41904 :         PHAR_RELEASE(lstat);
    1132       41904 :         PHAR_RELEASE(stat);
    1133       41904 :         PHAR_RELEASE(readfile);
    1134       20952 :         PHAR_G(intercepted) = 0;
    1135       20952 : }
    1136             : /* }}} */
    1137             : 
    1138             : static struct _phar_orig_functions {
    1139             :         void        (*orig_fopen)(INTERNAL_FUNCTION_PARAMETERS);
    1140             :         void        (*orig_file_get_contents)(INTERNAL_FUNCTION_PARAMETERS);
    1141             :         void        (*orig_is_file)(INTERNAL_FUNCTION_PARAMETERS);
    1142             :         void        (*orig_is_link)(INTERNAL_FUNCTION_PARAMETERS);
    1143             :         void        (*orig_is_dir)(INTERNAL_FUNCTION_PARAMETERS);
    1144             :         void        (*orig_opendir)(INTERNAL_FUNCTION_PARAMETERS);
    1145             :         void        (*orig_file_exists)(INTERNAL_FUNCTION_PARAMETERS);
    1146             :         void        (*orig_fileperms)(INTERNAL_FUNCTION_PARAMETERS);
    1147             :         void        (*orig_fileinode)(INTERNAL_FUNCTION_PARAMETERS);
    1148             :         void        (*orig_filesize)(INTERNAL_FUNCTION_PARAMETERS);
    1149             :         void        (*orig_fileowner)(INTERNAL_FUNCTION_PARAMETERS);
    1150             :         void        (*orig_filegroup)(INTERNAL_FUNCTION_PARAMETERS);
    1151             :         void        (*orig_fileatime)(INTERNAL_FUNCTION_PARAMETERS);
    1152             :         void        (*orig_filemtime)(INTERNAL_FUNCTION_PARAMETERS);
    1153             :         void        (*orig_filectime)(INTERNAL_FUNCTION_PARAMETERS);
    1154             :         void        (*orig_filetype)(INTERNAL_FUNCTION_PARAMETERS);
    1155             :         void        (*orig_is_writable)(INTERNAL_FUNCTION_PARAMETERS);
    1156             :         void        (*orig_is_readable)(INTERNAL_FUNCTION_PARAMETERS);
    1157             :         void        (*orig_is_executable)(INTERNAL_FUNCTION_PARAMETERS);
    1158             :         void        (*orig_lstat)(INTERNAL_FUNCTION_PARAMETERS);
    1159             :         void        (*orig_readfile)(INTERNAL_FUNCTION_PARAMETERS);
    1160             :         void        (*orig_stat)(INTERNAL_FUNCTION_PARAMETERS);
    1161             : } phar_orig_functions = {NULL};
    1162             : 
    1163       20916 : void phar_save_orig_functions(void) /* {{{ */
    1164             : {
    1165       20916 :         phar_orig_functions.orig_fopen             = PHAR_G(orig_fopen);
    1166       20916 :         phar_orig_functions.orig_file_get_contents = PHAR_G(orig_file_get_contents);
    1167       20916 :         phar_orig_functions.orig_is_file           = PHAR_G(orig_is_file);
    1168       20916 :         phar_orig_functions.orig_is_link           = PHAR_G(orig_is_link);
    1169       20916 :         phar_orig_functions.orig_is_dir            = PHAR_G(orig_is_dir);
    1170       20916 :         phar_orig_functions.orig_opendir           = PHAR_G(orig_opendir);
    1171       20916 :         phar_orig_functions.orig_file_exists       = PHAR_G(orig_file_exists);
    1172       20916 :         phar_orig_functions.orig_fileperms         = PHAR_G(orig_fileperms);
    1173       20916 :         phar_orig_functions.orig_fileinode         = PHAR_G(orig_fileinode);
    1174       20916 :         phar_orig_functions.orig_filesize          = PHAR_G(orig_filesize);
    1175       20916 :         phar_orig_functions.orig_fileowner         = PHAR_G(orig_fileowner);
    1176       20916 :         phar_orig_functions.orig_filegroup         = PHAR_G(orig_filegroup);
    1177       20916 :         phar_orig_functions.orig_fileatime         = PHAR_G(orig_fileatime);
    1178       20916 :         phar_orig_functions.orig_filemtime         = PHAR_G(orig_filemtime);
    1179       20916 :         phar_orig_functions.orig_filectime         = PHAR_G(orig_filectime);
    1180       20916 :         phar_orig_functions.orig_filetype          = PHAR_G(orig_filetype);
    1181       20916 :         phar_orig_functions.orig_is_writable       = PHAR_G(orig_is_writable);
    1182       20916 :         phar_orig_functions.orig_is_readable       = PHAR_G(orig_is_readable);
    1183       20916 :         phar_orig_functions.orig_is_executable     = PHAR_G(orig_is_executable);
    1184       20916 :         phar_orig_functions.orig_lstat             = PHAR_G(orig_lstat);
    1185       20916 :         phar_orig_functions.orig_readfile          = PHAR_G(orig_readfile);
    1186       20916 :         phar_orig_functions.orig_stat              = PHAR_G(orig_stat);
    1187       20916 : }
    1188             : /* }}} */
    1189             : 
    1190       20916 : void phar_restore_orig_functions(void) /* {{{ */
    1191             : {
    1192       20916 :         PHAR_G(orig_fopen)             = phar_orig_functions.orig_fopen;
    1193       20916 :         PHAR_G(orig_file_get_contents) = phar_orig_functions.orig_file_get_contents;
    1194       20916 :         PHAR_G(orig_is_file)           = phar_orig_functions.orig_is_file;
    1195       20916 :         PHAR_G(orig_is_link)           = phar_orig_functions.orig_is_link;
    1196       20916 :         PHAR_G(orig_is_dir)            = phar_orig_functions.orig_is_dir;
    1197       20916 :         PHAR_G(orig_opendir)           = phar_orig_functions.orig_opendir;
    1198       20916 :         PHAR_G(orig_file_exists)       = phar_orig_functions.orig_file_exists;
    1199       20916 :         PHAR_G(orig_fileperms)         = phar_orig_functions.orig_fileperms;
    1200       20916 :         PHAR_G(orig_fileinode)         = phar_orig_functions.orig_fileinode;
    1201       20916 :         PHAR_G(orig_filesize)          = phar_orig_functions.orig_filesize;
    1202       20916 :         PHAR_G(orig_fileowner)         = phar_orig_functions.orig_fileowner;
    1203       20916 :         PHAR_G(orig_filegroup)         = phar_orig_functions.orig_filegroup;
    1204       20916 :         PHAR_G(orig_fileatime)         = phar_orig_functions.orig_fileatime;
    1205       20916 :         PHAR_G(orig_filemtime)         = phar_orig_functions.orig_filemtime;
    1206       20916 :         PHAR_G(orig_filectime)         = phar_orig_functions.orig_filectime;
    1207       20916 :         PHAR_G(orig_filetype)          = phar_orig_functions.orig_filetype;
    1208       20916 :         PHAR_G(orig_is_writable)       = phar_orig_functions.orig_is_writable;
    1209       20916 :         PHAR_G(orig_is_readable)       = phar_orig_functions.orig_is_readable;
    1210       20916 :         PHAR_G(orig_is_executable)     = phar_orig_functions.orig_is_executable;
    1211       20916 :         PHAR_G(orig_lstat)             = phar_orig_functions.orig_lstat;
    1212       20916 :         PHAR_G(orig_readfile)          = phar_orig_functions.orig_readfile;
    1213       20916 :         PHAR_G(orig_stat)              = phar_orig_functions.orig_stat;
    1214       20916 : }
    1215             : /* }}} */
    1216             : 
    1217             : /*
    1218             :  * Local variables:
    1219             :  * tab-width: 4
    1220             :  * c-basic-offset: 4
    1221             :  * End:
    1222             :  * vim600: noet sw=4 ts=4 fdm=marker
    1223             :  * vim<600: noet sw=4 ts=4
    1224             :  */
    1225             : 

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:59:01 +0000 (4 days ago)

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