예제 #1
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_list, *access_key = NULL;
	zval *role_inherits, *inherited_roles = NULL, *inherited_role = NULL;
	HashTable *ah0, *ah1, *ah2;
	HashPosition hp0, hp1, hp2;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 3, 0, &role, &resource, &access);
	
	phalcon_update_property_this(this_ptr, SL("_activeRole"), role TSRMLS_CC);
	phalcon_update_property_this(this_ptr, SL("_activeResource"), resource TSRMLS_CC);
	phalcon_update_property_this(this_ptr, SL("_activeAccess"), access TSRMLS_CC);
	
	PHALCON_OBS_VAR(events_manager);
	phalcon_read_property_this(&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_p2(status, events_manager, "fire", event_name, this_ptr);
		if (PHALCON_IS_FALSE(status)) {
			RETURN_CCTOR(status);
		}
	}
	
	PHALCON_OBS_VAR(default_access);
	phalcon_read_property_this(&default_access, this_ptr, SL("_defaultAccess"), PH_NOISY_CC);
	
	/** 
	 * Check if the role exists
	 */
	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)) {
		RETURN_CCTOR(default_access);
	}
	
	PHALCON_INIT_VAR(have_access);
	
	PHALCON_OBS_VAR(access_list);
	phalcon_read_property_this(&access_list, this_ptr, SL("_access"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(access_key);
	PHALCON_CONCAT_VSVSV(access_key, role, "!", resource, "!", access);
	
	/** 
	 * Check if there is a direct combination for role-resource-access
	 */
	if (phalcon_array_isset(access_list, access_key)) {
		PHALCON_OBS_NVAR(have_access);
		phalcon_array_fetch(&have_access, access_list, access_key, PH_NOISY);
	}
	
	/** 
	 * Check in the inherits roles
	 */
	if (Z_TYPE_P(have_access) == IS_NULL) {
	
		PHALCON_OBS_VAR(role_inherits);
		phalcon_read_property_this(&role_inherits, this_ptr, SL("_roleInherits"), PH_NOISY_CC);
		if (phalcon_array_isset(role_inherits, role)) {
			PHALCON_OBS_VAR(inherited_roles);
			phalcon_array_fetch(&inherited_roles, role_inherits, role, PH_NOISY);
		} else {
			PHALCON_INIT_NVAR(inherited_roles);
		}
	
		if (Z_TYPE_P(inherited_roles) == IS_ARRAY) { 
	
			phalcon_is_iterable(inherited_roles, &ah0, &hp0, 0, 0);
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_HVALUE(inherited_role);
	
				PHALCON_INIT_NVAR(access_key);
				PHALCON_CONCAT_VSVSV(access_key, inherited_role, "!", resource, "!", access);
	
				/** 
				 * Check if there is a direct combination in one of the inherited roles
				 */
				if (phalcon_array_isset(access_list, access_key)) {
					PHALCON_OBS_NVAR(have_access);
					phalcon_array_fetch(&have_access, access_list, access_key, PH_NOISY);
					break;
				}
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
		}
	}
	
	/** 
	 * If access wasn't found yet, try role-resource-*
	 */
	if (Z_TYPE_P(have_access) == IS_NULL) {
	
		PHALCON_INIT_NVAR(access_key);
		PHALCON_CONCAT_VSVS(access_key, role, "!", resource, "!*");
	
		/** 
		 * In the direct role
		 */
		if (phalcon_array_isset(access_list, access_key)) {
			PHALCON_OBS_NVAR(have_access);
			phalcon_array_fetch(&have_access, access_list, access_key, PH_NOISY);
		} else {
			if (Z_TYPE_P(inherited_roles) == IS_ARRAY) { 
	
				phalcon_is_iterable(inherited_roles, &ah1, &hp1, 0, 0);
	
				while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
					PHALCON_GET_HVALUE(inherited_role);
	
					PHALCON_INIT_NVAR(access_key);
					PHALCON_CONCAT_VSVS(access_key, inherited_role, "!", resource, "!*");
	
					/** 
					 * In the inherited roles
					 */
					if (phalcon_array_isset(access_list, access_key)) {
						PHALCON_OBS_NVAR(have_access);
						phalcon_array_fetch(&have_access, access_list, access_key, PH_NOISY);
						break;
					}
	
					zend_hash_move_forward_ex(ah1, &hp1);
				}
	
			}
		}
	}
	
	/** 
	 * If access wasn't found yet, try role-*-*
	 */
	if (Z_TYPE_P(have_access) == IS_NULL) {
	
		PHALCON_INIT_NVAR(access_key);
		PHALCON_CONCAT_VS(access_key, role, "!*!*");
	
		/** 
		 * Try in the direct role
		 */
		if (phalcon_array_isset(access_list, access_key)) {
			PHALCON_OBS_NVAR(have_access);
			phalcon_array_fetch(&have_access, access_list, access_key, PH_NOISY);
		} else {
			if (Z_TYPE_P(inherited_roles) == IS_ARRAY) { 
	
				phalcon_is_iterable(inherited_roles, &ah2, &hp2, 0, 0);
	
				while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) {
	
					PHALCON_GET_HVALUE(inherited_role);
	
					PHALCON_INIT_NVAR(access_key);
					PHALCON_CONCAT_VS(access_key, inherited_role, "!*!*");
					if (phalcon_array_isset(access_list, access_key)) {
						PHALCON_OBS_NVAR(have_access);
						phalcon_array_fetch(&have_access, access_list, access_key, PH_NOISY);
						break;
					}
	
					zend_hash_move_forward_ex(ah2, &hp2);
				}
	
			}
		}
	}
	
	phalcon_update_property_this(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_p3_noret(events_manager, "fire", event_name, this_ptr, have_access);
	}
	
	if (Z_TYPE_P(have_access) == IS_NULL) {
		PHALCON_MM_RESTORE();
		RETURN_LONG(0);
	}
	
	RETURN_CCTOR(have_access);
}
예제 #2
0
/**
 * Executes the validator
 *
 * @return boolean
 */
