
#ifdef HAVE_CONFIG_H
#include "../../../../ext_config.h"
#endif

#include <php.h>
#include "../../../../php_ext.h"
#include "../../../../ext.h"

#include <Zend/zend_operators.h>
#include <Zend/zend_exceptions.h>
#include <Zend/zend_interfaces.h>

#include "kernel/main.h"
#include "kernel/memory.h"
#include "kernel/fcall.h"
#include "kernel/operators.h"
#include "kernel/object.h"
#include "kernel/exception.h"
#include "kernel/concat.h"


/**
 * This file is part of the Phalcon Framework.
 *
 * (c) Phalcon Team <team@phalcon.io>
 *
 * For the full copyright and license information, please view the LICENSE.txt
 * file that was distributed with this source code.
 *
 * Implementation of this file has been influenced by sinbadxiii/cphalcon-auth
 * @link    https://github.com/sinbadxiii/cphalcon-auth
 */
/**
 * Configuration for the Session guard. Holds the names under which the
 * session key and remember-me cookie are stored. Defaults to 'auth' and
 * 'remember'; multi-guard apps can pass a $suffix ('web', 'admin', ...)
 * to derive 'auth_web' / 'remember_web' style names, or override either
 * full name explicitly.
 */
ZEPHIR_INIT_CLASS(Phalcon_Auth_Guard_Config_SessionGuardConfig)
{
	ZEPHIR_REGISTER_CLASS_EX(Phalcon\\Auth\\Guard\\Config, SessionGuardConfig, phalcon, auth_guard_config_sessionguardconfig, phalcon_auth_guard_config_abstractguardconfig_ce, phalcon_auth_guard_config_sessionguardconfig_method_entry, 0);

	{
		zval _zc0;
		ZVAL_UNDEF(&_zc0);
		zephir_declare_typed_property(phalcon_auth_guard_config_sessionguardconfig_ce, SL("name"), &_zc0, ZEND_ACC_PRIVATE, MAY_BE_STRING, NULL, 0);
	}

	{
		zval _zc0;
		ZVAL_UNDEF(&_zc0);
		zephir_declare_typed_property(phalcon_auth_guard_config_sessionguardconfig_ce, SL("rememberName"), &_zc0, ZEND_ACC_PRIVATE, MAY_BE_STRING, NULL, 0);
	}

	{
		zval _zc0;
		ZVAL_UNDEF(&_zc0);
		zephir_declare_typed_property(phalcon_auth_guard_config_sessionguardconfig_ce, SL("rememberSecure"), &_zc0, ZEND_ACC_PRIVATE, MAY_BE_BOOL, NULL, 0);
	}

	{
		zval _zc0;
		ZVAL_UNDEF(&_zc0);
		zephir_declare_typed_property(phalcon_auth_guard_config_sessionguardconfig_ce, SL("rememberTtl"), &_zc0, ZEND_ACC_PRIVATE, MAY_BE_LONG, NULL, 0);
	}

	/**
	 * Default remember-me cookie lifetime, in seconds (365 days).
	 *
	 * @var int
	 */
	zephir_declare_class_constant_long(phalcon_auth_guard_config_sessionguardconfig_ce, SL("DEFAULT_REMEMBER_TTL"), 31536000);

	return SUCCESS;
}

/**
 * @throws Exception
 */
