Beispiel #1
0
 /**
  * Add parse rule to router.
  *
  * @param   JRouter  &$router  JRouter object.
  * @param   JUri     &$uri     JUri object.
  *
  * @return  void
  *
  * @since   1.6
  */
 public function parseRule(&$router, &$uri)
 {
     // Did we find the current and existing language yet?
     $found = false;
     $lang_code = false;
     // Are we in SEF mode or not?
     if ($this->mode_sef) {
         $path = $uri->getPath();
         $parts = explode('/', $path);
         $sef = $parts[0];
         // Do we have a URL Language Code ?
         if (!isset($this->sefs[$sef])) {
             // Check if remove default url language code is set
             if ($this->params->get('remove_default_prefix', 0)) {
                 if ($parts[0]) {
                     // We load a default site language page
                     $lang_code = $this->default_lang;
                 } else {
                     // We check for an existing language cookie
                     $lang_code = $this->getLanguageCookie();
                 }
             } else {
                 $lang_code = $this->getLanguageCookie();
             }
             // No language code. Try using browser settings or default site language
             if (!$lang_code && $this->params->get('detect_browser', 0) == 1) {
                 $lang_code = JLanguageHelper::detectLanguage();
             }
             if (!$lang_code) {
                 $lang_code = $this->default_lang;
             }
             if ($this->params->get('remove_default_prefix', 0) && $lang_code == $this->default_lang) {
                 $found = true;
             }
         } else {
             // We found our language
             $found = true;
             $lang_code = $this->sefs[$sef]->lang_code;
             // If we found our language, but its the default language and we don't want a prefix for that, we are on a wrong URL.
             // Or we try to change the language back to the default language. We need a redirect to the proper URL for the default language.
             if ($this->params->get('remove_default_prefix', 0) && $lang_code == $this->default_lang) {
                 // Create a cookie.
                 $this->setLanguageCookie($lang_code);
                 $found = false;
                 array_shift($parts);
                 $path = implode('/', $parts);
             }
             // We have found our language and the first part of our URL is the language prefix
             if ($found) {
                 array_shift($parts);
                 $uri->setPath(implode('/', $parts));
             }
         }
     } else {
         $lang_code = $this->getLanguageCookie();
         if ($this->params->get('detect_browser', 1) && !$lang_code) {
             $lang_code = JLanguageHelper::detectLanguage();
         }
         if (!isset($this->lang_codes[$lang_code])) {
             $lang_code = $this->default_lang;
         }
     }
     $lang = $uri->getVar('lang', $lang_code);
     if (isset($this->sefs[$lang])) {
         // We found our language
         $found = true;
         $lang_code = $this->sefs[$lang]->lang_code;
     }
     // We are called via POST. We don't care about the language
     // and simply set the default language as our current language.
     if ($this->app->input->getMethod() == "POST" || count($this->app->input->post) > 0 || count($this->app->input->files) > 0) {
         $found = true;
         if (!isset($lang_code)) {
             $lang_code = $this->getLanguageCookie();
         }
         if ($this->params->get('detect_browser', 1) && !$lang_code) {
             $lang_code = JLanguageHelper::detectLanguage();
         }
         if (!isset($this->lang_codes[$lang_code])) {
             $lang_code = $this->default_lang;
         }
     }
     // We have not found the language and thus need to redirect
     if (!$found) {
         // Lets find the default language for this user
         if (!isset($lang_code) || !isset($this->lang_codes[$lang_code])) {
             $lang_code = false;
             if ($this->params->get('detect_browser', 1)) {
                 $lang_code = JLanguageHelper::detectLanguage();
                 if (!isset($this->lang_codes[$lang_code])) {
                     $lang_code = false;
                 }
             }
             if (!$lang_code) {
                 $lang_code = $this->default_lang;
             }
         }
         if ($this->mode_sef) {
             // Use the current language sef or the default one.
             if (!$this->params->get('remove_default_prefix', 0) || $lang_code != $this->default_lang) {
                 $path = $this->lang_codes[$lang_code]->sef . '/' . $path;
             }
             $uri->setPath($path);
             if (!$this->app->get('sef_rewrite')) {
                 $uri->setPath('index.php/' . $uri->getPath());
             }
             $redirectUri = $uri->base() . $uri->toString(array('path', 'query', 'fragment'));
         } else {
             $uri->setVar('lang', $this->lang_codes[$lang_code]->sef);
             $redirectUri = $uri->base() . 'index.php?' . $uri->getQuery();
         }
         // Set redirect HTTP code to "302 Found".
         $redirectHttpCode = 302;
         // If selected language is the default language redirect code is "301 Moved Permanently".
         if ($lang_code === $this->default_lang) {
             $redirectHttpCode = 301;
             // We cannot cache this redirect in browser. 301 is cachable by default so we need to force to not cache it in browsers.
             $this->app->setHeader('Expires', 'Wed, 17 Aug 2005 00:00:00 GMT', true);
             $this->app->setHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT', true);
             $this->app->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', false);
             $this->app->setHeader('Pragma', 'no-cache');
             $this->app->sendHeaders();
         }
         // Redirect to language.
         $this->app->redirect($redirectUri, $redirectHttpCode);
     }
     // We have found our language and now need to set the cookie and the language value in our system
     $array = array('lang' => $lang_code);
     $this->current_lang = $lang_code;
     // Set the request var.
     $this->app->input->set('language', $lang_code);
     $this->app->set('language', $lang_code);
     $language = JFactory::getLanguage();
     if ($language->getTag() != $lang_code) {
         $newLang = JLanguage::getInstance($lang_code);
         foreach ($language->getPaths() as $extension => $files) {
             $newLang->load($extension);
         }
         JFactory::$language = $newLang;
         $this->app->loadLanguage($newLang);
     }
     // Create a cookie.
     if ($this->getLanguageCookie() != $lang_code) {
         $this->setLanguageCookie($lang_code);
     }
     return $array;
 }
Beispiel #2
0
 /**
  * {@inheritdoc}
  */
 public function setHeader($name, $value)
 {
     $this->_app->setHeader($name, $value, true);
 }