Source for file languagefilter.php

Documentation is available at languagefilter.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  System.languagefilter
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. JLoader::register('MenusHelper'JPATH_ADMINISTRATOR '/components/com_menus/helpers/menus.php');
  13.  
  14. JLoader::register('MultilangstatusHelper'JPATH_ADMINISTRATOR.'/components/com_languages/helpers/multilangstatus.php');
  15.  
  16. /**
  17.  * Joomla! Language Filter Plugin
  18.  *
  19.  * @package     Joomla.Plugin
  20.  * @subpackage  System.languagefilter
  21.  * @since       1.6
  22.  */
  23. {
  24.     protected static $mode_sef;
  25.  
  26.     protected static $tag;
  27.  
  28.     protected static $sefs;
  29.  
  30.     protected static $lang_codes;
  31.  
  32.     protected static $homes;
  33.  
  34.     protected static $default_lang;
  35.  
  36.     protected static $default_sef;
  37.  
  38.     protected static $cookie;
  39.  
  40.     private static $_user_lang_code;
  41.  
  42.     public function __construct(&$subject$config)
  43.     {
  44.         parent::__construct($subject$config);
  45.  
  46.         // Ensure that constructor is called one time
  47.         self::$cookie SID == '';
  48.         if (!self::$default_lang)
  49.         {
  50.             $app JFactory::getApplication();
  51.             $router $app->getRouter();
  52.  
  53.             if ($app->isSite())
  54.             {
  55.                 // setup language data
  56.                 self::$mode_sef     ($router->getMode(== JROUTER_MODE_SEFtrue false;
  57.                 self::$sefs         JLanguageHelper::getLanguages('sef');
  58.                 self::$lang_codes     JLanguageHelper::getLanguages('lang_code');
  59.                 self::$default_lang JComponentHelper::getParams('com_languages')->get('site''en-GB');
  60.                 self::$default_sef     self::$lang_codes[self::$default_lang]->sef;
  61.                 self::$homes        MultilangstatusHelper::getHomes();
  62.  
  63.                 $user JFactory::getUser();
  64.                 $levels $user->getAuthorisedViewLevels();
  65.  
  66.                 foreach (self::$sefs as $sef => &$language)
  67.                 {
  68.                     if (isset($language->access&& $language->access && !in_array($language->access$levels))
  69.                     {
  70.                         unset(self::$sefs[$sef]);
  71.                     }
  72.                 }
  73.  
  74.                 $app->setLanguageFilter(true);
  75.                 jimport('joomla.environment.uri');
  76.                 $uri JUri::getInstance();
  77.  
  78.                 if (self::$mode_sef)
  79.                 {
  80.                     // Get the route path from the request.
  81.                     $path JString::substr($uri->toString()JString::strlen($uri->base()));
  82.  
  83.                     // Apache mod_rewrite is Off
  84.                     $path JFactory::getConfig()->get('sef_rewrite'$path JString::substr($path10);
  85.  
  86.                     // Trim any spaces or slashes from the ends of the path and explode into segments.
  87.                     $path  JString::trim($path'/ ');
  88.                     $parts explode('/'$path);
  89.  
  90.                     // The language segment is always at the beginning of the route path if it exists.
  91.                     $sef $uri->getVar('lang');
  92.  
  93.                     if (!empty($parts&& empty($sef))
  94.                     {
  95.                         $sef reset($parts);
  96.                     }
  97.                 }
  98.                 else
  99.                 {
  100.                     $sef $uri->getVar('lang');
  101.                 }
  102.  
  103.                 if (isset(self::$sefs[$sef]))
  104.                 {
  105.                     $lang_code self::$sefs[$sef]->lang_code;
  106.                     // Create a cookie
  107.                     $conf JFactory::getConfig();
  108.                     $cookie_domain     $conf->get('config.cookie_domain''');
  109.                     $cookie_path     $conf->get('config.cookie_path''/');
  110.                     setcookie(JApplication::getHash('language')$lang_code$this->getLangCookieTime()$cookie_path$cookie_domain);
  111.                     $app->input->cookie->set(JApplication::getHash('language')$lang_code);
  112.                     // set the request var
  113.                     $app->input->set('language'$lang_code);
  114.                 }
  115.             }
  116.             parent::__construct($subject$config);
  117.             //     Detect browser feature
  118.             if ($app->isSite())
  119.             {
  120.                 $app->setDetectBrowser($this->params->get('detect_browser''1'== '1');
  121.             }
  122.         }
  123.     }
  124.  
  125.     public function onAfterInitialise()
  126.     {
  127.         $app JFactory::getApplication();
  128.         $app->item_associations $this->params->get('item_associations'0);
  129.  
  130.         if ($app->isSite())
  131.         {
  132.             self::$tag JFactory::getLanguage()->getTag();
  133.  
  134.             $router $app->getRouter();
  135.             // attach build rules for language SEF
  136.             $router->attachBuildRule(array($this'buildRule'));
  137.  
  138.             // attach parse rules for language SEF
  139.             $router->attachParseRule(array($this'parseRule'));
  140.  
  141.             // Adding custom site name
  142.             $languages JLanguageHelper::getLanguages('lang_code');
  143.             if (isset($languages[self::$tag]&& $languages[self::$tag]->sitename)
  144.             {
  145.                 JFactory::getConfig()->set('sitename'$languages[self::$tag]->sitename);
  146.             }
  147.         }
  148.     }
  149.  
  150.     public function buildRule(&$router&$uri)
  151.     {
  152.         $sef $uri->getVar('lang');
  153.         if (empty($sef))
  154.         {
  155.             $sef self::$lang_codes[self::$tag]->sef;
  156.         }
  157.         elseif (!isset(self::$sefs[$sef]))
  158.         {
  159.             $sef self::$default_sef;
  160.         }
  161.  
  162.         $Itemid $uri->getVar('Itemid');
  163.         if (!is_null($Itemid))
  164.         {
  165.             if ($item JFactory::getApplication()->getMenu()->getItem($Itemid))
  166.             {
  167.                 if ($item->home && $uri->getVar('option'!= 'com_search')
  168.                 {
  169.                     $link $item->link;
  170.                     $parts JString::parse_url($link);
  171.  
  172.                     if (isset ($parts['query']&& strpos($parts['query']'&amp;'))
  173.                     {
  174.                         $parts['query'str_replace('&amp;''&'$parts['query']);
  175.                     }
  176.                     parse_str($parts['query']$vars);
  177.  
  178.                     // test if the url contains same vars as in menu link
  179.                     $test true;
  180.  
  181.                     foreach ($uri->getQuery(trueas $key => $value)
  182.                     {
  183.                         if (!in_array($keyarray('format''Itemid''lang')) && !(isset($vars[$key]&& $vars[$key== $value))
  184.                         {
  185.                             $test false;
  186.                             break;
  187.                         }
  188.                     }
  189.  
  190.                     if ($test)
  191.                     {
  192.                         foreach ($vars as $key => $value)
  193.                         {
  194.                             $uri->delVar($key);
  195.                         }
  196.                         $uri->delVar('Itemid');
  197.                     }
  198.                 }
  199.             }
  200.             else
  201.             {
  202.                 $uri->delVar('Itemid');
  203.             }
  204.         }
  205.  
  206.         if (self::$mode_sef)
  207.         {
  208.             $uri->delVar('lang');
  209.             if (
  210.                 $this->params->get('remove_default_prefix'0== 0
  211.                 || $sef != self::$default_sef
  212.                 || $sef != self::$lang_codes[self::$tag]->sef
  213.                 || $this->params->get('detect_browser'1&& JLanguageHelper::detectLanguage(!= self::$tag && !self::$cookie
  214.             )
  215.             {
  216.                 $uri->setPath($uri->getPath().'/'.$sef.'/');
  217.             }
  218.             else
  219.             {
  220.                 $uri->setPath($uri->getPath());
  221.             }
  222.         }
  223.         else
  224.         {
  225.             $uri->setVar('lang'$sef);
  226.         }
  227.     }
  228.  
  229.     public function parseRule(&$router&$uri)
  230.     {
  231.         $app JFactory::getApplication();
  232.  
  233.         $lang_code $app->input->cookie->getString(JApplication::getHash('language'));
  234.         // No cookie - let's try to detect browser language or use site default
  235.         if (!$lang_code)
  236.         {
  237.             if ($this->params->get('detect_browser'1))
  238.             {
  239.                 $lang_code JLanguageHelper::detectLanguage();
  240.             }
  241.             else
  242.             {
  243.                 $lang_code self::$default_lang;
  244.             }
  245.         }
  246.  
  247.         if (self::$mode_sef)
  248.         {
  249.             $path $uri->getPath();
  250.             $parts explode('/'$path);
  251.  
  252.             $sef $parts[0];
  253.  
  254.             // Redirect only if not in post
  255.             if (!empty($lang_code&& ($app->input->getMethod(!= "POST" || count($app->input->post== 0))
  256.             {
  257.                 if ($this->params->get('remove_default_prefix'0== 0)
  258.                 {
  259.                     // redirect if sef does not exists
  260.                     if (!isset(self::$sefs[$sef]))
  261.                     {
  262.                         // Use the current language sef or the default one
  263.                         $sef = isset(self::$lang_codes[$lang_code]self::$lang_codes[$lang_code]->sef self::$default_sef;
  264.                         $uri->setPath($sef '/' $path);
  265.  
  266.                         if ($app->getCfg('sef_rewrite'))
  267.                         {
  268.                             $app->redirect($uri->base().$uri->toString(array('path''query''fragment')));
  269.                         }
  270.                         else
  271.                         {
  272.                             $path $uri->toString(array('path''query''fragment'));
  273.                             $app->redirect($uri->base().'index.php'.($path ('/' $path''));
  274.                         }
  275.                     }
  276.                 }
  277.                 else
  278.                 {
  279.                     // redirect if sef does not exists and language is not the default one
  280.                     if (!isset(self::$sefs[$sef]&& $lang_code != self::$default_lang)
  281.                     {
  282.                         $sef = isset(self::$lang_codes[$lang_code]self::$lang_codes[$lang_code]->sef self::$default_sef;
  283.                         $uri->setPath($sef '/' $path);
  284.  
  285.                         if ($app->getCfg('sef_rewrite'))
  286.                         {
  287.                             $app->redirect($uri->base().$uri->toString(array('path''query''fragment')));
  288.                         }
  289.                         else
  290.                         {
  291.                             $path $uri->toString(array('path''query''fragment'));
  292.                             $app->redirect($uri->base().'index.php'.($path ('/' $path''));
  293.                         }
  294.                     }
  295.                     // redirect if sef is the default one
  296.                     elseif (isset(self::$sefs[$sef]&&
  297.                         self::$default_lang == self::$sefs[$sef]->lang_code &&
  298.                         (!$this->params->get('detect_browser'1|| JLanguageHelper::detectLanguage(== self::$tag || self::$cookie)
  299.                     )
  300.                     {
  301.                         array_shift($parts);
  302.                         $uri->setPath(implode('/'$parts));
  303.  
  304.                         if ($app->getCfg('sef_rewrite'))
  305.                         {
  306.                             $app->redirect($uri->base().$uri->toString(array('path''query''fragment')));
  307.                         }
  308.                         else
  309.                         {
  310.                             $path $uri->toString(array('path''query''fragment'));
  311.                             $app->redirect($uri->base().'index.php'.($path ('/' $path''));
  312.                         }
  313.                     }
  314.                 }
  315.             }
  316.  
  317.             $lang_code = isset(self::$sefs[$sef]self::$sefs[$sef]->lang_code '';
  318.             if ($lang_code && JLanguage::exists($lang_code))
  319.             {
  320.                 array_shift($parts);
  321.                 $uri->setPath(implode('/'$parts));
  322.             }
  323.         }
  324.         else
  325.         {
  326.             $sef $uri->getVar('lang');
  327.             if (!isset(self::$sefs[$sef]))
  328.             {
  329.                 $sef = isset(self::$lang_codes[$lang_code]self::$lang_codes[$lang_code]->sef self::$default_sef;
  330.                 $uri->setVar('lang'$sef);
  331.                 if ($app->input->getMethod(!= "POST" || count($app->input->post== 0)
  332.                 {
  333.                     $app->redirect(JUri::base(true).'/index.php?'.$uri->getQuery());
  334.                 }
  335.             }
  336.         }
  337.  
  338.         $array array('lang' => $sef);
  339.         return $array;
  340.     }
  341.     /**
  342.      * before store user method
  343.      *
  344.      * Method is called before user data is stored in the database
  345.      *
  346.      * @param   array  $user    Holds the old user data.
  347.      * @param   boolean        $isnew    True if a new user is stored.
  348.      * @param   array  $new    Holds the new user data.
  349.      *
  350.      * @return  void 
  351.      * @since   1.6
  352.      */
  353.     public function onUserBeforeSave($user$isnew$new)
  354.     {
  355.         if ($this->params->get('automatic_change''1'== '1' && key_exists('params'$user))
  356.         {
  357.             $registry new JRegistry;
  358.             $registry->loadString($user['params']);
  359.             self::$_user_lang_code $registry->get('language');
  360.  
  361.             if (empty(self::$_user_lang_code))
  362.             {
  363.                 self::$_user_lang_code self::$default_lang;
  364.             }
  365.         }
  366.     }
  367.  
  368.     /**
  369.      * after store user method
  370.      *
  371.      * Method is called after user data is stored in the database
  372.      *
  373.      * @param   array  $user        Holds the new user data.
  374.      * @param   boolean        $isnew        True if a new user is stored.
  375.      * @param   boolean        $success    True if user was succesfully stored in the database.
  376.      * @param   string  $msg        Message.
  377.      *
  378.      * @return  void 
  379.      * @since   1.6
  380.      */
  381.     public function onUserAfterSave($user$isnew$success$msg)
  382.     {
  383.         if ($this->params->get('automatic_change''1'== '1' && key_exists('params'$user&& $success)
  384.         {
  385.             $registry new JRegistry;
  386.             $registry->loadString($user['params']);
  387.             $lang_code $registry->get('language');
  388.  
  389.             if (empty($lang_code))
  390.             {
  391.                 $lang_code self::$default_lang;
  392.             }
  393.  
  394.             $app JFactory::getApplication();
  395.             if ($lang_code == self::$_user_lang_code || !isset(self::$lang_codes[$lang_code]))
  396.             {
  397.                 if ($app->isSite())
  398.                 {
  399.                     $app->setUserState('com_users.edit.profile.redirect'null);
  400.                 }
  401.             }
  402.             else
  403.             {
  404.                 if ($app->isSite())
  405.                 {
  406.                     $app->setUserState('com_users.edit.profile.redirect''index.php?Itemid='.$app->getMenu()->getDefault($lang_code)->id.'&lang='.self::$lang_codes[$lang_code]->sef);
  407.                     self::$tag $lang_code;
  408.                     // Create a cookie
  409.                     $conf JFactory::getConfig();
  410.                     $cookie_domain     $conf->get('config.cookie_domain''');
  411.                     $cookie_path     $conf->get('config.cookie_path''/');
  412.                     setcookie(JApplication::getHash('language')$lang_code$this->getLangCookieTime()$cookie_path$cookie_domain);
  413.                 }
  414.             }
  415.         }
  416.     }
  417.  
  418.     /**
  419.      * This method should handle any login logic and report back to the subject
  420.      *
  421.      * @param   array  $user        Holds the user data
  422.      * @param   array  $options    Array holding options (remember, autoregister, group)
  423.      *
  424.      * @return  boolean  True on success
  425.      * @since   1.5
  426.      */
  427.     public function onUserLogin($user$options array())
  428.     {
  429.         $app  JFactory::getApplication();
  430.         $menu $app->getMenu();
  431.         if ($app->isSite(&& $this->params->get('automatic_change'1))
  432.         {
  433.             // Load associations
  434.             $assoc JLanguageAssociations::isEnabled();
  435.  
  436.             if ($assoc)
  437.             {
  438.                 $active $menu->getActive();
  439.                 if ($active)
  440.                 {
  441.                     $associations MenusHelper::getAssociations($active->id);
  442.                 }
  443.             }
  444.  
  445.             $lang_code $user['language'];
  446.             if (empty($lang_code))
  447.             {
  448.                 $lang_code self::$default_lang;
  449.             }
  450.  
  451.             if ($lang_code != self::$tag)
  452.             {
  453.                 // Change language
  454.                 self::$tag $lang_code;
  455.  
  456.                 // Create a cookie
  457.                 $conf JFactory::getConfig();
  458.                 $cookie_domain     $conf->get('config.cookie_domain''');
  459.                 $cookie_path     $conf->get('config.cookie_path''/');
  460.                 setcookie(JApplication::getHash('language')$lang_code$this->getLangCookieTime()$cookie_path$cookie_domain);
  461.  
  462.                 // Change the language code
  463.                 JFactory::getLanguage()->setLanguage($lang_code);
  464.  
  465.                 // Change the redirect (language have changed)
  466.                 if (isset($associations[$lang_code]&& $menu->getItem($associations[$lang_code]))
  467.                 {
  468.                     $itemid $associations[$lang_code];
  469.                     $app->setUserState('users.login.form.return''index.php?&Itemid='.$itemid);
  470.                 }
  471.                 else
  472.                 {
  473.                     $itemid = isset(self::$homes[$lang_code]self::$homes[$lang_code]->id self::$homes['*']->id;
  474.                     $app->setUserState('users.login.form.return''index.php?&Itemid='.$itemid);
  475.                 }
  476.             }
  477.         }
  478.     }
  479.  
  480.     /**
  481.      * This method adds alternate meta tags for associated menu items
  482.      *
  483.      * @return  nothing 
  484.      * @since   1.7
  485.      */
  486.     public function onAfterDispatch()
  487.     {
  488.         $app JFactory::getApplication();
  489.         $doc JFactory::getDocument();
  490.         $menu $app->getMenu();
  491.         $server JUri::getInstance()->toString(array('scheme''host''port'));
  492.         $option $app->input->get('option');
  493.         $eName JString::ucfirst(JString::str_ireplace('com_'''$option));
  494.  
  495.         if ($app->isSite(&& $this->params->get('alternate_meta'&& $doc->getType(== 'html')
  496.         {
  497.             // Get active menu item
  498.             $active $menu->getActive();
  499.  
  500.             // load menu associations
  501.             if ($active)
  502.             {
  503.  
  504.                 // Get menu item link
  505.                 if ($app->getCfg('sef'))
  506.                 {
  507.                     $active_link JRoute::_('index.php?Itemid='.$active->idfalse);
  508.                 }
  509.                 else
  510.                 {
  511.                     $active_link JRoute::_($active->link.'&Itemid='.$active->idfalse);
  512.                 }
  513.  
  514.                 if ($active_link == JUri::base(true).'/')
  515.                 {
  516.                     $active_link .= 'index.php';
  517.                 }
  518.  
  519.                 // Get current link
  520.                 $current_link JUri::getInstance()->toString(array('path''query'));
  521.                 if ($current_link == JUri::base(true).'/')
  522.                 {
  523.                     $current_link .= 'index.php';
  524.                 }
  525.  
  526.                 // Check the exact menu item's URL
  527.                 if ($active_link == $current_link)
  528.                 {
  529.                     $associations MenusHelper::getAssociations($active->id);
  530.                     unset($associations[$active->language]);
  531.                 }
  532.             }
  533.             // load component associations
  534.             $cName JString::ucfirst($eName.'HelperAssociation');
  535.             JLoader::register($cNameJPath::clean(JPATH_COMPONENT_SITE '/helpers/association.php'));
  536.  
  537.             if (class_exists($cName&& is_callable(array($cName'getAssociations')))
  538.             {
  539.                 $cassociations call_user_func(array($cName'getAssociations'));
  540.  
  541.                 $lang_code $app->input->cookie->getString(JApplication::getHash('language'));
  542.                 // No cookie - let's try to detect browser language or use site default
  543.                 if (!$lang_code)
  544.                 {
  545.                     if ($this->params->get('detect_browser'1)){
  546.                         $lang_code JLanguageHelper::detectLanguage();
  547.                     else {
  548.                         $lang_code self::$default_lang;
  549.                     }
  550.                 }
  551.                 unset($cassociations[$lang_code]);
  552.             }
  553.  
  554.             // handle the default associations
  555.             if ((!empty($associations|| !empty($cassociations)) && $this->params->get('item_associations'))
  556.             {
  557.  
  558.                 foreach (JLanguageHelper::getLanguages(as $language)
  559.                 {
  560.                     if (!JLanguage::exists($language->lang_code))
  561.                     {
  562.                         continue;
  563.                     }
  564.  
  565.                     if (isset($cassociations[$language->lang_code]))
  566.                     {
  567.                         $link JRoute::_($cassociations[$language->lang_code].'&lang='.$language->sef);
  568.                         $doc->addHeadLink($server $link'alternate''rel'array('hreflang' => $language->lang_code));
  569.                     }
  570.                     elseif (isset($associations[$language->lang_code]))
  571.                     {
  572.                         $item $menu->getItem($associations[$language->lang_code]);
  573.  
  574.                         if ($item)
  575.                         {
  576.                             if ($app->getCfg('sef'))
  577.                             {
  578.                                 $link JRoute::_('index.php?Itemid='.$item->id.'&lang='.$language->sef);
  579.                             }
  580.                             else
  581.                             {
  582.                                 $link JRoute::_($item->link.'&Itemid='.$item->id.'&lang='.$language->sef);
  583.                             }
  584.                             $doc->addHeadLink($server $link'alternate''rel'array('hreflang' => $language->lang_code));
  585.                         }
  586.                     }
  587.                 }
  588.             }
  589.             // link to the home page of each language
  590.             elseif ($active && $active->home)
  591.             {
  592.  
  593.                 foreach (JLanguageHelper::getLanguages(as $language)
  594.                 {
  595.                     if (!JLanguage::exists($language->lang_code))
  596.                     {
  597.                         continue;
  598.                     }
  599.  
  600.                     $item $menu->getDefault($language->lang_code);
  601.  
  602.                     if ($item && $item->language != $active->language && $item->language != '*')
  603.                     {
  604.                         if ($app->getCfg('sef'))
  605.                         {
  606.                             $link JRoute::_('index.php?Itemid='.$item->id.'&lang='.$language->sef);
  607.                         }
  608.                         else
  609.                         {
  610.                             $link JRoute::_($item->link.'&Itemid='.$item->id.'&lang='.$language->sef);
  611.                         }
  612.                         $doc->addHeadLink($server $link'alternate''rel'array('hreflang' => $language->lang_code));
  613.                     }
  614.                 }
  615.             }
  616.         }
  617.     }
  618.  
  619.     /**
  620.      * Getting the Language Cookie settings
  621.      *
  622.      * @return  string  The cookie time.
  623.      *
  624.      * @since   3.0.4
  625.      */
  626.     private function getLangCookieTime()
  627.     {
  628.         if ($this->params->get('lang_cookie'1== 1)
  629.         {
  630.             $lang_cookie time(365 86400;
  631.         }
  632.         else
  633.         {
  634.             $lang_cookie 0;
  635.         }
  636.  
  637.         return $lang_cookie;
  638.     }
  639. }

Documentation generated on Tue, 19 Nov 2013 15:06:36 +0100 by phpDocumentor 1.4.3