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.

218 lines
6.8 KiB

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