bludit/bl-kernel/ajax/upload-images.php

80 lines
2.2 KiB
PHP
Raw Normal View History

<?php defined('BLUDIT') or die('Bludit CMS.');
header('Content-Type: application/json');
2019-04-23 23:12:38 +02:00
/*
| Upload an image to a particular page
|
| @_POST['uuid'] string Page uuid
|
| @return array
*/
// $_POST
// ----------------------------------------------------------------------------
$uuid = empty($_POST['uuid']) ? false : $_POST['uuid'];
// ----------------------------------------------------------------------------
2019-09-08 10:45:56 +02:00
// Check path traversal on $uuid
if ($uuid) {
if (Text::stringContains($uuid, DS, false)) {
$message = 'Path traversal detected.';
Log::set($message, LOG_TYPE_ERROR);
ajaxResponse(1, $message);
}
}
// Set upload directory
if ($uuid && IMAGE_RESTRICT) {
2019-04-23 23:12:38 +02:00
$imageDirectory = PATH_UPLOADS_PAGES.$uuid.DS;
$thumbnailDirectory = $imageDirectory.'thumbnails'.DS;
if (!Filesystem::directoryExists($thumbnailDirectory)) {
Filesystem::mkdir($thumbnailDirectory, true);
}
} else {
2019-04-23 23:12:38 +02:00
$imageDirectory = PATH_UPLOADS;
$thumbnailDirectory = PATH_UPLOADS_THUMBNAILS;
}
2019-04-23 23:12:38 +02:00
$images = array();
foreach ($_FILES['images']['name'] as $uuid=>$filename) {
// Check for errors
2019-04-23 23:12:38 +02:00
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);
}
// Convert URL characters such as spaces or quotes to characters
$filename = urldecode($filename);
2019-09-08 10:45:56 +02:00
// Check path traversal on $filename
2019-09-05 23:10:39 +02:00
if (Text::stringContains($filename, DS, false)) {
$message = 'Path traversal detected.';
Log::set($message, LOG_TYPE_ERROR);
ajaxResponse(1, $message);
}
2019-04-23 23:12:38 +02:00
// Move from PHP tmp file to Bludit tmp directory
Filesystem::mv($_FILES['images']['tmp_name'][$uuid], PATH_TMP.$filename);
2019-04-23 23:12:38 +02:00
// Transform the image and generate the thumbnail
$image = transformImage(PATH_TMP.$filename, $imageDirectory, $thumbnailDirectory);
2019-09-05 23:10:39 +02:00
// Delete temporary file
Filesystem::rmfile(PATH_TMP.$filename);
2019-04-23 23:12:38 +02:00
if ($image) {
$filename = Filesystem::filename($image);
array_push($images, $filename);
} else {
2019-05-29 19:28:11 +02:00
$message = $L->g('File type is not supported. Allowed types:').' '.implode(', ',$GLOBALS['ALLOWED_IMG_EXTENSION']);
2019-04-23 23:12:38 +02:00
Log::set($message, LOG_TYPE_ERROR);
ajaxResponse(1, $message);
}
}
2019-04-23 23:12:38 +02:00
ajaxResponse(0, 'Images uploaded.', array(
'images'=>$images
));
?>