Improve for upload image, new method for filesystem class
This commit is contained in:
parent
27917f3f6e
commit
8ab9af8bb2
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -805,3 +805,36 @@ function ajaxResponse($status=0, $message="", $data=array()) {
|
|||
$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;
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ class pluginAPI extends Plugin {
|
|||
|
||||
$html .= '<div>';
|
||||
$html .= '<label>'.$L->get('URL').'</label>';
|
||||
$html .= '<p class="text-muted">'.DOMAIN.'/api/{endpoint}</p>';
|
||||
$html .= '<p class="text-muted">'.DOMAIN_BASE.'api/{endpoint}</p>';
|
||||
$html .= '</div>';
|
||||
|
||||
$html .= '<div>';
|
||||
|
@ -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.'
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue