-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
155 lines (146 loc) · 4.59 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
$dbhost = 'localhost';
$dbname = 's257348';
$dbuser = 's257348';
$dbpass = 'nsesssad';
$appname = "AirplaneApp";
// To generically query the db
function queryMysql($query)
{
global $dbhost, $dbname , $dbuser, $dbpass,$appname;
$connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($connection->connect_error) die($connection->connect_error);
$result = $connection->query($query);
if (!$result) die($connection->error);
$connection->close();
return $result;
}
// To update atomically the status of a seat
function updateSeat($email, $seat){
global $dbhost, $dbname , $dbuser, $dbpass,$appname;
$connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($connection->connect_error) die($connection->connect_error);
$return ="";
try{
$connection->autocommit(FALSE);
$connection->begin_transaction();
$result=$connection->query("SELECT status, email FROM Seat WHERE seat='$seat' FOR UPDATE");
if (!$result) {
trigger_error('Invalid query: ' . $connection->error);
}
if ($result->num_rows){
$row = $result->fetch_assoc();
if($row['status']=='b'){
$return = 'nothing';
}
elseif($row['status']=='r' && $row['email']==$email){
$connection->query("DELETE FROM Seat WHERE seat='$seat'");
$return = 'delete';
}
else{
$connection->query("UPDATE Seat SET email='$email' WHERE seat='$seat'");
$return = 'reserved';
}
}
else{
$connection->query("INSERT INTO Seat VALUES('$email','$seat','r')");
$return='reserved';
}
$connection->commit();
}
catch(PDOException $e){
$connection->rollback();
$return = '0';
}
mysqli_free_result($result);
$connection->close();
return $return;
}
// To check and buy atomically a seat
function buySeats($email){
global $dbhost, $dbname , $dbuser, $dbpass,$appname;
$connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($connection->connect_error) die($connection->connect_error);
$return ="";
try{
$connection->autocommit(FALSE);
$connection->begin_transaction();
$result=$connection->query("SELECT seat FROM Seat WHERE email='$email' AND status='r' FOR UPDATE");
if (!$result) {
trigger_error('Invalid query: ' . $connection->error);
}
if ($result->num_rows){
$rows = [];
while($row = mysqli_fetch_array($result))
{
array_push($rows, $row[0]);
}
if(array_equal($rows,$_SESSION['seats'])){
for($i=0; $i < sizeof($rows); $i++){
$row = $rows[$i];
$connection->query("UPDATE Seat SET status='b' WHERE seat='$row'");
}
$return = 'possible';
}
else{
$connection->query("DELETE FROM Seat WHERE status='r' AND email='$email'");
$return = "impossible";
}
}
else{
$connection->query("DELETE FROM Seat WHERE status='r' AND email='$email'");
$return = 'impossible';
}
$connection->commit();
}
catch(PDOException $e){
$connection->rollback();
$return = 'impossible';
}
mysqli_free_result($result);
$connection->close();
return $return;
}
// To compare two arrays
function array_equal($a, $b) {
return (is_array($a) && is_array($b) && array_diff($a, $b) === array_diff($b, $a));
}
// Destroy a session and all cookies
function destroySession()
{
$_SESSION=array();
if (session_id() != "" || isset($_COOKIE[session_name()]))
setcookie(session_name(), '', time()-2592000, '/');
session_destroy();
}
// to clear input string to avoid injections
function sanitizeString($var)
{
global $connection;
$var = strip_tags($var);
$var = htmlentities($var);
$var = stripslashes($var);
global $dbhost, $dbname , $dbuser, $dbpass,$appname;
$connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($connection->connect_error) die($connection->connect_error);
$var= $connection->real_escape_string($var);
$connection->close();
return $var;
}
function checkInactivity(){
if(!isset($_SESSION['lasttime'])){
$_SESSION['lasttime']=time();
}
$lastactivity = $_SESSION['lasttime'];
if($lastactivity < time() - 120){
destroySession();
$loggedin = FALSE;
$emailstr = ' (Guest)';
header("Location: ./login.php");
exit("Your session expired");
}
else{
$_SESSION['lasttime'] = time();
}
}
?>