-
Notifications
You must be signed in to change notification settings - Fork 2
/
ajaxfile.php
256 lines (210 loc) · 8.84 KB
/
ajaxfile.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
<?php
session_start();
include_once 'config.php';
function test_input($text){
$text = trim($text);
$text = strip_tags($text);
$text = stripslashes($text);
return $text;
}
if (isset($_POST['request'])) {
$request =preg_replace('#[^0-9]#','',$_POST['request']);
}
//SIGNUP REPORTER
if ($request == 1) {
$lastname = test_input($_POST["lastname"]);
$firstname = test_input($_POST["firstname"]);
$email = test_input($_POST["email"]);
$occupation = test_input($_POST["occupation"]);
$state = test_input($_POST["state"]);
$address = test_input($_POST["address"]);
$password = test_input($_POST["password"]);
$password = password_hash($password, PASSWORD_DEFAULT);
$user_type = 'reporter';
$status = 'active';
$date = date("Y-m-d H:i:s");
if ($lastname != "" && $firstname != "" && $email != "" && $password != "" && $occupation != "" && $state != "" && $address != "") {
//Select all responders in the same state as the reporter
$responder = 'responder';
$stmt = $conn->prepare("SELECT user_id FROM users WHERE user_type = ? AND status = ? AND state = ?");
$stmt->bind_param("sss", $responder, $status, $state);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($responder_id);
if($stmt->num_rows > 0 ) {
while($stmt->fetch()){
$responders[] = $responder_id;
//select all reporter assigned to each responder
$reporter = 'reporter';
$stmt1 = $conn->prepare("SELECT user_id FROM users WHERE assigned = ? AND user_type = ?");
$stmt1->bind_param("is", $responder_id, $reporter);
$stmt1->execute();
$stmt1->store_result();
$assigned = $stmt1->num_rows;
$assigned_reporters[] = $assigned;
}}else{
//If no responder in reporter's state, select all responders in the other states
$stmt = $conn->prepare("SELECT user_id FROM users WHERE user_type = ? AND status = ?");
$stmt->bind_param("ss", $responder, $status);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($responder_id);
if($stmt->num_rows > 0 ) {
while($stmt->fetch()){
$responders[] = $responder_id;
//select all reporter assigned to each responder
$reporter = 'reporter';
$stmt1 = $conn->prepare("SELECT user_id FROM users WHERE assigned = ? AND user_type = ?");
$stmt1->bind_param("is", $responder_id, $reporter);
$stmt1->execute();
$stmt1->store_result();
$assigned = $stmt1->num_rows;
$assigned_reporters[] = $assigned;
}}
}
//select the least number of reporter assigned to each responder
$least_assigned = min($assigned_reporters);
$key = array_search($least_assigned, $assigned_reporters);
//select the responder with the least reporter
$assigned_responder = $responders[$key];
//check for duplicate
$stmt = $conn->prepare("SELECT email FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0 ) {
echo json_encode( array("status" => 0, "message" => "Oop's! This email already exist") );
exit;
}else{
//insert reporter into database
$stmt = $conn->prepare("INSERT INTO users (assigned, lastname, firstname, email, occupation, state, address, password, status, user_type, date_reg) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("issssssssss", $assigned_responder, $lastname, $firstname, $email, $occupation, $state, $address, $password, $status, $user_type, $date);
if($stmt->execute()){
$user_id = $stmt->insert_id;
$subject = 'Welcome!';
$notification_slug = rand();
$body = '<p>Hello dear!</p><p>We are happy to have you. This platform will help you get help on any situation, make report as an eyewitness or a victim and get response from professional responders.</p><p>We hope you would make use of this opportunity to the fullest.</p><p>Thanks and best regards<br>SpeakUp Board of Director</p>';
$notification_status = 'unread';
$stmt = $conn->prepare("INSERT INTO notifications (user_id, subject, notification_slug, body, status, date_sent) VALUES ( ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("isssss", $user_id, $subject, $notification_slug, $body, $notification_status, $date);
$stmt->execute();
$_SESSION["user_id"] = $user_id;
$_SESSION["user_type"] = $user_type;
echo json_encode( array("status" => 1, "message" => "Signed up successfully!") );
exit;
}
}
}
}
//SIGNUP RESPONDER
if ($request == 2) {
$lastname = test_input($_POST["lastname"]);
$firstname = test_input($_POST["firstname"]);
$email = test_input($_POST["email"]);
$phone = test_input($_POST["phone"]);
$password = test_input($_POST["password"]);
$password = password_hash($password, PASSWORD_DEFAULT);
$gender = test_input($_POST["gender"]);
$dob = test_input($_POST["dob"]);
$state = test_input($_POST["state"]);
$address = test_input($_POST["address"]);
$occupation = test_input($_POST["occupation"]);
$organization = test_input($_POST["organization"]);
$position = test_input($_POST["position"]);
$cv = test_input($_FILES['cv']['name']);
$motive = test_input($_POST["motive"]);
$user_type = 'responder';
$date = date("Y-m-d H:i:s");
if ($lastname != "" && $firstname != "" && $email != "" && $phone != "" && $password != "" && $gender != "" && $dob != "" && $state != "" && $address != "" && $occupation != "" && $organization != "" && $position != "" && $cv != "" && $motive != "" ) {
$path = $_FILES["cv"]["name"];
$extension = pathinfo($path, PATHINFO_EXTENSION);
$file_name = $email.'_cv.'.$extension;
// image file directory
$target = "cv/".$file_name;
$file_type = array('docx','pdf','doc');
if(!in_array($extension,$file_type)){
echo json_encode( array("status" => 0,"message" => 'Please upload a word document or a pdf file') );
exit;
}
$stmt = $conn->prepare("SELECT email FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0 ) {
echo json_encode( array("status" => 0, "message" => "oop's! This email already exist") );
exit;
}else{
$status = 'reviewing';
$stmt = $conn->prepare("INSERT INTO users (lastname, firstname, email, phone, password, gender, status, user_type, dob, state, address, occupation, organization, position, cv, motive, date_reg) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssssssssssssss", $lastname, $firstname, $email, $phone, $password, $gender, $status, $user_type, $dob, $state, $address, $occupation, $organization, $position, $file_name, $motive, $date);
if($stmt->execute()){
$move= move_uploaded_file($_FILES['cv']['tmp_name'], $target);
echo json_encode( array("status" => 1, "message" => "Signed up successfully!") );
exit;
} }
}
}
//lOGIN USERS
if ($request == 3) {
$email = test_input($_POST["email"]);
$password = test_input($_POST["password"]);
if ($email != "" && $password != "") {
$stmt = $conn->prepare("SELECT user_id, password, status, user_type FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($user_id, $hash, $status, $user_type);
if($stmt->num_rows > 0 ) {
$stmt->fetch();
if ($status == 'active') {
if (password_verify($password, $hash)) {
$_SESSION["user_id"] = $user_id;
$_SESSION["user_type"] = $user_type;
echo json_encode( array("status" => 1, "user_type" => $user_type, "message" => 'Logged in successfully') );
exit();
}else{
echo json_encode( array("status" => 0, "message" => 'Invalid login credentials!') );
exit;
}
}else if($status == 'supended'){
echo json_encode( array("status" => 0, "message" => 'Sorry! This account has been suspended') );
exit;
}else{
echo json_encode( array("status" => 0, "message" => 'Invalid login credentials!') );
exit;
}
}else{
echo json_encode( array("status" => 5, "message" => 'Invalid login credentials!') );
exit;
}
}
}
//CHATBOT MESSAGES
if ($request == 4) {
$msg = test_input($_POST["msg"]);
$msg = "[[:<:]]".$msg."[[:>:]]";
$status = 0;
$html = '';
$stmt = $conn->prepare("SELECT answer FROM chatbot WHERE question RLIKE ? ");
$stmt->bind_param("s", $msg);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($answer);
if($stmt->num_rows > 0 ) {
while($stmt->fetch()){
$status = 1;
$html .= '<li class="by-me msg">
<div class="avatar pull-left">
<img class="bot-img" src="images/avatar.jpg" alt="" />
</div>
<div class="chat-content">
<div class="chat-meta">SpeakUp <span class="pull-right">'.date("h:i:s A").'</span></div>
'.$answer.'
<div class="clearfix"></div>
</div>
</li>';
}}
echo json_encode( array("status" => $status, "message" => $html) );
exit;
}
?>