1 : /*
2 : +----------------------------------------------------------------------+
3 : | Zend Engine |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1998-2009 Zend Technologies Ltd. (http://www.zend.com) |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 2.00 of the Zend 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.zend.com/license/2_00.txt. |
11 : | If you did not receive a copy of the Zend license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@zend.com so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Authors: Andi Gutmans <andi@zend.com> |
16 : | Zeev Suraski <zeev@zend.com> |
17 : +----------------------------------------------------------------------+
18 : */
19 :
20 : /* $Id: zend_object_handlers.c 290642 2009-11-12 23:18:04Z felipe $ */
21 :
22 : #include "zend.h"
23 : #include "zend_globals.h"
24 : #include "zend_variables.h"
25 : #include "zend_API.h"
26 : #include "zend_objects.h"
27 : #include "zend_objects_API.h"
28 : #include "zend_object_handlers.h"
29 : #include "zend_interfaces.h"
30 : #include "zend_closures.h"
31 :
32 : #define DEBUG_OBJECT_HANDLERS 0
33 :
34 : #define Z_OBJ_P(zval_p) zend_objects_get_address(zval_p TSRMLS_CC)
35 :
36 : /*
37 : __X accessors explanation:
38 :
39 : if we have __get and property that is not part of the properties array is
40 : requested, we call __get handler. If it fails, we return uninitialized.
41 :
42 : if we have __set and property that is not part of the properties array is
43 : set, we call __set handler. If it fails, we do not change the array.
44 :
45 : for both handlers above, when we are inside __get/__set, no further calls for
46 : __get/__set for this property of this object will be made, to prevent endless
47 : recursion and enable accessors to change properties array.
48 :
49 : if we have __call and method which is not part of the class function table is
50 : called, we cal __call handler.
51 : */
52 :
53 : ZEND_API HashTable *zend_std_get_properties(zval *object TSRMLS_DC) /* {{{ */
54 192503 : {
55 : zend_object *zobj;
56 192503 : zobj = Z_OBJ_P(object);
57 192503 : return zobj->properties;
58 : }
59 : /* }}} */
60 :
61 : ZEND_API HashTable *zend_std_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
62 0 : {
63 0 : *is_temp = 0;
64 0 : return zend_std_get_properties(object TSRMLS_CC);
65 : }
66 : /* }}} */
67 :
68 : static zval *zend_std_call_getter(zval *object, zval *member TSRMLS_DC) /* {{{ */
69 118 : {
70 118 : zval *retval = NULL;
71 118 : zend_class_entry *ce = Z_OBJCE_P(object);
72 :
73 : /* __get handler is called with one argument:
74 : property name
75 :
76 : it should return whether the call was successfull or not
77 : */
78 :
79 118 : SEPARATE_ARG_IF_REF(member);
80 :
81 118 : zend_call_method_with_1_params(&object, ce, &ce->__get, ZEND_GET_FUNC_NAME, &retval, member);
82 :
83 118 : zval_ptr_dtor(&member);
84 :
85 118 : if (retval) {
86 116 : Z_DELREF_P(retval);
87 : }
88 :
89 118 : return retval;
90 : }
91 : /* }}} */
92 :
93 : static int zend_std_call_setter(zval *object, zval *member, zval *value TSRMLS_DC) /* {{{ */
94 82 : {
95 82 : zval *retval = NULL;
96 : int result;
97 82 : zend_class_entry *ce = Z_OBJCE_P(object);
98 :
99 82 : SEPARATE_ARG_IF_REF(member);
100 82 : Z_ADDREF_P(value);
101 :
102 : /* __set handler is called with two arguments:
103 : property name
104 : value to be set
105 :
106 : it should return whether the call was successfull or not
107 : */
108 82 : zend_call_method_with_2_params(&object, ce, &ce->__set, ZEND_SET_FUNC_NAME, &retval, member, value);
109 :
110 82 : zval_ptr_dtor(&member);
111 82 : zval_ptr_dtor(&value);
112 :
113 82 : if (retval) {
114 81 : result = i_zend_is_true(retval) ? SUCCESS : FAILURE;
115 81 : zval_ptr_dtor(&retval);
116 81 : return result;
117 : } else {
118 1 : return FAILURE;
119 : }
120 : }
121 : /* }}} */
122 :
123 : static void zend_std_call_unsetter(zval *object, zval *member TSRMLS_DC) /* {{{ */
124 6 : {
125 6 : zend_class_entry *ce = Z_OBJCE_P(object);
126 :
127 : /* __unset handler is called with one argument:
128 : property name
129 : */
130 :
131 6 : SEPARATE_ARG_IF_REF(member);
132 :
133 6 : zend_call_method_with_1_params(&object, ce, &ce->__unset, ZEND_UNSET_FUNC_NAME, NULL, member);
134 :
135 6 : zval_ptr_dtor(&member);
136 6 : }
137 : /* }}} */
138 :
139 : static zval *zend_std_call_issetter(zval *object, zval *member TSRMLS_DC) /* {{{ */
140 7 : {
141 7 : zval *retval = NULL;
142 7 : zend_class_entry *ce = Z_OBJCE_P(object);
143 :
144 : /* __isset handler is called with one argument:
145 : property name
146 :
147 : it should return whether the property is set or not
148 : */
149 :
150 7 : SEPARATE_ARG_IF_REF(member);
151 :
152 7 : zend_call_method_with_1_params(&object, ce, &ce->__isset, ZEND_ISSET_FUNC_NAME, &retval, member);
153 :
154 7 : zval_ptr_dtor(&member);
155 :
156 7 : return retval;
157 : }
158 : /* }}} */
159 :
160 : static int zend_verify_property_access(zend_property_info *property_info, zend_class_entry *ce TSRMLS_DC) /* {{{ */
161 47803 : {
162 47803 : switch (property_info->flags & ZEND_ACC_PPP_MASK) {
163 : case ZEND_ACC_PUBLIC:
164 33011 : return 1;
165 : case ZEND_ACC_PROTECTED:
166 7428 : return zend_check_protected(property_info->ce, EG(scope));
167 : case ZEND_ACC_PRIVATE:
168 7364 : if ((ce==EG(scope) || property_info->ce == EG(scope)) && EG(scope)) {
169 7250 : return 1;
170 : } else {
171 114 : return 0;
172 : }
173 : break;
174 : }
175 0 : return 0;
176 : }
177 : /* }}} */
178 :
179 : static inline zend_bool is_derived_class(zend_class_entry *child_class, zend_class_entry *parent_class) /* {{{ */
180 380505 : {
181 380505 : child_class = child_class->parent;
182 763387 : while (child_class) {
183 4762 : if (child_class == parent_class) {
184 2385 : return 1;
185 : }
186 2377 : child_class = child_class->parent;
187 : }
188 :
189 378120 : return 0;
190 : }
191 : /* }}} */
192 :
193 : ZEND_API struct _zend_property_info *zend_get_property_info(zend_class_entry *ce, zval *member, int silent TSRMLS_DC) /* {{{ */
194 423703 : {
195 423703 : zend_property_info *property_info = NULL;
196 : zend_property_info *scope_property_info;
197 423703 : zend_bool denied_access = 0;
198 : ulong h;
199 :
200 423703 : if ((Z_TYPE_P(member) == IS_UNICODE && Z_USTRVAL_P(member)[0] == 0) ||
201 : (Z_TYPE_P(member) == IS_STRING && Z_STRVAL_P(member)[0] == '\0')) {
202 17 : if (!silent) {
203 1 : if (Z_UNILEN_P(member) == 0) {
204 1 : zend_error(E_ERROR, "Cannot access empty property");
205 : } else {
206 0 : zend_error(E_ERROR, "Cannot access property started with '\\0'");
207 : }
208 : }
209 16 : return NULL;
210 : }
211 423686 : h = zend_u_get_hash_value(Z_TYPE_P(member), Z_UNIVAL_P(member), Z_UNILEN_P(member) + 1);
212 423686 : if (zend_u_hash_quick_find(&ce->properties_info, Z_TYPE_P(member), Z_UNIVAL_P(member), Z_UNILEN_P(member)+1, h, (void **) &property_info)==SUCCESS) {
213 48816 : if(property_info->flags & ZEND_ACC_SHADOW) {
214 : /* if it's a shadow - go to access it's private */
215 2266 : property_info = NULL;
216 : } else {
217 46550 : if (zend_verify_property_access(property_info, ce TSRMLS_CC)) {
218 46389 : if (property_info->flags & ZEND_ACC_CHANGED
219 : && !(property_info->flags & ZEND_ACC_PRIVATE)) {
220 : /* We still need to make sure that we're not in a context
221 : * where the right property is a different 'statically linked' private
222 : * continue checking below...
223 : */
224 : } else {
225 46368 : if (!silent && (property_info->flags & ZEND_ACC_STATIC)) {
226 12 : zend_error(E_STRICT, "Accessing static property %v::$%R as non static", ce->name, Z_TYPE_P(member), Z_UNIVAL_P(member));
227 : }
228 46368 : return property_info;
229 : }
230 : } else {
231 : /* Try to look in the scope instead */
232 161 : denied_access = 1;
233 : }
234 : }
235 : }
236 377318 : if (EG(scope) != ce
237 : && is_derived_class(ce, EG(scope))
238 : && EG(scope)
239 : && zend_u_hash_quick_find(&EG(scope)->properties_info, Z_TYPE_P(member), Z_UNIVAL_P(member), Z_UNILEN_P(member)+1, h, (void **) &scope_property_info)==SUCCESS
240 : && scope_property_info->flags & ZEND_ACC_PRIVATE) {
241 2245 : return scope_property_info;
242 375073 : } else if (property_info) {
243 157 : if (denied_access) {
244 : /* Information was available, but we were denied access. Error out. */
245 147 : if (silent) {
246 132 : return NULL;
247 : }
248 15 : zend_error(E_ERROR, "Cannot access %s property %v::$%R", zend_visibility_string(property_info->flags), ce->name, Z_TYPE_P(member), Z_STRVAL_P(member));
249 : } else {
250 : /* fall through, return property_info... */
251 : }
252 : } else {
253 374916 : EG(std_property_info).flags = ZEND_ACC_PUBLIC;
254 374916 : EG(std_property_info).name = Z_UNIVAL_P(member);
255 374916 : EG(std_property_info).name_length = Z_UNILEN_P(member);
256 374916 : EG(std_property_info).h = h;
257 374916 : EG(std_property_info).ce = ce;
258 374916 : property_info = &EG(std_property_info);
259 : }
260 374926 : return property_info;
261 : }
262 : /* }}} */
263 :
264 : ZEND_API int zend_check_property_access(zend_object *zobj, zend_uchar utype, zstr prop_info_name, int prop_info_name_len TSRMLS_DC) /* {{{ */
265 563 : {
266 : zend_property_info *property_info;
267 : zstr class_name, prop_name;
268 : zval member;
269 :
270 563 : zend_u_unmangle_property_name(utype, prop_info_name, prop_info_name_len, &class_name, &prop_name);
271 563 : if (utype == IS_UNICODE) {
272 563 : ZVAL_UNICODE(&member, prop_name.u, 0);
273 : } else {
274 0 : ZVAL_STRING(&member, prop_name.s, 0);
275 : }
276 563 : property_info = zend_get_property_info(zobj->ce, &member, 1 TSRMLS_CC);
277 563 : if (!property_info) {
278 70 : return FAILURE;
279 : }
280 493 : if (class_name.s && class_name.s[0] != '*') {
281 83 : if (!(property_info->flags & ZEND_ACC_PRIVATE)) {
282 : /* we we're looking for a private prop but found a non private one of the same name */
283 17 : return FAILURE;
284 66 : } else if (u_strcmp(prop_info_name.u+1, property_info->name.u+1)) {
285 : /* we we're looking for a private prop but found a private one of the same name but another class */
286 11 : return FAILURE;
287 : }
288 : }
289 465 : return zend_verify_property_access(property_info, zobj->ce TSRMLS_CC) ? SUCCESS : FAILURE;
290 : }
291 : /* }}} */
292 :
293 : static int zend_get_property_guard(zend_object *zobj, zend_property_info *property_info, zval *member, zend_guard **pguard TSRMLS_DC) /* {{{ */
294 264 : {
295 : zend_property_info info;
296 : zend_guard stub;
297 :
298 264 : if (!property_info) {
299 34 : property_info = &info;
300 34 : info.name = Z_UNIVAL_P(member);
301 34 : info.name_length = Z_UNILEN_P(member);
302 34 : info.h = zend_u_get_hash_value(Z_TYPE_P(member), Z_UNIVAL_P(member), Z_UNILEN_P(member) + 1);
303 : }
304 264 : if (!zobj->guards) {
305 58 : ALLOC_HASHTABLE(zobj->guards);
306 58 : zend_u_hash_init(zobj->guards, 0, NULL, NULL, 0, UG(unicode));
307 206 : } else if (zend_u_hash_quick_find(zobj->guards, IS_UNICODE, property_info->name, property_info->name_length+1, property_info->h, (void **) pguard) == SUCCESS) {
308 163 : return SUCCESS;
309 : }
310 101 : stub.in_get = 0;
311 101 : stub.in_set = 0;
312 101 : stub.in_unset = 0;
313 101 : stub.in_isset = 0;
314 101 : return zend_u_hash_quick_add(zobj->guards, IS_UNICODE, property_info->name, property_info->name_length+1, property_info->h, (void**)&stub, sizeof(stub), (void**) pguard);
315 : }
316 : /* }}} */
317 :
318 : zval *zend_std_read_property(zval *object, zval *member, int type TSRMLS_DC) /* {{{ */
319 33343 : {
320 : zend_object *zobj;
321 33343 : zval *tmp_member = NULL;
322 : zval **retval;
323 33343 : zval *rv = NULL;
324 : zend_property_info *property_info;
325 : int silent;
326 :
327 33343 : silent = (type == BP_VAR_IS);
328 33343 : zobj = Z_OBJ_P(object);
329 :
330 33343 : if (Z_TYPE_P(member) != IS_UNICODE) {
331 1030 : ALLOC_ZVAL(tmp_member);
332 1030 : *tmp_member = *member;
333 1030 : INIT_PZVAL(tmp_member);
334 1030 : zval_copy_ctor(tmp_member);
335 1030 : convert_to_unicode(tmp_member);
336 1030 : member = tmp_member;
337 : }
338 :
339 : #if DEBUG_OBJECT_HANDLERS
340 : fprintf(stderr, "Read object #%d property: %R\n", Z_OBJ_HANDLE_P(object), Z_TYPE_P(member), Z_STRVAL_P(member));
341 : #endif
342 :
343 : /* make zend_get_property_info silent if we have getter - we may want to use it */
344 33343 : property_info = zend_get_property_info(zobj->ce, member, (zobj->ce->__get != NULL) TSRMLS_CC);
345 :
346 33339 : if (!property_info || zend_u_hash_quick_find(zobj->properties, Z_TYPE_P(member), property_info->name, property_info->name_length+1, property_info->h, (void **) &retval) == FAILURE) {
347 : zend_guard *guard;
348 :
349 434 : if (zobj->ce->__get &&
350 : zend_get_property_guard(zobj, property_info, member, &guard TSRMLS_CC) == SUCCESS &&
351 : !guard->in_get) {
352 : /* have getter - try with it! */
353 117 : Z_ADDREF_P(object);
354 117 : guard->in_get = 1; /* prevent circular getting */
355 117 : rv = zend_std_call_getter(object, member TSRMLS_CC);
356 117 : guard->in_get = 0;
357 :
358 117 : if (rv) {
359 115 : retval = &rv;
360 115 : if (!Z_ISREF_P(rv) &&
361 : (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)) {
362 19 : if (Z_REFCOUNT_P(rv) > 0) {
363 10 : zval *tmp = rv;
364 :
365 10 : ALLOC_ZVAL(rv);
366 10 : *rv = *tmp;
367 10 : zval_copy_ctor(rv);
368 10 : Z_UNSET_ISREF_P(rv);
369 10 : Z_SET_REFCOUNT_P(rv, 0);
370 : }
371 19 : if (Z_TYPE_P(rv) != IS_OBJECT) {
372 13 : zend_error(E_NOTICE, "Indirect modification of overloaded property %v::$%R has no effect", zobj->ce->name, Z_TYPE_P(member), Z_UNIVAL_P(member));
373 : }
374 : }
375 : } else {
376 2 : retval = &EG(uninitialized_zval_ptr);
377 : }
378 117 : zval_ptr_dtor(&object);
379 : } else {
380 200 : if (!silent) {
381 36 : zend_error(E_NOTICE,"Undefined property: %v::$%R", zobj->ce->name, Z_TYPE_P(member), Z_STRVAL_P(member));
382 : }
383 200 : retval = &EG(uninitialized_zval_ptr);
384 : }
385 : }
386 33339 : if (tmp_member) {
387 1030 : Z_ADDREF_PP(retval);
388 1030 : zval_ptr_dtor(&tmp_member);
389 1030 : Z_DELREF_PP(retval);
390 : }
391 33339 : return *retval;
392 : }
393 : /* }}} */
394 :
395 : static void zend_std_write_property(zval *object, zval *member, zval *value TSRMLS_DC) /* {{{ */
396 384259 : {
397 : zend_object *zobj;
398 384259 : zval *tmp_member = NULL;
399 : zval **variable_ptr;
400 : zend_property_info *property_info;
401 :
402 384259 : zobj = Z_OBJ_P(object);
403 :
404 384259 : if (Z_TYPE_P(member) != IS_UNICODE) {
405 22 : ALLOC_ZVAL(tmp_member);
406 22 : *tmp_member = *member;
407 22 : INIT_PZVAL(tmp_member);
408 22 : zval_copy_ctor(tmp_member);
409 22 : convert_to_unicode(tmp_member);
410 22 : member = tmp_member;
411 : }
412 :
413 384259 : property_info = zend_get_property_info(zobj->ce, member, (zobj->ce->__set != NULL) TSRMLS_CC);
414 :
415 396753 : if (property_info && zend_u_hash_quick_find(zobj->properties, Z_TYPE_P(member), property_info->name, property_info->name_length+1, property_info->h, (void **) &variable_ptr) == SUCCESS) {
416 : /* if we already have this value there, we don't actually need to do anything */
417 12504 : if (*variable_ptr != value) {
418 : /* if we are assigning reference, we shouldn't move it, but instead assign variable
419 : to the same pointer */
420 12493 : if (PZVAL_IS_REF(*variable_ptr)) {
421 41 : zval garbage = **variable_ptr; /* old value should be destroyed */
422 :
423 : /* To check: can't *variable_ptr be some system variable like error_zval here? */
424 41 : Z_TYPE_PP(variable_ptr) = Z_TYPE_P(value);
425 41 : (*variable_ptr)->value = value->value;
426 41 : if (Z_REFCOUNT_P(value) > 0) {
427 41 : zval_copy_ctor(*variable_ptr);
428 : }
429 41 : zval_dtor(&garbage);
430 : } else {
431 12452 : zval *garbage = *variable_ptr;
432 :
433 : /* if we assign referenced variable, we should separate it */
434 12452 : Z_ADDREF_P(value);
435 12452 : if (PZVAL_IS_REF(value)) {
436 1 : SEPARATE_ZVAL(&value);
437 : }
438 12452 : *variable_ptr = value;
439 12452 : zval_ptr_dtor(&garbage);
440 : }
441 : }
442 : } else {
443 371745 : int setter_done = 0;
444 : zend_guard *guard;
445 :
446 371745 : if (zobj->ce->__set &&
447 : zend_get_property_guard(zobj, property_info, member, &guard TSRMLS_CC) == SUCCESS &&
448 : !guard->in_set) {
449 82 : Z_ADDREF_P(object);
450 82 : guard->in_set = 1; /* prevent circular setting */
451 82 : if (zend_std_call_setter(object, member, value TSRMLS_CC) != SUCCESS) {
452 : /* for now, just ignore it - __set should take care of warnings, etc. */
453 : }
454 82 : setter_done = 1;
455 82 : guard->in_set = 0;
456 82 : zval_ptr_dtor(&object);
457 : }
458 371745 : if (!setter_done && property_info) {
459 : zval **foo;
460 :
461 : /* if we assign referenced variable, we should separate it */
462 371656 : Z_ADDREF_P(value);
463 371656 : if (PZVAL_IS_REF(value)) {
464 10013 : SEPARATE_ZVAL(&value);
465 : }
466 371656 : zend_u_hash_quick_update(zobj->properties, Z_TYPE_P(member), property_info->name, property_info->name_length+1, property_info->h, &value, sizeof(zval *), (void **) &foo);
467 : }
468 : }
469 :
470 384249 : if (tmp_member) {
471 22 : zval_ptr_dtor(&tmp_member);
472 : }
473 384249 : }
474 : /* }}} */
475 :
476 : zval *zend_std_read_dimension(zval *object, zval *offset, int type TSRMLS_DC) /* {{{ */
477 472 : {
478 472 : zend_class_entry *ce = Z_OBJCE_P(object);
479 : zval *retval;
480 :
481 472 : if (instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC)) {
482 470 : if(offset == NULL) {
483 : /* [] construct */
484 0 : ALLOC_INIT_ZVAL(offset);
485 : } else {
486 470 : SEPARATE_ARG_IF_REF(offset);
487 : }
488 470 : zend_call_method_with_1_params(&object, ce, NULL, "offsetget", &retval, offset);
489 :
490 470 : zval_ptr_dtor(&offset);
491 :
492 470 : if (!retval) {
493 5 : if (!EG(exception)) {
494 0 : zend_error(E_ERROR, "Undefined offset for object of type %v used as array", ce->name);
495 : }
496 5 : return 0;
497 : }
498 :
499 : /* Undo PZVAL_LOCK() */
500 465 : Z_DELREF_P(retval);
501 :
502 465 : return retval;
503 : } else {
504 2 : zend_error(E_ERROR, "Cannot use object of type %v as array", ce->name);
505 0 : return 0;
506 : }
507 : }
508 : /* }}} */
509 :
510 : static void zend_std_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC) /* {{{ */
511 312 : {
512 312 : zend_class_entry *ce = Z_OBJCE_P(object);
513 :
514 312 : if (instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC)) {
515 311 : if (!offset) {
516 10 : ALLOC_INIT_ZVAL(offset);
517 : } else {
518 301 : SEPARATE_ARG_IF_REF(offset);
519 : }
520 311 : zend_call_method_with_2_params(&object, ce, NULL, "offsetset", NULL, offset, value);
521 311 : zval_ptr_dtor(&offset);
522 : } else {
523 1 : zend_error(E_ERROR, "Cannot use object of type %v as array", ce->name);
524 : }
525 311 : }
526 : /* }}} */
527 :
528 : static int zend_std_has_dimension(zval *object, zval *offset, int check_empty TSRMLS_DC) /* {{{ */
529 78 : {
530 78 : zend_class_entry *ce = Z_OBJCE_P(object);
531 : zval *retval;
532 : int result;
533 :
534 78 : if (instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC)) {
535 78 : SEPARATE_ARG_IF_REF(offset);
536 78 : zend_call_method_with_1_params(&object, ce, NULL, "offsetexists", &retval, offset);
537 78 : if (retval) {
538 76 : result = i_zend_is_true(retval);
539 76 : zval_ptr_dtor(&retval);
540 76 : if (check_empty && result && !EG(exception)) {
541 8 : zend_call_method_with_1_params(&object, ce, NULL, "offsetget", &retval, offset);
542 8 : if (retval) {
543 8 : result = i_zend_is_true(retval);
544 8 : zval_ptr_dtor(&retval);
545 : }
546 : }
547 : } else {
548 2 : result = 0;
549 : }
550 78 : zval_ptr_dtor(&offset);
551 : } else {
552 0 : zend_error(E_ERROR, "Cannot use object of type %v as array", ce->name);
553 0 : return 0;
554 : }
555 78 : return result;
556 : }
557 : /* }}} */
558 :
559 : static zval **zend_std_get_property_ptr_ptr(zval *object, zval *member TSRMLS_DC) /* {{{ */
560 4862 : {
561 : zend_object *zobj;
562 : zval tmp_member;
563 : zval **retval;
564 : zend_property_info *property_info;
565 :
566 4862 : zobj = Z_OBJ_P(object);
567 :
568 4862 : if (Z_TYPE_P(member) != IS_UNICODE) {
569 2 : tmp_member = *member;
570 2 : zval_copy_ctor(&tmp_member);
571 2 : convert_to_unicode(&tmp_member);
572 2 : member = &tmp_member;
573 : }
574 :
575 : #if DEBUG_OBJECT_HANDLERS
576 : fprintf(stderr, "Ptr object #%d property: %R\n", Z_OBJ_HANDLE_P(object), Z_TYPE_P(member), Z_STRVAL_P(member));
577 : #endif
578 :
579 4862 : property_info = zend_get_property_info(zobj->ce, member, (zobj->ce->__get != NULL) TSRMLS_CC);
580 :
581 4861 : if (!property_info || zend_u_hash_quick_find(zobj->properties, Z_TYPE_P(member), property_info->name, property_info->name_length+1, property_info->h, (void **) &retval) == FAILURE) {
582 : zval *new_zval;
583 : zend_guard *guard;
584 :
585 250 : if (!zobj->ce->__get ||
586 : zend_get_property_guard(zobj, property_info, member, &guard TSRMLS_CC) != SUCCESS ||
587 : (property_info && guard->in_get)) {
588 : /* we don't have access controls - will just add it */
589 106 : new_zval = &EG(uninitialized_zval);
590 :
591 : /* zend_error(E_NOTICE, "Undefined property: %R", Z_TYPE_P(member), Z_STRVAL_P(member)); */
592 106 : Z_ADDREF_P(new_zval);
593 106 : zend_u_hash_quick_update(zobj->properties, Z_TYPE_P(member), property_info->name, property_info->name_length+1, property_info->h, &new_zval, sizeof(zval *), (void **) &retval);
594 : } else {
595 : /* we do have getter - fail and let it try again with usual get/set */
596 38 : retval = NULL;
597 : }
598 : }
599 4861 : if (member == &tmp_member) {
600 2 : zval_dtor(member);
601 : }
602 4861 : return retval;
603 : }
604 : /* }}} */
605 :
606 : static void zend_std_unset_property(zval *object, zval *member TSRMLS_DC) /* {{{ */
607 149 : {
608 : zend_object *zobj;
609 149 : zval *tmp_member = NULL;
610 : zend_property_info *property_info;
611 :
612 149 : zobj = Z_OBJ_P(object);
613 :
614 149 : if (Z_TYPE_P(member) != IS_UNICODE) {
615 55 : ALLOC_ZVAL(tmp_member);
616 55 : *tmp_member = *member;
617 55 : INIT_PZVAL(tmp_member);
618 55 : zval_copy_ctor(tmp_member);
619 55 : convert_to_unicode(tmp_member);
620 55 : member = tmp_member;
621 : }
622 :
623 149 : property_info = zend_get_property_info(zobj->ce, member, (zobj->ce->__unset != NULL) TSRMLS_CC);
624 :
625 148 : if (!property_info || zend_u_hash_quick_del(zobj->properties, Z_TYPE_P(member), property_info->name, property_info->name_length+1, property_info->h)) {
626 : zend_guard *guard;
627 :
628 14 : if (zobj->ce->__unset &&
629 : zend_get_property_guard(zobj, property_info, member, &guard TSRMLS_CC) == SUCCESS &&
630 : !guard->in_unset) {
631 : /* have unseter - try with it! */
632 6 : Z_ADDREF_P(object);
633 6 : guard->in_unset = 1; /* prevent circular unsetting */
634 6 : zend_std_call_unsetter(object, member TSRMLS_CC);
635 6 : guard->in_unset = 0;
636 6 : zval_ptr_dtor(&object);
637 : }
638 : }
639 :
640 148 : if (tmp_member) {
641 55 : zval_ptr_dtor(&tmp_member);
642 : }
643 148 : }
644 : /* }}} */
645 :
646 : static void zend_std_unset_dimension(zval *object, zval *offset TSRMLS_DC) /* {{{ */
647 27 : {
648 27 : zend_class_entry *ce = Z_OBJCE_P(object);
649 :
650 27 : if (instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC)) {
651 27 : SEPARATE_ARG_IF_REF(offset);
652 27 : zend_call_method_with_1_params(&object, ce, NULL, "offsetunset", NULL, offset);
653 27 : zval_ptr_dtor(&offset);
654 : } else {
655 0 : zend_error(E_ERROR, "Cannot use object of type %v as array", ce->name);
656 : }
657 27 : }
658 : /* }}} */
659 :
660 : ZEND_API void zend_std_call_user_call(INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
661 412 : {
662 412 : zend_internal_function *func = (zend_internal_function *)EG(current_execute_data)->function_state.function;
663 : zval *method_name_ptr, *method_args_ptr;
664 412 : zval *method_result_ptr = NULL;
665 412 : zend_class_entry *ce = Z_OBJCE_P(this_ptr);
666 :
667 412 : ALLOC_ZVAL(method_args_ptr);
668 412 : INIT_PZVAL(method_args_ptr);
669 412 : array_init_size(method_args_ptr, ZEND_NUM_ARGS());
670 :
671 412 : if (zend_copy_parameters_array(ZEND_NUM_ARGS(), method_args_ptr TSRMLS_CC) == FAILURE) {
672 0 : zval_dtor(method_args_ptr);
673 0 : zend_error(E_ERROR, "Cannot get arguments for __call");
674 0 : RETURN_FALSE;
675 : }
676 :
677 412 : ALLOC_ZVAL(method_name_ptr);
678 412 : INIT_PZVAL(method_name_ptr);
679 412 : ZVAL_UNICODE(method_name_ptr, func->function_name.u, 0); /* no dup - it's a copy */
680 :
681 : /* __call handler is called with two arguments:
682 : method name
683 : array of method parameters
684 :
685 : */
686 412 : zend_call_method_with_2_params(&this_ptr, ce, &ce->__call, ZEND_CALL_FUNC_NAME, &method_result_ptr, method_name_ptr, method_args_ptr);
687 :
688 404 : if (method_result_ptr) {
689 400 : if (Z_ISREF_P(method_result_ptr) || Z_REFCOUNT_P(method_result_ptr) > 1) {
690 2 : RETVAL_ZVAL(method_result_ptr, 1, 1);
691 : } else {
692 396 : RETVAL_ZVAL(method_result_ptr, 0, 1);
693 : }
694 : }
695 :
696 : /* now destruct all auxiliaries */
697 404 : zval_ptr_dtor(&method_args_ptr);
698 404 : zval_ptr_dtor(&method_name_ptr);
699 :
700 : /* destruct the function also, then - we have allocated it in get_method */
701 404 : efree(func);
702 : }
703 : /* }}} */
704 :
705 : /* Ensures that we're allowed to call a private method.
706 : * Returns the function address that should be called, or NULL
707 : * if no such function exists.
708 : */
709 : static inline zend_function *zend_check_private_int(zend_function *fbc, zend_class_entry *ce, zstr function_name_strval, int function_name_strlen TSRMLS_DC) /* {{{ */
710 84 : {
711 84 : if (!ce) {
712 5 : return 0;
713 : }
714 :
715 : /* We may call a private function if:
716 : * 1. The class of our object is the same as the scope, and the private
717 : * function (EX(fbc)) has the same scope.
718 : * 2. One of our parent classes are the same as the scope, and it contains
719 : * a private function with the same name that has the same scope.
720 : */
721 79 : if (fbc->common.scope == ce && EG(scope) == ce) {
722 : /* rule #1 checks out ok, allow the function call */
723 40 : return fbc;
724 : }
725 :
726 :
727 : /* Check rule #2 */
728 39 : ce = ce->parent;
729 90 : while (ce) {
730 23 : if (ce == EG(scope)) {
731 11 : if (zend_u_hash_find(&ce->function_table, IS_UNICODE, function_name_strval, function_name_strlen+1, (void **) &fbc)==SUCCESS
732 : && fbc->op_array.fn_flags & ZEND_ACC_PRIVATE
733 : && fbc->common.scope == EG(scope)) {
734 9 : return fbc;
735 : }
736 2 : break;
737 : }
738 12 : ce = ce->parent;
739 : }
740 30 : return NULL;
741 : }
742 : /* }}} */
743 :
744 : ZEND_API int zend_check_private(zend_function *fbc, zend_class_entry *ce, zstr function_name_strval, int function_name_strlen TSRMLS_DC) /* {{{ */
745 29 : {
746 29 : return zend_check_private_int(fbc, ce, function_name_strval, function_name_strlen TSRMLS_CC) != NULL;
747 : }
748 : /* }}} */
749 :
750 : /* Ensures that we're allowed to call a protected method.
751 : */
752 : ZEND_API int zend_check_protected(zend_class_entry *ce, zend_class_entry *scope) /* {{{ */
753 7559 : {
754 7559 : zend_class_entry *fbc_scope = ce;
755 :
756 : /* Is the context that's calling the function, the same as one of
757 : * the function's parents?
758 : */
759 15493 : while (fbc_scope) {
760 7701 : if (fbc_scope==scope) {
761 7326 : return 1;
762 : }
763 375 : fbc_scope = fbc_scope->parent;
764 : }
765 :
766 : /* Is the function's scope the same as our current object context,
767 : * or any of the parents of our context?
768 : */
769 631 : while (scope) {
770 293 : if (scope==ce) {
771 128 : return 1;
772 : }
773 165 : scope = scope->parent;
774 : }
775 105 : return 0;
776 : }
777 : /* }}} */
778 :
779 : static inline zend_class_entry * zend_get_function_root_class(zend_function *fbc) /* {{{ */
780 77 : {
781 77 : return fbc->common.prototype ? fbc->common.prototype->common.scope : fbc->common.scope;
782 : }
783 : /* }}} */
784 :
785 : static inline union _zend_function *zend_get_user_call_function(zend_class_entry *ce, const zstr method_name, int method_len) /* {{{ */
786 414 : {
787 414 : zend_internal_function *call_user_call = emalloc(sizeof(zend_internal_function));
788 :
789 414 : call_user_call->type = ZEND_INTERNAL_FUNCTION;
790 414 : call_user_call->module = ce->module;
791 414 : call_user_call->handler = zend_std_call_user_call;
792 414 : call_user_call->arg_info = NULL;
793 414 : call_user_call->num_args = 0;
794 414 : call_user_call->scope = ce;
795 414 : call_user_call->fn_flags = ZEND_ACC_CALL_VIA_HANDLER;
796 414 : call_user_call->function_name.u = eustrndup(method_name.u, method_len);
797 414 : call_user_call->pass_rest_by_reference = 0;
798 414 : call_user_call->return_reference = ZEND_RETURN_VALUE;
799 :
800 414 : return (union _zend_function *)call_user_call;
801 : }
802 : /* }}} */
803 :
804 : static union _zend_function *zend_std_get_method(zval **object_ptr, zstr method_name, int method_len TSRMLS_DC) /* {{{ */
805 218700 : {
806 : zend_object *zobj;
807 : zend_function *fbc;
808 : unsigned int lc_method_name_len;
809 : zstr lc_method_name;
810 218700 : zval *object = *object_ptr;
811 :
812 : /* FIXME: type is default */
813 218700 : zend_uchar type = IS_UNICODE;
814 :
815 : /* Create a zend_copy_str_tolower(dest, src, src_length); */
816 218700 : lc_method_name = zend_u_str_case_fold(type, method_name, method_len, 1, &lc_method_name_len);
817 :
818 218700 : zobj = Z_OBJ_P(object);
819 218700 : if (zend_u_hash_find(&zobj->ce->function_table, type, lc_method_name, lc_method_name_len+1, (void **)&fbc) == FAILURE) {
820 646 : efree(lc_method_name.v);
821 646 : if (zobj->ce->__call) {
822 380 : return zend_get_user_call_function(zobj->ce, method_name, method_len);
823 : } else {
824 266 : return NULL;
825 : }
826 : }
827 :
828 : /* Check access level */
829 218054 : if (fbc->op_array.fn_flags & ZEND_ACC_PRIVATE) {
830 : zend_function *updated_fbc;
831 :
832 : /* Ensure that if we're calling a private function, we're allowed to do so.
833 : * If we're not and __call() handler exists, invoke it, otherwise error out.
834 : */
835 38 : updated_fbc = zend_check_private_int(fbc, Z_OBJ_HANDLER_P(object, get_class_entry)(object TSRMLS_CC), lc_method_name, method_len TSRMLS_CC);
836 38 : if (updated_fbc) {
837 29 : fbc = updated_fbc;
838 : } else {
839 9 : if (zobj->ce->__call) {
840 3 : fbc = zend_get_user_call_function(zobj->ce, method_name, method_len);
841 : } else {
842 6 : zend_error(E_ERROR, "Call to %s method %v::%v() from context '%v'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), method_name, EG(scope) ? EG(scope)->name : EMPTY_ZSTR);
843 : }
844 : }
845 : } else {
846 : /* Ensure that we haven't overridden a private function and end up calling
847 : * the overriding public function...
848 : */
849 218016 : if (EG(scope) &&
850 : is_derived_class(fbc->common.scope, EG(scope)) &&
851 : fbc->op_array.fn_flags & ZEND_ACC_CHANGED) {
852 : zend_function *priv_fbc;
853 :
854 7 : if (zend_u_hash_find(&EG(scope)->function_table, type, lc_method_name, lc_method_name_len+1, (void **) &priv_fbc)==SUCCESS
855 : && priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE
856 : && priv_fbc->common.scope == EG(scope)) {
857 6 : fbc = priv_fbc;
858 : }
859 : }
860 218016 : if ((fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
861 : /* Ensure that if we're calling a protected function, we're allowed to do so.
862 : * If we're not and __call() handler exists, invoke it, otherwise error out.
863 : */
864 38 : if (!zend_check_protected(zend_get_function_root_class(fbc), EG(scope))) {
865 11 : if (zobj->ce->__call) {
866 3 : fbc = zend_get_user_call_function(zobj->ce, method_name, method_len);
867 : } else {
868 8 : zend_error(E_ERROR, "Call to %s method %v::%v() from context '%v'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), method_name, EG(scope) ? EG(scope)->name : EMPTY_ZSTR);
869 : }
870 : }
871 : }
872 : }
873 :
874 218040 : efree(lc_method_name.v);
875 218040 : return fbc;
876 : }
877 : /* }}} */
878 :
879 : ZEND_API void zend_std_callstatic_user_call(INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
880 37 : {
881 37 : zend_internal_function *func = (zend_internal_function *)EG(current_execute_data)->function_state.function;
882 : zval *method_name_ptr, *method_args_ptr;
883 37 : zval *method_result_ptr = NULL;
884 37 : zend_class_entry *ce = EG(scope);
885 :
886 37 : ALLOC_ZVAL(method_args_ptr);
887 37 : INIT_PZVAL(method_args_ptr);
888 37 : array_init_size(method_args_ptr, ZEND_NUM_ARGS());
889 :
890 37 : if (zend_copy_parameters_array(ZEND_NUM_ARGS(), method_args_ptr TSRMLS_CC) == FAILURE) {
891 0 : zval_dtor(method_args_ptr);
892 0 : zend_error(E_ERROR, "Cannot get arguments for " ZEND_CALLSTATIC_FUNC_NAME);
893 0 : RETURN_FALSE;
894 : }
895 :
896 37 : ALLOC_ZVAL(method_name_ptr);
897 37 : INIT_PZVAL(method_name_ptr);
898 37 : ZVAL_UNICODE(method_name_ptr, func->function_name.u, 0); /* no dup - it's a copy */
899 :
900 : /* __callStatic handler is called with two arguments:
901 : method name
902 : array of method parameters
903 : */
904 37 : zend_call_method_with_2_params(NULL, ce, &ce->__callstatic, ZEND_CALLSTATIC_FUNC_NAME, &method_result_ptr, method_name_ptr, method_args_ptr);
905 :
906 37 : if (method_result_ptr) {
907 37 : if (Z_ISREF_P(method_result_ptr) || Z_REFCOUNT_P(method_result_ptr) > 1) {
908 0 : RETVAL_ZVAL(method_result_ptr, 1, 1);
909 : } else {
910 37 : RETVAL_ZVAL(method_result_ptr, 0, 1);
911 : }
912 : }
913 :
914 : /* now destruct all auxiliaries */
915 37 : zval_ptr_dtor(&method_args_ptr);
916 37 : zval_ptr_dtor(&method_name_ptr);
917 :
918 : /* destruct the function also, then - we have allocated it in get_method */
919 37 : efree(func);
920 : }
921 : /* }}} */
922 :
923 : static inline union _zend_function *zend_get_user_callstatic_function(zend_class_entry *ce, const zstr method_name, int method_len) /* {{{ */
924 37 : {
925 37 : zend_internal_function *callstatic_user_call = emalloc(sizeof(zend_internal_function));
926 37 : callstatic_user_call->type = ZEND_INTERNAL_FUNCTION;
927 37 : callstatic_user_call->module = ce->module;
928 37 : callstatic_user_call->handler = zend_std_callstatic_user_call;
929 37 : callstatic_user_call->arg_info = NULL;
930 37 : callstatic_user_call->num_args = 0;
931 37 : callstatic_user_call->scope = ce;
932 37 : callstatic_user_call->fn_flags = ZEND_ACC_STATIC | ZEND_ACC_PUBLIC | ZEND_ACC_CALL_VIA_HANDLER;
933 37 : callstatic_user_call->function_name.u = eustrndup(method_name.u, method_len);
934 37 : callstatic_user_call->pass_rest_by_reference = 0;
935 37 : callstatic_user_call->return_reference = ZEND_RETURN_VALUE;
936 :
937 37 : return (union _zend_function *)callstatic_user_call;
938 : }
939 : /* }}} */
940 :
941 : ZEND_API zend_function *zend_std_get_static_method(zend_class_entry *ce, zend_uchar type, zstr function_name_strval, int function_name_strlen TSRMLS_DC) /* {{{ */
942 4062 : {
943 4062 : zend_function *fbc = NULL;
944 : zstr lc_function_name;
945 : unsigned int lc_function_name_len;
946 :
947 4062 : lc_function_name = zend_u_str_case_fold(type, function_name_strval, function_name_strlen, 0, &lc_function_name_len);
948 :
949 4062 : if (function_name_strlen == ce->name_length && ce->constructor) {
950 : zstr lc_class_name;
951 : unsigned int lc_class_name_len, real_len;
952 :
953 37 : lc_class_name = zend_u_str_case_fold(type, ce->name, ce->name_length, 0, &lc_class_name_len);
954 37 : real_len = USTR_BYTES(type, lc_class_name_len);
955 :
956 37 : if (!memcmp(lc_class_name.s, function_name_strval.s, real_len)) {
957 1 : fbc = ce->constructor;
958 : }
959 37 : efree(lc_class_name.v);
960 : }
961 :
962 4062 : if (!fbc && zend_u_hash_find(&ce->function_table, type, lc_function_name, function_name_strlen + 1, (void **) &fbc)==FAILURE) {
963 81 : efree(lc_function_name.v);
964 :
965 81 : if (ce->__call &&
966 : EG(This) &&
967 : Z_OBJ_HT_P(EG(This))->get_class_entry &&
968 : instanceof_function(Z_OBJCE_P(EG(This)), ce TSRMLS_CC)) {
969 28 : return zend_get_user_call_function(ce, function_name_strval, function_name_strlen);
970 53 : } else if (ce->__callstatic) {
971 35 : return zend_get_user_callstatic_function(ce, function_name_strval, function_name_strlen);
972 : } else {
973 18 : return NULL;
974 : }
975 : }
976 3981 : efree(lc_function_name.v);
977 :
978 : #if MBO_0
979 : /* right now this function is used for non static method lookup too */
980 : /* Is the function static */
981 : if (!(fbc->common.fn_flags & ZEND_ACC_STATIC)) {
982 : zend_error(E_ERROR, "Cannot call non static method %v::%v() without object", ZEND_FN_SCOPE_NAME(fbc), fbc->common.function_name);
983 : }
984 : #endif
985 3981 : if (fbc->op_array.fn_flags & ZEND_ACC_PUBLIC) {
986 : /* No further checks necessary, most common case */
987 50 : } else if (fbc->op_array.fn_flags & ZEND_ACC_PRIVATE) {
988 : zend_function *updated_fbc;
989 :
990 : /* Ensure that if we're calling a private function, we're allowed to do so.
991 : */
992 17 : updated_fbc = zend_check_private_int(fbc, EG(scope), function_name_strval, function_name_strlen TSRMLS_CC);
993 17 : if (updated_fbc) {
994 10 : fbc = updated_fbc;
995 : } else {
996 7 : if (ce->__callstatic) {
997 1 : return zend_get_user_callstatic_function(ce, function_name_strval, function_name_strlen);
998 : }
999 6 : zend_error(E_ERROR, "Call to %s method %v::%v() from context '%v'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), fbc->common.function_name, EG(scope) ? EG(scope)->name : EMPTY_ZSTR);
1000 : }
1001 33 : } else if ((fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
1002 : /* Ensure that if we're calling a protected function, we're allowed to do so.
1003 : */
1004 33 : if (!zend_check_protected(zend_get_function_root_class(fbc), EG(scope))) {
1005 3 : if (ce->__callstatic) {
1006 1 : return zend_get_user_callstatic_function(ce, function_name_strval, function_name_strlen);
1007 : }
1008 2 : zend_error(E_ERROR, "Call to %s method %v::%v() from context '%v'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), fbc->common.function_name, EG(scope) ? EG(scope)->name : EMPTY_ZSTR);
1009 : }
1010 : }
1011 :
1012 3971 : return fbc;
1013 : }
1014 : /* }}} */
1015 :
1016 : ZEND_API zval **zend_std_get_static_property(zend_class_entry *ce, zend_uchar type, zstr property_name, int property_name_len, zend_bool silent TSRMLS_DC) /* {{{ */
1017 788 : {
1018 788 : zval **retval = NULL;
1019 788 : zend_class_entry *tmp_ce = ce;
1020 : zend_property_info *property_info;
1021 : zend_property_info std_property_info;
1022 :
1023 788 : if (zend_u_hash_find(&ce->properties_info, type, property_name, property_name_len+1, (void **) &property_info)==FAILURE) {
1024 23 : std_property_info.flags = ZEND_ACC_PUBLIC;
1025 23 : std_property_info.name = property_name;
1026 23 : std_property_info.name_length = property_name_len;
1027 23 : std_property_info.h = zend_u_get_hash_value(IS_UNICODE, std_property_info.name, std_property_info.name_length+1);
1028 23 : std_property_info.ce = ce;
1029 23 : property_info = &std_property_info;
1030 : }
1031 :
1032 : #if DEBUG_OBJECT_HANDLERS
1033 : zend_printf("Access type for %v::%R is %s\n", ce->name, type, property_name, zend_visibility_string(property_info->flags));
1034 : #endif
1035 :
1036 788 : if (!zend_verify_property_access(property_info, ce TSRMLS_CC)) {
1037 14 : if (!silent) {
1038 2 : zend_error(E_ERROR, "Cannot access %s property %v::$%R", zend_visibility_string(property_info->flags), ce->name, type, property_name);
1039 : }
1040 12 : return NULL;
1041 : }
1042 :
1043 774 : zend_update_class_constants(tmp_ce TSRMLS_CC);
1044 :
1045 774 : zend_u_hash_quick_find(CE_STATIC_MEMBERS(tmp_ce), IS_UNICODE, property_info->name, property_info->name_length+1, property_info->h, (void **) &retval);
1046 :
1047 774 : if (!retval) {
1048 13 : if (silent) {
1049 5 : return NULL;
1050 : } else {
1051 8 : zend_error(E_ERROR, "Access to undeclared static property: %v::$%R", ce->name, type, property_name);
1052 : }
1053 : }
1054 :
1055 761 : return retval;
1056 : }
1057 : /* }}} */
1058 :
1059 : ZEND_API zend_bool zend_std_unset_static_property(zend_class_entry *ce, zend_uchar type, zstr property_name, int property_name_len TSRMLS_DC) /* {{{ */
1060 0 : {
1061 0 : zend_error(E_ERROR, "Attempt to unset static property %v::$%R", ce->name, type, property_name);
1062 0 : return 0;
1063 : }
1064 : /* }}} */
1065 :
1066 : ZEND_API union _zend_function *zend_std_get_constructor(zval *object TSRMLS_DC) /* {{{ */
1067 115036 : {
1068 115036 : zend_object *zobj = Z_OBJ_P(object);
1069 115036 : zend_function *constructor = zobj->ce->constructor;
1070 :
1071 115036 : if (constructor) {
1072 8017 : if (constructor->op_array.fn_flags & ZEND_ACC_PUBLIC) {
1073 : /* No further checks necessary */
1074 16 : } else if (constructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
1075 : /* Ensure that if we're calling a private function, we're allowed to do so.
1076 : */
1077 10 : if (constructor->common.scope != EG(scope)) {
1078 3 : if (EG(scope)) {
1079 1 : zend_error(E_ERROR, "Call to private %v::%v() from context '%v'", constructor->common.scope->name, constructor->common.function_name, EG(scope)->name);
1080 : } else {
1081 2 : zend_error(E_ERROR, "Call to private %v::%v() from invalid context", constructor->common.scope->name, constructor->common.function_name);
1082 : }
1083 : }
1084 6 : } else if ((constructor->common.fn_flags & ZEND_ACC_PROTECTED)) {
1085 : /* Ensure that if we're calling a protected function, we're allowed to do so.
1086 : * Constructors only have prototype if they are defined by an interface but
1087 : * it is the compilers responsibility to take care of the prototype.
1088 : */
1089 6 : if (!zend_check_protected(zend_get_function_root_class(constructor), EG(scope))) {
1090 2 : if (EG(scope)) {
1091 1 : zend_error(E_ERROR, "Call to protected %v::%v() from context '%v'", constructor->common.scope->name, constructor->common.function_name, EG(scope)->name);
1092 : } else {
1093 1 : zend_error(E_ERROR, "Call to protected %v::%v() from invalid context", constructor->common.scope->name, constructor->common.function_name);
1094 : }
1095 : }
1096 : }
1097 : }
1098 :
1099 115031 : return constructor;
1100 : }
1101 : /* }}} */
1102 :
1103 : int zend_compare_symbol_tables_i(HashTable *ht1, HashTable *ht2 TSRMLS_DC);
1104 :
1105 : static int zend_std_compare_objects(zval *o1, zval *o2 TSRMLS_DC) /* {{{ */
1106 507 : {
1107 : zend_object *zobj1, *zobj2;
1108 :
1109 507 : zobj1 = Z_OBJ_P(o1);
1110 507 : zobj2 = Z_OBJ_P(o2);
1111 :
1112 507 : if (zobj1->ce != zobj2->ce) {
1113 34 : return 1; /* different classes */
1114 : }
1115 473 : return zend_compare_symbol_tables_i(zobj1->properties, zobj2->properties TSRMLS_CC);
1116 : }
1117 : /* }}} */
1118 :
1119 : static int zend_std_has_property(zval *object, zval *member, int has_set_exists TSRMLS_DC) /* {{{ */
1120 235 : {
1121 : zend_object *zobj;
1122 : int result;
1123 : zval **value;
1124 235 : zval *tmp_member = NULL;
1125 : zend_property_info *property_info;
1126 :
1127 235 : zobj = Z_OBJ_P(object);
1128 :
1129 235 : if (Z_TYPE_P(member) != IS_UNICODE) {
1130 6 : ALLOC_ZVAL(tmp_member);
1131 6 : *tmp_member = *member;
1132 6 : INIT_PZVAL(tmp_member);
1133 6 : zval_copy_ctor(tmp_member);
1134 6 : convert_to_unicode(tmp_member);
1135 6 : member = tmp_member;
1136 : }
1137 :
1138 : #if DEBUG_OBJECT_HANDLERS
1139 : fprintf(stderr, "Read object #%d property: %R\n", Z_OBJ_HANDLE_P(object), Z_TYPE_P(member), Z_STRVAL_P(member));
1140 : #endif
1141 :
1142 235 : property_info = zend_get_property_info(zobj->ce, member, 1 TSRMLS_CC);
1143 :
1144 353 : if (!property_info || zend_u_hash_quick_find(zobj->properties, Z_TYPE_P(member), property_info->name, property_info->name_length+1, property_info->h, (void **) &value) == FAILURE) {
1145 : zend_guard *guard;
1146 :
1147 118 : result = 0;
1148 118 : if ((has_set_exists != 2) &&
1149 : zobj->ce->__isset &&
1150 : zend_get_property_guard(zobj, property_info, member, &guard TSRMLS_CC) == SUCCESS &&
1151 : !guard->in_isset) {
1152 : zval *rv;
1153 :
1154 : /* have issetter - try with it! */
1155 7 : Z_ADDREF_P(object);
1156 7 : guard->in_isset = 1; /* prevent circular getting */
1157 7 : rv = zend_std_call_issetter(object, member TSRMLS_CC);
1158 7 : if (rv) {
1159 7 : result = zend_is_true(rv);
1160 7 : zval_ptr_dtor(&rv);
1161 7 : if (has_set_exists && result) {
1162 3 : if (!EG(exception) && zobj->ce->__get && !guard->in_get) {
1163 1 : guard->in_get = 1;
1164 1 : rv = zend_std_call_getter(object, member TSRMLS_CC);
1165 1 : guard->in_get = 0;
1166 1 : if (rv) {
1167 1 : Z_ADDREF_P(rv);
1168 1 : result = i_zend_is_true(rv);
1169 1 : zval_ptr_dtor(&rv);
1170 : } else {
1171 0 : result = 0;
1172 : }
1173 : } else {
1174 1 : result = 0;
1175 : }
1176 : }
1177 : }
1178 7 : guard->in_isset = 0;
1179 7 : zval_ptr_dtor(&object);
1180 : }
1181 : } else {
1182 117 : switch (has_set_exists) {
1183 : case 0:
1184 64 : result = (Z_TYPE_PP(value) != IS_NULL);
1185 64 : break;
1186 : default:
1187 23 : result = zend_is_true(*value);
1188 23 : break;
1189 : case 2:
1190 30 : result = 1;
1191 : break;
1192 : }
1193 : }
1194 :
1195 235 : if (tmp_member) {
1196 6 : zval_ptr_dtor(&tmp_member);
1197 : }
1198 235 : return result;
1199 : }
1200 : /* }}} */
1201 :
1202 : zend_class_entry *zend_std_object_get_class(const zval *object TSRMLS_DC) /* {{{ */
1203 619282 : {
1204 : zend_object *zobj;
1205 619282 : zobj = Z_OBJ_P(object);
1206 :
1207 619282 : return zobj->ce;
1208 : }
1209 : /* }}} */
1210 :
1211 : int zend_std_object_get_class_name(const zval *object, zstr *class_name, zend_uint *class_name_len, int parent TSRMLS_DC) /* {{{ */
1212 4386 : {
1213 : zend_object *zobj;
1214 : zend_class_entry *ce;
1215 4386 : zobj = Z_OBJ_P(object);
1216 :
1217 4386 : if (parent) {
1218 11 : if (!zobj->ce->parent) {
1219 8 : return FAILURE;
1220 : }
1221 3 : ce = zobj->ce->parent;
1222 : } else {
1223 4375 : ce = zobj->ce;
1224 : }
1225 :
1226 4378 : *class_name_len = ce->name_length;
1227 4378 : class_name->u = eustrndup(ce->name.u, ce->name_length);
1228 4378 : return SUCCESS;
1229 : }
1230 : /* }}} */
1231 :
1232 : ZEND_API int zend_std_cast_object_tostring(zval *readobj, zval *writeobj, int type, void *extra TSRMLS_DC) /* {{{ */
1233 178162 : {
1234 : zval *retval;
1235 : zend_class_entry *ce;
1236 : UConverter *conv;
1237 :
1238 178162 : switch (type) {
1239 : case IS_STRING:
1240 : case IS_UNICODE:
1241 101554 : if (extra) {
1242 85 : conv = (UConverter *) extra;
1243 : } else {
1244 101469 : conv = ZEND_U_CONVERTER(UG(runtime_encoding_conv));
1245 : }
1246 101554 : ce = Z_OBJCE_P(readobj);
1247 101554 : if (ce->__tostring &&
1248 : (zend_call_method_with_0_params(&readobj, ce, &ce->__tostring, "__tostring", &retval) || EG(exception))) {
1249 100999 : if (EG(exception)) {
1250 3 : if (retval) {
1251 0 : zval_ptr_dtor(&retval);
1252 : }
1253 3 : zend_error(E_ERROR, "Method %v::__toString() must not throw an exception", ce->name);
1254 0 : return FAILURE;
1255 : }
1256 100996 : if (Z_TYPE_P(retval) == IS_UNICODE || Z_TYPE_P(retval) == IS_STRING) {
1257 100992 : INIT_PZVAL(writeobj);
1258 100992 : if (readobj == writeobj) {
1259 100231 : zval_dtor(readobj);
1260 : }
1261 100992 : ZVAL_ZVAL(writeobj, retval, 1, 1);
1262 100992 : if (Z_TYPE_P(writeobj) != type) {
1263 157 : if (type == IS_UNICODE) {
1264 19 : convert_to_unicode_with_converter(writeobj, conv);
1265 : } else {
1266 138 : convert_to_string_with_converter(writeobj, conv);
1267 : }
1268 : }
1269 100992 : return SUCCESS;
1270 : } else {
1271 4 : zval_ptr_dtor(&retval);
1272 4 : INIT_PZVAL(writeobj);
1273 4 : if (readobj == writeobj) {
1274 0 : zval_dtor(readobj);
1275 : }
1276 4 : if (type == IS_UNICODE) {
1277 4 : ZVAL_EMPTY_UNICODE(writeobj);
1278 : } else {
1279 0 : ZVAL_EMPTY_STRING(writeobj);
1280 : }
1281 4 : zend_error(E_RECOVERABLE_ERROR, "Method %v::__toString() must return a string value", ce->name);
1282 4 : return SUCCESS;
1283 : }
1284 : }
1285 555 : return FAILURE;
1286 : case IS_BOOL:
1287 76347 : INIT_PZVAL(writeobj);
1288 76347 : ZVAL_BOOL(writeobj, 1);
1289 76347 : return SUCCESS;
1290 : case IS_LONG:
1291 144 : ce = Z_OBJCE_P(readobj);
1292 144 : zend_error(E_NOTICE, "Object of class %v could not be converted to int", ce->name);
1293 144 : INIT_PZVAL(writeobj);
1294 144 : if (readobj == writeobj) {
1295 0 : zval_dtor(readobj);
1296 : }
1297 144 : ZVAL_LONG(writeobj, 1);
1298 144 : return SUCCESS;
1299 : case IS_DOUBLE:
1300 49 : ce = Z_OBJCE_P(readobj);
1301 49 : zend_error(E_NOTICE, "Object of class %v could not be converted to double", ce->name);
1302 49 : INIT_PZVAL(writeobj);
1303 49 : if (readobj == writeobj) {
1304 0 : zval_dtor(writeobj);
1305 : }
1306 49 : ZVAL_DOUBLE(writeobj, 1);
1307 49 : return SUCCESS;
1308 : default:
1309 68 : INIT_PZVAL(writeobj);
1310 68 : Z_TYPE_P(writeobj) = IS_NULL;
1311 : break;
1312 : }
1313 68 : return FAILURE;
1314 : }
1315 : /* }}} */
1316 :
1317 : int zend_std_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zval **zobj_ptr TSRMLS_DC) /* {{{ */
1318 67 : {
1319 : zstr key;
1320 67 : zend_uchar utype = IS_UNICODE;
1321 : zend_class_entry *ce;
1322 :
1323 67 : if (Z_TYPE_P(obj) != IS_OBJECT) {
1324 0 : return FAILURE;
1325 : }
1326 :
1327 67 : ce = Z_OBJCE_P(obj);
1328 :
1329 67 : if (utype == IS_UNICODE) {
1330 67 : key.u = USTR_MAKE(ZEND_INVOKE_FUNC_NAME);
1331 : } else {
1332 0 : key.s = ZEND_INVOKE_FUNC_NAME;
1333 : }
1334 :
1335 67 : if (zend_u_hash_find(&ce->function_table, utype, key, sizeof(ZEND_INVOKE_FUNC_NAME), (void**)fptr_ptr) == FAILURE) {
1336 50 : if (utype == IS_UNICODE) {
1337 50 : efree(key.u);
1338 : }
1339 50 : return FAILURE;
1340 : }
1341 :
1342 17 : *ce_ptr = ce;
1343 17 : if ((*fptr_ptr)->common.fn_flags & ZEND_ACC_STATIC) {
1344 0 : if (zobj_ptr) {
1345 0 : *zobj_ptr = NULL;
1346 : }
1347 : } else {
1348 17 : if (zobj_ptr) {
1349 17 : *zobj_ptr = obj;
1350 : }
1351 : }
1352 :
1353 17 : if (utype == IS_UNICODE) {
1354 17 : efree(key.u);
1355 : }
1356 :
1357 17 : return SUCCESS;
1358 : }
1359 : /* }}} */
1360 :
1361 : ZEND_API zend_object_handlers std_object_handlers = {
1362 : zend_objects_store_add_ref, /* add_ref */
1363 : zend_objects_store_del_ref, /* del_ref */
1364 : zend_objects_clone_obj, /* clone_obj */
1365 :
1366 : zend_std_read_property, /* read_property */
1367 : zend_std_write_property, /* write_property */
1368 : zend_std_read_dimension, /* read_dimension */
1369 : zend_std_write_dimension, /* write_dimension */
1370 : zend_std_get_property_ptr_ptr, /* get_property_ptr_ptr */
1371 : NULL, /* get */
1372 : NULL, /* set */
1373 : zend_std_has_property, /* has_property */
1374 : zend_std_unset_property, /* unset_property */
1375 : zend_std_has_dimension, /* has_dimension */
1376 : zend_std_unset_dimension, /* unset_dimension */
1377 : zend_std_get_properties, /* get_properties */
1378 : zend_std_get_method, /* get_method */
1379 : NULL, /* call_method */
1380 : zend_std_get_constructor, /* get_constructor */
1381 : zend_std_object_get_class, /* get_class_entry */
1382 : zend_std_object_get_class_name, /* get_class_name */
1383 : zend_std_compare_objects, /* compare_objects */
1384 : zend_std_cast_object_tostring, /* cast_object */
1385 : NULL, /* count_elements */
1386 : NULL, /* get_debug_info */
1387 : zend_std_get_closure, /* get_closure */
1388 : };
1389 :
1390 : /*
1391 : * Local variables:
1392 : * tab-width: 4
1393 : * c-basic-offset: 4
1394 : * indent-tabs-mode: t
1395 : * End:
1396 : */
|