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()) {
echo '<li><a href="'.Paginator::prevPageUrl().'" class="previous"><i class="fa fa-arrow-circle-o-left"></i> Previous</a></li>';
} 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++) {
@ -93,7 +93,7 @@ echo '
// Show next page link
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 {
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.');
header('Content-Type: application/json');
// Request $_POST
// $filename: Name of file to delete, just the filename
// $_POST
// (string) $filename: Name of file to delete, just the filename
$filename = isset($_POST['filename']) ? $_POST['filename'] : '';
if( empty($filename) ) {
echo json_encode( array('status'=>1, 'msg'=>'The filename is empty.') );
exit;
if (Text::isEmpty($filename)) {
exit (json_encode(array(
'status'=>1,
'message'=>'The filename is empty.'
)));
}
// Check if the filename exist and Sanitize::pathFile it's necesary for security reasons.
if( Sanitize::pathFile(PATH_UPLOADS.$filename) ) {
// Delete the file.
// Check if the filename exist
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 thumnails.
// Check if the file has a thumbnail
if (Sanitize::pathFile(PATH_UPLOADS_THUMBNAILS.$filename)) {
// Delete the file
Filesystem::rmfile(PATH_UPLOADS_THUMBNAILS.$filename);
echo json_encode( array('status'=>0, 'msg'=>'The file was deleted.') );
exit;
}
exit (json_encode(array(
'status'=>1,
'msg'=>'The file does not exist.'
'status'=>0,
'message'=>'File deleted.'
)));
?>

View File

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

View File

@ -1,5 +1,4 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
header('Content-Type: application/json');
// Type
@ -8,10 +7,7 @@ if(!empty($_POST['type'])) {
$type = Sanitize::html($_POST['type']);
}
// Source.
$source = $_FILES['files']['tmp_name'][0];
// Filename and extension.
// Filename and extension
$filename = Text::lowercase($_FILES['files']['name'][0]);
$fileExtension = pathinfo($filename, PATHINFO_EXTENSION);
$filename = pathinfo($filename, PATHINFO_FILENAME);
@ -19,67 +15,85 @@ $filename = Text::replace(' ', '', $filename);
$filename = Text::replace('_', '', $filename);
// Check extension
$validExtension = array('tiff', 'gif', 'png', 'jpg', 'jpeg', 'bmp');
$validExtension = array('tiff', 'gif', 'png', 'jpg', 'jpeg', 'bmp', 'svg');
if (!in_array($fileExtension, $validExtension)) {
$validExtensionString = implode(',', $validExtension);
exit (json_encode(array(
'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;
if( file_exists(PATH_UPLOADS.$tmpName) )
{
if (Sanitize::pathFile(PATH_UPLOADS.$tmpName)) {
$number = 0;
$tmpName = $filename.'_'.$number.'.'.$fileExtension;
while(file_exists(PATH_UPLOADS.$tmpName)) {
while (Sanitize::pathFile(PATH_UPLOADS.$tmpName)) {
$number++;
$tmpName = $filename.'_'.$number.'.'.$fileExtension;
}
}
// Move from temporary PHP folder to temporary Bludit folder.
move_uploaded_file($source, PATH_TMP.'original'.'.'.$fileExtension);
// Move from temporary PHP folder to temporary Bludit folder
$originalFile = PATH_TMP.'original'.'.'.$fileExtension;
move_uploaded_file($_FILES['files']['tmp_name'][0], $originalFile);
// Returned variables
$absoluteURL = '';
$absoluteURLThumbnail = '';
$absolutePath = '';
// --- PROFILE PICTURE ---
if($type=='profilePicture')
{
// Resize and crop profile image.
if ($type=='profilePicture') {
// Resize and crop profile image
$username = Sanitize::html($_POST['username']);
$tmpName = $username.'.png';
$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);
// Paths
$absoluteURL = DOMAIN_UPLOADS_PROFILES.$tmpName;
$absoluteURLThumbnail = '';
$absolutePath = PATH_UPLOADS_PROFILES.$tmpName;
}
// --- OTHERS ---
else {
// Exclude generate thumbnail for SVG format
if (strcasecmp($fileExtension, 'svg')!=0) {
// Generate the thumbnail
$Image = new Image();
//Handling all other formats than svg
if (strcasecmp($fileExtension, 'svg') != 0) {
$Image->setImage(PATH_TMP.'original'.'.'.$fileExtension, THUMBNAILS_WIDTH, THUMBNAILS_HEIGHT, 'crop');
$Image->setImage($originalFile, THUMBNAILS_WIDTH, THUMBNAILS_HEIGHT, 'crop');
$Image->saveImage(PATH_UPLOADS_THUMBNAILS.$tmpName, THUMBNAILS_QUALITY, true);
}
// Move the original to the upload folder.
rename(PATH_TMP.'original'.'.'.$fileExtension, PATH_UPLOADS.$tmpName);
// Move the original to the upload folder
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) {
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.
if(file_exists(PATH_TMP.'original'.'.'.$fileExtension)) {
unlink(PATH_TMP.'original'.'.'.$fileExtension);
// Remove the Bludit temporary file
if (Sanitize::pathFile($originalFile)) {
unlink($originalFile);
}
exit (json_encode(array(
'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?",
"pagebreak": "Page break",
"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"
}