File indexing completed on 2024-05-12 06:03:09

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category  Zend
0016  * @package   Zend_TimeSync
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version   $Id$
0020  */
0021 
0022 /**
0023  * Abstract class definition for all timeserver protocols
0024  *
0025  * @category  Zend
0026  * @package   Zend_TimeSync
0027  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0028  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0029  */
0030 abstract class Zend_TimeSync_Protocol
0031 {
0032     /**
0033      * Holds the current socket connection
0034      *
0035      * @var array
0036      */
0037     protected $_socket;
0038 
0039     /**
0040      * Exceptions that might have occured
0041      *
0042      * @var array
0043      */
0044     protected $_exceptions;
0045 
0046     /**
0047      * Hostname for timeserver
0048      *
0049      * @var string
0050      */
0051     protected $_timeserver;
0052 
0053     /**
0054      * Holds information passed/returned from timeserver
0055      *
0056      * @var array
0057      */
0058     protected $_info = array();
0059 
0060     /**
0061      * Abstract method that prepares the data to send to the timeserver
0062      *
0063      * @return mixed
0064      */
0065     abstract protected function _prepare();
0066 
0067     /**
0068      * Abstract method that reads the data returned from the timeserver
0069      *
0070      * @return mixed
0071      */
0072     abstract protected function _read();
0073 
0074     /**
0075      * Abstract method that writes data to to the timeserver
0076      *
0077      * @param  string $data Data to write
0078      * @return void
0079      */
0080     abstract protected function _write($data);
0081 
0082     /**
0083      * Abstract method that extracts the binary data returned from the timeserver
0084      *
0085      * @param  string|array $data Data returned from the timeserver
0086      * @return integer
0087      */
0088     abstract protected function _extract($data);
0089 
0090     /**
0091      * Connect to the specified timeserver.
0092      *
0093      * @return void
0094      * @throws Zend_TimeSync_Exception When the connection failed
0095      */
0096     protected function _connect()
0097     {
0098         $socket = @fsockopen($this->_timeserver, $this->_port, $errno, $errstr,
0099                              Zend_TimeSync::$options['timeout']);
0100         if ($socket === false) {
0101             throw new Zend_TimeSync_Exception('could not connect to ' .
0102                 "'$this->_timeserver' on port '$this->_port', reason: '$errstr'");
0103         }
0104 
0105         $this->_socket = $socket;
0106     }
0107 
0108     /**
0109      * Disconnects from the peer, closes the socket.
0110      *
0111      * @return void
0112      */
0113     protected function _disconnect()
0114     {
0115         @fclose($this->_socket);
0116         $this->_socket = null;
0117     }
0118 
0119     /**
0120      * Return information sent/returned from the timeserver
0121      *
0122      * @return  array
0123      */
0124     public function getInfo()
0125     {
0126         if (empty($this->_info) === true) {
0127             $this->_write($this->_prepare());
0128             $timestamp = $this->_extract($this->_read());
0129         }
0130 
0131         return $this->_info;
0132     }
0133 
0134     /**
0135      * Query this timeserver without using the fallback mechanism
0136      *
0137      * @param  string|Zend_Locale $locale (Optional) Locale
0138      * @return Zend_Date
0139      */
0140     public function getDate($locale = null)
0141     {
0142         $this->_write($this->_prepare());
0143         $timestamp = $this->_extract($this->_read());
0144 
0145         $date = new Zend_Date($this, null, $locale);
0146         return $date;
0147     }
0148 }