blob: a701850bb5c853acf1da35c39ce2b13fce4e3a0f [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3namespace RobThree\Auth\Providers\Time;
4
5/**
6 * Takes the time from any NTP server
7 */
8class NTPTimeProvider implements ITimeProvider
9{
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020010 /** @var string */
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010011 public $host;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020012
13 /** @var int */
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010014 public $port;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020015
16 /** @var int */
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010017 public $timeout;
18
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020019 /**
20 * @param string $host
21 * @param int $port
22 * @param int $timeout
23 */
24 public function __construct($host = 'time.google.com', $port = 123, $timeout = 1)
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010025 {
26 $this->host = $host;
27
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020028 if (!is_int($port) || $port <= 0 || $port > 65535) {
29 throw new TimeException('Port must be 0 < port < 65535');
30 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010031 $this->port = $port;
32
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020033 if (!is_int($timeout) || $timeout < 0) {
34 throw new TimeException('Timeout must be >= 0');
35 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010036 $this->timeout = $timeout;
37 }
38
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020039 /**
40 * {@inheritdoc}
41 */
42 public function getTime()
43 {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010044 try {
45 /* Create a socket and connect to NTP server */
46 $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
47 socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, ['sec' => $this->timeout, 'usec' => 0]);
48 socket_connect($sock, $this->host, $this->port);
49
50 /* Send request */
51 $msg = "\010" . str_repeat("\0", 47);
52 socket_send($sock, $msg, strlen($msg), 0);
53
54 /* Receive response and close socket */
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020055 if (socket_recv($sock, $recv, 48, MSG_WAITALL) === false) {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010056 throw new \Exception(socket_strerror(socket_last_error($sock)));
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020057 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010058 socket_close($sock);
59
60 /* Interpret response */
61 $data = unpack('N12', $recv);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020062 $timestamp = (int) sprintf('%u', $data[9]);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010063
64 /* NTP is number of seconds since 0000 UT on 1 January 1900 Unix time is seconds since 0000 UT on 1 January 1970 */
65 return $timestamp - 2208988800;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020066 } catch (\Exception $ex) {
67 throw new TimeException(sprintf('Unable to retrieve time from %s (%s)', $this->host, $ex->getMessage()));
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010068 }
69 }
70}