blob: abaac74426156b5af41d9c82dbd3742c419d4748 [file] [log] [blame]
Andreu0d72bd62019-09-17 23:31:14 +02001<?php
Andreu2457e402019-09-22 00:52:41 +02002 require '../credentials.php';
Andreuabbcb7e2019-09-21 18:22:14 +02003 require 'utils.php';
Andreuefe66eb2019-09-21 18:41:49 +02004
Andreu09b8b052019-09-21 21:47:20 +02005 // Set the 'user' POST and COOKIE variable
Andreu20cbd1d2019-09-22 00:00:57 +02006 $user = '';
7 if (isset($_POST['user'])) $user = $_POST['user'];
8 else if (isset($_COOKIE['user'])) $user = $_COOKIE['user'];
9 else {
Andreu4b2fbd92019-09-22 22:30:25 +020010 die("<script>window.location.href = '../index.php'</script>");
Andreu09b8b052019-09-21 21:47:20 +020011 }
12
Andreu20cbd1d2019-09-22 00:00:57 +020013 // Check if password is correct
14 $query_password = "SELECT password FROM users WHERE id=".$user;
15 $real_password = query($query_password)->fetch_row()[0];
16
17 // Prioritize input rather than memory
18 $password = '';
19 if (isset($_POST['password'])) $password = $_POST['password'];
20 else if (isset($_COOKIE['password'])) $password = $_COOKIE['password'];
21
22 // Redirect if wrong
23 if ($real_password != "" && $real_password != md5($password)) {
Andreu543e70c2019-09-22 14:08:49 +020024 // Forget cookies
25 setcookie('user', '', -1, "/");
26 setcookie('password', '', -1, "/");
27
Andreu4b2fbd92019-09-22 22:30:25 +020028 die("<script>window.location.href = '../index.php?wrongpassword=1'</script>");
Andreu20cbd1d2019-09-22 00:00:57 +020029 }
30
31 // Save variables as cookies
32 setcookie('user', $user, time() + (86400 * 10), "/");
Andreu543e70c2019-09-22 14:08:49 +020033 if ($real_password != "") setcookie('password', md5($password), time() + (86400 * 10), "/");
Andreu20cbd1d2019-09-22 00:00:57 +020034 else setcookie('password', '', -1, "/");
35
36 // Success, proceed to main page
Andreua0fc8272019-09-23 00:35:25 +020037 die("<script>window.location.href = '../main.php';</script>");
Andreu0d72bd62019-09-17 23:31:14 +020038?>