<commit_before><?php
class User_PageTool extends PageTool {
	public function go($api, $dom, $template, $tool) {
		if(empty($_COOKIE["PhpGt_Track"])) {
			$anonId = $this->generateSalt();
			$expires = strtotime("+2 weeks");
			if(setcookie("PhpGt_Track", $anonId, $expires) === false) {
				// TODO: Throw proper error. Cookie can't be set if
				// any output has been made!
				die("Can't set PhpGt_Track cookie!");
			}
			return $anonId;
		}

		return $_COOKIE["PhpGt_Track"];
	}

	public function __get($name) {
		switch($name) {
		case "id":
		case "Id":
			return $_SESSION["PhpGt_User"]["Id"];
			break;
		case "username":
		case "userName":
		case "Username":
		case "UserName":
			return $_SESSION["PhpGt_User"]["Username"];
			break;
		case "UUID":
		case "Uuid":
		case "uuid":
			return $_COOKIE["PhpGt_Login"][0];
			break;
		default:
			// TODO: Throw proper error;
			break;
		}
	}

	public function get() {
		return $_SESSION["PhpGt_User"];
	}

	public function auth($method = "Google") {
		$oid = new OpenId_Utility($method);
		$username = $oid->getData();
		if(is_null($username)) {
			$this->unAuth();
			return false;
		}

		$_SESSION["PhpGt_User.tool_AuthData"] = $username;

		$uuid = hash("sha512", $username);
		$userSalt = $this->generateSalt();
		$expires = strtotime("+2 weeks");
		setcookie(
			"PhpGt_Login[0]",
			$uuid,
			$expires);
		setcookie(
			"PhpGt_Login[1]",
			$userSalt,
			$expires);
		setcookie(
			"PhpGt_Login[2]",
			hash("sha512", $uuid . $userSalt . APPSALT),
			$expires);
		return true;
	}

	public function unAuth($forwardTo = "/") {
		unset($_SESSION["PhpGt_User.tool_AuthData"]);
		unset($_SESSION["PhpGt_User"]);
		$this->deleteCookies();
		header("Location: " . $forwardTo);
		return;
	}

	/**
	 * Checks the current session for authentication data. This may be
	 * authentication with OpenId or using a simple username/password stored
	 * in the User database table.
	 */
	public function checkAuth() {
		$userId = null;
		$username = null;
		
		// The openid_identity variable is sent by an OpenId provider on
		// successful authentication.
		if(isset($_GET["openid_identity"])) {
			$this->auth();
			if(isset($_GET["openid_return_to"])) {
				header("Location: " . $_GET["openid_return_to"]);
				exit;
			}
		}
		
		// User auth through OpenId or saved cookie is stored in the session.
		if(isset($_SESSION["PhpGt_User.tool_AuthData"])) {
			$username = $_SESSION["PhpGt_User.tool_AuthData"];
			// User has authenticated, need to match to database.
			// The database user may not exist and need creating.
			$dbUser = $this->_api["User"]->get(["Username" => $username]);
			if(is_null($dbUser) || $dbUser->hasResult) {
				// Already exists - use this user.
				$userId = $dbUser["Id"];
			}
			else {
				// Doesn't already exist - create and use new user.
				$newDbUser = $this->_api["User"]->addEmpty(array(
					"Username" => $username,
					"Uuid" => hash("sha512", $username)
				));
				$userId = $newDbUser->lastInsertId;
			}
		}
		else {
			// Cookie information:
			// Login 0 = sha512 of username (the user's UUID)
			// Login 1 = sha512 unique salt, generated for this user only
			// Login 2 = sha512 authentication hash sha(login0.login1.site_salt)
			// The site salt is stored as a named constant, defined in security
			// config.
			if(isset($_COOKIE["PhpGt_Login"])) {
				if(isset($_COOKIE["PhpGt_Login"][0])
				&& isset($_COOKIE["PhpGt_Login"][1])
				&& isset($_COOKIE["PhpGt_Login"][2])) {
					$userHash = $_COOKIE["PhpGt_Login"][0];
					$saltHash = $_COOKIE["PhpGt_Login"][1];
					$authHash = $_COOKIE["PhpGt_Login"][2];
					
					// Find the user from their UUID.
					$dbResult = $this->_api["User"]->getByUuid(array(
						"Uuid" => $userHash
					));
					if($dbResult->hasResult) {
						// Check authenticity of cookies before logging user in.
						$authHash2 = hash("sha512",
							$userHash . $saltHash . APPSALT);
						if($authHash == $authHash2) {
							// Log in.
							$dbUser = $this->_api["User"]->getByUuid(array(
								"Uuid" => $userHash
							));
							if($dbUser->hasResult) {
								// Already exists - use this user.
								$userId = $dbUser["Id"];
								$_SESSION["PhpGt_User.tool_AuthData"] = 
									$dbUser["Username"];
								$this->refreshCookies();
							}
							else {
								// Something's gone wrong... there is proper
								// cookie authentication, but no user exists
								// in database!
								// TODO: Log this error.
							}
						}
						else {
							// Failure - kill all cookies.
							$this->deleteCookies();
						}
					}
				}
				else {
					// Kill any trace of the login cookie.
					$this->deleteCookies();
				}
			}

			if(isset($_SESSION["PhpGt_User"])) {
				if(empty($_SESSION["PhpGt_User"])) {
					unset($_SESSION["PhpGt_User"]);
					return false;
				}
			}
		}

		if(!is_null($userId)) {
			$_SESSION["PhpGt_User"] = [
				"Id" => $userId,
				"Username" => $_SESSION["PhpGt_User.tool_AuthData"]
			];
		}

		if(empty($_SESSION["PhpGt_User"])) {
			return false;
		}
		return $_SESSION["PhpGt_User"];
	}

