Index: globalredirect.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/globalredirect/globalredirect.admin.inc,v retrieving revision 1.1.2.8 diff -u -r1.1.2.8 globalredirect.admin.inc --- globalredirect.admin.inc 3 Mar 2010 16:56:21 -0000 1.1.2.8 +++ globalredirect.admin.inc 19 May 2010 23:24:47 -0000 @@ -10,6 +10,8 @@ * Function to generate the form setting array */ function globalredirect_settings() { + $settings = _globalredirect_get_settings(); + $form['globalredirect_deslash'] = array( '#type' => 'radios', '#title' => t('Deslash'), @@ -18,7 +20,7 @@ GLOBALREDIRECT_FEATURE_DISABLED => t('Off'), GLOBALREDIRECT_DESLASH_ENABLED => t('On'), ), - '#default_value' => variable_get('globalredirect_deslash', GLOBALREDIRECT_DESLASH_ENABLED), + '#default_value' => $settings->deslash, ); $form['globalredirect_nonclean2clean'] = array( @@ -29,7 +31,7 @@ GLOBALREDIRECT_FEATURE_DISABLED => t('Off'), GLOBALREDIRECT_NONCLEAN2CLEAN_ENABLED => t('On'), ), - '#default_value' => variable_get('globalredirect_nonclean2clean', GLOBALREDIRECT_NONCLEAN2CLEAN_ENABLED), + '#default_value' => $settings->nonclean2clean, ); $form['globalredirect_trailingzero'] = array( @@ -41,7 +43,7 @@ GLOBALREDIRECT_TRAILINGZERO_TAXTERM => t('Enabled for taxonomy term pages only'), GLOBALREDIRECT_TRAILINGZERO_ALL => t('Enabled for all pages'), ), - '#default_value' => variable_get('globalredirect_trailingzero', GLOBALREDIRECT_FEATURE_DISABLED), + '#default_value' => $settings->trailingzero, ); $form['globalredirect_menu_check'] = array( @@ -52,7 +54,7 @@ GLOBALREDIRECT_FEATURE_DISABLED => t('Disabled'), GLOBALREDIRECT_MENU_CHECK_ENABLED => t('Enabled'), ), - '#default_value' => variable_get('globalredirect_menu_check', GLOBALREDIRECT_FEATURE_DISABLED), + '#default_value' => $settings->menu_check, ); $form['globalredirect_case_sensitive_urls'] = array( @@ -63,7 +65,7 @@ GLOBALREDIRECT_FEATURE_DISABLED => t('Disabled'), GLOBALREDIRECT_CASE_SENSITIVE_URLS_ENABLED => t('Enabled'), ), - '#default_value' => variable_get('globalredirect_case_sensitive_urls', GLOBALREDIRECT_CASE_SENSITIVE_URLS_ENABLED), + '#default_value' => $settings->case_sensitive_urls, ); return system_settings_form($form); Index: globalredirect.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/globalredirect/globalredirect.module,v retrieving revision 1.1.2.4.2.5.2.21 diff -u -r1.1.2.4.2.5.2.21 globalredirect.module --- globalredirect.module 18 Apr 2010 13:34:47 -0000 1.1.2.4.2.5.2.21 +++ globalredirect.module 20 May 2010 11:24:26 -0000 @@ -31,10 +31,14 @@ } /** - * Implements hook_init(). + * Check if globalredirect should run at all. + * + * @return bool + * if FALSE, globalredirect will not run this time. + * It might still run, if its hook_init is called + * a second time during the same request. */ -function globalredirect_init() { - global $language; +function _globalredirect_is_active($settings) { /** * We need to do a test to make sure we only clean up URL's for the main @@ -66,13 +70,6 @@ if (variable_get('site_offline', 0) == 1) return FALSE; /** - * Use of menu_get_item should be optional as it appears in some situations - * it causes WSOD's... - */ - $menu_check = variable_get('globalredirect_menu_check', GLOBALREDIRECT_FEATURE_DISABLED); - - - /** * We need to make sure this hook only fires in certain conditions: * 1) If the 'drupal_get_path' function exists. Sometimes hook_init gets * called twice, the first call hasn't included path.inc. @@ -80,19 +77,38 @@ * posts to an source path rather than the alias. GlobalRedirect * sometimes interrupts the post and redirects to the alias instead. */ - - if (function_exists('drupal_get_path_alias') && - ($menu_check == GLOBALREDIRECT_FEATURE_DISABLED || ($menu_check == GLOBALREDIRECT_MENU_CHECK_ENABLED && function_exists('menu_get_item'))) && - empty($_POST)) { - - // If menu checking is enabled, do the check. Feature disabled by default. - if ($menu_check == GLOBALREDIRECT_MENU_CHECK_ENABLED) { - // Check the access on the current path, return FALSE if access not - // allowed. This stops redirection for paths without access permission. + if (!function_exists('drupal_get_path_alias')) { + return FALSE; + } + + if (!empty($_POST)) { + return FALSE; + } + + if ($settings->menu_check) { + if (!function_exists('menu_get_item')) { + return FALSE; + } else { $item = menu_get_item(); _menu_check_access($item, $item['map']); - if (!$item['access']) return FALSE; + if (!$item['access']) { + return FALSE; + } } + } + + return TRUE; +} + + +/** + * Implements hook_init(). + */ +function globalredirect_init() { + global $language; + $settings = _globalredirect_get_settings(TRUE); + + if (_globalredirect_is_active($settings)) { // Store the destination from the $_REQUEST as it breaks things if we leave // it in - restore it at the end... @@ -133,9 +149,11 @@ if ($_REQUEST['q'] != $prefix) { drupal_goto('', $query_string, NULL, 301); } - elseif((variable_get('globalredirect_nonclean2clean', GLOBALREDIRECT_NONCLEAN2CLEAN_ENABLED) == GLOBALREDIRECT_NONCLEAN2CLEAN_ENABLED) && - ((bool)variable_get('clean_url', 0)) && - (strpos(request_uri(), '?q=') || strpos(request_uri(), 'index.php'))) { + elseif( + $settings->nonclean2clean && + (bool)variable_get('clean_url', 0) && + (strpos(request_uri(), '?q=') || strpos(request_uri(), 'index.php')) + ) { drupal_goto('', $query_string, NULL, 301); } // If we've got to this point then we're on a front page with a VALID @@ -144,11 +162,10 @@ } // Trim any trailing slash off the end (eg, 'node/1/' to 'node/1') - $redirect_slash = variable_get('globalredirect_deslash', GLOBALREDIRECT_DESLASH_ENABLED) == GLOBALREDIRECT_DESLASH_ENABLED; - $request = $redirect_slash ? trim($_GET['q'], '/') : $_GET['q']; + $request = $settings->deslash ? trim($_GET['q'], '/') : $_GET['q']; // Optional stripping of "/0". Disabled by default. - switch (variable_get('globalredirect_trailingzero', GLOBALREDIRECT_FEATURE_DISABLED)) { + switch ($settings->trailingzero) { case GLOBALREDIRECT_TRAILINGZERO_TAXTERM : // If 'taxonomy/term/*' only. If not, break out. if (drupal_substr($request, 0, 14) != 'taxonomy/term/') { @@ -176,9 +193,9 @@ // Alias case sensitivity check. If there is an alias from the previous // lookup, do a query to test for case. - if ($alias && variable_get('globalredirect_case_sensitive_urls', GLOBALREDIRECT_CASE_SENSITIVE_URLS_ENABLED) == GLOBALREDIRECT_CASE_SENSITIVE_URLS_ENABLED) { + if ($alias && $settings->case_sensitive_urls) { $alias_sensitive = db_result(db_query("SELECT dst FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $alias, $langcode)); - if ($alias_sensitive && $alias != $alias_sensitive) { + if ($alias_sensitive) { // There is a match and there is a difference in case. $alias = $alias_sensitive; } @@ -188,14 +205,18 @@ // agent. If we have a language prefix then prefix the alias if ($_REQUEST['q'] != $prefix . $alias) { // If it's not just a slash or user has deslash on, redirect - if (str_replace($prefix . $alias, '', $_REQUEST['q']) != '/' || $redirect_slash) { + if (str_replace($prefix . $alias, '', $_REQUEST['q']) != '/' || $settings->deslash) { drupal_goto($alias, $query_string, NULL, 301); } } // If no alias was returned, the final check is to direct non-clean to // clean - if clean is enabled - if ((variable_get('globalredirect_nonclean2clean', GLOBALREDIRECT_NONCLEAN2CLEAN_ENABLED) == GLOBALREDIRECT_NONCLEAN2CLEAN_ENABLED) && ((bool)variable_get('clean_url', 0)) && strpos(request_uri(), '?q=')) { + if ( + $settings->nonclean2clean && + (bool)variable_get('clean_url', 0) && + strpos(request_uri(), '?q=') + ) { drupal_goto($request, $query_string, NULL, 301); } @@ -205,6 +226,35 @@ } /** + * Load all settings into an object. + * + * @param $disabled_is_false + * If TRUE, all -1 values will be changed to FALSE. + * This allows for more compact if() conditions. + */ +function _globalredirect_get_settings($disabled_is_false = FALSE) { + $settings = array( + 'deslash' => GLOBALREDIRECT_DESLASH_ENABLED, + 'nonclean2clean' => GLOBALREDIRECT_NONCLEAN2CLEAN_ENABLED, + 'trailingzero' => GLOBALREDIRECT_FEATURE_DISABLED, + // Use of menu_get_item should be optional as + // it appears in some situations it causes WSOD's... + 'menu_check' => GLOBALREDIRECT_FEATURE_DISABLED, + 'case_sensitive_urls' => GLOBALREDIRECT_CASE_SENSITIVE_URLS_ENABLED, + ); + + foreach ($settings as $suffix => $value) { + $settings[$suffix] = variable_get('globalredirect_'.$suffix, $value); + if ($disabled_is_false && $settings[$suffix] == GLOBALREDIRECT_FEATURE_DISABLED) { + $settings[$suffix] = FALSE; + } + } + + return (object)$settings; +} + + +/** * Implements hook_menu(). */ function globalredirect_menu() {