-
Notifications
You must be signed in to change notification settings - Fork 1
/
ActiveResource.php
352 lines (322 loc) · 8.63 KB
/
ActiveResource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* Basic implementation of the Ruby on Rails ActiveResource REST client.
* Intended to work with RoR-based REST servers, which all share similar
* API patterns.
*
* Usage:
*
* <?php
*
* require_once('ActiveResource.php');
*
* class Song extends ActiveResource {
* var $site = 'http://localhost:3000/';
* var $element_name = 'songs';
* }
*
* // create new item
* $song = new Song(array('artist' => 'Joe Cocker', 'title' => 'A Little Help From My Friends'));
* $song->save();
*
* // fetch and update an item
* $song->find(44)->set('title', 'The River')->save();
*
* // line by line
* $song->find(44);
* $song->title = 'The River';
* $song->save();
*
* // get all songs
* $songs = $song->find('all');
*
* // delete a song
* $song->find(44);
* $song->destroy();
*
* // custom method
* $songs = $song->get('by_year', array('year' => 1999));
*
* ?>
*
* @author John Luxford <[email protected]>
* @version 0.1 alpha
* @license http://opensource.org/licenses/lgpl-2.1.php
*/
class ActiveResource {
/**
* The REST site address, e.g., http://user:pass@domain:port/
*/
var $site = false;
/**
* The remote collection, e.g., person or things
*/
var $element_name = false;
/**
* The data of the current object, accessed via the anonymous get/set methods.
*/
var $_data = array();
/**
* An error message if an error occurred.
*/
var $error = false;
/**
* The error number if an error occurred.
*/
var $errno = false;
var $collection_url = "%s/%s.xml";
var $member_url = "%s/%s/%d.xml";
var $method_url = "%s/%s/%d/%s.xml";
function collection_url(){
return sprintf($this->collection_url, $this->site, $this->element_name);
}
function member_url($id=false,$method=false){
if(!$id){
$id = $this->_data['id'];
}
if($method){
return sprintf($this->method_url, $this->site, $this->element_name, $id, $method);
}else{
return sprintf($this->member_url, $this->site, $this->element_name, $id);
}
}
/**
* Constructor method.
*/
function __construct($data = array()) {
$this->_data = $data;
}
/**
* Saves a new record or updates an existing one via:
*
* POST /collection.xml
* PUT /collection/id.xml
*/
function save() {
if( isset($this->_data['id']) ){
// update
return $this->_send_and_receive($this->member_url(), 'PUT', $this->_data);
}
// create
return $this->_send_and_receive($this->collection_url(), 'POST', $this->_data);
}
/**
* Deletes a record via:
*
* DELETE /collection/id.xml
*/
function destroy() {
return $this->_send_and_receive($this->site . $this->element_name . '/' . $this->_data['id'] . '.xml', 'DELETE');
}
/**
* Finds a record or records via:
*
* GET /collection/id.xml
* GET /collection.xml
*/
function find($id = false, $options = null){
if(! $id){
$id = $this->_data['id'];
}
if($id == 'all'){
return $this->_send_and_receive($this->collection_url(), 'GET');
}
if($id == 'first'){
$req = $this->collection_url();
if(count($options) > 0){
$req .= '?' . http_build_query($options);
}
return $this->_send_and_receive($req, 'GET');
}
return $this->_send_and_receive($this->member_url($id), 'GET');
}
/**
* Gets a specified custom method on the current object via:
*
* GET /collection/id/method.xml
* GET /collection/id/method.xml?attr=value
*/
function get($method, $options = array()) {
$req = $this->member_url( $method );
// $this->site . $this->element_name . '/' . $this->_data['id'] . '/' . $method . '.xml';
if(count($options) > 0) {
$req .= '?' . http_build_query($options);
}
return $this->_send_and_receive($req, 'GET');
}
/**
* Posts to a specified custom method on the current object via:
*
* POST /collection/id/method.xml
*/
function post($method, $options = array()) {
return $this->_send_and_receive($this->site . $this->element_name . '/' . $this->_data['id'] . '/' . $method . '.xml', 'POST', $options);
}
/**
* Puts to a specified custom method on the current object via:
*
* PUT /collection/id/method.xml
*/
function put($method, $options = array(), $use_class = true) {
if(substr($this->site,-1) == '/'){
$site = $this->site;
}
else {
$site = $this->site . '/';
}
return $this->_send_and_receive($site . $this->element_name . '/' . $this->_data['id'] . '/' . $method . '.xml', 'PUT', $options, $use_class);
}
/**
* Build the request, call _fetch() and parse the results.
*/
function _send_and_receive($url, $method, $data = array(), $use_class = true) {
$params = '';
$el = substr($this->element_name, 0, -1);
foreach($data as $k => $v) {
if($k != 'id' && $k != 'created-at' && $k != 'updated-at') {
if($use_class) {
$params .= '&' . $el . '[' . $k . ']=' . urlencode($v);
} else {
$params .= '&' . $k . '=' . urlencode($v);
}
if($k == 'list' || $k == 'list_id') {
$params .= '&' . $k . '=' . urlencode($v);
}
}
}
$params = substr($params, 1);
$res = $this->_fetch($url, $method, $params);
preg_match('/Status: (\d{3})/', $res, $match);
$this->status = $match[1];
if ($this->status == 422) {
$this->error = 'Unprocessable Entity';
return $this;
}
preg_match('/<\?xml/', $res, $match);
if( ! $match ){
return $this;
}
list($headers, $res) = explode('<?xml version="1.0" encoding="UTF-8"?>', $res);
if(! $res) {
return $this;
} elseif($res == ' ') {
$this->error = 'Empty reply';
return $this;
}
// parse XML response
$xml = new SimpleXMLElement($res);
if($xml->getName() == $this->element_name) {
// multiple
$res = array();
$cls = get_class($this);
foreach($xml->children() as $child) {
$obj = new $cls;
foreach((array) $child as $k => $v) {
if(isset($v['nil']) && $v['nil'] == 'true') {
continue;
} else {
$obj->_data[$k] = $v;
}
}
$res[] = $obj;
}
return $res;
} elseif($xml->getName() == 'errors') {
// parse error message
$this->error = $xml->error;
return false;
}
foreach((array) $xml as $k => $v) {
if(isset($v['nil']) && $v['nil'] == 'true') {
continue;
} else {
$this->_data[$k] = $v;
}
}
return $this;
}
/**
* Fetch the specified request via cURL.
*/
function _fetch($url, $method, $params){
if(! extension_loaded('curl')) {
$this->error = 'cURL extension not loaded.';
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
/**
Corrige BUG SSL certificate problem
SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate
verify failed
*/
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
switch($method) {
case 'POST':
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
break;
case 'DELETE':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case 'PUT':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
$params .= '&_method=put'; // RoR Hack
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
break;
case 'GET':
default:
break;
}
$res = curl_exec($ch);
if(! $res) {
$this->errno = curl_errno($ch);
$this->error = curl_error($ch);
curl_close($ch);
return false;
}
curl_close($ch);
return $res;
}
/**
* Getter for internal object data.
*/
function __get($k) {
if(isset($this->_data[$k])) {
return $this->_data[$k];
}
return $this->{$k};
}
/**
* Setter for internal object data.
*/
function __set($k, $v) {
if(isset($this->_data[$k])) {
$this->_data[$k] = $v;
return;
}
$this->{$k} = $v;
}
/**
* Quick setter for chaining methods.
*/
function set($k, $v = false) {
if(! $v && is_array($k)) {
foreach($k as $key => $value) {
$this->_data[$key] = $value;
}
} else {
$this->_data[$k] = $v;
}
return $this;
}
}
?>