-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_validation-mobile.php
140 lines (91 loc) · 3.07 KB
/
api_validation-mobile.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
<?php
$required_parameters = array("client_id", "signature", "timestamp", "member_id");
$required_data = array("client_id","timestamp", "member_id");
function DBConnection() {
//diambil dari config.php
$db = $GLOBALS['config']['db'];
return new PDO("mysql:host=" . $db['servername'] . ";dbname=" . $db['dbname'], $db['username'], $db['password']);
}
function request_parameter_validation($request_parameters) {
$is_exist = TRUE;
foreach ($GLOBALS['required_parameters'] as $value) {
if (!array_key_exists($value, $request_parameters)) {
$is_exist = FALSE;
}
}
return $is_exist;
}
function client_id_validation($client_id) {
$is_exist = FALSE;
try {
$conn = DBConnection();
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $conn->query("SELECT * FROM clients WHERE client_id = '".$client_id."' AND (end_time IS NULL OR end_time > CURDATE())");
$result = $query->fetch();
if (count($result) > 1) {
$is_exist = TRUE;
}
$conn = null;
} catch (\PDOException $ex) {
echo "CLIENT ID VALIDATION. ERROR : " . $ex->getMessage();
}
return $is_exist;
}
function get_secret_key_by_client_id($client_id) {
$secret_key = "";
try {
$conn = DBConnection();
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $conn->query("SELECT * FROM clients WHERE client_id = '".$client_id."'");
$result = $query->fetch(PDO::FETCH_ASSOC);
$secret_key = $result['secret_key'];
$conn = null;
} catch (\PDOException $ex) {
echo "GET SECRET KEY BY CLIENT ID. ERROR : " . $ex->getMessage();
}
return $secret_key;
}
function insert_data($sql_query) {
try {
$conn = DBConnection();
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec($sql_query);
$conn = null;
} catch (\PDOException $ex) {
echo "SAVE DATA. ERROR : " . $ex->getMessage();
}
}
function signature_validation ($request_parameters) {
$is_valid = FALSE;
$padding = "";
$string_data = "";
foreach ($GLOBALS['required_data'] as $value) {
if (array_key_exists($value, $request_parameters)) {
$string_data .= $value . "=" . $request_parameters[$value] . "&";
}
}
for ($i = strlen($string_data)%32; $i<32; $i++) {
$padding .= '0';
}
$secret_key = get_secret_key_by_client_id($request_parameters['client_id']);
$new_signature = md5($secret_key . $padding . $string_data . $secret_key);
$old_signature = $request_parameters['signature'];
if ($old_signature == $new_signature) {
$is_valid = TRUE;
}
return $is_valid;
}
function timestamp_validation($timestamp) {
$is_valid = FALSE;
$minutes = 30;
$api_timestamp = new DateTime($timestamp);
$add_current_datetime = new DateTime();
$sub_current_datetime = new DateTime();
$add_current_datetime->add(new DateInterval('PT' . $minutes . 'M'));
$sub_current_datetime->sub(new DateInterval('PT' . $minutes . 'M'));
if ( ($api_timestamp > $sub_current_datetime) && ($api_timestamp < $add_current_datetime) ) {
$is_valid = TRUE;
}
return $is_valid;
}
?>