Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Socket.php
Go to the documentation of this file.
1 <?php
13 
21 {
26  private $_host = 'localhost';
27 
32  private $_port = 80;
33 
38  private $_sock = null;
39 
44  private $_headers = [];
45 
50  private $_postFields = [];
51 
56  private $_cookies = [];
57 
62  private $_responseHeaders = [];
63 
68  private $_responseBody = '';
69 
74  private $_responseStatus = 0;
75 
80  private $_timeout = 300;
81 
86  private $_redirectCount = 0;
87 
94  public function setTimeout($value)
95  {
96  $this->_timeout = (int)$value;
97  }
98 
105  public function __construct($host = null, $port = 80)
106  {
107  if ($host) {
108  $this->connect($host, (int)$port);
109  }
110  }
111 
119  public function connect($host, $port = 80)
120  {
121  $this->_host = $host;
122  $this->_port = (int)$port;
123  }
124 
130  public function disconnect()
131  {
132  @fclose($this->_sock);
133  }
134 
140  public function setHeaders($headers)
141  {
142  $this->_headers = $headers;
143  }
144 
152  public function addHeader($name, $value)
153  {
154  $this->_headers[$name] = $value;
155  }
156 
163  public function removeHeader($name)
164  {
165  unset($this->_headers[$name]);
166  }
167 
176  public function setCredentials($login, $pass)
177  {
178  $val = base64_encode("{$login}:{$pass}");
179  $this->addHeader("Authorization", "Basic {$val}");
180  }
181 
189  public function addCookie($name, $value)
190  {
191  $this->_cookies[$name] = $value;
192  }
193 
200  public function removeCookie($name)
201  {
202  unset($this->_cookies[$name]);
203  }
204 
211  public function setCookies($cookies)
212  {
213  $this->_cookies = $cookies;
214  }
215 
221  public function removeCookies()
222  {
223  $this->setCookies([]);
224  }
225 
232  public function get($uri)
233  {
234  $this->makeRequest("GET", $this->parseUrl($uri));
235  }
236 
245  protected function parseUrl($uri)
246  {
247  $parts = parse_url($uri);
248  if (!empty($parts['user']) && !empty($parts['pass'])) {
249  $this->setCredentials($parts['user'], $parts['pass']);
250  }
251  if (!empty($parts['port'])) {
252  $this->_port = (int)$parts['port'];
253  }
254 
255  if (!empty($parts['host'])) {
256  $this->_host = $parts['host'];
257  } else {
258  throw new \InvalidArgumentException("Uri doesn't contain host part");
259  }
260 
261  if (!empty($parts['path'])) {
262  $requestUri = $parts['path'];
263  } else {
264  throw new \InvalidArgumentException("Uri doesn't contain path part");
265  }
266  if (!empty($parts['query'])) {
267  $requestUri .= "?" . $parts['query'];
268  }
269  return $requestUri;
270  }
271 
279  public function post($uri, $params)
280  {
281  $this->makeRequest("POST", $this->parseUrl($uri), $params);
282  }
283 
289  public function getHeaders()
290  {
291  return $this->_responseHeaders;
292  }
293 
299  public function getBody()
300  {
301  return $this->_responseBody;
302  }
303 
309  public function getCookies()
310  {
311  if (empty($this->_responseHeaders['Set-Cookie'])) {
312  return [];
313  }
314  $out = [];
315  foreach ($this->_responseHeaders['Set-Cookie'] as $row) {
316  $values = explode("; ", $row);
317  $c = count($values);
318  if (!$c) {
319  continue;
320  }
321  list($key, $val) = explode("=", $values[0]);
322  if ($val === null) {
323  continue;
324  }
325  $out[trim($key)] = trim($val);
326  }
327  return $out;
328  }
329 
335  public function getCookiesFull()
336  {
337  if (empty($this->_responseHeaders['Set-Cookie'])) {
338  return [];
339  }
340  $out = [];
341  foreach ($this->_responseHeaders['Set-Cookie'] as $row) {
342  $values = explode("; ", $row);
343  $c = count($values);
344  if (!$c) {
345  continue;
346  }
347  list($key, $val) = explode("=", $values[0]);
348  if ($val === null) {
349  continue;
350  }
351  $out[trim($key)] = ['value' => trim($val)];
352  array_shift($values);
353  $c--;
354  if (!$c) {
355  continue;
356  }
357  for ($i = 0; $i < $c; $i++) {
358  list($subkey, $val) = explode("=", $values[$i]);
359  $out[trim($key)][trim($subkey)] = trim($val);
360  }
361  }
362  return $out;
363  }
364 
370  protected function processResponseHeaders()
371  {
372  $crlf = "\r\n";
373  $this->_responseHeaders = [];
374  while (!feof($this->_sock)) {
375  $line = fgets($this->_sock, 1024);
376  if ($line === $crlf) {
377  return;
378  }
379  $name = $value = '';
380  $out = explode(": ", trim($line), 2);
381  if (count($out) == 2) {
382  $name = $out[0];
383  $value = $out[1];
384  }
385  if (!empty($value)) {
386  if ($name == "Set-Cookie") {
387  if (!isset($this->_responseHeaders[$name])) {
388  $this->_responseHeaders[$name] = [];
389  }
390  $this->_responseHeaders[$name][] = $value;
391  } else {
392  $this->_responseHeaders[$name] = $value;
393  }
394  }
395  }
396  }
397 
403  protected function processResponseBody()
404  {
405  $this->_responseBody = '';
406 
407  while (!feof($this->_sock)) {
408  $this->_responseBody .= @fread($this->_sock, 1024);
409  }
410  }
411 
418  protected function processResponse()
419  {
420  $response = '';
421  $responseLine = trim(fgets($this->_sock, 1024));
422 
423  $line = explode(" ", $responseLine, 3);
424  if (count($line) != 3) {
425  return $this->doError("Invalid response line returned from server: " . $responseLine);
426  }
427  $this->_responseStatus = intval($line[1]);
428  $this->processResponseHeaders();
429 
430  $this->processRedirect();
431 
432  $this->processResponseBody();
433  }
434 
440  protected function processRedirect()
441  {
442  // TODO: implement redirects support
443  }
444 
451  public function getStatus()
452  {
453  return $this->_responseStatus;
454  }
455 
465  protected function makeRequest($method, $uri, $params = [])
466  {
467  $errno = $errstr = '';
468  $this->_sock = @fsockopen($this->_host, $this->_port, $errno, $errstr, $this->_timeout);
469  if (!$this->_sock) {
470  return $this->doError(sprintf("[errno: %d] %s", $errno, $errstr));
471  }
472 
473  $crlf = "\r\n";
474  $isPost = $method == "POST";
475 
476  $appendHeaders = [];
477  $paramsStr = false;
478  if ($isPost && $params) {
479  $paramsStr = is_array($params) ? http_build_query($params) : $params;
480  $appendHeaders['Content-type'] = 'application/x-www-form-urlencoded';
481  $appendHeaders['Content-length'] = strlen($paramsStr);
482  }
483 
484  $out = "{$method} {$uri} HTTP/1.1{$crlf}";
485  $out .= $this->headersToString($appendHeaders);
486  $out .= $crlf;
487  if ($paramsStr) {
488  $out .= $paramsStr . $crlf;
489  }
490 
491  fwrite($this->_sock, $out);
492  $this->processResponse();
493  }
494 
501  public function doError($string)
502  {
503  throw new \Exception($string);
504  }
505 
511  protected function headersToString($append = [])
512  {
513  $headers = [];
514  $headers["Host"] = $this->_host;
515  $headers['Connection'] = "close";
516  $headers = array_merge($headers, $this->_headers, $append);
517  $str = [];
518  foreach ($headers as $k => $v) {
519  $str[] = "{$k}: {$v}\r\n";
520  }
521  return implode($str);
522  }
523 
531  public function setOptions($arr)
532  {
533  // Stub
534  }
535 
544  public function setOption($name, $value)
545  {
546  // Stub
547  }
548 }
$response
Definition: 404.php:11
fsockopen(&$errorNumber, &$errorMessage)
Definition: http_mock.php:37
$values
Definition: options.phtml:88
makeRequest($method, $uri, $params=[])
Definition: Socket.php:465
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$i
Definition: gallery.phtml:31
__construct($host=null, $port=80)
Definition: Socket.php:105
if(!isset($_GET['name'])) $name
Definition: log.php:14