Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.43 KB | None | 0 0
  1. <?php
  2. /**
  3. * @package Joomla.Plugin
  4. * @subpackage System.languagefilter
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. use Joomla\Registry\Registry;
  11. JLoader::register('MenusHelper', JPATH_ADMINISTRATOR . '/components/com_menus/helpers/menus.php');
  12. /**
  13. * Joomla! Language Filter Plugin.
  14. *
  15. * @since 1.6
  16. */
  17. class PlgSystemLanguageFilter extends JPlugin
  18. {
  19. protected $mode_sef;
  20. protected $sefs;
  21. protected $lang_codes;
  22. protected $default_lang;
  23. protected $cookie_lang_code;
  24. private $user_lang_code;
  25. /**
  26. * Application object.
  27. *
  28. * @var JApplicationCms
  29. * @since 3.3
  30. */
  31. protected $app;
  32. /**
  33. * Constructor.
  34. *
  35. * @param object &$subject The object to observe
  36. * @param array $config An optional associative array of configuration settings.
  37. *
  38. * @since 1.6
  39. */
  40. public function __construct(&$subject, $config)
  41. {
  42. parent::__construct($subject, $config);
  43. $this->app = JFactory::getApplication();
  44. if ($this->app->isSite())
  45. {
  46. // Setup language data.
  47. $this->mode_sef = $this->app->get('sef', 0);
  48. $this->sefs = JLanguageHelper::getLanguages('sef');
  49. $this->lang_codes = JLanguageHelper::getLanguages('lang_code');
  50. $this->default_lang = JComponentHelper::getParams('com_languages')->get('site', 'en-GB');
  51. $levels = JFactory::getUser()->getAuthorisedViewLevels();
  52. foreach ($this->sefs as $sef => $language)
  53. {
  54. // @todo: In Joomla 2.5.4 and earlier access wasn't set. Non modified Content Languages got 0 as access value
  55. if ($language->access && !in_array($language->access, $levels))
  56. {
  57. unset($this->lang_codes[$language->lang_code]);
  58. unset($this->sefs[$language->sef]);
  59. }
  60. }
  61. $cookieLangCode = $this->app->input->cookie->getString(JApplicationHelper::getHash('language'));
  62. if (!$cookieLangCode)
  63. {
  64. if ($this->params->get('detect_browser', '1'))
  65. {
  66. $cookieLangCode = JLanguageHelper::detectLanguage();
  67. }
  68. }
  69. if (!$cookieLangCode || !isset($this->lang_codes[$cookieLangCode]))
  70. {
  71. $cookieLangCode = $this->default_lang;
  72. }
  73. $this->cookie_lang_code = $cookieLangCode;
  74. $this->app->setLanguageFilter(true);
  75. // Detect browser feature.
  76. $this->app->setDetectBrowser($this->params->get('detect_browser', '1') == '1');
  77. }
  78. }
  79. /**
  80. * After initialise.
  81. *
  82. * @return void
  83. *
  84. * @since 1.6
  85. */
  86. public function onAfterInitialise()
  87. {
  88. $this->app->item_associations = $this->params->get('item_associations', 0);
  89. if ($this->app->isSite())
  90. {
  91. $router = $this->app->getRouter();
  92. // Attach build rules for language SEF.
  93. $router->attachBuildRule(array($this, 'preprocessBuildRule'), JRouter::PROCESS_BEFORE);
  94. $router->attachBuildRule(array($this, 'buildRule'), JRouter::PROCESS_DURING);
  95. if ($this->mode_sef)
  96. {
  97. $router->attachBuildRule(array($this, 'postprocessSEFBuildRule'), JRouter::PROCESS_AFTER);
  98. }
  99. else
  100. {
  101. $router->attachBuildRule(array($this, 'postprocessNonSEFBuildRule'), JRouter::PROCESS_AFTER);
  102. }
  103. // Attach parse rules for language SEF.
  104. $router->attachParseRule(array($this, 'parseRule'), JRouter::PROCESS_DURING);
  105. }
  106. }
  107. /**
  108. * After route.
  109. *
  110. * @return void
  111. *
  112. * @since 3.4
  113. */
  114. public function onAfterRoute()
  115. {
  116. // Add custom site name.
  117. if (isset($this->lang_codes[$this->default_lang]) && $this->lang_codes[$this->default_lang]->sitename)
  118. {
  119. $this->app->set('sitename', $this->lang_codes[$this->default_lang]->sitename);
  120. }
  121. }
  122. /**
  123. * Add build preprocess rule to router.
  124. *
  125. * @param JRouter &$router JRouter object.
  126. * @param JUri &$uri JUri object.
  127. *
  128. * @return void
  129. *
  130. * @since 3.4
  131. */
  132. public function preprocessBuildRule(&$router, &$uri)
  133. {
  134. $lang = $uri->getVar('lang', $this->default_lang);
  135. $uri->setVar('lang', $lang);
  136. if (isset($this->sefs[$lang]))
  137. {
  138. $lang = $this->sefs[$lang]->lang_code;
  139. $uri->setVar('lang', $lang);
  140. }
  141. }
  142. /**
  143. * Add build rule to router.
  144. *
  145. * @param JRouter &$router JRouter object.
  146. * @param JUri &$uri JUri object.
  147. *
  148. * @return void
  149. *
  150. * @since 1.6
  151. */
  152. public function buildRule(&$router, &$uri)
  153. {
  154. $lang = $uri->getVar('lang');
  155. if (isset($this->lang_codes[$lang]))
  156. {
  157. $sef = $this->lang_codes[$lang]->sef;
  158. }
  159. else
  160. {
  161. $sef = $this->lang_codes[$this->default_lang]->sef;
  162. }
  163. if ($this->mode_sef
  164. && (!$this->params->get('remove_default_prefix', 0) || $lang != JComponentHelper::getParams('com_languages')->get('site', 'en-GB')))
  165. {
  166. $uri->setPath($uri->getPath() . '/' . $sef . '/');
  167. }
  168. }
  169. /**
  170. * postprocess build rule for SEF URLs
  171. *
  172. * @param JRouter &$router JRouter object.
  173. * @param JUri &$uri JUri object.
  174. *
  175. * @return void
  176. *
  177. * @since 3.4
  178. */
  179. public function postprocessSEFBuildRule(&$router, &$uri)
  180. {
  181. $uri->delVar('lang');
  182. }
  183. /**
  184. * postprocess build rule for non-SEF URLs
  185. *
  186. * @param JRouter &$router JRouter object.
  187. * @param JUri &$uri JUri object.
  188. *
  189. * @return void
  190. *
  191. * @since 3.4
  192. */
  193. public function postprocessNonSEFBuildRule(&$router, &$uri)
  194. {
  195. $lang = $uri->getVar('lang');
  196. if (isset($this->lang_codes[$lang]))
  197. {
  198. $uri->setVar('lang', $this->lang_codes[$lang]->sef);
  199. }
  200. }
  201. /**
  202. * Add parse rule to router.
  203. *
  204. * @param JRouter &$router JRouter object.
  205. * @param JUri &$uri JUri object.
  206. *
  207. * @return void
  208. *
  209. * @since 1.6
  210. */
  211. public function parseRule(&$router, &$uri)
  212. {
  213. // Did we find the current and existing language yet?
  214. $found = false;
  215. // Are we in SEF mode or not?
  216. if ($this->mode_sef)
  217. {
  218. $path = $uri->getPath();
  219. $parts = explode('/', $path);
  220. $sef = $parts[0];
  221. // If the default prefix should be removed and the SEF prefix is not among those
  222. // that we have in our system, its the default language and we "found" the right language
  223. if ($this->params->get('remove_default_prefix', 0) && !isset($this->sefs[$sef]))
  224. {
  225. $found = true;
  226. //$lang_code = JComponentHelper::getParams('com_languages')->get('site', 'en-GB');
  227. $lang_code = $this->cookie_lang_code;
  228. }
  229. else
  230. {
  231. // If the language prefix should always be present or it is indeed , we can now look it up in our array
  232. if (isset($this->sefs[$sef]))
  233. {
  234. // We found our language
  235. $found = true;
  236. $lang_code = $this->sefs[$sef]->lang_code;
  237. }
  238. // 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
  239. if ($this->params->get('remove_default_prefix', 0)
  240. && $lang_code == JComponentHelper::getParams('com_languages')->get('site', 'en-GB'))
  241. {
  242. $found = false;
  243. array_shift($parts);
  244. $path = implode('/', $parts);
  245. }
  246. // We have found our language and the first part of our URL is the language prefix
  247. if ($found)
  248. {
  249. array_shift($parts);
  250. $uri->setPath(implode('/', $parts));
  251. }
  252. }
  253. }
  254. else
  255. {
  256. // We are not in SEF mode
  257. $lang = $uri->getVar('lang');
  258. if (isset($this->sefs[$lang]))
  259. {
  260. // We found our language
  261. $found = true;
  262. $lang_code = $this->sefs[$lang]->lang_code;
  263. }
  264. }
  265. // We are called via POST. We don't care about the language
  266. // and simply set the default language as our current language.
  267. if ($this->app->input->getMethod() == "POST"
  268. || count($this->app->input->post) > 0
  269. || count($this->app->input->files) > 0)
  270. {
  271. if (!$found)
  272. {
  273. $found = true;
  274. $lang_code = $this->cookie_lang_code;
  275. }
  276. }
  277. // We have not found the language and thus need to redirect
  278. if (!$found)
  279. {
  280. // Lets find the default language for this user
  281. if (!isset($lang_code) || !isset($this->lang_codes[$lang_code]))
  282. {
  283. // Either we detected the language via the browser or we got it from the cookie. In worst case
  284. // we fall back to the application setting
  285. $lang_code = $this->app->input->cookie->getString(JApplicationHelper::getHash('language'), false);
  286. if (!$lang_code && $this->params->get('detect_browser', 1))
  287. {
  288. $lang_code = JLanguageHelper::detectLanguage();
  289. if (!isset($this->lang_codes[$lang_code]))
  290. {
  291. $lang_code = false;
  292. }
  293. }
  294. if (!$lang_code)
  295. {
  296. $lang_code = JComponentHelper::getParams('com_languages')->get('site', 'en-GB');
  297. }
  298. }
  299. if ($this->mode_sef)
  300. {
  301. // Use the current language sef or the default one.
  302. if (!$this->params->get('remove_default_prefix', 0)
  303. || $lang_code != JComponentHelper::getParams('com_languages')->get('site', 'en-GB'))
  304. {
  305. $path = $this->lang_codes[$lang_code]->sef . '/' . $path;
  306. }
  307. $uri->setPath($path);
  308. if (!$this->app->get('sef_rewrite'))
  309. {
  310. $uri->setPath('index.php/' . $uri->getPath());
  311. }
  312. $this->app->redirect($uri->base() . $uri->toString(array('path', 'query', 'fragment')));
  313. }
  314. else
  315. {
  316. $uri->setVar('lang', $this->lang_codes[$lang_code]->sef);
  317. $this->app->redirect($uri->base() . 'index.php?' . $uri->getQuery());
  318. }
  319. }
  320. // We have found our language and now need to set the cookie and the language value in our system
  321. $array = array('lang' => $lang_code);
  322. $this->default_lang = $lang_code;
  323. // Set the request var.
  324. $this->app->input->set('language', $lang_code);
  325. $this->app->set('language', $lang_code);
  326. $language = JFactory::getLanguage();
  327. if ($language->getTag() != $lang_code)
  328. {
  329. $newLang = JLanguage::getInstance($lang_code);
  330. foreach ($language->getPaths() as $extension => $files)
  331. {
  332. $newLang->load($extension);
  333. }
  334. JFactory::$language = $newLang;
  335. $this->app->loadLanguage($newLang);
  336. }
  337. // Create a cookie.
  338. if ($this->app->input->cookie->getString(JApplicationHelper::getHash('language')) != $lang_code)
  339. {
  340. $cookie_domain = $this->app->get('cookie_domain');
  341. $cookie_path = $this->app->get('cookie_path', $uri->base(true));
  342. $this->app->input->cookie->set(JApplicationHelper::getHash('language'), $lang_code, $this->getLangCookieTime(), $cookie_path, $cookie_domain);
  343. }
  344. return $array;
  345. }
  346. /**
  347. * Before store user method.
  348. *
  349. * Method is called before user data is stored in the database.
  350. *
  351. * @param array $user Holds the old user data.
  352. * @param boolean $isnew True if a new user is stored.
  353. * @param array $new Holds the new user data.
  354. *
  355. * @return void
  356. *
  357. * @since 1.6
  358. */
  359. public function onUserBeforeSave($user, $isnew, $new)
  360. {
  361. if ($this->params->get('automatic_change', '1') == '1' && key_exists('params', $user))
  362. {
  363. $registry = new Registry;
  364. $registry->loadString($user['params']);
  365. $this->user_lang_code = $registry->get('language');
  366. if (empty($this->user_lang_code))
  367. {
  368. $this->user_lang_code = $this->default_lang;
  369. }
  370. }
  371. }
  372. /**
  373. * After store user method.
  374. *
  375. * Method is called after user data is stored in the database.
  376. *
  377. * @param array $user Holds the new user data.
  378. * @param boolean $isnew True if a new user is stored.
  379. * @param boolean $success True if user was succesfully stored in the database.
  380. * @param string $msg Message.
  381. *
  382. * @return void
  383. *
  384. * @since 1.6
  385. */
  386. public function onUserAfterSave($user, $isnew, $success, $msg)
  387. {
  388. if ($this->params->get('automatic_change', '1') == '1' && key_exists('params', $user) && $success)
  389. {
  390. $registry = new Registry;
  391. $registry->loadString($user['params']);
  392. $lang_code = $registry->get('language');
  393. if (empty($lang_code))
  394. {
  395. $lang_code = $this->default_lang;
  396. }
  397. if ($lang_code == $this->user_lang_code || !isset($this->lang_codes[$lang_code]))
  398. {
  399. if ($this->app->isSite())
  400. {
  401. $this->app->setUserState('com_users.edit.profile.redirect', null);
  402. }
  403. }
  404. else
  405. {
  406. if ($this->app->isSite())
  407. {
  408. $this->app->setUserState('com_users.edit.profile.redirect', 'index.php?Itemid='
  409. . $this->app->getMenu()->getDefault($lang_code)->id . '&lang=' . $this->lang_codes[$lang_code]->sef
  410. );
  411. // Create a cookie.
  412. $cookie_domain = $this->app->get('cookie_domain', '');
  413. $cookie_path = $this->app->get('cookie_path', '/');
  414. setcookie(JApplicationHelper::getHash('language'), $lang_code, $this->getLangCookieTime(), $cookie_path, $cookie_domain);
  415. }
  416. }
  417. }
  418. }
  419. /**
  420. * Method to handle any login logic and report back to the subject.
  421. *
  422. * @param array $user Holds the user data.
  423. * @param array $options Array holding options (remember, autoregister, group).
  424. *
  425. * @return boolean True on success.
  426. *
  427. * @since 1.5
  428. */
  429. public function onUserLogin($user, $options = array())
  430. {
  431. $menu = $this->app->getMenu();
  432. if ($this->app->isSite() && $this->params->get('automatic_change', 1))
  433. {
  434. // Load associations.
  435. $assoc = JLanguageAssociations::isEnabled();
  436. if ($assoc)
  437. {
  438. $active = $menu->getActive();
  439. if ($active)
  440. {
  441. $associations = MenusHelper::getAssociations($active->id);
  442. }
  443. }
  444. $lang_code = $user['language'];
  445. if (empty($lang_code))
  446. {
  447. $lang_code = $this->default_lang;
  448. }
  449. if ($lang_code != $this->default_lang)
  450. {
  451. // Change language.
  452. $this->default_lang = $lang_code;
  453. // Create a cookie.
  454. $cookie_domain = $this->app->get('cookie_domain', '');
  455. $cookie_path = $this->app->get('cookie_path', '/');
  456. setcookie(JApplicationHelper::getHash('language'), $lang_code, $this->getLangCookieTime(), $cookie_path, $cookie_domain);
  457. // Change the language code.
  458. JFactory::getLanguage()->setLanguage($lang_code);
  459. // Change the redirect (language has changed).
  460. if (isset($associations[$lang_code]) && $menu->getItem($associations[$lang_code]))
  461. {
  462. $itemid = $associations[$lang_code];
  463. $this->app->setUserState('users.login.form.return', 'index.php?&Itemid=' . $itemid);
  464. }
  465. else
  466. {
  467. JLoader::register('MultilangstatusHelper', JPATH_ADMINISTRATOR . '/components/com_languages/helpers/multilangstatus.php');
  468. $homes = MultilangstatusHelper::getHomepages();
  469. $itemid = isset($homes[$lang_code]) ? $homes[$lang_code]->id : $homes['*']->id;
  470. $this->app->setUserState('users.login.form.return', 'index.php?&Itemid=' . $itemid);
  471. }
  472. }
  473. }
  474. }
  475. /**
  476. * Method to add alternative meta tags for associated menu items.
  477. *
  478. * @return void
  479. *
  480. * @since 1.7
  481. */
  482. public function onAfterDispatch()
  483. {
  484. $doc = JFactory::getDocument();
  485. $menu = $this->app->getMenu();
  486. $server = JUri::getInstance()->toString(array('scheme', 'host', 'port'));
  487. $option = $this->app->input->get('option');
  488. $eName = JString::ucfirst(JString::str_ireplace('com_', '', $option));
  489. if ($this->app->isSite() && $this->params->get('alternate_meta') && $doc->getType() == 'html')
  490. {
  491. // Get active menu item.
  492. $active = $menu->getActive();
  493. $assocs = array();
  494. $home = false;
  495. // Load menu associations.
  496. if ($active)
  497. {
  498. $active_link = JRoute::_($active->link . '&Itemid=' . $active->id, false);
  499. // Get current link.
  500. $current_link = JUri::getInstance()->toString(array('path', 'query'));
  501. // Check the exact menu item's URL.
  502. if ($active_link == $current_link)
  503. {
  504. $associations = MenusHelper::getAssociations($active->id);
  505. unset($associations[$active->language]);
  506. $assocs = array_keys($associations);
  507. // If the menu item is a home menu item and the URLs are identical, we are on the homepage
  508. $home = true;
  509. }
  510. }
  511. // Load component associations.
  512. $cName = JString::ucfirst($eName . 'HelperAssociation');
  513. JLoader::register($cName, JPath::clean(JPATH_COMPONENT_SITE . '/helpers/association.php'));
  514. if (class_exists($cName) && is_callable(array($cName, 'getAssociations')))
  515. {
  516. $cassociations = call_user_func(array($cName, 'getAssociations'));
  517. $lang_code = $this->app->input->cookie->getString(JApplicationHelper::getHash('language'));
  518. // No cookie - let's try to detect browser language or use site default.
  519. if (!$lang_code)
  520. {
  521. if ($this->params->get('detect_browser', 1))
  522. {
  523. $lang_code = JLanguageHelper::detectLanguage();
  524. }
  525. else
  526. {
  527. $lang_code = $this->default_lang;
  528. }
  529. }
  530. unset($cassociations[$lang_code]);
  531. $assocs = array_merge(array_keys($cassociations), $assocs);
  532. }
  533. // Handle the default associations.
  534. if ($this->params->get('item_associations') || ($active && $active->home && $home))
  535. {
  536. $languages = JLanguageHelper::getLanguages('lang_code');
  537. foreach ($assocs as $language)
  538. {
  539. if (!JLanguage::exists($language))
  540. {
  541. continue;
  542. }
  543. $lang = $languages[$language];
  544. if (isset($cassociations[$language]))
  545. {
  546. $link = JRoute::_($cassociations[$language] . '&lang=' . $lang->sef);
  547. // Check if language is the default site language and remove url language code is on
  548. if ($lang->sef == $this->lang_codes[$this->default_lang]->sef && $this->params->get('remove_default_prefix') == '1')
  549. {
  550. $link = preg_replace('|/' . $lang->sef . '/|', '/', $link, 1);
  551. }
  552. $doc->addHeadLink($server . $link, 'alternate', 'rel', array('hreflang' => $language));
  553. }
  554. elseif (isset($associations[$language]))
  555. {
  556. $item = $menu->getItem($associations[$language]);
  557. if ($item)
  558. {
  559. $link = JRoute::_($item->link . '&Itemid=' . $item->id . '&lang=' . $lang->sef);
  560. $doc->addHeadLink($server . $link, 'alternate', 'rel', array('hreflang' => $language));
  561. }
  562. }
  563. }
  564. }
  565. }
  566. }
  567. /**
  568. * Get the language cookie settings.
  569. *
  570. * @return string The cookie time.
  571. *
  572. * @since 3.0.4
  573. */
  574. private function getLangCookieTime()
  575. {
  576. if ($this->params->get('lang_cookie', 1) == 1)
  577. {
  578. $lang_cookie = time() + 365 * 86400;
  579. }
  580. else
  581. {
  582. $lang_cookie = 0;
  583. }
  584. return $lang_cookie;
  585. }
  586. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement