Esempio n. 1
0
/**
 * Returns the annotations found in a specific property
 *
 * @param string $className
 * @param string $propertyName
 * @return Phalcon\Annotations\Collection
 */
PHP_METHOD(Phalcon_Annotations_Adapter, getProperty){

	zval *class_name, *property_name, *class_annotations;
	zval *properties, *property = NULL, *name = NULL, *collection;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &class_name, &property_name);
	
	/** 
	 * Get the full annotations from the class
	 */
	PHALCON_INIT_VAR(class_annotations);
	PHALCON_CALL_METHOD_PARAMS_1(class_annotations, this_ptr, "get", class_name);
	
	/** 
	 * A valid annotations reflection is an object
	 */
	if (Z_TYPE_P(class_annotations) == IS_OBJECT) {
	
		PHALCON_INIT_VAR(properties);
		PHALCON_CALL_METHOD(properties, class_annotations, "getpropertyannotations");
		if (Z_TYPE_P(properties) == IS_ARRAY) { 
	
			if (!phalcon_is_iterable(properties, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_FOREACH_KEY(name, ah0, hp0);
				PHALCON_GET_FOREACH_VALUE(property);
	
				if (PHALCON_IS_EQUAL(name, property_name)) {
					RETURN_CCTOR(property);
				}
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
		}
	}
	
	/** 
	 * Returns a collection anyways
	 */
	PHALCON_INIT_VAR(collection);
	object_init_ex(collection, phalcon_annotations_collection_ce);
	PHALCON_CALL_METHOD_NORETURN(collection, "__construct");
	
	
	RETURN_CTOR(collection);
}
Esempio n. 2
0
/**
 * Do a role inherit from another existing role
 *
 * @param string $roleName
 * @param string $roleToInherit
 */
PHP_METHOD(Phalcon_Acl_Adapter_Memory, addInherit){

	zval *role_name, *role_to_inherit, *roles_names;
	zval *exception_message = NULL, *role_inherit_name = NULL;
	zval *roles_inherits, *empty_arr, *_roleInherits;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &role_name, &role_to_inherit);
	
	PHALCON_OBS_VAR(roles_names);
	phalcon_read_property_this(&roles_names, this_ptr, SL("_rolesNames"), PH_NOISY_CC);
	if (!phalcon_array_isset(roles_names, role_name)) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Role '", role_name, "' does not exist in the role list");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
		return;
	}
	
	if (Z_TYPE_P(role_to_inherit) == IS_OBJECT) {
		PHALCON_INIT_VAR(role_inherit_name);
		phalcon_call_method(role_inherit_name, role_to_inherit, "getname");
	} else {
		PHALCON_CPY_WRT(role_inherit_name, role_to_inherit);
	}
	
	/** 
	 * Check if the role to inherit is valid
	 */
	if (!phalcon_array_isset(roles_names, role_inherit_name)) {
		PHALCON_INIT_NVAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Role '", role_inherit_name, "' (to inherit) does not exist in the role list");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
		return;
	}
	
	if (PHALCON_IS_EQUAL(role_inherit_name, role_name)) {
		RETURN_MM_FALSE;
	}
	
	PHALCON_OBS_VAR(roles_inherits);
	phalcon_read_property_this(&roles_inherits, this_ptr, SL("_roleInherits"), PH_NOISY_CC);
	if (!phalcon_array_isset(roles_inherits, role_name)) {
		PHALCON_INIT_VAR(empty_arr);
		array_init(empty_arr);
		phalcon_update_property_array(this_ptr, SL("_roleInherits"), role_name, empty_arr TSRMLS_CC);
	}
	
	PHALCON_OBS_VAR(_roleInherits);
	phalcon_read_property_this(&_roleInherits, this_ptr, SL("_roleInherits"), PH_NOISY_CC);
	phalcon_array_update_append_multi_2(&_roleInherits, role_name, role_inherit_name, 0);
	phalcon_update_property_this(this_ptr, SL("_roleInherits"), _roleInherits TSRMLS_CC);
	RETURN_MM_TRUE;
}
Esempio n. 3
0
/**
 * Executes the validation
 *
 * @param string $value
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_Identical, valid){

	zval *value, *identical_value;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &value, &identical_value);

	if (!PHALCON_IS_EQUAL(value, identical_value)) {
		RETURN_MM_FALSE;
	}
	
	RETURN_MM_TRUE;
}
Esempio n. 4
0
/**
 * Gets row in a specific position of the resultset
 *
 * @param int $index
 * @return Phalcon\Mvc\ModelInterface
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, offsetGet){

	zval *index, *count, *exists, *pointer, *current = NULL, *valid;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &index);
	
	PHALCON_INIT_VAR(count);
	phalcon_call_method(count, this_ptr, "count");
	
	PHALCON_INIT_VAR(exists);
	is_smaller_function(exists, index, count TSRMLS_CC);
	if (PHALCON_IS_TRUE(exists)) {
	
		/** 
		 * Check if the last record returned is the current requested
		 */
		PHALCON_OBS_VAR(pointer);
		phalcon_read_property_this(&pointer, this_ptr, SL("_pointer"), PH_NOISY_CC);
		if (PHALCON_IS_EQUAL(pointer, index)) {
			PHALCON_INIT_VAR(current);
			phalcon_call_method(current, this_ptr, "current");
			RETURN_CCTOR(current);
		}
	
		/** 
		 * Move the cursor to the specific position
		 */
		phalcon_call_method_p1_noret(this_ptr, "seek", index);
	
		/** 
		 * Check if the last record returned is the requested
		 */
		PHALCON_INIT_VAR(valid);
		phalcon_call_method(valid, this_ptr, "valid");
		if (PHALCON_IS_NOT_FALSE(valid)) {
			PHALCON_INIT_NVAR(current);
			phalcon_call_method(current, this_ptr, "current");
			RETURN_CCTOR(current);
		}
	
		RETURN_MM_FALSE;
	}
	
	PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The index does not exist in the cursor");
	return;
}
Esempio n. 5
0
/**
 * Executes the validation
 *
 * @param Phalcon\Validation $validator
 * @param string $attribute
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_Identical, validate){

	zval *validator, *attribute, *value, *option = NULL, *identical_value;
	zval *message_str = NULL, *type, *message;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &validator, &attribute) == FAILURE) {
		RETURN_MM_NULL();
	}

	PHALCON_INIT_VAR(value);
	PHALCON_CALL_METHOD_PARAMS_1(value, validator, "getvalue", attribute);
	
	PHALCON_INIT_VAR(option);
	ZVAL_STRING(option, "value", 1);
	
	PHALCON_INIT_VAR(identical_value);
	PHALCON_CALL_METHOD_PARAMS_1(identical_value, this_ptr, "getoption", option);
	if (!PHALCON_IS_EQUAL(value, identical_value)) {
	
		PHALCON_INIT_NVAR(option);
		ZVAL_STRING(option, "message", 1);
	
		PHALCON_INIT_VAR(message_str);
		PHALCON_CALL_METHOD_PARAMS_1(message_str, this_ptr, "getoption", option);
		if (!zend_is_true(message_str)) {
			PHALCON_INIT_NVAR(message_str);
			PHALCON_CONCAT_VS(message_str, attribute, " does not have the expected value");
		}
	
		PHALCON_INIT_VAR(type);
		ZVAL_STRING(type, "Identical", 1);
	
		PHALCON_INIT_VAR(message);
		object_init_ex(message, phalcon_validation_message_ce);
		PHALCON_CALL_METHOD_PARAMS_3_NORETURN(message, "__construct", message_str, attribute, type);
	
		PHALCON_CALL_METHOD_PARAMS_1_NORETURN(validator, "appendmessage", message);
		RETURN_MM_FALSE;
	}
	
	RETURN_MM_TRUE;
}
Esempio n. 6
0
/**
 * Check if HTTP method match any of the passed methods
 *
 * @param string|array $methods
 * @return boolean
 */
PHP_METHOD(Phalcon_Http_Request, isMethod){

	zval *methods, http_method = {}, *method;

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

	PHALCON_CALL_METHODW(&http_method, getThis(), "getmethod");

	if (Z_TYPE_P(methods) == IS_STRING) {
		is_equal_function(return_value, methods, &http_method);
		return;
	}

	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(methods), method) {
		if (PHALCON_IS_EQUAL(method, &http_method)) {
			RETURN_TRUE;
		}
	} ZEND_HASH_FOREACH_END();

	RETURN_FALSE;
}
Esempio n. 7
0
/**
 * Check if HTTP method match any of the passed methods
 *
 * @param string|array $methods
 * @return boolean
 */