PHP_METHOD(Phalcon_Model_Validator_Uniqueness, validate){

	zval *record = NULL, *field = NULL, *conditions = NULL, *compose_field = NULL;
	zval *value = NULL, *manager = NULL, *meta_data = NULL, *primary_fields = NULL;
	zval *primary_field = NULL, *options = NULL;
	zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL, *r5 = NULL, *r6 = NULL;
	zval *r7 = NULL, *r8 = NULL, *r9 = NULL, *r10 = NULL, *r11 = NULL, *r12 = NULL, *r13 = NULL;
	zval *r14 = NULL, *r15 = NULL, *r16 = NULL, *r17 = NULL, *r18 = NULL, *r19 = NULL, *r20 = NULL;
	zval *i0 = NULL;
	zval *a0 = NULL;
	zval *c0 = NULL, *c1 = NULL, *c2 = NULL;
	zval *t0 = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;
	int eval_int;

	PHALCON_MM_GROW();
	PHALCON_ALLOC_ZVAL_MM(r0);
	PHALCON_CALL_METHOD(r0, this_ptr, "isrequired", PHALCON_NO_CHECK);
	if (zend_is_true(r0)) {
		PHALCON_ALLOC_ZVAL_MM(r1);
		PHALCON_CALL_METHOD(r1, this_ptr, "getrecord", PHALCON_NO_CHECK);
		PHALCON_ALLOC_ZVAL_MM(i0);
		if (phalcon_clone(i0, r1 TSRMLS_CC) == FAILURE){
			return;
		}
		PHALCON_CPY_WRT(record, i0);
		
		PHALCON_ALLOC_ZVAL_MM(r2);
		PHALCON_CALL_METHOD(r2, this_ptr, "getfieldname", PHALCON_NO_CHECK);
		PHALCON_CPY_WRT(field, r2);
		
		PHALCON_INIT_VAR(a0);
		array_init(a0);
		PHALCON_CPY_WRT(conditions, a0);
		if (Z_TYPE_P(field) == IS_ARRAY) { 
			if (phalcon_valid_foreach(field TSRMLS_CC)) {
				ah0 = Z_ARRVAL_P(field);
				zend_hash_internal_pointer_reset_ex(ah0, &hp0);
				fes_d73d_0:
				if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
					goto fee_d73d_0;
				}
				PHALCON_INIT_VAR(compose_field);
				ZVAL_ZVAL(compose_field, *hd, 1, 0);
				PHALCON_INIT_VAR(r3);
				PHALCON_INIT_VAR(r4);
				PHALCON_CALL_METHOD_PARAMS_1(r4, record, "readattribute", compose_field, PHALCON_NO_CHECK);
				PHALCON_CALL_FUNC_PARAMS_1(r3, "addslashes", r4, 0x00F);
				PHALCON_CPY_WRT(value, r3);
				
				PHALCON_INIT_VAR(r5);
				PHALCON_CONCAT_VSVS(r5, compose_field, "='", value, "'");
				phalcon_array_append(&conditions, r5, PHALCON_SEPARATE_PLZ TSRMLS_CC);
				zend_hash_move_forward_ex(ah0, &hp0);
				goto fes_d73d_0;
				fee_d73d_0:
				if(0){}
			} else {
				return;
			}
		} else {
			PHALCON_ALLOC_ZVAL_MM(r6);
			PHALCON_ALLOC_ZVAL_MM(r7);
			PHALCON_CALL_METHOD(r7, this_ptr, "getvalue", PHALCON_NO_CHECK);
			PHALCON_CALL_FUNC_PARAMS_1(r6, "addslashes", r7, 0x00F);
			PHALCON_CPY_WRT(value, r6);
			
			PHALCON_ALLOC_ZVAL_MM(r8);
			PHALCON_CONCAT_VSVS(r8, field, "='", value, "'");
			phalcon_array_append(&conditions, r8, PHALCON_SEPARATE_PLZ TSRMLS_CC);
		}
		
		PHALCON_ALLOC_ZVAL_MM(r9);
		PHALCON_CALL_METHOD(r9, record, "getmanager", PHALCON_NO_CHECK);
		PHALCON_CPY_WRT(manager, r9);
		
		PHALCON_ALLOC_ZVAL_MM(r10);
		PHALCON_CALL_METHOD(r10, manager, "getmetadata", PHALCON_NO_CHECK);
		PHALCON_CPY_WRT(meta_data, r10);
		
		PHALCON_ALLOC_ZVAL_MM(r11);
		PHALCON_CALL_METHOD_PARAMS_1(r11, meta_data, "getprimarykeyattributes", record, PHALCON_NO_CHECK);
		PHALCON_CPY_WRT(primary_fields, r11);
		if (phalcon_valid_foreach(primary_fields TSRMLS_CC)) {
			ah1 = Z_ARRVAL_P(primary_fields);
			zend_hash_internal_pointer_reset_ex(ah1, &hp1);
			fes_d73d_1:
			if(zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) != SUCCESS){
				goto fee_d73d_1;
			}
			
			PHALCON_INIT_VAR(primary_field);
			ZVAL_ZVAL(primary_field, *hd, 1, 0);
			PHALCON_INIT_VAR(r12);
			PHALCON_INIT_VAR(r13);
			PHALCON_INIT_VAR(r14);
			PHALCON_CALL_METHOD_PARAMS_1(r14, record, "readattribute", primary_field, PHALCON_NO_CHECK);
			PHALCON_CALL_FUNC_PARAMS_1(r13, "addslashes", r14, 0x00F);
			PHALCON_CONCAT_VSVS(r12, primary_field, "<>'", r13, "'");
			phalcon_array_append(&conditions, r12, PHALCON_SEPARATE_PLZ TSRMLS_CC);
			zend_hash_move_forward_ex(ah1, &hp1);
			goto fes_d73d_1;
			fee_d73d_1:
			if(0){}
		} else {
			return;
		}
		
		PHALCON_INIT_VAR(c0);
		ZVAL_STRING(c0, " AND ", 1);
		
		PHALCON_ALLOC_ZVAL_MM(r15);
		phalcon_fast_join(r15, c0, conditions TSRMLS_CC);
		PHALCON_CPY_WRT(conditions, r15);
		
		PHALCON_ALLOC_ZVAL_MM(r16);
		PHALCON_CALL_STATIC_ZVAL_PARAMS_1(r16, record, "count", conditions);
		
		PHALCON_INIT_VAR(t0);
		ZVAL_LONG(t0, 0);
		
		PHALCON_INIT_VAR(r17);
		is_smaller_function(r17, t0, r16 TSRMLS_CC);
		if (zend_is_true(r17)) {
			PHALCON_ALLOC_ZVAL_MM(r18);
			PHALCON_CALL_METHOD(r18, this_ptr, "getoptions", PHALCON_NO_CHECK);
			PHALCON_CPY_WRT(options, r18);
			eval_int = phalcon_array_isset_string(options, "message", strlen("message")+1);
			if (eval_int) {
				PHALCON_ALLOC_ZVAL_MM(r19);
				phalcon_array_fetch_string(&r19, options, "message", strlen("message"), PHALCON_NOISY TSRMLS_CC);
				PHALCON_INIT_VAR(c1);
				ZVAL_STRING(c1, "unique", 1);
				PHALCON_CALL_METHOD_PARAMS_3_NORETURN(this_ptr, "appendmessage", r19, field, c1, PHALCON_NO_CHECK);
			} else {
				PHALCON_ALLOC_ZVAL_MM(r20);
				PHALCON_CONCAT_SVS(r20, "Value of field '", field, "' is already present in another record");
				PHALCON_INIT_VAR(c2);
				ZVAL_STRING(c2, "unique", 1);
				PHALCON_CALL_METHOD_PARAMS_3_NORETURN(this_ptr, "appendmessage", r20, field, c2, PHALCON_NO_CHECK);
			}
			
			PHALCON_MM_RESTORE();
			RETURN_FALSE;
		}
	}
	PHALCON_MM_RESTORE();
	RETURN_TRUE;
}
예제 #3
0
/**
 * Checks if a role has access to a resource
 *
 * @param string $roleName
 * @param string $resourceName
 * @param string $access
 * @param string $action
 */
