Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TimeSync.php
Go to the documentation of this file.
1 <?php
2 
26 #require_once 'Zend/Date.php';
27 
34 class Zend_TimeSync implements IteratorAggregate
35 {
40  const DEFAULT_PROTOCOL = 'Ntp';
41 
47  protected $_timeservers = array();
48 
54  protected $_current;
55 
61  protected $_allowedSchemes = array(
62  'Ntp',
63  'Sntp'
64  );
65 
72  public static $options = array(
73  'timeout' => 1
74  );
75 
83  public function __construct($target = null, $alias = null)
84  {
85  if ($target !== null) {
86  $this->addServer($target, $alias);
87  }
88  }
89 
96  public function getIterator()
97  {
98  return new ArrayObject($this->_timeservers);
99  }
100 
131  public function addServer($target, $alias = null)
132  {
133  if (is_array($target)) {
134  foreach ($target as $key => $server) {
135  $this->_addServer($server, $key);
136  }
137  } else {
138  $this->_addServer($target, $alias);
139  }
140  }
141 
149  public static function setOptions(array $options)
150  {
151  foreach ($options as $key => $value) {
153  }
154  }
155 
162  public function setServer($alias)
163  {
164  if (isset($this->_timeservers[$alias]) === true) {
165  $this->_current = $this->_timeservers[$alias];
166  } else {
167  #require_once 'Zend/TimeSync/Exception.php';
168  throw new Zend_TimeSync_Exception("'$alias' does not point to valid timeserver");
169  }
170  }
171 
179  public static function getOptions($key = null)
180  {
181  if ($key == null) {
183  }
184 
185  if (isset(Zend_TimeSync::$options[$key]) === true) {
186  return Zend_TimeSync::$options[$key];
187  } else {
188  #require_once 'Zend/TimeSync/Exception.php';
189  throw new Zend_TimeSync_Exception("'$key' does not point to valid option");
190  }
191  }
192 
201  public function getServer($alias = null)
202  {
203  if ($alias === null) {
204  if (isset($this->_current) && $this->_current !== false) {
205  return $this->_current;
206  } else {
207  #require_once 'Zend/TimeSync/Exception.php';
208  throw new Zend_TimeSync_Exception('there is no timeserver set');
209  }
210  }
211  if (isset($this->_timeservers[$alias]) === true) {
212  return $this->_timeservers[$alias];
213  } else {
214  #require_once 'Zend/TimeSync/Exception.php';
215  throw new Zend_TimeSync_Exception("'$alias' does not point to valid timeserver");
216  }
217  }
218 
224  public function getInfo()
225  {
226  return $this->getServer()->getInfo();
227  }
228 
240  public function getDate($locale = null)
241  {
242  #require_once 'Zend/TimeSync/Exception.php';
243  foreach ($this->_timeservers as $alias => $server) {
244  $this->_current = $server;
245  try {
246  return $server->getDate($locale);
247  } catch (Zend_TimeSync_Exception $e) {
248  if (!isset($masterException)) {
249  $masterException = new Zend_TimeSync_Exception('all timeservers are bogus');
250  }
251  $masterException->addException($e);
252  }
253  }
254 
255  throw $masterException;
256  }
257 
264  protected function _addServer($target, $alias)
265  {
266  if ($pos = strpos($target, '://')) {
267  $protocol = substr($target, 0, $pos);
268  $adress = substr($target, $pos + 3);
269  } else {
270  $adress = $target;
271  $protocol = self::DEFAULT_PROTOCOL;
272  }
273 
274  if ($pos = strrpos($adress, ':')) {
275  $posbr = strpos($adress, ']');
276  if ($posbr and ($pos > $posbr)) {
277  $port = substr($adress, $pos + 1);
278  $adress = substr($adress, 0, $pos);
279  } else if (!$posbr and $pos) {
280  $port = substr($adress, $pos + 1);
281  $adress = substr($adress, 0, $pos);
282  } else {
283  $port = null;
284  }
285  } else {
286  $port = null;
287  }
288 
289  $protocol = ucfirst(strtolower($protocol));
290  if (!in_array($protocol, $this->_allowedSchemes)) {
291  #require_once 'Zend/TimeSync/Exception.php';
292  throw new Zend_TimeSync_Exception("'$protocol' is not a supported protocol");
293  }
294 
295  $className = 'Zend_TimeSync_' . $protocol;
296  if (!class_exists($className)) {
297  #require_once 'Zend/Loader.php';
299  }
300  $timeServerObj = new $className($adress, $port);
301 
302  $this->_timeservers[$alias] = $timeServerObj;
303  }
304 }
static setOptions(array $options)
Definition: TimeSync.php:149
static loadClass($class, $dirs=null)
Definition: Loader.php:52
const DEFAULT_PROTOCOL
Definition: TimeSync.php:40
$target
Definition: skip.phtml:8
addServer($target, $alias=null)
Definition: TimeSync.php:131
static getOptions($key=null)
Definition: TimeSync.php:179
_addServer($target, $alias)
Definition: TimeSync.php:264
$value
Definition: gender.phtml:16
__construct($target=null, $alias=null)
Definition: TimeSync.php:83
setServer($alias)
Definition: TimeSync.php:162
$pos
Definition: list.phtml:42
getDate($locale=null)
Definition: TimeSync.php:240
if(!trim($html)) $alias
Definition: details.phtml:20
static $options
Definition: TimeSync.php:72
getServer($alias=null)
Definition: TimeSync.php:201
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31