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
|
|
|
$logger = null; |
47
|
|
|
if ($this->context->hasService('apiLogger')) { |
48
|
|
|
$logger = $this->context->getService('apiLogger'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// get handler |
52
|
|
|
$hand = $this->apiDecider->getApiHandler( |
53
|
|
|
$this->getRequest()->getMethod(), |
54
|
|
|
$this->params['version'], |
55
|
|
|
$this->params['package'], |
56
|
|
|
$this->params['apiAction'] |
57
|
|
|
); |
58
|
|
|
$handler = $hand['handler']; |
59
|
|
|
$authorization = $hand['authorization']; |
60
|
|
|
|
61
|
|
|
// check authorization |
62
|
|
|
if (!$authorization->authorized()) { |
63
|
|
|
$this->getHttpResponse()->setCode(Response::S403_FORBIDDEN); |
64
|
|
|
$this->sendResponse(new JsonResponse(['status' => 'error', 'message' => $authorization->getErrorMessage()])); |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// process params |
69
|
|
|
$paramsProcessor = new ParamsProcessor($handler->params()); |
70
|
|
|
if ($paramsProcessor->isError()) { |
71
|
|
|
$this->getHttpResponse()->setCode(Response::S500_INTERNAL_SERVER_ERROR); |
72
|
|
|
$this->sendResponse(new JsonResponse(['status' => 'error', 'message' => 'wrong input'])); |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
$params = $paramsProcessor->getValues(); |
76
|
|
|
|
77
|
|
|
// process handler |
78
|
|
|
try { |
79
|
|
|
$response = $handler->handle($params); |
80
|
|
|
$code = $response->getCode(); |
81
|
|
|
} catch (Exception $exception) { |
82
|
|
|
$response = new JsonApiResponse(500, ['status' => 'error', 'message' => 'Internal server error']); |
83
|
|
|
$code = $response->getCode(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$end = microtime(true); |
87
|
|
|
|
88
|
|
|
if ($logger) { |
89
|
|
|
$headers = []; |
90
|
|
|
if (function_exists('getallheaders')) { |
91
|
|
|
$headers = getallheaders(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$requestHeaders = ''; |
95
|
|
|
foreach ($headers as $key => $value) { |
96
|
|
|
$requestHeaders .= "$key: $value\n"; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$logger->log( |
100
|
|
|
$code, |
101
|
|
|
$this->getRequest()->getMethod(), |
102
|
|
|
$requestHeaders, |
103
|
|
|
filter_input(INPUT_SERVER, 'REQUEST_URI'), |
104
|
|
|
$this->ipDetector->getRequestIp(), |
105
|
|
|
filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'), |
106
|
|
|
($end-$start) * 1000 |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// output to nette |
111
|
|
|
$this->getHttpResponse()->setCode($code); |
112
|
|
|
$this->sendResponse($response); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|