PHP_METHOD(Phalcon_Auth_Guard_Config_SessionGuardConfig, __construct)
{
	zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
	zend_bool rememberSecure;
	zend_long rememberTtl, ZEPHIR_LAST_CALL_STATUS;
	zval suffix_zv, name_zv, rememberName_zv, *rememberTtl_param = NULL, *rememberSecure_param = NULL, __$true, __$false, _0, _1, _2, _3, _4, _5, _6$$3;
	zend_string *suffix = NULL, *name = NULL, *rememberName = NULL;
	zval *this_ptr = getThis();

	ZVAL_UNDEF(&suffix_zv);
	ZVAL_UNDEF(&name_zv);
	ZVAL_UNDEF(&rememberName_zv);
	ZVAL_BOOL(&__$true, 1);
	ZVAL_BOOL(&__$false, 0);
	ZVAL_UNDEF(&_0);
	ZVAL_UNDEF(&_1);
	ZVAL_UNDEF(&_2);
	ZVAL_UNDEF(&_3);
	ZVAL_UNDEF(&_4);
	ZVAL_UNDEF(&_5);
	ZVAL_UNDEF(&_6$$3);
	static zend_string *_zephir_prop_0 = NULL;
	static zend_string *_zephir_prop_1 = NULL;
	static zend_string *_zephir_prop_2 = NULL;
	static zend_string *_zephir_prop_3 = NULL;
	if (UNEXPECTED(!_zephir_prop_0)) {
		_zephir_prop_0 = zend_string_init("name", 4, 1);
	}
	if (UNEXPECTED(!_zephir_prop_1)) {
		_zephir_prop_1 = zend_string_init("rememberName", 12, 1);
	}
	if (UNEXPECTED(!_zephir_prop_2)) {
		_zephir_prop_2 = zend_string_init("rememberTtl", 11, 1);
	}
	if (UNEXPECTED(!_zephir_prop_3)) {
		_zephir_prop_3 = zend_string_init("rememberSecure", 14, 1);
	}

	bool is_null_true = 1;
	ZEND_PARSE_PARAMETERS_START(0, 5)
		Z_PARAM_OPTIONAL
		Z_PARAM_STR_OR_NULL(suffix)
		Z_PARAM_STR_OR_NULL(name)
		Z_PARAM_STR_OR_NULL(rememberName)
		Z_PARAM_LONG_OR_NULL(rememberTtl, is_null_true)
		Z_PARAM_BOOL(rememberSecure)
	ZEND_PARSE_PARAMETERS_END();
	ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);
	zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);
	if (ZEND_NUM_ARGS() > 3) {
		rememberTtl_param = ZEND_CALL_ARG(execute_data, 4);
	}
	if (ZEND_NUM_ARGS() > 4) {
		rememberSecure_param = ZEND_CALL_ARG(execute_data, 5);
	}
	if (!suffix) {
		ZEPHIR_INIT_VAR(&suffix_zv);
	} else {
		zephir_memory_observe(&suffix_zv);
	ZVAL_STR_COPY(&suffix_zv, suffix);
	}
	if (!name) {
		ZEPHIR_INIT_VAR(&name_zv);
	} else {
		zephir_memory_observe(&name_zv);
	ZVAL_STR_COPY(&name_zv, name);
	}
	if (!rememberName) {
		ZEPHIR_INIT_VAR(&rememberName_zv);
	} else {
		zephir_memory_observe(&rememberName_zv);
	ZVAL_STR_COPY(&rememberName_zv, rememberName);
	}
	if (!rememberTtl_param) {
		rememberTtl = 0;
	} else {
		}
	if (!rememberSecure_param) {
		rememberSecure = 1;
	} else {
		}
	ZEPHIR_INIT_VAR(&_0);
	ZVAL_STRING(&_0, "suffix");
	ZEPHIR_CALL_METHOD(NULL, this_ptr, "validatenonempty", NULL, 413, &_0, &suffix_zv);
	zephir_check_call_status();
	ZEPHIR_INIT_NVAR(&_0);
	ZVAL_STRING(&_0, "name");
	ZEPHIR_CALL_METHOD(NULL, this_ptr, "validatenonempty", NULL, 413, &_0, &name_zv);
	zephir_check_call_status();
	ZEPHIR_INIT_NVAR(&_0);
	ZVAL_STRING(&_0, "rememberName");
	ZEPHIR_CALL_METHOD(NULL, this_ptr, "validatenonempty", NULL, 413, &_0, &rememberName_zv);
	zephir_check_call_status();
	ZEPHIR_INIT_VAR(&_1);
	if (Z_TYPE_P(&name_zv) != IS_NULL) {
		ZEPHIR_CPY_WRT(&_1, &name_zv);
	} else {
		ZEPHIR_INIT_NVAR(&_0);
		ZVAL_STRING(&_0, "auth");
		ZEPHIR_CALL_METHOD(&_1, this_ptr, "derive", NULL, 414, &_0, &suffix_zv);
		zephir_check_call_status();
	}
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_0, 442, &_1);
	ZEPHIR_INIT_VAR(&_2);
	if (Z_TYPE_P(&rememberName_zv) != IS_NULL) {
		ZEPHIR_CPY_WRT(&_2, &rememberName_zv);
	} else {
		ZEPHIR_INIT_NVAR(&_0);
		ZVAL_STRING(&_0, "remember");
		ZEPHIR_CALL_METHOD(&_2, this_ptr, "derive", NULL, 414, &_0, &suffix_zv);
		zephir_check_call_status();
	}
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_1, 443, &_2);
	ZEPHIR_INIT_VAR(&_3);
	if (0 != rememberTtl) {
		ZEPHIR_INIT_NVAR(&_3);
		ZVAL_LONG(&_3, rememberTtl);
	} else {
		ZEPHIR_INIT_NVAR(&_3);
		ZVAL_LONG(&_3, 31536000);
	}
	zephir_update_property_zval_cached(this_ptr, _zephir_prop_2, 444, &_3);
	if (rememberSecure) {
		zephir_update_property_zval_cached(this_ptr, _zephir_prop_3, 445, &__$true);
	} else {
		zephir_update_property_zval_cached(this_ptr, _zephir_prop_3, 445, &__$false);
	}
	zephir_read_property_cached(&_4, this_ptr, _zephir_prop_0, 442, PH_NOISY_CC | PH_READONLY);
	zephir_read_property_cached(&_5, this_ptr, _zephir_prop_1, 443, PH_NOISY_CC | PH_READONLY);
	if (ZEPHIR_IS_IDENTICAL(&_4, &_5)) {
		ZEPHIR_INIT_VAR(&_6$$3);
		object_init_ex(&_6$$3, phalcon_auth_exceptions_sessionnamesmustdiffer_ce);
		ZEPHIR_CALL_METHOD(NULL, &_6$$3, "__construct", NULL, 415);
		zephir_check_call_status();
		zephir_throw_exception_debug(&_6$$3, "phalcon/Auth/Guard/Config/SessionGuardConfig.zep", 61);
		ZEPHIR_MM_RESTORE();
		return;
	}
	ZEPHIR_MM_RESTORE();
}

