|
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
|
|
|
public function __construct(array $serverData, array $getData, array $postData) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->serverData = $serverData; |
|
|
|
|
|
|
27
|
|
|
$this->getData = $getData; |
|
|
|
|
|
|
28
|
|
|
$this->postData = $postData; |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getRequestMethod() |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->serverData['REQUEST_METHOD']; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getServerName() |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->serverData['SERVER_NAME']; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getPathInfo() |
|
42
|
|
|
{ |
|
43
|
|
|
if (!array_key_exists('PATH_INFO', $this->serverData)) { |
|
44
|
|
|
return '/'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $this->serverData['PATH_INFO']; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getUrl() |
|
51
|
|
|
{ |
|
52
|
|
|
// deal with non-standard port |
|
53
|
|
|
// deal with non-existing request_scheme |
|
54
|
|
|
return sprintf( |
|
55
|
|
|
'%s://%s%s', |
|
56
|
|
|
$this->serverData['REQUEST_SCHEME'], |
|
57
|
|
|
$this->serverData['SERVER_NAME'], |
|
58
|
|
|
$this->serverData['REQUEST_URI'] |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
View Code Duplication |
public function getQueryParameter($key, $defaultValue = null) |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
if (array_key_exists($key, $this->getData)) { |
|
65
|
|
|
return $this->getData[$key]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (is_null($defaultValue)) { |
|
69
|
|
|
throw new HttpException( |
|
70
|
|
|
sprintf('missing query parameter "%s"', $key), |
|
71
|
|
|
400 |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $defaultValue; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
View Code Duplication |
public function getHeader($key, $defaultValue = null) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
// do some header key normalization |
|
81
|
|
|
if (array_key_exists($key, $this->serverData)) { |
|
82
|
|
|
return $this->serverData[$key]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (is_null($defaultValue)) { |
|
86
|
|
|
throw new HttpException( |
|
87
|
|
|
sprintf('missing header "%s"', $key), |
|
88
|
|
|
400 |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $defaultValue; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
public function getPostParameter($key, $defaultValue = null) |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
if (array_key_exists($key, $this->postData)) { |
|
98
|
|
|
return $this->postData[$key]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (is_null($defaultValue)) { |
|
102
|
|
|
throw new HttpException( |
|
103
|
|
|
sprintf('missing post parameter "%s"', $key), |
|
104
|
|
|
400 |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $defaultValue; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: