This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
/
common.php
executable file
·342 lines (301 loc) · 9.64 KB
/
common.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/* File for common/shared functions, classes and global variables (A grab bag - this should be distributed!)
*
* Created by: Justin Szaday on Tues. Feb 17, 2014
*/
require_once "dbconstants.php";
/************************************************************
* "Getter" Functions *
************************************************************/
// Get something of type $type with id $id
function get_x_with_id($conn, $type, $id) {
$result = $conn->query("SELECT *
FROM `$type`
WHERE `id` =$id;");
if (!$result) {
return NULL;
} else {
$row = $result->fetch_row();
$result->close();
return $row;
}
}
function get_all_x_with_query($conn, $x, $query, $desired_field = -1) {
$db_query ="SELECT * FROM `$x`" . $query;
$sections = array();
$result = $conn->query($db_query);
// $sections = $result->fetch_all();
while ($row = $result->fetch_row()) {
if ($desired_field != -1) {
array_push($sections, $row[$desired_field]);
} else {
array_push($sections, $row);
}
}
$result->close();
return $sections;
}
// Get all sections with an additional query
function get_all_sections_with_query($conn, $query) {
return get_all_x_with_query($conn, "Section", $query);
}
// Get the Building ID given a Room ID
function get_building_for_room($conn, $room_id) {
$room = get_x_with_id($conn, "Room", $room_id);
$building = $room[ROOM_BLDG];
return $building;
}
// Parse meeting times into a more appropriate format
function parse_meeting_times($section, $rooms, $buildings) {
$meeting_times = $section->meeting_times;
$parsed_times = array();
for ($i = 0; $i < count($meeting_times); $i++) {
$room_id = $section->room_ids[$i];
if ($rooms && !in_array($room_id, $rooms)) {
continue;
}
if ($buildings) {
$room = get_x_with_id($conn, "Room", $room_id);
if(!in_array($room[ROOM_BLDG], $buildings)) {
continue;
}
}
preg_match($GLOBALS["date_regex"], $meeting_times[$i], $matches);
$days = $matches[1];
$start = strtotime($matches[2] . "m", 0);
$end = strtotime($matches[3] . "m", 0);
$range = array($start, $end, $i);
for ($j = 0; $j < strlen($days); $j++) {
$parsed_times[$GLOBALS["day_abbreviations"][$days{$j}]] = $range;
}
}
return $parsed_times;
}
/************************************************************
* Utility Functions *
************************************************************/
// Replace the first occurence of something
function str_replace_first($search, $replace, $subject) {
$pos = strpos($subject, $search);
if ($pos !== false) {
$subject = substr_replace($subject, $replace, $pos, strlen($search));
}
return $subject;
}
// Check a bunch o" parameters & make sure they ain"t null
function check_parameters() {
$params = func_get_args();
foreach ($params as $param) {
if (!$param) {
return false;
}
}
return true;
}
function json_to_sql($json) {
$parameter = json_decode($json);
if (is_array($parameter)) {
return implode(",", $parameter);
} else {
return $parameter;
}
}
function implode_parameters($parameter) {
if (is_array($parameter)) {
return implode(",", $parameter);
} else {
return $parameter;
}
}
/************************************************************
* User Management Functions *
************************************************************/
// creates a 3 character sequence
function create_salt()
{
$string = md5(uniqid(rand() , true));
return substr($string, 0, 3);
}
// this is a security measure
function validate_user($user_data)
{
session_regenerate_id();
$_SESSION = array_merge($_SESSION, $user_data);
$_SESSION[SESSION_VALID] = true;
session_write_close();
}
// destroys all of the session variables
function logout_user()
{
$_SESSION = array();
session_destroy();
}
// returns true if the user is logged in
function is_user_logged_in()
{
return isset($_SESSION[SESSION_VALID]) && $_SESSION[SESSION_VALID];
}
function update_user_info($conn, $username, $firstname, $lastname) {
$query = "UPDATE `Users`
SET `Username`='$username', `FirstName`='$firstname', `LastName`='$lastname'
WHERE `Username`='$username';";
$user_data = array(
"Username" => $username,
"FirstName" => $firstname,
"LastName" => $lastname
);
if ($conn->query($query)) {
validate_user($user_data);
return "Successful";
} else {
return "Could not update user's information, " . $conn->error;
}
}
function change_user_password($conn, $username, $new_password) {
$hash = hash("sha256", $new_password);
$salt = create_salt();
$hash = hash("sha256", $salt . $hash);
$query = "UPDATE `Users`
SET `Password`='$hash', `Salt`='$salt'
WHERE `Username`='$username';";
if ($conn->query($query)) {
return "Successful";
} else {
return "Could not update user's password, " . $conn->error;
}
}
// logs a user in, returns an error string if unsuccessful
function login_user($username, $password)
{
$username = strtolower($username);
$mysqli = new mysqli($GLOBALS["dbhost"], $GLOBALS["dbuser"], $GLOBALS["dbpass"], $GLOBALS["dbname"]);
if (mysqli_connect_errno())
{
return mysqli_connect_error();
}
$username = $mysqli->real_escape_string($username);
$query = "SELECT * FROM Users WHERE Username = '$username';";
$result = $mysqli->query($query);
if ($result->num_rows < 1)
{
$result->close();
$mysqli->close();
return "User \"$username\" not found!";
}
$user_data = $result->fetch_assoc();
$result->close();
$hash = hash("sha256", $user_data[USER_SALT] . hash("sha256", $password));
if ($hash != $user_data[USER_PASSWORD])
{
$mysqli->close();
return "Incorrect password for user \"$username\"!";
}
$mysqli->close();
validate_user($user_data);
return "Successful";
}
// registers a user, returns an error string if unsuccessful
function register_user($username, $password, $firstname, $lastname)
{
$username = strtolower($username);
$hash = hash("sha256", $password);
$salt = create_salt();
$hash = hash("sha256", $salt . $hash);
$mysqli = new mysqli($GLOBALS["dbhost"], $GLOBALS["dbuser"], $GLOBALS["dbpass"], $GLOBALS["dbname"]);
if (mysqli_connect_errno())
{
return mysqli_connect_error();
}
// sanitize username
$username = $mysqli->real_escape_string($username);
// check if user is registered
$query = "SELECT * FROM Users WHERE Username = '$username';";
$result = $mysqli->query($query);
if ($result->num_rows > 0)
{
// if they are, close the connection and result and return
$result->close();
$mysqli->close();
return "Username already registered!";
}
// close the result
$result->close();
// insert user into database
$query = "INSERT INTO Users ( Username, FirstName, LastName, Password, Salt ) VALUES ( '$username' , '$firstname' , '$lastname', '$hash', '$salt' );";
$result = $mysqli->query($query);
if (!$result) {
$mysqli->close();
return "Could not insert user.";
}
// Close the connection
$mysqli->close();
// Return Successful
return "Successful";
}
/************************************************************
* Misc. Classes *
************************************************************/
class ValpoSection {
public $name;
public $credit_hours;
public $tl_credits;
public $title;
public $meeting_times;
public $room_ids;
public $rooms;
public $semester;
public $meeting_type;
public $professor;
public $capacity;
public $database_id;
public static function new_from_db_row($conn, $db_row) {
$section = new ValpoSection();
$class = get_x_with_id($conn, "Class", $db_row[SECTION_CLASS]);
$professor = get_x_with_id($conn, "Professor", $db_row[SECTION_PROF]);
$semester = get_x_with_id($conn, "Semester", $db_row[SECTION_SEM]);
$section->database_id = $db_row[SECTION_DBID];
$section->name = $class[CLASS_NAME] . "-" . $db_row[SECTION_ID];
$section->title = $class[CLASS_TITLE];
$section->semester = $semester[SEMESTER_NAME];
$section->meeting_type = $db_row[SECTION_TYPE];
$section->professor = $professor[PROFESSOR_NAME];
$section->meeting_times = json_decode($db_row[SECTION_TIMES]);
// Load the number of credit hours from the row at first
$section->credit_hours = $db_row[SECTION_CRHR];
// But if it is null
if (is_null($section->credit_hours)) {
// Grab them from the class instead
$section->credit_hours = $class[CLASS_CREDITHOURS];
}
// Load the number of credit hours from the row at first
$section->tl_credits = $db_row[SECTION_TLC];
// But if it is null
if (is_null($section->tl_credits)) {
// Grab them from the class instead
$section->tl_credits = $class[CLASS_CONTACTHOURS];
// If it is still null, the number of tlc = credithours
if (is_null($section->tl_credits)) {
$section->tl_credits = $section->credit_hours;
}
}
$section->rooms = array();
$smallest_cap = PHP_INT_MAX;
$section->room_ids = json_decode($db_row[SECTION_ROOMS]);
foreach ($section->room_ids as $room_id) {
$room = get_x_with_id($conn, "Room", $room_id);
$building = get_x_with_id($conn, "Building", $room[ROOM_BLDG]);
if ($room[ROOM_CAP] < $smallest_cap) {
$smallest_cap = $room[ROOM_CAP];
}
array_push($section->rooms, $building[BUILDING_ABRV] . "-" . $room[ROOM_NMBR]);
}
$section->capacity = $db_row[SECTION_MCAP];
// If the section does not provide a meeting size cap
if (!$section->capacity) {
// Assign to smallest capacity
$section->capacity = $smallest_cap;
}
return $section;
}
}
?>