You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.5 KiB

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2006 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: Sascha Schumann <sascha@schumann.cx> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
  20. #include "php.h"
  21. #include "php_ini.h"
  22. #include "php_apache.h"
  23. #include "apr_strings.h"
  24. #include "ap_config.h"
  25. #include "util_filter.h"
  26. #include "httpd.h"
  27. #include "http_config.h"
  28. #include "http_request.h"
  29. #include "http_core.h"
  30. #include "http_protocol.h"
  31. #include "http_log.h"
  32. #include "http_main.h"
  33. #include "util_script.h"
  34. #include "http_core.h"
  35. #ifdef PHP_AP_DEBUG
  36. #define phpapdebug(a) fprintf a
  37. #else
  38. #define phpapdebug(a)
  39. #endif
  40. typedef struct {
  41. HashTable config;
  42. } php_conf_rec;
  43. typedef struct {
  44. char *value;
  45. size_t value_len;
  46. char status;
  47. } php_dir_entry;
  48. static const char *real_value_hnd(cmd_parms *cmd, void *dummy, const char *name, const char *value, int status)
  49. {
  50. php_conf_rec *d = dummy;
  51. php_dir_entry e;
  52. phpapdebug((stderr, "Getting %s=%s for %p (%d)\n", name, value, dummy, zend_hash_num_elements(&d->config)));
  53. if (!strncasecmp(value, "none", sizeof("none"))) {
  54. value = "";
  55. }
  56. e.value = apr_pstrdup(cmd->pool, value);
  57. e.value_len = strlen(value);
  58. e.status = status;
  59. zend_hash_update(&d->config, (char *) name, strlen(name) + 1, &e, sizeof(e), NULL);
  60. return NULL;
  61. }
  62. static const char *php_apache_value_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
  63. {
  64. return real_value_hnd(cmd, dummy, name, value, PHP_INI_PERDIR);
  65. }
  66. static const char *php_apache_admin_value_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
  67. {
  68. return real_value_hnd(cmd, dummy, name, value, PHP_INI_SYSTEM);
  69. }
  70. static const char *real_flag_hnd(cmd_parms *cmd, void *dummy, const char *arg1, const char *arg2, int status)
  71. {
  72. char bool_val[2];
  73. if (!strcasecmp(arg2, "On") || (arg2[0] == '1' && arg2[1] == '\0')) {
  74. bool_val[0] = '1';
  75. } else {
  76. bool_val[0] = '0';
  77. }
  78. bool_val[1] = 0;
  79. return real_value_hnd(cmd, dummy, arg1, bool_val, status);
  80. }
  81. static const char *php_apache_flag_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
  82. {
  83. return real_flag_hnd(cmd, dummy, name, value, PHP_INI_PERDIR);
  84. }
  85. static const char *php_apache_admin_flag_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
  86. {
  87. return real_flag_hnd(cmd, dummy, name, value, PHP_INI_SYSTEM);
  88. }
  89. static const char *php_apache_phpini_set(cmd_parms *cmd, void *mconfig, const char *arg)
  90. {
  91. if (apache2_php_ini_path_override) {
  92. return "Only first PHPINIDir directive honored per configuration tree - subsequent ones ignored";
  93. }
  94. apache2_php_ini_path_override = ap_server_root_relative(cmd->pool, arg);
  95. return NULL;
  96. }
  97. void *merge_php_config(apr_pool_t *p, void *base_conf, void *new_conf)
  98. {
  99. php_conf_rec *d = base_conf, *e = new_conf;
  100. php_dir_entry *pe;
  101. php_dir_entry *data;
  102. char *str;
  103. uint str_len;
  104. ulong num_index;
  105. phpapdebug((stderr, "Merge dir (%p) (%p)\n", base_conf, new_conf));
  106. for (zend_hash_internal_pointer_reset(&d->config);
  107. zend_hash_get_current_key_ex(&d->config, &str, &str_len,
  108. &num_index, 0, NULL) == HASH_KEY_IS_STRING;
  109. zend_hash_move_forward(&d->config)) {
  110. pe = NULL;
  111. zend_hash_get_current_data(&d->config, (void **) &data);
  112. if (zend_hash_find(&e->config, str, str_len, (void **) &pe) == SUCCESS) {
  113. if (pe->status >= data->status) continue;
  114. }
  115. zend_hash_update(&e->config, str, str_len, data, sizeof(*data), NULL);
  116. phpapdebug((stderr, "ADDING/OVERWRITING %s (%d vs. %d)\n", str, data->status, pe?pe->status:-1));
  117. }
  118. return new_conf;
  119. }
  120. char *get_php_config(void *conf, char *name, size_t name_len)
  121. {
  122. php_conf_rec *d = conf;
  123. php_dir_entry *pe;
  124. if (zend_hash_find(&d->config, name, name_len, (void **) &pe) == SUCCESS) {
  125. return pe->value;
  126. }
  127. return "";
  128. }
  129. void apply_config(void *dummy)
  130. {
  131. php_conf_rec *d = dummy;
  132. char *str;
  133. uint str_len;
  134. php_dir_entry *data;
  135. for (zend_hash_internal_pointer_reset(&d->config);
  136. zend_hash_get_current_key_ex(&d->config, &str, &str_len, NULL, 0,
  137. NULL) == HASH_KEY_IS_STRING;
  138. zend_hash_move_forward(&d->config)) {
  139. zend_hash_get_current_data(&d->config, (void **) &data);
  140. phpapdebug((stderr, "APPLYING (%s)(%s)\n", str, data->value));
  141. if (zend_alter_ini_entry(str, str_len, data->value, data->value_len, data->status, PHP_INI_STAGE_ACTIVATE) == FAILURE) {
  142. phpapdebug((stderr, "..FAILED\n"));
  143. }
  144. }
  145. }
  146. const command_rec php_dir_cmds[] =
  147. {
  148. AP_INIT_TAKE2("php_value", php_apache_value_handler, NULL, OR_OPTIONS, "PHP Value Modifier"),
  149. AP_INIT_TAKE2("php_flag", php_apache_flag_handler, NULL, OR_OPTIONS, "PHP Flag Modifier"),
  150. AP_INIT_TAKE2("php_admin_value", php_apache_admin_value_handler, NULL, ACCESS_CONF|RSRC_CONF, "PHP Value Modifier (Admin)"),
  151. AP_INIT_TAKE2("php_admin_flag", php_apache_admin_flag_handler, NULL, ACCESS_CONF|RSRC_CONF, "PHP Flag Modifier (Admin)"),
  152. AP_INIT_TAKE1("PHPINIDir", php_apache_phpini_set, NULL, RSRC_CONF, "Directory containing the php.ini file"),
  153. {NULL}
  154. };
  155. static apr_status_t destroy_php_config(void *data)
  156. {
  157. php_conf_rec *d = data;
  158. phpapdebug((stderr, "Destroying config %p\n", data));
  159. zend_hash_destroy(&d->config);
  160. return APR_SUCCESS;
  161. }
  162. void *create_php_config(apr_pool_t *p, char *dummy)
  163. {
  164. php_conf_rec *newx = (php_conf_rec *) apr_pcalloc(p, sizeof(*newx));
  165. phpapdebug((stderr, "Creating new config (%p) for %s\n", newx, dummy));
  166. zend_hash_init(&newx->config, 0, NULL, NULL, 1);
  167. apr_pool_cleanup_register(p, newx, destroy_php_config, apr_pool_cleanup_null);
  168. return (void *) newx;
  169. }
  170. /*
  171. * Local variables:
  172. * tab-width: 4
  173. * c-basic-offset: 4
  174. * End:
  175. * vim600: sw=4 ts=4 fdm=marker
  176. * vim<600: sw=4 ts=4
  177. */