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
|
// Debug mode
|
||||||
// Change to FALSE, for prevent warning or errors on browser
|
// Change to FALSE, for prevent warning or errors on browser
|
||||||
define('DEBUG_MODE', TRUE);
|
define('DEBUG_MODE', TRUE);
|
||||||
define('DEBUG_TYPE', 'TRACE'); // INFO, TRACE
|
define('DEBUG_TYPE', 'INFO'); // INFO, TRACE
|
||||||
error_reporting(0); // Turn off all error reporting
|
error_reporting(0); // Turn off all error reporting
|
||||||
if (DEBUG_MODE) {
|
if (DEBUG_MODE) {
|
||||||
// Turn on all error reporting
|
// Turn on all error reporting
|
||||||
|
|
|
@ -95,6 +95,9 @@ define('SESSION_COOKIE_LIFE_TIME', 0);
|
||||||
// Tags, type of pages included in the tag database
|
// Tags, type of pages included in the tag database
|
||||||
define('DB_TAGS_TYPES', array('published','static','sticky'));
|
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
|
// Alert notification dissappear in X seconds
|
||||||
$GLOBALS['ALERT_DISSAPEAR_IN'] = 3; // Seconds
|
$GLOBALS['ALERT_DISSAPEAR_IN'] = 3; // Seconds
|
||||||
|
|
||||||
|
|
|
@ -805,3 +805,36 @@ function ajaxResponse($status=0, $message="", $data=array()) {
|
||||||
$output = array_merge($default, $data);
|
$output = array_merge($default, $data);
|
||||||
exit (json_encode($output));
|
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();
|
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) {
|
public static function nextFilename($path=PATH_UPLOADS, $filename) {
|
||||||
// Clean filename and get extension
|
// Clean filename and get extension
|
||||||
$fileExtension = pathinfo($filename, PATHINFO_EXTENSION);
|
$fileExtension = pathinfo($filename, PATHINFO_EXTENSION);
|
||||||
|
@ -224,4 +231,32 @@ class Filesystem {
|
||||||
}
|
}
|
||||||
return $tmpName;
|
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 .= '<div>';
|
||||||
$html .= '<label>'.$L->get('URL').'</label>';
|
$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>';
|
||||||
|
|
||||||
$html .= '<div>';
|
$html .= '<div>';
|
||||||
|
@ -423,11 +423,19 @@ 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)
|
private function uploadImage($inputs)
|
||||||
{
|
{
|
||||||
global $site;
|
// Where store the image
|
||||||
|
|
||||||
// Where save the image
|
|
||||||
if (isset($inputs['uuid']) && IMAGE_RESTRICT) {
|
if (isset($inputs['uuid']) && IMAGE_RESTRICT) {
|
||||||
$imageDirectory = PATH_UPLOADS_PAGES.$inputs['uuid'].DS;
|
$imageDirectory = PATH_UPLOADS_PAGES.$inputs['uuid'].DS;
|
||||||
$thumbnailDirectory = $imageDirectory.'thumbnails'.DS;
|
$thumbnailDirectory = $imageDirectory.'thumbnails'.DS;
|
||||||
|
@ -440,46 +448,38 @@ class pluginAPI extends Plugin {
|
||||||
$thumbnailEndpoint = DOMAIN_UPLOADS_THUMBNAILS;
|
$thumbnailEndpoint = DOMAIN_UPLOADS_THUMBNAILS;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for errors
|
if (!isset($_FILES['image'])) {
|
||||||
|
return array(
|
||||||
|
'status'=>'1',
|
||||||
|
'message'=>'No image sent.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if ($_FILES['image']['error'] != 0) {
|
if ($_FILES['image']['error'] != 0) {
|
||||||
return array(
|
return array(
|
||||||
'status'=>'1',
|
'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'];
|
// Move from php tmp file to Bludit tmp directory
|
||||||
$allowedExtensions = array('gif', 'png', 'jpg', 'jpeg', 'svg');
|
$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) ) {
|
|
||||||
return array(
|
|
||||||
'status'=>'1',
|
|
||||||
'message'=>'File type is not supported. Allowed types: '.implode(', ',$allowedExtensions)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$image = uploadImage($tmp, $imageDirectory, $thumbnailDirectory);
|
||||||
|
if ($image) {
|
||||||
|
$filename = Filesystem::filename($image);
|
||||||
return array(
|
return array(
|
||||||
'status'=>'0',
|
'status'=>'0',
|
||||||
'message'=>'Image uploaded.',
|
'message'=>'Image uploaded.',
|
||||||
'image'=>$imageEndpoint.$nextFilename,
|
'image'=>$imageEndpoint.$filename,
|
||||||
'thumbnail'=>$thumbnailEndpoint.$nextFilename
|
'thumbnail'=>$thumbnailEndpoint.$filename
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'status'=>'1',
|
||||||
|
'message'=>'Image extension not allowed.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue