apache_config.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. static zend_bool should_overwrite_per_dir_entry(HashTable *target_ht, php_dir_entry *new_per_dir_entry, zend_hash_key *hash_key, void *pData)
  100. {
  101. php_dir_entry *orig_per_dir_entry;
  102. if (zend_hash_find(target_ht, hash_key->arKey, hash_key->nKeyLength, (void **) &orig_per_dir_entry)==FAILURE) {
  103. return 1; /* does not exist in dest, copy from source */
  104. }
  105. if (new_per_dir_entry->status >= orig_per_dir_entry->status) {
  106. /* use new entry */
  107. phpapdebug((stderr, "ADDING/OVERWRITING %s (%d vs. %d)\n", hash_key->arKey, new_per_dir_entry->status, orig_per_dir_entry->status));
  108. return 1;
  109. } else {
  110. return 0;
  111. }
  112. }
  113. void *merge_php_config(apr_pool_t *p, void *base_conf, void *new_conf)
  114. {
  115. php_conf_rec *d = base_conf, *e = new_conf, *n = NULL;
  116. #if STAS_0
  117. php_dir_entry *pe;
  118. php_dir_entry *data;
  119. char *str;
  120. uint str_len;
  121. ulong num_index;
  122. #endif
  123. n = create_php_config(p, "merge_php_config");
  124. /* copy old config */
  125. zend_hash_copy(&n->config, &d->config, NULL, NULL, sizeof(php_dir_entry));
  126. /* merge new config */
  127. phpapdebug((stderr, "Merge dir (%p)+(%p)=(%p)\n", base_conf, new_conf, n));
  128. zend_hash_merge_ex(&n->config, &e->config, NULL, sizeof(php_dir_entry), (merge_checker_func_t) should_overwrite_per_dir_entry, NULL);
  129. #if STAS_0
  130. for (zend_hash_internal_pointer_reset(&d->config);
  131. zend_hash_get_current_key_ex(&d->config, &str, &str_len,
  132. &num_index, 0, NULL) == HASH_KEY_IS_STRING;
  133. zend_hash_move_forward(&d->config)) {
  134. pe = NULL;
  135. zend_hash_get_current_data(&d->config, (void **) &data);
  136. if (zend_hash_find(&n->config, str, str_len, (void **) &pe) == SUCCESS) {
  137. if (pe->status >= data->status) continue;
  138. }
  139. phpapdebug((stderr, "ADDING/OVERWRITING %s (%d vs. %d)\n", str, data->status, pe?pe->status:-1));
  140. zend_hash_update(&n->config, str, str_len, data, sizeof(*data), NULL);
  141. }
  142. #endif
  143. return n;
  144. }
  145. char *get_php_config(void *conf, char *name, size_t name_len)
  146. {
  147. php_conf_rec *d = conf;
  148. php_dir_entry *pe;
  149. if (zend_hash_find(&d->config, name, name_len, (void **) &pe) == SUCCESS) {
  150. return pe->value;
  151. }
  152. return "";
  153. }
  154. void apply_config(void *dummy)
  155. {
  156. php_conf_rec *d = dummy;
  157. char *str;
  158. uint str_len;
  159. php_dir_entry *data;
  160. for (zend_hash_internal_pointer_reset(&d->config);
  161. zend_hash_get_current_key_ex(&d->config, &str, &str_len, NULL, 0,
  162. NULL) == HASH_KEY_IS_STRING;
  163. zend_hash_move_forward(&d->config)) {
  164. if (zend_hash_get_current_data(&d->config, (void **) &data) == SUCCESS) {
  165. phpapdebug((stderr, "APPLYING (%s)(%s)\n", str, data->value));
  166. 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) {
  167. phpapdebug((stderr, "..FAILED\n"));
  168. }
  169. }
  170. }
  171. }
  172. const command_rec php_dir_cmds[] =
  173. {
  174. AP_INIT_TAKE2("php_value", php_apache_value_handler, NULL, OR_OPTIONS, "PHP Value Modifier"),
  175. AP_INIT_TAKE2("php_flag", php_apache_flag_handler, NULL, OR_OPTIONS, "PHP Flag Modifier"),
  176. AP_INIT_TAKE2("php_admin_value", php_apache_admin_value_handler, NULL, ACCESS_CONF|RSRC_CONF, "PHP Value Modifier (Admin)"),
  177. AP_INIT_TAKE2("php_admin_flag", php_apache_admin_flag_handler, NULL, ACCESS_CONF|RSRC_CONF, "PHP Flag Modifier (Admin)"),
  178. AP_INIT_TAKE1("PHPINIDir", php_apache_phpini_set, NULL, RSRC_CONF, "Directory containing the php.ini file"),
  179. {NULL}
  180. };
  181. static apr_status_t destroy_php_config(void *data)
  182. {
  183. php_conf_rec *d = data;
  184. phpapdebug((stderr, "Destroying config %p\n", data));
  185. zend_hash_destroy(&d->config);
  186. return APR_SUCCESS;
  187. }
  188. void *create_php_config(apr_pool_t *p, char *dummy)
  189. {
  190. php_conf_rec *newx = (php_conf_rec *) apr_pcalloc(p, sizeof(*newx));
  191. phpapdebug((stderr, "Creating new config (%p) for %s\n", newx, dummy));
  192. zend_hash_init(&newx->config, 0, NULL, NULL, 1);
  193. apr_pool_cleanup_register(p, newx, destroy_php_config, apr_pool_cleanup_null);
  194. return (void *) newx;
  195. }
  196. /*
  197. * Local variables:
  198. * tab-width: 4
  199. * c-basic-offset: 4
  200. * End:
  201. * vim600: sw=4 ts=4 fdm=marker
  202. * vim<600: sw=4 ts=4
  203. */