1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
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 18360 : {
79 18360 : if (ini_entry->displayer) {
80 6300 : ini_entry->displayer(ini_entry, type);
81 : } else {
82 : char *display_string;
83 12060 : uint display_string_length, esc_html=0;
84 :
85 12073 : if (type == ZEND_INI_DISPLAY_ORIG && ini_entry->modified) {
86 26 : if (ini_entry->orig_value && ini_entry->orig_value[0]) {
87 13 : display_string = ini_entry->orig_value;
88 13 : display_string_length = ini_entry->orig_value_length;
89 13 : 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 19884 : } else if (ini_entry->value && ini_entry->value[0]) {
100 7837 : display_string = ini_entry->value;
101 7837 : display_string_length = ini_entry->value_length;
102 7837 : esc_html = !sapi_module.phpinfo_as_text;
103 : } else {
104 4210 : 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 4108 : display_string = "no value";
109 4108 : display_string_length = sizeof("no value") - 1;
110 : }
111 : }
112 :
113 12060 : if (esc_html) {
114 200 : php_html_puts(display_string, display_string_length TSRMLS_CC);
115 : } else {
116 11860 : PHPWRITE(display_string, display_string_length);
117 : }
118 : }
119 18360 : }
120 : /* }}} */
121 :
122 : /* {{{ php_ini_displayer
123 : */
124 : static int php_ini_displayer(zend_ini_entry *ini_entry, int module_number TSRMLS_DC)
125 246591 : {
126 246591 : if (ini_entry->module_number != module_number) {
127 237411 : return 0;
128 : }
129 9180 : if (!sapi_module.phpinfo_as_text) {
130 226 : PUTS("<tr>");
131 226 : PUTS("<td class=\"e\">");
132 226 : PHPWRITE(ini_entry->name, ini_entry->name_length - 1);
133 226 : PUTS("</td><td class=\"v\">");
134 226 : php_ini_displayer_cb(ini_entry, ZEND_INI_DISPLAY_ACTIVE TSRMLS_CC);
135 226 : PUTS("</td><td class=\"v\">");
136 226 : php_ini_displayer_cb(ini_entry, ZEND_INI_DISPLAY_ORIG TSRMLS_CC);
137 226 : PUTS("</td></tr>\n");
138 : } else {
139 8954 : PHPWRITE(ini_entry->name, ini_entry->name_length - 1);
140 8954 : PUTS(" => ");
141 8954 : php_ini_displayer_cb(ini_entry, ZEND_INI_DISPLAY_ACTIVE TSRMLS_CC);
142 8954 : PUTS(" => ");
143 8954 : php_ini_displayer_cb(ini_entry, ZEND_INI_DISPLAY_ORIG TSRMLS_CC);
144 8954 : PUTS("\n");
145 : }
146 9180 : 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 114206 : {
154 114206 : if (ini_entry->module_number == *module_number_available) {
155 1095 : *module_number_available = -1;
156 1095 : return ZEND_HASH_APPLY_STOP;
157 : } else {
158 113111 : 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 1095 : {
167 : int module_number, module_number_available;
168 : TSRMLS_FETCH();
169 :
170 1095 : if (module) {
171 1095 : module_number = module->module_number;
172 : } else {
173 0 : module_number = 0;
174 : }
175 1095 : module_number_available = module_number;
176 1095 : zend_hash_apply_with_argument(EG(ini_directives), (apply_func_arg_t) php_ini_available, &module_number_available TSRMLS_CC);
177 1095 : if (module_number_available == -1) {
178 1095 : php_info_print_table_start();
179 1095 : php_info_print_table_header(3, "Directive", "Local Value", "Master Value");
180 1095 : zend_hash_apply_with_argument(EG(ini_directives), (apply_func_arg_t) php_ini_displayer, (void *) (zend_intptr_t) module_number TSRMLS_CC);
181 1095 : php_info_print_table_end();
182 : }
183 1095 : }
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 738719 : {
194 738719 : if (Z_TYPE_P(zvalue) == IS_ARRAY) {
195 0 : zend_hash_destroy(Z_ARRVAL_P(zvalue));
196 0 : free(Z_ARRVAL_P(zvalue));
197 738719 : } else if (Z_TYPE_P(zvalue) == IS_STRING) {
198 738719 : free(Z_STRVAL_P(zvalue));
199 : }
200 738719 : }
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 685242 : {
211 : zval *entry;
212 : HashTable *active_hash;
213 : char *extension_name;
214 :
215 685242 : if (active_ini_hash) {
216 0 : active_hash = active_ini_hash;
217 : } else {
218 685242 : active_hash = target_hash;
219 : }
220 :
221 685242 : switch (callback_type) {
222 : case ZEND_INI_PARSER_ENTRY: {
223 685242 : if (!arg2) {
224 : /* bare string - nothing to do */
225 2 : break;
226 : }
227 :
228 : /* PHP and Zend extensions are not added into configuration hash! */
229 685240 : if (!is_special_section && !strcasecmp(Z_STRVAL_P(arg1), PHP_EXTENSION_TOKEN)) { /* load PHP extension */
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 685240 : } 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 685240 : zend_hash_update(active_hash, Z_STRVAL_P(arg1), Z_STRLEN_P(arg1) + 1, arg2, sizeof(zval), (void **) &entry);
240 685240 : Z_STRVAL_P(entry) = zend_strndup(Z_STRVAL_P(entry), Z_STRLEN_P(entry));
241 : }
242 : }
243 685240 : 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 685242 : }
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 17633 : {
365 17633 : char *php_ini_file_name = NULL;
366 17633 : char *php_ini_search_path = NULL;
367 : int php_ini_scanned_path_len;
368 : int safe_mode_state;
369 : char *open_basedir;
370 17633 : int free_ini_search_path = 0;
371 : zend_file_handle fh;
372 :
373 17633 : if (zend_hash_init(&configuration_hash, 0, NULL, (dtor_func_t) config_zval_dtor, 1) == FAILURE) {
374 0 : return FAILURE;
375 : }
376 :
377 17633 : if (sapi_module.ini_defaults) {
378 17315 : sapi_module.ini_defaults(&configuration_hash);
379 : }
380 :
381 17633 : zend_llist_init(&extension_lists.engine, sizeof(char *), (llist_dtor_func_t) free_estring, 1);
382 17633 : zend_llist_init(&extension_lists.functions, sizeof(char *), (llist_dtor_func_t) free_estring, 1);
383 :
384 17633 : safe_mode_state = PG(safe_mode);
385 17633 : open_basedir = PG(open_basedir);
386 :
387 17633 : if (sapi_module.php_ini_path_override) {
388 0 : php_ini_file_name = sapi_module.php_ini_path_override;
389 0 : php_ini_search_path = sapi_module.php_ini_path_override;
390 0 : free_ini_search_path = 0;
391 17633 : } else if (!sapi_module.php_ini_ignore) {
392 : int search_path_size;
393 : char *default_location;
394 : char *env_location;
395 : char *binary_location;
396 : static const char paths_separator[] = { ZEND_PATHS_SEPARATOR, 0 };
397 : #ifdef PHP_WIN32
398 : char *reg_location;
399 : #endif
400 :
401 17519 : env_location = getenv("PHPRC");
402 17519 : if (!env_location) {
403 17519 : env_location = "";
404 : }
405 :
406 : /*
407 : * Prepare search path
408 : */
409 :
410 17519 : search_path_size = MAXPATHLEN * 4 + strlen(env_location) + 3 + 1;
411 17519 : php_ini_search_path = (char *) emalloc(search_path_size);
412 17519 : free_ini_search_path = 1;
413 17519 : php_ini_search_path[0] = 0;
414 :
415 : /* Add environment location */
416 17519 : if (env_location[0]) {
417 0 : if (*php_ini_search_path) {
418 0 : strlcat(php_ini_search_path, paths_separator, search_path_size);
419 : }
420 0 : strlcat(php_ini_search_path, env_location, search_path_size);
421 0 : php_ini_file_name = env_location;
422 : }
423 :
424 : #ifdef PHP_WIN32
425 : /* Add registry location */
426 : reg_location = GetIniPathFromRegistry();
427 : if (reg_location != NULL) {
428 : if (*php_ini_search_path) {
429 : strlcat(php_ini_search_path, paths_separator, search_path_size);
430 : }
431 : strlcat(php_ini_search_path, reg_location, search_path_size);
432 : efree(reg_location);
433 : }
434 : #endif
435 :
436 : /* Add cwd (not with CLI) */
437 17519 : if (strcmp(sapi_module.name, "cli") != 0) {
438 284 : if (*php_ini_search_path) {
439 0 : strlcat(php_ini_search_path, paths_separator, search_path_size);
440 : }
441 284 : strlcat(php_ini_search_path, ".", search_path_size);
442 : }
443 :
444 : /* Add binary directory */
445 : #ifdef PHP_WIN32
446 : binary_location = (char *) emalloc(MAXPATHLEN);
447 : if (GetModuleFileName(0, binary_location, MAXPATHLEN) == 0) {
448 : efree(binary_location);
449 : binary_location = NULL;
450 : }
451 : #else
452 17519 : if (sapi_module.executable_location) {
453 17519 : binary_location = (char *)emalloc(MAXPATHLEN);
454 17519 : if (!strchr(sapi_module.executable_location, '/')) {
455 : char *envpath, *path;
456 0 : int found = 0;
457 :
458 0 : if ((envpath = getenv("PATH")) != NULL) {
459 : char *search_dir, search_path[MAXPATHLEN];
460 : char *last;
461 :
462 0 : path = estrdup(envpath);
463 0 : search_dir = php_strtok_r(path, ":", &last);
464 :
465 0 : while (search_dir) {
466 0 : snprintf(search_path, MAXPATHLEN, "%s/%s", search_dir, sapi_module.executable_location);
467 0 : if (VCWD_REALPATH(search_path, binary_location) && !VCWD_ACCESS(binary_location, X_OK)) {
468 0 : found = 1;
469 0 : break;
470 : }
471 0 : search_dir = php_strtok_r(NULL, ":", &last);
472 : }
473 0 : efree(path);
474 : }
475 0 : if (!found) {
476 0 : efree(binary_location);
477 0 : binary_location = NULL;
478 : }
479 17519 : } else if (!VCWD_REALPATH(sapi_module.executable_location, binary_location) || VCWD_ACCESS(binary_location, X_OK)) {
480 0 : efree(binary_location);
481 0 : binary_location = NULL;
482 : }
483 : } else {
484 0 : binary_location = NULL;
485 : }
486 : #endif
487 17519 : if (binary_location) {
488 17519 : char *separator_location = strrchr(binary_location, DEFAULT_SLASH);
489 :
490 17519 : if (separator_location && separator_location != binary_location) {
491 17519 : *(separator_location) = 0;
492 : }
493 17519 : if (*php_ini_search_path) {
494 284 : strlcat(php_ini_search_path, paths_separator, search_path_size);
495 : }
496 17519 : strlcat(php_ini_search_path, binary_location, search_path_size);
497 17519 : efree(binary_location);
498 : }
499 :
500 : /* Add default location */
501 : #ifdef PHP_WIN32
502 : default_location = (char *) emalloc(MAXPATHLEN + 1);
503 :
504 : if (0 < GetWindowsDirectory(default_location, MAXPATHLEN)) {
505 : if (*php_ini_search_path) {
506 : strlcat(php_ini_search_path, paths_separator, search_path_size);
507 : }
508 : strlcat(php_ini_search_path, default_location, search_path_size);
509 : }
510 :
511 : /* For people running under terminal services, GetWindowsDirectory will
512 : * return their personal Windows directory, so lets add the system
513 : * windows directory too */
514 : if (0 < GetSystemWindowsDirectory(default_location, MAXPATHLEN)) {
515 : if (*php_ini_search_path) {
516 : strlcat(php_ini_search_path, paths_separator, search_path_size);
517 : }
518 : strlcat(php_ini_search_path, default_location, search_path_size);
519 : }
520 : efree(default_location);
521 :
522 : #else
523 17519 : default_location = PHP_CONFIG_FILE_PATH;
524 17519 : if (*php_ini_search_path) {
525 17519 : strlcat(php_ini_search_path, paths_separator, search_path_size);
526 : }
527 17519 : strlcat(php_ini_search_path, default_location, search_path_size);
528 : #endif
529 : }
530 :
531 17633 : PG(safe_mode) = 0;
532 17633 : PG(open_basedir) = NULL;
533 :
534 : /*
535 : * Find and open actual ini file
536 : */
537 :
538 17633 : memset(&fh, 0, sizeof(fh));
539 :
540 : /* If SAPI does not want to ignore all ini files OR an overriding file/path is given.
541 : * This allows disabling scanning for ini files in the PHP_CONFIG_FILE_SCAN_DIR but still
542 : * load an optional ini file. */
543 17633 : if (!sapi_module.php_ini_ignore || sapi_module.php_ini_path_override) {
544 :
545 : /* Check if php_ini_file_name is a file and can be opened */
546 17519 : if (php_ini_file_name && php_ini_file_name[0]) {
547 : struct stat statbuf;
548 :
549 0 : if (!VCWD_STAT(php_ini_file_name, &statbuf)) {
550 0 : if (!((statbuf.st_mode & S_IFMT) == S_IFDIR)) {
551 0 : fh.handle.fp = VCWD_FOPEN(php_ini_file_name, "r");
552 0 : if (fh.handle.fp) {
553 0 : fh.filename = php_ini_opened_path = expand_filepath(php_ini_file_name, NULL TSRMLS_CC);
554 : }
555 : }
556 : }
557 : }
558 :
559 : /* Otherwise search for php-%sapi-module-name%.ini file in search path */
560 17519 : if (!fh.handle.fp) {
561 17519 : const char *fmt = "php-%s.ini";
562 : char *ini_fname;
563 17519 : spprintf(&ini_fname, 0, fmt, sapi_module.name);
564 17519 : fh.handle.fp = php_fopen_with_path(ini_fname, "r", php_ini_search_path, &php_ini_opened_path TSRMLS_CC);
565 17519 : efree(ini_fname);
566 17519 : if (fh.handle.fp) {
567 0 : fh.filename = php_ini_opened_path;
568 : }
569 : }
570 :
571 : /* If still no ini file found, search for php.ini file in search path */
572 17519 : if (!fh.handle.fp) {
573 17519 : fh.handle.fp = php_fopen_with_path("php.ini", "r", php_ini_search_path, &php_ini_opened_path TSRMLS_CC);
574 17519 : if (fh.handle.fp) {
575 17519 : fh.filename = php_ini_opened_path;
576 : }
577 : }
578 : }
579 :
580 17633 : if (free_ini_search_path) {
581 17519 : efree(php_ini_search_path);
582 : }
583 :
584 17633 : PG(safe_mode) = safe_mode_state;
585 17633 : PG(open_basedir) = open_basedir;
586 :
587 17633 : if (fh.handle.fp) {
588 17519 : fh.type = ZEND_HANDLE_FP;
589 17519 : RESET_ACTIVE_INI_HASH();
590 :
591 17519 : zend_parse_ini_file(&fh, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash TSRMLS_CC);
592 :
593 : {
594 : zval tmp;
595 :
596 17519 : Z_STRLEN(tmp) = strlen(fh.filename);
597 17519 : Z_STRVAL(tmp) = zend_strndup(fh.filename, Z_STRLEN(tmp));
598 17519 : Z_TYPE(tmp) = IS_STRING;
599 17519 : Z_SET_REFCOUNT(tmp, 0);
600 :
601 17519 : zend_hash_update(&configuration_hash, "cfg_file_path", sizeof("cfg_file_path"), (void *) &tmp, sizeof(zval), NULL);
602 17519 : if (php_ini_opened_path) {
603 17519 : efree(php_ini_opened_path);
604 : }
605 17519 : php_ini_opened_path = zend_strndup(Z_STRVAL(tmp), Z_STRLEN(tmp));
606 : }
607 : }
608 :
609 : /* Check for PHP_INI_SCAN_DIR environment variable to override/set config file scan directory */
610 17633 : php_ini_scanned_path = getenv("PHP_INI_SCAN_DIR");
611 17633 : if (!php_ini_scanned_path) {
612 : /* Or fall back using possible --with-config-file-scan-dir setting (defaults to empty string!) */
613 17633 : php_ini_scanned_path = PHP_CONFIG_FILE_SCAN_DIR;
614 : }
615 17633 : php_ini_scanned_path_len = strlen(php_ini_scanned_path);
616 :
617 : /* Scan and parse any .ini files found in scan path if path not empty. */
618 17633 : if (!sapi_module.php_ini_ignore && php_ini_scanned_path_len) {
619 : struct dirent **namelist;
620 : int ndir, i;
621 : struct stat sb;
622 : char ini_file[MAXPATHLEN];
623 : char *p;
624 : zend_file_handle fh;
625 : zend_llist scanned_ini_list;
626 : zend_llist_element *element;
627 0 : int l, total_l = 0;
628 :
629 : /* Reset active ini section */
630 0 : RESET_ACTIVE_INI_HASH();
631 :
632 0 : if ((ndir = php_scandir(php_ini_scanned_path, &namelist, 0, php_alphasort)) > 0) {
633 0 : zend_llist_init(&scanned_ini_list, sizeof(char *), (llist_dtor_func_t) free_estring, 1);
634 0 : memset(&fh, 0, sizeof(fh));
635 :
636 0 : for (i = 0; i < ndir; i++) {
637 :
638 : /* check for any file with .ini extension */
639 0 : if (!(p = strrchr(namelist[i]->d_name, '.')) || (p && strcmp(p, ".ini"))) {
640 0 : free(namelist[i]);
641 0 : continue;
642 : }
643 0 : if (IS_SLASH(php_ini_scanned_path[php_ini_scanned_path_len - 1])) {
644 0 : snprintf(ini_file, MAXPATHLEN, "%s%s", php_ini_scanned_path, namelist[i]->d_name);
645 : } else {
646 0 : snprintf(ini_file, MAXPATHLEN, "%s%c%s", php_ini_scanned_path, DEFAULT_SLASH, namelist[i]->d_name);
647 : }
648 0 : if (VCWD_STAT(ini_file, &sb) == 0) {
649 0 : if (S_ISREG(sb.st_mode)) {
650 0 : if ((fh.handle.fp = VCWD_FOPEN(ini_file, "r"))) {
651 0 : fh.filename = ini_file;
652 0 : fh.type = ZEND_HANDLE_FP;
653 :
654 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) {
655 : /* Here, add it to the list of ini files read */
656 0 : l = strlen(ini_file);
657 0 : total_l += l + 2;
658 0 : p = estrndup(ini_file, l);
659 0 : zend_llist_add_element(&scanned_ini_list, &p);
660 : }
661 : }
662 : }
663 : }
664 0 : free(namelist[i]);
665 : }
666 0 : free(namelist);
667 :
668 0 : if (total_l) {
669 0 : int php_ini_scanned_files_len = (php_ini_scanned_files) ? strlen(php_ini_scanned_files) + 1 : 0;
670 0 : php_ini_scanned_files = (char *) realloc(php_ini_scanned_files, php_ini_scanned_files_len + total_l + 1);
671 0 : if (!php_ini_scanned_files_len) {
672 0 : *php_ini_scanned_files = '\0';
673 : }
674 0 : total_l += php_ini_scanned_files_len;
675 0 : for (element = scanned_ini_list.head; element; element = element->next) {
676 0 : if (php_ini_scanned_files_len) {
677 0 : strlcat(php_ini_scanned_files, ",\n", total_l);
678 : }
679 0 : strlcat(php_ini_scanned_files, *(char **)element->data, total_l);
680 0 : strlcat(php_ini_scanned_files, element->next ? ",\n" : "\n", total_l);
681 : }
682 : }
683 0 : zend_llist_destroy(&scanned_ini_list);
684 : }
685 : } else {
686 : /* Make sure an empty php_ini_scanned_path ends up as NULL */
687 17633 : php_ini_scanned_path = NULL;
688 : }
689 :
690 17633 : if (sapi_module.ini_entries) {
691 : /* Reset active ini section */
692 17614 : RESET_ACTIVE_INI_HASH();
693 17614 : 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);
694 : }
695 :
696 17633 : return SUCCESS;
697 : }
698 : /* }}} */
699 :
700 : /* {{{ php_shutdown_config
701 : */
702 : int php_shutdown_config(void)
703 17665 : {
704 17665 : zend_hash_destroy(&configuration_hash);
705 17665 : if (php_ini_opened_path) {
706 17554 : free(php_ini_opened_path);
707 17554 : php_ini_opened_path = NULL;
708 : }
709 17665 : if (php_ini_scanned_files) {
710 0 : free(php_ini_scanned_files);
711 0 : php_ini_scanned_files = NULL;
712 : }
713 17665 : return SUCCESS;
714 : }
715 : /* }}} */
716 :
717 : /* {{{ php_ini_register_extensions
718 : */
719 : void php_ini_register_extensions(TSRMLS_D)
720 17633 : {
721 17633 : zend_llist_apply(&extension_lists.engine, php_load_zend_extension_cb TSRMLS_CC);
722 17633 : zend_llist_apply(&extension_lists.functions, php_load_php_extension_cb TSRMLS_CC);
723 :
724 17633 : zend_llist_destroy(&extension_lists.engine);
725 17633 : zend_llist_destroy(&extension_lists.functions);
726 17633 : }
727 : /* }}} */
728 :
729 : /* {{{ php_parse_user_ini_file
730 : */
731 : PHPAPI int php_parse_user_ini_file(const char *dirname, char *ini_filename, HashTable *target_hash TSRMLS_DC)
732 0 : {
733 : struct stat sb;
734 : char ini_file[MAXPATHLEN];
735 : zend_file_handle fh;
736 :
737 0 : snprintf(ini_file, MAXPATHLEN, "%s%c%s", dirname, DEFAULT_SLASH, ini_filename);
738 :
739 0 : if (VCWD_STAT(ini_file, &sb) == 0) {
740 0 : if (S_ISREG(sb.st_mode)) {
741 0 : memset(&fh, 0, sizeof(fh));
742 0 : if ((fh.handle.fp = VCWD_FOPEN(ini_file, "r"))) {
743 0 : fh.filename = ini_file;
744 0 : fh.type = ZEND_HANDLE_FP;
745 :
746 : /* Reset active ini section */
747 0 : RESET_ACTIVE_INI_HASH();
748 :
749 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) {
750 : /* FIXME: Add parsed file to the list of user files read? */
751 0 : return SUCCESS;
752 : }
753 0 : return FAILURE;
754 : }
755 : }
756 : }
757 0 : return FAILURE;
758 : }
759 : /* }}} */
760 :
761 : /* {{{ php_ini_activate_config
762 : */
763 : PHPAPI void php_ini_activate_config(HashTable *source_hash, int modify_type, int stage TSRMLS_DC)
764 0 : {
765 : char *str;
766 : zval *data;
767 : uint str_len;
768 : ulong num_index;
769 :
770 : /* Walk through config hash and alter matching ini entries using the values found in the hash */
771 0 : for (zend_hash_internal_pointer_reset(source_hash);
772 0 : zend_hash_get_current_key_ex(source_hash, &str, &str_len, &num_index, 0, NULL) == HASH_KEY_IS_STRING;
773 : zend_hash_move_forward(source_hash)
774 0 : ) {
775 0 : zend_hash_get_current_data(source_hash, (void **) &data);
776 0 : zend_alter_ini_entry_ex(str, str_len, Z_STRVAL_P(data), Z_STRLEN_P(data), modify_type, stage, 0 TSRMLS_CC);
777 : }
778 0 : }
779 : /* }}} */
780 :
781 : /* {{{ php_ini_has_per_dir_config
782 : */
783 : PHPAPI int php_ini_has_per_dir_config(void)
784 315 : {
785 315 : return has_per_dir_config;
786 : }
787 : /* }}} */
788 :
789 : /* {{{ php_ini_activate_per_dir_config
790 : */
791 : PHPAPI void php_ini_activate_per_dir_config(char *path, uint path_len TSRMLS_DC)
792 315 : {
793 : zval *tmp;
794 : char *ptr;
795 :
796 : #if PHP_WIN32
797 : char path_bak[MAXPATHLEN];
798 : memcpy(path_bak, path, path_len);
799 : path_bak[path_len] = 0;
800 : TRANSLATE_SLASHES_LOWER(path_bak);
801 : path = path_bak;
802 : #endif
803 :
804 : /* Walk through each directory in path and apply any found per-dir-system-configuration from configuration_hash */
805 315 : if (has_per_dir_config && path && path_len) {
806 0 : ptr = path + 1;
807 0 : while ((ptr = strchr(ptr, '/')) != NULL) {
808 0 : *ptr = 0;
809 : /* Search for source array matching the path from configuration_hash */
810 0 : if (zend_hash_find(&configuration_hash, path, path_len, (void **) &tmp) == SUCCESS) {
811 0 : php_ini_activate_config(Z_ARRVAL_P(tmp), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE TSRMLS_CC);
812 : }
813 0 : *ptr = '/';
814 0 : ptr++;
815 : }
816 : }
817 315 : }
818 : /* }}} */
819 :
820 : /* {{{ php_ini_has_per_host_config
821 : */
822 : PHPAPI int php_ini_has_per_host_config(void)
823 315 : {
824 315 : return has_per_host_config;
825 : }
826 : /* }}} */
827 :
828 : /* {{{ php_ini_activate_per_host_config
829 : */
830 : PHPAPI void php_ini_activate_per_host_config(const char *host, uint host_len TSRMLS_DC)
831 0 : {
832 : zval *tmp;
833 :
834 0 : if (has_per_host_config && host && host_len) {
835 : /* Search for source array matching the host from configuration_hash */
836 0 : if (zend_hash_find(&configuration_hash, host, host_len, (void **) &tmp) == SUCCESS) {
837 0 : php_ini_activate_config(Z_ARRVAL_P(tmp), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE TSRMLS_CC);
838 : }
839 : }
840 0 : }
841 : /* }}} */
842 :
843 : /* {{{ cfg_get_entry
844 : */
845 : PHPAPI zval *cfg_get_entry(const char *name, uint name_length)
846 3970027 : {
847 : zval *tmp;
848 :
849 3970027 : if (zend_hash_find(&configuration_hash, name, name_length, (void **) &tmp) == SUCCESS) {
850 580753 : return tmp;
851 : } else {
852 3389274 : return NULL;
853 : }
854 : }
855 : /* }}} */
856 :
857 : /* {{{ cfg_get_long
858 : */
859 : PHPAPI int cfg_get_long(const char *varname, long *result)
860 158697 : {
861 : zval *tmp, var;
862 :
863 158697 : if (zend_hash_find(&configuration_hash, varname, strlen(varname) + 1, (void **) &tmp) == FAILURE) {
864 123594 : *result = 0;
865 123594 : return FAILURE;
866 : }
867 35103 : var = *tmp;
868 35103 : zval_copy_ctor(&var);
869 35103 : convert_to_long(&var);
870 35103 : *result = Z_LVAL(var);
871 35103 : return SUCCESS;
872 : }
873 : /* }}} */
874 :
875 : /* {{{ cfg_get_double
876 : */
877 : PHPAPI int cfg_get_double(const char *varname, double *result)
878 0 : {
879 : zval *tmp, var;
880 :
881 0 : if (zend_hash_find(&configuration_hash, varname, strlen(varname) + 1, (void **) &tmp) == FAILURE) {
882 0 : *result = (double) 0;
883 0 : return FAILURE;
884 : }
885 0 : var = *tmp;
886 0 : zval_copy_ctor(&var);
887 0 : convert_to_double(&var);
888 0 : *result = Z_DVAL(var);
889 0 : return SUCCESS;
890 : }
891 : /* }}} */
892 :
893 : /* {{{ cfg_get_string
894 : */
895 : PHPAPI int cfg_get_string(const char *varname, char **result)
896 5 : {
897 : zval *tmp;
898 :
899 5 : if (zend_hash_find(&configuration_hash, varname, strlen(varname)+1, (void **) &tmp) == FAILURE) {
900 5 : *result = NULL;
901 5 : return FAILURE;
902 : }
903 0 : *result = Z_STRVAL_P(tmp);
904 0 : return SUCCESS;
905 : }
906 : /* }}} */
907 :
908 : PHPAPI HashTable* php_ini_get_configuration_hash(void) /* {{{ */
909 0 : {
910 0 : return &configuration_hash;
911 : } /* }}} */
912 :
913 : /*
914 : * Local variables:
915 : * tab-width: 4
916 : * c-basic-offset: 4
917 : * indent-tabs-mode: t
918 : * End:
919 : * vim600: sw=4 ts=4 fdm=marker
920 : * vim<600: sw=4 ts=4
921 : */
|