PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/xlanguage/include/functions.php

https://gitlab.com/BaseX/BaseX
PHP | 276 lines | 218 code | 12 blank | 46 comment | 24 complexity | b7d73c90131d4a6497533faf075681e1 MD5 | raw file
  1. <?php
  2. // $Id: functions.php,v 1.5 2004/12/12 04:50:37 phppp Exp $
  3. // ------------------------------------------------------------------------ //
  4. // Xlanguage: eXtensible Language Management For Xoops //
  5. // Copyright (c) 2004 Xoops China Community //
  6. // <http://www.xoops.org.cn/> //
  7. // ------------------------------------------------------------------------ //
  8. // This program is free software; you can redistribute it and/or modify //
  9. // it under the terms of the GNU General Public License as published by //
  10. // the Free Software Foundation; either version 2 of the License, or //
  11. // (at your option) any later version. //
  12. // //
  13. // You may not change or alter any portion of this comment or credits //
  14. // of supporting developers from this source code or any supporting //
  15. // source code which is considered copyrighted (c) material of the //
  16. // original comment or credit authors. //
  17. // //
  18. // This program is distributed in the hope that it will be useful, //
  19. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  20. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  21. // GNU General Public License for more details. //
  22. // //
  23. // You should have received a copy of the GNU General Public License //
  24. // along with this program; if not, write to the Free Software //
  25. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
  26. // ------------------------------------------------------------------------ //
  27. // Author: D.J.(phppp) php_pp@hotmail.com //
  28. // URL: http://www.xoops.org.cn //
  29. // ------------------------------------------------------------------------- //
  30. function xlanguage_convert_encoding($value, $out_charset, $in_charset)
  31. {
  32. if (is_array($value)) {
  33. foreach($value as $key => $val){
  34. $value[$key] = xlanguage_convert_encoding($val, $out_charset, $in_charset);
  35. }
  36. }
  37. else {
  38. $value = xlanguage_convert_item($value, $out_charset, $in_charset);
  39. }
  40. return $value;
  41. }
  42. function xlanguage_convert_item($value, $out_charset, $in_charset)
  43. {
  44. if(strtolower($in_charset) == strtolower($out_charset)) {
  45. return $value;
  46. }
  47. $xconv_handler = @xoops_getmodulehandler('xconv', 'xconv', true);
  48. if(is_object($xconv_handler) &&
  49. $converted_value = @$xconv_handler->convert_encoding($value, $out_charset, $in_charset)
  50. ){
  51. return $converted_value;
  52. }
  53. if(XOOPS_USE_MULTIBYTES && function_exists('mb_convert_encoding')) {
  54. $converted_value = @mb_convert_encoding($value, $out_charset, $in_charset);
  55. }elseif(function_exists('iconv')) {
  56. $converted_value = @iconv($in_charset, $out_charset, $value);
  57. }
  58. $value = empty($converted_value) ? $value : $converted_value;
  59. return $value;
  60. }
  61. function xlanguage_createConfig()
  62. {
  63. $xlang_handler=& xoops_getmodulehandler('language', 'xlanguage');
  64. return $xlang_handler->createConfig();
  65. }
  66. function &xlanguage_loadConfig()
  67. {
  68. $xlang_handler=& xoops_getmodulehandler('language', 'xlanguage');
  69. $config =& $xlang_handler->loadFileConfig();
  70. return $config;
  71. }
  72. /**
  73. * Analyzes some PHP environment variables to find the most probable language
  74. * that should be used
  75. *
  76. * @param string $ string to analyze
  77. * @param integer $ type of the PHP environment variable which value is $str
  78. * @global array the list of available translations
  79. * @global string the retained translation keyword
  80. * @access private
  81. */
  82. function xlanguage_lang_detect($str = '', $envType = '')
  83. {
  84. global $available_languages;
  85. foreach ($available_languages AS $key => $value) {
  86. // $envType = 1 for the 'HTTP_ACCEPT_LANGUAGE' environment variable,
  87. // 2 for the 'HTTP_USER_AGENT' one
  88. $expr = $value[0];
  89. if (strpos($expr, '[-_]') === FALSE) {
  90. $expr = str_replace('|', '([-_][[:alpha:]]{2,3})?|', $expr);
  91. }
  92. if (($envType == 1 && eregi('^(' . $expr . ')(;q=[0-9]\\.[0-9])?$', $str))
  93. || ($envType == 2 && eregi('(\(|\[|;[[:space:]])(' . $expr . ')(;|\]|\))', $str))) {
  94. $lang = $key;
  95. //if($lang != 'en')
  96. break;
  97. }
  98. }
  99. return $lang;
  100. }
  101. function xlanguage_detectLang()
  102. {
  103. global $available_languages,$_SERVER;
  104. if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  105. $HTTP_ACCEPT_LANGUAGE = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  106. }
  107. if (!empty($_SERVER['HTTP_USER_AGENT'])) {
  108. $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
  109. }
  110. $lang = '';
  111. $xoops_lang ='';
  112. // 1. try to findout user's language by checking its HTTP_ACCEPT_LANGUAGE
  113. // variable
  114. if (empty($lang) && !empty($HTTP_ACCEPT_LANGUAGE)) {
  115. $accepted = explode(',', $HTTP_ACCEPT_LANGUAGE);
  116. $acceptedCnt = count($accepted);
  117. reset($accepted);
  118. for ($i = 0; $i < $acceptedCnt; $i++) {
  119. $lang = xlanguage_lang_detect($accepted[$i], 1);
  120. if(strncasecmp($lang,'en',2)){
  121. break;
  122. }
  123. }
  124. }
  125. // 2. try to findout user's language by checking its HTTP_USER_AGENT variable
  126. if (empty($lang) && !empty($HTTP_USER_AGENT)) {
  127. $lang = xlanguage_lang_detect($HTTP_USER_AGENT, 2);
  128. }
  129. // 3. If we catch a valid language, configure it
  130. if (!empty($lang)) {
  131. $xoops_lang = $available_languages[$lang][1];
  132. }
  133. return $xoops_lang;
  134. }
  135. function xlanguage_encoding($output){
  136. global $xlanguage;
  137. $output = xlanguage_ml($output);
  138. // escape XML doc
  139. if(preg_match("/^\<\?[\s]?xml[\s]+version=([\"'])[^\>]+\\1[\s]+encoding=([\"'])[^\>]+\\2[\s]?\?\>/i", $output)){
  140. return $output;
  141. }
  142. $in_charset = $xlanguage["charset_base"];
  143. $out_charset = $xlanguage["charset"];
  144. return $output = xlanguage_convert_encoding($output, $out_charset, $in_charset);
  145. }
  146. function xlanguage_ml($s)
  147. {
  148. global $xoopsConfig;
  149. global $xlanguage_langs;
  150. if(!isset($xlanguage_langs)){
  151. $langs =& $GLOBALS["xlanguage_handler"]->getAll(true);
  152. foreach( array_keys($langs) as $_lang ) {
  153. $xlanguage_langs[$_lang] = $langs[$_lang]->getVar("lang_code");
  154. }
  155. unset($langs);
  156. }
  157. if(empty($xlanguage_langs) || count($xlanguage_langs) ==0) return $s;
  158. // escape brackets inside of <code>...</code>
  159. $patterns[] = '/(\<code>.*\<\/code>)/isU';
  160. // escape brackets inside of <input type="..." value="...">
  161. $patterns[] = '/(\<input\b(?![^\>]*\btype=([\'"]?)(submit|image|reset|button))[^\>]*\>)/isU';
  162. // escape brackets inside of <textarea></textarea>
  163. $patterns[] = '/(\<textarea\b[^>]*>[^\<]*\<\/textarea>)/isU';
  164. $s = preg_replace_callback( $patterns , 'xlanguage_ml_escape_bracket' , $s ) ;
  165. // create the pattern between language tags
  166. $pqhtmltags = explode( ',' , preg_quote( XLANGUAGE_TAGS_RESERVED , '/' ) ) ;
  167. $mid_pattern = '(?:(?!(' . implode( '|' , $pqhtmltags ) . ')).)*' ;
  168. $patterns = array();
  169. $replaces = array();
  170. /* */
  171. if(isset($xlanguage_langs[$xoopsConfig['language']])) {
  172. $lang = $xlanguage_langs[$xoopsConfig['language']];
  173. $patterns[] = '/(\[([^\]]*\|)?'.preg_quote($lang).'(\|[^\]]*)?\])('.$mid_pattern.')(\[\/([^\]]*\|)?'.preg_quote($lang).'(\|[^\]]*)?\])/isU';
  174. $replaces[] = '$4';
  175. }
  176. /* */
  177. foreach( array_keys($xlanguage_langs) as $_lang ) {
  178. if($_lang == @$xoopsConfig['language']) continue;
  179. $name = $xlanguage_langs[$_lang];
  180. $patterns[] = '/(\[([^\]]*\|)?'.preg_quote($name).'(\|[^\]]*)?\])('.$mid_pattern.')(\[\/([^\]]*\|)?'.preg_quote($name).'(\|[^\]]*)?(\]\<br[\s]?[\/]?\>|\]))/isU';
  181. $replaces[] = '';
  182. }
  183. if(!empty($xoopsConfig['language'])){
  184. $s = preg_replace( '/\[[\/]?[\|]?'.preg_quote($xoopsConfig['language']).'[\|]?\](\<br \/\>)?/i' , '' , $s ) ;
  185. }
  186. if(count($replaces)>0){
  187. $s = preg_replace( $patterns , $replaces , $s ) ;
  188. }
  189. return $s ;
  190. }
  191. function xlanguage_ml_escape_bracket( $matches )
  192. {
  193. global $xlanguage_langs;
  194. $ret = $matches[1];
  195. if(!empty($xlanguage_langs)) {
  196. $pattern = '/(\[([\/])?('.implode("|",array_map("preg_quote", array_values($xlanguage_langs))).')([\|\]]))/isU';
  197. $ret = preg_replace($pattern, "&#91;\\2\\3\\4", $ret);
  198. }
  199. return $ret;
  200. }
  201. function xlanguage_select_show($options = null)
  202. {
  203. include_once XOOPS_ROOT_PATH."/modules/xlanguage/blocks/xlanguage_blocks.php";
  204. if(empty($options)){
  205. $options[0] = "images"; // display style: image, text, select
  206. $options[1] = " "; // delimitor
  207. $options[2] = 5; // items per line
  208. }
  209. $block = b_xlanguage_select_show($options);
  210. $block["tag"] = "xlanguage";
  211. $content = "";
  212. $i = 1;
  213. if(in_array($block["display"], array("images", "text"))){
  214. foreach($block["languages"] as $name => $lang){
  215. $content .= "<a href=\"".$block["url"].$lang["name"]."\" title=\"".$lang["desc"]."\">";
  216. if($block["display"] == "images"){
  217. $content .= "<img src=\"".$lang["image"]."\" alt=\"".$lang["desc"]."\"";
  218. if($block["selected"] != $lang["name"]){
  219. $content .= " style=\"MozOpacity: .8; opacity: .8; filter:Alpha(opacity=80);\"";
  220. }
  221. $content .= "/>";
  222. }else{
  223. $content .= $lang["desc"];
  224. }
  225. $content .= "</a>";
  226. if( ( $i++ % $block["number"] ) == 0){
  227. $content .= "<br />";
  228. }
  229. }
  230. }else{
  231. $content .= "<select name=\"".$block["tag"]."\"
  232. onChange=\"if(this.options[this.selectedIndex].value.length >0 ) { window.document.location=this.options[this.selectedIndex].value;}\"
  233. >";
  234. foreach($block["languages"] as $name => $lang){
  235. $content .= "<option value=\"".$block["url"].$lang["name"]."\"";
  236. if($block["selected"] == $lang["name"]){
  237. $content .= " selected ";
  238. }
  239. $content .= "/>".$lang["desc"]."</option>";
  240. }
  241. $content .= "</select>";
  242. }
  243. define("XLANGUAGE_SWITCH_CODE", $content);
  244. return true;
  245. }
  246. ?>