/** * 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); } }
/** * Return whether the user is requesting the mobile site - either by query string * or by saved cookie. Falls back to browser detection for first time visitors * * @return boolean */ public function requestedMobileSite() { $config = SiteConfig::current_site_config(); if ($config->MobileSiteType == 'Disabled') { return false; } $request = $this->owner->getRequest(); $fullSite = $request->getVar('fullSite'); if (is_numeric($fullSite)) { return $fullSite == 0; } $fullSiteCookie = Cookie::get('fullSite'); if (is_numeric($fullSiteCookie)) { return $fullSiteCookie == 0; } return MobileBrowserDetector::is_mobile(); }