incomplete_class.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. /* $Id$ */
  19. #include "php.h"
  20. #include "basic_functions.h"
  21. #include "php_incomplete_class.h"
  22. #define INCOMPLETE_CLASS_MSG \
  23. "The script tried to execute a method or " \
  24. "access a property of an incomplete object. " \
  25. "Please ensure that the class definition \"%s\" of the object " \
  26. "you are trying to operate on was loaded _before_ " \
  27. "unserialize() gets called or provide a __autoload() function " \
  28. "to load the class definition "
  29. static zend_object_handlers php_incomplete_object_handlers;
  30. /* {{{ incomplete_class_message
  31. */
  32. static void incomplete_class_message(zval *object, int error_type TSRMLS_DC)
  33. {
  34. char *class_name;
  35. zend_bool class_name_alloced = 1;
  36. class_name = php_lookup_class_name(object, NULL);
  37. if (!class_name) {
  38. class_name_alloced = 0;
  39. class_name = "unknown";
  40. }
  41. php_error_docref(NULL TSRMLS_CC, error_type, INCOMPLETE_CLASS_MSG, class_name);
  42. if (class_name_alloced) {
  43. efree(class_name);
  44. }
  45. }
  46. /* }}} */
  47. static zval *incomplete_class_get_property(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) /* {{{ */
  48. {
  49. incomplete_class_message(object, E_NOTICE TSRMLS_CC);
  50. if (type == BP_VAR_W || type == BP_VAR_RW) {
  51. return EG(error_zval_ptr);
  52. } else {
  53. return EG(uninitialized_zval_ptr);
  54. }
  55. }
  56. /* }}} */
  57. static void incomplete_class_write_property(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC) /* {{{ */
  58. {
  59. incomplete_class_message(object, E_NOTICE TSRMLS_CC);
  60. }
  61. /* }}} */
  62. static zval **incomplete_class_get_property_ptr_ptr(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) /* {{{ */
  63. {
  64. incomplete_class_message(object, E_NOTICE TSRMLS_CC);
  65. return &EG(error_zval_ptr);
  66. }
  67. /* }}} */
  68. static void incomplete_class_unset_property(zval *object, zval *member, const zend_literal *key TSRMLS_DC) /* {{{ */
  69. {
  70. incomplete_class_message(object, E_NOTICE TSRMLS_CC);
  71. }
  72. /* }}} */
  73. static int incomplete_class_has_property(zval *object, zval *member, int check_empty, const zend_literal *key TSRMLS_DC) /* {{{ */
  74. {
  75. incomplete_class_message(object, E_NOTICE TSRMLS_CC);
  76. return 0;
  77. }
  78. /* }}} */
  79. static union _zend_function *incomplete_class_get_method(zval **object, char *method, int method_len, const zend_literal *key TSRMLS_DC) /* {{{ */
  80. {
  81. incomplete_class_message(*object, E_ERROR TSRMLS_CC);
  82. return NULL;
  83. }
  84. /* }}} */
  85. /* {{{ php_create_incomplete_class
  86. */
  87. static zend_object_value php_create_incomplete_object(zend_class_entry *class_type TSRMLS_DC)
  88. {
  89. zend_object *object;
  90. zend_object_value value;
  91. value = zend_objects_new(&object, class_type TSRMLS_CC);
  92. value.handlers = &php_incomplete_object_handlers;
  93. object_properties_init(object, class_type);
  94. return value;
  95. }
  96. PHPAPI zend_class_entry *php_create_incomplete_class(TSRMLS_D)
  97. {
  98. zend_class_entry incomplete_class;
  99. INIT_CLASS_ENTRY(incomplete_class, INCOMPLETE_CLASS, NULL);
  100. incomplete_class.create_object = php_create_incomplete_object;
  101. memcpy(&php_incomplete_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  102. php_incomplete_object_handlers.read_property = incomplete_class_get_property;
  103. php_incomplete_object_handlers.has_property = incomplete_class_has_property;
  104. php_incomplete_object_handlers.unset_property = incomplete_class_unset_property;
  105. php_incomplete_object_handlers.write_property = incomplete_class_write_property;
  106. php_incomplete_object_handlers.get_property_ptr_ptr = incomplete_class_get_property_ptr_ptr;
  107. php_incomplete_object_handlers.get_method = incomplete_class_get_method;
  108. return zend_register_internal_class(&incomplete_class TSRMLS_CC);
  109. }
  110. /* }}} */
  111. /* {{{ php_lookup_class_name
  112. */
  113. PHPAPI char *php_lookup_class_name(zval *object, zend_uint *nlen)
  114. {
  115. zval **val;
  116. char *retval = NULL;
  117. HashTable *object_properties;
  118. TSRMLS_FETCH();
  119. object_properties = Z_OBJPROP_P(object);
  120. if (zend_hash_find(object_properties, MAGIC_MEMBER, sizeof(MAGIC_MEMBER), (void **) &val) == SUCCESS && Z_TYPE_PP(val) == IS_STRING) {
  121. retval = estrndup(Z_STRVAL_PP(val), Z_STRLEN_PP(val));
  122. if (nlen) {
  123. *nlen = Z_STRLEN_PP(val);
  124. }
  125. }
  126. return retval;
  127. }
  128. /* }}} */
  129. /* {{{ php_store_class_name
  130. */
  131. PHPAPI void php_store_class_name(zval *object, const char *name, zend_uint len)
  132. {
  133. zval *val;
  134. TSRMLS_FETCH();
  135. MAKE_STD_ZVAL(val);
  136. Z_TYPE_P(val) = IS_STRING;
  137. Z_STRVAL_P(val) = estrndup(name, len);
  138. Z_STRLEN_P(val) = len;
  139. zend_hash_update(Z_OBJPROP_P(object), MAGIC_MEMBER, sizeof(MAGIC_MEMBER), &val, sizeof(val), NULL);
  140. }
  141. /* }}} */
  142. /*
  143. * Local variables:
  144. * tab-width: 4
  145. * c-basic-offset: 4
  146. * End:
  147. * vim600: sw=4 ts=4 fdm=marker
  148. * vim<600: sw=4 ts=4
  149. */