2018-10-08 14:29:18 +02:00
|
|
|
<?php
|
|
|
|
define('MSZ_USER_BACKGROUND_FORMAT', '%d.msz');
|
|
|
|
|
|
|
|
// attachment and attributes are to be stored in the same byte
|
|
|
|
// left half is for attributes, right half is for attachments
|
|
|
|
// this makes for 16 possible attachments and 4 possible attributes
|
|
|
|
// since attachments are just an incrementing number and attrs are flags
|
|
|
|
|
|
|
|
define('MSZ_USER_BACKGROUND_ATTACHMENT_NONE', 0);
|
|
|
|
define('MSZ_USER_BACKGROUND_ATTACHMENT_COVER', 1);
|
|
|
|
define('MSZ_USER_BACKGROUND_ATTACHMENT_STRETCH', 2);
|
|
|
|
define('MSZ_USER_BACKGROUND_ATTACHMENT_TILE', 3);
|
2018-10-27 20:50:34 +02:00
|
|
|
define('MSZ_USER_BACKGROUND_ATTACHMENT_CONTAIN', 4);
|
2018-10-08 14:29:18 +02:00
|
|
|
|
|
|
|
define('MSZ_USER_BACKGROUND_ATTACHMENTS', [
|
|
|
|
MSZ_USER_BACKGROUND_ATTACHMENT_NONE,
|
|
|
|
MSZ_USER_BACKGROUND_ATTACHMENT_COVER,
|
|
|
|
MSZ_USER_BACKGROUND_ATTACHMENT_STRETCH,
|
|
|
|
MSZ_USER_BACKGROUND_ATTACHMENT_TILE,
|
2018-10-27 20:50:34 +02:00
|
|
|
MSZ_USER_BACKGROUND_ATTACHMENT_CONTAIN,
|
2018-10-08 14:29:18 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
define('MSZ_USER_BACKGROUND_ATTACHMENTS_NAMES', [
|
|
|
|
MSZ_USER_BACKGROUND_ATTACHMENT_COVER => 'cover',
|
|
|
|
MSZ_USER_BACKGROUND_ATTACHMENT_STRETCH => 'stretch',
|
|
|
|
MSZ_USER_BACKGROUND_ATTACHMENT_TILE => 'tile',
|
2018-10-27 20:50:34 +02:00
|
|
|
MSZ_USER_BACKGROUND_ATTACHMENT_CONTAIN => 'contain',
|
2018-10-08 14:29:18 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
define('MSZ_USER_BACKGROUND_ATTRIBUTE_BLEND', 0x10);
|
|
|
|
define('MSZ_USER_BACKGROUND_ATTRIBUTE_SLIDE', 0x20);
|
|
|
|
|
|
|
|
define('MSZ_USER_BACKGROUND_ATTRIBUTES', [
|
|
|
|
MSZ_USER_BACKGROUND_ATTRIBUTE_BLEND,
|
|
|
|
MSZ_USER_BACKGROUND_ATTRIBUTE_SLIDE,
|
|
|
|
]);
|
|
|
|
|
|
|
|
define('MSZ_USER_BACKGROUND_ATTRIBUTES_NAMES', [
|
|
|
|
MSZ_USER_BACKGROUND_ATTRIBUTE_BLEND => 'blend',
|
|
|
|
MSZ_USER_BACKGROUND_ATTRIBUTE_SLIDE => 'slide',
|
|
|
|
]);
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
function user_background_settings_strings(int $settings, string $format = '%s'): array {
|
2018-10-08 14:29:18 +02:00
|
|
|
$arr = [];
|
|
|
|
|
|
|
|
$attachment = $settings & 0x0F;
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
if(array_key_exists($attachment, MSZ_USER_BACKGROUND_ATTACHMENTS_NAMES)) {
|
2018-10-08 14:29:18 +02:00
|
|
|
$arr[] = sprintf($format, MSZ_USER_BACKGROUND_ATTACHMENTS_NAMES[$attachment]);
|
|
|
|
}
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
foreach(MSZ_USER_BACKGROUND_ATTRIBUTES_NAMES as $flag => $name) {
|
|
|
|
if(($settings & $flag) > 0) {
|
2018-10-08 14:29:18 +02:00
|
|
|
$arr[] = sprintf($format, $name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arr;
|
|
|
|
}
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
function user_background_set_settings(int $userId, int $settings): void {
|
|
|
|
if($userId < 1) {
|
2018-10-08 14:29:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$setAttrs = db_prepare('
|
|
|
|
UPDATE `msz_users`
|
|
|
|
SET `user_background_settings` = :settings
|
|
|
|
WHERE `user_id` = :user
|
|
|
|
');
|
|
|
|
$setAttrs->bindValue('settings', $settings & 0xFF);
|
|
|
|
$setAttrs->bindValue('user', $userId);
|
|
|
|
$setAttrs->execute();
|
|
|
|
}
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
function user_background_delete(int $userId): void {
|
2018-10-08 14:29:18 +02:00
|
|
|
$backgroundFileName = sprintf(MSZ_USER_BACKGROUND_FORMAT, $userId);
|
2019-02-05 21:29:37 +01:00
|
|
|
safe_delete(MSZ_STORAGE . '/backgrounds/original/' . $backgroundFileName);
|
2018-10-08 14:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
define('MSZ_USER_BACKGROUND_TYPE_PNG', IMAGETYPE_PNG);
|
|
|
|
define('MSZ_USER_BACKGROUND_TYPE_JPG', IMAGETYPE_JPEG);
|
2018-10-27 20:50:34 +02:00
|
|
|
define('MSZ_USER_BACKGROUND_TYPE_GIF', IMAGETYPE_GIF);
|
2018-10-08 14:29:18 +02:00
|
|
|
define('MSZ_USER_BACKGROUND_TYPES', [
|
|
|
|
MSZ_USER_BACKGROUND_TYPE_PNG,
|
|
|
|
MSZ_USER_BACKGROUND_TYPE_JPG,
|
2018-10-27 20:50:34 +02:00
|
|
|
MSZ_USER_BACKGROUND_TYPE_GIF,
|
2018-10-08 14:29:18 +02:00
|
|
|
]);
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
function user_background_is_allowed_type(int $type): bool {
|
2018-10-08 14:29:18 +02:00
|
|
|
return in_array($type, MSZ_USER_BACKGROUND_TYPES, true);
|
|
|
|
}
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
function user_background_default_options(): array {
|
2019-08-14 21:40:36 +02:00
|
|
|
return [
|
|
|
|
'max_width' => config_get('background.max_width', MSZ_CFG_INT, 3840),
|
|
|
|
'max_height' => config_get('background.max_height', MSZ_CFG_INT, 2160),
|
|
|
|
'max_size' => config_get('background.max_height', MSZ_CFG_INT, 1000000),
|
|
|
|
];
|
2018-10-08 14:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
define('MSZ_USER_BACKGROUND_NO_ERRORS', 0);
|
|
|
|
define('MSZ_USER_BACKGROUND_ERROR_INVALID_IMAGE', 1);
|
|
|
|
define('MSZ_USER_BACKGROUND_ERROR_PROHIBITED_TYPE', 2);
|
|
|
|
define('MSZ_USER_BACKGROUND_ERROR_DIMENSIONS_TOO_LARGE', 3);
|
|
|
|
define('MSZ_USER_BACKGROUND_ERROR_DATA_TOO_LARGE', 4);
|
|
|
|
define('MSZ_USER_BACKGROUND_ERROR_TMP_FAILED', 5);
|
|
|
|
define('MSZ_USER_BACKGROUND_ERROR_STORE_FAILED', 6);
|
|
|
|
define('MSZ_USER_BACKGROUND_ERROR_FILE_NOT_FOUND', 7);
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
function user_background_set_from_path(int $userId, string $path, array $options = []): int {
|
|
|
|
if(!file_exists($path)) {
|
2018-10-08 14:29:18 +02:00
|
|
|
return MSZ_USER_BACKGROUND_ERROR_FILE_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2019-08-14 21:40:36 +02:00
|
|
|
$options = array_merge(user_background_default_options(), $options);
|
2018-10-08 14:29:18 +02:00
|
|
|
|
|
|
|
// 0 => width, 1 => height, 2 => type
|
|
|
|
$imageInfo = getimagesize($path);
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
if($imageInfo === false
|
2018-10-08 14:29:18 +02:00
|
|
|
|| count($imageInfo) < 3
|
|
|
|
|| $imageInfo[0] < 1
|
|
|
|
|| $imageInfo[1] < 1) {
|
|
|
|
return MSZ_USER_BACKGROUND_ERROR_INVALID_IMAGE;
|
|
|
|
}
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
if(!user_background_is_allowed_type($imageInfo[2])) {
|
2018-10-08 14:29:18 +02:00
|
|
|
return MSZ_USER_BACKGROUND_ERROR_PROHIBITED_TYPE;
|
|
|
|
}
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
if($imageInfo[0] > $options['max_width']
|
2018-10-08 14:29:18 +02:00
|
|
|
|| $imageInfo[1] > $options['max_height']) {
|
|
|
|
return MSZ_USER_BACKGROUND_ERROR_DIMENSIONS_TOO_LARGE;
|
|
|
|
}
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
if(filesize($path) > $options['max_size']) {
|
2018-10-08 14:29:18 +02:00
|
|
|
return MSZ_USER_BACKGROUND_ERROR_DATA_TOO_LARGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
user_background_delete($userId);
|
|
|
|
|
|
|
|
$fileName = sprintf(MSZ_USER_BACKGROUND_FORMAT, $userId);
|
2019-02-05 21:29:37 +01:00
|
|
|
$storageDir = MSZ_STORAGE . '/backgrounds/original';
|
|
|
|
mkdirs($storageDir, true);
|
|
|
|
$backgroundPath = "{$storageDir}/{$fileName}";
|
2018-10-08 14:29:18 +02:00
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
if(!copy($path, $backgroundPath)) {
|
2018-10-08 14:29:18 +02:00
|
|
|
return MSZ_USER_BACKGROUND_ERROR_STORE_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MSZ_USER_BACKGROUND_NO_ERRORS;
|
|
|
|
}
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
function user_background_set_from_data(int $userId, string $data, array $options = []): int {
|
2018-10-08 14:29:18 +02:00
|
|
|
$tmp = tempnam(sys_get_temp_dir(), 'msz');
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
if($tmp === false || !file_exists($tmp)) {
|
2018-10-08 14:29:18 +02:00
|
|
|
return MSZ_USER_BACKGROUND_ERROR_TMP_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
chmod($tmp, 644);
|
|
|
|
file_put_contents($tmp, $data);
|
|
|
|
$result = user_background_set_from_path($userId, $tmp, $options);
|
|
|
|
safe_delete($tmp);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|