fixes for windows and 5.3 compatibility
[m6w6/ext-http] / php_http_property_proxy.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #ifndef PHP_HTTP_PPDBG
16 # define PHP_HTTP_PPDBG 0
17 #endif
18
19 PHP_HTTP_API php_http_property_proxy_t *php_http_property_proxy_init(php_http_property_proxy_t *proxy, zval *object, zval *member, zval *parent TSRMLS_DC)
20 {
21 if (!proxy) {
22 proxy = emalloc(sizeof(*proxy));
23 }
24 memset(proxy, 0, sizeof(*proxy));
25
26 MAKE_STD_ZVAL(proxy->myself);
27 ZVAL_OBJVAL(proxy->myself, php_http_property_proxy_object_new_ex(php_http_property_proxy_get_class_entry(), proxy, NULL TSRMLS_CC), 0);
28 Z_ADDREF_P(object);
29 proxy->object = object;
30 proxy->member = php_http_ztyp(IS_STRING, member);
31 if (parent) {
32 Z_ADDREF_P(parent);
33 proxy->parent = parent;
34 }
35
36 #if PHP_HTTP_PPDBG
37 fprintf(stderr, "proxy_init: %s\n", Z_STRVAL_P(proxy->member));
38 #endif
39 return proxy;
40 }
41
42 PHP_HTTP_API void php_http_property_proxy_dtor(php_http_property_proxy_t *proxy)
43 {
44 zval_ptr_dtor(&proxy->object);
45 zval_ptr_dtor(&proxy->member);
46 zval_ptr_dtor(&proxy->myself);
47 if (proxy->parent) {
48 zval_ptr_dtor(&proxy->parent);
49 }
50 }
51
52 PHP_HTTP_API void php_http_property_proxy_free(php_http_property_proxy_t **proxy)
53 {
54 if (*proxy) {
55 php_http_property_proxy_dtor(*proxy);
56 efree(*proxy);
57 *proxy = NULL;
58 }
59 }
60
61
62 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpPropertyProxy, method, 0, req_args)
63 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpPropertyProxy, method, 0)
64 #define PHP_HTTP_PP_ME(method, visibility) PHP_ME(HttpPropertyProxy, method, PHP_HTTP_ARGS(HttpPropertyProxy, method), visibility)
65
66 PHP_HTTP_EMPTY_ARGS(__construct);
67
68 static zend_class_entry *php_http_property_proxy_class_entry;
69
70 zend_class_entry *php_http_property_proxy_get_class_entry(void)
71 {
72 return php_http_property_proxy_class_entry;
73 }
74
75 static zend_function_entry php_http_property_proxy_method_entry[] = {
76 PHP_HTTP_PP_ME(__construct, ZEND_ACC_FINAL|ZEND_ACC_PRIVATE)
77 EMPTY_FUNCTION_ENTRY
78 };
79
80 static zend_object_handlers php_http_property_proxy_object_handlers;
81
82 zend_object_value php_http_property_proxy_object_new(zend_class_entry *ce TSRMLS_DC)
83 {
84 return php_http_property_proxy_object_new_ex(ce, NULL, NULL TSRMLS_CC);
85 }
86
87 zend_object_value php_http_property_proxy_object_new_ex(zend_class_entry *ce, php_http_property_proxy_t *proxy, php_http_property_proxy_object_t **ptr TSRMLS_DC)
88 {
89 zend_object_value ov;
90 php_http_property_proxy_object_t *o;
91
92 o = ecalloc(1, sizeof(*o));
93 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
94 #if PHP_VERSION_ID < 50339
95 zend_hash_copy(((zend_object *) o)->properties, &(ce->default_properties), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval*));
96 #else
97 object_properties_init((zend_object *) o, ce);
98 #endif
99
100 if (ptr) {
101 *ptr = o;
102 }
103 o->proxy = proxy;
104
105 ov.handle = zend_objects_store_put(o, NULL, php_http_property_proxy_object_free, NULL TSRMLS_CC);
106 ov.handlers = &php_http_property_proxy_object_handlers;
107
108 return ov;
109 }
110
111 void php_http_property_proxy_object_free(void *object TSRMLS_DC)
112 {
113 php_http_property_proxy_object_t *o = object;
114
115 if (o->proxy) {
116 php_http_property_proxy_free(&o->proxy);
117 }
118 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
119 efree(o);
120 }
121
122 static zval *php_http_property_proxy_object_get(zval *object TSRMLS_DC)
123 {
124 php_http_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
125
126 #if PHP_HTTP_PPDBG
127 fprintf(stderr, "proxy_get: %s\n", Z_STRVAL_P(obj->proxy->member));
128 #endif
129 if (Z_TYPE_P(obj->proxy->object) == IS_OBJECT) {
130 return zend_read_property(Z_OBJCE_P(obj->proxy->object), obj->proxy->object, Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member), 0 TSRMLS_CC);
131 } else {
132 zval **data = NULL;
133
134 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(obj->proxy->object), Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member)+1, (void *) &data)) {
135 Z_ADDREF_PP(data);
136 } else {
137 zval *unset;
138
139 MAKE_STD_ZVAL(unset);
140 ZVAL_NULL(unset);
141 zend_symtable_update(Z_ARRVAL_P(obj->proxy->object), Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member)+1, (void *) &unset, sizeof(zval *), (void *) &data);
142 Z_ADDREF_PP(data);
143 }
144
145 return *data;
146 }
147
148 return NULL;
149 }
150
151 static void php_http_property_proxy_object_set(zval **object, zval *value TSRMLS_DC)
152 {
153 php_http_property_proxy_object_t *obj = zend_object_store_get_object(*object TSRMLS_CC);
154 zval *target = obj->proxy->parent ? php_http_property_proxy_object_get(obj->proxy->parent TSRMLS_CC) : obj->proxy->object;
155
156 #if PHP_HTTP_PPDBG
157 fprintf(stderr, "proxy_set: %s\n", Z_STRVAL_P(obj->proxy->member));
158 #endif
159 Z_ADDREF_P(value);
160 if (Z_TYPE_P(target) == IS_OBJECT) {
161 zend_update_property(Z_OBJCE_P(target), target, Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member), value TSRMLS_CC);
162 } else {
163 zend_symtable_update(Z_ARRVAL_P(target), Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member)+1, (void *) &value, sizeof(zval *), NULL);
164 }
165 /* propagate */
166 if (obj->proxy->parent) {
167 php_http_property_proxy_object_set(&obj->proxy->parent, target TSRMLS_CC);
168 zval_ptr_dtor(&target);
169 }
170 }
171
172 static STATUS php_http_property_proxy_object_cast(zval *object, zval *return_value, int type TSRMLS_DC)
173 {
174 zval *old_value, *new_value;
175
176 old_value = php_http_property_proxy_object_get(object TSRMLS_CC);
177 new_value = php_http_ztyp(type, old_value);
178
179 if (old_value != new_value) {
180 zval_ptr_dtor(&old_value);
181 }
182
183 RETVAL_ZVAL(new_value, 0, 0);
184
185 return SUCCESS;
186 }
187
188 static zval *php_http_property_proxy_object_read_dimension(zval *object, zval *offset, int type TSRMLS_DC)
189 {
190 php_http_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
191 zval *retval = NULL, *property;
192
193 #if PHP_HTTP_PPDBG
194 zval *ocpy = php_http_ztyp(IS_STRING, offset);
195 fprintf(stderr, "read_dimension: %s.%s (%d)\n", Z_STRVAL_P(obj->proxy->member), Z_STRVAL_P(ocpy), type);
196 zval_ptr_dtor(&ocpy);
197 #endif
198
199 property = php_http_property_proxy_object_get(object TSRMLS_CC);
200 if (type != BP_VAR_R) {
201 Z_ADDREF_P(property);
202 retval = php_http_property_proxy_init(NULL, property, offset, obj->proxy->myself TSRMLS_CC)->myself;
203 } else if (Z_TYPE_P(property) == IS_ARRAY) {
204 zval **data = NULL;
205
206 if (Z_TYPE_P(offset) == IS_LONG) {
207 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(property), Z_LVAL_P(offset), (void *) &data)) {
208 retval = *data;
209 }
210 } else {
211 offset = php_http_ztyp(IS_STRING, offset);
212 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(property), Z_STRVAL_P(offset), Z_STRLEN_P(offset), (void *) &data)) {
213 retval = *data;
214 }
215 zval_ptr_dtor(&offset);
216 }
217
218 if (data) {
219 Z_ADDREF_PP(data);
220 }
221 }
222 zval_ptr_dtor(&property);
223
224 return retval;
225 }
226
227 static void php_http_property_proxy_object_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC)
228 {
229 zval *property = php_http_property_proxy_object_get(object TSRMLS_CC);
230
231 #if PHP_HTTP_PPDBG
232 php_http_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
233 zval *ocpy = offset ? php_http_ztyp(IS_STRING, offset) : offset;
234 fprintf(stderr, "write_dimension: %s.%s (%d)\n", Z_STRVAL_P(obj->proxy->member), ocpy?Z_STRVAL_P(ocpy):"", Z_TYPE_P(property));
235 if(ocpy)zval_ptr_dtor(&ocpy);
236 #endif
237 switch (Z_TYPE_P(property)) {
238 case IS_NULL:
239 array_init(property);
240 /* no break */
241 case IS_ARRAY:
242 Z_ADDREF_P(value);
243 if (!offset) {
244 add_next_index_zval(property, value);
245 } else if (Z_TYPE_P(offset) == IS_LONG) {
246 add_index_zval(property, Z_LVAL_P(offset), value);
247 } else {
248 offset = php_http_ztyp(IS_STRING, offset);
249 add_assoc_zval_ex(property, Z_STRVAL_P(offset), Z_STRLEN_P(offset) + 1, value);
250 zval_ptr_dtor(&offset);
251 }
252 php_http_property_proxy_object_set(&object, property TSRMLS_CC);
253 zval_ptr_dtor(&property);
254 break;
255
256 default:
257 zval_ptr_dtor(&property);
258 break;
259 }
260 }
261
262 PHP_METHOD(HttpPropertyProxy, __construct)
263 {
264 }
265
266 PHP_MINIT_FUNCTION(http_property_proxy)
267 {
268 PHP_HTTP_REGISTER_CLASS(http\\Object, PropertyProxy, http_property_proxy, NULL, ZEND_ACC_FINAL);
269 php_http_property_proxy_class_entry->create_object = php_http_property_proxy_object_new;
270 memcpy(&php_http_property_proxy_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
271 php_http_property_proxy_object_handlers.set = php_http_property_proxy_object_set;
272 php_http_property_proxy_object_handlers.get = php_http_property_proxy_object_get;
273 php_http_property_proxy_object_handlers.cast_object = php_http_property_proxy_object_cast;
274 php_http_property_proxy_object_handlers.read_dimension = php_http_property_proxy_object_read_dimension;
275 php_http_property_proxy_object_handlers.write_dimension = php_http_property_proxy_object_write_dimension;
276
277 return SUCCESS;
278 }
279
280
281 /*
282 * Local variables:
283 * tab-width: 4
284 * c-basic-offset: 4
285 * End:
286 * vim600: noet sw=4 ts=4 fdm=marker
287 * vim<600: noet sw=4 ts=4
288 */
289