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 | https://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_handlers(void)111 PHPAPI void php_register_incomplete_class_handlers(void)
112 {
113 memcpy(&php_incomplete_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
114 php_incomplete_object_handlers.read_property = incomplete_class_get_property;
115 php_incomplete_object_handlers.has_property = incomplete_class_has_property;
116 php_incomplete_object_handlers.unset_property = incomplete_class_unset_property;
117 php_incomplete_object_handlers.write_property = incomplete_class_write_property;
118 php_incomplete_object_handlers.get_property_ptr_ptr = incomplete_class_get_property_ptr_ptr;
119 php_incomplete_object_handlers.get_method = incomplete_class_get_method;
120
121 php_ce_incomplete_class->create_object = php_create_incomplete_object;
122 }
123 /* }}} */
124
125 /* {{{ php_lookup_class_name */
php_lookup_class_name(zend_object * object)126 PHPAPI zend_string *php_lookup_class_name(zend_object *object)
127 {
128 if (object->properties) {
129 zval *val = zend_hash_str_find(object->properties, MAGIC_MEMBER, sizeof(MAGIC_MEMBER)-1);
130
131 if (val != NULL && Z_TYPE_P(val) == IS_STRING) {
132 return zend_string_copy(Z_STR_P(val));
133 }
134 }
135
136 return NULL;
137 }
138 /* }}} */
139
140 /* {{{ php_store_class_name */
php_store_class_name(zval * object,zend_string * name)141 PHPAPI void php_store_class_name(zval *object, zend_string *name)
142 {
143 zval val;
144
145 ZVAL_STR_COPY(&val, name);
146 zend_hash_str_update(Z_OBJPROP_P(object), MAGIC_MEMBER, sizeof(MAGIC_MEMBER)-1, &val);
147 }
148 /* }}} */
149