浏览代码

Update Files, File and Image

Visman 2 年之前
父节点
当前提交
5cdec31ed1
共有 3 个文件被更改,包括 82 次插入65 次删除
  1. 51 36
      app/Core/File.php
  2. 23 21
      app/Core/Files.php
  3. 8 8
      app/Core/Image.php

+ 51 - 36
app/Core/File.php

@@ -10,6 +10,7 @@ declare(strict_types=1);
 
 
 namespace ForkBB\Core;
 namespace ForkBB\Core;
 
 
+use ForkBB\Core\Files;
 use ForkBB\Core\Exceptions\FileException;
 use ForkBB\Core\Exceptions\FileException;
 use InvalidArgumentException;
 use InvalidArgumentException;
 
 
@@ -54,7 +55,7 @@ class File
      * Флаг автопереименования файла
      * Флаг автопереименования файла
      * @var bool
      * @var bool
      */
      */
-    protected $rename  = false;
+    protected $rename = false;
 
 
     /**
     /**
      * Флаг перезаписи файла
      * Флаг перезаписи файла
@@ -66,10 +67,18 @@ class File
      * Паттерн для pathinfo
      * Паттерн для pathinfo
      * @var string
      * @var string
      */
      */
-    protected $pattern = '%^(?!.*?\.\.)([\w.\x5C/:-]*[\x5C/])?(\*|[\w.-]+)\.(\*|[a-z\d]+)$%i';
+    protected $pattern = '%^(?!.*?\.\.)([\w.\x5C/:-]*[\x5C/])?(\*|[\w.-]+)\.(\*|[a-z\d]+)$%iD';
 
 
-    public function __construct(string $path, array $options)
+    /**
+     * @var Files
+     */
+    protected $files;
+
+    public function __construct(string $path, string $name, string $ext, Files $files)
     {
     {
+        if ($files->isBadPath($path)) {
+            throw new FileException('Bad path to file');
+        }
         if (! \is_file($path)) {
         if (! \is_file($path)) {
             throw new FileException('File not found');
             throw new FileException('File not found');
         }
         }
@@ -77,24 +86,13 @@ class File
             throw new FileException('File can not be read');
             throw new FileException('File can not be read');
         }
         }
 
 
-        $this->path = $path;
-        $this->data = null;
-
-        $name = null;
-        $ext  = null;
-        if (isset($options['basename'])) {
-            if (false === ($pos = \strrpos($options['basename'], '.'))) {
-                $name = $options['basename'];
-            } else {
-                $name = \substr($options['basename'], 0, $pos);
-                $ext  = \substr($options['basename'], $pos + 1);
-            }
-        }
-
-        $this->name = isset($options['filename']) && \is_string($options['filename']) ? $options['filename'] : $name;
-        $this->ext  = isset($options['extension']) && \is_string($options['extension']) ? $options['extension'] : $ext;
+        $this->files = $files;
+        $this->path  = $path;
+        $this->name  = $name;
+        $this->ext   = $ext;
+        $this->data  = null;
+        $this->size  = \is_string($this->data) ? \strlen($this->data) : \filesize($path);
 
 
-        $this->size = \is_string($this->data) ? \strlen($this->data) : \filesize($path);
         if (! $this->size) {
         if (! $this->size) {
             throw new FileException('File size is undefined');
             throw new FileException('File size is undefined');
         }
         }
