PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/WebsiteFiles/admin/bin/pear/Net/Socket.php

https://github.com/mcherryleigh/seniordesign
PHP | 429 lines | 169 code | 34 blank | 226 comment | 34 complexity | 7f183ad177f7af4171c87e4d089553f4 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 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@fast.no> |
  17. // | Chuck Hagenbuch <chuck@horde.org> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Socket.php,v 1.1 2005/07/22 05:10:14 max Exp $
  21. //
  22. require_once PEAR_DIR . 'PEAR.php';
  23. /**
  24. * Generalized Socket class. More docs to be written.
  25. *
  26. * @version 1.0
  27. * @author Stig Bakken <ssb@fast.no>
  28. * @author Chuck Hagenbuch <chuck@horde.org>
  29. */
  30. class Net_Socket extends PEAR {
  31. // {{{ properties
  32. /** Socket file pointer. */
  33. var $fp = null;
  34. /** Whether the socket is blocking. */
  35. var $blocking = true;
  36. /** Whether the socket is persistent. */
  37. var $persistent = false;
  38. /** The IP address to connect to. */
  39. var $addr = '';
  40. /** The port number to connect to. */
  41. var $port = 0;
  42. /** Number of seconds to wait on socket connections before
  43. assuming there's no more data. */
  44. var $timeout = false;
  45. /** Number of bytes to read at a time in readLine() and
  46. readAll(). */
  47. var $lineLength = 2048;
  48. // }}}
  49. // {{{ constructor
  50. /**
  51. * Constructs a new Net_Socket object.
  52. *
  53. * @access public
  54. */
  55. function Net_Socket() {
  56. $this->PEAR();
  57. }
  58. // }}}
  59. // {{{ connect()
  60. /**
  61. * Connect to the specified port. If called when the socket is
  62. * already connected, it disconnects and connects again.
  63. *
  64. * @param $addr string IP address or host name
  65. * @param $port int TCP port number
  66. * @param $persistent bool (optional) whether the connection is
  67. * persistent (kept open between requests by the web server)
  68. * @param $timeout int (optional) how long to wait for data
  69. * @access public
  70. * @return mixed true on success or error object
  71. */
  72. function connect($addr, $port, $persistent = null, $timeout = null) {
  73. if (is_resource($this->fp)) {
  74. @fclose($this->fp);
  75. $this->fp = null;
  76. }
  77. if (strspn($addr, '.0123456789') == strlen($addr)) {
  78. $this->addr = $addr;
  79. } else {
  80. $this->addr = gethostbyname($addr);
  81. }
  82. $this->port = $port % 65536;
  83. if ($persistent !== null) {
  84. $this->persistent = $persistent;
  85. }
  86. if ($timeout !== null) {
  87. $this->timeout = $timeout;
  88. }
  89. $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen';
  90. $errno = 0;
  91. $errstr = '';
  92. if ($this->timeout) {
  93. $fp = $openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout);
  94. } else {
  95. $fp = $openfunc($this->addr, $this->port, $errno, $errstr);
  96. }
  97. if (!$fp) {
  98. return $this->raiseError($errstr, $errno);
  99. }
  100. $this->fp = $fp;
  101. return $this->setBlocking($this->blocking);
  102. }
  103. // }}}
  104. // {{{ disconnect()
  105. /**
  106. * Disconnects from the peer, closes the socket.
  107. *
  108. * @access public
  109. * @return mixed true on success or an error object otherwise
  110. */
  111. function disconnect() {
  112. if (is_resource($this->fp)) {
  113. fclose($this->fp);
  114. $this->fp = null;
  115. return true;
  116. }
  117. return $this->raiseError("not connected");
  118. }
  119. // }}}
  120. // {{{ isBlocking()
  121. /**
  122. * Find out if the socket is in blocking mode.
  123. *
  124. * @access public
  125. * @return bool the current blocking mode.
  126. */
  127. function isBlocking() {
  128. return $this->blocking;
  129. }
  130. // }}}
  131. // {{{ setBlocking()
  132. /**
  133. * Sets whether the socket connection should be blocking or
  134. * not. A read call to a non-blocking socket will return immediately
  135. * if there is no data available, whereas it will block until there
  136. * is data for blocking sockets.
  137. *
  138. * @param $mode bool true for blocking sockets, false for nonblocking
  139. * @access public
  140. * @return mixed true on success or an error object otherwise
  141. */
  142. function setBlocking($mode) {
  143. if (is_resource($this->fp)) {
  144. $this->blocking = $mode;
  145. socket_set_blocking($this->fp, $this->blocking);
  146. return true;
  147. }
  148. return $this->raiseError("not connected");
  149. }
  150. // }}}
  151. // {{{ setTimeout()
  152. /**
  153. * Sets the timeout value on socket descriptor,
  154. * expressed in the sum of seconds and microseconds
  155. *
  156. * @param $seconds int seconds
  157. * @param $microseconds int microseconds
  158. * @access public
  159. * @return mixed true on success or an error object otherwise
  160. */
  161. function setTimeout($seconds, $microseconds) {
  162. if (is_resource($this->fp)) {
  163. socket_set_timeout($this->fp, $seconds, $microseconds);
  164. return true;
  165. }
  166. return $this->raiseError("not connected");
  167. }
  168. // }}}
  169. // {{{ getStatus()
  170. /**
  171. * Returns information about an existing socket resource.
  172. * Currently returns four entries in the result array:
  173. *
  174. * <p>
  175. * timed_out (bool) - The socket timed out waiting for data<br>
  176. * blocked (bool) - The socket was blocked<br>
  177. * eof (bool) - Indicates EOF event<br>
  178. * unread_bytes (int) - Number of bytes left in the socket buffer<br>
  179. * </p>
  180. *
  181. * @access public
  182. * @return mixed Array containing information about existing socket resource or an error object otherwise
  183. */
  184. function getStatus() {
  185. if (is_resource($this->fp)) {
  186. return socket_get_status($this->fp);
  187. }
  188. return $this->raiseError("not connected");
  189. }
  190. // }}}
  191. // {{{ gets()
  192. /**
  193. * Get a specified line of data
  194. *
  195. * @access public
  196. * @return $size bytes of data from the socket, or a PEAR_Error if
  197. * not connected.
  198. */
  199. function gets($size) {
  200. if (is_resource($this->fp)) {
  201. return fgets($this->fp, $size);
  202. }
  203. return $this->raiseError("not connected");
  204. }
  205. // }}}
  206. // {{{ read()
  207. /**
  208. * Read a specified amount of data. This is guaranteed to return,
  209. * and has the added benefit of getting everything in one fread()
  210. * chunk; if you know the size of the data you're getting
  211. * beforehand, this is definitely the way to go.
  212. *
  213. * @param $size The number of bytes to read from the socket.
  214. * @access public
  215. * @return $size bytes of data from the socket, or a PEAR_Error if
  216. * not connected.
  217. */
  218. function read($size) {
  219. if (is_resource($this->fp)) {
  220. return fread($this->fp, $size);
  221. }
  222. return $this->raiseError("not connected");
  223. }
  224. // }}}
  225. // {{{ write()
  226. /**
  227. * Write a specified amount of data.
  228. *
  229. * @access public
  230. * @return mixed true on success or an error object otherwise
  231. */
  232. function write($data) {
  233. if (is_resource($this->fp)) {
  234. return fwrite($this->fp, $data);
  235. }
  236. return $this->raiseError("not connected");
  237. }
  238. // }}}
  239. // {{{ writeLine()
  240. /**
  241. * Write a line of data to the socket, followed by a trailing "\r\n".
  242. *
  243. * @access public
  244. * @return mixed fputs result, or an error
  245. */
  246. function writeLine ($data) {
  247. if (is_resource($this->fp)) {
  248. return $this->write($data . "\r\n");
  249. }
  250. return $this->raiseError("not connected");
  251. }
  252. // }}}
  253. // {{{ eof()
  254. /**
  255. * Tests for end-of-file on a socket descriptor
  256. *
  257. * @access public
  258. * @return bool
  259. */
  260. function eof() {
  261. return (is_resource($this->fp) && feof($this->fp));
  262. }
  263. // }}}
  264. // {{{ readByte()
  265. /**
  266. * Reads a byte of data
  267. *
  268. * @access public
  269. * @return 1 byte of data from the socket, or a PEAR_Error if
  270. * not connected.
  271. */
  272. function readByte() {
  273. if (is_resource($this->fp)) {
  274. return ord($this->read(1));
  275. }
  276. return $this->raiseError("not connected");
  277. }
  278. // }}}
  279. // {{{ readWord()
  280. /**
  281. * Reads a word of data
  282. *
  283. * @access public
  284. * @return 1 word of data from the socket, or a PEAR_Error if
  285. * not connected.
  286. */
  287. function readWord() {
  288. if (is_resource($this->fp)) {
  289. $buf = $this->read(2);
  290. return (ord($buf[0]) + (ord($buf[1]) << 8));
  291. }
  292. return $this->raiseError("not connected");
  293. }
  294. // }}}
  295. // {{{ readInt()
  296. /**
  297. * Reads an int of data
  298. *
  299. * @access public
  300. * @return 1 int of data from the socket, or a PEAR_Error if
  301. * not connected.
  302. */
  303. function readInt() {
  304. if (is_resource($this->fp)) {
  305. $buf = $this->read(4);
  306. return (ord($buf[0]) + (ord($buf[1]) << 8) +
  307. (ord($buf[2]) << 16) + (ord($buf[3]) << 24));
  308. }
  309. return $this->raiseError("not connected");
  310. }
  311. // }}}
  312. // {{{ readString()
  313. /**
  314. * Reads a zeroterminated string of data
  315. *
  316. * @access public
  317. * @return string, or a PEAR_Error if
  318. * not connected.
  319. */
  320. function readString() {
  321. if (is_resource($this->fp)) {
  322. $string = '';
  323. while (($char = $this->read(1)) != "\x00") {
  324. $string .= $char;
  325. }
  326. return $string;
  327. }
  328. return $this->raiseError("not connected");
  329. }
  330. // }}}
  331. // {{{ readIPAddress()
  332. /**
  333. * Reads an IP Address and returns it in a dot formated string
  334. *
  335. * @access public
  336. * @return Dot formated string, or a PEAR_Error if
  337. * not connected.
  338. */
  339. function readIPAddress() {
  340. if (is_resource($this->fp)) {
  341. $buf = $this->read(4);
  342. return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]),
  343. ord($buf[2]), ord($buf[3]));
  344. }
  345. return $this->raiseError("not connected");
  346. }
  347. // }}}
  348. // {{{ readLine()
  349. /**
  350. * Read until either the end of the socket or a newline, whichever
  351. * comes first. Strips the trailing newline from the returned data.
  352. *
  353. * @access public
  354. * @return All available data up to a newline, without that
  355. * newline, or until the end of the socket, or a PEAR_Error if
  356. * not connected.
  357. */
  358. function readLine() {
  359. if (is_resource($this->fp)) {
  360. $line = '';
  361. $timeout = time() + $this->timeout;
  362. while (!$this->eof() && (!$this->timeout || time() < $timeout)) {
  363. $line .= $this->gets($this->lineLength);
  364. if (strlen($line) >= 2 &&
  365. (substr($line, -2) == "\r\n" ||
  366. substr($line, -1) == "\n")) {
  367. return rtrim($line);
  368. }
  369. }
  370. return $line;
  371. }
  372. return $this->raiseError("not connected");
  373. }
  374. // }}}
  375. // {{{ readAll()
  376. /**
  377. * Read until the socket closes. THIS FUNCTION WILL NOT EXIT if the
  378. * socket is in blocking mode until the socket closes.
  379. *
  380. * @access public
  381. * @return All data until the socket closes, or a PEAR_Error if
  382. * not connected.
  383. */
  384. function readAll() {
  385. if (is_resource($this->fp)) {
  386. $data = '';
  387. while (!$this->eof())
  388. $data .= $this->read($this->lineLength);
  389. return $data;
  390. }
  391. return $this->raiseError("not connected");
  392. }
  393. // }}}
  394. }
  395. ?>