XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
StreamBuffer.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2004-2009 Chris Corbyn
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
11 //@require 'Swift/ByteStream/AbstractFilterableInputStream.php';
12 //@require 'Swift/ReplacementFilterFactory.php';
13 //@require 'Swift/Transport/IoBuffer.php';
14 //@require 'Swift/TransportException.php';
15 
24  implements Swift_Transport_IoBuffer
25 {
26 
28  private $_stream;
29 
31  private $_in;
32 
34  private $_out;
35 
37  private $_params = array();
38 
41 
43  private $_translations = array();
44 
49  public function __construct(
50  Swift_ReplacementFilterFactory $replacementFactory)
51  {
52  $this->_replacementFactory = $replacementFactory;
53  }
54 
60  public function initialize(array $params)
61  {
62  $this->_params = $params;
63  switch ($params['type'])
64  {
65  case self::TYPE_PROCESS:
67  break;
68  case self::TYPE_SOCKET:
69  default:
71  break;
72  }
73  }
74 
80  public function setParam($param, $value)
81  {
82  if (isset($this->_stream))
83  {
84  switch ($param)
85  {
86  case 'protocol':
87  if (!array_key_exists('protocol', $this->_params)
88  || $value != $this->_params['protocol'])
89  {
90  if ('tls' == $value)
91  {
92  stream_socket_enable_crypto(
93  $this->_stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT
94  );
95  }
96  }
97  break;
98  }
99  }
100  $this->_params[$param] = $value;
101  }
102 
106  public function terminate()
107  {
108  if (isset($this->_stream))
109  {
110  switch ($this->_params['type'])
111  {
112  case self::TYPE_PROCESS:
113  fclose($this->_in);
114  fclose($this->_out);
115  proc_close($this->_stream);
116  break;
117  case self::TYPE_SOCKET:
118  default:
119  fclose($this->_stream);
120  break;
121  }
122  }
123  $this->_stream = null;
124  $this->_out = null;
125  $this->_in = null;
126  }
127 
133  public function setWriteTranslations(array $replacements)
134  {
135  foreach ($this->_translations as $search => $replace)
136  {
137  if (!isset($replacements[$search]))
138  {
139  $this->removeFilter($search);
140  unset($this->_translations[$search]);
141  }
142  }
143 
144  foreach ($replacements as $search => $replace)
145  {
146  if (!isset($this->_translations[$search]))
147  {
148  $this->addFilter(
149  $this->_replacementFactory->createFilter($search, $replace), $search
150  );
151  $this->_translations[$search] = true;
152  }
153  }
154  }
155 
163  public function readLine($sequence)
164  {
165  if (isset($this->_out) && !feof($this->_out))
166  {
167  $line = fgets($this->_out);
168  return $line;
169  }
170  }
171 
180  public function read($length)
181  {
182  if (isset($this->_out) && !feof($this->_out))
183  {
184  $ret = fread($this->_out, $length);
185  return $ret;
186  }
187  }
188 
190  public function setReadPointer($byteOffset)
191  {
192  }
193 
194  // -- Protected methods
195 
197  protected function _flush()
198  {
199  if (isset($this->_in))
200  {
201  fflush($this->_in);
202  }
203  }
204 
206  protected function _commit($bytes)
207  {
208  if (isset($this->_in)
209  && fwrite($this->_in, $bytes))
210  {
211  return ++$this->_sequence;
212  }
213  }
214 
215  // -- Private methods
216 
221  private function _establishSocketConnection()
222  {
223  $host = $this->_params['host'];
224  if (!empty($this->_params['protocol']))
225  {
226  $host = $this->_params['protocol'] . '://' . $host;
227  }
228  $timeout = 15;
229  if (!empty($this->_params['timeout']))
230  {
231  $timeout = $this->_params['timeout'];
232  }
233  if (!$this->_stream = fsockopen($host, $this->_params['port'], $errno, $errstr, $timeout))
234  {
235  throw new Swift_TransportException(
236  'Connection could not be established with host ' . $this->_params['host'] .
237  ' [' . $errstr . ' #' . $errno . ']'
238  );
239  }
240  if (!empty($this->_params['blocking']))
241  {
242  stream_set_blocking($this->_stream, 1);
243  }
244  else
245  {
246  stream_set_blocking($this->_stream, 0);
247  }
248  $this->_in =& $this->_stream;
249  $this->_out =& $this->_stream;
250  }
251 
256  private function _establishProcessConnection()
257  {
258  $command = $this->_params['command'];
259  $descriptorSpec = array(
260  0 => array('pipe', 'r'),
261  1 => array('pipe', 'w'),
262  2 => array('pipe', 'w')
263  );
264  $this->_stream = proc_open($command, $descriptorSpec, $pipes);
265  stream_set_blocking($pipes[2], 0);
266  if ($err = stream_get_contents($pipes[2]))
267  {
268  throw new Swift_TransportException(
269  'Process could not be started [' . $err . ']'
270  );
271  }
272  $this->_in =& $pipes[0];
273  $this->_out =& $pipes[1];
274  }
275 
276 }