-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.php
292 lines (269 loc) · 10.8 KB
/
process.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
<?php
include_once("functions.php");
if(isset($_REQUEST['comment']) && !empty($_REQUEST['comment'])){
$errors = array();
$comment = is_string($_REQUEST['comment'])?
strip_tags($_REQUEST['comment']): $errors[] = "Comment is required";
$type = isset($_REQUEST['type'])?
strip_tags($_REQUEST['type']): $errors[] = "An error occured. Try again";
$id = isset($_REQUEST['type_id'])? intval($_REQUEST['type_id']):
$errors[] = "An error occured. Try again";
$username = isset($_REQUEST['username'])? strval($_REQUEST['username']):
$errors[] = "An error occured. Try again";
if(!$errors){
$conn = get_connection_handle();
$today = date("Y-m-d");
$now = date("H:i:sa");
$stmt = $conn->prepare("insert into comments(type, type_id, comment,
username, added_on, added_at) values(?,?,?,?,?,?)");
$stmt->bind_param("sissss", $type, $id, $comment, $username, $today,$now);
$stmt->execute();
if($conn->affected_rows == 1){
echo "Comment has been added";
}else{
echo "Something happened, try again.".$conn->error;
}
}else{
foreach($errors as $error){
echo $error;
}
}
}elseif(isset($_REQUEST['name']) && $_REQUEST['name'] !== ""){
if(!session_id())
session_start();
$username = $_SESSION['username'];
$name = strip_tags($_REQUEST['name']);
include_once("functions.php");
$conn = get_connection_handle();
$result = $conn->query("select firstname,lastname,username from users where
username != '$username' and (username like '%$name%' or firstname like '%$name%' or
lastname like '%$name%' or user_id like '%$name%') and type = 'student' limit 10");
if($result->num_rows > 0){
while($row = $result->fetch_object()){
$url = "http://localhost/UniPortal/dashboard.php?tab=chat";
$real_name = $row->firstname." ".$row->lastname;
$user = $row->username;
// $_SESSION['chatwith-name'] = $real_name;
// $_SESSION['chatwith'] = $user;
echo "<a href='$url&chatwith=$user' class='chat-list'>$real_name ($user)</a>";
}
}else{
echo "No matches were found";
}
}elseif(isset($_REQUEST['message']) && !empty($_REQUEST['message'])){
$msg = strip_tags($_REQUEST['message']);
$sender = strip_tags($_REQUEST['sender']);
$receiver = strip_tags($_REQUEST['receiver']);
$today = date("Y-m-d H:i:sa");
$status = "unviewed";
$conn = get_connection_handle();
$stmt = $conn->prepare("insert into chats(sender,receiver,message,status,
added_on) values(?,?,?,?,?)");
$stmt->bind_param("sssss", $sender,$receiver,$msg,$status,$today);
$stmt->execute();
if($conn->affected_rows == 1){
echo "Chat has been added";
}else{
echo "Could not send message: ".$conn->error;
}
}elseif(isset($_REQUEST['vote-type']) && $_REQUEST['type'] == "answer"){
include_once("User.php");
include_once("Answer.php");
$vote = 1;
$vote_type = strip_tags($_REQUEST['vote-type']);
$question = strip_tags($_REQUEST['question']);
$username = strip_tags($_REQUEST['username']);
$id = strip_tags($_REQUEST['answer_id']);
$user = new User($username);
if(!$user->has_voted($id, "answer")){
$ans = new Answer();
$ans->set_id($id);
if($vote_type == "upvote"){
$ans->insert_votes($username, $vote);
}else{
$ans->insert_votes($username, null, $vote);
}
}
}elseif(isset($_REQUEST['vote-type']) && $_REQUEST['type'] == "question"){
include_once("User.php");
include_once("Question.php");
$vote = 1;
$vote_type = strip_tags($_REQUEST['vote-type']);
$question = strip_tags($_REQUEST['question']);
$username = strip_tags($_REQUEST['username']);
$id = strip_tags($_REQUEST['question_id']);
$user = new User($username);
if(!$user->has_voted($id, "question")){
$qn = new Question($question);
$qn->set_id($id);
if($vote_type == "upvote"){
$qn->insert_votes($username, $vote);
}else{
$qn->insert_votes($username, null, $vote);
}
}
}elseif(!empty($_FILES['file'])){
$assign = strip_tags($_POST['assign_id']);
$user = strip_tags($_POST['username']);
$tutor = strip_tags($_POST['tutor']);
$date_ = new DateTime();
$today = $date_->format("Y-m-d H:i:s");
$temp_file = $_FILES['file']['tmp_name'];
$code = strip_tags($_POST['course-code']);
$stat = "true";
if(!empty($assign) && !empty($user)){
$conn = get_connection_handle();
$stmt = $conn->prepare("insert into submissions(assignment_id,
lecturer, username, date_submitted, is_submitted) values(?,?,?,?,?)");
$stmt->bind_param("issss", $assign, $tutor, $user,$today,$stat);
$stmt->execute();
if($conn->affected_rows == 1){
$yr = date("Y");
$targe_path = "images/assignments/".$yr."/".$code."/";
if(!file_exists($targe_path)){
mkdir($targe_path, 0777, true);
}
$target_file = $targe_path.$user."-".$assign.".pdf";
if(move_uploaded_file($temp_file, $target_file)){
echo "File has been submitted";
}else{
echo "File could not be submitted";
}
}
}
}elseif(isset($_POST['username']) && isset($_POST['email'])) {
$errors = array();
if (!isset($_POST['email']) || $_POST['email'] == '') {
$errors[] = 'Email address is required';
} else if (!((strpos($_POST['email'], ".") > 0) && (strpos($_POST['email'], "@") > 0)) || preg_match("/[^a-zA-Z0-9.@_-]/", $_POST['email'])) {
$errors[] = 'Invalid email address';
} else {
$email = strip_tags(trim(htmlspecialchars($_POST['email'])));
}
if (!isset($_POST['username']) || $_POST['username'] == '') {
$errors[] = 'Username is required';
} elseif (strlen($_POST['username']) < 4) {
$errors[] = 'Username must be six/more charaters';
} else {
$username = strip_tags(trim(htmlspecialchars($_POST['username'])));
}
if (!isset($_POST['firstname']) || $_POST['firstname'] == '') {
$errors[] = 'Firstname cannot be blank';
} elseif (!preg_match("/[a-zA-Z]/", $_POST['firstname'])) {
$errors[] = 'Invalid characters for firstname';
} else {
$firstname = strip_tags(trim(htmlspecialchars($_POST['firstname'])));
}
if (!isset($_POST['lastname']) || $_POST['lastname'] == '') {
$errors[] = 'Lastname cannot be left blank';
} elseif (!preg_match("/[a-zA-Z]/", $_POST['lastname'])) {
$errors[] = 'Invalid lastname';
} else {
$lastname = strip_tags(trim(htmlspecialchars($_POST['lastname'])));
}
if (isset($_POST['middlename']) && $_POST['middlename'] !== ""){
if(!preg_match("/[a-zA-Z]/", $_POST['middlename'])) {
$errors[] = 'Invalid middle name';
}else{
$middlename = strip_tags(trim(htmlspecialchars($_POST['middlename'])));
}
}else{
$middlename = "";
}
if (!isset($_POST['password']) || $_POST['password'] == '') {
$errors[] = 'Password is required';
} elseif (strlen($_POST['password']) < 6) {
$errors[] = 'Password must be six(6) or more characters';
} else if (!preg_match("/[a-z]/", $_POST['password']) || !preg_match("/[A-Z]/", $_POST['password']) || !preg_match("/[0-9]/", $_POST['password'])) {
$errors[] = "Password must contain uppercase, lowercase and number(s)";
} else {
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
}
if (!isset($_POST['phone']) || $_POST['phone'] == '') {
$errors[] = 'Please provide a phone number';
} elseif (strlen($_POST['phone']) < 10) {
$errors[] = 'Invalid phone number';
} else {
$phone = strip_tags(trim(htmlspecialchars($_POST['phone'])));
}
$added = date('Y-m-d h:i:sa');
if (!$errors) {
$conn = get_connection_handle();
$stmt = $conn->prepare("INSERT INTO admins(username, firstname,
middlename, lastname, email, phone, password, added_on) VALUES(?,?,?,?,?,?,?,?)");
$stmt->bind_param('ssssssss', $username, $firstname, $middlename,
$lastname, $email, $phone, $password, $added);
$stmt->execute();
echo $conn->error;
$row = $conn->affected_rows;
if ($row == 1) {
if(!file_exists("admins/$username/")){
mkdir("admins/$username/", 0777, true);
}
$msg = "User has been created";
echo($msg);
} else {
echo "Cannot create user $username" . $conn->error;
}
} else {
foreach($errors as $error){
echo($error."<br>");
}
}
}elseif(isset($_POST['user_id'])){
$errors = array();
$msg = "";
if(!is_post("user_id")){
$errors[] = 'Your ID is required';
} else{
$user_id = strip_tags(trim(htmlspecialchars($_POST['user_id'])));
}
if(!is_post("password")){
$errors[] = 'Password is required';
}else{
$password = strip_tags(trim(htmlspecialchars($_POST['password'])));
}
if(!$errors){
$conn = get_connection_handle();
$query = $conn->query("SELECT * FROM users WHERE user_id = '$user_id' or username = '$user_id'");
$qry = $conn->query("SELECT * FROM admins WHERE username = '$user_id'");
if($query->num_rows == 1){
$r = $query->fetch_assoc();
if(password_verify($password, $r['password'])){
if(!session_id())
session_start();
$_SESSION["user_id"] = $user_id;
$_SESSION['user_type'] = "Normal";
$_SESSION["username"] = $r['username'];
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
$_SESSION['email'] = $r['email'];
$_SESSION['id'] = $r['id'];
echo "Login successful";
}else{
$msg = "Invalid ID and/or password";
echo $msg;
}
}elseif($qry->num_rows == 1){
$r = $qry->fetch_assoc();
if(password_verify($password, $r['password'])){
if(!session_id())
session_start();
$_SESSION["user_type"] = "Admin";
$_SESSION["username"] = $r['username'];
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
$_SESSION['email'] = $r['email'];
$_SESSION['id'] = $r['id'];
echo "Login successful";
}else{
$msg = "Invalid ID and/or password";
echo $msg;
}
}else{
$msg = "Invalid ID and/or password";
echo $msg;
}
}else{
$msg = implode("<br>", $errors);
echo $msg;
}
}