mirror of
https://codeberg.org/icewind/SMB.git
synced 2026-06-03 17:24:07 +02:00
improve timezone detection
This commit is contained in:
parent
20da159fe4
commit
5d1e55f67f
4 changed files with 99 additions and 4 deletions
|
|
@ -55,6 +55,13 @@ interface ISystem {
|
|||
*/
|
||||
public function getStdBufPath();
|
||||
|
||||
/**
|
||||
* Get the full path to the `date` binary of false if the binary is not available
|
||||
*
|
||||
* @return string|bool
|
||||
*/
|
||||
public function getDatePath();
|
||||
|
||||
/**
|
||||
* Whether or not the smbclient php extension is enabled
|
||||
*
|
||||
|
|
|
|||
|
|
@ -45,6 +45,10 @@ class System implements ISystem {
|
|||
return $this->getBinaryPath('stdbuf');
|
||||
}
|
||||
|
||||
public function getDatePath() {
|
||||
return $this->getBinaryPath('date');
|
||||
}
|
||||
|
||||
public function libSmbclientAvailable() {
|
||||
return function_exists('smbclient_state_new');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ class TimeZoneProvider implements ITimeZoneProvider {
|
|||
|
||||
public function get($host) {
|
||||
if (!isset($this->timeZones[$host])) {
|
||||
$timeZone = null;
|
||||
$net = $this->system->getNetPath();
|
||||
// for local domain names we can assume same timezone
|
||||
if ($net && $host && strpos($host, '.') !== false) {
|
||||
|
|
@ -36,13 +37,17 @@ class TimeZoneProvider implements ITimeZoneProvider {
|
|||
escapeshellarg($host)
|
||||
);
|
||||
$timeZone = exec($command);
|
||||
}
|
||||
|
||||
if (!$timeZone) {
|
||||
$date = $this->system->getDatePath();
|
||||
if ($date) {
|
||||
$timeZone = exec($date . " +%z");
|
||||
} else {
|
||||
$timeZone = date_default_timezone_get();
|
||||
}
|
||||
$this->timeZones[$host] = $timeZone;
|
||||
} else { // fallback to server timezone
|
||||
$this->timeZones[$host] = date_default_timezone_get();
|
||||
}
|
||||
$this->timeZones[$host] = $timeZone;
|
||||
}
|
||||
return $this->timeZones[$host];
|
||||
}
|
||||
|
|
|
|||
79
tests/TimeZoneProviderTest.php
Normal file
79
tests/TimeZoneProviderTest.php
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
<?php declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2019 Robin Appelman <robin@icewind.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Icewind\SMB\Test;
|
||||
|
||||
use Icewind\SMB\ISystem;
|
||||
use Icewind\SMB\TimeZoneProvider;
|
||||
|
||||
class TimeZoneProviderTest extends TestCase {
|
||||
/** @var ISystem|\PHPUnit_Framework_MockObject_MockObject */
|
||||
private $system;
|
||||
/** @var TimeZoneProvider */
|
||||
private $provider;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->system = $this->createMock(ISystem::class);
|
||||
$this->provider = new TimeZoneProvider($this->system);
|
||||
}
|
||||
|
||||
private function getDummyCommand($output) {
|
||||
return "echo '$output' || false";
|
||||
}
|
||||
|
||||
public function testFQDN() {
|
||||
$this->system->method('getNetPath')
|
||||
->willReturn($this->getDummyCommand("+800"));
|
||||
$this->system->method('getDatePath')
|
||||
->willReturn($this->getDummyCommand("+700"));
|
||||
|
||||
$this->assertEquals('+800', $this->provider->get('foo.bar.com'));
|
||||
}
|
||||
|
||||
public function testLocal() {
|
||||
$this->system->method('getNetPath')
|
||||
->willReturn($this->getDummyCommand("+800"));
|
||||
$this->system->method('getDatePath')
|
||||
->willReturn($this->getDummyCommand("+700"));
|
||||
|
||||
$this->assertEquals('+700', $this->provider->get('foobar'));
|
||||
}
|
||||
|
||||
public function testFQDNNoNet() {
|
||||
$this->system->method('getNetPath')
|
||||
->willReturn(false);
|
||||
$this->system->method('getDatePath')
|
||||
->willReturn($this->getDummyCommand("+700"));
|
||||
|
||||
$this->assertEquals('+700', $this->provider->get('foo.bar.com'));
|
||||
}
|
||||
|
||||
public function testNoNetNoDate() {
|
||||
$this->system->method('getNetPath')
|
||||
->willReturn(false);
|
||||
$this->system->method('getDatePath')
|
||||
->willReturn(false);
|
||||
|
||||
$this->assertEquals(date_default_timezone_get(), $this->provider->get('foo.bar.com'));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue