REST.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. /**
  3. * PEAR_REST
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * @category pear
  8. * @package PEAR
  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
  15. */
  16. /**
  17. * For downloading xml files
  18. */
  19. require_once 'PEAR.php';
  20. require_once 'PEAR/XMLParser.php';
  21. /**
  22. * Intelligently retrieve data, following hyperlinks if necessary, and re-directing
  23. * as well
  24. * @category pear
  25. * @package PEAR
  26. * @author Greg Beaver <cellog@php.net>
  27. * @copyright 1997-2009 The Authors
  28. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  29. * @version Release: 1.9.4
  30. * @link http://pear.php.net/package/PEAR
  31. * @since Class available since Release 1.4.0a1
  32. */
  33. class PEAR_REST
  34. {
  35. var $config;
  36. var $_options;
  37. function PEAR_REST(&$config, $options = array())
  38. {
  39. $this->config = &$config;
  40. $this->_options = $options;
  41. }
  42. /**
  43. * Retrieve REST data, but always retrieve the local cache if it is available.
  44. *
  45. * This is useful for elements that should never change, such as information on a particular
  46. * release
  47. * @param string full URL to this resource
  48. * @param array|false contents of the accept-encoding header
  49. * @param boolean if true, xml will be returned as a string, otherwise, xml will be
  50. * parsed using PEAR_XMLParser
  51. * @return string|array
  52. */
  53. function retrieveCacheFirst($url, $accept = false, $forcestring = false, $channel = false)
  54. {
  55. $cachefile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
  56. md5($url) . 'rest.cachefile';
  57. if (file_exists($cachefile)) {
  58. return unserialize(implode('', file($cachefile)));
  59. }
  60. return $this->retrieveData($url, $accept, $forcestring, $channel);
  61. }
  62. /**
  63. * Retrieve a remote REST resource
  64. * @param string full URL to this resource
  65. * @param array|false contents of the accept-encoding header
  66. * @param boolean if true, xml will be returned as a string, otherwise, xml will be
  67. * parsed using PEAR_XMLParser
  68. * @return string|array
  69. */
  70. function retrieveData($url, $accept = false, $forcestring = false, $channel = false)
  71. {
  72. $cacheId = $this->getCacheId($url);
  73. if ($ret = $this->useLocalCache($url, $cacheId)) {
  74. return $ret;
  75. }
  76. $file = $trieddownload = false;
  77. if (!isset($this->_options['offline'])) {
  78. $trieddownload = true;
  79. $file = $this->downloadHttp($url, $cacheId ? $cacheId['lastChange'] : false, $accept, $channel);
  80. }
  81. if (PEAR::isError($file)) {
  82. if ($file->getCode() !== -9276) {
  83. return $file;
  84. }
  85. $trieddownload = false;
  86. $file = false; // use local copy if available on socket connect error
  87. }
  88. if (!$file) {
  89. $ret = $this->getCache($url);
  90. if (!PEAR::isError($ret) && $trieddownload) {
  91. // reset the age of the cache if the server says it was unmodified
  92. $result = $this->saveCache($url, $ret, null, true, $cacheId);
  93. if (PEAR::isError($result)) {
  94. return PEAR::raiseError($result->getMessage());
  95. }
  96. }
  97. return $ret;
  98. }
  99. if (is_array($file)) {
  100. $headers = $file[2];
  101. $lastmodified = $file[1];
  102. $content = $file[0];
  103. } else {
  104. $headers = array();
  105. $lastmodified = false;
  106. $content = $file;
  107. }
  108. if ($forcestring) {
  109. $result = $this->saveCache($url, $content, $lastmodified, false, $cacheId);
  110. if (PEAR::isError($result)) {
  111. return PEAR::raiseError($result->getMessage());
  112. }
  113. return $content;
  114. }
  115. if (isset($headers['content-type'])) {
  116. switch ($headers['content-type']) {
  117. case 'text/xml' :
  118. case 'application/xml' :
  119. case 'text/plain' :
  120. if ($headers['content-type'] === 'text/plain') {
  121. $check = substr($content, 0, 5);
  122. if ($check !== '<?xml') {
  123. break;
  124. }
  125. }
  126. $parser = new PEAR_XMLParser;
  127. PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  128. $err = $parser->parse($content);
  129. PEAR::popErrorHandling();
  130. if (PEAR::isError($err)) {
  131. return PEAR::raiseError('Invalid xml downloaded from "' . $url . '": ' .
  132. $err->getMessage());
  133. }
  134. $content = $parser->getData();
  135. case 'text/html' :
  136. default :
  137. // use it as a string
  138. }
  139. } else {
  140. // assume XML
  141. $parser = new PEAR_XMLParser;
  142. $parser->parse($content);
  143. $content = $parser->getData();
  144. }
  145. $result = $this->saveCache($url, $content, $lastmodified, false, $cacheId);
  146. if (PEAR::isError($result)) {
  147. return PEAR::raiseError($result->getMessage());
  148. }
  149. return $content;
  150. }
  151. function useLocalCache($url, $cacheid = null)
  152. {
  153. if ($cacheid === null) {
  154. $cacheidfile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
  155. md5($url) . 'rest.cacheid';
  156. if (!file_exists($cacheidfile)) {
  157. return false;
  158. }
  159. $cacheid = unserialize(implode('', file($cacheidfile)));
  160. }
  161. $cachettl = $this->config->get('cache_ttl');
  162. // If cache is newer than $cachettl seconds, we use the cache!
  163. if (time() - $cacheid['age'] < $cachettl) {
  164. return $this->getCache($url);
  165. }
  166. return false;
  167. }
  168. function getCacheId($url)
  169. {
  170. $cacheidfile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
  171. md5($url) . 'rest.cacheid';
  172. if (!file_exists($cacheidfile)) {
  173. return false;
  174. }
  175. $ret = unserialize(implode('', file($cacheidfile)));
  176. return $ret;
  177. }
  178. function getCache($url)
  179. {
  180. $cachefile = $this->config->get('cache_dir') . DIRECTORY_SEPARATOR .
  181. md5($url) . 'rest.cachefile';
  182. if (!file_exists($cachefile)) {
  183. return PEAR::raiseError('No cached content available for "' . $url . '"');
  184. }
  185. return unserialize(implode('', file($cachefile)));
  186. }
  187. /**
  188. * @param string full URL to REST resource
  189. * @param string original contents of the REST resource
  190. * @param array HTTP Last-Modified and ETag headers
  191. * @param bool if true, then the cache id file should be regenerated to
  192. * trigger a new time-to-live value
  193. */
  194. function saveCache($url, $contents, $lastmodified, $nochange = false, $cacheid = null)
  195. {
  196. $cache_dir = $this->config->get('cache_dir');
  197. $d = $cache_dir . DIRECTORY_SEPARATOR . md5($url);
  198. $cacheidfile = $d . 'rest.cacheid';
  199. $cachefile = $d . 'rest.cachefile';
  200. if (!is_dir($cache_dir)) {
  201. if (System::mkdir(array('-p', $cache_dir)) === false) {
  202. return PEAR::raiseError("The value of config option cache_dir ($cache_dir) is not a directory and attempts to create the directory failed.");
  203. }
  204. }
  205. if ($cacheid === null && $nochange) {
  206. $cacheid = unserialize(implode('', file($cacheidfile)));
  207. }
  208. $idData = serialize(array(
  209. 'age' => time(),
  210. 'lastChange' => ($nochange ? $cacheid['lastChange'] : $lastmodified),
  211. ));
  212. $result = $this->saveCacheFile($cacheidfile, $idData);
  213. if (PEAR::isError($result)) {
  214. return $result;
  215. } elseif ($nochange) {
  216. return true;
  217. }
  218. $result = $this->saveCacheFile($cachefile, serialize($contents));
  219. if (PEAR::isError($result)) {
  220. if (file_exists($cacheidfile)) {
  221. @unlink($cacheidfile);
  222. }
  223. return $result;
  224. }
  225. return true;
  226. }
  227. function saveCacheFile($file, $contents)
  228. {
  229. $len = strlen($contents);
  230. $cachefile_fp = @fopen($file, 'xb'); // x is the O_CREAT|O_EXCL mode
  231. if ($cachefile_fp !== false) { // create file
  232. if (fwrite($cachefile_fp, $contents, $len) < $len) {
  233. fclose($cachefile_fp);
  234. return PEAR::raiseError("Could not write $file.");
  235. }
  236. } else { // update file
  237. $cachefile_lstat = lstat($file);
  238. $cachefile_fp = @fopen($file, 'wb');
  239. if (!$cachefile_fp) {
  240. return PEAR::raiseError("Could not open $file for writing.");
  241. }
  242. $cachefile_fstat = fstat($cachefile_fp);
  243. if (
  244. $cachefile_lstat['mode'] == $cachefile_fstat['mode'] &&
  245. $cachefile_lstat['ino'] == $cachefile_fstat['ino'] &&
  246. $cachefile_lstat['dev'] == $cachefile_fstat['dev'] &&
  247. $cachefile_fstat['nlink'] === 1
  248. ) {
  249. if (fwrite($cachefile_fp, $contents, $len) < $len) {
  250. fclose($cachefile_fp);
  251. return PEAR::raiseError("Could not write $file.");
  252. }
  253. } else {
  254. fclose($cachefile_fp);
  255. $link = function_exists('readlink') ? readlink($file) : $file;
  256. return PEAR::raiseError('SECURITY ERROR: Will not write to ' . $file . ' as it is symlinked to ' . $link . ' - Possible symlink attack');
  257. }
  258. }
  259. fclose($cachefile_fp);
  260. return true;
  261. }
  262. /**
  263. * Efficiently Download a file through HTTP. Returns downloaded file as a string in-memory
  264. * This is best used for small files
  265. *
  266. * If an HTTP proxy has been configured (http_proxy PEAR_Config
  267. * setting), the proxy will be used.
  268. *
  269. * @param string $url the URL to download
  270. * @param string $save_dir directory to save file in
  271. * @param false|string|array $lastmodified header values to check against for caching
  272. * use false to return the header values from this download
  273. * @param false|array $accept Accept headers to send
  274. * @return string|array Returns the contents of the downloaded file or a PEAR
  275. * error on failure. If the error is caused by
  276. * socket-related errors, the error object will
  277. * have the fsockopen error code available through
  278. * getCode(). If caching is requested, then return the header
  279. * values.
  280. *
  281. * @access public
  282. */
  283. function downloadHttp($url, $lastmodified = null, $accept = false, $channel = false)
  284. {
  285. static $redirect = 0;
  286. // always reset , so we are clean case of error
  287. $wasredirect = $redirect;
  288. $redirect = 0;
  289. $info = parse_url($url);
  290. if (!isset($info['scheme']) || !in_array($info['scheme'], array('http', 'https'))) {
  291. return PEAR::raiseError('Cannot download non-http URL "' . $url . '"');
  292. }
  293. if (!isset($info['host'])) {
  294. return PEAR::raiseError('Cannot download from non-URL "' . $url . '"');
  295. }
  296. $host = isset($info['host']) ? $info['host'] : null;
  297. $port = isset($info['port']) ? $info['port'] : null;
  298. $path = isset($info['path']) ? $info['path'] : null;
  299. $schema = (isset($info['scheme']) && $info['scheme'] == 'https') ? 'https' : 'http';
  300. $proxy_host = $proxy_port = $proxy_user = $proxy_pass = '';
  301. if ($this->config->get('http_proxy')&&
  302. $proxy = parse_url($this->config->get('http_proxy'))
  303. ) {
  304. $proxy_host = isset($proxy['host']) ? $proxy['host'] : null;
  305. if ($schema === 'https') {
  306. $proxy_host = 'ssl://' . $proxy_host;
  307. }
  308. $proxy_port = isset($proxy['port']) ? $proxy['port'] : 8080;
  309. $proxy_user = isset($proxy['user']) ? urldecode($proxy['user']) : null;
  310. $proxy_pass = isset($proxy['pass']) ? urldecode($proxy['pass']) : null;
  311. $proxy_schema = (isset($proxy['scheme']) && $proxy['scheme'] == 'https') ? 'https' : 'http';
  312. }
  313. if (empty($port)) {
  314. $port = (isset($info['scheme']) && $info['scheme'] == 'https') ? 443 : 80;
  315. }
  316. if (isset($proxy['host'])) {
  317. $request = "GET $url HTTP/1.1\r\n";
  318. } else {
  319. $request = "GET $path HTTP/1.1\r\n";
  320. }
  321. $request .= "Host: $host\r\n";
  322. $ifmodifiedsince = '';
  323. if (is_array($lastmodified)) {
  324. if (isset($lastmodified['Last-Modified'])) {
  325. $ifmodifiedsince = 'If-Modified-Since: ' . $lastmodified['Last-Modified'] . "\r\n";
  326. }
  327. if (isset($lastmodified['ETag'])) {
  328. $ifmodifiedsince .= "If-None-Match: $lastmodified[ETag]\r\n";
  329. }
  330. } else {
  331. $ifmodifiedsince = ($lastmodified ? "If-Modified-Since: $lastmodified\r\n" : '');
  332. }
  333. $request .= $ifmodifiedsince .
  334. "User-Agent: PEAR/1.9.4/PHP/" . PHP_VERSION . "\r\n";
  335. $username = $this->config->get('username', null, $channel);
  336. $password = $this->config->get('password', null, $channel);
  337. if ($username && $password) {
  338. $tmp = base64_encode("$username:$password");
  339. $request .= "Authorization: Basic $tmp\r\n";
  340. }
  341. if ($proxy_host != '' && $proxy_user != '') {
  342. $request .= 'Proxy-Authorization: Basic ' .
  343. base64_encode($proxy_user . ':' . $proxy_pass) . "\r\n";
  344. }
  345. if ($accept) {
  346. $request .= 'Accept: ' . implode(', ', $accept) . "\r\n";
  347. }
  348. $request .= "Accept-Encoding:\r\n";
  349. $request .= "Connection: close\r\n";
  350. $request .= "\r\n";
  351. if ($proxy_host != '') {
  352. $fp = @fsockopen($proxy_host, $proxy_port, $errno, $errstr, 15);
  353. if (!$fp) {
  354. return PEAR::raiseError("Connection to `$proxy_host:$proxy_port' failed: $errstr", -9276);
  355. }
  356. } else {
  357. if ($schema === 'https') {
  358. $host = 'ssl://' . $host;
  359. }
  360. $fp = @fsockopen($host, $port, $errno, $errstr);
  361. if (!$fp) {
  362. return PEAR::raiseError("Connection to `$host:$port' failed: $errstr", $errno);
  363. }
  364. }
  365. fwrite($fp, $request);
  366. $headers = array();
  367. $reply = 0;
  368. while ($line = trim(fgets($fp, 1024))) {
  369. if (preg_match('/^([^:]+):\s+(.*)\s*\\z/', $line, $matches)) {
  370. $headers[strtolower($matches[1])] = trim($matches[2]);
  371. } elseif (preg_match('|^HTTP/1.[01] ([0-9]{3}) |', $line, $matches)) {
  372. $reply = (int)$matches[1];
  373. if ($reply == 304 && ($lastmodified || ($lastmodified === false))) {
  374. return false;
  375. }
  376. if (!in_array($reply, array(200, 301, 302, 303, 305, 307))) {
  377. return PEAR::raiseError("File $schema://$host:$port$path not valid (received: $line)");
  378. }
  379. }
  380. }
  381. if ($reply != 200) {
  382. if (!isset($headers['location'])) {
  383. return PEAR::raiseError("File $schema://$host:$port$path not valid (redirected but no location)");
  384. }
  385. if ($wasredirect > 4) {
  386. return PEAR::raiseError("File $schema://$host:$port$path not valid (redirection looped more than 5 times)");
  387. }
  388. $redirect = $wasredirect + 1;
  389. return $this->downloadHttp($headers['location'], $lastmodified, $accept, $channel);
  390. }
  391. $length = isset($headers['content-length']) ? $headers['content-length'] : -1;
  392. $data = '';
  393. while ($chunk = @fread($fp, 8192)) {
  394. $data .= $chunk;
  395. }
  396. fclose($fp);
  397. if ($lastmodified === false || $lastmodified) {
  398. if (isset($headers['etag'])) {
  399. $lastmodified = array('ETag' => $headers['etag']);
  400. }
  401. if (isset($headers['last-modified'])) {
  402. if (is_array($lastmodified)) {
  403. $lastmodified['Last-Modified'] = $headers['last-modified'];
  404. } else {
  405. $lastmodified = $headers['last-modified'];
  406. }
  407. }
  408. return array($data, $lastmodified, $headers);
  409. }
  410. return $data;
  411. }
  412. }