-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
63 lines (48 loc) · 1.96 KB
/
api.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
<?php
require_once("class.rest.php");
class API extends REST {
public function __construct() {
parent::__construct();
$this->dbConnect();
}
public function dbConnect() {
mysql_connect("localhost", "root", "");
mysql_select_db("vipul_test");
}
public function processApi() {
$func = strtolower(trim(str_replace("/", "", $_REQUEST['action'])));
if ((int) method_exists($this, $func) > 0)
$this->$func();
else
$this->response('', 404);
}
private function login() {
if ($this->get_request_method() != "POST") {
$this->response($this->json(array('status' => 'false', 'message' => 'method not allowed.')), 405);
}
if (isset($this->_request['email']) && isset($this->_request['password']) && !empty($this->_request['email']) && !empty($this->_request['password'])) {
$email = $this->_request['email'];
$password = $this->_request['password'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$sql = "SELECT * FROM user WHERE email='" . $email . "' AND password='" . $password . "'";
$q = mysql_query($sql);
if (mysql_num_rows($q) > 0) {
$res = array('status' => 'true', 'message' => 'user successfully logged in.');
$this->response($this->json($res), 200);
}
$res = array('status' => 'false', 'message' => 'wrong email or password.');
$this->response($this->json($res), 404);
}
}
$error = array('status' => 'false', 'message' => 'Invalid email or password');
$this->response($this->json($error), 200);
}
private function json($data) {
if (is_array($data)) {
return json_encode($data);
}
}
}
$api = new API;
$api->processApi();
?>