-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.php
More file actions
95 lines (77 loc) · 2.45 KB
/
Copy pathapp.php
File metadata and controls
95 lines (77 loc) · 2.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
<?php
namespace kr\bartnet;
class App {
private $params = array();
// constructor
public function __construct(){
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
}
// request OPTIONS
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
}
// print to json
public function print($arr, $code=200) {
http_response_code($code);
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
exit;
}
// get DATA
public function getData() {
return json_decode(file_get_contents('php://input'), true);
}
// get url parameter
public function getParams() {
return $this->params;
}
// process
private function route($pat) {
$pat = '~^'.$pat.'$~i';
$request = $_SERVER['REQUEST_URI'];
$ret = preg_match($pat, $request, $mat);
if (count($mat) > 1) {
array_shift($mat);
$this->params = $mat;
}
return $ret;
}
// check METHOD
private function checkMethod($method) {
$_method = strtolower($_SERVER["REQUEST_METHOD"]);
if ($_method !== $method) return false;
return true;
}
// GET
public function get($pat) {
if(!$this->checkMethod('get')) return false;
return $this->route($pat);
}
// POST
public function post($pat) {
if(!$this->checkMethod('post')) return false;
return $this->route($pat);
}
// PUT
public function put($pat) {
if(!$this->checkMethod('put')) return false;
return $this->route($pat);
}
// PATCH
public function patch($pat) {
if(!$this->checkMethod('patch')) return false;
return $this->route($pat);
}
// DELETE
public function delete($pat) {
if(!$this->checkMethod('delete')) return false;
return $this->route($pat);
}
}