]> git.andy128k.dev Git - ipf.git/commitdiff
remove global from admin filebrowser controller
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 7 Apr 2019 08:55:41 +0000 (10:55 +0200)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 7 Apr 2019 08:55:41 +0000 (10:55 +0200)
ipf/admin/controllers/file_browser.php
ipf/controller/base.php

index 332b4ed629eaa7dec72ff2098ee72e72bd6a3449..c7b80a45ecdaa2d7bad9964e5e76842b7b238ba4 100644 (file)
@@ -2,55 +2,9 @@
 
 class IPF_Admin_FileBrowser_Controller extends IPF_Admin_Base_Controller
 {
-    static function cmp($a, $b)
-    {
-        if ($a['name'] == $b['name'])
-            return 0;
-        return ($a['name'] < $b['name']) ? -1 : 1;
-    }
-
-    /** @var string */
-    protected $root;
-
-    /** @var string */
-    protected $dir;
-
-    /** @var string */
-    protected $relative;
-
-    protected function setRoot()
-    {
-        $this->root = realpath(IPF::getUploadPath()) . DIRECTORY_SEPARATOR;
-    }
-
-    protected function validatePath($path)
-    {
-        $path = realpath($path) . DIRECTORY_SEPARATOR;
-        if (!\PFF\Str::startsWith($path, $this->root))
-            throw new IPF_Admin_AccessDenied;
-        return $path;
-    }
-
-    protected function setCurrentDir()
-    {
-        $this->setRoot();
-        $this->dir = $this->validatePath(IPF::getUploadPath() . DIRECTORY_SEPARATOR . \PFF\Arr::get($this->request->REQUEST, 'dir', ''));
-        $this->relative = substr($this->dir, strlen($this->root));
-    }
-
-    protected function validateName($name)
-    {
-        $name = basename($name);
-        if (!$name || $name === '.' || $name === '..')
-            throw new IPF_Admin_AccessDenied;
-
-        return $name;
-    }
-
     protected function before($action)
     {
         $this->ensureUserIsStaff();
-        $this->setCurrentDir();
     }
 
     function index()
@@ -70,7 +24,9 @@ class IPF_Admin_FileBrowser_Controller extends IPF_Admin_Base_Controller
 
     function listDirectory()
     {
-        list($dirs, $files) = self::listDir($this->dir);
+        $dir = $this->validateDirectory($this->request->GET['dir']);
+
+        list($dirs, $files) = self::listDir($dir);
         return new IPF_HTTP_Response_Json([
             'dirs' => $dirs,
             'files' => $files,
@@ -79,43 +35,69 @@ class IPF_Admin_FileBrowser_Controller extends IPF_Admin_Base_Controller
 
     function rename()
     {
+        $dir = $this->validateDirectory($this->request->POST['dir']);
         $old_name = $this->validateName($this->request->POST['old_name']);
         $new_name = $this->validateName($this->request->POST['new_name']);
-        rename($this->dir . $old_name, $this->dir . $new_name);
+        rename($dir . $old_name, $dir . $new_name);
         return $this->noContent();
     }
 
     function move()
     {
-        $destination = $this->validatePath(IPF::getUploadPath() . DIRECTORY_SEPARATOR . \PFF\Arr::get($this->request->POST, 'destination', ''));
+        $dir = $this->validateDirectory($this->request->POST['dir']);
+        $destination = $this->validateDirectory($this->request->POST['destination']);
         $name = $this->validateName($this->request->POST['name']);
-        rename($this->dir . $name, $destination . $name);
+
+        rename($dir . $name, $destination . $name);
         return $this->noContent();
     }
 
     function mkdir()
     {
+        $dir = $this->validateDirectory($this->request->POST['dir']);
         $name = $this->validateName($this->request->POST['name']);
-        mkdir($this->dir . $name);
+        mkdir($dir . $name);
         return $this->noContent();
     }
 
     function delete()
     {
+        $dir = $this->validateDirectory($this->request->POST['dir']);
         $name = $this->validateName($this->request->POST['name']);
-        IPF_Utils::removeDirectories($this->dir . $name);
+        IPF_Utils::removeDirectories($dir . $name);
         return $this->noContent();
     }
 
     function upload()
     {
+        $dir = $this->validateDirectory($this->request->POST['dir']);
         foreach ($this->request->FILES['files'] as $file) {
-            $uploadfile = $this->dir . basename($file['name']);
+            $uploadfile = $dir . basename($file['name']);
             move_uploaded_file($file['tmp_name'], $uploadfile);
         }
         return $this->noContent();
     }
 
+    private function validateDirectory($path)
+    {
+        $settings = $this->container['settings'];
+        $uploadPath = $settings->get('document_root') . $settings->get('upload_url');
+        $root = realpath($uploadPath) . DIRECTORY_SEPARATOR;
+        $realpath = realpath($root . $path) . DIRECTORY_SEPARATOR;
+        if (!\PFF\Str::startsWith($realpath, $root))
+            throw new IPF_Admin_AccessDenied;
+        return $realpath;
+    }
+
+    private function validateName($name)
+    {
+        $name = basename($name);
+        if (!$name || $name === '.' || $name === '..')
+            throw new IPF_Admin_AccessDenied;
+
+        return $name;
+    }
+
     private static function withErrors($callable)
     {
         set_error_handler(function ($errno, $errstr, $errfile, $errline) {
@@ -167,6 +149,13 @@ class IPF_Admin_FileBrowser_Controller extends IPF_Admin_Base_Controller
         });
     }
 
+    static function cmp($a, $b)
+    {
+        if ($a['name'] == $b['name'])
+            return 0;
+        return ($a['name'] < $b['name']) ? -1 : 1;
+    }
+
     /**
      * @return IPF_HTTP_Response
      */
index 3818b068e907c98ea786ae3081774a4203c371e7..c1b7fcfc2fe462245da5ec776509065e6e7bc9db 100644 (file)
@@ -4,6 +4,7 @@ use Pimple\Container;
 
 class IPF_Controller
 {
+    /** @var IPF_HTTP_Request */
     protected $request;
     protected $params;
     /** @var Container */