Removed some old utility functions.

This commit is contained in:
flash 2019-02-05 21:29:37 +01:00
parent 5d36e24fc7
commit 06b34d6133
6 changed files with 41 additions and 86 deletions

View file

@ -83,7 +83,8 @@ db_setup([
]); ]);
// replace this with a better storage mechanism // replace this with a better storage mechanism
define('MSZ_STORAGE', create_directory(config_get_default(MSZ_ROOT . '/store', 'Storage', 'path'))); define('MSZ_STORAGE', config_get_default(MSZ_ROOT . '/store', 'Storage', 'path'));
mkdirs(MSZ_STORAGE, true);
if (PHP_SAPI === 'cli') { if (PHP_SAPI === 'cli') {
if (realpath($_SERVER['SCRIPT_FILENAME']) === __FILE__) { if (realpath($_SERVER['SCRIPT_FILENAME']) === __FILE__) {
@ -285,10 +286,15 @@ MIG;
cache_init(config_get_default([], 'Cache')); cache_init(config_get_default([], 'Cache'));
geoip_init(config_get_default('', 'GeoIP', 'database_path')); geoip_init(config_get_default('', 'GeoIP', 'database_path'));
if (!MSZ_DEBUG) {
$twigCache = sys_get_temp_dir() . '/msz-tpl-cache-' . md5(MSZ_ROOT);
mkdirs($twigCache, true);
}
tpl_init([ tpl_init([
'debug' => MSZ_DEBUG, 'debug' => MSZ_DEBUG,
'auto_reload' => MSZ_DEBUG, 'auto_reload' => MSZ_DEBUG,
'cache' => MSZ_DEBUG ? false : create_directory(build_path(sys_get_temp_dir(), 'msz-tpl-cache-' . md5(MSZ_ROOT))), 'cache' => $twigCache ?? false,
]); ]);
tpl_var('globals', [ tpl_var('globals', [

View file

@ -18,28 +18,22 @@ switch ($mode) {
MSZ_PERM_USER_MANAGE_USERS MSZ_PERM_USER_MANAGE_USERS
) )
)) { )) {
$avatarFilename = build_path( $avatarFilename = config_get_default(MSZ_ROOT . '/public/images/banned-avatar.png', 'Avatar', 'banned_path');
MSZ_ROOT,
config_get_default('public/images/banned-avatar.png', 'Avatar', 'banned_path')
);
} else { } else {
$avatarFilename = build_path( $avatarFilename = config_get_default(MSZ_ROOT . '/public/images/no-avatar.png', 'Avatar', 'default_path');
MSZ_ROOT,
config_get_default('public/images/no-avatar.png', 'Avatar', 'default_path')
);
$userAvatar = "{$userId}.msz"; $userAvatar = "{$userId}.msz";
$croppedAvatar = build_path( $storageDir = MSZ_STORAGE . '/avatars/200x200';
create_directory(build_path(MSZ_STORAGE, 'avatars/200x200')), $croppedAvatar = $storageDir . '/' . $userAvatar;
$userAvatar
);
if (is_file($croppedAvatar)) { if (is_file($croppedAvatar)) {
$avatarFilename = $croppedAvatar; $avatarFilename = $croppedAvatar;
} else { } else {
$originalAvatar = build_path(MSZ_STORAGE, 'avatars/original', $userAvatar); $originalAvatar = MSZ_STORAGE . '/avatars/original/' . $userAvatar;
if (is_file($originalAvatar)) { if (is_file($originalAvatar)) {
try { try {
mkdirs($storageDir, true);
file_put_contents( file_put_contents(
$croppedAvatar, $croppedAvatar,
crop_image_centred_path($originalAvatar, 200, 200)->getImagesBlob(), crop_image_centred_path($originalAvatar, 200, 200)->getImagesBlob(),
@ -83,10 +77,9 @@ switch ($mode) {
break; break;
} }
$userBackground = build_path( $storageDir = MSZ_STORAGE . '/backgrounds/original';
create_directory(build_path(MSZ_STORAGE, 'backgrounds/original')), $userBackground = "{$storageDir}/{$userId}.msz";
"{$userId}.msz" mkdirs($storageDir, true);
);
if (!is_file($userBackground)) { if (!is_file($userBackground)) {
echo render_error(404); echo render_error(404);
@ -362,8 +355,7 @@ switch ($mode) {
} }
$profile = user_profile_get($userId); $profile = user_profile_get($userId);
$backgroundPath = MSZ_STORAGE . "/backgrounds/original/{$profile['user_id']}.msz";
$backgroundPath = build_path(MSZ_STORAGE, 'backgrounds/original', "{$profile['user_id']}.msz");
if (is_file($backgroundPath)) { if (is_file($backgroundPath)) {
$backgroundInfo = getimagesize($backgroundPath); $backgroundInfo = getimagesize($backgroundPath);

View file

@ -6,8 +6,8 @@ function user_avatar_delete(int $userId): void
$avatarFileName = sprintf(MSZ_USER_AVATAR_FORMAT, $userId); $avatarFileName = sprintf(MSZ_USER_AVATAR_FORMAT, $userId);
$deleteThis = [ $deleteThis = [
build_path(MSZ_STORAGE, 'avatars/original', $avatarFileName), MSZ_STORAGE . '/avatars/original/' . $avatarFileName,
build_path(MSZ_STORAGE, 'avatars/200x200', $avatarFileName), MSZ_STORAGE . '/avatars/200x200/' . $avatarFileName,
]; ];
foreach ($deleteThis as $deleteAvatar) { foreach ($deleteThis as $deleteAvatar) {
@ -83,10 +83,9 @@ function user_avatar_set_from_path(int $userId, string $path, array $options = [
user_avatar_delete($userId); user_avatar_delete($userId);
$fileName = sprintf(MSZ_USER_AVATAR_FORMAT, $userId); $fileName = sprintf(MSZ_USER_AVATAR_FORMAT, $userId);
$avatarPath = build_path( $storageDir = MSZ_STORAGE . '/avatars/original';
create_directory(build_path(MSZ_STORAGE, 'avatars/original')), mkdirs($storageDir, true);
$fileName $avatarPath = "{$storageDir}/{$fileName}";
);
if (!copy($path, $avatarPath)) { if (!copy($path, $avatarPath)) {
return MSZ_USER_AVATAR_ERROR_STORE_FAILED; return MSZ_USER_AVATAR_ERROR_STORE_FAILED;

View file

@ -78,7 +78,7 @@ function user_background_set_settings(int $userId, int $settings): void
function user_background_delete(int $userId): void function user_background_delete(int $userId): void
{ {
$backgroundFileName = sprintf(MSZ_USER_BACKGROUND_FORMAT, $userId); $backgroundFileName = sprintf(MSZ_USER_BACKGROUND_FORMAT, $userId);
safe_delete(build_path(MSZ_STORAGE, 'backgrounds/original', $backgroundFileName)); safe_delete(MSZ_STORAGE . '/backgrounds/original/' . $backgroundFileName);
} }
define('MSZ_USER_BACKGROUND_TYPE_PNG', IMAGETYPE_PNG); define('MSZ_USER_BACKGROUND_TYPE_PNG', IMAGETYPE_PNG);
@ -149,10 +149,9 @@ function user_background_set_from_path(int $userId, string $path, array $options
user_background_delete($userId); user_background_delete($userId);
$fileName = sprintf(MSZ_USER_BACKGROUND_FORMAT, $userId); $fileName = sprintf(MSZ_USER_BACKGROUND_FORMAT, $userId);
$backgroundPath = build_path( $storageDir = MSZ_STORAGE . '/backgrounds/original';
create_directory(build_path(MSZ_STORAGE, 'backgrounds/original')), mkdirs($storageDir, true);
$fileName $backgroundPath = "{$storageDir}/{$fileName}";
);
if (!copy($path, $backgroundPath)) { if (!copy($path, $backgroundPath)) {
return MSZ_USER_BACKGROUND_ERROR_STORE_FAILED; return MSZ_USER_BACKGROUND_ERROR_STORE_FAILED;

View file

@ -268,7 +268,6 @@ function user_set_birthdate(int $userId, int $day, int $month, int $year, int $y
if ($year === 0) { if ($year === 0) {
$checkYear = date('Y'); $checkYear = date('Y');
} else { } else {
echo $year;
if ($year < date('Y') - $yearRange || $year > date('Y')) { if ($year < date('Y') - $yearRange || $year > date('Y')) {
return MSZ_E_USER_BIRTHDATE_YEAR; return MSZ_E_USER_BIRTHDATE_YEAR;
} }
@ -432,14 +431,14 @@ define('MSZ_TMP_USER_ERROR_STRINGS', [
], ],
'set' => [ 'set' => [
'_' => 'Something happened? (SET:%1$d)', '_' => 'Something happened? (SET:%1$d)',
MSZ_USER_AVATAR_NO_ERRORS => '', MSZ_USER_BACKGROUND_NO_ERRORS => '',
MSZ_USER_AVATAR_ERROR_INVALID_IMAGE => 'The file you uploaded was not an image!', MSZ_USER_BACKGROUND_ERROR_INVALID_IMAGE => 'The file you uploaded was not an image!',
MSZ_USER_AVATAR_ERROR_PROHIBITED_TYPE => 'This type of image is not supported!', MSZ_USER_BACKGROUND_ERROR_PROHIBITED_TYPE => 'This type of image is not supported!',
MSZ_USER_AVATAR_ERROR_DIMENSIONS_TOO_LARGE => 'Your background can\'t be larger than %3$dx%4$d!', MSZ_USER_BACKGROUND_ERROR_DIMENSIONS_TOO_LARGE => 'Your background can\'t be larger than %3$dx%4$d!',
MSZ_USER_AVATAR_ERROR_DATA_TOO_LARGE => 'Your background is not allowed to be larger in file size than %2$s!', MSZ_USER_BACKGROUND_ERROR_DATA_TOO_LARGE => 'Your background is not allowed to be larger in file size than %2$s!',
MSZ_USER_AVATAR_ERROR_TMP_FAILED => 'Unable to save your background, contact an administator!', MSZ_USER_BACKGROUND_ERROR_TMP_FAILED => 'Unable to save your background, contact an administator!',
MSZ_USER_AVATAR_ERROR_STORE_FAILED => 'Unable to save your background, contact an administator!', MSZ_USER_BACKGROUND_ERROR_STORE_FAILED => 'Unable to save your background, contact an administator!',
MSZ_USER_AVATAR_ERROR_FILE_NOT_FOUND => 'Unable to save your background, contact an administator!', MSZ_USER_BACKGROUND_ERROR_FILE_NOT_FOUND => 'Unable to save your background, contact an administator!',
], ],
], ],
'profile' => [ 'profile' => [

View file

@ -17,11 +17,6 @@ function password_entropy(string $password): int
return count(count_chars(utf8_decode($password), 1)) * 8; return count(count_chars(utf8_decode($password), 1)) * 8;
} }
function fix_path_separator(string $path, string $separator = DIRECTORY_SEPARATOR, array $separators = ['/', '\\']): string
{
return str_replace($separators, $separator, rtrim($path, implode($separators)));
}
function safe_delete(string $path): void function safe_delete(string $path): void
{ {
$path = realpath($path); $path = realpath($path);
@ -40,44 +35,14 @@ function safe_delete(string $path): void
} }
} }
// mkdir + recursion // mkdir but it fails silently
function create_directory(string $path): string function mkdirs(string $path, bool $recursive = false, int $mode = 0777): bool
{ {
if (is_file($path)) { if (file_exists($path)) {
return ''; return true;
} }
if (is_dir($path)) { return mkdir($path, $mode, $recursive);
return realpath($path);
}
$on_windows = running_on_windows();
$path = fix_path_separator($path);
$split_path = explode(DIRECTORY_SEPARATOR, $path);
$existing_path = $on_windows ? '' : DIRECTORY_SEPARATOR;
foreach ($split_path as $path_part) {
$existing_path .= $path_part . DIRECTORY_SEPARATOR;
if ($on_windows && mb_substr($path_part, 1, 2) === ':\\') {
continue;
}
if (!file_exists($existing_path)) {
mkdir($existing_path);
}
}
return ($path = realpath($path)) === false ? '' : $path;
}
function build_path(string ...$path): string
{
for ($i = 0; $i < count($path); $i++) {
$path[$i] = fix_path_separator($path[$i]);
}
return implode(DIRECTORY_SEPARATOR, $path);
} }
function check_mx_record(string $email): bool function check_mx_record(string $email): bool
@ -182,11 +147,6 @@ function crop_image_centred(Imagick $image, int $target_width, int $target_heigh
return $image->deconstructImages(); return $image->deconstructImages();
} }
function running_on_windows(): bool
{
return starts_with(mb_strtolower(PHP_OS), 'win');
}
function pdo_prepare_array_update(array $keys, bool $useKeys = false, string $format = '%s'): string function pdo_prepare_array_update(array $keys, bool $useKeys = false, string $format = '%s'): string
{ {
return pdo_prepare_array($keys, $useKeys, sprintf($format, '`%1$s` = :%1$s')); return pdo_prepare_array($keys, $useKeys, sprintf($format, '`%1$s` = :%1$s'));