PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mambots/system/jfdatabase.systembot.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 292 lines | 189 code | 34 blank | 69 comment | 51 complexity | adda386c0fe5bd3802a463914bbedffd MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Joom!Fish - Multi Lingual extention and translation manager for Joomla!
  4. * Copyright (C) 2003-2006 Think Network GmbH, Munich
  5. *
  6. * All rights reserved. The Joom!Fish project is a set of extentions for
  7. * the content management system Joomla!. It enables Joomla!
  8. * to manage multi lingual sites especially in all dynamic information
  9. * which are stored in the database.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
  24. *
  25. * The "GNU General Public License" (GPL) is available at
  26. * http: *www.gnu.org/copyleft/gpl.html
  27. * -----------------------------------------------------------------------------
  28. * $Id:jfdatabase.systembot.php 580 2007-07-24 18:52:05Z akede $
  29. * @package joomfish
  30. * @subpackage system.jfdatabase_bot
  31. *
  32. */
  33. /** ensure this file is being included by a parent file */
  34. defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  35. $_MAMBOTS->registerFunction( 'onStart', 'botJFDatabase' );
  36. $_MAMBOTS->registerFunction( 'onAfterStart', 'botJFDiscoverLanguage' );
  37. $_MAMBOTS->registerFunction( 'onAfterStart', 'botJFInitialize' );
  38. if (file_exists( $mosConfig_absolute_path . '/components/com_joomfish/joomfish.php' )) {
  39. require_once( $mosConfig_absolute_path . '/administrator/components/com_joomfish/mldatabase.class.php' );
  40. require_once( $mosConfig_absolute_path . '/administrator/components/com_joomfish/joomfish.class.php' );
  41. //require_once( $mosConfig_absolute_path '/components/com_joomfish/includes/joomfish.class.php' );
  42. }
  43. /**
  44. * Exchange of the database abstraction layer for multi lingual translations.
  45. */
  46. function botJFDatabase() {
  47. global $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix,$mosConfig_debug;
  48. $GLOBALS[ 'mosConfig_mbf_content' ] = '1'; // ToDo: Might need correction!!
  49. $GLOBALS[ 'mosConfig_multilingual_support' ] = 1;
  50. $database = new mlDatabase( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  51. $database->debug( $mosConfig_debug );
  52. // if running mysql 5, set sql-mode to mysql40 - thereby circumventing strict mode problems
  53. if ( strpos( $database->getVersion(), '5' ) === 0 ) {
  54. $query = "SET sql_mode = 'MYSQL40'";
  55. $database->setQuery( $query );
  56. $database->query();
  57. }
  58. $GLOBALS['database'] = $database;
  59. }
  60. /** The function finds the language which is to be used for the user/session
  61. *
  62. * It is possible to choose the language based on the client browsers configuration,
  63. * the activated language of the configuration and the language a user has choosen in
  64. * the past. The decision of this order is done in the JoomFish configuration.
  65. *
  66. * The other topic while choosing the language is to change the $mosConfig_lang var
  67. * for the other CMT's and same this for the user in the session or cockie.
  68. */
  69. function botJFDiscoverLanguage ( ) {
  70. global $mosConfig_lang, $database, $_MAMBOTS;
  71. $GLOBALS['mosConfig_defaultLang'] = $mosConfig_lang; // Save the default language of the site
  72. // get instance of JoomFishManager to obtain active language list and config values
  73. $jfm =& JoomFishManager::getInstance();
  74. // check if param query has previously been processed
  75. if ( !isset($_MAMBOTS->_system_mambot_params['jfSystembot']) ) {
  76. // load mambot params info
  77. $query = "SELECT params"
  78. . "\n FROM #__mambots"
  79. . "\n WHERE element = 'jfdatabase.systembot'"
  80. . "\n AND folder = 'system'"
  81. ;
  82. $database->setQuery( $query );
  83. $database->loadObject($mambot);
  84. // save query to class variable
  85. $_MAMBOTS->_system_mambot_params['jfSystembot'] = $mambot;
  86. }
  87. // pull query data from class variable
  88. $mambot = $_MAMBOTS->_system_mambot_params['jfSystembot'];
  89. $botParams = new mosParameters( $mambot->params );
  90. $determitLanguage = $botParams->def( 'determitLanguage', 1 );
  91. $newVisitorAction = $botParams->def( 'newVisitorAction', 'browser' );
  92. $use302redirect = $botParams->def( 'use302redirect', 0 );
  93. $enableCookie = $botParams->def( 'enableCookie', 1 );
  94. $client_lang = '';
  95. $lang_known = false;
  96. $jfcookie = mosGetParam( $_COOKIE, 'jfcookie', null );
  97. if (isset($jfcookie["lang"]) && $jfcookie["lang"] != "") {
  98. $client_lang = $jfcookie["lang"];
  99. $lang_known = true;
  100. }
  101. if (($lang = mosGetParam( $_GET, 'lang', '' )) ) {
  102. if( $lang != '' ) {
  103. $client_lang = $lang;
  104. $lang_known = true;
  105. }
  106. }
  107. if ( !$lang_known && $determitLanguage &&
  108. key_exists( 'HTTP_ACCEPT_LANGUAGE', $_SERVER ) && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ) {
  109. switch ($newVisitorAction) {
  110. // usesing the first defined Joom!Fish language
  111. case 'joomfish':
  112. $activeLanguages = $jfm->getActiveLanguages();
  113. $keys = array_keys($activeLanguages);
  114. $client_lang = $activeLanguages[$keys[0]]->getLanguageCode();
  115. break;
  116. case 'site':
  117. $jfLang = jfLanguage::createByJoomla( $mosConfig_lang );
  118. $client_lang = $jfLang->getLanguageCode();
  119. break;
  120. // no language chooses - assume from browser configuration
  121. case 'browser':
  122. default:
  123. // language negotiation by Kochin Chang, June 16, 2004
  124. // retrieve active languages from database
  125. $active_iso = array();
  126. $active_isocountry = array();
  127. $active_code = array();
  128. $activeLanguages = $jfm->getActiveLanguages();
  129. if( count( $activeLanguages ) == 0 ) {
  130. return $mosConfig_lang;
  131. }
  132. foreach ($activeLanguages as $lang) {
  133. $active_iso[] = $lang->iso;
  134. if( eregi('[_-]', $lang->iso) ) {
  135. $isocountry = split('[_-]',$lang->iso);
  136. $active_isocountry[] = $isocountry[0];
  137. }
  138. $active_code[] = $lang->shortcode;
  139. }
  140. // figure out which language to use - browser languages are based on ISO codes
  141. $browserLang = explode(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
  142. foreach( $browserLang as $lang ) {
  143. if( in_array($lang, $active_iso) ) {
  144. $client_lang = $lang;
  145. break;
  146. }
  147. $shortLang = substr( $lang, 0, 2 );
  148. if( in_array($shortLang, $active_isocountry) ) {
  149. $client_lang = $shortLang;
  150. break;
  151. }
  152. // compare with code
  153. if ( in_array($shortLang, $active_code) ) {
  154. $client_lang = $shortLang;
  155. break;
  156. }
  157. }
  158. break;
  159. }
  160. }
  161. // get the name of the language file for joomla
  162. $jfLang = jfLanguage::createByShortcode($client_lang, false);
  163. if( $jfLang === null ) {
  164. $jfLang = jfLanguage::createByISO( $client_lang, false );
  165. }
  166. if( !$lang_known && $use302redirect ) {
  167. // 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
  168. $href= "index.php";
  169. $hrefVars = '';
  170. $queryString = mosGetParam($_SERVER, 'QUERY_STRING', null);
  171. if( !empty($queryString) ) {
  172. $vars = explode( "&", $queryString );
  173. if( count($vars) > 0 && $queryString) {
  174. foreach ($vars as $var) {
  175. if( eregi('=', $var ) ) {
  176. list($key, $value) = explode( "=", $var);
  177. if( $key != "lang" ) {
  178. if( $hrefVars != "" ) {
  179. $hrefVars .= "&amp;";
  180. }
  181. // ignore mosmsg to ensure it is visible in frontend
  182. if( $key != 'mosmsg' ) {
  183. $hrefVars .= "$key=$value";
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. // Add the existing variables
  191. if( $hrefVars != "" ) {
  192. $href .= '?' .$hrefVars;
  193. }
  194. if( $jfLang->getLanguageCode() != null ) {
  195. $lang = 'lang=' .$jfLang->getLanguageCode();
  196. } else {
  197. // it's important that we add at least the basic parameter - as of the sef is adding the actual otherwise!
  198. $lang = 'lang=';
  199. }
  200. // if there are other vars we need to add a & otherwiese ?
  201. if( $hrefVars == '' ) {
  202. $href .= '?' . $lang;
  203. } else {
  204. $href .= '&amp;' . $lang;
  205. }
  206. $href = sefRelToAbs($href);
  207. $GLOBALS['iso_client_lang'] = $jfLang->getLanguageCode();
  208. header( 'HTTP/1.1 303 See Other' );
  209. header( "Location: ". sefRelToAbs($href) );
  210. exit();
  211. }
  212. if( isset($jfLang) && $jfLang->code != "" && ($jfLang->active || $jfm->getCfg("frontEndPreview") )) {
  213. $mosConfig_lang = $jfLang->code;
  214. } else {
  215. $jfLang = jfLanguage::createByJoomla( $mosConfig_lang );
  216. if( !$jfLang->active ) {
  217. ?>
  218. <div style="background-color: #c00; color: #fff">
  219. <p style="font-size: 1.5em; font-weight: bold; padding: 10px 0px 10px 0px; text-align: center; font-family: Arial, Helvetica, sans-serif;">
  220. Joom!Fish config error: Default language is inactive!<br />&nbsp;<br />
  221. Please check configuration, try to use first active language</p>
  222. </div>
  223. <?php
  224. $activeLanguages = $jfm->getActiveLanguages();
  225. if( count($activeLanguages) > 0 ) {
  226. $jfLang = $activeLanguages[0];
  227. $mosConfig_lang = $jfLang->code;
  228. } else {
  229. // No active language defined - using system default is only alternative!
  230. }
  231. }
  232. $client_lang = $jfLang->getLanguageCode();
  233. }
  234. // set locale for this ISO code
  235. $lang = strtolower($jfLang->iso).'_'.strtoupper($jfLang->iso);
  236. setlocale(LC_ALL, $lang);
  237. $overwriteGlobalConfig = $botParams->def( 'overwriteGlobalConfig', 0 );
  238. if( $overwriteGlobalConfig ) {
  239. // We should overwrite additional global variables based on the language parameter configuration
  240. $langParams = new mosParameters( $jfLang->params );
  241. foreach ($langParams->toArray() as $key => $value) {
  242. $GLOBALS['mosConfig_' .$key] =$value;
  243. }
  244. }
  245. if($enableCookie) {
  246. setcookie( "lang", "", time() - 1800, "/" );
  247. setcookie( "jfcookie", "", time() - 1800, "/" );
  248. setcookie( "jfcookie[lang]", $client_lang, time()+24*3600, '/' );
  249. }
  250. $GLOBALS['iso_client_lang'] = $client_lang;
  251. }
  252. /** This function initialize the Joom!Fish manager in order to have
  253. * easy access and prepare certain information.
  254. */
  255. function botJFInitialize ( ) {
  256. $GLOBALS[ '_JOOMFISH_MANAGER'] =& JoomFishManager::getInstance();
  257. }
  258. ?>