|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
namespace SURFnet\VPN\Server\Api; |
|
19
|
|
|
|
|
20
|
|
|
use SURFnet\VPN\Server\Api\Exception\HttpException; |
|
21
|
|
|
|
|
22
|
|
|
class Request |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var array */ |
|
25
|
|
|
private $serverData; |
|
26
|
|
|
|
|
27
|
|
|
/** @var array */ |
|
28
|
|
|
private $getData; |
|
29
|
|
|
|
|
30
|
|
|
/** @var array */ |
|
31
|
|
|
private $postData; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(array $serverData, array $getData, array $postData) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->serverData = $serverData; |
|
36
|
|
|
$this->getData = $getData; |
|
37
|
|
|
$this->postData = $postData; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getRequestMethod() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->serverData['REQUEST_METHOD']; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getServerName() |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->serverData['SERVER_NAME']; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getPathInfo() |
|
51
|
|
|
{ |
|
52
|
|
|
if (!array_key_exists('PATH_INFO', $this->serverData)) { |
|
53
|
|
|
return '/'; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->serverData['PATH_INFO']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getUrl() |
|
60
|
|
|
{ |
|
61
|
|
|
// deal with non-standard port |
|
62
|
|
|
// deal with non-existing request_scheme |
|
63
|
|
|
return sprintf( |
|
64
|
|
|
'%s://%s%s', |
|
65
|
|
|
$this->serverData['REQUEST_SCHEME'], |
|
66
|
|
|
$this->serverData['SERVER_NAME'], |
|
67
|
|
|
$this->serverData['REQUEST_URI'] |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getQueryParameter($key, $isRequired = true, $defaultValue = null) |
|
72
|
|
|
{ |
|
73
|
|
|
return self::getValueFromArray('query parameter', $this->getData, $key, $isRequired, $defaultValue); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getPostParameter($key, $isRequired = true, $defaultValue = null) |
|
77
|
|
|
{ |
|
78
|
|
|
return self::getValueFromArray('post parameter', $this->postData, $key, $isRequired, $defaultValue); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getHeader($key, $isRequired = true, $defaultValue = null) |
|
82
|
|
|
{ |
|
83
|
|
|
return self::getValueFromArray('header', $this->serverData, $key, $isRequired, $defaultValue); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private static function getValueFromArray($type, array $sourceData, $key, $isRequired, $defaultValue) |
|
87
|
|
|
{ |
|
88
|
|
|
if (array_key_exists($key, $sourceData)) { |
|
89
|
|
|
return $sourceData[$key]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if ($isRequired) { |
|
93
|
|
|
throw new HttpException( |
|
94
|
|
|
sprintf('missing required %s "%s"', $type, $key), |
|
95
|
|
|
400 |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $defaultValue; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|