/
functions.php
executable file
·335 lines (294 loc) · 9.51 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
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
<?php
require_once('PasswordHash.php');
session_start();
function login($email, $password)
{
global $db;
if(isset($_COOKIE['session']))
return 3;
$db->Prepare('SELECT * FROM `users` WHERE email=\'$0\'');
$db->Execute($email);
if($db->RowCount() <= 0)
return 1;
$row = $db->Fetch();
$hasher = new PasswordHash(8, false);
if(!$hasher->CheckPassword($password, $row['password']))
return 2;
if($row['activated'] == 0)
return 4;
$secure = $hasher->HashPassword($email . ':' . uniqid() . ':' . $password);
$db->Prepare('SELECT id FROM `sessions` WHERE user_id=\'$0\'');
$db->Execute($row['id']);
if($db->RowCount() <= 0)
{
$db->Prepare('INSERT INTO `sessions` (ip, session, user_id) VALUES (\'$0\', \'$1\', \'$2\')');
$db->Execute($_SERVER['REMOTE_ADDR'], $secure, $row['id']);
}
else
{
$sess_row = $db->Fetch();
$db->Prepare('UPDATE `sessions` SET ip=\'$0\', session=\'$1\', user_id=\'$2\' WHERE id=$3');
$db->Execute($_SERVER['REMOTE_ADDR'], $secure, $row['id'], $sess_row['id']);
}
setcookie('session', $secure);
return 0;
}
function load_session()
{
global $db;
global $account;
$account = false;
if(isset($_COOKIE['session']))
{
$db->Prepare('SELECT * FROM `sessions` WHERE session=\'$0\' LIMIT 1');
$db->Execute($_COOKIE['session']);
if($db->RowCount() <= 0)
{
setcookie('session', "", time() - 1);
}
else
{
$row = $db->Fetch();
$account = $row['user_id'];
}
}
}
function logout()
{
global $db;
if(isset($_COOKIE['session']))
{
$db->Prepare('DELETE FROM `sessions` WHERE session=\'$0\' LIMIT 1');
$db->Execute($_COOKIE['session']);
} else
return -1;
setcookie('session', "", time() - 1);
return 0;
}
function register($email, $password, $first, $last)
{
global $db;
$db->Prepare('SELECT id FROM `users` WHERE email=\'$0\'');
$db->Execute($email);
if($db->RowCount() > 0)
return -1;
$hasher = new PasswordHash(8, false);
$password = $hasher->HashPassword($password);
$firstname = ucfirst($first);
$lastname = ucfirst($last);
$db->Prepare("INSERT INTO users (email, password, first_name, last_name, activated) VALUES ('$0', '$1', '$2', '$3', '$4')");
$db->Execute(trim($email), $password, trim($firstname), trim($lastname), 0);
$db->Prepare("SELECT LAST_INSERT_ID()");
$db->Execute();
$id = $db->Fetch();
$id = implode($id, "");
$db->Prepare("SELECT UUID()");
$db->Execute();
$uuid = $db->Fetch();
$uuid = str_replace("-", "", implode($uuid, ""));
$uuid = substr($uuid, 0, 16);
$db->Prepare("INSERT INTO activation_keys (`key`, user_id) VALUES ('$0', '$1')");
$db->Execute($uuid, $id);
return $uuid;
}
function activate_account($key)
{
global $db;
$db->Prepare("SELECT user_id FROM activation_keys WHERE `key`='$0'");
$db->Execute($key);
if($db->RowCount() == 1)
{
$id = $db->Fetch();
$db->Prepare("UPDATE users SET activated='1' WHERE id='$0'");
$db->Execute($id["user_id"]);
$db->Prepare("DELETE FROM activation_keys WHERE `key`='$0'");
$db->Execute($key);
return $id;
}
return false;
}
function get_account_info($id)
{
global $db;
$db->Prepare("SELECT * FROM users WHERE id='$0'");
$db->Execute($id);
return $db->Fetch();
}
function get_college_from_email($email)
{
global $db;
$domain = substr(strrchr($email, "@"), 1);
$db->Prepare("SELECT * FROM colleges WHERE domain='$0'");
$db->Execute($domain);
return $db->Fetch();
}
function get_registered_courses($id)
{
global $db;
$db->Prepare("SELECT * FROM courses WHERE id IN (SELECT course_id FROM user_courses WHERE user_id='$0')");
$db->Execute($id);
$ret = array();
while($result = $db->Fetch())
{
array_push($ret, $result);
}
return $ret;
}
function get_courses_in_department($dept, $college)
{
global $db;
$db->Prepare("SELECT * FROM courses WHERE department='$0' AND college_id='$1'");
$db->Execute($dept, $college);
$ret = array();
while($result = $db->Fetch())
{
array_push($ret, $result);
}
return $ret;
}
function get_departments_in_college($college)
{
global $db;
$db->Prepare("SELECT DISTINCT(department) AS department FROM courses WHERE college_id='$0' ORDER BY department ASC");
$db->Execute($college);
$ret = array();
while($result = $db->Fetch())
{
array_push($ret, $result["department"]);
}
return $ret;
}
function set_user_courses($user_id, $courses)
{
global $db;
$db->Prepare("DELETE FROM user_courses WHERE user_id='$0'");
$db->Execute($user_id);
foreach($courses as $c)
{
$db->Prepare("INSERT INTO user_courses (user_id, course_id) VALUES ('$0', '$1')");
$db->Execute($user_id, $c);
}
}
function get_course_info($id)
{
global $db;
$db->Prepare("SELECT * FROM courses WHERE id='$0'");
$db->Execute($id);
return $db->Fetch();
}
function is_course_registered($course, $user)
{
global $db;
$db->Prepare("SELECT * FROM user_courses WHERE user_id='$0' AND course_id='$1'");
$db->Execute($user, $course);
return $db->RowCount() > 0;
}
function add_post($user, $course, $content)
{
global $db;
$db->Prepare("INSERT INTO posts (user_id, course_id, content, active) VALUES ('$0', '$1', '$2', '1')");
$db->Execute($user, $course, $content);
}
function get_users_in_vicinity($lat, $long, $course)
{
global $db;
$db->Prepare("SELECT * FROM users WHERE id IN (SELECT user_id FROM user_courses WHERE course_id='$0')");
$db->Execute($course);
$users = array();
while($result = $db->Fetch())
{
$dist = haversineGreatCircleDistance($lat, $long, $result["lat"], $result["long"]);
if ($dist < 150)
array_push($users, $result["id"]);
}
return $users;
}
function get_posts_in_vicinity($lat, $long, $course)
{
global $db;
$db->Prepare("SELECT * FROM users");
$db->Execute();
$users = array();
while($result = $db->Fetch())
{
$dist = haversineGreatCircleDistance($lat, $long, $result["lat"], $result["long"]);
if ($dist < 150)
array_push($users, $result["id"]);
}
$ret = array();
foreach($users as $u)
{
$db->Prepare("SELECT * FROM posts WHERE user_id='$0' AND course_id='$1'");
$db->Execute($u, $course);
while($result = $db->Fetch())
{
array_push($ret, $result);
}
}
$temp = array();
foreach ($ret as $key => $row)
{
$temp[$key] = $row['post_time'];
}
array_multisort($temp, SORT_DESC, $ret);
return $ret;
}
function attend_post($user, $post)
{
global $db;
$db->Prepare("INSERT INTO attendees (user_id, post_id) VALUES ('$0', '$1')");
$db->Execute($user, $post);
}
function leave_post($user, $post)
{
global $db;
$db->Prepare("DELETE FROM attendees WHERE user_id='$0' AND post_id='$1'");
$db->Execute($user, $post);
}
function is_attending($user, $post)
{
global $db;
$db->Prepare("SELECT * FROM attendees WHERE user_id='$0' AND post_id='$1'");
$db->Execute($user, $post);
return $db->RowCount() > 0;
}
function finish_post($post)
{
global $db;
$db->Prepare("UPDATE posts SET active='0' WHERE id='$0'");
$db->Execute($post);
}
function get_attendees($post)
{
global $db;
$db->Prepare("SELECT * FROM users WHERE id IN (SELECT user_id FROM attendees WHERE post_id='$0' ORDER BY `id` DESC)");
$db->Execute($post);
$ret = array();
while($result = $db->Fetch())
{
array_push($ret, $result);
}
echo mysql_error();
return $ret;
}
function set_user_location($id, $lat, $lng)
{
global $db;
$db->Prepare("UPDATE users SET lat='$0', `long`='$1' WHERE id='$2'");
$db->Execute($lat, $lng, $id);
}
function haversineGreatCircleDistance(
$latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);
$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
return $angle * $earthRadius;
}
?>