Ejemplo n.º 1
0
 public function submitImportLang()
 {
     if (!isset($_FILES['file']['tmp_name']) || !$_FILES['file']['tmp_name']) {
         $this->errors[] = Tools::displayError('No file has been selected.');
     } else {
         require_once _PS_TOOL_DIR_ . 'tar/Archive_Tar.php';
         $gz = new Archive_Tar($_FILES['file']['tmp_name'], true);
         $filename = $_FILES['file']['name'];
         $iso_code = str_replace(array('.tar.gz', '.gzip'), '', $filename);
         if (Validate::isLangIsoCode($iso_code)) {
             $themes_selected = Tools::getValue('theme', array(self::DEFAULT_THEME_NAME));
             $files_list = AdminTranslationsController::filterTranslationFiles($gz->listContent());
             $files_paths = AdminTranslationsController::filesListToPaths($files_list);
             $uniqid = uniqid();
             $sandbox = _PS_CACHE_DIR_ . 'sandbox' . DIRECTORY_SEPARATOR . $uniqid . DIRECTORY_SEPARATOR;
             if ($gz->extractList($files_paths, $sandbox)) {
                 foreach ($files_list as $file2check) {
                     //don't validate index.php, will be overwrite when extract in translation directory
                     if (pathinfo($file2check['filename'], PATHINFO_BASENAME) == 'index.php') {
                         continue;
                     }
                     if (preg_match('@^[0-9a-z-_/\\\\]+\\.php$@i', $file2check['filename'])) {
                         if (!AdminTranslationsController::checkTranslationFile(file_get_contents($sandbox . $file2check['filename']))) {
                             $this->errors[] = sprintf(Tools::displayError('Validation failed for: %s'), $file2check['filename']);
                         }
                     } elseif (!preg_match('@^[0-9a-z-_/\\\\]+\\.(html|tpl|txt)$@i', $file2check['filename'])) {
                         $this->errors[] = sprintf(Tools::displayError('Unidentified file found: %s'), $file2check['filename']);
                     }
                 }
                 Tools::deleteDirectory($sandbox, true);
             }
             if (count($this->errors)) {
                 return false;
             }
             if ($gz->extractList($files_paths, _PS_TRANSLATIONS_DIR_ . '../')) {
                 foreach ($files_list as $file2check) {
                     if (pathinfo($file2check['filename'], PATHINFO_BASENAME) == 'index.php' && file_put_contents(_PS_TRANSLATIONS_DIR_ . '../' . $file2check['filename'], Tools::getDefaultIndexContent())) {
                         continue;
                     }
                 }
                 // Clear smarty modules cache
                 Tools::clearCache();
                 if (Validate::isLanguageFileName($filename)) {
                     if (!Language::checkAndAddLanguage($iso_code)) {
                         $conf = 20;
                     } else {
                         // Reset cache
                         Language::loadLanguages();
                         AdminTranslationsController::checkAndAddMailsFiles($iso_code, $files_list);
                         $this->checkAndAddThemesFiles($files_list, $themes_selected);
                         $tab_errors = AdminTranslationsController::addNewTabs($iso_code, $files_list);
                         if (count($tab_errors)) {
                             $this->errors += $tab_errors;
                             return false;
                         }
                     }
                 }
                 $this->redirect(false, isset($conf) ? $conf : '15');
             }
             $this->errors[] = Tools::displayError('The archive cannot be extracted.');
         } else {
             $this->errors[] = sprintf(Tools::displayError('ISO CODE invalid "%1$s" for the following file: "%2$s"'), $iso_code, $filename);
         }
     }
 }
 public function submitImportLang()
 {
     if (!isset($_FILES['file']['tmp_name']) || !$_FILES['file']['tmp_name']) {
         $this->errors[] = $this->trans('No file has been selected.', array(), 'Admin.Notifications.Error');
     } else {
         $gz = new \Archive_Tar($_FILES['file']['tmp_name'], true);
         $filename = $_FILES['file']['name'];
         $iso_code = str_replace(array('.tar.gz', '.gzip'), '', $filename);
         if (Validate::isLangIsoCode($iso_code)) {
             $themes_selected = Tools::getValue('theme', array(self::DEFAULT_THEME_NAME));
             $files_list = AdminTranslationsController::filterTranslationFiles($gz->listContent());
             $files_paths = AdminTranslationsController::filesListToPaths($files_list);
             $uniqid = uniqid();
             $sandbox = _PS_CACHE_DIR_ . 'sandbox' . DIRECTORY_SEPARATOR . $uniqid . DIRECTORY_SEPARATOR;
             if ($gz->extractList($files_paths, $sandbox)) {
                 foreach ($files_list as $file2check) {
                     //don't validate index.php, will be overwrite when extract in translation directory
                     if (pathinfo($file2check['filename'], PATHINFO_BASENAME) == 'index.php') {
                         continue;
                     }
                     if (preg_match('@^[0-9a-z-_/\\\\]+\\.php$@i', $file2check['filename'])) {
                         if (!@filemtime($sandbox . $file2check['filename']) || !AdminTranslationsController::checkTranslationFile(file_get_contents($sandbox . $file2check['filename']))) {
                             $this->errors[] = sprintf($this->trans('Validation failed for: %s', array(), 'Admin.International.Notification'), $file2check['filename']);
                         }
                     } elseif (!preg_match('@mails[0-9a-z-_/\\\\]+\\.(html|tpl|txt)$@i', $file2check['filename'])) {
                         $this->errors[] = sprintf($this->trans('Unidentified file found: %s', array(), 'Admin.International.Notification'), $file2check['filename']);
                     }
                 }
                 Tools::deleteDirectory($sandbox, true);
             }
             $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)) {
                     $this->errors[] = (!$i++ ? $this->trans('The archive cannot be extracted.', array(), 'Admin.International.Notification') . ' ' : '') . $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'), $path);
                     $tmp_array[] = $path;
                 }
             }
             if (count($this->errors)) {
                 return false;
             }
             if ($error = $gz->extractList($files_paths, _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 {
                     foreach ($files_list as $file2check) {
                         if (pathinfo($file2check['filename'], PATHINFO_BASENAME) == 'index.php' && file_put_contents(_PS_TRANSLATIONS_DIR_ . '../' . $file2check['filename'], Tools::getDefaultIndexContent())) {
                             continue;
                         }
                     }
                     // Clear smarty modules cache
                     Tools::clearCache();
                     if (Validate::isLanguageFileName($filename)) {
                         if (!Language::checkAndAddLanguage($iso_code)) {
                             $conf = 20;
                         } else {
                             // Reset cache
                             Language::loadLanguages();
                             AdminTranslationsController::checkAndAddMailsFiles($iso_code, $files_list);
                             $this->checkAndAddThemesFiles($files_list, $themes_selected);
                             $tab_errors = AdminTranslationsController::addNewTabs($iso_code, $files_list);
                             if (count($tab_errors)) {
                                 $this->errors += $tab_errors;
                                 return false;
                             }
                         }
                     }
                     //fetch cldr datas for the new imported locale
                     $languageCode = explode('-', Language::getLanguageCodeByIso($iso_code));
                     $cldrUpdate = new Update(_PS_TRANSLATIONS_DIR_);
                     $cldrUpdate->fetchLocale($languageCode[0] . '-' . strtoupper($languageCode[1]));
                     $this->redirect(false, isset($conf) ? $conf : '15');
                 }
             }
             $this->errors[] = $this->trans('The archive cannot be extracted.', array(), 'Admin.International.Notification');
         } else {
             $this->errors[] = sprintf($this->trans('ISO CODE invalid "%1$s" for the following file: "%2$s"', array(), 'Admin.International.Notification'), $iso_code, $filename);
         }
     }
 }