diff --git a/simple_mobile_redirect.admin.inc b/simple_mobile_redirect.admin.inc index d12b57b..8c3f333 100644 --- a/simple_mobile_redirect.admin.inc +++ b/simple_mobile_redirect.admin.inc @@ -8,7 +8,7 @@ function simple_mobile_redirect_admin_settings_form($form, &$form_state) { $form['simple_mobile_redirect_settings'] = array( '#type' => 'fieldset', '#title' => t('Simple Mobile Redirect Settings'), - '#description' => t('Link back to /?nomobi=true to set a cookie to stay on the full version of the site.
Link to /clearsimplemobileredirect to clear the cookie and redirect to Mobile sites.'), + '#description' => t('Link back to /?nomobi=true to set a cookie to stay on the full version of the site.
Link to /clearsimplemobileredirect to clear the cookie and redirect to Mobile sites.'), ); $form['simple_mobile_redirect_settings']['simple_mobile_persistent_redirect'] = array( '#type' => 'select', @@ -19,88 +19,100 @@ function simple_mobile_redirect_admin_settings_form($form, &$form_state) { REDIRECT_EVERY_TIME => t('Every Time'), ), '#default_value' => variable_get('simple_mobile_persistent_redirect', REDIRECT_ONCE_PER_TIMEFRAME), - '#required' => FALSE, + '#required' => FALSE, '#description' => t('How often would you like to have the redirect occur? '), - ); + ); + $form['simple_mobile_redirect_settings']['simple_mobile_redirect_expire'] = array( + '#type' => 'select', + '#title' => t('Cookie Expiration'), + '#description' => t('Set a time out for cookies provided to users on the site'), + '#default_value' => variable_get('simple_mobile_redirect_expire', 2592000), + '#options' => drupal_map_assoc(array(3600, 10800, 21600, 43200, 86400, 604800, 2592000), 'format_interval'), + '#states' => array( + 'visible' => array( + ':input[name="simple_mobile_persistent_redirect"]' => array('value' => 3), + ), + ), + ); $form['simple_mobile_redirect_settings']['simple_mobile_redirect_home'] = array( '#type' => 'textfield', '#title' => t('non-mobile site'), '#default_value' => variable_get('simple_mobile_redirect_home', '/'), - '#size' => 40, - '#maxlength' => 255, - '#required' => FALSE, + '#size' => 40, + '#maxlength' => 255, + '#required' => FALSE, '#description' => t('Enter a the non-mobile site'), ); $form['simple_mobile_redirect_settings']['simple_mobile_redirect_ipad'] = array( '#type' => 'textfield', '#title' => t('Redirect iPad'), '#default_value' => variable_get('simple_mobile_redirect_ipad', ''), - '#size' => 40, - '#maxlength' => 255, - '#required' => FALSE, + '#size' => 40, + '#maxlength' => 255, + '#required' => FALSE, '#description' => t('Enter a URL for iPad sites'), ); $form['simple_mobile_redirect_settings']['simple_mobile_redirect_iphone'] = array( '#type' => 'textfield', '#title' => t('Redirect iPhone'), '#default_value' => variable_get('simple_mobile_redirect_iphone', ''), - '#size' => 40, - '#maxlength' => 255, - '#required' => FALSE, + '#size' => 40, + '#maxlength' => 255, + '#required' => FALSE, '#description' => t('Enter a URL for iPhone/iPod touch sites'), ); $form['simple_mobile_redirect_settings']['simple_mobile_redirect_android'] = array( '#type' => 'textfield', '#title' => t('Redirect Android'), '#default_value' => variable_get('simple_mobile_redirect_android', ''), - '#size' => 40, - '#maxlength' => 255, - '#required' => FALSE, + '#size' => 40, + '#maxlength' => 255, + '#required' => FALSE, '#description' => t('Enter a URL for Android sites'), ); $form['simple_mobile_redirect_settings']['simple_mobile_redirect_opera'] = array( '#type' => 'textfield', '#title' => t('Redirect Opera Mini'), '#default_value' => variable_get('simple_mobile_redirect_opera', ''), - '#size' => 40, - '#maxlength' => 255, - '#required' => FALSE, + '#size' => 40, + '#maxlength' => 255, + '#required' => FALSE, '#description' => t('Enter a URL for Opera Mini sites'), ); $form['simple_mobile_redirect_settings']['simple_mobile_redirect_blackberry'] = array( '#type' => 'textfield', '#title' => t('Redirect Blackbery'), '#default_value' => variable_get('simple_mobile_redirect_blackberry', ''), - '#size' => 40, - '#maxlength' => 255, - '#required' => FALSE, + '#size' => 40, + '#maxlength' => 255, + '#required' => FALSE, '#description' => t('Enter a URL for Blackberry sites'), ); $form['simple_mobile_redirect_settings']['simple_mobile_redirect_palm'] = array( '#type' => 'textfield', '#title' => t('Redirect Palm Web OS'), '#default_value' => variable_get('simple_mobile_redirect_palm', ''), - '#size' => 40, - '#maxlength' => 255, - '#required' => FALSE, + '#size' => 40, + '#maxlength' => 255, + '#required' => FALSE, '#description' => t('Enter a URL for Palm OS sites'), ); $form['simple_mobile_redirect_settings']['simple_mobile_redirect_windows'] = array( '#type' => 'textfield', '#title' => t('Redirect Window Mobile'), '#default_value' => variable_get('simple_mobile_redirect_windows', ''), - '#size' => 40, - '#maxlength' => 255, - '#required' => FALSE, + '#size' => 40, + '#maxlength' => 255, + '#required' => FALSE, '#description' => t('Enter a URL for Windows Mobile sites'), ); $form['simple_mobile_redirect_settings']['simple_mobile_redirect_mobileredirect'] = array( '#type' => 'textfield', '#title' => t('Mobile Web Site'), '#default_value' => variable_get('simple_mobile_redirect_mobileredirect', ''), - '#size' => 40, - '#maxlength' => 255, - '#required' => FALSE, + '#size' => 40, + '#maxlength' => 255, + '#required' => FALSE, '#description' => t('URL to redirect to if not one of the mobile browsers above'), ); return system_settings_form($form); diff --git a/simple_mobile_redirect.info b/simple_mobile_redirect.info index 4c78362..652b8a5 100644 --- a/simple_mobile_redirect.info +++ b/simple_mobile_redirect.info @@ -2,5 +2,6 @@ name = Simple Mobile Redirect description = Redirect to mobile site, optional cookie to not redirect core = 7.x package = Mobile +configure = admin/config/system/simple_mobile_redirect files[] = simple_module_redirect.test diff --git a/simple_mobile_redirect.module b/simple_mobile_redirect.module index d2a9c61..5df1d69 100644 --- a/simple_mobile_redirect.module +++ b/simple_mobile_redirect.module @@ -45,33 +45,61 @@ function simple_mobile_redirect_permission() { * Function to clear the do not redirect cookie. */ function _simple_mobile_redirect_clearcookie() { - setcookie("nomobi", "", REQUEST_TIME - 3600); - $simplemobileredirect = variable_get('simple_mobile_redirect_mobileredirect', '/'); - if ($simplemobileredirect == '/') { - $simplemobileredirect = ''; - } - drupal_goto($simplemobileredirect); + _simple_mobile_redirect_setcookie(NULL); + drupal_goto(''); } /** - * Function to set the do not redirect cookie. + * Function to set the redirect cookie. */ -function _simple_mobile_redirect_setcookie($redirect = FALSE) { +function _simple_mobile_redirect_setcookie($location, $redirect = FALSE) { global $base_url; - $simplemobilehome = variable_get('simple_mobile_redirect_home', '/'); + global $cookie_domain; + + // TODO Per session redirects. $persistence = variable_get('simple_mobile_persistent_redirect', REDIRECT_ONCE_PER_TIMEFRAME); - $expire = 0; + + if (!is_null($location)) { + $location = ltrim($location, '/'); + + // check to see if the redirect is an external redirect + if (strpos($location, '://') === FALSE) { + $location = $base_url . '/' . $location; + } + } + if ($persistence == REDIRECT_ONCE_PER_TIMEFRAME) { - $expire = REQUEST_TIME + 60 * 60 * 24 * 30; + // If the module is set to have a cookie then set it to expire per the admin menu + $expire = is_null($location) ? REQUEST_TIME - 3600 : REQUEST_TIME + variable_get('simple_mobile_redirect_expire', 2592000); + // Ensure we set a cookie with the cookie domain so we're not left in a situation with different + // cookie values set on different domains. + setcookie("smr_redirect", $location, $expire, '', $cookie_domain); } - if ($persistence != REDIRECT_EVERY_TIME) { - setcookie("nomobi", "yes", $expire); + + // Split up the URL for evaluation against the base URL. + $parsed_url = parse_url($location); + + // Check to see if we are redirecting and then check to see if we are not there already. + if ($redirect && $parsed_url['scheme'] . '://' . $parsed_url['host'] != $base_url) { + simple_mobile_redirect_redirect($location); + // header('Location: ' . $location, TRUE, 302); + // exit(); } - // drupal_goto() does not exist here, as we are triggering at boot. - // suggestions to work around this, perhaps a variable setting to get the full URL via the admin. - if ($redirect) { - header('Location: ' . $base_url . '/' . $simplemobilehome, TRUE, 302); + return; +} + +/** + * Redirect function + */ +function simple_mobile_redirect_redirect($location) { + // If there are additional parameters we redirect the + // user to the alternate domain keeping parameters intact. + if ($_GET['q']) { + $location .= '/' . $_GET['q']; } + // drupal_goto() does not exist here, as we are triggering at boot. + header('Location: ' . $location, TRUE, 302); + exit(); } /** @@ -81,91 +109,88 @@ function _simple_mobile_redirect_setcookie($redirect = FALSE) { */ function simple_mobile_redirect_boot() { global $base_url; - // if we've already redirected break out so we don't loop. - // check for nomobi to skip out as well - if ((isset($_COOKIE['smr_redirect']) && $_COOKIE['smr_redirect'] == 1) || - (isset($_COOKIE['nomobi']) && $_COOKIE['nomobi'] == 'yes') || - isset($_GET['mobi_redirect']) || isset($_GET['mobi_bypass']) ) { + // If the user has just exercised their right to view the non-mobile site, + // respect this and alter their cookie. They will encounter hook_boot + // for a second time, will then have their cookie evaluated below as non-mobile. + if (isset($_GET['nomobi']) && $_GET['nomobi'] == 'true') { + $nonmobile = variable_get('simple_mobile_redirect_home', ''); + _simple_mobile_redirect_setcookie($nonmobile, TRUE); + exit(); + } + // If we have the smr_redirect cookie + elseif (isset($_COOKIE['smr_redirect'])) { + $location = $_COOKIE['smr_redirect']; + + // redirect to the right url for this device. + // TODO: validate the URL before sending the user there. + $parsed_url = parse_url($location); + // Check to see if we are redirecting and then check to see if we are not there already. + if ($location && $parsed_url['scheme'] . '://' . $parsed_url['host'] != $base_url) { + simple_mobile_redirect_redirect($location); + // header('Location: ' . $simplemobileredirect, TRUE, 302); + // exit(); + } return; } + // If the user is coming to the site for the first time. + // Detect their browser and find out where they should be sent. + else { + $simplemobileredirect = ''; + // get the user agent + $user_agent = $_SERVER['HTTP_USER_AGENT']; + + // using a switch to check for the user agent, smoother than a series of if's + switch (TRUE) { + // find the word ipad in the user agent + case (preg_match('/ipad/i', $user_agent)): + $simplemobileredirect = variable_get('simple_mobile_redirect_ipad', ''); + break; - if (isset($_GET['nomobi']) && $_GET['nomobi'] === TRUE) { - _simple_mobile_redirect_setcookie(); - } + // find the word iphone in the user agent + case (preg_match('/ipod/i', $user_agent) || preg_match('/iphone/i', $user_agent)): + $simplemobileredirect = variable_get('simple_mobile_redirect_iphone', ''); + break; - $simplemobileredirect = ''; - // get the user agent - $user_agent = $_SERVER['HTTP_USER_AGENT']; - - // using a switch to check for the user agent, smoother than a searies of if's - switch (TRUE) { - // find the word ipad in the user agent - case (preg_match('/ipad/i', $user_agent)): - $simplemobileredirect = variable_get('simple_mobile_redirect_ipad', ''); - break; - - // find the word iphone in the user agent - case (preg_match('/ipod/i', $user_agent) || preg_match('/iphone/i', $user_agent)): - $simplemobileredirect = variable_get('simple_mobile_redirect_iphone', ''); - break; - - // we find android in the user agent - case (preg_match('/android/i', $user_agent)): - $simplemobileredirect = variable_get('simple_mobile_redirect_android', ''); - break; - - // we find opera mini in the user agent - case (preg_match('/opera mini/i', $user_agent)): - $simplemobileredirect = variable_get('simple_mobile_redirect_opera', ''); - break; - - // we find blackberry in the user agent - case (preg_match('/blackberry/i', $user_agent)): - $simplemobileredirect = variable_get('simple_mobile_redirect_blackberry', ''); - break; - - // we find palm os in the user agent - case (preg_match('/(pre\/|palm os|palm|hiptop|avantgo|plucker|xiino|blazer|elaine)/i', $user_agent)): - $simplemobileredirect = variable_get('simple_mobile_redirect_palm', ''); - break; - - // we find windows mobile in the user agent - the i at the end makes it case insensitive - case (preg_match('/(iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile)/i', $user_agent)): - $simplemobileredirect = variable_get('simple_mobile_redirect_windows', ''); - break; - - // check if any of the values listed create a match on the user agent - these are some of the most common terms used in agents to identify them as being mobile devices - case (preg_match('/(mini 9.5|vx1000|lge |m800|e860|u940|ux840|compal|wireless| mobi|ahong|lg380|lgku|lgu900|lg210|lg47|lg920|lg840|lg370|sam-r|mg50|s55|g83|t66|vx400|mk99|d615|d763|el370|sl900|mp500|samu3|samu4|vx10|xda_|samu5|samu6|samu7|samu9|a615|b832|m881|s920|n210|s700|c-810|_h797|mob-x|sk16d|848b|mowser|s580|r800|471x|v120|rim8|c500foma:|160x|x160|480x|x640|t503|w839|i250|sprint|w398samr810|m5252|c7100|mt126|x225|s5330|s820|htil-g1|fly v71|s302|-x113|novarra|k610i|-three|8325rc|8352rc|sanyo|vx54|c888|nx250|n120|mtk |c5588|s710|t880|c5005|i;458x|p404i|s210|c5100|teleca|s940|c500|s590|foma|samsu|vx8|vx9|a1000|_mms|myx|a700|gu1100|bc831|e300|ems100|me701|me702m-three|sd588|s800|8325rc|ac831|mw200|brew |d88|htc\/|htc_touch|355x|m50|km100|d736|p-9521|telco|sl74|ktouch|m4u\/|me702|8325rc|kddi|phone|lg |sonyericsson|samsung|240x|x320|vx10|nokia|sony cmd|motorola|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|psp|treo)/i', $user_agent)): - // is the device showing signs of support for text/vnd.wap.wml or application/vnd.wap.xhtml+xml - case (isset($_SERVER['HTTP_ACCEPT']) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/vnd.wap.wml') > 0) || (strpos($_SERVER['HTTP_ACCEPT'], 'application/vnd.wap.xhtml+xml') > 0)): - // is the device giving us a HTTP_X_WAP_PROFILE or HTTP_PROFILE header - only mobile devices would do this - case (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])): - $simplemobileredirect = variable_get('simple_mobile_redirect_mobileredirect', ''); - break; - - default: + // we find android in the user agent + case (preg_match('/android/i', $user_agent)): + $simplemobileredirect = variable_get('simple_mobile_redirect_android', ''); break; - } - // if redirect (either the value of the mobile or desktop redirect depending on the value of $is_mobile_browser) is TRUE redirect else we return the status of $is_mobile_browser - if ($simplemobileredirect != '') { - // set a cookie so we don't loop. - if (variable_get('simple_mobile_persistent_redirect', REDIRECT_ONCE_PER_TIMEFRAME) == REDIRECT_EVERY_TIME) { - setcookie('smr_redirect', 1); - } + // we find opera mini in the user agent + case (preg_match('/opera mini/i', $user_agent)): + $simplemobileredirect = variable_get('simple_mobile_redirect_opera', ''); + break; - $simplemobileredirect = ltrim($simplemobileredirect, '/'); + // we find blackberry in the user agent + case (preg_match('/blackberry/i', $user_agent)): + $simplemobileredirect = variable_get('simple_mobile_redirect_blackberry', ''); + break; - // check to see if the redirect is an external redirect - if (strpos($simplemobileredirect, '://') === FALSE) { - $simplemobileredirect = $base_url . '/' . $simplemobileredirect; + // we find palm os in the user agent + case (preg_match('/(pre\/|palm os|palm|hiptop|avantgo|plucker|xiino|blazer|elaine)/i', $user_agent)): + $simplemobileredirect = variable_get('simple_mobile_redirect_palm', ''); + break; + + // we find windows mobile in the user agent - the i at the end makes it case insensitive + case (preg_match('/(iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile)/i', $user_agent)): + $simplemobileredirect = variable_get('simple_mobile_redirect_windows', ''); + break; + + // check if any of the values listed create a match on the user agent - these are some of the most common terms used in agents to identify them as being mobile devices + case (preg_match('/(mini 9.5|vx1000|lge |m800|e860|u940|ux840|compal|wireless| mobi|ahong|lg380|lgku|lgu900|lg210|lg47|lg920|lg840|lg370|sam-r|mg50|s55|g83|t66|vx400|mk99|d615|d763|el370|sl900|mp500|samu3|samu4|vx10|xda_|samu5|samu6|samu7|samu9|a615|b832|m881|s920|n210|s700|c-810|_h797|mob-x|sk16d|848b|mowser|s580|r800|471x|v120|rim8|c500foma:|160x|x160|480x|x640|t503|w839|i250|sprint|w398samr810|m5252|c7100|mt126|x225|s5330|s820|htil-g1|fly v71|s302|-x113|novarra|k610i|-three|8325rc|8352rc|sanyo|vx54|c888|nx250|n120|mtk |c5588|s710|t880|c5005|i;458x|p404i|s210|c5100|teleca|s940|c500|s590|foma|samsu|vx8|vx9|a1000|_mms|myx|a700|gu1100|bc831|e300|ems100|me701|me702m-three|sd588|s800|8325rc|ac831|mw200|brew |d88|htc\/|htc_touch|355x|m50|km100|d736|p-9521|telco|sl74|ktouch|m4u\/|me702|8325rc|kddi|phone|lg |sonyericsson|samsung|240x|x320|vx10|nokia|sony cmd|motorola|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|psp|treo)/i', $user_agent)): + // is the device showing signs of support for text/vnd.wap.wml or application/vnd.wap.xhtml+xml + case (isset($_SERVER['HTTP_ACCEPT']) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/vnd.wap.wml') > 0) || (strpos($_SERVER['HTTP_ACCEPT'], 'application/vnd.wap.xhtml+xml') > 0)): + // is the device giving us a HTTP_X_WAP_PROFILE or HTTP_PROFILE header - only mobile devices would do this + case (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])): + $simplemobileredirect = variable_get('simple_mobile_redirect_mobileredirect', ''); + break; + + default: + $simplemobileredirect = variable_get('simple_mobile_redirect_home', ''); + break; } - // redirect to the right url for this device - header('Location: ' . $simplemobileredirect, TRUE, 302); - exit; - } - else { - // set a cookie so we don't need to go through that again. - _simple_mobile_redirect_setcookie(); + // Set the cookie and redirect the user to the correct location + _simple_mobile_redirect_setcookie($simplemobileredirect, TRUE); } -} + return; +}