CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

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

Generated using CakePHP API Docs