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

53 lines
1.4 KiB
PHP
Raw Normal View History

2018-12-21 19:45:53 +01:00
<?php defined('BLUDIT') or die('Bludit CMS.');
header('Content-Type: application/json');
2019-05-27 19:41:46 +02:00
/*
| Upload site logo
| The final filename is the site's name and the extension is the same as the file uploaded
|
| @_FILES['inputFile'] multipart/form-data File from form
|
| @return array
*/
2018-12-21 19:45:53 +01:00
if (!isset($_FILES['inputFile'])) {
2019-01-31 20:07:59 +01:00
ajaxResponse(1, 'Error trying to upload the site logo.');
2018-12-21 19:45:53 +01:00
}
// File extension
2019-05-27 19:24:11 +02:00
$fileExtension = Filesystem::extension($_FILES['inputFile']['name']);
$fileExtension = Text::lowercase($fileExtension);
2019-05-29 19:28:11 +02:00
if (!in_array($fileExtension, $GLOBALS['ALLOWED_IMG_EXTENSION']) ) {
$message = 'File type is not supported. Allowed types: '.implode(', ',$GLOBALS['ALLOWED_IMG_EXTENSION']);
2019-05-27 19:41:46 +02:00
Log::set($message, LOG_TYPE_ERROR);
ajaxResponse(1, $message);
2019-05-27 19:24:11 +02:00
}
2018-12-21 19:45:53 +01:00
// Final filename
$filename = 'logo.'.$fileExtension;
2019-01-15 19:42:15 +01:00
if (Text::isNotEmpty( $site->title() )) {
$filename = $site->title().'.'.$fileExtension;
}
2018-12-21 19:45:53 +01:00
// Delete old image
$oldFilename = $site->logo(false);
if ($oldFilename) {
Filesystem::rmfile(PATH_UPLOADS.$oldFilename);
}
// Move from temporary directory to uploads
2019-05-27 19:24:11 +02:00
Filesystem::mv($_FILES['inputFile']['tmp_name'], PATH_UPLOADS.$filename);
2018-12-21 19:45:53 +01:00
// Permissions
chmod(PATH_UPLOADS.$filename, 0644);
// Store the filename in the database
$site->set(array('logo'=>$filename));
2019-01-31 20:07:59 +01:00
ajaxResponse(0, 'Image uploaded.', array(
2018-12-21 19:45:53 +01:00
'filename'=>$filename,
'absoluteURL'=>DOMAIN_UPLOADS.$filename,
'absolutePath'=>PATH_UPLOADS.$filename
2019-01-31 20:07:59 +01:00
));
2018-12-21 19:45:53 +01:00
?>