forked from rpiambulance/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.functions.php
96 lines (78 loc) · 2.84 KB
/
.functions.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
function checkIfAdmin($connection) {
if(!isset($_GET['session_id'])) {
return false;
}
$user = getUser($_GET['session_id'], $connection);
$username = $user['username'];
if(!isset($username)) {
return false;
} else {
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(!isset($dname)) {
$dname = 'ambulanc_web';
}
$connection->exec("USE `$dname`");
$statement=$connection->prepare("SELECT username, admin, schedco FROM members WHERE username = :username");
$statement->bindParam(':username', $username);
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
return ($results[0]['admin'] == 1 || $results[0]['schedco'] == 1);
}
}
function getUser($sessionID, $connection){
if(isset($sessionID)){
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(!isset($dname)) {
$dname = 'ambulanc_web';
}
$connection->exec("USE `$dname`");
$statement = $connection->prepare('SELECT * FROM members LEFT JOIN sessions on sessions.userID = members.id WHERE sessions.sessionID = :sessionID');
$statement->bindParam(":sessionID", $sessionID);
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
updateSession($sessionID, $connection);
return $results[0];
}
}
function updateSession($sessionID, $connection){
if(!isset($dname)) {
$dname = 'ambulanc_web';
}
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Just prune the database for expired sessions everytime
$stmt = $connection->prepare('DELETE FROM sessions WHERE expiration <= NOW()');
$stmt->execute();
$stmt = $connection->prepare('SELECT * FROM `sessions` WHERE sessionID = :sessionID');
$stmt->bindParam(":sessionID", $sessionID);
$stmt->execute();
$current_session = $stmt->fetch();
date_default_timezone_set('America/New_York');
$current_date = new DateTime();
$expiration = strtotime($current_session['expiration']);
if (time() < $expiration){
$current_date = $current_date->modify('+5 day');
$current_date = $current_date->format('Y-m-d H:i:s');
$stmt = $connection->prepare('UPDATE sessions SET expiration=:expiration WHERE sessionID=:sessionID');
$stmt->bindParam(":sessionID",$sessionID);
$stmt->bindParam(":expiration", $current_date);
$stmt->execute();
setcookie("RPIA-SESSION", $sessionID, strtotime("+5 day", time()), "/");
}
}
function openDatabaseConnection(){
try{
include '.db_config.php';
$connection = new PDO($dsn, $duser, $dpassword);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(!isset($dname)) {
$dname = 'ambulanc_web';
}
// Selecting Database
$connection->exec("USE `$dname`");
return $connection;
}catch(PDOException $e){
echo $e;
return null;
}
}