@@ -234,30 +232,47 @@ class File
     {
     {
         $info = $this->pathinfo($path);
         $info = $this->pathinfo($path);
 
 
+        if (empty($info)) {
+            return false;
+        }
+        if ($this->files->isBadPath($info['dirname'])) {
+            $this->error = 'Bad path to file';
+
+            return false;
+        }
         if (
         if (
-            null === $info
-            || ! $this->dirProc($info['dirname'])
+            ! $this->dirProc($info['dirname'])
         ) {
         ) {
             return false;
             return false;
         }
         }
 
 
-        if ($this->rename) {
-            $old = $info['filename'];
-            $i   = 1;
-            while (\file_exists($info['dirname'] . $info['filename'] . '.' . $info['extension'])) {
-                ++$i;
-                $info['filename'] = $old . '_' . $i;
+        $name = $info['filename'];
+        $i    = 1;
+
+        while (true) {
+            $path = $info['dirname'] . $info['filename'] . '.' . $info['extension'];
+
+            if ($this->files->isBadPath($path)) {
+                $this->error = 'Bad path to file';
+
+                return false;
             }
             }
-        } elseif (
-            ! $this->rewrite
-            && \file_exists($info['dirname'] . $info['filename'] . '.' . $info['extension'])
-        ) {
-            $this->error = 'Such file already exists';
 
 
-            return false;
-        }
+            if (\file_exists($path)) {
+                if ($this->rename) {
+                    ++$i;
+                    $info['filename'] = $name . '_' . $i;
+
+                    continue;
+                } elseif (! $this->rewrite) {
+                    $this->error = 'Such file already exists';
 
 
-        $path = $info['dirname'] . $info['filename'] . '.' . $info['extension'];
+                    return false;
+                }
+            }
+
+            break;
+        }
 
 
         if ($this->fileProc($path)) {
         if ($this->fileProc($path)) {
             $this->size = \filesize($path);
             $this->size = \filesize($path);

+ 23 - 21
app/Core/Files.php

@@ -56,6 +56,12 @@ class Files
      */
      */
     protected $imageDriver;
     protected $imageDriver;
 
 
+    /**
+     * Список имён драйверов
+     * @var array
+     */
+    protected $imageDrivers;
+
     /**
     /**
      * Список mime типов считающихся картинками
      * Список mime типов считающихся картинками
      * @var array
      * @var array
@@ -857,7 +863,8 @@ class Files
 
 
     public function __construct(/* string|int */ $maxFileSize, /* string|int */ $maxImgSize, array $imageDrivers, Container $c)
     public function __construct(/* string|int */ $maxFileSize, /* string|int */ $maxImgSize, array $imageDrivers, Container $c)
     {
     {
-        $this->c = $c;
+        $this->c            = $c;
+        $this->imageDrivers = $imageDrivers;
 
 
         $init = \min(
         $init = \min(
             \PHP_INT_MAX,
             \PHP_INT_MAX,
@@ -875,19 +882,22 @@ class Files
             $this->size($maxFileSize),
             $this->size($maxFileSize),
             $init
             $init
         );
         );
-        $this->imageDriver = $this->curImageDriver($imageDrivers);
     }
     }
 
 
     /**
     /**
      * Возращает драйвер для работы с изображениями
      * Возращает драйвер для работы с изображениями
      */
      */
-    protected function curImageDriver(array $drivers): DefaultDriver
+    public function imageDriver(): DefaultDriver
     {
     {
-        foreach ($drivers as $class) {
-            $driver = new $class($this);
+        if ($this->imageDriver instanceof DefaultDriver) {
+            return $this->imageDriver;
+        }
 
 
-            if (true === $driver->ready()) {
-                return $driver;
+        foreach ($this->imageDrivers as $class) {
+            $this->imageDriver = new $class($this);
+
+            if (true === $this->imageDriver->ready()) {
+                return $this->imageDriver;
             }
             }
         }
         }
 
 
@@ -1100,12 +1110,12 @@ class Files
             return null;
             return null;
         }
         }
 
 
-        if (\preg_match('%^(.+)\.([^.\\\/]++)$%D', $file['name'], $matches)) {
-            $name = $matches[1];
-            $ext  = $matches[2];
-        } else {
+        if (false === ($pos = \strrpos($file['name'], '.'))) {
             $name = $file['name'];
             $name = $file['name'];
-            $ext  = '';
+            $exy  = '';
+        } else {
+            $name = \substr($file['name'], 0, $pos);
+            $ext  = \substr($file['name'], $pos + 1);
         }
         }
 
 
         $imageExt = $this->imageExt($file['tmp_name']);
         $imageExt = $this->imageExt($file['tmp_name']);
@@ -1145,18 +1155,10 @@ class Files
             return null;
             return null;
         }
         }
 
 
-        $options = [
-            'filename'  => $name,
-            'extension' => $ext,
-            'basename'  => $name . '.' . $ext,
-            'mime'      => $mimeType, //$file['type'],
-//            'size'      => $file['size'],
-        ];
-
         $level = $this->c->ErrorHandler->logOnly(\E_WARNING);
         $level = $this->c->ErrorHandler->logOnly(\E_WARNING);
 
 
         try {
         try {
-            $result = new $className($file['tmp_name'], $options, $this->imageDriver);
+            $result = new $className($file['tmp_name'], $name, $ext, $this);
         } catch (FileException $e) {
         } catch (FileException $e) {
             $this->error = $e->getMessage();
             $this->error = $e->getMessage();
 
 

+ 8 - 8
app/Core/Image.php

@@ -40,22 +40,22 @@ class Image extends File
      * Паттерн для pathinfo
      * Паттерн для pathinfo
      * @var string
      * @var string
      */
      */
-    protected $pattern = '%^(?!.*?\.\.)([\w.\x5C/:-]*[\x5C/])?(\*|[\w.-]+)\.(\*|[a-z\d]+|\([a-z\d]+(?:\|[a-z\d]+)*\))$%i';
+    protected $pattern = '%^(?!.*?\.\.)([\w.\x5C/:-]*[\x5C/])?(\*|[\w.-]+)\.(\*|[a-z\d]+|\([a-z\d]+(?:\|[a-z\d]+)*\))$%iD';
 
 
-    public function __construct(string $path, array $options, DefaultDriver $imgDriver)
+    public function __construct(string $path, string $name, string $ext, Files $files)
     {
     {
-        parent::__construct($path, $options);
+        parent::__construct($path, $name, $ext, $files);
 
 
-        if ($imgDriver::DEFAULT) {
+        $this->imgDriver = $files->imageDriver();
+
+        if ($this->imgDriver::DEFAULT) {
             throw new FileException('No library for work with images');
             throw new FileException('No library for work with images');
         }
         }
 
 
-        $this->imgDriver = $imgDriver;
-
         if (\is_string($this->data)) {
         if (\is_string($this->data)) {
-            $this->image = $imgDriver->readFromStr($this->data);
+            $this->image = $this->imgDriver->readFromStr($this->data);
         } else {
         } else {
-            $this->image = $imgDriver->readFromPath($this->path);
+            $this->image = $this->imgDriver->readFromPath($this->path);
         }
         }
 
 
         if (false === $this->image) {
         if (false === $this->image) {