--- /dev/null
+<?php
+/**
+ * Base functionality for a Mobile Site
+ *
+ * PHP version 5
+ *
+ * @category Mobile_Toolbox
+ * @package GLM_MOBILE_TOOLBOX
+ * @author Chuck Scott <cscott@gaslightmedia.com>
+ * @copyright 2010 Gaslight Media
+ * @license http://www.gaslightmedia.com Gaslightmedia
+ * @link http://demo.gaslightmedia.com
+ */
+
+/**
+ * Mobile device detection for standard site
+ *
+ * @category Mobile_Toolbox
+ * @package GLM_MOBILE_TOOLBOX
+ * @author Chuck Scott <cscott@gaslightmedia.com>
+ * @copyright 2010 Gaslight Media
+ * @license http://www.gaslightmedia.com Gaslightmedia
+ * @link http://www.uptravel.com
+ */
+class GLM_MOBILE_DETECT
+{
+ /**
+ * $browserData
+ * @access public
+ */
+ public $browserData = false;
+ /**
+ * $haveSession
+ * Indicates if we have an active session
+ * This should be the case except for the first entry to the home page
+ * @access public
+ */
+ public $haveSession = false;
+ /**
+ * dbh
+ * Database Connection
+ * @access public
+ */
+ public $dbh = false;
+
+ // {{{ __construct()
+
+ /**
+ * Mobile Site setup function
+ *
+ * @return void
+ * @access public
+ */
+ function GLM_MOBILE_DETECT($catid)
+ {
+
+ $this->dbh = new GLM_DB();
+
+ // Setup Template and Page
+ $this->flexyOptions = array(
+ 'templateDir' => BASE_PATH.'templates',
+ 'compileDir' => BASE_PATH.'templates/compile',
+ 'forceCompile' => false,
+ 'flexyIgnore' => true,
+ 'allowPHP' => true,
+ 'debug' => false
+ );
+ $this->mTemplate = new HTML_Template_Flexy($this->flexyOptions);
+ $this->mPage = new stdClass();
+
+ $this->mPage->mobileSiteURL = MOBILE_SITE_URL;
+ if ($catid > 1) {
+ $this->mPage->mobileSiteURL = MOBILE_SITE_URL."&catid=".$catid;
+ }
+
+ }
+
+ /**
+ * Convert an array to an object
+ *
+ * This is used to convert db_auto_get_data() output to something that
+ * can be used by HTML_Template_Flexy.
+ *
+ * @param array $array of values to convert
+ *
+ * @return object containing array as sub-objects
+ */
+ function bindArrayToObject($array)
+ {
+ $return = new stdClass();
+ foreach ($array as $k => $v) {
+ if (is_array($v)) {
+ $return->$k = $this->bindArrayToObject($v);
+ } else {
+ $return->$k = $v;
+ }
+ }
+ return $return;
+ }
+
+
+ /**
+ * Mobile device detection using DeviceAtlas service.
+ *
+ * http://deviceatlas.com
+ *
+ * Requires a license per site
+ *
+ * isMobile() determines if the request is for a mobile version Web page.
+ * If so, the "IS_MOBILE" defined parameter is set to true and true is returned.
+ *
+ * @return array list of mobile attributes
+ */
+ function isMobileDeviceAtlas()
+ {
+
+ // Name of key to the DA tree data for memcache
+ $mc_key = 'DAtree';
+
+ // Time in seconds to retain data in memcache - 0 = Infinate
+ $mc_time = 0;
+
+ // File Names
+ $api_path = GLM_APP_BASE.'mobile/DeviceAtlas/Mtld/DA/Api.php';
+ $tree_file = GLM_APP_BASE.'mobile/DeviceAtlas/DeviceAtlas.tree';
+ $jsonpath = GLM_APP_BASE."mobile/DeviceAtlas/DeviceAtlas.json";
+
+ // Check if this is an Opera Mini proxy request and get actual USER_AGENT
+ $h = "HTTP_X_OPERAMINI_PHONE_UA";
+ if (array_key_exists($h, $_SERVER)) {
+ $ua = $_SERVER[$h];
+ } else {
+ $ua = $_SERVER['HTTP_USER_AGENT'];
+ }
+
+ // Check for data in memcache
+ $memcache_enabled = extension_loaded("memcache");
+ if ($memcache_enabled) {
+
+ // Connect to memcache - use @ to suppress warning if no connection
+ $memcache = new Memcache;
+ if (@$memcache->connect('localhost', 11211)) {
+
+ // Look for cached properties for this user agent using MD5 string as memcache key
+ $properties = $memcache->get(md5($ua));
+ $cached = 'properties';
+
+
+ } else { // If memcache connect
+
+ // Connection to memcache failed, so say we don't have it enabled
+ $memcache_enabled = false;
+ }
+
+ }// if memcache enabled
+
+ // If we didn't get the properties, we'll need to use the DA API
+ if(!is_array($properties)) {
+
+ // Try to get the tree file from disk
+ if ($handle = @fopen($tree_file, "r")) {
+ $t = fread($handle, filesize($tree_file));
+ $tree = unserialize($t);
+ fclose($handle);
+ }
+
+ // load the API
+ include $api_path;
+ $da = new Mobi_Mtld_DA_Api();
+
+ // If we didn't get the tree from the disk file, try to create from json
+ if (!is_array($tree)) {
+
+ // Load the current JSON file and get the tree from that
+ $tree = $da->getTreeFromFile($jsonpath);
+
+ // See if we got a valid tree
+ if (!is_array($tree)) {
+
+ // Didn't get a valid tree, so quit
+ return false;
+ }
+
+ }
+
+ // Get the properties from the tree
+ $properties = $da->getProperties($tree, $ua);
+
+ // If memcache is enabled, store those properties using MD5 of UA as key
+ if (is_array($properties) && $memcache_enabled) {
+ $memcache->set(md5($ua), $properties, false, $mc_time);
+ }
+ }
+
+ if ($memcache_enabled) {
+ $memcache->close();
+ }
+
+ // Check for exceptions - Used to deal with DeviceAtlas database errors
+ /* --- left old exception as an example
+ if ($properties['_matched'] ==
+ 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2') {
+ $properties = array( 'mobileDevice' => 0, 'GLM_Exception' => 1 );
+ }
+ */
+
+ return $properties;
+
+ }
+
+ /**
+ * Detect Mobile Browser or Mobile Site Request
+ *
+ * isMobile() determines if the request is for a mobile version Web page.
+ * If so, the "IS_MOBILE" defined parameter is set to true and true is returned.
+ *
+ * This function uses the DeviceAtlast process above to get the mobile data.
+ *
+ * IS_MOBILE is determined from...
+ * - If defined parameter MOBILE_SITE is not true then always false.
+ * - If there a request parameter called "isMobile"
+ * set to "yes". If so then true.
+ * - If there a request parameter called "isMobile"
+ * set to anything else. If so then false.
+ * - If there a session parameter ['Mobile']['isMobile']
+ * then use that (true/false).
+ * - Detect if there is a mobile browser and if so then true.
+ *
+ * @return boolean true if mobile, false if not
+ */
+ function isMobile()
+ {
+
+ // Use DeviceAtlas to get mobile device capabilities
+ $this->browserData = $this->isMobileDeviceAtlas();
+
+ // Check for browser width
+ $browserWidth = 0;
+ // Look for usableDisplayWidth first
+ if (isset($this->browserData['usableDisplayWidth'])
+ && $this->browserData['usableDisplayWidth'] > 0
+ ) {
+ $browserWidth = ($this->browserData['usableDisplayWidth']-0);
+ // If that's not available, look for displayWidth
+ } elseif (isset($this->browserData['displayWidth'])
+ && $this->browserData['displayWidth'] > 0
+ ) {
+ $browserWidth = ($this->browserData['displayWidth']-0);
+ }
+
+ // Say whether we have width
+ $this->browserData['haveWidth'] = false;
+ if ($browserWidth > 0) {
+ $this->browserData['haveWidth'] = true;
+ }
+
+ // Make browser data available to templates
+ $this->mPage->browserData = $this->bindArrayToObject($this->browserData);
+
+ // Check for mobile version request
+ // Is this site not mobile capable
+ if (!MOBILE_SITE) {
+
+ $isMobile = false;
+
+ // Is there a request to set the session isMobile flag
+ } elseif ( $_REQUEST['isMobile'] == 'yes' ) {
+
+ $isMobile = $_SESSION['Mobile']['isMobile'] = true;
+
+ // Is there a request to clear the session isMobile flag
+ } elseif ( $_REQUEST['isMobile'] == 'no' ) {
+
+ $isMobile = $_SESSION['Mobile']['isMobile'] = false;
+
+ // Otherwise - Has a mobile preference been set for this session
+ } elseif ( isset($_SESSION['Mobile'])
+ && isset($_SESSION['Mobile']['isMobile'])
+ ) {
+
+ $isMobile = $_SESSION['Mobile']['isMobile'];
+
+ // Otherwise - Check if using a mobile browser
+ } elseif ($this->browserData['mobileDevice']) {
+ $isMobile = true;
+
+ // If it's a tablet computer don't opt for mobile
+ if ($this->browserData['isTablet']) {
+ $isMobile = false;
+ }
+
+ // If the display is wider than a set width, don't opt for mobile
+ if ($browserWidth >= MOBILE_MAX_WIDTH) {
+ $isMobile = false;
+ }
+
+ }
+
+ define('IS_MOBILE', $isMobile);
+ return $isMobile;
+ }
+
+
+}
+
+define ('MOBILE_MAX_WIDTH', $siteConfig->mobile_max_width);
+
+$gm = new GLM_MOBILE_DETECT($catid);
+
+if ($gm->isMobile()) {
+
+ // Output page according to selected template
+ $gm->mPage->stylesheet = $m_style;
+ $gm->mPage->mobileSiteURL = $siteConfig->mobile_site_url;
+ $gm->mTemplate->compile('mobile_site.html');
+ $gm->mTemplate->outputObject($gm->mPage);
+
+ exit;
+
+} else {
+ // Return back to normal page processing.
+}
+