ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
Socket.php
Go to the documentation of this file.
1 <?php
2 //
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4 |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.0 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Authors: Stig Bakken <ssb@php.net> |
17 // | Chuck Hagenbuch <chuck@horde.org> |
18 // +----------------------------------------------------------------------+
19 //
20 // $Id: Socket.php,v 1.38 2008/02/15 18:24:17 chagenbu Exp $
21 
22 require_once 'PEAR.php';
23 
24 define('NET_SOCKET_READ', 1);
25 define('NET_SOCKET_WRITE', 2);
26 define('NET_SOCKET_ERROR', 4);
27 
35 class Net_Socket extends PEAR {
36 
41  var $fp = null;
42 
47  var $blocking = true;
48 
53  var $persistent = false;
54 
59  var $addr = '';
60 
65  var $port = 0;
66 
72  var $timeout = false;
73 
79  var $lineLength = 2048;
80 
97  function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null)
98  {
99  if (is_resource($this->fp)) {
100  @fclose($this->fp);
101  $this->fp = null;
102  }
103 
104  if (!$addr) {
105  return $this->raiseError('$addr cannot be empty');
106  } elseif (strspn($addr, '.0123456789') == strlen($addr) ||
107  strstr($addr, '/') !== false) {
108  $this->addr = $addr;
109  } else {
110  $this->addr = @gethostbyname($addr);
111  }
112 
113  $this->port = $port % 65536;
114 
115  if ($persistent !== null) {
116  $this->persistent = $persistent;
117  }
118 
119  if ($timeout !== null) {
120  $this->timeout = $timeout;
121  }
122 
123  $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen';
124  $errno = 0;
125  $errstr = '';
126  $old_track_errors = @ini_set('track_errors', 1);
127  if ($options && function_exists('stream_context_create')) {
128  if ($this->timeout) {
130  } else {
131  $timeout = 0;
132  }
133  $context = stream_context_create($options);
134 
135  // Since PHP 5 fsockopen doesn't allow context specification
136  if (function_exists('stream_socket_client')) {
137  $flags = $this->persistent ? STREAM_CLIENT_PERSISTENT : STREAM_CLIENT_CONNECT;
138  $addr = $this->addr . ':' . $this->port;
139  $fp = stream_socket_client($addr, $errno, $errstr, $timeout, $flags, $context);
140  } else {
141  $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context);
142  }
143  } else {
144  if ($this->timeout) {
145  $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout);
146  } else {
147  $fp = @$openfunc($this->addr, $this->port, $errno, $errstr);
148  }
149  }
150 
151  if (!$fp) {
152  if ($errno == 0 && isset($php_errormsg)) {
153  $errstr = $php_errormsg;
154  }
155  @ini_set('track_errors', $old_track_errors);
156  return $this->raiseError($errstr, $errno);
157  }
158 
159  @ini_set('track_errors', $old_track_errors);
160  $this->fp = $fp;
161 
162  return $this->setBlocking($this->blocking);
163  }
164 
171  function disconnect()
172  {
173  if (!is_resource($this->fp)) {
174  return $this->raiseError('not connected');
175  }
176 
177  @fclose($this->fp);
178  $this->fp = null;
179  return true;
180  }
181 
188  function isBlocking()
189  {
190  return $this->blocking;
191  }
192 
203  function setBlocking($mode)
204  {
205  if (!is_resource($this->fp)) {
206  return $this->raiseError('not connected');
207  }
208 
209  $this->blocking = $mode;
210  socket_set_blocking($this->fp, $this->blocking);
211  return true;
212  }
213 
223  function setTimeout($seconds, $microseconds)
224  {
225  if (!is_resource($this->fp)) {
226  return $this->raiseError('not connected');
227  }
228 
229  return socket_set_timeout($this->fp, $seconds, $microseconds);
230  }
231 
241  {
242  if (!is_resource($this->fp)) {
243  return $this->raiseError('not connected');
244  }
245 
246  $returned = stream_set_write_buffer($this->fp, $size);
247  if ($returned == 0) {
248  return true;
249  }
250  return $this->raiseError('Cannot set write buffer.');
251  }
252 
267  function getStatus()
268  {
269  if (!is_resource($this->fp)) {
270  return $this->raiseError('not connected');
271  }
272 
273  return socket_get_status($this->fp);
274  }
275 
283  function gets($size)
284  {
285  if (!is_resource($this->fp)) {
286  return $this->raiseError('not connected');
287  }
288 
289  return @fgets($this->fp, $size);
290  }
291 
303  function read($size)
304  {
305  if (!is_resource($this->fp)) {
306  return $this->raiseError('not connected');
307  }
308 
309  return @fread($this->fp, $size);
310  }
311 
324  function write($data, $blocksize = null)
325  {
326  if (!is_resource($this->fp)) {
327  return $this->raiseError('not connected');
328  }
329 
330  if (is_null($blocksize) && !OS_WINDOWS) {
331  return @fwrite($this->fp, $data);
332  } else {
333  if (is_null($blocksize)) {
334  $blocksize = 1024;
335  }
336 
337  $pos = 0;
338  $size = strlen($data);
339  while ($pos < $size) {
340  $written = @fwrite($this->fp, substr($data, $pos, $blocksize));
341  if ($written === false) {
342  return false;
343  }
344  $pos += $written;
345  }
346 
347  return $pos;
348  }
349  }
350 
357  function writeLine($data)
358  {
359  if (!is_resource($this->fp)) {
360  return $this->raiseError('not connected');
361  }
362 
363  return fwrite($this->fp, $data . "\r\n");
364  }
365 
374  function eof()
375  {
376  return (!is_resource($this->fp) || feof($this->fp));
377  }
378 
386  function readByte()
387  {
388  if (!is_resource($this->fp)) {
389  return $this->raiseError('not connected');
390  }
391 
392  return ord(@fread($this->fp, 1));
393  }
394 
402  function readWord()
403  {
404  if (!is_resource($this->fp)) {
405  return $this->raiseError('not connected');
406  }
407 
408  $buf = @fread($this->fp, 2);
409  return (ord($buf[0]) + (ord($buf[1]) << 8));
410  }
411 
419  function readInt()
420  {
421  if (!is_resource($this->fp)) {
422  return $this->raiseError('not connected');
423  }
424 
425  $buf = @fread($this->fp, 4);
426  return (ord($buf[0]) + (ord($buf[1]) << 8) +
427  (ord($buf[2]) << 16) + (ord($buf[3]) << 24));
428  }
429 
437  function readString()
438  {
439  if (!is_resource($this->fp)) {
440  return $this->raiseError('not connected');
441  }
442 
443  $string = '';
444  while (($char = @fread($this->fp, 1)) != "\x00") {
445  $string .= $char;
446  }
447  return $string;
448  }
449 
457  function readIPAddress()
458  {
459  if (!is_resource($this->fp)) {
460  return $this->raiseError('not connected');
461  }
462 
463  $buf = @fread($this->fp, 4);
464  return sprintf('%d.%d.%d.%d', ord($buf[0]), ord($buf[1]),
465  ord($buf[2]), ord($buf[3]));
466  }
467 
477  function readLine()
478  {
479  if (!is_resource($this->fp)) {
480  return $this->raiseError('not connected');
481  }
482 
483  $line = '';
484  $timeout = time() + $this->timeout;
485  while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) {
486  $line .= @fgets($this->fp, $this->lineLength);
487  if (substr($line, -1) == "\n") {
488  return rtrim($line, "\r\n");
489  }
490  }
491  return $line;
492  }
493 
507  function readAll()
508  {
509  if (!is_resource($this->fp)) {
510  return $this->raiseError('not connected');
511  }
512 
513  $data = '';
514  while (!feof($this->fp)) {
515  $data .= @fread($this->fp, $this->lineLength);
516  }
517  return $data;
518  }
519 
532  function select($state, $tv_sec, $tv_usec = 0)
533  {
534  if (!is_resource($this->fp)) {
535  return $this->raiseError('not connected');
536  }
537 
538  $read = null;
539  $write = null;
540  $except = null;
541  if ($state & NET_SOCKET_READ) {
542  $read[] = $this->fp;
543  }
544  if ($state & NET_SOCKET_WRITE) {
545  $write[] = $this->fp;
546  }
547  if ($state & NET_SOCKET_ERROR) {
548  $except[] = $this->fp;
549  }
550  if (false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) {
551  return false;
552  }
553 
554  $result = 0;
555  if (count($read)) {
557  }
558  if (count($write)) {
560  }
561  if (count($except)) {
563  }
564  return $result;
565  }
566 
580  function enableCrypto($enabled, $type)
581  {
582  if (version_compare(phpversion(), "5.1.0", ">=")) {
583  if (!is_resource($this->fp)) {
584  return $this->raiseError('not connected');
585  }
586  return @stream_socket_enable_crypto($this->fp, $enabled, $type);
587  } else {
588  return $this->raiseError('Net_Socket::enableCrypto() requires php version >= 5.1.0');
589  }
590  }
591 
592 }