/** * Checks whether obj is an object and updates property with another zval */ int zephir_update_property_zval(zval *object, const char *property_name, unsigned int property_length, zval *value) { zend_class_entry *ce, *old_scope; zval property; old_scope = EG(scope); if (Z_TYPE_P(object) != IS_OBJECT) { php_error_docref(NULL, E_WARNING, "Attempt to assign property of non-object"); return FAILURE; } ce = Z_OBJCE_P(object); if (ce->parent) { ce = zephir_lookup_class_ce(ce, property_name, property_length); } EG(scope) = ce; if (!Z_OBJ_HT_P(object)->write_property) { const char *class_name; class_name = Z_OBJ_P(object) ? ZSTR_VAL(Z_OBJCE_P(object)->name) : ""; zend_error(E_CORE_ERROR, "Property %s of class %s cannot be updated", property_name, class_name); } ZVAL_STRINGL(&property, property_name, property_length); /* write_property will add 1 to refcount, so no Z_TRY_ADDREF_P(value); is necessary */ Z_OBJ_HT_P(object)->write_property(object, &property, value, 0); zval_ptr_dtor(&property); EG(scope) = old_scope; return SUCCESS; }
/** * Reads a property from an object */ int zephir_read_property(zval *result, zval *object, const char *property_name, zend_uint property_length, int flags) { zval property; zend_class_entry *ce, *old_scope; zval tmp; zval *res; ZVAL_UNDEF(&tmp); if (Z_TYPE_P(object) != IS_OBJECT) { if ((flags & PH_NOISY) == PH_NOISY) { php_error_docref(NULL, E_NOTICE, "Trying to get property \"%s\" of non-object", property_name); } ZVAL_NULL(result); return FAILURE; } ce = Z_OBJCE_P(object); if (ce->parent) { ce = zephir_lookup_class_ce(ce, property_name, property_length); } #if PHP_VERSION_ID >= 70100 old_scope = EG(fake_scope); EG(fake_scope) = ce; #else old_scope = EG(scope); EG(scope) = ce; #endif if (!Z_OBJ_HT_P(object)->read_property) { const char *class_name; class_name = Z_OBJ_P(object) ? ZSTR_VAL(Z_OBJCE_P(object)->name) : ""; zend_error(E_CORE_ERROR, "Property %s of class %s cannot be read", property_name, class_name); } ZVAL_STRINGL(&property, property_name, property_length); res = Z_OBJ_HT_P(object)->read_property(object, &property, flags ? BP_VAR_IS : BP_VAR_R, NULL, &tmp); if ((flags & PH_READONLY) == PH_READONLY) { ZVAL_COPY_VALUE(result, res); } else { ZVAL_COPY(result, res); } zval_ptr_dtor(&property); #if PHP_VERSION_ID >= 70100 EG(fake_scope) = old_scope; #else EG(scope) = old_scope; #endif return SUCCESS; }
int zephir_unset_property(zval* object, const char* name) { if (Z_TYPE_P(object) == IS_OBJECT) { zval member; zend_class_entry *old_scope; ZVAL_STRING(&member, name); #if PHP_VERSION_ID >= 70100 old_scope = EG(fake_scope); EG(fake_scope) = Z_OBJCE_P(object); #else old_scope = EG(scope); EG(scope) = Z_OBJCE_P(object); #endif Z_OBJ_HT_P(object)->unset_property(object, &member, 0); #if PHP_VERSION_ID >= 70100 EG(fake_scope) = old_scope; #else EG(scope) = old_scope; #endif return SUCCESS; } return FAILURE; }
/** * Clones an object from obj to destination */ int zephir_clone(zval *destination, zval *obj) { int status = SUCCESS; zend_class_entry *ce; zend_object_clone_obj_t clone_call; if (Z_TYPE_P(obj) != IS_OBJECT) { php_error_docref(NULL, E_ERROR, "__clone method called on non-object"); status = FAILURE; } else { ce = Z_OBJCE_P(obj); clone_call = Z_OBJ_HT_P(obj)->clone_obj; if (!clone_call) { if (ce) { php_error_docref(NULL, E_ERROR, "Trying to clone an uncloneable object of class %s", ce->name); } else { php_error_docref(NULL, E_ERROR, "Trying to clone an uncloneable object"); } status = FAILURE; } else { if (!EG(exception)) { //zval_ptr_dtor(destination); ZVAL_OBJ(destination, clone_call(obj)); if (EG(exception)) { zval_ptr_dtor(destination); } } } } return status; }
void uopz_set_property(zval *object, zval *member, zval *value) { /* {{{ */ zend_class_entry *scope = uopz_get_scope(1); zend_class_entry *ce = Z_OBJCE_P(object); zend_property_info *info; do { uopz_set_scope(ce); info = zend_get_property_info(ce, Z_STR_P(member), 1); if (info && info != ZEND_WRONG_PROPERTY_INFO) { break; } ce = ce->parent; } while (ce); if (info && info != ZEND_WRONG_PROPERTY_INFO) { uopz_set_scope(info->ce); } else { uopz_set_scope(Z_OBJCE_P(object)); } Z_OBJ_HT_P(object) ->write_property(object, member, value, NULL); uopz_set_scope(scope); } /* }}} */
void uopz_get_property(zval *object, zval *member, zval *value) { /* {{{ */ zend_class_entry *scope = uopz_get_scope(1); zend_class_entry *ce = Z_OBJCE_P(object); zend_property_info *info; zval *prop, rv; do { uopz_set_scope(ce); info = zend_get_property_info(ce, Z_STR_P(member), 1); if (info && info != ZEND_WRONG_PROPERTY_INFO) { break; } ce = ce->parent; } while (ce); if (info && info != ZEND_WRONG_PROPERTY_INFO) { uopz_set_scope(info->ce); } else { uopz_set_scope(Z_OBJCE_P(object)); } prop = Z_OBJ_HT_P(object) ->read_property(object, member, BP_VAR_R, NULL, &rv); uopz_set_scope(scope); if (!prop) { return; } ZVAL_COPY(value, prop); } /* }}} */
static inline void ast_update_property(zval *object, zend_string *name, zval *value, void **cache_slot) { zval name_zv; ZVAL_STR(&name_zv, name); Z_TRY_DELREF_P(value); Z_OBJ_HT_P(object)->write_property(object, &name_zv, value, cache_slot); }
ZEND_RESULT_CODE php_http_object_method_call(php_http_object_method_t *cb, zval *zobject, zval *retval_ptr, int argc, zval *args) { ZEND_RESULT_CODE rv; zval retval; ZVAL_UNDEF(&retval); Z_ADDREF_P(zobject); cb->fci.object = Z_OBJ_P(zobject); cb->fcc.object = Z_OBJ_P(zobject); cb->fci.retval = retval_ptr ? retval_ptr : &retval; cb->fci.param_count = argc; cb->fci.params = args; if (cb->fcc.called_scope != Z_OBJCE_P(zobject)) { cb->fcc.called_scope = Z_OBJCE_P(zobject); cb->fcc.function_handler = Z_OBJ_HT_P(zobject)->get_method(&Z_OBJ_P(zobject), Z_STR(cb->fci.function_name), NULL); } rv = zend_call_function(&cb->fci, &cb->fcc); zval_ptr_dtor(zobject); if (!retval_ptr && !Z_ISUNDEF(retval)) { zval_ptr_dtor(&retval); } return rv; }
/* {{{ proto string SolrUtils::digestJsonResponse(string jsonResponse) Digests the json response into a php serialize string. */ PHP_METHOD(SolrUtils, digestJsonResponse) { solr_char_t *jsonResponse = NULL; long jsonResponse_len = 0; unsigned char *raw_resp = NULL, *str_end = NULL; solr_string_t buffer; php_unserialize_data_t var_hash; size_t raw_res_length; int successful = 1; int json_translation_result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &jsonResponse, &jsonResponse_len) == FAILURE) { RETURN_FALSE; } memset(&buffer, 0, sizeof(solr_string_t)); json_translation_result = solr_json_to_php_native(&buffer, jsonResponse, jsonResponse_len TSRMLS_CC); if (json_translation_result > 0) { solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_1000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, solr_get_json_error_msg(json_translation_result)); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error in JSON->PHP conversion. JSON Error Code %d", json_translation_result); }else{ solr_sarray_to_sobject(&buffer TSRMLS_CC); } memset(&var_hash, 0, sizeof(php_unserialize_data_t)); PHP_VAR_UNSERIALIZE_INIT(var_hash); raw_resp = (unsigned char *) buffer.str; raw_res_length = buffer.len; str_end = (unsigned char *) (raw_resp + raw_res_length); if (!php_var_unserialize( &return_value, (const unsigned char **)&raw_resp, str_end, &var_hash TSRMLS_CC) ) { solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_1000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, SOLR_ERROR_1000_MSG); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error unserializing raw response."); successful = 0; } solr_string_free(&buffer); PHP_VAR_UNSERIALIZE_DESTROY(var_hash); if (successful) { /* Overriding the default object handlers */ Z_OBJ_HT_P(return_value) = &solr_object_handlers; } }
ZEND_API zend_object_iterator* zend_iterator_unwrap(zval *array_ptr) { if (Z_TYPE_P(array_ptr) && Z_OBJ_HT_P(array_ptr) == &iterator_object_handlers) { return (zend_object_iterator *)Z_OBJ_P(array_ptr); } return NULL; }
static int32_t qb_transfer_value_from_import_source(qb_interpreter_context *cxt, qb_variable *ivar, qb_import_scope *scope) { int32_t result = TRUE; if(!(ivar->flags & QB_VARIABLE_IMPORTED)) { USE_TSRM zval *zvalue = NULL, **p_zvalue = NULL; switch(scope->type) { case QB_IMPORT_SCOPE_GLOBAL: case QB_IMPORT_SCOPE_LEXICAL: { // copy value from symbol table zend_hash_quick_find(scope->symbol_table, ivar->name, ivar->name_length + 1, ivar->hash_value, (void **) &p_zvalue); } break; case QB_IMPORT_SCOPE_CLASS: { if(ivar->flags & QB_VARIABLE_CLASS_CONSTANT) { // static:: constants are treated like variables zend_class_entry *ce = scope->class_entry; zval **p_value; zend_hash_quick_find(&ce->constants_table, ivar->name, ivar->name_length + 1, ivar->hash_value, (void **) &p_value); } else { zend_class_entry *ce = scope->class_entry; p_zvalue = Z_CLASS_GET_PROP(ce, ivar->name, ivar->name_length); } } break; case QB_IMPORT_SCOPE_OBJECT: { // copy value from class instance zval *name = qb_string_to_zval(ivar->name, ivar->name_length TSRMLS_CC); zval *container = scope->object; p_zvalue = Z_OBJ_GET_PROP_PTR_PTR(container, name); if(!p_zvalue) { if(Z_OBJ_HT_P(container)->read_property) { zvalue = Z_OBJ_READ_PROP(container, name); } } } break; default: { } break; } if(p_zvalue) { zvalue = *p_zvalue; } if(qb_transfer_value_from_zval(scope->storage, ivar->address, (zvalue) ? zvalue : &zval_used_for_init, QB_TRANSFER_CAN_BORROW_MEMORY | QB_TRANSFER_CAN_AUTOVIVIFICATE)) { ivar->flags |= QB_VARIABLE_IMPORTED; ivar->value_pointer = p_zvalue; ivar->value = zvalue; if(!p_zvalue && zvalue) { // we got the zval from a getter function // need to up the reference count Z_ADDREF_P(zvalue); } } else { uint32_t line_id = qb_get_zend_line_id(TSRMLS_C); qb_append_exception_variable_name(ivar TSRMLS_CC); qb_set_exception_line_id(line_id TSRMLS_CC); result = FALSE; } } return result; }
/* {{{ proto string SolrUtils::digestXMLResponse(string xml_response [, int parse_mode]) Digests the xml response into a php serialize string. */ PHP_METHOD(SolrUtils, digestXmlResponse) { solr_char_t *xmlresponse = NULL; COMPAT_ARG_SIZE_T xmlresponse_len = 0; long int parse_mode = 0L; solr_string_t sbuilder; unsigned char *raw_resp = NULL, *str_end = NULL; size_t raw_res_length = 0; php_unserialize_data_t var_hash; int successful = 1; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &xmlresponse, &xmlresponse_len, &parse_mode) == FAILURE) { RETURN_FALSE; } if (!xmlresponse_len) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Raw response is empty"); RETURN_NULL(); } parse_mode = ((parse_mode < 0L) ? 0L : ((parse_mode > 1L) ? 1L : parse_mode)); memset(&sbuilder, 0, sizeof(solr_string_t)); solr_encode_generic_xml_response(&sbuilder, xmlresponse, xmlresponse_len, parse_mode TSRMLS_CC); if (sbuilder.str == NULL || sbuilder.len == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Raw response was not valid"); RETURN_NULL(); } memset(&var_hash, 0, sizeof(php_unserialize_data_t)); PHP_VAR_UNSERIALIZE_INIT(var_hash); raw_resp = (unsigned char *) sbuilder.str; raw_res_length = sbuilder.len; str_end = (unsigned char *) (raw_resp + raw_res_length); if (!php_var_unserialize(return_value, (const unsigned char **) &raw_resp, str_end, &var_hash TSRMLS_CC)) { solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_1000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, SOLR_ERROR_1000_MSG); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error unserializing raw response."); successful = 0; } PHP_VAR_UNSERIALIZE_DESTROY(var_hash); solr_string_free(&sbuilder); if (successful) { /* Overriding the default object handlers */ Z_OBJ_HT_P(return_value) = &solr_object_handlers; } }
PHP_METHOD(Phalcon_Forms_Manager, __construct) { zval z = {}; array_init(&z); phalcon_update_property_this(getThis(), SL("_forms"), &z); Z_OBJ_HT_P(getThis()) = &phalcon_forms_manager_object_handlers; }
/** * Counts how many properties were added to the row * * @return int */ PHP_METHOD(Phalcon_Mvc_Model_Row, count) { HashTable *properties; properties = Z_OBJ_HT_P(getThis())->get_properties(getThis()); if (properties) { RETURN_LONG(zend_hash_num_elements(properties)); } RETURN_LONG(0); }
SharedStringList KPHPObject::GetPropertyNames() { TSRMLS_FETCH(); SharedStringList filteredNames(new StringList()); // If there is no get_properties handler, return. Why this would // happen, I have no idea -- from zend_builtin_functions.c:2363 if (Z_OBJ_HT_P(object)->get_properties == NULL) return filteredNames; HashTable* properties = Z_OBJ_HT_P(object)->get_properties(object TSRMLS_CC); if (properties == NULL) return filteredNames; SharedStringList names(PHPUtils::GetHashKeys(properties)); // Get the internal zend_object*. zend_object* internal = reinterpret_cast<zend_object*>( zend_object_store_get_object(object TSRMLS_CC)); for (int i = 0; i < names->size(); i++) { std::string& name = *names->at(i); unsigned int nameLength = name.size(); if (!zend_check_property_access(internal, (char*) name.c_str(), nameLength-1 TSRMLS_CC) == SUCCESS) continue; char* unmangledPropertyName; char* className; zend_unmangle_property_name((char*) name.c_str(), nameLength-1, &className, &unmangledPropertyName); filteredNames->push_back(new std::string(unmangledPropertyName)); } SharedStringList methods(PHPUtils::GetClassMethods(Z_OBJCE_P(object) TSRMLS_CC)); for (size_t i = 0; i < methods->size(); i++) { filteredNames->push_back(methods->at(i)); } return filteredNames; }
PHP_METHOD(Phalcon_Forms_Manager, __construct) { zval *z; PHALCON_ALLOC_GHOST_ZVAL(z); array_init(z); phalcon_update_property_this(getThis(), SL("_forms"), z TSRMLS_CC); Z_OBJ_HT_P(getThis()) = &phalcon_forms_manager_object_handlers; }
/** * Checks if property exists on object */ int zephir_isset_property(zval *object, const char *property_name, unsigned int property_length) { if (Z_TYPE_P(object) == IS_OBJECT) { if (likely(zend_hash_str_exists(&Z_OBJCE_P(object)->properties_info, property_name, property_length))) { return 1; } return zend_hash_str_exists(Z_OBJ_HT_P(object)->get_properties(object), property_name, property_length); } return 0; }
static void init_generated_pool_once(TSRMLS_D) { if (generated_pool_php == NULL) { MAKE_STD_ZVAL(generated_pool_php); Z_TYPE_P(generated_pool_php) = IS_OBJECT; generated_pool = ALLOC(DescriptorPool); descriptor_pool_init_c_instance(generated_pool TSRMLS_CC); Z_OBJ_HANDLE_P(generated_pool_php) = zend_objects_store_put( generated_pool, NULL, (zend_objects_free_object_storage_t)descriptor_pool_free, NULL TSRMLS_CC); Z_OBJ_HT_P(generated_pool_php) = zend_get_std_object_handlers(); } }
/** * Returns the instance as an array representation * * @return array */ PHP_METHOD(Phalcon_Mvc_Model_Row, toArray) { HashTable *properties; properties = Z_OBJ_HT_P(getThis())->get_properties(getThis()); if (!properties) { RETURN_FALSE; } array_init_size(return_value, zend_hash_num_elements(properties)); zend_hash_copy(Z_ARRVAL_P(return_value), properties, (copy_ctor_func_t)zval_add_ref); }
PHP_METHOD(RegExp, __construct) { char *pattern; char modifier[] = {0}; size_t pattern_len, modifier_len = 0; if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &pattern, &pattern_len, &modifier, &modifier_len) == FAILURE) { return; } zval *this = getThis(); #if PHP_API_VERSION <= 20131106 add_property_stringl(this, "pattern", pattern, pattern_len, 1); add_property_stringl(this, "modifier", modifier, modifier_len, 1); #else zval key; zval *value = (zval *) emalloc(sizeof(zval)); ZVAL_STRINGL(&key, "pattern", sizeof("pattern") - 1); ZVAL_STRINGL(value, pattern, pattern_len); Z_OBJ_HT_P(this)->write_property(this, &key, value, NULL); value = (zval *) emalloc(sizeof(zval)); ZVAL_STRINGL(&key, "modifier", sizeof("modifier") - 1); ZVAL_STRINGL(value, modifier, modifier_len); Z_OBJ_HT_P(this)->write_property(this, &key, value, NULL); zval_ptr_dtor(&key); #endif }
/** * Checks if string property exists on object */ int zephir_isset_property_zval(zval *object, const zval *property) { if (Z_TYPE_P(object) == IS_OBJECT) { if (Z_TYPE_P(property) == IS_STRING) { if (likely(zend_hash_str_exists(&Z_OBJCE_P(object)->properties_info, Z_STRVAL_P(property), Z_STRLEN_P(property)))) { return 1; } else { return zend_hash_str_exists(Z_OBJ_HT_P(object)->get_properties(object), Z_STRVAL_P(property), Z_STRLEN_P(property)); } } } return 0; }
PHP_METHOD(Date, __construct) { long timestamp; if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", ×tamp) == FAILURE) { return; } zval *this = getThis(); zval key; zval *value = (zval *) emalloc(sizeof(zval)); #if PHP_API_VERSION <= 20131106 add_property_long_ex(this, "timestamp", sizeof("timestamp"), timestamp); #else ZVAL_STRINGL(&key, "timestamp", sizeof("timestamp") - 1); ZVAL_LONG(value, timestamp); Z_OBJ_HT_P(this)->write_property(this, &key, value, NULL); zval_ptr_dtor(&key); #endif }
/* {{{ proto void SolrInputDocument::__construct() SolrInputDocument constructor */ PHP_METHOD(SolrInputDocument, __construct) { zval *objptr = getThis(); uint nSize = SOLR_INITIAL_HASH_TABLE_SIZE; ulong document_index = SOLR_UNIQUE_DOCUMENT_INDEX(); auto solr_document_t solr_doc; solr_document_t *doc_entry = NULL, *doc_ptr = NULL; memset(&solr_doc, 0, sizeof(solr_document_t)); doc_entry = &solr_doc; doc_entry->document_index = document_index; doc_entry->field_count = 0L; doc_entry->document_boost = 0.0f; /* Allocated memory for the fields HashTable using fast cache for HashTables */ ALLOC_HASHTABLE(doc_entry->fields); ALLOC_HASHTABLE(doc_entry->children); /* Initializing the hash table used for storing fields in this SolrDocument */ zend_hash_init(doc_entry->fields, nSize, NULL, (dtor_func_t) solr_destroy_field_list, SOLR_DOCUMENT_FIELD_PERSISTENT); zend_hash_init(doc_entry->children, nSize, NULL, ZVAL_PTR_DTOR, SOLR_DOCUMENT_FIELD_PERSISTENT); /* Let's check one more time before insert into the HashTable */ if (zend_hash_index_exists(SOLR_GLOBAL(documents), document_index)) { pefree(doc_entry->fields, SOLR_DOCUMENT_FIELD_PERSISTENT); pefree(doc_entry->children, SOLR_DOCUMENT_FIELD_PERSISTENT); return; } /* Add the document entry to the directory of documents */ zend_hash_index_update(SOLR_GLOBAL(documents), document_index, (void *) doc_entry, sizeof(solr_document_t), (void **) &doc_ptr); /* Set the value of the internal id property */ zend_update_property_long(solr_ce_SolrInputDocument, objptr, SOLR_INDEX_PROPERTY_NAME, sizeof(SOLR_INDEX_PROPERTY_NAME) - 1, document_index TSRMLS_CC); /* Keep track of how many SolrDocument instances we currently have */ SOLR_GLOBAL(document_count)++; /* Overriding the default object handlers */ Z_OBJ_HT_P(objptr) = &solr_input_document_object_handlers; }
php_http_object_method_t *php_http_object_method_init(php_http_object_method_t *cb, zval *zobject, const char *method_str, size_t method_len) { if (!cb) { cb = ecalloc(1, sizeof(*cb)); } else { memset(cb, 0, sizeof(*cb)); } cb->fci.size = sizeof(cb->fci); ZVAL_STRINGL(&cb->fci.function_name, method_str, method_len); #if PHP_VERSION_ID < 70300 cb->fcc.initialized = 1; #endif cb->fcc.calling_scope = cb->fcc.called_scope = Z_OBJCE_P(zobject); cb->fcc.function_handler = Z_OBJ_HT_P(zobject)->get_method(&Z_OBJ_P(zobject), Z_STR(cb->fci.function_name), NULL); return cb; }
int zephir_unset_property(zval* object, const char* name) { if (Z_TYPE_P(object) == IS_OBJECT) { zval member; zend_class_entry *old_scope; ZVAL_STRING(&member, name); old_scope = EG(scope); EG(scope) = Z_OBJCE_P(object); Z_OBJ_HT_P(object)->unset_property(object, &member, 0); EG(scope) = old_scope; return SUCCESS; } return FAILURE; }
/** * Makes fast count on implicit array types */ void zephir_fast_count(zval *result, zval *value) { if (Z_TYPE_P(value) == IS_ARRAY) { ZVAL_LONG(result, zend_hash_num_elements(Z_ARRVAL_P(value))); return; } if (Z_TYPE_P(value) == IS_OBJECT) { #ifdef HAVE_SPL zval retval; #endif if (Z_OBJ_HT_P(value)->count_elements) { ZVAL_LONG(result, 1); if (SUCCESS == Z_OBJ_HT(*value)->count_elements(value, &Z_LVAL_P(result))) { return; } } #ifdef HAVE_SPL if (instanceof_function(Z_OBJCE_P(value), spl_ce_Countable)) { zend_call_method_with_0_params(value, NULL, NULL, "count", &retval); if (Z_TYPE(retval) != IS_UNDEF) { convert_to_long_ex(&retval); ZVAL_LONG(result, Z_LVAL(retval)); zval_ptr_dtor(&retval); } return; } #endif ZVAL_LONG(result, 0); return; } if (Z_TYPE_P(value) == IS_NULL) { ZVAL_LONG(result, 0); return; } ZVAL_LONG(result, 1); }
/** * Makes fast count on implicit array types without creating a return zval value */ int zephir_fast_count_int(zval *value) { long count = 0; if (Z_TYPE_P(value) == IS_ARRAY) { return zend_hash_num_elements(Z_ARRVAL_P(value)); } if (Z_TYPE_P(value) == IS_OBJECT) { #ifdef HAVE_SPL zval retval; #endif if (Z_OBJ_HT_P(value)->count_elements) { Z_OBJ_HT(*value)->count_elements(value, &count TSRMLS_CC); return (int) count; } #ifdef HAVE_SPL if (instanceof_function(Z_OBJCE_P(value), spl_ce_Countable)) { zend_call_method_with_0_params(value, NULL, NULL, "count", &retval); if (Z_TYPE(retval) != IS_UNDEF) { convert_to_long_ex(&retval); count = Z_LVAL(retval); zval_ptr_dtor(&retval); return (int) count; } return 0; } #endif return 0; } if (Z_TYPE_P(value) == IS_NULL) { return 0; } return 1; }
static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type) { zval *arg; #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) { RETURN_FALSE; } ZVAL_DEREF(arg); #else ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_ZVAL_DEREF(arg) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); #endif if (Z_TYPE_P(arg) == type) { if (type == IS_OBJECT) { zend_class_entry *ce; if (Z_OBJ_HT_P(arg)->get_class_entry == NULL) { /* if there's no get_class_entry it's not a PHP object, so it can't be INCOMPLETE_CLASS */ RETURN_TRUE; } ce = Z_OBJCE_P(arg); if (ce->name->len == sizeof(INCOMPLETE_CLASS) - 1 && !strncmp(ce->name->val, INCOMPLETE_CLASS, ce->name->len)) { RETURN_FALSE; } } else if (type == IS_RESOURCE) { const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg) TSRMLS_CC); if (!type_name) { RETURN_FALSE; } } RETURN_TRUE; } else { RETURN_FALSE; } }
int msgpack_convert_object(zval *return_value, zval *tpl, zval **value) { zend_class_entry *ce, **pce; TSRMLS_FETCH(); switch (Z_TYPE_P(tpl)) { case IS_STRING: if (zend_lookup_class( Z_STRVAL_P(tpl), Z_STRLEN_P(tpl), &pce TSRMLS_CC) != SUCCESS) { MSGPACK_ERROR("[msgpack] (%s) Class '%s' not found", __FUNCTION__, Z_STRVAL_P(tpl)); return FAILURE; } ce = *pce; break; case IS_OBJECT: ce = zend_get_class_entry(tpl TSRMLS_CC); break; default: MSGPACK_ERROR("[msgpack] (%s) object type is unsupported", __FUNCTION__); return FAILURE; } if (Z_TYPE_PP(value) == IS_OBJECT) { zend_class_entry *vce; vce = zend_get_class_entry(*value TSRMLS_CC); if (strcmp(ce->name, vce->name) == 0) { *return_value = **value; zval_copy_ctor(return_value); zval_ptr_dtor(value); return SUCCESS; } } object_init_ex(return_value, ce); /* Run the constructor if there is one */ if (ce->constructor && (ce->constructor->common.fn_flags & ZEND_ACC_PUBLIC)) { zval *retval_ptr = NULL; zval ***params = NULL; int num_args = 0; zend_fcall_info fci; zend_fcall_info_cache fcc; #if ZEND_MODULE_API_NO >= 20090626 fci.size = sizeof(fci); fci.function_table = EG(function_table); fci.function_name = NULL; fci.symbol_table = NULL; fci.object_ptr = return_value; fci.retval_ptr_ptr = &retval_ptr; fci.param_count = num_args; fci.params = params; fci.no_separation = 1; fcc.initialized = 1; fcc.function_handler = ce->constructor; fcc.calling_scope = EG(scope); fcc.called_scope = Z_OBJCE_P(return_value); fcc.object_ptr = return_value; #else fci.size = sizeof(fci); fci.function_table = EG(function_table); fci.function_name = NULL; fci.symbol_table = NULL; fci.object_pp = &return_value; fci.retval_ptr_ptr = &retval_ptr; fci.param_count = num_args; fci.params = params; fci.no_separation = 1; fcc.initialized = 1; fcc.function_handler = ce->constructor; fcc.calling_scope = EG(scope); fcc.object_pp = &return_value; #endif if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) { if (params) { efree(params); } if (retval_ptr) { zval_ptr_dtor(&retval_ptr); } MSGPACK_WARNING( "[msgpack] (%s) Invocation of %s's constructor failed", __FUNCTION__, ce->name); return FAILURE; } if (retval_ptr) { zval_ptr_dtor(&retval_ptr); } if (params) { efree(params); } } switch (Z_TYPE_PP(value)) { case IS_ARRAY: { char *key; uint key_len; int key_type; ulong key_index; zval **data; HashPosition pos; HashTable *ht, *ret; HashTable *var = NULL; int num; ht = HASH_OF(*value); ret = HASH_OF(return_value); num = zend_hash_num_elements(ht); if (num <= 0) { zval_ptr_dtor(value); break; } /* string - php_only mode? */ if (ht->nNumOfElements != ht->nNextFreeElement || ht->nNumOfElements != ret->nNumOfElements) { HashTable *properties = NULL; HashPosition prop_pos; ALLOC_HASHTABLE(var); zend_hash_init(var, num, NULL, NULL, 0); zend_hash_internal_pointer_reset_ex(ht, &pos); for (;; zend_hash_move_forward_ex(ht, &pos)) { key_type = zend_hash_get_current_key_ex( ht, &key, &key_len, &key_index, 0, &pos); if (key_type == HASH_KEY_NON_EXISTANT) { break; } if (zend_hash_get_current_data_ex( ht, (void *)&data, &pos) != SUCCESS) { continue; } if (key_type == HASH_KEY_IS_STRING) { zval *val; MSGPACK_CONVERT_COPY_ZVAL(val, data); if (msgpack_convert_string_to_properties( return_value, key, key_len, val, var) != SUCCESS) { zval_ptr_dtor(&val); MSGPACK_WARNING( "[msgpack] (%s) " "illegal offset type, skip this decoding", __FUNCTION__); } } } /* index */ properties = Z_OBJ_HT_P(return_value)->get_properties( return_value TSRMLS_CC); if (HASH_OF(tpl)) { properties = HASH_OF(tpl); } zend_hash_internal_pointer_reset_ex(properties, &prop_pos); zend_hash_internal_pointer_reset_ex(ht, &pos); for (;; zend_hash_move_forward_ex(ht, &pos)) { key_type = zend_hash_get_current_key_ex( ht, &key, &key_len, &key_index, 0, &pos); if (key_type == HASH_KEY_NON_EXISTANT) { break; } if (zend_hash_get_current_data_ex( ht, (void *)&data, &pos) != SUCCESS) { continue; } switch (key_type) { case HASH_KEY_IS_LONG: { zval *val; MSGPACK_CONVERT_COPY_ZVAL(val, data); if (msgpack_convert_long_to_properties( ret, &properties, &prop_pos, key_index, val, var) != SUCCESS) { zval_ptr_dtor(&val); MSGPACK_WARNING( "[msgpack] (%s) " "illegal offset type, skip this decoding", __FUNCTION__); } break; } case HASH_KEY_IS_STRING: break; default: MSGPACK_WARNING( "[msgpack] (%s) key is not string nor array", __FUNCTION__); break; } } zend_hash_destroy(var); FREE_HASHTABLE(var); } else { HashPosition valpos; int (*convert_function)(zval *, zval *, zval **) = NULL; zval **arydata, *aryval; /* index */ zend_hash_internal_pointer_reset_ex(ret, &pos); zend_hash_internal_pointer_reset_ex(ht, &valpos); for (;; zend_hash_move_forward_ex(ret, &pos), zend_hash_move_forward_ex(ht, &valpos)) { key_type = zend_hash_get_current_key_ex( ret, &key, &key_len, &key_index, 0, &pos); if (key_type == HASH_KEY_NON_EXISTANT) { break; } if (zend_hash_get_current_data_ex( ret, (void *)&data, &pos) != SUCCESS) { continue; } switch (Z_TYPE_PP(data)) { case IS_ARRAY: convert_function = msgpack_convert_array; break; case IS_OBJECT: //case IS_STRING: -- may have default values of // class members, so it's not wise to allow convert_function = msgpack_convert_object; break; default: break; } if (zend_hash_get_current_data_ex( ht, (void *)&arydata, &valpos) != SUCCESS) { MSGPACK_WARNING( "[msgpack] (%s) can't get data value by index", __FUNCTION__); return FAILURE; } MSGPACK_CONVERT_COPY_ZVAL(aryval, arydata); if (convert_function) { zval *rv; ALLOC_INIT_ZVAL(rv); if (convert_function(rv, *data, &aryval) != SUCCESS) { zval_ptr_dtor(&aryval); MSGPACK_WARNING( "[msgpack] (%s) " "convert failure in convert_object", __FUNCTION__); return FAILURE; } zend_symtable_update( ret, key, key_len, &rv, sizeof(rv), NULL); } else { zend_symtable_update( ret, key, key_len, &aryval, sizeof(aryval), NULL); } } } zval_ptr_dtor(value); break; } default: { HashTable *properties = NULL; HashPosition prop_pos; properties = Z_OBJ_HT_P(return_value)->get_properties( return_value TSRMLS_CC); zend_hash_internal_pointer_reset_ex(properties, &prop_pos); if (msgpack_convert_long_to_properties( HASH_OF(return_value), &properties, &prop_pos, 0, *value, NULL) != SUCCESS) { MSGPACK_WARNING( "[msgpack] (%s) illegal offset type, skip this decoding", __FUNCTION__); } break; } } return SUCCESS; }
/** * Checks if a method is callable */ static int zephir_alt_is_callable_check_method(zend_class_entry *ce, int check_flags, char *method_name, unsigned int method_len, zend_fcall_info_cache *fcc, char **error, unsigned long method_key TSRMLS_DC) { int retval = 0; #ifndef ZEPHIR_RELEASE int call_via_handler = 0; #endif if (error) { *error = NULL; } if (!method_key) { method_key = zend_inline_hash_func(method_name, method_len + 1); } /* Try to fetch find static method of given class. */ if (likely(zephir_hash_quick_find(&ce->function_table, method_name, method_len + 1, method_key, (void**) &fcc->function_handler) == SUCCESS)) { retval = 1; if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) && ZEPHIR_EG(scope) && instanceof_function(fcc->function_handler->common.scope, EG(scope) TSRMLS_CC)) { zend_function *priv_fbc; if (zephir_hash_quick_find(&ZEPHIR_EG(scope)->function_table, method_name, method_len + 1, method_key, (void **) &priv_fbc)==SUCCESS && (priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE) && priv_fbc->common.scope == EG(scope)) { fcc->function_handler = priv_fbc; } } #ifndef ZEPHIR_RELEASE if ((check_flags & IS_CALLABLE_CHECK_NO_ACCESS) == 0 && (fcc->calling_scope && (fcc->calling_scope->__call || fcc->calling_scope->__callstatic))) { if (fcc->function_handler->op_array.fn_flags & ZEND_ACC_PRIVATE) { if (!zend_check_private(fcc->function_handler, ce, method_name, method_len TSRMLS_CC)) { retval = 0; fcc->function_handler = NULL; goto get_function_via_handler; } } else { if (fcc->function_handler->common.fn_flags & ZEND_ACC_PROTECTED) { if (!zend_check_protected(fcc->function_handler->common.scope, ZEPHIR_EG(scope))) { retval = 0; fcc->function_handler = NULL; goto get_function_via_handler; } } } } #endif } else { #ifndef ZEPHIR_RELEASE get_function_via_handler: if (Z_OBJ_HT_P(fcc->object_ptr)->get_method) { #if PHP_VERSION_ID < 50400 fcc->function_handler = Z_OBJ_HT_P(fcc->object_ptr)->get_method(&fcc->object_ptr, method_name, method_len TSRMLS_CC); #else fcc->function_handler = Z_OBJ_HT_P(fcc->object_ptr)->get_method(&fcc->object_ptr, method_name, method_len, NULL TSRMLS_CC); #endif if (fcc->function_handler) { retval = 1; call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) != 0; } } #endif } if (retval) { #ifndef ZEPHIR_RELEASE if (fcc->calling_scope && !call_via_handler) { if (!fcc->object_ptr && (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT)) { if (error) { zephir_spprintf(error, 0, "cannot call abstract method %s::%s()", fcc->calling_scope->name, fcc->function_handler->common.function_name); retval = 0; } else { zend_error(E_ERROR, "Cannot call abstract method %s::%s()", fcc->calling_scope->name, fcc->function_handler->common.function_name); } } if (retval && (check_flags & IS_CALLABLE_CHECK_NO_ACCESS) == 0) { if (fcc->function_handler->op_array.fn_flags & ZEND_ACC_PRIVATE) { if (!zend_check_private(fcc->function_handler, ce, method_name, method_len TSRMLS_CC)) { if (error) { if (*error) { efree(*error); } zephir_spprintf(error, 0, "cannot access private method %s::%s()", fcc->calling_scope->name, fcc->function_handler->common.function_name); } retval = 0; } } else { if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PROTECTED)) { if (!zend_check_protected(fcc->function_handler->common.scope, EG(scope))) { if (error) { if (*error) { efree(*error); } zephir_spprintf(error, 0, "cannot access protected method %s::%s()", fcc->calling_scope->name, fcc->function_handler->common.function_name); } retval = 0; } } } } } #endif } else { if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) { if (fcc->calling_scope) { if (error) { zephir_spprintf(error, 0, "class '%s' does not have a method '%s'", fcc->calling_scope->name, method_name); } } else { if (error) { zephir_spprintf(error, 0, "function '%s' does not exist", method_name); } } } } if (retval) { fcc->initialized = 1; } return retval; }