From 06b34d6133be54b7a5e00db4744e8db3903462df Mon Sep 17 00:00:00 2001 From: flashwave Date: Tue, 5 Feb 2019 21:29:37 +0100 Subject: [PATCH] Removed some old utility functions. --- misuzu.php | 10 ++++++-- public/profile.php | 30 +++++++++--------------- src/Users/avatar.php | 11 ++++----- src/Users/background.php | 9 ++++---- src/Users/user.php | 17 +++++++------- utility.php | 50 ++++------------------------------------ 6 files changed, 41 insertions(+), 86 deletions(-) diff --git a/misuzu.php b/misuzu.php index fab6df62..b8c963bf 100644 --- a/misuzu.php +++ b/misuzu.php @@ -83,7 +83,8 @@ db_setup([ ]); // 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 (realpath($_SERVER['SCRIPT_FILENAME']) === __FILE__) { @@ -285,10 +286,15 @@ MIG; cache_init(config_get_default([], 'Cache')); 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([ 'debug' => 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', [ diff --git a/public/profile.php b/public/profile.php index 8f2ec7f7..0114b8c5 100644 --- a/public/profile.php +++ b/public/profile.php @@ -18,28 +18,22 @@ switch ($mode) { MSZ_PERM_USER_MANAGE_USERS ) )) { - $avatarFilename = build_path( - MSZ_ROOT, - config_get_default('public/images/banned-avatar.png', 'Avatar', 'banned_path') - ); + $avatarFilename = config_get_default(MSZ_ROOT . '/public/images/banned-avatar.png', 'Avatar', 'banned_path'); } else { - $avatarFilename = build_path( - MSZ_ROOT, - config_get_default('public/images/no-avatar.png', 'Avatar', 'default_path') - ); + $avatarFilename = config_get_default(MSZ_ROOT . '/public/images/no-avatar.png', 'Avatar', 'default_path'); $userAvatar = "{$userId}.msz"; - $croppedAvatar = build_path( - create_directory(build_path(MSZ_STORAGE, 'avatars/200x200')), - $userAvatar - ); + $storageDir = MSZ_STORAGE . '/avatars/200x200'; + $croppedAvatar = $storageDir . '/' . $userAvatar; if (is_file($croppedAvatar)) { $avatarFilename = $croppedAvatar; } else { - $originalAvatar = build_path(MSZ_STORAGE, 'avatars/original', $userAvatar); + $originalAvatar = MSZ_STORAGE . '/avatars/original/' . $userAvatar; if (is_file($originalAvatar)) { try { + mkdirs($storageDir, true); + file_put_contents( $croppedAvatar, crop_image_centred_path($originalAvatar, 200, 200)->getImagesBlob(), @@ -83,10 +77,9 @@ switch ($mode) { break; } - $userBackground = build_path( - create_directory(build_path(MSZ_STORAGE, 'backgrounds/original')), - "{$userId}.msz" - ); + $storageDir = MSZ_STORAGE . '/backgrounds/original'; + $userBackground = "{$storageDir}/{$userId}.msz"; + mkdirs($storageDir, true); if (!is_file($userBackground)) { echo render_error(404); @@ -362,8 +355,7 @@ switch ($mode) { } $profile = user_profile_get($userId); - - $backgroundPath = build_path(MSZ_STORAGE, 'backgrounds/original', "{$profile['user_id']}.msz"); + $backgroundPath = MSZ_STORAGE . "/backgrounds/original/{$profile['user_id']}.msz"; if (is_file($backgroundPath)) { $backgroundInfo = getimagesize($backgroundPath); diff --git a/src/Users/avatar.php b/src/Users/avatar.php index b74e8e88..bd5aefb6 100644 --- a/src/Users/avatar.php +++ b/src/Users/avatar.php @@ -6,8 +6,8 @@ function user_avatar_delete(int $userId): void $avatarFileName = sprintf(MSZ_USER_AVATAR_FORMAT, $userId); $deleteThis = [ - build_path(MSZ_STORAGE, 'avatars/original', $avatarFileName), - build_path(MSZ_STORAGE, 'avatars/200x200', $avatarFileName), + MSZ_STORAGE . '/avatars/original/' . $avatarFileName, + MSZ_STORAGE . '/avatars/200x200/' . $avatarFileName, ]; foreach ($deleteThis as $deleteAvatar) { @@ -83,10 +83,9 @@ function user_avatar_set_from_path(int $userId, string $path, array $options = [ user_avatar_delete($userId); $fileName = sprintf(MSZ_USER_AVATAR_FORMAT, $userId); - $avatarPath = build_path( - create_directory(build_path(MSZ_STORAGE, 'avatars/original')), - $fileName - ); + $storageDir = MSZ_STORAGE . '/avatars/original'; + mkdirs($storageDir, true); + $avatarPath = "{$storageDir}/{$fileName}"; if (!copy($path, $avatarPath)) { return MSZ_USER_AVATAR_ERROR_STORE_FAILED; diff --git a/src/Users/background.php b/src/Users/background.php index 9f52ad82..a2a17b3f 100644 --- a/src/Users/background.php +++ b/src/Users/background.php @@ -78,7 +78,7 @@ function user_background_set_settings(int $userId, int $settings): void function user_background_delete(int $userId): void { $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); @@ -149,10 +149,9 @@ function user_background_set_from_path(int $userId, string $path, array $options user_background_delete($userId); $fileName = sprintf(MSZ_USER_BACKGROUND_FORMAT, $userId); - $backgroundPath = build_path( - create_directory(build_path(MSZ_STORAGE, 'backgrounds/original')), - $fileName - ); + $storageDir = MSZ_STORAGE . '/backgrounds/original'; + mkdirs($storageDir, true); + $backgroundPath = "{$storageDir}/{$fileName}"; if (!copy($path, $backgroundPath)) { return MSZ_USER_BACKGROUND_ERROR_STORE_FAILED; diff --git a/src/Users/user.php b/src/Users/user.php index 471b2c16..7c610b63 100644 --- a/src/Users/user.php +++ b/src/Users/user.php @@ -268,7 +268,6 @@ function user_set_birthdate(int $userId, int $day, int $month, int $year, int $y if ($year === 0) { $checkYear = date('Y'); } else { - echo $year; if ($year < date('Y') - $yearRange || $year > date('Y')) { return MSZ_E_USER_BIRTHDATE_YEAR; } @@ -432,14 +431,14 @@ define('MSZ_TMP_USER_ERROR_STRINGS', [ ], 'set' => [ '_' => 'Something happened? (SET:%1$d)', - MSZ_USER_AVATAR_NO_ERRORS => '', - MSZ_USER_AVATAR_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_AVATAR_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_AVATAR_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_AVATAR_ERROR_FILE_NOT_FOUND => 'Unable to save your background, contact an administator!', + MSZ_USER_BACKGROUND_NO_ERRORS => '', + MSZ_USER_BACKGROUND_ERROR_INVALID_IMAGE => 'The file you uploaded was not an image!', + MSZ_USER_BACKGROUND_ERROR_PROHIBITED_TYPE => 'This type of image is not supported!', + MSZ_USER_BACKGROUND_ERROR_DIMENSIONS_TOO_LARGE => 'Your background can\'t be larger than %3$dx%4$d!', + MSZ_USER_BACKGROUND_ERROR_DATA_TOO_LARGE => 'Your background is not allowed to be larger in file size than %2$s!', + MSZ_USER_BACKGROUND_ERROR_TMP_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_BACKGROUND_ERROR_FILE_NOT_FOUND => 'Unable to save your background, contact an administator!', ], ], 'profile' => [ diff --git a/utility.php b/utility.php index 0115d730..3d6e1e28 100644 --- a/utility.php +++ b/utility.php @@ -17,11 +17,6 @@ function password_entropy(string $password): int 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 { $path = realpath($path); @@ -40,44 +35,14 @@ function safe_delete(string $path): void } } -// mkdir + recursion -function create_directory(string $path): string +// mkdir but it fails silently +function mkdirs(string $path, bool $recursive = false, int $mode = 0777): bool { - if (is_file($path)) { - return ''; + if (file_exists($path)) { + return true; } - if (is_dir($path)) { - 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); + return mkdir($path, $mode, $recursive); } 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(); } -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 { return pdo_prepare_array($keys, $useKeys, sprintf($format, '`%1$s` = :%1$s'));