/
User.php
64 lines (55 loc) · 1.65 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* Sports Match Administrator
*
* Copyright © 2014-2015, Jack P. Harley, jackpharley.com
* All Rights Reserved
*/
namespace sma\controllers;
use sma\models\Alert;
use sma\Controller;
use sma\View;
use sma\models\User as UserModel;
use Exception;
class User {
public static function index() {
$visitor = UserModel::getVisitor();
if (!$visitor->id)
Controller::redirect("/user/login");
View::load("user/index.twig", array(
"visitor" => $visitor
));
}
public static function login() {
if (UserModel::getVisitor()->id != 0)
Controller::redirect(array_key_exists("r", $_GET) ? urlencode($_GET["r"]) : "");
if (empty($_POST)) {
View::load("user/login.twig", [
"redirectTo" => (array_key_exists("r", $_GET) ? urlencode($_GET["r"]) : "")
]);
}
else {
if (isset($_POST["register"]) && $_POST["register"]) {
Controller::redirect(ForumsFactory::getForumsInstance()->getRegistrationPage(
(isset($_POST["email"]) ? $_POST["email"] : null)
), true);
}
try {
UserModel::attemptLogin($_POST["email"], $_POST["password"], isset($_POST["remember-me"]));
Controller::addAlert(new Alert("success", "You have been logged in successfully"));
Controller::redirect((array_key_exists("r", $_GET)) ? $_GET["r"] : "");
}
catch (Exception $e) {
Controller::addAlert(new Alert("danger", "The login credentials you entered were incorrect, please try again"));
Controller::redirect("/user/login");
}
}
}
public static function logout() {
if ((array_key_exists("target", $_GET)) && ($_GET["target"] == "all"))
UserModel::logoutAll();
else
UserModel::logout();
Controller::redirect("");
}
}