Improves in AJAX functions

This commit is contained in:
Diego Najar 2019-04-23 23:12:38 +02:00
parent c0bf0f6583
commit f978de490f
3 changed files with 47 additions and 53 deletions

View File

@ -1,12 +1,18 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
header('Content-Type: application/json');
/*
| Delete an image from a particular page
|
| @_POST['filename'] string Name of the file to delete
| @_POST['uuid'] string Page uuid
|
| @return array
*/
// $_POST
// ----------------------------------------------------------------------------
// (string) $_POST['path'] Name of file to delete, just the filename
$filename = isset($_POST['filename']) ? $_POST['filename'] : false;
// (string) $_POST['uuid']
$uuid = empty($_POST['uuid']) ? false : $_POST['uuid'];
// ----------------------------------------------------------------------------

View File

@ -1,16 +1,23 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
header('Content-Type: application/json');
/*
| List filename of image from a particular page
|
| @_POST['pageNumber'] int Page number for the paginator
| @_POST['path'] string Pre-defined name for the directory to read, its pre-defined to avoid security issues
| @_POST['uuid'] string Page uuid
|
| @return array
*/
// $_POST
// ----------------------------------------------------------------------------
// (integer) $_POST['pageNumber'] > 0
// $_POST['pageNumber'] > 0
$pageNumber = empty($_POST['pageNumber']) ? 1 : (int)$_POST['pageNumber'];
$pageNumber = $pageNumber - 1;
// (string) $_POST['path']
$path = empty($_POST['path']) ? false : $_POST['path'];
// (string) $_POST['uuid']
$uuid = empty($_POST['uuid']) ? false : $_POST['uuid'];
// ----------------------------------------------------------------------------
@ -27,7 +34,7 @@ if ($path=='thumbnails') {
// Get all files from the directory $path, also split the array by numberOfItems
// The function listFiles split in chunks
$listOfFilesByPage = Filesystem::listFiles($path, '*', '*', $GLOBALS['MEDIA_MANAGER_SORT_BY_DATE'], $GLOBALS['MEDIA_MANAGER_NUMBER_OF_FILES']);
$listOfFilesByPage = Filesystem::listFiles($path, '*', '*', MEDIA_MANAGER_SORT_BY_DATE, MEDIA_MANAGER_NUMBER_OF_FILES);
// Check if the page number exists in the chunks
if (isset($listOfFilesByPage[$pageNumber])) {

View File

@ -1,39 +1,32 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
header('Content-Type: application/json');
/*
| Upload an image to a particular page
|
| @_POST['uuid'] string Page uuid
|
| @return array
*/
// $_POST
// ----------------------------------------------------------------------------
// (string) $_POST['uuid']
$uuid = empty($_POST['uuid']) ? false : $_POST['uuid'];
// ----------------------------------------------------------------------------
// Set upload directory
if ($uuid && IMAGE_RESTRICT) {
$uploadDirectory = PATH_UPLOADS_PAGES.$uuid.DS;
$thumbnailDirectory = $uploadDirectory.'thumbnails'.DS;
$imageDirectory = PATH_UPLOADS_PAGES.$uuid.DS;
$thumbnailDirectory = $imageDirectory.'thumbnails'.DS;
} else {
$uploadDirectory = PATH_UPLOADS;
$imageDirectory = PATH_UPLOADS;
$thumbnailDirectory = PATH_UPLOADS_THUMBNAILS;
}
// Create directory for images
if (!is_dir($uploadDirectory)){
Filesystem::mkdir($uploadDirectory, true);
}
// Create directory for thumbnails
if (!is_dir($thumbnailDirectory)){
Filesystem::mkdir($thumbnailDirectory, true);
}
// File extensions allowed
$allowedExtensions = array('gif', 'png', 'jpg', 'jpeg', 'svg');
// Upload all images
foreach ($_FILES['bluditInputFiles']['name'] as $key=>$filename) {
$images = array();
foreach ($_FILES['images']['name'] as $uuid=>$filename) {
// Check for errors
if ($_FILES['bluditInputFiles']['error'][$key] != 0) {
if ($_FILES['images']['error'][$uuid] != 0) {
$message = $L->g('Maximum load file size allowed:').' '.ini_get('upload_max_filesize');
Log::set($message, LOG_TYPE_ERROR);
ajaxResponse(1, $message);
@ -42,35 +35,23 @@ foreach ($_FILES['bluditInputFiles']['name'] as $key=>$filename) {
// Convert URL characters such as spaces or quotes to characters
$filename = urldecode($filename);
// Check file extension
$fileExtension = pathinfo($filename, PATHINFO_EXTENSION);
$fileExtension = Text::lowercase($fileExtension);
if (!in_array($fileExtension, $allowedExtensions) ) {
$message = $L->g('File type is not supported. Allowed types:').' '.implode(', ',$allowedExtensions);
// Move from PHP tmp file to Bludit tmp directory
Filesystem::mv($_FILES['images']['tmp_name'][$uuid], PATH_TMP.$filename);
// Transform the image and generate the thumbnail
$image = transformImage(PATH_TMP.$filename, $imageDirectory, $thumbnailDirectory);
if ($image) {
$filename = Filesystem::filename($image);
array_push($images, $filename);
} else {
$message = $L->g('File type is not supported. Allowed types:').' '.implode(', ',ALLOWED_IMG_EXTENSION);
Log::set($message, LOG_TYPE_ERROR);
ajaxResponse(1, $message);
}
// Generate the next filename to not overwrite the original file
$nextFilename = Filesystem::nextFilename($uploadDirectory, $filename);
// Move from temporary directory to uploads folder
rename($_FILES['bluditInputFiles']['tmp_name'][$key], $uploadDirectory.$nextFilename);
chmod($uploadDirectory.$nextFilename, 0644);
// Generate Thumbnail
// Exclude generate thumbnail for SVG format and generate a symlink to the svg
if ($fileExtension == 'svg') {
symlink($uploadDirectory.$nextFilename, $thumbnailDirectory.$nextFilename);
} else {
$Image = new Image();
$Image->setImage($uploadDirectory.$nextFilename, $site->thumbnailWidth(), $site->thumbnailHeight(), 'crop');
$Image->saveImage($thumbnailDirectory.$nextFilename, $site->thumbnailQuality(), true);
}
}
ajaxResponse(0, 'List of files and number of chunks.', array(
'filename'=>$nextFilename
ajaxResponse(0, 'Images uploaded.', array(
'images'=>$images
));
?>