1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Sascha Schumann <sascha@schumann.cx> |
14 +----------------------------------------------------------------------+
15 */
16
17 #include "php.h"
18 #include "basic_functions.h"
19 #include "php_incomplete_class.h"
20
21 #define INCOMPLETE_CLASS_MSG \
22 "The script tried to %s on an incomplete object. " \
23 "Please ensure that the class definition \"%s\" of the object " \
24 "you are trying to operate on was loaded _before_ " \
25 "unserialize() gets called or provide an autoloader " \
26 "to load the class definition"
27
28 PHPAPI zend_class_entry *php_ce_incomplete_class;
29 static zend_object_handlers php_incomplete_object_handlers;
30
incomplete_class_message(zend_object * object)31 static void incomplete_class_message(zend_object *object)
32 {
33 zend_string *class_name = php_lookup_class_name(object);
34 php_error_docref(NULL, E_WARNING, INCOMPLETE_CLASS_MSG,
35 "access a property", class_name ? ZSTR_VAL(class_name) : "unknown");
36 if (class_name) {
37 zend_string_release_ex(class_name, 0);
38 }
39 }
40
throw_incomplete_class_error(zend_object * object,const char * what)41 static void throw_incomplete_class_error(zend_object *object, const char *what)
42 {
43 zend_string *class_name = php_lookup_class_name(object);
44 zend_throw_error(NULL, INCOMPLETE_CLASS_MSG,
45 what, class_name ? ZSTR_VAL(class_name) : "unknown");
46 if (class_name) {
47 zend_string_release_ex(class_name, 0);
48 }
49 }
50
incomplete_class_get_property(zend_object * object,zend_string * member,int type,void ** cache_slot,zval * rv)51 static zval *incomplete_class_get_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv) /* {{{ */
52 {
53 incomplete_class_message(object);
54
55 if (type == BP_VAR_W || type == BP_VAR_RW) {
56 ZVAL_ERROR(rv);
57 return rv;
58 } else {
59 return &EG(uninitialized_zval);
60 }
61 }
62 /* }}} */
63
incomplete_class_write_property(zend_object * object,zend_string * member,zval * value,void ** cache_slot)64 static zval *incomplete_class_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot) /* {{{ */
65 {
66 throw_incomplete_class_error(object, "modify a property");
67 return value;
68 }
69 /* }}} */
70
incomplete_class_get_property_ptr_ptr(zend_object * object,zend_string * member,int type,void ** cache_slot)71 static zval *incomplete_class_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot) /* {{{ */
72 {
73 throw_incomplete_class_error(object, "modify a property");
74 return &EG(error_zval);
75 }
76 /* }}} */
77
incomplete_class_unset_property(zend_object * object,zend_string * member,void ** cache_slot)78 static void incomplete_class_unset_property(zend_object *object, zend_string *member, void **cache_slot) /* {{{ */
79 {
80 throw_incomplete_class_error(object, "modify a property");
81 }
82 /* }}} */
83
incomplete_class_has_property(zend_object * object,zend_string * member,int check_empty,void ** cache_slot)84 static int incomplete_class_has_property(zend_object *object, zend_string *member, int check_empty, void **cache_slot) /* {{{ */
85 {
86 incomplete_class_message(object);
87 return 0;
88 }
89 /* }}} */
90
incomplete_class_get_method(zend_object ** object,zend_string * method,const zval * key)91 static zend_function *incomplete_class_get_method(zend_object **object, zend_string *method, const zval *key) /* {{{ */
92 {
93 throw_incomplete_class_error(*object, "call a method");
94 return NULL;
95 }
96 /* }}} */
97
98 /* {{{ php_create_incomplete_class */
php_create_incomplete_object(zend_class_entry * class_type)99 static zend_object *php_create_incomplete_object(zend_class_entry *class_type)
100 {
101 zend_object *object;
102
103 object = zend_objects_new( class_type);
104 object->handlers = &php_incomplete_object_handlers;
105
106 object_properties_init(object, class_type);
107
108 return object;
109 }
110
php_register_incomplete_class(void)111 PHPAPI void php_register_incomplete_class(void)
112 {
113 zend_class_entry incomplete_class;
114
115 INIT_CLASS_ENTRY(incomplete_class, INCOMPLETE_CLASS, NULL);
116
117 incomplete_class.create_object = php_create_incomplete_object;
118
119 memcpy(&php_incomplete_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
120 php_incomplete_object_handlers.read_property = incomplete_class_get_property;
121 php_incomplete_object_handlers.has_property = incomplete_class_has_property;
122 php_incomplete_object_handlers.unset_property = incomplete_class_unset_property;
123 php_incomplete_object_handlers.write_property = incomplete_class_write_property;
124 php_incomplete_object_handlers.get_property_ptr_ptr = incomplete_class_get_property_ptr_ptr;
125 php_incomplete_object_handlers.get_method = incomplete_class_get_method;
126
127 php_ce_incomplete_class = zend_register_internal_class(&incomplete_class);
128 php_ce_incomplete_class->ce_flags |= ZEND_ACC_FINAL;
129 }
130 /* }}} */
131
132 /* {{{ php_lookup_class_name */
php_lookup_class_name(zend_object * object)133 PHPAPI zend_string *php_lookup_class_name(zend_object *object)
134 {
135 if (object->properties) {
136 zval *val = zend_hash_str_find(object->properties, MAGIC_MEMBER, sizeof(MAGIC_MEMBER)-1);
137
138 if (val != NULL && Z_TYPE_P(val) == IS_STRING) {
139 return zend_string_copy(Z_STR_P(val));
140 }
141 }
142
143 return NULL;
144 }
145 /* }}} */
146
147 /* {{{ php_store_class_name */
php_store_class_name(zval * object,zend_string * name)148 PHPAPI void php_store_class_name(zval *object, zend_string *name)
149 {
150 zval val;
151
152 ZVAL_STR_COPY(&val, name);
153 zend_hash_str_update(Z_OBJPROP_P(object), MAGIC_MEMBER, sizeof(MAGIC_MEMBER)-1, &val);
154 }
155 /* }}} */
156