/cake/cake/libs/socket.php

https://github.com/geeknbar/projet-tut-site-web · PHP · 290 lines · 133 code · 11 blank · 146 comment · 31 complexity · 2d26373fed7a598d32ff5858ae94a71c MD5 · raw file

  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Cake Socket connection class.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package cake
  17. * @subpackage cake.cake.libs
  18. * @since CakePHP(tm) v 1.2.0
  19. * @version $Revision$
  20. * @modifiedby $LastChangedBy$
  21. * @lastmodified $Date$
  22. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  23. */
  24. App::import('Core', 'Validation');
  25. /**
  26. * Cake network socket connection class.
  27. *
  28. * Core base class for network communication.
  29. *
  30. * @package cake
  31. * @subpackage cake.cake.libs
  32. */
  33. class CakeSocket extends Object {
  34. /**
  35. * Object description
  36. *
  37. * @var string
  38. * @access public
  39. */
  40. var $description = 'Remote DataSource Network Socket Interface';
  41. /**
  42. * Base configuration settings for the socket connection
  43. *
  44. * @var array
  45. * @access protected
  46. */
  47. var $_baseConfig = array(
  48. 'persistent' => false,
  49. 'host' => 'localhost',
  50. 'protocol' => 'tcp',
  51. 'port' => 80,
  52. 'timeout' => 30
  53. );
  54. /**
  55. * Configuration settings for the socket connection
  56. *
  57. * @var array
  58. * @access public
  59. */
  60. var $config = array();
  61. /**
  62. * Reference to socket connection resource
  63. *
  64. * @var resource
  65. * @access public
  66. */
  67. var $connection = null;
  68. /**
  69. * This boolean contains the current state of the CakeSocket class
  70. *
  71. * @var boolean
  72. * @access public
  73. */
  74. var $connected = false;
  75. /**
  76. * This variable contains an array with the last error number (num) and string (str)
  77. *
  78. * @var array
  79. * @access public
  80. */
  81. var $lastError = array();
  82. /**
  83. * Constructor.
  84. *
  85. * @param array $config Socket configuration, which will be merged with the base configuration
  86. */
  87. function __construct($config = array()) {
  88. parent::__construct();
  89. $this->config = array_merge($this->_baseConfig, $config);
  90. if (!is_numeric($this->config['protocol'])) {
  91. $this->config['protocol'] = getprotobyname($this->config['protocol']);
  92. }
  93. }
  94. /**
  95. * Connect the socket to the given host and port.
  96. *
  97. * @return boolean Success
  98. * @access public
  99. */
  100. function connect() {
  101. if ($this->connection != null) {
  102. $this->disconnect();
  103. }
  104. $scheme = null;
  105. if (isset($this->config['request']) && $this->config['request']['uri']['scheme'] == 'https') {
  106. $scheme = 'ssl://';
  107. }
  108. if ($this->config['persistent'] == true) {
  109. $tmp = null;
  110. $this->connection = @pfsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
  111. } else {
  112. $this->connection = @fsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
  113. }
  114. if (!empty($errNum) || !empty($errStr)) {
  115. $this->setLastError($errStr, $errNum);
  116. }
  117. $this->connected = is_resource($this->connection);
  118. if ($this->connected) {
  119. stream_set_timeout($this->connection, $this->config['timeout']);
  120. }
  121. return $this->connected;
  122. }
  123. /**
  124. * Get the host name of the current connection.
  125. *
  126. * @return string Host name
  127. * @access public
  128. */
  129. function host() {
  130. if (Validation::ip($this->config['host'])) {
  131. return gethostbyaddr($this->config['host']);
  132. } else {
  133. return gethostbyaddr($this->address());
  134. }
  135. }
  136. /**
  137. * Get the IP address of the current connection.
  138. *
  139. * @return string IP address
  140. * @access public
  141. */
  142. function address() {
  143. if (Validation::ip($this->config['host'])) {
  144. return $this->config['host'];
  145. } else {
  146. return gethostbyname($this->config['host']);
  147. }
  148. }
  149. /**
  150. * Get all IP addresses associated with the current connection.
  151. *
  152. * @return array IP addresses
  153. * @access public
  154. */
  155. function addresses() {
  156. if (Validation::ip($this->config['host'])) {
  157. return array($this->config['host']);
  158. } else {
  159. return gethostbynamel($this->config['host']);
  160. }
  161. }
  162. /**
  163. * Get the last error as a string.
  164. *
  165. * @return string Last error
  166. * @access public
  167. */
  168. function lastError() {
  169. if (!empty($this->lastError)) {
  170. return $this->lastError['num'].': '.$this->lastError['str'];
  171. } else {
  172. return null;
  173. }
  174. }
  175. /**
  176. * Set the last error.
  177. *
  178. * @param integer $errNum Error code
  179. * @param string $errStr Error string
  180. * @access public
  181. */
  182. function setLastError($errNum, $errStr) {
  183. $this->lastError = array('num' => $errNum, 'str' => $errStr);
  184. }
  185. /**
  186. * Write data to the socket.
  187. *
  188. * @param string $data The data to write to the socket
  189. * @return boolean Success
  190. * @access public
  191. */
  192. function write($data) {
  193. if (!$this->connected) {
  194. if (!$this->connect()) {
  195. return false;
  196. }
  197. }
  198. return fwrite($this->connection, $data, strlen($data));
  199. }
  200. /**
  201. * Read data from the socket. Returns false if no data is available or no connection could be
  202. * established.
  203. *
  204. * @param integer $length Optional buffer length to read; defaults to 1024
  205. * @return mixed Socket data
  206. * @access public
  207. */
  208. function read($length = 1024) {
  209. if (!$this->connected) {
  210. if (!$this->connect()) {
  211. return false;
  212. }
  213. }
  214. if (!feof($this->connection)) {
  215. $buffer = fread($this->connection, $length);
  216. $info = stream_get_meta_data($this->connection);
  217. if ($info['timed_out']) {
  218. $this->setLastError(E_WARNING, __('Connection timed out', true));
  219. return false;
  220. }
  221. return $buffer;
  222. } else {
  223. return false;
  224. }
  225. }
  226. /**
  227. * Abort socket operation.
  228. *
  229. * @return boolean Success
  230. * @access public
  231. */
  232. function abort() {
  233. }
  234. /**
  235. * Disconnect the socket from the current connection.
  236. *
  237. * @return boolean Success
  238. * @access public
  239. */
  240. function disconnect() {
  241. if (!is_resource($this->connection)) {
  242. $this->connected = false;
  243. return true;
  244. }
  245. $this->connected = !fclose($this->connection);
  246. if (!$this->connected) {
  247. $this->connection = null;
  248. }
  249. return !$this->connected;
  250. }
  251. /**
  252. * Destructor, used to disconnect from current connection.
  253. *
  254. * @access private
  255. */
  256. function __destruct() {
  257. $this->disconnect();
  258. }
  259. /**
  260. * Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
  261. *
  262. * @return boolean True on success
  263. * @access public
  264. */
  265. function reset($state = null) {
  266. if (empty($state)) {
  267. static $initalState = array();
  268. if (empty($initalState)) {
  269. $initalState = get_class_vars(__CLASS__);
  270. }
  271. $state = $initalState;
  272. }
  273. foreach ($state as $property => $value) {
  274. $this->{$property} = $value;
  275. }
  276. return true;
  277. }
  278. }
  279. ?>