From 8ab9af8bb2616fbc53d86bc3bb13a5acda907c06 Mon Sep 17 00:00:00 2001 From: Diego Najar Date: Sun, 7 Apr 2019 20:43:42 +0200 Subject: [PATCH] Improve for upload image, new method for filesystem class --- bl-kernel/boot/init.php | 2 +- bl-kernel/boot/variables.php | 3 + bl-kernel/functions.php | 33 +++++++++++ bl-kernel/helpers/filesystem.class.php | 37 +++++++++++- bl-plugins/api/plugin.php | 80 +++++++++++++------------- 5 files changed, 113 insertions(+), 42 deletions(-) diff --git a/bl-kernel/boot/init.php b/bl-kernel/boot/init.php index 12489adb..df1d3ea4 100644 --- a/bl-kernel/boot/init.php +++ b/bl-kernel/boot/init.php @@ -9,7 +9,7 @@ define('BLUDIT_BUILD', '20190228'); // Debug mode // Change to FALSE, for prevent warning or errors on browser define('DEBUG_MODE', TRUE); -define('DEBUG_TYPE', 'TRACE'); // INFO, TRACE +define('DEBUG_TYPE', 'INFO'); // INFO, TRACE error_reporting(0); // Turn off all error reporting if (DEBUG_MODE) { // Turn on all error reporting diff --git a/bl-kernel/boot/variables.php b/bl-kernel/boot/variables.php index e1786835..6de0f009 100644 --- a/bl-kernel/boot/variables.php +++ b/bl-kernel/boot/variables.php @@ -95,6 +95,9 @@ define('SESSION_COOKIE_LIFE_TIME', 0); // Tags, type of pages included in the tag database define('DB_TAGS_TYPES', array('published','static','sticky')); +// Allowed image extensions +define('ALLOWED_IMG_EXTENSION', array('gif', 'png', 'jpg', 'jpeg', 'svg')); + // Alert notification dissappear in X seconds $GLOBALS['ALERT_DISSAPEAR_IN'] = 3; // Seconds diff --git a/bl-kernel/functions.php b/bl-kernel/functions.php index 45405b71..b8a50f6e 100644 --- a/bl-kernel/functions.php +++ b/bl-kernel/functions.php @@ -804,4 +804,37 @@ function ajaxResponse($status=0, $message="", $data=array()) { $default = array('status'=>$status, 'message'=>$message); $output = array_merge($default, $data); exit (json_encode($output)); +} + +function uploadImage($file, $imageDir, $thumbnailDir) { + global $site; + + // Check image extension + $fileExtension = Filesystem::extension($file); + $fileExtension = Text::lowercase($fileExtension); + if (!in_array($fileExtension, ALLOWED_IMG_EXTENSION) ) { + return false; + } + + // Generate a filename to not overwrite current image if exists + $filename = Filesystem::filename($file); + $nextFilename = Filesystem::nextFilename($imageDir, $filename); + + // Move the image to a proper place and name + $image = $imageDir.$nextFilename; + Filesystem::mv($file, $image); + chmod($image, 0644); + + // Generate Thumbnail + if (!empty($thumbnailDir)) { + if ($fileExtension == 'svg') { + symlink($image, $thumbnailDir.$nextFilename); + } else { + $Image = new Image(); + $Image->setImage($image, $site->thumbnailWidth(), $site->thumbnailHeight(), 'crop'); + $Image->saveImage($thumbnailDir.$nextFilename, $site->thumbnailQuality(), true); + } + } + + return $image; } \ No newline at end of file diff --git a/bl-kernel/helpers/filesystem.class.php b/bl-kernel/helpers/filesystem.class.php index 139c18be..3af90466 100644 --- a/bl-kernel/helpers/filesystem.class.php +++ b/bl-kernel/helpers/filesystem.class.php @@ -203,7 +203,14 @@ class Filesystem { return $zip->close(); } - // Returns the next filename if the filename already exist + /* + | Returns the next filename if the filename already exist otherwise returns the original filename + | + | @path string Path + | @filename string Filename + | + | @return string + */ public static function nextFilename($path=PATH_UPLOADS, $filename) { // Clean filename and get extension $fileExtension = pathinfo($filename, PATHINFO_EXTENSION); @@ -224,4 +231,32 @@ class Filesystem { } return $tmpName; } + + /* + | Returns the filename + | Example: + | @file /home/diego/dog.jpg + | @return dog.jpg + | + | @file string Full path of the file + | + | @return string + */ + public static function filename($file) { + return basename($file); + } + + /* + | Returns the file extension + | Example: + | @file /home/diego/dog.jpg + | @return jpg + | + | @file string Full path of the file + | + | @return string + */ + public static function extension($file) { + return pathinfo($file, PATHINFO_EXTENSION); + } } diff --git a/bl-plugins/api/plugin.php b/bl-plugins/api/plugin.php index 0a736573..25a81bb8 100644 --- a/bl-plugins/api/plugin.php +++ b/bl-plugins/api/plugin.php @@ -30,7 +30,7 @@ class pluginAPI extends Plugin { $html .= '
'; $html .= ''; - $html .= '

