2c2
< // $Id: ip2locale.module,v 1.1.2.7 2009/05/03 17:17:07 mrfelton Exp $
---
> // $Id: ip2locale.module,v 1.1.2.19 2010/05/07 12:19:02 mrfelton Exp $
25c25
<       $output = '<p>'. t('Drupal is set up fairly well for multilingual sites. However, it does make the assumption that each site version will be in a different language. It is quite feasible however, that there be two versions of a site in the same language. For example, A site which has a UK version, and a version for the Republic of Ireland. Both sites are written in English, but the content varies for each. Drupals multilingual features provide the perfect tools to allow \'translation\' of content from one version of the site to another, but In a setup like this, it may also be desirable to automatically redirect users to a particular version of the site based on their IP address...') .'</p>';
---
>       $output = '<p>'. t('Drupal is set up fairly well for multilingual sites. However, it does make the assumption that each site version will be in a different language. It is quite feasible however, that there be two versions of a site in the same language. For example, a site which has a UK version, and a version for the Republic of Ireland. Both sites are written in English, but the content varies for each. Drupals multilingual features provide the perfect tools to allow \'translation\' of content from one version of the site to another, but in a setup like this, it may also be desirable to automatically redirect users to a particular version of the site based on their IP address...') .'</p>';
99c99
<   
---
> 
131,133c131
<   /**
<    * If the site is in offline there is little point doing any of this as you might end up redirecting to a 503.
<    */
---
>   // If the site is in offline there is little point doing any of this as you might end up redirecting to a 503.
135a134,173
>   // Do not redirect if we are running from cron.
>   if (basename(request_uri()) == 'cron.php') return FALSE;
> 
>   // Do not redirect if we are running from the command line.
>   if (empty($_SERVER['SERVER_ADDR'])) return FALSE;
> 
>   // Do not redirect if we are accessing the update page.
>   if (basename(request_uri()) == 'update.php') return FALSE;
>  
>   // Do not redirect if the site is visited by a search engine bot/crawler.
>   if (module_exists(browscap)) {
>     if (variable_get('ip2locale_debug', FALSE)) {
>         $message = 'Browscap Module: Enabled.';
>         drupal_set_message($message);
>     }
> 
>     // check if user agent string matches a search engine user agent string or other crawler (robot / bot / spider)
>     $useragent = $_SERVER['HTTP_USER_AGENT'];
>     if (browscap_is_crawler()) {
>       if (variable_get('ip2locale_debug', FALSE)) {
>         $message = 'Crawler Detected.  No redirect applied.';
>         drupal_set_message($message);
>         drupal_set_message('User Agent: '. $useragent);
>       }
>       return FALSE;
>     }
>     else {
>       if (variable_get('ip2locale_debug', FALSE)) {
>         $message = 'No crawler detected. Redirect will be applied if configured at the <a href="/admin/settings/ip2locale/settings">ip2locale admin settings page</a>.';
>         drupal_set_message($message);
>         drupal_set_message('User Agent: '. $useragent);
>       }
>     }
>   }
>   else {
>     if (variable_get('ip2locale_debug', FALSE)) {
>       drupal_set_message('Browscap module not found. Search engines and other crawlers will also be redirected. Inorder to prevent ip2locale from redirecting search engines and other crawlers (which you may want to index the website) please install the <a href="http://drupal.org/project/browscap">browsercap module</a> and ip2locale will not redirect search engines/crawlers.');
>     }
>   }
>  
137,140c175,176
<     /**
<      * We only want to act if the user comes to the page from an external site/direct hit
<      */
<     if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME'])) return FALSE;
---
>     // We only want to act if the user comes to the page from an external site/direct hit
>     if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME'])) return FALSE;
141a178,186
> 
>   // If we ip2locale has just been processed, do not process it again.
>   if (isset($_SESSION['ip2locale_lc_processed'])) {
>     unset($_SESSION['ip2locale_lc_processed']);
>     return FALSE;
>   }
> 
>   // load in the code required to deal with paths properly.
>   drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH); 
142a188,191
>   // Should we attempt to perform a redirect on this path?
>   $page_match = ip2locale_match();
>   if (!$page_match) return FALSE;
> 
145,147c194,196
<   require_once './includes/common.inc'; 
<   
<   // Attempt to register backends
---
>   require_once './includes/common.inc';
> 
>   // Attempt to register backends.
150,156c199,206
<   drupal_load('module', 'uc_ip2country');
<   
<   // Get the users language code from their session if it exists
<   if ((variable_get('ip2locale_debug', FALSE) && $_GET['ip2locale_debug']) || !$_SESSION['ip2locale_lc']) {
<     // Work out the the users language code based on their IP address.
<     $ip = ip2locale_ip_address();
<     $lc = $_SESSION['ip2locale_lc'] = ip2locale_get_locale($ip);
---
>   drupal_load('module', 'ip2country');
> 
>   // Try to get saved locale from cookies.
>   if (variable_get('ip2locale_save_locale', TRUE) && !empty($_COOKIE['ip2locale_lc'])) {
>     $lc = $_COOKIE['ip2locale_lc'];
>     if (variable_get('ip2locale_debug', FALSE)) {
>       drupal_set_message("IP to Locale debug: Got language code '". $lc ."' from user cookie.");
>     }
159c209,220
<     $lc = $_SESSION['ip2locale_lc'];
---
>     // Get the users language code from their session if it exists.
>     if ((variable_get('ip2locale_debug', FALSE) && $_GET['ip2locale_debug']) || !$_SESSION['ip2locale_lc']) {
>       // Work out the the users language code based on their IP address.
>       $ip = ip2locale_ip_address();
>       $lc = $_SESSION['ip2locale_lc'] = ip2locale_get_locale($ip);
>     }
>     else {
>       if (variable_get('ip2locale_debug', FALSE)) {
>         drupal_set_message("IP to Locale debug: Got language code '". $lc ."' from users session.");
>       }
>       $lc = $_SESSION['ip2locale_lc'];
>     }
166,170c227,230
<   
<   drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH); // load in the code required to deal with paths properly
<   drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE); // load in the code required to deal with languages
<   
<   // get the current language
---
> 
>   drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE); // Load in the code required to deal with languages.
> 
>   // Get the current language.
172,173c232,233
<   
<   // Get the Query String (minus the 'q'). If none set, set to NULL
---
> 
>   // Get the Query String (minus the 'q'). If none set, set to NULL.
179,180c239,240
<   // Establish the language prefix that should be used, ie. the one that drupal_goto() would use
<   $options = array('prefix' => '');
---
>   // Establish the language prefix that should be used, ie. the one that drupal_goto() would use.
>   $options = array('prefix' => '', 'external' => FALSE);
182c242
<     // Note that language_url_rewrite() takes path (by reference) as the first argument but does not use it at all
---
>     // Note that language_url_rewrite() takes path (by reference) as the first argument but does not use it at all.
183a244
> 
187c248
<     
---
> 
189c250
<   $alias = drupal_get_path_alias($_GET['q']);
---
>   $alias = drupal_get_path_alias($_GET['q'], $lc);
193c254
<   
---
> 
195,197c256,258
<   // 1. the country code maps to an enabled language
<   // 2. the country code is not the active language
<   // 3. the user has not asked for a specific, prefixed version of a page
---
>   // 1. the country code maps to an enabled language.
>   // 2. the country code is not the active language.
>   // 3. the user has not asked for a specific, prefixed version of a page.
199c260
<   
---
> 
201c262
<     
---
> 
208c269
<         
---
> 
216c277
<         drupal_set_message('IP to Locale debug... Redirecting to: '. $redirect);
---
>         drupal_set_message('IP to Locale debug: Redirecting to '. $redirect);
218c279,281
<       drupal_load('module', 'filter'); // this is needed to use drupal_goto, since it uses url() which requires access to filter_xss_bad_protocol()
---
>       // Set a session variable so that we can detect if we have already run or not.
>       $_SESSION['ip2locale_lc_processed'] = TRUE;
>       drupal_load('module', 'filter'); // This is needed to use drupal_goto, since it uses url() which requires access to filter_xss_bad_protocol().
223a287,323
> /**
>  * Implementation of hook_init().
>  */
> function ip2locale_init() {
>   global $language;
>   if (variable_get('ip2locale_save_locale', TRUE)) {
>     setcookie('ip2locale_lc', $language->language, time() + 60*60*24*30, '/');
>   }
> }
> 
> /**
>  * ip2locale_match()
>  *
>  * check the page past and see if it should attempt to redirect or not.
>  *
>  * @param $path
>  *  the page of the page to check.
>  *
>  * @return
>  *  1 - page should attempt to redirect.
>  *  NULL - do not change page.
>  */
> function ip2locale_match() {
>   /**
>    * Check to see if the page matches the current settings.
>    */
>   
>   $off_pages = variable_get('ip2locale_pages', "*");
>   
>   $path = drupal_get_path_alias($_GET['q']);
>   // Compare with the internal and path alias (if any).
>   $page_match = drupal_match_path($path, $off_pages);
>   if ($path != $_GET['q']) {
>     $page_match = $page_match || drupal_match_path($_GET['q'], $off_pages);
>   }
>   return $page_match;
> }
232c332
<   
---
> 
237c337
<   
---
> 
266c366
<     drupal_set_message('Status of '.$backend.' updated.');
---
>     drupal_set_message('Status of '. $backend .' updated.');
274c374
<   
---
> 
279,280c379,380
<   variable_set('ip2locale_backend_'.$backend.'_enabled', $status);
<   
---
>   variable_set('ip2locale_backend_'. $backend .'_enabled', $status);
> 
302c402
<     if (preg_match('/^([a-z]{2})\,([a-z]+)$/', $option, $matches)) {
---
>     if (preg_match('/^([^,]+)\,(.*)$/', $option, $matches)) {
320c420
<     'uc_ip2country' => 'uc_ip2country_get_country',
---
>     'ip2country' => 'ip2country_get_country',
338c438
<       $backends[$module] = variable_get('ip2locale_'.$module.'_weight', 50);
---
>       $backends[$module] = variable_get('ip2locale_'. $module .'_weight', 50);
355c455
<     if (variable_get('ip2locale_backend_'.$module.'_enabled', FALSE)) {
---
>     if (variable_get('ip2locale_backend_'. $module .'_enabled', FALSE)) {
374c474
<       
---
> 
377c477
<       
---
> 
381c481
< } 
---
> }
393c493
<   foreach($backends as $module => $function) {
---
>   foreach ($backends as $module => $function) {
400,401c500,501
<       case 'uc_ip2country':
<         $cc = uc_ip2country_get_country($ip);
---
>       case 'ip2country':
>         $cc = ip2country_get_country($ip);
403c503
<         
---
> 
411c511
<         drupal_set_message('IP to Locale debug... IP mapped to country code: '. $cc .' (by '. $module .')');
---
>         drupal_set_message('IP to Locale debug: IP mapped to country code '. $cc .' (by '. $module .')');
416c516
<   
---
> 
419c519
<   
---
> 
422c522
<     drupal_set_message('IP to Locale debug... Country code mapped to locale: '. $mapped);
---
>     drupal_set_message('IP to Locale debug: Country code mapped to locale '. $mapped);
424c524
<   
---
> 
434c534
<     drupal_set_message('IP to Locale debug... Using IP: '. $ip);
---
>     drupal_set_message('IP to Locale debug: Using IP '. $ip);
440c540
< }
\ No newline at end of file
---
> }
