sruupl / batflat

Lightweight, fast and easy CMS for free. Bootstrap ready. https://batflat.org
MIT License
134 stars 54 forks source link

Fix translations load #9

Closed sim2github closed 4 years ago

sim2github commented 6 years ago

Would be nice to zip parser fix relative path to inc/ folder. For example batflat-se-master.zip or taged realeases like batflat-ru-1.3.3.zip.

Any way translation contain js file for wysiwyg editor and most available translations not compatible with current parser.

My proposal:

diff --git a/inc/modules/settings/Admin.php b/inc/modules/settings/Admin.php
index f05b812..856a178 100644
--- a/inc/modules/settings/Admin.php
+++ b/inc/modules/settings/Admin.php
@@ -271,31 +271,38 @@ class Admin extends AdminModule

         if (isset($_POST['upload']) && FILE_LOCK === false) {
             $zip = new ZipArchive();
-            $error = false;
+            $allowedDest = '/(.*?inc\/)((jscripts|lang|modules).*$)/';
+            $count = 0;
             $file = !empty($_FILES['lang_package']['tmp_name']) ? $_FILES['lang_package']['tmp_name'] : '/';
             $open = $zip->open($file);
             if ($open === true) {
                 for ($i = 0; $i < $zip->numFiles; $i++) {
                     $filename = pathinfo($zip->getNameIndex($i));
+                    if (isset($filename['extension'])
+                            && ($filename['extension'] == 'ini' || $filename['extension'] == 'js')
+                        ) {
+                        preg_match($allowedDest, $filename['dirname'], $matches);
+                        $dest = realpath(BASE_DIR) . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR . $matches[2];
+                        if (!file_exists($dest)) {
+                            mkdir($dest, 0755, true);
+                        }

-                    if (strpos($filename['dirname'].'/', '/lang/') === false) {
-                        $error = true;
-                        break;
-                    }
-
-                    if ($filename['extension'] != 'ini') {
-                        $error = true;
-                        break;
+                        copy(
+                            'zip://' . $file . '#' . $filename['dirname']
+                                . DIRECTORY_SEPARATOR . $filename['basename'],
+                            $dest . DIRECTORY_SEPARATOR . $filename['basename']
+                        );
+                        $count++;
                     }
                 }
-
-                if (!$error) {
-                    $zip->extractTo(BASE_DIR);
-                    $zip->close();
+                
+                if ($count > 0) {
                     $this->notify('success', $this->lang('lang_import_success'));
                 } else {
                     $this->notify('failure', $this->lang('lang_import_error'));
                 }
+                
+                $zip->close();
             }
         }

P.S.: Why you not load repo on github for community contribution? Much simpler use git function to share and discuss code.

sruupl commented 6 years ago

@sim2github Hey! Batflat is already on this repository :)

sim2github commented 6 years ago

As i see now all translation embedded into distribution. So we need mechanism to delete/disable not required languages.

sruupl commented 6 years ago

@sim2github, yes, it is possible that such a function will appear soon. Thanks for your commits!