From 8ff10f9d3c99e18a2685e70571a713761bc7a9da Mon Sep 17 00:00:00 2001 From: flashwave Date: Fri, 5 Oct 2018 11:14:54 +0200 Subject: [PATCH] Moved storage path into a constant. --- misuzu.php | 6 ++++-- public/profile.php | 8 ++++---- public/settings.php | 4 ++-- src/Application.php | 9 --------- src/Users/user.php | 13 +++++-------- 5 files changed, 15 insertions(+), 25 deletions(-) diff --git a/misuzu.php b/misuzu.php index fb089990..5db6315d 100644 --- a/misuzu.php +++ b/misuzu.php @@ -73,6 +73,9 @@ if (!empty($errorReporter)) { db_setup(MSZ_DATABASE_NAMES[0], config_get_default([], 'Database.' . MSZ_DATABASE_NAMES[0])); +// replace this with a better storage mechanism +define('MSZ_STORAGE', create_directory(config_get_default(MSZ_ROOT . '/store', 'Storage', 'path'))); + if (PHP_SAPI === 'cli') { if ($argv[0] === basename(__FILE__)) { switch ($argv[1] ?? null) { @@ -237,8 +240,7 @@ MIG; // we're running this again because ob_clean breaks gzip otherwise ob_start(); - $mszStoragePath = $app->getStoragePath(); - if (!is_readable($mszStoragePath) || !is_writable($mszStoragePath)) { + if (!is_readable(MSZ_STORAGE) || !is_writable(MSZ_STORAGE)) { echo 'Cannot access storage directory.'; exit; } diff --git a/public/profile.php b/public/profile.php index e87c3c07..0aceab71 100644 --- a/public/profile.php +++ b/public/profile.php @@ -16,14 +16,14 @@ switch ($mode) { ); $user_avatar = "{$userId}.msz"; $cropped_avatar = build_path( - create_directory(build_path($app->getStoragePath(), 'avatars/200x200')), + create_directory(build_path(MSZ_STORAGE, 'avatars/200x200')), $user_avatar ); if (is_file($cropped_avatar)) { $avatar_filename = $cropped_avatar; } else { - $original_avatar = build_path($app->getStoragePath(), 'avatars/original', $user_avatar); + $original_avatar = build_path(MSZ_STORAGE, 'avatars/original', $user_avatar); if (is_file($original_avatar)) { try { @@ -45,7 +45,7 @@ switch ($mode) { case 'background': $user_background = build_path( - create_directory(build_path($app->getStoragePath(), 'backgrounds/original')), + create_directory(build_path(MSZ_STORAGE, 'backgrounds/original')), "{$userId}.msz" ); @@ -165,7 +165,7 @@ switch ($mode) { 'is_editing' => $isEditing, 'perms' => $perms, 'profile_fields' => user_session_active() ? user_profile_fields_display($profile, !$isEditing) : [], - 'has_background' => is_file(build_path($app->getStoragePath(), 'backgrounds/original', "{$profile['user_id']}.msz")), + 'has_background' => is_file(build_path(MSZ_STORAGE, 'backgrounds/original', "{$profile['user_id']}.msz")), ]); echo tpl_render('user.profile'); break; diff --git a/public/settings.php b/public/settings.php index e6b56d8a..db8e3ffb 100644 --- a/public/settings.php +++ b/public/settings.php @@ -367,8 +367,8 @@ switch ($settingsMode) { $getAccountInfo->bindValue('user_id', $settingsUserId); $accountInfo = $getAccountInfo->execute() ? $getAccountInfo->fetch(PDO::FETCH_ASSOC) : []; - $userHasAvatar = is_file(build_path($app->getStoragePath(), 'avatars/original', $avatarFileName)); - $userHasBackground = is_file(build_path($app->getStoragePath(), 'backgrounds/original', $avatarFileName)); + $userHasAvatar = is_file(build_path(MSZ_STORAGE, 'avatars/original', $avatarFileName)); + $userHasBackground = is_file(build_path(MSZ_STORAGE, 'backgrounds/original', $avatarFileName)); tpl_vars([ 'avatar' => $avatarProps, diff --git a/src/Application.php b/src/Application.php index 84b69c8d..cd067556 100644 --- a/src/Application.php +++ b/src/Application.php @@ -23,15 +23,6 @@ final class Application self::$instance = $this; } - /** - * Gets a data storage path. - * @return string - */ - public function getStoragePath(): string - { - return create_directory(config_get_default(MSZ_ROOT . '/store', 'Storage', 'path')); - } - public function startGeoIP(): void { if (!empty($this->geoipInstance)) { diff --git a/src/Users/user.php b/src/Users/user.php index 1779872b..f025b01a 100644 --- a/src/Users/user.php +++ b/src/Users/user.php @@ -1,5 +1,4 @@ getStoragePath(); $deleteThis = [ - build_path($storePath, 'avatars/original', $avatarFileName), - build_path($storePath, 'avatars/200x200', $avatarFileName), + build_path(MSZ_STORAGE, 'avatars/original', $avatarFileName), + build_path(MSZ_STORAGE, 'avatars/200x200', $avatarFileName), ]; foreach ($deleteThis as $deleteAvatar) { @@ -217,7 +215,7 @@ function user_avatar_set_from_path(int $userId, string $path, array $options = [ $fileName = sprintf(MSZ_USER_AVATAR_FORMAT, $userId); $avatarPath = build_path( - create_directory(build_path(Application::getInstance()->getStoragePath(), 'avatars/original')), + create_directory(build_path(MSZ_STORAGE, 'avatars/original')), $fileName ); @@ -320,8 +318,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); - $storePath = Application::getInstance()->getStoragePath(); - safe_delete(build_path($storePath, 'backgrounds/original', $backgroundFileName)); + safe_delete(build_path(MSZ_STORAGE, 'backgrounds/original', $backgroundFileName)); } define('MSZ_USER_BACKGROUND_TYPE_PNG', IMAGETYPE_PNG); @@ -391,7 +388,7 @@ function user_background_set_from_path(int $userId, string $path, array $options $fileName = sprintf(MSZ_USER_BACKGROUND_FORMAT, $userId); $backgroundPath = build_path( - create_directory(build_path(Application::getInstance()->getStoragePath(), 'backgrounds/original')), + create_directory(build_path(MSZ_STORAGE, 'backgrounds/original')), $fileName );