	private function refreshCookies() {
		$expires = strtotime("+2 weeks");
		setcookie(
			"PhpGt_Login[0]",
			$_COOKIE["PhpGt_Login"][0],
			$expires);
		setcookie(
			"PhpGt_Login[1]",
			$_COOKIE["PhpGt_Login"][1],
			$expires);
		setcookie(
			"PhpGt_Login[2]",
			$_COOKIE["PhpGt_Login"][2],
			$expires);
	}

	private function deleteCookies() {
		unset($_COOKIE["PhpGt_Login"]);
		setcookie("PhpGt_Login[0]", "deleted", 0);
		setcookie("PhpGt_Login[1]", "deleted", 0);
		setcookie("PhpGt_Login[2]", "deleted", 0);
	}

	private function generateSalt() {
		// TODO: A real salt function.
		return hash("sha512", rand(0, 10000));
	}
}
?><commit_msg>Updated User tool's database scripts to upgrade new users' fields.<commit_after><?php
class User_PageTool extends PageTool {
	public function go($api, $dom, $template, $tool) {
		if(empty($_COOKIE["PhpGt_Track"])) {
			$anonId = $this->generateSalt();
			$expires = strtotime("+2 weeks");
			if(setcookie("PhpGt_Track", $anonId, $expires) === false) {
				// TODO: Throw proper error. Cookie can't be set if
				// any output has been made!
				die("Can't set PhpGt_Track cookie!");
			}
			return $anonId;
		}

		return $_COOKIE["PhpGt_Track"];
	}

	public function __get($name) {
		switch($name) {
		case "id":
			return $_SESSION["PhpGt_User"]["Id"];
			break;
		case "username":
		case "userName":
			return $_SESSION["PhpGt_User"]["Username"];
			break;
		case "uuid":
			return $_COOKIE["PhpGt_Login"][0];
			break;
		case "firstName":
		case "firstname":
			$this->checkNames();
			return $_SESSION["PhpGt_User"]["FirstName"];
			break;
		case "lastName":
		case "lastname":
			$this->checkNames();
			return $_SESSION["PhpGt_User"]["LastName"];
			break;
		case "fullName":
		case "fullname":
			$this->checkNames();
			return $this->firstName . " " . $this->lastName;
			break;
		default:
			// If non-standard property is requested, check in database for
			// the field.
			$dbUser = $this->_api["User"]->getById($this->id);
			return $dbUser[$name];
			break;
		}
	}

	private function checkNames() {
		if(empty($_SESSION["PhpGt_User"]["FirstName"])
		|| empty($_SESSION["PhpGt_User"]["LastName"])) {
			if(empty($_SESSION["PhpGt_User"]["Id"])) {
				// TODO: Throw proper error.
				die("Error: No user ID. (checkNames)");
			}
			$user = $this->_api["User"]->getById(
				["Id" => $_SESSION["PhpGt_User"]["Id"]]);

			$_SESSION["PhpGt_User"]["FirstName"] = $user["FirstName"];
			$_SESSION["PhpGt_User"]["LastName"] = $user["LastName"];
		}
	}

	public function get() {
		return $_SESSION["PhpGt_User"];
	}

	public function auth($method = "Google") {
		$oid = new OpenId_Utility($method);
		$username = $oid->getData();
		if(is_null($username)) {
			$this->unAuth();
			return false;
		}

		$_SESSION["PhpGt_User.tool_AuthData"] = $username;

		$uuid = hash("sha512", $username);
		$userSalt = $this->generateSalt();
		$expires = strtotime("+2 weeks");
		setcookie(
			"PhpGt_Login[0]",
			$uuid,
			$expires);
		setcookie(
			"PhpGt_Login[1]",
			$userSalt,
			$expires);
		setcookie(
			"PhpGt_Login[2]",
			hash("sha512", $uuid . $userSalt . APPSALT),
			$expires);
		return true;
	}

	public function unAuth($forwardTo = "/") {
		unset($_SESSION["PhpGt_User.tool_AuthData"]);
		unset($_SESSION["PhpGt_User"]);
		$this->deleteCookies();
		header("Location: " . $forwardTo);
		return;
	}

