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 1.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.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

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • __enclose
  • make_clean_css
  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  • write_css_cache
  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-2012, 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-2012, 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: 
 90:         $this->config = array_merge($this->_baseConfig, $config);
 91:         if (!is_numeric($this->config['protocol'])) {
 92:             $this->config['protocol'] = getprotobyname($this->config['protocol']);
 93:         }
 94:     }
 95: /**
 96:  * Connect the socket to the given host and port.
 97:  *
 98:  * @return boolean Success
 99:  * @access public
100:  */
101:     function connect() {
102:         if ($this->connection != null) {
103:             $this->disconnect();
104:         }
105: 
106:         $scheme = null;
107:         if (isset($this->config['request']) && $this->config['request']['uri']['scheme'] == 'https') {
108:             $scheme = 'ssl://';
109:         }
110: 
111:         if ($this->config['persistent'] == true) {
112:             $tmp = null;
113:             $this->connection = @pfsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
114:         } else {
115:             $this->connection = @fsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
116:         }
117: 
118:         if (!empty($errNum) || !empty($errStr)) {
119:             $this->setLastError($errStr, $errNum);
120:         }
121: 
122:         $this->connected = is_resource($this->connection);
123:         if ($this->connected) {
124:             stream_set_timeout($this->connection, $this->config['timeout']);
125:         }
126:         return $this->connected;
127:     }
128: 
129: /**
130:  * Get the host name of the current connection.
131:  *
132:  * @return string Host name
133:  * @access public
134:  */
135:     function host() {
136:         if (Validation::ip($this->config['host'])) {
137:             return gethostbyaddr($this->config['host']);
138:         } else {
139:             return gethostbyaddr($this->address());
140:         }
141:     }
142: /**
143:  * Get the IP address of the current connection.
144:  *
145:  * @return string IP address
146:  * @access public
147:  */
148:     function address() {
149:         if (Validation::ip($this->config['host'])) {
150:             return $this->config['host'];
151:         } else {
152:             return gethostbyname($this->config['host']);
153:         }
154:     }
155: /**
156:  * Get all IP addresses associated with the current connection.
157:  *
158:  * @return array IP addresses
159:  * @access public
160:  */
161:     function addresses() {
162:         if (Validation::ip($this->config['host'])) {
163:             return array($this->config['host']);
164:         } else {
165:             return gethostbynamel($this->config['host']);
166:         }
167:     }
168: /**
169:  * Get the last error as a string.
170:  *
171:  * @return string Last error
172:  * @access public
173:  */
174:     function lastError() {
175:         if (!empty($this->lastError)) {
176:             return $this->lastError['num'].': '.$this->lastError['str'];
177:         } else {
178:             return null;
179:         }
180:     }
181: /**
182:  * Set the last error.
183:  *
184:  * @param integer $errNum Error code
185:  * @param string $errStr Error string
186:  * @access public
187:  */
188:     function setLastError($errNum, $errStr) {
189:         $this->lastError = array('num' => $errNum, 'str' => $errStr);
190:     }
191: /**
192:  * Write data to the socket.
193:  *
194:  * @param string $data The data to write to the socket
195:  * @return boolean Success
196:  * @access public
197:  */
198:     function write($data) {
199:         if (!$this->connected) {
200:             if (!$this->connect()) {
201:                 return false;
202:             }
203:         }
204: 
205:         return fwrite($this->connection, $data, strlen($data));
206:     }
207: 
208: /**
209:  * Read data from the socket. Returns false if no data is available or no connection could be
210:  * established.
211:  *
212:  * @param integer $length Optional buffer length to read; defaults to 1024
213:  * @return mixed Socket data
214:  * @access public
215:  */
216:     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, __('Connection timed out', true));
228:                 return false;
229:             }
230:             return $buffer;
231:         } else {
232:             return false;
233:         }
234:     }
235: /**
236:  * Abort socket operation.
237:  *
238:  * @return boolean Success
239:  * @access public
240:  */
241:     function abort() {
242:     }
243: /**
244:  * Disconnect the socket from the current connection.
245:  *
246:  * @return boolean Success
247:  * @access public
248:  */
249:     function disconnect() {
250:         if (!is_resource($this->connection)) {
251:             $this->connected = false;
252:             return true;
253:         }
254:         $this->connected = !fclose($this->connection);
255: 
256:         if (!$this->connected) {
257:             $this->connection = null;
258:         }
259:         return !$this->connected;
260:     }
261: /**
262:  * Destructor, used to disconnect from current connection.
263:  *
264:  * @access private
265:  */
266:     function __destruct() {
267:         $this->disconnect();
268:     }
269: /**
270:  * Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
271:  *
272:  * @return boolean True on success
273:  * @access public
274:  */
275:     function reset($state = null) {
276:         if (empty($state)) {
277:             static $initalState = array();
278:             if (empty($initalState)) {
279:                 $initalState = get_class_vars(__CLASS__);
280:             }
281:             $state = $initalState;
282:         }
283: 
284:         foreach ($state as $property => $value) {
285:             $this->{$property} = $value;
286:         }
287:         return true;
288:     }
289: }
290: ?>
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