Exemplo n.º 1
0
/**
 * Phalcon\Config\Adapter\Ini constructor
 *
 * @param string $filePath
 */
PHP_METHOD(Phalcon_Config_Adapter_Ini, __construct){

	zval *file_path, *process_sections, *ini_config;
	zval *exception_message, *config, *directives = NULL;
	zval *section = NULL, *value = NULL, *key = NULL, *directive_parts = NULL, *left_part = NULL;
	zval *right_part = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &file_path);
	
	PHALCON_INIT_VAR(process_sections);
	ZVAL_BOOL(process_sections, 1);
	
	/** 
	 * Use the standard parse_ini_file
	 */
	PHALCON_INIT_VAR(ini_config);
	phalcon_call_func_p2(ini_config, "parse_ini_file", file_path, process_sections);
	
	/** 
	 * Check if the file had errors
	 */
	if (PHALCON_IS_FALSE(ini_config)) {
		PHALCON_INIT_VAR(exception_message);
		PHALCON_CONCAT_SVS(exception_message, "Configuration file ", file_path, " can't be loaded");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_config_exception_ce, exception_message);
		return;
	}
	
	PHALCON_INIT_VAR(config);
	array_init(config);
	
	phalcon_is_iterable(ini_config, &ah0, &hp0, 0, 0);
	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HKEY(section, ah0, hp0);
		PHALCON_GET_HVALUE(directives);
	
		if (unlikely(Z_TYPE_P(directives) != IS_ARRAY)) {
			Z_ADDREF_P(directives);
			if (phalcon_array_update_zval(&config, section, &directives, 0 TSRMLS_CC) != SUCCESS) {
				Z_DELREF_P(directives);
			}
			zend_hash_move_forward_ex(ah0, &hp0);
			continue;
	}
	
		phalcon_is_iterable(directives, &ah1, &hp1, 0, 0);
	
		if (zend_hash_num_elements(ah1) == 0) {
			phalcon_array_update_zval(&config, section, &directives, 0 TSRMLS_CC);
			zend_hash_move_forward_ex(ah0, &hp0);
			continue;
		}
	
		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
			PHALCON_GET_HKEY(key, ah1, hp1);
			PHALCON_GET_HVALUE(value);
	
			if (phalcon_memnstr_str(key, SL(".") TSRMLS_CC)) {
				PHALCON_INIT_NVAR(directive_parts);
				phalcon_fast_explode_str(directive_parts, SL("."), key TSRMLS_CC);
	
				PHALCON_OBS_NVAR(left_part);
				phalcon_array_fetch_long(&left_part, directive_parts, 0, PH_NOISY_CC);
	
				PHALCON_OBS_NVAR(right_part);
				phalcon_array_fetch_long(&right_part, directive_parts, 1, PH_NOISY_CC);
				phalcon_array_update_zval_zval_zval_multi_3(&config, section, left_part, right_part, &value, 0 TSRMLS_CC);
			} else {
				phalcon_array_update_multi_2(&config, section, key, &value, 0 TSRMLS_CC);
			}
	
			zend_hash_move_forward_ex(ah1, &hp1);
		}
	
		zend_hash_move_forward_ex(ah0, &hp0);
	}
	
	/** 
	 * Calls the Phalcon\Config constructor
	 */
	PHALCON_CALL_PARENT_PARAMS_1_NORETURN(this_ptr, "Phalcon\\Config\\Adapter\\Ini", "__construct", config);
	
	PHALCON_MM_RESTORE();
}
Exemplo n.º 2
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, *access_name = NULL;
	zval *empty_arr = NULL, *_access = NULL;
	zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL, *r5 = NULL, *r6 = NULL;
	zval *r7 = NULL;
	zval *t0 = NULL, *t1 = NULL, *t2 = NULL, *t3 = NULL, *t4 = NULL, *t5 = NULL, *t6 = NULL;
	zval *t7 = NULL, *t8 = NULL, *t9 = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;

	PHALCON_MM_GROW();

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzzz", &role_name, &resource_name, &access, &action) == 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 ACL");
		PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
		return;
	}
	
	PHALCON_OBS_VAR(resources_names);
	phalcon_read_property(&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(&default_access, this_ptr, SL("_defaultAccess"), PH_NOISY_CC);
	if (Z_TYPE_P(access) == IS_ARRAY) { 
	
		PHALCON_OBS_VAR(access_list);
		phalcon_read_property(&access_list, this_ptr, SL("_accessList"), PH_NOISY_CC);
	
		if (!phalcon_is_iterable(access, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_FOREACH_VALUE(access_name);
	
			PHALCON_OBS_NVAR(r0);
			phalcon_array_fetch(&r0, access_list, resource_name, PH_NOISY_CC);
			if (!phalcon_array_isset(r0, access_name)) {
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVSVS(exception_message, "Acccess '", access_name, "' does not exist in resource '", resource_name, "' in ACL");
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
				return;
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
	
		if (!phalcon_is_iterable(access, &ah1, &hp1, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
			PHALCON_GET_FOREACH_VALUE(access_name);
	
			PHALCON_OBS_NVAR(t0);
			phalcon_read_property(&t0, this_ptr, SL("_access"), PH_NOISY_CC);
			PHALCON_OBS_NVAR(r1);
			phalcon_array_fetch(&r1, t0, role_name, PH_NOISY_CC);
			if (!phalcon_array_isset(r1, resource_name)) {
				PHALCON_INIT_NVAR(empty_arr);
				array_init(empty_arr);
	
				PHALCON_OBS_NVAR(_access);
				phalcon_read_property(&_access, this_ptr, SL("_access"), PH_NOISY_CC);
				phalcon_array_update_multi_2(&_access, role_name, resource_name, &empty_arr, 0 TSRMLS_CC);
				phalcon_update_property_zval(this_ptr, SL("_access"), _access TSRMLS_CC);
			}
	
			PHALCON_OBS_NVAR(t1);
			phalcon_read_property(&t1, this_ptr, SL("_access"), PH_NOISY_CC);
			phalcon_array_update_zval_zval_zval_multi_3(&t1, role_name, resource_name, access_name, &action, 0 TSRMLS_CC);
	
			PHALCON_OBS_NVAR(t2);
			phalcon_read_property(&t2, this_ptr, SL("_access"), PH_NOISY_CC);
	
			PHALCON_OBS_NVAR(r2);
			phalcon_array_fetch(&r2, t2, role_name, PH_NOISY_CC);
	
			PHALCON_OBS_NVAR(r3);
			phalcon_array_fetch(&r3, r2, resource_name, PH_NOISY_CC);
			if (!phalcon_array_isset_string(r3, SS("*"))) {
				PHALCON_OBS_NVAR(t3);
				phalcon_read_property(&t3, this_ptr, SL("_access"), PH_NOISY_CC);
				phalcon_array_update_string_zval_zval_multi_3(&t3, role_name, resource_name, SL("*"), &default_access, 0 TSRMLS_CC);
			}
	
			zend_hash_move_forward_ex(ah1, &hp1);
		}
	
	} else {
		if (!PHALCON_IS_STRING(access, "*")) {
	
			PHALCON_OBS_VAR(t4);
			phalcon_read_property(&t4, this_ptr, SL("_accessList"), PH_NOISY_CC);
			PHALCON_OBS_VAR(r4);
			phalcon_array_fetch(&r4, t4, resource_name, PH_NOISY_CC);
			if (!phalcon_array_isset(r4, access)) {
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVSVS(exception_message, "Acccess '", access, "' does not exist in resource '", resource_name, "' in ACL");
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
				return;
			}
		}
	
		PHALCON_OBS_VAR(t5);
		phalcon_read_property(&t5, this_ptr, SL("_access"), PH_NOISY_CC);
	
		PHALCON_OBS_VAR(r5);
		phalcon_array_fetch(&r5, t5, role_name, PH_NOISY_CC);
		if (!phalcon_array_isset(r5, resource_name)) {
			PHALCON_INIT_NVAR(empty_arr);
			array_init(empty_arr);
	
			PHALCON_OBS_VAR(t6);
			phalcon_read_property(&t6, this_ptr, SL("_access"), PH_NOISY_CC);
			phalcon_array_update_multi_2(&t6, role_name, resource_name, &empty_arr, 0 TSRMLS_CC);
			phalcon_update_property_zval(this_ptr, SL("_access"), t6 TSRMLS_CC);
		}
	
		PHALCON_OBS_VAR(t7);
		phalcon_read_property(&t7, this_ptr, SL("_access"), PH_NOISY_CC);
	
		PHALCON_OBS_VAR(r6);
		phalcon_array_fetch(&r6, t7, role_name, PH_NOISY_CC);
	
		PHALCON_OBS_VAR(r7);
		phalcon_array_fetch(&r7, r6, resource_name, PH_NOISY_CC);
		if (!phalcon_array_isset_string(r7, SS("*"))) {
			PHALCON_OBS_VAR(t8);
			phalcon_read_property(&t8, this_ptr, SL("_access"), PH_NOISY_CC);
			phalcon_array_update_string_zval_zval_multi_3(&t8, role_name, resource_name, SL("*"), &default_access, 0 TSRMLS_CC);
		}
	
		PHALCON_OBS_VAR(t9);
		phalcon_read_property(&t9, this_ptr, SL("_access"), PH_NOISY_CC);
		phalcon_array_update_zval_zval_zval_multi_3(&t9, role_name, resource_name, access, &action, 0 TSRMLS_CC);
	}
	
	PHALCON_CALL_METHOD_NORETURN(this_ptr, "_rebuildaccesslist");
	
	PHALCON_MM_RESTORE();
}
Exemplo n.º 3
0
/**
 * Rebuild the list of access from the inherit lists
 *
 */
PHP_METHOD(Phalcon_Acl_Adapter_Memory, _rebuildAccessList){

	zval *roles, *number_roles, *pow_roles, *two, *middle_roles;
	zval *middle, *roles_names, *roles_inherits;
	zval *changed = NULL, *i, *internal_access = NULL, *one = NULL, *role_name = NULL;
	zval *role_inherit = NULL, *inherit_internal = NULL, *access = NULL;
	zval *resource_name = NULL, *value = NULL, *name = NULL;
	zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL;
	HashTable *ah0, *ah1, *ah2, *ah3;
	HashPosition hp0, hp1, hp2, hp3;
	zval **hd;

	PHALCON_MM_GROW();

	PHALCON_OBS_VAR(roles);
	phalcon_read_property(&roles, this_ptr, SL("_roles"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(number_roles);
	phalcon_fast_count(number_roles, roles TSRMLS_CC);
	
	PHALCON_INIT_VAR(pow_roles);
	Z_LVAL_P(pow_roles) = Z_LVAL_P(number_roles) * Z_LVAL_P(number_roles);
	
	PHALCON_INIT_VAR(two);
	ZVAL_LONG(two, 2);
	
	PHALCON_INIT_VAR(middle_roles);
	div_function(middle_roles, pow_roles, two TSRMLS_CC);
	
	PHALCON_INIT_VAR(middle);
	PHALCON_CALL_FUNC_PARAMS_1(middle, "ceil", middle_roles);
	
	PHALCON_OBS_VAR(roles_names);
	phalcon_read_property(&roles_names, this_ptr, SL("_rolesNames"), PH_NOISY_CC);
	
	PHALCON_OBS_VAR(roles_inherits);
	phalcon_read_property(&roles_inherits, this_ptr, SL("_roleInherits"), PH_NOISY_CC);
	
	PHALCON_INIT_VAR(changed);
	ZVAL_BOOL(changed, 1);
	
	PHALCON_INIT_VAR(i);
	ZVAL_LONG(i, 0);
	while (1) {
	
		PHALCON_INIT_NVAR(r0);
		is_smaller_or_equal_function(r0, i, middle TSRMLS_CC);
		if (!zend_is_true(r0)) {
			break;
		}
		PHALCON_OBS_NVAR(internal_access);
		phalcon_read_property(&internal_access, this_ptr, SL("_access"), PH_NOISY_CC);
	
		if (!phalcon_is_iterable(roles_names, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
			return;
		}
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_FOREACH_KEY(role_name, ah0, hp0);
			PHALCON_GET_FOREACH_VALUE(one);
	
			if (phalcon_array_isset(roles_inherits, role_name)) {
	
				PHALCON_OBS_NVAR(r1);
				phalcon_array_fetch(&r1, roles_inherits, role_name, PH_NOISY_CC);
	
				if (!phalcon_is_iterable(r1, &ah1, &hp1, 0, 0 TSRMLS_CC)) {
					return;
				}
	
				while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
					PHALCON_GET_FOREACH_VALUE(role_inherit);
	
					if (phalcon_array_isset(internal_access, role_inherit)) {
	
						PHALCON_OBS_NVAR(inherit_internal);
						phalcon_array_fetch(&inherit_internal, internal_access, role_inherit, PH_NOISY_CC);
	
						if (!phalcon_is_iterable(inherit_internal, &ah2, &hp2, 0, 0 TSRMLS_CC)) {
							return;
						}
	
						while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) {
	
							PHALCON_GET_FOREACH_KEY(resource_name, ah2, hp2);
							PHALCON_GET_FOREACH_VALUE(access);
	
	
							if (!phalcon_is_iterable(access, &ah3, &hp3, 0, 0 TSRMLS_CC)) {
								return;
							}
	
							while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) {
	
								PHALCON_GET_FOREACH_KEY(name, ah3, hp3);
								PHALCON_GET_FOREACH_VALUE(value);
	
								if (phalcon_array_isset(internal_access, role_name)) {
	
									PHALCON_OBS_NVAR(r2);
									phalcon_array_fetch(&r2, internal_access, role_name, PH_NOISY_CC);
									if (phalcon_array_isset(r2, resource_name)) {
	
										PHALCON_OBS_NVAR(r3);
										phalcon_array_fetch(&r3, internal_access, role_name, PH_NOISY_CC);
										PHALCON_OBS_NVAR(r4);
										phalcon_array_fetch(&r4, r3, resource_name, PH_NOISY_CC);
										if (phalcon_array_isset(r4, name)) {
											zend_hash_move_forward_ex(ah3, &hp3);
											continue;
										}
									}
								}
								phalcon_array_update_zval_zval_zval_multi_3(&internal_access, role_name, resource_name, name, &value, 0 TSRMLS_CC);
	
								PHALCON_INIT_NVAR(changed);
								ZVAL_BOOL(changed, 1);
	
								zend_hash_move_forward_ex(ah3, &hp3);
							}
	
	
							zend_hash_move_forward_ex(ah2, &hp2);
						}
	
					}
	
					zend_hash_move_forward_ex(ah1, &hp1);
				}
	
			}
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
		if (zend_is_true(changed)) {
			phalcon_update_property_zval(this_ptr, SL("_access"), internal_access TSRMLS_CC);
		}
		PHALCON_SEPARATE(i);
		increment_function(i);
	}
	
	PHALCON_MM_RESTORE();
}
Exemplo n.º 4
0
/**
 * Phalcon\Config\Adapter\Ini constructor
 *
 * @param string $filePath
 */
PHP_METHOD(Phalcon_Config_Adapter_Ini, __construct) {

    zval *file_path, *config, *process_sections;
    zval *ini_config, *base_path, *exception_message;
    zval *dot, *directives = NULL, *section = NULL, *value = NULL, *key = NULL, *directive_parts = NULL;
    zval *left_part = NULL, *right_part = NULL;
    HashTable *ah0, *ah1;
    HashPosition hp0, hp1;
    zval **hd;

    PHALCON_MM_GROW();

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

    PHALCON_INIT_VAR(config);
    array_init(config);

    PHALCON_INIT_VAR(process_sections);
    ZVAL_BOOL(process_sections, 1);

    PHALCON_INIT_VAR(ini_config);
    PHALCON_CALL_FUNC_PARAMS_2(ini_config, "parse_ini_file", file_path, process_sections);
    if (PHALCON_IS_FALSE(ini_config)) {
        PHALCON_INIT_VAR(base_path);
        PHALCON_CALL_FUNC_PARAMS_1(base_path, "basename", file_path);

        PHALCON_INIT_VAR(exception_message);
        PHALCON_CONCAT_SVS(exception_message, "Configuration file ", base_path, " can't be loaded");
        PHALCON_THROW_EXCEPTION_ZVAL(phalcon_config_exception_ce, exception_message);
        return;
    }

    PHALCON_INIT_VAR(dot);
    ZVAL_STRING(dot, ".", 1);

    if (!phalcon_is_iterable(ini_config, &ah0, &hp0, 0, 0 TSRMLS_CC)) {
        return;
    }

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

        PHALCON_GET_FOREACH_KEY(section, ah0, hp0);
        PHALCON_GET_FOREACH_VALUE(directives);


        if (!phalcon_is_iterable(directives, &ah1, &hp1, 0, 0 TSRMLS_CC)) {
            return;
        }

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

            PHALCON_GET_FOREACH_KEY(key, ah1, hp1);
            PHALCON_GET_FOREACH_VALUE(value);

            if (phalcon_memnstr_str(key, SL(".") TSRMLS_CC)) {
                PHALCON_INIT_NVAR(directive_parts);
                phalcon_fast_explode(directive_parts, dot, key TSRMLS_CC);

                PHALCON_OBS_NVAR(left_part);
                phalcon_array_fetch_long(&left_part, directive_parts, 0, PH_NOISY_CC);

                PHALCON_OBS_NVAR(right_part);
                phalcon_array_fetch_long(&right_part, directive_parts, 1, PH_NOISY_CC);
                phalcon_array_update_zval_zval_zval_multi_3(&config, section, left_part, right_part, &value, 0 TSRMLS_CC);
            } else {
                phalcon_array_update_multi_2(&config, section, key, &value, 0 TSRMLS_CC);
            }

            zend_hash_move_forward_ex(ah1, &hp1);
        }


        zend_hash_move_forward_ex(ah0, &hp0);
    }

    PHALCON_CALL_PARENT_PARAMS_1_NORETURN(this_ptr, "Phalcon\\Config\\Adapter\\Ini", "__construct", config);

    PHALCON_MM_RESTORE();
}