Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Http.php
Go to the documentation of this file.
1 <?php
10 
12 
17 class Http extends File
18 {
24  protected $scheme = 'http';
25 
33  public function isExists($path)
34  {
35  $headers = array_change_key_case(get_headers($this->getScheme() . $path, 1), CASE_LOWER);
36 
37  $status = $headers[0];
38 
39  if (strpos($status, '200 OK') === false) {
40  $result = false;
41  } else {
42  $result = true;
43  }
44 
45  return $result;
46  }
47 
55  public function stat($path)
56  {
57  $headers = array_change_key_case(get_headers($this->getScheme() . $path, 1), CASE_LOWER);
58 
59  $result = [
60  'dev' => 0,
61  'ino' => 0,
62  'mode' => 0,
63  'nlink' => 0,
64  'uid' => 0,
65  'gid' => 0,
66  'rdev' => 0,
67  'atime' => 0,
68  'ctime' => 0,
69  'blksize' => 0,
70  'blocks' => 0,
71  'size' => isset($headers['content-length']) ? $headers['content-length'] : 0,
72  'type' => isset($headers['content-type']) ? $headers['content-type'] : '',
73  'mtime' => isset($headers['last-modified']) ? $headers['last-modified'] : 0,
74  'disposition' => isset($headers['content-disposition']) ? $headers['content-disposition'] : null,
75  ];
76  return $result;
77  }
78 
88  public function fileGetContents($path, $flags = null, $context = null)
89  {
90  clearstatcache();
91  $result = @file_get_contents($this->getScheme() . $path, $flags, $context);
92  if (false === $result) {
93  throw new FileSystemException(
94  new \Magento\Framework\Phrase(
95  'The contents from the "%1" file can\'t be read. %2',
96  [$path, $this->getWarningMessage()]
97  )
98  );
99  }
100  return $result;
101  }
102 
113  public function filePutContents($path, $content, $mode = null, $context = null)
114  {
115  $result = @file_put_contents($this->getScheme() . $path, $content, $mode, $context);
116  if (!$result) {
117  throw new FileSystemException(
118  new \Magento\Framework\Phrase(
119  'The specified "%1" file couldn\'t be written. %2',
120  [$path, $this->getWarningMessage()]
121  )
122  );
123  }
124  return $result;
125  }
126 
136  public function fileOpen($path, $mode)
137  {
138  $urlProp = $this->parseUrl($this->getScheme() . $path);
139 
140  if (false === $urlProp) {
141  throw new FileSystemException(
142  new \Magento\Framework\Phrase('The download URL is incorrect. Verify and try again.')
143  );
144  }
145 
146  $hostname = $urlProp['host'];
147  $port = 80;
148 
149  if (isset($urlProp['port'])) {
150  $port = (int)$urlProp['port'];
151  }
152 
153  $path = '/';
154  if (isset($urlProp['path'])) {
155  $path = $urlProp['path'];
156  }
157 
158  $query = '';
159  if (isset($urlProp['query'])) {
160  $query = '?' . $urlProp['query'];
161  }
162 
163  $result = $this->open($hostname, $port);
164 
165  $headers = 'GET ' .
166  $path .
167  $query .
168  ' HTTP/1.0' .
169  "\r\n" .
170  'Host: ' .
171  $hostname .
172  "\r\n" .
173  'User-Agent: Magento' .
174  "\r\n" .
175  'Connection: close' .
176  "\r\n" .
177  "\r\n";
178 
179  fwrite($result, $headers);
180 
181  // trim headers
182  while (!feof($result)) {
183  $str = fgets($result, 1024);
184  if ($str == "\r\n") {
185  break;
186  }
187  }
188 
189  return $result;
190  }
191 
201  public function fileReadLine($resource, $length, $ending = null)
202  {
203  $result = @stream_get_line($resource, $length, $ending);
204 
205  return $result;
206  }
207 
217  public function getAbsolutePath($basePath, $path, $scheme = null)
218  {
219  // check if the path given is already an absolute path containing the
220  // basepath. so if the basepath starts at position 0 in the path, we
221  // must not concatinate them again because path is already absolute.
222  if (0 === strpos($path, $basePath)) {
223  return $this->getScheme() . $path;
224  }
225 
226  return $this->getScheme() . $basePath . $path;
227  }
228 
235  protected function getScheme($scheme = null)
236  {
238  return $scheme ? $scheme . '://' : '';
239  }
240 
249  protected function open($hostname, $port)
250  {
251  $result = @fsockopen($hostname, $port, $errorNumber, $errorMessage);
252  if ($result === false) {
253  throw new FileSystemException(
254  new \Magento\Framework\Phrase(
255  'Something went wrong while connecting to the host. Error#%1 - %2.',
256  [$errorNumber, $errorMessage]
257  )
258  );
259  }
260  return $result;
261  }
262 
269  protected function parseUrl($path)
270  {
271  return parse_url($path);
272  }
273 }
fsockopen(&$errorNumber, &$errorMessage)
Definition: http_mock.php:37
fileGetContents($path, $flags=null, $context=null)
Definition: Http.php:88
$resource
Definition: bulk.php:12
getAbsolutePath($basePath, $path, $scheme=null)
Definition: Http.php:217
fileReadLine($resource, $length, $ending=null)
Definition: Http.php:201
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
$status
Definition: order_status.php:8
filePutContents($path, $content, $mode=null, $context=null)
Definition: Http.php:113