PHP_METHOD(Phalcon_Acl_Adapter_Memory, _allowOrDeny){

	zval *role_name, *resource_name, *access, *action;
	zval *roles_names, *exception_message = NULL, *resources_names;
	zval *default_access, *access_list, *internal_access;
	zval *access_name = NULL, *access_key = NULL, *access_key_all = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 4, 0, &role_name, &resource_name, &access, &action);
	
	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 ACL");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
		return;
	}
	
	PHALCON_OBS_VAR(resources_names);
	phalcon_read_property_this(&resources_names, this_ptr, SL("_resourcesNames"), PH_NOISY_CC);
	if (!phalcon_array_isset(resources_names, resource_name)) {
		PHALCON_INIT_NVAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Resource \"", resource_name, "\" does not exist in ACL");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
		return;
	}
	
	PHALCON_OBS_VAR(default_access);
	phalcon_read_property_this(&default_access, this_ptr, SL("_defaultAccess"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(access_list);
	phalcon_read_property_this(&access_list, this_ptr, SL("_accessList"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(internal_access);
	phalcon_read_property_this(&internal_access, this_ptr, SL("_access"), PH_NOISY_CC);
	if (Z_TYPE_P(access) == IS_ARRAY) { 
	
		phalcon_is_iterable(access, &ah0, &hp0, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_HVALUE(access_name);
	
			PHALCON_INIT_NVAR(access_key);
			PHALCON_CONCAT_VSV(access_key, resource_name, "!", access_name);
			if (!phalcon_array_isset(access_list, access_key)) {
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVSVS(exception_message, "Acccess '", access_name, "' does not exist in resource '", resource_name, "'");
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
				return;
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
		phalcon_is_iterable(access, &ah1, &hp1, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
			PHALCON_GET_HVALUE(access_name);
	
			PHALCON_INIT_NVAR(access_key);
			PHALCON_CONCAT_VSVSV(access_key, role_name, "!", resource_name, "!", access_name);
			phalcon_update_property_array(this_ptr, SL("_access"), access_key, action TSRMLS_CC);
			if (!PHALCON_IS_STRING(access_name, "*")) {
	
				PHALCON_INIT_NVAR(access_key_all);
				PHALCON_CONCAT_VSVS(access_key_all, role_name, "!", resource_name, "!*");
				if (!phalcon_array_isset(internal_access, access_key_all)) {
					phalcon_update_property_array(this_ptr, SL("_access"), access_key_all, default_access TSRMLS_CC);
				}
			}
	
			zend_hash_move_forward_ex(ah1, &hp1);
		}
	
	} else {
		if (!PHALCON_IS_STRING(access, "*")) {
	
			PHALCON_INIT_NVAR(access_key);
			PHALCON_CONCAT_VSV(access_key, resource_name, "!", access);
			if (!phalcon_array_isset(access_list, access_key)) {
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVSVS(exception_message, "Acccess '", access, "' does not exist in resource '", resource_name, "'");
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
				return;
			}
		}
	
		PHALCON_INIT_NVAR(access_key);
		PHALCON_CONCAT_VSVSV(access_key, role_name, "!", resource_name, "!", access);
	
		/** 
		 * Define the access action for the specified accessKey
		 */
		phalcon_update_property_array(this_ptr, SL("_access"), access_key, action TSRMLS_CC);
		if (!PHALCON_IS_STRING(access, "*")) {
	
			PHALCON_INIT_NVAR(access_key);
			PHALCON_CONCAT_VSVS(access_key, role_name, "!", resource_name, "!*");
	
			/** 
			 * If there is no default action for all the rest actions in the resource set the
			 * default one
			 */
			if (!phalcon_array_isset(internal_access, access_key)) {
				phalcon_update_property_array(this_ptr, SL("_access"), access_key, default_access TSRMLS_CC);
			}
		}
	}
	
	PHALCON_MM_RESTORE();
}
예제 #4
0
파일: dialect.c 프로젝트: Gildus/cphalcon
/**
 * Transforms an intermediate representation for a expression into a database system valid expression
 *
 * @param array $expression
 * @param string $escapeChar
 * @return string
 */
PHP_METHOD(Phalcon_Db_Dialect, getSqlExpression){

	zval *expression, *escape_char = NULL, *type, *name = NULL, *escaped_name = NULL;
	zval *domain, *escaped_domain = NULL, *value = NULL, *operator = NULL;
	zval *left = NULL, *expression_left = NULL, *right = NULL, *expression_right = NULL;
	zval *binary_expr, *unary_expr = NULL, *expression_group;
	zval *sql_arguments, *arguments, *argument = NULL, *argument_expression = NULL;
	zval *arguments_joined, *function_expression = NULL;
	zval *sql_items, *items, *item = NULL, *item_expression = NULL;
	zval *list_expression, *group_expression;
	zval *exception_message;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;

	PHALCON_MM_GROW();

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

	if (!escape_char) {
		PHALCON_INIT_VAR(escape_char);
	} else {
		PHALCON_SEPARATE_PARAM(escape_char);
	}
	
	if (PHALCON_GLOBAL(db).escape_identifiers) {
		if (Z_TYPE_P(escape_char) == IS_NULL) {
			PHALCON_OBS_NVAR(escape_char);
			phalcon_read_property(&escape_char, this_ptr, SL("_escapeChar"), PH_NOISY_CC);
		}
	}
	if (Z_TYPE_P(expression) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid SQL expression");
		return;
	}
	
	if (!phalcon_array_isset_string(expression, SS("type"))) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid SQL expression");
		return;
	}
	
	PHALCON_OBS_VAR(type);
	phalcon_array_fetch_string(&type, expression, SL("type"), PH_NOISY_CC);
	
	/** 
	 * Resolve qualified expressions
	 */
	if (PHALCON_IS_STRING(type, "qualified")) {
	
		PHALCON_OBS_VAR(name);
		phalcon_array_fetch_string(&name, expression, SL("name"), PH_NOISY_CC);
		if (PHALCON_GLOBAL(db).escape_identifiers) {
			PHALCON_INIT_VAR(escaped_name);
			PHALCON_CONCAT_VVV(escaped_name, escape_char, name, escape_char);
		} else {
			PHALCON_CPY_WRT(escaped_name, name);
		}
	
		/** 
		 * A domain could be a table/schema
		 */
		if (phalcon_array_isset_string(expression, SS("domain"))) {
	
			PHALCON_OBS_VAR(domain);
			phalcon_array_fetch_string(&domain, expression, SL("domain"), PH_NOISY_CC);
			if (PHALCON_GLOBAL(db).escape_identifiers) {
				PHALCON_INIT_VAR(escaped_domain);
				PHALCON_CONCAT_VVVSV(escaped_domain, escape_char, domain, escape_char, ".", escaped_name);
			} else {
				PHALCON_INIT_NVAR(escaped_domain);
				PHALCON_CONCAT_VSV(escaped_domain, domain, ".", escaped_name);
			}
	
	
			RETURN_CTOR(escaped_domain);
		}
	
	
		RETURN_CCTOR(escaped_name);
	}
	
	/** 
	 * Resolve literal expressions
	 */
	if (PHALCON_IS_STRING(type, "literal")) {
		PHALCON_OBS_VAR(value);
		phalcon_array_fetch_string(&value, expression, SL("value"), PH_NOISY_CC);
		RETURN_CCTOR(value);
	}
	
	/** 
	 * Resolve binary operations expressions
	 */
	if (PHALCON_IS_STRING(type, "binary-op")) {
		PHALCON_OBS_VAR(operator);
		phalcon_array_fetch_string(&operator, expression, SL("op"), PH_NOISY_CC);
	
		PHALCON_OBS_VAR(left);
		phalcon_array_fetch_string(&left, expression, SL("left"), PH_NOISY_CC);
	
		PHALCON_INIT_VAR(expression_left);
		PHALCON_CALL_METHOD_PARAMS_2(expression_left, this_ptr, "getsqlexpression", left, escape_char);
	
		PHALCON_OBS_VAR(right);
		phalcon_array_fetch_string(&right, expression, SL("right"), PH_NOISY_CC);
	
		PHALCON_INIT_VAR(expression_right);
		PHALCON_CALL_METHOD_PARAMS_2(expression_right, this_ptr, "getsqlexpression", right, escape_char);
	
		PHALCON_INIT_VAR(binary_expr);
		PHALCON_CONCAT_VSVSV(binary_expr, expression_left, " ", operator, " ", expression_right);
		RETURN_CTOR(binary_expr);
	}
	
	/** 
	 * Resolve unary operations expressions
	 */
	if (PHALCON_IS_STRING(type, "unary-op")) {
	
		PHALCON_OBS_NVAR(operator);
		phalcon_array_fetch_string(&operator, expression, SL("op"), PH_NOISY_CC);
	
		/** 
		 * Some unary operators uses the left operand...
		 */
		if (phalcon_array_isset_string(expression, SS("left"))) {
			PHALCON_OBS_NVAR(left);
			phalcon_array_fetch_string(&left, expression, SL("left"), PH_NOISY_CC);
	
			PHALCON_INIT_NVAR(expression_left);
			PHALCON_CALL_METHOD_PARAMS_2(expression_left, this_ptr, "getsqlexpression", left, escape_char);
	
			PHALCON_INIT_VAR(unary_expr);
			PHALCON_CONCAT_VV(unary_expr, expression_left, operator);
			RETURN_CTOR(unary_expr);
		}
	
		/** 
		 * ...Others uses the right operand
		 */
		if (phalcon_array_isset_string(expression, SS("right"))) {
			PHALCON_OBS_NVAR(right);
			phalcon_array_fetch_string(&right, expression, SL("right"), PH_NOISY_CC);
	
			PHALCON_INIT_NVAR(expression_right);
			PHALCON_CALL_METHOD_PARAMS_2(expression_right, this_ptr, "getsqlexpression", right, escape_char);
	
			PHALCON_INIT_NVAR(unary_expr);
			PHALCON_CONCAT_VV(unary_expr, operator, expression_right);
			RETURN_CTOR(unary_expr);
		}
	}
	
	/** 
	 * Resolve placeholder
	 */
	if (PHALCON_IS_STRING(type, "placeholder")) {
		PHALCON_OBS_NVAR(value);
		phalcon_array_fetch_string(&value, expression, SL("value"), PH_NOISY_CC);
		RETURN_CCTOR(value);
	}
	
	/** 
	 * Resolve parentheses
	 */
	if (PHALCON_IS_STRING(type, "parentheses")) {
		PHALCON_OBS_NVAR(left);
		phalcon_array_fetch_string(&left, expression, SL("left"), PH_NOISY_CC);
	
		PHALCON_INIT_NVAR(expression_left);
		PHALCON_CALL_METHOD_PARAMS_2(expression_left, this_ptr, "getsqlexpression", left, escape_char);
	
		PHALCON_INIT_VAR(expression_group);
		PHALCON_CONCAT_SVS(expression_group, "(", expression_left, ")");
		RETURN_CTOR(expression_group);
	}
	
	/** 
	 * Resolve function calls
	 */
	if (PHALCON_IS_STRING(type, "functionCall")) {
	
		PHALCON_OBS_NVAR(name);
		phalcon_array_fetch_string(&name, expression, SL("name"), PH_NOISY_CC);
	
		PHALCON_INIT_VAR(sql_arguments);
		array_init(sql_arguments);
		if (phalcon_array_isset_string(expression, SS("arguments"))) {
	
			PHALCON_OBS_VAR(arguments);
			phalcon_array_fetch_string(&arguments, expression, SL("arguments"), PH_NOISY_CC);
	
			if (!phalcon_is_iterable(arguments, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
				return;
			}
	
			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
				PHALCON_GET_FOREACH_VALUE(argument);
	
				PHALCON_INIT_NVAR(argument_expression);
				PHALCON_CALL_METHOD_PARAMS_2(argument_expression, this_ptr, "getsqlexpression", argument, escape_char);
				phalcon_array_append(&sql_arguments, argument_expression, PH_SEPARATE TSRMLS_CC);
	
				zend_hash_move_forward_ex(ah0, &hp0);
			}
	
			PHALCON_INIT_VAR(arguments_joined);
			phalcon_fast_join_str(arguments_joined, SL(", "), sql_arguments TSRMLS_CC);
	
			PHALCON_INIT_VAR(function_expression);
			PHALCON_CONCAT_VSVS(function_expression, name, "(", arguments_joined, ")");
		} else {
			PHALCON_INIT_NVAR(function_expression);
			PHALCON_CONCAT_VS(function_expression, name, "()");
		}
	
	
		RETURN_CTOR(function_expression);
	}
	
	/** 
	 * Resolve lists
	 */
	if (PHALCON_IS_STRING(type, "list")) {
	
		PHALCON_INIT_VAR(sql_items);
		array_init(sql_items);
	
		PHALCON_OBS_VAR(items);
		phalcon_array_fetch_long(&items, expression, 0, PH_NOISY_CC);
	
		if (!phalcon_is_iterable(items, &ah1, &hp1, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
			PHALCON_GET_FOREACH_VALUE(item);
	
			PHALCON_INIT_NVAR(item_expression);
			PHALCON_CALL_METHOD_PARAMS_2(item_expression, this_ptr, "getsqlexpression", item, escape_char);
			phalcon_array_append(&sql_items, item_expression, PH_SEPARATE TSRMLS_CC);
	
			zend_hash_move_forward_ex(ah1, &hp1);
		}
	
		PHALCON_INIT_VAR(list_expression);
		phalcon_fast_join_str(list_expression, SL(", "), sql_items TSRMLS_CC);
	
		PHALCON_INIT_VAR(group_expression);
		PHALCON_CONCAT_SVS(group_expression, "(", list_expression, ")");
	
		RETURN_CTOR(group_expression);
	}
	
	/** 
	 * Resolve *
	 */
	if (PHALCON_IS_STRING(type, "all")) {
		PHALCON_MM_RESTORE();
		RETURN_STRING("*", 1);
	}
	
	/** 
	 * Expression type wasn't found
	 */
	PHALCON_INIT_VAR(exception_message);
	PHALCON_CONCAT_SVS(exception_message, "Invalid SQL expression type '", type, "'");
	PHALCON_THROW_EXCEPTION_ZVAL(phalcon_db_exception_ce, exception_message);
	return;
}
예제 #5
0
파일: criteria.c 프로젝트: meibk/cphalcon
/**
 * Builds a Phalcon\Mvc\Model\Criteria based on an input array like $_POST
 *
 * @param Phalcon\DI $dependencyInjector
 * @param string $modelName
 * @param array $data
 */
PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){

	zval *dependency_injector = NULL, *model_name = NULL, *data = NULL;
	zval *conditions = NULL, *number_data = NULL, *service = NULL, *meta_data = NULL;
	zval *model = NULL, *data_types = NULL, *bind = NULL, *value = NULL, *field = NULL, *type = NULL;
	zval *condition = NULL, *value_pattern = NULL, *criteria = NULL, *number_conditions = NULL;
	zval *join_conditions = NULL;
	zval *c0 = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;
	int eval_int;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", &dependency_injector, &model_name, &data) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (Z_TYPE_P(data) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Input data must be an Array");
		return;
	}
	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A dependency injector container is required to obtain the ORM services");
		return;
	}
	
	PHALCON_INIT_VAR(conditions);
	array_init(conditions);
	
	PHALCON_INIT_VAR(number_data);
	phalcon_fast_count(number_data, data TSRMLS_CC);
	if (!phalcon_compare_strict_long(number_data, 0 TSRMLS_CC)) {
		PHALCON_INIT_VAR(service);
		ZVAL_STRING(service, "modelsMetadata", 1);
		
		PHALCON_INIT_VAR(meta_data);
		PHALCON_CALL_METHOD_PARAMS_1(meta_data, dependency_injector, "getshared", service, PH_NO_CHECK);
		ce0 = phalcon_fetch_class(model_name TSRMLS_CC);
		
		PHALCON_INIT_VAR(model);
		object_init_ex(model, ce0);
		PHALCON_CALL_METHOD_NORETURN(model, "__construct", PH_CHECK);
		
		PHALCON_INIT_VAR(data_types);
		PHALCON_CALL_METHOD_PARAMS_1(data_types, meta_data, "getdatatypes", model, PH_NO_CHECK);
		
		PHALCON_INIT_VAR(bind);
		array_init(bind);
		if (!phalcon_valid_foreach(data TSRMLS_CC)) {
			return;
		}
		
		ah0 = Z_ARRVAL_P(data);
		zend_hash_internal_pointer_reset_ex(ah0, &hp0);
		fes_1d26_0:
			if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
				goto fee_1d26_0;
			}
			
			PHALCON_INIT_VAR(field);
			PHALCON_GET_FOREACH_KEY(field, ah0, hp0);
			PHALCON_INIT_VAR(value);
			ZVAL_ZVAL(value, *hd, 1, 0);
			eval_int = phalcon_array_isset(data_types, field);
			if (eval_int) {
				if (Z_TYPE_P(value) != IS_NULL) {
					if (!PHALCON_COMPARE_STRING(value, "")) {
						PHALCON_INIT_VAR(type);
						phalcon_array_fetch(&type, data_types, field, PH_NOISY_CC);
						if (phalcon_compare_strict_long(type, 2 TSRMLS_CC)) {
							PHALCON_INIT_VAR(condition);
							PHALCON_CONCAT_VSVS(condition, field, " LIKE :", field, ":");
							
							PHALCON_INIT_VAR(value_pattern);
							PHALCON_CONCAT_SVS(value_pattern, "%", value, "%");
							phalcon_array_update_zval(&bind, field, &value_pattern, PH_COPY | PH_SEPARATE TSRMLS_CC);
						} else {
							PHALCON_INIT_VAR(condition);
							PHALCON_CONCAT_VSVS(condition, field, "=:", field, ":");
							phalcon_array_update_zval(&bind, field, &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
						}
						
						phalcon_array_append(&conditions, condition, PH_SEPARATE TSRMLS_CC);
					}
				}
			}
			zend_hash_move_forward_ex(ah0, &hp0);
			goto fes_1d26_0;
		fee_1d26_0:
		if(0){}
		
	}
	
	PHALCON_INIT_VAR(criteria);
	object_init_ex(criteria, phalcon_mvc_model_criteria_ce);
	PHALCON_CALL_METHOD_NORETURN(criteria, "__construct", PH_CHECK);
	
	PHALCON_INIT_VAR(number_conditions);
	phalcon_fast_count(number_conditions, conditions TSRMLS_CC);
	if (!phalcon_compare_strict_long(number_conditions, 0 TSRMLS_CC)) {
		PHALCON_INIT_VAR(c0);
		ZVAL_STRING(c0, " AND ", 1);
		PHALCON_INIT_VAR(join_conditions);
		phalcon_fast_join(join_conditions, c0, conditions TSRMLS_CC);
		PHALCON_CALL_METHOD_PARAMS_1_NORETURN(criteria, "where", join_conditions, PH_NO_CHECK);
		PHALCON_CALL_METHOD_PARAMS_1_NORETURN(criteria, "bind", bind, PH_NO_CHECK);
	}
	
	
	RETURN_CTOR(criteria);
}
예제 #6
0
/**
 * Builds a Phalcon\Mvc\Model\Criteria based on an input array like $_POST
 *
 * @param Phalcon\DiInterface $dependencyInjector
 * @param string $modelName
 * @param array $data
 * @return static
 */
PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){

	zval *dependency_injector, *model_name, *data;
	zval *conditions, *service, *meta_data, *model;
	zval *data_types, *bind, *value = NULL, *field = NULL, *type = NULL, *condition = NULL;
	zval *value_pattern = NULL, *criteria, *join_conditions;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 3, 0, &dependency_injector, &model_name, &data);
	
	if (Z_TYPE_P(data) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Input data must be an Array");
		return;
	}
	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A dependency injector container is required to obtain the ORM services");
		return;
	}
	
	PHALCON_INIT_VAR(conditions);
	array_init(conditions);
	if (phalcon_fast_count_ev(data TSRMLS_CC)) {
	
		PHALCON_INIT_VAR(service);
		ZVAL_STRING(service, "modelsMetadata", 1);
	
		PHALCON_INIT_VAR(meta_data);
		phalcon_call_method_p1(meta_data, dependency_injector, "getshared", service);
		ce0 = phalcon_fetch_class(model_name TSRMLS_CC);
	
		PHALCON_INIT_VAR(model);
		object_init_ex(model, ce0);
		if (phalcon_has_constructor(model TSRMLS_CC)) {
			phalcon_call_method_noret(model, "__construct");
		}
	
		PHALCON_INIT_VAR(data_types);
		phalcon_call_method_p1(data_types, meta_data, "getdatatypes", model);
	
		PHALCON_INIT_VAR(bind);
		array_init(bind);
	
		/** 
		 * We look for attributes in the array passed as data
		 */
		phalcon_is_iterable(data, &ah0, &hp0, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_HKEY(field, ah0, hp0);
			PHALCON_GET_HVALUE(value);
	
			if (phalcon_array_isset(data_types, field)) {
				if (Z_TYPE_P(value) != IS_NULL) {
					if (!PHALCON_IS_STRING(value, "")) {
	
						PHALCON_OBS_NVAR(type);
						phalcon_array_fetch(&type, data_types, field, PH_NOISY_CC);
						if (PHALCON_IS_LONG(type, 2)) {
							/** 
							 * For varchar types we use LIKE operator
							 */
							PHALCON_INIT_NVAR(condition);
							PHALCON_CONCAT_VSVS(condition, field, " LIKE :", field, ":");
	
							PHALCON_INIT_NVAR(value_pattern);
							PHALCON_CONCAT_SVS(value_pattern, "%", value, "%");
							phalcon_array_update_zval(&bind, field, &value_pattern, PH_COPY | PH_SEPARATE TSRMLS_CC);
						} else {
							/** 
							 * For the rest of data types we use a plain = operator
							 */
							PHALCON_INIT_NVAR(condition);
							PHALCON_CONCAT_VSVS(condition, field, "=:", field, ":");
							phalcon_array_update_zval(&bind, field, &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
						}
	
						phalcon_array_append(&conditions, condition, PH_SEPARATE TSRMLS_CC);
					}
				}
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
	}
	
	/** 
	 * Create an object instance and pass the paramaters to it
	 */
	PHALCON_INIT_VAR(criteria);
	object_init_ex(criteria, phalcon_mvc_model_criteria_ce);
	if (phalcon_fast_count_ev(conditions TSRMLS_CC)) {
		PHALCON_INIT_VAR(join_conditions);
		phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC);
		phalcon_call_method_p1_noret(criteria, "where", join_conditions);
		phalcon_call_method_p1_noret(criteria, "bind", bind);
	}
	
	RETURN_CTOR(criteria);
}
예제 #7
0
/**
 * Appends a NOT IN condition to the current conditions
 *
 *<code>
 *	$criteria->notInWhere('id', [1, 2, 3]);
 *</code>
 *
 * @param string $expr
 * @param array $values
 * @return Phalcon\Mvc\Model\Query\Builder
 */
PHP_METHOD(Phalcon_Mvc_Model_Criteria, notInWhere){

	zval *expr, *values, *hidden_param, *bind_params;
	zval *bind_keys, *value = NULL, *key = NULL, *query_key = NULL, *joined_keys;
	zval *conditions;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 0, &expr, &values);
	
	if (Z_TYPE_P(values) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Values must be an array");
		return;
	}
	
	PHALCON_OBS_VAR(hidden_param);
	phalcon_read_property_this(&hidden_param, this_ptr, SL("_hiddenParamNumber"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(bind_params);
	array_init(bind_params);
	
	PHALCON_INIT_VAR(bind_keys);
	array_init(bind_keys);
	
	phalcon_is_iterable(values, &ah0, &hp0, 0, 0);
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HVALUE(value);
	
		/** 
		 * Key with auto bind-params
		 */
		PHALCON_INIT_NVAR(key);
		PHALCON_CONCAT_SV(key, "phi", hidden_param);
	
		PHALCON_INIT_NVAR(query_key);
		PHALCON_CONCAT_SVS(query_key, ":", key, ":");
		phalcon_array_append(&bind_keys, query_key, PH_SEPARATE TSRMLS_CC);
		phalcon_array_update_zval(&bind_params, key, &value, PH_COPY | PH_SEPARATE TSRMLS_CC);
		PHALCON_SEPARATE(hidden_param);
		phalcon_increment(hidden_param);
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	PHALCON_INIT_VAR(joined_keys);
	phalcon_fast_join_str(joined_keys, SL(", "), bind_keys TSRMLS_CC);
	
	/** 
	 * Create a standard IN condition with bind params
	 */
	PHALCON_INIT_VAR(conditions);
	PHALCON_CONCAT_VSVS(conditions, expr, " NOT IN (", joined_keys, ")");
	
	/** 
	 * Append the IN to the current conditions using and 'and'
	 */
	phalcon_call_method_p2_noret(this_ptr, "andwhere", conditions, bind_params);
	phalcon_update_property_this(this_ptr, SL("_hiddenParamNumber"), hidden_param TSRMLS_CC);
	
	RETURN_THIS();
}
예제 #8
0
파일: tag.c 프로젝트: meibk/cphalcon
/**
 * Builds a HTML FORM tag
 *
 * <code>
 * echo Phalcon\Tag::form("posts/save");
 * echo Phalcon\Tag::form(array("posts/save", "method" => "post"));
 * </code>
 *
 * @param array $parameters
 * @return string
 */
PHP_METHOD(Phalcon_Tag, form){

	zval *parameters = NULL, *params = NULL, *dispatcher = NULL, *dispatch_params = NULL;
	zval *slash = NULL, *action_parameters = NULL, *action = NULL, *controller_name = NULL;
	zval *action_name = NULL, *url = NULL, *form_action = NULL, *query_parameters = NULL;
	zval *code = NULL, *avalue = NULL, *key = NULL, *attribute = NULL, *final_code = NULL;
	zval *r0 = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;
	int eval_int;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &parameters) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (!parameters) {
		PHALCON_ALLOC_ZVAL_MM(parameters);
		ZVAL_NULL(parameters);
	} else {
		PHALCON_SEPARATE_PARAM(parameters);
	}
	
	if (Z_TYPE_P(parameters) != IS_ARRAY) { 
		PHALCON_INIT_VAR(params);
		array_init(params);
		phalcon_array_append(&params, parameters, PH_SEPARATE TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(params, parameters);
	}
	
	PHALCON_INIT_VAR(dispatcher);
	PHALCON_CALL_SELF(dispatcher, this_ptr, "getdispatcherservice");
	
	PHALCON_INIT_VAR(dispatch_params);
	PHALCON_CALL_METHOD(dispatch_params, dispatcher, "getparams", PH_NO_CHECK);
	
	PHALCON_INIT_VAR(slash);
	ZVAL_STRING(slash, "/", 1);
	
	PHALCON_INIT_VAR(action_parameters);
	phalcon_fast_join(action_parameters, slash, dispatch_params TSRMLS_CC);
	eval_int = phalcon_array_isset_long(params, 0);
	if (!eval_int) {
		eval_int = phalcon_array_isset_string(params, SL("action")+1);
		if (eval_int) {
			PHALCON_INIT_VAR(action);
			phalcon_array_fetch_string(&action, params, SL("action"), PH_NOISY_CC);
		} else {
			PHALCON_INIT_VAR(controller_name);
			PHALCON_CALL_METHOD(controller_name, dispatcher, "getcontrollername", PH_NO_CHECK);
			
			PHALCON_INIT_VAR(action_name);
			PHALCON_CALL_METHOD(action_name, dispatcher, "getactionname", PH_NO_CHECK);
			
			PHALCON_INIT_VAR(action);
			PHALCON_CONCAT_VSV(action, controller_name, "/", action_name);
		}
	} else {
		PHALCON_INIT_VAR(action);
		phalcon_array_fetch_long(&action, params, 0, PH_NOISY_CC);
	}
	
	eval_int = phalcon_array_isset_string(params, SL("method")+1);
	if (!eval_int) {
		phalcon_array_update_string_string(&params, SL("method"), SL("post"), PH_SEPARATE TSRMLS_CC);
	}
	
	PHALCON_INIT_VAR(url);
	PHALCON_CALL_SELF(url, this_ptr, "geturlservice");
	if (zend_is_true(action_parameters)) {
		PHALCON_ALLOC_ZVAL_MM(r0);
		PHALCON_CALL_METHOD_PARAMS_1(r0, url, "get", action, PH_NO_CHECK);
		PHALCON_CPY_WRT(action, r0);
	} else {
		PHALCON_INIT_VAR(form_action);
		PHALCON_CONCAT_VSV(form_action, action, "/", action_parameters);
		
		PHALCON_INIT_VAR(action);
		PHALCON_CALL_METHOD_PARAMS_1(action, url, "get", form_action, PH_NO_CHECK);
	}
	
	eval_int = phalcon_array_isset_string(params, SL("parameters")+1);
	if (eval_int) {
		PHALCON_INIT_VAR(parameters);
		phalcon_array_fetch_string(&parameters, params, SL("parameters"), PH_NOISY_CC);
		
		PHALCON_INIT_VAR(query_parameters);
		PHALCON_CONCAT_SV(query_parameters, "?", parameters);
		phalcon_concat_self(&action, query_parameters TSRMLS_CC);
	}
	
	PHALCON_INIT_VAR(code);
	PHALCON_CONCAT_SVS(code, "<form action=\"", action, "\" ");
	if (!phalcon_valid_foreach(params TSRMLS_CC)) {
		return;
	}
	
	ah0 = Z_ARRVAL_P(params);
	zend_hash_internal_pointer_reset_ex(ah0, &hp0);
	fes_9b93_5:
		if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
			goto fee_9b93_5;
		}
		
		PHALCON_INIT_VAR(key);
		PHALCON_GET_FOREACH_KEY(key, ah0, hp0);
		PHALCON_INIT_VAR(avalue);
		ZVAL_ZVAL(avalue, *hd, 1, 0);
		if (Z_TYPE_P(key) != IS_LONG) {
			PHALCON_INIT_VAR(attribute);
			PHALCON_CONCAT_VSVS(attribute, key, "= \"", avalue, "\" ");
			phalcon_concat_self(&code, attribute TSRMLS_CC);
		}
		zend_hash_move_forward_ex(ah0, &hp0);
		goto fes_9b93_5;
	fee_9b93_5:
	
	PHALCON_INIT_VAR(final_code);
	PHALCON_CONCAT_VS(final_code, code, ">");
	
	RETURN_CTOR(final_code);
}
예제 #9
0
파일: tag.c 프로젝트: meibk/cphalcon
/**
 * Builds a HTML TEXTAREA tag
 *
 * <code>echo Phalcon\Tag::textArea(array("comments", "cols" => 10, "rows" => 4))</code>
 *
 * @param array $parameters
 * @return string
 */
PHP_METHOD(Phalcon_Tag, textArea){

	zval *parameters = NULL, *params = NULL, *id = NULL, *name = NULL, *content = NULL, *code = NULL;
	zval *avalue = NULL, *key = NULL, *attribute = NULL, *end_code = NULL;
	zval *r0 = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;
	int eval_int;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &parameters) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (Z_TYPE_P(parameters) != IS_ARRAY) { 
		PHALCON_INIT_VAR(params);
		array_init(params);
		phalcon_array_append(&params, parameters, PH_SEPARATE TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(params, parameters);
	}
	eval_int = phalcon_array_isset_long(params, 0);
	if (!eval_int) {
		PHALCON_ALLOC_ZVAL_MM(r0);
		phalcon_array_fetch_string(&r0, params, SL("id"), PH_NOISY_CC);
		phalcon_array_update_long(&params, 0, &r0, PH_COPY | PH_SEPARATE TSRMLS_CC);
	}
	
	PHALCON_INIT_VAR(id);
	phalcon_array_fetch_long(&id, params, 0, PH_NOISY_CC);
	eval_int = phalcon_array_isset_string(params, SL("name")+1);
	if (!eval_int) {
		phalcon_array_update_string(&params, SL("name"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
	} else {
		PHALCON_INIT_VAR(name);
		phalcon_array_fetch_string(&name, params, SL("name"), PH_NOISY_CC);
		if (!zend_is_true(name)) {
			phalcon_array_update_string(&params, SL("name"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
		}
	}
	
	eval_int = phalcon_array_isset_string(params, SL("id")+1);
	if (!eval_int) {
		phalcon_array_update_string(&params, SL("id"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC);
	}
	
	eval_int = phalcon_array_isset_string(params, SL("value")+1);
	if (eval_int) {
		PHALCON_INIT_VAR(content);
		phalcon_array_fetch_string(&content, params, SL("value"), PH_NOISY_CC);
		PHALCON_SEPARATE(params);
		phalcon_array_unset_string(params, SL("value")+1);
	} else {
		PHALCON_INIT_VAR(content);
		PHALCON_CALL_SELF_PARAMS_1(content, this_ptr, "getvalue", id);
	}
	
	PHALCON_INIT_VAR(code);
	ZVAL_STRING(code, "<textarea ", 1);
	if (!phalcon_valid_foreach(params TSRMLS_CC)) {
		return;
	}
	
	ah0 = Z_ARRVAL_P(params);
	zend_hash_internal_pointer_reset_ex(ah0, &hp0);
	fes_9b93_4:
		if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
			goto fee_9b93_4;
		}
		
		PHALCON_INIT_VAR(key);
		PHALCON_GET_FOREACH_KEY(key, ah0, hp0);
		PHALCON_INIT_VAR(avalue);
		ZVAL_ZVAL(avalue, *hd, 1, 0);
		if (Z_TYPE_P(key) != IS_LONG) {
			PHALCON_INIT_VAR(attribute);
			PHALCON_CONCAT_VSVS(attribute, key, "=\"", avalue, "\" ");
			phalcon_concat_self(&code, attribute TSRMLS_CC);
		}
		zend_hash_move_forward_ex(ah0, &hp0);
		goto fes_9b93_4;
	fee_9b93_4:
	
	PHALCON_INIT_VAR(end_code);
	PHALCON_CONCAT_SVS(end_code, ">", content, "</textarea>");
	phalcon_concat_self(&code, end_code TSRMLS_CC);
	
	RETURN_CTOR(code);
}
예제 #10
0
파일: tag.c 프로젝트: meibk/cphalcon
/**
 * Builds a HTML input[type="submit"] tag
 *
 * <code>echo Phalcon\Tag::submitButton("Save")</code>
 *
 * @param array $params
 * @return string
 */
PHP_METHOD(Phalcon_Tag, submitButton){

	zval *parameters = NULL, *params = NULL, *value = NULL, *code = NULL, *avalue = NULL;
	zval *key = NULL, *attribute = NULL;
	zval *t0 = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	char *hash_index;
	uint hash_index_len;
	ulong hash_num;
	int hash_type;
	int eval_int;

	PHALCON_MM_GROW();
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &parameters) == FAILURE) {
		PHALCON_MM_RESTORE();
		RETURN_NULL();
	}

	if (Z_TYPE_P(parameters) != IS_ARRAY) { 
		PHALCON_INIT_VAR(params);
		array_init(params);
		phalcon_array_append(&params, parameters, PH_SEPARATE TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT(params, parameters);
	}
	eval_int = phalcon_array_isset_string(params, SL("value")+1);
	if (eval_int) {
		PHALCON_INIT_VAR(value);
		phalcon_array_fetch_string(&value, params, SL("value"), PH_NOISY_CC);
		PHALCON_SEPARATE(params);
		phalcon_array_unset_string(params, SL("value")+1);
	} else {
		eval_int = phalcon_array_isset_long(params, 0);
		if (eval_int) {
			PHALCON_INIT_VAR(value);
			phalcon_array_fetch_long(&value, params, 0, PH_NOISY_CC);
		} else {
			PHALCON_INIT_VAR(value);
			ZVAL_STRING(value, "", 1);
		}
	}
	
	PHALCON_INIT_VAR(code);
	PHALCON_CONCAT_SVS(code, "<input type=\"submit\" value=\"", value, "\" ");
	if (!phalcon_valid_foreach(params TSRMLS_CC)) {
		return;
	}
	
	ah0 = Z_ARRVAL_P(params);
	zend_hash_internal_pointer_reset_ex(ah0, &hp0);
	fes_9b93_3:
		if(zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) != SUCCESS){
			goto fee_9b93_3;
		}
		
		PHALCON_INIT_VAR(key);
		PHALCON_GET_FOREACH_KEY(key, ah0, hp0);
		PHALCON_INIT_VAR(avalue);
		ZVAL_ZVAL(avalue, *hd, 1, 0);
		if (Z_TYPE_P(key) != IS_LONG) {
			PHALCON_INIT_VAR(attribute);
			PHALCON_CONCAT_VSVS(attribute, key, "=\"", avalue, "\" ");
			phalcon_concat_self(&code, attribute TSRMLS_CC);
		}
		zend_hash_move_forward_ex(ah0, &hp0);
		goto fes_9b93_3;
	fee_9b93_3:
	
	PHALCON_INIT_VAR(t0);
	ZVAL_STRING(t0, " />", 1);
	phalcon_concat_self(&code, t0 TSRMLS_CC);
	
	RETURN_CTOR(code);
}
예제 #11
0
파일: where.c 프로젝트: dreamsxin/cphalcon7
/**
 * Gets the type of PHQL queries
 *
 *
 * @return int
 */
PHP_METHOD(Phalcon_Mvc_Model_Query_Builder_Where, setConditions){

	zval *conditions, *bind_params = NULL, *bind_types = NULL, *type = NULL, merge = {}, merged_conditions = {}, merged_bind_params = {}, merged_bind_types;
	zval joind_condition = {}, *single_condition_array = NULL, current_conditions = {}, new_conditions = {};
	zend_string *str_key;
	ulong idx;

	phalcon_fetch_params(0, 1, 3, &conditions, &bind_params, &bind_types, &type);

	if (!bind_params) {
		bind_params = &PHALCON_GLOBAL(z_null);
	}

	if (!bind_types) {
		bind_types = &PHALCON_GLOBAL(z_null);
	}

	if (!type) {
		type = &PHALCON_GLOBAL(z_null);
	}

	ZVAL_BOOL(&merge, Z_TYPE_P(type) != IS_NULL ? 1 : 0);

	if (Z_TYPE_P(conditions) == IS_ARRAY) {
		/* ----------- INITIALIZING LOOP VARIABLES ----------- */

		/*
		 * array containing single condition for example:
		 * array(
		 *      array(
		 *           'status = :status:',
		 *           array('status' => 5),
		 *           array('status' => PDO::PARAM_INT),
		 *      ),
		 *      'name' => 'Dreamsxin',
		 * )
		 */
		array_init(&merged_conditions);
		array_init(&merged_bind_params);
		array_init(&merged_bind_types);

		ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(conditions), idx, str_key, single_condition_array) {
			zval single_condition_key = {}, condition_string = {}, tmp_bind_params = {}, tmp_bind_types = {};
			if (str_key) {
				ZVAL_STR(&single_condition_key, str_key);
			} else {
				ZVAL_LONG(&single_condition_key, idx);
			}
			if (Z_TYPE_P(single_condition_array) == IS_ARRAY
				&& phalcon_array_isset_fetch_long(&condition_string, single_condition_array, 0)
				&& phalcon_array_isset_fetch_long(&tmp_bind_params, single_condition_array, 1)
				&& Z_TYPE(condition_string) == IS_STRING
				&& Z_TYPE(tmp_bind_params) == IS_ARRAY
			) {	
				phalcon_array_update_zval(&merged_conditions, &condition_string, &condition_string, PH_COPY);
				phalcon_array_merge_recursive_n(&merged_bind_params, &tmp_bind_params);

				if (phalcon_array_isset_fetch_long(&tmp_bind_types, single_condition_array, 2) && Z_TYPE(tmp_bind_types) == IS_ARRAY) {
					phalcon_array_merge_recursive_n(&merged_bind_types, &tmp_bind_types);
				}
			} else if (Z_TYPE(single_condition_key) == IS_STRING) {
				PHALCON_CONCAT_VSVS(&condition_string, &single_condition_key, " = :", &single_condition_key, ":");

				phalcon_array_update_zval(&merged_conditions, &single_condition_key, &condition_string, PH_COPY);

				if (Z_TYPE_P(single_condition_array) == IS_ARRAY) {
					phalcon_array_merge_recursive_n(&merged_bind_params, single_condition_array);
				} else {
					phalcon_array_update_zval(&merged_bind_params, &single_condition_key, single_condition_array, PH_COPY);
				}
			}
		} ZEND_HASH_FOREACH_END();
예제 #12
0
/**
 * Builds a Phalcon\Mvc\Model\Criteria based on an input array like $_POST
 *
 * @param Phalcon\DiInterface $dependencyInjector
 * @param string $modelName
 * @param array $data
 * @return Phalcon\Mvc\Model\Criteria
 */
PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){

	zval *dependency_injector, *model_name, *data;
	zval *conditions, *service, *meta_data = NULL, *model;
	zval *data_types = NULL, *bind, *value = NULL, *field = NULL, *type, *condition = NULL;
	zval *value_pattern = NULL, *join_conditions;
	zval *column_map = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;
	zend_class_entry *ce0;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 3, 0, &dependency_injector, &model_name, &data);
	
	if (Z_TYPE_P(data) != IS_ARRAY) { 
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Input data must be an Array");
		return;
	}

	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A dependency injector container is required to obtain the ORM services");
		return;
	}
	
	object_init_ex(return_value, phalcon_mvc_model_criteria_ce);

	if (zend_hash_num_elements(Z_ARRVAL_P(data))) {
	
		PHALCON_INIT_VAR(service);
		PHALCON_ZVAL_MAYBE_INTERNED_STRING(service, phalcon_interned_modelsMetadata);
	
		PHALCON_CALL_METHOD(&meta_data, dependency_injector, "getshared", service);
		PHALCON_VERIFY_INTERFACE(meta_data, phalcon_mvc_model_metadatainterface_ce);
		ce0 = phalcon_fetch_class(model_name TSRMLS_CC);
	
		PHALCON_INIT_VAR(model);
		object_init_ex(model, ce0);
		if (phalcon_has_constructor(model TSRMLS_CC)) {
			PHALCON_CALL_METHOD(NULL, model, "__construct");
		}

		PHALCON_VERIFY_INTERFACE_EX(model, phalcon_mvc_modelinterface_ce, phalcon_mvc_model_exception_ce, 1);

		if (PHALCON_GLOBAL(orm).column_renaming) {
			PHALCON_CALL_METHOD(&column_map, meta_data, "getreversecolumnmap", model);
			if (Z_TYPE_P(column_map) != IS_ARRAY) {
				PHALCON_INIT_NVAR(column_map);
			}
		}
		else {
			column_map = PHALCON_GLOBAL(z_null);
		}

		PHALCON_CALL_METHOD(&data_types, meta_data, "getdatatypes", model);
	
		PHALCON_INIT_VAR(bind);
		array_init(bind);
	
		PHALCON_INIT_VAR(conditions);
		array_init(conditions);

		/** 
		 * We look for attributes in the array passed as data
		 */
		phalcon_is_iterable(data, &ah0, &hp0, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
			zval *real_field;
	
			PHALCON_GET_HKEY(field, ah0, hp0);
			PHALCON_GET_HVALUE(value);

			if (Z_TYPE_P(column_map) != IS_ARRAY || !phalcon_array_isset_fetch(&real_field, column_map, field)) {
				real_field = field;
			}
	
			if (phalcon_array_isset_fetch(&type, data_types, real_field)) {
				if (Z_TYPE_P(value) != IS_NULL && !PHALCON_IS_STRING(value, "")) {
					if (PHALCON_IS_LONG(type, 2)) {
						/**
						 * For varchar types we use LIKE operator
						 */
						PHALCON_INIT_NVAR(condition);
						PHALCON_CONCAT_VSVS(condition, field, " LIKE :", field, ":");

						PHALCON_INIT_NVAR(value_pattern);
						PHALCON_CONCAT_SVS(value_pattern, "%", value, "%");
						phalcon_array_update_zval(&bind, field, value_pattern, PH_COPY);
					} else {
						/**
						 * For the rest of data types we use a plain = operator
						 */
						PHALCON_INIT_NVAR(condition);
						PHALCON_CONCAT_VSVS(condition, field, "=:", field, ":");
						phalcon_array_update_zval(&bind, field, value, PH_COPY);
					}

					phalcon_array_append(&conditions, condition, 0);
				}
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
		if (zend_hash_num_elements(Z_ARRVAL_P(conditions))) {
			PHALCON_INIT_VAR(join_conditions);
			phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC);
			PHALCON_CALL_METHOD(NULL, return_value, "where", join_conditions, bind);
		}
	}
	
	PHALCON_CALL_METHOD(NULL, return_value, "setmodelname", model_name);
	RETURN_MM();
}