'.DOMAIN.'/api/{endpoint}

'; + $html .= '

'.DOMAIN_BASE.'api/{endpoint}

'; $html .= '
'; $html .= '
'; @@ -423,63 +423,63 @@ class pluginAPI extends Plugin { ); } + /* + | Upload an image and generate the thumbnails + | Returns the image and thumbnail URL + | + | @inputs array + | @inputs['uuid'] string Page UUID + | @_FILE array https://www.php.net/manual/en/reserved.variables.files.php + | + | @return array + */ private function uploadImage($inputs) { - global $site; - - // Where save the image + // Where store the image if (isset($inputs['uuid']) && IMAGE_RESTRICT) { - $imageDirectory = PATH_UPLOADS_PAGES.$inputs['uuid'].DS; - $thumbnailDirectory = $imageDirectory.'thumbnails'.DS; - $imageEndpoint = DOMAIN_UPLOADS_PAGES.$inputs['uuid'].'/'; - $thumbnailEndpoint = $imageEndpoint.'thumbnails'.'/'; + $imageDirectory = PATH_UPLOADS_PAGES.$inputs['uuid'].DS; + $thumbnailDirectory = $imageDirectory.'thumbnails'.DS; + $imageEndpoint = DOMAIN_UPLOADS_PAGES.$inputs['uuid'].'/'; + $thumbnailEndpoint = $imageEndpoint.'thumbnails'.'/'; } else { - $imageDirectory = PATH_UPLOADS; - $thumbnailDirectory = PATH_UPLOADS_THUMBNAILS; - $imageEndpoint = DOMAIN_UPLOADS; - $thumbnailEndpoint = DOMAIN_UPLOADS_THUMBNAILS; + $imageDirectory = PATH_UPLOADS; + $thumbnailDirectory = PATH_UPLOADS_THUMBNAILS; + $imageEndpoint = DOMAIN_UPLOADS; + $thumbnailEndpoint = DOMAIN_UPLOADS_THUMBNAILS; + } + + if (!isset($_FILES['image'])) { + return array( + 'status'=>'1', + 'message'=>'No image sent.' + ); } - // Check for errors if ($_FILES['image']['error'] != 0) { return array( 'status'=>'1', - 'message'=>'Maximum load file size allowed: '.ini_get('upload_max_filesize') + 'message'=>'Error uploading the image, maximum load file size allowed: '.ini_get('upload_max_filesize') ); } - $filename = $_FILES['image']['name']; - $allowedExtensions = array('gif', 'png', 'jpg', 'jpeg', 'svg'); + // Move from php tmp file to Bludit tmp directory + $tmp = PATH_TMP.$_FILES['image']['name']; + Filesystem::mv($_FILES['image']['tmp_name'], $tmp); - // File extension - $fileExtension = pathinfo($filename, PATHINFO_EXTENSION); - $fileExtension = Text::lowercase($fileExtension); - if (!in_array($fileExtension, $allowedExtensions) ) { + $image = uploadImage($tmp, $imageDirectory, $thumbnailDirectory); + if ($image) { + $filename = Filesystem::filename($image); return array( - 'status'=>'1', - 'message'=>'File type is not supported. Allowed types: '.implode(', ',$allowedExtensions) + 'status'=>'0', + 'message'=>'Image uploaded.', + 'image'=>$imageEndpoint.$filename, + 'thumbnail'=>$thumbnailEndpoint.$filename ); } - // Filename and move from temporary directory to upload directory - $nextFilename = Filesystem::nextFilename($imageDirectory, $filename); - rename($_FILES['image']['tmp_name'], $imageDirectory.$nextFilename); - chmod($imageDirectory.$nextFilename, 0644); - - // Thumbnail - if ($fileExtension == 'svg') { - symlink($imageDirectory.$nextFilename, $thumbnailDirectory.$nextFilename); - } else { - $Image = new Image(); - $Image->setImage($imageDirectory.$nextFilename, $site->thumbnailWidth(), $site->thumbnailHeight(), 'crop'); - $Image->saveImage($thumbnailDirectory.$nextFilename, $site->thumbnailQuality(), true); - } - return array( - 'status'=>'0', - 'message'=>'Image uploaded.', - 'image'=>$imageEndpoint.$nextFilename, - 'thumbnail'=>$thumbnailEndpoint.$nextFilename + 'status'=>'1', + 'message'=>'Image extension not allowed.' ); }