|
@@ -0,0 +1,120 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+require_once '../src/models/user.php';
|
|
|
|
|
+
|
|
|
|
|
+function signin() {
|
|
|
|
|
+ session_start();
|
|
|
|
|
+ if (isset($_SESSION['user'])) {
|
|
|
|
|
+ header('Location: home');
|
|
|
|
|
+ exit();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $template = get_view("signin");
|
|
|
|
|
+
|
|
|
|
|
+ echo $template->render();
|
|
|
|
|
+ unset($_SESSION['message']);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function home() {
|
|
|
|
|
+ session_start();
|
|
|
|
|
+
|
|
|
|
|
+ if (!isset($_SESSION['user'])) {
|
|
|
|
|
+ header('Location: /');
|
|
|
|
|
+ exit();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $template = get_view("home");
|
|
|
|
|
+
|
|
|
|
|
+ echo $template->render();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function signout() {
|
|
|
|
|
+ session_start();
|
|
|
|
|
+ session_destroy();
|
|
|
|
|
+ header('Location: /');
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function passwd_error($message) {
|
|
|
|
|
+ $_SESSION['message'] = $message;
|
|
|
|
|
+ header('Location: changepassword');
|
|
|
|
|
+ exit();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function form_password() {
|
|
|
|
|
+ session_start();
|
|
|
|
|
+
|
|
|
|
|
+ if (!isset($_SESSION['user'])) {
|
|
|
|
|
+ header('Location: signin.php');
|
|
|
|
|
+ exit();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $template = get_view("formpasswd");
|
|
|
|
|
+
|
|
|
|
|
+ echo $template->render();
|
|
|
|
|
+ unset($_SESSION['message']);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function change_password() {
|
|
|
|
|
+ session_start();
|
|
|
|
|
+
|
|
|
|
|
+ if (!isset($_SESSION['user'])) {
|
|
|
|
|
+ header('Location: /');
|
|
|
|
|
+ exit();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($_SERVER['REQUEST_METHOD'] != 'POST') {
|
|
|
|
|
+ header('Location: /');
|
|
|
|
|
+ exit();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!isset($_POST['password_current'], $_POST['password'], $_POST['password_conf'])) {
|
|
|
|
|
+ header('Location: /');
|
|
|
|
|
+ exit();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $passwd = $_POST['password'];
|
|
|
|
|
+ $passwd_conf = $_POST['password_conf'];
|
|
|
|
|
+ $current_password = $_POST["password_current"];
|
|
|
|
|
+
|
|
|
|
|
+ if ($passwd != $passwd_conf) {
|
|
|
|
|
+ passwd_error("Passwords do not match");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (strlen($passwd) < 8) {
|
|
|
|
|
+ passwd_error("Password too short (min 8 chars)");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $res = UserModel\change_password($current_password, $passwd);
|
|
|
|
|
+
|
|
|
|
|
+ if (!$res[0]) {
|
|
|
|
|
+ passwd_error($res[1]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ header('Location: home');
|
|
|
|
|
+ exit();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function auth() {
|
|
|
|
|
+ session_start();
|
|
|
|
|
+
|
|
|
|
|
+ if ($_SERVER['REQUEST_METHOD'] != 'POST') {
|
|
|
|
|
+ header('Location: /');
|
|
|
|
|
+ exit();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!isset($_POST['id'], $_POST['password'])) {
|
|
|
|
|
+ header('Location: /');
|
|
|
|
|
+ exit();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $id = htmlspecialchars($_POST['id']);
|
|
|
|
|
+
|
|
|
|
|
+ if (!UserModel\auth($id, $_POST['password'])) {
|
|
|
|
|
+ $_SESSION['message'] = "Wrong username or password";
|
|
|
|
|
+ header('Location: /');
|
|
|
|
|
+ exit();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $_SESSION['user'] = $id;
|
|
|
|
|
+ header('Location: home');
|
|
|
|
|
+ exit();
|
|
|
|
|
+}
|