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.

516 lines
12 KiB

28 years ago
27 years ago
28 years ago
28 years ago
27 years ago
27 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP version 4.0 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.02 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license/2_02.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. #include <stdlib.h>
  19. #include "php.h"
  20. #include "php_ini.h"
  21. #include "zend_alloc.h"
  22. #include "php_globals.h"
  23. #include "ext/standard/info.h"
  24. static HashTable known_directives;
  25. /*
  26. * hash_apply functions
  27. */
  28. static int php_remove_ini_entries(php_ini_entry *ini_entry, int *module_number)
  29. {
  30. if (ini_entry->module_number == *module_number) {
  31. return 1;
  32. } else {
  33. return 0;
  34. }
  35. }
  36. static int php_restore_ini_entry_cb(php_ini_entry *ini_entry, int stage)
  37. {
  38. if (ini_entry->modified) {
  39. if (ini_entry->on_modify) {
  40. ini_entry->on_modify(ini_entry, ini_entry->orig_value, ini_entry->orig_value_length, ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage);
  41. }
  42. efree(ini_entry->value);
  43. ini_entry->value = ini_entry->orig_value;
  44. ini_entry->value_length = ini_entry->orig_value_length;
  45. ini_entry->modified = 0;
  46. ini_entry->orig_value = NULL;
  47. ini_entry->orig_value_length = 0;
  48. }
  49. return 0;
  50. }
  51. /*
  52. * Startup / shutdown
  53. */
  54. int php_ini_mstartup()
  55. {
  56. if (zend_hash_init_ex(&known_directives, 100, NULL, NULL, 1, 0)==FAILURE) {
  57. return FAILURE;
  58. }
  59. return SUCCESS;
  60. }
  61. int php_ini_mshutdown()
  62. {
  63. zend_hash_destroy(&known_directives);
  64. return SUCCESS;
  65. }
  66. int php_ini_rshutdown()
  67. {
  68. zend_hash_apply_with_argument(&known_directives, (int (*)(void *, void *)) php_restore_ini_entry_cb, (void *) PHP_INI_STAGE_DEACTIVATE);
  69. return SUCCESS;
  70. }
  71. static int ini_key_compare(const void *a, const void *b)
  72. {
  73. Bucket *f;
  74. Bucket *s;
  75. f = *((Bucket **) a);
  76. s = *((Bucket **) b);
  77. if (f->nKeyLength==0 && s->nKeyLength==0) { /* both numeric */
  78. return ZEND_NORMALIZE_BOOL(f->nKeyLength - s->nKeyLength);
  79. } else if (f->nKeyLength==0) { /* f is numeric, s is not */
  80. return -1;
  81. } else if (s->nKeyLength==0) { /* s is numeric, f is not */
  82. return 1;
  83. } else { /* both strings */
  84. return zend_binary_strcasecmp(f->arKey, f->nKeyLength, s->arKey, s->nKeyLength);
  85. }
  86. }
  87. void php_ini_sort_entries()
  88. {
  89. zend_hash_sort(&known_directives, qsort, ini_key_compare, 0);
  90. }
  91. /*
  92. * Registration / unregistration
  93. */
  94. PHPAPI int php_register_ini_entries(php_ini_entry *ini_entry, int module_number)
  95. {
  96. php_ini_entry *p = ini_entry;
  97. php_ini_entry *hashed_ini_entry;
  98. pval *default_value;
  99. while (p->name) {
  100. p->module_number = module_number;
  101. if (zend_hash_add(&known_directives, p->name, p->name_length, p, sizeof(php_ini_entry), (void **) &hashed_ini_entry)==FAILURE) {
  102. php_unregister_ini_entries(module_number);
  103. return FAILURE;
  104. }
  105. if (hashed_ini_entry->on_modify) {
  106. hashed_ini_entry->on_modify(hashed_ini_entry, hashed_ini_entry->value, hashed_ini_entry->value_length, hashed_ini_entry->mh_arg1, hashed_ini_entry->mh_arg2, hashed_ini_entry->mh_arg3, PHP_INI_STAGE_STARTUP);
  107. }
  108. if ((default_value=cfg_get_entry(p->name, p->name_length))) {
  109. if (!hashed_ini_entry->on_modify
  110. || hashed_ini_entry->on_modify(hashed_ini_entry, default_value->value.str.val, default_value->value.str.len, hashed_ini_entry->mh_arg1, hashed_ini_entry->mh_arg2, hashed_ini_entry->mh_arg3, PHP_INI_STAGE_STARTUP)==SUCCESS) {
  111. hashed_ini_entry->value = default_value->value.str.val;
  112. hashed_ini_entry->value_length = default_value->value.str.len;
  113. }
  114. } else {
  115. if (hashed_ini_entry->on_modify) {
  116. hashed_ini_entry->on_modify(hashed_ini_entry, hashed_ini_entry->value, hashed_ini_entry->value_length, hashed_ini_entry->mh_arg1, hashed_ini_entry->mh_arg2, hashed_ini_entry->mh_arg3, PHP_INI_STAGE_STARTUP);
  117. }
  118. }
  119. hashed_ini_entry->modified = 0;
  120. p++;
  121. }
  122. return SUCCESS;
  123. }
  124. PHPAPI void php_unregister_ini_entries(int module_number)
  125. {
  126. zend_hash_apply_with_argument(&known_directives, (int (*)(void *, void *)) php_remove_ini_entries, (void *) &module_number);
  127. }
  128. static int php_ini_refresh_cache(php_ini_entry *p, int stage)
  129. {
  130. if (p->on_modify) {
  131. p->on_modify(p, p->value, p->value_length, p->mh_arg1, p->mh_arg2, p->mh_arg3, stage);
  132. }
  133. return 0;
  134. }
  135. PHPAPI void php_ini_refresh_caches(int stage)
  136. {
  137. zend_hash_apply_with_argument(&known_directives, (int (*)(void *, void *)) php_ini_refresh_cache, (void *) stage);
  138. }
  139. PHPAPI int php_alter_ini_entry(char *name, uint name_length, char *new_value, uint new_value_length, int modify_type, int stage)
  140. {
  141. php_ini_entry *ini_entry;
  142. char *duplicate;
  143. if (zend_hash_find(&known_directives, name, name_length, (void **) &ini_entry)==FAILURE) {
  144. return FAILURE;
  145. }
  146. if (!(ini_entry->modifyable & modify_type)) {
  147. return FAILURE;
  148. }
  149. duplicate = estrndup(new_value, new_value_length);
  150. if (!ini_entry->on_modify
  151. || ini_entry->on_modify(ini_entry, duplicate, new_value_length, ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage)==SUCCESS) {
  152. if (!ini_entry->modified) {
  153. ini_entry->orig_value = ini_entry->value;
  154. ini_entry->orig_value_length = ini_entry->value_length;
  155. } else { /* we already changed the value, free the changed value */
  156. efree(ini_entry->value);
  157. }
  158. ini_entry->value = duplicate;
  159. ini_entry->value_length = new_value_length;
  160. ini_entry->modified = 1;
  161. } else {
  162. efree(duplicate);
  163. }
  164. return SUCCESS;
  165. }
  166. PHPAPI int php_restore_ini_entry(char *name, uint name_length, int stage)
  167. {
  168. php_ini_entry *ini_entry;
  169. if (zend_hash_find(&known_directives, name, name_length, (void **) &ini_entry)==FAILURE) {
  170. return FAILURE;
  171. }
  172. php_restore_ini_entry_cb(ini_entry, stage);
  173. return SUCCESS;
  174. }
  175. PHPAPI int php_ini_register_displayer(char *name, uint name_length, void (*displayer)(php_ini_entry *ini_entry, int type))
  176. {
  177. php_ini_entry *ini_entry;
  178. if (zend_hash_find(&known_directives, name, name_length, (void **) &ini_entry)==FAILURE) {
  179. return FAILURE;
  180. }
  181. ini_entry->displayer = displayer;
  182. return SUCCESS;
  183. }
  184. /*
  185. * Data retrieval
  186. */
  187. PHPAPI long php_ini_long(char *name, uint name_length, int orig)
  188. {
  189. php_ini_entry *ini_entry;
  190. if (zend_hash_find(&known_directives, name, name_length, (void **) &ini_entry)==SUCCESS) {
  191. if (orig && ini_entry->modified) {
  192. return (ini_entry->orig_value ? strtol(ini_entry->orig_value, NULL, 0) : 0);
  193. } else if (ini_entry->value) {
  194. return strtol(ini_entry->value, NULL, 0);
  195. }
  196. }
  197. return 0;
  198. }
  199. PHPAPI double php_ini_double(char *name, uint name_length, int orig)
  200. {
  201. php_ini_entry *ini_entry;
  202. if (zend_hash_find(&known_directives, name, name_length, (void **) &ini_entry)==SUCCESS) {
  203. if (orig && ini_entry->modified) {
  204. return (double) (ini_entry->orig_value ? strtod(ini_entry->orig_value, NULL) : 0.0);
  205. } else if (ini_entry->value) {
  206. return (double) strtod(ini_entry->value, NULL);
  207. }
  208. }
  209. return 0.0;
  210. }
  211. PHPAPI char *php_ini_string(char *name, uint name_length, int orig)
  212. {
  213. php_ini_entry *ini_entry;
  214. if (zend_hash_find(&known_directives, name, name_length, (void **) &ini_entry)==SUCCESS) {
  215. if (orig && ini_entry->modified) {
  216. return ini_entry->orig_value;
  217. } else {
  218. return ini_entry->value;
  219. }
  220. }
  221. return "";
  222. }
  223. php_ini_entry *get_ini_entry(char *name, uint name_length)
  224. {
  225. php_ini_entry *ini_entry;
  226. if (zend_hash_find(&known_directives, name, name_length, (void **) &ini_entry)==SUCCESS) {
  227. return ini_entry;
  228. } else {
  229. return NULL;
  230. }
  231. }
  232. static void php_ini_displayer_cb(php_ini_entry *ini_entry, int type)
  233. {
  234. if (ini_entry->displayer) {
  235. ini_entry->displayer(ini_entry, type);
  236. } else {
  237. char *display_string;
  238. uint display_string_length;
  239. if (type==PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
  240. if (ini_entry->orig_value) {
  241. display_string = ini_entry->orig_value;
  242. display_string_length = ini_entry->orig_value_length;
  243. } else {
  244. display_string = "<i>no value</i>";
  245. display_string_length = sizeof("<i>no value</i>")-1;
  246. }
  247. } else if (ini_entry->value && ini_entry->value[0]) {
  248. display_string = ini_entry->value;
  249. display_string_length = ini_entry->value_length;
  250. } else {
  251. display_string = "<i>no value</i>";
  252. display_string_length = sizeof("<i>no value</i>")-1;
  253. }
  254. PHPWRITE(display_string, display_string_length);
  255. }
  256. }
  257. PHP_INI_DISP(php_ini_boolean_displayer_cb)
  258. {
  259. int value;
  260. if (type==PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
  261. value = (ini_entry->orig_value ? atoi(ini_entry->orig_value) : 0);
  262. } else if (ini_entry->value) {
  263. value = atoi(ini_entry->value);
  264. } else {
  265. value = 0;
  266. }
  267. if (value) {
  268. PUTS("On");
  269. } else {
  270. PUTS("Off");
  271. }
  272. }
  273. PHP_INI_DISP(php_ini_color_displayer_cb)
  274. {
  275. char *value;
  276. if (type==PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
  277. value = ini_entry->orig_value;
  278. } else if (ini_entry->value) {
  279. value = ini_entry->value;
  280. } else {
  281. value = NULL;
  282. }
  283. if (value) {
  284. php_printf("<font color=\"%s\">%s</font>", value, value);
  285. } else {
  286. PUTS("<i>no value</i>;");
  287. }
  288. }
  289. PHP_INI_DISP(display_link_numbers)
  290. {
  291. char *value;
  292. if (type==PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
  293. value = ini_entry->orig_value;
  294. } else if (ini_entry->value) {
  295. value = ini_entry->value;
  296. } else {
  297. value = NULL;
  298. }
  299. if (value) {
  300. if (atoi(value)==-1) {
  301. PUTS("Unlimited");
  302. } else {
  303. php_printf("%s", value);
  304. }
  305. }
  306. }
  307. static int php_ini_displayer(php_ini_entry *ini_entry, int module_number)
  308. {
  309. if (ini_entry->module_number != module_number) {
  310. return 0;
  311. }
  312. PUTS("<TR VALIGN=\"baseline\" BGCOLOR=\"" PHP_CONTENTS_COLOR "\">");
  313. PUTS("<TD BGCOLOR=\"" PHP_ENTRY_NAME_COLOR "\"><B>");
  314. PHPWRITE(ini_entry->name, ini_entry->name_length-1);
  315. PUTS("</B><BR></TD><TD ALIGN=\"center\">");
  316. php_ini_displayer_cb(ini_entry, PHP_INI_DISPLAY_ACTIVE);
  317. PUTS("</TD><TD ALIGN=\"center\">");
  318. php_ini_displayer_cb(ini_entry, PHP_INI_DISPLAY_ORIG);
  319. PUTS("</TD></TR>\n");
  320. return 0;
  321. }
  322. PHPAPI void display_ini_entries(zend_module_entry *module)
  323. {
  324. int module_number;
  325. if (module) {
  326. module_number = module->module_number;
  327. } else {
  328. module_number = 0;
  329. }
  330. php_info_print_table_start();
  331. php_info_print_table_header(3, "Directive", "Local Value", "Master Value");
  332. zend_hash_apply_with_argument(&known_directives, (int (*)(void *, void *)) php_ini_displayer, (void *) (long) module_number);
  333. php_info_print_table_end();
  334. }
  335. /* Standard message handlers */
  336. PHPAPI PHP_INI_MH(OnUpdateBool)
  337. {
  338. zend_bool *p;
  339. #ifndef ZTS
  340. char *base = (char *) mh_arg2;
  341. #else
  342. char *base;
  343. base = (char *) ts_resource(*((int *) mh_arg2));
  344. #endif
  345. p = (zend_bool *) (base+(size_t) mh_arg1);
  346. *p = (zend_bool) atoi(new_value);
  347. return SUCCESS;
  348. }
  349. PHPAPI PHP_INI_MH(OnUpdateInt)
  350. {
  351. long *p;
  352. #ifndef ZTS
  353. char *base = (char *) mh_arg2;
  354. #else
  355. char *base;
  356. base = (char *) ts_resource(*((int *) mh_arg2));
  357. #endif
  358. p = (long *) (base+(size_t) mh_arg1);
  359. *p = atoi(new_value);
  360. return SUCCESS;
  361. }
  362. PHPAPI PHP_INI_MH(OnUpdateReal)
  363. {
  364. double *p;
  365. #ifndef ZTS
  366. char *base = (char *) mh_arg2;
  367. #else
  368. char *base;
  369. base = (char *) ts_resource(*((int *) mh_arg2));
  370. #endif
  371. p = (double *) (base+(size_t) mh_arg1);
  372. *p = strtod(new_value, NULL);
  373. return SUCCESS;
  374. }
  375. PHPAPI PHP_INI_MH(OnUpdateString)
  376. {
  377. char **p;
  378. #ifndef ZTS
  379. char *base = (char *) mh_arg2;
  380. #else
  381. char *base;
  382. base = (char *) ts_resource(*((int *) mh_arg2));
  383. #endif
  384. p = (char **) (base+(size_t) mh_arg1);
  385. *p = new_value;
  386. return SUCCESS;
  387. }
  388. PHPAPI PHP_INI_MH(OnUpdateStringUnempty)
  389. {
  390. char **p;
  391. #ifndef ZTS
  392. char *base = (char *) mh_arg2;
  393. #else
  394. char *base;
  395. base = (char *) ts_resource(*((int *) mh_arg2));
  396. #endif
  397. if (new_value && !new_value[0]) {
  398. return FAILURE;
  399. }
  400. p = (char **) (base+(size_t) mh_arg1);
  401. *p = new_value;
  402. return SUCCESS;
  403. }
  404. /*
  405. * Local variables:
  406. * tab-width: 4
  407. * c-basic-offset: 4
  408. * End:
  409. */