PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Upload/inc/class_language.php

https://gitlab.com/mybbpl/ppm-1.6
PHP | 244 lines | 143 code | 23 blank | 78 comment | 28 complexity | 1d555ccc87f57c497a945bf86d4bf61d MD5 | raw file
  1. <?php
  2. /**
  3. * MyBB 1.6
  4. * Copyright 2010 MyBB Group, All Rights Reserved
  5. *
  6. * Website: http://mybb.com
  7. * License: http://mybb.com/about/license
  8. *
  9. * $Id$
  10. */
  11. class MyLanguage
  12. {
  13. /**
  14. * The path to the languages folder.
  15. *
  16. * @var string
  17. */
  18. public $path;
  19. /**
  20. * The language we are using.
  21. *
  22. * @var string
  23. */
  24. public $language;
  25. /**
  26. * The fallback language we are using.
  27. *
  28. * @var string
  29. */
  30. public $fallback = 'english';
  31. /**
  32. * Information about the current language.
  33. *
  34. * @var array
  35. */
  36. public $settings;
  37. /**
  38. * Set the path for the language folder.
  39. *
  40. * @param string The path to the language folder.
  41. */
  42. function set_path($path)
  43. {
  44. $this->path = $path;
  45. }
  46. /**
  47. * Check if a specific language exists.
  48. *
  49. * @param string The language to check for.
  50. * @return boolean True when exists, false when does not exist.
  51. */
  52. function language_exists($language)
  53. {
  54. $language = preg_replace("#[^a-z0-9\-_]#i", "", $language);
  55. if(file_exists($this->path."/".$language.".php"))
  56. {
  57. return true;
  58. }
  59. else
  60. {
  61. return false;
  62. }
  63. }
  64. /**
  65. * Set the language for an area.
  66. *
  67. * @param string The language to use.
  68. * @param string The area to set the language for.
  69. */
  70. function set_language($language="english", $area="user")
  71. {
  72. global $mybb;
  73. $language = preg_replace("#[^a-z0-9\-_]#i", "", $language);
  74. // Default language is English.
  75. if($language == "")
  76. {
  77. $language = "english";
  78. }
  79. // Check if the language exists.
  80. if(!$this->language_exists($language))
  81. {
  82. die("Language $language ($this->path/$language) is not installed");
  83. }
  84. $this->language = $language;
  85. require $this->path."/".$language.".php";
  86. $this->settings = $langinfo;
  87. // Load the admin language files as well, if needed.
  88. if($area == "admin")
  89. {
  90. if(!is_dir($this->path."/".$language."/{$area}"))
  91. {
  92. if(!is_dir($this->path."/".$mybb->settings['cplanguage']."/{$area}"))
  93. {
  94. if(!is_dir($this->path."/english/{$area}"))
  95. {
  96. die("Your forum does not contain an Administration set. Please reupload the english language administration pack.");
  97. }
  98. else
  99. {
  100. $language = "english";
  101. }
  102. }
  103. else
  104. {
  105. $language = $mybb->settings['cplanguage'];
  106. }
  107. }
  108. $this->language = $language."/{$area}";
  109. $this->fallback = $this->fallback."/{$area}";
  110. }
  111. }
  112. /**
  113. * Load the language variables for a section.
  114. *
  115. * @param string The section name.
  116. * @param boolean Is this a datahandler?
  117. * @param boolean supress the error if the file doesn't exist?
  118. */
  119. function load($section, $isdatahandler=false, $supress_error=false)
  120. {
  121. // Assign language variables.
  122. // Datahandlers are never in admin lang directory.
  123. if($isdatahandler === true)
  124. {
  125. $lfile = $this->path."/".str_replace('/admin', '', $this->language)."/".$section.".lang.php";
  126. }
  127. else
  128. {
  129. $lfile = $this->path."/".$this->language."/".$section.".lang.php";
  130. }
  131. if(file_exists($lfile))
  132. {
  133. require_once $lfile;
  134. }
  135. elseif(file_exists($this->path."/".$this->fallback."/".$section.".lang.php"))
  136. {
  137. require_once $this->path."/".$this->fallback."/".$section.".lang.php";
  138. }
  139. // Deprecated! This fallback will be removed in future versions!
  140. elseif(file_exists($this->path."/english/".$section.".lang.php"))
  141. {
  142. require_once $this->path."/english/".$section.".lang.php";
  143. }
  144. else
  145. {
  146. if($supress_error != true)
  147. {
  148. die("$lfile does not exist");
  149. }
  150. }
  151. // We must unite and protect our language variables!
  152. $lang_keys_ignore = array('language', 'path', 'settings');
  153. if(is_array($l))
  154. {
  155. foreach($l as $key => $val)
  156. {
  157. if((empty($this->$key) || $this->$key != $val) && !in_array($key, $lang_keys_ignore))
  158. {
  159. $this->$key = $val;
  160. }
  161. }
  162. }
  163. }
  164. function sprintf($string)
  165. {
  166. $arg_list = func_get_args();
  167. $num_args = count($arg_list);
  168. for($i = 1; $i < $num_args; $i++)
  169. {
  170. $string = str_replace('{'.$i.'}', $arg_list[$i], $string);
  171. }
  172. return $string;
  173. }
  174. /**
  175. * Get the language variables for a section.
  176. *
  177. * @param boolean Admin variables when true, user when false.
  178. * @return array The language variables.
  179. */
  180. function get_languages($admin=0)
  181. {
  182. $dir = @opendir($this->path);
  183. while($lang = readdir($dir))
  184. {
  185. $ext = my_strtolower(get_extension($lang));
  186. if($lang != "." && $lang != ".." && $ext == "php")
  187. {
  188. $lname = str_replace(".".$ext, "", $lang);
  189. require $this->path."/".$lang;
  190. if(!$admin || ($admin && $langinfo['admin']))
  191. {
  192. $languages[$lname] = $langinfo['name'];
  193. }
  194. }
  195. }
  196. @ksort($languages);
  197. return $languages;
  198. }
  199. /**
  200. * Parse contents for language variables.
  201. *
  202. * @param string The contents to parse.
  203. * @return string The parsed contents.
  204. */
  205. function parse($contents)
  206. {
  207. $contents = preg_replace_callback("#<lang:([a-zA-Z0-9_]+)>#", array($this, 'parse_replace'), $contents);
  208. return $contents;
  209. }
  210. /**
  211. * Replace content with language variable.
  212. *
  213. * @param array Matches.
  214. * @return string Language variable.
  215. */
  216. function parse_replace($matches)
  217. {
  218. return $this->$matches[1];
  219. }
  220. }
  221. ?>