PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pkp/classes/file/wrappers/HTTPFileWrapper.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 134 lines | 101 code | 17 blank | 16 comment | 18 complexity | 9be60074c7133c0893bb73f2ca5ee3c3 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/file/wrappers/HTTPFileWrapper.inc.php
  4. *
  5. * Copyright (c) 2000-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @package file.wrappers
  9. * @ingroup file_wrappers
  10. *
  11. * Class providing a wrapper for the HTTP protocol.
  12. * (for when allow_url_fopen is disabled).
  13. *
  14. * $Id$
  15. */
  16. class HTTPFileWrapper extends FileWrapper {
  17. var $headers;
  18. var $defaultPort;
  19. var $defaultHost;
  20. var $defaultPath;
  21. var $redirects;
  22. var $proxyHost;
  23. var $proxyPort;
  24. var $proxyUsername;
  25. var $proxyPassword;
  26. function HTTPFileWrapper($url, &$info, $redirects = 5) {
  27. parent::FileWrapper($url, $info);
  28. $this->setDefaultPort(80);
  29. $this->setDefaultHost('localhost');
  30. $this->setDefaultPath('/');
  31. $this->redirects = 5;
  32. $this->proxyHost = Config::getVar('proxy', 'http_host');
  33. $this->proxyPort = Config::getVar('proxy', 'http_port');
  34. $this->proxyUsername = Config::getVar('proxy', 'proxy_username');
  35. $this->proxyPassword = Config::getVar('proxy', 'proxy_password');
  36. }
  37. function setDefaultPort($port) {
  38. $this->defaultPort = $port;
  39. }
  40. function setDefaultHost($host) {
  41. $this->defaultHost = $host;
  42. }
  43. function setDefaultPath($path) {
  44. $this->defaultPath = $path;
  45. }
  46. function addHeader($name, $value) {
  47. if (!isset($this->headers)) {
  48. $this->headers = array();
  49. }
  50. $this->headers[$name] = $value;
  51. }
  52. function open($mode = 'r') {
  53. $realHost = $host = isset($this->info['host']) ? $this->info['host'] : $this->defaultHost;
  54. $port = isset($this->info['port']) ? (int)$this->info['port'] : $this->defaultPort;
  55. $path = isset($this->info['path']) ? $this->info['path'] : $this->defaultPath;
  56. if (isset($this->info['query'])) $path .= '?' . $this->info['query'];
  57. if (!empty($this->proxyHost)) {
  58. $realHost = $host;
  59. $host = $this->proxyHost;
  60. $port = $this->proxyPort;
  61. if (!empty($this->proxyUsername)) {
  62. $this->headers['Proxy-Authorization'] = 'Basic ' . base64_encode($this->proxyUsername . ':' . $this->proxyPassword);
  63. }
  64. }
  65. if (!($this->fp = fsockopen($host, $port, $errno, $errstr)))
  66. return false;
  67. $additionalHeadersString = '';
  68. if (is_array($this->headers)) foreach ($this->headers as $name => $value) {
  69. $additionalHeadersString .= "$name: $value\r\n";
  70. }
  71. $request = 'GET ' . (empty($this->proxyHost)?$path:$this->url) . " HTTP/1.0\r\n" .
  72. "Host: $realHost\r\n" .
  73. $additionalHeadersString .
  74. "Connection: Close\r\n\r\n";
  75. fwrite($this->fp, $request);
  76. $response = fgets($this->fp, 4096);
  77. $rc = 0;
  78. sscanf($response, "HTTP/%*s %u %*[^\r\n]\r\n", $rc);
  79. if ($rc == 200) {
  80. while(fgets($this->fp, 4096) !== "\r\n");
  81. return true;
  82. }
  83. if(preg_match('!^3\d\d$!', $rc) && $this->redirects >= 1) {
  84. for($response = '', $time = time(); !feof($this->fp) && $time >= time() - 15; ) $response .= fgets($this->fp, 128);
  85. if (preg_match('!^(?:(?:Location)|(?:URI)|(?:location)): ([^\s]+)[\r\n]!m', $response, $matches)) {
  86. $this->close();
  87. $location = $matches[1];
  88. if (preg_match('!^[a-z]+://!', $location)) {
  89. $this->url = $location;
  90. } else {
  91. $newPath = ($this->info['path'] !== '' && strpos($location, '/') !== 0 ? dirname($this->info['path']) . '/' : (strpos($location, '/') === 0 ? '' : '/')) . $location;
  92. $this->info['path'] = $newPath;
  93. $this->url = $this->glue_url($this->info);
  94. }
  95. $returner =& FileWrapper::wrapper($this->url);
  96. $returner->redirects = $this->redirects - 1;
  97. return $returner;
  98. }
  99. }
  100. $this->close();
  101. return false;
  102. }
  103. function glue_url ($parsed) {
  104. // Thanks to php dot net at NOSPAM dot juamei dot com
  105. // See http://www.php.net/manual/en/function.parse-url.php
  106. if (! is_array($parsed)) return false;
  107. $uri = isset($parsed['scheme']) ? $parsed['scheme'].':'.((strtolower($parsed['scheme']) == 'mailto') ? '':'//'): '';
  108. $uri .= isset($parsed['user']) ? $parsed['user'].($parsed['pass']? ':'.$parsed['pass']:'').'@':'';
  109. $uri .= isset($parsed['host']) ? $parsed['host'] : '';
  110. $uri .= isset($parsed['port']) ? ':'.$parsed['port'] : '';
  111. $uri .= isset($parsed['path']) ? $parsed['path'] : '';
  112. $uri .= isset($parsed['query']) ? '?'.$parsed['query'] : '';
  113. $uri .= isset($parsed['fragment']) ? '#'.$parsed['fragment'] : '';
  114. return $uri;
  115. }
  116. }
  117. ?>