-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
410 lines (228 loc) · 9.45 KB
/
api.php
File metadata and controls
410 lines (228 loc) · 9.45 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
/*
****************************************
******************************
******************
APIs For Prep App by Niks.
******************
******************************
****************************************
*/
set_time_limit(0);
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
date_default_timezone_set('Asia/Kolkata');
$current_time = date('Y-m-d H:i:s');
require_once('include/config.php');
function secureCode($key) {
return strtoupper($key .date('y') .date('s') .date('n') .bin2hex(random_bytes(2)));
}
function encrypt($key) {
$ciphering = "AES-128-CTR";
// Use OpenSSl Encryption method
$options = 0;
// Non-NULL Initialization Vector for encryption
$encryption_iv = '12e582122H232046';
// Store the encryption key
$encryption_key = "!Fdj65%dj#s^sdT#@mH";
// Use openssl_encrypt() function to encrypt the data
return openssl_encrypt($key, $ciphering,
$encryption_key, $options, $encryption_iv);
}
function decrypt($key) {
$ciphering = "AES-128-CTR";
// Use OpenSSl Encryption method
$options = 0;
// Non-NULL Initialization Vector for decryption
$decryption_iv = '12e582122H232046';
// Store the decryption key
$decryption_key = "!Fdj65%dj#s^sdT#@mH";
// Use openssl_decrypt() function to decrypt the data
return openssl_decrypt ($key, $ciphering,
$decryption_key, $options, $decryption_iv);
}
// Add New User
if(isset($_POST['AddNewUser']) === true) {
$c = SecureCode('user');
$username = $conn->real_escape_string(strip_tags($_POST['username']));
$name = $conn->real_escape_string(strip_tags($_POST['name']));
$passcode = $conn->real_escape_string(strip_tags($_POST['passcode']));
$securityque = $conn->real_escape_string(strip_tags($_POST['securityque']));
$securityans = $conn->real_escape_string(strip_tags($_POST['securityans']));
$passcode = encrypt($passcode);
$stmt = $conn -> prepare("INSERT INTO users (uid,name,user,identity,security,answer) VALUES (?,?,?,?,?,?)");
$stmt -> bind_param('ssssss',$c,$name,$username,$passcode,$securityque,$securityans);
if($stmt -> execute() === True) {
echo json_encode(array('newuser' => True ));
}else {
echo json_encode(array('newuser' => True));
}
$conn -> close();
}
// Login User
else if(isset($_POST['LoginUser']) === True) {
$username = $conn->real_escape_string(strip_tags($_POST['username']));
$passcode = $conn->real_escape_string(strip_tags($_POST['passcode']));
$result = $conn->query("SELECT `uid`,`identity`,`user` FROM users WHERE user = '$username'");
if($result -> num_rows > 0 ) {
$row = $result -> fetch_assoc();
$pass = decrypt($row['identity']);
if($passcode === $pass) {
$uid = $row['uid'];
$chk_user = $conn -> query("SELECT `status` FROM users WHERE uid = '$uid'");
$chk_detail = $chk_user -> fetch_assoc();
if ($chk_detail['status'] == 1) {
$response = array();
$response[] = array ('auth'=>'Login Success');
foreach($result as $q) {
$response[] = $q ;
}
echo json_encode($response);
}
else {
echo json_encode(array("auth" => "Your Account Disable By Admin, Contact us!"));
}
}
else {
echo json_encode(array("auth" => "password was wrong!"));
}
} else {
echo json_encode(array("auth" => "please try again"));
}
$conn -> close();
}
// Update User Profile
else if (isset($_POST['UpdProfile'])) {
$uid = $conn->real_escape_string(strip_tags($_POST['uid']));
$username = $conn->real_escape_string(strip_tags($_POST['username']));
$name = $conn->real_escape_string(strip_tags($_POST['name']));
if($uid.is_null(0) && $username.is_null(0) && $name.is_null(0) ) {
$stmt = $conn -> query("UPDATE users SET `name` = '$name' ,`user` = '$username' WHERE `uid` LIKE '$uid' ");
if($stmt === true) {
$response = array();
$result = $conn -> query("SELECT `uid`,`user`,`name`,`identity` FROM users WHERE `uid` = '$uid'");
$response[] = array('profile' => 'update');
foreach($result as $q) {
$response[] = $q ;
}
echo json_encode($response);
} else {
echo json_encode(array("profile" => 'fail_upd'));
}
}
else {
echo json_encode(array("profile" => 'Try Again!'));
}
}
// Change Password
else if(isset($_POST['ChngPassword'])) {
$uid = $conn->real_escape_string(strip_tags($_POST['uid']));
$oldpass = $conn->real_escape_string(strip_tags($_POST['oldpass']));
$newpass = $conn->real_escape_string(strip_tags($_POST['newpass']));
$result_chk_pwd = $conn -> query("SELECT identity FROM users WHERE uid LIKE '$uid'");
$row_pwd = $result_chk_pwd -> fetch_assoc();
$decrypt_oldpwd = decrypt($row_pwd['identity']);
if($decrypt_oldpwd == $oldpass) {
$encrypt_newpass = encrypt($newpass);
$stmt_updpass = $conn -> query("UPDATE users SET identity = '$encrypt_newpass' WHERE uid LIKE '$uid'");
if($stmt_updpass === TRUE) {
echo json_encode(array("password" =>'password changed'));
}
} else {
echo json_encode(array("password" =>'old password was wrong'));
}
}
// Fetch Test Questions
else if (isset($_POST['FetchQuestion']) === True) {
$limit = 2;
$category = $conn->real_escape_string(strtolower(strip_tags($_POST['category'])));
$mode = $conn->real_escape_string(strtolower(strip_tags($_POST['mode'])));
$response = array();
$result = $conn -> query("SELECT `qid`,`ques`,`optn1`,`optn2`,`optn3`,`optn4`,`ans` FROM questions WHERE `category` = '$category' AND `mode` = '$mode' AND `status` = 1 ORDER BY RAND() LIMIT $limit");
if($result -> num_rows >0) {
foreach($result as $q) {
$response[] = $q ;
}
echo json_encode($response);
}
}
// Fetch Learning Questions
else if (isset($_POST['FetchLearnQuestion']) === True) {
$limit = 4;
$page;
$category = $conn->real_escape_string(strtolower(strip_tags($_POST['category'])));
if(isset($_POST['page'])) {
$page = $_POST['page'];
} else {
$page = 1;
}
$start = ($page - 1) * $limit;
$response = array();
$result = $conn -> query("SELECT `id`,`ques`,`answer` FROM learn WHERE `category` = '$category' ORDER BY id DESC LIMIT $start,$limit ");
$result_count = $conn -> query("SELECT `id`,`ques`,`answer` FROM learn WHERE `category` = '$category' ");
$count = round($result_count -> num_rows / $limit) ;
if($result -> num_rows >0) {
foreach($result as $q) {
$response[] = $q ;
}
$response[] = array("total_page" => $count);
echo json_encode($response);
}
}
// Fetch User Quiz History
else if (isset($_POST['FetchHistory']) === True) {
$uid = $conn->real_escape_string(strtolower(strip_tags($_POST['uid'])));
$response = array();
$result = $conn -> query("SELECT `mode`,`skip`,`score`,`date` FROM history WHERE `uid` = '$uid'");
if($result -> num_rows >0) {
foreach($result as $q) {
$response[] = $q ;
}
echo json_encode($response);
} else {
echo json_encode(array("history" => "No Record Found"));
}
}
// Add User Quiz History
else if (isset($_POST['AddQuizHistory']) === True) {
$uid = $conn->real_escape_string(strtolower(strip_tags($_POST['uid'])));
$mode = $conn->real_escape_string(strtolower(strip_tags($_POST['mode'])));
$skip = $conn->real_escape_string(strtolower(strip_tags($_POST['skip'])));
$score = $conn->real_escape_string(strtolower(strip_tags($_POST['score'])));
$stmt = $conn -> prepare("INSERT INTO history (`uid`,`mode`,`skip`,`score`,`date`) VALUES (?,?,?,?,?)");
$stmt-> bind_param('sssss',$uid,$mode,$skip,$score,$current_time);
if($stmt -> execute() === True) {
echo json_encode(array("history" => "success"));
}else {
echo json_encode(array("history" => "fail"));
}
}
else {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Prep App </title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<!-- Libraries CSS -->
<link href="assets/css/animate.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="assets/css/particle_video_dark.css" rel="stylesheet">
</head>
<body >
<!-- Hero Section
================================================== -->
<section id="hero" class="hero-section-video-dark">
<div class="video-overlay"></div>
<!-- /.video-overlay -->
<div class="hero-video">
<video autoplay loop poster="assets/video/particle-video-dark.jpg" id="bgvid" muted playsinline>
<source src="assets/video/particle-video-dark.mp4" type="video/mp4">
<source src="assets/video/particle-video-dark.webm" type="video/webm">
</video>
</div>
</section>
</body>
</html>
<?php }