Ejemplo n.º 1
0
static void php_object_property_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
{
	const char *prop_name, *class_name;

	if (key == NULL) { /* numeric key */
		php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
	} else { /* string key */
		int unmangle = zend_unmangle_property_name(key, &class_name, &prop_name);
		php_printf("%*c[", level + 1, ' ');

		if (class_name && unmangle == SUCCESS) {
			if (class_name[0] == '*') {
				php_printf("\"%s\":protected", prop_name);
			} else {
				php_printf("\"%s\":\"%s\":private", prop_name, class_name);
			}
		} else {
			php_printf("\"");
			PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
			php_printf("\"");
		}
		ZEND_PUTS("]=>\n");
	}
	php_var_dump(zv, level + 2);
}
Ejemplo n.º 2
0
static void print_hash(zend_write_func_t write_func, HashTable *ht, int indent, zend_bool is_object TSRMLS_DC) /* {{{ */
{
	zval **tmp;
	char *string_key;
	HashPosition iterator;
	ulong num_key;
	uint str_len;
	int i;

	for (i = 0; i < indent; i++) {
		ZEND_PUTS_EX(" ");
	}
	ZEND_PUTS_EX("(\n");
	indent += PRINT_ZVAL_INDENT;
	zend_hash_internal_pointer_reset_ex(ht, &iterator);
	while (zend_hash_get_current_data_ex(ht, (void **) &tmp, &iterator) == SUCCESS) {
		for (i = 0; i < indent; i++) {
			ZEND_PUTS_EX(" ");
		}
		ZEND_PUTS_EX("[");
		switch (zend_hash_get_current_key_ex(ht, &string_key, &str_len, &num_key, 0, &iterator)) {
			case HASH_KEY_IS_STRING:
				if (is_object) {
					char *prop_name, *class_name;
					int mangled = zend_unmangle_property_name(string_key, str_len - 1, &class_name, &prop_name);

					ZEND_PUTS_EX(prop_name);
					if (class_name && mangled == SUCCESS) {
						if (class_name[0]=='*') {
							ZEND_PUTS_EX(":protected");
						} else {
							ZEND_PUTS_EX(":");
							ZEND_PUTS_EX(class_name);
							ZEND_PUTS_EX(":private");
						}
					}
				} else {
					ZEND_WRITE_EX(string_key, str_len-1);
				}
				break;
			case HASH_KEY_IS_LONG:
				{
					char key[25];
					snprintf(key, sizeof(key), "%ld", num_key);
					ZEND_PUTS_EX(key);
				}
				break;
		}
		ZEND_PUTS_EX("] => ");
		zend_print_zval_r_ex(write_func, *tmp, indent+PRINT_ZVAL_INDENT TSRMLS_CC);
		ZEND_PUTS_EX("\n");
		zend_hash_move_forward_ex(ht, &iterator);
	}
	indent -= PRINT_ZVAL_INDENT;
	for (i = 0; i < indent; i++) {
		ZEND_PUTS_EX(" ");
	}
	ZEND_PUTS_EX(")\n");
}
Ejemplo n.º 3
0
static char* xdebug_get_property_info(char *mangled_property, int mangled_len, char **property_name, char **class_name)
{
	char *prop_name, *cls_name;

#if PHP_VERSION_ID >= 50200
	zend_unmangle_property_name(mangled_property, mangled_len - 1, &cls_name, &prop_name);
#else
	zend_unmangle_property_name(mangled_property, &cls_name, &prop_name);
#endif
	*property_name = prop_name;
	*class_name = cls_name;
	if (cls_name) {
		if (cls_name[0] == '*') {
			return "protected";
		} else {
			return "private";
		}
	} else {
		return "public";
	}
}
Ejemplo n.º 4
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;
	}
Ejemplo n.º 5
0
static void php_object_property_dump(zend_property_info *prop_info, zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
{
	const char *prop_name, *class_name;

	if (key == NULL) { /* numeric key */
		php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
	} else { /* string key */
		int unmangle = zend_unmangle_property_name(key, &class_name, &prop_name);
		php_printf("%*c[", level + 1, ' ');

		if (class_name && unmangle == SUCCESS) {
			if (class_name[0] == '*') {
				php_printf("\"%s\":protected", prop_name);
			} else {
				php_printf("\"%s\":\"%s\":private", prop_name, class_name);
			}
		} else {
			php_printf("\"");
			PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
			php_printf("\"");
		}
		ZEND_PUTS("]=>\n");
	}

	if (Z_TYPE_P(zv) == IS_UNDEF) {
		ZEND_ASSERT(prop_info->type);
		php_printf("%*cuninitialized(%s%s)\n",
			level + 1, ' ',
			ZEND_TYPE_ALLOW_NULL(prop_info->type) ? "?" : "",
			ZEND_TYPE_IS_CLASS(prop_info->type) ?
				ZSTR_VAL(ZEND_TYPE_IS_CE(prop_info->type) ? ZEND_TYPE_CE(prop_info->type)->name : ZEND_TYPE_NAME(prop_info->type)) :
				zend_get_type_by_const(ZEND_TYPE_CODE(prop_info->type)));
	} else {
		php_var_dump(zv, level + 2);
	}
}
Ejemplo n.º 6
0
/**
 * Reads annotations from the class dockblocks, its methods and/or properties
 *
 * @param string $className
 * @return array
 */
PHP_METHOD(Phalcon_Annotations_Reader, parse){

	zval *class_name, *annotations;
	zval *class_annotations, *annotations_properties, *annotations_methods;
	zend_class_entry *class_ce;
	const char *file;
	uint32_t line;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &class_name);

	if (unlikely(Z_TYPE_P(class_name) != IS_STRING)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_annotations_exception_ce, "The class name must be a string");
		return;
	}

	class_ce = zend_fetch_class(Z_STR_P(class_name), ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_SILENT);
	if (!class_ce) {
		PHALCON_THROW_EXCEPTION_FORMAT(phalcon_annotations_exception_ce, "Class %s does not exist", Z_STRVAL_P(class_name));
		return;
	}

	if (class_ce->type != ZEND_USER_CLASS) {
		array_init(return_value);
		RETURN_MM();
	}

	PHALCON_INIT_VAR(annotations);
	array_init(annotations);

	file = ZSTR_VAL(phalcon_get_class_filename(class_ce));
	if (!file) {
		file = "(unknown)";
	}

	/* Class info */
	{
		zend_string *cmt;

		if ((cmt = phalcon_get_class_doc_comment(class_ce)) != NULL) {
			line = phalcon_get_class_startline(class_ce);

			PHALCON_INIT_VAR(class_annotations);
			RETURN_MM_ON_FAILURE(phannot_parse_annotations(class_annotations, cmt, file, line));

			if (Z_TYPE_P(class_annotations) == IS_ARRAY) {
				phalcon_array_update_str(annotations, SL("class"), class_annotations, PH_COPY);
			}
		}
	}

	/* Get class properties */
	{
		HashTable *props = &class_ce->properties_info;
		if (zend_hash_num_elements(props) > 0) {
			zend_property_info *property;

			PHALCON_INIT_VAR(annotations_properties);
			array_init_size(annotations_properties, zend_hash_num_elements(props));

			ZEND_HASH_FOREACH_PTR(props, property) {
				zend_string *cmt;

				if ((cmt = phalcon_get_property_doc_comment(property)) != NULL) {
					zval *property_annotations;

					PHALCON_ALLOC_INIT_ZVAL(property_annotations);
					if (FAILURE == phannot_parse_annotations(property_annotations, cmt, file, 0)) {
						zval_ptr_dtor(property_annotations);
						RETURN_MM();
					}

					if (Z_TYPE_P(property_annotations) == IS_ARRAY) {
						const char *prop_name, *class_name;
						if (zend_unmangle_property_name(property->name, &class_name, &prop_name) == SUCCESS) {
							add_assoc_zval_ex(annotations_properties, prop_name, strlen(prop_name), property_annotations);
						}
					} else {
						zval_ptr_dtor(property_annotations);
					}
				}
			} ZEND_HASH_FOREACH_END();

			if (zend_hash_num_elements(Z_ARRVAL_P(annotations_properties))) {
				phalcon_array_update_str(annotations, SL("properties"), annotations_properties, PH_COPY);
			}
		}
Ejemplo n.º 7
0
/* {{{ php_url_encode_hash */
PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
				const char *num_prefix, int num_prefix_len,
				const char *key_prefix, int key_prefix_len,
				const char *key_suffix, int key_suffix_len,
			  zval *type, char *arg_sep, int enc_type TSRMLS_DC)
{
	char *key = NULL;
	char *ekey, *newprefix, *p;
	int arg_sep_len, ekey_len, key_type, newprefix_len;
	uint key_len;
	ulong idx;
	zval **zdata = NULL, *copyzval;

	if (!ht) {
		return FAILURE;
	}

	if (ht->nApplyCount > 0) {
		/* Prevent recursion */
		return SUCCESS;
	}

	if (!arg_sep) {
		arg_sep = INI_STR("arg_separator.output");
		if (!arg_sep || !strlen(arg_sep)) {
			arg_sep = URL_DEFAULT_ARG_SEP;
		}
	}
	arg_sep_len = strlen(arg_sep);

	for (zend_hash_internal_pointer_reset(ht);
		(key_type = zend_hash_get_current_key_ex(ht, &key, &key_len, &idx, 0, NULL)) != HASH_KEY_NON_EXISTANT;
		zend_hash_move_forward(ht)
	) {
		if (key_type == HASH_KEY_IS_STRING && key_len && key[key_len-1] == '\0') {
			/* We don't want that trailing NULL */
			key_len -= 1;
		}

		/* handling for private & protected object properties */
		if (key && *key == '\0' && type != NULL) {
			const char *tmp;

			zend_object *zobj = zend_objects_get_address(type TSRMLS_CC);
			if (zend_check_property_access(zobj, key, key_len-1 TSRMLS_CC) != SUCCESS) {
				/* private or protected property access outside of the class */
				continue;
			}
			zend_unmangle_property_name(key, key_len-1, &tmp, (const char**)&key);
			key_len = strlen(key);		
		}

		if (zend_hash_get_current_data_ex(ht, (void **)&zdata, NULL) == FAILURE || !zdata || !(*zdata)) {
			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error traversing form data array");
			return FAILURE;
		}
		if (Z_TYPE_PP(zdata) == IS_ARRAY || Z_TYPE_PP(zdata) == IS_OBJECT) {
			if (key_type == HASH_KEY_IS_STRING) {
				if (enc_type == PHP_QUERY_RFC3986) {
					ekey = php_raw_url_encode(key, key_len, &ekey_len);
				} else {
					ekey = php_url_encode(key, key_len, &ekey_len);
				}
				newprefix_len = key_suffix_len + ekey_len + key_prefix_len + 3 /* %5B */;
				newprefix = emalloc(newprefix_len + 1);
				p = newprefix;

				if (key_prefix) {
					memcpy(p, key_prefix, key_prefix_len);
					p += key_prefix_len;
				}

				memcpy(p, ekey, ekey_len);
				p += ekey_len;
				efree(ekey);

				if (key_suffix) {
					memcpy(p, key_suffix, key_suffix_len);
					p += key_suffix_len;
				}
				*(p++) = '%';
				*(p++) = '5';
				*(p++) = 'B';
				*p = '\0';
			} else {
				/* Is an integer key */
				ekey_len = spprintf(&ekey, 0, "%ld", idx);
				newprefix_len = key_prefix_len + num_prefix_len + ekey_len + key_suffix_len + 3 /* %5B */;
				newprefix = emalloc(newprefix_len + 1);
				p = newprefix;

				if (key_prefix) {
					memcpy(p, key_prefix, key_prefix_len);
					p += key_prefix_len;
				}

				memcpy(p, num_prefix, num_prefix_len);
				p += num_prefix_len;

				memcpy(p, ekey, ekey_len);
				p += ekey_len;
				efree(ekey);

				if (key_suffix) {
					memcpy(p, key_suffix, key_suffix_len);
					p += key_suffix_len;
				}
				*(p++) = '%';
				*(p++) = '5';
				*(p++) = 'B';
				*p = '\0';
			}
			ht->nApplyCount++;
			php_url_encode_hash_ex(HASH_OF(*zdata), formstr, NULL, 0, newprefix, newprefix_len, "%5D", 3, (Z_TYPE_PP(zdata) == IS_OBJECT ? *zdata : NULL), arg_sep, enc_type TSRMLS_CC);
			ht->nApplyCount--;
			efree(newprefix);
		} else if (Z_TYPE_PP(zdata) == IS_NULL || Z_TYPE_PP(zdata) == IS_RESOURCE) {
			/* Skip these types */
			continue;
		} else {
			if (formstr->len) {
				smart_str_appendl(formstr, arg_sep, arg_sep_len);
			}
			/* Simple key=value */
			smart_str_appendl(formstr, key_prefix, key_prefix_len);
			if (key_type == HASH_KEY_IS_STRING) {
				if (enc_type == PHP_QUERY_RFC3986) {
					ekey = php_raw_url_encode(key, key_len, &ekey_len);
				} else {
					ekey = php_url_encode(key, key_len, &ekey_len);
				}
				smart_str_appendl(formstr, ekey, ekey_len);
				efree(ekey);
			} else {
				/* Numeric key */
				if (num_prefix) {
					smart_str_appendl(formstr, num_prefix, num_prefix_len);
				}
				ekey_len = spprintf(&ekey, 0, "%ld", idx);
				smart_str_appendl(formstr, ekey, ekey_len);
				efree(ekey);
			}
			smart_str_appendl(formstr, key_suffix, key_suffix_len);
			smart_str_appendl(formstr, "=", 1);
			switch (Z_TYPE_PP(zdata)) {
				case IS_STRING:
					if (enc_type == PHP_QUERY_RFC3986) {
						ekey = php_raw_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);
					} else {
						ekey = php_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);						
					}
					break;
				case IS_LONG:
				case IS_BOOL:
					ekey_len = spprintf(&ekey, 0, "%ld", Z_LVAL_PP(zdata));
					break;
				case IS_DOUBLE:
					ekey_len = spprintf(&ekey, 0, "%.*G", (int) EG(precision), Z_DVAL_PP(zdata));
					break;
				default:
					/* fall back on convert to string */
					MAKE_STD_ZVAL(copyzval);
					*copyzval = **zdata;
					zval_copy_ctor(copyzval);
					convert_to_string_ex(&copyzval);
					if (enc_type == PHP_QUERY_RFC3986) {
						ekey = php_raw_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
					} else {
						ekey = php_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
					}
					zval_ptr_dtor(&copyzval);
			}
			smart_str_appendl(formstr, ekey, ekey_len);
			efree(ekey);
		}
	}

	return SUCCESS;
}
Ejemplo n.º 8
0
/**
 * Reads annotations from the class dockblocks, its methods and/or properties
 *
 * @param string $className
 * @return array
 */
