-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
219 lines (194 loc) · 9.5 KB
/
index.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
// Require configuration file.
require 'config.php';
// Require database file for database connecton.
require 'database/db_connection.php';
$login_button = '';
// This $_GET["code"] variable value received after user has login into their Google Account redirct to PHP script then this variable value has been received
if(isset($_GET["code"]))
{
// It will Attempt to exchange a code for an valid authentication token.
$token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);
//This condition will check there is any error occur during geting authentication token. If there is no any error occur then it will execute if block of code/
if(!isset($token['error']))
{
// Set the access token used for requests.
$google_client->setAccessToken($token['access_token']);
// Store "access_token" value in $_SESSION variable for future use.
$_SESSION['access_token'] = $token['access_token'];
// Create Object of Google Service OAuth 2 class.
$google_service = new Google_Service_Oauth2($google_client);
// Get user profile data from google.
$data = $google_service->userinfo->get();
// Below you can find Get profile data and store into $_SESSION variable.
if(!empty($data['given_name']))
{
$_SESSION['given_name'] = $data['given_name'];
}
if(!empty($data['family_name']))
{
$_SESSION['family_name'] = $data['family_name'];
}
if(!empty($data['email']))
{
$_SESSION['email'] = $data['email'];
}
if(!empty($data['picture']))
{
$_SESSION['picture'] = $data['picture'];
}
}
header('Location: ../index.php?');
}
//This is for check user has login into system by using Google account, if User not login into system then it will execute if block of code and make code for display Login link for Login using Google account.
if(!isset($_SESSION['access_token']))
{
//Create a URL to obtain user authorization.
$login_button = '<a href="'.$google_client->createAuthUrl().'"><img src="website_structure/img/btn_google_signin_dark_normal_web.png"></a>';
}
$email_template_temp = str_replace('§given_name§', $_SESSION['given_name'], $email_template);
$email_template_temp = str_replace('§family_name§', $_SESSION['family_name'], $email_template_temp);
$email_template_temp = str_replace('§email_domain§', $email_domain, $email_template_temp);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>RDL</title>
<meta content='width=device-width, initial-scale=1, maximum-scale=1' name='viewport'/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<?php
// Importing standardized header.
include 'website_structure/header.php';
// Checking for error scripts.
require 'scripts/error_checking-script.php';
// Defining sql query and initalizing a connectoin to the database.
$sql = "SELECT `rank` FROM RDL_users WHERE `email` = ?";
$stmt = mysqli_stmt_init($conn);
// Checking if their is a problem with the sql query.
if (!mysqli_stmt_prepare($stmt, $sql))
{
// Sends back user if their is a problem with sql querys sent to database.
header('Location: ../index.php?err=sqlerr1');
exit();
}
// Checks what rank the user have.
else
{
// Inputs the email as an argument to find rank.
mysqli_stmt_bind_param($stmt, 's', strtolower($_SESSION['email']));
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $_SESSION['rank']);
if (mysqli_stmt_fetch($stmt));
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
// Checking if the login button is pressed.
if ($login_button == '')
{
if (
(
strtolower(substr($email_restriction, 0, 1)) == 'y'
&& strtolower($email_template_temp) == $_SESSION['email']
)
|| strtolower(substr($email_restriction, 0, 1)) == 'n'
|| $_SESSION['rank'] == '3'
|| $_SESSION['rank'] == '4'
)
{
// Checks so the user have an appropriate rank.
if($_SESSION['rank'] == '1' || $_SESSION['rank'] == '2' || $_SESSION['rank'] == '3' || $_SESSION['rank'] == '4')
{
echo '
<img src="'.$_SESSION["picture"].'" class="img-responsive img-circle img-thumbnail" />
<p>Name : '.$_SESSION['given_name'].' '.$_SESSION['family_name'].'</p>
';
// A logout button.
echo '
<form action="scripts/logout-script.php" method="post">
<button type="submit" name="logout-submit">
<p>LOGOUT</p>
</button>
</form>
';
// A button that sends a request to open the door.
echo '
<form action="scripts/request-script.php" method="post">
<button type="submit" name="request-submit">
<p>OPEN DOOR</p>
</button>
</form>
';
// Adds a user add form for moderator, admins and fallbackadmin.
if($_SESSION['rank'] == '2' || $_SESSION['rank'] == '3' || $_SESSION['rank'] == '4')
{
echo '
<form action="scripts/add_user-script.php" method="post">
<input type="text" placeholder="given_name" name="given_name">
<input type="text" placeholder="family_name" name="family_name">
<input type="text" placeholder="email" name="email">
';
// Adds the option for admins adn fallbackadmins too select what rank a new user is supose to have.
if($_SESSION['rank'] == '3' || $_SESSION['rank'] == '4')
{
echo '
<input type="radio" id="user" value="1" name="rank">
<label for="user">User</label>
<input type="radio" id="moderator" value="2" name="rank">
<label for="moderator">Moderator</label>
<input type="radio" id="admin" value="3" name="rank">
<label for="admin">Admin</label>
';
}
// A button to add user email, first and lastname to the database.
echo '
<button type="submit" name="user-submit">ADD USER</button>
</form>
';
}
// Shows admins and fallbackadmins a list of all users bellow their rank.
if ($_SESSION['rank'] == '3'|| $_SESSION['rank'] == '4')
{
// A button that sends you to the user list page.
echo '
<form action="user_list.php" method="post">
<button type="submit" name="list_users-submit"><p>USER LIST</p></button>
</form>
';
}
// Shows fallbackadmins a list of all units.
if ($_SESSION['rank'] == '4')
{
// A button that sends you to the unit list page.
echo '
<form action="unit_list.php" method="post">
<button type="submit" name="list_units-submit"><p>UNIT LIST</p></button>
</form>
';
}
}
// Logs a person out if they dont have a rank/do not exist in the database.
else
{
require "scripts/logout-script.php";
header('Location: ../index.php?err=no-access');
exit();
}
}
else
{
require "scripts/logout-script.php";
header('Location: ../index.php?err=no-access');
exit();
}
}
else
{
echo '<div>'.$login_button . '</div>';
}
// Importing standardized footer.
include 'website_structure/footer.php';
?>
</body>
</html>