summaryrefslogtreecommitdiff
path: root/main/php_ini.c
blob: 49464fdc99a9d84d7d5d55230511fd6c08bb29f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdlib.h>

#include "php.h"
#include "php_ini.h"
#include "zend_alloc.h"

static HashTable known_directives;


/*
 * hash_apply functions
 */
static int zend_remove_ini_entries(zend_ini_entry *ini_entry, int *module_number)
{
	if (ini_entry->module_number == *module_number) {
		return 1;
	} else {
		return 0;
	}
}


static int zend_restore_ini_entry(zend_ini_entry *ini_entry)
{
	if (ini_entry->modified) {
		efree(ini_entry->value);
		ini_entry->value = ini_entry->orig_value;
		ini_entry->value_length = ini_entry->orig_value_length;
		ini_entry->modified = 0;
	}
	return 0;
}

/*
 * Startup / shutdown
 */
int zend_ini_mstartup()
{
	if (_php3_hash_init(&known_directives, 100, NULL, NULL, 1)==FAILURE) {
		return FAILURE;
	}
	return SUCCESS;
}


int zend_ini_mshutdown()
{
	_php3_hash_destroy(&known_directives);
	return SUCCESS;
}


int zend_ini_rshutdown()
{
	_php3_hash_apply(&known_directives, (int (*)(void *)) zend_restore_ini_entry);
	return SUCCESS;
}

/*
 * Registration / unregistration
 */

int zend_register_ini_entries(zend_ini_entry *ini_entry, int module_number)
{
	zend_ini_entry *p = ini_entry;
	zend_ini_entry *hashed_ini_entry;
	pval *default_value;

	while (p->name) {
		p->module_number = module_number;
		if (_php3_hash_add(&known_directives, p->name, p->name_length, p, sizeof(zend_ini_entry), (void **) &hashed_ini_entry)==FAILURE) {
			zend_unregister_ini_entries(module_number);
			return FAILURE;
		}
		if ((default_value=cfg_get_entry(p->name, p->name_length))) {
			hashed_ini_entry->value = default_value->value.str.val;
			hashed_ini_entry->value_length = default_value->value.str.len;
		}
		hashed_ini_entry->modified = 0;
		p++;
	}
	return SUCCESS;
}


void zend_unregister_ini_entries(int module_number)
{
	_php3_hash_apply_with_argument(&known_directives, (int (*)(void *, void *)) zend_remove_ini_entries, (void *) &module_number);
}

int zend_alter_ini_entry(char *name, uint name_length, char *new_value, uint new_value_length, int modify_type)
{
	zend_ini_entry *ini_entry;

	if (_php3_hash_find(&known_directives, name, name_length, (void **) &ini_entry)==FAILURE) {
		return FAILURE;
	}

	if (!(ini_entry->modifyable & modify_type)) {
		return FAILURE;
	}

	ini_entry->value = estrndup(new_value, new_value_length);
	ini_entry->value_length = new_value_length;
	ini_entry->modified = 1;

	return SUCCESS;
}


/*
 * Data retrieval
 */

long zend_ini_long(char *name, uint name_length)
{
	zend_ini_entry *ini_entry;

	if (_php3_hash_find(&known_directives, name, name_length, (void **) &ini_entry)==SUCCESS) {
		return (long) atoi(ini_entry->value);
	}

	return 0;
}


double zend_ini_double(char *name, uint name_length)
{
	zend_ini_entry *ini_entry;

	if (_php3_hash_find(&known_directives, name, name_length, (void **) &ini_entry)==SUCCESS) {
		return (double) strtod(ini_entry->value, NULL);
	}

	return 0.0;
}


char *zend_ini_string(char *name, uint name_length)
{
	zend_ini_entry *ini_entry;

	if (_php3_hash_find(&known_directives, name, name_length, (void **) &ini_entry)==SUCCESS) {
		return ini_entry->value;
	}

	return "";
}