コード例 #1
0
ファイル: jfrouter.php プロジェクト: JoomFish/joomfish-2.2
    function _discoverJFLanguage()
    {
        static $discovered;
        if (isset($discovered) && $discovered) {
            return;
        }
        $discovered = true;
        $registry = JFactory::getConfig();
        // Find language without loading strings
        $locale = $registry->getValue('config.language');
        // Attention - we need to access the site default values
        // #12943 explains that a user might overwrite the orignial settings based on his own profile
        $langparams = JComponentHelper::getParams('com_languages');
        $defLanguage = $langparams->get("site");
        $registry->setValue("config.defaultlang", isset($defLanguage) && $defLanguage != '' ? $defLanguage : $locale);
        // get params from registry in case function called statically
        $params = $registry->getValue("jfrouter.params");
        $determitLanguage = $params->get('determitLanguage', 1);
        $newVisitorAction = $params->get('newVisitorAction', 'browser');
        $use302redirect = $params->get('use302redirect', 0);
        $enableCookie = $params->get('enableCookie', 1);
        // get instance of JoomFishManager to obtain active language list and config values
        $jfm = JoomFishManager::getInstance();
        $client_lang = '';
        $lang_known = false;
        $jfcookie = JRequest::getVar('jfcookie', null, "COOKIE");
        if (isset($jfcookie["lang"]) && $jfcookie["lang"] != "") {
            $client_lang = $jfcookie["lang"];
            $lang_known = true;
        }
        $uri = JURI::getInstance();
        if ($requestlang = JRequest::getVar('lang', null, "REQUEST")) {
            if ($requestlang != '') {
                $client_lang = $requestlang;
                $lang_known = true;
            }
        }
        // no language choosen - Test plugin e.g. IP lookup tool
        if (!$lang_known) {
            // setup Joomfish pluginds
            $dispatcher = JDispatcher::getInstance();
            $iplang = "";
            JPluginHelper::importPlugin('joomfish');
            $dispatcher->trigger('onDiscoverLanguage', array(&$iplang));
            if ($iplang != "") {
                $client_lang = $iplang;
                $lang_known = true;
            }
        }
        if (!$lang_known && $determitLanguage && key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER) && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
            switch ($newVisitorAction) {
                // usesing the first defined Joom!Fish language
                case 'joomfish':
                    $activeLanguages = $jfm->getActiveLanguages();
                    reset($activeLanguages);
                    $first = key($activeLanguages);
                    $client_lang = $activeLanguages[$first]->getLanguageCode();
                    break;
                case 'site':
                    // We accept that this default locale might be overwritten by user settings!
                    $jfLang = TableJFLanguage::createByJoomla($locale);
                    $client_lang = $jfLang->getLanguageCode();
                    break;
                    // no language chooses - assume from browser configuration
                // no language chooses - assume from browser configuration
                case 'browser':
                default:
                    // language negotiation by Kochin Chang, June 16, 2004
                    // retrieve active languages from database
                    $active_iso = array();
                    $active_isocountry = array();
                    $active_code = array();
                    $activeLanguages = $jfm->getActiveLanguages();
                    if (count($activeLanguages) == 0) {
                        return;
                    }
                    foreach ($activeLanguages as $alang) {
                        $active_iso[] = $alang->iso;
                        if (preg_match('/[_-]/i', $alang->iso)) {
                            $isocountry = preg_split('[_-]', $alang->iso);
                            $active_isocountry[] = $isocountry[0];
                        }
                        $active_code[] = $alang->shortcode;
                    }
                    // figure out which language to use - browser languages are based on ISO codes
                    $browserLang = explode(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
                    foreach ($browserLang as $blang) {
                        if (in_array($blang, $active_iso)) {
                            $client_lang = $blang;
                            break;
                        }
                        $shortLang = substr($blang, 0, 2);
                        if (in_array($shortLang, $active_isocountry)) {
                            $client_lang = $shortLang;
                            break;
                        }
                        // compare with code
                        if (in_array($shortLang, $active_code)) {
                            $client_lang = $shortLang;
                            break;
                        }
                    }
                    break;
            }
        }
        // get the name of the language file for joomla
        $jfLang = TableJFLanguage::createByShortcode($client_lang, false);
        if ($jfLang === null && $client_lang != "") {
            $jfLang = TableJFLanguage::createByISO($client_lang, false);
        } else {
            if ($jfLang === null) {
                $jfLang = TableJFLanguage::createByJoomla($locale);
            }
        }
        if (!$lang_known && $use302redirect) {
            // using a 302 redirect means that we do not change the language on the fly the first time, but with a clean reload of the page
            $href = "index.php";
            $hrefVars = '';
            $queryString = JRequest::getVar('QUERY_STRING', null, "SERVER");
            if (!empty($queryString)) {
                $vars = explode("&", $queryString);
                if (count($vars) > 0 && $queryString) {
                    foreach ($vars as $var) {
                        if (preg_match('/=/i', $var)) {
                            list($key, $value) = explode("=", $var);
                            if ($key != "lang") {
                                if ($hrefVars != "") {
                                    $hrefVars .= "&";
                                }
                                // ignore mosmsg to ensure it is visible in frontend
                                if ($key != 'mosmsg') {
                                    $hrefVars .= "{$key}={$value}";
                                }
                            }
                        }
                    }
                }
            }
            // Add the existing variables
            if ($hrefVars != "") {
                $href .= '?' . $hrefVars;
            }
            if ($jfLang->getLanguageCode() != null) {
                $ulang = 'lang=' . $jfLang->getLanguageCode();
            } else {
                // it's important that we add at least the basic parameter - as of the sef is adding the actual otherwise!
                $ulang = 'lang=';
            }
            // if there are other vars we need to add a & otherwiese ?
            if ($hrefVars == '') {
                $href .= '?' . $ulang;
            } else {
                $href .= '&' . $ulang;
            }
            $registry->setValue("config.multilingual_support", true);
            global $mainframe;
            $mainframe->setUserState('application.lang', $jfLang->code);
            $registry->setValue("config.jflang", $jfLang->code);
            $registry->setValue("config.lang_site", $jfLang->code);
            $registry->setValue("config.language", $jfLang->code);
            $registry->setValue("joomfish.language", $jfLang);
            $href = JRoute::_($href, false);
            header('HTTP/1.1 303 See Other');
            header("Location: " . $href);
            exit;
        }
        if (isset($jfLang) && $jfLang->code != "" && ($jfLang->active || $jfm->getCfg("frontEndPreview"))) {
            $locale = $jfLang->code;
        } else {
            $jfLang = TableJFLanguage::createByJoomla($locale);
            if (!$jfLang->active) {
                ?>
			<div style="background-color: #c00; color: #fff">
				<p style="font-size: 1.5em; font-weight: bold; padding: 10px 0px 10px 0px; text-align: center; font-family: Arial, Helvetica, sans-serif;">
				Joom!Fish config error: Default language is inactive!<br />&nbsp;<br />
				Please check configuration, try to use first active language</p>
			</div>
			<?php 
                $activeLanguages = $jfm->getActiveLanguages();
                if (count($activeLanguages) > 0) {
                    $jfLang = $activeLanguages[0];
                    $locale = $jfLang->code;
                } else {
                    // No active language defined - using system default is only alternative!
                }
            }
            $client_lang = $jfLang->shortcode != '' ? $jfLang->shortcode : $jfLang->iso;
        }
        // TODO set the cookie domain so that it works for all subdomains
        if ($enableCookie) {
            setcookie("lang", "", time() - 1800, "/");
            setcookie("jfcookie", "", time() - 1800, "/");
            setcookie("jfcookie[lang]", $client_lang, time() + 24 * 3600, '/');
        }
        if (defined("_JLEGACY")) {
            $GLOBALS['iso_client_lang'] = $client_lang;
            $GLOBALS['mosConfig_lang'] = $jfLang->code;
        }
        $registry->setValue("config.multilingual_support", true);
        global $mainframe;
        $mainframe->setUserState('application.lang', $jfLang->code);
        $registry->setValue("config.jflang", $jfLang->code);
        $registry->setValue("config.lang_site", $jfLang->code);
        $registry->setValue("config.language", $jfLang->code);
        $registry->setValue("joomfish.language", $jfLang);
        // Force factory static instance to be updated if necessary
        $lang =& JFactory::getLanguage();
        if ($jfLang->code != $lang->getTag()) {
            // Must not assign by reference in order to overwrite the existing reference to the static instance of the language
            $lang = JFactory::_createLanguage();
            $lang->setDefault($jfLang->code);
            $lang->_metadata = array();
            $lang->_metadata['tag'] = $jfLang->code;
            $lang->_metadata['rtl'] = false;
        }
        // no need to set locale for this ISO code its done by JLanguage
        // overwrite with the valued from $jfLang
        $jfparams = JComponentHelper::getParams("com_joomfish");
        $overwriteGlobalConfig = $jfparams->get('overwriteGlobalConfig', 0);
        if ($overwriteGlobalConfig) {
            // We should overwrite additional global variables based on the language parameter configuration
            $params = new JParameter($jfLang->params);
            $paramarray = $params->toArray();
            foreach ($paramarray as $key => $val) {
                if (trim($val) !== '') {
                    $registry->setValue("config." . $key, $val);
                }
                if (defined("_JLEGACY")) {
                    $name = 'mosConfig_' . $key;
                    $GLOBALS[$name] = $val;
                }
            }
        }
    }
