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 - main - php_ini.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 200 357 56.0 %
Date: 2015-08-29 Functions: 17 23 73.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2015 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.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             :    | Author: Zeev Suraski <zeev@zend.com>                                 |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #include "php.h"
      22             : #include "ext/standard/info.h"
      23             : #include "zend_ini.h"
      24             : #include "zend_ini_scanner.h"
      25             : #include "php_ini.h"
      26             : #include "ext/standard/dl.h"
      27             : #include "zend_extensions.h"
      28             : #include "zend_highlight.h"
      29             : #include "SAPI.h"
      30             : #include "php_main.h"
      31             : #include "php_scandir.h"
      32             : #ifdef PHP_WIN32
      33             : #include "win32/php_registry.h"
      34             : #endif
      35             : 
      36             : #if HAVE_SCANDIR && HAVE_ALPHASORT && HAVE_DIRENT_H
      37             : #include <dirent.h>
      38             : #endif
      39             : 
      40             : #ifdef PHP_WIN32
      41             : #define TRANSLATE_SLASHES_LOWER(path) \
      42             :         { \
      43             :                 char *tmp = path; \
      44             :                 while (*tmp) { \
      45             :                         if (*tmp == '\\') *tmp = '/'; \
      46             :                         else *tmp = tolower(*tmp); \
      47             :                                 tmp++; \
      48             :                 } \
      49             :         }
      50             : #else
      51             : #define TRANSLATE_SLASHES_LOWER(path)
      52             : #endif
      53             : 
      54             : 
      55             : typedef struct _php_extension_lists {
      56             :         zend_llist engine;
      57             :         zend_llist functions;
      58             : } php_extension_lists;
      59             : 
      60             : /* True globals */
      61             : static int is_special_section = 0;
      62             : static HashTable *active_ini_hash;
      63             : static HashTable configuration_hash;
      64             : static int has_per_dir_config = 0;
      65             : static int has_per_host_config = 0;
      66             : PHPAPI char *php_ini_opened_path=NULL;
      67             : static php_extension_lists extension_lists;
      68             : PHPAPI char *php_ini_scanned_path=NULL;
      69             : PHPAPI char *php_ini_scanned_files=NULL;
      70             : 
      71             : /* {{{ php_ini_displayer_cb
      72             :  */
      73       67588 : static void php_ini_displayer_cb(zend_ini_entry *ini_entry, int type)
      74             : {
      75       67588 :         if (ini_entry->displayer) {
      76       21568 :                 ini_entry->displayer(ini_entry, type);
      77             :         } else {
      78             :                 char *display_string;
      79             :                 size_t display_string_length;
      80       46020 :                 int esc_html=0;
      81             : 
      82       46139 :                 if (type == ZEND_INI_DISPLAY_ORIG && ini_entry->modified) {
      83         230 :                         if (ini_entry->orig_value && ZSTR_VAL(ini_entry->orig_value)[0]) {
      84         111 :                                 display_string = ZSTR_VAL(ini_entry->orig_value);
      85         111 :                                 display_string_length = ZSTR_LEN(ini_entry->orig_value);
      86         111 :                                 esc_html = !sapi_module.phpinfo_as_text;
      87             :                         } else {
      88           8 :                                 if (!sapi_module.phpinfo_as_text) {
      89           0 :                                         display_string = "<i>no value</i>";
      90           0 :                                         display_string_length = sizeof("<i>no value</i>") - 1;
      91             :                                 } else {
      92           8 :                                         display_string = "no value";
      93           8 :                                         display_string_length = sizeof("no value") - 1;
      94             :                                 }
      95             :                         }
      96       74206 :                 } else if (ini_entry->value && ZSTR_VAL(ini_entry->value)[0]) {
      97       28305 :                         display_string = ZSTR_VAL(ini_entry->value);
      98       28305 :                         display_string_length = ZSTR_LEN(ini_entry->value);
      99       28305 :                         esc_html = !sapi_module.phpinfo_as_text;
     100             :                 } else {
     101       17596 :                         if (!sapi_module.phpinfo_as_text) {
     102         126 :                                 display_string = "<i>no value</i>";
     103         126 :                                 display_string_length = sizeof("<i>no value</i>") - 1;
     104             :                         } else {
     105       17470 :                                 display_string = "no value";
     106       17470 :                                 display_string_length = sizeof("no value") - 1;
     107             :                         }
     108             :                 }
     109             : 
     110       46020 :                 if (esc_html) {
     111         214 :                         php_html_puts(display_string, display_string_length);
     112             :                 } else {
     113       45806 :                         PHPWRITE(display_string, display_string_length);
     114             :                 }
     115             :         }
     116       67588 : }
     117             : /* }}} */
     118             : 
     119             : /* {{{ php_ini_displayer
     120             :  */
     121      958522 : static int php_ini_displayer(zval *el, void *arg)
     122             : {
     123      958522 :         zend_ini_entry *ini_entry = (zend_ini_entry*)Z_PTR_P(el);
     124      958522 :         int module_number = *(int *)arg;
     125             : 
     126      958522 :         if (ini_entry->module_number != module_number) {
     127      924728 :                 return 0;
     128             :         }
     129       33794 :         if (!sapi_module.phpinfo_as_text) {
     130         246 :                 PUTS("<tr>");
     131         246 :                 PUTS("<td class=\"e\">");
     132         246 :                 PHPWRITE(ZSTR_VAL(ini_entry->name), ZSTR_LEN(ini_entry->name));
     133         246 :                 PUTS("</td><td class=\"v\">");
     134         246 :                 php_ini_displayer_cb(ini_entry, ZEND_INI_DISPLAY_ACTIVE);
     135         246 :                 PUTS("</td><td class=\"v\">");
     136         246 :                 php_ini_displayer_cb(ini_entry, ZEND_INI_DISPLAY_ORIG);
     137         246 :                 PUTS("</td></tr>\n");
     138             :         } else {
     139       33548 :                 PHPWRITE(ZSTR_VAL(ini_entry->name), ZSTR_LEN(ini_entry->name));
     140       33548 :                 PUTS(" => ");
     141       33548 :                 php_ini_displayer_cb(ini_entry, ZEND_INI_DISPLAY_ACTIVE);
     142       33548 :                 PUTS(" => ");
     143       33548 :                 php_ini_displayer_cb(ini_entry, ZEND_INI_DISPLAY_ORIG);
     144       33548 :                 PUTS("\n");
     145             :         }
     146       33794 :         return 0;
     147             : }
     148             : /* }}} */
     149             : 
     150             : /* {{{ php_ini_available
     151             :  */
     152      484668 : static int php_ini_available(zval *el, void *arg)
     153             : {
     154      484668 :         zend_ini_entry *ini_entry = (zend_ini_entry *)Z_PTR_P(el);
     155      484668 :         int *module_number_available = (int *)arg;
     156      484668 :         if (ini_entry->module_number == *(int *)module_number_available) {
     157        3836 :                 *(int *)module_number_available = -1;
     158        3836 :                 return ZEND_HASH_APPLY_STOP;
     159             :         } else {
     160      480832 :                 return ZEND_HASH_APPLY_KEEP;
     161             :         }
     162             : }
     163             : /* }}} */
     164             : 
     165             : /* {{{ display_ini_entries
     166             :  */
     167        4120 : PHPAPI void display_ini_entries(zend_module_entry *module)
     168             : {
     169             :         int module_number, module_number_available;
     170             : 
     171        4120 :         if (module) {
     172        4120 :                 module_number = module->module_number;
     173             :         } else {
     174           0 :                 module_number = 0;
     175             :         }
     176        4120 :         module_number_available = module_number;
     177        4120 :         zend_hash_apply_with_argument(EG(ini_directives), php_ini_available, &module_number_available);
     178        4120 :         if (module_number_available == -1) {
     179        3836 :                 php_info_print_table_start();
     180        3836 :                 php_info_print_table_header(3, "Directive", "Local Value", "Master Value");
     181        3836 :                 zend_hash_apply_with_argument(EG(ini_directives), php_ini_displayer, (void *)&module_number);
     182        3836 :                 php_info_print_table_end();
     183             :         }
     184        4120 : }
     185             : /* }}} */
     186             : 
     187             : /* php.ini support */
     188             : #define PHP_EXTENSION_TOKEN             "extension"
     189             : #define ZEND_EXTENSION_TOKEN    "zend_extension"
     190             : 
     191             : /* {{{ config_zval_dtor
     192             :  */
     193      851155 : PHPAPI void config_zval_dtor(zval *zvalue)
     194             : {
     195      851155 :         if (Z_TYPE_P(zvalue) == IS_ARRAY) {
     196           0 :                 zend_hash_destroy(Z_ARRVAL_P(zvalue));
     197           0 :                 free(Z_ARR_P(zvalue));
     198      851155 :         } else if (Z_TYPE_P(zvalue) == IS_STRING) {
     199      851155 :                 zend_string_release(Z_STR_P(zvalue));
     200             :         }
     201      851155 : }
     202             : /* Reset / free active_ini_sectin global */
     203             : #define RESET_ACTIVE_INI_HASH() do { \
     204             :         active_ini_hash = NULL;          \
     205             :         is_special_section = 0;          \
     206             : } while (0)
     207             : /* }}} */
     208             : 
     209             : /* {{{ php_ini_parser_cb
     210             :  */
     211      808023 : static void php_ini_parser_cb(zval *arg1, zval *arg2, zval *arg3, int callback_type, HashTable *target_hash)
     212             : {
     213             :         zval *entry;
     214             :         HashTable *active_hash;
     215             :         char *extension_name;
     216             : 
     217      808023 :         if (active_ini_hash) {
     218           0 :                 active_hash = active_ini_hash;
     219             :         } else {
     220      808023 :                 active_hash = target_hash;
     221             :         }
     222             : 
     223      808023 :         switch (callback_type) {
     224             :                 case ZEND_INI_PARSER_ENTRY: {
     225      808023 :                                 if (!arg2) {
     226             :                                         /* bare string - nothing to do */
     227           0 :                                         break;
     228             :                                 }
     229             : 
     230             :                                 /* PHP and Zend extensions are not added into configuration hash! */
     231      808023 :                                 if (!is_special_section && !strcasecmp(Z_STRVAL_P(arg1), PHP_EXTENSION_TOKEN)) { /* load PHP extension */
     232           0 :                                         extension_name = estrndup(Z_STRVAL_P(arg2), Z_STRLEN_P(arg2));
     233           0 :                                         zend_llist_add_element(&extension_lists.functions, &extension_name);
     234      829145 :                                 } else if (!is_special_section && !strcasecmp(Z_STRVAL_P(arg1), ZEND_EXTENSION_TOKEN)) { /* load Zend extension */
     235       21122 :                                         extension_name = estrndup(Z_STRVAL_P(arg2), Z_STRLEN_P(arg2));
     236       21122 :                                         zend_llist_add_element(&extension_lists.engine, &extension_name);
     237             : 
     238             :                                 /* All other entries are added into either configuration_hash or active ini section array */
     239             :                                 } else {
     240             :                                         /* Store in active hash */
     241      786901 :                                         entry = zend_hash_update(active_hash, Z_STR_P(arg1), arg2);
     242     1573802 :                                         Z_STR_P(entry) = zend_string_dup(Z_STR_P(entry), 1);
     243             :                                 }
     244             :                         }
     245      808023 :                         break;
     246             : 
     247             :                 case ZEND_INI_PARSER_POP_ENTRY: {
     248             :                                 zval option_arr;
     249             :                                 zval *find_arr;
     250             : 
     251           0 :                                 if (!arg2) {
     252             :                                         /* bare string - nothing to do */
     253           0 :                                         break;
     254             :                                 }
     255             : 
     256             : /* fprintf(stdout, "ZEND_INI_PARSER_POP_ENTRY: %s[%s] = %s\n",Z_STRVAL_P(arg1), Z_STRVAL_P(arg3), Z_STRVAL_P(arg2)); */
     257             : 
     258             :                                 /* If option not found in hash or is not an array -> create array, otherwise add to existing array */
     259           0 :                                 if ((find_arr = zend_hash_find(active_hash, Z_STR_P(arg1))) == NULL || Z_TYPE_P(find_arr) != IS_ARRAY) {
     260           0 :                                         ZVAL_NEW_PERSISTENT_ARR(&option_arr);
     261           0 :                                         zend_hash_init(Z_ARRVAL(option_arr), 8, NULL, config_zval_dtor, 1);
     262           0 :                                         find_arr = zend_hash_update(active_hash, Z_STR_P(arg1), &option_arr);
     263             :                                 }
     264             : 
     265             :                                 /* arg3 is possible option offset name */
     266           0 :                                 if (arg3 && Z_STRLEN_P(arg3) > 0) {
     267           0 :                                         entry = zend_symtable_update(Z_ARRVAL_P(find_arr), Z_STR_P(arg3), arg2);
     268             :                                 } else {
     269           0 :                                         entry = zend_hash_next_index_insert(Z_ARRVAL_P(find_arr), arg2);
     270             :                                 }
     271           0 :                                 Z_STR_P(entry) = zend_string_dup(Z_STR_P(entry), 1);
     272             :                         }
     273           0 :                         break;
     274             : 
     275             :                 case ZEND_INI_PARSER_SECTION: { /* Create an array of entries of each section */
     276             : 
     277             : /* fprintf(stdout, "ZEND_INI_PARSER_SECTION: %s\n",Z_STRVAL_P(arg1)); */
     278             : 
     279           0 :                                 char *key = NULL;
     280             :                                 size_t key_len;
     281             : 
     282             :                                 /* PATH sections */
     283           0 :                                 if (!strncasecmp(Z_STRVAL_P(arg1), "PATH", sizeof("PATH") - 1)) {
     284           0 :                                         key = Z_STRVAL_P(arg1);
     285           0 :                                         key = key + sizeof("PATH") - 1;
     286           0 :                                         key_len = Z_STRLEN_P(arg1) - sizeof("PATH") + 1;
     287           0 :                                         is_special_section = 1;
     288           0 :                                         has_per_dir_config = 1;
     289             : 
     290             :                                         /* make the path lowercase on Windows, for case insensitivity. Does nothing for other platforms */
     291             :                                         TRANSLATE_SLASHES_LOWER(key);
     292             : 
     293             :                                 /* HOST sections */
     294           0 :                                 } else if (!strncasecmp(Z_STRVAL_P(arg1), "HOST", sizeof("HOST") - 1)) {
     295           0 :                                         key = Z_STRVAL_P(arg1);
     296           0 :                                         key = key + sizeof("HOST") - 1;
     297           0 :                                         key_len = Z_STRLEN_P(arg1) - sizeof("HOST") + 1;
     298           0 :                                         is_special_section = 1;
     299           0 :                                         has_per_host_config = 1;
     300           0 :                                         zend_str_tolower(key, key_len); /* host names are case-insensitive. */
     301             : 
     302             :                                 } else {
     303           0 :                                         is_special_section = 0;
     304             :                                 }
     305             : 
     306           0 :                                 if (key && key_len > 0) {
     307             :                                         /* Strip any trailing slashes */
     308           0 :                                         while (key_len > 0 && (key[key_len - 1] == '/' || key[key_len - 1] == '\\')) {
     309           0 :                                                 key_len--;
     310           0 :                                                 key[key_len] = 0;
     311             :                                         }
     312             : 
     313             :                                         /* Strip any leading whitespace and '=' */
     314           0 :                                         while (*key && (
     315           0 :                                                 *key == '=' ||
     316           0 :                                                 *key == ' ' ||
     317           0 :                                                 *key == '\t'
     318             :                                         )) {
     319           0 :                                                 key++;
     320           0 :                                                 key_len--;
     321             :                                         }
     322             : 
     323             :                                         /* Search for existing entry and if it does not exist create one */
     324           0 :                                         if ((entry = zend_hash_str_find(target_hash, key, key_len)) == NULL) {
     325             :                                                 zval section_arr;
     326             : 
     327           0 :                                                 ZVAL_NEW_PERSISTENT_ARR(&section_arr);
     328           0 :                                                 zend_hash_init(Z_ARRVAL(section_arr), 8, NULL, (dtor_func_t) config_zval_dtor, 1);
     329           0 :                                                 entry = zend_hash_str_update(target_hash, key, key_len, &section_arr);
     330             :                                         }
     331           0 :                                         active_ini_hash = Z_ARRVAL_P(entry);
     332             :                                 }
     333             :                         }
     334             :                         break;
     335             :         }
     336      808023 : }
     337             : /* }}} */
     338             : 
     339             : /* {{{ php_load_php_extension_cb
     340             :  */
     341           0 : static void php_load_php_extension_cb(void *arg)
     342             : {
     343             : #ifdef HAVE_LIBDL
     344           0 :         php_load_extension(*((char **) arg), MODULE_PERSISTENT, 0);
     345             : #endif
     346           0 : }
     347             : /* }}} */
     348             : 
     349             : /* {{{ php_load_zend_extension_cb
     350             :  */
     351       21122 : static void php_load_zend_extension_cb(void *arg)
     352             : {
     353       21122 :         char *filename = *((char **) arg);
     354       21122 :         const int length = (int)strlen(filename);
     355             : 
     356             : #ifndef PHP_WIN32
     357             :         (void) length;
     358             : #endif
     359             : 
     360       21122 :         if (IS_ABSOLUTE_PATH(filename, length)) {
     361       21122 :                 zend_load_extension(filename);
     362             :         } else {
     363             :             char *libpath;
     364           0 :                 char *extension_dir = INI_STR("extension_dir");
     365           0 :                 int extension_dir_len = (int)strlen(extension_dir);
     366             : 
     367           0 :                 if (IS_SLASH(extension_dir[extension_dir_len-1])) {
     368           0 :                         spprintf(&libpath, 0, "%s%s", extension_dir, filename);
     369             :                 } else {
     370           0 :                         spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, filename);
     371             :                 }
     372           0 :                 zend_load_extension(libpath);
     373           0 :                 efree(libpath);
     374             :         }
     375       21122 : }
     376             : /* }}} */
     377             : 
     378             : /* {{{ php_init_config
     379             :  */
     380       21291 : int php_init_config(void)
     381             : {
     382       21291 :         char *php_ini_file_name = NULL;
     383       21291 :         char *php_ini_search_path = NULL;
     384             :         int php_ini_scanned_path_len;
     385             :         char *open_basedir;
     386       21291 :         int free_ini_search_path = 0;
     387             :         zend_file_handle fh;
     388       21291 :         zend_string *opened_path = NULL;
     389             : 
     390       21291 :         zend_hash_init(&configuration_hash, 8, NULL, config_zval_dtor, 1);
     391             : 
     392       21291 :         if (sapi_module.ini_defaults) {
     393       20885 :                 sapi_module.ini_defaults(&configuration_hash);
     394             :         }
     395             : 
     396       21291 :         zend_llist_init(&extension_lists.engine, sizeof(char *), (llist_dtor_func_t) free_estring, 1);
     397       21291 :         zend_llist_init(&extension_lists.functions, sizeof(char *), (llist_dtor_func_t) free_estring, 1);
     398             : 
     399       21291 :         open_basedir = PG(open_basedir);
     400             : 
     401       21291 :         if (sapi_module.php_ini_path_override) {
     402       21123 :                 php_ini_file_name = sapi_module.php_ini_path_override;
     403       21123 :                 php_ini_search_path = sapi_module.php_ini_path_override;
     404       21123 :                 free_ini_search_path = 0;
     405         168 :         } else if (!sapi_module.php_ini_ignore) {
     406             :                 int search_path_size;
     407             :                 char *default_location;
     408             :                 char *env_location;
     409             :                 static const char paths_separator[] = { ZEND_PATHS_SEPARATOR, 0 };
     410             : #ifdef PHP_WIN32
     411             :                 char *reg_location;
     412             :                 char phprc_path[MAXPATHLEN];
     413             : #endif
     414             : 
     415          41 :                 env_location = getenv("PHPRC");
     416             : 
     417             : #ifdef PHP_WIN32
     418             :                 if (!env_location) {
     419             :                         char dummybuf;
     420             :                         int size;
     421             : 
     422             :                         SetLastError(0);
     423             : 
     424             :                         /*If the given bugger is not large enough to hold the data, the return value is
     425             :                         the buffer size,  in characters, required to hold the string and its terminating
     426             :                         null character. We use this return value to alloc the final buffer. */
     427             :                         size = GetEnvironmentVariableA("PHPRC", &dummybuf, 0);
     428             :                         if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
     429             :                                 /* The environment variable doesn't exist. */
     430             :                                 env_location = "";
     431             :                         } else {
     432             :                                 if (size == 0) {
     433             :                                         env_location = "";
     434             :                                 } else {
     435             :                                         size = GetEnvironmentVariableA("PHPRC", phprc_path, size);
     436             :                                         if (size == 0) {
     437             :                                                 env_location = "";
     438             :                                         } else {
     439             :                                                 env_location = phprc_path;
     440             :                                         }
     441             :                                 }
     442             :                         }
     443             :                 }
     444             : #else
     445          41 :                 if (!env_location) {
     446          41 :                         env_location = "";
     447             :                 }
     448             : #endif
     449             :                 /*
     450             :                  * Prepare search path
     451             :                  */
     452             : 
     453          41 :                 search_path_size = MAXPATHLEN * 4 + (int)strlen(env_location) + 3 + 1;
     454          41 :                 php_ini_search_path = (char *) emalloc(search_path_size);
     455          41 :                 free_ini_search_path = 1;
     456          41 :                 php_ini_search_path[0] = 0;
     457             : 
     458             :                 /* Add environment location */
     459          41 :                 if (env_location[0]) {
     460           0 :                         if (*php_ini_search_path) {
     461           0 :                                 strlcat(php_ini_search_path, paths_separator, search_path_size);
     462             :                         }
     463           0 :                         strlcat(php_ini_search_path, env_location, search_path_size);
     464           0 :                         php_ini_file_name = env_location;
     465             :                 }
     466             : 
     467             : #ifdef PHP_WIN32
     468             :                 /* Add registry location */
     469             :                 reg_location = GetIniPathFromRegistry();
     470             :                 if (reg_location != NULL) {
     471             :                         if (*php_ini_search_path) {
     472             :                                 strlcat(php_ini_search_path, paths_separator, search_path_size);
     473             :                         }
     474             :                         strlcat(php_ini_search_path, reg_location, search_path_size);
     475             :                         efree(reg_location);
     476             :                 }
     477             : #endif
     478             : 
     479             :                 /* Add cwd (not with CLI) */
     480          41 :                 if (!sapi_module.php_ini_ignore_cwd) {
     481           0 :                         if (*php_ini_search_path) {
     482           0 :                                 strlcat(php_ini_search_path, paths_separator, search_path_size);
     483             :                         }
     484           0 :                         strlcat(php_ini_search_path, ".", search_path_size);
     485             :                 }
     486             : 
     487          41 :                 if (PG(php_binary)) {
     488             :                         char *separator_location, *binary_location;
     489             : 
     490          41 :                         binary_location = estrdup(PG(php_binary));
     491          41 :                         separator_location = strrchr(binary_location, DEFAULT_SLASH);
     492             : 
     493          41 :                         if (separator_location && separator_location != binary_location) {
     494          41 :                                 *(separator_location) = 0;
     495             :                         }
     496          41 :                         if (*php_ini_search_path) {
     497           0 :                                 strlcat(php_ini_search_path, paths_separator, search_path_size);
     498             :                         }
     499          41 :                         strlcat(php_ini_search_path, binary_location, search_path_size);
     500          41 :                         efree(binary_location);
     501             :                 }
     502             : 
     503             :                 /* Add default location */
     504             : #ifdef PHP_WIN32
     505             :                 default_location = (char *) emalloc(MAXPATHLEN + 1);
     506             : 
     507             :                 if (0 < GetWindowsDirectory(default_location, MAXPATHLEN)) {
     508             :                         if (*php_ini_search_path) {
     509             :                                 strlcat(php_ini_search_path, paths_separator, search_path_size);
     510             :                         }
     511             :                         strlcat(php_ini_search_path, default_location, search_path_size);
     512             :                 }
     513             : 
     514             :                 /* For people running under terminal services, GetWindowsDirectory will
     515             :                  * return their personal Windows directory, so lets add the system
     516             :                  * windows directory too */
     517             :                 if (0 < GetSystemWindowsDirectory(default_location, MAXPATHLEN)) {
     518             :                         if (*php_ini_search_path) {
     519             :                                 strlcat(php_ini_search_path, paths_separator, search_path_size);
     520             :                         }
     521             :                         strlcat(php_ini_search_path, default_location, search_path_size);
     522             :                 }
     523             :                 efree(default_location);
     524             : 
     525             : #else
     526          41 :                 default_location = PHP_CONFIG_FILE_PATH;
     527          41 :                 if (*php_ini_search_path) {
     528          41 :                         strlcat(php_ini_search_path, paths_separator, search_path_size);
     529             :                 }
     530          41 :                 strlcat(php_ini_search_path, default_location, search_path_size);
     531             : #endif
     532             :         }
     533             : 
     534       21291 :         PG(open_basedir) = NULL;
     535             : 
     536             :         /*
     537             :          * Find and open actual ini file
     538             :          */
     539             : 
     540       21291 :         memset(&fh, 0, sizeof(fh));
     541             : 
     542             :         /* If SAPI does not want to ignore all ini files OR an overriding file/path is given.
     543             :          * This allows disabling scanning for ini files in the PHP_CONFIG_FILE_SCAN_DIR but still
     544             :          * load an optional ini file. */
     545       21291 :         if (!sapi_module.php_ini_ignore || sapi_module.php_ini_path_override) {
     546             : 
     547             :                 /* Check if php_ini_file_name is a file and can be opened */
     548       21164 :                 if (php_ini_file_name && php_ini_file_name[0]) {
     549             :                         zend_stat_t statbuf;
     550             : 
     551       21123 :                         if (!VCWD_STAT(php_ini_file_name, &statbuf)) {
     552       21123 :                                 if (!((statbuf.st_mode & S_IFMT) == S_IFDIR)) {
     553       21123 :                                         fh.handle.fp = VCWD_FOPEN(php_ini_file_name, "r");
     554       21123 :                                         if (fh.handle.fp) {
     555       21123 :                                                 fh.filename = expand_filepath(php_ini_file_name, NULL);
     556       42246 :                                                 opened_path = zend_string_init(fh.filename, strlen(fh.filename), 0);
     557             :                                         }
     558             :                                 }
     559             :                         }
     560             :                 }
     561             : 
     562             :                 /* Otherwise search for php-%sapi-module-name%.ini file in search path */
     563       21164 :                 if (!fh.handle.fp) {
     564          41 :                         const char *fmt = "php-%s.ini";
     565             :                         char *ini_fname;
     566          41 :                         spprintf(&ini_fname, 0, fmt, sapi_module.name);
     567          41 :                         fh.handle.fp = php_fopen_with_path(ini_fname, "r", php_ini_search_path, &opened_path);
     568          41 :                         efree(ini_fname);
     569          41 :                         if (fh.handle.fp) {
     570           0 :                                 fh.filename = ZSTR_VAL(opened_path);
     571             :                         }
     572             :                 }
     573             : 
     574             :                 /* If still no ini file found, search for php.ini file in search path */
     575       21164 :                 if (!fh.handle.fp) {
     576          41 :                         fh.handle.fp = php_fopen_with_path("php.ini", "r", php_ini_search_path, &opened_path);
     577          41 :                         if (fh.handle.fp) {
     578           0 :                                 fh.filename = ZSTR_VAL(opened_path);
     579             :                         }
     580             :                 }
     581             :         }
     582             : 
     583       21291 :         if (free_ini_search_path) {
     584          41 :                 efree(php_ini_search_path);
     585             :         }
     586             : 
     587       21291 :         PG(open_basedir) = open_basedir;
     588             : 
     589       21291 :         if (fh.handle.fp) {
     590       21123 :                 fh.type = ZEND_HANDLE_FP;
     591       21123 :                 RESET_ACTIVE_INI_HASH();
     592             : 
     593       21123 :                 zend_parse_ini_file(&fh, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash);
     594             : 
     595             :                 {
     596             :                         zval tmp;
     597             : 
     598       42246 :                         ZVAL_NEW_STR(&tmp, zend_string_init(fh.filename, strlen(fh.filename), 1));
     599       21123 :                         zend_hash_str_update(&configuration_hash, "cfg_file_path", sizeof("cfg_file_path")-1, &tmp);
     600       21123 :                         if (opened_path) {
     601       21123 :                                 zend_string_release(opened_path);
     602             :                         }
     603       21123 :                         php_ini_opened_path = zend_strndup(Z_STRVAL(tmp), Z_STRLEN(tmp));
     604             :                 }
     605             :         }
     606             : 
     607             :         /* Check for PHP_INI_SCAN_DIR environment variable to override/set config file scan directory */
     608       21291 :         php_ini_scanned_path = getenv("PHP_INI_SCAN_DIR");
     609       21291 :         if (!php_ini_scanned_path) {
     610             :                 /* Or fall back using possible --with-config-file-scan-dir setting (defaults to empty string!) */
     611       21291 :                 php_ini_scanned_path = PHP_CONFIG_FILE_SCAN_DIR;
     612             :         }
     613       21291 :         php_ini_scanned_path_len = (int)strlen(php_ini_scanned_path);
     614             : 
     615             :         /* Scan and parse any .ini files found in scan path if path not empty. */
     616       21291 :         if (!sapi_module.php_ini_ignore && php_ini_scanned_path_len) {
     617             :                 struct dirent **namelist;
     618             :                 int ndir, i;
     619             :                 zend_stat_t sb;
     620             :                 char ini_file[MAXPATHLEN];
     621             :                 char *p;
     622             :                 zend_file_handle fh2;
     623             :                 zend_llist scanned_ini_list;
     624             :                 zend_llist_element *element;
     625           0 :                 int l, total_l = 0;
     626             :                 char *bufpath, *debpath, *endpath;
     627             :                 int lenpath;
     628             : 
     629           0 :                 zend_llist_init(&scanned_ini_list, sizeof(char *), (llist_dtor_func_t) free_estring, 1);
     630           0 :                 memset(&fh2, 0, sizeof(fh2));
     631             : 
     632           0 :                 bufpath = estrdup(php_ini_scanned_path);
     633           0 :                 for (debpath = bufpath ; debpath ; debpath=endpath) {
     634           0 :                         endpath = strchr(debpath, DEFAULT_DIR_SEPARATOR);
     635           0 :                         if (endpath) {
     636           0 :                                 *(endpath++) = 0;
     637             :                         }
     638           0 :                         if (!debpath[0]) {
     639             :                                 /* empty string means default builtin value
     640             :                                    to allow "/foo/phd.d:" or ":/foo/php.d" */
     641           0 :                                 debpath = PHP_CONFIG_FILE_SCAN_DIR;
     642             :                         }
     643           0 :                         lenpath = (int)strlen(debpath);
     644             : 
     645           0 :                         if (lenpath > 0 && (ndir = php_scandir(debpath, &namelist, 0, php_alphasort)) > 0) {
     646             : 
     647           0 :                                 for (i = 0; i < ndir; i++) {
     648             : 
     649             :                                         /* check for any file with .ini extension */
     650           0 :                                         if (!(p = strrchr(namelist[i]->d_name, '.')) || (p && strcmp(p, ".ini"))) {
     651           0 :                                                 free(namelist[i]);
     652           0 :                                                 continue;
     653             :                                         }
     654             :                                         /* Reset active ini section */
     655           0 :                                         RESET_ACTIVE_INI_HASH();
     656             : 
     657           0 :                                         if (IS_SLASH(debpath[lenpath - 1])) {
     658           0 :                                                 snprintf(ini_file, MAXPATHLEN, "%s%s", debpath, namelist[i]->d_name);
     659             :                                         } else {
     660           0 :                                                 snprintf(ini_file, MAXPATHLEN, "%s%c%s", debpath, DEFAULT_SLASH, namelist[i]->d_name);
     661             :                                         }
     662           0 :                                         if (VCWD_STAT(ini_file, &sb) == 0) {
     663           0 :                                                 if (S_ISREG(sb.st_mode)) {
     664           0 :                                                         if ((fh2.handle.fp = VCWD_FOPEN(ini_file, "r"))) {
     665           0 :                                                                 fh2.filename = ini_file;
     666           0 :                                                                 fh2.type = ZEND_HANDLE_FP;
     667             : 
     668           0 :                                                                 if (zend_parse_ini_file(&fh2, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash) == SUCCESS) {
     669             :                                                                         /* Here, add it to the list of ini files read */
     670           0 :                                                                         l = (int)strlen(ini_file);
     671           0 :                                                                         total_l += l + 2;
     672           0 :                                                                         p = estrndup(ini_file, l);
     673           0 :                                                                         zend_llist_add_element(&scanned_ini_list, &p);
     674             :                                                                 }
     675             :                                                         }
     676             :                                                 }
     677             :                                         }
     678           0 :                                         free(namelist[i]);
     679             :                                 }
     680           0 :                                 free(namelist);
     681             :                         }
     682             :                 }
     683           0 :                 efree(bufpath);
     684             : 
     685           0 :                 if (total_l) {
     686           0 :                         int php_ini_scanned_files_len = (php_ini_scanned_files) ? (int)strlen(php_ini_scanned_files) + 1 : 0;
     687           0 :                         php_ini_scanned_files = (char *) realloc(php_ini_scanned_files, php_ini_scanned_files_len + total_l + 1);
     688           0 :                         if (!php_ini_scanned_files_len) {
     689           0 :                                 *php_ini_scanned_files = '\0';
     690             :                         }
     691           0 :                         total_l += php_ini_scanned_files_len;
     692           0 :                         for (element = scanned_ini_list.head; element; element = element->next) {
     693           0 :                                 if (php_ini_scanned_files_len) {
     694           0 :                                         strlcat(php_ini_scanned_files, ",\n", total_l);
     695             :                                 }
     696           0 :                                 strlcat(php_ini_scanned_files, *(char **)element->data, total_l);
     697           0 :                                 strlcat(php_ini_scanned_files, element->next ? ",\n" : "\n", total_l);
     698             :                         }
     699             :                 }
     700           0 :                 zend_llist_destroy(&scanned_ini_list);
     701             :         } else {
     702             :                 /* Make sure an empty php_ini_scanned_path ends up as NULL */
     703       21291 :                 php_ini_scanned_path = NULL;
     704             :         }
     705             : 
     706       21291 :         if (sapi_module.ini_entries) {
     707             :                 /* Reset active ini section */
     708       21268 :                 RESET_ACTIVE_INI_HASH();
     709       21268 :                 zend_parse_ini_string(sapi_module.ini_entries, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash);
     710             :         }
     711             : 
     712       21291 :         return SUCCESS;
     713             : }
     714             : /* }}} */
     715             : 
     716             : /* {{{ php_shutdown_config
     717             :  */
     718       21327 : int php_shutdown_config(void)
     719             : {
     720       21327 :         zend_hash_destroy(&configuration_hash);
     721       21327 :         if (php_ini_opened_path) {
     722       21162 :                 free(php_ini_opened_path);
     723       21162 :                 php_ini_opened_path = NULL;
     724             :         }
     725       21327 :         if (php_ini_scanned_files) {
     726           0 :                 free(php_ini_scanned_files);
     727           0 :                 php_ini_scanned_files = NULL;
     728             :         }
     729       21327 :         return SUCCESS;
     730             : }
     731             : /* }}} */
     732             : 
     733             : /* {{{ php_ini_register_extensions
     734             :  */
     735       21291 : void php_ini_register_extensions(void)
     736             : {
     737       21291 :         zend_llist_apply(&extension_lists.engine, php_load_zend_extension_cb);
     738       21291 :         zend_llist_apply(&extension_lists.functions, php_load_php_extension_cb);
     739             : 
     740       21291 :         zend_llist_destroy(&extension_lists.engine);
     741       21291 :         zend_llist_destroy(&extension_lists.functions);
     742       21291 : }
     743             : /* }}} */
     744             : 
     745             : /* {{{ php_parse_user_ini_file
     746             :  */
     747           0 : PHPAPI int php_parse_user_ini_file(const char *dirname, char *ini_filename, HashTable *target_hash)
     748             : {
     749             :         zend_stat_t sb;
     750             :         char ini_file[MAXPATHLEN];
     751             :         zend_file_handle fh;
     752             : 
     753           0 :         snprintf(ini_file, MAXPATHLEN, "%s%c%s", dirname, DEFAULT_SLASH, ini_filename);
     754             : 
     755           0 :         if (VCWD_STAT(ini_file, &sb) == 0) {
     756           0 :                 if (S_ISREG(sb.st_mode)) {
     757           0 :                         memset(&fh, 0, sizeof(fh));
     758           0 :                         if ((fh.handle.fp = VCWD_FOPEN(ini_file, "r"))) {
     759           0 :                                 fh.filename = ini_file;
     760           0 :                                 fh.type = ZEND_HANDLE_FP;
     761             : 
     762             :                                 /* Reset active ini section */
     763           0 :                                 RESET_ACTIVE_INI_HASH();
     764             : 
     765           0 :                                 if (zend_parse_ini_file(&fh, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, target_hash) == SUCCESS) {
     766             :                                         /* FIXME: Add parsed file to the list of user files read? */
     767           0 :                                         return SUCCESS;
     768             :                                 }
     769           0 :                                 return FAILURE;
     770             :                         }
     771             :                 }
     772             :         }
     773           0 :         return FAILURE;
     774             : }
     775             : /* }}} */
     776             : 
     777             : /* {{{ php_ini_activate_config
     778             :  */
     779           0 : PHPAPI void php_ini_activate_config(HashTable *source_hash, int modify_type, int stage)
     780             : {
     781             :         zend_string *str;
     782             :         zval *data;
     783             : 
     784             :         /* Walk through config hash and alter matching ini entries using the values found in the hash */
     785           0 :         ZEND_HASH_FOREACH_STR_KEY_VAL(source_hash, str, data) {
     786           0 :                 zend_alter_ini_entry_ex(str, Z_STR_P(data), modify_type, stage, 0);
     787             :         } ZEND_HASH_FOREACH_END();
     788           0 : }
     789             : /* }}} */
     790             : 
     791             : /* {{{ php_ini_has_per_dir_config
     792             :  */
     793         403 : PHPAPI int php_ini_has_per_dir_config(void)
     794             : {
     795         403 :         return has_per_dir_config;
     796             : }
     797             : /* }}} */
     798             : 
     799             : /* {{{ php_ini_activate_per_dir_config
     800             :  */
     801         403 : PHPAPI void php_ini_activate_per_dir_config(char *path, size_t path_len)
     802             : {
     803             :         zval *tmp2;
     804             :         char *ptr;
     805             : 
     806             : #if PHP_WIN32
     807             :         char path_bak[MAXPATHLEN];
     808             : #endif
     809             : 
     810             : #if PHP_WIN32
     811             :         /* MAX_PATH is \0-terminated, path_len == MAXPATHLEN would overrun path_bak */
     812             :         if (path_len >= MAXPATHLEN) {
     813             : #else
     814         403 :         if (path_len > MAXPATHLEN) {
     815             : #endif
     816           0 :                 return;
     817             :         }
     818             : 
     819             : #if PHP_WIN32
     820             :         memcpy(path_bak, path, path_len);
     821             :         path_bak[path_len] = 0;
     822             :         TRANSLATE_SLASHES_LOWER(path_bak);
     823             :         path = path_bak;
     824             : #endif
     825             : 
     826             :         /* Walk through each directory in path and apply any found per-dir-system-configuration from configuration_hash */
     827         403 :         if (has_per_dir_config && path && path_len) {
     828           0 :                 ptr = path + 1;
     829           0 :                 while ((ptr = strchr(ptr, '/')) != NULL) {
     830           0 :                         *ptr = 0;
     831             :                         /* Search for source array matching the path from configuration_hash */
     832           0 :                         if ((tmp2 = zend_hash_str_find(&configuration_hash, path, strlen(path))) != NULL) {
     833           0 :                                 php_ini_activate_config(Z_ARRVAL_P(tmp2), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
     834             :                         }
     835           0 :                         *ptr = '/';
     836           0 :                         ptr++;
     837             :                 }
     838             :         }
     839             : }
     840             : /* }}} */
     841             : 
     842             : /* {{{ php_ini_has_per_host_config
     843             :  */
     844         403 : PHPAPI int php_ini_has_per_host_config(void)
     845             : {
     846         403 :         return has_per_host_config;
     847             : }
     848             : /* }}} */
     849             : 
     850             : /* {{{ php_ini_activate_per_host_config
     851             :  */
     852           0 : PHPAPI void php_ini_activate_per_host_config(const char *host, size_t host_len)
     853             : {
     854             :         zval *tmp;
     855             : 
     856           0 :         if (has_per_host_config && host && host_len) {
     857             :                 /* Search for source array matching the host from configuration_hash */
     858           0 :                 if ((tmp = zend_hash_str_find(&configuration_hash, host, host_len)) != NULL) {
     859           0 :                         php_ini_activate_config(Z_ARRVAL_P(tmp), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
     860             :                 }
     861             :         }
     862           0 : }
     863             : /* }}} */
     864             : 
     865             : /* {{{ cfg_get_entry
     866             :  */
     867     5321630 : PHPAPI zval *cfg_get_entry_ex(zend_string *name)
     868             : {
     869     5321630 :         return zend_hash_find(&configuration_hash, name);
     870             : }
     871             : /* }}} */
     872             : 
     873             : /* {{{ cfg_get_entry
     874             :  */
     875          31 : PHPAPI zval *cfg_get_entry(const char *name, size_t name_length)
     876             : {
     877          31 :         return zend_hash_str_find(&configuration_hash, name, name_length);
     878             : }
     879             : /* }}} */
     880             : 
     881             : /* {{{ cfg_get_long
     882             :  */
     883      361925 : PHPAPI int cfg_get_long(const char *varname, zend_long *result)
     884             : {
     885             :         zval *tmp;
     886             : 
     887      361925 :         if ((tmp = zend_hash_str_find(&configuration_hash, varname, strlen(varname))) == NULL) {
     888      340800 :                 *result = 0;
     889      340800 :                 return FAILURE;
     890             :         }
     891       21125 :         *result = zval_get_long(tmp);
     892       21125 :         return SUCCESS;
     893             : }
     894             : /* }}} */
     895             : 
     896             : /* {{{ cfg_get_double
     897             :  */
     898           0 : PHPAPI int cfg_get_double(const char *varname, double *result)
     899             : {
     900             :         zval *tmp;
     901             : 
     902           0 :         if ((tmp = zend_hash_str_find(&configuration_hash, varname, strlen(varname))) == NULL) {
     903           0 :                 *result = (double) 0;
     904           0 :                 return FAILURE;
     905             :         }
     906           0 :         *result = zval_get_double(tmp);
     907           0 :         return SUCCESS;
     908             : }
     909             : /* }}} */
     910             : 
     911             : /* {{{ cfg_get_string
     912             :  */
     913       21296 : PHPAPI int cfg_get_string(const char *varname, char **result)
     914             : {
     915             :         zval *tmp;
     916             : 
     917       21296 :         if ((tmp = zend_hash_str_find(&configuration_hash, varname, strlen(varname))) == NULL) {
     918       21296 :                 *result = NULL;
     919       21296 :                 return FAILURE;
     920             :         }
     921           0 :         *result = Z_STRVAL_P(tmp);
     922           0 :         return SUCCESS;
     923             : }
     924             : /* }}} */
     925             : 
     926           0 : PHPAPI HashTable* php_ini_get_configuration_hash(void) /* {{{ */
     927             : {
     928           0 :         return &configuration_hash;
     929             : } /* }}} */
     930             : 
     931             : /*
     932             :  * Local variables:
     933             :  * tab-width: 4
     934             :  * c-basic-offset: 4
     935             :  * indent-tabs-mode: t
     936             :  * End:
     937             :  * vim600: sw=4 ts=4 fdm=marker
     938             :  * vim<600: sw=4 ts=4
     939             :  */

Generated by: LCOV version 1.10

Generated at Sat, 29 Aug 2015 10:22:29 +0000 (40 hours ago)

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