bludit/bl-kernel/ajax/uploader.php

99 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2015-11-15 22:37:34 +01:00
<?php defined('BLUDIT') or die('Bludit CMS.');
header('Content-Type: application/json');
// Type
2015-11-17 03:45:03 +01:00
$type = 'other';
2017-10-06 18:28:06 +02:00
if (!empty($_POST['type'])) {
2015-11-17 03:45:03 +01:00
$type = Sanitize::html($_POST['type']);
}
2015-11-04 01:28:11 +01:00
2017-10-06 18:28:06 +02:00
// Filename and extension
2015-11-04 01:28:11 +01:00
$filename = Text::lowercase($_FILES['files']['name'][0]);
$fileExtension = pathinfo($filename, PATHINFO_EXTENSION);
$filename = pathinfo($filename, PATHINFO_FILENAME);
$filename = Text::replace(' ', '', $filename);
$filename = Text::replace('_', '', $filename);
2016-09-26 04:30:06 +02:00
// Check extension
2017-10-06 18:28:06 +02:00
$validExtension = array('tiff', 'gif', 'png', 'jpg', 'jpeg', 'bmp', 'svg');
if (!in_array($fileExtension, $validExtension)) {
$validExtensionString = implode(',', $validExtension);
exit (json_encode(array(
2016-09-26 04:30:06 +02:00
'status'=>1,
2017-10-06 18:28:06 +02:00
'message'=>'Invalid extension file. Supported extensions:'.$validExtensionString
2016-09-26 04:30:06 +02:00
)));
}
2017-10-06 18:28:06 +02:00
// Generate the next filename if the filename already exist
$tmpName = $filename.'.'.$fileExtension;
2017-10-06 18:28:06 +02:00
if (Sanitize::pathFile(PATH_UPLOADS.$tmpName)) {
2015-11-04 01:28:11 +01:00
$number = 0;
$tmpName = $filename.'_'.$number.'.'.$fileExtension;
2017-10-06 18:28:06 +02:00
while (Sanitize::pathFile(PATH_UPLOADS.$tmpName)) {
2015-11-04 01:28:11 +01:00
$number++;
$tmpName = $filename.'_'.$number.'.'.$fileExtension;
}
}
2017-10-06 18:28:06 +02:00
// 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 = '';
2015-11-04 01:28:11 +01:00
2015-11-16 00:04:54 +01:00
// --- PROFILE PICTURE ---
2017-10-06 18:28:06 +02:00
if ($type=='profilePicture') {
// Resize and crop profile image
2015-11-20 05:00:52 +01:00
$username = Sanitize::html($_POST['username']);
2016-01-02 23:51:12 +01:00
$tmpName = $username.'.png';
2015-11-16 00:04:54 +01:00
$Image = new Image();
2017-10-06 18:28:06 +02:00
$Image->setImage($originalFile, PROFILE_IMG_WIDTH, PROFILE_IMG_HEIGHT, 'crop');
2016-06-03 02:57:13 +02:00
$Image->saveImage(PATH_UPLOADS_PROFILES.$tmpName, PROFILE_IMG_QUALITY, false, true);
2017-10-06 18:28:06 +02:00
// Paths
$absoluteURL = DOMAIN_UPLOADS_PROFILES.$tmpName;
$absoluteURLThumbnail = '';
$absolutePath = PATH_UPLOADS_PROFILES.$tmpName;
2015-11-15 22:37:34 +01:00
}
2015-11-16 00:04:54 +01:00
// --- OTHERS ---
2015-11-15 22:37:34 +01:00
else {
2017-10-06 18:28:06 +02:00
// Exclude generate thumbnail for SVG format
if (strcasecmp($fileExtension, 'svg')!=0) {
// Generate the thumbnail
$Image = new Image();
$Image->setImage($originalFile, THUMBNAILS_WIDTH, THUMBNAILS_HEIGHT, 'crop');
$Image->saveImage(PATH_UPLOADS_THUMBNAILS.$tmpName, THUMBNAILS_QUALITY, true);
}
2017-10-06 18:28:06 +02:00
// Move the original to the upload folder
rename($originalFile, PATH_UPLOADS.$tmpName);
2016-09-26 04:30:06 +02:00
2017-10-06 18:28:06 +02:00
// 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);
}
2017-10-06 18:28:06 +02:00
// Paths
$absoluteURL = DOMAIN_UPLOADS.$tmpName;
$absoluteURLThumbnail = DOMAIN_UPLOADS_THUMBNAILS.$tmpName;
$absolutePath = PATH_UPLOADS.$tmpName;
}
2017-10-06 18:28:06 +02:00
// Remove the Bludit temporary file
if (Sanitize::pathFile($originalFile)) {
unlink($originalFile);
2015-11-15 22:37:34 +01:00
}
2015-11-04 01:28:11 +01:00
2017-10-06 18:28:06 +02:00
exit (json_encode(array(
2015-11-04 01:28:11 +01:00
'status'=>0,
2017-10-06 18:28:06 +02:00
'message'=>'Image uploaded success.',
'filename'=>$tmpName,
'absoluteURL'=>$absoluteURL,
'absoluteURLThumbnail'=>$absoluteURLThumbnail,
'absolutePath'=>$absolutePath
2015-11-04 01:28:11 +01:00
)));
2016-09-26 04:30:06 +02:00
?>