forked from luutruong/XF2_Api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListener.php
140 lines (118 loc) · 3.87 KB
/
Listener.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
<?php
namespace Truonglv\Api;
use XF\Container;
use XF\Entity\ApiKey;
use XF\Http\ResponseFile;
use XF\Http\ResponseStream;
use Truonglv\Api\Entity\Log;
use Truonglv\Api\Http\Request;
use Truonglv\Api\Entity\AccessToken;
use XF\Api\Controller\AbstractController;
class Listener
{
/**
* @param \XF\Api\App $app
* @return void
*/
public static function appApiSetup(\XF\Api\App $app)
{
$app->container()->set('request', function (Container $c) {
$request = new Http\Request($c['inputFilterer']);
$request->setCookiePrefix($c['config']['cookie']['prefix']);
return $request;
});
}
/**
* @param \XF\Http\Request $request
* @param mixed $result
* @param mixed $error
* @param mixed $code
* @return void
*/
public static function appApiValidateRequest(\XF\Http\Request $request, &$result, &$error, &$code)
{
$requestApiKey = $request->getServer('HTTP_XF_TAPI_KEY');
if (strlen($requestApiKey) === 0) {
return;
}
$app = \XF::app();
$ourKey = $app->options()->tApi_apiKey;
/** @var ApiKey|null $apiKeyEntity */
$apiKeyEntity = $app->em()->find('XF:ApiKey', $ourKey['apiKeyId']);
if ($apiKeyEntity === null) {
$error = 'api_error.api_key_not_found';
$code = 401;
$result = false;
return;
}
/** @var Request $ourRequest */
$ourRequest = $request;
$ourRequest->setApiKey($apiKeyEntity->api_key);
$ourRequest->setApiUser(0);
$accessToken = $request->getServer(App::HEADER_KEY_ACCESS_TOKEN);
if ($accessToken !== false) {
/** @var AccessToken|null $token */
$token = $app->finder('Truonglv\Api:AccessToken')
->where('token', $accessToken)
->whereOr([
['expire_date', '=', 0],
['expire_date', '>', \XF::$time]
])
->fetchOne();
if ($token !== null) {
$ourRequest->setApiUser($token->user_id);
}
}
}
/**
* @param \XF\Mvc\Controller $controller
* @param mixed $action
* @param \XF\Mvc\ParameterBag $params
* @return void
*/
public static function onControllerPreDispatch(\XF\Mvc\Controller $controller, $action, \XF\Mvc\ParameterBag $params)
{
if (!$controller instanceof AbstractController) {
return;
}
App::$enableLogging = App::isRequestFromApp($controller->request());
}
/**
* @param \XF\Api\App $app
* @param \XF\Http\Response $response
* @throws \XF\PrintableException
* @return void
*/
public static function onAppApiComplete(\XF\Api\App $app, \XF\Http\Response &$response)
{
if (!App::$enableLogging) {
return;
}
$request = $app->request();
/** @var \Truonglv\Api\Repository\Log $logRepo */
$logRepo = $app->repository('Truonglv\Api:Log');
/** @var Log $log */
$log = $app->em()->create('Truonglv\Api:Log');
$log->user_id = \XF::visitor()->user_id;
$log->app_version = $request->getServer(App::HEADER_KEY_APP_VERSION);
$log->end_point = $request->getRequestUri();
$log->method = \strtoupper($request->getRequestMethod());
$post = $_POST;
if (isset($post['password'])) {
$post['password'] = '******';
}
$log->payload = [
'_POST' => $post
];
$log->response_code = $response->httpCode();
$body = $response->body();
if ($body instanceof ResponseFile
|| $body instanceof ResponseStream
) {
$log->response = '';
} else {
$log->response = trim($logRepo->prepareDataForLog($body));
}
$log->save();
}
}