|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace fkooman\VPN\Server\Log; |
|
4
|
|
|
|
|
5
|
|
|
use fkooman\Http\Request; |
|
6
|
|
|
use fkooman\Rest\Service; |
|
7
|
|
|
use fkooman\Rest\ServiceModuleInterface; |
|
8
|
|
|
use fkooman\VPN\Server\Utils; |
|
9
|
|
|
use fkooman\Http\Exception\BadRequestException; |
|
10
|
|
|
use fkooman\IO\IO; |
|
11
|
|
|
use fkooman\Http\JsonResponse; |
|
12
|
|
|
|
|
13
|
|
|
class LogModule implements ServiceModuleInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var ConnectionLog */ |
|
16
|
|
|
private $connectionLog; |
|
17
|
|
|
|
|
18
|
|
|
/** @var fkooman\IO\IO */ |
|
19
|
|
|
private $io; |
|
20
|
|
|
public function __construct(ConnectionLog $connectionLog, IO $io = null) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->connectionLog = $connectionLog; |
|
23
|
|
|
if (is_null($io)) { |
|
24
|
|
|
$io = new IO(); |
|
25
|
|
|
} |
|
26
|
|
|
$this->io = $io; |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function init(Service $service) |
|
30
|
|
|
{ |
|
31
|
|
|
$service->get( |
|
32
|
|
|
'/log/history', |
|
33
|
|
|
function (Request $request) { |
|
34
|
|
|
$showDate = $request->getUrl()->getQueryParameter('showDate'); |
|
35
|
|
|
if (is_null($showDate)) { |
|
36
|
|
|
$showDate = date('Y-m-d', $this->io->getTime()); |
|
37
|
|
|
} |
|
38
|
|
|
if (!is_string($showDate)) { |
|
39
|
|
|
$showDate = date('Y-m-d', $this->io->getTime()); |
|
40
|
|
|
} |
|
41
|
|
|
Utils::validateDate($showDate); |
|
42
|
|
|
$showDateUnix = strtotime($showDate); |
|
43
|
|
|
|
|
44
|
|
|
$minDate = strtotime('today -31 days'); |
|
45
|
|
|
$maxDate = strtotime('tomorrow'); |
|
46
|
|
|
|
|
47
|
|
|
if ($showDateUnix < $minDate || $showDateUnix >= $maxDate) { |
|
48
|
|
|
throw new BadRequestException('invalid date range'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$showDateUnixMin = strtotime('today', $showDateUnix); |
|
52
|
|
|
$showDateUnixMax = strtotime('tomorrow', $showDateUnix); |
|
53
|
|
|
|
|
54
|
|
|
$response = new JsonResponse(); |
|
55
|
|
|
if (is_null($this->connectionLog)) { |
|
56
|
|
|
$responseData = array( |
|
57
|
|
|
'ok' => false, |
|
58
|
|
|
'error' => 'unable to connect to log database', |
|
59
|
|
|
); |
|
60
|
|
|
} else { |
|
61
|
|
|
$responseData = array( |
|
62
|
|
|
'ok' => true, |
|
63
|
|
|
'history' => $this->connectionLog->getConnectionHistory($showDateUnixMin, $showDateUnixMax), |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
$response->setBody($responseData); |
|
67
|
|
|
|
|
68
|
|
|
return $response; |
|
69
|
|
|
} |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..