/**
  * Override the default behavior to ensure that if this is a mobile device
  * or if they are on the configured mobile domain then they receive the mobile site.
  */
 public function onAfterInit()
 {
     self::$is_mobile = false;
     $config = SiteConfig::current_site_config();
     $request = $this->owner->getRequest();
     // If we've accessed the homepage as /home/, then we redirect to / and don't want to double redirect here
     if ($this->owner->redirectedTo()) {
         return;
     }
     // Enforce the site (cookie expires in 1 day)
     $fullSite = $request->getVar('fullSite');
     if (is_numeric($fullSite)) {
         $fullSiteCookie = (int) $fullSite;
         Cookie::set('fullSite', $fullSiteCookie);
         // use the host of the desktop version of the site to set cross-(sub)domain cookie
         $domain = $config->FullSiteDomainNormalized;
         if (!empty($domain)) {
             Cookie::set('fullSite', $fullSite, self::$cookie_expire_time, null, '.' . parse_url($domain, PHP_URL_HOST));
         } else {
             // otherwise just use a normal cookie with the default domain
             Cookie::set('fullSite', $fullSite, self::$cookie_expire_time);
         }
     } else {
         $fullSiteCookie = Cookie::get('fullSite');
     }
     if (is_numeric($fullSiteCookie)) {
         // Full site requested
         if ($fullSiteCookie) {
             if ($this->onMobileDomain() && $config->MobileSiteType == 'RedirectToDomain') {
                 return $this->owner->redirect($config->FullSiteDomainNormalized, 301);
             }
             return;
         } else {
             if (!$this->onMobileDomain() && $config->MobileSiteType == 'RedirectToDomain') {
                 return $this->owner->redirect($config->MobileDomainNormalized, 301);
             }
             Config::inst()->update('SSViewer', 'theme', $config->MobileTheme);
             self::$is_mobile = true;
             return;
         }
     }
     // If the user requested the mobile domain, set the right theme
     if ($this->onMobileDomain()) {
         Config::inst()->update('SSViewer', 'theme', $config->MobileTheme);
         self::$is_mobile = true;
     }
     // User just wants to see a theme, but no redirect occurs
     if (MobileBrowserDetector::is_mobile() && $config->MobileSiteType == 'MobileThemeOnly') {
         Config::inst()->update('SSViewer', 'theme', $config->MobileTheme);
         self::$is_mobile = true;
     }
     // If on a mobile device, but not on the mobile domain and has been setup for redirection
     if (!$this->onMobileDomain() && MobileBrowserDetector::is_mobile() && $config->MobileSiteType == 'RedirectToDomain') {
         return $this->owner->redirect($config->MobileDomainNormalized, 301);
     }
 }
 public function testIsMobile()
 {
     $page = $this->objFromFixture('Page', 'page');
     $config = SiteConfig::current_site_config();
     // Immobile device
     $_SERVER['HTTP_USER_AGENT'] = 'immobile browser';
     $response = $this->get($page->URLSegment);
     $this->assertFalse(MobileSiteControllerExtension::is_mobile());
     // Force mobile site
     $_SERVER['HTTP_USER_AGENT'] = 'immobile browser';
     $response = $this->get($page->URLSegment . '?fullSite=0');
     $this->assertTrue(MobileSiteControllerExtension::is_mobile());
     $config->MobileSiteType = 'MobileThemeOnly';
     $config->write();
     // Mobile device
     $_SERVER['HTTP_USER_AGENT'] = 'iPhone';
     $response = $this->get($page->URLSegment);
     $this->assertTrue(MobileSiteControllerExtension::is_mobile());
     // Force full site
     $_SERVER['HTTP_USER_AGENT'] = 'iPhone';
     $response = $this->get($page->URLSegment . '?fullSite=1');
     $this->assertFalse(MobileSiteControllerExtension::is_mobile());
     $config->MobileSiteType = 'RedirectToDomain';
     $config->write();
     // Mobile domain
     $_SERVER['HTTP_USER_AGENT'] = 'anything can be here';
     $_SERVER['HTTP_HOST'] = 'm.' . $_SERVER['HTTP_HOST'];
     $page = $this->objFromFixture('Page', 'page');
     $response = $this->get($page->URLSegment);
     $this->assertTrue(MobileSiteControllerExtension::is_mobile());
 }
 /**
  * Override the default behavior to ensure that if this is a mobile device
  * or if they are on the configured mobile domain then they receive the mobile site.
  */
 public function onAfterInit()
 {
     self::$is_mobile = false;
     $config = SiteConfig::current_site_config();
     $request = $this->owner->getRequest();
     // If we've accessed the homepage as /home/, then we redirect to / and don't want to double redirect here
     if ($this->owner->redirectedTo()) {
         return;
     }
     // Enforce the site (cookie expires in 30 minutes)
     $fullSite = $request->getVar('fullSite');
     if (is_numeric($fullSite)) {
         Cookie::set('fullSite', (int) $fullSite);
         // use the host of the desktop version of the site to set cross-(sub)domain cookie
         if (!empty($config->FullSiteDomain)) {
             $parsedURL = parse_url($config->FullSiteDomain);
             if (!headers_sent($file, $line)) {
                 setcookie('fullSite', $fullSite, time() + self::$cookie_expire_time, null, '.' . $parsedURL['host']);
             } else {
                 user_error(sprintf('Cookie \'fullSite\' can\'t be set. Output started at line %s in %s', $line, $file));
             }
         } else {
             // otherwise just use a normal cookie with the default domain
             if (!headers_sent($file, $line)) {
                 setcookie('fullSite', $fullSite, time() + self::$cookie_expire_time);
             } else {
                 user_error(sprintf('Cookie \'fullSite\' can\'t be set. Output started at line %s in %s', $line, $file));
             }
         }
     }
     // Site is being forced via flag or cookie
     $fullSiteCookie = Cookie::get('fullSite');
     if (is_numeric($fullSiteCookie)) {
         // Full site requested
         if ($fullSiteCookie) {
             if ($this->onMobileDomain() && $config->MobileSiteType == 'RedirectToDomain') {
                 return $this->owner->redirect($config->FullSiteDomain, 301);
             }
             return;
         } else {
             if (!$this->onMobileDomain() && $config->MobileSiteType == 'RedirectToDomain') {
                 return $this->owner->redirect($config->MobileDomain, 301);
             }
             SSViewer::set_theme($config->MobileTheme);
             self::$is_mobile = true;
             return;
         }
     }
     // If the user requested the mobile domain, set the right theme
     if ($this->onMobileDomain()) {
         SSViewer::set_theme($config->MobileTheme);
         self::$is_mobile = true;
     }
     // User just wants to see a theme, but no redirect occurs
     if (MobileBrowserDetector::is_mobile() && $config->MobileSiteType == 'MobileThemeOnly') {
         SSViewer::set_theme($config->MobileTheme);
         self::$is_mobile = true;
     }
     // If on a mobile device, but not on the mobile domain and has been setup for redirection
     if (!$this->onMobileDomain() && MobileBrowserDetector::is_mobile() && $config->MobileSiteType == 'RedirectToDomain') {
         return $this->owner->redirect($config->MobileDomain, 301);
     }
 }