-
Notifications
You must be signed in to change notification settings - Fork 0
/
easyRestAPI.php
123 lines (123 loc) · 3.46 KB
/
easyRestAPI.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
<?php
class easyR {
public $useFilter = FALSE;
public $saveLogs = FALSE;
public $defaultLogFile = 'easyR.log';
public $responses = array(
"OK" => array(
"code" => 0,
"message" => "Ok"
),
"SUCCESS" => array(
"code" => 1,
"message" => "Operation successful"
),
"ERROR" => array(
"code" => 3,
"message" => "An error occurred"
),
"INTERNAL-ERROR" => array(
"code" => 4,
"message" => "Internal error, try again later"
),
"AUTH-ERROR" => array(
"code" => 5,
"message" => "Authentication failed because of an uknown error, try again later"
),
"ACCESS-DENIED" => array(
"code" => 6,
"message" => "Access denied"
),
"FAILOR" => array(
"code" => 7,
"message" => "Operation failed"
),
"AUTH-FAILOR" => array(
"code" => 8,
"message" => "Authentication failed"
),
"BAD-REQUEST" => array(
"code" => 9,
"message" => "Bad request"
)
);
private $inputs = array();
public function __construct() {
foreach ($_POST as $key => $value) {
array_merge($inputs,
array(
$this->filter($key) => array(
"value" => $this->filter($value),
"method" => "POST"
)
)
);
}
foreach ($_GET as $key => $value) {
array_merge($inputs,
array(
$this->filter($key) => array(
"value" => $this->filter($value),
"method" => "GET"
)
)
);
}
$this->saveLogs();
}
private function filter($inp) {
if($this->useFilter === TRUE) return htmlspecialchars($inp);return $inp;
}
private function saveLogs() {
if($this->saveLogs === FALSE)
return FALSE;
$method = 'Uknown';
if( count($_POST) > 0 ) {
$method = 'POST';
}
if( count($_GET) > 0 ) {
if( $method == 'Uknow')
$method = 'GET';
else
$method .= ' and GET';
}
$fp = fopen($this->defaultLogFile, 'a');
fwrite($fp,
sprintf("Remote ip %s, %s , Method %s",
$_SERVER['REMOTE_ADDR'],
date('Y-m-d H:i:s'),
$method
)
);
fwrite($file, PHP_EOL);
fclose($fp);
}
public function response($res = '-', $type = 'text') {
switch ($type) {
case 'json':
header("Content-type:application/json");
break;
default:
header("Content-type:text/html; charset=UTF-8");
break;
}
if($res == '-' || is_numeric($res)) {
echo $res;
return true;
}
if(is_array($res)) {
echo json_decode($res);
return true;
}
if(isset($this->responses[$res])) {
echo json_decode($this->responses[$res]);
} else {
if($type == 'text')
echo $res;
else
echo json_decode($this->responses['INTERNAL-ERROR']);
}
return true;
}
}
?>