Plugin Directory

Changeset 480258


Ignore:
Timestamp:
12/24/2011 07:23:33 PM (14 years ago)
Author:
Ladrower
Message:

Version 3.0 released

Location:
geographical-redirect/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • geographical-redirect/trunk/geo-redirect-admin.php

    r431960 r480258  
    1414    $html = '<p>This plugin allows you to redirect your visitors according to their country.</p>
    1515             <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>
    1918             <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>';
    2120   
    2221    return $html;   
    2322}
    2423
     24function 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}
    2530
    2631function geo_redirect_admin_page_display(){
     
    3540    <div class="wrap">
    3641    <div id="icon-options-general" class="icon32"><br></div>
    37     <h2>' . __('Geographical Redirect Options') . '</h2>
     42    <h2>Geographical Redirect Options</h2>
    3843    <form action="" method="post" enctype="multipart/form-data">';
    3944   
     
    4247    $redirect = '';
    4348    $only_outsite = 0;
     49    $only_root = 0;
    4450    $lang_slug = 'lang';
    4551   
     
    5056        $redirect = $geo_redirect_data['redirect'];
    5157        $only_outsite = $geo_redirect_data['only_outsite'];
     58        $only_root = $geo_redirect_data['only_root'];
    5259        $lang_slug = $geo_redirect_data['lang_slug'];
    5360    }
     
    5562    $geoip = new GeoIP();
    5663    $countries = $geoip->GEOIP_COUNTRY_NAMES;
     64    $country_codes = $geoip->GEOIP_COUNTRY_CODES;
     65    $lang_codes = $geoip->GEOIP_LANG_CODES;
    5766    if (is_array($countries)) :
    5867        $html .= '<div class="tablenav top">';
     
    6170            foreach ($countries as $country_id => $country) :
    6271                if ($country_id == 0)
    63                     $html .= '<option value="' . $country_id . '">' . __('Select country') . '</option>';
     72                    $html .= '<option value="' . $country_id . '">Select country</option>';
    6473                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>';
    6675            endforeach;
    6776        $html .= '</select>';
    68         $html .= '<input onclick="return geoAddCountry();" 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" />';
    6978        $html .= '</div></div><br clear="all" />';
    7079    endif;
    71    
    72     $html .= '<div class="geo-redirect-options">
     80
     81
     82    $html .= '<div class="geo-redirect-options">
    7383    <table class="wp-list-table widefat plugins" cellspacing="0">
    7484        <thead>
    7585            <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>
    8190            </tr>
    8291        </thead>
    8392        <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)) {
    86102        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
    128108            }
    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']).'"/>&nbsp;' . 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']).'"/>&nbsp;' . 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
    153158    $html .= '</tbody>
    154159        </table>
     
    166171                    <tr>
    167172                        <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>
    170175                        </td>
    171176                    </tr>
     
    174179   
    175180    $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>&nbsp;<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>&nbsp;<b style="cursor:help" title="Redirect options will be considered only if visitor is on ' . get_home_url() . ' page">(?)</b>';
     187
    180188    $html .= '  <p class="submit">
    181189                    <input type="submit" name="submit" class="button-primary" value="Save Changes">
     
    190198    }
    191199   
    192 function geo_redirect_javascript(){
     200function geo_redirect_javascript() {
     201
     202    $site_url_parsed = parse_url(get_home_url());
    193203    ?>
    194204    <script type="text/javascript">
    195205    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+'"/>&nbsp;<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    }
    250298    </script>   
    251299    <?php
     
    255303function geo_redirect_save(){
    256304   
    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) {
    264315        $redirect = array();
    265316        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] ) ) ) );
    271330           
    272331        }
     
    278337    $data = array(  'redirect'      => $redirect,
    279338                    'only_outsite'  => $only_outsite,
     339                    'only_root'     => $only_root,
    280340                    'lang_slug'     => $lang_slug );
    281341                   
  • geographical-redirect/trunk/geo-redirect.php

    r431960 r480258  
    77Author URI: http://profiles.wordpress.org/users/Ladrower/
    88Author e-mail: ladrower@gmail.com
    9 Version: 2.0
     9Version: 3.0
    1010License: Free
    1111*/
     
    1616
    1717class Geo_Redirect{
    18     public  $ip;
     18    private $ip;
    1919    private $gi;
    20     public $country_code;
    21     public $country_id;
    22     public  $geo_redirect_data;
    23     public $site_lang;
    24     public $lang_slug;
    25     public  $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;
    2626    private $no_redirect;
    2727    private $referer;
     
    3131        $this->ip = $_SERVER['REMOTE_ADDR'];
    3232        $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();
    3435        $this->no_redirect = (isset($_GET['no_redirect']) || (isset($_POST['pwd']) && isset($_POST['log']))) ? true : false;
    3536        $this->referer = $_SERVER['HTTP_REFERER'];
     
    3839        $this->getSiteLang();
    3940    }
     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    }
    4053   
    4154    public function getCountryCode()
     
    4760    public function getCountryId()
    4861    {
    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);
    5063        return $this->country_id;
    5164    }
     
    6780        return $this->site_lang;
    6881    }
    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    {
    77133        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;
    79144       
    80145        if (is_array($this->geo_redirect_data['redirect'])) {
    81             $country_found = false;
     146
     147            $this->getCountryId();
     148
    82149            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'])
    104157                    $default_data = $data;
    105158       
    106159            }
    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);
    124163           
    125164        }
     
    137176    {
    138177        if ($domain != ''){
    139             $to = 'http://' . $domain . $_SERVER['REQUEST_URI'];
     178            $to = $domain . $this->request_uri;
    140179            $this->redirectTo($to);
    141180        }
     
    145184    {
    146185        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;
    149188            $this->redirectTo($to);
    150189        }
    151190    }
     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    }
    152217   
    153218    private function redirectTo($to)
    154219    {
    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);
    162222        exit;
    163223    }
     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    }
    164234   
    165235    private function checkReferer()
    166236    {
    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
    170239        if ($only_outsite == 1) {
    171240            if (empty($this->referer))
    172241                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'])
    177246                return false;
    178247               
     
    195264
    196265if (!is_admin())
    197     do_action('check_client_location'); 
     266    do_action('check_client_location');
    198267
    199268?>
  • geographical-redirect/trunk/geoip/geoip.inc

    r431665 r480258  
    124124"MF" => 252
    125125);
     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
    126182    var $GEOIP_COUNTRY_CODES = array(
    127183"", "AP", "EU", "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN", "AO", "AQ",
  • geographical-redirect/trunk/readme.txt

    r464386 r480258  
    44Tags: geo-redirect, multilanguage, multidomain
    55Requires at least: 3.0
    6 Tested up to: 3.2.1
    7 Stable tag: 2.0
     6Tested up to: 3.3
     7Stable tag: 3.0
    88
    99This plugin allows you to redirect your visitors or switch language according to their country.
     
    1212
    1313Just 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.
     14You can specify URL parameters for switching language or domain.
    1815
    1916***Explanation***:
     
    4340== Changelog ==
    4441
     42= 3.0 =
     43* Improved, more flexible logic
     44* Added new options
     45* Simpler user interface
     46
    4547= 2.0 =
    4648* New stable improved release.
Note: See TracChangeset for help on using the changeset viewer.