<commit_before><?php

namespace Lorry\Service;

use Lorry\ModelFactory;
use Lorry\Model\User;
use Lorry\Exception\FileNotFoundException;
use InvalidArgumentException;
use Exception;

class SessionService {

	/**
	 *
	 * @var \Lorry\Service\Config
	 */
	protected $config;

	public function setConfigService(ConfigService $config) {
		$this->config = $config;
	}

	protected $started = false;
	protected $user = false;

	public function __construct() {
		session_name('lorry_session');
	}

	/**
	 * 
	 * @param \Lorry\Model\User $user
	 * @param bool $remember
	 * @param bool $identify
	 * @throws Exception
	 */
	public final function start(User $user, $remember = false, $identify = false) {
		$this->ensureSession();
		if(!$user->isLoaded()) {
			throw new InvalidArgumentException('user is not loaded or does not exist');
		}
		$this->authenticate($user);
		if($identify) {
			$this->identify();
		}
		if($remember === true) {
			$this->remember();
		}
	}

	public final function refresh() {
		$this->ensureSession();
		$this->ensureUser();
		$this->ensureSecret($this->user);
		session_regenerate_id(true);
		$_SESSION['secret'] = $this->user->getSecret();
	}

	/**
	 * 
	 * @param \Lorry\Model\User $user
	 */
	protected final function authenticate(User $user) {
		$this->ensureSession();
		$this->ensureSecret($user);
		session_regenerate_id(true);
		$this->user = $user;
		$_SESSION['user'] = $user->getId();
		$_SESSION['secret'] = $user->getSecret();
		$_SESSION['identified'] = false; // whether the user has personally identifed via password, as opposed to login cookie
		$this->setFlag('knows_clonk');
	}

	public final function identify() {
		$this->ensureSession();
		$_SESSION['identified'] = true;
	}

	/**
	 * 
	 * @return bool
	 */
	public final function identified() {
		$this->ensureUser();
		return $this->user->hasPassword() && isset($_SESSION['identified']) && $_SESSION['identified'] === true;
	}

	public final function remember() {
		$this->ensureUser();
		$this->ensureSecret($this->user);
		$secret = $this->user->getSecret();
		setcookie('lorry_login', '$'.$this->user->getId().'$'.$secret, time() + 60 * 60 * 24 * 365, '/');
	}

	/**
	 * 
	 * @return bool
	 */
	public final function authenticated() {
		if(!isset($_COOKIE['lorry_session']) && !isset($_COOKIE['lorry_login']))<commit_msg>make knows_clonk persistent when picking up a remembered user login<commit_after><?php

namespace Lorry\Service;

use Lorry\ModelFactory;
use Lorry\Model\User;
use Lorry\Exception\FileNotFoundException;
use InvalidArgumentException;
use Exception;

class SessionService {

	/**
	 *
	 * @var \Lorry\Service\Config
	 */
	protected $config;

	public function setConfigService(ConfigService $config) {
		$this->config = $config;
	}

	protected $started = false;
	protected $user = false;

	public function __construct() {
		session_name('lorry_session');
	}

	/**
	 * 
	 * @param \Lorry\Model\User $user
	 * @param bool $remember
	 * @param bool $identify
	 * @throws Exception
	 */
	public final function start(User $user, $remember = false, $identify = false) {
		$this->ensureSession();
		if(!$user->isLoaded()) {
			throw new InvalidArgumentException('user is not loaded or does not exist');
		}
		$this->authenticate($user);
		if($identify) {
			$this->identify();
		}
		if($remember === true) {
			$this->remember();
		}
	}

	public final function refresh() {
		$this->ensureSession();
		$this->ensureUser();
		$this->ensureSecret($this->user);
		session_regenerate_id(true);
		$_SESSION['secret'] = $this->user->getSecret();
	}

	/**
	 * 
	 * @param \Lorry\Model\User $user
	 */
	protected final function authenticate(User $user) {
		$this->ensureSession();
		$this->ensureSecret($user);
		session_regenerate_id(true);
		$this->user = $user;
		$_SESSION['user'] = $user->getId();
		$_SESSION['secret'] = $user->getSecret();
		$_SESSION['identified'] = false; // whether the user has personally identifed via password, as opposed to login cookie
		$this->setFlag('knows_clonk', true);
	}

	public final function identify() {
		$this->ensureSession();
		$_SESSION['identified'] = true;
	}

	/**
	 * 
	 * @return bool
	 */
	public final function identified() {
		$this->ensureUser();
		return $this->user->hasPassword() && isset($_SESSION['identified']) && $_SESSION['identified'] === true;
	}

	public final function remember() {
		$this->ensureUser();
		$this->ensureSecret($this->user);
		$secret = $this->user->getSecret();
		setcookie('lorry_login', '$'.$this->user->getId().'$'.$secret, time() + 60 * 60 * 24 * 365, '/');
	}

	/**
	 * 
	 * @return bool
	 */
	public final function authenticated() {
		if(!isset($_COOKIE['lorry_session']) && !isset($_COOKIE['lorry_login']))
{'fraction_non_alphanumeric': 0.17901234567901234, 'fraction_numerical': 0.0038314176245210726, 'mean_word_length': 4.285714285714286, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '946791', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1481, 'n_words': 325}