|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Basic SOAP Server to access and modify DataObject instances. |
|
4
|
|
|
* You can enable SOAP access on a DataObject by setting {@link DataObject::$api_access} to true. |
|
5
|
|
|
* This means that you'll also enable a RESTful API through {@link RestfulServer}. |
|
6
|
|
|
* |
|
7
|
|
|
* @todo Test relation methods |
|
8
|
|
|
*/ |
|
9
|
|
|
class SOAPModelAccess extends SapphireSoapServer |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
private static $methods = array( |
|
|
|
|
|
|
12
|
|
|
'getXML' => array( |
|
13
|
|
|
'class' => 'string', |
|
14
|
|
|
'id' => 'int', |
|
15
|
|
|
'relation' => 'string', |
|
16
|
|
|
'_returns' => 'string', |
|
17
|
|
|
), |
|
18
|
|
|
'getJSON' => array( |
|
19
|
|
|
'class' => 'string', |
|
20
|
|
|
'id' => 'int', |
|
21
|
|
|
'relation' => 'string', |
|
22
|
|
|
'_returns' => 'string', |
|
23
|
|
|
), |
|
24
|
|
|
'putXML' => array( |
|
25
|
|
|
'class' => 'string', |
|
26
|
|
|
'id' => 'int', |
|
27
|
|
|
'relation' => 'string', |
|
28
|
|
|
'data' => 'string', |
|
29
|
|
|
'username' => 'string', |
|
30
|
|
|
'password' => 'string', |
|
31
|
|
|
'_returns' => 'boolean', |
|
32
|
|
|
), |
|
33
|
|
|
'putJSON' => array( |
|
34
|
|
|
'class' => 'string', |
|
35
|
|
|
'id' => 'int', |
|
36
|
|
|
'relation' => 'string', |
|
37
|
|
|
'_returns' => 'boolean', |
|
38
|
|
|
), |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
public function Link($action = null) |
|
42
|
|
|
{ |
|
43
|
|
|
return Controller::join_links('soap/v1/', $action); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Used to emulate RESTful GET requests with XML data. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $class |
|
50
|
|
|
* @param Number $id |
|
51
|
|
|
* @param string $relation Relation name |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
View Code Duplication |
public function getXML($class, $id, $relation = false, $username = null, $password = null) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$this->authenticate($username, $password); |
|
58
|
|
|
|
|
59
|
|
|
$response = Director::test( |
|
60
|
|
|
$this->buildRestfulURL($class, $id, $relation, 'xml'), |
|
|
|
|
|
|
61
|
|
|
null, |
|
62
|
|
|
null, |
|
|
|
|
|
|
63
|
|
|
'GET' |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Used to emulate RESTful GET requests with JSON data. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $class |
|
73
|
|
|
* @param Number $id |
|
74
|
|
|
* @param string $relation Relation name |
|
75
|
|
|
* @param string $username |
|
76
|
|
|
* @param string $password |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
View Code Duplication |
public function getJSON($class, $id, $relation = false, $username = null, $password = null) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
$this->authenticate($username, $password); |
|
83
|
|
|
|
|
84
|
|
|
$response = Director::test( |
|
85
|
|
|
$this->buildRestfulURL($class, $id, $relation, 'json'), |
|
|
|
|
|
|
86
|
|
|
null, |
|
87
|
|
|
null, |
|
|
|
|
|
|
88
|
|
|
'GET' |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Used to emulate RESTful POST and PUT requests with XML data. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $class |
|
98
|
|
|
* @param Number $id |
|
99
|
|
|
* @param string $relation Relation name |
|
100
|
|
|
* @param array $data |
|
101
|
|
|
* @param string $username |
|
102
|
|
|
* @param string $password |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
View Code Duplication |
public function putXML($class, $id = false, $relation = false, $data, $username = null, $password = null) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
$this->authenticate($username, $password); |
|
109
|
|
|
|
|
110
|
|
|
$response = Director::test( |
|
111
|
|
|
$this->buildRestfulURL($class, $id, $relation, 'xml'), |
|
|
|
|
|
|
112
|
|
|
array(), |
|
113
|
|
|
null, |
|
|
|
|
|
|
114
|
|
|
($id) ? 'PUT' : 'POST', |
|
115
|
|
|
$data |
|
|
|
|
|
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Used to emulate RESTful POST and PUT requests with JSON data. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $class |
|
125
|
|
|
* @param Number $id |
|
126
|
|
|
* @param string $relation Relation name |
|
127
|
|
|
* @param array $data |
|
128
|
|
|
* @param string $username |
|
129
|
|
|
* @param string $password |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
View Code Duplication |
public function putJSON($class = false, $id = false, $relation = false, $data, $username = null, $password = null) |
|
|
|
|
|
|
134
|
|
|
{ |
|
135
|
|
|
$this->authenticate($username, $password); |
|
136
|
|
|
|
|
137
|
|
|
$response = Director::test( |
|
138
|
|
|
$this->buildRestfulURL($class, $id, $relation, 'json'), |
|
|
|
|
|
|
139
|
|
|
array(), |
|
140
|
|
|
null, |
|
|
|
|
|
|
141
|
|
|
($id) ? 'PUT' : 'POST', |
|
142
|
|
|
$data |
|
|
|
|
|
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Used to emulate RESTful DELETE requests. |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $class |
|
152
|
|
|
* @param Number $id |
|
153
|
|
|
* @param string $relation Relation name |
|
154
|
|
|
* @param string $username |
|
155
|
|
|
* @param string $password |
|
156
|
|
|
* |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
|
View Code Duplication |
public function deleteXML($class, $id, $relation = false, $username = null, $password = null) |
|
|
|
|
|
|
160
|
|
|
{ |
|
161
|
|
|
$this->authenticate($username, $password); |
|
162
|
|
|
|
|
163
|
|
|
$response = Director::test( |
|
164
|
|
|
$this->buildRestfulURL($class, $id, $relation, 'xml'), |
|
|
|
|
|
|
165
|
|
|
null, |
|
166
|
|
|
null, |
|
|
|
|
|
|
167
|
|
|
'DELETE' |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Used to emulate RESTful DELETE requests. |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $class |
|
177
|
|
|
* @param Number $id |
|
178
|
|
|
* @param string $relation Relation name |
|
179
|
|
|
* @param string $username |
|
180
|
|
|
* @param string $password |
|
181
|
|
|
* |
|
182
|
|
|
* @return string |
|
183
|
|
|
*/ |
|
184
|
|
View Code Duplication |
public function deleteJSON($class, $id, $relation = false, $username = null, $password = null) |
|
|
|
|
|
|
185
|
|
|
{ |
|
186
|
|
|
$this->authenticate($username, $password); |
|
187
|
|
|
|
|
188
|
|
|
$response = Director::test( |
|
189
|
|
|
$this->buildRestfulURL($class, $id, $relation, 'json'), |
|
|
|
|
|
|
190
|
|
|
null, |
|
191
|
|
|
null, |
|
|
|
|
|
|
192
|
|
|
'DELETE' |
|
193
|
|
|
); |
|
194
|
|
|
|
|
195
|
|
|
return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody(); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Faking an HTTP Basicauth login in the PHP environment |
|
200
|
|
|
* that RestfulServer can pick up. |
|
201
|
|
|
* |
|
202
|
|
|
* @param string $username Username |
|
203
|
|
|
* @param string $password Plaintext password |
|
204
|
|
|
*/ |
|
205
|
|
|
protected function authenticate($username, $password) |
|
|
|
|
|
|
206
|
|
|
{ |
|
207
|
|
|
if (is_string($username)) { |
|
208
|
|
|
$_SERVER['PHP_AUTH_USER'] = $username; |
|
209
|
|
|
} |
|
210
|
|
|
if (is_string($password)) { |
|
211
|
|
|
$_SERVER['PHP_AUTH_PW'] = $password; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param string $class |
|
217
|
|
|
* @param Number $id |
|
218
|
|
|
* @param string $relation |
|
219
|
|
|
* @param string $extension |
|
220
|
|
|
* |
|
221
|
|
|
* @return string |
|
222
|
|
|
*/ |
|
223
|
|
|
protected function buildRestfulURL($class, $id, $relation, $extension) |
|
224
|
|
|
{ |
|
225
|
|
|
$url = "api/v1/{$class}"; |
|
226
|
|
|
if ($id) { |
|
227
|
|
|
$url .= "/{$id}"; |
|
228
|
|
|
} |
|
229
|
|
|
if ($relation) { |
|
230
|
|
|
$url .= "/{$relation}"; |
|
231
|
|
|
} |
|
232
|
|
|
if ($extension) { |
|
233
|
|
|
$url .= "/.{$extension}"; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return $url; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param SS_HTTPResponse $response |
|
241
|
|
|
* |
|
242
|
|
|
* @return string XML string containing the HTTP error message |
|
243
|
|
|
*/ |
|
244
|
|
|
protected function getErrorMessage($response) |
|
245
|
|
|
{ |
|
246
|
|
|
return '<error type="authentication" code="'.$response->getStatusCode().'">'.$response->getStatusDescription().'</error>'; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.