|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_TimeSync |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: Sntp.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * Zend_TimeSync_Protocol |
|
24 */ |
|
25 require_once 'Zend/TimeSync/Protocol.php'; |
|
26 |
|
27 /** |
|
28 * SNTP Protocol handling class |
|
29 * |
|
30 * @category Zend |
|
31 * @package Zend_TimeSync |
|
32 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
33 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
34 */ |
|
35 class Zend_TimeSync_Sntp extends Zend_TimeSync_Protocol |
|
36 { |
|
37 /** |
|
38 * Port number for this timeserver |
|
39 * |
|
40 * @var integer |
|
41 */ |
|
42 protected $_port = 37; |
|
43 |
|
44 /** |
|
45 * Socket delay |
|
46 * |
|
47 * @var integer |
|
48 */ |
|
49 private $_delay; |
|
50 |
|
51 /** |
|
52 * Class constructor, sets the timeserver and port number |
|
53 * |
|
54 * @param string $timeserver Timeserver to connect to |
|
55 * @param integer $port Port of the timeserver when it differs from the default port |
|
56 */ |
|
57 public function __construct($timeserver, $port) |
|
58 { |
|
59 $this->_timeserver = 'udp://' . $timeserver; |
|
60 if ($port !== null) { |
|
61 $this->_port = $port; |
|
62 } |
|
63 } |
|
64 |
|
65 /** |
|
66 * Prepares the data that will be send to the timeserver |
|
67 * |
|
68 * @return array |
|
69 */ |
|
70 protected function _prepare() |
|
71 { |
|
72 return "\n"; |
|
73 } |
|
74 |
|
75 /** |
|
76 * Reads the data returned from the timeserver |
|
77 * |
|
78 * @return string |
|
79 */ |
|
80 protected function _read() |
|
81 { |
|
82 $result = fread($this->_socket, 49); |
|
83 $this->_delay = (($this->_delay - time()) / 2); |
|
84 |
|
85 return $result; |
|
86 } |
|
87 |
|
88 /** |
|
89 * Writes data to to the timeserver |
|
90 * |
|
91 * @param string $data Data to write to the timeserver |
|
92 * @return void |
|
93 */ |
|
94 protected function _write($data) |
|
95 { |
|
96 $this->_connect(); |
|
97 $this->_delay = time(); |
|
98 fputs($this->_socket, $data); |
|
99 } |
|
100 |
|
101 /** |
|
102 * Extracts the data returned from the timeserver |
|
103 * |
|
104 * @param string $result Data to extract |
|
105 * @return integer |
|
106 */ |
|
107 protected function _extract($result) |
|
108 { |
|
109 $dec = hexdec('7fffffff'); |
|
110 $time = abs(($dec - hexdec(bin2hex($result))) - $dec); |
|
111 $time -= 2208988800; |
|
112 // Socket delay |
|
113 $time -= $this->_delay; |
|
114 |
|
115 $this->_info['offset'] = $this->_delay; |
|
116 |
|
117 return $time; |
|
118 } |
|
119 } |