MobileComponent.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. App::uses('Component', 'Controller');
  3. App::uses('Router', 'Routing');
  4. /**
  5. * Uses Session: User.mobile and User.nomobile
  6. * - mobile is the auto-detection (true/false)
  7. * - nomobile can be set by the user and overrides the default behavior/detection
  8. * (1=true/0=false or -1=null which will remove the override)
  9. *
  10. * TODO: differentaite between "isMobile" and "has/wants mobile"
  11. * @author Mark Scherer
  12. * @license MIT
  13. * 2011-12-28 ms
  14. */
  15. class MobileComponent extends Component {
  16. public $components = array('Session');
  17. public $isMobile = null;
  18. public $setMobile = null;
  19. public $Controller = null;
  20. protected $_defaults = array(
  21. 'engine' => 'cake',
  22. );
  23. public function __construct(ComponentCollection $collection, $settings = array()) {
  24. $settings = am($this->_defaults, $settings);
  25. parent::__construct($collection, $settings);
  26. }
  27. public function initialize(Controller $Controller) {
  28. parent::initialize($Controller);
  29. $this->Controller = $Controller;
  30. if (isset($this->Controller->request->params['named']['mobile'])) {
  31. if ($this->Controller->request->params['named']['mobile'] == '-1') {
  32. $noMobile = null;
  33. } else {
  34. $wantsMobile = (bool) $this->Controller->request->params['named']['mobile'];
  35. $noMobile = (int) (!$wantsMobile);
  36. }
  37. $this->Session->write('User.nomobile', $noMobile);
  38. }
  39. $this->setMobile();
  40. $urlParams = Router::getParams(true);
  41. if (!isset($urlParams['named'])) {
  42. $urlParams['named'] = array();
  43. }
  44. if (!isset($urlParams['pass'])) {
  45. $urlParams['pass'] = array();
  46. }
  47. $urlParams = am($urlParams, $urlParams['named'], $urlParams['pass']);
  48. unset($urlParams['named']);
  49. unset($urlParams['pass']);
  50. if (isset($urlParams['prefix'])) {
  51. unset($urlParams['prefix']);
  52. }
  53. if ($this->setMobile) {
  54. $url = Router::url(am($urlParams, array('mobile'=>0)));
  55. $this->Controller->set('desktopUrl', $url);
  56. } else {
  57. $url = Router::url(am($urlParams, array('mobile'=>1)));
  58. $this->Controller->set('mobileUrl', $url);
  59. }
  60. Configure::write('User.mobile', $this->isMobile);
  61. Configure::write('User.setMobile', $this->setMobile);
  62. }
  63. public function setMobile() {
  64. if ($this->isMobile === null) {
  65. $mobile = $this->isMobile();
  66. $this->isMobile = $mobile;
  67. }
  68. $noMobile = $this->Session->read('User.nomobile');
  69. if (!$this->isMobile && $noMobile === null || $noMobile) {
  70. $this->setMobile = false;
  71. return;
  72. }
  73. $this->setMobile = true;
  74. $this->Controller->viewClass = 'Theme';
  75. $this->Controller->theme = 'Mobile';
  76. //$this->Controller->layoutPath = 'mobile';
  77. }
  78. /**
  79. *
  80. * @return bool $success
  81. */
  82. public function isMobile() {
  83. $isMobile = $this->Session->read('User.mobile');
  84. if ($isMobile !== null) {
  85. return $isMobile;
  86. }
  87. if ($this->settings['engine'] !== 'cake') {
  88. throw new CakeException(__('Engine %s not available', $this->settings['engine']));
  89. //TODO
  90. }
  91. $isMobile = (int)$this->detect();
  92. $this->Session->write('User.mobile', $isMobile);
  93. return $isMobile;
  94. }
  95. public function detect() {
  96. $this->Controller->request->addDetector('mobile', array('options' => array('OMNIA7')));
  97. return $this->Controller->request->is('mobile');
  98. }
  99. public function detectByTools() {
  100. $isMobile = $this->Session->read('Session.mobile');
  101. if ($isMobile !== null) {
  102. return $isMobile;
  103. }
  104. App::uses('UserAgentLib', 'Tools.Lib');
  105. $UserAgentLib = new UserAgentLib();
  106. $mobile = (int)$UserAgentLib->isMobile();
  107. $this->Session->write('Session.mobile', $mobile);
  108. return $mobile;
  109. }
  110. public function detectByWurfl() {
  111. App::import('Vendor', 'WURFL', array('file' => 'WURFLManagerProvider.php'));
  112. $wurflConfigFile = APP . 'Config' . DS . 'wurfl ' . DS . 'config.xml';
  113. $wurflManager = WURFL_WURFLManagerProvider::getWURFLManager($wurflConfigFile);
  114. $requestingDevice = $wurflManager->getDeviceForHttpRequest($_SERVER);
  115. if ($requestingDevice->getCapability('is_wireless_device') == 'true') {
  116. return true;
  117. }
  118. return false;
  119. }
  120. }