June 6, 2016♦
<?php
namespace account;
require_once "verify_user_credentials.php";
require_once "verify_temporary_credentials.php";
require_once "add_temporary_account.php";
$GLOBALS["USERS_DIRECTORY"] = "users/";
$GLOBALS["COOKIE_LENGTH"] = "2592000";
$GLOBALS["COOKIE_PATH"] = "/";
$GLOBALS["TEMP_USERS_DIRECTORY"] = "_tmp/";
function get_user_path()
{
$root = find_users_root();
if (permanent_credentials_exist())
{
$name = $_COOKIE["name"];
if (verify_user_credentials($name, null, $_COOKIE["hash"])->count() == 0)
{
renew_cookie("name");
renew_cookie("hash");
return $root . $name . "/";
}
}
$id = null;
if (temporary_credentials_exist())
{
$id = $_COOKIE["id"];
if (!verify_temporary_credentials($id, $_COOKIE["code"]))
{
$id = null;
}
else
{
renew_cookie("id");
renew_cookie("code");
}
}
if (is_null($id))
{
$id = add_temporary_account();
}
return $root . $GLOBALS["TEMP_USERS_DIRECTORY"] . $id . "/";
}
function find_users_root()
{
$path = $GLOBALS["USERS_DIRECTORY"];
while (!is_dir($path))
{
$path = "../" . $path;
}
return $path;
}
function permanent_credentials_exist()
{
$name = array_key_exists("name", $_COOKIE);
$hash = array_key_exists("hash", $_COOKIE);
return $name && $hash;
}
function renew_cookie($name)
{
$expiration = time() + intval($GLOBALS["COOKIE_LENGTH"]);
$path = $GLOBALS["COOKIE_PATH"];
setcookie($name, $_COOKIE[$name], $expiration, $path);
}
function temporary_credentials_exist()
{
$id = array_key_exists("id", $_COOKIE);
$code = array_key_exists("code", $_COOKIE);
return $id && $code;
}
function is_permanent_account($path)
{
return !preg_match("/_tmp\/.+\/$/", $path);
}
function find_temp_users_root()
{
$path = find_users_root() . $GLOBALS["TEMP_USERS_DIRECTORY"];
if (!is_dir($path))
{
mkdir($path, 0770);
}
return $path;
}
function build_user_path($name)
{
return find_users_root() . "$name/";
}
<?php
namespace account;
require_once "Errors.php";
require_once "validate_submission.php";
require_once "get_user_path.php";
require_once "add_user_account.php";
require_once "add_user_cookie.php";
submit_change_password_request();
function submit_change_password_request()
{
$name = $_GET["name"];
$old_password = $_GET["old_pass"];
$new_password = array($_GET["new_pass"], $_GET["new_pass_confirmation"]);
$errors = verify_user_credentials($name, $old_password);
validate_password($new_password, $errors);
if ($errors->count() == 0)
{
store_password(build_user_path($name), $new_password[0]);
add_user_cookie($name);
}
echo $errors;
}
<?php
namespace account;
require_once "user_exists.php";
require_once "validate_submission.php";
require_once "add_user_account.php";
require_once "add_user_cookie.php";
register_user();
function register_user()
{
$username = $_GET["user"];
$password = array($_GET["pass"], $_GET["repeat"]);
$email_address = $_GET["email"];
$errors = validate_submission($username, $password, $email_address);
if ($errors->count() > 0)
{
echo $errors;
return;
}
add_user_account($username, $password[0], $email_address);
add_user_cookie($username);
}
EVR.include("account/form/forms/Forms.js");
EVR.include("account/form/input/Input.js");
EVR.include("account/form/error/Error.js");
EVR.Account.Form = function(container, title)
{
this.container = container;
this.title = title;
this.focused = false;
this.inputs = [];
this.element = document.createElement("form");
this.element.method = "post";
}
EVR.Account.Form.prototype.initialize = function()
{
this.errors = new EVR.Account.Form.Error.Errors(this);
this.add_title();
this.add_listeners();
}
EVR.Account.Form.prototype.append = function()
{
this.container.appendChild(this.element);
}
EVR.Account.Form.prototype.add_title = function()
{
var element = document.createElement("div");
element.innerHTML = this.title;
this.element.appendChild(element);
}
EVR.Account.Form.prototype.add_listeners = function()
{
var current = this;
this.element.onkeydown =
function(event) {
var code = event ? event.keyCode : window.event.keyCode;
if (code == 13)
{
current.respond();
return false;
}
};
this.element.onsubmit = function() { return false };
}
EVR.Account.Form.prototype.add_input = function(name, value, type)
{
if (type == null)
{
type = "text";
}
var input = new EVR.Account.Form.Input(this, value, type);
input.append();
this.inputs[name] = input;
return input;
}
EVR.Account.Form.prototype.neutralize = function()
{
var inputs = this.inputs;
for (name in inputs)
{
inputs[name].set_background(FORM_NEUTRAL_COLOR);
}
}
EVR.Account.Form.prototype.build_query = function()
{
var inputs = this.inputs;
var value, query = "";
for (name in inputs)
{
value = encodeURIComponent(inputs[name].get_value());
query += name + "=" + value + "&";
}
return query;
}
EVR.Account.Form.prototype.clear_errors = function()
{
this.errors.clear();
this.neutralize();
}
EVR.Account.Form.prototype.add_error = function(message, affected)
{
if (typeof(affected) == "string")
{
affected = [affected];
}
this.errors.add_error(message, affected);
}
EVR.Account.Form.prototype.display_errors = function()
{
this.errors.display();
}
EVR.Account.Form.prototype.reset = function()
{
var input, inputs = this.inputs;
for (name in inputs)
{
input = inputs[name];
input.set_value("");
input.swap();
}
this.clear_errors();
}