Http.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * Origin filesystem driver
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Filesystem\Driver;
  9. use Magento\Framework\Exception\FileSystemException;
  10. /**
  11. * Class Http
  12. */
  13. class Http extends File
  14. {
  15. /**
  16. * Scheme distinguisher
  17. *
  18. * @var string
  19. */
  20. protected $scheme = 'http';
  21. /**
  22. * Checks if path exists
  23. *
  24. * @param string $path
  25. * @return bool
  26. * @throws FileSystemException
  27. */
  28. public function isExists($path)
  29. {
  30. $headers = array_change_key_case(get_headers($this->getScheme() . $path, 1), CASE_LOWER);
  31. $status = $headers[0];
  32. /* Handling 302 redirection */
  33. if (strpos($status, '302 Found') !== false && isset($headers[1])) {
  34. $status = $headers[1];
  35. }
  36. if (strpos($status, '200 OK') === false) {
  37. $result = false;
  38. } else {
  39. $result = true;
  40. }
  41. return $result;
  42. }
  43. /**
  44. * Gathers the statistics of the given path
  45. *
  46. * @param string $path
  47. * @return array
  48. * @SuppressWarnings(PHPMD.NPathComplexity)
  49. */
  50. public function stat($path)
  51. {
  52. $headers = array_change_key_case(get_headers($this->getScheme() . $path, 1), CASE_LOWER);
  53. $result = [
  54. 'dev' => 0,
  55. 'ino' => 0,
  56. 'mode' => 0,
  57. 'nlink' => 0,
  58. 'uid' => 0,
  59. 'gid' => 0,
  60. 'rdev' => 0,
  61. 'atime' => 0,
  62. 'ctime' => 0,
  63. 'blksize' => 0,
  64. 'blocks' => 0,
  65. 'size' => isset($headers['content-length']) ? $headers['content-length'] : 0,
  66. 'type' => isset($headers['content-type']) ? $headers['content-type'] : '',
  67. 'mtime' => isset($headers['last-modified']) ? $headers['last-modified'] : 0,
  68. 'disposition' => isset($headers['content-disposition']) ? $headers['content-disposition'] : null,
  69. ];
  70. return $result;
  71. }
  72. /**
  73. * Retrieve file contents from given path
  74. *
  75. * @param string $path
  76. * @param string|null $flags
  77. * @param resource|null $context
  78. * @return string
  79. * @throws FileSystemException
  80. */
  81. public function fileGetContents($path, $flags = null, $context = null)
  82. {
  83. clearstatcache();
  84. $result = @file_get_contents($this->getScheme() . $path, $flags, $context);
  85. if (false === $result) {
  86. throw new FileSystemException(
  87. new \Magento\Framework\Phrase(
  88. 'The contents from the "%1" file can\'t be read. %2',
  89. [$path, $this->getWarningMessage()]
  90. )
  91. );
  92. }
  93. return $result;
  94. }
  95. /**
  96. * Open file in given path
  97. *
  98. * @param string $path
  99. * @param string $content
  100. * @param string|null $mode
  101. * @param resource|null $context
  102. * @return int The number of bytes that were written
  103. * @throws FileSystemException
  104. */
  105. public function filePutContents($path, $content, $mode = null, $context = null)
  106. {
  107. $result = @file_put_contents($this->getScheme() . $path, $content, $mode, $context);
  108. if (!$result) {
  109. throw new FileSystemException(
  110. new \Magento\Framework\Phrase(
  111. 'The specified "%1" file couldn\'t be written. %2',
  112. [$path, $this->getWarningMessage()]
  113. )
  114. );
  115. }
  116. return $result;
  117. }
  118. /**
  119. * Open file
  120. *
  121. * @param string $path
  122. * @param string $mode
  123. * @return resource file
  124. * @throws FileSystemException
  125. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  126. */
  127. public function fileOpen($path, $mode)
  128. {
  129. $urlProp = $this->parseUrl($this->getScheme() . $path);
  130. if (false === $urlProp) {
  131. throw new FileSystemException(
  132. new \Magento\Framework\Phrase('The download URL is incorrect. Verify and try again.')
  133. );
  134. }
  135. $hostname = $urlProp['host'];
  136. $port = 80;
  137. if (isset($urlProp['port'])) {
  138. $port = (int)$urlProp['port'];
  139. }
  140. $path = '/';
  141. if (isset($urlProp['path'])) {
  142. $path = $urlProp['path'];
  143. }
  144. $query = '';
  145. if (isset($urlProp['query'])) {
  146. $query = '?' . $urlProp['query'];
  147. }
  148. $result = $this->open($hostname, $port);
  149. $headers = 'GET ' .
  150. $path .
  151. $query .
  152. ' HTTP/1.0' .
  153. "\r\n" .
  154. 'Host: ' .
  155. $hostname .
  156. "\r\n" .
  157. 'User-Agent: Magento' .
  158. "\r\n" .
  159. 'Connection: close' .
  160. "\r\n" .
  161. "\r\n";
  162. fwrite($result, $headers);
  163. // trim headers
  164. while (!feof($result)) {
  165. $str = fgets($result, 1024);
  166. if ($str == "\r\n") {
  167. break;
  168. }
  169. }
  170. return $result;
  171. }
  172. /**
  173. * Reads the line content from file pointer (with specified number of bytes from the current position).
  174. *
  175. * @param resource $resource
  176. * @param int $length
  177. * @param string $ending [optional]
  178. * @return string
  179. * @throws FileSystemException
  180. */
  181. public function fileReadLine($resource, $length, $ending = null)
  182. {
  183. $result = @stream_get_line($resource, $length, $ending);
  184. return $result;
  185. }
  186. /**
  187. * Get absolute path
  188. *
  189. * @param string $basePath
  190. * @param string $path
  191. * @param string|null $scheme
  192. * @return string
  193. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  194. */
  195. public function getAbsolutePath($basePath, $path, $scheme = null)
  196. {
  197. // check if the path given is already an absolute path containing the
  198. // basepath. so if the basepath starts at position 0 in the path, we
  199. // must not concatinate them again because path is already absolute.
  200. if (0 === strpos($path, $basePath)) {
  201. return $this->getScheme() . $path;
  202. }
  203. return $this->getScheme() . $basePath . $path;
  204. }
  205. /**
  206. * Return path with scheme
  207. *
  208. * @param null|string $scheme
  209. * @return string
  210. */
  211. protected function getScheme($scheme = null)
  212. {
  213. $scheme = $scheme ?: $this->scheme;
  214. return $scheme ? $scheme . '://' : '';
  215. }
  216. /**
  217. * Open a url
  218. *
  219. * @param string $hostname
  220. * @param int $port
  221. * @throws \Magento\Framework\Exception\FileSystemException
  222. * @return array
  223. */
  224. protected function open($hostname, $port)
  225. {
  226. $result = @fsockopen($hostname, $port, $errorNumber, $errorMessage);
  227. if ($result === false) {
  228. throw new FileSystemException(
  229. new \Magento\Framework\Phrase(
  230. 'Something went wrong while connecting to the host. Error#%1 - %2.',
  231. [$errorNumber, $errorMessage]
  232. )
  233. );
  234. }
  235. return $result;
  236. }
  237. /**
  238. * Parse a http url
  239. *
  240. * @param string $path
  241. * @return array
  242. */
  243. protected function parseUrl($path)
  244. {
  245. return parse_url($path);
  246. }
  247. }