Ajax files improves

This commit is contained in:
Diego Najar 2017-10-06 18:28:06 +02:00
parent 4f21defa47
commit 5e7915d2bf
5 changed files with 81 additions and 64 deletions

View File

@ -84,7 +84,7 @@ echo '
if(Paginator::showPrev()) { if(Paginator::showPrev()) {
echo '<li><a href="'.Paginator::prevPageUrl().'" class="previous"><i class="fa fa-arrow-circle-o-left"></i> Previous</a></li>'; echo '<li><a href="'.Paginator::prevPageUrl().'" class="previous"><i class="fa fa-arrow-circle-o-left"></i> Previous</a></li>';
} else { } else {
echo '<li class="disabled"><i class="fa fa-arrow-circle-o-left"></i> Previous</li>'; echo '<li class="disabled"><i class="fa fa-arrow-circle-o-left"></i> '.$Language->g('Previous').'</li>';
} }
for($i=1; $i<=Paginator::amountOfPages(); $i++) { for($i=1; $i<=Paginator::amountOfPages(); $i++) {
@ -93,7 +93,7 @@ echo '
// Show next page link // Show next page link
if(Paginator::showNext()) { if(Paginator::showNext()) {
echo '<li><a href="'.Paginator::nextPageUrl().'" class="next">Next <i class="fa fa-arrow-circle-o-right"></i></a></li>'; echo '<li><a href="'.Paginator::nextPageUrl().'" class="next">'.$Language->g('Next').' <i class="fa fa-arrow-circle-o-right"></i></a></li>';
} else { } else {
echo '<li class="disabled">Next <i class="fa fa-arrow-circle-o-right"></i></li>'; echo '<li class="disabled">Next <i class="fa fa-arrow-circle-o-right"></i></li>';
} }

View File

@ -1,34 +1,36 @@
<?php defined('BLUDIT') or die('Bludit CMS.'); <?php defined('BLUDIT') or die('Bludit CMS.');
header('Content-Type: application/json'); header('Content-Type: application/json');
// Request $_POST // $_POST
// $filename: Name of file to delete, just the filename // (string) $filename: Name of file to delete, just the filename
$filename = isset($_POST['filename']) ? $_POST['filename'] : ''; $filename = isset($_POST['filename']) ? $_POST['filename'] : '';
if (Text::isEmpty($filename)) {
if( empty($filename) ) { exit (json_encode(array(
echo json_encode( array('status'=>1, 'msg'=>'The filename is empty.') ); 'status'=>1,
exit; 'message'=>'The filename is empty.'
)));
} }
// Check if the filename exist and Sanitize::pathFile it's necesary for security reasons. // Check if the filename exist
if( Sanitize::pathFile(PATH_UPLOADS.$filename) ) { if (!Sanitize::pathFile(PATH_UPLOADS.$filename)) {
exit (json_encode(array(
'status'=>1,
'message'=>'The file does not exist.'
)));
}
// Delete the file
Filesystem::rmfile(PATH_UPLOADS.$filename);
// Delete the file. // Check if the file has a thumbnail
Filesystem::rmfile(PATH_UPLOADS.$filename); if (Sanitize::pathFile(PATH_UPLOADS_THUMBNAILS.$filename)) {
// Delete the file
// Delete the thumnails.
Filesystem::rmfile(PATH_UPLOADS_THUMBNAILS.$filename); Filesystem::rmfile(PATH_UPLOADS_THUMBNAILS.$filename);
echo json_encode( array('status'=>0, 'msg'=>'The file was deleted.') );
exit;
} }
exit(json_encode(array( exit (json_encode(array(
'status'=>1, 'status'=>0,
'msg'=>'The file does not exist.' 'message'=>'File deleted.'
))); )));
?> ?>

View File

@ -7,11 +7,9 @@ $oldKey = isset($_POST['currentKey']) ? $_POST['currentKey'] : '';
$slug = $dbPages->generateKey($text, $parent, $returnSlug=true, $oldKey); $slug = $dbPages->generateKey($text, $parent, $returnSlug=true, $oldKey);
exit(json_encode( exit (json_encode(array(
array( 'status'=>0,
'status'=>0, 'slug'=>$slug
'slug'=>$slug )));
)
));
?> ?>

View File

@ -1,17 +1,13 @@
<?php defined('BLUDIT') or die('Bludit CMS.'); <?php defined('BLUDIT') or die('Bludit CMS.');
header('Content-Type: application/json'); header('Content-Type: application/json');
// Type // Type
$type = 'other'; $type = 'other';
if(!empty($_POST['type'])) { if (!empty($_POST['type'])) {
$type = Sanitize::html($_POST['type']); $type = Sanitize::html($_POST['type']);
} }
// Source. // Filename and extension
$source = $_FILES['files']['tmp_name'][0];
// Filename and extension.
$filename = Text::lowercase($_FILES['files']['name'][0]); $filename = Text::lowercase($_FILES['files']['name'][0]);
$fileExtension = pathinfo($filename, PATHINFO_EXTENSION); $fileExtension = pathinfo($filename, PATHINFO_EXTENSION);
$filename = pathinfo($filename, PATHINFO_FILENAME); $filename = pathinfo($filename, PATHINFO_FILENAME);
@ -19,67 +15,85 @@ $filename = Text::replace(' ', '', $filename);
$filename = Text::replace('_', '', $filename); $filename = Text::replace('_', '', $filename);
// Check extension // Check extension
$validExtension = array('tiff', 'gif', 'png', 'jpg', 'jpeg', 'bmp'); $validExtension = array('tiff', 'gif', 'png', 'jpg', 'jpeg', 'bmp', 'svg');
if( !in_array($fileExtension, $validExtension) ) { if (!in_array($fileExtension, $validExtension)) {
exit(json_encode(array( $validExtensionString = implode(',', $validExtension);
exit (json_encode(array(
'status'=>1, 'status'=>1,
'msg'=>'Invalid extension file.' 'message'=>'Invalid extension file. Supported extensions:'.$validExtensionString
))); )));
} }
// Generate the next filename if the filename already exist. // Generate the next filename if the filename already exist
$tmpName = $filename.'.'.$fileExtension; $tmpName = $filename.'.'.$fileExtension;
if( file_exists(PATH_UPLOADS.$tmpName) ) if (Sanitize::pathFile(PATH_UPLOADS.$tmpName)) {
{
$number = 0; $number = 0;
$tmpName = $filename.'_'.$number.'.'.$fileExtension; $tmpName = $filename.'_'.$number.'.'.$fileExtension;
while(file_exists(PATH_UPLOADS.$tmpName)) { while (Sanitize::pathFile(PATH_UPLOADS.$tmpName)) {
$number++; $number++;
$tmpName = $filename.'_'.$number.'.'.$fileExtension; $tmpName = $filename.'_'.$number.'.'.$fileExtension;
} }
} }
// Move from temporary PHP folder to temporary Bludit folder. // Move from temporary PHP folder to temporary Bludit folder
move_uploaded_file($source, PATH_TMP.'original'.'.'.$fileExtension); $originalFile = PATH_TMP.'original'.'.'.$fileExtension;
move_uploaded_file($_FILES['files']['tmp_name'][0], $originalFile);
// Returned variables
$absoluteURL = '';
$absoluteURLThumbnail = '';
$absolutePath = '';
// --- PROFILE PICTURE --- // --- PROFILE PICTURE ---
if($type=='profilePicture') if ($type=='profilePicture') {
{ // Resize and crop profile image
// Resize and crop profile image.
$username = Sanitize::html($_POST['username']); $username = Sanitize::html($_POST['username']);
$tmpName = $username.'.png'; $tmpName = $username.'.png';
$Image = new Image(); $Image = new Image();
$Image->setImage(PATH_TMP.'original'.'.'.$fileExtension, PROFILE_IMG_WIDTH, PROFILE_IMG_HEIGHT, 'crop'); $Image->setImage($originalFile, PROFILE_IMG_WIDTH, PROFILE_IMG_HEIGHT, 'crop');
$Image->saveImage(PATH_UPLOADS_PROFILES.$tmpName, PROFILE_IMG_QUALITY, false, true); $Image->saveImage(PATH_UPLOADS_PROFILES.$tmpName, PROFILE_IMG_QUALITY, false, true);
// Paths
$absoluteURL = DOMAIN_UPLOADS_PROFILES.$tmpName;
$absoluteURLThumbnail = '';
$absolutePath = PATH_UPLOADS_PROFILES.$tmpName;
} }
// --- OTHERS --- // --- OTHERS ---
else { else {
// Generate the thumbnail // Exclude generate thumbnail for SVG format
$Image = new Image(); if (strcasecmp($fileExtension, 'svg')!=0) {
// Generate the thumbnail
//Handling all other formats than svg $Image = new Image();
if (strcasecmp($fileExtension, 'svg') != 0) { $Image->setImage($originalFile, THUMBNAILS_WIDTH, THUMBNAILS_HEIGHT, 'crop');
$Image->setImage(PATH_TMP.'original'.'.'.$fileExtension, THUMBNAILS_WIDTH, THUMBNAILS_HEIGHT, 'crop');
$Image->saveImage(PATH_UPLOADS_THUMBNAILS.$tmpName, THUMBNAILS_QUALITY, true); $Image->saveImage(PATH_UPLOADS_THUMBNAILS.$tmpName, THUMBNAILS_QUALITY, true);
} }
// Move the original to the upload folder. // Move the original to the upload folder
rename(PATH_TMP.'original'.'.'.$fileExtension, PATH_UPLOADS.$tmpName); rename($originalFile, PATH_UPLOADS.$tmpName);
//If it is a svg file, just save a copy in thumbnail-folder // Generate a link to the SVG file and save on thumbnails folder
if (strcasecmp($fileExtension, 'svg') == 0) { if (strcasecmp($fileExtension, 'svg')==0) {
symlink(PATH_UPLOADS.$tmpName, PATH_UPLOADS_THUMBNAILS.$tmpName); symlink(PATH_UPLOADS.$tmpName, PATH_UPLOADS_THUMBNAILS.$tmpName);
} }
// Paths
$absoluteURL = DOMAIN_UPLOADS.$tmpName;
$absoluteURLThumbnail = DOMAIN_UPLOADS_THUMBNAILS.$tmpName;
$absolutePath = PATH_UPLOADS.$tmpName;
} }
// Remove the Bludit temporary file. // Remove the Bludit temporary file
if(file_exists(PATH_TMP.'original'.'.'.$fileExtension)) { if (Sanitize::pathFile($originalFile)) {
unlink(PATH_TMP.'original'.'.'.$fileExtension); unlink($originalFile);
} }
exit(json_encode(array( exit (json_encode(array(
'status'=>0, 'status'=>0,
'filename'=>$tmpName 'message'=>'Image uploaded success.',
'filename'=>$tmpName,
'absoluteURL'=>$absoluteURL,
'absoluteURLThumbnail'=>$absoluteURLThumbnail,
'absolutePath'=>$absolutePath
))); )));
?> ?>

View File

@ -226,5 +226,8 @@
"have-you-seen-my-ball": "Have you seen my ball?", "have-you-seen-my-ball": "Have you seen my ball?",
"pagebreak": "Page break", "pagebreak": "Page break",
"pages": "Pages", "pages": "Pages",
"this-plugin-may-not-be-supported-by-this-version-of-bludit": "This plugin may not be supported by this version of Bludit" "this-plugin-may-not-be-supported-by-this-version-of-bludit": "This plugin may not be supported by this version of Bludit",
"previous": "Previous",
"previous-page": "Previous page",
"next-page": "Next page"
} }