PHP_METHOD(Phalcon_Annotations_Reader, parse){

	zval *class_name, *annotations;
	zval *class_annotations, *annotations_properties, *annotations_methods;
	zend_class_entry *class_ce;
	const char *file;
	zend_uint line;

	phalcon_fetch_params(0, 1, 0, &class_name);

	if (unlikely(Z_TYPE_P(class_name) != IS_STRING)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_annotations_exception_ce, "The class name must be a string");
		return;
	}

	class_ce = zend_fetch_class(Z_STRVAL_P(class_name), Z_STRLEN_P(class_name), ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
	if (!class_ce) {
		zend_throw_exception_ex(phalcon_annotations_exception_ce, 0 TSRMLS_CC, "Class %s does not exist", Z_STRVAL_P(class_name));
		return;
	}

	if (class_ce->type != ZEND_USER_CLASS) {
		array_init(return_value);
		return;
	}

	PHALCON_MM_GROW();
	PHALCON_INIT_VAR(annotations);
	array_init(annotations);

	file = phalcon_get_class_filename(class_ce);
	if (!file) {
		file = "(unknown)";
	}

	/* Class info */
	{
		const char *cmt;
		zend_uint cmt_len;

		if (phalcon_get_class_doc_comment(class_ce, &cmt, &cmt_len)) {
			line = phalcon_get_class_startline(class_ce);

			PHALCON_INIT_VAR(class_annotations);
			RETURN_MM_ON_FAILURE(phannot_parse_annotations(class_annotations, cmt, cmt_len, file, line TSRMLS_CC));

			if (Z_TYPE_P(class_annotations) == IS_ARRAY) {
				phalcon_array_update_string(&annotations, SL("class"), class_annotations, PH_COPY);
			}
		}
	}

	/* Get class properties */
	{
		HashTable *props = &class_ce->properties_info;
		if (zend_hash_num_elements(props) > 0) {
			HashPosition hp;
			zend_property_info *property;

			PHALCON_INIT_VAR(annotations_properties);
			array_init_size(annotations_properties, zend_hash_num_elements(props));

			for (
				zend_hash_internal_pointer_reset_ex(props, &hp);
				zend_hash_get_current_data_ex(props, (void**)&property, &hp) != FAILURE;
				zend_hash_move_forward_ex(props, &hp)
			) {
				const char *cmt;
				zend_uint cmt_len;

				if (phalcon_get_property_doc_comment(property, &cmt, &cmt_len)) {
					zval *property_annotations;

					MAKE_STD_ZVAL(property_annotations);
					if (FAILURE == phannot_parse_annotations(property_annotations, cmt, cmt_len, file, 0 TSRMLS_CC)) {
						zval_ptr_dtor(&property_annotations);
						RETURN_MM();
					}

					if (Z_TYPE_P(property_annotations) == IS_ARRAY) {
						#if PHP_VERSION_ID >= 50400
						{
							const char *prop_name, *class_name;
							if (zend_unmangle_property_name(property->name, property->name_length - 1, &class_name, &prop_name) == SUCCESS) {
								add_assoc_zval_ex(annotations_properties, prop_name, strlen(prop_name) + 1, property_annotations);
							}
						}
						#else
						{
							char *prop_name, *class_name;
							if (zend_unmangle_property_name(property->name, property->name_length - 1, &class_name, &prop_name) == SUCCESS) {
								add_assoc_zval_ex(annotations_properties, prop_name, strlen(prop_name) + 1, property_annotations);
							}
						}
						#endif
					} else {
						zval_ptr_dtor(&property_annotations);
					}
				}
			}

			if (zend_hash_num_elements(Z_ARRVAL_P(annotations_properties))) {
				phalcon_array_update_string(&annotations, SL("properties"), annotations_properties, PH_COPY);
			}
		}
	}

	/* Get class methods */
	{
		HashTable *methods = &class_ce->function_table;
		if (zend_hash_num_elements(methods) > 0) {
			HashPosition hp;
			zend_function *method;

			PHALCON_INIT_VAR(annotations_methods);
			array_init_size(annotations_methods, zend_hash_num_elements(methods));

			for (
				zend_hash_internal_pointer_reset_ex(methods, &hp);
				zend_hash_get_current_data_ex(methods, (void**)&method, &hp) != FAILURE;
				zend_hash_move_forward_ex(methods, &hp)
			) {
				const char *cmt;
				zend_uint cmt_len;

				if (phalcon_get_function_doc_comment(method, &cmt, &cmt_len)) {
					zval *method_annotations;

					line = phalcon_get_function_startline(method);

					MAKE_STD_ZVAL(method_annotations);
					if (FAILURE == phannot_parse_annotations(method_annotations, cmt, cmt_len, file, line TSRMLS_CC)) {
						zval_ptr_dtor(&method_annotations);
						RETURN_MM();
					}

					if (Z_TYPE_P(method_annotations) == IS_ARRAY) {
						add_assoc_zval_ex(annotations_methods, method->common.function_name, strlen(method->common.function_name) + 1, method_annotations);
					}
					else {
						zval_ptr_dtor(&method_annotations);
					}
				}
			}

			if (zend_hash_num_elements(Z_ARRVAL_P(annotations_methods))) {
				phalcon_array_update_string(&annotations, SL("methods"), annotations_methods, PH_COPY);
			}
		}
	}

	RETURN_CTOR(annotations);
}