Lang.php 11 KB

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