Lang.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\Lang;
  3. use ModulesGarden\ProxmoxAddon\Core\ModuleConstants;
  4. use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs as wModels;
  5. ;
  6. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  7. /**
  8. * Simple class to translating languages
  9. * @author Michal Czech <michael@modulesgarden.com>
  10. * @SuppressWarnings(PHPMD)
  11. */
  12. class Lang
  13. {
  14. /**
  15. * @var string
  16. */
  17. private $dir;
  18. private $isDebug;
  19. /**
  20. * @var Array
  21. */
  22. private $langs = [];
  23. /**
  24. * @var type
  25. */
  26. private $currentLang;
  27. /**
  28. * @var bool
  29. */
  30. private $fillLangFile = true;
  31. /**
  32. * @var array
  33. */
  34. public $context = [];
  35. /**
  36. * @var array
  37. */
  38. private $staggedContext = [];
  39. /**
  40. * @var array
  41. */
  42. private $missingLangs = [];
  43. private $langReplacements = [];
  44. /**
  45. * @param type $dir
  46. * @param type $lang
  47. */
  48. public function __construct($dir = null, $lang = null)
  49. {
  50. $this->setDir($dir);
  51. $this->isDebug = (bool) (int) sl('configurationAddon')->getConfigValue('debug', false);
  52. if (!$lang)
  53. {
  54. $lang = $this->getLang();
  55. }
  56. if ($lang)
  57. {
  58. $this->setLang($lang);
  59. }
  60. }
  61. public function setDir($dir = null)
  62. {
  63. if ($dir !== null && $dir !== "")
  64. {
  65. $this->dir = $dir;
  66. }
  67. else
  68. {
  69. $this->dir = ModuleConstants::getLangsDir();
  70. }
  71. return $this;
  72. }
  73. public function setLang($lang = 'english')
  74. {
  75. if ($lang)
  76. {
  77. $this->loadLang($lang);
  78. }
  79. else
  80. {
  81. $this->loadLang('english');
  82. }
  83. return $this;
  84. }
  85. public function getMissingLangs()
  86. {
  87. return $this->missingLangs;
  88. }
  89. public function getLang()
  90. {
  91. $language = '';
  92. if (isset($_SESSION['Language']))
  93. { // GET LANG FROM SESSION
  94. $language = strtolower($_SESSION['Language']);
  95. if (!$this->checkIfLangFileExists($language))
  96. {
  97. $language = '';
  98. }
  99. }
  100. if (!$language && isset($_SESSION['uid']))
  101. {
  102. $language = $this->getLangByUserId($_SESSION['uid']);
  103. if (!$this->checkIfLangFileExists($language))
  104. {
  105. $language = '';
  106. }
  107. }
  108. if (!$language)
  109. {
  110. $language = $this->getDefaultConfigLang();
  111. if (!$this->checkIfLangFileExists($language))
  112. {
  113. $language = '';
  114. }
  115. }
  116. if (!$language)
  117. {
  118. $language = 'english';
  119. }
  120. return strtolower($language);
  121. }
  122. protected function getLangByUserId($uid = null)
  123. {
  124. if ($uid)
  125. {
  126. try
  127. {
  128. $cModle = new wModels\Client();
  129. $res = $cModle->where('id', $uid)->first(['language'])->toArray();
  130. return $res['language'];
  131. }
  132. catch (\Exception $exc)
  133. {
  134. return false;
  135. }
  136. }
  137. return false;
  138. }
  139. protected function getDefaultConfigLang()
  140. {
  141. try
  142. {
  143. $cModle = new wModels\Configuration();
  144. $res = $cModle->where('setting', 'Language')->first(['value'])->toArray();
  145. return $res['value'];
  146. }
  147. catch (\Exception $exc)
  148. {
  149. return false;
  150. }
  151. }
  152. protected function checkIfLangFileExists($langName = null)
  153. {
  154. if (is_string($langName))
  155. {
  156. $file = $this->dir . DS . strtolower($langName) . '.php';
  157. if (file_exists($file))
  158. {
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164. public function getAvaiable()
  165. {
  166. $langArray = [];
  167. $handle = opendir($this->dir);
  168. while (false !== ($entry = readdir($handle)))
  169. {
  170. list($lang, $ext) = explode('.', $entry);
  171. if ($lang && isset($ext) && strtolower($ext) == 'php')
  172. {
  173. $langArray[] = $lang;
  174. }
  175. }
  176. return $langArray;
  177. }
  178. public function loadLang($lang)
  179. {
  180. $file = $this->dir . DS . $lang . '.php';
  181. if (file_exists($file))
  182. {
  183. include $file;
  184. $this->langs = array_merge((array) $this->langs,(array) $_LANG);
  185. $this->currentLang = $lang;
  186. }
  187. }
  188. public function setContext()
  189. {
  190. $this->context = [];
  191. foreach (func_get_args() as $name)
  192. {
  193. $this->context[] = $name;
  194. }
  195. }
  196. public function addToContext()
  197. {
  198. foreach (func_get_args() as $name)
  199. {
  200. $this->context[] = $name;
  201. }
  202. }
  203. public function stagCurrentContext($stagName)
  204. {
  205. $this->staggedContext[$stagName] = $this->context;
  206. }
  207. public function unstagContext($stagName)
  208. {
  209. if (isset($this->staggedContext[$stagName]))
  210. {
  211. $this->context = $this->staggedContext[$stagName];
  212. unset($this->staggedContext[$stagName]);
  213. }
  214. }
  215. /**
  216. * Get Translated Lang
  217. *
  218. * @author Michal Czech <michael@modulesgarden.com>
  219. * @return string
  220. */
  221. public function translate()
  222. {
  223. $lang = $this->langs;
  224. $history = [];
  225. foreach ($this->context as $name)
  226. {
  227. if (isset($lang[$name]))
  228. {
  229. $lang = $lang[$name];
  230. }
  231. $history[] = $name;
  232. }
  233. $returnLangArray = false;
  234. foreach (func_get_args() as $find)
  235. {
  236. $find = trim($find);
  237. $history[] = $find;
  238. if (isset($lang[$find]))
  239. {
  240. if (is_array($lang[$find]))
  241. {
  242. $lang = $lang[$find];
  243. }
  244. else
  245. {
  246. $this->replaceConstantVars($lang[$find]);
  247. return htmlentities($lang[$find]);
  248. }
  249. }
  250. else
  251. {
  252. if ($this->fillLangFile)
  253. {
  254. $returnLangArray = true;
  255. }
  256. else
  257. {
  258. return htmlentities($find);
  259. }
  260. }
  261. }
  262. if ($returnLangArray)
  263. {
  264. $this->addMissingLang($history, $returnLangArray);
  265. return $this->parserMissingLang($history);
  266. }
  267. if (is_array($lang) && $this->fillLangFile)
  268. {
  269. $this->addMissingLang($history);
  270. return $this->parserMissingLang($history);
  271. }
  272. return htmlentities($find);
  273. }
  274. /**
  275. * Alias for translate method
  276. * @return type mixed
  277. */
  278. public function tr()
  279. {
  280. return call_user_func_array([$this, 'translate'], func_get_args());
  281. }
  282. /**
  283. * Deprecated
  284. * @return type mixed
  285. */
  286. public function T()
  287. {
  288. return call_user_func_array([$this, 'translate'], func_get_args());
  289. }
  290. /**
  291. * Get Translated Absolute Lang
  292. *
  293. * @return string
  294. */
  295. public function absoluteTranslate()
  296. {
  297. $lang = $this->langs;
  298. $returnLangArray = false;
  299. foreach (func_get_args() as $find)
  300. {
  301. $find = trim($find);
  302. $history[] = $find;
  303. if (isset($lang[$find]))
  304. {
  305. if (is_array($lang[$find]))
  306. {
  307. $lang = $lang[$find];
  308. }
  309. else
  310. {
  311. $this->replaceConstantVars($lang[$find]);
  312. return htmlentities($lang[$find]);
  313. }
  314. }
  315. else
  316. {
  317. if ($this->fillLangFile)
  318. {
  319. $returnLangArray = true;
  320. }
  321. else
  322. {
  323. return htmlentities($find);
  324. }
  325. }
  326. }
  327. if ($returnLangArray)
  328. {
  329. $this->addMissingLang($history);
  330. return $this->parserMissingLang($history);
  331. }
  332. return htmlentities($lang);
  333. }
  334. /**
  335. * Alias for absoluteTranslate method
  336. * @return type mixed
  337. */
  338. public function abtr()
  339. {
  340. return call_user_func_array([$this, 'absoluteTranslate'], func_get_args());
  341. }
  342. /**
  343. * Deprecated
  344. * @return type mixed
  345. */
  346. public function absoluteT()
  347. {
  348. return call_user_func_array([$this, 'absoluteTranslate'], func_get_args());
  349. }
  350. /**
  351. * Get Translated Lang From Main Controler Context
  352. *
  353. * @author Sławomir Miśkowicz <slawomir@modulesgarden.com>
  354. * @return string
  355. */
  356. public function controlerContextTranslate()
  357. {
  358. $tempContext = $this->context;
  359. $controlerContext = array_slice($tempContext, 0, 2);
  360. $this->context = $controlerContext;
  361. $args = func_get_args();
  362. $last = end($args);
  363. $lastKey = key($args);
  364. unset($args[$lastKey]);
  365. foreach ($args as $cont)
  366. {
  367. $this->context[] = $cont;
  368. }
  369. $result = $this->T($last);
  370. $this->context = $tempContext;
  371. return $result;
  372. }
  373. /**
  374. * Alias for absoluteTranslate method
  375. * @return type mixed
  376. */
  377. public function cctr()
  378. {
  379. return call_user_func_array([$this, 'controlerContextTranslate'], func_get_args());
  380. }
  381. /**
  382. * Deprecated
  383. * @return type mixed
  384. */
  385. public function controlerContextT()
  386. {
  387. return call_user_func_array([$this, 'controlerContextTranslate'], func_get_args());
  388. }
  389. /**
  390. * @param array $history
  391. * @return string
  392. */
  393. protected function parserMissingLang($history)
  394. {
  395. if ($this->isDebug)
  396. {
  397. return '$' . "_LANG['" . implode("']['", $history) . "']";
  398. }
  399. return end($history);
  400. }
  401. /**
  402. *
  403. * @param array $history
  404. * @param bool $returnLangArray
  405. */
  406. protected function addMissingLang($history, $returnLangArray = false)
  407. {
  408. if ($returnLangArray)
  409. {
  410. $this->missingLangs['$' . "_LANG['" . implode("']['", $history) . "']"] = ucfirst(end($history));
  411. }
  412. else
  413. {
  414. $this->missingLangs['$' . "_LANG['" . implode("']['", $history) . "']"] = implode(" ", array_slice($history, -3, 3, true));
  415. }
  416. }
  417. /**
  418. *
  419. * @param string $key
  420. * @param string $value
  421. * @return $this
  422. */
  423. public function addReplacementConstant($key, $value)
  424. {
  425. $this->langReplacements[$key] = $value;
  426. return $this;
  427. }
  428. protected function replaceConstantVars(& $langString)
  429. {
  430. if (count($this->langReplacements) === 0)
  431. {
  432. return false;
  433. }
  434. foreach ($this->langReplacements as $key => $value)
  435. {
  436. if (stripos($langString, ':' . $key . ':') !== false)
  437. {
  438. $langString = str_replace(':' . $key . ':', $value, $langString);
  439. //to do:
  440. //unset($this->langReplacements[$key]);
  441. }
  442. }
  443. }
  444. }