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.9 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.9
      • 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
  • None

Classes

  • CakeRequest
  • CakeResponse
  • CakeSocket
  1: <?php
  2: /**
  3:  * CakePHP Socket connection class.
  4:  *
  5:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7:  *
  8:  * Licensed under The MIT License
  9:  * For full copyright and license information, please see the LICENSE.txt
 10:  * Redistributions of files must retain the above copyright notice.
 11:  *
 12:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 13:  * @link          http://cakephp.org CakePHP(tm) Project
 14:  * @package       Cake.Network
 15:  * @since         CakePHP(tm) v 1.2.0
 16:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 17:  */
 18: 
 19: App::uses('Validation', 'Utility');
 20: 
 21: /**
 22:  * CakePHP network socket connection class.
 23:  *
 24:  * Core base class for network communication.
 25:  *
 26:  * @package       Cake.Network
 27:  */
 28: class CakeSocket {
 29: 
 30: /**
 31:  * CakeSocket description
 32:  *
 33:  * @var string
 34:  */
 35:     public $description = 'Remote DataSource Network Socket Interface';
 36: 
 37: /**
 38:  * Base configuration settings for the socket connection
 39:  *
 40:  * @var array
 41:  */
 42:     protected $_baseConfig = array(
 43:         'persistent' => false,
 44:         'host' => 'localhost',
 45:         'protocol' => 'tcp',
 46:         'port' => 80,
 47:         'timeout' => 30,
 48:         'cryptoType' => 'tls',
 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 bool
 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:  * True if the socket stream is encrypted after a CakeSocket::enableCrypto() call
 81:  *
 82:  * @var bool
 83:  */
 84:     public $encrypted = false;
 85: 
 86: /**
 87:  * Contains all the encryption methods available
 88:  *
 89:  * @var array
 90:  */
 91:     protected $_encryptMethods = array(
 92:         // @codingStandardsIgnoreStart
 93:         'sslv2_client' => STREAM_CRYPTO_METHOD_SSLv2_CLIENT,
 94:         'sslv3_client' => STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
 95:         'sslv23_client' => STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
 96:         'tls_client' => STREAM_CRYPTO_METHOD_TLS_CLIENT,
 97:         'sslv2_server' => STREAM_CRYPTO_METHOD_SSLv2_SERVER,
 98:         'sslv3_server' => STREAM_CRYPTO_METHOD_SSLv3_SERVER,
 99:         'sslv23_server' => STREAM_CRYPTO_METHOD_SSLv23_SERVER,
100:         'tls_server' => STREAM_CRYPTO_METHOD_TLS_SERVER,
101:         // @codingStandardsIgnoreEnd
102:     );
103: 
104: /**
105:  * Used to capture connection warnings which can happen when there are
106:  * SSL errors for example.
107:  *
108:  * @var array
109:  */
110:     protected $_connectionErrors = array();
111: 
112: /**
113:  * Constructor.
114:  *
115:  * @param array $config Socket configuration, which will be merged with the base configuration
116:  * @see CakeSocket::$_baseConfig
117:  */
118:     public function __construct($config = array()) {
119:         $this->config = array_merge($this->_baseConfig, $config);
120: 
121:         $this->_addTlsVersions();
122:     }
123: 
124: /**
125:  * Add TLS versions that are dependent on specific PHP versions.
126:  *
127:  * These TLS versions are not supported by older PHP versions,
128:  * so we have to conditionally set them if they are supported.
129:  *
130:  * As of PHP5.6.6, STREAM_CRYPTO_METHOD_TLS_CLIENT does not include
131:  * TLS1.1 or 1.2. If we have TLS1.2 support we need to update the method map.
132:  *
133:  * @see https://bugs.php.net/bug.php?id=69195
134:  * @see https://github.com/php/php-src/commit/10bc5fd4c4c8e1dd57bd911b086e9872a56300a0
135:  * @return void
136:  */
137:     protected function _addTlsVersions() {
138:         $conditionalCrypto = array(
139:             'tlsv1_1_client' => 'STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT',
140:             'tlsv1_2_client' => 'STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT',
141:             'tlsv1_1_server' => 'STREAM_CRYPTO_METHOD_TLSv1_1_SERVER',
142:             'tlsv1_2_server' => 'STREAM_CRYPTO_METHOD_TLSv1_2_SERVER'
143:         );
144:         foreach ($conditionalCrypto as $key => $const) {
145:             if (defined($const)) {
146:                 $this->_encryptMethods[$key] = constant($const);
147:             }
148:         }
149: 
150:         // @codingStandardsIgnoreStart
151:         if (isset($this->_encryptMethods['tlsv1_2_client'])) {
152:             $this->_encryptMethods['tls_client'] = STREAM_CRYPTO_METHOD_TLS_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
153:         }
154:         if (isset($this->_encryptMethods['tlsv1_2_server'])) {
155:             $this->_encryptMethods['tls_server'] = STREAM_CRYPTO_METHOD_TLS_SERVER | STREAM_CRYPTO_METHOD_TLSv1_1_SERVER | STREAM_CRYPTO_METHOD_TLSv1_2_SERVER;
156:         }
157:         // @codingStandardsIgnoreEnd
158:     }
159: 
160: /**
161:  * Connects the socket to the given host and port.
162:  *
163:  * @return bool Success
164:  * @throws SocketException
165:  */
166:     public function connect() {
167:         if ($this->connection) {
168:             $this->disconnect();
169:         }
170: 
171:         $hasProtocol = strpos($this->config['host'], '://') !== false;
172:         if ($hasProtocol) {
173:             list($this->config['protocol'], $this->config['host']) = explode('://', $this->config['host']);
174:         }
175:         $scheme = null;
176:         if (!empty($this->config['protocol'])) {
177:             $scheme = $this->config['protocol'] . '://';
178:         }
179:         if (!empty($this->config['proxy'])) {
180:             $scheme = 'tcp://';
181:         }
182: 
183:         $host = $this->config['host'];
184:         if (isset($this->config['request']['uri']['host'])) {
185:             $host = $this->config['request']['uri']['host'];
186:         }
187:         $this->_setSslContext($host);
188: 
189:         if (!empty($this->config['context'])) {
190:             $context = stream_context_create($this->config['context']);
191:         } else {
192:             $context = stream_context_create();
193:         }
194: 
195:         $connectAs = STREAM_CLIENT_CONNECT;
196:         if ($this->config['persistent']) {
197:             $connectAs |= STREAM_CLIENT_PERSISTENT;
198:         }
199: 
200:         set_error_handler(array($this, '_connectionErrorHandler'));
201:         $this->connection = stream_socket_client(
202:             $scheme . $this->config['host'] . ':' . $this->config['port'],
203:             $errNum,
204:             $errStr,
205:             $this->config['timeout'],
206:             $connectAs,
207:             $context
208:         );
209:         restore_error_handler();
210: 
211:         if (!empty($errNum) || !empty($errStr)) {
212:             $this->setLastError($errNum, $errStr);
213:             throw new SocketException($errStr, $errNum);
214:         }
215: 
216:         if (!$this->connection && $this->_connectionErrors) {
217:             $message = implode("\n", $this->_connectionErrors);
218:             throw new SocketException($message, E_WARNING);
219:         }
220: 
221:         $this->connected = is_resource($this->connection);
222:         if ($this->connected) {
223:             stream_set_timeout($this->connection, $this->config['timeout']);
224: 
225:             if (!empty($this->config['request']) &&
226:                 $this->config['request']['uri']['scheme'] === 'https' &&
227:                 !empty($this->config['proxy'])
228:             ) {
229:                 $req = array();
230:                 $req[] = 'CONNECT ' . $this->config['request']['uri']['host'] . ':' .
231:                     $this->config['request']['uri']['port'] . ' HTTP/1.1';
232:                 $req[] = 'Host: ' . $this->config['host'];
233:                 $req[] = 'User-Agent: php proxy';
234:                 if (!empty($this->config['proxyauth'])) {
235:                     $req[] = 'Proxy-Authorization: ' . $this->config['proxyauth'];
236:                 }
237: 
238:                 fwrite($this->connection, implode("\r\n", $req) . "\r\n\r\n");
239: 
240:                 while (!feof($this->connection)) {
241:                     $s = rtrim(fgets($this->connection, 4096));
242:                     if (preg_match('/^$/', $s)) {
243:                         break;
244:                     }
245:                 }
246: 
247:                 $this->enableCrypto($this->config['cryptoType'], 'client');
248:             }
249:         }
250:         return $this->connected;
251:     }
252: 
253: /**
254:  * Configure the SSL context options.
255:  *
256:  * @param string $host The host name being connected to.
257:  * @return void
258:  */
259:     protected function _setSslContext($host) {
260:         foreach ($this->config as $key => $value) {
261:             if (substr($key, 0, 4) !== 'ssl_') {
262:                 continue;
263:             }
264:             $contextKey = substr($key, 4);
265:             if (empty($this->config['context']['ssl'][$contextKey])) {
266:                 $this->config['context']['ssl'][$contextKey] = $value;
267:             }
268:             unset($this->config[$key]);
269:         }
270:         if (version_compare(PHP_VERSION, '5.3.2', '>=')) {
271:             if (!isset($this->config['context']['ssl']['SNI_enabled'])) {
272:                 $this->config['context']['ssl']['SNI_enabled'] = true;
273:             }
274:             if (version_compare(PHP_VERSION, '5.6.0', '>=')) {
275:                 if (empty($this->config['context']['ssl']['peer_name'])) {
276:                     $this->config['context']['ssl']['peer_name'] = $host;
277:                 }
278:             } else {
279:                 if (empty($this->config['context']['ssl']['SNI_server_name'])) {
280:                     $this->config['context']['ssl']['SNI_server_name'] = $host;
281:                 }
282:             }
283:         }
284:         if (empty($this->config['context']['ssl']['cafile'])) {
285:             $this->config['context']['ssl']['cafile'] = CAKE . 'Config' . DS . 'cacert.pem';
286:         }
287:         if (!empty($this->config['context']['ssl']['verify_host'])) {
288:             $this->config['context']['ssl']['CN_match'] = $host;
289:         }
290:         unset($this->config['context']['ssl']['verify_host']);
291:     }
292: 
293: /**
294:  * socket_stream_client() does not populate errNum, or $errStr when there are
295:  * connection errors, as in the case of SSL verification failure.
296:  *
297:  * Instead we need to handle those errors manually.
298:  *
299:  * @param int $code Code.
300:  * @param string $message Message.
301:  * @return void
302:  */
303:     protected function _connectionErrorHandler($code, $message) {
304:         $this->_connectionErrors[] = $message;
305:     }
306: 
307: /**
308:  * Gets the connection context.
309:  *
310:  * @return null|array Null when there is no connection, an array when there is.
311:  */
312:     public function context() {
313:         if (!$this->connection) {
314:             return null;
315:         }
316:         return stream_context_get_options($this->connection);
317:     }
318: 
319: /**
320:  * Gets the host name of the current connection.
321:  *
322:  * @return string Host name
323:  */
324:     public function host() {
325:         if (Validation::ip($this->config['host'])) {
326:             return gethostbyaddr($this->config['host']);
327:         }
328:         return gethostbyaddr($this->address());
329:     }
330: 
331: /**
332:  * Gets the IP address of the current connection.
333:  *
334:  * @return string IP address
335:  */
336:     public function address() {
337:         if (Validation::ip($this->config['host'])) {
338:             return $this->config['host'];
339:         }
340:         return gethostbyname($this->config['host']);
341:     }
342: 
343: /**
344:  * Gets all IP addresses associated with the current connection.
345:  *
346:  * @return array IP addresses
347:  */
348:     public function addresses() {
349:         if (Validation::ip($this->config['host'])) {
350:             return array($this->config['host']);
351:         }
352:         return gethostbynamel($this->config['host']);
353:     }
354: 
355: /**
356:  * Gets the last error as a string.
357:  *
358:  * @return string|null Last error
359:  */
360:     public function lastError() {
361:         if (!empty($this->lastError)) {
362:             return $this->lastError['num'] . ': ' . $this->lastError['str'];
363:         }
364:         return null;
365:     }
366: 
367: /**
368:  * Sets the last error.
369:  *
370:  * @param int $errNum Error code
371:  * @param string $errStr Error string
372:  * @return void
373:  */
374:     public function setLastError($errNum, $errStr) {
375:         $this->lastError = array('num' => $errNum, 'str' => $errStr);
376:     }
377: 
378: /**
379:  * Writes data to the socket.
380:  *
381:  * @param string $data The data to write to the socket
382:  * @return bool Success
383:  */
384:     public function write($data) {
385:         if (!$this->connected) {
386:             if (!$this->connect()) {
387:                 return false;
388:             }
389:         }
390:         $totalBytes = strlen($data);
391:         for ($written = 0, $rv = 0; $written < $totalBytes; $written += $rv) {
392:             $rv = fwrite($this->connection, substr($data, $written));
393:             if ($rv === false || $rv === 0) {
394:                 return $written;
395:             }
396:         }
397:         return $written;
398:     }
399: 
400: /**
401:  * Reads data from the socket. Returns false if no data is available or no connection could be
402:  * established.
403:  *
404:  * @param int $length Optional buffer length to read; defaults to 1024
405:  * @return mixed Socket data
406:  */
407:     public function read($length = 1024) {
408:         if (!$this->connected) {
409:             if (!$this->connect()) {
410:                 return false;
411:             }
412:         }
413: 
414:         if (!feof($this->connection)) {
415:             $buffer = fread($this->connection, $length);
416:             $info = stream_get_meta_data($this->connection);
417:             if ($info['timed_out']) {
418:                 $this->setLastError(E_WARNING, __d('cake_dev', 'Connection timed out'));
419:                 return false;
420:             }
421:             return $buffer;
422:         }
423:         return false;
424:     }
425: 
426: /**
427:  * Disconnects the socket from the current connection.
428:  *
429:  * @return bool Success
430:  */
431:     public function disconnect() {
432:         if (!is_resource($this->connection)) {
433:             $this->connected = false;
434:             return true;
435:         }
436:         $this->connected = !fclose($this->connection);
437: 
438:         if (!$this->connected) {
439:             $this->connection = null;
440:         }
441:         return !$this->connected;
442:     }
443: 
444: /**
445:  * Destructor, used to disconnect from current connection.
446:  */
447:     public function __destruct() {
448:         $this->disconnect();
449:     }
450: 
451: /**
452:  * Resets the state of this Socket instance to it's initial state (before CakeObject::__construct got executed)
453:  *
454:  * @param array $state Array with key and values to reset
455:  * @return bool True on success
456:  */
457:     public function reset($state = null) {
458:         if (empty($state)) {
459:             static $initalState = array();
460:             if (empty($initalState)) {
461:                 $initalState = get_class_vars(__CLASS__);
462:             }
463:             $state = $initalState;
464:         }
465: 
466:         foreach ($state as $property => $value) {
467:             $this->{$property} = $value;
468:         }
469:         return true;
470:     }
471: 
472: /**
473:  * Encrypts current stream socket, using one of the defined encryption methods.
474:  *
475:  * @param string $type Type which can be one of 'sslv2', 'sslv3', 'sslv23', 'tls', 'tlsv1_1' or 'tlsv1_2'.
476:  * @param string $clientOrServer Can be one of 'client', 'server'. Default is 'client'.
477:  * @param bool $enable Enable or disable encryption. Default is true (enable)
478:  * @return bool True on success
479:  * @throws InvalidArgumentException When an invalid encryption scheme is chosen.
480:  * @throws SocketException When attempting to enable SSL/TLS fails.
481:  * @see stream_socket_enable_crypto
482:  */
483:     public function enableCrypto($type, $clientOrServer = 'client', $enable = true) {
484:         if (!array_key_exists($type . '_' . $clientOrServer, $this->_encryptMethods)) {
485:             throw new InvalidArgumentException(__d('cake_dev', 'Invalid encryption scheme chosen'));
486:         }
487:         $enableCryptoResult = false;
488:         try {
489:             $enableCryptoResult = stream_socket_enable_crypto($this->connection, $enable,
490:                 $this->_encryptMethods[$type . '_' . $clientOrServer]);
491:         } catch (Exception $e) {
492:             $this->setLastError(null, $e->getMessage());
493:             throw new SocketException($e->getMessage());
494:         }
495:         if ($enableCryptoResult === true) {
496:             $this->encrypted = $enable;
497:             return true;
498:         }
499:         $errorMessage = __d('cake_dev', 'Unable to perform enableCrypto operation on CakeSocket');
500:         $this->setLastError(null, $errorMessage);
501:         throw new SocketException($errorMessage);
502:     }
503: }
504: 
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