Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UserAgent.php
Go to the documentation of this file.
1 <?php
37 class Zend_Http_UserAgent implements Serializable
38 {
42  const DEFAULT_IDENTIFICATION_SEQUENCE = 'mobile,desktop';
43 
48 
52  const DEFAULT_BROWSER_TYPE = 'desktop';
53 
57  const DEFAULT_HTTP_USER_AGENT = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)';
58 
62  const DEFAULT_HTTP_ACCEPT = "application/xhtml+xml";
63 
67  const DEFAULT_MARKUP_LANGUAGE = "xhtml";
68 
74  protected $_browserType;
75 
83  protected $_browserTypeClass = array();
84 
93  protected $_config = array(
94  'identification_sequence' => self::DEFAULT_IDENTIFICATION_SEQUENCE,
95  'storage' => array(
96  'adapter' => self::DEFAULT_PERSISTENT_STORAGE_ADAPTER,
97  ),
98  );
99 
105  protected $_device;
106 
119  protected $_immutable = false;
120 
125  protected $_loaders = array();
126 
131  protected $_loaderTypes = array('storage', 'device');
132 
138  protected $_matchLog = array();
139 
145  protected $_server;
146 
152  protected $_storage;
153 
160  public function __construct($options = null)
161  {
162  if (null !== $options) {
163  $this->setOptions($options);
164  }
165  }
166 
172  public function serialize()
173  {
174  $device = $this->getDevice();
175  $spec = array(
176  'browser_type' => $this->_browserType,
177  'config' => $this->_config,
178  'device_class' => get_class($device),
179  'device' => $device->serialize(),
180  'user_agent' => $this->getServerValue('http_user_agent'),
181  'http_accept' => $this->getServerValue('http_accept'),
182  );
183  return serialize($spec);
184  }
185 
192  public function unserialize($serialized)
193  {
194  $spec = unserialize($serialized);
195 
196  $this->setOptions($spec);
197 
198  // Determine device class and ensure the class is loaded
199  $deviceClass = $spec['device_class'];
200  if (!class_exists($deviceClass)) {
201  $this->_getUserAgentDevice($this->getBrowserType());
202  }
203 
204  // Get device specification and instantiate
205  $deviceSpec = unserialize($spec['device']);
206  $deviceSpec['_config'] = $this->getConfig();
207  $deviceSpec['_server'] = $this->getServer();
208  $this->_device = new $deviceClass($deviceSpec);
209  }
210 
217  public function setOptions($options)
218  {
219  if ($options instanceof Zend_Config) {
220  $options = $options->toArray();
221  }
222 
223  if (!is_array($options)
224  && !$options instanceof ArrayAccess
225  && !$options instanceof Traversable
226  ) {
227  #require_once 'Zend/Http/UserAgent/Exception.php';
228  throw new Zend_Http_UserAgent_Exception(sprintf(
229  'Invalid argument; expected array, Zend_Config object, or object implementing ArrayAccess and Traversable; received %s',
230  (is_object($options) ? get_class($options) : gettype($options))
231  ));
232  }
233 
234  // Set $_SERVER first
235  if (isset($options['server'])) {
236  $this->setServer($options['server']);
237  unset($options['server']);
238  }
239 
240  // Get plugin loaders sorted
241  if (isset($options['plugin_loader'])) {
242  $plConfig = $options['plugin_loader'];
243  if (is_array($plConfig) || $plConfig instanceof Traversable) {
244  foreach ($plConfig as $type => $class) {
245  $this->setPluginLoader($type, $class);
246  }
247  }
248  unset($plConfig, $options['plugin_loader']);
249  }
250 
251  // And then loop through the remaining options
252  $config = array();
253  foreach ($options as $key => $value) {
254  switch (strtolower($key)) {
255  case 'browser_type':
256  $this->setBrowserType($value);
257  break;
258  case 'http_accept':
259  $this->setHttpAccept($value);
260  break;
261  case 'user_agent':
262  $this->setUserAgent($value);
263  break;
264  default:
265  // Cache remaining options for $_config
266  $config[$key] = $value;
267  break;
268  }
269  }
270  $this->setConfig($config);
271 
272  return $this;
273  }
274 
284  protected function _match($deviceClass)
285  {
286  // Validate device class
287  $r = new ReflectionClass($deviceClass);
288  if (!$r->implementsInterface('Zend_Http_UserAgent_Device')) {
289  throw new Zend_Http_UserAgent_Exception(sprintf(
290  'Invalid device class provided ("%s"); must implement Zend_Http_UserAgent_Device',
291  $deviceClass
292  ));
293  }
294 
295  $userAgent = $this->getUserAgent();
296 
297  // Call match method on device class
298  return call_user_func(
299  array($deviceClass, 'match'),
300  $userAgent,
301  $this->getServer()
302  );
303  }
304 
312  protected function _getUserAgentDevice($browserType)
313  {
314  $browserType = strtolower($browserType);
315  if (isset($this->_browserTypeClass[$browserType])) {
316  return $this->_browserTypeClass[$browserType];
317  }
318 
319  if (isset($this->_config[$browserType])
320  && isset($this->_config[$browserType]['device'])
321  ) {
322  $deviceConfig = $this->_config[$browserType]['device'];
323  if (is_array($deviceConfig) && isset($deviceConfig['classname'])) {
324  $device = (string) $deviceConfig['classname'];
325  if (!class_exists($device)) {
326  #require_once 'Zend/Http/UserAgent/Exception.php';
327  throw new Zend_Http_UserAgent_Exception(sprintf(
328  'Invalid classname "%s" provided in device configuration for browser type "%s"',
329  $device,
330  $browserType
331  ));
332  }
333  } elseif (is_array($deviceConfig) && isset($deviceConfig['path'])) {
334  $loader = $this->getPluginLoader('device');
335  $path = $deviceConfig['path'];
336  $prefix = isset($deviceConfig['prefix']) ? $deviceConfig['prefix'] : 'Zend_Http_UserAgent';
337  $loader->addPrefixPath($prefix, $path);
338 
339  $device = $loader->load($browserType);
340  } else {
341  $loader = $this->getPluginLoader('device');
342  $device = $loader->load($browserType);
343  }
344  } else {
345  $loader = $this->getPluginLoader('device');
346  $device = $loader->load($browserType);
347  }
348 
349  $this->_browserTypeClass[$browserType] = $device;
350 
351  return $device;
352  }
353 
362  public function getUserAgent()
363  {
364  if (null === ($ua = $this->getServerValue('http_user_agent'))) {
366  $this->setUserAgent($ua);
367  }
368 
369  return $ua;
370  }
371 
378  public function setUserAgent($userAgent)
379  {
380  $this->setServerValue('http_user_agent', $userAgent);
381  return $this;
382  }
383 
390  public function getHttpAccept($httpAccept = null)
391  {
392  if (null === ($accept = $this->getServerValue('http_accept'))) {
393  $accept = self::DEFAULT_HTTP_ACCEPT;
394  $this->setHttpAccept($accept);
395  }
396  return $accept;
397  }
398 
405  public function setHttpAccept($httpAccept)
406  {
407  $this->setServerValue('http_accept', $httpAccept);
408  return $this;
409  }
410 
422  public function getStorage($browser = null)
423  {
424  if (null === $browser) {
425  $browser = $this->getUserAgent();
426  }
427  if (null === $this->_storage) {
428  $config = $this->_config['storage'];
429  $adapter = $config['adapter'];
430  if (!class_exists($adapter)) {
431  $loader = $this->getPluginLoader('storage');
432  $adapter = $loader->load($adapter);
433  $loader = $this->getPluginLoader('storage');
434  }
435  $options = array('browser_type' => $browser);
436  if (isset($config['options'])) {
437  $options = array_merge($options, $config['options']);
438  }
439  $this->setStorage(new $adapter($options));
440  }
441  return $this->_storage;
442  }
443 
450  public function setStorage(Zend_Http_UserAgent_Storage $storage)
451  {
452  if ($this->_immutable) {
453  #require_once 'Zend/Http/UserAgent/Exception.php';
455  'The User-Agent device object has already been retrieved; the storage object is now immutable'
456  );
457  }
458 
459  $this->_storage = $storage;
460  return $this;
461  }
462 
469  public function clearStorage($browser = null)
470  {
471  $this->getStorage($browser)->clear();
472  }
473 
479  public function getConfig()
480  {
481  return $this->_config;
482  }
483 
503  public function setConfig($config = array())
504  {
505  if ($config instanceof Zend_Config) {
506  $config = $config->toArray();
507  }
508 
509  // Verify that Config parameters are in an array.
510  if (!is_array($config) && !$config instanceof Traversable) {
511  #require_once 'Zend/Http/UserAgent/Exception.php';
512  throw new Zend_Http_UserAgent_Exception(sprintf(
513  'Config parameters must be in an array or a Traversable object; received "%s"',
514  (is_object($config) ? get_class($config) : gettype($config))
515  ));
516  }
517 
518  if ($config instanceof Traversable) {
519  $tmp = array();
520  foreach ($config as $key => $value) {
521  $tmp[$key] = $value;
522  }
523  $config = $tmp;
524  unset($tmp);
525  }
526 
527  $this->_config = array_merge($this->_config, $config);
528  return $this;
529  }
530 
539  public function getDevice()
540  {
541  if (null !== $this->_device) {
542  return $this->_device;
543  }
544 
545  $userAgent = $this->getUserAgent();
546 
547  // search an existing identification in the session
548  $storage = $this->getStorage($userAgent);
549 
550  if (!$storage->isEmpty()) {
551  // If the user agent and features are already existing, the
552  // Zend_Http_UserAgent object is serialized in the session
553  $object = $storage->read();
554  $this->unserialize($object);
555  } else {
556  // Otherwise, the identification is made and stored in the session.
557  // Find the browser type:
558  $this->setBrowserType($this->_matchUserAgent());
559  $this->_createDevice();
560 
561  // put the result in storage:
562  $this->getStorage($userAgent)
563  ->write($this->serialize());
564  }
565 
566  // Mark the object as immutable
567  $this->_immutable = true;
568 
569  // Return the device instance
570  return $this->_device;
571  }
572 
578  public function getBrowserType()
579  {
580  return $this->_browserType;
581  }
582 
589  public function setBrowserType($browserType)
590  {
591  if ($this->_immutable) {
592  #require_once 'Zend/Http/UserAgent/Exception.php';
594  'The User-Agent device object has already been retrieved; the browser type is now immutable'
595  );
596  }
597 
598  $this->_browserType = $browserType;
599  return $this;
600  }
601 
613  public function getServer()
614  {
615  if (null === $this->_server) {
616  $this->setServer($_SERVER);
617  }
618  return $this->_server;
619  }
620 
631  public function setServer($server)
632  {
633  if ($this->_immutable) {
634  #require_once 'Zend/Http/UserAgent/Exception.php';
636  'The User-Agent device object has already been retrieved; the server array is now immutable'
637  );
638  }
639 
640  if (!is_array($server) && !$server instanceof Traversable) {
641  #require_once 'Zend/Http/UserAgent/Exception.php';
642  throw new Zend_Http_UserAgent_Exception(sprintf(
643  'Expected an array or object implementing Traversable; received %s',
644  (is_object($server) ? get_class($server) : gettype($server))
645  ));
646  }
647 
648  // Get an array if we don't have one
649  if ($server instanceof ArrayObject) {
650  $server = $server->getArrayCopy();
651  } elseif ($server instanceof Traversable) {
652  $tmp = array();
653  foreach ($server as $key => $value) {
654  $tmp[$key] = $value;
655  }
656  $server = $tmp;
657  unset($tmp);
658  }
659 
660  // Normalize key case
661  $server = array_change_key_case($server, CASE_LOWER);
662 
663  $this->_server = $server;
664  return $this;
665  }
666 
673  public function getServerValue($key)
674  {
675  $key = strtolower($key);
676  $server = $this->getServer();
677  $return = null;
678  if (isset($server[$key])) {
679  $return = $server[$key];
680  }
681  unset($server);
682  return $return;
683  }
684 
692  public function setServerValue($key, $value)
693  {
694  if ($this->_immutable) {
695  #require_once 'Zend/Http/UserAgent/Exception.php';
697  'The User-Agent device object has already been retrieved; the server array is now immutable'
698  );
699  }
700 
701  $server = $this->getServer(); // ensure it's been initialized
702  $key = strtolower($key);
703  $this->_server[$key] = $value;
704  return $this;
705  }
706 
714  public function setPluginLoader($type, $loader)
715  {
716  $type = $this->_validateLoaderType($type);
717 
718  if (is_string($loader)) {
719  if (!class_exists($loader)) {
720  #require_once 'Zend/Loader.php';
722  }
723  $loader = new $loader();
724  } elseif (!is_object($loader)) {
725  #require_once 'Zend/Http/UserAgent/Exception.php';
726  throw new Zend_Http_UserAgent_Exception(sprintf(
727  'Expected a plugin loader class or object; received %s',
728  gettype($loader)
729  ));
730  }
731  if (!$loader instanceof Zend_Loader_PluginLoader) {
732  #require_once 'Zend/Http/UserAgent/Exception.php';
733  throw new Zend_Http_UserAgent_Exception(sprintf(
734  'Expected an object extending Zend_Loader_PluginLoader; received %s',
735  get_class($loader)
736  ));
737  }
738 
739  $basePrefix = 'Zend_Http_UserAgent_';
740  $basePath = 'Zend/Http/UserAgent/';
741  switch ($type) {
742  case 'storage':
743  $prefix = $basePrefix . 'Storage';
744  $path = $basePath . 'Storage';
745  break;
746  case 'device':
747  $prefix = $basePrefix;
748  $path = $basePath;
749  break;
750  }
751  $loader->addPrefixPath($prefix, $path);
752  $this->_loaders[$type] = $loader;
753  return $this;
754  }
755 
762  public function getPluginLoader($type)
763  {
764  $type = $this->_validateLoaderType($type);
765  if (!isset($this->_loaders[$type])) {
766  #require_once 'Zend/Loader/PluginLoader.php';
767  $this->setPluginLoader($type, new Zend_Loader_PluginLoader());
768  }
769  return $this->_loaders[$type];
770  }
771 
782  protected function _validateLoaderType($type)
783  {
784  $type = strtolower($type);
785  if (!in_array($type, $this->_loaderTypes)) {
786  $types = implode(', ', $this->_loaderTypes);
787 
788  #require_once 'Zend/Http/UserAgent/Exception.php';
789  throw new Zend_Http_UserAgent_Exception(sprintf(
790  'Expected one of "%s" for plugin loader type; received "%s"',
791  $types,
792  (string) $type
793  ));
794  }
795  return $type;
796  }
797 
804  protected function _matchUserAgent()
805  {
807 
808  // If we have no identification sequence, just return the default type
809  if (empty($this->_config['identification_sequence'])) {
810  return $type;
811  }
812 
813  // Get sequence against which to match
814  $sequence = explode(',', $this->_config['identification_sequence']);
815 
816  // If a browser type is already configured, push that to the front of the list
817  if (null !== ($browserType = $this->getBrowserType())) {
818  array_unshift($sequence, $browserType);
819  }
820 
821  // Append the default browser type to the list if not alread in the list
822  if (!in_array($type, $sequence)) {
823  $sequence[] = $type;
824  }
825 
826  // Test each type until we find a match
827  foreach ($sequence as $browserType) {
828  $browserType = trim($browserType);
829  $className = $this->_getUserAgentDevice($browserType);
830 
831  // Attempt to match this device class
832  if ($this->_match($className)) {
833  $type = $browserType;
834  $this->_browserTypeClass[$type] = $className;
835  break;
836  }
837  }
838 
839  return $type;
840  }
841 
847  protected function _createDevice()
848  {
849  $browserType = $this->getBrowserType();
850  $classname = $this->_getUserAgentDevice($browserType);
851  $this->_device = new $classname($this->getUserAgent(), $this->getServer(), $this->getConfig());
852  }
853 }
setConfig($config=array())
Definition: UserAgent.php:503
setHttpAccept($httpAccept)
Definition: UserAgent.php:405
_validateLoaderType($type)
Definition: UserAgent.php:782
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
static loadClass($class, $dirs=null)
Definition: Loader.php:52
_getUserAgentDevice($browserType)
Definition: UserAgent.php:312
$config
Definition: fraud_order.php:17
_match($deviceClass)
Definition: UserAgent.php:284
getStorage($browser=null)
Definition: UserAgent.php:422
clearStorage($browser=null)
Definition: UserAgent.php:469
setServerValue($key, $value)
Definition: UserAgent.php:692
$loader
Definition: autoload.php:8
setUserAgent($userAgent)
Definition: UserAgent.php:378
$adapter
Definition: webapi_user.php:16
const DEFAULT_IDENTIFICATION_SEQUENCE
Definition: UserAgent.php:42
$type
Definition: item.phtml:13
const DEFAULT_HTTP_ACCEPT
Definition: UserAgent.php:62
$prefix
Definition: name.phtml:25
setPluginLoader($type, $loader)
Definition: UserAgent.php:714
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
setBrowserType($browserType)
Definition: UserAgent.php:589
__construct($options=null)
Definition: UserAgent.php:160
const DEFAULT_MARKUP_LANGUAGE
Definition: UserAgent.php:67
setStorage(Zend_Http_UserAgent_Storage $storage)
Definition: UserAgent.php:450
const DEFAULT_BROWSER_TYPE
Definition: UserAgent.php:52
setOptions($options)
Definition: UserAgent.php:217
const DEFAULT_PERSISTENT_STORAGE_ADAPTER
Definition: UserAgent.php:47
const DEFAULT_HTTP_USER_AGENT
Definition: UserAgent.php:57
unserialize($serialized)
Definition: UserAgent.php:192
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31
getHttpAccept($httpAccept=null)
Definition: UserAgent.php:390