Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Abstract.php
Go to the documentation of this file.
1 <?php
2 
28 #require_once 'Zend/Validate.php';
29 
30 
34 #require_once 'Zend/Validate/Hostname.php';
35 
36 
51 {
55  const EOL = "\r\n";
56 
57 
61  const TIMEOUT_CONNECTION = 30;
62 
67  protected $_maximumLog = 64;
68 
69 
74  protected $_host;
75 
76 
81  protected $_port;
82 
83 
88  protected $_validHost;
89 
90 
95  protected $_socket;
96 
97 
102  protected $_request;
103 
104 
109  protected $_response;
110 
111 
117  protected $_template = '%d%s';
118 
119 
124  private $_log = array();
125 
126 
135  public function __construct($host = '127.0.0.1', $port = null)
136  {
137  $this->_validHost = new Zend_Validate();
138  $this->_validHost->addValidator(new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_ALL));
139 
140  if (!$this->_validHost->isValid($host)) {
144  #require_once 'Zend/Mail/Protocol/Exception.php';
145  throw new Zend_Mail_Protocol_Exception(join(', ', $this->_validHost->getMessages()));
146  }
147 
148  $this->_host = $host;
149  $this->_port = $port;
150  }
151 
152 
158  public function __destruct()
159  {
160  $this->_disconnect();
161  }
162 
169  public function setMaximumLog($maximumLog)
170  {
171  $this->_maximumLog = (int) $maximumLog;
172  }
173 
174 
180  public function getMaximumLog()
181  {
182  return $this->_maximumLog;
183  }
184 
185 
191  abstract public function connect();
192 
193 
199  public function getRequest()
200  {
201  return $this->_request;
202  }
203 
204 
210  public function getResponse()
211  {
212  return $this->_response;
213  }
214 
215 
221  public function getLog()
222  {
223  return implode('', $this->_log);
224  }
225 
226 
232  public function resetLog()
233  {
234  $this->_log = array();
235  }
236 
243  protected function _addLog($value)
244  {
245  if ($this->_maximumLog >= 0 && count($this->_log) >= $this->_maximumLog) {
246  array_shift($this->_log);
247  }
248 
249  $this->_log[] = $value;
250  }
251 
261  protected function _connect($remote)
262  {
263  $errorNum = 0;
264  $errorStr = '';
265 
266  // open connection
267  $this->_socket = @stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
268 
269  if ($this->_socket === false) {
270  if ($errorNum == 0) {
271  $errorStr = 'Could not open socket';
272  }
276  #require_once 'Zend/Mail/Protocol/Exception.php';
277  throw new Zend_Mail_Protocol_Exception($errorStr);
278  }
279 
280  if (($result = $this->_setStreamTimeout(self::TIMEOUT_CONNECTION)) === false) {
284  #require_once 'Zend/Mail/Protocol/Exception.php';
285  throw new Zend_Mail_Protocol_Exception('Could not set stream timeout');
286  }
287 
288  return $result;
289  }
290 
291 
297  protected function _disconnect()
298  {
299  if (is_resource($this->_socket)) {
300  fclose($this->_socket);
301  }
302  }
303 
304 
312  protected function _send($request)
313  {
314  if (!is_resource($this->_socket)) {
318  #require_once 'Zend/Mail/Protocol/Exception.php';
319  throw new Zend_Mail_Protocol_Exception('No connection has been established to ' . $this->_host);
320  }
321 
322  $this->_request = $request;
323 
324  $result = fwrite($this->_socket, $request . self::EOL);
325 
326  // Save request to internal log
327  $this->_addLog($request . self::EOL);
328 
329  if ($result === false) {
333  #require_once 'Zend/Mail/Protocol/Exception.php';
334  throw new Zend_Mail_Protocol_Exception('Could not send request to ' . $this->_host);
335  }
336 
337  return $result;
338  }
339 
340 
348  protected function _receive($timeout = null)
349  {
350  if (!is_resource($this->_socket)) {
354  #require_once 'Zend/Mail/Protocol/Exception.php';
355  throw new Zend_Mail_Protocol_Exception('No connection has been established to ' . $this->_host);
356  }
357 
358  // Adapters may wish to supply per-commend timeouts according to appropriate RFC
359  if ($timeout !== null) {
360  $this->_setStreamTimeout($timeout);
361  }
362 
363  // Retrieve response
364  $reponse = fgets($this->_socket, 1024);
365 
366  // Save request to internal log
367  $this->_addLog($reponse);
368 
369  // Check meta data to ensure connection is still valid
370  $info = stream_get_meta_data($this->_socket);
371 
372  if (!empty($info['timed_out'])) {
376  #require_once 'Zend/Mail/Protocol/Exception.php';
377  throw new Zend_Mail_Protocol_Exception($this->_host . ' has timed out');
378  }
379 
380  if ($reponse === false) {
384  #require_once 'Zend/Mail/Protocol/Exception.php';
385  throw new Zend_Mail_Protocol_Exception('Could not read from ' . $this->_host);
386  }
387 
388  return $reponse;
389  }
390 
391 
402  protected function _expect($code, $timeout = null)
403  {
404  $this->_response = array();
405  $cmd = '';
406  $more = '';
407  $msg = '';
408  $errMsg = '';
409 
410  if (!is_array($code)) {
411  $code = array($code);
412  }
413 
414  do {
415  $this->_response[] = $result = $this->_receive($timeout);
416  list($cmd, $more, $msg) = preg_split('/([\s-]+)/', $result, 2, PREG_SPLIT_DELIM_CAPTURE);
417 
418  if ($errMsg !== '') {
419  $errMsg .= ' ' . $msg;
420  } elseif ($cmd === null || !in_array($cmd, $code)) {
421  $errMsg = $msg;
422  }
423 
424  } while (strpos($more, '-') === 0); // The '-' message prefix indicates an information string instead of a response string.
425 
426  if ($errMsg !== '') {
430  #require_once 'Zend/Mail/Protocol/Exception.php';
431  throw new Zend_Mail_Protocol_Exception($errMsg, $cmd);
432  }
433 
434  return $msg;
435  }
436 
443  protected function _setStreamTimeout($timeout)
444  {
445  return stream_set_timeout($this->_socket, $timeout);
446  }
447 }
_expect($code, $timeout=null)
Definition: Abstract.php:402
__construct($host='127.0.0.1', $port=null)
Definition: Abstract.php:135
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
setMaximumLog($maximumLog)
Definition: Abstract.php:169
$value
Definition: gender.phtml:16
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
_receive($timeout=null)
Definition: Abstract.php:348
$code
Definition: info.phtml:12