incomplete_class.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #include "php.h"
  17. #include "basic_functions.h"
  18. #include "php_incomplete_class.h"
  19. #define INCOMPLETE_CLASS_MSG \
  20. "The script tried to %s on an incomplete object. " \
  21. "Please ensure that the class definition \"%s\" of the object " \
  22. "you are trying to operate on was loaded _before_ " \
  23. "unserialize() gets called or provide an autoloader " \
  24. "to load the class definition"
  25. PHPAPI zend_class_entry *php_ce_incomplete_class;
  26. static zend_object_handlers php_incomplete_object_handlers;
  27. static void incomplete_class_message(zend_object *object)
  28. {
  29. zend_string *class_name = php_lookup_class_name(object);
  30. php_error_docref(NULL, E_WARNING, INCOMPLETE_CLASS_MSG,
  31. "access a property", class_name ? ZSTR_VAL(class_name) : "unknown");
  32. if (class_name) {
  33. zend_string_release_ex(class_name, 0);
  34. }
  35. }
  36. static void throw_incomplete_class_error(zend_object *object, const char *what)
  37. {
  38. zend_string *class_name = php_lookup_class_name(object);
  39. zend_throw_error(NULL, INCOMPLETE_CLASS_MSG,
  40. what, class_name ? ZSTR_VAL(class_name) : "unknown");
  41. if (class_name) {
  42. zend_string_release_ex(class_name, 0);
  43. }
  44. }
  45. static zval *incomplete_class_get_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv) /* {{{ */
  46. {
  47. incomplete_class_message(object);
  48. if (type == BP_VAR_W || type == BP_VAR_RW) {
  49. ZVAL_ERROR(rv);
  50. return rv;
  51. } else {
  52. return &EG(uninitialized_zval);
  53. }
  54. }
  55. /* }}} */
  56. static zval *incomplete_class_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot) /* {{{ */
  57. {
  58. throw_incomplete_class_error(object, "modify a property");
  59. return value;
  60. }
  61. /* }}} */
  62. static zval *incomplete_class_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot) /* {{{ */
  63. {
  64. throw_incomplete_class_error(object, "modify a property");
  65. return &EG(error_zval);
  66. }
  67. /* }}} */
  68. static void incomplete_class_unset_property(zend_object *object, zend_string *member, void **cache_slot) /* {{{ */
  69. {
  70. throw_incomplete_class_error(object, "modify a property");
  71. }
  72. /* }}} */
  73. static int incomplete_class_has_property(zend_object *object, zend_string *member, int check_empty, void **cache_slot) /* {{{ */
  74. {
  75. incomplete_class_message(object);
  76. return 0;
  77. }
  78. /* }}} */
  79. static zend_function *incomplete_class_get_method(zend_object **object, zend_string *method, const zval *key) /* {{{ */
  80. {
  81. throw_incomplete_class_error(*object, "call a method");
  82. return NULL;
  83. }
  84. /* }}} */
  85. /* {{{ php_create_incomplete_class */
  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 void php_register_incomplete_class_handlers(void)
  95. {
  96. memcpy(&php_incomplete_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  97. php_incomplete_object_handlers.read_property = incomplete_class_get_property;
  98. php_incomplete_object_handlers.has_property = incomplete_class_has_property;
  99. php_incomplete_object_handlers.unset_property = incomplete_class_unset_property;
  100. php_incomplete_object_handlers.write_property = incomplete_class_write_property;
  101. php_incomplete_object_handlers.get_property_ptr_ptr = incomplete_class_get_property_ptr_ptr;
  102. php_incomplete_object_handlers.get_method = incomplete_class_get_method;
  103. php_ce_incomplete_class->create_object = php_create_incomplete_object;
  104. }
  105. /* }}} */
  106. /* {{{ php_lookup_class_name */
  107. PHPAPI zend_string *php_lookup_class_name(zend_object *object)
  108. {
  109. if (object->properties) {
  110. zval *val = zend_hash_str_find(object->properties, MAGIC_MEMBER, sizeof(MAGIC_MEMBER)-1);
  111. if (val != NULL && Z_TYPE_P(val) == IS_STRING) {
  112. return zend_string_copy(Z_STR_P(val));
  113. }
  114. }
  115. return NULL;
  116. }
  117. /* }}} */
  118. /* {{{ php_store_class_name */
  119. PHPAPI void php_store_class_name(zval *object, zend_string *name)
  120. {
  121. zval val;
  122. ZVAL_STR_COPY(&val, name);
  123. zend_hash_str_update(Z_OBJPROP_P(object), MAGIC_MEMBER, sizeof(MAGIC_MEMBER)-1, &val);
  124. }
  125. /* }}} */