|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace fkooman\VPN\Server\Api; |
|
19
|
|
|
|
|
20
|
|
|
use fkooman\Http\Request; |
|
21
|
|
|
use fkooman\Rest\Service; |
|
22
|
|
|
use fkooman\Rest\ServiceModuleInterface; |
|
23
|
|
|
use fkooman\Http\Exception\BadRequestException; |
|
24
|
|
|
use fkooman\VPN\Server\InputValidation; |
|
25
|
|
|
use fkooman\Rest\Plugin\Authentication\Bearer\TokenInfo; |
|
26
|
|
|
use DateTime; |
|
27
|
|
|
use fkooman\VPN\Server\ConnectionLog; |
|
28
|
|
|
use fkooman\VPN\Server\ApiResponse; |
|
29
|
|
|
|
|
30
|
|
|
class LogModule implements ServiceModuleInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var ConnectionLog */ |
|
33
|
|
|
private $connectionLog; |
|
34
|
|
|
|
|
35
|
|
|
/** @var \DateTime */ |
|
36
|
|
|
private $dateTime; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(ConnectionLog $connectionLog, DateTime $dateTime = null) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->connectionLog = $connectionLog; |
|
41
|
|
|
if (null === $dateTime) { |
|
42
|
|
|
$dateTime = new DateTime(); |
|
43
|
|
|
} |
|
44
|
|
|
$this->dateTime = $dateTime; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function init(Service $service) |
|
48
|
|
|
{ |
|
49
|
|
|
$service->get( |
|
50
|
|
|
'/log/:showDate', |
|
51
|
|
|
function (Request $request, TokenInfo $tokenInfo, $showDate) { |
|
52
|
|
|
$tokenInfo->getScope()->requireScope(['admin']); |
|
53
|
|
|
|
|
54
|
|
|
InputValidation::date($showDate); |
|
55
|
|
|
|
|
56
|
|
|
$showDateUnix = strtotime($showDate); |
|
57
|
|
|
$minDate = strtotime('today -31 days', $this->dateTime->getTimeStamp()); |
|
58
|
|
|
$maxDate = strtotime('tomorrow', $this->dateTime->getTimeStamp()); |
|
59
|
|
|
|
|
60
|
|
|
if ($showDateUnix < $minDate || $showDateUnix >= $maxDate) { |
|
61
|
|
|
throw new BadRequestException('invalid date range'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$showDateUnixMin = strtotime('today', $showDateUnix); |
|
65
|
|
|
$showDateUnixMax = strtotime('tomorrow', $showDateUnix); |
|
66
|
|
|
|
|
67
|
|
|
return new ApiResponse('log', $this->connectionLog->getConnectionHistory($showDateUnixMin, $showDateUnixMax)); |
|
68
|
|
|
} |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|