PHP_METHOD(Phalcon_Http_Request, isMethod){

	zval *methods, *http_method, *is_equals, *method = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &methods) == FAILURE) {
		RETURN_MM_NULL();
	}

	PHALCON_INIT_VAR(http_method);
	PHALCON_CALL_METHOD(http_method, this_ptr, "getmethod");
	if (Z_TYPE_P(methods) == IS_STRING) {
		PHALCON_INIT_VAR(is_equals);
		is_equal_function(is_equals, methods, http_method TSRMLS_CC);
		RETURN_NCTOR(is_equals);
	} else {
	
		if (!phalcon_is_iterable(methods, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_FOREACH_VALUE(method);
	
			if (PHALCON_IS_EQUAL(method, http_method)) {
				RETURN_MM_TRUE;
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
	}
	
	RETURN_MM_FALSE;
}
Esempio n. 8
0
/**
 * Gets row in a specific position of the resultset
 *
 * @param int $index
 * @return Phalcon\Mvc\ModelInterface
 */
PHP_METHOD(Phalcon_Mvc_Model_Resultset, offsetGet){

	zval *index, *count = NULL, *pointer, *valid = NULL;

	PHALCON_MM_GROW();

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

	PHALCON_CALL_METHOD(&count, getThis(), "count");
	if (PHALCON_LT(index, count)) {
		/** 
		 * Check if the last record returned is the current requested
		 */
		pointer = phalcon_read_property(getThis(), SL("_pointer"), PH_NOISY);
		if (PHALCON_IS_EQUAL(pointer, index)) {
			PHALCON_RETURN_CALL_METHOD(getThis(), "current");
			RETURN_MM();
		}

		/** 
		 * Move the cursor to the specific position
		 */
		PHALCON_CALL_METHOD(NULL, getThis(), "seek", index);

		/** 
		 * Check if the last record returned is the requested
		 */
		PHALCON_CALL_METHOD(&valid, getThis(), "valid");
		if (PHALCON_IS_NOT_FALSE(valid)) {
			PHALCON_RETURN_CALL_METHOD(getThis(), "current");
			RETURN_MM();
		}

		RETURN_MM_FALSE;
	}

	PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The index does not exist in the cursor");
	return;
}
Esempio n. 9
0
/**
 * Removes transactions from the TransactionManager
 *
 * @param Phalcon\Mvc\Model\TransactionInterface $transaction
 */
PHP_METHOD(Phalcon_Mvc_Model_Transaction_Manager, _collectTransaction){

	zval *transaction, *transactions, *new_transactions;
	zval *managed_transaction = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &transaction);
	
	PHALCON_OBS_VAR(transactions);
	phalcon_read_property_this(&transactions, this_ptr, SL("_transactions"), PH_NOISY_CC);
	if (phalcon_fast_count_ev(transactions TSRMLS_CC)) {
	
		PHALCON_INIT_VAR(new_transactions);
		array_init(new_transactions);
	
		phalcon_is_iterable(transactions, &ah0, &hp0, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_HVALUE(managed_transaction);
	
			if (PHALCON_IS_EQUAL(managed_transaction, transaction)) {
				phalcon_array_append(&new_transactions, transaction, PH_SEPARATE);
				phalcon_property_decr(this_ptr, SL("_number") TSRMLS_CC);
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
		phalcon_update_property_this(this_ptr, SL("_transactions"), new_transactions TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
}
Esempio n. 10
0
/**
 * Executes the validation
 *
 * @param string $value
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_Json, valid){

	zval *value, assoc = {}, valid = {}, json = {}, *constant, ret = {}, option = {}, keys = {};

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

	ZVAL_TRUE(&valid);
	ZVAL_TRUE(&assoc);

	PHALCON_CALL_FUNCTIONW(&json, "json_decode", value, &assoc);

	if (Z_TYPE(json) == IS_NULL) {
		if ((constant = zend_get_constant_str(SL("JSON_ERROR_NONE"))) != NULL) {
			PHALCON_CALL_FUNCTIONW(&ret, "json_last_error");

			if (!PHALCON_IS_EQUAL(&ret, constant)) {
				ZVAL_FALSE(&valid);
			}
		}
	}

	if (!zend_is_true(&valid)) {
		RETURN_FALSE;
	}

	ZVAL_STRING(&option, "keys");

	PHALCON_CALL_METHODW(&keys, getThis(), "getoption", &option);

	if (Z_TYPE(keys) != IS_NULL) {
		PHALCON_CALL_FUNCTIONW(&ret, "array_key_exists", &keys, &json);
		if (!zend_is_true(&ret)) {
			RETURN_FALSE;
		}
	}

	RETURN_TRUE;
}
Esempio n. 11
0
/**
 * Check if HTTP method match any of the passed methods
 *
 * @param string|array $methods
 * @return boolean
 */
PHP_METHOD(Phalcon_Http_Request, isMethod){

	zval *methods, *http_method, *is_equals, *method = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &methods);
	
	PHALCON_INIT_VAR(http_method);
	phalcon_call_method(http_method, this_ptr, "getmethod");

	if (Z_TYPE_P(methods) == IS_STRING) {
		PHALCON_INIT_VAR(is_equals);
		is_equal_function(is_equals, methods, http_method TSRMLS_CC);
		RETURN_NCTOR(is_equals);
	} else {
	
		phalcon_is_iterable(methods, &ah0, &hp0, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_HVALUE(method);
	
			if (PHALCON_IS_EQUAL(method, http_method)) {
				RETURN_MM_TRUE;
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
	}
	
	RETURN_MM_FALSE;
}
Esempio n. 12
0
/**
 * Returns an array of prepared attributes for Phalcon\Tag helpers
 * according to the element's parameters
 *
 * @param array $attributes
 * @param boolean $useChecked
 * @return array
 */
PHP_METHOD(Phalcon_Forms_Element, prepareAttributes){

	zval *attributes = NULL, *use_checked = NULL, *name, *widget_attributes = NULL;
	zval *default_attributes, *merged_attributes = NULL;
	zval *value, *current_value;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 0, 2, &attributes, &use_checked);
	
	if (!attributes) {
		PHALCON_INIT_VAR(attributes);
	}
	
	if (!use_checked) {
		PHALCON_INIT_VAR(use_checked);
		ZVAL_BOOL(use_checked, 0);
	}
	
	PHALCON_OBS_VAR(name);
	phalcon_read_property_this(&name, this_ptr, SL("_name"), PH_NOISY_CC);
	
	/** 
	 * Create an array of parameters
	 */
	if (Z_TYPE_P(attributes) != IS_ARRAY) { 
		PHALCON_INIT_VAR(widget_attributes);
		array_init(widget_attributes);
	} else {
		PHALCON_CPY_WRT(widget_attributes, attributes);
	}
	
	phalcon_array_update_long(&widget_attributes, 0, &name, PH_COPY | PH_SEPARATE TSRMLS_CC);
	
	/** 
	 * Merge passed parameters with default ones
	 */
	PHALCON_OBS_VAR(default_attributes);
	phalcon_read_property_this(&default_attributes, this_ptr, SL("_attributes"), PH_NOISY_CC);
	if (Z_TYPE_P(default_attributes) == IS_ARRAY) { 
		PHALCON_INIT_VAR(merged_attributes);
		phalcon_fast_array_merge(merged_attributes, &default_attributes, &widget_attributes TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(merged_attributes, widget_attributes);
	}
	
	/** 
	 * Get the current element's value
	 */
	PHALCON_INIT_VAR(value);
	phalcon_call_method(value, this_ptr, "getvalue");
	
	/** 
	 * If the widget has a value set it as default value
	 */
	if (Z_TYPE_P(value) != IS_NULL) {
		if (zend_is_true(use_checked)) {
	
			/** 
			 * Check if the element already has a default value, compare it with the one in the
			 * attributes, if they are the same mark the element as checked
			 */
			if (phalcon_array_isset_string(merged_attributes, SS("value"))) {
	
				PHALCON_OBS_VAR(current_value);
				phalcon_array_fetch_string(&current_value, merged_attributes, SL("value"), PH_NOISY_CC);
				if (PHALCON_IS_EQUAL(current_value, value)) {
					phalcon_array_update_string_string(&merged_attributes, SL("checked"), SL("checked"), PH_SEPARATE TSRMLS_CC);
				}
			} else {
				/** 
				 * Evaluate the current value and mark the check as checked
				 */
				if (zend_is_true(value)) {
					phalcon_array_update_string_string(&merged_attributes, SL("checked"), SL("checked"), PH_SEPARATE TSRMLS_CC);
				}
				phalcon_array_update_string(&merged_attributes, SL("value"), &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
			}
		} else {
			phalcon_array_update_string(&merged_attributes, SL("value"), &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
	}
	
	RETURN_CCTOR(merged_attributes);
}
Esempio n. 13
0
/**
 * Returns a slice of the resultset to show in the pagination
 *
 * @return \stdClass
 */
PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate) {

    zval show = {}, config = {}, items = {}, page_number = {}, rowcount = {}, page = {}, last_show_page = {}, start = {}, possible_pages = {}, total_pages = {};
    zval page_items = {}, maximum_pages = {}, next = {}, additional_page = {}, before = {}, remainder = {}, pages_total = {};
    long int i, i_show;

    phalcon_return_property(&show, getThis(), SL("_limitRows"));
    phalcon_return_property(&config, getThis(), SL("_config"));
    phalcon_return_property(&page_number, getThis(), SL("_page"));

    i_show = phalcon_get_intval(&show);

    phalcon_array_fetch_str(&items, &config, SL("data"), PH_NOISY);

    if (Z_TYPE(page_number) == IS_NULL || PHALCON_LT(&show, &PHALCON_GLOBAL(z_zero))) {
        PHALCON_CPY_WRT_CTOR(&page_number, &PHALCON_GLOBAL(z_one));
    }

    phalcon_fast_count(&rowcount, &items);

    object_init(&page);

    phalcon_sub_function(&last_show_page, &page_number, &PHALCON_GLOBAL(z_one));

    mul_function(&start, &show, &last_show_page);
    phalcon_div_function(&possible_pages, &rowcount, &show);

    if (unlikely(Z_TYPE(possible_pages)) != IS_DOUBLE) {
        convert_to_double(&possible_pages);
    }

    ZVAL_LONG(&total_pages, (long int)ceil(Z_DVAL(possible_pages)));
    if (Z_TYPE(items) != IS_OBJECT) {
        PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "Invalid data for paginator");
        return;
    }

    array_init(&page_items);
    if (PHALCON_GT(&rowcount, &PHALCON_GLOBAL(z_zero))) {
        /**
         * Seek to the desired position
         */
        if (PHALCON_LT(&start, &rowcount)) {
            PHALCON_CALL_METHODW(NULL, &items, "seek", &start);
        } else {
            PHALCON_CALL_METHODW(NULL, &items, "rewind");
            PHALCON_CPY_WRT_CTOR(&page_number, &PHALCON_GLOBAL(z_one));
            PHALCON_CPY_WRT_CTOR(&start, &PHALCON_GLOBAL(z_zero));
        }

        /**
         * The record must be iterable
         */
        for (i=1; ; ++i) {
            zval valid = {}, current = {};
            PHALCON_CALL_METHODW(&valid, &items, "valid");
            if (!PHALCON_IS_NOT_FALSE(&valid)) {
                break;
            }

            PHALCON_CALL_METHODW(&current, &items, "current");
            phalcon_array_append(&page_items, &current, PH_COPY);

            if (i >= i_show) {
                break;
            }
        }
    }

    phalcon_update_property_zval(&page, SL("items"), &page_items);

    phalcon_add_function(&maximum_pages, &start, &show);
    if (PHALCON_LT(&maximum_pages, &rowcount)) {
        phalcon_add_function(&next, &page_number, &PHALCON_GLOBAL(z_one));
    } else if (PHALCON_IS_EQUAL(&maximum_pages, &rowcount)) {
        PHALCON_CPY_WRT_CTOR(&next, &rowcount);
    } else {
        phalcon_div_function(&possible_pages, &rowcount, &show);

        phalcon_add_function(&additional_page, &possible_pages, &PHALCON_GLOBAL(z_one));

        ZVAL_LONG(&next, phalcon_get_intval(&additional_page));
    }

    if (PHALCON_GT(&next, &total_pages)) {
        PHALCON_CPY_WRT_CTOR(&next, &total_pages);
    }

    phalcon_update_property_zval(&page, SL("next"), &next);
    if (PHALCON_GT(&page_number, &PHALCON_GLOBAL(z_one))) {
        phalcon_sub_function(&before, &page_number, &PHALCON_GLOBAL(z_one));
    } else {
        PHALCON_CPY_WRT_CTOR(&before, &PHALCON_GLOBAL(z_one));
    }

    phalcon_update_property_zval(&page, SL("first"), &PHALCON_GLOBAL(z_one));
    phalcon_update_property_zval(&page, SL("before"), &before);
    phalcon_update_property_zval(&page, SL("current"), &page_number);

    mod_function(&remainder, &rowcount, &show);

    phalcon_div_function(&possible_pages, &rowcount, &show);
    if (!PHALCON_IS_LONG(&remainder, 0)) {
        phalcon_add_function(&next, &possible_pages, &PHALCON_GLOBAL(z_one));

        ZVAL_LONG(&pages_total, phalcon_get_intval(&next));
    } else {
        PHALCON_CPY_WRT_CTOR(&pages_total, &possible_pages);
    }

    phalcon_update_property_zval(&page, SL("last"), &pages_total);
    phalcon_update_property_zval(&page, SL("total_pages"), &pages_total);
    phalcon_update_property_zval(&page, SL("total_items"), &rowcount);

    RETURN_CTORW(&page);
}
Esempio n. 14
0
/**
 * Executes the validator
 *
 * @param Phalcon\Mvc\ModelInterface $record
 * @return boolean
 */
PHP_METHOD(Phalcon_Mvc_Model_Validator_Json, validate){

	zval *record, *option = NULL, *field_name = NULL, *invalid = NULL;
	zval *value = NULL, *keys = NULL, *assoc, *json = NULL, *constant, *ret = NULL;
	zval *message = NULL, *type, *is_set_code = NULL, *code = NULL;

	PHALCON_MM_GROW();

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

	PHALCON_INIT_VAR(option);
	ZVAL_STRING(option, "field", 1);

	PHALCON_CALL_METHOD(&field_name, this_ptr, "getoption", option);
	if (Z_TYPE_P(field_name) != IS_STRING) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Field name must be a string");
		return;
	}

	PHALCON_INIT_VAR(invalid);
	ZVAL_BOOL(invalid, 0);
	
	PHALCON_CALL_METHOD(&value, record, "readattribute", field_name);

	PHALCON_INIT_VAR(assoc);
	ZVAL_TRUE(assoc);

	PHALCON_CALL_FUNCTION(&json, "json_decode", value, assoc);

	if (Z_TYPE_P(json) == IS_NULL) {
		PHALCON_INIT_VAR(constant);
		if (zend_get_constant(SL("JSON_ERROR_NONE"), constant TSRMLS_CC)) {
			PHALCON_INIT_NVAR(ret);
			PHALCON_CALL_FUNCTION(&ret, "json_last_error");

			if (!PHALCON_IS_EQUAL(ret, constant)) {
				PHALCON_INIT_NVAR(invalid);
				ZVAL_BOOL(invalid, 1);
			}
		}
	}

	if (!PHALCON_IS_TRUE(invalid)) {
		PHALCON_INIT_NVAR(option);
		ZVAL_STRING(option, "keys", 1);

		PHALCON_CALL_METHOD(&keys, this_ptr, "getoption", option);

		if (Z_TYPE_P(keys) != IS_NULL) {
			PHALCON_INIT_NVAR(ret);
			PHALCON_CALL_FUNCTION(&ret, "array_key_exists", keys, json);
			if (!zend_is_true(ret)) {
				PHALCON_INIT_NVAR(invalid);
				ZVAL_BOOL(invalid, 1);
			}
		}
	}

	if (PHALCON_IS_TRUE(invalid)) {

		/** 
		 * Check if the developer has defined a custom message
		 */
		PHALCON_INIT_NVAR(option);
		PHALCON_ZVAL_MAYBE_INTERNED_STRING(option, phalcon_interned_message);
	
		PHALCON_CALL_METHOD(&message, this_ptr, "getoption", option);
		if (!zend_is_true(message)) {
			PHALCON_INIT_NVAR(message);
			PHALCON_CONCAT_SVS(message, "Value of field '", field_name, "' must have a valid json format");
		}

		PHALCON_INIT_VAR(type);
		ZVAL_STRING(type, "Json", 1);

		/*
		 * Is code set
		 */
		PHALCON_INIT_NVAR(option);
		PHALCON_ZVAL_MAYBE_INTERNED_STRING(option, phalcon_interned_code);

		PHALCON_CALL_METHOD(&is_set_code, this_ptr, "issetoption", option);
		if (zend_is_true(is_set_code)) {
			PHALCON_CALL_METHOD(&code, this_ptr, "getoption", option);
		} else {
			PHALCON_INIT_VAR(code);
			ZVAL_LONG(code, 0);
		}

		PHALCON_CALL_METHOD(NULL, this_ptr, "appendmessage", message, field_name, type, code);
		RETURN_MM_FALSE;
	}

	RETURN_MM_TRUE;
}
Esempio n. 15
0
/**
 * Phalcon\Db\Reference constructor
 *
 * @param string $referenceName
 * @param array $definition
 */
PHP_METHOD(Phalcon_Db_Reference, __construct){

	zval *reference_name, *definition, *referenced_table;
	zval *columns, *referenced_columns, *schema;
	zval *referenced_schema, *number_columns;
	zval *number_referenced_columns;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &reference_name, &definition);
	
	phalcon_update_property_this(this_ptr, SL("_referenceName"), reference_name TSRMLS_CC);
	if (phalcon_array_isset_string(definition, SS("referencedTable"))) {
		PHALCON_OBS_VAR(referenced_table);
		phalcon_array_fetch_string(&referenced_table, definition, SL("referencedTable"), PH_NOISY);
		phalcon_update_property_this(this_ptr, SL("_referencedTable"), referenced_table TSRMLS_CC);
	} else {
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Referenced table is required");
		return;
	}
	
	if (phalcon_array_isset_string(definition, SS("columns"))) {
		PHALCON_OBS_VAR(columns);
		phalcon_array_fetch_string(&columns, definition, SL("columns"), PH_NOISY);
		phalcon_update_property_this(this_ptr, SL("_columns"), columns TSRMLS_CC);
	} else {
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Foreign key columns are required");
		return;
	}
	
	if (phalcon_array_isset_string(definition, SS("referencedColumns"))) {
		PHALCON_OBS_VAR(referenced_columns);
		phalcon_array_fetch_string(&referenced_columns, definition, SL("referencedColumns"), PH_NOISY);
		phalcon_update_property_this(this_ptr, SL("_referencedColumns"), referenced_columns TSRMLS_CC);
	} else {
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Referenced columns of the foreign key are required");
		return;
	}
	
	if (phalcon_array_isset_string(definition, SS("schema"))) {
		PHALCON_OBS_VAR(schema);
		phalcon_array_fetch_string(&schema, definition, SL("schema"), PH_NOISY);
		phalcon_update_property_this(this_ptr, SL("_schemaName"), schema TSRMLS_CC);
	}
	
	if (phalcon_array_isset_string(definition, SS("referencedSchema"))) {
		PHALCON_OBS_VAR(referenced_schema);
		phalcon_array_fetch_string(&referenced_schema, definition, SL("referencedSchema"), PH_NOISY);
		phalcon_update_property_this(this_ptr, SL("_referencedSchema"), referenced_schema TSRMLS_CC);
	}
	
	PHALCON_INIT_VAR(number_columns);
	phalcon_fast_count(number_columns, columns TSRMLS_CC);
	
	PHALCON_INIT_VAR(number_referenced_columns);
	phalcon_fast_count(number_referenced_columns, referenced_columns TSRMLS_CC);
	if (!PHALCON_IS_EQUAL(number_columns, number_referenced_columns)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Number of columns is not equals than the number of columns referenced");
		return;
	}
	
	PHALCON_MM_RESTORE();
}
Esempio n. 16
0
/**
 * Listens for notifications from the models manager
 *
 * @param string $type
 * @param Phalcon\Mvc\ModelInterface $model
 */
PHP_METHOD(Phalcon_Mvc_Model_Behavior_SoftDelete, notify){

	zval *type, *model, *options, *skip, *value, *field, *actual_value;
	zval *update_model, *status, *messages, *message = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &type, &model);
	
	if (PHALCON_IS_STRING(type, "beforeDelete")) {
	
		PHALCON_INIT_VAR(options);
		phalcon_call_method(options, this_ptr, "getoptions");
		if (!phalcon_array_isset_string(options, SS("value"))) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The option 'value' is required");
			return;
		}
	
		if (!phalcon_array_isset_string(options, SS("field"))) {
			PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The option 'field' is required");
			return;
		}
	
		PHALCON_INIT_VAR(skip);
		ZVAL_BOOL(skip, 1);
	
		/** 
		 * Skip the current operation
		 */
		phalcon_call_method_p1_noret(model, "skipoperation", skip);
	
		/** 
		 * 'value' is the value to be updated instead of delete the record
		 */
		PHALCON_OBS_VAR(value);
		phalcon_array_fetch_string(&value, options, SL("value"), PH_NOISY_CC);
	
		/** 
		 * 'field' is the attribute to be updated instead of delete the record
		 */
		PHALCON_OBS_VAR(field);
		phalcon_array_fetch_string(&field, options, SL("field"), PH_NOISY_CC);
	
		PHALCON_INIT_VAR(actual_value);
		phalcon_call_method_p1(actual_value, model, "readattribute", field);
	
		/** 
		 * If the record is already flagged as 'deleted' we don't delete it again
		 */
		if (!PHALCON_IS_EQUAL(actual_value, value)) {
	
			/** 
			 * Clone the current model to make a clean new operation
			 */
			PHALCON_INIT_VAR(update_model);
			if (phalcon_clone(update_model, model TSRMLS_CC) == FAILURE) {
				return;
			}
			phalcon_call_method_p2_noret(update_model, "writeattribute", field, value);
	
			/** 
			 * Update the cloned model
			 */
			PHALCON_INIT_VAR(status);
			phalcon_call_method(status, update_model, "save");
			if (!zend_is_true(status)) {
	
				/** 
				 * Transfer the messages from the cloned model to the original model
				 */
				PHALCON_INIT_VAR(messages);
				phalcon_call_method(messages, update_model, "getmessages");
	
				phalcon_is_iterable(messages, &ah0, &hp0, 0, 0);
	
				while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
					PHALCON_GET_HVALUE(message);
	
					phalcon_call_method_p1_noret(model, "appendmessage", message);
	
					zend_hash_move_forward_ex(ah0, &hp0);
				}
	
				RETURN_MM_FALSE;
			}
	
			/** 
			 * Update the original model too
			 */
			phalcon_call_method_p2_noret(model, "writeattribute", field, value);
		}
	}
	
	PHALCON_MM_RESTORE();
}
Esempio n. 17
0
/**
 * Do a role inherit from another existing role
 *
 * @param string $roleName
 * @param string $roleToInherit
 */
PHP_METHOD(Phalcon_Acl_Adapter_Memory, addInherit){

	zval *role_name, *role_to_inherit, *roles_names;
	zval *exception_message = NULL, *role_inherit_name = NULL;
	zval *roles_inherits, *empty_arr, *_roleInherits;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &role_name, &role_to_inherit) == FAILURE) {
		RETURN_MM_NULL();
	}

	PHALCON_OBS_VAR(roles_names);
	phalcon_read_property(&roles_names, this_ptr, SL("_rolesNames"), PH_NOISY_CC);
	if (!phalcon_array_isset(roles_names, role_name)) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Role '", role_name, "' does not exist in the role list");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
		return;
	}
	
	if (Z_TYPE_P(role_to_inherit) == IS_OBJECT) {
		PHALCON_INIT_VAR(role_inherit_name);
		PHALCON_CALL_METHOD(role_inherit_name, role_to_inherit, "getname");
	} else {
		PHALCON_CPY_WRT(role_inherit_name, role_to_inherit);
	}
	
	/** 
	 * Check if the role to inherit is valid
	 */
	if (!phalcon_array_isset(roles_names, role_inherit_name)) {
		PHALCON_INIT_NVAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Role '", role_inherit_name, "' (to inherit) does not exist in the role list");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
		return;
	}
	
	if (PHALCON_IS_EQUAL(role_inherit_name, role_name)) {
		RETURN_MM_FALSE;
	}
	
	PHALCON_OBS_VAR(roles_inherits);
	phalcon_read_property(&roles_inherits, this_ptr, SL("_roleInherits"), PH_NOISY_CC);
	if (!phalcon_array_isset(roles_inherits, role_name)) {
		PHALCON_INIT_VAR(empty_arr);
		array_init(empty_arr);
		phalcon_update_property_array(this_ptr, SL("_roleInherits"), role_name, empty_arr TSRMLS_CC);
	}
	
	PHALCON_OBS_VAR(_roleInherits);
	phalcon_read_property(&_roleInherits, this_ptr, SL("_roleInherits"), PH_NOISY_CC);
	phalcon_array_update_append_multi_2(&_roleInherits, role_name, role_inherit_name, 0 TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_roleInherits"), _roleInherits TSRMLS_CC);
	
	/** 
	 * Re-build the access list with its inherited roles
	 */
	PHALCON_CALL_METHOD_NORETURN(this_ptr, "_rebuildaccesslist");
	RETURN_MM_TRUE;
}
Esempio n. 18
0
/**
 * Check whether a role is allowed to access an action from a resource
 *
 * <code>
 * //Does andres have access to the customers resource to create?
 * $acl->isAllowed('andres', 'Products', 'create');
 *
 * //Do guests have access to any resource to edit?
 * $acl->isAllowed('guests', '*', 'edit');
 * </code>
 *
 * @param  string $role
 * @param  string $resource
 * @param  string $access
 * @return boolean
 */
