Changeset 480258
- Timestamp:
- 12/24/2011 07:23:33 PM (14 years ago)
- Location:
- geographical-redirect/trunk
- Files:
-
- 5 edited
-
geo-redirect-admin.php (modified) (11 diffs)
-
geo-redirect.php (modified) (9 diffs)
-
geoip/geoip.inc (modified) (1 diff)
-
geoip/ipdatabase/GeoIP.dat/GeoIP.dat (modified) (previous)
-
readme.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
geographical-redirect/trunk/geo-redirect-admin.php
r431960 r480258 14 14 $html = '<p>This plugin allows you to redirect your visitors according to their country.</p> 15 15 <p>Just add the country from selectbox and fill in preferable options.</p> 16 <p>Static URL redirect has the highest priority if not blank.<br /> 17 Domain redirect has lower priority if not blank.<br /> 18 Redirect by language has the lowest priority among the options.</p> 16 <p>This plugin does not create different language versions of your site, but just redirects to existing ones.<br/> 17 Be attentive while entering redirect options, because wrong parameters can lead to infinite redirections etc.</p> 19 18 <p>If you have some troubles accessing your site because of incorrect redirect, 20 just add to browser\'s url «no_redirect» (example: '.get_ bloginfo('url').'/?page_id=5&<b>no_redirect</b>) to ignore redirection.</p>';19 just add to browser\'s url «no_redirect» (example: '.get_home_url().'/sample-page/?<b>no_redirect</b>) to switch off the redirection.</p>'; 21 20 22 21 return $html; 23 22 } 24 23 24 function geo_redirect_pretty_permalink_checkbox($data){ 25 if ($data['lang_code'] == '') 26 $data['lang_code'] = 'some_lang'; 27 $html = '<label title="Redirect to ' . get_home_url() . '/' . $data['lang_code'] .'/ instead of ' . get_home_url() . '/?lang=' . $data['lang_code'] . '">Use pretty permalink<input type="checkbox" name="pretties[]" value="' . $data['country_id'] . '" ' . (($data['pretty'] == 1) ? 'checked="checked"' : '') . '></label>'; 28 return $html; 29 } 25 30 26 31 function geo_redirect_admin_page_display(){ … … 35 40 <div class="wrap"> 36 41 <div id="icon-options-general" class="icon32"><br></div> 37 <h2> ' . __('Geographical Redirect Options') . '</h2>42 <h2>Geographical Redirect Options</h2> 38 43 <form action="" method="post" enctype="multipart/form-data">'; 39 44 … … 42 47 $redirect = ''; 43 48 $only_outsite = 0; 49 $only_root = 0; 44 50 $lang_slug = 'lang'; 45 51 … … 50 56 $redirect = $geo_redirect_data['redirect']; 51 57 $only_outsite = $geo_redirect_data['only_outsite']; 58 $only_root = $geo_redirect_data['only_root']; 52 59 $lang_slug = $geo_redirect_data['lang_slug']; 53 60 } … … 55 62 $geoip = new GeoIP(); 56 63 $countries = $geoip->GEOIP_COUNTRY_NAMES; 64 $country_codes = $geoip->GEOIP_COUNTRY_CODES; 65 $lang_codes = $geoip->GEOIP_LANG_CODES; 57 66 if (is_array($countries)) : 58 67 $html .= '<div class="tablenav top">'; … … 61 70 foreach ($countries as $country_id => $country) : 62 71 if ($country_id == 0) 63 $html .= '<option value="' . $country_id . '"> ' . __('Select country') . '</option>';72 $html .= '<option value="' . $country_id . '">Select country</option>'; 64 73 elseif (!in_array($country_id,array(1,2))) 65 $html .= '<option value="' . $country_id . '" >' . htmlspecialchars($country) . '</option>';74 $html .= '<option value="' . $country_id . '" data-lang="' . strtolower($lang_codes[$country_id]) . '" data-country-code="' . strtolower($country_codes[$country_id]) . '">' . htmlspecialchars($country) . '</option>'; 66 75 endforeach; 67 76 $html .= '</select>'; 68 $html .= '<input onclick="return geo AddCountry();" type="submit" class="button-secondary action" value="Add country" />';77 $html .= '<input onclick="return geoRedirect.addCountry();" type="submit" class="button-secondary action" value="Add country" />'; 69 78 $html .= '</div></div><br clear="all" />'; 70 79 endif; 71 72 $html .= '<div class="geo-redirect-options"> 80 81 82 $html .= '<div class="geo-redirect-options"> 73 83 <table class="wp-list-table widefat plugins" cellspacing="0"> 74 84 <thead> 75 85 <tr> 76 <th scope="col" id="name" class="manage-column column-name" style="">Country</th> 77 <th scope="col" id="name" class="manage-column column-name" style="">Language Code</th> 78 <th scope="col" id="name" class="manage-column column-name" style="">Domain Name</th> 79 <th scope="col" id="name" class="manage-column column-name" style="">Static URL</th> 80 <th scope="col" id="name" class="manage-column column-name" style=""></th> 86 <th scope="col" id="name" class="manage-column column-name" width="20%">Country</th> 87 <th scope="col" id="name" class="manage-column column-name" width="20%">Redirect Option</th> 88 <th scope="col" id="name" class="manage-column column-name" width="55%">Option Value</th> 89 <th scope="col" id="name" class="manage-column column-name" width="5%"></th> 81 90 </tr> 82 91 </thead> 83 92 <tbody>'; 84 $default_shown = false; 85 if (is_array($redirect)) { 93 94 $default_redirect = array( 'country_id' => -1, 95 'redirect_option' => -1, 96 'lang_code' => '', 97 'domain' => '', 98 'pretty' => 0, 99 'url' => ''); 100 101 if (is_array($redirect)) { 86 102 foreach ($redirect as $data) { 87 88 if ($data['country_id'] == -1) { 89 $default_shown = true; 90 $html .='<tr class="geo-redirect-option active">'. 91 '<td>'. 92 '<input type="hidden" name="country_ids[]" value="-1">'. 93 '<p><b>Not assigned countries</b></p>'. 94 '</td>'. 95 '<td>'. 96 '<p><input id="geo-redirect-country--1" class="small-text" name="lang_codes[]" type="text" maxlength="2" value="'.stripslashes($data['lang_code']).'"></p>'. 97 '</td>'. 98 '<td>'. 99 '<p><input class="regular-text" name="domains[]" type="text" value="'.stripslashes($data['domain']).'"></p>'. 100 '</td>'. 101 '<td>'. 102 '<p><input class="regular-text" name="urls[]" type="text" value="'.stripslashes($data['url']).'"></p>'. 103 '</td>'. 104 '<td>'. 105 '</td>'. 106 '</tr>'; 107 108 } else { 109 110 $html .='<tr class="geo-redirect-option active">'. 111 '<td>'. 112 '<input type="hidden" name="country_ids[]" value="'.$data['country_id'].'">'. 113 '<p><b>'.$countries[$data['country_id']].'</b></p>'. 114 '</td>'. 115 '<td>'. 116 '<p><input id="geo-redirect-country-'.$data['country_id'].'" class="small-text" name="lang_codes[]" type="text" maxlength="2" value="'.stripslashes($data['lang_code']).'"></p>'. 117 '</td>'. 118 '<td>'. 119 '<p><input class="regular-text" name="domains[]" type="text" value="'.stripslashes($data['domain']).'"></p>'. 120 '</td>'. 121 '<td>'. 122 '<p><input class="regular-text" name="urls[]" type="text" value="'.stripslashes($data['url']).'"></p>'. 123 '</td>'. 124 '<td>'. 125 '<p style="line-height:2.3"><a onclick="return geoRemoveCountry(this);" href="#" class="delete">Remove</a></p>'. 126 '</td>'. 127 '</tr>'; 103 104 if ($data['country_id'] == $default_redirect['country_id']) { 105 $default_redirect = $data; 106 continue; 107 128 108 } 129 130 } 131 } 132 133 if (!$default_shown) { 134 $html .='<tr class="geo-redirect-option active">'. 135 '<td>'. 136 '<input type="hidden" name="country_ids[]" value="-1">'. 137 '<b>Not assigned countries</b></p>'. 138 '</td>'. 139 '<td>'. 140 '<p><input id="geo-redirect-country--1" class="small-text" name="lang_codes[]" type="text" maxlength="2"></p>'. 141 '</td>'. 142 '<td>'. 143 '<p><input class="regular-text" name="domains[]" type="text"></p>'. 144 '</td>'. 145 '<td>'. 146 '<p><input class="regular-text" name="urls[]" type="text"></p>'. 147 '</td>'. 148 '<td>'. 149 '</td>'. 150 '</tr>'; 151 } 152 109 110 $html .='<tr class="geo-redirect-option active">'. 111 '<td>'. 112 '<input type="hidden" name="country_ids[]" value="'.$data['country_id'].'"/>'. 113 '<p><b>'.$countries[$data['country_id']].'</b></p>'. 114 '</td>'. 115 '<td>'. 116 '<p><select class="redirect_options" name="redirect_options[]" onchange="geoRedirect.switchOption(this);">'. 117 '<option value="1" ' . (($data['redirect_option'] == 1) ? 'selected="selected"' : '') . ' >Language Code</option>'. 118 '<option value="2" ' . (($data['redirect_option'] == 2) ? 'selected="selected"' : '') . ' >Domain Name</option>'. 119 '<option value="3" ' . (($data['redirect_option'] == 3) ? 'selected="selected"' : '') . ' >Static URL</option>'. 120 '</select></p>'. 121 '</td>'. 122 '<td id="redirect_options_container_' . $data['country_id'] . '">'. 123 '<p id="redirect_option_value_' . $data['country_id'] . '_1" style="display:' . (($data['redirect_option'] == 1 || empty($data['redirect_option'])) ? 'block' : 'none') . '"><input class="small-text" name="lang_codes[]" type="text" maxlength="3" value="'.stripslashes($data['lang_code']).'"/> ' . geo_redirect_pretty_permalink_checkbox($data) . '</p>'. 124 '<p id="redirect_option_value_' . $data['country_id'] . '_2" style="display:' . (($data['redirect_option'] == 2) ? 'block' : 'none') . '"><input class="regular-text" name="domains[]" type="text" value="'.stripslashes($data['domain']).'"/></p>'. 125 '<p id="redirect_option_value_' . $data['country_id'] . '_3" style="display:' . (($data['redirect_option'] == 3) ? 'block' : 'none') . '"><input class="regular-text" name="urls[]" type="text" value="'.stripslashes($data['url']).'"/></p>'. 126 '</td>'. 127 '<td>'. 128 '<p style="line-height:2.3"><a onclick="return geoRedirect.removeCountry(this);" href="#" class="delete">Remove</a></p>'. 129 '</td>'. 130 '</tr>'; 131 132 } 133 } 134 135 136 $html .='<tr class="geo-redirect-option default active">'. 137 '<td>'. 138 '<input type="hidden" name="country_ids[]" value="' . $default_redirect['country_id'] . '"/>'. 139 '<p><b>Default redirect</b></p>'. 140 '</td>'. 141 '<td>'. 142 '<p><select class="redirect_options" name="redirect_options[]" onchange="geoRedirect.switchOption(this);">'. 143 '<option value="-1" ' . (($default_redirect['redirect_option'] == -1) ? 'selected="selected"' : '') . ' >None</option>'. 144 '<option value="1" ' . (($default_redirect['redirect_option'] == 1) ? 'selected="selected"' : '') . ' >Language Code</option>'. 145 '<option value="2" ' . (($default_redirect['redirect_option'] == 2) ? 'selected="selected"' : '') . ' >Domain Name</option>'. 146 '<option value="3" ' . (($default_redirect['redirect_option'] == 3) ? 'selected="selected"' : '') . ' >Static URL</option>'. 147 '</select></p>'. 148 '</td>'. 149 '<td id="redirect_options_container_' . $default_redirect['country_id'] . '">'. 150 '<p id="redirect_option_value_' . $default_redirect['country_id'] . '_1" style="display:' . (($default_redirect['redirect_option'] == 1) ? 'block' : 'none') . '"><input class="small-text" name="lang_codes[]" type="text" maxlength="3" value="'.stripslashes($default_redirect['lang_code']).'"/> ' . geo_redirect_pretty_permalink_checkbox($default_redirect) . '</p>'. 151 '<p id="redirect_option_value_' . $default_redirect['country_id'] . '_2" style="display:' . (($default_redirect['redirect_option'] == 2) ? 'block' : 'none') . '"><input class="regular-text" name="domains[]" type="text" value="'.stripslashes($default_redirect['domain']).'"/></p>'. 152 '<p id="redirect_option_value_' . $default_redirect['country_id'] . '_3" style="display:' . (($default_redirect['redirect_option'] == 3) ? 'block' : 'none') . '"><input class="regular-text" name="urls[]" type="text" value="'.stripslashes($default_redirect['url']).'"/></p>'. 153 '</td>'. 154 '<td>'. 155 '</td>'. 156 '</tr>'; 157 153 158 $html .= '</tbody> 154 159 </table> … … 166 171 <tr> 167 172 <td> 168 <p><input class="small-text" name="lang_slug" value="'.$lang_slug.'" type="text" >'.169 ' (example: '.get_ bloginfo('url').'/?page_id=10&<b>lang</b>=en)</p>173 <p><input class="small-text" name="lang_slug" value="'.$lang_slug.'" type="text"/>'. 174 ' (example: '.get_home_url().'/?page_id=10&<b>lang</b>=en)</p> 170 175 </td> 171 176 </tr> … … 174 179 175 180 $html .= '<br clear="all" />'; 176 177 $checked = ($only_outsite == 1)?'checked="checked"':''; 178 $html .= '<label><input type="checkbox" name="only_outsite" value="1" '.$checked.'> Redirect only visitors who came from another site by link</label>'; 179 181 182 $html .= '<label><input type="checkbox" name="only_outsite" value="1" ' . (($only_outsite == 1) ? 'checked="checked"' : '' ) . '/> Redirect only visitors who come from another site by link</label> <b style="cursor:help" title="This means your pages will be always accessible by direct link entered in browser, but clients that come, for example, from google.com will be redirected according to installed parameters">(?)</b>'; 183 184 $html .= '<br clear="all" />'; 185 186 $html .= '<label><input type="checkbox" name="only_root" value="1" ' . (($only_root == 1) ? 'checked="checked"' : '' ) . '/> Redirect only visitors of the site\'s root</label> <b style="cursor:help" title="Redirect options will be considered only if visitor is on ' . get_home_url() . ' page">(?)</b>'; 187 180 188 $html .= ' <p class="submit"> 181 189 <input type="submit" name="submit" class="button-primary" value="Save Changes"> … … 190 198 } 191 199 192 function geo_redirect_javascript(){ 200 function geo_redirect_javascript() { 201 202 $site_url_parsed = parse_url(get_home_url()); 193 203 ?> 194 204 <script type="text/javascript"> 195 205 var j = jQuery; 196 197 function geoAddCountry(){ 198 var exist = false; 199 var country_id = j('select.countries').val(); 200 country_id = parseInt(country_id, 10); 201 if (country_id == 0 || isNaN(country_id)) 202 return false; 203 204 205 j('.geo-redirect-options input[name="country_ids[]"]').each(function(index) { 206 if (j(this).val() == country_id) { 207 exist = true; 208 j('#geo-redirect-country-'+country_id).focus(); 209 } 210 }); 211 if (exist === true) 212 return false; 213 214 var country_name = j('select.countries option:selected').text(); 215 var option_html = '<tr class="geo-redirect-option inactive">'+ 216 '<td>'+ 217 '<input type="hidden" name="country_ids[]" value="'+country_id+'">'+ 218 '<p><b>'+country_name+'</b></p>'+ 219 '</td>'+ 220 '<td>'+ 221 '<p><input id="geo-redirect-country-'+country_id+'" class="small-text" name="lang_codes[]" type="text" maxlength="2"></p>'+ 222 '</td>'+ 223 '<td>'+ 224 '<p><input class="regular-text" name="domains[]" type="text"></p>'+ 225 '</td>'+ 226 '<td>'+ 227 '<p><input class="regular-text" name="urls[]" type="text"></p>'+ 228 '</td>'+ 229 '<td>'+ 230 '<p style="line-height:2.3"><a onclick="return geoRemoveCountry(this);" href="#" class="delete">Remove</a></p>'+ 231 '</td>'+ 232 '</tr>'; 233 j('.geo-redirect-options table tbody').prepend(option_html); 234 setTimeout(function() { 235 var first = j('.geo-redirect-option').first(); 236 first.addClass('active'); 237 first.removeClass('inactive'); 238 },500); 239 return false; 240 } 241 242 function geoRemoveCountry(option){ 243 j(option).parents('.geo-redirect-option').addClass('inactive'); 244 setTimeout(function() { 245 j(option).parents('.geo-redirect-option').remove(); 246 },500); 247 248 return false; 249 } 206 var geoRedirect = { 207 208 url_scheme: '<?php echo $site_url_parsed['scheme']; ?>', 209 domain_url: '<?php echo $site_url_parsed['host']; ?>', 210 home_url: '<?php echo get_home_url(); ?>', 211 212 addCountry: function() 213 { 214 var exist = false; 215 var country_id = j('select.countries').val(); 216 country_id = parseInt(country_id, 10); 217 if (country_id == 0 || isNaN(country_id)) 218 return false; 219 220 j('.geo-redirect-options input[name="country_ids[]"]').each(function(index) { 221 if (j(this).val() == country_id) { 222 exist = true; 223 j('#redirect_options_container_'+country_id).parents('.geo-redirect-option').addClass('inactive'); 224 setTimeout(function() { 225 j('#redirect_options_container_'+country_id).parents('.geo-redirect-option').removeClass('inactive'); 226 },1000); 227 228 j('#redirect_options_container_'+country_id).parents('.geo-redirect-option').find('select.redirect_options').focus(); 229 } 230 }); 231 if (exist === true) 232 return false; 233 234 var country_name = j('select.countries option:selected').text(); 235 var lang_code = j('select.countries option:selected').attr('data-lang'); 236 var country_code = j('select.countries option:selected').attr('data-country-code'); 237 var option_html = '<tr class="geo-redirect-option inactive">'+ 238 '<td>'+ 239 '<input type="hidden" name="country_ids[]" value="'+country_id+'"/>'+ 240 '<p><b>'+country_name+'</b></p>'+ 241 '</td>'+ 242 '<td>'+ 243 '<p><select class="redirect_options" name="redirect_options[]" onchange="geoRedirect.switchOption(this);">'+ 244 '<option value="1" >Language Code</option>'+ 245 '<option value="2" >Domain Name</option>'+ 246 '<option value="3" >Static URL</option>'+ 247 '</select></p>'+ 248 '</td>'+ 249 '<td id="redirect_options_container_'+country_id+'">'+ 250 '<p id="redirect_option_value_'+country_id+'_1" style="display:block"><input class="small-text" name="lang_codes[]" type="text" maxlength="3" value="'+lang_code+'"/> <label title="Redirect to '+geoRedirect.home_url+'/'+lang_code+'/ instead of '+geoRedirect.home_url+'/?lang='+lang_code+'">Use pretty permalink<input type="checkbox" name="pretties[]" value="'+country_id+'" ></label></p>'+ 251 '<p id="redirect_option_value_'+country_id+'_2" style="display:none"><input class="regular-text" name="domains[]" type="text" value="'+geoRedirect.url_scheme+'://'+country_code+'.'+geoRedirect.domain_url+'"/></p>'+ 252 '<p id="redirect_option_value_'+country_id+'_3" style="display:none"><input class="regular-text" name="urls[]" type="text" value="'+geoRedirect.home_url+'/'+country_code+'_visitors_sample_page/"/></p>'+ 253 '</td>'+ 254 '<td>'+ 255 '<p style="line-height:2.3"><a onclick="return geoRedirect.removeCountry(this);" href="#" class="delete">Remove</a></p>'+ 256 '</td>'+ 257 '</tr>'; 258 259 j('.geo-redirect-options table tbody').prepend(option_html); 260 261 setTimeout(function() { 262 j('.geo-redirect-option').first().addClass('active').removeClass('inactive'); 263 },500); 264 265 return false; 266 267 }, 268 269 clearCountry: function(option){ 270 var inputs = j(option).parents('.geo-redirect-option').find('input:visible'); 271 j(inputs).each(function(){ 272 j(this).val(''); 273 }); 274 return false; 275 }, 276 277 removeCountry: function(option) 278 { 279 j(option).parents('.geo-redirect-option').addClass('inactive'); 280 setTimeout(function() { 281 j(option).parents('.geo-redirect-option').remove(); 282 },500); 283 284 return false; 285 286 }, 287 288 switchOption: function(select){ 289 var option_id = j(select).val(); 290 var country_id = j(select).parents('.geo-redirect-option').find('input[name="country_ids[]"]').val(); 291 j('#redirect_options_container_'+country_id+' p').hide(); 292 j('#redirect_option_value_'+country_id+'_'+option_id).show(); 293 294 295 } 296 297 } 250 298 </script> 251 299 <?php … … 255 303 function geo_redirect_save(){ 256 304 257 $country_ids = $_POST['country_ids']; 258 $lang_codes = $_POST['lang_codes']; 259 $domains = $_POST['domains']; 260 $urls = $_POST['urls']; 261 $only_outsite = intval($_POST['only_outsite']); 262 $lang_slug = (trim($_POST['lang_slug']) != '') ? (string)urlencode(strtolower(trim($_POST['lang_slug']))) : 'lang'; 263 if (is_array($country_ids)) { 305 $country_ids = (array) $_POST['country_ids']; 306 $redirect_options = (array) $_POST['redirect_options']; 307 $lang_codes = (array) $_POST['lang_codes']; 308 $pretties = (array) $_POST['pretties']; 309 $domains = (array) $_POST['domains']; 310 $urls = (array) $_POST['urls']; 311 $only_outsite = intval($_POST['only_outsite']); 312 $only_root = intval($_POST['only_root']); 313 $lang_slug = (trim($_POST['lang_slug']) != '') ? (string)urlencode(strtolower(trim($_POST['lang_slug']))) : 'lang'; 314 if (count($country_ids) > 0) { 264 315 $redirect = array(); 265 316 foreach ($country_ids as $key => $country_id) { 266 267 $redirect[] = array('country_id' => intval($country_id), 268 'lang_code' => (string)htmlspecialchars(strtolower(trim(strip_tags($lang_codes[$key])))), 269 'domain' => (string)htmlspecialchars(strtolower(trim(str_ireplace('http://','',strip_tags($domains[$key]))))), 270 'url' => (string)htmlspecialchars(trim(strip_tags($urls[$key]))) ); 317 318 $domain = (string) htmlspecialchars( strtolower( rtrim( trim( strip_tags( $domains[$key] ) ),'/') ) ); 319 if ($domain != '') { 320 $domain_url_parsed = parse_url($domain); 321 $domain = $domain_url_parsed['scheme'] . '://' . $domain_url_parsed['host']; 322 } 323 324 $redirect[] = array('country_id' => intval($country_id), 325 'redirect_option' => intval($redirect_options[$key]), 326 'lang_code' => (string) htmlspecialchars( strtolower( trim( strip_tags( $lang_codes[$key] ) ) ) ), 327 'pretty' => (in_array(intval($country_id),$pretties))?1:0, 328 'domain' => $domain, 329 'url' => (string) htmlspecialchars( trim( strip_tags( $urls[$key] ) ) ) ); 271 330 272 331 } … … 278 337 $data = array( 'redirect' => $redirect, 279 338 'only_outsite' => $only_outsite, 339 'only_root' => $only_root, 280 340 'lang_slug' => $lang_slug ); 281 341 -
geographical-redirect/trunk/geo-redirect.php
r431960 r480258 7 7 Author URI: http://profiles.wordpress.org/users/Ladrower/ 8 8 Author e-mail: ladrower@gmail.com 9 Version: 2.09 Version: 3.0 10 10 License: Free 11 11 */ … … 16 16 17 17 class Geo_Redirect{ 18 p ublic$ip;18 private $ip; 19 19 private $gi; 20 p ublic$country_code;21 p ublic$country_id;22 p ublic$geo_redirect_data;23 p ublic$site_lang;24 p ublic$lang_slug;25 p ublic $site_domain;20 private $country_code; 21 private $country_id; 22 private $geo_redirect_data; 23 private $site_lang; 24 private $lang_slug; 25 private $site_url; 26 26 private $no_redirect; 27 27 private $referer; … … 31 31 $this->ip = $_SERVER['REMOTE_ADDR']; 32 32 $this->gi = geoip_open( dirname(__FILE__) . "/geoip/ipdatabase/GeoIP.dat/GeoIP.dat", GEOIP_STANDARD); 33 $this->site_domain = $_SERVER['SERVER_NAME']; 33 $this->site_url = get_home_url(); 34 $this->request_uri = $this->getRequestUri(); 34 35 $this->no_redirect = (isset($_GET['no_redirect']) || (isset($_POST['pwd']) && isset($_POST['log']))) ? true : false; 35 36 $this->referer = $_SERVER['HTTP_REFERER']; … … 38 39 $this->getSiteLang(); 39 40 } 41 42 private function getRequestUri() 43 { 44 if (!$this->site_url) 45 $this->site_url = get_home_url(); 46 47 $site_url_parsed = parse_url($this->site_url); 48 $site_root = $site_url_parsed['scheme'] . '://' . $site_url_parsed['host']; 49 $uri = str_ireplace($this->site_url, '', $site_root . $_SERVER['REQUEST_URI']); 50 51 return $uri; 52 } 40 53 41 54 public function getCountryCode() … … 47 60 public function getCountryId() 48 61 { 49 $this->country_id = geoip_country_id_by_addr($this->gi,$this->ip); 62 $this->country_id = geoip_country_id_by_addr($this->gi,$this->ip); 50 63 return $this->country_id; 51 64 } … … 67 80 return $this->site_lang; 68 81 } 69 70 public function checkIfRedirectNeeded() 71 { 72 $this->getCountryId(); 73 74 if (empty($this->country_id)) 75 return false; 76 82 83 public function getAllLangCodes() 84 { 85 $geoip = new GeoIP(); 86 $codes = $geoip->GEOIP_LANG_CODES; 87 array_shift($codes); 88 if (is_array($this->geo_redirect_data['redirect'])) { 89 foreach ($this->geo_redirect_data['redirect'] as $data) { 90 if ($data['lang_code'] != '') 91 array_push($codes, strtoupper($data['lang_code'])); 92 } 93 } 94 return array_unique($codes, SORT_STRING); 95 } 96 97 private function selectRedirectOption($data) 98 { 99 switch ($data['redirect_option']) { 100 case 1: 101 if ($data['lang_code'] != '') { 102 if ($data['pretty'] == 1) { 103 if (stripos($this->site_url . $this->request_uri, $this->site_url . '/' . $data['lang_code'] . '/') === false) { 104 $queries = explode('?', $this->request_uri, 2); 105 $query = (isset($queries[1])) ? '?' . $queries[1] : ''; 106 $paths = explode('/', ltrim($queries[0],'/')); 107 if (in_array(strtoupper($paths[0]), $this->getAllLangCodes())) 108 array_shift($paths); 109 $uri = '/' . implode('/',$paths) . $query; 110 111 $this->redirectByUrl(home_url($data['lang_code']) . $uri); 112 } 113 } elseif ($this->site_lang != $data['lang_code']) 114 $this->redirectByLang($data['lang_code']); 115 } 116 break; 117 case 2: 118 if ($data['domain'] != '' && $this->site_url != $data['domain']) 119 $this->redirectByDomain($data['domain']); 120 break; 121 case 3: 122 if ($data['url'] != '') 123 $current_url = $this->site_url . $this->request_uri; 124 if ($current_url != $data['url']) 125 $this->redirectByUrl($data['url']); 126 break; 127 } 128 129 } 130 131 public function checkIfRedirectNeeded() 132 { 77 133 if (!is_array($this->geo_redirect_data)) 78 return false; 134 return; 135 136 if ($this->no_redirect) 137 return; 138 139 if ($this->checkReferer()) 140 return; 141 142 if ($this->checkRoot()) 143 return; 79 144 80 145 if (is_array($this->geo_redirect_data['redirect'])) { 81 $country_found = false; 146 147 $this->getCountryId(); 148 82 149 foreach($this->geo_redirect_data['redirect'] as $data) { 83 if ($data['country_id'] == $this->country_id) { 84 $country_found = true; 85 $lang_code = $data['lang_code']; 86 $domain = $data['domain']; 87 $url = $data['url']; 88 if (!empty($url)) { 89 $current_url = 'http://' . $this->site_domain . $_SERVER['REQUEST_URI']; 90 if ($current_url != $url) 91 $this->redirectByUrl($url); 92 93 } elseif (!empty($domain)) { 94 if ($this->site_domain != $domain) 95 $this->redirectByDomain($domain); 96 97 } elseif(!empty($lang_code)) { 98 if ($this->site_lang != $lang_code) 99 $this->redirectByLang($lang_code); 100 } 101 break; 102 } 103 if ($data['country_id'] == -1) 150 151 if ($this->country_id == $data['country_id']) { 152 153 $this->selectRedirectOption($data); 154 return; 155 156 } elseif (-1 == $data['country_id']) 104 157 $default_data = $data; 105 158 106 159 } 107 if (!$country_found && !empty($default_data)) { 108 $lang_code = $default_data['lang_code']; 109 $domain = $default_data['domain']; 110 $url = $default_data['url']; 111 if (!empty($url)) { 112 if ('http://' . $this->site_domain . $_SERVER['REQUEST_URI'] != $url) 113 $this->redirectByUrl($url); 114 115 } elseif (!empty($domain)) { 116 if ($this->site_domain != $domain) 117 $this->redirectByDomain($domain); 118 119 } elseif(!empty($lang_code)) { 120 if ($this->site_lang != $lang_code) 121 $this->redirectByLang($lang_code); 122 } 123 } 160 161 if (!empty($default_data)) 162 $this->selectRedirectOption($default_data); 124 163 125 164 } … … 137 176 { 138 177 if ($domain != ''){ 139 $to = 'http://' . $domain . $_SERVER['REQUEST_URI'];178 $to = $domain . $this->request_uri; 140 179 $this->redirectTo($to); 141 180 } … … 145 184 { 146 185 if ($lang_code != ''){ 147 $lang_url = ( strpos($_SERVER['REQUEST_URI'],'?') === false) ? '?' : '&';148 $to = get_bloginfo('url') . $_SERVER['REQUEST_URI'] . $lang_url . $this->lang_slug . '=' . $lang_code;186 $lang_url = ((strpos($this->request_uri,'?') === false) ? '?' : '&') . $this->lang_slug . '=' . urlencode($lang_code); 187 $to = $this->site_url . $this->request_uri . $lang_url; 149 188 $this->redirectTo($to); 150 189 } 151 190 } 191 192 private function deepReplace( $search, $subject ) { 193 $found = true; 194 $subject = (string) $subject; 195 while ( $found ) { 196 $found = false; 197 foreach ( (array) $search as $val ) { 198 while ( strpos( $subject, $val ) !== false ) { 199 $found = true; 200 $subject = str_replace( $val, '', $subject ); 201 } 202 } 203 } 204 205 return $subject; 206 } 207 208 private function sanitizeRedirect($location) { 209 $location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%!]|i', '', $location); 210 $location = wp_kses_no_null($location); 211 212 // remove %0d and %0a from location 213 $strip = array('%0d', '%0a', '%0D', '%0A'); 214 $location = $this->deepReplace($strip, $location); 215 return $location; 216 } 152 217 153 218 private function redirectTo($to) 154 219 { 155 if ($this->no_redirect) 156 return false; 157 158 if ($this->checkReferer()) 159 return false; 160 161 header("Location: " . $to); 220 $to = $this->sanitizeRedirect($to); 221 header("Location: " . $to); 162 222 exit; 163 223 } 224 225 private function checkRoot() 226 { 227 $only_root = (isset($this->geo_redirect_data['only_root'])) ? $this->geo_redirect_data['only_root'] : 0; 228 if ($only_root == 1) { 229 if (trim($this->request_uri,'/') != '') 230 return true; 231 } 232 return false; 233 } 164 234 165 235 private function checkReferer() 166 236 { 167 if (is_array($this->geo_redirect_data)) 168 $only_outsite = $this->geo_redirect_data['only_outsite']; 169 237 $only_outsite = (isset($this->geo_redirect_data['only_outsite'])) ? $this->geo_redirect_data['only_outsite'] : 0; 238 170 239 if ($only_outsite == 1) { 171 240 if (empty($this->referer)) 172 241 return true; 173 174 $ outsite = parse_url($this->referer);175 $domain = $outsite['host'];176 if ($ this->site_domain != $domain)242 243 $insite = parse_url($this->site_url); 244 $outsite = parse_url($this->referer); 245 if ($insite['scheme'] . '://' . $insite['host'] != $outsite['scheme'] . '://' . $outsite['host']) 177 246 return false; 178 247 … … 195 264 196 265 if (!is_admin()) 197 do_action('check_client_location'); 266 do_action('check_client_location'); 198 267 199 268 ?> -
geographical-redirect/trunk/geoip/geoip.inc
r431665 r480258 124 124 "MF" => 252 125 125 ); 126 127 var $GEOIP_LANG_CODES = array( 128 "", "", "", "CA", "AR", 129 "FA", "EN", "EN", "SQ", "HY", 130 "EN", "PT", "EN", "ES", "SM", 131 "DE", "EN", "NL", "AZ", "BS", 132 "EN", "BN", "NL", "FR", "BG", "AR", 133 "FR", "FR", "EN", "MS", "ES", "PT", 134 "EN", "DZ", "NO", "EN", "BE", "EN", 135 "EN", "EN", "FR", 136 "FR", "SW", "DE", "FR", "RAR", 137 "ES", "FR", "ZH", "ES", "ES", "ES", "PT", 138 "EN", "EL", "CS", "DE", "FR", 139 "DA", "EN", "ES", "AR", "ES", "ET", 140 "AR", "ES", "TI", "ES", "AM", "FI", "FJ", 141 "EN", "EN", "FO", 142 "FR", "FR", "FR", "EN", 143 "EN", "KA", "FR", "EN", "EN", "KL", 144 "EN", "FR", "FR", "ES", "EL", "EN", 145 "ES", "CH", "PT", 146 "EN", "ZH", "EN", "ES", 147 "HR", "FR", "HU", "IN", "GA", "HE", "HI", 148 "EN", "AR", "FA", 149 "IS", "IT", "EN", "AR", "JA", "SW", "RU", 150 "KM", "EN", "AR", "EN", "KO", 151 "KO", "AR", "EN", 152 "KK", "LO", "AR", "EN", 153 "DE", "SI", "EN", "EN", "LT", "LB", 154 "LV", "AR", "AR", "FR", "MO", 155 "MG", "EN", "MK", 156 "FR", "MY", "MN", "ZH", "EN", 157 "FR", "AR", "EN", "EN", "EN", "DV", 158 "EN", "ES", "ZLM", "PT", "EN", "FR", 159 "FR", "EN", "EN", "ES", "NL", "NO", 160 "NE", "EN", "EN", "EN", "AR", "ES", "ES", "FR", 161 "EN", "FL", "UR", "PL", "FR", 162 "EN", "ES", "AR", 163 "PT", "PAU", "ES", "AR", "FR", "RO", 164 "RU", "RW", "AR", "EN", 165 "FR", "AR", "SV", "MS", "EN", "SL", 166 "NO", "SK", "EN", "IT", "FR", 167 "SO", "NL", "PT", "ES", "AR", 168 "SS", "EN", "FR", "FR", 169 "FR", "TH", "TG", "TKL", "TK", 170 "AR", "EN", "TET", "TR", "EN", "TVL", 171 "ZH", "SW", "UK", 172 "SW", "EN", "EN", "ES", 173 "UZ", "IT", "EN", 174 "ES", "EN", "EN", 175 "VI", "BI", "FR", "SM", "AR", "FR", 176 "SR", "AF", "EN", "SRP", "EN", 177 "EN","EN","EN", 178 "SV","EN","EN","EN","FR","FR" 179 ); 180 181 126 182 var $GEOIP_COUNTRY_CODES = array( 127 183 "", "AP", "EU", "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN", "AO", "AQ", -
geographical-redirect/trunk/readme.txt
r464386 r480258 4 4 Tags: geo-redirect, multilanguage, multidomain 5 5 Requires at least: 3.0 6 Tested up to: 3. 2.17 Stable tag: 2.06 Tested up to: 3.3 7 Stable tag: 3.0 8 8 9 9 This plugin allows you to redirect your visitors or switch language according to their country. … … 12 12 13 13 Just add the country from selectbox and fill in preferable options. 14 You can specify URL parameters for switching language or another domain. 15 Domain redirect has higher priority if specified. 16 17 You can also type static URL which in contrast to domain option will not just change the domain name, but redirect to certain page. Static URL option has the highest priority if not blank. 14 You can specify URL parameters for switching language or domain. 18 15 19 16 ***Explanation***: … … 43 40 == Changelog == 44 41 42 = 3.0 = 43 * Improved, more flexible logic 44 * Added new options 45 * Simpler user interface 46 45 47 = 2.0 = 46 48 * New stable improved release.
Note: See TracChangeset
for help on using the changeset viewer.