|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Zend Framework |
|
5
|
|
|
* |
|
6
|
|
|
* LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the new BSD license that is bundled |
|
9
|
|
|
* with this package in the file LICENSE.txt. |
|
10
|
|
|
* It is also available through the world-wide-web at this URL: |
|
11
|
|
|
* http://framework.zend.com/license/new-bsd |
|
12
|
|
|
* If you did not receive a copy of the license and are unable to |
|
13
|
|
|
* obtain it through the world-wide-web, please send an email |
|
14
|
|
|
* to [email protected] so we can send you a copy immediately. |
|
15
|
|
|
* |
|
16
|
|
|
* @category Zend |
|
17
|
|
|
* @package Zend_Gdata |
|
18
|
|
|
* @subpackage App |
|
19
|
|
|
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) |
|
20
|
|
|
* @license http://framework.zend.com/license/new-bsd New BSD License |
|
21
|
|
|
* @version $Id$ |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Zend_Gdata_Feed |
|
26
|
|
|
*/ |
|
27
|
|
|
require_once 'Zend/Gdata/App/Feed.php'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Zend_Gdata_Http_Client |
|
31
|
|
|
*/ |
|
32
|
|
|
require_once 'Zend/Http/Client.php'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Zend_Version |
|
36
|
|
|
*/ |
|
37
|
|
|
require_once 'Zend/Version.php'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Zend_Gdata_App_MediaSource |
|
41
|
|
|
*/ |
|
42
|
|
|
require_once 'Zend/Gdata/App/MediaSource.php'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Zend_Uri/Http |
|
46
|
|
|
*/ |
|
47
|
|
|
require_once 'Zend/Uri/Http.php'; |
|
48
|
|
|
|
|
49
|
|
|
/** @see Zend_Xml_Security */ |
|
50
|
|
|
require_once 'Zend/Xml/Security.php'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Provides Atom Publishing Protocol (APP) functionality. This class and all |
|
54
|
|
|
* other components of Zend_Gdata_App are designed to work independently from |
|
55
|
|
|
* other Zend_Gdata components in order to interact with generic APP services. |
|
56
|
|
|
* |
|
57
|
|
|
* @category Zend |
|
58
|
|
|
* @package Zend_Gdata |
|
59
|
|
|
* @subpackage App |
|
60
|
|
|
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) |
|
61
|
|
|
* @license http://framework.zend.com/license/new-bsd New BSD License |
|
62
|
|
|
*/ |
|
63
|
|
|
class Zend_Gdata_App |
|
64
|
|
|
{ |
|
65
|
|
|
|
|
66
|
|
|
/** Default major protocol version. |
|
67
|
|
|
* |
|
68
|
|
|
* @see _majorProtocolVersion |
|
69
|
|
|
*/ |
|
70
|
|
|
const DEFAULT_MAJOR_PROTOCOL_VERSION = 1; |
|
71
|
|
|
|
|
72
|
|
|
/** Default minor protocol version. |
|
73
|
|
|
* |
|
74
|
|
|
* @see _minorProtocolVersion |
|
75
|
|
|
*/ |
|
76
|
|
|
const DEFAULT_MINOR_PROTOCOL_VERSION = null; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Client object used to communicate |
|
80
|
|
|
* |
|
81
|
|
|
* @var Zend_Http_Client |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $_httpClient; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Client object used to communicate in static context |
|
87
|
|
|
* |
|
88
|
|
|
* @var Zend_Http_Client |
|
89
|
|
|
*/ |
|
90
|
|
|
protected static $_staticHttpClient = null; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Override HTTP PUT and DELETE request methods? |
|
94
|
|
|
* |
|
95
|
|
|
* @var boolean |
|
96
|
|
|
*/ |
|
97
|
|
|
protected static $_httpMethodOverride = false; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Enable gzipped responses? |
|
101
|
|
|
* |
|
102
|
|
|
* @var boolean |
|
103
|
|
|
*/ |
|
104
|
|
|
protected static $_gzipEnabled = false; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Use verbose exception messages. In the case of HTTP errors, |
|
108
|
|
|
* use the body of the HTTP response in the exception message. |
|
109
|
|
|
* |
|
110
|
|
|
* @var boolean |
|
111
|
|
|
*/ |
|
112
|
|
|
protected static $_verboseExceptionMessages = true; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Default URI to which to POST. |
|
116
|
|
|
* |
|
117
|
|
|
* @var string |
|
118
|
|
|
*/ |
|
119
|
|
|
protected $_defaultPostUri = null; |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Packages to search for classes when using magic __call method, in order. |
|
123
|
|
|
* |
|
124
|
|
|
* @var array |
|
125
|
|
|
*/ |
|
126
|
|
|
protected $_registeredPackages = array( |
|
127
|
|
|
'Zend_Gdata_App_Extension', |
|
128
|
|
|
'Zend_Gdata_App'); |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Maximum number of redirects to follow during HTTP operations |
|
132
|
|
|
* |
|
133
|
|
|
* @var int |
|
134
|
|
|
*/ |
|
135
|
|
|
protected static $_maxRedirects = 5; |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Indicates the major protocol version that should be used. |
|
139
|
|
|
* At present, recognized values are either 1 or 2. However, any integer |
|
140
|
|
|
* value >= 1 is considered valid. |
|
141
|
|
|
* |
|
142
|
|
|
* Under most circumtances, this will be automatically set by |
|
143
|
|
|
* Zend_Gdata_App subclasses. |
|
144
|
|
|
* |
|
145
|
|
|
* @see setMajorProtocolVersion() |
|
146
|
|
|
* @see getMajorProtocolVersion() |
|
147
|
|
|
*/ |
|
148
|
|
|
protected $_majorProtocolVersion; |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Indicates the minor protocol version that should be used. Can be set |
|
152
|
|
|
* to either an integer >= 0, or NULL if no minor version should be sent |
|
153
|
|
|
* to the server. |
|
154
|
|
|
* |
|
155
|
|
|
* At present, this field is not used by any Google services, but may be |
|
156
|
|
|
* used in the future. |
|
157
|
|
|
* |
|
158
|
|
|
* Under most circumtances, this will be automatically set by |
|
159
|
|
|
* Zend_Gdata_App subclasses. |
|
160
|
|
|
* |
|
161
|
|
|
* @see setMinorProtocolVersion() |
|
162
|
|
|
* @see getMinorProtocolVersion() |
|
163
|
|
|
*/ |
|
164
|
|
|
protected $_minorProtocolVersion; |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Whether we want to use XML to object mapping when fetching data. |
|
168
|
|
|
* |
|
169
|
|
|
* @var boolean |
|
170
|
|
|
*/ |
|
171
|
|
|
protected $_useObjectMapping = true; |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Create Gdata object |
|
175
|
|
|
* |
|
176
|
|
|
* @param Zend_Http_Client $client |
|
177
|
|
|
* @param string $applicationId |
|
178
|
|
|
*/ |
|
179
|
|
|
public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0') |
|
180
|
|
|
{ |
|
181
|
|
|
$this->setHttpClient($client, $applicationId); |
|
182
|
|
|
// Set default protocol version. Subclasses should override this as |
|
183
|
|
|
// needed once a given service supports a new version. |
|
184
|
|
|
$this->setMajorProtocolVersion(self::DEFAULT_MAJOR_PROTOCOL_VERSION); |
|
185
|
|
|
$this->setMinorProtocolVersion(self::DEFAULT_MINOR_PROTOCOL_VERSION); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Adds a Zend Framework package to the $_registeredPackages array. |
|
190
|
|
|
* This array is searched when using the magic __call method below |
|
191
|
|
|
* to instantiante new objects. |
|
192
|
|
|
* |
|
193
|
|
|
* @param string $name The name of the package (eg Zend_Gdata_App) |
|
194
|
|
|
* @return void |
|
195
|
|
|
*/ |
|
196
|
|
|
public function registerPackage($name) |
|
197
|
|
|
{ |
|
198
|
|
|
array_unshift($this->_registeredPackages, $name); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Retrieve feed as string or object |
|
203
|
|
|
* |
|
204
|
|
|
* @param string $uri The uri from which to retrieve the feed |
|
205
|
|
|
* @param string $className The class which is used as the return type |
|
206
|
|
|
* @return string|Zend_Gdata_App_Feed Returns string only if the object |
|
207
|
|
|
* mapping has been disabled explicitly |
|
208
|
|
|
* by passing false to the |
|
209
|
|
|
* useObjectMapping() function. |
|
210
|
|
|
*/ |
|
211
|
|
|
public function getFeed($uri, $className='Zend_Gdata_App_Feed') |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->importUrl($uri, $className, null); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Retrieve entry as string or object |
|
218
|
|
|
* |
|
219
|
|
|
* @param string $uri |
|
220
|
|
|
* @param string $className The class which is used as the return type |
|
221
|
|
|
* @return string|Zend_Gdata_App_Entry Returns string only if the object |
|
222
|
|
|
* mapping has been disabled explicitly |
|
223
|
|
|
* by passing false to the |
|
224
|
|
|
* useObjectMapping() function. |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getEntry($uri, $className='Zend_Gdata_App_Entry') |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->importUrl($uri, $className, null); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Get the Zend_Http_Client object used for communication |
|
233
|
|
|
* |
|
234
|
|
|
* @return Zend_Http_Client |
|
235
|
|
|
*/ |
|
236
|
|
|
public function getHttpClient() |
|
237
|
|
|
{ |
|
238
|
|
|
return $this->_httpClient; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Set the Zend_Http_Client object used for communication |
|
243
|
|
|
* |
|
244
|
|
|
* @param Zend_Http_Client $client The client to use for communication |
|
245
|
|
|
* @throws Zend_Gdata_App_HttpException |
|
246
|
|
|
* @return Zend_Gdata_App Provides a fluent interface |
|
247
|
|
|
*/ |
|
248
|
|
|
public function setHttpClient($client, |
|
249
|
|
|
$applicationId = 'MyCompany-MyApp-1.0') |
|
250
|
|
|
{ |
|
251
|
|
|
if ($client === null) { |
|
252
|
|
|
$client = new Zend_Http_Client(); |
|
253
|
|
|
} |
|
254
|
|
|
if (!$client instanceof Zend_Http_Client) { |
|
|
|
|
|
|
255
|
|
|
require_once 'Zend/Gdata/App/HttpException.php'; |
|
256
|
|
|
throw new Zend_Gdata_App_HttpException( |
|
257
|
|
|
'Argument is not an instance of Zend_Http_Client.'); |
|
258
|
|
|
} |
|
259
|
|
|
$userAgent = $applicationId . ' Zend_Framework_Gdata/' . |
|
260
|
|
|
Zend_Version::VERSION; |
|
261
|
|
|
$client->setHeaders('User-Agent', $userAgent); |
|
262
|
|
|
$client->setConfig(array( |
|
263
|
|
|
'strictredirects' => true |
|
264
|
|
|
) |
|
265
|
|
|
); |
|
266
|
|
|
$this->_httpClient = $client; |
|
267
|
|
|
self::setStaticHttpClient($client); |
|
268
|
|
|
return $this; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Set the static HTTP client instance |
|
273
|
|
|
* |
|
274
|
|
|
* Sets the static HTTP client object to use for retrieving the feed. |
|
275
|
|
|
* |
|
276
|
|
|
* @param Zend_Http_Client $httpClient |
|
277
|
|
|
* @return void |
|
278
|
|
|
*/ |
|
279
|
|
|
public static function setStaticHttpClient(Zend_Http_Client $httpClient) |
|
280
|
|
|
{ |
|
281
|
|
|
self::$_staticHttpClient = $httpClient; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Gets the HTTP client object. If none is set, a new Zend_Http_Client will be used. |
|
287
|
|
|
* |
|
288
|
|
|
* @return Zend_Http_Client |
|
289
|
|
|
*/ |
|
290
|
|
|
public static function getStaticHttpClient() |
|
291
|
|
|
{ |
|
292
|
|
|
if (!self::$_staticHttpClient instanceof Zend_Http_Client) { |
|
|
|
|
|
|
293
|
|
|
$client = new Zend_Http_Client(); |
|
294
|
|
|
$userAgent = 'Zend_Framework_Gdata/' . Zend_Version::VERSION; |
|
295
|
|
|
$client->setHeaders('User-Agent', $userAgent); |
|
296
|
|
|
$client->setConfig(array( |
|
297
|
|
|
'strictredirects' => true |
|
298
|
|
|
) |
|
299
|
|
|
); |
|
300
|
|
|
self::$_staticHttpClient = $client; |
|
301
|
|
|
} |
|
302
|
|
|
return self::$_staticHttpClient; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Toggle using POST instead of PUT and DELETE HTTP methods |
|
307
|
|
|
* |
|
308
|
|
|
* Some feed implementations do not accept PUT and DELETE HTTP |
|
309
|
|
|
* methods, or they can't be used because of proxies or other |
|
310
|
|
|
* measures. This allows turning on using POST where PUT and |
|
311
|
|
|
* DELETE would normally be used; in addition, an |
|
312
|
|
|
* X-Method-Override header will be sent with a value of PUT or |
|
313
|
|
|
* DELETE as appropriate. |
|
314
|
|
|
* |
|
315
|
|
|
* @param boolean $override Whether to override PUT and DELETE with POST. |
|
316
|
|
|
* @return void |
|
317
|
|
|
*/ |
|
318
|
|
|
public static function setHttpMethodOverride($override = true) |
|
319
|
|
|
{ |
|
320
|
|
|
self::$_httpMethodOverride = $override; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Get the HTTP override state |
|
325
|
|
|
* |
|
326
|
|
|
* @return boolean |
|
327
|
|
|
*/ |
|
328
|
|
|
public static function getHttpMethodOverride() |
|
329
|
|
|
{ |
|
330
|
|
|
return self::$_httpMethodOverride; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Toggle requesting gzip encoded responses |
|
335
|
|
|
* |
|
336
|
|
|
* @param boolean $enabled Whether or not to enable gzipped responses |
|
337
|
|
|
* @return void |
|
338
|
|
|
*/ |
|
339
|
|
|
public static function setGzipEnabled($enabled = false) |
|
340
|
|
|
{ |
|
341
|
|
|
if ($enabled && !function_exists('gzinflate')) { |
|
342
|
|
|
require_once 'Zend/Gdata/App/InvalidArgumentException.php'; |
|
343
|
|
|
throw new Zend_Gdata_App_InvalidArgumentException( |
|
344
|
|
|
'You cannot enable gzipped responses if the zlib module ' . |
|
345
|
|
|
'is not enabled in your PHP installation.'); |
|
346
|
|
|
|
|
347
|
|
|
} |
|
348
|
|
|
self::$_gzipEnabled = $enabled; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* Get the HTTP override state |
|
353
|
|
|
* |
|
354
|
|
|
* @return boolean |
|
355
|
|
|
*/ |
|
356
|
|
|
public static function getGzipEnabled() |
|
357
|
|
|
{ |
|
358
|
|
|
return self::$_gzipEnabled; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* Get whether to use verbose exception messages |
|
363
|
|
|
* |
|
364
|
|
|
* In the case of HTTP errors, use the body of the HTTP response |
|
365
|
|
|
* in the exception message. |
|
366
|
|
|
* |
|
367
|
|
|
* @return boolean |
|
368
|
|
|
*/ |
|
369
|
|
|
public static function getVerboseExceptionMessages() |
|
370
|
|
|
{ |
|
371
|
|
|
return self::$_verboseExceptionMessages; |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* Set whether to use verbose exception messages |
|
376
|
|
|
* |
|
377
|
|
|
* In the case of HTTP errors, use the body of the HTTP response |
|
378
|
|
|
* in the exception message. |
|
379
|
|
|
* |
|
380
|
|
|
* @param boolean $verbose Whether to use verbose exception messages |
|
381
|
|
|
*/ |
|
382
|
|
|
public static function setVerboseExceptionMessages($verbose) |
|
383
|
|
|
{ |
|
384
|
|
|
self::$_verboseExceptionMessages = $verbose; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* Set the maximum number of redirects to follow during HTTP operations |
|
389
|
|
|
* |
|
390
|
|
|
* @param int $maxRedirects Maximum number of redirects to follow |
|
391
|
|
|
* @return void |
|
392
|
|
|
*/ |
|
393
|
|
|
public static function setMaxRedirects($maxRedirects) |
|
394
|
|
|
{ |
|
395
|
|
|
self::$_maxRedirects = $maxRedirects; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
/** |
|
399
|
|
|
* Get the maximum number of redirects to follow during HTTP operations |
|
400
|
|
|
* |
|
401
|
|
|
* @return int Maximum number of redirects to follow |
|
402
|
|
|
*/ |
|
403
|
|
|
public static function getMaxRedirects() |
|
404
|
|
|
{ |
|
405
|
|
|
return self::$_maxRedirects; |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
/** |
|
409
|
|
|
* Set the major protocol version that should be used. Values < 1 will |
|
410
|
|
|
* cause a Zend_Gdata_App_InvalidArgumentException to be thrown. |
|
411
|
|
|
* |
|
412
|
|
|
* @see _majorProtocolVersion |
|
413
|
|
|
* @param int $value The major protocol version to use. |
|
414
|
|
|
* @throws Zend_Gdata_App_InvalidArgumentException |
|
415
|
|
|
*/ |
|
416
|
|
|
public function setMajorProtocolVersion($value) |
|
417
|
|
|
{ |
|
418
|
|
|
if (!($value >= 1)) { |
|
419
|
|
|
require_once('Zend/Gdata/App/InvalidArgumentException.php'); |
|
420
|
|
|
throw new Zend_Gdata_App_InvalidArgumentException( |
|
421
|
|
|
'Major protocol version must be >= 1'); |
|
422
|
|
|
} |
|
423
|
|
|
$this->_majorProtocolVersion = $value; |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
/** |
|
427
|
|
|
* Get the major protocol version that is in use. |
|
428
|
|
|
* |
|
429
|
|
|
* @see _majorProtocolVersion |
|
430
|
|
|
* @return int The major protocol version in use. |
|
431
|
|
|
*/ |
|
432
|
|
|
public function getMajorProtocolVersion() |
|
433
|
|
|
{ |
|
434
|
|
|
return $this->_majorProtocolVersion; |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
/** |
|
438
|
|
|
* Set the minor protocol version that should be used. If set to NULL, no |
|
439
|
|
|
* minor protocol version will be sent to the server. Values < 0 will |
|
440
|
|
|
* cause a Zend_Gdata_App_InvalidArgumentException to be thrown. |
|
441
|
|
|
* |
|
442
|
|
|
* @see _minorProtocolVersion |
|
443
|
|
|
* @param (int|NULL) $value The minor protocol version to use. |
|
444
|
|
|
* @throws Zend_Gdata_App_InvalidArgumentException |
|
445
|
|
|
*/ |
|
446
|
|
|
public function setMinorProtocolVersion($value) |
|
447
|
|
|
{ |
|
448
|
|
|
if (!($value >= 0)) { |
|
449
|
|
|
require_once('Zend/Gdata/App/InvalidArgumentException.php'); |
|
450
|
|
|
throw new Zend_Gdata_App_InvalidArgumentException( |
|
451
|
|
|
'Minor protocol version must be >= 0'); |
|
452
|
|
|
} |
|
453
|
|
|
$this->_minorProtocolVersion = $value; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
/** |
|
457
|
|
|
* Get the minor protocol version that is in use. |
|
458
|
|
|
* |
|
459
|
|
|
* @see _minorProtocolVersion |
|
460
|
|
|
* @return (int|NULL) The major protocol version in use, or NULL if no |
|
461
|
|
|
* minor version is specified. |
|
462
|
|
|
*/ |
|
463
|
|
|
public function getMinorProtocolVersion() |
|
464
|
|
|
{ |
|
465
|
|
|
return $this->_minorProtocolVersion; |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
/** |
|
469
|
|
|
* Provides pre-processing for HTTP requests to APP services. |
|
470
|
|
|
* |
|
471
|
|
|
* 1. Checks the $data element and, if it's an entry, extracts the XML, |
|
472
|
|
|
* multipart data, edit link (PUT,DELETE), etc. |
|
473
|
|
|
* 2. If $data is a string, sets the default content-type header as |
|
474
|
|
|
* 'application/atom+xml' if it's not already been set. |
|
475
|
|
|
* 3. Adds a x-http-method override header and changes the HTTP method |
|
476
|
|
|
* to 'POST' if necessary as per getHttpMethodOverride() |
|
477
|
|
|
* |
|
478
|
|
|
* @param string $method The HTTP method for the request - 'GET', 'POST', |
|
479
|
|
|
* 'PUT', 'DELETE' |
|
480
|
|
|
* @param string $url The URL to which this request is being performed, |
|
481
|
|
|
* or null if found in $data |
|
482
|
|
|
* @param array $headers An associative array of HTTP headers for this |
|
483
|
|
|
* request |
|
484
|
|
|
* @param mixed $data The Zend_Gdata_App_Entry or XML for the |
|
485
|
|
|
* body of the request |
|
486
|
|
|
* @param string $contentTypeOverride The override value for the |
|
487
|
|
|
* content type of the request body |
|
488
|
|
|
* @return array An associative array containing the determined |
|
489
|
|
|
* 'method', 'url', 'data', 'headers', 'contentType' |
|
490
|
|
|
*/ |
|
491
|
|
|
public function prepareRequest($method, |
|
492
|
|
|
$url = null, |
|
493
|
|
|
$headers = array(), |
|
494
|
|
|
$data = null, |
|
495
|
|
|
$contentTypeOverride = null) |
|
496
|
|
|
{ |
|
497
|
|
|
// As a convenience, if $headers is null, we'll convert it back to |
|
498
|
|
|
// an empty array. |
|
499
|
|
|
if ($headers === null) { |
|
|
|
|
|
|
500
|
|
|
$headers = array(); |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
$rawData = null; |
|
504
|
|
|
$finalContentType = null; |
|
505
|
|
|
if ($url == null) { |
|
|
|
|
|
|
506
|
|
|
$url = $this->_defaultPostUri; |
|
507
|
|
|
} |
|
508
|
|
|
|
|
509
|
|
|
if (is_string($data)) { |
|
510
|
|
|
$rawData = $data; |
|
511
|
|
|
if ($contentTypeOverride === null) { |
|
512
|
|
|
$finalContentType = 'application/atom+xml'; |
|
513
|
|
|
} |
|
514
|
|
|
} elseif ($data instanceof Zend_Gdata_App_MediaEntry) { |
|
515
|
|
|
$rawData = $data->encode(); |
|
516
|
|
|
if ($data->getMediaSource() !== null) { |
|
517
|
|
|
$finalContentType = $rawData->getContentType(); |
|
518
|
|
|
$headers['MIME-version'] = '1.0'; |
|
519
|
|
|
$headers['Slug'] = $data->getMediaSource()->getSlug(); |
|
520
|
|
|
} else { |
|
521
|
|
|
$finalContentType = 'application/atom+xml'; |
|
522
|
|
|
} |
|
523
|
|
|
if ($method == 'PUT' || $method == 'DELETE') { |
|
524
|
|
|
$editLink = $data->getEditLink(); |
|
525
|
|
|
if ($editLink != null && $url == null) { |
|
526
|
|
|
$url = $editLink->getHref(); |
|
527
|
|
|
} |
|
528
|
|
|
} |
|
529
|
|
|
} elseif ($data instanceof Zend_Gdata_App_Entry) { |
|
530
|
|
|
$rawData = $data->saveXML(); |
|
531
|
|
|
$finalContentType = 'application/atom+xml'; |
|
532
|
|
|
if ($method == 'PUT' || $method == 'DELETE') { |
|
533
|
|
|
$editLink = $data->getEditLink(); |
|
534
|
|
|
if ($editLink != null) { |
|
535
|
|
|
$url = $editLink->getHref(); |
|
536
|
|
|
} |
|
537
|
|
|
} |
|
538
|
|
|
} elseif ($data instanceof Zend_Gdata_App_MediaSource) { |
|
539
|
|
|
$rawData = $data->encode(); |
|
540
|
|
|
if ($data->getSlug() !== null) { |
|
|
|
|
|
|
541
|
|
|
$headers['Slug'] = $data->getSlug(); |
|
542
|
|
|
} |
|
543
|
|
|
$finalContentType = $data->getContentType(); |
|
544
|
|
|
} |
|
545
|
|
|
|
|
546
|
|
|
if ($method == 'DELETE') { |
|
547
|
|
|
$rawData = null; |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
// Set an If-Match header if: |
|
551
|
|
|
// - This isn't a DELETE |
|
552
|
|
|
// - If this isn't a GET, the Etag isn't weak |
|
553
|
|
|
// - A similar header (If-Match/If-None-Match) hasn't already been |
|
554
|
|
|
// set. |
|
555
|
|
|
if ($method != 'DELETE' && ( |
|
556
|
|
|
!array_key_exists('If-Match', $headers) && |
|
557
|
|
|
!array_key_exists('If-None-Match', $headers) |
|
558
|
|
|
) ) { |
|
559
|
|
|
$allowWeak = $method == 'GET'; |
|
560
|
|
|
if ($ifMatchHeader = $this->generateIfMatchHeaderData( |
|
561
|
|
|
$data, $allowWeak)) { |
|
562
|
|
|
$headers['If-Match'] = $ifMatchHeader; |
|
563
|
|
|
} |
|
564
|
|
|
} |
|
565
|
|
|
|
|
566
|
|
|
if ($method != 'POST' && $method != 'GET' && Zend_Gdata_App::getHttpMethodOverride()) { |
|
567
|
|
|
$headers['x-http-method-override'] = $method; |
|
568
|
|
|
$method = 'POST'; |
|
569
|
|
|
} else { |
|
570
|
|
|
$headers['x-http-method-override'] = null; |
|
571
|
|
|
} |
|
572
|
|
|
|
|
573
|
|
|
if ($contentTypeOverride != null) { |
|
|
|
|
|
|
574
|
|
|
$finalContentType = $contentTypeOverride; |
|
575
|
|
|
} |
|
576
|
|
|
|
|
577
|
|
|
return array('method' => $method, 'url' => $url, |
|
578
|
|
|
'data' => $rawData, 'headers' => $headers, |
|
579
|
|
|
'contentType' => $finalContentType); |
|
580
|
|
|
} |
|
581
|
|
|
|
|
582
|
|
|
/** |
|
583
|
|
|
* Performs a HTTP request using the specified method |
|
584
|
|
|
* |
|
585
|
|
|
* @param string $method The HTTP method for the request - 'GET', 'POST', |
|
586
|
|
|
* 'PUT', 'DELETE' |
|
587
|
|
|
* @param string $url The URL to which this request is being performed |
|
588
|
|
|
* @param array $headers An associative array of HTTP headers |
|
589
|
|
|
* for this request |
|
590
|
|
|
* @param string $body The body of the HTTP request |
|
591
|
|
|
* @param string $contentType The value for the content type |
|
592
|
|
|
* of the request body |
|
593
|
|
|
* @param int $remainingRedirects Number of redirects to follow if request |
|
594
|
|
|
* s results in one |
|
595
|
|
|
* @return Zend_Http_Response The response object |
|
596
|
|
|
*/ |
|
597
|
|
|
public function performHttpRequest($method, $url, $headers = null, |
|
598
|
|
|
$body = null, $contentType = null, $remainingRedirects = null) |
|
599
|
|
|
{ |
|
600
|
|
|
require_once 'Zend/Http/Client/Exception.php'; |
|
601
|
|
|
if ($remainingRedirects === null) { |
|
602
|
|
|
$remainingRedirects = self::getMaxRedirects(); |
|
603
|
|
|
} |
|
604
|
|
|
if ($headers === null) { |
|
605
|
|
|
$headers = array(); |
|
606
|
|
|
} |
|
607
|
|
|
// Append a Gdata version header if protocol v2 or higher is in use. |
|
608
|
|
|
// (Protocol v1 does not use this header.) |
|
609
|
|
|
$major = $this->getMajorProtocolVersion(); |
|
610
|
|
|
$minor = $this->getMinorProtocolVersion(); |
|
611
|
|
|
if ($major >= 2) { |
|
612
|
|
|
$headers['GData-Version'] = $major + |
|
613
|
|
|
(($minor === null) ? '.' + $minor : ''); |
|
614
|
|
|
} |
|
615
|
|
|
|
|
616
|
|
|
// check the overridden method |
|
617
|
|
|
if (($method == 'POST' || $method == 'PUT') && $body === null && |
|
618
|
|
|
$headers['x-http-method-override'] != 'DELETE') { |
|
619
|
|
|
require_once 'Zend/Gdata/App/InvalidArgumentException.php'; |
|
620
|
|
|
throw new Zend_Gdata_App_InvalidArgumentException( |
|
621
|
|
|
'You must specify the data to post as either a ' . |
|
622
|
|
|
'string or a child of Zend_Gdata_App_Entry'); |
|
623
|
|
|
} |
|
624
|
|
|
if ($url === null) { |
|
|
|
|
|
|
625
|
|
|
require_once 'Zend/Gdata/App/InvalidArgumentException.php'; |
|
626
|
|
|
throw new Zend_Gdata_App_InvalidArgumentException( |
|
627
|
|
|
'You must specify an URI to which to post.'); |
|
628
|
|
|
} |
|
629
|
|
|
$headers['Content-Type'] = $contentType; |
|
630
|
|
|
if (Zend_Gdata_App::getGzipEnabled()) { |
|
631
|
|
|
// some services require the word 'gzip' to be in the user-agent |
|
632
|
|
|
// header in addition to the accept-encoding header |
|
633
|
|
|
if (strpos($this->_httpClient->getHeader('User-Agent'), |
|
|
|
|
|
|
634
|
|
|
'gzip') === false) { |
|
635
|
|
|
$headers['User-Agent'] = |
|
636
|
|
|
$this->_httpClient->getHeader('User-Agent') . ' (gzip)'; |
|
|
|
|
|
|
637
|
|
|
} |
|
638
|
|
|
$headers['Accept-encoding'] = 'gzip, deflate'; |
|
639
|
|
|
} else { |
|
640
|
|
|
$headers['Accept-encoding'] = 'identity'; |
|
641
|
|
|
} |
|
642
|
|
|
|
|
643
|
|
|
// Make sure the HTTP client object is 'clean' before making a request |
|
644
|
|
|
// In addition to standard headers to reset via resetParameters(), |
|
645
|
|
|
// also reset the Slug and If-Match headers |
|
646
|
|
|
$this->_httpClient->resetParameters(); |
|
647
|
|
|
$this->_httpClient->setHeaders(array('Slug', 'If-Match')); |
|
648
|
|
|
|
|
649
|
|
|
// Set the params for the new request to be performed |
|
650
|
|
|
$this->_httpClient->setHeaders($headers); |
|
651
|
|
|
require_once 'Zend/Uri/Http.php'; |
|
652
|
|
|
$uri = Zend_Uri_Http::fromString($url); |
|
653
|
|
|
preg_match("/^(.*?)(\?.*)?$/", $url, $matches); |
|
654
|
|
|
$this->_httpClient->setUri($matches[1]); |
|
655
|
|
|
$queryArray = $uri->getQueryAsArray(); |
|
656
|
|
|
foreach ($queryArray as $name => $value) { |
|
657
|
|
|
$this->_httpClient->setParameterGet($name, $value); |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
|
|
661
|
|
|
$this->_httpClient->setConfig(array('maxredirects' => 0)); |
|
662
|
|
|
|
|
663
|
|
|
// Set the proper adapter if we are handling a streaming upload |
|
664
|
|
|
$usingMimeStream = false; |
|
665
|
|
|
$oldHttpAdapter = null; |
|
666
|
|
|
|
|
667
|
|
|
if ($body instanceof Zend_Gdata_MediaMimeStream) { |
|
668
|
|
|
$usingMimeStream = true; |
|
669
|
|
|
$this->_httpClient->setRawDataStream($body, $contentType); |
|
|
|
|
|
|
670
|
|
|
$oldHttpAdapter = $this->_httpClient->getAdapter(); |
|
671
|
|
|
|
|
672
|
|
|
if ($oldHttpAdapter instanceof Zend_Http_Client_Adapter_Proxy) { |
|
673
|
|
|
require_once 'Zend/Gdata/HttpAdapterStreamingProxy.php'; |
|
674
|
|
|
$newAdapter = new Zend_Gdata_HttpAdapterStreamingProxy(); |
|
675
|
|
|
} else { |
|
676
|
|
|
require_once 'Zend/Gdata/HttpAdapterStreamingSocket.php'; |
|
677
|
|
|
$newAdapter = new Zend_Gdata_HttpAdapterStreamingSocket(); |
|
678
|
|
|
} |
|
679
|
|
|
$this->_httpClient->setAdapter($newAdapter); |
|
680
|
|
|
} else { |
|
681
|
|
|
$this->_httpClient->setRawData($body, $contentType); |
|
682
|
|
|
} |
|
683
|
|
|
|
|
684
|
|
|
try { |
|
685
|
|
|
$response = $this->_httpClient->request($method); |
|
686
|
|
|
// reset adapter |
|
687
|
|
|
if ($usingMimeStream) { |
|
688
|
|
|
$this->_httpClient->setAdapter($oldHttpAdapter); |
|
689
|
|
|
} |
|
690
|
|
|
} catch (Zend_Http_Client_Exception $e) { |
|
691
|
|
|
// reset adapter |
|
692
|
|
|
if ($usingMimeStream) { |
|
693
|
|
|
$this->_httpClient->setAdapter($oldHttpAdapter); |
|
694
|
|
|
} |
|
695
|
|
|
require_once 'Zend/Gdata/App/HttpException.php'; |
|
696
|
|
|
throw new Zend_Gdata_App_HttpException($e->getMessage(), $e); |
|
697
|
|
|
} |
|
698
|
|
|
if ($response->isRedirect() && $response->getStatus() != '304') { |
|
699
|
|
|
if ($remainingRedirects > 0) { |
|
700
|
|
|
$newUrl = $response->getHeader('Location'); |
|
701
|
|
|
$response = $this->performHttpRequest( |
|
702
|
|
|
$method, $newUrl, $headers, $body, |
|
|
|
|
|
|
703
|
|
|
$contentType, $remainingRedirects); |
|
704
|
|
|
} else { |
|
705
|
|
|
require_once 'Zend/Gdata/App/HttpException.php'; |
|
706
|
|
|
throw new Zend_Gdata_App_HttpException( |
|
707
|
|
|
'Number of redirects exceeds maximum', null, $response); |
|
708
|
|
|
} |
|
709
|
|
|
} |
|
710
|
|
|
if (!$response->isSuccessful()) { |
|
711
|
|
|
require_once 'Zend/Gdata/App/HttpException.php'; |
|
712
|
|
|
$exceptionMessage = 'Expected response code 200, got ' . |
|
713
|
|
|
$response->getStatus(); |
|
714
|
|
|
if (self::getVerboseExceptionMessages()) { |
|
715
|
|
|
$exceptionMessage .= "\n" . $response->getBody(); |
|
716
|
|
|
} |
|
717
|
|
|
$exception = new Zend_Gdata_App_HttpException($exceptionMessage); |
|
718
|
|
|
$exception->setResponse($response); |
|
719
|
|
|
throw $exception; |
|
720
|
|
|
} |
|
721
|
|
|
return $response; |
|
722
|
|
|
} |
|
723
|
|
|
|
|
724
|
|
|
/** |
|
725
|
|
|
* Imports a feed located at $uri. |
|
726
|
|
|
* |
|
727
|
|
|
* @param string $uri |
|
728
|
|
|
* @param Zend_Http_Client $client The client used for communication |
|
729
|
|
|
* @param string $className The class which is used as the return type |
|
730
|
|
|
* @param bool $useObjectMapping Enable/disable the use of XML to object mapping. |
|
731
|
|
|
* @throws Zend_Gdata_App_Exception |
|
732
|
|
|
* @return string|Zend_Gdata_App_Feed Returns string only if the fourth |
|
733
|
|
|
* parameter ($useObjectMapping) is set |
|
734
|
|
|
* to false. |
|
735
|
|
|
*/ |
|
736
|
|
|
public static function import($uri, $client = null, |
|
737
|
|
|
$className='Zend_Gdata_App_Feed', $useObjectMapping = true) |
|
738
|
|
|
{ |
|
739
|
|
|
$app = new Zend_Gdata_App($client); |
|
740
|
|
|
$requestData = $app->prepareRequest('GET', $uri); |
|
741
|
|
|
$response = $app->performHttpRequest( |
|
742
|
|
|
$requestData['method'], $requestData['url']); |
|
743
|
|
|
|
|
744
|
|
|
$feedContent = $response->getBody(); |
|
745
|
|
|
if (false === $useObjectMapping) { |
|
746
|
|
|
return $feedContent; |
|
747
|
|
|
} |
|
748
|
|
|
$feed = self::importString($feedContent, $className); |
|
749
|
|
|
if ($client != null) { |
|
750
|
|
|
$feed->setHttpClient($client); |
|
|
|
|
|
|
751
|
|
|
} |
|
752
|
|
|
return $feed; |
|
753
|
|
|
} |
|
754
|
|
|
|
|
755
|
|
|
/** |
|
756
|
|
|
* Imports the specified URL (non-statically). |
|
757
|
|
|
* |
|
758
|
|
|
* @param string $url The URL to import |
|
759
|
|
|
* @param string $className The class which is used as the return type |
|
760
|
|
|
* @param array $extraHeaders Extra headers to add to the request, as an |
|
761
|
|
|
* array of string-based key/value pairs. |
|
762
|
|
|
* @throws Zend_Gdata_App_Exception |
|
763
|
|
|
* @return string|Zend_Gdata_App_Feed Returns string only if the object |
|
764
|
|
|
* mapping has been disabled explicitly |
|
765
|
|
|
* by passing false to the |
|
766
|
|
|
* useObjectMapping() function. |
|
767
|
|
|
*/ |
|
768
|
|
|
public function importUrl($url, $className='Zend_Gdata_App_Feed', |
|
769
|
|
|
$extraHeaders = array()) |
|
770
|
|
|
{ |
|
771
|
|
|
|
|
772
|
|
|
|
|
773
|
|
|
$response = $this->get($url, $extraHeaders); |
|
774
|
|
|
|
|
775
|
|
|
$feedContent = $response->getBody(); |
|
776
|
|
|
if (!$this->_useObjectMapping) { |
|
777
|
|
|
return $feedContent; |
|
778
|
|
|
} |
|
779
|
|
|
|
|
780
|
|
|
$protocolVersionStr = $response->getHeader('GData-Version'); |
|
781
|
|
|
$majorProtocolVersion = null; |
|
782
|
|
|
$minorProtocolVersion = null; |
|
783
|
|
|
if ($protocolVersionStr !== null) { |
|
784
|
|
|
// Extract protocol major and minor version from header |
|
785
|
|
|
$delimiterPos = strpos($protocolVersionStr, '.'); |
|
|
|
|
|
|
786
|
|
|
$length = strlen($protocolVersionStr); |
|
|
|
|
|
|
787
|
|
|
$major = substr($protocolVersionStr, 0, $delimiterPos); |
|
|
|
|
|
|
788
|
|
|
$minor = substr($protocolVersionStr, $delimiterPos + 1, $length); |
|
789
|
|
|
$majorProtocolVersion = $major; |
|
790
|
|
|
$minorProtocolVersion = $minor; |
|
791
|
|
|
} |
|
792
|
|
|
|
|
793
|
|
|
$feed = self::importString($feedContent, $className, |
|
794
|
|
|
$majorProtocolVersion, $minorProtocolVersion); |
|
795
|
|
|
|
|
796
|
|
|
|
|
797
|
|
|
if ($this->getHttpClient() != null) { |
|
798
|
|
|
$feed->setHttpClient($this->getHttpClient()); |
|
|
|
|
|
|
799
|
|
|
} |
|
800
|
|
|
|
|
801
|
|
|
$etag = $response->getHeader('ETag'); |
|
802
|
|
|
if ($etag !== null) { |
|
803
|
|
|
$feed->setEtag($etag); |
|
|
|
|
|
|
804
|
|
|
} |
|
805
|
|
|
return $feed; |
|
806
|
|
|
} |
|
807
|
|
|
|
|
808
|
|
|
|
|
809
|
|
|
/** |
|
810
|
|
|
* Imports a feed represented by $string. |
|
811
|
|
|
* |
|
812
|
|
|
* @param string $string |
|
813
|
|
|
* @param string $className The class which is used as the return type |
|
814
|
|
|
* @param integer $majorProcolVersion (optional) The major protocol version |
|
815
|
|
|
* of the data model object that is to be created. |
|
816
|
|
|
* @param integer $minorProcolVersion (optional) The minor protocol version |
|
817
|
|
|
* of the data model object that is to be created. |
|
818
|
|
|
* @throws Zend_Gdata_App_Exception |
|
819
|
|
|
* @return Zend_Gdata_App_Feed |
|
820
|
|
|
*/ |
|
821
|
|
|
public static function importString($string, |
|
822
|
|
|
$className='Zend_Gdata_App_Feed', $majorProtocolVersion = null, |
|
823
|
|
|
$minorProtocolVersion = null) |
|
824
|
|
|
{ |
|
825
|
|
|
|
|
826
|
|
|
|
|
827
|
|
|
if (!class_exists($className, false)) { |
|
828
|
|
|
require_once 'Zend/Loader.php'; |
|
829
|
|
|
@Zend_Loader::loadClass($className); |
|
|
|
|
|
|
830
|
|
|
} |
|
831
|
|
|
|
|
832
|
|
|
// Load the feed as an XML DOMDocument object |
|
833
|
|
|
@ini_set('track_errors', 1); |
|
|
|
|
|
|
834
|
|
|
$doc = new DOMDocument(); |
|
835
|
|
|
//$doc = @Zend_Xml_Security::scan($string, $doc); |
|
836
|
|
|
$doc->loadXML($string); |
|
837
|
|
|
@ini_restore('track_errors'); |
|
|
|
|
|
|
838
|
|
|
|
|
839
|
|
|
|
|
840
|
|
|
if (!$doc) { |
|
|
|
|
|
|
841
|
|
|
require_once 'Zend/Gdata/App/Exception.php'; |
|
842
|
|
|
throw new Zend_Gdata_App_Exception( |
|
843
|
|
|
"DOMDocument cannot parse XML: $php_errormsg"); |
|
844
|
|
|
} |
|
845
|
|
|
|
|
846
|
|
|
|
|
847
|
|
|
$feed = new $className(); |
|
848
|
|
|
|
|
849
|
|
|
$feed->setMajorProtocolVersion($majorProtocolVersion); |
|
850
|
|
|
$feed->setMinorProtocolVersion($minorProtocolVersion); |
|
851
|
|
|
$feed->transferFromXML($string); |
|
852
|
|
|
$feed->setHttpClient(self::getstaticHttpClient()); |
|
853
|
|
|
|
|
854
|
|
|
return $feed; |
|
855
|
|
|
} |
|
856
|
|
|
|
|
857
|
|
|
|
|
858
|
|
|
/** |
|
859
|
|
|
* Imports a feed from a file located at $filename. |
|
860
|
|
|
* |
|
861
|
|
|
* @param string $filename |
|
862
|
|
|
* @param string $className The class which is used as the return type |
|
863
|
|
|
* @param string $useIncludePath Whether the include_path should be searched |
|
864
|
|
|
* @throws Zend_Gdata_App_Exception |
|
865
|
|
|
* @return Zend_Gdata_App_Feed |
|
866
|
|
|
*/ |
|
867
|
|
|
public static function importFile($filename, |
|
868
|
|
|
$className='Zend_Gdata_App_Feed', $useIncludePath = false) |
|
869
|
|
|
{ |
|
870
|
|
|
@ini_set('track_errors', 1); |
|
|
|
|
|
|
871
|
|
|
$feed = @file_get_contents($filename, $useIncludePath); |
|
|
|
|
|
|
872
|
|
|
@ini_restore('track_errors'); |
|
|
|
|
|
|
873
|
|
|
if ($feed === false) { |
|
874
|
|
|
require_once 'Zend/Gdata/App/Exception.php'; |
|
875
|
|
|
throw new Zend_Gdata_App_Exception( |
|
876
|
|
|
"File could not be loaded: $php_errormsg"); |
|
877
|
|
|
} |
|
878
|
|
|
return self::importString($feed, $className); |
|
879
|
|
|
} |
|
880
|
|
|
|
|
881
|
|
|
/** |
|
882
|
|
|
* GET a URI using client object. |
|
883
|
|
|
* |
|
884
|
|
|
* @param string $uri GET URI |
|
885
|
|
|
* @param array $extraHeaders Extra headers to add to the request, as an |
|
886
|
|
|
* array of string-based key/value pairs. |
|
887
|
|
|
* @throws Zend_Gdata_App_HttpException |
|
888
|
|
|
* @return Zend_Http_Response |
|
889
|
|
|
*/ |
|
890
|
|
|
public function get($uri, $extraHeaders = array()) |
|
891
|
|
|
{ |
|
892
|
|
|
$requestData = $this->prepareRequest('GET', $uri, $extraHeaders); |
|
893
|
|
|
return $this->performHttpRequest( |
|
894
|
|
|
$requestData['method'], $requestData['url'], |
|
895
|
|
|
$requestData['headers']); |
|
896
|
|
|
} |
|
897
|
|
|
|
|
898
|
|
|
/** |
|
899
|
|
|
* POST data with client object |
|
900
|
|
|
* |
|
901
|
|
|
* @param mixed $data The Zend_Gdata_App_Entry or XML to post |
|
902
|
|
|
* @param string $uri POST URI |
|
903
|
|
|
* @param array $headers Additional HTTP headers to insert. |
|
904
|
|
|
* @param string $contentType Content-type of the data |
|
905
|
|
|
* @param array $extraHeaders Extra headers to add to the request, as an |
|
906
|
|
|
* array of string-based key/value pairs. |
|
907
|
|
|
* @return Zend_Http_Response |
|
908
|
|
|
* @throws Zend_Gdata_App_Exception |
|
909
|
|
|
* @throws Zend_Gdata_App_HttpException |
|
910
|
|
|
* @throws Zend_Gdata_App_InvalidArgumentException |
|
911
|
|
|
*/ |
|
912
|
|
|
public function post($data, $uri = null, $remainingRedirects = null, |
|
|
|
|
|
|
913
|
|
|
$contentType = null, $extraHeaders = null) |
|
914
|
|
|
{ |
|
915
|
|
|
$requestData = $this->prepareRequest( |
|
916
|
|
|
'POST', $uri, $extraHeaders, $data, $contentType); |
|
917
|
|
|
return $this->performHttpRequest( |
|
918
|
|
|
$requestData['method'], $requestData['url'], |
|
919
|
|
|
$requestData['headers'], $requestData['data'], |
|
|
|
|
|
|
920
|
|
|
$requestData['contentType']); |
|
921
|
|
|
} |
|
922
|
|
|
|
|
923
|
|
|
/** |
|
924
|
|
|
* PUT data with client object |
|
925
|
|
|
* |
|
926
|
|
|
* @param mixed $data The Zend_Gdata_App_Entry or XML to post |
|
927
|
|
|
* @param string $uri PUT URI |
|
928
|
|
|
* @param array $headers Additional HTTP headers to insert. |
|
929
|
|
|
* @param string $contentType Content-type of the data |
|
930
|
|
|
* @param array $extraHeaders Extra headers to add to the request, as an |
|
931
|
|
|
* array of string-based key/value pairs. |
|
932
|
|
|
* @return Zend_Http_Response |
|
933
|
|
|
* @throws Zend_Gdata_App_Exception |
|
934
|
|
|
* @throws Zend_Gdata_App_HttpException |
|
935
|
|
|
* @throws Zend_Gdata_App_InvalidArgumentException |
|
936
|
|
|
*/ |
|
937
|
|
|
public function put($data, $uri = null, $remainingRedirects = null, |
|
|
|
|
|
|
938
|
|
|
$contentType = null, $extraHeaders = null) |
|
939
|
|
|
{ |
|
940
|
|
|
$requestData = $this->prepareRequest( |
|
941
|
|
|
'PUT', $uri, $extraHeaders, $data, $contentType); |
|
942
|
|
|
return $this->performHttpRequest( |
|
943
|
|
|
$requestData['method'], $requestData['url'], |
|
944
|
|
|
$requestData['headers'], $requestData['data'], |
|
|
|
|
|
|
945
|
|
|
$requestData['contentType']); |
|
946
|
|
|
} |
|
947
|
|
|
|
|
948
|
|
|
/** |
|
949
|
|
|
* DELETE entry with client object |
|
950
|
|
|
* |
|
951
|
|
|
* @param mixed $data The Zend_Gdata_App_Entry or URL to delete |
|
952
|
|
|
* @return void |
|
953
|
|
|
* @throws Zend_Gdata_App_Exception |
|
954
|
|
|
* @throws Zend_Gdata_App_HttpException |
|
955
|
|
|
* @throws Zend_Gdata_App_InvalidArgumentException |
|
956
|
|
|
*/ |
|
957
|
|
|
public function delete($data, $remainingRedirects = null) |
|
958
|
|
|
{ |
|
959
|
|
|
if (is_string($data)) { |
|
960
|
|
|
$requestData = $this->prepareRequest('DELETE', $data); |
|
961
|
|
|
} else { |
|
962
|
|
|
$headers = array(); |
|
963
|
|
|
|
|
964
|
|
|
$requestData = $this->prepareRequest( |
|
965
|
|
|
'DELETE', null, $headers, $data); |
|
966
|
|
|
} |
|
967
|
|
|
return $this->performHttpRequest($requestData['method'], |
|
|
|
|
|
|
968
|
|
|
$requestData['url'], |
|
969
|
|
|
$requestData['headers'], |
|
970
|
|
|
'', |
|
971
|
|
|
$requestData['contentType'], |
|
972
|
|
|
$remainingRedirects); |
|
973
|
|
|
} |
|
974
|
|
|
|
|
975
|
|
|
/** |
|
976
|
|
|
* Inserts an entry to a given URI and returns the response as a |
|
977
|
|
|
* fully formed Entry. |
|
978
|
|
|
* |
|
979
|
|
|
* @param mixed $data The Zend_Gdata_App_Entry or XML to post |
|
980
|
|
|
* @param string $uri POST URI |
|
981
|
|
|
* @param string $className The class of entry to be returned. |
|
982
|
|
|
* @param array $extraHeaders Extra headers to add to the request, as an |
|
983
|
|
|
* array of string-based key/value pairs. |
|
984
|
|
|
* @return Zend_Gdata_App_Entry The entry returned by the service after |
|
985
|
|
|
* insertion. |
|
986
|
|
|
*/ |
|
987
|
|
|
public function insertEntry($data, $uri, $className='Zend_Gdata_App_Entry', |
|
988
|
|
|
$extraHeaders = array()) |
|
989
|
|
|
{ |
|
990
|
|
|
if (!class_exists($className, false)) { |
|
991
|
|
|
require_once 'Zend/Loader.php'; |
|
992
|
|
|
@Zend_Loader::loadClass($className); |
|
|
|
|
|
|
993
|
|
|
} |
|
994
|
|
|
|
|
995
|
|
|
$response = $this->post($data, $uri, null, null, $extraHeaders); |
|
996
|
|
|
|
|
997
|
|
|
$returnEntry = new $className($response->getBody()); |
|
998
|
|
|
$returnEntry->setHttpClient(self::getstaticHttpClient()); |
|
999
|
|
|
|
|
1000
|
|
|
$etag = $response->getHeader('ETag'); |
|
1001
|
|
|
if ($etag !== null) { |
|
1002
|
|
|
$returnEntry->setEtag($etag); |
|
1003
|
|
|
} |
|
1004
|
|
|
|
|
1005
|
|
|
return $returnEntry; |
|
1006
|
|
|
} |
|
1007
|
|
|
|
|
1008
|
|
|
/** |
|
1009
|
|
|
* Update an entry |
|
1010
|
|
|
* |
|
1011
|
|
|
* @param mixed $data Zend_Gdata_App_Entry or XML (w/ID and link rel='edit') |
|
1012
|
|
|
* @param string|null The URI to send requests to, or null if $data |
|
|
|
|
|
|
1013
|
|
|
* contains the URI. |
|
1014
|
|
|
* @param string|null The name of the class that should be deserialized |
|
1015
|
|
|
* from the server response. If null, then 'Zend_Gdata_App_Entry' |
|
1016
|
|
|
* will be used. |
|
1017
|
|
|
* @param array $extraHeaders Extra headers to add to the request, as an |
|
1018
|
|
|
* array of string-based key/value pairs. |
|
1019
|
|
|
* @return Zend_Gdata_App_Entry The entry returned from the server |
|
1020
|
|
|
* @throws Zend_Gdata_App_Exception |
|
1021
|
|
|
*/ |
|
1022
|
|
|
public function updateEntry($data, $uri = null, $className = null, |
|
1023
|
|
|
$extraHeaders = array()) |
|
1024
|
|
|
{ |
|
1025
|
|
|
if ($className === null && $data instanceof Zend_Gdata_App_Entry) { |
|
1026
|
|
|
$className = get_class($data); |
|
1027
|
|
|
} elseif ($className === null) { |
|
1028
|
|
|
$className = 'Zend_Gdata_App_Entry'; |
|
1029
|
|
|
} |
|
1030
|
|
|
|
|
1031
|
|
|
if (!class_exists($className, false)) { |
|
1032
|
|
|
require_once 'Zend/Loader.php'; |
|
1033
|
|
|
@Zend_Loader::loadClass($className); |
|
|
|
|
|
|
1034
|
|
|
} |
|
1035
|
|
|
|
|
1036
|
|
|
$response = $this->put($data, $uri, null, null, $extraHeaders); |
|
1037
|
|
|
$returnEntry = new $className($response->getBody()); |
|
1038
|
|
|
$returnEntry->setHttpClient(self::getstaticHttpClient()); |
|
1039
|
|
|
|
|
1040
|
|
|
$etag = $response->getHeader('ETag'); |
|
1041
|
|
|
if ($etag !== null) { |
|
1042
|
|
|
$returnEntry->setEtag($etag); |
|
1043
|
|
|
} |
|
1044
|
|
|
|
|
1045
|
|
|
return $returnEntry; |
|
1046
|
|
|
} |
|
1047
|
|
|
|
|
1048
|
|
|
/** |
|
1049
|
|
|
* Provides a magic factory method to instantiate new objects with |
|
1050
|
|
|
* shorter syntax than would otherwise be required by the Zend Framework |
|
1051
|
|
|
* naming conventions. For instance, to construct a new |
|
1052
|
|
|
* Zend_Gdata_Calendar_Extension_Color, a developer simply needs to do |
|
1053
|
|
|
* $gCal->newColor(). For this magic constructor, packages are searched |
|
1054
|
|
|
* in the same order as which they appear in the $_registeredPackages |
|
1055
|
|
|
* array |
|
1056
|
|
|
* |
|
1057
|
|
|
* @param string $method The method name being called |
|
1058
|
|
|
* @param array $args The arguments passed to the call |
|
1059
|
|
|
* @throws Zend_Gdata_App_Exception |
|
1060
|
|
|
*/ |
|
1061
|
|
|
public function __call($method, $args) |
|
1062
|
|
|
{ |
|
1063
|
|
|
if (preg_match('/^new(\w+)/', $method, $matches)) { |
|
1064
|
|
|
$class = $matches[1]; |
|
1065
|
|
|
$foundClassName = null; |
|
1066
|
|
|
foreach ($this->_registeredPackages as $name) { |
|
1067
|
|
|
try { |
|
1068
|
|
|
// Autoloading disabled on next line for compatibility |
|
1069
|
|
|
// with magic factories. See ZF-6660. |
|
1070
|
|
|
if (!class_exists($name . '_' . $class, false)) { |
|
1071
|
|
|
require_once 'Zend/Loader.php'; |
|
1072
|
|
|
@Zend_Loader::loadClass($name . '_' . $class); |
|
|
|
|
|
|
1073
|
|
|
} |
|
1074
|
|
|
$foundClassName = $name . '_' . $class; |
|
1075
|
|
|
break; |
|
1076
|
|
|
} catch (Zend_Exception $e) { |
|
1077
|
|
|
// package wasn't here- continue searching |
|
1078
|
|
|
} catch (ErrorException $e) { |
|
1079
|
|
|
// package wasn't here- continue searching |
|
1080
|
|
|
// @see ZF-7013 and ZF-11959 |
|
1081
|
|
|
} |
|
1082
|
|
|
} |
|
1083
|
|
|
if ($foundClassName != null) { |
|
|
|
|
|
|
1084
|
|
|
$reflectionObj = new ReflectionClass($foundClassName); |
|
1085
|
|
|
$instance = $reflectionObj->newInstanceArgs($args); |
|
1086
|
|
|
if ($instance instanceof Zend_Gdata_App_FeedEntryParent) { |
|
1087
|
|
|
$instance->setHttpClient($this->_httpClient); |
|
|
|
|
|
|
1088
|
|
|
|
|
1089
|
|
|
// Propogate version data |
|
1090
|
|
|
$instance->setMajorProtocolVersion( |
|
1091
|
|
|
$this->_majorProtocolVersion); |
|
1092
|
|
|
$instance->setMinorProtocolVersion( |
|
1093
|
|
|
$this->_minorProtocolVersion); |
|
1094
|
|
|
} |
|
1095
|
|
|
return $instance; |
|
1096
|
|
|
} else { |
|
1097
|
|
|
require_once 'Zend/Gdata/App/Exception.php'; |
|
1098
|
|
|
throw new Zend_Gdata_App_Exception( |
|
1099
|
|
|
"Unable to find '${class}' in registered packages"); |
|
1100
|
|
|
} |
|
1101
|
|
|
} else { |
|
1102
|
|
|
require_once 'Zend/Gdata/App/Exception.php'; |
|
1103
|
|
|
throw new Zend_Gdata_App_Exception("No such method ${method}"); |
|
1104
|
|
|
} |
|
1105
|
|
|
} |
|
1106
|
|
|
|
|
1107
|
|
|
/** |
|
1108
|
|
|
* Retrieve all entries for a feed, iterating through pages as necessary. |
|
1109
|
|
|
* Be aware that calling this function on a large dataset will take a |
|
1110
|
|
|
* significant amount of time to complete. In some cases this may cause |
|
1111
|
|
|
* execution to timeout without proper precautions in place. |
|
1112
|
|
|
* |
|
1113
|
|
|
* @param object $feed The feed to iterate through. |
|
1114
|
|
|
* @return mixed A new feed of the same type as the one originally |
|
1115
|
|
|
* passed in, containing all relevent entries. |
|
1116
|
|
|
*/ |
|
1117
|
|
|
public function retrieveAllEntriesForFeed($feed) { |
|
1118
|
|
|
$feedClass = get_class($feed); |
|
1119
|
|
|
$reflectionObj = new ReflectionClass($feedClass); |
|
1120
|
|
|
$result = $reflectionObj->newInstance(); |
|
1121
|
|
|
do { |
|
1122
|
|
|
foreach ($feed as $entry) { |
|
1123
|
|
|
$result->addEntry($entry); |
|
1124
|
|
|
} |
|
1125
|
|
|
|
|
1126
|
|
|
$next = $feed->getLink('next'); |
|
1127
|
|
|
if ($next !== null) { |
|
1128
|
|
|
$feed = $this->getFeed($next->href, $feedClass); |
|
1129
|
|
|
} else { |
|
1130
|
|
|
$feed = null; |
|
1131
|
|
|
} |
|
1132
|
|
|
} |
|
1133
|
|
|
while ($feed != null); |
|
|
|
|
|
|
1134
|
|
|
return $result; |
|
1135
|
|
|
} |
|
1136
|
|
|
|
|
1137
|
|
|
/** |
|
1138
|
|
|
* This method enables logging of requests by changing the |
|
1139
|
|
|
* Zend_Http_Client_Adapter used for performing the requests. |
|
1140
|
|
|
* NOTE: This will not work if you have customized the adapter |
|
1141
|
|
|
* already to use a proxy server or other interface. |
|
1142
|
|
|
* |
|
1143
|
|
|
* @param string $logfile The logfile to use when logging the requests |
|
1144
|
|
|
*/ |
|
1145
|
|
|
public function enableRequestDebugLogging($logfile) |
|
1146
|
|
|
{ |
|
1147
|
|
|
$this->_httpClient->setConfig(array( |
|
1148
|
|
|
'adapter' => 'Zend_Gdata_App_LoggingHttpClientAdapterSocket', |
|
1149
|
|
|
'logfile' => $logfile |
|
1150
|
|
|
)); |
|
1151
|
|
|
} |
|
1152
|
|
|
|
|
1153
|
|
|
/** |
|
1154
|
|
|
* Retrieve next set of results based on a given feed. |
|
1155
|
|
|
* |
|
1156
|
|
|
* @param Zend_Gdata_App_Feed $feed The feed from which to |
|
1157
|
|
|
* retreive the next set of results. |
|
1158
|
|
|
* @param string $className (optional) The class of feed to be returned. |
|
1159
|
|
|
* If null, the next feed (if found) will be the same class as |
|
1160
|
|
|
* the feed that was given as the first argument. |
|
1161
|
|
|
* @return Zend_Gdata_App_Feed|null Returns a |
|
1162
|
|
|
* Zend_Gdata_App_Feed or null if no next set of results |
|
1163
|
|
|
* exists. |
|
1164
|
|
|
*/ |
|
1165
|
|
|
public function getNextFeed($feed, $className = null) |
|
1166
|
|
|
{ |
|
1167
|
|
|
$nextLink = $feed->getNextLink(); |
|
1168
|
|
|
if (!$nextLink) { |
|
|
|
|
|
|
1169
|
|
|
return null; |
|
1170
|
|
|
} |
|
1171
|
|
|
$nextLinkHref = $nextLink->getHref(); |
|
1172
|
|
|
|
|
1173
|
|
|
if ($className === null) { |
|
1174
|
|
|
$className = get_class($feed); |
|
1175
|
|
|
} |
|
1176
|
|
|
|
|
1177
|
|
|
return $this->getFeed($nextLinkHref, $className); |
|
|
|
|
|
|
1178
|
|
|
} |
|
1179
|
|
|
|
|
1180
|
|
|
/** |
|
1181
|
|
|
* Retrieve previous set of results based on a given feed. |
|
1182
|
|
|
* |
|
1183
|
|
|
* @param Zend_Gdata_App_Feed $feed The feed from which to |
|
1184
|
|
|
* retreive the previous set of results. |
|
1185
|
|
|
* @param string $className (optional) The class of feed to be returned. |
|
1186
|
|
|
* If null, the previous feed (if found) will be the same class as |
|
1187
|
|
|
* the feed that was given as the first argument. |
|
1188
|
|
|
* @return Zend_Gdata_App_Feed|null Returns a |
|
1189
|
|
|
* Zend_Gdata_App_Feed or null if no previous set of results |
|
1190
|
|
|
* exists. |
|
1191
|
|
|
*/ |
|
1192
|
|
|
public function getPreviousFeed($feed, $className = null) |
|
1193
|
|
|
{ |
|
1194
|
|
|
$previousLink = $feed->getPreviousLink(); |
|
1195
|
|
|
if (!$previousLink) { |
|
|
|
|
|
|
1196
|
|
|
return null; |
|
1197
|
|
|
} |
|
1198
|
|
|
$previousLinkHref = $previousLink->getHref(); |
|
1199
|
|
|
|
|
1200
|
|
|
if ($className === null) { |
|
1201
|
|
|
$className = get_class($feed); |
|
1202
|
|
|
} |
|
1203
|
|
|
|
|
1204
|
|
|
return $this->getFeed($previousLinkHref, $className); |
|
|
|
|
|
|
1205
|
|
|
} |
|
1206
|
|
|
|
|
1207
|
|
|
/** |
|
1208
|
|
|
* Returns the data for an If-Match header based on the current Etag |
|
1209
|
|
|
* property. If Etags are not supported by the server or cannot be |
|
1210
|
|
|
* extracted from the data, then null will be returned. |
|
1211
|
|
|
* |
|
1212
|
|
|
* @param boolean $allowWeak If false, then if a weak Etag is detected, |
|
1213
|
|
|
* then return null rather than the Etag. |
|
1214
|
|
|
* @return string|null $data |
|
1215
|
|
|
*/ |
|
1216
|
|
|
public function generateIfMatchHeaderData($data, $allowWeek) |
|
1217
|
|
|
{ |
|
1218
|
|
|
$result = ''; |
|
1219
|
|
|
// Set an If-Match header if an ETag has been set (version >= 2 only) |
|
1220
|
|
|
if ($this->_majorProtocolVersion >= 2 && |
|
1221
|
|
|
$data instanceof Zend_Gdata_App_Entry) { |
|
1222
|
|
|
$etag = $data->getEtag(); |
|
1223
|
|
|
if (($etag !== null) && |
|
1224
|
|
|
($allowWeek || substr($etag, 0, 2) != 'W/')) { |
|
1225
|
|
|
$result = $data->getEtag(); |
|
1226
|
|
|
} |
|
1227
|
|
|
} |
|
1228
|
|
|
return $result; |
|
1229
|
|
|
} |
|
1230
|
|
|
|
|
1231
|
|
|
/** |
|
1232
|
|
|
* Determine whether service object is using XML to object mapping. |
|
1233
|
|
|
* |
|
1234
|
|
|
* @return boolean True if service object is using XML to object mapping, |
|
1235
|
|
|
* false otherwise. |
|
1236
|
|
|
*/ |
|
1237
|
|
|
public function usingObjectMapping() |
|
1238
|
|
|
{ |
|
1239
|
|
|
return $this->_useObjectMapping; |
|
1240
|
|
|
} |
|
1241
|
|
|
|
|
1242
|
|
|
/** |
|
1243
|
|
|
* Enable/disable the use of XML to object mapping. |
|
1244
|
|
|
* |
|
1245
|
|
|
* @param boolean $value Pass in true to use the XML to object mapping. |
|
1246
|
|
|
* Pass in false or null to disable it. |
|
1247
|
|
|
* @return void |
|
1248
|
|
|
*/ |
|
1249
|
|
|
public function useObjectMapping($value) |
|
1250
|
|
|
{ |
|
1251
|
|
|
if ($value === True) { |
|
1252
|
|
|
$this->_useObjectMapping = true; |
|
1253
|
|
|
} else { |
|
1254
|
|
|
$this->_useObjectMapping = false; |
|
1255
|
|
|
} |
|
1256
|
|
|
} |
|
1257
|
|
|
|
|
1258
|
|
|
} |
|
1259
|
|
|
|