9 * @author Greg Beaver <cellog@php.net>
10 * @copyright 1997-2009 The Authors
11 * @license http://opensource.org/licenses/bsd-license.php New BSD License
12 * @version CVS: $Id: REST.php 313023 2011-07-06 19:17:11Z dufuz $
13 * @link http://pear.php.net/package/PEAR
14 * @since File available since Release 1.4.0a1
18 * For downloading xml files
20 require_once 'PEAR.php';
21 require_once 'PEAR/XMLParser.php';
24 * Intelligently retrieve data, following hyperlinks if necessary, and re-directing
28 * @author Greg Beaver <cellog@php.net>
29 * @copyright 1997-2009 The Authors
30 * @license http://opensource.org/licenses/bsd-license.php New BSD License
31 * @version Release: 1.9.4
32 * @link http://pear.php.net/package/PEAR
33 * @since Class available since Release 1.4.0a1
40 function PEAR_REST(&$config, $options = array())
42 $this->config = &$config;
43 $this->_options = $options;
47 * Retrieve REST data, but always retrieve the local cache if it is available.
49 * This is useful for elements that should never change, such as information on a particular
51 * @param string full URL to this resource
52 * @param array|false contents of the accept-encoding header
53 * @param boolean if true, xml will be returned as a string, otherwise, xml will be
54 * parsed using PEAR_XMLParser
55 * @return string|array
57 function retrieveCacheFirst($url, $accept = false, $forcestring = false, $channel = false)
59 $cachefile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
60 md5($url) . 'rest.cachefile';
62 if (file_exists($cachefile)) {
63 return unserialize(implode('', file($cachefile)));
66 return $this->retrieveData($url, $accept, $forcestring, $channel);
70 * Retrieve a remote REST resource
71 * @param string full URL to this resource
72 * @param array|false contents of the accept-encoding header
73 * @param boolean if true, xml will be returned as a string, otherwise, xml will be
74 * parsed using PEAR_XMLParser
75 * @return string|array
77 function retrieveData($url, $accept = false, $forcestring = false, $channel = false)
79 $cacheId = $this->getCacheId($url);
80 if ($ret = $this->useLocalCache($url, $cacheId)) {
84 $file = $trieddownload = false;
85 if (!isset($this->_options['offline'])) {
86 $trieddownload = true;
87 $file = $this->downloadHttp($url, $cacheId ? $cacheId['lastChange'] : false, $accept, $channel);
90 if (PEAR::isError($file)) {
91 if ($file->getCode() !== -9276) {
95 $trieddownload = false;
96 $file = false; // use local copy if available on socket connect error
100 $ret = $this->getCache($url);
101 if (!PEAR::isError($ret) && $trieddownload) {
102 // reset the age of the cache if the server says it was unmodified
103 $result = $this->saveCache($url, $ret, null, true, $cacheId);
104 if (PEAR::isError($result)) {
105 return PEAR::raiseError($result->getMessage());
112 if (is_array($file)) {
114 $lastmodified = $file[1];
118 $lastmodified = false;
123 $result = $this->saveCache($url, $content, $lastmodified, false, $cacheId);
124 if (PEAR::isError($result)) {
125 return PEAR::raiseError($result->getMessage());
131 if (isset($headers['content-type'])) {
132 switch ($headers['content-type']) {
134 case 'application/xml' :
136 if ($headers['content-type'] === 'text/plain') {
137 $check = substr($content, 0, 5);
138 if ($check !== '<?xml') {
143 $parser = new PEAR_XMLParser;
144 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
145 $err = $parser->parse($content);
146 PEAR::popErrorHandling();
147 if (PEAR::isError($err)) {
148 return PEAR::raiseError('Invalid xml downloaded from "' . $url . '": ' .
151 $content = $parser->getData();
154 // use it as a string
158 $parser = new PEAR_XMLParser;
159 $parser->parse($content);
160 $content = $parser->getData();
163 $result = $this->saveCache($url, $content, $lastmodified, false, $cacheId);
164 if (PEAR::isError($result)) {
165 return PEAR::raiseError($result->getMessage());
171 function useLocalCache($url, $cacheid = null)
173 if ($cacheid === null) {
174 $cacheidfile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
175 md5($url) . 'rest.cacheid';
176 if (!file_exists($cacheidfile)) {
180 $cacheid = unserialize(implode('', file($cacheidfile)));
183 $cachettl = $this->config->get('cache_ttl');
184 // If cache is newer than $cachettl seconds, we use the cache!
185 if (time() - $cacheid['age'] < $cachettl) {
186 return $this->getCache($url);
192 function getCacheId($url)
194 $cacheidfile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
195 md5($url) . 'rest.cacheid';
197 if (!file_exists($cacheidfile)) {
201 $ret = unserialize(implode('', file($cacheidfile)));
205 function getCache($url)
207 $cachefile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
208 md5($url) . 'rest.cachefile';
210 if (!file_exists($cachefile)) {
211 return PEAR::raiseError('No cached content available for "' . $url . '"');
214 return unserialize(implode('', file($cachefile)));
218 * @param string full URL to REST resource
219 * @param string original contents of the REST resource
220 * @param array HTTP Last-Modified and ETag headers
221 * @param bool if true, then the cache id file should be regenerated to
222 * trigger a new time-to-live value
224 function saveCache($url, $contents, $lastmodified, $nochange = false, $cacheid = null)
226 $cache_dir = $this->config->get('cache_dir');
227 $d = $cache_dir . DIRECTORY_SEPARATOR . md5($url);
228 $cacheidfile = $d . 'rest.cacheid';
229 $cachefile = $d . 'rest.cachefile';
231 if (!is_dir($cache_dir)) {
232 if (System::mkdir(array('-p', $cache_dir)) === false) {
233 return PEAR::raiseError("The value of config option cache_dir ($cache_dir) is not a directory and attempts to create the directory failed.");
237 if ($cacheid === null && $nochange) {
238 $cacheid = unserialize(implode('', file($cacheidfile)));
241 $idData = serialize(array(
243 'lastChange' => ($nochange ? $cacheid['lastChange'] : $lastmodified),
246 $result = $this->saveCacheFile($cacheidfile, $idData);
247 if (PEAR::isError($result)) {
249 } elseif ($nochange) {
253 $result = $this->saveCacheFile($cachefile, serialize($contents));
254 if (PEAR::isError($result)) {
255 if (file_exists($cacheidfile)) {
256 @unlink($cacheidfile);
265 function saveCacheFile($file, $contents)
267 $len = strlen($contents);
269 $cachefile_fp = @fopen($file, 'xb'); // x is the O_CREAT|O_EXCL mode
270 if ($cachefile_fp !== false) { // create file
271 if (fwrite($cachefile_fp, $contents, $len) < $len) {
272 fclose($cachefile_fp);
273 return PEAR::raiseError("Could not write $file.");
275 } else { // update file
276 $cachefile_lstat = lstat($file);
277 $cachefile_fp = @fopen($file, 'wb');
278 if (!$cachefile_fp) {
279 return PEAR::raiseError("Could not open $file for writing.");
282 $cachefile_fstat = fstat($cachefile_fp);
284 $cachefile_lstat['mode'] == $cachefile_fstat['mode'] &&
285 $cachefile_lstat['ino'] == $cachefile_fstat['ino'] &&
286 $cachefile_lstat['dev'] == $cachefile_fstat['dev'] &&
287 $cachefile_fstat['nlink'] === 1
289 if (fwrite($cachefile_fp, $contents, $len) < $len) {
290 fclose($cachefile_fp);
291 return PEAR::raiseError("Could not write $file.");
294 fclose($cachefile_fp);
295 $link = function_exists('readlink') ? readlink($file) : $file;
296 return PEAR::raiseError('SECURITY ERROR: Will not write to ' . $file . ' as it is symlinked to ' . $link . ' - Possible symlink attack');
300 fclose($cachefile_fp);
305 * Efficiently Download a file through HTTP. Returns downloaded file as a string in-memory
306 * This is best used for small files
308 * If an HTTP proxy has been configured (http_proxy PEAR_Config
309 * setting), the proxy will be used.
311 * @param string $url the URL to download
312 * @param string $save_dir directory to save file in
313 * @param false|string|array $lastmodified header values to check against for caching
314 * use false to return the header values from this download
315 * @param false|array $accept Accept headers to send
316 * @return string|array Returns the contents of the downloaded file or a PEAR
317 * error on failure. If the error is caused by
318 * socket-related errors, the error object will
319 * have the fsockopen error code available through
320 * getCode(). If caching is requested, then return the header
325 function downloadHttp($url, $lastmodified = null, $accept = false, $channel = false)
327 static $redirect = 0;
328 // always reset , so we are clean case of error
329 $wasredirect = $redirect;
332 $info = parse_url($url);
333 if (!isset($info['scheme']) || !in_array($info['scheme'], array('http', 'https'))) {
334 return PEAR::raiseError('Cannot download non-http URL "' . $url . '"');
337 if (!isset($info['host'])) {
338 return PEAR::raiseError('Cannot download from non-URL "' . $url . '"');
341 $host = isset($info['host']) ? $info['host'] : null;
342 $port = isset($info['port']) ? $info['port'] : null;
343 $path = isset($info['path']) ? $info['path'] : null;
344 $schema = (isset($info['scheme']) && $info['scheme'] == 'https') ? 'https' : 'http';
346 $proxy_host = $proxy_port = $proxy_user = $proxy_pass = '';
347 if ($this->config->get('http_proxy')&&
348 $proxy = parse_url($this->config->get('http_proxy'))
350 $proxy_host = isset($proxy['host']) ? $proxy['host'] : null;
351 if ($schema === 'https') {
352 $proxy_host = 'ssl://' . $proxy_host;
355 $proxy_port = isset($proxy['port']) ? $proxy['port'] : 8080;
356 $proxy_user = isset($proxy['user']) ? urldecode($proxy['user']) : null;
357 $proxy_pass = isset($proxy['pass']) ? urldecode($proxy['pass']) : null;
358 $proxy_schema = (isset($proxy['scheme']) && $proxy['scheme'] == 'https') ? 'https' : 'http';
362 $port = (isset($info['scheme']) && $info['scheme'] == 'https') ? 443 : 80;
365 if (isset($proxy['host'])) {
366 $request = "GET $url HTTP/1.1\r\n";
368 $request = "GET $path HTTP/1.1\r\n";
371 $request .= "Host: $host\r\n";
372 $ifmodifiedsince = '';
373 if (is_array($lastmodified)) {
374 if (isset($lastmodified['Last-Modified'])) {
375 $ifmodifiedsince = 'If-Modified-Since: ' . $lastmodified['Last-Modified'] . "\r\n";
378 if (isset($lastmodified['ETag'])) {
379 $ifmodifiedsince .= "If-None-Match: $lastmodified[ETag]\r\n";
382 $ifmodifiedsince = ($lastmodified ? "If-Modified-Since: $lastmodified\r\n" : '');
385 $request .= $ifmodifiedsince .
386 "User-Agent: PEAR/1.9.4/PHP/" . PHP_VERSION . "\r\n";
388 $username = $this->config->get('username', null, $channel);
389 $password = $this->config->get('password', null, $channel);
391 if ($username && $password) {
392 $tmp = base64_encode("$username:$password");
393 $request .= "Authorization: Basic $tmp\r\n";
396 if ($proxy_host != '' && $proxy_user != '') {
397 $request .= 'Proxy-Authorization: Basic ' .
398 base64_encode($proxy_user . ':' . $proxy_pass) . "\r\n";
402 $request .= 'Accept: ' . implode(', ', $accept) . "\r\n";
405 $request .= "Accept-Encoding:\r\n";
406 $request .= "Connection: close\r\n";
409 if ($proxy_host != '') {
410 $fp = @fsockopen($proxy_host, $proxy_port, $errno, $errstr, 15);
412 return PEAR::raiseError("Connection to `$proxy_host:$proxy_port' failed: $errstr", -9276);
415 if ($schema === 'https') {
416 $host = 'ssl://' . $host;
419 $fp = @fsockopen($host, $port, $errno, $errstr);
421 return PEAR::raiseError("Connection to `$host:$port' failed: $errstr", $errno);
425 fwrite($fp, $request);
429 while ($line = trim(fgets($fp, 1024))) {
430 if (preg_match('/^([^:]+):\s+(.*)\s*\\z/', $line, $matches)) {
431 $headers[strtolower($matches[1])] = trim($matches[2]);
432 } elseif (preg_match('|^HTTP/1.[01] ([0-9]{3}) |', $line, $matches)) {
433 $reply = (int)$matches[1];
434 if ($reply == 304 && ($lastmodified || ($lastmodified === false))) {
438 if (!in_array($reply, array(200, 301, 302, 303, 305, 307))) {
439 return PEAR::raiseError("File $schema://$host:$port$path not valid (received: $line)");
445 if (!isset($headers['location'])) {
446 return PEAR::raiseError("File $schema://$host:$port$path not valid (redirected but no location)");
449 if ($wasredirect > 4) {
450 return PEAR::raiseError("File $schema://$host:$port$path not valid (redirection looped more than 5 times)");
453 $redirect = $wasredirect + 1;
454 return $this->downloadHttp($headers['location'], $lastmodified, $accept, $channel);
457 $length = isset($headers['content-length']) ? $headers['content-length'] : -1;
460 while ($chunk = @fread($fp, 8192)) {
465 if ($lastmodified === false || $lastmodified) {
466 if (isset($headers['etag'])) {
467 $lastmodified = array('ETag' => $headers['etag']);
470 if (isset($headers['last-modified'])) {
471 if (is_array($lastmodified)) {
472 $lastmodified['Last-Modified'] = $headers['last-modified'];
474 $lastmodified = $headers['last-modified'];
478 return array($data, $lastmodified, $headers);