From 4c708d578be7ad00f1a81810bc17e45157e5f453 Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Mon, 2 Oct 2023 13:53:41 +0700 Subject: [PATCH] Small refactoring in Upload class (#927) --- src/Legacy/Common/Upload.php | 77 +++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 14 deletions(-) diff --git a/src/Legacy/Common/Upload.php b/src/Legacy/Common/Upload.php index 5cd212cf0..518c30c75 100644 --- a/src/Legacy/Common/Upload.php +++ b/src/Legacy/Common/Upload.php @@ -15,7 +15,12 @@ namespace TorrentPier\Legacy\Common; */ class Upload { - public $cfg = [ + /** + * Default config pattern + * + * @var array + */ + public array $cfg = [ 'max_size' => 0, 'max_width' => 0, 'max_height' => 0, @@ -23,24 +28,56 @@ class Upload 'upload_path' => '', 'up_allowed' => false, ]; - public $file = [ + + /** + * File params pattern + * + * @var array + */ + public array $file = [ 'name' => '', 'type' => '', 'size' => 0, 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, ]; + + /** + * Trash... + * + * TODO: Refactoring + * @var string + */ public $orig_name = ''; - public $file_path = ''; // Stored file path + public $file_path = ''; public $file_ext = ''; public $file_ext_id = ''; - public $file_size = ''; - public $ext_ids = []; // array_flip($bb_cfg['file_id_ext']) - public $errors = []; + + /** + * File size + * + * @var int + */ + public int $file_size = 0; + + /** + * All allowed extensions to upload + * + * @var array + */ + public array $ext_ids = []; + + /** + * Store caught errors while uploading + * + * @var array + */ + public array $errors = []; /** * Image types array * + * @see https://www.php.net/manual/en/image.constants.php * @var array|string[] */ public array $img_types = [ @@ -52,12 +89,14 @@ class Upload ]; /** + * Initialize uploader + * * @param array $cfg * @param array $post_params * @param bool $uploaded_only * @return bool */ - public function init(array $cfg = [], array $post_params = [], $uploaded_only = true) + public function init(array $cfg = [], array $post_params = [], bool $uploaded_only = true): bool { global $bb_cfg, $lang; @@ -77,12 +116,14 @@ class Upload $this->errors[] = $msg; return false; } - // file_exists + + // Check file exists if (!file_exists($this->file['tmp_name'])) { $this->errors[] = "Uploaded file not exists: {$this->file['tmp_name']}"; return false; } - // size + + // Check file size if (!$this->file_size = filesize($this->file['tmp_name'])) { $this->errors[] = "Uploaded file is empty: {$this->file['tmp_name']}"; return false; @@ -91,17 +132,20 @@ class Upload $this->errors[] = sprintf($lang['UPLOAD_ERROR_SIZE'], humn_size($this->cfg['max_size'])); return false; } + // is_uploaded_file if ($uploaded_only && !is_uploaded_file($this->file['tmp_name'])) { $this->errors[] = "Not uploaded file: {$this->file['tmp_name']}"; return false; } - // get ext - $this->ext_ids = array_flip($bb_cfg['file_id_ext']); + + // Got file extension $file_name_ary = explode('.', $this->file['name']); $this->file_ext = strtolower(end($file_name_ary)); - // img + $this->ext_ids = array_flip($bb_cfg['file_id_ext']); + + // Actions for images [E.g. Change avatar] if ($this->cfg['max_width'] || $this->cfg['max_height']) { if ($img_info = getimagesize($this->file['tmp_name'])) { [$width, $height, $type, $attr] = $img_info; @@ -123,7 +167,8 @@ class Upload return false; } } - // check ext + + // Check extension if ($uploaded_only && (!isset($this->ext_ids[$this->file_ext]) || !\in_array($this->file_ext, $this->cfg['allowed_ext'], true))) { $this->errors[] = sprintf($lang['UPLOAD_ERROR_NOT_ALLOWED'], htmlCHR($this->file_ext)); return false; @@ -134,11 +179,13 @@ class Upload } /** + * Store uploaded file + * * @param string $mode * @param array $params * @return bool */ - public function store($mode = '', array $params = []) + public function store(string $mode = '', array $params = []) { if ($mode == 'avatar') { delete_avatar($params['user_id'], $params['avatar_ext_id']); @@ -155,6 +202,8 @@ class Upload } /** + * Move file to target path + * * @param $file_path * @return bool */