PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/pacore/ext/Net/Socket.php

https://github.com/DigitalCityMechanics/PeopleAggregator
PHP | 540 lines | 247 code | 57 blank | 236 comment | 52 complexity | 3effca9b28513c1e8750c1a4c861963f MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, LGPL-2.1
  1. <?php
  2. /** !
  3. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  4. * [filename] is a part of PeopleAggregator.
  5. * [description including history]
  6. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  7. * @author [creator, or "Original Author"]
  8. * @license http://bit.ly/aVWqRV PayAsYouGo License
  9. * @copyright Copyright (c) 2010 Broadband Mechanics
  10. * @package PeopleAggregator
  11. */
  12. ?>
  13. <?php
  14. //
  15. // +----------------------------------------------------------------------+
  16. // | PHP Version 4 |
  17. // +----------------------------------------------------------------------+
  18. // | Copyright (c) 1997-2003 The PHP Group |
  19. // +----------------------------------------------------------------------+
  20. // | This source file is subject to version 2.0 of the PHP license, |
  21. // | that is bundled with this package in the file LICENSE, and is |
  22. // | available at through the world-wide-web at |
  23. // | http://www.php.net/license/2_02.txt. |
  24. // | If you did not receive a copy of the PHP license and are unable to |
  25. // | obtain it through the world-wide-web, please send a note to |
  26. // | license@php.net so we can mail you a copy immediately. |
  27. // +----------------------------------------------------------------------+
  28. // | Authors: Stig Bakken <ssb@php.net> |
  29. // | Chuck Hagenbuch <chuck@horde.org> |
  30. // +----------------------------------------------------------------------+
  31. //
  32. // $Id: Socket.php,v 1.24 2005/02/03 20:40:16 chagenbu Exp $
  33. require_once 'PEAR.php';
  34. define('NET_SOCKET_READ', 1);
  35. define('NET_SOCKET_WRITE', 2);
  36. define('NET_SOCKET_ERROR', 3);
  37. /**
  38. * Generalized Socket class.
  39. *
  40. * @version 1.1
  41. * @author Stig Bakken <ssb@php.net>
  42. * @author Chuck Hagenbuch <chuck@horde.org>
  43. */
  44. class Net_Socket extends PEAR {
  45. /**
  46. * Socket file pointer.
  47. * @var resource $fp
  48. */
  49. var $fp = null;
  50. /**
  51. * Whether the socket is blocking. Defaults to true.
  52. * @var boolean $blocking
  53. */
  54. var $blocking = true;
  55. /**
  56. * Whether the socket is persistent. Defaults to false.
  57. * @var boolean $persistent
  58. */
  59. var $persistent = false;
  60. /**
  61. * The IP address to connect to.
  62. * @var string $addr
  63. */
  64. var $addr = '';
  65. /**
  66. * The port number to connect to.
  67. * @var integer $port
  68. */
  69. var $port = 0;
  70. /**
  71. * Number of seconds to wait on socket connections before assuming
  72. * there's no more data. Defaults to no timeout.
  73. * @var integer $timeout
  74. */
  75. var $timeout = false;
  76. /**
  77. * Number of bytes to read at a time in readLine() and
  78. * readAll(). Defaults to 2048.
  79. * @var integer $lineLength
  80. */
  81. var $lineLength = 2048;
  82. /**
  83. * Connect to the specified port. If called when the socket is
  84. * already connected, it disconnects and connects again.
  85. *
  86. * @param string $addr IP address or host name.
  87. * @param integer $port TCP port number.
  88. * @param boolean $persistent (optional) Whether the connection is
  89. * persistent (kept open between requests
  90. * by the web server).
  91. * @param integer $timeout (optional) How long to wait for data.
  92. * @param array $options See options for stream_context_create.
  93. *
  94. * @access public
  95. *
  96. * @return boolean | PEAR_Error True on success or a PEAR_Error on failure.
  97. */
  98. function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null)
  99. {
  100. if (is_resource($this->fp)) {
  101. @fclose($this->fp);
  102. $this->fp = null;
  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. $this->port = $port % 65536;
  113. if ($persistent !== null) {
  114. $this->persistent = $persistent;
  115. }
  116. if ($timeout !== null) {
  117. $this->timeout = $timeout;
  118. }
  119. $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen';
  120. $errno = 0;
  121. $errstr = '';
  122. if ($options && function_exists('stream_context_create')) {
  123. if ($this->timeout) {
  124. $timeout = $this->timeout;
  125. } else {
  126. $timeout = 0;
  127. }
  128. $context = stream_context_create($options);
  129. $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context);
  130. } else {
  131. if ($this->timeout) {
  132. $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout);
  133. } else {
  134. $fp = @$openfunc($this->addr, $this->port, $errno, $errstr);
  135. }
  136. }
  137. if (!$fp) {
  138. return $this->raiseError($errstr, $errno);
  139. }
  140. $this->fp = $fp;
  141. return $this->setBlocking($this->blocking);
  142. }
  143. /**
  144. * Disconnects from the peer, closes the socket.
  145. *
  146. * @access public
  147. * @return mixed true on success or an error object otherwise
  148. */
  149. function disconnect()
  150. {
  151. if (!is_resource($this->fp)) {
  152. return $this->raiseError('not connected');
  153. }
  154. @fclose($this->fp);
  155. $this->fp = null;
  156. return true;
  157. }
  158. /**
  159. * Find out if the socket is in blocking mode.
  160. *
  161. * @access public
  162. * @return boolean The current blocking mode.
  163. */
  164. function isBlocking()
  165. {
  166. return $this->blocking;
  167. }
  168. /**
  169. * Sets whether the socket connection should be blocking or
  170. * not. A read call to a non-blocking socket will return immediately
  171. * if there is no data available, whereas it will block until there
  172. * is data for blocking sockets.
  173. *
  174. * @param boolean $mode True for blocking sockets, false for nonblocking.
  175. * @access public
  176. * @return mixed true on success or an error object otherwise
  177. */
  178. function setBlocking($mode)
  179. {
  180. if (!is_resource($this->fp)) {
  181. return $this->raiseError('not connected');
  182. }
  183. $this->blocking = $mode;
  184. socket_set_blocking($this->fp, $this->blocking);
  185. return true;
  186. }
  187. /**
  188. * Sets the timeout value on socket descriptor,
  189. * expressed in the sum of seconds and microseconds
  190. *
  191. * @param integer $seconds Seconds.
  192. * @param integer $microseconds Microseconds.
  193. * @access public
  194. * @return mixed true on success or an error object otherwise
  195. */
  196. function setTimeout($seconds, $microseconds)
  197. {
  198. if (!is_resource($this->fp)) {
  199. return $this->raiseError('not connected');
  200. }
  201. return socket_set_timeout($this->fp, $seconds, $microseconds);
  202. }
  203. /**
  204. * Returns information about an existing socket resource.
  205. * Currently returns four entries in the result array:
  206. *
  207. * <p>
  208. * timed_out (bool) - The socket timed out waiting for data<br>
  209. * blocked (bool) - The socket was blocked<br>
  210. * eof (bool) - Indicates EOF event<br>
  211. * unread_bytes (int) - Number of bytes left in the socket buffer<br>
  212. * </p>
  213. *
  214. * @access public
  215. * @return mixed Array containing information about existing socket resource or an error object otherwise
  216. */
  217. function getStatus()
  218. {
  219. if (!is_resource($this->fp)) {
  220. return $this->raiseError('not connected');
  221. }
  222. return socket_get_status($this->fp);
  223. }
  224. /**
  225. * Get a specified line of data
  226. *
  227. * @access public
  228. * @return $size bytes of data from the socket, or a PEAR_Error if
  229. * not connected.
  230. */
  231. function gets($size)
  232. {
  233. if (!is_resource($this->fp)) {
  234. return $this->raiseError('not connected');
  235. }
  236. return @fgets($this->fp, $size);
  237. }
  238. /**
  239. * Read a specified amount of data. This is guaranteed to return,
  240. * and has the added benefit of getting everything in one fread()
  241. * chunk; if you know the size of the data you're getting
  242. * beforehand, this is definitely the way to go.
  243. *
  244. * @param integer $size The number of bytes to read from the socket.
  245. * @access public
  246. * @return $size bytes of data from the socket, or a PEAR_Error if
  247. * not connected.
  248. */
  249. function read($size)
  250. {
  251. if (!is_resource($this->fp)) {
  252. return $this->raiseError('not connected');
  253. }
  254. return @fread($this->fp, $size);
  255. }
  256. /**
  257. * Write a specified amount of data.
  258. *
  259. * @param string $data Data to write.
  260. * @param integer $blocksize Amount of data to write at once.
  261. * NULL means all at once.
  262. *
  263. * @access public
  264. * @return mixed true on success or an error object otherwise
  265. */
  266. function write($data, $blocksize = null)
  267. {
  268. if (!is_resource($this->fp)) {
  269. return $this->raiseError('not connected');
  270. }
  271. if (is_null($blocksize) && !OS_WINDOWS) {
  272. return fwrite($this->fp, $data);
  273. } else {
  274. if (is_null($blocksize)) {
  275. $blocksize = 1024;
  276. }
  277. $pos = 0;
  278. $size = strlen($data);
  279. while ($pos < $size) {
  280. $written = @fwrite($this->fp, substr($data, $pos, $blocksize));
  281. if ($written === false) {
  282. return false;
  283. }
  284. $pos += $written;
  285. }
  286. return $pos;
  287. }
  288. }
  289. /**
  290. * Write a line of data to the socket, followed by a trailing "\r\n".
  291. *
  292. * @access public
  293. * @return mixed fputs result, or an error
  294. */
  295. function writeLine($data)
  296. {
  297. if (!is_resource($this->fp)) {
  298. return $this->raiseError('not connected');
  299. }
  300. return fwrite($this->fp, $data . "\r\n");
  301. }
  302. /**
  303. * Tests for end-of-file on a socket descriptor.
  304. *
  305. * @access public
  306. * @return bool
  307. */
  308. function eof()
  309. {
  310. return (is_resource($this->fp) && feof($this->fp));
  311. }
  312. /**
  313. * Reads a byte of data
  314. *
  315. * @access public
  316. * @return 1 byte of data from the socket, or a PEAR_Error if
  317. * not connected.
  318. */
  319. function readByte()
  320. {
  321. if (!is_resource($this->fp)) {
  322. return $this->raiseError('not connected');
  323. }
  324. return ord(@fread($this->fp, 1));
  325. }
  326. /**
  327. * Reads a word of data
  328. *
  329. * @access public
  330. * @return 1 word of data from the socket, or a PEAR_Error if
  331. * not connected.
  332. */
  333. function readWord()
  334. {
  335. if (!is_resource($this->fp)) {
  336. return $this->raiseError('not connected');
  337. }
  338. $buf = @fread($this->fp, 2);
  339. return (ord($buf[0]) + (ord($buf[1]) << 8));
  340. }
  341. /**
  342. * Reads an int of data
  343. *
  344. * @access public
  345. * @return integer 1 int of data from the socket, or a PEAR_Error if
  346. * not connected.
  347. */
  348. function readInt()
  349. {
  350. if (!is_resource($this->fp)) {
  351. return $this->raiseError('not connected');
  352. }
  353. $buf = @fread($this->fp, 4);
  354. return (ord($buf[0]) + (ord($buf[1]) << 8) +
  355. (ord($buf[2]) << 16) + (ord($buf[3]) << 24));
  356. }
  357. /**
  358. * Reads a zero-terminated string of data
  359. *
  360. * @access public
  361. * @return string, or a PEAR_Error if
  362. * not connected.
  363. */
  364. function readString()
  365. {
  366. if (!is_resource($this->fp)) {
  367. return $this->raiseError('not connected');
  368. }
  369. $string = '';
  370. while (($char = @fread($this->fp, 1)) != "\x00") {
  371. $string .= $char;
  372. }
  373. return $string;
  374. }
  375. /**
  376. * Reads an IP Address and returns it in a dot formated string
  377. *
  378. * @access public
  379. * @return Dot formated string, or a PEAR_Error if
  380. * not connected.
  381. */
  382. function readIPAddress()
  383. {
  384. if (!is_resource($this->fp)) {
  385. return $this->raiseError('not connected');
  386. }
  387. $buf = @fread($this->fp, 4);
  388. return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]),
  389. ord($buf[2]), ord($buf[3]));
  390. }
  391. /**
  392. * Read until either the end of the socket or a newline, whichever
  393. * comes first. Strips the trailing newline from the returned data.
  394. *
  395. * @access public
  396. * @return All available data up to a newline, without that
  397. * newline, or until the end of the socket, or a PEAR_Error if
  398. * not connected.
  399. */
  400. function readLine()
  401. {
  402. if (!is_resource($this->fp)) {
  403. return $this->raiseError('not connected');
  404. }
  405. $line = '';
  406. $timeout = time() + $this->timeout;
  407. while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) {
  408. $line .= @fgets($this->fp, $this->lineLength);
  409. if (substr($line, -1) == "\n") {
  410. return rtrim($line, "\r\n");
  411. }
  412. }
  413. return $line;
  414. }
  415. /**
  416. * Read until the socket closes, or until there is no more data in
  417. * the inner PHP buffer. If the inner buffer is empty, in blocking
  418. * mode we wait for at least 1 byte of data. Therefore, in
  419. * blocking mode, if there is no data at all to be read, this
  420. * function will never exit (unless the socket is closed on the
  421. * remote end).
  422. *
  423. * @access public
  424. *
  425. * @return string All data until the socket closes, or a PEAR_Error if
  426. * not connected.
  427. */
  428. function readAll()
  429. {
  430. if (!is_resource($this->fp)) {
  431. return $this->raiseError('not connected');
  432. }
  433. $data = '';
  434. while (!feof($this->fp)) {
  435. $data .= @fread($this->fp, $this->lineLength);
  436. }
  437. return $data;
  438. }
  439. /**
  440. * Runs the equivalent of the select() system call on the socket
  441. * with a timeout specified by tv_sec and tv_usec.
  442. *
  443. * @param integer $state Which of read/write/error to check for.
  444. * @param integer $tv_sec Number of seconds for timeout.
  445. * @param integer $tv_usec Number of microseconds for timeout.
  446. *
  447. * @access public
  448. * @return False if select fails, integer describing which of read/write/error
  449. * are ready, or PEAR_Error if not connected.
  450. */
  451. function select($state, $tv_sec, $tv_usec = 0)
  452. {
  453. if (!is_resource($this->fp)) {
  454. return $this->raiseError('not connected');
  455. }
  456. $read = null;
  457. $write = null;
  458. $except = null;
  459. if ($state & NET_SOCKET_READ) {
  460. $read[] = $this->fp;
  461. }
  462. if ($state & NET_SOCKET_WRITE) {
  463. $write[] = $this->fp;
  464. }
  465. if ($state & NET_SOCKET_ERROR) {
  466. $except[] = $this->fp;
  467. }
  468. if (false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) {
  469. return false;
  470. }
  471. $result = 0;
  472. if (count($read)) {
  473. $result |= NET_SOCKET_READ;
  474. }
  475. if (count($write)) {
  476. $result |= NET_SOCKET_WRITE;
  477. }
  478. if (count($except)) {
  479. $result |= NET_SOCKET_ERROR;
  480. }
  481. return $result;
  482. }
  483. }