Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | namespace RobThree\Auth\Providers\Time; |
| 4 | |
| 5 | /** |
| 6 | * Takes the time from any NTP server |
| 7 | */ |
| 8 | class NTPTimeProvider implements ITimeProvider |
| 9 | { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 10 | /** @var string */ |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 11 | public $host; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 12 | |
| 13 | /** @var int */ |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 14 | public $port; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 15 | |
| 16 | /** @var int */ |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 17 | public $timeout; |
| 18 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 19 | /** |
| 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 Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 25 | { |
| 26 | $this->host = $host; |
| 27 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 28 | if (!is_int($port) || $port <= 0 || $port > 65535) { |
| 29 | throw new TimeException('Port must be 0 < port < 65535'); |
| 30 | } |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 31 | $this->port = $port; |
| 32 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 33 | if (!is_int($timeout) || $timeout < 0) { |
| 34 | throw new TimeException('Timeout must be >= 0'); |
| 35 | } |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 36 | $this->timeout = $timeout; |
| 37 | } |
| 38 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 39 | /** |
| 40 | * {@inheritdoc} |
| 41 | */ |
| 42 | public function getTime() |
| 43 | { |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 44 | 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 Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 55 | if (socket_recv($sock, $recv, 48, MSG_WAITALL) === false) { |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 56 | throw new \Exception(socket_strerror(socket_last_error($sock))); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 57 | } |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 58 | socket_close($sock); |
| 59 | |
| 60 | /* Interpret response */ |
| 61 | $data = unpack('N12', $recv); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 62 | $timestamp = (int) sprintf('%u', $data[9]); |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 63 | |
| 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 Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 66 | } catch (\Exception $ex) { |
| 67 | throw new TimeException(sprintf('Unable to retrieve time from %s (%s)', $this->host, $ex->getMessage())); |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | } |