<commit_before><?php
namespace FW\User;
use \Core;

class Registration {
	public $error = [];

	function registByField($data = []):bool {
		if(empty($data['email']) || !filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
			$this->error['email'] = 'Корректный e-mail является обязательным для регистрации';
			return false;
		}

		if(isset($data['password'])) {
			if(mb_strlen($data['password']) < 7) {
				$this->error['password'] = 'Вы ввели короткий пароль. Минимум 7 символов';
				return false;
			}

			$data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);
		}

		$res = q("
			SELECT 1
			FROM `fw_users`
			WHERE `email` = '".\es($data['email'])."'
		");
		if($res->num_rows) {
			$this->error['email'] = 'E-mail занят';
			return false;
		}

		$insert = [];
		foreach($data as $k=>$v) {
			$insert[] = "`".\es($k)."` = '".\es($v)."'";
		}

		q("
			INSERT INTO `fw_users` SET
			".implode(',',$insert)."
		");
		$id = \DB::_()->insert_id;
		$hash = md5($id.microtime(true).rand(1,1000000).(isset($data['password']) ?? time()));

		q("
			UPDATE `fw_users` SET
			`hash` = '".es($hash)."'
			WHERE `id` = ".$id."
		");

		return $this->sendActivate($id,$hash,$data['email']);
	}

	function edit(array $data, int $id):bool {
		if(isset($data['id'])) {
			$this->error['form'] = 'Нельзя изменять ID';
			return false;
		}

		if(!empty($data['password'])) {
			if(mb_strlen($data['password']) < 7) {
				$this->error['password'] = 'В<PASSWORD>ели к<PASSWORD>. Минимум 7 символов';
				return false;
			}

			$data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);
		}

		if(isset($data['email'])) {
			if(!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
				$this->error['email'] = 'Корректный e-mail является обязательным для регистрации';
				return false;
			}

			$res = q("
				SELECT 1
				FROM `fw_users`
				WHERE `email` = '".\es($data['email'])."'<commit_msg>Правки<commit_after><?php
namespace FW\User;
use \Core;

class Registration {
	public $error = [];

	function registByField($data = []):bool {
		if(empty($data['email']) || !filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
			$this->error['email'] = 'Корректный e-mail является обязательным для регистрации';
			return false;
		}

		if(isset($data['password'])) {
			if(mb_strlen($data['password']) < 7) {
				$this->error['password'] = '<PASSWORD>ов';
				return false;
			}

			$data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);
		}

		$res = q("
			SELECT 1
			FROM `fw_users`
			WHERE `email` = '".\es($data['email'])."'
		");
		if($res->num_rows) {
			$this->error['email'] = 'E-mail занят';
			return false;
		}

		$insert = [];
		foreach($data as $k=>$v) {
			$insert[] = "`".\es($k)."` = '".\es($v)."'";
		}

		q("
			INSERT INTO `fw_users` SET
			".implode(',',$insert)."
		");
		$id = \DB::_()->insert_id;
		$hash = md5($id.microtime(true).rand(1,1000000).(isset($data['password']) ?? time()));

		q("
			UPDATE `fw_users` SET
			`hash` = '".es($hash)."'
			WHERE `id` = ".$id."
		");

		return $this->sendActivate($id,$hash,$data['email']);
	}

	function edit(array $data, int $id):bool {
		if(isset($data['id'])) {
			$this->error['form'] = 'Нельзя изменять ID';
			return false;
		}

		if(isset($data['password'])) {
			if(!empty($data['password'])) {
				if(mb_strlen($data['password']) < 7) {
					$this->error['password'] = '<PASSWORD>';

					return false;
				}

				$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
			} else {
				unset($data['password']);
			}
		}

		if(isset($data['email'])) {
			if(!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
				$this->error['email'] = 'Корректный e-mail является обязательным для регистрации';
				return false;
			}

			$res = q("
				SELECT 1
				FROM `fw_users`
				WHERE `email` = '".\es($data['email'])."'
{'fraction_non_alphanumeric': 0.23417556655379004, 'fraction_numerical': 0.0072935660328210476, 'mean_word_length': 3.81203007518797, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, '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': '5247085', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1467, 'n_words': 251}