incomplete_class.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Sascha Schumann <sascha@schumann.cx> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php.h"
  19. #include "basic_functions.h"
  20. #include "php_incomplete_class.h"
  21. #define INCOMPLETE_CLASS_MSG \
  22. "The script tried to execute a method or " \
  23. "access a property of an incomplete object. " \
  24. "Please ensure that the class definition \"%s\" of the object " \
  25. "you are trying to operate on was loaded _before_ " \
  26. "unserialize() gets called or provide an autoloader " \
  27. "to load the class definition"
  28. static zend_object_handlers php_incomplete_object_handlers;
  29. /* {{{ incomplete_class_message
  30. */
  31. static void incomplete_class_message(zval *object, int error_type)
  32. {
  33. zend_string *class_name;
  34. class_name = php_lookup_class_name(object);
  35. if (class_name) {
  36. php_error_docref(NULL, error_type, INCOMPLETE_CLASS_MSG, ZSTR_VAL(class_name));
  37. zend_string_release_ex(class_name, 0);
  38. } else {
  39. php_error_docref(NULL, error_type, INCOMPLETE_CLASS_MSG, "unknown");
  40. }
  41. }
  42. /* }}} */
  43. static zval *incomplete_class_get_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) /* {{{ */
  44. {
  45. incomplete_class_message(object, E_NOTICE);
  46. if (type == BP_VAR_W || type == BP_VAR_RW) {
  47. ZVAL_ERROR(rv);
  48. return rv;
  49. } else {
  50. return &EG(uninitialized_zval);
  51. }
  52. }
  53. /* }}} */
  54. static void incomplete_class_write_property(zval *object, zval *member, zval *value, void **cache_slot) /* {{{ */
  55. {
  56. incomplete_class_message(object, E_NOTICE);
  57. }
  58. /* }}} */
  59. static zval *incomplete_class_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) /* {{{ */
  60. {
  61. incomplete_class_message(object, E_NOTICE);
  62. return &EG(error_zval);
  63. }
  64. /* }}} */
  65. static void incomplete_class_unset_property(zval *object, zval *member, void **cache_slot) /* {{{ */
  66. {
  67. incomplete_class_message(object, E_NOTICE);
  68. }
  69. /* }}} */
  70. static int incomplete_class_has_property(zval *object, zval *member, int check_empty, void **cache_slot) /* {{{ */
  71. {
  72. incomplete_class_message(object, E_NOTICE);
  73. return 0;
  74. }
  75. /* }}} */
  76. static union _zend_function *incomplete_class_get_method(zend_object **object, zend_string *method, const zval *key) /* {{{ */
  77. {
  78. zval zobject;
  79. ZVAL_OBJ(&zobject, *object);
  80. incomplete_class_message(&zobject, E_ERROR);
  81. return NULL;
  82. }
  83. /* }}} */
  84. /* {{{ php_create_incomplete_class
  85. */
  86. static zend_object *php_create_incomplete_object(zend_class_entry *class_type)
  87. {
  88. zend_object *object;
  89. object = zend_objects_new( class_type);
  90. object->handlers = &php_incomplete_object_handlers;
  91. object_properties_init(object, class_type);
  92. return object;
  93. }
  94. PHPAPI zend_class_entry *php_create_incomplete_class(void)
  95. {
  96. zend_class_entry incomplete_class;
  97. INIT_CLASS_ENTRY(incomplete_class, INCOMPLETE_CLASS, NULL);
  98. incomplete_class.create_object = php_create_incomplete_object;
  99. memcpy(&php_incomplete_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  100. php_incomplete_object_handlers.read_property = incomplete_class_get_property;
  101. php_incomplete_object_handlers.has_property = incomplete_class_has_property;
  102. php_incomplete_object_handlers.unset_property = incomplete_class_unset_property;
  103. php_incomplete_object_handlers.write_property = incomplete_class_write_property;
  104. php_incomplete_object_handlers.get_property_ptr_ptr = incomplete_class_get_property_ptr_ptr;
  105. php_incomplete_object_handlers.get_method = incomplete_class_get_method;
  106. return zend_register_internal_class(&incomplete_class);
  107. }
  108. /* }}} */
  109. /* {{{ php_lookup_class_name
  110. */
  111. PHPAPI zend_string *php_lookup_class_name(zval *object)
  112. {
  113. zval *val;
  114. HashTable *object_properties;
  115. object_properties = Z_OBJPROP_P(object);
  116. if ((val = zend_hash_str_find(object_properties, MAGIC_MEMBER, sizeof(MAGIC_MEMBER)-1)) != NULL && Z_TYPE_P(val) == IS_STRING) {
  117. return zend_string_copy(Z_STR_P(val));
  118. }
  119. return NULL;
  120. }
  121. /* }}} */
  122. /* {{{ php_store_class_name
  123. */
  124. PHPAPI void php_store_class_name(zval *object, const char *name, size_t len)
  125. {
  126. zval val;
  127. ZVAL_STRINGL(&val, name, len);
  128. zend_hash_str_update(Z_OBJPROP_P(object), MAGIC_MEMBER, sizeof(MAGIC_MEMBER)-1, &val);
  129. }
  130. /* }}} */
  131. /*
  132. * Local variables:
  133. * tab-width: 4
  134. * c-basic-offset: 4
  135. * End:
  136. * vim600: sw=4 ts=4 fdm=marker
  137. * vim<600: sw=4 ts=4
  138. */