|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace JSKOS; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\RequestInterface; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Http\Message\ResponseFactory; |
|
8
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Psr\Log\NullLogger; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A JSKOS Server. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Server implements \Psr\Log\LoggerAwareInterface |
|
16
|
|
|
{ |
|
17
|
|
|
protected $service; |
|
18
|
|
|
protected $logger; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct( |
|
21
|
|
|
Service $service, |
|
22
|
|
|
ResponseFactory $responseFactory=null, |
|
23
|
|
|
LoggerInterface $logger=null |
|
24
|
|
|
) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->service = $service; |
|
27
|
|
|
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); |
|
|
|
|
|
|
28
|
|
|
$this->logger = $logger ?: new NullLogger(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function setLogger(LoggerInterface $logger) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->logger = $logger; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function parseRequest(RequestInterface $request): array |
|
37
|
|
|
{ |
|
38
|
|
|
$uri = $request->getUri(); |
|
39
|
|
|
$path = $uri->getPath(); |
|
40
|
|
|
$query = []; |
|
41
|
|
|
parse_str($uri->getQuery(), $query); |
|
42
|
|
|
|
|
43
|
|
|
$callback = null; |
|
44
|
|
|
if (isset($query['callback'])) { |
|
45
|
|
|
if (preg_match('/^[$A-Z_][0-9A-Z_$.]*$/i', $query['callback'])) { |
|
46
|
|
|
$callback = $query['callback']; |
|
47
|
|
|
} |
|
48
|
|
|
unset($query['callback']); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
# TODO: get language parameter from headers |
|
52
|
|
|
|
|
53
|
|
|
return [$query, $path, $callback]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function query(RequestInterface $request): ResponseInterface |
|
57
|
|
|
{ |
|
58
|
|
|
$method = $request->getMethod(); |
|
59
|
|
|
|
|
60
|
|
|
$result = null; |
|
61
|
|
|
$callback = null; |
|
62
|
|
|
|
|
63
|
|
|
if ($method == 'GET' or $method == 'HEAD') { |
|
|
|
|
|
|
64
|
|
|
list ($query, $path, $callback) = $this->parseRequest($request); |
|
65
|
|
|
|
|
66
|
|
|
# TODO: detect conflicting parameters? |
|
67
|
|
|
# if (isset($params['uri']) and isset($params['search'])) { |
|
|
|
|
|
|
68
|
|
|
# $error = new Error(422, 'request_error', 'Conflicting request parameters uri & search'); |
|
|
|
|
|
|
69
|
|
|
# } |
|
70
|
|
|
|
|
71
|
|
|
try { |
|
72
|
|
|
$result = $this->service->query($query, $path); |
|
73
|
|
|
} catch(Error $e) { |
|
74
|
|
|
$error = $e; |
|
75
|
|
|
} |
|
76
|
|
|
# TODO: catch other kinds of errors: |
|
77
|
|
|
# } catch (\Exception $e) { |
|
|
|
|
|
|
78
|
|
|
# $this->logger->error('Service Exception', ['exception' => $e]); |
|
|
|
|
|
|
79
|
|
|
# $error = new Error(500, 'Internal server error'); |
|
|
|
|
|
|
80
|
|
|
} elseif ($request->getMethod() == 'OPTIONS') { |
|
81
|
|
|
return $this->optionsResponse(); |
|
82
|
|
|
} else { |
|
83
|
|
|
$error = new Error(405, 'Method not allowed'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
if ($result) { |
|
88
|
|
|
$code = 200; |
|
89
|
|
|
$headers = [ |
|
90
|
|
|
'Access-Control-Allow-Origin' => '*', |
|
91
|
|
|
'Content-Type' => 'application/json; charset=UTF-8', |
|
92
|
|
|
'X-Total-Count' => $result->getTotalCount() |
|
93
|
|
|
]; |
|
94
|
|
|
$body = $result->json(); |
|
95
|
|
|
} else { |
|
96
|
|
|
$code = $error->code; |
|
|
|
|
|
|
97
|
|
|
$headers = [ |
|
98
|
|
|
'Access-Control-Allow-Origin' => '*', |
|
99
|
|
|
'Content-Type' => 'application/json; charset=UTF-8', |
|
100
|
|
|
]; |
|
101
|
|
|
$body = $error->json(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$headers['Content-Length'] = strlen($body); |
|
105
|
|
|
if ($method == 'HEAD') { |
|
106
|
|
|
$body = ''; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ($callback) { |
|
110
|
|
|
$body = "/**/$callback($body);"; |
|
111
|
|
|
$headers['Content-Type'] = 'application/javascript; charset=UTF-8'; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this->responseFactory->createResponse($code, null, $headers, $body); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
public function optionsResponse() |
|
119
|
|
|
{ |
|
120
|
|
|
$headers = [ |
|
121
|
|
|
'Access-Control-Allow-Methods' => 'GET, HEAD, OPTIONS', |
|
122
|
|
|
]; |
|
123
|
|
|
|
|
124
|
|
|
# TODO: |
|
125
|
|
|
# if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) && |
|
|
|
|
|
|
126
|
|
|
# $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'GET') { |
|
|
|
|
|
|
127
|
|
|
# $response->headers['Access-Control-Allow-Origin'] = '*'; |
|
|
|
|
|
|
128
|
|
|
# $response->headers['Acess-Control-Expose-Headers'] = 'Link, X-Total-Count'; |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
return $this->responseFactory->createResponse(200, null, $headers, ''); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* TODO: Extract requested languages(s) from request. |
|
135
|
|
|
public function extractRequestLanguage($params) |
|
136
|
|
|
{ |
|
137
|
|
|
$language = null; |
|
138
|
|
|
|
|
139
|
|
|
# get query modifier: language |
|
140
|
|
|
if (isset($params['language'])) { |
|
141
|
|
|
$language = $params['language']; |
|
142
|
|
|
unset($params['language']); |
|
143
|
|
|
# TODO: parse language |
|
144
|
|
|
} elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
|
145
|
|
|
# parse accept-language-header |
|
146
|
|
|
preg_match_all( |
|
147
|
|
|
'/([a-z]+(?:-[a-z]+)?)\s*(?:;\s*q\s*=\s*(1|0?\.[0-9]+))?/i', |
|
148
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'], |
|
149
|
|
|
$match); |
|
150
|
|
|
if (count($match[1])) { |
|
151
|
|
|
foreach ($match[1] as $i => $l) { |
|
152
|
|
|
if (isset($match[2][$i]) && $match[2][$i] != '') { |
|
153
|
|
|
$langs[strtolower($l)] = (float) $match[2][$i]; |
|
154
|
|
|
} else { |
|
155
|
|
|
$langs[strtolower($l)] = 1; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
arsort($langs, SORT_NUMERIC); |
|
159
|
|
|
reset($langs); |
|
160
|
|
|
$language = key($langs); # most wanted language |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return $language; |
|
165
|
|
|
} |
|
166
|
|
|
*/ |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Utility function to emit a Response without additional framework. |
|
170
|
|
|
*/ |
|
171
|
|
|
public static function sendResponse(ResponseInterface $response) |
|
172
|
|
|
{ |
|
173
|
|
|
$code = $response->getStatusCode(); |
|
174
|
|
|
$reason = $response->getReasonPhrase(); |
|
175
|
|
|
header( |
|
176
|
|
|
sprintf('HTTP/%s %s %s', $response->getProtocolVersion(), $code, $reason), |
|
177
|
|
|
true, $code |
|
178
|
|
|
); |
|
179
|
|
|
|
|
180
|
|
|
foreach ($response->getHeaders() as $header => $values) { |
|
181
|
|
|
foreach ($values as $value) { |
|
182
|
|
|
header("$header: $value", false); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
echo $response->getBody(); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: