Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class user_301redirect {
- var $enable = 1; // enable or disable redirect
- var $debugmode = 0; // enable or disable debugmode (redirect or only print of the new url)
- var $languageForRobots = 'en'; // language for visiting robot (former de)
- var $index2html = 1; // domain.de/page/index.html => domain.de/page.html redirect
- var $useGeoIpInsteadOfBrowserlang = 1; // Deactivate browser lang detection and activate from geoip
- var $geoDBpath = 'fileadmin/conject/administration/scripts/GeoLiteCity.dat'; // path to geoip database
- // Test some ip addresses
- // DEU: 83.236.183.130
- // AUT: 213.235.230.170
- // UAE: 195.229.237.36
- // RUS: 89.113.208.15
- // CHE: 80.74.157.3
- // POL: 80.54.62.213
- // CZE: 83.208.36.242
- // SLO: 90.157.147.182
- // BEL: 62.88.83.151
- // ROU: 217.10.222.250
- // HRV: 195.29.117.50
- // ZAF: 196.21.143.253
- // CHN: 202.203.40.2
- // GRC: 195.66.111.34
- // EGY: 41.196.84.138
- var $ip = ''; // Fill only if you want to test an especially IP address
- var $fallback = array ( // fallback language if no language could be found (first: fallback for browserlang / second: fallback for geoip)
- 'en',
- 'en'
- );
- var $languages = array ( // L=0 => en, L=1 => de (Attention: the first is default if no defined language read!)
- 0 => 'en',
- 1 => 'de',
- 2 => 'es',
- 3 => 'pt',
- 4 => 'ru',
- 5 => 'me',
- 6 => 'bg',
- 7 => 'ro',
- 8 => 'hr',
- 9 => 'fr',
- 10 => 'nl',
- 11 => 'tr',
- 12 => 'pl',
- 13 => 'cz',
- 14 => 'it',
- 15 => 'ar',
- 17 => 'at',
- 18 => 'za',
- 19 => 'sg',
- 20 => 'eg'
- );
- var $geoIPcountries = array ( // Set relation between countries and languages
- 'de' => array ( // German
- 'de',
- 'ch' => array (
- 'de' => 'de-ch',
- 'de' => 'de',
- 'en' => 'en',
- 'en' => 'en-ch',
- 'fr' => 'fr',
- 'fr' => 'fr-ch',
- 'it' => 'it-ch'
- )
- ),
- 'at' => array ( // Austria
- 'at'
- ),
- 'ru' => array ( // Russia
- 'az',
- 'by',
- 'kz',
- 'kg',
- 'md',
- 'ru',
- 'tj',
- 'tm',
- 'ua',
- 'uz'
- ),
- 'za' => array ( // South Africa
- 'za'
- ),
- 'me' => array ( // Middle East for Africa, Asia and
- 'dz',
- 'ao',
- 'bj',
- 'bw',
- 'bf',
- 'bi',
- 'cm',
- 'cv',
- 'cf',
- 'td',
- 'km',
- 'cd',
- 'cg',
- 'ci',
- 'dj',
- 'gq',
- 'er',
- 'et',
- 'ga',
- 'gm',
- 'gh',
- 'gn',
- 'gw',
- 'ke',
- 'ls',
- 'lr',
- 'ly',
- 'mg',
- 'mw',
- 'ml',
- 'mr',
- 'mu',
- 'yt',
- 'ma',
- 'mz',
- 'na',
- 'ne',
- 'ng',
- 're',
- 'rw',
- 'sh',
- 'st',
- 'sn',
- 'sc',
- 'sl',
- 'so',
- 'sd',
- 'sz',
- 'tz',
- 'tg',
- 'tn',
- 'ug',
- 'eh',
- 'zm',
- 'zw',
- 'aq',
- 'bv',
- 'tf',
- 'hm',
- 'gs',
- 'af',
- 'am',
- 'az',
- 'bh',
- 'io',
- 'cx',
- 'cc',
- 'cy',
- 'ge',
- 'in',
- 'ir',
- 'iq',
- 'il',
- 'jo',
- 'kz',
- 'kp',
- 'kw',
- 'kg',
- 'lb',
- 'my',
- 'mv',
- 'mm',
- 'np',
- 'om',
- 'pk',
- 'ps',
- 'qa',
- 'sa',
- 'lk',
- 'sy',
- 'tj',
- 'tr',
- 'tm',
- 'ae',
- 'uz',
- 'ye',
- 'as',
- 'ck',
- 'fj',
- 'pf',
- 'gu',
- 'ki',
- 'mh',
- 'fm',
- 'nr',
- 'nc',
- 'nu',
- 'nf',
- 'mp',
- 'pw',
- 'pg',
- 'pn',
- 'ws',
- 'sb',
- 'tk',
- 'to',
- 'tv',
- 'um',
- 'vu',
- 'wf'
- ),
- 'eg' => array ( // Egypt
- 'eg'
- ),
- 'sg' => array ( // Singapore
- 'bn',
- 'cn',
- 'hk',
- 'id',
- 'jp',
- 'kh',
- 'la',
- 'mo',
- 'my',
- 'mn',
- 'tl',
- 'ph',
- 'sg',
- 'kr',
- 'tw',
- 'th',
- 'vn',
- 'bd',
- 'bt',
- 'au',
- 'nz'
- ),
- 'es' => array ( // Spain
- 'es',
- 'mx',
- 'cu',
- 'do',
- 'pr',
- 'bz',
- 'cr',
- 'sv',
- 'gt',
- 'hn',
- 'ni',
- 'pa',
- 'ar',
- 'bo',
- 'cl',
- 'co',
- 'ec',
- 'py',
- 'pe',
- 'uy',
- 've'
- ),
- 'bg' => array ( // Bulgaria
- 'bg'
- ),
- 'ro' => array ( // Romania
- 'ro'
- ),
- 'hr' => array ( // Croatia
- 'hr'
- ),
- 'fr' => array ( // France
- 'fr',
- 'bj',
- 'bf',
- 'cf',
- 'cg',
- 'cd',
- 'ci',
- 'ga',
- 'gn',
- 'lu',
- 'ml',
- 'mc',
- 'ni',
- 'sn',
- 'tg'
- ),
- 'nl' => array ( // Netherlands
- 'nl',
- 'sr'
- ),
- 'tr' => array ( // Turkey
- 'tr'
- ),
- 'pl' => array ( // Polish
- 'pl'
- ),
- 'cz' => array ( // Czech
- 'cz'
- ),
- 'it' => array ( // Italy
- 'it',
- 'va',
- 'sm'
- ),
- 'pt' => array ( // Portuguese
- 'pt',
- 'br',
- 'mz',
- 'ao',
- 'st',
- 'tl'
- ),
- 'browserlang' => array ( // special countries - overwrite any values before with browserlang - don't care about geoip !!!
- 'ar' => array ( // arabic language
- 'ar',
- 'ar-eg',
- 'ar-dz',
- 'ar-bh',
- 'ar-iq',
- 'ar-ye',
- 'ar-jo',
- 'ar-qa',
- 'ar-kw',
- 'ar-lb',
- 'ar-ly',
- 'ar-ma',
- 'ar-om',
- 'ar-sa',
- 'ar-sy',
- 'ar-tn',
- 'ar-ae'
- )
- )
- );
- // Main function for 301 redirect of old or wrong URIs
- function start($content='', $conf=array()) {
- // config
- $newurl = '';
- $this->check(); // check functions (geoip loaded, static_info_tables loaded and so on)
- if ($this->useGeoIpInsteadOfBrowserlang) $this->geoInfo(); // init geoip Array
- // let's go
- 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
- // Let's go
- 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
- if ($_GET['L'] == '') { // no language variable set, so add one
- 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
- else $newurl = $GLOBALS['TSFE']->tmpl->setup['config.']['baseURL'] . $this->lang_getfromgeoip() . $_SERVER['REQUEST_URI']; // add correct domain (with language variable
- } else { // language variable set, so redirect only to the same directory with baseurl postfix
- $newurl = $GLOBALS['TSFE']->tmpl->setup['config.']['baseURL'] . substr(t3lib_div::getIndpEnv('REQUEST_URI'), 1); // add correct domain (without language variable
- }
- if (strpos($newurl, '/index.html') !== false && $this->index2html) { // if there is an /index.html within the url AND redirect active
- $newurl = str_replace('/index', '', $newurl); // remove "/index" folder/index.html => folder.html
- }
- }
- if (!empty($newurl)) { // only if set
- if ($this->debugmode != 1) { // if no debugmode
- // redirect to new domain via 301
- header('HTTP/1.1 301 Moved Permanently');
- header('Location: '.$newurl);
- header('Connection: close');
- } else { // if debugmode active
- echo $newurl; // only show url
- }
- }
- }
- return false; // always return nothing
- }
- // Function lang_getfromgeoip() gets language from geoip
- function lang_getfromgeoip() {
- if ($this->checkRobot() == false) { // no robot is visiting
- // config
- $lang = $this->fallback[1]; // default lang
- $tmp_lang1 = t3lib_div::trimExplode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE'], 1); // split at ,
- $tmp_lang2 = t3lib_div::trimExplode(';', $tmp_lang1[0], 1); // split at ;
- // let's go
- if (is_array($this->geoIPcountries) && count($this->geoIPcountries) > 0) { // if geoarray is set
- foreach ($this->geoIPcountries as $key => $value) { // one loop for every defined country
- if (is_array($value)) {
- foreach ($value as $key2 => $value2) { // loop in second level
- if (!is_array($value2)) { // if no further settings
- if (is_numeric(array_search(strtolower($this->changeISO_3_2($this->GEOinfos['countryCode'])), $this->geoIPcountries[$key]))) { // country from ip fits to $key
- $lang = $key; // set langkey
- }
- } else { // further settings
- if (in_array($tmp_lang2[0], $value2)) $lang = array_search(strtolower($tmp_lang2[0]), $value2); // give value from array
- }
- }
- }
- }
- }
- // Check for Browserlanguage in addition
- if (is_array($this->geoIPcountries['browserlang'])) { // if values available
- foreach ((array) $this->geoIPcountries['browserlang'] as $key => $value) { // one loop for every fe lang in array
- if (in_array(strtolower($tmp_lang2[0]), $this->geoIPcountries['browserlang'][$key])) { // check if browserlang is listed in the array
- $lang = $key; // write fe lang
- }
- }
- }
- }
- else { // user is a robot
- $lang = $this->languageForRobots; // robot language
- }
- return $lang;
- }
- // Function lang_getfrombrowser() gets the language variable from users browser
- function lang_getfrombrowser ($allowed_languages, $default_language, $lang_variable = null, $strict_mode = true) {
- if ($this->checkRobot() == false) { // no robot is visiting
- // use $_SERVER['HTTP_ACCEPT_LANGUAGE'] if no lang variable
- if ($lang_variable === null) {
- $lang_variable = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
- }
- // are there any information
- if (empty($lang_variable)) {
- // NO? => use default lang
- return $default_language;
- }
- // Split header
- $accepted_languages = preg_split('/,\s*/', $lang_variable);
- // Default values
- $current_lang = $default_language;
- $current_q = 0;
- // One loop for every lang
- foreach ($accepted_languages as $accepted_language) {
- // Get all infos about lang
- $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);
- // Sytax correct
- if (!$res) {
- // No - so ignore
- continue;
- }
- // Sprachcode holen und dann sofort in die Einzelteile trennen
- $lang_code = explode ('-', $matches[1]);
- // Wurde eine Qualität mitgegeben?
- if (isset($matches[2])) {
- // die Qualität benutzen
- $lang_quality = (float)$matches[2];
- } else {
- // Kompabilitätsmodus: Qualität 1 annehmen
- $lang_quality = 1.0;
- }
- // Bis der Sprachcode leer ist...
- while (count ($lang_code)) {
- // mal sehen, ob der Sprachcode angeboten wird
- if (in_array (strtolower (join ('-', $lang_code)), $allowed_languages)) {
- // Check quality
- if ($lang_quality > $current_q) {
- // use this lang
- $current_lang = strtolower (join ('-', $lang_code));
- $current_q = $lang_quality;
- // Stop while loop
- break;
- }
- }
- // Don't minimalize language if in strict mode
- if ($strict_mode) {
- // stop while loop
- break;
- }
- // deactivate right part of the language code
- array_pop ($lang_code);
- }
- }
- } else { // if robot is visiting
- $current_lang = $this->languageForRobots; // robot language
- }
- return $current_lang;
- }
- // use geo ip if loaded
- function geoInfo() {
- if (t3lib_extMgm::isLoaded('geoip')) { // if geoip is loaded
- require_once(t3lib_extMgm::extPath('geoip').'/pi1/class.tx_geoip_pi1.php'); // include geoip class
- $this->media = t3lib_div::makeInstance('tx_geoip_pi1'); // new instance
- $this->media->init($this->geoDBpath); // Initialize the Geoip Ext
- $this->GEOinfos = $this->media->getGeoIP(($this->ip ? $this->ip : t3lib_div::getIndpEnv('REMOTE_ADDR'))); // get all the infos
- if (!$this->ip && strpos(t3lib_div::getIndpEnv('REMOTE_ADDR'), '192.168.') !== false) $this->GEOinfos['countryCode'] = 'DEU'; // overwrite country if local ip address
- }
- }
- // Function checkRobot() checks if visiter is a robot
- function checkRobot() {
- $trackUserAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
- if (stristr($trackUserAgent, 'archiver')) $trackrobot = 'Alexa';
- if (stristr($trackUserAgent, 'exabot')) $trackrobot = 'Exalead';
- if (stristr($trackUserAgent, 'fast')) $trackrobot = 'Fast';
- if (stristr($trackUserAgent, 'firefly')) $trackrobot = 'Fireball';
- if (stristr($trackUserAgent, 'googlebot')) $trackrobot = 'Google';
- if (stristr($trackUserAgent, 'msnbot')) $trackrobot = 'MSN';
- if (stristr($trackUserAgent, 'architextspider')) $trackrobot = 'Excite';
- if (stristr($trackUserAgent, 'scooter')) $trackrobot = 'Altavista';
- if (stristr($trackUserAgent, 'lycos_spider')) $trackrobot = 'Lycos';
- if (stristr($trackUserAgent, 'slurp')) $trackrobot = 'Yahoo';
- if (stristr($trackUserAgent, 'nagios')) $trackrobot = 'Nagios';
- if (stristr($trackUserAgent, 'robot')) $trackrobot = 'Robot';
- if (stristr($trackUserAgent, 'crawl')) $trackrobot = 'Crawler';
- if (stristr($trackUserAgent, 'gigabot')) $trackrobot = 'GigaBot';
- if (stristr($trackUserAgent, 'echo!')) $trackrobot = 'EchO!';
- if (stristr($trackUserAgent, 'baiduspider')) $trackrobot = 'BaiDuSpider';
- if (stristr($trackUserAgent, 'askjeeves')) $trackrobot = 'AskJeeves';
- if (stristr($trackUserAgent, 'turnitin')) $trackrobot = 'Turn It In';
- if (stristr($trackUserAgent, 'speedyspider')) $trackrobot = 'Speedy Spider';
- if (stristr($trackUserAgent, 'bot/')) $trackrobot = 'Bot';
- if (stristr($trackUserAgent, 'bot-')) $trackrobot = 'Bot';
- if (stristr($trackUserAgent, 'psbot')) $trackrobot = 'PS Bot';
- if (stristr($trackUserAgent, 'thepythonrobot')) $trackrobot = 'The Python Robot';
- if (stristr($trackUserAgent, 'voila')) $trackrobot = 'Voila';
- if (stristr($trackUserAgent, 'bspider')) $trackrobot = 'BSpider';
- if (stristr($trackUserAgent, 'surveybot')) $trackrobot = 'SurveyBot';
- if (stristr($trackUserAgent, 'grub.org')) $trackrobot = 'Grub.org';
- if (stristr($trackUserAgent, 'alexa')) $trackrobot = 'Alexa';
- if (stristr($trackUserAgent, 'arks')) $trackrobot = 'Arks';
- if (stristr($trackUserAgent, 'spider')) $trackrobot = 'Spider';
- if (stristr($trackUserAgent, 'yandex')) $trackrobot = 'Yandex bot';
- if (stristr($trackUserAgent, 'holmes')) $trackrobot = 'Holmes';
- if ($trackrobot != '') return $trackrobot;
- else return false;
- }
- // Function check() checks if all is allright
- function check() {
- if (!t3lib_extMgm::isLoaded('geoip') || !t3lib_extMgm::isLoaded('static_info_tables')) { // geoip or static_info_tables is not loaded
- die ('Install geoip and static_info_tables first');
- }
- }
- // function changeISO_3_2 changes country with ISO code 3 to ISO code 2 (DEU => DE, AUT => AT)
- function changeISO_3_2($country) {
- if (t3lib_extMgm::isLoaded('static_info_tables', 0)) { // if ext static_info_tables is loaded
- $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery (
- 'cn_iso_2',
- 'static_countries',
- 'cn_iso_3 = "'.$country.'"',
- '',
- '',
- '1'
- );
- $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
- if (!empty($row['cn_iso_2'])) return $row['cn_iso_2'];
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment