1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DronePHP (http://www.dronephp.com) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
7
|
|
|
* @license http://www.dronephp.com/license |
8
|
|
|
* @author Darío Rivera <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Drone\Network\Rest; |
12
|
|
|
|
13
|
|
|
use Drone\Network\Http; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Rest class |
17
|
|
|
* |
18
|
|
|
* Abstract class for REST. Basic and Digest muest be implemented. |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractRest |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Description of the protected area |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $realm; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* White list for authentication |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $whiteList; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The username credential |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $username; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Server response |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $response; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* HTTP instance |
52
|
|
|
* |
53
|
|
|
* @var Http |
54
|
|
|
*/ |
55
|
|
|
protected $http; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the response attribute |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getRealm() |
63
|
|
|
{ |
64
|
|
|
return $this->realm; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns the white list for authentication |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function getWhiteList() |
73
|
|
|
{ |
74
|
|
|
return $this->whiteList; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the username credential |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getUsername() |
83
|
|
|
{ |
84
|
|
|
return $this->username; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the response attribute |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getResponse() |
93
|
|
|
{ |
94
|
|
|
return $this->response; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the HTTP instance |
99
|
|
|
* |
100
|
|
|
* @return Http |
101
|
|
|
*/ |
102
|
|
|
public function getHttp() |
103
|
|
|
{ |
104
|
|
|
return $this->http; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets realm attribute |
109
|
|
|
* |
110
|
|
|
* @param string $realm |
111
|
|
|
* |
112
|
|
|
* @return null |
113
|
|
|
*/ |
114
|
|
|
public function setRealm($realm) |
115
|
|
|
{ |
116
|
|
|
$this->realm = $realm; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Sets a white list for authentication |
121
|
|
|
* |
122
|
|
|
* @param array $whiteList |
123
|
|
|
* |
124
|
|
|
* @throws RuntimeException |
125
|
|
|
* |
126
|
|
|
* @return null |
127
|
|
|
*/ |
128
|
|
|
public function setWhiteList(array $whiteList) |
129
|
|
|
{ |
130
|
|
|
if (empty($whiteList)) { |
131
|
|
|
throw new \RuntimeException("Empty whitelist!"); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->whiteList = $whiteList; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Sets the HTTP instance |
139
|
|
|
* |
140
|
|
|
* @param Http $http |
141
|
|
|
* |
142
|
|
|
* @return null |
143
|
|
|
*/ |
144
|
|
|
public function setHttp($http) |
145
|
|
|
{ |
146
|
|
|
$this->http = $http; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* REST Constructor |
151
|
|
|
* |
152
|
|
|
* All modifiable attributes (i.e. with setter method) can be passed as key |
153
|
|
|
* |
154
|
|
|
* @param array $options |
155
|
|
|
*/ |
156
|
|
|
public function __construct($options) |
157
|
|
|
{ |
158
|
|
|
foreach ($options as $option => $value) { |
159
|
|
|
if (property_exists(__CLASS__, strtolower($option)) && method_exists($this, 'set'.$option)) { |
160
|
|
|
$this->{'set'.$option}($value); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
# HTTP instance |
165
|
|
|
$this->http = new Http(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Requests client authentication |
170
|
|
|
* |
171
|
|
|
* @return boolean |
172
|
|
|
*/ |
173
|
|
|
abstract public function request(); |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Checks credentials |
177
|
|
|
* |
178
|
|
|
* @return boolean |
179
|
|
|
*/ |
180
|
|
|
abstract public function authenticate(); |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Writes the response |
184
|
|
|
* |
185
|
|
|
* @return boolean |
186
|
|
|
*/ |
187
|
|
|
abstract public function response(); |
188
|
|
|
} |
189
|
|
|
|