PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/TimeSync/Sntp.php

https://github.com/jpratt/cal
PHP | 119 lines | 38 code | 11 blank | 70 comment | 1 complexity | 7b26ef5f694aae110688f9005d43219d MD5 | raw file
  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-2008 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 10835 2008-08-08 22:28:26Z thomas $
  20. */
  21. /**
  22. * Zend_TimeSync_Protocol
  23. */
  24. #require_once 'Zend/TimeSync/Protocol.php';
  25. /**
  26. * SNTP Protocol handling class
  27. *
  28. * @category Zend
  29. * @package Zend_TimeSync
  30. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_TimeSync_Sntp extends Zend_TimeSync_Protocol
  34. {
  35. /**
  36. * Port number for this timeserver
  37. *
  38. * @var integer
  39. */
  40. protected $_port = 37;
  41. /**
  42. * Socket delay
  43. *
  44. * @var integer
  45. */
  46. private $_delay;
  47. /**
  48. * Class constructor, sets the timeserver and port number
  49. *
  50. * @param string $timeserver Timeserver to connect to
  51. * @param integer $port Port of the timeserver when it differs from the default port
  52. */
  53. public function __construct($timeserver, $port)
  54. {
  55. $this->_timeserver = 'udp://' . $timeserver;
  56. if (is_null($port) === false) {
  57. $this->_port = $port;
  58. }
  59. }
  60. /**
  61. * Prepares the data that will be send to the timeserver
  62. *
  63. * @return array
  64. */
  65. protected function _prepare()
  66. {
  67. return "\n";
  68. }
  69. /**
  70. * Reads the data returned from the timeserver
  71. *
  72. * @return string
  73. */
  74. protected function _read()
  75. {
  76. $result = fread($this->_socket, 49);
  77. $this->_delay = (($this->_delay - time()) / 2);
  78. return $result;
  79. }
  80. /**
  81. * Writes data to to the timeserver
  82. *
  83. * @param string $data Data to write to the timeserver
  84. * @return void
  85. */
  86. protected function _write($data)
  87. {
  88. $this->_connect();
  89. $this->_delay = time();
  90. fputs($this->_socket, $data);
  91. }
  92. /**
  93. * Extracts the data returned from the timeserver
  94. *
  95. * @param string $result Data to extract
  96. * @return integer
  97. */
  98. protected function _extract($result)
  99. {
  100. $dec = hexdec('7fffffff');
  101. $time = abs(($dec - hexdec(bin2hex($result))) - $dec);
  102. $time -= 2208988800;
  103. // Socket delay
  104. $time -= $this->_delay;
  105. $this->_info['offset'] = $this->_delay;
  106. return $time;
  107. }
  108. }