From 6d48e98ca723d46db2addd2869cd912408dafdc4 Mon Sep 17 00:00:00 2001 From: otengkwame Date: Sat, 4 Jun 2022 11:20:53 +0000 Subject: [PATCH] :sparkles: add file upload functionality to input Signed-off-by: otengkwame --- framework/core/Input.php | 285 ++++++++++++++++++++++++++++++++++----- 1 file changed, 254 insertions(+), 31 deletions(-) diff --git a/framework/core/Input.php b/framework/core/Input.php index 38d7c7d..5648cab 100644 --- a/framework/core/Input.php +++ b/framework/core/Input.php @@ -127,6 +127,24 @@ class CI_Input protected $security; protected $uni; + /** + * File Upload variables + * + * @var + */ + protected $tempfile; + protected $error; + protected $filepath; + protected $filesize; + protected $extension; + protected $originalName; + protected $originalMimeType; + protected $givenName; + protected $rawname; + protected $isUploadedFile = false; + + + // -------------------------------------------------------------------- /** @@ -246,6 +264,50 @@ public function post($index = null, $xss_clean = false) return $this->_fetch_from_array($_POST, $index, $xss_clean); } + /** + * Verify if an item from the POST array exists + * + * @param mixed $index Index for item to be checked from $_POST + * @param bool $xss_clean Whether to apply XSS filtering + * @return bool + */ + public function has($index = null, $xss_clean = false) + { + $exists = $this->_fetch_from_array($_POST, $index, $xss_clean); + + if ($exists) { + return true; + } + + return false; + } + + /** + * Fetch only items from the POST array + * + * @param mixed $indexes Indexes for item to be fetched from $_POST + * @param bool $xss_clean Whether to apply XSS filtering + * @return mixed + */ + public function only(array $indexes = [], $xss_clean = false) + { + return $this->_fetch_from_array($_POST, $indexes, $xss_clean); + } + + /** + * Fetch all except items given form the POST array + * + * @param array $indexes + * @param bool $xss_clean + * @return void + */ + public function except(array $indexes = [], $xss_clean = false) + { + $post = array_diff_key($_POST, array_flip($indexes)); + + return $this->_fetch_from_array($post, null, $xss_clean); + } + // -------------------------------------------------------------------- /** @@ -310,11 +372,11 @@ public function getPost($index, $xss_clean = false) */ public function file($index = '') { - if ($index !== '') { + if (isset($_FILES[$index])) { return $_FILES[$index]; } - return ''; + return []; } /** @@ -334,66 +396,208 @@ public function files() * @param string $file * @return bool */ - public function hasFile($file) + public function hasFile($file = '') { + + if ($file !== '' && isset($_FILES[$file])) { + $file = $_FILES[$file]; + } + return (empty($file['name'])) ? false : true; } + /** + * Is this file uploaded with a POST request? + * + * hard dependency on the `is_uploaded_file` function. + * + * @return bool + */ + public function isUploadedFile($fieldname) + { + $file = $_FILES[$fieldname]; + return is_uploaded_file($file['tmp_name']); + } + + /** + * Verify uploaded file is true + * + * @return bool + */ + public function isValid() + { + return is_uploaded_file($this->tempfile) ? true : false; + } + + /** + * Retrieve all file data for easy manipulation + * + * @param array $file + * @param string $name + * @param string $path + * @return CI_Input + */ + public function filedata($file = [], $name = null, $path = '') + { + if (empty($file)) { + return ''; + } + + $this->tempfile = $file['tmp_name']; + $this->error = $file['error']; + $this->filepath = ($path) ? realpath($path) : realpath(WRITABLEPATH . 'uploads'); + $this->extension = pathinfo($file['name'], PATHINFO_EXTENSION); + $this->originalName = $file['name']; + $this->originalMimeType = $file['type']; + $this->filesize = $file['size']; + $this->rawname = substr($this->originalName, 0, strrpos($this->originalName, '.') - strlen($this->extension)); + + $filename = ''; + + if ($name !== null) { + $filename = $name . '.' . $this->extension; + } + + if ($name === null) { + $filename = random_bytes(2) . str_shuffle('file') . random_bytes(16); + $filename = bin2hex($filename) . '.' . $this->extension; + } + + $this->givenName = $filename; + + return $this; + } + /** * Upload file to a given destination * * @param mixed $file * @param string $path * @param string $name - * @return string + * @return CI_Input */ - public function storeFile($file = [], $path = '', $name = null) + public function upload($file = [], $path = '', $name = null) { if (empty($file)) { return ''; } - $tempfile = $file['tmp_name']; - $filepath = realpath($path); + $this->tempfile = $file['tmp_name']; + $this->filepath = ($path) ? realpath($path) : realpath(WRITABLEPATH.'uploads'); + $this->extension = pathinfo($file['name'], PATHINFO_EXTENSION); + $this->originalName = $file['name']; + $this->originalMimeType = $file['type']; + $this->filesize = $file['size']; + $this->rawname = substr($this->originalName, 0, strrpos($this->originalName, DOT)-strlen($this->extension)); + $filename = ''; if ($name !== null) { + $filename = $name . '.' . $this->extension; + } + + if ($name === null) { + $filename = random_bytes(2) . str_shuffle('file') . random_bytes(16); + $filename = bin2hex($filename) . '.' . $this->extension; + } + + $this->givenName = $filename; + + $this->move($this->filepath, $this->givenName); - $extension = pathinfo($file['name'], PATHINFO_EXTENSION); + return $this; + } - $filename = $name . '.' . $extension; + /** + * Move file from location to destination + * + * @param string $filepath + * @param string $filename + * @return bool + */ + public function move($filepath, $filename) + { + $targetfile = $filepath . DIRECTORY_SEPARATOR . $filename; - $targetfile = $filepath . $file['name']; - $targetfile = $filepath . DIRECTORY_SEPARATOR . $filename; + if (move_uploaded_file($this->tempfile, $targetfile)) { + $this->isUploadedFile = true; + return true; } - if ($name === null) { + return false; + } - $filename = random_bytes(2) . str_shuffle('file') . random_bytes(16); - $filename = bin2hex($filename); + /** + * Upload file size + * + * @return string + */ + public function size() + { + return $this->filesize; + } - $targetfile = $filepath . DIRECTORY_SEPARATOR . $filename; - } - - move_uploaded_file($tempfile, $targetfile); - - return $targetfile; + /** + * Upload file path + * + * @return string + */ + public function path() + { + return $this->filepath; } /** - * Is this file uploaded with a POST request? - * - * hard dependency on the `is_uploaded_file` function. - * - * @return bool - */ - public function isUploadedFile($fieldname) - { - $file = $_FILES[$fieldname]; - return is_uploaded_file($file['tmp_name']); - } + * Upload file extension + * + * @return string + */ + public function extension() + { + return $this->extension; + } + + /** + * Upload file original name + * + * @return string + */ + public function originalName() + { + return $this->originalName; + } + + /** + * Upload file given name + * + * @return string + */ + public function filename() + { + return $this->givenName; + } + + /** + * Upload file rawname + * + * @return string + */ + public function rawname() + { + return $this->rawname; + } + + /** + * Upload file mime type + * + * @return string + */ + public function mimetype() + { + return $this->originalMimeType; + } // -------------------------------------------------------------------- @@ -555,6 +759,25 @@ public function set_cookie($name, $value = '', $expire = 0, $domain = '', $path setcookie($prefix . $name, $value, $setcookie_options); } + /** + * Alias To Method Above + * + * @param string|mixed[] $name Cookie name or an array containing parameters + * @param string $value Cookie value + * @param int $expire Cookie expiration time in seconds + * @param string $domain Cookie domain (e.g.: '.yourdomain.com') + * @param string $path Cookie path (default: '/') + * @param string $prefix Cookie name prefix + * @param bool $secure Whether to only transfer cookies via SSL + * @param bool $httponly Whether to only makes the cookie accessible via HTTP (no javascript) + * @param string|null $samesite The SameSite cookie setting (Possible values: 'Lax', 'Strict', 'None', null, default: null) + * @return void + */ + public function setCookie($name, $value = '', $expire = 0, $domain = '', $path = '/', $prefix = '', $secure = null, $httponly = null, $samesite = null) + { + $this->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure, $httponly, $samesite); + } + // -------------------------------------------------------------------- /**