PHP_METHOD(Phalcon_Acl_Adapter_Memory, isAllowed){

	zval *role, *resource, *access, *events_manager;
	zval *event_name = NULL, *status, *default_access, *roles_names;
	zval *have_access = NULL, *access_roles, *resource_access = NULL;
	zval *resource_name = NULL;
	zval *t0 = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", &role, &resource, &access) == FAILURE) {
		RETURN_MM_NULL();
	}

	phalcon_update_property_zval(this_ptr, SL("_activeRole"), role TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_activeResource"), resource TSRMLS_CC);
	phalcon_update_property_zval(this_ptr, SL("_activeAccess"), access TSRMLS_CC);
	
	PHALCON_OBS_VAR(events_manager);
	phalcon_read_property(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC);
	if (Z_TYPE_P(events_manager) == IS_OBJECT) {
	
		PHALCON_INIT_VAR(event_name);
		ZVAL_STRING(event_name, "acl:beforeCheckAccess", 1);
	
		PHALCON_INIT_VAR(status);
		PHALCON_CALL_METHOD_PARAMS_2(status, events_manager, "fire", event_name, this_ptr);
		if (PHALCON_IS_FALSE(status)) {
			RETURN_CCTOR(status);
		}
	}
	
	PHALCON_OBS_VAR(default_access);
	phalcon_read_property(&default_access, this_ptr, SL("_defaultAccess"), PH_NOISY_CC);
	
	/** 
	 * Check if the role exists
	 */
	PHALCON_OBS_VAR(roles_names);
	phalcon_read_property(&roles_names, this_ptr, SL("_rolesNames"), PH_NOISY_CC);
	if (!phalcon_array_isset(roles_names, role)) {
		RETURN_CCTOR(default_access);
	}
	
	PHALCON_INIT_VAR(have_access);
	
	PHALCON_OBS_VAR(t0);
	phalcon_read_property(&t0, this_ptr, SL("_access"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(access_roles);
	phalcon_array_fetch(&access_roles, t0, role, PH_NOISY_CC);
	
	if (!phalcon_is_iterable(access_roles, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
		return;
	}
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_FOREACH_KEY(resource_name, ah0, hp0);
		PHALCON_GET_FOREACH_VALUE(resource_access);
	
		if (PHALCON_IS_EQUAL(resource_name, resource)) {
			if (phalcon_array_isset(resource_access, access)) {
				PHALCON_OBS_NVAR(have_access);
				phalcon_array_fetch(&have_access, resource_access, access, PH_NOISY_CC);
				break;
			}
	
			PHALCON_OBS_NVAR(have_access);
			phalcon_array_fetch_string(&have_access, resource_access, SL("*"), PH_NOISY_CC);
			break;
		}
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	if (Z_TYPE_P(have_access) == IS_NULL) {
	
		if (!phalcon_is_iterable(access_roles, &ah1, &hp1, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
			PHALCON_GET_FOREACH_KEY(resource_name, ah1, hp1);
			PHALCON_GET_FOREACH_VALUE(resource_access);
	
			if (phalcon_array_isset_string(resource_access, SS("*"))) {
				if (phalcon_array_isset(resource_access, access)) {
					PHALCON_OBS_NVAR(have_access);
					phalcon_array_fetch(&have_access, resource_access, access, PH_NOISY_CC);
					break;
				}
	
				PHALCON_OBS_NVAR(have_access);
				phalcon_array_fetch_string(&have_access, resource_access, SL("*"), PH_NOISY_CC);
				break;
			}
	
			zend_hash_move_forward_ex(ah1, &hp1);
		}
	
	}
	
	phalcon_update_property_zval(this_ptr, SL("_accessGranted"), have_access TSRMLS_CC);
	if (Z_TYPE_P(events_manager) == IS_OBJECT) {
		PHALCON_INIT_NVAR(event_name);
		ZVAL_STRING(event_name, "acl:afterCheckAccess", 1);
		PHALCON_CALL_METHOD_PARAMS_2_NORETURN(events_manager, "fire", event_name, this_ptr);
	}
	
	if (Z_TYPE_P(have_access) == IS_NULL) {
		PHALCON_MM_RESTORE();
		RETURN_LONG(0);
	}
	
	
	RETURN_CCTOR(have_access);
}
Esempio n. 19
0
/**
 * Executes the validation
 *
 * @param Phalcon\Validation $validator
 * @param string $attribute
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_Confirmation, validate){

	zval *validator, *attribute, *with_attribute;
	zval *value = NULL, *with_value = NULL, *message_str, *message, *code;
	zval *label, *with_label, *pairs, *prepared = NULL;
	zend_class_entry *ce = Z_OBJCE_P(getThis());

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &validator, &attribute);
	
	PHALCON_VERIFY_CLASS_EX(validator, phalcon_validation_ce, phalcon_validation_exception_ce, 1);

	PHALCON_OBS_VAR(with_attribute);
	RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &with_attribute, getThis(), "with" TSRMLS_CC));
	
	PHALCON_CALL_METHOD(&value,      validator, "getvalue", attribute);
	PHALCON_CALL_METHOD(&with_value, validator, "getvalue", with_attribute);

	if (!PHALCON_IS_EQUAL(value, with_value)) {
		PHALCON_OBS_VAR(label);
		RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &label, getThis(), phalcon_interned_label TSRMLS_CC));
		if (!zend_is_true(label)) {
			PHALCON_CALL_METHOD(&label, validator, "getlabel", attribute);
			if (!zend_is_true(label)) {
				PHALCON_CPY_WRT(label, attribute);
			}
		}
                
                PHALCON_OBS_VAR(with_label);
		RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &with_label, getThis(), phalcon_interned_label TSRMLS_CC));
		if (!zend_is_true(with_label)) {
			PHALCON_CALL_METHOD(&with_label, validator, "getlabel", with_attribute);
			if (!zend_is_true(with_label)) {
				PHALCON_CPY_WRT(with_label, with_attribute);
			}
		}

		PHALCON_ALLOC_GHOST_ZVAL(pairs);
		array_init_size(pairs, 2);
		Z_ADDREF_P(label);          add_assoc_zval_ex(pairs, SS(":field"), label);
		Z_ADDREF_P(with_label); add_assoc_zval_ex(pairs, SS(":with"), with_label);

		PHALCON_OBS_VAR(message_str);
		RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &message_str, getThis(), phalcon_interned_message TSRMLS_CC));
		if (!zend_is_true(message_str)) {
			PHALCON_OBSERVE_OR_NULLIFY_VAR(message_str);
			RETURN_MM_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(Z_OBJCE_P(validator), &message_str, validator, "Confirmation" TSRMLS_CC));
		}
	
		PHALCON_OBS_VAR(code);
		RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &code, getThis(), phalcon_interned_code TSRMLS_CC));
		if (Z_TYPE_P(code) == IS_NULL) {
			ZVAL_LONG(code, 0);
		}

		PHALCON_CALL_FUNCTION(&prepared, "strtr", message_str, pairs);
	
		message = phalcon_validation_message_construct_helper(prepared, attribute, "Confirmation", code TSRMLS_CC);
		Z_DELREF_P(message);
	
		PHALCON_CALL_METHOD(NULL, validator, "appendmessage", message);
		RETURN_MM_FALSE;
	}
	
	RETURN_MM_TRUE;
}
Esempio n. 20
0
/**
 * Returns a slice of the resultset to show in the pagination
 *
 * @return stdClass
 */
PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate){

	zval *one, *zero, *show, *config, *items, *page_number = NULL;
	zval *n, *page, *last_show_page, *start;
	zval *possible_pages = NULL, *total_pages, *page_items;
	zval *i, *valid = NULL, *current = NULL, *maximum_pages, *next = NULL, *additional_page;
	zval *before = NULL, *remainder, *pages_total = NULL;
	zval *r0 = NULL;

	PHALCON_MM_GROW();

	PHALCON_INIT_VAR(one);
	ZVAL_LONG(one, 1);
	
	PHALCON_INIT_VAR(zero);
	ZVAL_LONG(zero, 0);
	
	PHALCON_OBS_VAR(show);
	phalcon_read_property_this(&show, this_ptr, SL("_limitRows"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(config);
	phalcon_read_property_this(&config, this_ptr, SL("_config"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(items);
	phalcon_array_fetch_string(&items, config, SL("data"), PH_NOISY);
	
	PHALCON_OBS_VAR(page_number);
	phalcon_read_property_this(&page_number, this_ptr, SL("_page"), PH_NOISY_CC);
	if (Z_TYPE_P(page_number) == IS_NULL) {
		PHALCON_CPY_WRT(page_number, one);
	}
	
	if (PHALCON_LT(show, zero)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_paginator_exception_ce, "The start page number is zero or less");
		return;
	}
	
	PHALCON_INIT_VAR(n);
	phalcon_fast_count(n, items TSRMLS_CC);
	
	PHALCON_INIT_VAR(page);
	object_init(page);
	
	PHALCON_INIT_VAR(last_show_page);
	sub_function(last_show_page, page_number, one TSRMLS_CC);
	
	PHALCON_INIT_VAR(start);
	mul_function(start, show, last_show_page TSRMLS_CC);
	
	PHALCON_INIT_VAR(possible_pages);
	div_function(possible_pages, n, show TSRMLS_CC);
	
	PHALCON_INIT_VAR(total_pages);
	phalcon_call_func_p1(total_pages, "ceil", possible_pages);
	if (Z_TYPE_P(items) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_paginator_exception_ce, "Invalid data for paginator");
		return;
	}
	
	if (!zend_is_true(page_number)) {
		PHALCON_CPY_WRT(page_number, one);
	}
	
	PHALCON_INIT_VAR(page_items);
	array_init(page_items);
	if (PHALCON_GT(n, zero)) {
	
		/** 
		 * Seek to the desired position
		 */
		if (PHALCON_LE(start, n)) {
			phalcon_call_method_p1_noret(items, "seek", start);
		} else {
			phalcon_call_method_p1_noret(items, "seek", one);
			PHALCON_CPY_WRT(page_number, one);
		}
	
		/** 
		 * The record must be iterable
		 */
		PHALCON_INIT_VAR(i);
		ZVAL_LONG(i, 1);
	
		while (1) {
	
			PHALCON_INIT_NVAR(r0);
			phalcon_call_method(r0, items, "valid");
			PHALCON_CPY_WRT(valid, r0);
			if (PHALCON_IS_NOT_FALSE(valid)) {
			} else {
				break;
			}
	
			PHALCON_INIT_NVAR(current);
			phalcon_call_method(current, items, "current");
			phalcon_array_append(&page_items, current, PH_SEPARATE);
			if (PHALCON_GE(i, show)) {
				break;
			}
	
			phalcon_increment(i);
		}
	}
	
	phalcon_update_property_zval(page, SL("items"), page_items TSRMLS_CC);
	
	PHALCON_INIT_VAR(maximum_pages);
	phalcon_add_function(maximum_pages, start, show TSRMLS_CC);
	if (PHALCON_LT(maximum_pages, n)) {
		PHALCON_INIT_VAR(next);
		phalcon_add_function(next, page_number, one TSRMLS_CC);
	} else {
		if (PHALCON_IS_EQUAL(maximum_pages, n)) {
			PHALCON_CPY_WRT(next, n);
		} else {
			div_function(possible_pages, n, show TSRMLS_CC);
	
			PHALCON_INIT_VAR(additional_page);
			phalcon_add_function(additional_page, possible_pages, one TSRMLS_CC);
	
			PHALCON_INIT_NVAR(next);
			ZVAL_LONG(next, phalcon_get_intval(additional_page));
		}
	}
	
	if (PHALCON_GT(next, total_pages)) {
		PHALCON_CPY_WRT(next, total_pages);
	}
	
	phalcon_update_property_zval(page, SL("next"), next TSRMLS_CC);
	if (PHALCON_GT(page_number, one)) {
		PHALCON_INIT_VAR(before);
		sub_function(before, page_number, one TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(before, one);
	}
	
	phalcon_update_property_zval(page, SL("first"), one TSRMLS_CC);
	phalcon_update_property_zval(page, SL("before"), before TSRMLS_CC);
	phalcon_update_property_zval(page, SL("current"), page_number TSRMLS_CC);
	
	PHALCON_INIT_VAR(remainder);
	mod_function(remainder, n, show TSRMLS_CC);
	
	PHALCON_INIT_NVAR(possible_pages);
	div_function(possible_pages, n, show TSRMLS_CC);
	if (!PHALCON_IS_LONG(remainder, 0)) {
		PHALCON_INIT_NVAR(next);
		phalcon_add_function(next, possible_pages, one TSRMLS_CC);
	
		PHALCON_INIT_VAR(pages_total);
		ZVAL_LONG(pages_total, phalcon_get_intval(next));
	} else {
		PHALCON_CPY_WRT(pages_total, possible_pages);
	}
	
	phalcon_update_property_zval(page, SL("last"), pages_total TSRMLS_CC);
	phalcon_update_property_zval(page, SL("total_pages"), pages_total TSRMLS_CC);
	phalcon_update_property_zval(page, SL("total_items"), n TSRMLS_CC);
	
	RETURN_CTOR(page);
}
Esempio n. 21
0
/**
 * Shows a backtrace item
 *
 * @param int $n
 * @param array $trace
 */
PHP_METHOD(Phalcon_Debug, showTraceItem){

	zval *n, *trace, *space, *two_spaces, *underscore;
	zval *minus, *html, *class_name, *pattern, *is_phalcon_class;
	zval *namespace_separator, *prepare_uri_class;
	zval *class_reflection, *is_internal = NULL, *lower_class_name;
	zval *prepare_internal_class, *type, *function_name = NULL;
	zval *function_reflection, *prepared_function_name;
	zval *trace_args, *arguments, *argument = NULL, *dumped_argument = NULL;
	zval *span_argument = NULL, *joined_arguments, *one;
	zval *file, *line, *show_files, *lines, *number_lines;
	zval *show_file_fragment, *seven, *before_line;
	zval *first_line = NULL, *five, *after_line, *last_line = NULL;
	zval *comment_pattern, *utf8, *ent_compat, *tab;
	zval *comment, *i = NULL, *line_position = NULL, *current_line = NULL;
	zval *trimmed = NULL, *is_comment = NULL, *spaced_current_line = NULL;
	zval *escaped_line = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	zend_class_entry *ce0, *ce1;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &n, &trace);
	
	PHALCON_INIT_VAR(space);
	ZVAL_STRING(space, " ", 1);
	
	PHALCON_INIT_VAR(two_spaces);
	ZVAL_STRING(two_spaces, "  ", 1);
	
	PHALCON_INIT_VAR(underscore);
	ZVAL_STRING(underscore, "_", 1);
	
	PHALCON_INIT_VAR(minus);
	ZVAL_STRING(minus, "-", 1);
	
	/** 
	 * Every trace in the backtrace have a unique number
	 */
	PHALCON_INIT_VAR(html);
	PHALCON_CONCAT_SVS(html, "<tr><td align=\"right\" valign=\"top\" class=\"error-number\">#", n, "</td><td>");
	if (phalcon_array_isset_string(trace, SS("class"))) {
	
		PHALCON_OBS_VAR(class_name);
		phalcon_array_fetch_string(&class_name, trace, SL("class"), PH_NOISY);
	
		PHALCON_INIT_VAR(pattern);
		ZVAL_STRING(pattern, "/^Phalcon/", 1);
	
		PHALCON_INIT_VAR(is_phalcon_class);
	
		phalcon_preg_match(is_phalcon_class, pattern, class_name, NULL TSRMLS_CC);
	
		/** 
		 * We assume that classes starting by Phalcon are framework's classes
		 */
		if (zend_is_true(is_phalcon_class)) {
			PHALCON_INIT_VAR(namespace_separator);
			ZVAL_STRING(namespace_separator, "\\", 1);
	
			/** 
			 * Prepare the class name according to the Phalcon's conventions
			 */
			PHALCON_INIT_VAR(prepare_uri_class);
			phalcon_fast_str_replace(prepare_uri_class, namespace_separator, underscore, class_name);
	
			/** 
			 * Generate a link to the official docs
			 */
			PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://docs.phalconphp.com/en/latest/api/", prepare_uri_class, ".html\">", class_name, "</a></span>");
		} else {
			ce0 = zend_fetch_class(SL("ReflectionClass"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
			PHALCON_INIT_VAR(class_reflection);
			object_init_ex(class_reflection, ce0);
			if (phalcon_has_constructor(class_reflection TSRMLS_CC)) {
				phalcon_call_method_p1_noret(class_reflection, "__construct", class_name);
			}
	
			/** 
			 * Check if classes are PHP's classes
			 */
			PHALCON_INIT_VAR(is_internal);
			phalcon_call_method(is_internal, class_reflection, "isinternal");
			if (zend_is_true(is_internal)) {
				PHALCON_INIT_VAR(lower_class_name);
				phalcon_fast_strtolower(lower_class_name, class_name);
	
				PHALCON_INIT_VAR(prepare_internal_class);
				phalcon_fast_str_replace(prepare_internal_class, underscore, minus, lower_class_name);
	
				/** 
				 * Generate a link to the official docs
				 */
				PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://php.net/manual/en/class.", prepare_internal_class, ".php\">", class_name, "</a></span>");
			} else {
				PHALCON_SCONCAT_SVS(html, "<span class=\"error-class\">", class_name, "</span>");
			}
		}
	
		/** 
		 * Object access operator: static/instance
		 */
		PHALCON_OBS_VAR(type);
		phalcon_array_fetch_string(&type, trace, SL("type"), PH_NOISY);
		phalcon_concat_self(&html, type TSRMLS_CC);
	}
	
	/** 
	 * Normally the backtrace contains only classes
	 */
	if (phalcon_array_isset_string(trace, SS("class"))) {
		PHALCON_OBS_VAR(function_name);
		phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY);
		PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
	} else {
		PHALCON_OBS_NVAR(function_name);
		phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY);
	
		/** 
		 * Check if the function exists
		 */
		if (phalcon_function_exists(function_name TSRMLS_CC) == SUCCESS) {
			ce1 = zend_fetch_class(SL("ReflectionFunction"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
			PHALCON_INIT_VAR(function_reflection);
			object_init_ex(function_reflection, ce1);
			if (phalcon_has_constructor(function_reflection TSRMLS_CC)) {
				phalcon_call_method_p1_noret(function_reflection, "__construct", function_name);
			}
	
			PHALCON_INIT_NVAR(is_internal);
			phalcon_call_method(is_internal, function_reflection, "isinternal");
	
			/** 
			 * Internal functions links to the PHP documentation
			 */
			if (zend_is_true(is_internal)) {
				/** 
				 * Prepare function's name according to the conventions in the docs
				 */
				PHALCON_INIT_VAR(prepared_function_name);
				phalcon_fast_str_replace(prepared_function_name, underscore, minus, function_name);
				PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-function\"><a target=\"_new\" href=\"http://php.net/manual/en/function.", prepared_function_name, ".php\">", function_name, "</a></span>");
			} else {
				PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
			}
		} else {
			PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
		}
	}
	
	/** 
	 * Check for arguments in the function
	 */
	if (phalcon_array_isset_string(trace, SS("args"))) {
	
		PHALCON_OBS_VAR(trace_args);
		phalcon_array_fetch_string(&trace_args, trace, SL("args"), PH_NOISY);
		if (phalcon_fast_count_ev(trace_args TSRMLS_CC)) {
	
			PHALCON_INIT_VAR(arguments);
			array_init(arguments);
	
			phalcon_is_iterable(trace_args, &ah0, &hp0, 0, 0);
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_HVALUE(argument);
	
				/** 
				 * Every argument is generated using _getVarDump
				 */
				PHALCON_INIT_NVAR(dumped_argument);
				phalcon_call_method_p1(dumped_argument, this_ptr, "_getvardump", argument);
	
				PHALCON_INIT_NVAR(span_argument);
				PHALCON_CONCAT_SVS(span_argument, "<span class=\"error-parameter\">", dumped_argument, "</span>");
	
				/** 
				 * Append the HTML generated to the argument's list
				 */
				phalcon_array_append(&arguments, span_argument, PH_SEPARATE);
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
			/** 
			 * Join all the arguments
			 */
			PHALCON_INIT_VAR(joined_arguments);
			phalcon_fast_join_str(joined_arguments, SL(", "), arguments TSRMLS_CC);
			PHALCON_SCONCAT_SVS(html, "(", joined_arguments, ")");
		} else {
			phalcon_concat_self_str(&html, SL("()") TSRMLS_CC);
		}
	}
	
	/** 
	 * When 'file' is present, it usually means the function is provided by the user
	 */
	if (phalcon_array_isset_string(trace, SS("file"))) {
	
		PHALCON_INIT_VAR(one);
		ZVAL_LONG(one, 1);
	
		PHALCON_OBS_VAR(file);
		phalcon_array_fetch_string(&file, trace, SL("file"), PH_NOISY);
	
		PHALCON_OBS_VAR(line);
		phalcon_array_fetch_string(&line, trace, SL("line"), PH_NOISY);
	
		/** 
		 * Realpath to the file and its line using a special header
		 */
		PHALCON_SCONCAT_SVSVS(html, "<br/><div class=\"error-file\">", file, " (", line, ")</div>");
	
		PHALCON_OBS_VAR(show_files);
		phalcon_read_property_this(&show_files, this_ptr, SL("_showFiles"), PH_NOISY_CC);
	
		/** 
		 * The developer can change if the files must be opened or not
		 */
		if (zend_is_true(show_files)) {
	
			/** 
			 * Open the file to an array using 'file', this respects the openbase-dir directive
			 */
			PHALCON_INIT_VAR(lines);
			phalcon_call_func_p1(lines, "file", file);
	
			PHALCON_INIT_VAR(number_lines);
			phalcon_fast_count(number_lines, lines TSRMLS_CC);
	
			PHALCON_OBS_VAR(show_file_fragment);
			phalcon_read_property_this(&show_file_fragment, this_ptr, SL("_showFileFragment"), PH_NOISY_CC);
	
			/** 
			 * File fragments just show a piece of the file where the exception is located
			 */
			if (zend_is_true(show_file_fragment)) {
	
				/** 
				 * Take seven lines back to the current exception's line, @TODO add an option for
				 * this
				 */
				PHALCON_INIT_VAR(seven);
				ZVAL_LONG(seven, 7);
	
				PHALCON_INIT_VAR(before_line);
				sub_function(before_line, line, seven TSRMLS_CC);
	
				/** 
				 * Check for overflows
				 */
				if (PHALCON_LT_LONG(before_line, 1)) {
					PHALCON_CPY_WRT(first_line, one);
				} else {
					PHALCON_CPY_WRT(first_line, before_line);
				}
	
				/** 
				 * Take five lines after the current exception's line, @TODO add an option for this
				 */
				PHALCON_INIT_VAR(five);
				ZVAL_LONG(five, 5);
	
				PHALCON_INIT_VAR(after_line);
				phalcon_add_function(after_line, line, five TSRMLS_CC);
	
				/** 
				 * Check for overflows
				 */
				if (PHALCON_GT(after_line, number_lines)) {
					PHALCON_CPY_WRT(last_line, number_lines);
				} else {
					PHALCON_CPY_WRT(last_line, after_line);
				}
	
				PHALCON_SCONCAT_SVSVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums:", first_line, "'>");
			} else {
				PHALCON_CPY_WRT(first_line, one);
				PHALCON_CPY_WRT(last_line, number_lines);
				PHALCON_SCONCAT_SVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums error-scroll'>");
			}
	
			PHALCON_INIT_VAR(comment_pattern);
			ZVAL_STRING(comment_pattern, "#\\*\\/$#", 1);
	
			/** 
			 * We assume the file is utf-8 encoded, @TODO add an option for this
			 */
			PHALCON_INIT_VAR(utf8);
			ZVAL_STRING(utf8, "UTF-8", 1);
	
			/** 
			 * Don't escape quotes
			 */
			PHALCON_INIT_VAR(ent_compat);
			ZVAL_LONG(ent_compat, 2);
	
			PHALCON_INIT_VAR(tab);
			ZVAL_STRING(tab, "\t", 1);
	
			PHALCON_INIT_VAR(comment);
			ZVAL_STRING(comment, "* /", 1);
			PHALCON_CPY_WRT(i, first_line);
	
			while (1) {
	
				if (PHALCON_LE(i, last_line)) {
				} else {
					break;
				}
	
				/** 
				 * Current line in the file
				 */
				PHALCON_INIT_NVAR(line_position);
				sub_function(line_position, i, one TSRMLS_CC);
	
				/** 
				 * Current line content in the piece of file
				 */
				PHALCON_OBS_NVAR(current_line);
				phalcon_array_fetch(&current_line, lines, line_position, PH_NOISY);
	
				/** 
				 * File fragments are cleaned, removing tabs and comments
				 */
				if (zend_is_true(show_file_fragment)) {
					if (PHALCON_IS_EQUAL(i, first_line)) {
	
						PHALCON_INIT_NVAR(trimmed);
						phalcon_fast_trim(trimmed, current_line, PHALCON_TRIM_RIGHT TSRMLS_CC);
	
						PHALCON_INIT_NVAR(is_comment);
	
						phalcon_preg_match(is_comment, comment_pattern, current_line, NULL TSRMLS_CC);
	
						if (zend_is_true(is_comment)) {
							PHALCON_INIT_NVAR(spaced_current_line);
							phalcon_fast_str_replace(spaced_current_line, comment, space, current_line);
							PHALCON_CPY_WRT(current_line, spaced_current_line);
						}
					}
				}
	
				/** 
				 * Print a non break space if the current line is a line break, this allows to show
				 * the html zebra properly
				 */
				if (PHALCON_IS_STRING(current_line, "\n")) {
					phalcon_concat_self_str(&html, SL("&nbsp;\n") TSRMLS_CC);
				} else {
					if (PHALCON_IS_STRING(current_line, "\r\n")) {
						phalcon_concat_self_str(&html, SL("&nbsp;\n") TSRMLS_CC);
					} else {
						PHALCON_INIT_NVAR(spaced_current_line);
						phalcon_fast_str_replace(spaced_current_line, tab, two_spaces, current_line);
	
						PHALCON_INIT_NVAR(escaped_line);
						phalcon_call_func_p3(escaped_line, "htmlentities", spaced_current_line, ent_compat, utf8);
						phalcon_concat_self(&html, escaped_line TSRMLS_CC);
					}
				}
	
				PHALCON_SEPARATE(i);
				phalcon_increment(i);
			}
			phalcon_concat_self_str(&html, SL("</pre>") TSRMLS_CC);
		}
	}
	
	phalcon_concat_self_str(&html, SL("</td></tr>") TSRMLS_CC);
	
	RETURN_CTOR(html);
}
Esempio n. 22
0
/**
 * Returns a slice of the resultset to show in the pagination
 *
 * @return stdClass
 */
PHP_METHOD(Phalcon_Paginator_Adapter_NativeArray, getPaginate){

	zval *one, *config, *items, *show, *page_number = NULL, *page;
	zval *number, *rounded_total, *total_pages, *before_page_number;
	zval *start, *slice, *compare = NULL, *next = NULL, *before = NULL;

	PHALCON_MM_GROW();

	/** 
	 * TODO: Rewrite the whole method!
	 */
	PHALCON_INIT_VAR(one);
	ZVAL_LONG(one, 1);
	
	PHALCON_OBS_VAR(config);
	phalcon_read_property_this(&config, this_ptr, SL("_config"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(items);
	phalcon_array_fetch_string(&items, config, SL("data"), PH_NOISY_CC);
	if (Z_TYPE_P(items) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_paginator_exception_ce, "Invalid data for paginator");
		return;
	}
	
	PHALCON_OBS_VAR(show);
	phalcon_read_property_this(&show, this_ptr, SL("_limitRows"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(page_number);
	phalcon_read_property_this(&page_number, this_ptr, SL("_page"), PH_NOISY_CC);
	if (!zend_is_true(page_number)) {
		PHALCON_CPY_WRT(page_number, one);
	}
	
	PHALCON_INIT_VAR(page);
	object_init(page);
	
	PHALCON_INIT_VAR(number);
	phalcon_fast_count(number, items TSRMLS_CC);
	
	PHALCON_INIT_VAR(rounded_total);
	div_function(rounded_total, number, show TSRMLS_CC);
	
	PHALCON_INIT_VAR(total_pages);
	PHALCON_CALL_FUNC_PARAMS_1(total_pages, "intval", rounded_total);
	
	/** 
	 * Increase total_pages if wasn't integer
	 */
	if (!PHALCON_IS_EQUAL(total_pages, rounded_total)) {
		PHALCON_SEPARATE_NMO(total_pages);
		increment_function(total_pages);
	}
	
	PHALCON_INIT_VAR(before_page_number);
	sub_function(before_page_number, page_number, one TSRMLS_CC);
	
	PHALCON_INIT_VAR(start);
	mul_function(start, show, before_page_number TSRMLS_CC);
	
	PHALCON_INIT_VAR(slice);
	PHALCON_CALL_FUNC_PARAMS_3(slice, "array_slice", items, start, show);
	phalcon_update_property_zval(page, SL("items"), slice TSRMLS_CC);
	
	PHALCON_INIT_VAR(compare);
	is_smaller_function(compare, page_number, total_pages TSRMLS_CC);
	if (PHALCON_IS_TRUE(compare)) {
		PHALCON_INIT_VAR(next);
		phalcon_add_function(next, page_number, one TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(next, total_pages);
	}
	
	phalcon_update_property_zval(page, SL("next"), next TSRMLS_CC);
	
	is_smaller_function(compare, one, page_number TSRMLS_CC);
	if (PHALCON_IS_TRUE(compare)) {
		PHALCON_INIT_VAR(before);
		sub_function(before, page_number, one TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(before, one);
	}
	
	phalcon_update_property_zval(page, SL("first"), one TSRMLS_CC);
	phalcon_update_property_zval(page, SL("before"), before TSRMLS_CC);
	phalcon_update_property_zval(page, SL("current"), page_number TSRMLS_CC);
	phalcon_update_property_zval(page, SL("last"), total_pages TSRMLS_CC);
	phalcon_update_property_zval(page, SL("total_pages"), total_pages TSRMLS_CC);
	phalcon_update_property_zval(page, SL("total_items"), number TSRMLS_CC);
	
	RETURN_CTOR(page);
}
Esempio n. 23
0
/**
 * Composite one image onto another

 *
 * @param Phalcon\Image\Adapter $mask  mask Image instance
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, _mask){

	zval *mask, image = {}, mask_image = {}, blob = {}, mask_image_width = {}, mask_image_height = {}, newimage = {}, image_width = {}, image_height = {}, saveflag = {}, color = {}, c = {}, alpha = {}, temp_image = {};
	int x, y, w, h, i;

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

	phalcon_return_property(&image, getThis(), SL("_image"));

	PHALCON_CALL_METHODW(&blob, mask, "render");
	PHALCON_CALL_FUNCTIONW(&mask_image, "imagecreatefromstring", &blob);

	ZVAL_TRUE(&saveflag);

	PHALCON_CALL_FUNCTIONW(NULL, "imagesavealpha", &mask_image, &saveflag);

	PHALCON_CALL_FUNCTIONW(&mask_image_width, "imagesx", &mask_image);
	PHALCON_CALL_FUNCTIONW(&mask_image_height, "imagesy", &mask_image);

	phalcon_return_property(&image_width, getThis(), SL("_width"));
	phalcon_return_property(&image_height, getThis(), SL("_height"));

	PHALCON_CALL_METHODW(&newimage, getThis(), "_create", &image_width, &image_height);

	PHALCON_CALL_FUNCTIONW(NULL, "imagesavealpha", &newimage, &saveflag);

	ZVAL_LONG(&c, 0);
	ZVAL_LONG(&alpha, 127);

	PHALCON_CALL_FUNCTIONW(&color, "imagecolorallocatealpha", &newimage, &c, &c, &c, &alpha);
	PHALCON_CALL_FUNCTIONW(NULL, "imagefill", &newimage, &c, &c, &color);

	if(!PHALCON_IS_EQUAL(&image_width, &mask_image_width) || !PHALCON_IS_EQUAL(&image_height, &mask_image_height)) {
		PHALCON_CALL_FUNCTIONW(&temp_image, "imagecreatetruecolor", &image_width, &image_height);
		PHALCON_CALL_FUNCTIONW(NULL, "imagecopyresampled", &temp_image, &mask_image, &c, &c, &c, &c, &image_width, &image_height, &mask_image_width, &mask_image_height);
		PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &mask_image);

		PHALCON_CPY_WRT_CTOR(&mask_image, &temp_image);
	}

	w = phalcon_get_intval(&image_width);
	h = phalcon_get_intval(&image_height);

	for (x=0; x < w; x++) {
		zval zx = {};
		ZVAL_LONG(&zx, x);
		for (y=0; y < h; y++) {
			zval zy = {}, index = {}, red = {}, index2 = {}, color = {}, r = {}, g = {}, b = {};
			ZVAL_LONG(&zy, y);

			PHALCON_CALL_FUNCTIONW(&index, "imagecolorat", &mask_image, &zx, &zy);
			PHALCON_CALL_FUNCTIONW(&alpha, "imagecolorsforindex", &mask_image, &index);

			if (phalcon_array_isset_fetch_str(&red, &alpha, SL("red"))) {
				i = (int)(127 - (phalcon_get_intval(&red) / 2));
				ZVAL_LONG(&alpha, i);
			}

			PHALCON_CALL_FUNCTIONW(&index2, "imagecolorat", &image, &zx, &zy);
			PHALCON_CALL_FUNCTIONW(&color, "imagecolorsforindex", &image, &index2);

			phalcon_array_isset_fetch_str(&r, &color, SL("red"));
			phalcon_array_isset_fetch_str(&g, &color, SL("green"));
			phalcon_array_isset_fetch_str(&b, &color, SL("blue"));

			PHALCON_CALL_FUNCTIONW(&color, "imagecolorallocatealpha", &newimage, &r, &g, &b, &alpha);
			PHALCON_CALL_FUNCTIONW(NULL, "imagesetpixel", &newimage, &zx, &zy, &color);
		}
	}

	PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &image);
	PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &mask_image);

	phalcon_update_property_zval(getThis(), SL("_image"), &newimage);
}
Esempio n. 24
0
/**
 * Traverses a collection calling the callback to generate its HTML
 *
 * @param Phalcon\Assets\Collection $collection
 * @param callback $callback
 * @param string $type
 * @param array $args
 */
PHP_METHOD(Phalcon_Assets_Manager, output){

	zval *collection, *callback, *type = NULL, *args = NULL, *output, *use_implicit_output;
	zval *resources = NULL, *filters = NULL, *prefix = NULL, *source_base_path = NULL;
	zval *target_base_path = NULL, *options, *collection_source_path = NULL;
	zval *complete_source_path = NULL, *collection_target_path = NULL;
	zval *complete_target_path = NULL, *filtered_joined_content = NULL;
	zval *join = NULL, *exception_message = NULL, *is_directory;
	zval *resource = NULL, *filter_needed = NULL, *local = NULL, *source_path = NULL;
	zval *target_path = NULL, *path = NULL, *prefixed_path = NULL, *attributes = NULL;
	zval *parameters = NULL, *html = NULL, *content = NULL, *must_filter = NULL;
	zval *filter = NULL, *filtered_content = NULL, *target_uri = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;
	zval *type_css;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 2, &collection, &callback, &type, &args);

	if (!args) {
		args = PHALCON_GLOBAL(z_null);
	}

	PHALCON_INIT_VAR(output);

	PHALCON_OBS_VAR(use_implicit_output);
	phalcon_read_property_this(&use_implicit_output, this_ptr, SL("_implicitOutput"), PH_NOISY TSRMLS_CC);

	/** 
	 * Get the resources as an array
	 */
	PHALCON_CALL_METHOD(&resources, collection, "getresources");

	/** 
	 * Get filters in the collection
	 */
	PHALCON_CALL_METHOD(&filters, collection, "getfilters");

	/** 
	 * Get the collection's prefix
	 */
	PHALCON_CALL_METHOD(&prefix, collection, "getprefix");

	PHALCON_INIT_VAR(type_css);
	ZVAL_STRING(type_css, "css", 1);

	/** 
	 * Prepare options if the collection must be filtered
	 */
	if (Z_TYPE_P(filters) == IS_ARRAY) { 

		PHALCON_INIT_VAR(source_base_path);

		PHALCON_INIT_VAR(target_base_path);

		PHALCON_OBS_VAR(options);
		phalcon_read_property_this(&options, this_ptr, SL("_options"), PH_NOISY TSRMLS_CC);

		/** 
		 * Check for global options in the assets manager
		 */
		if (Z_TYPE_P(options) == IS_ARRAY) { 

			/** 
			 * The source base path is a global location where all resources are located
			 */
			if (phalcon_array_isset_string(options, SS("sourceBasePath"))) {
				PHALCON_OBS_NVAR(source_base_path);
				phalcon_array_fetch_string(&source_base_path, options, SL("sourceBasePath"), PH_NOISY);
			}

			/** 
			 * The target base path is a global location where all resources are written
			 */
			if (phalcon_array_isset_string(options, SS("targetBasePath"))) {
				PHALCON_OBS_NVAR(target_base_path);
				phalcon_array_fetch_string(&target_base_path, options, SL("targetBasePath"), PH_NOISY);
			}
		}

		/** 
		 * Check if the collection have its own source base path
		 */
		PHALCON_CALL_METHOD(&collection_source_path, collection, "getsourcepath");

		/** 
		 * Concatenate the global base source path with the collection one
		 */
		if (PHALCON_IS_NOT_EMPTY(collection_source_path)) {
			PHALCON_INIT_VAR(complete_source_path);
			PHALCON_CONCAT_VV(complete_source_path, source_base_path, collection_source_path);
		} else {
			PHALCON_CPY_WRT(complete_source_path, source_base_path);
		}

		/** 
		 * Check if the collection have its own target base path
		 */
		PHALCON_CALL_METHOD(&collection_target_path, collection, "gettargetpath");

		/** 
		 * Concatenate the global base source path with the collection one
		 */
		if (PHALCON_IS_NOT_EMPTY(collection_target_path)) {
			PHALCON_INIT_VAR(complete_target_path);
			PHALCON_CONCAT_VV(complete_target_path, target_base_path, collection_target_path);
		} else {
			PHALCON_CPY_WRT(complete_target_path, target_base_path);
		}

		/** 
		 * Global filtered content
		 */
		PHALCON_INIT_VAR(filtered_joined_content);

		/** 
		 * Check if all the resources in the collection must be joined
		 */
		PHALCON_CALL_METHOD(&join, collection, "getjoin");

		/** 
		 * Check for valid target paths if the collection must be joined
		 */
		if (zend_is_true(join)) {

			/** 
			 * We need a valid final target path
			 */
			if (PHALCON_IS_EMPTY(complete_target_path)) {
				PHALCON_INIT_VAR(exception_message);
				PHALCON_CONCAT_SVS(exception_message, "Path '", complete_target_path, "' is not a valid target path (1)");
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_assets_exception_ce, exception_message);
				return;
			}

			PHALCON_INIT_VAR(is_directory);
			phalcon_is_dir(is_directory, complete_target_path TSRMLS_CC);

			/** 
			 * The targetpath needs to be a valid file
			 */
			if (PHALCON_IS_TRUE(is_directory)) {
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVS(exception_message, "Path '", complete_target_path, "' is not a valid target path (2)");
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_assets_exception_ce, exception_message);
				return;
			}
		}
	}

	phalcon_is_iterable(resources, &ah0, &hp0, 0, 0);

	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

		PHALCON_GET_HVALUE(resource);

		PHALCON_INIT_NVAR(filter_needed);
		ZVAL_FALSE(filter_needed);

		if (!type) {
			PHALCON_CALL_METHOD(&type, resource, "gettype");
		}

		/** 
		 * Is the resource local?
		 */
		PHALCON_CALL_METHOD(&local, resource, "getlocal");

		/** 
		 * If the collection must not be joined we must print a HTML for each one
		 */
		if (Z_TYPE_P(filters) == IS_ARRAY) { 
			if (zend_is_true(local)) {

				/** 
				 * Get the complete path
				 */
				PHALCON_CALL_METHOD(&source_path, resource, "getrealsourcepath", complete_source_path);

				/** 
				 * We need a valid source path
				 */
				if (!zend_is_true(source_path)) {
					PHALCON_CALL_METHOD(&source_path, resource, "getpath");

					PHALCON_INIT_NVAR(exception_message);
					PHALCON_CONCAT_SVS(exception_message, "Resource '", source_path, "' does not have a valid source path");
					PHALCON_THROW_EXCEPTION_ZVAL(phalcon_assets_exception_ce, exception_message);
					return;
				}
			} else {
				/** 
				 * Get the complete source path
				 */
				PHALCON_CALL_METHOD(&source_path, resource, "getpath");

				/** 
				 * resources paths are always filtered
				 */
				PHALCON_INIT_NVAR(filter_needed);
				ZVAL_TRUE(filter_needed);
			}

			/** 
			 * Get the target path, we need to write the filtered content to a file
			 */
			PHALCON_CALL_METHOD(&target_path, resource, "getrealtargetpath", complete_target_path);

			/** 
			 * We need a valid final target path
			 */
			if (PHALCON_IS_EMPTY(target_path)) {
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVS(exception_message, "Resource '", source_path, "' does not have a valid target path");
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_assets_exception_ce, exception_message);
				return;
			}

			if (zend_is_true(local)) {

				/** 
				 * Make sure the target path is not the same source path
				 */
				if (PHALCON_IS_EQUAL(target_path, source_path)) {
					PHALCON_INIT_NVAR(exception_message);
					PHALCON_CONCAT_SVS(exception_message, "Resource '", target_path, "' have the same source and target paths");
					PHALCON_THROW_EXCEPTION_ZVAL(phalcon_assets_exception_ce, exception_message);
					return;
				}
				if (phalcon_file_exists(target_path TSRMLS_CC) == SUCCESS) {
					if (phalcon_compare_mtime(target_path, source_path TSRMLS_CC)) {
						PHALCON_INIT_NVAR(filter_needed);
						ZVAL_TRUE(filter_needed);
					}
				} else {
					PHALCON_INIT_NVAR(filter_needed);
					ZVAL_TRUE(filter_needed);
				}
			}
		}

		/** 
		 * If there are no filters, just print/buffer the HTML
		 */
		if (Z_TYPE_P(filters) != IS_ARRAY) { 
			PHALCON_CALL_METHOD(&path, resource, "getrealtargeturi");
			if (Z_TYPE_P(prefix) != IS_NULL) {
				PHALCON_INIT_NVAR(prefixed_path);
				PHALCON_CONCAT_VV(prefixed_path, prefix, path);
			} else {
				PHALCON_CPY_WRT(prefixed_path, path);
			}

			/** 
			 * Gets extra HTML attributes in the resource
			 */
			PHALCON_CALL_METHOD(&attributes, resource, "getattributes");

			/** 
			 * Prepare the parameters for the callback
			 */
			PHALCON_INIT_NVAR(parameters);
			array_init_size(parameters, 3);
			if (Z_TYPE_P(attributes) == IS_ARRAY) { 
				phalcon_array_update_long(&attributes, 0, prefixed_path, PH_COPY);

				phalcon_array_append(&parameters, attributes, PH_COPY);
			} else {
				phalcon_array_append(&parameters, prefixed_path, PH_COPY);
			}

			phalcon_array_append(&parameters, local, PH_COPY);
			phalcon_array_append(&parameters, args, PH_COPY);

			/** 
			 * Call the callback to generate the HTML
			 */
			PHALCON_INIT_NVAR(html);/**/
			PHALCON_CALL_USER_FUNC_ARRAY(html, callback, parameters);

			/** 
			 * Implicit output prints the content directly
			 */
			if (zend_is_true(use_implicit_output)) {
				zend_print_zval(html, 0);
			} else {
				phalcon_concat_self(&output, html TSRMLS_CC);
			}

			zend_hash_move_forward_ex(ah0, &hp0);
			continue;
		}

		if (zend_is_true(filter_needed)) {

			/** 
			 * Get the resource's content
			 */
			PHALCON_CALL_METHOD(&content, resource, "getcontent", complete_source_path);

			/** 
			 * Check if the resource must be filtered
			 */
			PHALCON_CALL_METHOD(&must_filter, resource, "getfilter");

			/** 
			 * Only filter the resource if it's marked as 'filterable'
			 */
			if (zend_is_true(must_filter)) {

				phalcon_is_iterable(filters, &ah1, &hp1, 0, 0);

				while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {

					PHALCON_GET_HVALUE(filter);

					/** 
					 * Filters must be valid objects
					 */
					if (Z_TYPE_P(filter) != IS_OBJECT) {
						PHALCON_THROW_EXCEPTION_STR(phalcon_assets_exception_ce, "Filter is invalid");
						return;
					}

					/** 
					 * Calls the method 'filter' which must return a filtered version of the content
					 */
					PHALCON_CALL_METHOD(&filtered_content, filter, "filter", content);
					PHALCON_CPY_WRT_CTOR(content, filtered_content);

					zend_hash_move_forward_ex(ah1, &hp1);
				}

				/**
				 * Update the joined filtered content
				 */
				if (zend_is_true(join)) {
					if (PHALCON_IS_EQUAL(type, type_css)) {
						if (Z_TYPE_P(filtered_joined_content) == IS_NULL) {
							PHALCON_INIT_NVAR(filtered_joined_content);
							PHALCON_CONCAT_VS(filtered_joined_content, content, "");
						} else {
							PHALCON_SCONCAT_VS(filtered_joined_content, content, "");
						}
					} else {
						if (Z_TYPE_P(filtered_joined_content) == IS_NULL) {
							PHALCON_INIT_NVAR(filtered_joined_content);
							PHALCON_CONCAT_VS(filtered_joined_content, content, ";");
						} else {
							PHALCON_SCONCAT_VS(filtered_joined_content, content, ";");
						}
					}
				}

			} else {
				/** 
				 * Update the joined filtered content
				 */
				if (zend_is_true(join)) {
					if (Z_TYPE_P(filtered_joined_content) == IS_NULL) {
						PHALCON_CPY_WRT(filtered_joined_content, content);
					} else {
						phalcon_concat_self(&filtered_joined_content, content TSRMLS_CC);
					}
				} else {
					PHALCON_CPY_WRT(filtered_content, content);
				}
			}

			if (!zend_is_true(join)) {
				/** 
				 * Write the file using file-put-contents. This respects the openbase-dir also
				 * writes to streams
				 */
				phalcon_file_put_contents(NULL, target_path, filtered_content TSRMLS_CC);
			}
		}

		if (!zend_is_true(join)) {

			/** 
			 * Generate the HTML using the original path in the resource
			 */
			PHALCON_CALL_METHOD(&path, resource, "getrealtargeturi");
			if (Z_TYPE_P(prefix) != IS_NULL) {
				PHALCON_INIT_NVAR(prefixed_path);
				PHALCON_CONCAT_VV(prefixed_path, prefix, path);
			} else {
				PHALCON_CPY_WRT(prefixed_path, path);
			}

			/** 
			 * Gets extra HTML attributes in the resource
			 */
			PHALCON_CALL_METHOD(&attributes, resource, "getattributes");

			/** 
			 * Filtered resources are always local
			 */
			PHALCON_INIT_NVAR(local);
			ZVAL_TRUE(local);

			/** 
			 * Prepare the parameters for the callback
			 */
			PHALCON_INIT_NVAR(parameters);
			array_init_size(parameters, 3);
			if (Z_TYPE_P(attributes) == IS_ARRAY) { 
				phalcon_array_update_long(&attributes, 0, prefixed_path, PH_COPY);

				phalcon_array_append(&parameters, attributes, PH_COPY);
			} else {
				phalcon_array_append(&parameters, prefixed_path, PH_COPY);
			}

			phalcon_array_append(&parameters, local, PH_COPY);
			phalcon_array_append(&parameters, args, PH_COPY);

			/** 
			 * Call the callback to generate the HTML
			 */
			PHALCON_INIT_NVAR(html);/**/
			PHALCON_CALL_USER_FUNC_ARRAY(html, callback, parameters);

			/** 
			 * Implicit output prints the content directly
			 */
			if (zend_is_true(use_implicit_output)) {
				zend_print_zval(html, 0);
			} else {
				phalcon_concat_self(&output, html TSRMLS_CC);
			}
		}

		zend_hash_move_forward_ex(ah0, &hp0);
	}

	if (Z_TYPE_P(filters) == IS_ARRAY) { 
		if (zend_is_true(join)) {

			/** 
			 * Write the file using file_put_contents. This respects the openbase-dir also
			 * writes to streams
			 */
			phalcon_file_put_contents(NULL, complete_target_path, filtered_joined_content TSRMLS_CC);

			/** 
			 * Generate the HTML using the original path in the resource
			 */
			PHALCON_CALL_METHOD(&target_uri, collection, "gettargeturi");
			if (Z_TYPE_P(prefix) != IS_NULL) {
				PHALCON_INIT_NVAR(prefixed_path);
				PHALCON_CONCAT_VV(prefixed_path, prefix, target_uri);
			} else {
				PHALCON_CPY_WRT(prefixed_path, target_uri);
			}

			/** 
			 * Gets extra HTML attributes in the resource
			 */
			PHALCON_CALL_METHOD(&attributes, collection, "getattributes");
			PHALCON_CALL_METHOD(&local, collection, "gettargetlocal");

			/** 
			 * Prepare the parameters for the callback
			 */
			PHALCON_INIT_NVAR(parameters);
			array_init_size(parameters, 3);
			if (Z_TYPE_P(attributes) == IS_ARRAY) { 
				phalcon_array_update_long(&attributes, 0, prefixed_path, PH_COPY);

				phalcon_array_append(&parameters, attributes, PH_COPY);
			} else {
				phalcon_array_append(&parameters, prefixed_path, PH_COPY);
			}

			phalcon_array_append(&parameters, local, PH_COPY);
			phalcon_array_append(&parameters, args, PH_COPY);

			/** 
			 * Call the callback to generate the HTML
			 */
			PHALCON_INIT_NVAR(html);/**/
			PHALCON_CALL_USER_FUNC_ARRAY(html, callback, parameters);

			/** 
			 * Implicit output prints the content directly
			 */
			if (zend_is_true(use_implicit_output)) {
				zend_print_zval(html, 0);
			} else {
				phalcon_concat_self(&output, html TSRMLS_CC);
			}
		}
	}

	RETURN_CCTOR(output);
}
Esempio n. 25
0
/**
 * Shows a backtrace item
 *
 * @param int $n
 * @param array $trace
 */
PHP_METHOD(Phalcon_Debug, showTraceItem){

	zval *n, *trace, *link_format, *space, *two_spaces, *underscore;
	zval *minus, *html, *class_name;
	zval *namespace_separator, *prepare_uri_class;
	zval *lower_class_name, *prepared_function_name;
	zval *prepare_internal_class, *type, *function_name = NULL;
	zval *trace_args, *arguments, *argument = NULL, *dumped_argument = NULL;
	zval *span_argument = NULL, *joined_arguments, *z_one;
	zval *file, *line, *show_files, *lines = NULL, *number_lines;
	zval *show_file_fragment, *before_context, *before_line;
	zval *first_line = NULL, *after_context, *after_line, *last_line = NULL;
	zval *comment_pattern, *charset, *tab;
	zval *comment, *i = NULL, *line_position = NULL, *current_line = NULL;
	zval *trimmed = NULL, *is_comment = NULL, *spaced_current_line = NULL;
	zval *escaped_line = NULL, *formatted_file = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 3, 0, &n, &trace, &link_format);

	PHALCON_INIT_VAR(space);
	ZVAL_STRING(space, " ", 1);

	PHALCON_INIT_VAR(two_spaces);
	ZVAL_STRING(two_spaces, "  ", 1);

	PHALCON_INIT_VAR(underscore);
	ZVAL_STRING(underscore, "_", 1);

	PHALCON_INIT_VAR(minus);
	ZVAL_STRING(minus, "-", 1);

	/** 
	 * Every trace in the backtrace have a unique number
	 */
	PHALCON_INIT_VAR(html);
	PHALCON_CONCAT_SVS(html, "<tr><td align=\"right\" valign=\"top\" class=\"error-number\">#", n, "</td><td>");
	if (phalcon_array_isset_string(trace, SS("class"))) {
		zend_class_entry *class_ce;

		PHALCON_OBS_VAR(class_name);
		phalcon_array_fetch_string(&class_name, trace, SL("class"), PH_NOISY);

		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) {
			/* Unable to load the class, should never happen */
		}
		else if (is_phalcon_class(class_ce)) {
			PHALCON_INIT_VAR(namespace_separator);
			ZVAL_STRING(namespace_separator, "\\", 1);

			/* Prepare the class name according to the Phalcon's conventions */
			PHALCON_INIT_VAR(prepare_uri_class);
			phalcon_fast_str_replace(prepare_uri_class, namespace_separator, underscore, class_name);

			/* Generate a link to the official docs */
			PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://docs.phalconphp.com/en/latest/api/", prepare_uri_class, ".html\">", class_name, "</a></span>");
		} else if (class_ce->type == ZEND_INTERNAL_CLASS) {
			PHALCON_INIT_VAR(lower_class_name);
			phalcon_fast_strtolower(lower_class_name, class_name);

			PHALCON_INIT_VAR(prepare_internal_class);
			phalcon_fast_str_replace(prepare_internal_class, underscore, minus, lower_class_name);

			/* Generate a link to the official docs */
			PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://php.net/manual/en/class.", prepare_internal_class, ".php\">", class_name, "</a></span>");
		} else {
			PHALCON_SCONCAT_SVS(html, "<span class=\"error-class\">", class_name, "</span>");
		}

		/** 
		 * Object access operator: static/instance
		 */
		PHALCON_OBS_VAR(type);
		phalcon_array_fetch_string(&type, trace, SL("type"), PH_NOISY);
		phalcon_concat_self(&html, type TSRMLS_CC);
	}

	/** 
	 * Normally the backtrace contains only classes
	 */
	if (phalcon_array_isset_string(trace, SS("class"))) {
		PHALCON_OBS_VAR(function_name);
		phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY);
		PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
	} else {
		zend_function *func;

		PHALCON_OBS_NVAR(function_name);
		phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY);
		convert_to_string(function_name);

		/** 
		 * Check if the function exists
		 */
		if (phalcon_fetch_function(&func, Z_STRVAL_P(function_name), Z_STRLEN_P(function_name) TSRMLS_CC) == SUCCESS) {

			/** 
			 * Internal functions links to the PHP documentation
			 */
			if (func->type == ZEND_INTERNAL_FUNCTION) {
				/** 
				 * Prepare function's name according to the conventions in the docs
				 */
				PHALCON_INIT_VAR(prepared_function_name);
				phalcon_fast_str_replace(prepared_function_name, underscore, minus, function_name);
				PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-function\"><a target=\"_new\" href=\"http://php.net/manual/en/function.", prepared_function_name, ".php\">", function_name, "</a></span>");
			} else {
				PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
			}
		} else {
			PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
		}
	}

	/** 
	 * Check for arguments in the function
	 */
	if (phalcon_array_isset_string(trace, SS("args"))) {

		PHALCON_OBS_VAR(trace_args);
		phalcon_array_fetch_string(&trace_args, trace, SL("args"), PH_NOISY);
		if (phalcon_fast_count_ev(trace_args TSRMLS_CC)) {

			PHALCON_INIT_VAR(arguments);
			array_init(arguments);

			phalcon_is_iterable(trace_args, &ah0, &hp0, 0, 0);

			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

				PHALCON_GET_HVALUE(argument);

				/** 
				 * Every argument is generated using _getVarDump
				 */
				PHALCON_CALL_METHOD(&dumped_argument, this_ptr, "_getvardump", argument);

				PHALCON_INIT_NVAR(span_argument);
				PHALCON_CONCAT_SVS(span_argument, "<span class=\"error-parameter\">", dumped_argument, "</span>");

				/** 
				 * Append the HTML generated to the argument's list
				 */
				phalcon_array_append(&arguments, span_argument, PH_COPY);

				zend_hash_move_forward_ex(ah0, &hp0);
			}

			/** 
			 * Join all the arguments
			 */
			PHALCON_INIT_VAR(joined_arguments);
			phalcon_fast_join_str(joined_arguments, SL(", "), arguments TSRMLS_CC);
			PHALCON_SCONCAT_SVS(html, "(", joined_arguments, ")");
		} else {
			phalcon_concat_self_str(&html, SL("()") TSRMLS_CC);
		}
	}

	/** 
	 * When 'file' is present, it usually means the function is provided by the user
	 */
	if (phalcon_array_isset_string(trace, SS("file"))) {

		z_one = PHALCON_GLOBAL(z_one);

		PHALCON_OBS_VAR(file);
		phalcon_array_fetch_string(&file, trace, SL("file"), PH_NOISY);

		PHALCON_OBS_VAR(line);
		phalcon_array_fetch_string(&line, trace, SL("line"), PH_NOISY);

		PHALCON_CALL_METHOD(&formatted_file, getThis(), "getfilelink", file, line, link_format);

		/** 
		 * Realpath to the file and its line using a special header
		 */
		PHALCON_SCONCAT_SVSVS(html, "<br/><div class=\"error-file\">", formatted_file, " (", line, ")</div>");

		PHALCON_OBS_VAR(show_files);
		phalcon_read_property_this(&show_files, this_ptr, SL("_showFiles"), PH_NOISY TSRMLS_CC);

		/** 
		 * The developer can change if the files must be opened or not
		 */
		if (zend_is_true(show_files)) {

			/** 
			 * Open the file to an array using 'file', this respects the openbase-dir directive
			 */
			PHALCON_CALL_FUNCTION(&lines, "file", file);

			PHALCON_INIT_VAR(number_lines);
			phalcon_fast_count(number_lines, lines TSRMLS_CC);

			PHALCON_OBS_VAR(show_file_fragment);
			phalcon_read_property_this(&show_file_fragment, this_ptr, SL("_showFileFragment"), PH_NOISY TSRMLS_CC);

			/** 
			 * File fragments just show a piece of the file where the exception is located
			 */
			if (zend_is_true(show_file_fragment)) {

				/** 
				 * Take lines back to the current exception's line
				 */
				before_context = phalcon_fetch_nproperty_this(getThis(), SL("_beforeContext"), PH_NOISY TSRMLS_CC);

				PHALCON_INIT_VAR(before_line);
				phalcon_sub_function(before_line, line, before_context);

				/** 
				 * Check for overflows
				 */
				if (PHALCON_LT_LONG(before_line, 1)) {
					PHALCON_CPY_WRT_CTOR(first_line, z_one);
				} else {
					PHALCON_CPY_WRT(first_line, before_line);
				}

				/** 
				 * Take lines after the current exception's line
				 */
				after_context = phalcon_fetch_nproperty_this(getThis(), SL("_afterContext"), PH_NOISY TSRMLS_CC);

				PHALCON_INIT_VAR(after_line);
				phalcon_add_function(after_line, line, after_context);

				/** 
				 * Check for overflows
				 */
				if (PHALCON_GT(after_line, number_lines)) {
					PHALCON_CPY_WRT(last_line, number_lines);
				} else {
					PHALCON_CPY_WRT(last_line, after_line);
				}

				PHALCON_SCONCAT_SVSVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums:", first_line, "'>");
			} else {
				PHALCON_CPY_WRT_CTOR(first_line, z_one);
				PHALCON_CPY_WRT(last_line, number_lines);
				PHALCON_SCONCAT_SVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums error-scroll'>");
			}

			PHALCON_INIT_VAR(comment_pattern);
			ZVAL_STRING(comment_pattern, "#\\*\\/$#", 1);

			charset = phalcon_fetch_static_property_ce(phalcon_debug_ce, SL("_charset") TSRMLS_CC);

			PHALCON_INIT_VAR(tab);
			ZVAL_STRING(tab, "\t", 1);

			PHALCON_INIT_VAR(comment);
			ZVAL_STRING(comment, "* /", 1);
			PHALCON_CPY_WRT(i, first_line);

			while (PHALCON_LE(i, last_line)) {

				/** 
				 * Current line in the file
				 */
				PHALCON_INIT_NVAR(line_position);
				phalcon_sub_function(line_position, i, z_one);

				/** 
				 * Current line content in the piece of file
				 */
				PHALCON_OBS_NVAR(current_line);
				phalcon_array_fetch(&current_line, lines, line_position, PH_NOISY);

				/** 
				 * File fragments are cleaned, removing tabs and comments
				 */
				if (zend_is_true(show_file_fragment)) {
					if (PHALCON_IS_EQUAL(i, first_line)) {

						PHALCON_INIT_NVAR(trimmed);
						phalcon_fast_trim(trimmed, current_line, NULL, PHALCON_TRIM_RIGHT TSRMLS_CC);

						PHALCON_INIT_NVAR(is_comment);

						RETURN_MM_ON_FAILURE(phalcon_preg_match(is_comment, comment_pattern, current_line, NULL TSRMLS_CC));

						if (zend_is_true(is_comment)) {
							PHALCON_INIT_NVAR(spaced_current_line);
							phalcon_fast_str_replace(spaced_current_line, comment, space, current_line);
							PHALCON_CPY_WRT(current_line, spaced_current_line);
						}
					}
				}

				/** 
				 * Print a non break space if the current line is a line break, this allows to show
				 * the html zebra properly
				 */
				if (PHALCON_IS_STRING(current_line, "\n")) {
					phalcon_concat_self_str(&html, SL("&nbsp;\n") TSRMLS_CC);
				} else {
					if (PHALCON_IS_STRING(current_line, "\r\n")) {
						phalcon_concat_self_str(&html, SL("&nbsp;\n") TSRMLS_CC);
					} else {
						PHALCON_INIT_NVAR(spaced_current_line);
						phalcon_fast_str_replace(spaced_current_line, tab, two_spaces, current_line);

						PHALCON_INIT_NVAR(escaped_line);
						phalcon_htmlentities(escaped_line, spaced_current_line, NULL, charset TSRMLS_CC);
						phalcon_concat_self(&html, escaped_line TSRMLS_CC);
					}
				}

				phalcon_increment(i);
			}
			phalcon_concat_self_str(&html, SL("</pre>") TSRMLS_CC);
		}
	}

	phalcon_concat_self_str(&html, SL("</td></tr>") TSRMLS_CC);

	RETURN_CTOR(html);
}