File indexing completed on 2024-12-22 05:37:08
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 * Zend_TimeSync_Protocol 0024 */ 0025 // require_once 'Zend/TimeSync/Protocol.php'; 0026 0027 /** 0028 * SNTP Protocol handling class 0029 * 0030 * @category Zend 0031 * @package Zend_TimeSync 0032 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0033 * @license http://framework.zend.com/license/new-bsd New BSD License 0034 */ 0035 class Zend_TimeSync_Sntp extends Zend_TimeSync_Protocol 0036 { 0037 /** 0038 * Port number for this timeserver 0039 * 0040 * @var integer 0041 */ 0042 protected $_port = 37; 0043 0044 /** 0045 * Socket delay 0046 * 0047 * @var integer 0048 */ 0049 private $_delay; 0050 0051 /** 0052 * Class constructor, sets the timeserver and port number 0053 * 0054 * @param string $timeserver Timeserver to connect to 0055 * @param integer $port Port of the timeserver when it differs from the default port 0056 */ 0057 public function __construct($timeserver, $port) 0058 { 0059 $this->_timeserver = 'udp://' . $timeserver; 0060 if ($port !== null) { 0061 $this->_port = $port; 0062 } 0063 } 0064 0065 /** 0066 * Prepares the data that will be send to the timeserver 0067 * 0068 * @return array 0069 */ 0070 protected function _prepare() 0071 { 0072 return "\n"; 0073 } 0074 0075 /** 0076 * Reads the data returned from the timeserver 0077 * 0078 * @return string 0079 */ 0080 protected function _read() 0081 { 0082 $result = fread($this->_socket, 49); 0083 $this->_delay = (($this->_delay - time()) / 2); 0084 0085 return $result; 0086 } 0087 0088 /** 0089 * Writes data to to the timeserver 0090 * 0091 * @param string $data Data to write to the timeserver 0092 * @return void 0093 */ 0094 protected function _write($data) 0095 { 0096 $this->_connect(); 0097 $this->_delay = time(); 0098 fputs($this->_socket, $data); 0099 } 0100 0101 /** 0102 * Extracts the data returned from the timeserver 0103 * 0104 * @param string $result Data to extract 0105 * @return integer 0106 */ 0107 protected function _extract($result) 0108 { 0109 $dec = hexdec('7fffffff'); 0110 $time = abs(($dec - hexdec(bin2hex($result))) - $dec); 0111 $time -= 2208988800; 0112 // Socket delay 0113 $time -= $this->_delay; 0114 0115 $this->_info['offset'] = $this->_delay; 0116 0117 return $time; 0118 } 0119 }