コード例 #2
0
 function _determineLanguage($getLang = null, $redir = false, $useMainLang = false)
 {
     // set the language for JoomFish
     if (SEFTools::JoomFishInstalled()) {
         $sefConfig =& SEFConfig::getConfig();
         $registry =& JFactory::getConfig();
         // Check if the Jfrouter is enabled
         $jfrouterEnabled = JPluginHelper::isEnabled('system', 'jfrouter');
         // save the default language of the site if needed
         if (!$jfrouterEnabled) {
             $locale = $registry->getValue('config.language');
             $GLOBALS['mosConfig_defaultLang'] = $locale;
             $registry->setValue("config.defaultlang", $locale);
         }
         // get instance of JoomFishManager to obtain active language list and config values
         $jfm =& JoomFishManager::getInstance();
         // Get language from request
         if (!empty($getLang)) {
             $lang = $getLang;
         }
         // Try to get language code from JF cookie
         if ($sefConfig->jfLangCookie) {
             $jfCookie = JRequest::getVar('jfcookie', null, 'COOKIE');
             if (isset($jfCookie['lang'])) {
                 $cookieCode = $jfCookie['lang'];
             }
         }
         // Try to find language from browser settings
         if ($sefConfig->jfBrowserLang && isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
             $active_iso = array();
             $active_isocountry = array();
             $active_code = array();
             $activeLanguages = $jfm->getActiveLanguages();
             if (count($activeLanguages) > 0) {
                 foreach ($activeLanguages as $alang) {
                     $active_iso[] = $alang->iso;
                     if (preg_match('/[_-]/i', $alang->iso)) {
                         $iso = str_replace('_', '-', $alang->iso);
                         $isocountry = explode('-', $iso);
                         $active_isocountry[] = $isocountry[0];
                     }
                     $active_code[] = $alang->shortcode;
                 }
                 // figure out which language to use - browser languages are based on ISO codes
                 $browserLang = explode(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
                 foreach ($browserLang as $blang) {
                     if (in_array($blang, $active_iso)) {
                         $client_lang = $blang;
                         break;
                     }
                     $shortLang = substr($blang, 0, 2);
                     if (in_array($shortLang, $active_isocountry)) {
                         $client_lang = $shortLang;
                         break;
                     }
                     // compare with code
                     if (in_array($shortLang, $active_code)) {
                         $client_lang = $shortLang;
                         break;
                     }
                 }
                 if (!empty($client_lang)) {
                     if (strlen($client_lang) == 2) {
                         $browserCode = SEFTools::getLangLongCode($client_lang);
                     } else {
                         $browserCode = $client_lang;
                     }
                 }
             }
         }
         if (!$jfrouterEnabled && $redir && $sefConfig->langPlacement != _COM_SEF_LANG_DOMAIN && (isset($cookieCode) || isset($browserCode)) && $sefConfig->mainLanguage != '0') {
             if (isset($cookieCode)) {
                 $sc = SEFTools::getLangCode($cookieCode);
             } else {
                 $sc = SEFTools::getLangCode($browserCode);
             }
             // Check the referer to see if we should redirect
             $shouldRedir = false;
             if (isset($_SERVER['HTTP_REFERER'])) {
                 $refUri = new JURI($_SERVER['HTTP_REFERER']);
                 $uri = JURI::getInstance();
                 $refHost = $refUri->getHost();
                 $host = $uri->getHost();
                 if ($refHost != $host) {
                     $shouldRedir = true;
                 }
             } else {
                 $shouldRedir = true;
             }
             if ($shouldRedir) {
                 if (!empty($lang) && $sc != $lang || empty($lang) && $sc != $sefConfig->mainLanguage) {
                     // Redirect to correct site
                     $mainframe =& JFactory::getApplication();
                     $href = JRoute::_('index.php?lang=' . $sc, false);
                     $mainframe->redirect($href);
                     exit;
                 }
             }
         }
         // Check if language is selected
         if (empty($lang) && !$jfrouterEnabled) {
             // If route and query string are empty, use the main language
             // note: removed  && $redir  - it was not possible to switch language to main language
             // on other page than homepage (let's see if it causes any other problem)
             // note: added $useMainLang - now it should work for both the VM checkout and using
             // main language with component's own router
             if ($useMainLang && (empty($code) || !JLanguage::exists($code))) {
                 if ($sefConfig->mainLanguage != '0') {
                     $code = SEFTools::GetLangLongCode($sefConfig->mainLanguage);
                 }
             }
             // Try to get language code from JF cookie
             if (empty($code) || !JLanguage::exists($code)) {
                 if (isset($cookieCode)) {
                     $code = $cookieCode;
                 }
             }
             // Try to get language from browser if needed
             if (empty($code) || !JLanguage::exists($code)) {
                 if (isset($browserCode)) {
                     $code = $browserCode;
                 }
             }
             // Get language from configuration if needed
             if (empty($code) || !JLanguage::exists($code)) {
                 if ($sefConfig->mainLanguage != '0') {
                     $code = SEFTools::GetLangLongCode($sefConfig->mainLanguage);
                 }
             }
             // Get default language if needed
             if (empty($code) || !JLanguage::exists($code)) {
                 $code = $registry->getValue('config.language');
             }
         }
         // get language long code if needed
         if (empty($code)) {
             if (empty($lang)) {
                 return;
             }
             $code = SEFTools::getLangLongCode($lang);
         }
         if (!empty($code)) {
             $jfrparams = $registry->getValue('jfrouter.params');
             // set the site language
             $resetLang = false;
             if ($code != SEFTools::getLangLongCode()) {
                 if (!$jfrouterEnabled || $jfrouterEnabled && $jfrparams->get('sefordomain', 'sefprefix') == 'sefprefix') {
                     $language =& JFactory::getLanguage();
                     $language->setLanguage($code);
                     $language->load();
                     // set the backward compatible language
                     $backLang = $language->getBackwardLang();
                     $GLOBALS['mosConfig_lang'] = $backLang;
                     $registry->setValue("config.lang", $backLang);
                     $resetLang = true;
                 }
             }
             // set joomfish language if needed
             if ($resetLang || !$jfrouterEnabled) {
                 $jfLang = TableJFLanguage::createByJoomla($code);
                 $registry->setValue("joomfish.language", $jfLang);
                 // set some more variables
                 $mainframe =& JFactory::getApplication();
                 $registry->setValue("config.multilingual_support", true);
                 $mainframe->setUserState('application.lang', $jfLang->code);
                 $registry->setValue("config.jflang", $jfLang->code);
                 $registry->setValue("config.lang_site", $jfLang->code);
                 $registry->setValue("config.language", $jfLang->code);
                 $registry->setValue("joomfish.language", $jfLang);
                 // overwrite global config with values from $jfLang if set to in JoomFish
                 $jfparams = JComponentHelper::getParams("com_joomfish");
                 $overwriteGlobalConfig = $jfparams->get('overwriteGlobalConfig', 0);
                 if ($overwriteGlobalConfig) {
                     // We should overwrite additional global variables based on the language parameter configuration
                     $langParams = new JParameter($jfLang->params);
                     $paramarray = $langParams->toArray();
                     foreach ($paramarray as $key => $val) {
                         $registry->setValue("config." . $key, $val);
                         if (defined("_JLEGACY")) {
                             $name = 'mosConfig_' . $key;
                             $GLOBALS[$name] = $val;
                         }
                     }
                 }
                 // set the cookie with language
                 if (!$jfrouterEnabled && $sefConfig->jfLangCookie || $jfrouterEnabled && $jfrparams->get('enableCookie', 1)) {
                     setcookie("lang", "", time() - 1800, "/");
                     setcookie("jfcookie", "", time() - 1800, "/");
                     setcookie("jfcookie[lang]", $code, time() + 24 * 3600, '/');
                 }
             }
         }
     }
 }
コード例 #3
0
ファイル: uri.php プロジェクト: alesconti/FF_2015
 function determineLanguage($get_lang = null)
 {
     // Set the language for JoomFish
     if (AcesefUtility::JoomFishInstalled()) {
         $registry = JFactory::getConfig();
         // save the default language of the site if needed
         $locale = $registry->get('config.language');
         $GLOBALS['mosConfig_defaultLang'] = $locale;
         $registry->set("config.defaultlang", $locale);
         // Get language from request
         if (!empty($get_lang)) {
             $lang = $get_lang;
         }
         // Try to get language code from JF cookie
         if ($this->AcesefConfig->joomfish_cookie) {
             $jf_cookie = JRequest::getVar('jfcookie', null, 'COOKIE');
             if (isset($jf_cookie['lang'])) {
                 $cookieCode = $jf_cookie['lang'];
             }
         }
         // Try to find language from browser settings
         if ($this->AcesefConfig->joomfish_browser && isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) && class_exists('JoomFishManager')) {
             $active_iso = array();
             $active_isocountry = array();
             $active_code = array();
             $active_languages = JoomFishManager::getInstance()->getActiveLanguages();
             if (count($active_languages) > 0) {
                 foreach ($active_languages as $a_lang) {
                     $active_iso[] = $a_lang->iso;
                     if (preg_match('/[_-]/i', $a_lang->iso)) {
                         $iso = str_replace('_', '-', $a_lang->iso);
                         $isocountry = explode('-', $iso);
                         $active_isocountry[] = $isocountry[0];
                     }
                     $active_code[] = $a_lang->shortcode;
                 }
                 // figure out which language to use - browser languages are based on ISO codes
                 $browser_lang = explode(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
                 foreach ($browser_lang as $b_lang) {
                     if (in_array($b_lang, $active_iso)) {
                         $client_lang = $b_lang;
                         break;
                     }
                     $short_lang = substr($b_lang, 0, 2);
                     if (in_array($short_lang, $active_isocountry)) {
                         $client_lang = $short_lang;
                         break;
                     }
                     // compare with code
                     if (in_array($short_lang, $active_code)) {
                         $client_lang = $short_lang;
                         break;
                     }
                 }
                 if (!empty($client_lang)) {
                     if (strlen($client_lang) == 2) {
                         $browser_code = self::getLangLongCode($client_lang);
                     } else {
                         $browser_code = $client_lang;
                     }
                 }
             }
         }
         // Check if language is selected
         if (empty($lang)) {
             if (empty($code) || !JLanguage::exists($code)) {
                 if ($this->AcesefConfig->joomfish_main_lang != '0') {
                     $code = self::getLangLongCode($this->AcesefConfig->joomfish_main_lang);
                 }
             }
             // Try to get language code from JF cookie
             if (empty($code) || !JLanguage::exists($code)) {
                 if (isset($cookieCode)) {
                     $code = $cookieCode;
                 }
             }
             // Try to get language from browser if needed
             if (empty($code) || !JLanguage::exists($code)) {
                 if (isset($browser_code)) {
                     $code = $browser_code;
                 }
             }
             // Get language from configuration if needed
             if (empty($code) || !JLanguage::exists($code)) {
                 if ($this->AcesefConfig->joomfish_main_lang != '0') {
                     $code = self::getLangLongCode($this->AcesefConfig->joomfish_main_lang);
                 }
             }
             // Get default language if needed
             if (empty($code) || !JLanguage::exists($code)) {
                 $code = $registry->get('config.language');
             }
         }
         // get language long code if needed
         if (empty($code)) {
             if (empty($lang)) {
                 return;
             }
             $code = self::getLangLongCode($lang);
         }
         if (!empty($code)) {
             // set the site language
             $reset_lang = false;
             if ($code != self::getLangLongCode()) {
                 $language = JFactory::getLanguage();
                 $language->setLanguage($code);
                 $language->load();
                 // set the backward compatible language
                 $back_lang = $language->getBackwardLang();
                 $GLOBALS['mosConfig_lang'] = $back_lang;
                 $registry->set("config.lang", $back_lang);
                 $reset_lang = true;
             }
             // set joomfish language if needed
             if ($reset_lang) {
                 $jf_lang = TableJFLanguage::createByJoomla($code);
                 $registry->set("joomfish.language", $jf_lang);
                 // set some more variables
                 $mainframe = JFactory::getApplication();
                 $registry->set("config.multilingual_support", true);
                 $mainframe->setUserState('application.lang', $jf_lang->code);
                 $registry->set("config.jflang", $jf_lang->code);
                 $registry->set("config.lang_site", $jf_lang->code);
                 $registry->set("config.language", $jf_lang->code);
                 $registry->set("joomfish.language", $jf_lang);
                 // overwrite global config with values from $jf_lang if set to in JoomFish
                 $jf_params = JComponentHelper::getParams("com_joomfish");
                 $overwriteGlobalConfig = $jf_params->get('overwriteGlobalConfig', 0);
                 if ($overwriteGlobalConfig) {
                     // We should overwrite additional global variables based on the language parameter configuration
                     $lang_params = new JParameter($jf_lang->params);
                     $param_array = $lang_params->toArray();
                     foreach ($param_array as $key => $val) {
                         $registry->set("config." . $key, $val);
                         if (defined("_JLEGACY")) {
                             $name = 'mosConfig_' . $key;
                             $GLOBALS[$name] = $val;
                         }
                     }
                 }
                 // set the cookie with language
                 if ($this->AcesefConfig->joomfish_cookie) {
                     setcookie("lang", "", time() - 1800, "/");
                     setcookie("jfcookie", "", time() - 1800, "/");
                     setcookie("jfcookie[lang]", $code, time() + 24 * 3600, '/');
                 }
             }
         }
     }
 }