PHP_METHOD(Phalcon_Auth_Guard_Config_SessionGuardConfig, getName)
{

	RETURN_MEMBER_TYPED(getThis(), "name", IS_STRING);
}

PHP_METHOD(Phalcon_Auth_Guard_Config_SessionGuardConfig, getRememberName)
{

	RETURN_MEMBER_TYPED(getThis(), "rememberName", IS_STRING);
}

/**
 * Whether the remember-me cookie carries the Secure flag. Defaults to
 * true: the cookie is a bearer credential. Set it to false only for a
 * deployment that serves plain HTTP on purpose.
 */
PHP_METHOD(Phalcon_Auth_Guard_Config_SessionGuardConfig, getRememberSecure)
{

	RETURN_MEMBER(getThis(), "rememberSecure");
}

PHP_METHOD(Phalcon_Auth_Guard_Config_SessionGuardConfig, getRememberTtl)
{

	RETURN_MEMBER_TYPED(getThis(), "rememberTtl", IS_LONG);
}

PHP_METHOD(Phalcon_Auth_Guard_Config_SessionGuardConfig, derive)
{
	zval _1;
	zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
	zval prefix_zv, suffix_zv, _0;
	zend_string *prefix = NULL, *suffix = NULL;

	ZVAL_UNDEF(&prefix_zv);
	ZVAL_UNDEF(&suffix_zv);
	ZVAL_UNDEF(&_0);
	ZVAL_UNDEF(&_1);
	bool is_null_true = 1;
	ZEND_PARSE_PARAMETERS_START(1, 2)
		Z_PARAM_STR(prefix)
		Z_PARAM_OPTIONAL
		Z_PARAM_STR_OR_NULL(suffix)
	ZEND_PARSE_PARAMETERS_END();
	ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);
	zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);
	zephir_memory_observe(&prefix_zv);
	ZVAL_STR_COPY(&prefix_zv, prefix);
	if (!suffix) {
		ZEPHIR_INIT_VAR(&suffix_zv);
	} else {
		zephir_memory_observe(&suffix_zv);
	ZVAL_STR_COPY(&suffix_zv, suffix);
	}
	ZEPHIR_INIT_VAR(&_0);
	if (ZEPHIR_IS_NULL(&suffix_zv)) {
		ZEPHIR_CPY_WRT(&_0, &prefix_zv);
	} else {
		ZEPHIR_INIT_VAR(&_1);
		ZEPHIR_CONCAT_VSV(&_1, &prefix_zv, "_", &suffix_zv);
		ZEPHIR_CPY_WRT(&_0, &_1);
	}
	RETURN_CCTOR(&_0);
}

/**
 * @throws Exception
 */
PHP_METHOD(Phalcon_Auth_Guard_Config_SessionGuardConfig, validateNonEmpty)
{
	zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
	zend_long ZEPHIR_LAST_CALL_STATUS;
	zval param_zv, value_zv, _0;
	zend_string *param = NULL, *value = NULL;

	ZVAL_UNDEF(&param_zv);
	ZVAL_UNDEF(&value_zv);
	ZVAL_UNDEF(&_0);
	bool is_null_true = 1;
	ZEND_PARSE_PARAMETERS_START(1, 2)
		Z_PARAM_STR(param)
		Z_PARAM_OPTIONAL
		Z_PARAM_STR_OR_NULL(value)
	ZEND_PARSE_PARAMETERS_END();
	ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);
	zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);
	zephir_memory_observe(&param_zv);
	ZVAL_STR_COPY(&param_zv, param);
	if (!value) {
		ZEPHIR_INIT_VAR(&value_zv);
	} else {
		zephir_memory_observe(&value_zv);
	ZVAL_STR_COPY(&value_zv, value);
	}
	ZEPHIR_INIT_VAR(&_0);
	ZVAL_STRING(&_0, "Session guard");
	ZEPHIR_CALL_CE_STATIC(NULL, phalcon_auth_exceptions_configrequiresnonemptyvalue_ce, "assert", NULL, 0, &value_zv, &_0, &param_zv);
	zephir_check_call_status();
	ZEPHIR_MM_RESTORE();
}

