/** * Get two or three letter country code. * * @param boolean $threeLetterCode TRUE to return 3-letter country code * * @return string|false Country code or FALSE on failure */ public function getCountryCode($threeLetterCode = false) { try { // Net_GeoIP provides no method to return 3-letter-code if ($threeLetterCode) { $location = $this->geoLiteCity->lookupLocation($this->ip); if ($location instanceof Net_GeoIP_Location) { $data = $location->getData(); return $data['countryCode3']; } return false; } return $this->geoLiteCountry->lookupCountryCode($this->ip); } catch (Exception $exception) { return false; } }
/** * The main function recognizes the browser's preferred languages and * reloads the page accordingly. Exits if successful. * * @param string $content HTML content * @param array $conf The mandatory configuration array * @return string */ public function main($content, $conf) { $this->conf = $conf; $this->cookieLifetime = (int) $conf['cookieLifetime']; $this->domainRecord = $this->getCurrentDomainRecord(); // Break out if domain record is disabled if ($this->domainRecord["disable_language_detection"]) { return $content; } // Break out if language already selected if (!$this->conf['dontBreakIfLanguageIsAlreadySelected'] && GeneralUtility::_GP($this->conf['languageGPVar']) !== NULL) { if (TYPO3_DLOG) { GeneralUtility::devLog('Break out since language is already selected', $this->extKey); } return $content; } // Break out if the last page visited was also on our site: $referrer = (string) GeneralUtility::getIndpEnv('HTTP_REFERER'); if (TYPO3_DLOG) { GeneralUtility::devLog('Referrer: ' . $referrer, $this->extKey); } if (!$this->conf['dontBreakIfLastPageWasOnSite'] && $referrer !== '' && (stripos($referrer, GeneralUtility::getIndpEnv('TYPO3_SITE_URL')) !== FALSE || stripos($referrer, $this->getTSFE()->baseUrl) !== FALSE || stripos($referrer . '/', GeneralUtility::getIndpEnv('TYPO3_SITE_URL')) !== FALSE || stripos($referrer . '/', $this->getTSFE()->baseUrl) !== FALSE)) { return $content; } // Break out if the session tells us that the user has selected language if (!$this->conf['dontBreakIfLanguageIsAlreadySelected']) { if ($this->cookieLifetime) { // read from browser-cookie $languageSessionKey = $_COOKIE[$this->extKey . '_languageSelected']; } else { $languageSessionKey = $this->getTSFE()->fe_user->getKey('ses', $this->extKey . '_languageSelected'); } // If session key exists but no language GP var - // we should redirect client to selected language if (isset($languageSessionKey)) { // Can redirect only in one tree method for now if ($this->conf['useOneTreeMethod'] && is_numeric($languageSessionKey)) { $this->doRedirect($languageSessionKey, $referrer); return ''; } return $content; } } //GATHER DATA //Get available languages $availableLanguagesArr = $this->conf['useOneTreeMethod'] ? $this->getSysLanguages() : $this->getMultipleTreeLanguages(); if (TYPO3_DLOG) { GeneralUtility::devLog('Detecting available languages in installation', $this->extKey, 0, $availableLanguagesArr); } //Collect language aliases $languageAliases = array(); if ($this->conf['useLanguageAliases']) { $tmp = $conf['languageAliases.']; foreach ($tmp as $key => $languageAlias) { $languageAliases[strtolower($key)] = GeneralUtility::trimExplode(',', strtolower($languageAlias), TRUE); } } $testOrder = GeneralUtility::trimExplode(',', $conf['testOrder'], TRUE); $preferredLanguageOrPageUid = FALSE; for ($i = 0; $i < count($testOrder) && $preferredLanguageOrPageUid === FALSE; $i++) { switch ($testOrder[$i]) { //Browser information case 'browser': //Get Accepted Languages from Browser $acceptedLanguagesArr = $this->getAcceptedLanguages(); if (TYPO3_DLOG) { GeneralUtility::devLog('Detecting user browser languages', $this->extKey, 0, $acceptedLanguagesArr); } //Break out if the default languange is already selected //Thanks to Stefan Mielke $first = substr(key($acceptedLanguagesArr), 0, 2); if ($first === $this->conf['defaultLang']) { $preferredLanguageOrPageUid = 0; break; } //Iterate through the user's accepted languages for ($j = 0; $j < count($acceptedLanguagesArr); $j++) { $currentLanguage = array_values($acceptedLanguagesArr); $currentLanguage = $currentLanguage[$j]; if (TYPO3_DLOG) { GeneralUtility::devLog('Testing language: ' . $currentLanguage, $this->extKey); } //If the current language is available (full "US_en" type check) if (isset($availableLanguagesArr[$currentLanguage])) { $preferredLanguageOrPageUid = $availableLanguagesArr[$currentLanguage]; if (TYPO3_DLOG) { GeneralUtility::devLog('Found: ' . $preferredLanguageOrPageUid . ' (full check)', $this->extKey); } break; } //Old-fashioned 2-char test ("en") if (strlen($currentLanguage) > 2 && $preferredLanguageOrPageUid === FALSE) { $currentLanguageShort = substr($currentLanguage, 0, 2); if (isset($availableLanguagesArr[$currentLanguageShort])) { $preferredLanguageOrPageUid = $availableLanguagesArr[$currentLanguageShort]; if (TYPO3_DLOG) { GeneralUtility::devLog('Found: ' . $preferredLanguageOrPageUid . ' (normal check)', $this->extKey); } break; } } //If the user's language is in language aliases if ($this->conf['useLanguageAliases'] && array_key_exists($currentLanguage, $languageAliases) && $preferredLanguageOrPageUid === FALSE) { $values = $languageAliases[$currentLanguage]; //Iterate through aliases and choose the first possible foreach ($values as $value) { if (isset($availableLanguagesArr[$value])) { $preferredLanguageOrPageUid = $availableLanguagesArr[$value]; if (TYPO3_DLOG) { GeneralUtility::devLog('Found: ' . $preferredLanguageOrPageUid . ' (alias check)', $this->extKey); } break 2; } } } } break; //GeoIP //GeoIP case 'ip': $countryCode = ''; if ($this->conf['pearDirectory']) { $pearDirectory = $this->conf['pearDirectory']; } else { $pearDirectory = PEAR_INSTALL_DIR; } if (file_exists($pearDirectory . '/Net/GeoIP.php') && $this->conf['pathToDatabaseForGeoIPData']) { require_once $pearDirectory . '/Net/GeoIP.php'; $pathToDatabase = GeneralUtility::getFileAbsFileName($this->conf['pathToDatabaseForGeoIPData']); $geoIp = new \Net_GeoIP($pathToDatabase); // Get country code from geoip if (TYPO3_DLOG) { GeneralUtility::devLog('IP: ' . $this->getUserIP(), $this->extKey); } $countryCode = strtolower($geoIp->lookupCountryCode($this->getUserIP())); if (TYPO3_DLOG) { GeneralUtility::devLog('GeoIP Country Code: ' . $countryCode, $this->extKey); } unset($geoIp); } // PHP module geoip if (!$countryCode && function_exists('geoip_country_code_by_name')) { // Get country code from geoip if (TYPO3_DLOG) { GeneralUtility::devLog('IP: ' . $this->getUserIP(), $this->extKey); } $countryCode = strtolower(geoip_country_code_by_name($this->getUserIP())); if (TYPO3_DLOG) { GeneralUtility::devLog('GeoIP Country Code: ' . $countryCode, $this->extKey); } } if ($countryCode) { //Check for the country code in the configured list of country to languages if (array_key_exists($countryCode, $this->conf['countryCodeToLanguageCode.']) && array_key_exists($this->conf['countryCodeToLanguageCode.'][$countryCode], $availableLanguagesArr)) { if (TYPO3_DLOG) { GeneralUtility::devLog('Available language found in configured: ' . $countryCode, $this->extKey); } $preferredLanguageOrPageUid = $availableLanguagesArr[$this->conf['countryCodeToLanguageCode.'][$countryCode]]; //Use the static_info_tables lg_collate_locale to attempt to find a country to language relation. } elseif (ExtensionManagementUtility::isLoaded('static_info_tables')) { if (TYPO3_DLOG) { GeneralUtility::devLog('Checking in static_info_tables.', $this->extKey); } //Get the language codes from lg_collate_locate $values = $this->getLanguageCodesForCountry($countryCode); foreach ($values as $value) { //If one of the languages exist if (array_key_exists($value, $availableLanguagesArr)) { if (TYPO3_DLOG) { GeneralUtility::devLog('Found in static_info_tables: ' . $value, $this->extKey); } $preferredLanguageOrPageUid = $availableLanguagesArr[$value]; break; } } } } break; //Handle hooks //Handle hooks default: //Hook for adding other language processing if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rlmp_language_detection']['preferredLanguageHooks'])) { foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rlmp_language_detection']['preferredLanguageHooks'] as $key => $_funcRef) { if ($key == $testOrder[$i]) { $preferredLanguageOrPageUid = GeneralUtility::callUserFunction($_funcRef, $availableLanguagesArr, $this); if ($preferredLanguageOrPageUid) { break; } } } } break; } } if (TYPO3_DLOG) { GeneralUtility::devLog('END result: Preferred=' . $preferredLanguageOrPageUid, $this->extKey); } if ($preferredLanguageOrPageUid !== FALSE) { $this->doRedirect($preferredLanguageOrPageUid, $referrer); } return ''; }