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