This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHub.php
254 lines (227 loc) · 8.08 KB
/
Hub.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
namespace Saft\Addition\Rest;
use Saft\Rdf\NodeUtils;
use Saft\Store\Store;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response;
/**
*
*/
class Hub
{
/**
* @var Store
*/
protected $store;
/**
* @param Store $store
*/
public function __construct(Store $store, NodeUtils $nodeUtils)
{
$this->nodeUtils = $nodeUtils;
$this->store = $store;
}
/**
* Validate given request. Specification is located under:
* http://safting.github.io/doc/restinterface/triplestore
*
* @param RequestInterface $request
* @return array|true Returns true, if request is valid, otherwise an array with keys 'message' and 'key' set.
*/
protected function checkRequest(RequestInterface $request)
{
/*
* Check method
*/
if ('GET' != $request->getMethod() && 'POST' != $request->getMethod()) {
// invalid method
return array('message' => 'php://temp', 'code' => 405);
}
/*
* Check HTTP headers
*/
$headers = $request->getHeaders();
/*
* check Accept
*/
if (isset($headers['Accept']) && is_array($headers['Accept'])) {
// no elements means error
if (0 == count($headers['Accept'])) {
return array(
'message' => 'Bad Request: Accept headers can not be empty.',
'code' => 400
);
}
// first entry must be a string
if (isset($headers['Accept'][0]) && false == is_string($headers['Accept'][0])) {
return array(
'message' => 'Bad Request: Accept headers must be a string.',
'code' => 400
);
}
}
/*
* Check parameter
*/
$serverParams = $request->getServerParams();
/*
* check s, p and o
*/
foreach (array('s', 'p', 'o') as $param) {
// check if s or p or o is set
if (false == isset($serverParams[$param])) {
return array(
'message' => 'Bad Request: Parameter '. $param .' not set.',
'code' => 400
);
}
// s or p or o must be * or an URI
if (isset($serverParams[$param])
&& ('*' != $serverParams[$param]
&& false == $this->nodeUtils->simpleCheckURI($serverParams[$param]))) {
return array(
'message' => 'Bad Request: Parameter '. $param .' is invalid. Must be * or an URI.',
'code' => 400
);
}
}
/*
* if o is set and not *, ot is mandatory (must be set to uri or literal)
*/
// check that ot is set
if (true == $this->nodeUtils->simpleCheckURI($serverParams['o'])
&& false === isset($serverParams['ot'])) {
return array(
'message' => 'Bad Request: Parameter o is an URI, so ot must be set.',
'code' => 400
);
}
// check that ot is either uri or literal
if (true == $this->nodeUtils->simpleCheckURI($serverParams['o'])
&& false == in_array($serverParams['ot'], array('literal', 'uri'))) {
return array(
'message' => 'Bad Request: Parameter ot is neither uri nor literal.',
'code' => 400
);
}
/*
* check optional parameters
*/
/*
* action - possible verbs: add, ask, count, delete, get
*/
if (isset($serverParams['action'])) {
// check for possible verbs
if (false == in_array($serverParams['action'], array('add', 'ask', 'count', 'delete', 'get'), true)) {
return array(
'message' =>
'Bad Request: Parameter action must be one of these verbs: add, ask, count, delete, get',
'code' => 400
);
}
}
/*
* case_insensitive - possible values are true or false
*/
if (isset($serverParams['case_insensitive'])) {
// check for possible verbs
if (false == in_array($serverParams['case_insensitive'], array('true', 'false', true, false), true)) {
return array(
'message' =>
'Bad Request: Parameter case_insensitive must be one of these verbs: true, false',
'code' => 400
);
}
}
/*
* graphUri - URI of the graph to execute the query on
*/
if (isset($serverParams['graphUri'])) {
// check for possible verbs
if (false == $this->nodeUtils->simpleCheckURI($serverParams['graphUri'])) {
return array(
'message' => 'Bad Request: Parameter graphUri must be an URI.',
'code' => 400
);
}
}
/*
* limit - must be an integer equal or higher than 0
*/
if (isset($serverParams['limit'])) {
// limit must be an integer
if (false == ctype_digit(ltrim((string)$serverParams['limit'], '-'))) {
return array(
'message' => 'Bad Request: Parameter limit is not an integer.',
'code' => 400
);
}
// limit must be equal or higher than 0
if (0 > (int)$serverParams['limit']) {
return array(
'message' => 'Bad Request: Parameter limit is not equal or higher than 0.',
'code' => 400
);
}
}
/*
* offset - must be an integer equal or higher than 0
*/
if (isset($serverParams['offset'])) {
// offset must be an integer
if (false == ctype_digit(ltrim((string)$serverParams['offset'], '-'))) {
return array(
'message' => 'Bad Request: Parameter offset is not an integer.',
'code' => 400
);
}
// offset must be equal or higher than 0
if (1 > (int)$serverParams['offset']) {
return array(
'message' => 'Bad Request: Parameter offset is not equal or higher than 1.',
'code' => 400
);
}
}
/*
* reasoning_on - possible values are true or false (as string or boolean)
*/
if (isset($serverParams['reasoning_on'])) {
// check for possible verbs
if (false == in_array($serverParams['reasoning_on'], array('true', 'false', true, false), true)) {
return array(
'message' =>
'Bad Request: Parameter reasoning_on must be one of these verbs: true, false',
'code' => 400
);
}
}
return true;
}
/**
* This function will compute a given request object and returns the response to return. To be
* compatible with different implementations, the given $request parameter must implement
* RequestInterface from PSR-7.
*
* @param RequestInterface $request
* @return ResponseInterface Instance of ResponseInterface which represents the response to return.
*/
public function computeRequest(RequestInterface $request)
{
/*
* Validate given request
*/
if (true === ($result = $this->checkRequest($request))) {
// if we reach that point, the given $request was considered valid.
return new Response('php://memory', 200);
/*
* invalid request given.
*/
} else {
$response = new Response('php://memory', $result['code']);
$response->getBody()->write($result['message']);
return $response;
}
}
}