public static function downloadAndInstallLanguagePack($iso, $version = null, $params = null, $install = true) { if (!Validate::isLanguageIsoCode((string) $iso)) { return false; } if ($version == null) { $version = _PS_VERSION_; } $lang_pack = false; $lang_pack_ok = false; $errors = array(); $file = _PS_TRANSLATIONS_DIR_ . (string) $iso . '.gzip'; if (!($lang_pack_link = Tools::file_get_contents('http://www.prestashop.com/download/lang_packs/get_language_pack.php?version=' . $version . '&iso_lang=' . Tools::strtolower((string) $iso)))) { $errors[] = Tools::displayError('Archive cannot be downloaded from prestashop.com.'); } elseif (!($lang_pack = Tools::jsonDecode($lang_pack_link))) { $errors[] = Tools::displayError('Error occurred when language was checked according to your Prestashop version.'); } elseif (empty($lang_pack->error) && ($content = Tools::file_get_contents('http://translations.prestashop.com/download/lang_packs/gzip/' . $lang_pack->version . '/' . Tools::strtolower($lang_pack->iso_code . '.gzip')))) { if (!@file_put_contents($file, $content)) { if (is_writable(dirname($file))) { @unlink($file); @file_put_contents($file, $content); } elseif (!is_writable($file)) { $errors[] = Tools::displayError('Server does not have permissions for writing.') . ' (' . $file . ')'; } } } if (!file_exists($file)) { $errors[] = Tools::displayError('No language pack is available for your version.'); } elseif ($install) { require_once _PS_TOOL_DIR_ . 'tar/Archive_Tar.php'; $gz = new Archive_Tar($file, true); $files_list = AdminTranslationsController::filterTranslationFiles(Language::getLanguagePackListContent((string) $iso, $gz)); $files_paths = AdminTranslationsController::filesListToPaths($files_list); $i = 0; $tmp_array = array(); foreach ($files_paths as $files_path) { $path = dirname($files_path); if (is_dir(_PS_TRANSLATIONS_DIR_ . '../' . $path) && !is_writable(_PS_TRANSLATIONS_DIR_ . '../' . $path) && !in_array($path, $tmp_array)) { $errors[] = (!$i++ ? Tools::displayError('The archive cannot be extracted.') . ' ' : '') . Tools::displayError('The server does not have permissions for writing.') . ' ' . sprintf(Tools::displayError('Please check rights for %s'), $path); $tmp_array[] = $path; } } if (defined('_PS_HOST_MODE_')) { $mails_files = array(); $other_files = array(); foreach ($files_list as $key => $data) { if (substr($data['filename'], 0, 5) == 'mails') { $mails_files[] = $data; } else { $other_files[] = $data; } } $files_list = $other_files; } if (!$gz->extractList(AdminTranslationsController::filesListToPaths($files_list), _PS_TRANSLATIONS_DIR_ . '../')) { $errors[] = sprintf(Tools::displayError('Cannot decompress the translation file for the following language: %s'), (string) $iso); } // Clear smarty modules cache Tools::clearCache(); if (!Language::checkAndAddLanguage((string) $iso, $lang_pack, false, $params)) { $errors[] = sprintf(Tools::displayError('An error occurred while creating the language: %s'), (string) $iso); } else { // Reset cache Language::loadLanguages(); AdminTranslationsController::checkAndAddMailsFiles((string) $iso, $files_list); AdminTranslationsController::addNewTabs((string) $iso, $files_list); } } return count($errors) ? $errors : true; }
public static function installLanguagePack($iso, $params, &$errors = array()) { $file = _PS_TRANSLATIONS_DIR_ . (string) $iso . '.gzip'; $gz = new \Archive_Tar($file, true); $files_list = AdminTranslationsController::filterTranslationFiles(Language::getLanguagePackListContent((string) $iso, $gz)); $files_paths = AdminTranslationsController::filesListToPaths($files_list); $tmp_array = array(); foreach ($files_paths as $files_path) { $path = dirname($files_path); if (is_dir(_PS_TRANSLATIONS_DIR_ . '../' . $path) && !is_writable(_PS_TRANSLATIONS_DIR_ . '../' . $path) && !in_array($path, $tmp_array)) { $error = Tools::displayError('The server does not have permissions for writing.') . ' ' . sprintf(Tools::displayError('Please check permissions for %s'), $path); $errors[] = count($tmp_array) == 0 ? Tools::displayError('The archive cannot be extracted.') . ' ' . $error : $error; $tmp_array[] = $path; } } if (defined('_PS_HOST_MODE_')) { $mails_files = array(); $other_files = array(); foreach ($files_list as $key => $data) { if (substr($data['filename'], 0, 5) == 'mails') { $mails_files[] = $data; } } $files_list = array_diff($files_list, $mails_files); } if (!$gz->extractList(AdminTranslationsController::filesListToPaths($files_list), _PS_TRANSLATIONS_DIR_ . '../')) { $errors[] = sprintf(Tools::displayError('Cannot decompress the translation file for the following language: %s'), (string) $iso); } // Clear smarty modules cache Tools::clearCache(); if (!Language::checkAndAddLanguage((string) $iso, false, false, $params)) { $errors[] = sprintf(Tools::displayError('An error occurred while creating the language: %s'), (string) $iso); } else { // Reset cache Language::loadLanguages(); AdminTranslationsController::checkAndAddMailsFiles((string) $iso, $files_list); AdminTranslationsController::addNewTabs((string) $iso, $files_list); } self::installSfLanguagePack(self::getLocaleByIso($iso), $errors); return count($errors) ? $errors : true; }
public function submitAddLang() { $arr_import_lang = explode('|', Tools::getValue('params_import_language')); /* 0 = Language ISO code, 1 = PS version */ if (Validate::isLangIsoCode($arr_import_lang[0])) { $array_stream_context = @stream_context_create(array('http' => array('method' => 'GET', 'timeout' => 10))); $content = Tools::file_get_contents('http://www.prestashop.com/download/lang_packs/gzip/' . $arr_import_lang[1] . '/' . Tools::strtolower($arr_import_lang[0]) . '.gzip', false, $array_stream_context); if ($content) { $file = _PS_TRANSLATIONS_DIR_ . $arr_import_lang[0] . '.gzip'; if ((bool) @file_put_contents($file, $content)) { require_once _PS_TOOL_DIR_ . '/tar/Archive_Tar.php'; $gz = new Archive_Tar($file, true); $files_list = AdminTranslationsController::filterTranslationFiles($gz->listContent()); if ($error = $gz->extractList(AdminTranslationsController::filesListToPaths($files_list), _PS_TRANSLATIONS_DIR_ . '../')) { if (is_object($error) && !empty($error->message)) { $this->errors[] = Tools::displayError('The archive cannot be extracted.') . ' ' . $error->message; } else { if (!Language::checkAndAddLanguage($arr_import_lang[0])) { $conf = 20; } else { // Reset cache Language::loadLanguages(); // Clear smarty modules cache Tools::clearCache(); AdminTranslationsController::checkAndAddMailsFiles($arr_import_lang[0], $files_list); if ($tab_errors = AdminTranslationsController::addNewTabs($arr_import_lang[0], $files_list)) { $this->errors += $tab_errors; } } if (!unlink($file)) { $this->errors[] = sprintf(Tools::displayError('Cannot delete the archive %s.'), $file); } $this->redirect(false, isset($conf) ? $conf : '15'); } } elseif (!unlink($file)) { $this->errors[] = sprintf(Tools::displayError('Cannot delete the archive %s.'), $file); } } else { $this->errors[] = Tools::displayError('The server does not have permissions for writing.') . ' ' . sprintf(Tools::displayError('Please check rights for %s'), dirname($file)); } } else { $this->errors[] = Tools::displayError('Language not found.'); } } else { $this->errors[] = Tools::displayError('Invalid parameter'); } }
public function submitAddLang() { $arr_import_lang = explode('|', Tools::getValue('params_import_language')); /* 0 = Language ISO code, 1 = PS version */ if (Validate::isLangIsoCode($arr_import_lang[0])) { $array_stream_context = @stream_context_create(array('http' => array('method' => 'GET', 'timeout' => 10))); $content = Tools::file_get_contents('http://www.prestashop.com/download/lang_packs/gzip/' . $arr_import_lang[1] . '/' . Tools::strtolower($arr_import_lang[0]) . '.gzip', false, $array_stream_context); if ($content) { $file = _PS_TRANSLATIONS_DIR_ . $arr_import_lang[0] . '.gzip'; if ((bool) @file_put_contents($file, $content)) { $gz = new \Archive_Tar($file, true); if (_PS_MODE_DEV_) { $gz->setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_WARNING); } $files_list = AdminTranslationsController::filterTranslationFiles($gz->listContent()); if ($error = $gz->extractList(AdminTranslationsController::filesListToPaths($files_list), _PS_TRANSLATIONS_DIR_ . '../')) { if (is_object($error) && !empty($error->message)) { $this->errors[] = $this->trans('The archive cannot be extracted.', array(), 'Admin.International.Notification') . ' ' . $error->message; } else { if (!Language::checkAndAddLanguage($arr_import_lang[0])) { $conf = 20; } else { // Reset cache Language::loadLanguages(); // Clear smarty modules cache Tools::clearCache(); AdminTranslationsController::checkAndAddMailsFiles($arr_import_lang[0], $files_list); if ($tab_errors = AdminTranslationsController::addNewTabs($arr_import_lang[0], $files_list)) { $this->errors += $tab_errors; } } if (!unlink($file)) { $this->errors[] = sprintf($this->trans('Cannot delete the archive %s.', array(), 'Admin.International.Notification'), $file); } //fetch cldr datas for the new imported locale $languageCode = explode('-', Language::getLanguageCodeByIso($arr_import_lang[0])); $cldrUpdate = new Update(_PS_TRANSLATIONS_DIR_); $cldrUpdate->fetchLocale($languageCode[0] . '-' . Tools::strtoupper($languageCode[1])); $this->redirect(false, isset($conf) ? $conf : '15'); } } else { $this->errors[] = sprintf($this->trans('Cannot decompress the translation file for the following language: %s', array(), 'Admin.International.Notification'), $arr_import_lang[0]); $checks = array(); foreach ($files_list as $f) { if (isset($f['filename'])) { if (is_file(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . $f['filename']) && !is_writable(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . $f['filename'])) { $checks[] = dirname($f['filename']); } elseif (is_dir(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . $f['filename']) && !is_writable(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . dirname($f['filename']))) { $checks[] = dirname($f['filename']); } } } $checks = array_unique($checks); foreach ($checks as $check) { $this->errors[] = sprintf($this->trans('Please check rights for folder and files in %s', array(), 'Admin.Notifications.Error'), $check); } if (!unlink($file)) { $this->errors[] = sprintf($this->trans('Cannot delete the archive %s.', array(), 'Admin.International.Notification'), $file); } } } else { $this->errors[] = $this->trans('The server does not have permissions for writing.', array(), 'Admin.Notifications.Error') . ' ' . sprintf($this->trans('Please check rights for %s', array(), 'Admin.Notifications.Error'), dirname($file)); } } else { $this->errors[] = $this->trans('Language not found.', array(), 'Admin.International.Notification'); } } else { $this->errors[] = $this->trans('Invalid parameter.', array(), 'Admin.Notifications.Error'); } }