1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tomaj\NetteApi\Presenters; |
4
|
|
|
|
5
|
|
|
use Nette\Application\Responses\JsonResponse; |
6
|
|
|
use Nette\Application\UI\Presenter; |
7
|
|
|
use Nette\Http\Response; |
8
|
|
|
use Tomaj\NetteApi\ApiDecider; |
9
|
|
|
use Tomaj\NetteApi\Misc\IpDetectorInterface; |
10
|
|
|
use Tomaj\NetteApi\Params\ParamsProcessor; |
11
|
|
|
use Exception; |
12
|
|
|
use Tomaj\NetteApi\Response\JsonApiResponse; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @property-read \Nette\DI\Container $context |
16
|
|
|
*/ |
17
|
|
|
class ApiPresenter extends Presenter |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ApiDecider @inject |
21
|
|
|
*/ |
22
|
|
|
public $apiDecider; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var IpDetectorInterface @inject |
26
|
|
|
*/ |
27
|
|
|
public $ipDetector; |
28
|
|
|
|
29
|
|
|
public function startup() |
30
|
|
|
{ |
31
|
|
|
parent::startup(); |
32
|
|
|
$this->autoCanonicalize = false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Nette render default method |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function renderDefault() |
41
|
|
|
{ |
42
|
|
|
$start = microtime(true); |
43
|
|
|
|
44
|
|
|
$this->getHttpResponse()->addHeader('Access-Control-Allow-Origin', '*'); |
45
|
|
|
|
46
|
|
|
$hand = $this->getHandler(); |
47
|
|
|
$handler = $hand['handler']; |
48
|
|
|
$authorization = $hand['authorization']; |
49
|
|
|
|
50
|
|
|
if ($this->checkAuth($authorization) == false) { |
|
|
|
|
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$params = $this->processParams($handler); |
55
|
|
|
if ($params == false) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// process handler |
60
|
|
|
try { |
61
|
|
|
$response = $handler->handle($params); |
62
|
|
|
$code = $response->getCode(); |
63
|
|
|
} catch (Exception $exception) { |
64
|
|
|
$response = new JsonApiResponse(500, ['status' => 'error', 'message' => 'Internal server error']); |
65
|
|
|
$code = $response->getCode(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$end = microtime(true); |
69
|
|
|
|
70
|
|
|
if ($this->context->hasService('apiLogger')) { |
71
|
|
|
$this->logRequest($this->context->getService('apiLogger'), $code, $end-$start); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// output to nette |
75
|
|
|
$this->getHttpResponse()->setCode($code); |
76
|
|
|
$this->sendResponse($response); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function getHandler() |
80
|
|
|
{ |
81
|
|
|
return $this->apiDecider->getApiHandler( |
82
|
|
|
$this->getRequest()->getMethod(), |
83
|
|
|
$this->params['version'], |
84
|
|
|
$this->params['package'], |
85
|
|
|
$this->params['apiAction'] |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function checkAuth($authorization) |
90
|
|
|
{ |
91
|
|
View Code Duplication |
if (!$authorization->authorized()) { |
|
|
|
|
92
|
|
|
$this->getHttpResponse()->setCode(Response::S403_FORBIDDEN); |
93
|
|
|
$this->sendResponse(new JsonResponse(['status' => 'error', 'message' => $authorization->getErrorMessage()])); |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function processParams($handler) |
100
|
|
|
{ |
101
|
|
|
$paramsProcessor = new ParamsProcessor($handler->params()); |
102
|
|
View Code Duplication |
if ($paramsProcessor->isError()) { |
|
|
|
|
103
|
|
|
$this->getHttpResponse()->setCode(Response::S500_INTERNAL_SERVER_ERROR); |
104
|
|
|
$this->sendResponse(new JsonResponse(['status' => 'error', 'message' => 'wrong input'])); |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
return $paramsProcessor->getValues(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private function logRequest($logger, $code, $elapsed) |
111
|
|
|
{ |
112
|
|
|
$headers = []; |
113
|
|
|
if (function_exists('getallheaders')) { |
114
|
|
|
$headers = getallheaders(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$requestHeaders = ''; |
118
|
|
|
foreach ($headers as $key => $value) { |
119
|
|
|
$requestHeaders .= "$key: $value\n"; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$logger->log( |
123
|
|
|
$code, |
124
|
|
|
$this->getRequest()->getMethod(), |
125
|
|
|
$requestHeaders, |
126
|
|
|
filter_input(INPUT_SERVER, 'REQUEST_URI'), |
127
|
|
|
$this->ipDetector->getRequestIp(), |
128
|
|
|
filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'), |
129
|
|
|
($elapsed) * 1000 |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.