00001 <?php 00002 00026 require_once 'Zend/Date.php'; 00027 00034 class Zend_TimeSync implements IteratorAggregate 00035 { 00040 const DEFAULT_PROTOCOL = 'Ntp'; 00041 00047 protected $_timeservers = array(); 00048 00054 protected $_current; 00055 00061 protected $_allowedSchemes = array( 00062 'Ntp', 00063 'Sntp' 00064 ); 00065 00072 public static $options = array( 00073 'timeout' => 1 00074 ); 00075 00083 public function __construct($target = null, $alias = null) 00084 { 00085 if ($target !== null) { 00086 $this->addServer($target, $alias); 00087 } 00088 } 00089 00096 public function getIterator() 00097 { 00098 return new ArrayObject($this->_timeservers); 00099 } 00100 00131 public function addServer($target, $alias = null) 00132 { 00133 if (is_array($target)) { 00134 foreach ($target as $key => $server) { 00135 $this->_addServer($server, $key); 00136 } 00137 } else { 00138 $this->_addServer($target, $alias); 00139 } 00140 } 00141 00149 public static function setOptions(array $options) 00150 { 00151 foreach ($options as $key => $value) { 00152 Zend_TimeSync::$options[$key] = $value; 00153 } 00154 } 00155 00162 public function setServer($alias) 00163 { 00164 if (isset($this->_timeservers[$alias]) === true) { 00165 $this->_current = $this->_timeservers[$alias]; 00166 } else { 00167 require_once 'Zend/TimeSync/Exception.php'; 00168 throw new Zend_TimeSync_Exception("'$alias' does not point to valid timeserver"); 00169 } 00170 } 00171 00179 public static function getOptions($key = null) 00180 { 00181 if ($key == null) { 00182 return Zend_TimeSync::$options; 00183 } 00184 00185 if (isset(Zend_TimeSync::$options[$key]) === true) { 00186 return Zend_TimeSync::$options[$key]; 00187 } else { 00188 require_once 'Zend/TimeSync/Exception.php'; 00189 throw new Zend_TimeSync_Exception("'$key' does not point to valid option"); 00190 } 00191 } 00192 00201 public function getServer($alias = null) 00202 { 00203 if ($alias === null) { 00204 if (isset($this->_current) && $this->_current !== false) { 00205 return $this->_current; 00206 } else { 00207 require_once 'Zend/TimeSync/Exception.php'; 00208 throw new Zend_TimeSync_Exception('there is no timeserver set'); 00209 } 00210 } 00211 if (isset($this->_timeservers[$alias]) === true) { 00212 return $this->_timeservers[$alias]; 00213 } else { 00214 require_once 'Zend/TimeSync/Exception.php'; 00215 throw new Zend_TimeSync_Exception("'$alias' does not point to valid timeserver"); 00216 } 00217 } 00218 00224 public function getInfo() 00225 { 00226 return $this->getServer()->getInfo(); 00227 } 00228 00240 public function getDate($locale = null) 00241 { 00242 require_once 'Zend/TimeSync/Exception.php'; 00243 foreach ($this->_timeservers as $alias => $server) { 00244 $this->_current = $server; 00245 try { 00246 return $server->getDate($locale); 00247 } catch (Zend_TimeSync_Exception $e) { 00248 if (!isset($masterException)) { 00249 $masterException = new Zend_TimeSync_Exception('all timeservers are bogus'); 00250 } 00251 $masterException->addException($e); 00252 } 00253 } 00254 00255 throw $masterException; 00256 } 00257 00264 protected function _addServer($target, $alias) 00265 { 00266 if ($pos = strpos($target, '://')) { 00267 $protocol = substr($target, 0, $pos); 00268 $adress = substr($target, $pos + 3); 00269 } else { 00270 $adress = $target; 00271 $protocol = self::DEFAULT_PROTOCOL; 00272 } 00273 00274 if ($pos = strrpos($adress, ':')) { 00275 $posbr = strpos($adress, ']'); 00276 if ($posbr and ($pos > $posbr)) { 00277 $port = substr($adress, $pos + 1); 00278 $adress = substr($adress, 0, $pos); 00279 } else if (!$posbr and $pos) { 00280 $port = substr($adress, $pos + 1); 00281 $adress = substr($adress, 0, $pos); 00282 } else { 00283 $port = null; 00284 } 00285 } else { 00286 $port = null; 00287 } 00288 00289 $protocol = ucfirst(strtolower($protocol)); 00290 if (!in_array($protocol, $this->_allowedSchemes)) { 00291 require_once 'Zend/TimeSync/Exception.php'; 00292 throw new Zend_TimeSync_Exception("'$protocol' is not a supported protocol"); 00293 } 00294 00295 $className = 'Zend_TimeSync_' . $protocol; 00296 if (!class_exists($className)) { 00297 require_once 'Zend/Loader.php'; 00298 Zend_Loader::loadClass($className); 00299 } 00300 $timeServerObj = new $className($adress, $port); 00301 00302 $this->_timeservers[$alias] = $timeServerObj; 00303 } 00304 }