PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_virtuemart/classes/language.class.php

http://vanphongphamdm.googlecode.com/
PHP | 283 lines | 180 code | 12 blank | 91 comment | 61 complexity | 1bad6b171475c24f5a0aaf9f06a1c762 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * This file contains the lanuages handler class
  4. *
  5. * @version $Id: language.class.php 1475 2008-07-16 17:35:35Z soeren_nb $
  6. * @package VirtueMart
  7. * @copyright Copyright (C) 2007 soeren, thepisu - All rights reserved.
  8. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  9. * VirtueMart is free software. This version may have been modified pursuant
  10. * to the GNU General Public License, and as distributed it includes or
  11. * is derivative of works licensed under the GNU General Public License or
  12. * other free or open source software licenses.
  13. * See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
  14. *
  15. * http://virtuemart.net
  16. */
  17. if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
  18. function utf8_to_cp1251($s) {
  19. for ($c=0;$c<strlen($s);$c++)
  20. {
  21. $i=ord($s[$c]);
  22. if ($i<=127) $out.=$s[$c];
  23. if ($byte2){
  24. $new_c2=($c1&3)*64+($i&63);
  25. $new_c1=($c1>>2)&5;
  26. $new_i=$new_c1*256+$new_c2;
  27. if ($new_i==1025){
  28. $out_i=168;
  29. } else {
  30. if ($new_i==1105){
  31. $out_i=184;
  32. } else {
  33. $out_i=$new_i-848;
  34. }
  35. }
  36. $out.=chr($out_i);
  37. $byte2=false;
  38. }
  39. if (($i>>5)==6) {
  40. $c1=$i;
  41. $byte2=true;
  42. }
  43. }
  44. return $out;
  45. }
  46. /**
  47. * Abstract lanuages/translation handler class
  48. */
  49. class vmAbstractLanguage {
  50. /** @var boolean If true, highlights string not found */
  51. var $_debug = false;
  52. var $modules = array();
  53. var $key_varname = '';
  54. function vmAbstractLanguage() {
  55. $this->setDebug();
  56. }
  57. /**
  58. * Translator function
  59. * @param string Name of the Class Variable
  60. * @param boolean Encode String to HTML entities?
  61. * @return string The value of $var (as an HTML Entitiy-encoded string if $htmlentities)
  62. */
  63. function _( $var, $htmlentities=false ) {
  64. global $modulename;
  65. $module = $modulename;
  66. $key = strtoupper( $var );
  67. // if language module not yet loaded, load now
  68. if (!isset($this->modules[$module])) {
  69. $this->load($module);
  70. }
  71. $text = false;
  72. $module = $this->exists($key);
  73. if( $module === false && $key[0] == '_' ) {
  74. $key = substr( $key, 1 );
  75. $module = $this->exists($key);
  76. }
  77. if ($module!==false) {
  78. $text = $this->modules[$module][$key];
  79. if( $htmlentities ) {
  80. $text = htmlentities( $text, ENT_QUOTES, $this->getCharset($module));
  81. // some symbols are not converted correctly... doing manually
  82. $text = str_replace(chr(128),'&euro;',$text);
  83. // enable the use of HTML tags in language file... is this really good?
  84. $text = str_replace('&lt;','<',$text);
  85. $text = str_replace('&gt;','>',$text);
  86. return $text;
  87. } else {
  88. $text = $this->convert($text,$module);
  89. return stripslashes( $text );
  90. }
  91. } elseif( $this->_debug ) {
  92. $GLOBALS['vmLogger']->debug( "$var is missing in language file.");
  93. }
  94. return '';
  95. }
  96. /**
  97. * Merges the class vars of another class --> TO BE REMOVED... ?
  98. * @param string The name of the class to merge
  99. * @return boolean True if successful, false is failed
  100. */
  101. function merge( $classname ) {
  102. if (class_exists( $classname )) {
  103. foreach (get_class_vars( $classname ) as $k=>$v) {
  104. if (is_string( $v )) {
  105. if ($k[0] != '_') {
  106. $this->$k = $v;
  107. }
  108. }
  109. }
  110. } else {
  111. return false;
  112. }
  113. }
  114. /**
  115. * Set the debug mode
  116. */
  117. function setDebug() {
  118. if( function_exists('vmshoulddebug')) {
  119. $this->_debug = vmShouldDebug() || $GLOBALS['mosConfig_debug'] == '1';
  120. } else {
  121. $this->_debug = DEBUG || $GLOBALS['mosConfig_debug'] == '1';
  122. }
  123. }
  124. /**
  125. * Set the charset of a language module (normally specified in language file)
  126. * @param string The name of the module
  127. * @param string Forced charset (optional)
  128. * @return none
  129. */
  130. function setCharset($module,$charset='') {
  131. if( !empty( $charset )) {
  132. $this->modules[$module]['CHARSET'] = $charset;
  133. } else {
  134. $this->modules[$module]['CHARSET'] = vmGetCharset();
  135. }
  136. }
  137. /**
  138. * Get the charset of a languge module
  139. * @param string The name of the module
  140. * @return string The charset code
  141. */
  142. function getCharset($module='common') {
  143. return $this->modules[$module]['CHARSET'];
  144. }
  145. /**
  146. * Convert a string, using the convert function set for this module
  147. * @param string The string to be converted
  148. * @param string The name of the module
  149. * @return string The converted string
  150. */
  151. function convert($string,$module='common') {
  152. $func = $this->modules[$module]['CONVERT_FUNC'];
  153. if( !function_exists( $func )) {
  154. $func = 'strval';
  155. }
  156. return $func($string);
  157. }
  158. /**
  159. * This safely converts an iso-8859 string into an utf-8 encoded
  160. * string. It does not convert when the string is already utf-8 encoded
  161. *
  162. * @param string $text iso-8859 encoded text
  163. * @param string $charset This is a k.o.-Argument. If it is NOT equal to 'utf-8', no conversion will take place
  164. * @return string
  165. */
  166. function safe_utf8_encode( $text, $charset ) {
  167. if( strtolower($charset) == 'utf-8' && !vmAbstractLanguage::seems_utf8( $text )) {
  168. // safely decode and reencode the string
  169. $text = utf8_encode($text);
  170. }
  171. // This converts the currency symbol from HTML entity to the utf-8 symbol
  172. // example: &euro; => รข&#x201A;?
  173. $text = vmHtmlEntityDecode( $text, null, vmGetCharset() );
  174. return $text;
  175. }
  176. /**
  177. * a simple function that can help, if you want to know
  178. * if a string could be UTF-8 or not
  179. * @author bmorel at ssi dot fr
  180. * @param unknown_type $Str
  181. * @return boolean
  182. */
  183. function seems_utf8($Str) {
  184. for ($i=0; $i<strlen($Str); $i++) {
  185. if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb
  186. elseif ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
  187. elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
  188. elseif ((ord($Str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
  189. elseif ((ord($Str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
  190. elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
  191. else return false; # Does not match any model
  192. for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
  193. if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80)) {
  194. return false;
  195. }
  196. }
  197. }
  198. return true;
  199. }
  200. /**
  201. * Check if a language variable exists in current language file
  202. * @param string Name of the Class Variable
  203. * @return mixed the name of the module if exists, false is not exists
  204. */
  205. function exists($var,$module=false) {
  206. global $modulename;
  207. if (!$module) $module=$modulename;
  208. $key = strtoupper( $var );
  209. if (isset($this->modules[$module][$key])) {
  210. return $module;
  211. } elseif (isset($this->modules['common'][$key])) {
  212. return 'common';
  213. } else {
  214. foreach ( $this->modules as $lang_module ) {
  215. if( isset( $lang_module[$key])) {
  216. return $lang_module[$this->key_varname];
  217. }
  218. }
  219. return false;
  220. }
  221. }
  222. /**
  223. * Load the language file of a specified module
  224. * @param string The language module to load
  225. * @return boolean True if file exists, false is non exists
  226. */
  227. function load($module) {
  228. global $mosConfig_lang;
  229. if( empty( $module )) return false;
  230. $module = basename( $module );
  231. if (file_exists( ADMINPATH. 'languages/'.$module.'/'.strtolower($mosConfig_lang).'.php' )) {
  232. require_once( ADMINPATH. 'languages/'.$module.'/'.strtolower($mosConfig_lang).'.php' );
  233. return true;
  234. } else if (file_exists( ADMINPATH. 'languages/'.$module.'/english.php' )) {
  235. require_once( ADMINPATH. 'languages/'.$module.'/english.php' );
  236. return true;
  237. } else {
  238. // setting the module to false, to know that has already tried to load this
  239. $this->modules[$module] = false;
  240. return false;
  241. }
  242. }
  243. /**
  244. * Initialize the strings array of a language module
  245. * @param string The language module to init
  246. * @param array The array of language strings
  247. * @return none
  248. */
  249. function initModule($module,&$vars) {
  250. $this->modules[$module] =& $vars;
  251. $this->modules[$module][$this->key_varname] = $module;
  252. $this->modules[$module]['CONVERT_FUNC'] = 'strval';
  253. if( empty( $this->modules[$module]['CHARSET'] )) $this->setCharset($module);
  254. // get global charset setting
  255. $iso = explode( '=', @constant('_ISO') );
  256. // If $iso[1] is NOT empty, it is Mambo or Joomla! 1.0.x - otherwise Joomla! >= 1.5
  257. $charset = !empty( $iso[1] ) ? $iso[1] : 'utf-8';
  258. // Prepare the convert function if necessary
  259. if( strtolower($charset)=='utf-8' && stristr($this->modules[$module]['CHARSET'], 'iso-8859-1' ) ) {
  260. $this->modules[$module]['CONVERT_FUNC'] = 'utf8_encode';
  261. } elseif( stristr($charset, 'iso-8859-1') && strtolower($this->modules[$module]['CHARSET'])=='utf-8' ) {
  262. $this->modules[$module]['CONVERT_FUNC'] = 'utf8_decode';
  263. } elseif( strpos($charset, '1251') && strtolower($this->modules[$module]['CHARSET'])=='utf-8' ) {
  264. $this->modules[$module]['CONVERT_FUNC'] = 'utf8_to_cp1251';
  265. }
  266. }
  267. }
  268. class mosAbstractLanguage extends vmAbstractLanguage { }
  269. class vmLanguage extends vmAbstractLanguage { }
  270. class phpShopLanguage extends vmLanguage { }
  271. ?>