<commit_before><?php

namespace Lorry\Presenter\Account;

use Lorry\Presenter;
use Lorry\ModelFactory;
use Lorry\Exception\ModelValueInvalidException;

class Register extends Presenter {

	public function get() {
		if($this->session->authenticated()) {
			return $this->redirect('/');
		}

		if(isset($_GET['oauth'])) {
			return $this->redirect($this->session->handleOauth());
		}

		if(isset($_GET['returnto'])) {
			$this->context['returnto'] = filter_input(INPUT_GET, 'returnto');
		}

		if(isset($_GET['cancel'])) {
			unset($_SESSION['register_oauth']);
		}

		$this->context['oauth'] = false;
		if(isset($_SESSION['register_oauth'])) {
			$register = $_SESSION['register_oauth'];

			if($register['username'] && !isset($this->context['username'])) {
				$this->context['username'] = $register['username'];
			}
			if($register['email'] && !isset($this->context['email'])) {
				$this->context['email'] = $register['email'];
			}
			$this->context['provider'] = $register['provider'];

			$this->context['oauth'] = true;
		}

		if(!$this->config->get('enable/registration')) {
			$this->context['registration_disabled'] = true;
		}

		$this->display('account/register.twig');
	}

	public function post() {
		if(!$this->config->get('enable/registration')) {
			return $this->get();
		}

		$username = filter_input(INPUT_POST, 'username');
		$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
		$password = filter_input(INPUT_POST, 'password');
		$password_repeat = filter_input(INPUT_POST, '<PASSWORD>');

		$this->context['username'] = $username;
		$this->context['email'] = $email;

		$errors = array();

		$oauth = false;
		if(isset($_SESSION['register_oauth']) && filter_input(INPUT_POST, 'use-oauth')) {
			$oauth = $_SESSION['register_oauth'];
		}

		$user = $this->persistence->build('User');

		if($this->persistence->build('User')->byUsername($username)) {
			$errors[] = gettext('Username already taken.');
		} else {
			try {
				$user->setUsername($username);
			} catch(ModelValueInvalidException $e) {
				$errors[] = sprintf(gettext('Username is %s.'), $e->getMessage());
			}
		}

		if($email && $this->persistence->build('User')->byEmail($email)) {
			$errors[] = sprintf(gettext('Email address is already in use.'));
		} else {
			try {
				$user->setEmail($email);
			} catch(ModelValueInvalidException $e) {
				$errors[] = sprintf(gettext('Email address is %s.'), gettext('invalid'));
			}
		}

		if(!$oauth) {
			if($password !== $password_<PASSWORD>) {
				$errors[] = gettext('Passwords do not match.');
			} else {
				try {
					$user->setPassword($password);
				} catch(ModelValueInvalidException $e) {
					$errors[] = sprintf(gettext('Password is %s.'), $e->getMessage());
				}
			}
		} else {
			$user->setOauth(strtolower($oauth['provider']), $oauth['uid']);
		}

		$user->setLanguage($this->localisation->getDisplayLanguage());

		if(empty($errors)) {
			$user->setRegistration(time());
			if($user->save()) {
				$this->notice('creating user "'.$user->getUsername().'"');
				try {
					$this->job->submit('Welcome', array('user' => $user->getId()));
				}
				catch(\Exception $ex) {
				}
				$this->session->setFlag('new_user', false);
				$returnto = filter_input(INPUT_GET, 'returnto');
				if($oauth) {
					$url = '/';
					if($returnto) {
						$url = $returnto;
					}
					$this->session->start($user, false, false);
					$this->redirect($url);
					return;
				}
				else {
					$url = '/login?registered='.$user->getUsername();
					if($returnto) {
						$url .= '&returnto='.$returnto;
					}
					$this->redirect($url);
					return;
				}
			} else {
				$this->error('register', gettext('Registration failed.'));
			}
		} else {
			$this->error('register', implode('<br>', $errors));
		}<commit_msg>fix invalid reference error when registering user<commit_after><?php

namespace Lorry\Presenter\Account;

use Lorry\Presenter;
use Lorry\ModelFactory;
use Lorry\Exception\ModelValueInvalidException;

class Register extends Presenter {

	public function get() {
		if($this->session->authenticated()) {
			return $this->redirect('/');
		}

		if(isset($_GET['oauth'])) {
			return $this->redirect($this->session->handleOauth());
		}

		if(isset($_GET['returnto'])) {
			$this->context['returnto'] = filter_input(INPUT_GET, 'returnto');
		}

		if(isset($_GET['cancel'])) {
			unset($_SESSION['register_oauth']);
		}

		$this->context['oauth'] = false;
		if(isset($_SESSION['register_oauth'])) {
			$register = $_SESSION['register_oauth'];

			if($register['username'] && !isset($this->context['username'])) {
				$this->context['username'] = $register['username'];
			}
			if($register['email'] && !isset($this->context['email'])) {
				$this->context['email'] = $register['email'];
			}
			$this->context['provider'] = $register['provider'];

			$this->context['oauth'] = true;
		}

		if(!$this->config->get('enable/registration')) {
			$this->context['registration_disabled'] = true;
		}

		$this->display('account/register.twig');
	}

	public function post() {
		if(!$this->config->get('enable/registration')) {
			return $this->get();
		}

		$username = filter_input(INPUT_POST, 'username');
		$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
		$password = filter_input(INPUT_POST, 'password');
		$password_repeat = filter_input(INPUT_POST, '<PASSWORD>');

		$this->context['username'] = $username;
		$this->context['email'] = $email;

		$errors = array();

		$oauth = false;
		if(isset($_SESSION['register_oauth']) && filter_input(INPUT_POST, 'use-oauth')) {
			$oauth = $_SESSION['register_oauth'];
		}

		$user = $this->persistence->build('User');

		if($this->persistence->build('User')->byUsername($username)) {
			$errors[] = gettext('Username already taken.');
		} else {
			try {
				$user->setUsername($username);
			} catch(ModelValueInvalidException $e) {
				$errors[] = sprintf(gettext('Username is %s.'), $e->getMessage());
			}
		}

		if($email && $this->persistence->build('User')->byEmail($email)) {
			$errors[] = sprintf(gettext('Email address is already in use.'));
		} else {
			try {
				$user->setEmail($email);
			} catch(ModelValueInvalidException $e) {
				$errors[] = sprintf(gettext('Email address is %s.'), gettext('invalid'));
			}
		}

		if(!$oauth) {
			if($password !== $password_repeat) {
				$errors[] = gettext('Passwords do not match.');
			} else {
				try {
					$user->setPassword($password);
				} catch(ModelValueInvalidException $e) {
					$errors[] = sprintf(gettext('Password is %s.'), $e->getMessage());
				}
			}
		} else {
			$user->setOauth(strtolower($oauth['provider']), $oauth['uid']);
		}

		$user->setLanguage($this->localisation->getDisplayLanguage());

		if(empty($errors)) {
			$user->setRegistration(time());
			if($user->save()) {
				$this->logger->notice('creating user "'.$user->getUsername().'"');
				try {
					$this->job->submit('Welcome', array('user' => $user->getId()));
				}
				catch(\Exception $ex) {
				}
				$this->session->setFlag('new_user', false);
				$returnto = filter_input(INPUT_GET, 'returnto');
				if($oauth) {
					$url = '/';
					if($returnto) {
						$url = $returnto;
					}
					$this->session->start($user, false, false);
					$this->redirect($url);
					return;
				}
				else {
					$url = '/login?registered='.$user->getUsername();
					if($returnto) {
						$url .= '&returnto='.$returnto;
					}
					$this->redirect($url);
					return;
				}
			} else {
				$this->error('register', gettext('Registration failed.'));
			}
		} else {
			$this->error('register', implode('<br>', $errors));
		}
{'fraction_non_alphanumeric': 0.21635883905013192, 'fraction_numerical': 0.0, 'mean_word_length': 4.081099195710456, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 123, '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': '3811924', 'n_tokens_mistral': 3024, 'n_tokens_neox': 2489, 'n_words': 372}