	/**
	 * Checks the current session for authentication data. This may be
	 * authentication with OpenId or using a simple username/password stored
	 * in the User database table.
	 */
	public function checkAuth() {
		$userId = null;
		$username = null;
		
		// The openid_identity variable is sent by an OpenId provider on
		// successful authentication.
		if(isset($_GET["openid_identity"])) {
			$this->auth();
			if(isset($_GET["openid_return_to"])) {
				header("Location: " . $_GET["openid_return_to"]);
				exit;
			}
		}
		
		// User auth through OpenId or saved cookie is stored in the session.
		if(isset($_SESSION["PhpGt_User.tool_AuthData"])) {
			$username = $_SESSION["PhpGt_User.tool_AuthData"];
			// User has authenticated, need to match to database.
			// The database user may not exist and need creating.
			$dbUser = $this->_api["User"]->get(["Username" => $username]);
			if(is_null($dbUser) || $dbUser->hasResult) {
				// Already exists - use this user.
				$userId = $dbUser["Id"];
			}
			else {
				// Doesn't already exist - create and use new user.
				$newDbUser = $this->_api["User"]->addEmpty(array(
					"Username" => $username,
					"Uuid" => hash("sha512", $username)
				));
				$userId = $newDbUser->lastInsertId;
			}
		}
		else {
			// Cookie information:
			// Login 0 = sha512 of username (the user's UUID)
			// Login 1 = sha512 unique salt, generated for this user only
			// Login 2 = sha512 authentication hash sha(login0.login1.site_salt)
			// The site salt is stored as a named constant, defined in security
			// config.
			if(isset($_COOKIE["PhpGt_Login"])) {
				if(isset($_COOKIE["PhpGt_Login"][0])
				&& isset($_COOKIE["PhpGt_Login"][1])
				&& isset($_COOKIE["PhpGt_Login"][2])) {
					$userHash = $_COOKIE["PhpGt_Login"][0];
					$saltHash = $_COOKIE["PhpGt_Login"][1];
					$authHash = $_COOKIE["PhpGt_Login"][2];
					
					// Find the user from their UUID.
					$dbResult = $this->_api["User"]->getByUuid(array(
						"Uuid" => $userHash
					));
					if($dbResult->hasResult) {
						// Check authenticity of cookies before logging user in.
						$authHash2 = hash("sha512",
							$userHash . $saltHash . APPSALT);
						if($authHash == $authHash2) {
							// Log in.
							$dbUser = $this->_api["User"]->getByUuid(array(
								"Uuid" => $userHash
							));
							if($dbUser->hasResult) {
								// Already exists - use this user.
								$userId = $dbUser["Id"];
								$_SESSION["PhpGt_User.tool_AuthData"] = 
									$dbUser["Username"];
								$this->refreshCookies();
							}
							else {
								// Something's gone wrong... there is proper
								// cookie authentication, but no user exists
								// in database!
								// TODO: Log this error.
							}
						}
						else {
							// Failure - kill all cookies.
							$this->deleteCookies();
						}
					}
				}
				else {
					// Kill any trace of the login cookie.
					$this->deleteCookies();
				}
			}

			if(isset($_SESSION["PhpGt_User"])) {
				if(empty($_SESSION["PhpGt_User"])) {
					unset($_SESSION["PhpGt_User"]);
					return false;
				}
			}
		}

		if(!is_null($userId)) {
			$_SESSION["PhpGt_User"] = [
				"Id" => $userId,
				"Username" => $_SESSION["PhpGt_User.tool_AuthData"]
			];
		}

		if(empty($_SESSION["PhpGt_User"])) {
			return false;
		}
		return $_SESSION["PhpGt_User"];
	}

	private function refreshCookies() {
		$expires = strtotime("+2 weeks");
		setcookie(
			"PhpGt_Login[0]",
			$_COOKIE["PhpGt_Login"][0],
			$expires);
		setcookie(
			"PhpGt_Login[1]",
			$_COOKIE["PhpGt_Login"][1],
			$expires);
		setcookie(
			"PhpGt_Login[2]",
			$_COOKIE["PhpGt_Login"][2],
			$expires);
	}

	private function deleteCookies() {
		unset($_COOKIE["PhpGt_Login"]);
		setcookie("PhpGt_Login[0]", "deleted", 0);
		setcookie("PhpGt_Login[1]", "deleted", 0);
		setcookie("PhpGt_Login[2]", "deleted", 0);
	}

	private function generateSalt() {
		// TODO: A real salt function.
		return hash("sha512", rand(0, 10000));
	}
}
?>
{'fraction_non_alphanumeric': 0.17385705086928527, 'fraction_numerical': 0.009980682549903412, 'mean_word_length': 3.1933850826864663, 'pattern_counts': {'":': 19, '<': 5, '<?xml version=': 0, '>': 72, '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': '3597194', 'n_tokens_mistral': 5549, 'n_tokens_neox': 4464, 'n_words': 920}