PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/controllers/admin/AdminTranslationsController.php

https://bitbucket.org/enurkov/prestashop
PHP | 2697 lines | 1932 code | 311 blank | 454 comment | 393 complexity | 92985ee301292543ad1fce51fa0b8ab4 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. define ('TEXTAREA_SIZED', 70);
  27. class AdminTranslationsControllerCore extends AdminController
  28. {
  29. /** Name of theme by default */
  30. const DEFAULT_THEME_NAME = 'default';
  31. /** @var string : Link which list all pack of language */
  32. protected $link_lang_pack = 'http://www.prestashop.com/download/lang_packs/get_each_language_pack.php';
  33. /** @var int : number of sentence which can be translated */
  34. protected $total_expression = 0;
  35. /** @var int : number of sentence which aren't translated */
  36. protected $missing_translations = 0;
  37. /** @var array : List of ISO code for all languages */
  38. protected $all_iso_lang = array();
  39. /** @var array */
  40. protected $modules_translations = array();
  41. /** @var array : List of folder which must be ignored */
  42. protected static $ignore_folder = array('.', '..', '.svn', '.htaccess', 'index.php');
  43. /** @var array : List of theme by translation type : FRONT, BACK, ERRORS... */
  44. protected $translations_informations = array();
  45. /** @var array : List of theme by translation type : FRONT, BACK, ERRORS... */
  46. protected $translations_type_for_theme = array('front', 'modules', 'pdf', 'mails');
  47. /** @var array : List of all languages */
  48. protected $languages;
  49. /** @var array : List of all themes */
  50. protected $themes;
  51. /** @var string : Directory of selected theme */
  52. protected $theme_selected;
  53. /** @var string : Name of translations type */
  54. protected $type_selected;
  55. /** @var object : Language for the selected language */
  56. protected $lang_selected;
  57. /** @var boolean : Is true if number of var exceed the suhosin request or post limit */
  58. protected $post_limit_exceed = false;
  59. public function __construct()
  60. {
  61. $this->multishop_context = Shop::CONTEXT_ALL;
  62. parent::__construct();
  63. $this->table = 'translations';
  64. // Include all file for create or read an archive
  65. include_once(_PS_ADMIN_DIR_.'/../tools/tar/Archive_Tar.php');
  66. include_once(_PS_ADMIN_DIR_.'/../tools/pear/PEAR.php');
  67. }
  68. /*
  69. * Set the type which is selected
  70. */
  71. public function setTypeSelected($type_selected)
  72. {
  73. $this->type_selected = $type_selected;
  74. }
  75. /**
  76. * AdminController::initContent() override
  77. * @see AdminController::initContent()
  78. */
  79. public function initContent()
  80. {
  81. if (!is_null($this->type_selected))
  82. {
  83. $method_name = 'initForm'.$this->type_selected;
  84. if (method_exists($this, $method_name))
  85. $this->content = $this->initForm($method_name);
  86. else
  87. {
  88. $this->errors[] = sprintf(Tools::displayError('"%s" does not exist. Maybe you typed the URL manually.'), $this->type_selected);
  89. $this->content = $this->initMain();
  90. }
  91. }
  92. else
  93. $this->content = $this->initMain();
  94. $this->context->smarty->assign(array('content' => $this->content));
  95. }
  96. /**
  97. * This function create vars by default and call the good method for generate form
  98. *
  99. * @param $method_name
  100. * @return call the method $this->method_name()
  101. */
  102. public function initForm($method_name)
  103. {
  104. // Create a title for each translation page
  105. $title = sprintf(
  106. $this->l('%1$s (Language: %2$s, Theme: %3$s)'),
  107. $this->translations_informations[$this->type_selected]['name'],
  108. $this->lang_selected->name,
  109. $this->theme_selected
  110. );
  111. // Set vars for all forms
  112. $this->tpl_view_vars = array(
  113. 'lang' => $this->lang_selected->iso_code,
  114. 'title' => $title,
  115. 'type' => $this->type_selected,
  116. 'theme' => $this->theme_selected,
  117. 'post_limit_exceeded' => $this->post_limit_exceed,
  118. 'url_submit' => self::$currentIndex.'&submitTranslations'.ucfirst($this->type_selected).'=1&token='.$this->token,
  119. 'toggle_button' => $this->displayToggleButton(),
  120. 'textarea_sized' => TEXTAREA_SIZED,
  121. 'auto_translate' => ''
  122. );
  123. // Call method initForm for a type
  124. return $this->{$method_name}();
  125. }
  126. /**
  127. * AdminController::initToolbar() override
  128. * @see AdminController::initToolbar()
  129. */
  130. public function initToolbar()
  131. {
  132. $this->toolbar_btn['save-and-stay'] = array(
  133. 'short' => 'SaveAndStay',
  134. 'href' => '#',
  135. 'desc' => $this->l('Save and stay'),
  136. );
  137. $this->toolbar_btn['save'] = array(
  138. 'href' => '#',
  139. 'desc' => $this->l('Update translations')
  140. );
  141. $this->toolbar_btn['cancel'] = array(
  142. 'href' => self::$currentIndex.'&token='.$this->token,
  143. 'desc' => $this->l('Cancel')
  144. );
  145. }
  146. /**
  147. * Generate the Main page
  148. */
  149. public function initMain()
  150. {
  151. // Block add/update a language
  152. $packs_to_install = array();
  153. $packs_to_update = array();
  154. $token = Tools::getAdminToken('AdminLanguages'.(int)Tab::getIdFromClassName('AdminLanguages').(int)$this->context->employee->id);
  155. $file_name = $this->link_lang_pack.'?version='._PS_VERSION_;
  156. $array_stream_context = array('http' => array('method' => 'GET', 'timeout' => 5));
  157. if ($lang_packs = Tools::file_get_contents($file_name, false, @stream_context_create($array_stream_context)))
  158. // Notice : for php < 5.2 compatibility, Tools::jsonDecode. The second parameter to true will set us
  159. if ($lang_packs != '' && $lang_packs = Tools::jsonDecode($lang_packs, true))
  160. foreach ($lang_packs as $key => $lang_pack)
  161. {
  162. if (!Language::isInstalled($lang_pack['iso_code']))
  163. $packs_to_install[$key] = $lang_pack;
  164. else
  165. $packs_to_update[$key] = $lang_pack;
  166. }
  167. $this->tpl_view_vars = array(
  168. 'theme_default' => self::DEFAULT_THEME_NAME,
  169. 'theme_lang_dir' =>_THEME_LANG_DIR_,
  170. 'token' => $this->token,
  171. 'languages' => $this->languages,
  172. 'translations_type' => $this->translations_informations,
  173. 'translations_type_for_theme' => $this->translations_type_for_theme,
  174. 'packs_to_install' => $packs_to_install,
  175. 'packs_to_update' => $packs_to_update,
  176. 'url_submit' => self::$currentIndex.'&token='.$this->token,
  177. 'themes' => $this->themes,
  178. 'id_theme_current' => $this->context->shop->id_theme,
  179. 'url_create_language' => 'index.php?controller=AdminLanguages&addlang&token='.$token,
  180. );
  181. $this->toolbar_scroll = false;
  182. $this->base_tpl_view = 'main.tpl';
  183. return parent::renderView();
  184. }
  185. /**
  186. * This method merge each arrays of modules translation in the array of modules translations
  187. */
  188. protected function getModuleTranslations()
  189. {
  190. global $_MODULE;
  191. $name_var = $this->translations_informations[$this->type_selected]['var'];
  192. if (!isset($_MODULE) && !isset($GLOBALS[$name_var]))
  193. $GLOBALS[$name_var] = array();
  194. else if (isset($_MODULE))
  195. if (is_array($GLOBALS[$name_var]) && is_array($_MODULE))
  196. $GLOBALS[$name_var] = array_merge($GLOBALS[$name_var], $_MODULE);
  197. else
  198. $GLOBALS[$name_var] = $_MODULE;
  199. }
  200. /**
  201. * This method is only used by AdminTranslations::submitCopyLang().
  202. *
  203. * It try to create folder in new theme.
  204. *
  205. * When a translation file is copied for a module, its translation key is wrong.
  206. * We have to change the translation key and rewrite the file.
  207. *
  208. * @param string $dest file name
  209. * @return bool
  210. */
  211. protected function checkDirAndCreate($dest)
  212. {
  213. $bool = true;
  214. // To get only folder path
  215. $path = dirname($dest);
  216. // If folder wasn't already added
  217. // Do not use Tools::file_exists_cache because it changes over time!
  218. if (!file_exists($path))
  219. if (!mkdir($path, 0777, true))
  220. {
  221. $bool &= false;
  222. $this->errors[] = sprintf($this->l('Cannot create the folder "%s". Check directory writing permisions.'), $path);
  223. }
  224. return $bool;
  225. }
  226. /**
  227. * Read the Post var and write the translation file.
  228. * This method overwrites the old translation file.
  229. *
  230. * @param bool $override_file : set true if this file is a override
  231. */
  232. protected function writeTranslationFile($override_file = false)
  233. {
  234. $type = Tools::toCamelCase($this->type_selected, true);
  235. $translation_informations = $this->translations_informations[$this->type_selected];
  236. if ($override_file)
  237. $file_path = $translation_informations['override']['dir'].$translation_informations['override']['file'];
  238. else
  239. $file_path = $translation_informations['dir'].$translation_informations['file'];
  240. if (!file_exists($file_path))
  241. {
  242. if (!file_exists(dirname($file_path)) && !mkdir(dirname($file_path), 0777, true))
  243. throw new PrestaShopException(sprintf(Tools::displayError('Directory "%s" cannot be created'), dirname($file_path)));
  244. elseif (!touch($file_path))
  245. throw new PrestaShopException(sprintf(Tools::displayError('File "%s" cannot be created'), $file_path));
  246. }
  247. if ($fd = fopen($file_path, 'w'))
  248. {
  249. // Get value of button save and stay
  250. $save_and_stay = Tools::getValue('submitTranslations'.$type.'AndStay');
  251. // Get language
  252. $lang = strtolower(Tools::getValue('lang'));
  253. // Unset all POST which are not translations
  254. unset(
  255. $_POST['submitTranslations'.$type],
  256. $_POST['submitTranslations'.$type.'AndStay'],
  257. $_POST['lang'],
  258. $_POST['token'],
  259. $_POST['theme'],
  260. $_POST['type']
  261. );
  262. // Get all POST which aren't empty
  263. $to_insert = array();
  264. foreach ($_POST as $key => $value)
  265. if (!empty($value))
  266. $to_insert[$key] = $value;
  267. // translations array is ordered by key (easy merge)
  268. ksort($to_insert);
  269. $tab = $translation_informations['var'];
  270. fwrite($fd, "<?php\n\nglobal \$".$tab.";\n\$".$tab." = array();\n");
  271. foreach ($to_insert as $key => $value)
  272. fwrite($fd, '$'.$tab.'[\''.pSQL($key, true).'\'] = \''.pSQL($value, true).'\';'."\n");
  273. fwrite($fd, "\n?>");
  274. fclose($fd);
  275. // Redirect
  276. if ($save_and_stay)
  277. $this->redirect(true);
  278. else
  279. $this->redirect();
  280. }
  281. else
  282. throw new PrestaShopException(sprintf(Tools::displayError('Cannot write this file: "%s"'), $file_path));
  283. }
  284. public function submitCopyLang()
  285. {
  286. if (!($from_lang = Tools::getValue('fromLang')) || !($to_lang = Tools::getValue('toLang')))
  287. $this->errors[] = $this->l('You must select 2 languages in order to copy data from one to another');
  288. else if (!($from_theme = Tools::getValue('fromTheme')) || !($to_theme = Tools::getValue('toTheme')))
  289. $this->errors[] = $this->l('You must select 2 themes in order to copy data from one to another');
  290. else if (!Language::copyLanguageData(Language::getIdByIso($from_lang), Language::getIdByIso($to_lang)))
  291. $this->errors[] = $this->l('An error occurred while copying data');
  292. else if ($from_lang == $to_lang && $from_theme == $to_theme)
  293. $this->errors[] = $this->l('Nothing to copy! (same language and theme)');
  294. else
  295. {
  296. $theme_exists = array('from_theme' => false, 'to_theme' => false);
  297. foreach ($this->themes as $theme)
  298. {
  299. if ($theme->directory == $from_theme)
  300. $theme_exists['from_theme'] = true;
  301. if ($theme->directory == $to_theme)
  302. $theme_exists['to_theme'] = true;
  303. }
  304. if ($theme_exists['from_theme'] == false || $theme_exists['to_theme'] == false)
  305. $this->errors[] = $this->l('Theme(s) not found');
  306. }
  307. if (count($this->errors))
  308. return;
  309. $bool = true;
  310. $items = Language::getFilesList($from_lang, $from_theme, $to_lang, $to_theme, false, false, true);
  311. foreach ($items as $source => $dest)
  312. {
  313. $bool &= $this->checkDirAndCreate($dest);
  314. $bool &= @copy($source, $dest);
  315. if (strpos($dest, 'modules') && basename($source) === $from_lang.'.php' && $bool !== false)
  316. $bool &= $this->changeModulesKeyTranslation($dest, $from_theme, $to_theme);
  317. }
  318. if ($bool)
  319. $this->redirect(false, 14);
  320. $this->errors[] = $this->l('A part of the data has been copied but some language files could not be found or copied');
  321. }
  322. /**
  323. * Change the key translation to according it to theme name.
  324. *
  325. * @param string $path
  326. * @param string $theme_from
  327. * @param string $theme_to
  328. * @return boolean
  329. */
  330. public function changeModulesKeyTranslation($path, $theme_from, $theme_to)
  331. {
  332. $content = file_get_contents($path);
  333. $arr_replace = array();
  334. $bool_flag = true;
  335. if (preg_match_all('#\$_MODULE\[\'([^\']+)\'\]#Ui', $content, $matches))
  336. {
  337. foreach ($matches[1] as $key => $value)
  338. $arr_replace[$value] = str_replace($theme_from, $theme_to, $value);
  339. $content = str_replace(array_keys($arr_replace), array_values($arr_replace), $content);
  340. $bool_flag = (file_put_contents($path, $content) === false) ? false : true;
  341. }
  342. return $bool_flag;
  343. }
  344. public function exportTabs()
  345. {
  346. // Get name tabs by iso code
  347. $tabs = Tab::getTabs($this->lang_selected->id);
  348. // Get name of the default tabs
  349. $tabs_default_lang = Tab::getTabs(1);
  350. $tabs_default = array();
  351. foreach ($tabs_default_lang as $tab)
  352. $tabs_default[$tab['class_name']] = pSQL($tab['name']);
  353. // Create content
  354. $content = "<?php\n\n\$tabs = array();";
  355. if (!empty($tabs))
  356. foreach ($tabs as $tab)
  357. if ($tabs_default[$tab['class_name']] != pSQL($tab['name']))
  358. $content .= "\n\$tabs['".$tab['class_name']."'] = '".pSQL($tab['name'])."';";
  359. $content .= "\n\nreturn \$tabs;";
  360. $dir = _PS_TRANSLATIONS_DIR_.$this->lang_selected->iso_code.DIRECTORY_SEPARATOR;
  361. $path = $dir.'tabs.php';
  362. // Check if tabs.php exists for the selected Iso Code
  363. if (!Tools::file_exists_cache($dir))
  364. if (!mkdir($dir, 0777, true))
  365. throw new PrestaShopException('The file '.$dir.' cannot be created.');
  366. if (!file_put_contents($path, $content))
  367. throw new PrestaShopException('File "'.$path.'" doesn\'t exists and cannot be created in '.$dir);
  368. if (!is_writable($path))
  369. $this->displayWarning(sprintf(Tools::displayError('This file must be writable: %s'), $path));
  370. }
  371. public function submitExportLang()
  372. {
  373. if ($this->lang_selected->iso_code && $this->theme_selected)
  374. {
  375. $this->exportTabs();
  376. $items = array_flip(Language::getFilesList($this->lang_selected->iso_code, $this->theme_selected, false, false, false, false, true));
  377. $gz = new Archive_Tar(_PS_TRANSLATIONS_DIR_.'/export/'.$this->lang_selected->iso_code.'.gzip', true);
  378. $file_name = Tools::getCurrentUrlProtocolPrefix().Tools::getShopDomain().__PS_BASE_URI__.'translations/export/'.$this->lang_selected->iso_code.'.gzip';
  379. if ($gz->createModify($items, null, _PS_ROOT_DIR_));
  380. {
  381. ob_start();
  382. header('Pragma: public');
  383. header('Expires: 0');
  384. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  385. header('Cache-Control: public');
  386. header('Content-Description: File Transfer');
  387. header('Content-type: application/octet-stream');
  388. header('Content-Disposition: attachment; filename="'.$this->lang_selected->iso_code.'.gzip'.'"');
  389. header('Content-Transfer-Encoding: binary');
  390. ob_end_flush();
  391. @readfile($file_name);
  392. exit;
  393. }
  394. $this->errors[] = Tools::displayError('An error occurred while creating archive.');
  395. }
  396. $this->errors[] = Tools::displayError('Please choose a language and a theme.');
  397. }
  398. public static function checkAndAddMailsFiles($iso_code, $files_list)
  399. {
  400. // 1 - Scan mails files
  401. $mails = scandir(_PS_MAIL_DIR_.'en/');
  402. $mails_new_lang = array();
  403. // Get all email files
  404. foreach ($files_list as $file)
  405. {
  406. if (preg_match('#^mails\/([a-z0-9]+)\/#Ui', $file['filename'], $matches))
  407. {
  408. $slash_pos = strrpos($file['filename'], '/');
  409. $mails_new_lang[] = substr($file['filename'], -(strlen($file['filename']) - $slash_pos - 1));
  410. }
  411. }
  412. // Get the difference
  413. $arr_mails_needed = array_diff($mails, $mails_new_lang);
  414. // Add mails files
  415. foreach ($arr_mails_needed as $mail_to_add)
  416. if (!in_array($mail_to_add, self::$ignore_folder))
  417. @copy(_PS_MAIL_DIR_.'en/'.$mail_to_add, _PS_MAIL_DIR_.$iso_code.'/'.$mail_to_add);
  418. // 2 - Scan modules files
  419. $modules = scandir(_PS_MODULE_DIR_);
  420. $module_mail_en = array();
  421. $module_mail_iso_code = array();
  422. foreach ($modules as $module)
  423. {
  424. if (!in_array($module, self::$ignore_folder) && Tools::file_exists_cache(_PS_MODULE_DIR_.$module.'/mails/en/'))
  425. {
  426. $arr_files = scandir(_PS_MODULE_DIR_.$module.'/mails/en/');
  427. foreach ($arr_files as $file)
  428. {
  429. if (!in_array($file, self::$ignore_folder))
  430. {
  431. if (Tools::file_exists_cache(_PS_MODULE_DIR_.$module.'/mails/en/'.$file))
  432. $module_mail_en[] = _PS_MODULE_DIR_.$module.'/mails/ISO_CODE/'.$file;
  433. if (Tools::file_exists_cache(_PS_MODULE_DIR_.$module.'/mails/'.$iso_code.'/'.$file))
  434. $module_mail_iso_code[] = _PS_MODULE_DIR_.$module.'/mails/ISO_CODE/'.$file;
  435. }
  436. }
  437. }
  438. }
  439. // Get the difference in this modules
  440. $arr_modules_mails_needed = array_diff($module_mail_en, $module_mail_iso_code);
  441. // Add mails files for this modules
  442. foreach ($arr_modules_mails_needed as $file)
  443. {
  444. $file_en = str_replace('ISO_CODE', 'en', $file);
  445. $file_iso_code = str_replace('ISO_CODE', $iso_code, $file);
  446. $dir_iso_code = substr($file_iso_code, 0, -(strlen($file_iso_code) - strrpos($file_iso_code, '/') - 1));
  447. if (!file_exists($dir_iso_code))
  448. mkdir($dir_iso_code);
  449. if (Tools::file_exists_cache($file_en))
  450. copy($file_en, $file_iso_code);
  451. }
  452. }
  453. /**
  454. * Move theme translations in selected themes
  455. *
  456. * @param array $files
  457. * @param array $themes_selected
  458. */
  459. public function checkAndAddThemesFiles($files, $themes_selected)
  460. {
  461. foreach ($files as $file)
  462. {
  463. // Check if file is a file theme
  464. if (preg_match('#^themes\/([a-z0-9]+)\/lang\/#Ui', $file['filename'], $matches))
  465. {
  466. $slash_pos = strrpos($file['filename'], '/');
  467. $name_file = substr($file['filename'], -(strlen($file['filename']) - $slash_pos - 1));
  468. $name_default_theme = $matches[1];
  469. $deleted_old_theme = false;
  470. // Get the old file theme
  471. if (file_exists(_PS_THEME_DIR_.'lang/'.$name_file))
  472. $theme_file_old = _PS_THEME_DIR_.'lang/'.$name_file;
  473. else
  474. {
  475. $deleted_old_theme = true;
  476. $theme_file_old = str_replace(self::DEFAULT_THEME_NAME, $name_default_theme, _PS_THEME_DIR_.'lang/'.$name_file);
  477. }
  478. // Move the old file theme in the new folder
  479. foreach ($themes_selected as $theme_name)
  480. if (file_exists($theme_file_old))
  481. copy($theme_file_old, str_replace($name_default_theme, $theme_name, $theme_file_old));
  482. if ($deleted_old_theme)
  483. @unlink($theme_file_old);
  484. }
  485. }
  486. }
  487. /**
  488. * Add new translations tabs by code ISO
  489. *
  490. * @param array $iso_code
  491. * @param array $files
  492. */
  493. public static function addNewTabs($iso_code, $files)
  494. {
  495. $errors = array();
  496. foreach ($files as $file)
  497. {
  498. // Check if file is a file theme
  499. if (preg_match('#^translations\/'.$iso_code.'\/tabs.php#Ui', $file['filename'], $matches) && Validate::isLanguageIsoCode($iso_code))
  500. {
  501. // Include array width new translations tabs
  502. $tabs = include _PS_ROOT_DIR_.DIRECTORY_SEPARATOR.$file['filename'];
  503. foreach ($tabs as $class_name => $translations)
  504. {
  505. // Get instance of this tab by class name
  506. $tab = Tab::getInstanceFromClassName($class_name);
  507. //Check if class name exists
  508. if (isset($tab->class_name) && !empty($tab->class_name))
  509. {
  510. $id_lang = Language::getIdByIso($iso_code);
  511. $tab->name[(int)$id_lang] = $translations;
  512. if (!Validate::isGenericName($tab->name[(int)$id_lang]))
  513. $errors[] = sprintf(Tools::displayError('Tab "%s" is not valid'), $tab->name[(int)$id_lang]);
  514. else
  515. $tab->update();
  516. }
  517. }
  518. }
  519. }
  520. return $errors;
  521. }
  522. public static function checkTranslationFile($content)
  523. {
  524. $lines = array_map('trim', explode("\n", $content));
  525. $global = false;
  526. foreach ($lines as $line)
  527. {
  528. if (in_array($line, array('<?php', '?>', '')))
  529. continue;
  530. if (!$global && preg_match('/^global\s+\$([a-z0-9-_]+)\s*;$/i', $line, $matches))
  531. {
  532. $global = $matches[1];
  533. continue;
  534. }
  535. if ($global != false && preg_match('/^\$'.preg_quote($global, '/').'\s*=\s*array\(\s*\)\s*;$/i', $line))
  536. continue;
  537. if (!$global && preg_match('/^\$([a-z0-9-_]+)\s*=\s*array\(\s*\)\s*;$/i', $line, $matches))
  538. {
  539. $global = $matches[1];
  540. continue;
  541. }
  542. if (preg_match('/^\$'.preg_quote($global, '/').'\[\''._PS_TRANS_PATTERN_.'\'\]\s*=\s*\''._PS_TRANS_PATTERN_.'\'\s*;$/i', $line))
  543. continue;
  544. if (preg_match('/^return\s+\$'.preg_quote($global, '/').'\s*;$/i', $line, $matches))
  545. continue;
  546. return false;
  547. }
  548. return true;
  549. }
  550. public function submitImportLang()
  551. {
  552. if (!isset($_FILES['file']['tmp_name']) || !$_FILES['file']['tmp_name'])
  553. $this->errors[] = Tools::displayError('No file selected');
  554. else
  555. {
  556. $gz = new Archive_Tar($_FILES['file']['tmp_name'], true);
  557. $filename = $_FILES['file']['name'];
  558. $iso_code = str_replace(array('.tar.gz', '.gzip'), '', $filename);
  559. if (Validate::isLangIsoCode($iso_code))
  560. {
  561. $themes_selected = Tools::getValue('theme', array(self::DEFAULT_THEME_NAME));
  562. $files_list = $gz->listContent();
  563. $uniqid = uniqid();
  564. $sandbox = _PS_CACHE_DIR_.'sandbox'.DIRECTORY_SEPARATOR.$uniqid.DIRECTORY_SEPARATOR;
  565. if ($gz->extract($sandbox, false))
  566. {
  567. foreach ($files_list as $file2check)
  568. {
  569. //don't validate index.php, will be overwrite when extract in translation directory
  570. if (pathinfo($file2check['filename'], PATHINFO_BASENAME) == 'index.php')
  571. continue;
  572. if (preg_match('@^[0-9a-z-_/\\\\]+\.php$@i', $file2check['filename']))
  573. {
  574. if (!AdminTranslationsController::checkTranslationFile(file_get_contents($sandbox.$file2check['filename'])))
  575. $this->errors[] = sprintf(Tools::displayError('Validation failed for: %s'), $file2check['filename']);
  576. }
  577. elseif (!preg_match('@^[0-9a-z-_/\\\\]+\.(html|tpl|txt)$@i', $file2check['filename']))
  578. $this->errors[] = sprintf(Tools::displayError('Unidentified file found: %s'), $file2check['filename']);
  579. }
  580. Tools::deleteDirectory($sandbox, true);
  581. }
  582. if (count($this->errors))
  583. return false;
  584. if ($gz->extract(_PS_TRANSLATIONS_DIR_.'../', false))
  585. {
  586. foreach ($files_list as $file2check)
  587. if (pathinfo($file2check['filename'], PATHINFO_BASENAME) == 'index.php' && file_put_contents(_PS_TRANSLATIONS_DIR_.'../'.$file2check['filename'], Tools::getDefaultIndexContent()))
  588. continue;
  589. AdminTranslationsController::checkAndAddMailsFiles($iso_code, $files_list);
  590. $this->checkAndAddThemesFiles($files_list, $themes_selected);
  591. $tab_errors = AdminTranslationsController::addNewTabs($iso_code, $files_list);
  592. if (count($tab_errors))
  593. {
  594. $this->errors += $tab_errors;
  595. return false;
  596. }
  597. if (Validate::isLanguageFileName($filename))
  598. {
  599. if (!Language::checkAndAddLanguage($iso_code))
  600. $conf = 20;
  601. }
  602. $this->redirect(false, (isset($conf) ? $conf : '15'));
  603. }
  604. $this->errors[] = Tools::displayError('Archive cannot be extracted.');
  605. }
  606. else
  607. $this->errors[] = sprintf(Tools::displayError('ISO CODE invalid "%1$s" for the following file: "%2$s"'), $iso_code, $filename);
  608. }
  609. }
  610. public function submitAddLang()
  611. {
  612. $arr_import_lang = explode('|', Tools::getValue('params_import_language')); /* 0 = Language ISO code, 1 = PS version */
  613. if (Validate::isLangIsoCode($arr_import_lang[0]))
  614. {
  615. if ($content = Tools::file_get_contents(
  616. 'http://www.prestashop.com/download/lang_packs/gzip/'.$arr_import_lang[1].'/'.$arr_import_lang[0].'.gzip', false,
  617. @stream_context_create(array('http' => array('method' => 'GET', 'timeout' => 5)))))
  618. {
  619. $file = _PS_TRANSLATIONS_DIR_.$arr_import_lang[0].'.gzip';
  620. if (file_put_contents($file, $content))
  621. {
  622. $gz = new Archive_Tar($file, true);
  623. $files_list = $gz->listContent();
  624. if ($gz->extract(_PS_TRANSLATIONS_DIR_.'../', false))
  625. {
  626. AdminTranslationsController::checkAndAddMailsFiles($arr_import_lang[0], $files_list);
  627. $tab_errors = AdminTranslationsController::addNewTabs($arr_import_lang[0], $files_list);
  628. if (count($tab_errors))
  629. $this->errors += $tab_errors;
  630. else
  631. {
  632. if (!Language::checkAndAddLanguage($arr_import_lang[0]))
  633. $conf = 20;
  634. }
  635. if (!unlink($file))
  636. $this->errors[] = Tools::displayError('Cannot delete archive');
  637. $this->redirect(false, (isset($conf) ? $conf : '15'));
  638. }
  639. $this->errors[] = Tools::displayError('Archive cannot be extracted.');
  640. if (!unlink($file))
  641. $this->errors[] = Tools::displayError('Cannot delete archive');
  642. }
  643. else
  644. $this->errors[] = Tools::displayError('Server does not have permissions for writing.');
  645. }
  646. else
  647. $this->errors[] = Tools::displayError('Language not found');
  648. }
  649. else
  650. $this->errors[] = Tools::displayError('Invalid parameter');
  651. }
  652. /**
  653. * This method check each file (tpl or php file), get its sentences to translate,
  654. * compare with posted values and write in iso code translation file.
  655. *
  656. * @param string $file_name
  657. * @param array $files
  658. * @param string $theme_name
  659. * @param string $module_name
  660. * @param string|boolean $dir
  661. * @return void
  662. */
  663. protected function findAndWriteTranslationsIntoFile($file_name, $files, $theme_name, $module_name, $dir = false)
  664. {
  665. // These static vars allow to use file to write just one time.
  666. static $cache_file = array();
  667. static $str_write = '';
  668. static $array_check_duplicate = array();
  669. // Default translations and Prestashop overriding themes are distinguish
  670. $is_default = $theme_name === self::DEFAULT_THEME_NAME ? true : false;
  671. // Set file_name in static var, this allow to open and wright the file just one time
  672. if (!isset($cache_file[$theme_name.'-'.$file_name]))
  673. {
  674. $str_write = '';
  675. $cache_file[$theme_name.'-'.$file_name] = true;
  676. if (!Tools::file_exists_cache($file_name))
  677. file_put_contents($file_name, '');
  678. if (!is_writable($file_name))
  679. throw new PrestaShopException(sprintf(
  680. Tools::displayError('Cannot write to the theme\'s language file (%s). Please check write permissions.'),
  681. $file_name
  682. ));
  683. // this string is initialized one time for a file
  684. $str_write .= "<?php\n\nglobal \$_MODULE;\n\$_MODULE = array();\n";
  685. $array_check_duplicate = array();
  686. }
  687. foreach ($files as $file)
  688. {
  689. if (preg_match('/^(.*).(tpl|php)$/', $file) && Tools::file_exists_cache($file_path = $dir.$file) && !in_array($file, self::$ignore_folder))
  690. {
  691. // Get content for this file
  692. $content = file_get_contents($file_path);
  693. // Get file type
  694. $type_file = substr($file, -4) == '.tpl' ? 'tpl' : 'php';
  695. // Parse this content
  696. $matches = $this->userParseFile($content, $this->type_selected, $type_file);
  697. // Write each translation on its module file
  698. $template_name = substr(basename($file), 0, -4);
  699. foreach ($matches as $key)
  700. {
  701. if ($is_default)
  702. {
  703. $post_key = md5(strtolower($module_name).'_'.self::DEFAULT_THEME_NAME.'_'.strtolower($template_name).'_'.md5($key));
  704. $pattern = '\'<{'.strtolower($module_name).'}prestashop>'.strtolower($template_name).'_'.md5($key).'\'';
  705. }
  706. else
  707. {
  708. $post_key = md5(strtolower($module_name).'_'.strtolower($theme_name).'_'.strtolower($template_name).'_'.md5($key));
  709. $pattern = '\'<{'.strtolower($module_name).'}'.strtolower($theme_name).'>'.strtolower($template_name).'_'.md5($key).'\'';
  710. }
  711. if (array_key_exists($post_key, $_POST) && !empty($_POST[$post_key]) && !in_array($pattern, $array_check_duplicate))
  712. {
  713. $array_check_duplicate[] = $pattern;
  714. $str_write .= '$_MODULE['.$pattern.'] = \''.pSQL(str_replace(array("\r\n", "\r", "\n"), ' ', $_POST[$post_key])).'\';'."\n";
  715. $this->total_expression++;
  716. }
  717. }
  718. }
  719. }
  720. if (isset($cache_file[$theme_name.'-'.$file_name]) && $str_write != "<?php\n\nglobal \$_MODULE;\n\$_MODULE = array();\n")
  721. file_put_contents($file_name, $str_write);
  722. }
  723. /**
  724. * Clear the list of module file by type (file or directory)
  725. *
  726. * @param $files : list of files
  727. * @param string $type_clear (file|directory)
  728. * @param string $path
  729. * @return array : list of a good files
  730. */
  731. public function clearModuleFiles($files, $type_clear = 'file', $path = '')
  732. {
  733. // List of directory which not must be parsed
  734. $arr_exclude = array('img', 'js', 'mails');
  735. // List of good extention files
  736. $arr_good_ext = array('.tpl', '.php');
  737. foreach ($files as $key => $file)
  738. {
  739. if ($file{0} === '.' || in_array(substr($file, 0, strrpos($file, '.')), $this->all_iso_lang))
  740. unset($files[$key]);
  741. else if ($type_clear === 'file' && !in_array(substr($file, strrpos($file, '.')), $arr_good_ext))
  742. unset($files[$key]);
  743. else if ($type_clear === 'directory' && (!is_dir($path.$file) || in_array($file, $arr_exclude)))
  744. unset($files[$key]);
  745. }
  746. return $files;
  747. }
  748. /**
  749. * This method get translation for each files of a module,
  750. * compare with global $_MODULES array and fill AdminTranslations::modules_translations array
  751. * With key as English sentences and values as their iso code translations.
  752. *
  753. * @param array $files
  754. * @param string $theme_name
  755. * @param string $module_name
  756. * @param string|boolean $dir
  757. * @param string $iso_code
  758. * @return void
  759. */
  760. protected function findAndFillTranslations($files, $theme_name, $module_name, $dir = false)
  761. {
  762. $name_var = $this->translations_informations[$this->type_selected]['var'];
  763. // added for compatibility
  764. $GLOBALS[$name_var] = array_change_key_case($GLOBALS[$name_var]);
  765. // Default translations and Prestashop overriding themes are distinguish
  766. $is_default = $theme_name === self::DEFAULT_THEME_NAME ? true : false;
  767. // Thank to this var similar keys are not duplicate
  768. // in AndminTranslation::modules_translations array
  769. // see below
  770. $array_check_duplicate = array();
  771. foreach ($files as $file)
  772. {
  773. if ((preg_match('/^(.*).tpl$/', $file) || preg_match('/^(.*).php$/', $file)) && Tools::file_exists_cache($file_path = $dir.$file))
  774. {
  775. // Get content for this file
  776. $content = file_get_contents($file_path);
  777. // Module files can now be ignored by adding this string in a file
  778. if (strpos($content, 'IGNORE_THIS_FILE_FOR_TRANSLATION') !== false)
  779. continue;
  780. // Get file type
  781. $type_file = substr($file, -4) == '.tpl' ? 'tpl' : 'php';
  782. // Parse this content
  783. $matches = $this->userParseFile($content, $this->type_selected, $type_file);
  784. // Write each translation on its module file
  785. $template_name = substr(basename($file), 0, -4);
  786. foreach ($matches as $key)
  787. {
  788. $module_key = '<{'.Tools::strtolower($module_name).'}'.
  789. strtolower($is_default ? 'prestashop' : $theme_name).'>'.Tools::strtolower($template_name).'_'.md5($key);
  790. // to avoid duplicate entry
  791. if (!in_array($module_key, $array_check_duplicate))
  792. {
  793. $array_check_duplicate[] = $module_key;
  794. if (!isset($this->modules_translations[$theme_name][$module_name][$template_name][$key]['trad']))
  795. $this->total_expression++;
  796. if (array_key_exists($module_key, $GLOBALS[$name_var]))
  797. $this->modules_translations[$theme_name][$module_name][$template_name][$key]['trad'] = html_entity_decode($GLOBALS[$name_var][$module_key], ENT_COMPAT, 'UTF-8');
  798. else
  799. {
  800. $this->modules_translations[$theme_name][$module_name][$template_name][$key]['trad'] = '';
  801. $this->missing_translations++;
  802. }
  803. $this->modules_translations[$theme_name][$module_name][$template_name][$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  804. }
  805. }
  806. }
  807. }
  808. }
  809. /**
  810. * Get list of files which must be parsed by directory and by type of translations
  811. *
  812. * @return array : list of files by directory
  813. */
  814. public function getFileToParseByTypeTranslation()
  815. {
  816. $directories = array();
  817. switch ($this->type_selected)
  818. {
  819. case 'front':
  820. $directories['tpl'] = array(_PS_ALL_THEMES_DIR_.'/' => scandir(_PS_ALL_THEMES_DIR_));
  821. self::$ignore_folder[] = 'modules';
  822. $directories['tpl'] = array_merge($directories['tpl'], $this->listFiles(_PS_THEME_SELECTED_DIR_));
  823. if (Tools::file_exists_cache(_PS_THEME_OVERRIDE_DIR_))
  824. $directories['tpl'] = array_merge($directories['tpl'], $this->listFiles(_PS_THEME_OVERRIDE_DIR_));
  825. break;
  826. case 'back':
  827. $directories = array(
  828. 'php' => array(
  829. _PS_ADMIN_CONTROLLER_DIR_.'/' => scandir(_PS_ADMIN_CONTROLLER_DIR_),
  830. _PS_OVERRIDE_DIR_.'controllers/admin/' => scandir(_PS_OVERRIDE_DIR_.'controllers/admin/'),
  831. _PS_CLASS_DIR_.'helper/' => scandir(_PS_CLASS_DIR_.'helper/'),
  832. _PS_CLASS_DIR_.'controller/' => array('AdminController.php'),
  833. _PS_CLASS_DIR_ => array('PaymentModule.php')
  834. ),
  835. 'tpl' => $this->listFiles(_PS_ADMIN_DIR_.'/themes/'),
  836. 'specific' => array(
  837. _PS_ADMIN_DIR_.'/' => array(
  838. 'header.inc.php',
  839. 'footer.inc.php',
  840. 'index.php',
  841. 'login.php',
  842. 'password.php',
  843. 'functions.php'
  844. )
  845. )
  846. );
  847. // For translate the template which are overridden
  848. if (file_exists(_PS_OVERRIDE_DIR_.'controllers'.DIRECTORY_SEPARATOR.'admin'.DIRECTORY_SEPARATOR.'templates'))
  849. $directories['tpl'] = array_merge($directories['tpl'], $this->listFiles(_PS_OVERRIDE_DIR_.'controllers'.DIRECTORY_SEPARATOR.'admin'.DIRECTORY_SEPARATOR.'templates'));
  850. break;
  851. case 'errors':
  852. $directories['php'] = array(
  853. _PS_ROOT_DIR_.'/' => scandir(_PS_ROOT_DIR_),
  854. _PS_ADMIN_DIR_.'/' => scandir(_PS_ADMIN_DIR_),
  855. _PS_FRONT_CONTROLLER_DIR_ => scandir(_PS_FRONT_CONTROLLER_DIR_),
  856. _PS_ADMIN_CONTROLLER_DIR_ => scandir(_PS_ADMIN_CONTROLLER_DIR_),
  857. _PS_OVERRIDE_DIR_.'controllers/front/' => scandir(_PS_OVERRIDE_DIR_.'controllers/front/'),
  858. _PS_OVERRIDE_DIR_.'controllers/admin/' => scandir(_PS_OVERRIDE_DIR_.'controllers/admin/')
  859. );
  860. // Get all files for folders classes/ and override/classes/ recursively
  861. $directories['php'] = array_merge($directories['php'], $this->listFiles(_PS_CLASS_DIR_, array(), 'php'));
  862. $directories['php'] = array_merge($directories['php'], $this->listFiles(_PS_OVERRIDE_DIR_.'classes/', array(), 'php'));
  863. break;
  864. case 'fields':
  865. $directories['php'] = $this->listFiles(_PS_CLASS_DIR_, array(), 'php');
  866. break;
  867. case 'pdf':
  868. $tpl_theme = Tools::file_exists_cache(_PS_THEME_SELECTED_DIR_.'pdf/') ? scandir(_PS_THEME_SELECTED_DIR_.'pdf/') : array();
  869. $directories = array(
  870. 'php' => array(
  871. _PS_CLASS_DIR_.'pdf/' => scandir(_PS_CLASS_DIR_.'pdf/'),
  872. _PS_OVERRIDE_DIR_.'classes/pdf/' => scandir(_PS_OVERRIDE_DIR_.'classes/pdf/')
  873. ),
  874. 'tpl' => array(
  875. _PS_PDF_DIR_ => scandir(_PS_PDF_DIR_),
  876. _PS_THEME_SELECTED_DIR_.'pdf/' => $tpl_theme
  877. )
  878. );
  879. break;
  880. case 'mails':
  881. $directories['php'] = array(
  882. _PS_FRONT_CONTROLLER_DIR_ => scandir(_PS_FRONT_CONTROLLER_DIR_),
  883. _PS_ADMIN_CONTROLLER_DIR_ => scandir(_PS_ADMIN_CONTROLLER_DIR_),
  884. _PS_OVERRIDE_DIR_.'controllers/front/' => scandir(_PS_OVERRIDE_DIR_.'controllers/front/'),
  885. _PS_OVERRIDE_DIR_.'controllers/admin/' => scandir(_PS_OVERRIDE_DIR_.'controllers/admin/'),
  886. _PS_ADMIN_DIR_.'/' => scandir(_PS_ADMIN_DIR_),
  887. _PS_ADMIN_DIR_.'/tabs/' => scandir(_PS_ADMIN_DIR_.'/tabs')
  888. );
  889. // Get all files for folders classes/ and override/classes/ recursively
  890. $directories['php'] = array_merge($directories['php'], $this->listFiles(_PS_CLASS_DIR_, array(), 'php'));
  891. $directories['php'] = array_merge($directories['php'], $this->listFiles(_PS_OVERRIDE_DIR_.'classes/', array(), 'php'));
  892. $directories['php'] = array_merge($directories['php'], $this->getModulesHasMails());
  893. break;
  894. }
  895. return $directories;
  896. }
  897. /**
  898. * This method parse a file by type of translation and type file
  899. *
  900. * @param $content
  901. * @param $type_translation : front, back, errors, modules...
  902. * @param string|bool $type_file : (tpl|php)
  903. * @return return $matches
  904. */
  905. protected function userParseFile($content, $type_translation, $type_file = false)
  906. {
  907. switch ($type_translation)
  908. {
  909. case 'front':
  910. // Parsing file in Front office
  911. $regex = '/\{l\s*s=\''._PS_TRANS_PATTERN_.'\'(\s*sprintf=.*)?(\s*js=1)?\s*\}/U';
  912. break;
  913. case 'back':
  914. // Parsing file in Back office
  915. if ($type_file == 'php')
  916. $regex = '/this->l\(\''._PS_TRANS_PATTERN_.'\'[\)|\,]/U';
  917. else if ($type_file == 'specific')
  918. $regex = '/translate\(\''._PS_TRANS_PATTERN_.'\'\)/U';
  919. else
  920. $regex = '/\{l\s*s\s*=\''._PS_TRANS_PATTERN_.'\'(\s*sprintf=.*)?(\s*js=1)?(\s*slashes=1)?\s*\}/U';
  921. break;
  922. case 'errors':
  923. // Parsing file for all errors syntax
  924. $regex = '/Tools::displayError\(\''._PS_TRANS_PATTERN_.'\'(,\s*(true|false))?\)/U';
  925. break;
  926. case 'modules':
  927. // Parsing modules file
  928. if ($type_file == 'php')
  929. $regex = '/->l\(\''._PS_TRANS_PATTERN_.'\'(, ?\'(.+)\')?(, ?(.+))?\)/U';
  930. else
  931. $regex = '/\{l\s*s=\''._PS_TRANS_PATTERN_.'\'(\s*sprintf=.*)?(\s*mod=\'.+\')?(\s*js=1)?\s*\}/U';
  932. break;
  933. case 'pdf':
  934. // Parsing PDF file
  935. if ($type_file == 'php')
  936. $regex = '/HTMLTemplate.*::l\(\''._PS_TRANS_PATTERN_.'\'[\)|\,]/U';
  937. else
  938. $regex = '/\{l\s*s=\''._PS_TRANS_PATTERN_.'\'(\s*sprintf=.*)?(\s*js=1)?(\s*pdf=\'true\')?\s*\}/U';
  939. break;
  940. }
  941. preg_match_all($regex, $content, $matches);
  942. return $matches[1];
  943. }
  944. /**
  945. * Get all translations informations for all type of translations
  946. *
  947. * array(
  948. * 'type' => array(
  949. * 'name' => string : title for the translation type,
  950. * 'var' => string : name of var for the translation file,
  951. * 'dir' => string : dir of translation file
  952. * 'file' => string : file name of translation file
  953. * )
  954. * )
  955. */
  956. public function getTranslationsInformations()
  957. {
  958. $this->translations_informations = array(
  959. 'front' => array(
  960. 'name' => $this->l('Front Office translations'),
  961. 'var' => '_LANG',
  962. 'dir' => _PS_THEME_SELECTED_DIR_.'lang/',
  963. 'file' => $this->lang_selected->iso_code.'.php'
  964. ),
  965. 'back' => array(
  966. 'name' => $this->l('Back Office translations'),
  967. 'var' => '_LANGADM',
  968. 'dir' => _PS_TRANSLATIONS_DIR_.$this->lang_selected->iso_code.'/',
  969. 'file' => 'admin.php'
  970. ),
  971. 'errors' => array(
  972. 'name' => $this->l('Error message translations'),
  973. 'var' => '_ERRORS',
  974. 'dir' => _PS_TRANSLATIONS_DIR_.$this->lang_selected->iso_code.'/',
  975. 'file' => 'errors.php'
  976. ),
  977. 'fields' => array(
  978. 'name' => $this->l('Field name translations'),
  979. 'var' => '_FIELDS',
  980. 'dir' => _PS_TRANSLATIONS_DIR_.$this->lang_selected->iso_code.'/',
  981. 'file' => 'fields.php'
  982. ),
  983. 'modules' => array(
  984. 'name' => $this->l('Installed module translations'),
  985. 'var' => '_MODULES',
  986. 'dir' => _PS_MODULE_DIR_,
  987. 'file' => '',
  988. 'override' => array(
  989. 'dir' => _PS_THEME_SELECTED_DIR_.'modules/',
  990. 'file' => ''
  991. )
  992. ),
  993. 'pdf' => array(
  994. 'name' => $this->l('PDF translations'),
  995. 'var' => '_LANGPDF',
  996. 'dir' => _PS_TRANSLATIONS_DIR_.$this->lang_selected->iso_code.'/',
  997. 'file' => 'pdf.php',
  998. 'override' => array(
  999. 'dir' => _PS_THEME_SELECTED_DIR_.'pdf/lang/',
  1000. 'file' => $this->lang_selected->iso_code.'.php'
  1001. )
  1002. ),
  1003. 'mails' => array(
  1004. 'name' => $this->l('E-mail template translations'),
  1005. 'var' => '_LANGMAIL',
  1006. 'dir' => _PS_MAIL_DIR_.$this->lang_selected->iso_code.'/',
  1007. 'file' => 'lang.php',
  1008. 'override' => array(
  1009. 'dir' => _PS_THEME_SELECTED_DIR_.'mails/'.$this->lang_selected->iso_code.'/',
  1010. 'file' => 'lang.php'
  1011. )
  1012. )
  1013. );
  1014. }
  1015. /**
  1016. * Get all informations on : languages, theme and the translation type.
  1017. */
  1018. public function getInformations()
  1019. {
  1020. // Get all Languages
  1021. $this->languages = Language::getLanguages(false);
  1022. // Get all iso_code of languages
  1023. foreach ($this->languages as $language)
  1024. $this->all_iso_lang[] = $language['iso_code'];
  1025. // Get all themes
  1026. $this->themes = Theme::getThemes();
  1027. // Get folder name of theme
  1028. if (($theme = Tools::getValue('theme')) && !is_array($theme))
  1029. {
  1030. $theme_exists = false;
  1031. foreach ($this->themes as $existing_theme)
  1032. if ($existing_theme->directory == $theme)
  1033. $theme_exists = true;
  1034. if ($theme_exists)
  1035. $this->theme_selected = Tools::safeOutput($theme);
  1036. else
  1037. throw new PrestaShopException(sprintf(Tools::displayError('Invalid theme "%s"'), $theme));
  1038. }
  1039. else
  1040. $this->theme_selected = self::DEFAULT_THEME_NAME;
  1041. // Set the path of selected theme
  1042. define('_PS_THEME_SELECTED_DIR_', _PS_ROOT_DIR_.'/themes/'.$this->theme_selected.'/');
  1043. // Get type of translation
  1044. if (($type = Tools::getValue('type')) && !is_array($type))
  1045. $this->type_selected = strtolower(Tools::safeOutput($type));
  1046. // Get selected language
  1047. if (Tools::getValue('lang') || Tools::getValue('iso_code'))
  1048. {
  1049. $iso_code = Tools::getValue('lang') ? Tools::getValue('lang') : Tools::getValue('iso_code');
  1050. if (!Validate::isLangIsoCode($iso_code) || !in_array($iso_code, $this->all_iso_lang))
  1051. throw new PrestaShopException(sprintf(Tools::displayError('Invalid iso code "%s"'), $iso_code));
  1052. $this->lang_selected = new Language((int)Language::getIdByIso($iso_code));
  1053. }
  1054. else
  1055. $this->lang_selected = new Language((int)Language::getIdByIso('en'));
  1056. // Get all information for translations
  1057. $this->getTranslationsInformations();
  1058. }
  1059. /**
  1060. * AdminController::postProcess() override
  1061. * @see AdminController::postProcess()
  1062. */
  1063. public function postProcess()
  1064. {
  1065. $this->getInformations();
  1066. /* PrestaShop demo mode */
  1067. if (_PS_MODE_DEMO_)
  1068. {
  1069. $this->errors[] = Tools::displayError('This functionality has been disabled.');
  1070. return;
  1071. }
  1072. /* PrestaShop demo mode */
  1073. try {
  1074. if (Tools::isSubmit('submitCopyLang'))
  1075. {
  1076. if ($this->tabAccess['add'] === '1')
  1077. $this->submitCopyLang();
  1078. else
  1079. $this->errors[] = Tools::displayError('You do not have permission to add here.');
  1080. }
  1081. else if (Tools::isSubmit('submitExport'))
  1082. {
  1083. if ($this->tabAccess['add'] === '1')
  1084. $this->submitExportLang();
  1085. else
  1086. $this->errors[] = Tools::displayError('You do not have permission to add here.');
  1087. }
  1088. else if (Tools::isSubmit('submitImport'))
  1089. {
  1090. if ($this->tabAccess['add'] === '1')
  1091. $this->submitImportLang();
  1092. else
  1093. $this->errors[] = Tools::displayError('You do not have permission to add here.');
  1094. }
  1095. else if (Tools::isSubmit('submitAddLanguage'))
  1096. {
  1097. if ($this->tabAccess['add'] === '1')
  1098. $this->submitAddLang();
  1099. else
  1100. $this->errors[] = Tools::displayError('You do not have permission to add here.');
  1101. }
  1102. else if (Tools::isSubmit('submitTranslationsFront'))
  1103. {
  1104. if ($this->tabAccess['edit'] === '1')
  1105. $this->writeTranslationFile();
  1106. else
  1107. $this->errors[] = Tools::displayError('You do not have permission to edit here.');
  1108. }
  1109. else if (Tools::isSubmit('submitTranslationsPdf'))
  1110. {
  1111. if ($this->tabAccess['edit'] === '1')
  1112. // Only the PrestaShop team should write the translations into the _PS_TRANSLATIONS_DIR_
  1113. if (($this->theme_selected == self::DEFAULT_THEME_NAME) && _PS_MODE_DEV_)
  1114. $this->writeTranslationFile();
  1115. else
  1116. $this->writeTranslationFile(true);
  1117. else
  1118. $this->errors[] = Tools::displayError('You do not have permission to edit here.');
  1119. }
  1120. else if (Tools::isSubmit('submitTranslationsBack'))
  1121. {
  1122. if ($this->tabAccess['edit'] === '1')
  1123. $this->writeTranslationFile();
  1124. else
  1125. $this->errors[] = Tools::displayError('You do not have permission to edit here.');
  1126. }
  1127. else if (Tools::isSubmit('submitTranslationsErrors'))
  1128. {
  1129. if ($this->tabAccess['edit'] === '1')
  1130. $this->writeTranslationFile();
  1131. else
  1132. $this->errors[] = Tools::displayError('You do not have permission to edit here.');
  1133. }
  1134. else if (Tools::isSubmit('submitTranslationsFields'))
  1135. {
  1136. if ($this->tabAccess['edit'] === '1')
  1137. $this->writeTranslationFile();
  1138. else
  1139. $this->errors[] = Tools::displayError('You do not have permission to edit here.');
  1140. }
  1141. else if (Tools::isSubmit('submitTranslationsMails') || Tools::isSubmit('submitTranslationsMailsAndStay'))
  1142. {
  1143. if ($this->tabAccess['edit'] === '1')
  1144. $this->submitTranslationsMails();
  1145. else
  1146. $this->errors[] = Tools::displayError('You do not have permission to edit here.');
  1147. }
  1148. else if (Tools::isSubmit('submitTranslationsModules'))
  1149. {
  1150. if ($this->tabAccess['edit'] === '1')
  1151. {
  1152. // Get a good path for module directory
  1153. if ($this->theme_selected == self::DEFAULT_THEME_NAME)
  1154. $i18n_dir = $this->translations_informations[$this->type_selected]['dir'];
  1155. else
  1156. $i18n_dir = $this->translations_informations[$this->type_selected]['override']['dir'];
  1157. // Get list of modules
  1158. if ($modules = $this->getListModules())
  1159. {
  1160. // Get files of all modules
  1161. $arr_files = $this->getAllModuleFiles($modules, $i18n_dir, $this->lang_selected->iso_code, true);
  1162. // Find and write all translation modules files
  1163. foreach ($arr_files as $value)
  1164. $this->findAndWriteTranslationsIntoFile($value['file_name'], $value['files'], $value['theme'], $value['module'], $value['dir']);
  1165. // Redirect
  1166. if (Tools::getValue('submitTranslationsModulesAndStay'))
  1167. $this->redirect(true);
  1168. else
  1169. $this->redirect();
  1170. }
  1171. }
  1172. else
  1173. $this->errors[] = Tools::displayError('You do not have permission to edit here.');
  1174. }
  1175. } catch (PrestaShopException $e) {
  1176. $this->errors[] = $e->getMessage();
  1177. }
  1178. }
  1179. /**
  1180. * This method redirect in the translation main page or in the translation page
  1181. *
  1182. * @param bool $save_and_stay : true if the user has clicked on the button "save and stay"
  1183. * @param bool $conf : id of confirmation message
  1184. */
  1185. protected function redirect($save_and_stay = false, $conf = false)
  1186. {
  1187. $conf = !$conf ? 4 : $conf;
  1188. $url_base = self::$currentIndex.'&token='.$this->token.'&conf='.$conf;
  1189. if ($save_and_stay)
  1190. Tools::redirectAdmin($url_base.'&lang='.$this->lang_selected->iso_code.'&type='.$this->type_selected.'&theme='.$this->theme_selected);
  1191. else
  1192. Tools::redirectAdmin($url_base);
  1193. }
  1194. protected function getMailPattern()
  1195. {
  1196. // Let the indentation like it.
  1197. return '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd">
  1198. <html>
  1199. <head>
  1200. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  1201. <title>#title</title>
  1202. </head>
  1203. <body>
  1204. #content
  1205. </body>
  1206. </html>';
  1207. }
  1208. /**
  1209. * This method is used to wright translation for mails.
  1210. * This wrights subject translation files
  1211. * (in root/mails/lang_choosen/lang.php or root/_PS_THEMES_DIR_/mails/lang_choosen/lang.php)
  1212. * and mails files.
  1213. */
  1214. protected function submitTranslationsMails()
  1215. {
  1216. $arr_mail_content = array();
  1217. $arr_mail_path = array();
  1218. if (Tools::getValue('core_mail'))
  1219. {
  1220. $arr_mail_content['core_mail'] = Tools::getValue('core_mail');
  1221. // Get path of directory for find a good path of translation file
  1222. if ($this->theme_selected != self::DEFAULT_THEME_NAME)
  1223. $arr_mail_path['core_mail'] = $this->translations_informations[$this->type_selected]['override']['dir'];
  1224. else
  1225. $arr_mail_path['core_mail'] = $this->translations_informations[$this->type_selected]['dir'];
  1226. }
  1227. if (Tools::getValue('module_mail'))
  1228. {
  1229. $arr_mail_content['module_mail'] = Tools::getValue('module_mail');
  1230. // Get path of directory for find a good path of translation file
  1231. if ($this->theme_selected != self::DEFAULT_THEME_NAME)
  1232. $arr_mail_path['module_mail'] = $this->translations_informations['modules']['override']['dir'].'{module}/mails/'.$this->lang_selected->iso_code.'/';
  1233. else
  1234. $arr_mail_path['module_mail'] = $this->translations_informations['modules']['dir'].'{module}/mails/'.$this->lang_selected->iso_code.'/';
  1235. }
  1236. // Save each mail content
  1237. foreach ($arr_mail_content as $group_name => $all_content)
  1238. {
  1239. foreach ($all_content as $type_content => $mails)
  1240. {
  1241. foreach ($mails as $mail_name => $content)
  1242. {
  1243. $module_name = false;
  1244. $module_name_pipe_pos = stripos($mail_name, '|');
  1245. if ($module_name_pipe_pos)
  1246. {
  1247. $module_name = substr($mail_name, 0, $module_name_pipe_pos);
  1248. if (!Validate::isModuleName($module_name))
  1249. throw new PrestaShopException(sprinf(Tools::displayError('Invalid module name "%s"'), $module_name));
  1250. $mail_name = substr($mail_name, $module_name_pipe_pos + 1);
  1251. if (!Validate::isTplName($mail_name))
  1252. throw new PrestaShopException(sprintf(Tools::displayError('Invalid mail name "%s"'), $mail_name));
  1253. }
  1254. if ($type_content == 'html')
  1255. {
  1256. $content = Tools::htmlentitiesUTF8($content);
  1257. $content = htmlspecialchars_decode($content);
  1258. // replace correct end of line
  1259. $content = str_replace("\r\n", PHP_EOL, $content);
  1260. $title = '';
  1261. if (Tools::getValue('title_'.$group_name.'_'.$mail_name))
  1262. $title = Tools::getValue('title_'.$group_name.'_'.$mail_name);
  1263. $string_mail = $this->getMailPattern();
  1264. $content = str_replace(array('#title', '#content'), array($title, $content), $string_mail);
  1265. // Magic Quotes shall... not.. PASS!
  1266. if (_PS_MAGIC_QUOTES_GPC_)
  1267. $content = stripslashes($content);
  1268. }
  1269. if (Validate::isCleanHTML($content))
  1270. {
  1271. $path = $arr_mail_path[$group_name];
  1272. if ($module_name)
  1273. $path = str_replace('{module}', $module_name, $path);
  1274. file_put_contents($path.$mail_name.'.'.$type_content, $content);
  1275. }
  1276. else
  1277. throw new PrestaShopException(Tools::displayError('HTML e-mail templates cannot contain JavaScript code.'));
  1278. }
  1279. }
  1280. }
  1281. // Update subjects
  1282. $array_subjects = array();
  1283. if (($subjects = Tools::getValue('subject')) && is_array($subjects))
  1284. {
  1285. $array_subjects['core_and_modules'] = array('translations'=>array(), 'path'=>$arr_mail_path['core_mail'].'lang.php');
  1286. foreach ($subjects as $subject_translation)
  1287. $array_subjects['core_and_modules']['translations'] = array_merge($array_subjects['core_and_modules']['translations'], $subject_translation);
  1288. }
  1289. if (!empty($array_subjects))
  1290. foreach ($array_subjects as $infos)
  1291. $this->writeSubjectTranslationFile($infos['translations'], $infos['path']);
  1292. if (Tools::isSubmit('submitTranslationsMailsAndStay'))
  1293. $this->redirect(true);
  1294. else
  1295. $this->redirect();
  1296. }
  1297. /**
  1298. * Include file $dir/$file and return the var $var declared in it.
  1299. * This create the file if not exists
  1300. *
  1301. * return array : translations
  1302. */
  1303. public function fileExists()
  1304. {
  1305. $var = $this->translations_informations[$this->type_selected]['var'];
  1306. $dir = $this->translations_informations[$this->type_selected]['dir'];
  1307. $file = $this->translations_informations[$this->type_selected]['file'];
  1308. $$var = array();
  1309. if (!Tools::file_exists_cache($dir))
  1310. if (!mkdir($dir, 0700))
  1311. throw new PrestaShopException('Directory '.$dir.' cannot be created.');
  1312. if (!Tools::file_exists_cache($dir.DIRECTORY_SEPARATOR.$file))
  1313. if (!file_put_contents($dir.'/'.$file, "<?php\n\nglobal \$".$var.";\n\$".$var." = array();\n\n?>"))
  1314. throw new PrestaShopException('File "'.$file.'" doesn\'t exists and cannot be created in '.$dir);
  1315. if (!is_writable($dir.DIRECTORY_SEPARATOR.$file))
  1316. $this->displayWarning(Tools::displayError('This file must be writable:').' '.$dir.'/'.$file);
  1317. include($dir.DIRECTORY_SEPARATOR.$file);
  1318. return $$var;
  1319. }
  1320. public function displayToggleButton($closed = false)
  1321. {
  1322. $str_output = '
  1323. <script type="text/javascript">';
  1324. if (Tools::getValue('type') == 'mails')
  1325. $str_output .= '$(document).ready(function(){
  1326. openCloseAllDiv(\''.$this->type_selected.'_div\', this.value == openAll); toggleElemValue(this.id, openAll, closeAll);
  1327. });';
  1328. $str_output .= '
  1329. var openAll = \''.html_entity_decode($this->l('Expand all fieldsets'), ENT_NOQUOTES, 'UTF-8').'\';
  1330. var closeAll = \''.html_entity_decode($this->l('Close all fieldsets'), ENT_NOQUOTES, 'UTF-8').'\';
  1331. </script>
  1332. <input type="button" class="button" id="buttonall" onclick="openCloseAllDiv(\''.$this->type_selected.'_div\', this.value == openAll); toggleElemValue(this.id, openAll, closeAll);" />
  1333. <script type="text/javascript">toggleElemValue(\'buttonall\', '.($closed ? 'openAll' : 'closeAll').', '.($closed ? 'closeAll' : 'openAll').');</script>';
  1334. return $str_output;
  1335. }
  1336. protected function displaySubmitButtons($name)
  1337. {
  1338. return '
  1339. <input type="submit" name="submitTranslations'.ucfirst($name).'" value="'.$this->l('Update translations').'" class="button" />
  1340. <input type="submit" name="submitTranslations'.ucfirst($name).'AndStay" value="'.$this->l('Update and stay').'" class="button" />';
  1341. }
  1342. /**
  1343. * Init js variables for translation with google
  1344. *
  1345. * @return array of variables to assign to the smarty template
  1346. */
  1347. public function initAutoTranslate()
  1348. {
  1349. $this->addJS('http://www.google.com/jsapi');
  1350. $this->addJS(_PS_JS_DIR_.'gg-translate.js');
  1351. $this->addJS(_PS_JS_DIR_.'admin-translations.js');
  1352. $language_code = Tools::htmlentitiesUTF8(Language::getLanguageCodeByIso(Tools::getValue('lang')));
  1353. return array('language_code' => $language_code,
  1354. 'not_available' => addslashes(html_entity_decode($this->l('this language is not available in Google Translate\'s API'), ENT_QUOTES, 'utf-8')),
  1355. 'tooltip_title' => addslashes(html_entity_decode($this->l('Google Translate suggests:'), ENT_QUOTES, 'utf-8'))
  1356. );
  1357. }
  1358. public function displayLimitPostWarning($count)
  1359. {
  1360. $return = array();
  1361. if ((ini_get('suhosin.post.max_vars') && ini_get('suhosin.post.max_vars') < $count) || (ini_get('suhosin.request.max_vars') && ini_get('suhosin.request.max_vars') < $count))
  1362. {
  1363. $return['error_type'] = 'suhosin';
  1364. $return['post.max_vars'] = ini_get('suhosin.post.max_vars');
  1365. $return['request.max_vars'] = ini_get('suhosin.request.max_vars');
  1366. $return['needed_limit'] = $count + 100;
  1367. }
  1368. elseif (ini_get('max_input_vars') && ini_get('max_input_vars') < $count)
  1369. {
  1370. $return['error_type'] = 'conf';
  1371. $return['max_input_vars'] = ini_get('max_input_vars');
  1372. $return['needed_limit'] = $count + 100;
  1373. }
  1374. return $return;
  1375. }
  1376. /**
  1377. * Find sentence which use %d, %s, %%, %1$d, %1$s...
  1378. *
  1379. * @param $key : english sentence
  1380. * @return array|bool return list of matches
  1381. */
  1382. public function checkIfKeyUseSprintf($key)
  1383. {
  1384. if (preg_match_all('#(?:%%|%(?:[0-9]+\$)?[+-]?(?:[ 0]|\'.)?-?[0-9]*(?:\.[0-9]+)?[bcdeufFosxX])#', $key, $matches))
  1385. return implode(', ', $matches[0]);
  1386. return false;
  1387. }
  1388. /**
  1389. * This method generate the form for front translations
  1390. */
  1391. public function initFormFront()
  1392. {
  1393. $missing_translations_front = array();
  1394. $name_var = $this->translations_informations[$this->type_selected]['var'];
  1395. $GLOBALS[$name_var] = $this->fileExists();
  1396. /* List templates to parse */
  1397. $files_by_directory = $this->getFileToParseByTypeTranslation();
  1398. $count = 0;
  1399. $tabs_array = array();
  1400. foreach ($files_by_directory['tpl'] as $dir => $files)
  1401. {
  1402. $prefix = '';
  1403. if ($dir == _PS_THEME_OVERRIDE_DIR_)
  1404. $prefix = 'override_';
  1405. foreach ($files as $file)
  1406. {
  1407. if (preg_match('/^(.*).tpl$/', $file) && (Tools::file_exists_cache($file_path = $dir.$file)))
  1408. {
  1409. $prefix_key = $prefix.substr(basename($file), 0, -4);
  1410. $new_lang = array();
  1411. // Get content for this file
  1412. $content = file_get_contents($file_path);
  1413. // Parse this content
  1414. $matches = $this->userParseFile($content, $this->type_selected);
  1415. /* Get string translation */
  1416. foreach ($matches as $key)
  1417. {
  1418. if (empty($key))
  1419. {
  1420. $this->errors[] = sprintf($this->l('Empty string found, please edit: "%s"'), $file_path);
  1421. $new_lang[$key] = '';
  1422. }
  1423. else
  1424. {
  1425. // Caution ! front has underscore between prefix key and md5, back has not
  1426. if (isset($GLOBALS[$name_var][$prefix_key.'_'.md5($key)]))
  1427. $new_lang[$key]['trad'] = stripslashes(html_entity_decode($GLOBALS[$name_var][$prefix_key.'_'.md5($key)], ENT_COMPAT, 'UTF-8'));
  1428. else
  1429. {
  1430. if (!isset($new_lang[$key]['trad']))
  1431. {
  1432. $new_lang[$key]['trad'] = '';
  1433. if (!isset($missing_translations_front[$prefix_key]))
  1434. $missing_translations_front[$prefix_key] = 1;
  1435. else
  1436. $missing_translations_front[$prefix_key]++;
  1437. }
  1438. }
  1439. $new_lang[$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  1440. }
  1441. }
  1442. if (isset($tabs_array[$prefix_key]))
  1443. $tabs_array[$prefix_key] = array_merge($tabs_array[$prefix_key], $new_lang);
  1444. else
  1445. $tabs_array[$prefix_key] = $new_lang;
  1446. $count += count($new_lang);
  1447. }
  1448. }
  1449. }
  1450. $this->tpl_view_vars = array_merge($this->tpl_view_vars, array(
  1451. 'missing_translations' => $missing_translations_front,
  1452. 'count' => $count,
  1453. 'limit_warning' => $this->displayLimitPostWarning($count),
  1454. 'tabsArray' => $tabs_array,
  1455. ));
  1456. // Add js variables needed for autotranslate
  1457. //$this->tpl_view_vars = array_merge($this->tpl_view_vars, $this->initAutoTranslate());
  1458. $this->initToolbar();
  1459. $this->base_tpl_view = 'translation_form.tpl';
  1460. return parent::renderView();
  1461. }
  1462. /**
  1463. * This method generate the form for back translations
  1464. */
  1465. public function initFormBack()
  1466. {
  1467. $name_var = $this->translations_informations[$this->type_selected]['var'];
  1468. $GLOBALS[$name_var] = $this->fileExists();
  1469. $missing_translations_back = array();
  1470. // Get all types of file (PHP, TPL...) and a list of files to parse by folder
  1471. $files_per_directory = $this->getFileToParseByTypeTranslation();
  1472. foreach ($files_per_directory['php'] as $dir => $files)
  1473. foreach ($files as $file)
  1474. // Check if is a PHP file and if the override file exists
  1475. if (preg_match('/^(.*)\.php$/', $file) && Tools::file_exists_cache($file_path = $dir.$file) && !in_array($file, self::$ignore_folder))
  1476. {
  1477. $prefix_key = basename($file);
  1478. // -4 becomes -14 to remove the ending "Controller.php" from the filename
  1479. if (strpos($file, 'Controller.php') !== false)
  1480. $prefix_key = basename(substr($file, 0, -14));
  1481. else if (strpos($file, 'Helper') !== false)
  1482. $prefix_key = 'Helper';
  1483. if ($prefix_key == 'Admin')
  1484. $prefix_key = 'AdminController';
  1485. if ($prefix_key == 'PaymentModule.php')
  1486. $prefix_key = 'PaymentModule';
  1487. // Get content for this file
  1488. $content = file_get_contents($file_path);
  1489. // Parse this content
  1490. $matches = $this->userParseFile($content, $this->type_selected, 'php');
  1491. foreach ($matches as $key)
  1492. {
  1493. // Caution ! front has underscore between prefix key and md5, back has not
  1494. if (isset($GLOBALS[$name_var][$prefix_key.md5($key)]))
  1495. $tabs_array[$prefix_key][$key]['trad'] = stripslashes(html_entity_decode($GLOBALS[$name_var][$prefix_key.md5($key)], ENT_COMPAT, 'UTF-8'));
  1496. else
  1497. {
  1498. if (!isset($tabs_array[$prefix_key][$key]['trad']))
  1499. {
  1500. $tabs_array[$prefix_key][$key]['trad'] = '';
  1501. if (!isset($missing_translations_back[$prefix_key]))
  1502. $missing_translations_back[$prefix_key] = 1;
  1503. else
  1504. $missing_translations_back[$prefix_key]++;
  1505. }
  1506. }
  1507. $tabs_array[$prefix_key][$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  1508. }
  1509. }
  1510. foreach ($files_per_directory['specific'] as $dir => $files)
  1511. foreach ($files as $file)
  1512. if (Tools::file_exists_cache($file_path = $dir.$file) && !in_array($file, self::$ignore_folder))
  1513. {
  1514. $prefix_key = 'index';
  1515. // Get content for this file
  1516. $content = file_get_contents($file_path);
  1517. // Parse this content
  1518. $matches = $this->userParseFile($content, $this->type_selected, 'specific');
  1519. foreach ($matches as $key)
  1520. {
  1521. // Caution ! front has underscore between prefix key and md5, back has not
  1522. if (isset($GLOBALS[$name_var][$prefix_key.md5($key)]))
  1523. $tabs_array[$prefix_key][$key]['trad'] = stripslashes(html_entity_decode($GLOBALS[$name_var][$prefix_key.md5($key)], ENT_COMPAT, 'UTF-8'));
  1524. else
  1525. {
  1526. if (!isset($tabs_array[$prefix_key][$key]['trad']))
  1527. {
  1528. $tabs_array[$prefix_key][$key]['trad'] = '';
  1529. if (!isset($missing_translations_back[$prefix_key]))
  1530. $missing_translations_back[$prefix_key] = 1;
  1531. else
  1532. $missing_translations_back[$prefix_key]++;
  1533. }
  1534. }
  1535. $tabs_array[$prefix_key][$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  1536. }
  1537. }
  1538. foreach ($files_per_directory['tpl'] as $dir => $files)
  1539. foreach ($files as $file)
  1540. if (preg_match('/^(.*).tpl$/', $file) && Tools::file_exists_cache($file_path = $dir.$file))
  1541. {
  1542. // get controller name instead of file name
  1543. $prefix_key = Tools::toCamelCase(str_replace(_PS_ADMIN_DIR_.'/themes', '', $file_path), true);
  1544. $pos = strrpos($prefix_key, DIRECTORY_SEPARATOR);
  1545. $tmp = substr($prefix_key, 0, $pos);
  1546. if (preg_match('#controllers#', $tmp))
  1547. {
  1548. $parent_class = explode(DIRECTORY_SEPARATOR, $tmp);
  1549. $key = array_search('controllers', $parent_class);
  1550. $prefix_key = 'Admin'.ucfirst($parent_class[$key + 1]);
  1551. }
  1552. else
  1553. $prefix_key = 'Admin'.ucfirst(substr($tmp, strrpos($tmp, DIRECTORY_SEPARATOR) + 1, $pos));
  1554. // Adding list, form, option in Helper Translations
  1555. $list_prefix_key = array('AdminHelpers', 'AdminList', 'AdminView', 'AdminOptions', 'AdminForm', 'AdminHelpAccess');
  1556. if (in_array($prefix_key, $list_prefix_key))
  1557. $prefix_key = 'Helper';
  1558. // Adding the folder backup/download/ in AdminBackup Translations
  1559. if ($prefix_key == 'AdminDownload')
  1560. $prefix_key = 'AdminBackup';
  1561. // use the prefix "AdminController" (like old php files 'header', 'footer.inc', 'index', 'login', 'password', 'functions'
  1562. if ($prefix_key == 'Admin' || $prefix_key == 'AdminTemplate')
  1563. $prefix_key = 'AdminController';
  1564. $new_lang = array();
  1565. // Get content for this file
  1566. $content = file_get_contents($file_path);
  1567. // Parse this content
  1568. $matches = $this->userParseFile($content, $this->type_selected, 'tpl');
  1569. /* Get string translation for each tpl file */
  1570. foreach ($matches as $english_string)
  1571. {
  1572. if (empty($english_string))
  1573. {
  1574. $this->errors[] = sprintf($this->l('Error in template - Empty string found, please edit: "%s"'), $file_path);
  1575. $new_lang[$english_string] = '';
  1576. }
  1577. else
  1578. {
  1579. $trans_key = $prefix_key.md5($english_string);
  1580. if (isset($GLOBALS[$name_var][$trans_key]))
  1581. $new_lang[$english_string]['trad'] = html_entity_decode($GLOBALS[$name_var][$trans_key], ENT_COMPAT, 'UTF-8');
  1582. else
  1583. {
  1584. if (!isset($new_lang[$english_string]['trad']))
  1585. {
  1586. $new_lang[$english_string]['trad'] = '';
  1587. if (!isset($missing_translations_back[$prefix_key]))
  1588. $missing_translations_back[$prefix_key] = 1;
  1589. else
  1590. $missing_translations_back[$prefix_key]++;
  1591. }
  1592. }
  1593. $new_lang[$english_string]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  1594. }
  1595. }
  1596. if (isset($tabs_array[$prefix_key]))
  1597. $tabs_array[$prefix_key] = array_merge($tabs_array[$prefix_key], $new_lang);
  1598. else
  1599. $tabs_array[$prefix_key] = $new_lang;
  1600. }
  1601. // count will contain the number of expressions of the page
  1602. $count = 0;
  1603. foreach ($tabs_array as $array)
  1604. $count += count($array);
  1605. $this->tpl_view_vars = array_merge($this->tpl_view_vars, array(
  1606. 'count' => $count,
  1607. 'limit_warning' => $this->displayLimitPostWarning($count),
  1608. 'tabsArray' => $tabs_array,
  1609. 'missing_translations' => $missing_translations_back
  1610. ));
  1611. // Add js variables needed for autotranslate
  1612. //$this->tpl_view_vars = array_merge($this->tpl_view_vars, $this->initAutoTranslate());
  1613. $this->initToolbar();
  1614. $this->base_tpl_view = 'translation_form.tpl';
  1615. return parent::renderView();
  1616. }
  1617. /**
  1618. * Check if directory and file exist and return an list of modules
  1619. *
  1620. * @return array : list of modules
  1621. */
  1622. public function getListModules()
  1623. {
  1624. if (!Tools::file_exists_cache($this->translations_informations['modules']['dir']))
  1625. throw new PrestaShopException(Tools::displayError('Fatal error: Module directory does not exist').'('.$this->translations_informations['modules']['dir'].')');
  1626. if (!is_writable($this->translations_informations['modules']['dir']))
  1627. throw new PrestaShopException(Tools::displayError('The module directory must be writable'));
  1628. $modules = array();
  1629. if (!_PS_MODE_DEV_ && $this->theme_selected == self::DEFAULT_THEME_NAME)
  1630. {
  1631. // Get all module which are installed for to have a minimum of POST
  1632. $modules = Module::getModulesInstalled();
  1633. foreach ($modules as &$module)
  1634. $module = $module['name'];
  1635. }
  1636. else if ($this->theme_selected == self::DEFAULT_THEME_NAME)
  1637. if (Tools::file_exists_cache($this->translations_informations['modules']['dir']))
  1638. $modules = scandir($this->translations_informations['modules']['dir']);
  1639. else
  1640. $this->displayWarning(Tools::displayError('There are no modules in your copy of PrestaShop. Use the Modules page to activate them or go to our Website to download additional Modules.'));
  1641. else
  1642. if (Tools::file_exists_cache($this->translations_informations['modules']['override']['dir']))
  1643. $modules = scandir($this->translations_informations['modules']['override']['dir']);
  1644. else
  1645. $this->displayWarning(Tools::displayError('There are no modules in your copy of PrestaShop. Use the Modules page to activate them or go to our Website to download additional Modules.'));
  1646. return $modules;
  1647. }
  1648. /**
  1649. * This method generate the form for errors translations
  1650. */
  1651. public function initFormErrors()
  1652. {
  1653. $name_var = $this->translations_informations[$this->type_selected]['var'];
  1654. $GLOBALS[$name_var] = $this->fileExists();
  1655. $count_empty = array();
  1656. /* List files to parse */
  1657. $string_to_translate = array();
  1658. $file_by_directory = $this->getFileToParseByTypeTranslation();
  1659. if ($modules = $this->getListModules())
  1660. {
  1661. foreach ($modules as $module)
  1662. if (is_dir(_PS_MODULE_DIR_.$module) && !in_array($module, self::$ignore_folder))
  1663. $file_by_directory['php'] = array_merge($file_by_directory['php'], $this->listFiles(_PS_MODULE_DIR_.$module.'/', array(), 'php'));
  1664. }
  1665. foreach ($file_by_directory['php'] as $dir => $files)
  1666. foreach ($files as $file)
  1667. if (preg_match('/\.php$/', $file) && Tools::file_exists_cache($file_path = $dir.$file) && !in_array($file, self::$ignore_folder))
  1668. {
  1669. if (!filesize($file_path))
  1670. continue;
  1671. // Get content for this file
  1672. $content = file_get_contents($file_path);
  1673. // Parse this content
  1674. $matches = $this->userParseFile($content, $this->type_selected);
  1675. foreach ($matches as $key)
  1676. {
  1677. if (array_key_exists(md5($key), $GLOBALS[$name_var]))
  1678. $string_to_translate[$key]['trad'] = html_entity_decode($GLOBALS[$name_var][md5($key)], ENT_COMPAT, 'UTF-8');
  1679. else
  1680. {
  1681. $string_to_translate[$key]['trad'] = '';
  1682. if (!isset($count_empty[$key]))
  1683. $count_empty[$key] = 1;
  1684. }
  1685. $string_to_translate[$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  1686. }
  1687. }
  1688. $this->tpl_view_vars = array_merge($this->tpl_view_vars, array(
  1689. 'count' => count($string_to_translate),
  1690. 'limit_warning' => $this->displayLimitPostWarning(count($string_to_translate)),
  1691. 'errorsArray' => $string_to_translate,
  1692. 'missing_translations' => $count_empty
  1693. ));
  1694. $this->initToolbar();
  1695. $this->base_tpl_view = 'translation_errors.tpl';
  1696. return parent::renderView();
  1697. }
  1698. /**
  1699. * This method generate the form for fields translations
  1700. */
  1701. public function initFormFields()
  1702. {
  1703. $name_var = $this->translations_informations[$this->type_selected]['var'];
  1704. $GLOBALS[$name_var] = $this->fileExists();
  1705. $missing_translations_fields = array();
  1706. $class_array = array();
  1707. $tabs_array = array();
  1708. $count = 0;
  1709. $files_by_directory = $this->getFileToParseByTypeTranslation();
  1710. foreach ($files_by_directory['php'] as $dir => $files)
  1711. foreach ($files as $file)
  1712. {
  1713. $exclude_files = array('index.php', 'Autoload.php', 'StockManagerInterface.php',
  1714. 'TaxManagerInterface.php', 'WebserviceOutputInterface.php', 'WebserviceSpecificManagementInterface.php');
  1715. if (!preg_match('/\.php$/', $file) || in_array($file, $exclude_files))
  1716. continue;
  1717. $class_name = substr($file, 0, -4);
  1718. if (!class_exists($class_name, false))
  1719. Autoload::getInstance()->load($class_name);
  1720. if (!is_subclass_of($class_name.'Core', 'ObjectModel'))
  1721. continue;
  1722. $class_array[$class_name] = call_user_func(array($class_name, 'getValidationRules'), $class_name);
  1723. }
  1724. foreach ($class_array as $prefix_key => $rules)
  1725. {
  1726. if (isset($rules['validate']))
  1727. foreach ($rules['validate'] as $key => $value)
  1728. {
  1729. if (isset($GLOBALS[$name_var][$prefix_key.'_'.md5($key)]))
  1730. {
  1731. $tabs_array[$prefix_key][$key]['trad'] = html_entity_decode($GLOBALS[$name_var][$prefix_key.'_'.md5($key)], ENT_COMPAT, 'UTF-8');
  1732. $count++;
  1733. }
  1734. else
  1735. {
  1736. if (!isset($tabs_array[$prefix_key][$key]['trad']))
  1737. {
  1738. $tabs_array[$prefix_key][$key]['trad'] = '';
  1739. if (!isset($missing_translations_fields[$prefix_key]))
  1740. $missing_translations_fields[$prefix_key] = 1;
  1741. else
  1742. $missing_translations_fields[$prefix_key]++;
  1743. $count++;
  1744. }
  1745. }
  1746. }
  1747. if (isset($rules['validateLang']))
  1748. foreach ($rules['validateLang'] as $key => $value)
  1749. {
  1750. if (isset($GLOBALS[$name_var][$prefix_key.'_'.md5($key)]))
  1751. {
  1752. $tabs_array[$prefix_key][$key]['trad'] = '';
  1753. if (array_key_exists($prefix_key.'_'.md5(addslashes($key)), $GLOBALS[$name_var]))
  1754. $tabs_array[$prefix_key][$key]['trad'] = html_entity_decode($GLOBALS[$name_var][$prefix_key.'_'.md5(addslashes($key))], ENT_COMPAT, 'UTF-8');
  1755. $count++;
  1756. }
  1757. else
  1758. {
  1759. if (!isset($tabs_array[$prefix_key][$key]['trad']))
  1760. {
  1761. $tabs_array[$prefix_key][$key]['trad'] = '';
  1762. if (!isset($missing_translations_fields[$prefix_key]))
  1763. $missing_translations_fields[$prefix_key] = 1;
  1764. else
  1765. $missing_translations_fields[$prefix_key]++;
  1766. $count++;
  1767. }
  1768. }
  1769. }
  1770. }
  1771. $this->tpl_view_vars = array_merge($this->tpl_view_vars, array(
  1772. 'count' => $count,
  1773. 'limit_warning' => $this->displayLimitPostWarning($count),
  1774. 'tabsArray' => $tabs_array,
  1775. 'missing_translations' => $missing_translations_fields
  1776. ));
  1777. $this->initToolbar();
  1778. $this->base_tpl_view = 'translation_form.tpl';
  1779. return parent::renderView();
  1780. }
  1781. /**
  1782. * Get each informations for each mails founded in the folder $dir.
  1783. *
  1784. * @since 1.4.0.14
  1785. * @param string $dir
  1786. * @param string $group_name
  1787. * @return array : list of mails
  1788. */
  1789. public function getMailFiles($dir, $group_name = 'mail')
  1790. {
  1791. $arr_return = array();
  1792. // Very usefull to name input and textarea fields
  1793. $arr_return['group_name'] = $group_name;
  1794. $arr_return['empty_values'] = 0;
  1795. $arr_return['total_filled'] = 0;
  1796. $arr_return['directory'] = $dir;
  1797. // Get path for english mail directory
  1798. $dir_en = str_replace('/'.$this->lang_selected->iso_code.'/', '/en/', $dir);
  1799. if (Tools::file_exists_cache($dir_en))
  1800. {
  1801. // Get all english files to compare with the language to translate
  1802. foreach (scandir($dir_en) as $email_file)
  1803. {
  1804. if (strripos($email_file, '.html') > 0 || strripos($email_file, '.txt') > 0)
  1805. {
  1806. $email_name = substr($email_file, 0, strripos($email_file, '.'));
  1807. $type = substr($email_file, strripos($email_file, '.') + 1);
  1808. if (!isset($arr_return['files'][$email_name]))
  1809. $arr_return['files'][$email_name] = array();
  1810. // $email_file is from scandir ($dir), so we already know that file exists
  1811. $arr_return['files'][$email_name][$type]['en'] = $this->getMailContent($dir_en, $email_file);
  1812. // check if the file exists in the language to translate
  1813. if (Tools::file_exists_cache($dir.'/'.$email_file))
  1814. {
  1815. $arr_return['files'][$email_name][$type][$this->lang_selected->iso_code] = $this->getMailContent($dir, $email_file);
  1816. $this->total_expression++;
  1817. }
  1818. else
  1819. $arr_return['files'][$email_name][$type][$this->lang_selected->iso_code] = '';
  1820. if ($arr_return['files'][$email_name][$type][$this->lang_selected->iso_code] == '')
  1821. $arr_return['empty_values']++;
  1822. else
  1823. $arr_return['total_filled']++;
  1824. }
  1825. }
  1826. }
  1827. else
  1828. $this->warnings[] = sprintf(Tools::displayError('mail directory exists for %1$s but not for english in %2$s'),
  1829. $this->lang_selected->iso_code, str_replace(_PS_ROOT_DIR_, '', $dir));
  1830. return $arr_return;
  1831. }
  1832. /**
  1833. * Get content of the mail file.
  1834. *
  1835. * @since 1.4.0.14
  1836. * @param string $dir
  1837. * @param string $file
  1838. * @return array : content of file
  1839. */
  1840. protected function getMailContent($dir, $file)
  1841. {
  1842. $content = file_get_contents($dir.'/'.$file);
  1843. if (Tools::strlen($content) === 0)
  1844. $content = '';
  1845. return $content;
  1846. }
  1847. /**
  1848. * Display mails in html format.
  1849. * This was create for factorize the html displaying
  1850. *
  1851. * @since 1.4.0.14
  1852. * @param array $mails
  1853. * @param array $all_subject_mail
  1854. * @param Language $obj_lang
  1855. * @param string $id_html use for set html id attribute for the block
  1856. * @param string $title Set the title for the block
  1857. * @param string|boolean $name_for_module is not false define add a name for disntiguish mails module
  1858. */
  1859. protected function displayMailContent($mails, $all_subject_mail, $obj_lang, $id_html, $title, $name_for_module = false)
  1860. {
  1861. $str_return = '';
  1862. $group_name = 'mail';
  1863. if (array_key_exists('group_name', $mails))
  1864. $group_name = $mails['group_name'];
  1865. $str_return .= '
  1866. <div class="mails_field" >
  1867. <h3 style="cursor : pointer" onclick="$(\'#'.$id_html.'\').slideToggle();">'.$title.' - <font color="red">'.$mails['empty_values'].'</font> '
  1868. .sprintf($this->l('missing translation(s) on %1$s template(s) for %2$s'),
  1869. '<font color="blue">'.((int)$mails['empty_values'] + (int)$mails['total_filled']).'</font>',
  1870. $obj_lang->name)
  1871. .':</h3>
  1872. <div name="mails_div" id="'.$id_html.'">';
  1873. if (!empty($mails['files']))
  1874. {
  1875. foreach ($mails['files'] as $mail_name => $mail_files)
  1876. {
  1877. if (array_key_exists('html', $mail_files) || array_key_exists('txt', $mail_files))
  1878. {
  1879. if (array_key_exists($mail_name, $all_subject_mail))
  1880. {
  1881. $subject_mail = $all_subject_mail[$mail_name];
  1882. $value_subject_mail = isset($mails['subject'][$subject_mail]) ? $mails['subject'][$subject_mail] : '';
  1883. $str_return .= '
  1884. <div class="label-subject" style="text-align:center;">
  1885. <label style="text-align:right">'.sprintf($this->l('Subject for %s:'), '<em>'.$mail_name.'</em>').'</label>
  1886. <div class="mail-form" style="text-align:left">
  1887. <b>'.$subject_mail.'</b><br />';
  1888. if (isset($value_subject_mail['trad']) && $value_subject_mail['trad'])
  1889. $str_return .= '<input type="text" name="subject['.$group_name.']['.$subject_mail.']" value="'.$value_subject_mail['trad'].'" />';
  1890. else
  1891. $str_return .= '<input type="text" name="subject['.$group_name.']['.$subject_mail.']" value="" />';
  1892. if (isset($value_subject_mail['use_sprintf']) && $value_subject_mail['use_sprintf'])
  1893. {
  1894. $str_return .= '<a class="useSpecialSyntax" title="'.$this->l('This expression uses a special syntax:').' '.$value_subject_mail['use_sprintf'].'" style="cursor:pointer">
  1895. <img src="'._PS_IMG_.'admin/error.png" alt="'.$value_subject_mail['use_sprintf'].'" />
  1896. </a>';
  1897. }
  1898. $str_return .= '</div>
  1899. </div>';
  1900. }
  1901. else
  1902. {
  1903. $str_return .= '
  1904. <div class="label-subject">
  1905. <b>'.sprintf($this->l('No Subject was found for %s, or subject is generated in database.'), '<em>'.$mail_name.'</em>').'</b>
  1906. </div>';
  1907. }
  1908. if (array_key_exists('html', $mail_files))
  1909. {
  1910. $base_uri = str_replace(_PS_ROOT_DIR_, __PS_BASE_URI__, $mails['directory']);
  1911. $base_uri = str_replace('//', '/', $base_uri);
  1912. $url_mail = $base_uri.$mail_name.'.html';
  1913. $str_return .= $this->displayMailBlockHtml($mail_files['html'], $obj_lang->iso_code, $url_mail, $mail_name, $group_name, $name_for_module);
  1914. }
  1915. if (array_key_exists('txt', $mail_files))
  1916. $str_return .= $this->displayMailBlockTxt($mail_files['txt'], $obj_lang->iso_code, $mail_name, $group_name, $name_for_module);
  1917. }
  1918. }
  1919. }
  1920. else
  1921. {
  1922. $str_return .= '
  1923. <p class="error">'.$this->l('There is a problem getting the Mail files.').'<br />'
  1924. .sprintf($this->l('Please ensure that English files exist in %s folder'), '<em>'.$mails['directory'].'en</em>')
  1925. .'</p>';
  1926. }
  1927. $str_return .= '
  1928. </div><!-- #'.$id_html.' -->
  1929. <div class="clear"></div>
  1930. </div>';
  1931. return $str_return;
  1932. }
  1933. /**
  1934. * Just build the html structure for display txt mails
  1935. *
  1936. * @since 1.4.0.14
  1937. * @param array $content with english and language needed contents
  1938. * @param string $lang iso code of the needed language
  1939. * @param string $mail_name name of the file to translate (same for txt and html files)
  1940. * @param string $group_name group name allow to distinguish each block of mail.
  1941. * @param string|boolean $name_for_module is not false define add a name for disntiguish mails module
  1942. */
  1943. protected function displayMailBlockTxt($content, $lang, $mail_name, $group_name, $name_for_module = false)
  1944. {
  1945. return '
  1946. <div class="block-mail" >
  1947. <label>'.$mail_name.'.txt</label>
  1948. <div class="mail-form">
  1949. <div><textarea class="rte mailrte noEditor" cols="80" rows="30" name="'.$group_name.'[txt]['.($name_for_module ? $name_for_module.'|' : '' ).$mail_name.']" style="width:560px;margin=0;">'.Tools::htmlentitiesUTF8(stripslashes(strip_tags($content[$lang]))).'</textarea></div>
  1950. </div><!-- .mail-form -->
  1951. </div><!-- .block-mail -->';
  1952. }
  1953. /**
  1954. * Just build the html structure for display html mails.
  1955. *
  1956. * @since 1.4.0.14
  1957. * @param array $content with english and language needed contents
  1958. * @param string $lang iso code of the needed language
  1959. * @param string $url for the html page and displaying an outline
  1960. * @param string $mail_name name of the file to translate (same for txt and html files)
  1961. * @param string $group_name group name allow to distinguish each block of mail.
  1962. * @param string|boolean $name_for_module is not false define add a name for disntiguish mails module
  1963. */
  1964. protected function displayMailBlockHtml($content, $lang, $url, $mail_name, $group_name, $name_for_module = false)
  1965. {
  1966. $title = array();
  1967. // Because TinyMCE don't work correctly with <DOCTYPE>, <html> and <body> tags
  1968. if (stripos($content[$lang], '<body'))
  1969. {
  1970. $array_lang = $lang != 'en' ? array('en', $lang) : array($lang);
  1971. foreach ($array_lang as $language)
  1972. {
  1973. $title[$language] = substr($content[$language], 0, stripos($content[$language], '<body'));
  1974. preg_match('#<title>([^<]+)</title>#Ui', $title[$language], $matches);
  1975. $title[$language] = empty($matches[1])?'':$matches[1];
  1976. // The 2 lines below allow to exlude <body> tag from the content.
  1977. // This allow to exclude body tag even if attributs are setted.
  1978. $content[$language] = substr($content[$language], stripos($content[$language], '<body') + 5);
  1979. $content[$language] = substr($content[$language], stripos($content[$language], '>') + 1);
  1980. $content[$language] = substr($content[$language], 0, stripos($content[$language], '</body>'));
  1981. }
  1982. }
  1983. $str_return = '';
  1984. $name_for_module = $name_for_module ? $name_for_module.'|' : '';
  1985. $content[$lang] = (isset($content[$lang]) ? Tools::htmlentitiesUTF8(stripslashes($content[$lang])) : '');
  1986. $str_return .= '
  1987. <div class="block-mail" >
  1988. <label>'.$mail_name.'.html</label>
  1989. <div class="mail-form">
  1990. <div>';
  1991. $str_return .= '
  1992. <div class="label-subject">
  1993. <b>'.$this->l('"title" tag:').'</b>&nbsp;'.(isset($title['en']) ? $title['en'] : '').'<br />
  1994. <input type="text" name="title_'.$group_name.'_'.$mail_name.'" value="'.(isset($title[$lang]) ? $title[$lang] : '').'" />
  1995. </div><!-- .label-subject -->';
  1996. $str_return .= '
  1997. <iframe style="background:white;border:1px solid #DFD5C3;" border="0" src ="'.$url.'?'.(rand(0, 1000000000)).'" width="565" height="497"></iframe>
  1998. <a style="display:block;margin-top:5px;width:130px;" href="#" onclick="$(this).parent().hide(); displayTiny($(this).parent().next()); return false;" class="button">'.$this->l('Edit this e-mail template').'</a>
  1999. </div>
  2000. <textarea style="display:none;" class="rte mailrte" cols="80" rows="30" name="'.$group_name.'[html]['.$name_for_module.$mail_name.']">'.$content[$lang].'</textarea>
  2001. </div><!-- .mail-form -->
  2002. </div><!-- .block-mail -->';
  2003. return $str_return;
  2004. }
  2005. /**
  2006. * Check in each module if contains mails folder.
  2007. *
  2008. * @return array of module which has mails
  2009. */
  2010. public function getModulesHasMails($with_module_name = false)
  2011. {
  2012. if ($this->theme_selected != self::DEFAULT_THEME_NAME)
  2013. $i18n_dir = $this->translations_informations['modules']['override']['dir'];
  2014. else
  2015. $i18n_dir = $this->translations_informations['modules']['dir'];
  2016. $arr_modules = array();
  2017. foreach (scandir($i18n_dir) as $module_dir)
  2018. {
  2019. $dir = $i18n_dir.$module_dir.'/';
  2020. if (!in_array($module_dir, self::$ignore_folder) && Tools::file_exists_cache($dir.'mails/'))
  2021. if ($with_module_name)
  2022. $arr_modules[$module_dir] = $dir;
  2023. else
  2024. $arr_modules[$dir] = scandir($dir);
  2025. }
  2026. return $arr_modules;
  2027. }
  2028. protected function getTinyMCEForMails($iso_lang)
  2029. {
  2030. // TinyMCE
  2031. $iso_tiny_mce = (Tools::file_exists_cache(_PS_ROOT_DIR_.'/js/tiny_mce/langs/'.$iso_lang.'.js') ? $iso_lang : 'en');
  2032. $ad = dirname($_SERVER['PHP_SELF']);
  2033. return '
  2034. <script type="text/javascript">
  2035. var iso = \''.$iso_tiny_mce.'\' ;
  2036. var pathCSS = \''._THEME_CSS_DIR_.'\' ;
  2037. var ad = \''.$ad.'\' ;
  2038. </script>
  2039. <script type="text/javascript" src="'.__PS_BASE_URI__.'js/tiny_mce/tiny_mce.js"></script>
  2040. <script type="text/javascript" src="'.__PS_BASE_URI__.'js/tinymce.inc.js"></script>
  2041. <script type="text/javascript">
  2042. $(document).ready(function () {
  2043. tinySetup();
  2044. });
  2045. function displayTiny(obj) {
  2046. tinyMCE.get(obj.attr(\'name\')).show();
  2047. }
  2048. </script>';
  2049. }
  2050. /**
  2051. * This method generate the form for mails translations
  2052. */
  2053. public function initFormMails($no_display = false)
  2054. {
  2055. $module_mails = array();
  2056. // get all mail subjects, this method parse each files in Prestashop !!
  2057. $subject_mail = array();
  2058. $modules_has_mails = $this->getModulesHasMails(true);
  2059. $files_by_directiories = $this->getFileToParseByTypeTranslation();
  2060. foreach ($files_by_directiories['php'] as $dir => $files)
  2061. foreach ($files as $file)
  2062. if (Tools::file_exists_cache($dir.$file) && is_file($dir.$file) && !in_array($file, self::$ignore_folder) && preg_match('/\.php$/', $file))
  2063. $subject_mail = $this->getSubjectMail($dir, $file, $subject_mail);
  2064. // Get path of directory for find a good path of translation file
  2065. if ($this->theme_selected != self::DEFAULT_THEME_NAME && @filemtime($this->translations_informations[$this->type_selected]['override']['dir']))
  2066. $i18n_dir = $this->translations_informations[$this->type_selected]['override']['dir'];
  2067. else
  2068. $i18n_dir = $this->translations_informations[$this->type_selected]['dir'];
  2069. $core_mails = $this->getMailFiles($i18n_dir, 'core_mail');
  2070. $core_mails['subject'] = $this->getSubjectMailContent($i18n_dir);
  2071. foreach ($modules_has_mails as $module_name => $module_path)
  2072. {
  2073. $module_mails[$module_name] = $this->getMailFiles($module_path.'mails/'.$this->lang_selected->iso_code.'/', 'module_mail');
  2074. $module_mails[$module_name]['subject'] = $core_mails['subject'];
  2075. $module_mails[$module_name]['display'] = $this->displayMailContent($module_mails[$module_name], $subject_mail, $this->lang_selected, Tools::strtolower($module_name), sprintf($this->l('E-mails for %s module'), '<em>'.$module_name.'</em>'), $module_name);
  2076. }
  2077. if ($no_display)
  2078. {
  2079. $empty = 0;
  2080. $total = 0;
  2081. $total += (int)$core_mails['total_filled'];
  2082. $empty += (int)$core_mails['empty_values'];
  2083. foreach ($module_mails as $mod_infos)
  2084. {
  2085. $total += (int)$mod_infos['total_filled'];
  2086. $empty += (int)$mod_infos['empty_values'];
  2087. }
  2088. return array('total' => $total, 'empty' => $empty);
  2089. }
  2090. $this->tpl_view_vars = array_merge($this->tpl_view_vars, array(
  2091. 'limit_warning' => $this->displayLimitPostWarning($this->total_expression),
  2092. 'tinyMCE' => $this->getTinyMCEForMails($this->lang_selected->iso_code),
  2093. 'mail_content' => $this->displayMailContent($core_mails, $subject_mail, $this->lang_selected, 'core', $this->l('Core e-mails')),
  2094. 'module_mails' => $module_mails,
  2095. 'theme_name' => $this->theme_selected
  2096. ));
  2097. $this->initToolbar();
  2098. $this->base_tpl_view = 'translation_mails.tpl';
  2099. return parent::renderView();
  2100. }
  2101. /**
  2102. * Get list of subjects of mails
  2103. *
  2104. * @param $dir
  2105. * @param $file
  2106. * @param $subject_mail
  2107. * @return array : list of subjects of mails
  2108. */
  2109. protected function getSubjectMail($dir, $file, $subject_mail)
  2110. {
  2111. $content = file_get_contents($dir.'/'.$file);
  2112. $content = str_replace("\n", ' ', $content);
  2113. if (preg_match_all('/Mail::Send([^;]*);/si', $content, $tab))
  2114. for ($i = 0; isset($tab[1][$i]); $i++)
  2115. {
  2116. $tab2 = explode(',', $tab[1][$i]);
  2117. if (is_array($tab2))
  2118. if ($tab2 && isset($tab2[1]))
  2119. {
  2120. $tab2[1] = trim(str_replace('\'', '', $tab2[1]));
  2121. if (preg_match('/Mail::l\(\''._PS_TRANS_PATTERN_.'\'/s', $tab2[2], $matches))
  2122. $subject_mail[$tab2[1]] = $matches[1];
  2123. }
  2124. }
  2125. if (!in_array($file, self::$ignore_folder) && is_dir($dir.'/'.$file))
  2126. $subject_mail = $this->getSubjectMail($dir, $file, $subject_mail);
  2127. return $subject_mail;
  2128. }
  2129. /**
  2130. * @param $directory : name of directory
  2131. * @return array
  2132. */
  2133. protected function getSubjectMailContent($directory)
  2134. {
  2135. $subject_mail_content = array();
  2136. if (Tools::file_exists_cache($directory.'/lang.php'))
  2137. {
  2138. // we need to include this even if already included (no include once)
  2139. include($directory.'/lang.php');
  2140. foreach ($GLOBALS[$this->translations_informations[$this->type_selected]['var']] as $key => $subject)
  2141. {
  2142. $this->total_expression++;
  2143. $subject = str_replace('\n', ' ', $subject);
  2144. $subject = str_replace("\\'", "\'", $subject);
  2145. $subject_mail_content[$key]['trad'] = htmlentities($subject, ENT_QUOTES, 'UTF-8');
  2146. $subject_mail_content[$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  2147. }
  2148. }
  2149. else
  2150. $this->errors[] = sprintf($this->l('Subject mail translation file not found in "%s"'), $directory);
  2151. return $subject_mail_content;
  2152. }
  2153. protected function writeSubjectTranslationFile($sub, $path)
  2154. {
  2155. if ($fd = @fopen($path, 'w'))
  2156. {
  2157. $tab = 'LANGMAIL';
  2158. fwrite($fd, "<?php\n\nglobal \$_".$tab.";\n\$_".$tab." = array();\n");
  2159. foreach ($sub as $key => $value)
  2160. {
  2161. // Magic Quotes shall... not.. PASS!
  2162. if (_PS_MAGIC_QUOTES_GPC_)
  2163. $value = stripslashes($value);
  2164. fwrite($fd, '$_'.$tab.'[\''.pSQL($key).'\'] = \''.pSQL($value).'\';'."\n");
  2165. }
  2166. fwrite($fd, "\n?>");
  2167. fclose($fd);
  2168. }
  2169. else
  2170. throw new PrestaShopException(sprintf(Tools::displayError('Cannot write language file for e-mail subjects, path is: %s'), $path));
  2171. }
  2172. /**
  2173. * This get files to translate in module directory.
  2174. * Recursive method allow to get each files for a module no matter his depth.
  2175. *
  2176. * @param string $path directory path to scan
  2177. * @param array $array_files by reference - array which saved files to parse.
  2178. * @param string $module_name module name
  2179. * @param string $lang_file full path of translation file
  2180. * @param boolean $is_default
  2181. */
  2182. protected function recursiveGetModuleFiles($path, &$array_files, $module_name, $lang_file, $is_default = false)
  2183. {
  2184. $files_module = array();
  2185. if (Tools::file_exists_cache($path))
  2186. $files_module = scandir($path);
  2187. $files_for_module = $this->clearModuleFiles($files_module, 'file');
  2188. if (!empty($files_for_module))
  2189. $array_files[] = array(
  2190. 'file_name' => $lang_file,
  2191. 'dir' => $path,
  2192. 'files' => $files_for_module,
  2193. 'module' => $module_name,
  2194. 'is_default' => $is_default,
  2195. 'theme' => $this->theme_selected,
  2196. );
  2197. $dir_module = $this->clearModuleFiles($files_module, 'directory', $path);
  2198. if (!empty($dir_module))
  2199. foreach ($dir_module as $folder)
  2200. $this->recursiveGetModuleFiles($path.$folder.'/', $array_files, $module_name, $lang_file, $is_default);
  2201. }
  2202. /**
  2203. * This method get translation in each translations file.
  2204. * The file depend on $lang param.
  2205. *
  2206. * @param array $modules list of modules
  2207. * @param string $root_dir path where it get each modules
  2208. * @param string $lang iso code of choosen language to translate
  2209. * @param boolean $is_default set it if modules are located in root/prestashop/modules folder
  2210. * This allow to distinguish overrided prestashop theme and original module
  2211. */
  2212. protected function getAllModuleFiles($modules, $root_dir, $lang, $is_default = false)
  2213. {
  2214. $array_files = array();
  2215. foreach ($modules as $module)
  2216. {
  2217. if ($module{0} != '.' && is_dir($root_dir.$module))
  2218. {
  2219. if (Tools::file_exists_cache($root_dir.$module.'/translations/'.$lang.'.php'))
  2220. $lang_file = $root_dir.$module.'/translations/'.$lang.'.php';
  2221. else
  2222. $lang_file = $root_dir.$module.'/'.$lang.'.php';
  2223. @include($lang_file);
  2224. $this->getModuleTranslations();
  2225. $this->recursiveGetModuleFiles($root_dir.$module.'/', $array_files, $module, $lang_file, $is_default);
  2226. }
  2227. }
  2228. return $array_files;
  2229. }
  2230. /**
  2231. * This method generate the form for modules translations
  2232. */
  2233. public function initFormModules()
  2234. {
  2235. // Get path of directory for find a good path of translation file
  2236. if ($this->theme_selected != self::DEFAULT_THEME_NAME)
  2237. $i18n_dir = $this->translations_informations[$this->type_selected]['override']['dir'];
  2238. else
  2239. $i18n_dir = $this->translations_informations[$this->type_selected]['dir'];
  2240. // Get list of modules
  2241. $modules = $this->getListModules();
  2242. if (!empty($modules))
  2243. {
  2244. // Get all modules files and include all translation files
  2245. $arr_files = $this->getAllModuleFiles($modules, $i18n_dir, $this->lang_selected->iso_code, true);
  2246. foreach ($arr_files as $value)
  2247. $this->findAndFillTranslations($value['files'], $value['theme'], $value['module'], $value['dir']);
  2248. $this->tpl_view_vars = array_merge($this->tpl_view_vars, array(
  2249. 'default_theme_name' => self::DEFAULT_THEME_NAME,
  2250. 'count' => $this->total_expression,
  2251. 'limit_warning' => $this->displayLimitPostWarning($this->total_expression),
  2252. 'textarea_sized' => TEXTAREA_SIZED,
  2253. 'modules_translations' => isset($this->modules_translations) ? $this->modules_translations : array(),
  2254. 'missing_translations' => $this->missing_translations
  2255. ));
  2256. $this->initToolbar();
  2257. $this->base_tpl_view = 'translation_modules.tpl';
  2258. return parent::renderView();
  2259. }
  2260. }
  2261. /** Parse PDF class
  2262. *
  2263. * @param string $file_path file to parse
  2264. * @param string $file_type type of file
  2265. * @param array $langArray contains expression in the chosen language
  2266. * @param string $tab name to use with the md5 key
  2267. * @param array $tabs_array
  2268. * @return array containing all datas needed for building the translation form
  2269. * @since 1.4.5.0
  2270. */
  2271. protected function parsePdfClass($file_path, $file_type, $lang_array, $tab, $tabs_array, &$count_missing)
  2272. {
  2273. // Get content for this file
  2274. $content = file_get_contents($file_path);
  2275. // Parse this content
  2276. $matches = $this->userParseFile($content, $this->type_selected, $file_type);
  2277. foreach ($matches as $key)
  2278. {
  2279. if (stripslashes(array_key_exists($tab.md5(addslashes($key)), $lang_array)))
  2280. $tabs_array[$tab][$key]['trad'] = html_entity_decode($lang_array[$tab.md5(addslashes($key))], ENT_COMPAT, 'UTF-8');
  2281. else
  2282. {
  2283. $tabs_array[$tab][$key]['trad'] = '';
  2284. if (!isset($count_missing[$tab]))
  2285. $count_missing[$tab] = 1;
  2286. else
  2287. $count_missing[$tab]++;
  2288. }
  2289. $tabs_array[$tab][$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  2290. }
  2291. return $tabs_array;
  2292. }
  2293. /**
  2294. * This method generate the form for PDF translations
  2295. */
  2296. public function initFormPDF()
  2297. {
  2298. $name_var = $this->translations_informations[$this->type_selected]['var'];
  2299. $GLOBALS[$name_var] = array();
  2300. $missing_translations_pdf = array();
  2301. $i18n_dir = $this->translations_informations[$this->type_selected]['dir'];
  2302. $default_i18n_file = $i18n_dir.$this->translations_informations[$this->type_selected]['file'];
  2303. if (($this->theme_selected == self::DEFAULT_THEME_NAME) && _PS_MODE_DEV_)
  2304. $i18n_file = $default_i18n_file;
  2305. else
  2306. {
  2307. $i18n_dir = $this->translations_informations[$this->type_selected]['override']['dir'];
  2308. $i18n_file = $i18n_dir.$this->translations_informations[$this->type_selected]['override']['file'];
  2309. }
  2310. $this->checkDirAndCreate($i18n_file);
  2311. if ((!file_exists($i18n_file) && !is_writable($i18n_dir)) && !is_writable($i18n_file))
  2312. $this->errors[] = sprintf(Tools::displayError('Cannot write into the "%s"'), $i18n_file);
  2313. @include($i18n_file);
  2314. // if the override's translation file is empty load the default file
  2315. if (!isset($GLOBALS[$name_var]) || count($GLOBALS[$name_var]) == 0)
  2316. @include($default_i18n_file);
  2317. $prefix_key = 'PDF';
  2318. $tabs_array = array($prefix_key => array());
  2319. $files_by_directory = $this->getFileToParseByTypeTranslation();
  2320. foreach ($files_by_directory as $type => $directories)
  2321. foreach ($directories as $dir => $files)
  2322. foreach ($files as $file)
  2323. if (!in_array($file, self::$ignore_folder) && Tools::file_exists_cache($file_path = $dir.$file))
  2324. {
  2325. if ($type == 'tpl')
  2326. {
  2327. if (Tools::file_exists_cache($file_path) && is_file($file_path))
  2328. {
  2329. // Get content for this file
  2330. $content = file_get_contents($file_path);
  2331. // Parse this content
  2332. $matches = $this->userParseFile($content, $this->type_selected, 'tpl');
  2333. foreach ($matches as $key)
  2334. {
  2335. if (isset($GLOBALS[$name_var][$prefix_key.md5($key)]))
  2336. $tabs_array[$prefix_key][$key]['trad'] = (html_entity_decode($GLOBALS[$name_var][$prefix_key.md5($key)], ENT_COMPAT, 'UTF-8'));
  2337. else
  2338. {
  2339. if (!isset($tabs_array[$prefix_key][$key]['trad']))
  2340. {
  2341. $tabs_array[$prefix_key][$key]['trad'] = '';
  2342. if (!isset($missing_translations_pdf[$prefix_key]))
  2343. $missing_translations_pdf[$prefix_key] = 1;
  2344. else
  2345. $missing_translations_pdf[$prefix_key]++;
  2346. }
  2347. }
  2348. $tabs_array[$prefix_key][$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
  2349. }
  2350. }
  2351. }
  2352. else
  2353. if (Tools::file_exists_cache($file_path))
  2354. $tabs_array = $this->parsePdfClass($file_path, 'php', $GLOBALS[$name_var], $prefix_key, $tabs_array, $missing_translations_pdf);
  2355. }
  2356. $this->tpl_view_vars = array_merge($this->tpl_view_vars, array(
  2357. 'count' => count($tabs_array['PDF']),
  2358. 'limit_warning' => $this->displayLimitPostWarning(count($tabs_array['PDF'])),
  2359. 'tabsArray' => $tabs_array,
  2360. 'missing_translations' => $missing_translations_pdf
  2361. ));
  2362. $this->initToolbar();
  2363. $this->base_tpl_view = 'translation_form.tpl';
  2364. return parent::renderView();
  2365. }
  2366. /**
  2367. * recursively list files in directory $dir
  2368. */
  2369. public function listFiles($dir, $list = array(), $file_ext = 'tpl')
  2370. {
  2371. $dir = rtrim($dir, '/').DIRECTORY_SEPARATOR;
  2372. $to_parse = scandir($dir);
  2373. // copied (and kind of) adapted from AdminImages.php
  2374. foreach ($to_parse as $file)
  2375. {
  2376. if (!in_array($file, self::$ignore_folder))
  2377. {
  2378. if (preg_match('#'.preg_quote($file_ext, '#').'$#i', $file))
  2379. $list[$dir][] = $file;
  2380. else if (is_dir($dir.$file))
  2381. $list = $this->listFiles($dir.$file, $list, $file_ext);
  2382. }
  2383. }
  2384. return $list;
  2385. }
  2386. }