Guest User

TYPO3 IP forwarding to a FE language

a guest
Aug 5th, 2010
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.60 KB | None | 0 0
  1. <?php
  2.  
  3.     class user_301redirect {
  4.    
  5.         var $enable = 1; // enable or disable redirect
  6.         var $debugmode = 0; // enable or disable debugmode (redirect or only print of the new url)
  7.         var $languageForRobots = 'en'; // language for visiting robot (former de)
  8.         var $index2html = 1; // domain.de/page/index.html => domain.de/page.html redirect
  9.         var $useGeoIpInsteadOfBrowserlang = 1; // Deactivate browser lang detection and activate from geoip
  10.         var $geoDBpath = 'fileadmin/conject/administration/scripts/GeoLiteCity.dat'; // path to geoip database
  11.        
  12.         // Test some ip addresses
  13.             // DEU: 83.236.183.130
  14.             // AUT: 213.235.230.170
  15.             // UAE: 195.229.237.36
  16.             // RUS: 89.113.208.15
  17.             // CHE: 80.74.157.3
  18.             // POL: 80.54.62.213
  19.             // CZE: 83.208.36.242
  20.             // SLO: 90.157.147.182
  21.             // BEL: 62.88.83.151
  22.             // ROU: 217.10.222.250
  23.             // HRV: 195.29.117.50
  24.             // ZAF: 196.21.143.253
  25.             // CHN: 202.203.40.2
  26.             // GRC: 195.66.111.34
  27.             // EGY: 41.196.84.138
  28.         var $ip = ''; // Fill only if you want to test an especially IP address
  29.        
  30.         var $fallback = array ( // fallback language if no language could be found (first: fallback for browserlang / second: fallback for geoip)
  31.             'en',
  32.             'en'
  33.         );
  34.        
  35.         var $languages = array ( // L=0 => en, L=1 => de (Attention: the first is default if no defined language read!)
  36.             0 => 'en',
  37.             1 => 'de',
  38.             2 => 'es',
  39.             3 => 'pt',
  40.             4 => 'ru',
  41.             5 => 'me',
  42.             6 => 'bg',
  43.             7 => 'ro',
  44.             8 => 'hr',
  45.             9 => 'fr',
  46.             10 => 'nl',
  47.             11 => 'tr',
  48.             12 => 'pl',
  49.             13 => 'cz',
  50.             14 => 'it',
  51.             15 => 'ar',
  52.             17 => 'at',
  53.             18 => 'za',
  54.             19 => 'sg',
  55.             20 => 'eg'
  56.         );
  57.        
  58.         var $geoIPcountries = array ( // Set relation between countries and languages
  59.             'de' => array ( // German
  60.                 'de',
  61.                 'ch' => array (
  62.                     'de' => 'de-ch',
  63.                     'de' => 'de',
  64.                     'en' => 'en',
  65.                     'en' => 'en-ch',
  66.                     'fr' => 'fr',
  67.                     'fr' => 'fr-ch',
  68.                     'it' => 'it-ch'
  69.                 )
  70.             ),
  71.             'at' => array ( // Austria
  72.                 'at'
  73.             ),
  74.             'ru' => array ( // Russia
  75.                  'az',
  76.                  'by',
  77.                  'kz',
  78.                  'kg',
  79.                  'md',
  80.                  'ru',
  81.                  'tj',
  82.                  'tm',
  83.                  'ua',
  84.                  'uz'
  85.             ),
  86.             'za' => array ( // South Africa
  87.                 'za'
  88.             ),
  89.             'me' => array ( // Middle East for Africa, Asia and
  90.                 'dz',
  91.                 'ao',
  92.                 'bj',
  93.                 'bw',
  94.                 'bf',
  95.                 'bi',
  96.                 'cm',
  97.                 'cv',
  98.                 'cf',
  99.                 'td',
  100.                 'km',
  101.                 'cd',
  102.                 'cg',
  103.                 'ci',
  104.                 'dj',
  105.                 'gq',
  106.                 'er',
  107.                 'et',
  108.                 'ga',
  109.                 'gm',
  110.                 'gh',
  111.                 'gn',
  112.                 'gw',
  113.                 'ke',
  114.                 'ls',
  115.                 'lr',
  116.                 'ly',
  117.                 'mg',
  118.                 'mw',
  119.                 'ml',
  120.                 'mr',
  121.                 'mu',
  122.                 'yt',
  123.                 'ma',
  124.                 'mz',
  125.                 'na',
  126.                 'ne',
  127.                 'ng',
  128.                 're',
  129.                 'rw',
  130.                 'sh',
  131.                 'st',
  132.                 'sn',
  133.                 'sc',
  134.                 'sl',
  135.                 'so',
  136.                 'sd',
  137.                 'sz',
  138.                 'tz',
  139.                 'tg',
  140.                 'tn',
  141.                 'ug',
  142.                 'eh',
  143.                 'zm',
  144.                 'zw',
  145.                 'aq',
  146.                 'bv',
  147.                 'tf',
  148.                 'hm',
  149.                 'gs',
  150.                 'af',
  151.                 'am',
  152.                 'az',
  153.                 'bh',
  154.                 'io',
  155.                 'cx',
  156.                 'cc',
  157.                 'cy',
  158.                 'ge',
  159.                 'in',
  160.                 'ir',
  161.                 'iq',
  162.                 'il',
  163.                 'jo',
  164.                 'kz',
  165.                 'kp',
  166.                 'kw',
  167.                 'kg',
  168.                 'lb',
  169.                 'my',
  170.                 'mv',
  171.                 'mm',
  172.                 'np',
  173.                 'om',
  174.                 'pk',
  175.                 'ps',
  176.                 'qa',
  177.                 'sa',
  178.                 'lk',
  179.                 'sy',
  180.                 'tj',
  181.                 'tr',
  182.                 'tm',
  183.                 'ae',
  184.                 'uz',
  185.                 'ye',
  186.                 'as',
  187.                 'ck',
  188.                 'fj',
  189.                 'pf',
  190.                 'gu',
  191.                 'ki',
  192.                 'mh',
  193.                 'fm',
  194.                 'nr',
  195.                 'nc',
  196.                 'nu',
  197.                 'nf',
  198.                 'mp',
  199.                 'pw',
  200.                 'pg',
  201.                 'pn',
  202.                 'ws',
  203.                 'sb',
  204.                 'tk',
  205.                 'to',
  206.                 'tv',
  207.                 'um',
  208.                 'vu',
  209.                 'wf'
  210.             ),
  211.             'eg' => array ( // Egypt
  212.                 'eg'
  213.             ),
  214.             'sg' => array ( // Singapore
  215.                 'bn',
  216.                 'cn',
  217.                 'hk',
  218.                 'id',
  219.                 'jp',
  220.                 'kh',
  221.                 'la',
  222.                 'mo',
  223.                 'my',
  224.                 'mn',
  225.                 'tl',
  226.                 'ph',
  227.                 'sg',
  228.                 'kr',
  229.                 'tw',
  230.                 'th',
  231.                 'vn',
  232.                 'bd',
  233.                 'bt',
  234.                 'au',
  235.                 'nz'   
  236.             ),
  237.             'es' => array ( // Spain
  238.                 'es',
  239.                 'mx',
  240.                 'cu',
  241.                 'do',
  242.                 'pr',
  243.                 'bz',
  244.                 'cr',
  245.                 'sv',
  246.                 'gt',
  247.                 'hn',
  248.                 'ni',
  249.                 'pa',
  250.                 'ar',
  251.                 'bo',
  252.                 'cl',
  253.                 'co',
  254.                 'ec',
  255.                 'py',
  256.                 'pe',
  257.                 'uy',
  258.                 've'
  259.             ),
  260.             'bg' => array ( // Bulgaria
  261.                 'bg'
  262.             ),
  263.             'ro' => array ( // Romania
  264.                 'ro'
  265.             ),
  266.             'hr' => array ( // Croatia
  267.                 'hr'
  268.             ),
  269.             'fr' => array ( // France
  270.                 'fr',
  271.                 'bj',
  272.                 'bf',
  273.                 'cf',
  274.                 'cg',
  275.                 'cd',
  276.                 'ci',
  277.                 'ga',
  278.                 'gn',
  279.                 'lu',
  280.                 'ml',
  281.                 'mc',
  282.                 'ni',
  283.                 'sn',
  284.                 'tg'
  285.             ),
  286.             'nl' => array ( // Netherlands
  287.                 'nl',
  288.                 'sr'
  289.             ),
  290.             'tr' => array ( // Turkey
  291.                 'tr'
  292.             ),
  293.             'pl' => array ( // Polish
  294.                 'pl'
  295.             ),
  296.             'cz' => array ( // Czech
  297.                 'cz'
  298.             ),
  299.             'it' => array ( // Italy
  300.                 'it',
  301.                 'va',
  302.                 'sm'
  303.             ),
  304.             'pt' => array ( // Portuguese
  305.                 'pt',
  306.                 'br',
  307.                 'mz',
  308.                 'ao',
  309.                 'st',
  310.                 'tl'
  311.             ),
  312.             'browserlang' => array ( // special countries - overwrite any values before with browserlang - don't care about geoip !!!
  313.                 'ar' => array ( // arabic language
  314.                     'ar',
  315.                     'ar-eg',
  316.                     'ar-dz',
  317.                     'ar-bh',
  318.                     'ar-iq',
  319.                     'ar-ye',
  320.                     'ar-jo',
  321.                     'ar-qa',
  322.                     'ar-kw',
  323.                     'ar-lb',
  324.                     'ar-ly',
  325.                     'ar-ma',
  326.                     'ar-om',
  327.                     'ar-sa',
  328.                     'ar-sy',
  329.                     'ar-tn',
  330.                     'ar-ae'
  331.                 )
  332.             )
  333.         );
  334.        
  335.        
  336.         // Main function for 301 redirect of old or wrong URIs
  337.         function start($content='', $conf=array()) {
  338.             // config
  339.             $newurl = '';
  340.             $this->check(); // check functions (geoip loaded, static_info_tables loaded and so on)
  341.             if ($this->useGeoIpInsteadOfBrowserlang) $this->geoInfo(); // init geoip Array
  342.            
  343.             // let's go
  344.             if (strpos(t3lib_div::getIndpEnv('REQUEST_URI'), 'index.php') === false && $GLOBALS['TSFE']->tmpl->setup['config.']['baseURL'] && $this->enable) { // only if no manual domain like www.conject.com/index.php?id=3 and baseurl must be set
  345.                
  346.                 // Let's go
  347.                 if ($_GET['L'] == '' || strpos(substr($GLOBALS['TSFE']->tmpl->setup['config.']['baseURL'], 0, -1), $_SERVER['SERVER_NAME']) === false || strpos(t3lib_div::getIndpEnv('REQUEST_URI'), '/index.html') !== false) { // if wrong url OR no language variable set
  348.                    
  349.                     if ($_GET['L'] == '') { // no language variable set, so add one
  350.                        
  351.                         if (!$this->useGeoIpInsteadOfBrowserlang) $newurl = $GLOBALS['TSFE']->tmpl->setup['config.']['baseURL'] . $this->lang_getfrombrowser($this->languages, $this->languages[0]) . $_SERVER['REQUEST_URI']; // add correct domain (with language variable
  352.                         else $newurl = $GLOBALS['TSFE']->tmpl->setup['config.']['baseURL'] . $this->lang_getfromgeoip() . $_SERVER['REQUEST_URI']; // add correct domain (with language variable
  353.                        
  354.                     } else { // language variable set, so redirect only to the same directory with baseurl postfix
  355.                        
  356.                         $newurl = $GLOBALS['TSFE']->tmpl->setup['config.']['baseURL'] . substr(t3lib_div::getIndpEnv('REQUEST_URI'), 1); // add correct domain (without language variable
  357.                    
  358.                     }
  359.                    
  360.                     if (strpos($newurl, '/index.html') !== false && $this->index2html) { // if there is an /index.html within the url AND redirect active
  361.                         $newurl = str_replace('/index', '', $newurl); // remove "/index" folder/index.html => folder.html
  362.                     }
  363.                    
  364.                 }
  365.                
  366.                 if (!empty($newurl)) { // only if set
  367.                     if ($this->debugmode != 1) { // if no debugmode
  368.                         // redirect to new domain via 301
  369.                         header('HTTP/1.1 301 Moved Permanently');
  370.                         header('Location: '.$newurl);
  371.                         header('Connection: close');
  372.                     } else { // if debugmode active
  373.                         echo $newurl; // only show url
  374.                     }
  375.                 }
  376.                
  377.             }
  378.            
  379.             return false; // always return nothing
  380.            
  381.         }
  382.        
  383.        
  384.         // Function lang_getfromgeoip() gets language from geoip
  385.         function lang_getfromgeoip() {
  386.             if ($this->checkRobot() == false) { // no robot is visiting
  387.                 // config
  388.                 $lang = $this->fallback[1]; // default lang
  389.                 $tmp_lang1 = t3lib_div::trimExplode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE'], 1); // split at ,
  390.                 $tmp_lang2 = t3lib_div::trimExplode(';', $tmp_lang1[0], 1); // split at ;
  391.                
  392.                 // let's go
  393.                 if (is_array($this->geoIPcountries) && count($this->geoIPcountries) > 0) { // if geoarray is set
  394.                     foreach ($this->geoIPcountries as $key => $value) { // one loop for every defined country
  395.                         if (is_array($value)) {
  396.                             foreach ($value as $key2 => $value2) { // loop in second level
  397.                                 if (!is_array($value2)) { // if no further settings
  398.                                     if (is_numeric(array_search(strtolower($this->changeISO_3_2($this->GEOinfos['countryCode'])), $this->geoIPcountries[$key]))) { // country from ip fits to $key
  399.                                         $lang = $key; // set langkey
  400.                                     }
  401.                                 } else { // further settings
  402.                                     if (in_array($tmp_lang2[0], $value2)) $lang = array_search(strtolower($tmp_lang2[0]), $value2); // give value from array
  403.                                 }
  404.                             }
  405.                         }
  406.                     }
  407.                 }
  408.                
  409.                 // Check for Browserlanguage in addition
  410.                 if (is_array($this->geoIPcountries['browserlang'])) { // if values available
  411.                     foreach ((array) $this->geoIPcountries['browserlang'] as $key => $value) { // one loop for every fe lang in array
  412.                         if (in_array(strtolower($tmp_lang2[0]), $this->geoIPcountries['browserlang'][$key]))  { // check if browserlang is listed in the array
  413.                             $lang = $key; // write fe lang
  414.                         }
  415.                     }
  416.                 }
  417.             }
  418.            
  419.             else { // user is a robot
  420.                 $lang = $this->languageForRobots; // robot language
  421.             }
  422.            
  423.        
  424.             return $lang;
  425.         }
  426.        
  427.        
  428.         // Function lang_getfrombrowser() gets the language variable from users browser
  429.         function lang_getfrombrowser ($allowed_languages, $default_language, $lang_variable = null, $strict_mode = true) {
  430.            
  431.             if ($this->checkRobot() == false) { // no robot is visiting
  432.            
  433.                 // use $_SERVER['HTTP_ACCEPT_LANGUAGE'] if no lang variable
  434.                 if ($lang_variable === null) {
  435.                     $lang_variable = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  436.                 }
  437.                
  438.                 // are there any information
  439.                 if (empty($lang_variable)) {
  440.                     // NO? => use default lang
  441.                     return $default_language;
  442.                 }
  443.                    
  444.                 // Split header
  445.                 $accepted_languages = preg_split('/,\s*/', $lang_variable);
  446.                
  447.                 // Default values
  448.                 $current_lang = $default_language;
  449.                 $current_q = 0;
  450.                
  451.                 // One loop for every lang
  452.                 foreach ($accepted_languages as $accepted_language) {
  453.                     // Get all infos about lang
  454.  
  455.                     $res = preg_match ('/^([a-z]{1,8}(?:-[a-z]{1,8})*)'.'(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', $accepted_language, $matches);
  456.                    
  457.                     // Sytax correct
  458.                     if (!$res) {
  459.                         // No - so ignore
  460.                         continue;
  461.                     }
  462.                    
  463.                     // Sprachcode holen und dann sofort in die Einzelteile trennen
  464.                     $lang_code = explode ('-', $matches[1]);
  465.                
  466.                     // Wurde eine Qualität mitgegeben?
  467.                     if (isset($matches[2])) {
  468.                         // die Qualität benutzen
  469.                         $lang_quality = (float)$matches[2];
  470.                     } else {
  471.                         // Kompabilitätsmodus: Qualität 1 annehmen
  472.                         $lang_quality = 1.0;
  473.                     }
  474.                
  475.                     // Bis der Sprachcode leer ist...
  476.                     while (count ($lang_code)) {
  477.                         // mal sehen, ob der Sprachcode angeboten wird
  478.                         if (in_array (strtolower (join ('-', $lang_code)), $allowed_languages)) {
  479.                             // Check quality
  480.                             if ($lang_quality > $current_q) {
  481.                                 // use this lang
  482.                                 $current_lang = strtolower (join ('-', $lang_code));
  483.                                 $current_q = $lang_quality;
  484.                                 // Stop while loop
  485.                                 break;
  486.                             }
  487.                         }
  488.                        
  489.                         // Don't minimalize language if in strict mode
  490.                         if ($strict_mode) {
  491.                             // stop while loop
  492.                             break;
  493.                         }
  494.                        
  495.                         // deactivate right part of the language code
  496.                         array_pop ($lang_code);
  497.                     }
  498.                 }
  499.                
  500.             } else { // if robot is visiting
  501.                 $current_lang = $this->languageForRobots; // robot language
  502.             }
  503.        
  504.             return $current_lang;
  505.         }
  506.    
  507.    
  508.         // use geo ip if loaded
  509.         function geoInfo() {
  510.             if (t3lib_extMgm::isLoaded('geoip')) { // if geoip is loaded
  511.                 require_once(t3lib_extMgm::extPath('geoip').'/pi1/class.tx_geoip_pi1.php'); // include geoip class
  512.                 $this->media = t3lib_div::makeInstance('tx_geoip_pi1'); // new instance
  513.                 $this->media->init($this->geoDBpath); // Initialize the Geoip Ext
  514.                 $this->GEOinfos = $this->media->getGeoIP(($this->ip ? $this->ip : t3lib_div::getIndpEnv('REMOTE_ADDR'))); // get all the infos
  515.                 if (!$this->ip && strpos(t3lib_div::getIndpEnv('REMOTE_ADDR'), '192.168.') !== false) $this->GEOinfos['countryCode'] = 'DEU'; // overwrite country if local ip address
  516.             }
  517.         }
  518.        
  519.        
  520.         // Function checkRobot() checks if visiter is a robot
  521.         function checkRobot() {
  522.             $trackUserAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
  523.            
  524.             if (stristr($trackUserAgent, 'archiver')) $trackrobot = 'Alexa';
  525.             if (stristr($trackUserAgent, 'exabot')) $trackrobot = 'Exalead';
  526.             if (stristr($trackUserAgent, 'fast')) $trackrobot = 'Fast';
  527.             if (stristr($trackUserAgent, 'firefly')) $trackrobot = 'Fireball';
  528.             if (stristr($trackUserAgent, 'googlebot')) $trackrobot = 'Google';
  529.             if (stristr($trackUserAgent, 'msnbot')) $trackrobot = 'MSN';
  530.             if (stristr($trackUserAgent, 'architextspider')) $trackrobot = 'Excite';
  531.             if (stristr($trackUserAgent, 'scooter')) $trackrobot = 'Altavista';
  532.             if (stristr($trackUserAgent, 'lycos_spider')) $trackrobot = 'Lycos';
  533.             if (stristr($trackUserAgent, 'slurp')) $trackrobot = 'Yahoo';
  534.             if (stristr($trackUserAgent, 'nagios')) $trackrobot = 'Nagios';
  535.             if (stristr($trackUserAgent, 'robot')) $trackrobot = 'Robot';
  536.             if (stristr($trackUserAgent, 'crawl')) $trackrobot = 'Crawler';
  537.             if (stristr($trackUserAgent, 'gigabot')) $trackrobot = 'GigaBot';
  538.             if (stristr($trackUserAgent, 'echo!')) $trackrobot = 'EchO!';
  539.             if (stristr($trackUserAgent, 'baiduspider')) $trackrobot = 'BaiDuSpider';
  540.             if (stristr($trackUserAgent, 'askjeeves')) $trackrobot = 'AskJeeves';
  541.             if (stristr($trackUserAgent, 'turnitin')) $trackrobot = 'Turn It In';
  542.             if (stristr($trackUserAgent, 'speedyspider')) $trackrobot = 'Speedy Spider';
  543.             if (stristr($trackUserAgent, 'bot/')) $trackrobot = 'Bot';
  544.             if (stristr($trackUserAgent, 'bot-')) $trackrobot = 'Bot';
  545.             if (stristr($trackUserAgent, 'psbot')) $trackrobot = 'PS Bot';
  546.             if (stristr($trackUserAgent, 'thepythonrobot')) $trackrobot = 'The Python Robot';
  547.             if (stristr($trackUserAgent, 'voila')) $trackrobot = 'Voila';
  548.             if (stristr($trackUserAgent, 'bspider')) $trackrobot = 'BSpider';
  549.             if (stristr($trackUserAgent, 'surveybot')) $trackrobot = 'SurveyBot';
  550.             if (stristr($trackUserAgent, 'grub.org')) $trackrobot = 'Grub.org';
  551.             if (stristr($trackUserAgent, 'alexa')) $trackrobot = 'Alexa';
  552.             if (stristr($trackUserAgent, 'arks')) $trackrobot = 'Arks';
  553.             if (stristr($trackUserAgent, 'spider')) $trackrobot = 'Spider';
  554.             if (stristr($trackUserAgent, 'yandex')) $trackrobot = 'Yandex bot';
  555.             if (stristr($trackUserAgent, 'holmes')) $trackrobot = 'Holmes';
  556.            
  557.             if ($trackrobot != '') return $trackrobot;
  558.             else return false;
  559.         }
  560.        
  561.        
  562.         // Function check() checks if all is allright
  563.         function check() {
  564.             if (!t3lib_extMgm::isLoaded('geoip') || !t3lib_extMgm::isLoaded('static_info_tables')) { // geoip or static_info_tables is not loaded
  565.                 die ('Install geoip and static_info_tables first');
  566.             }
  567.         }
  568.        
  569.         // function changeISO_3_2 changes country with ISO code 3 to ISO code 2 (DEU => DE, AUT => AT)
  570.         function changeISO_3_2($country) {
  571.             if (t3lib_extMgm::isLoaded('static_info_tables', 0)) { // if ext static_info_tables is loaded
  572.            
  573.                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery (
  574.                     'cn_iso_2',
  575.                     'static_countries',
  576.                     'cn_iso_3 = "'.$country.'"',
  577.                     '',
  578.                     '',
  579.                     '1'
  580.                 );
  581.                 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
  582.        
  583.                 if (!empty($row['cn_iso_2'])) return $row['cn_iso_2'];
  584.             }
  585.         }
  586.        
  587.     }
  588.  
  589. ?>
Advertisement
Add Comment
Please, Sign In to add comment