diff --git a/misuzu.php b/misuzu.php index e3056ed6..a966289b 100644 --- a/misuzu.php +++ b/misuzu.php @@ -53,6 +53,8 @@ require_once 'src/Net/cidr.php'; require_once 'src/Net/geoip.php'; require_once 'src/Net/ip.php'; require_once 'src/Parsers/parse.php'; +require_once 'src/Users/avatar.php'; +require_once 'src/Users/background.php'; require_once 'src/Users/login_attempt.php'; require_once 'src/Users/profile.php'; require_once 'src/Users/relations.php'; diff --git a/src/Users/avatar.php b/src/Users/avatar.php new file mode 100644 index 00000000..b74e8e88 --- /dev/null +++ b/src/Users/avatar.php @@ -0,0 +1,112 @@ + 1000, + 'max_height' => 1000, + 'max_size' => 500000, +]); + +function user_avatar_default_options(): array +{ + return array_merge(MSZ_USER_AVATAR_OPTIONS, config_get_default([], 'Avatar')); +} + +define('MSZ_USER_AVATAR_NO_ERRORS', 0); +define('MSZ_USER_AVATAR_ERROR_INVALID_IMAGE', 1); +define('MSZ_USER_AVATAR_ERROR_PROHIBITED_TYPE', 2); +define('MSZ_USER_AVATAR_ERROR_DIMENSIONS_TOO_LARGE', 3); +define('MSZ_USER_AVATAR_ERROR_DATA_TOO_LARGE', 4); +define('MSZ_USER_AVATAR_ERROR_TMP_FAILED', 5); +define('MSZ_USER_AVATAR_ERROR_STORE_FAILED', 6); +define('MSZ_USER_AVATAR_ERROR_FILE_NOT_FOUND', 7); + +function user_avatar_set_from_path(int $userId, string $path, array $options = []): int +{ + if (!file_exists($path)) { + return MSZ_USER_AVATAR_ERROR_FILE_NOT_FOUND; + } + + $options = array_merge(MSZ_USER_AVATAR_OPTIONS, $options); + + // 0 => width, 1 => height, 2 => type + $imageInfo = getimagesize($path); + + if ($imageInfo === false + || count($imageInfo) < 3 + || $imageInfo[0] < 1 + || $imageInfo[1] < 1) { + return MSZ_USER_AVATAR_ERROR_INVALID_IMAGE; + } + + if (!user_avatar_is_allowed_type($imageInfo[2])) { + return MSZ_USER_AVATAR_ERROR_PROHIBITED_TYPE; + } + + if ($imageInfo[0] > $options['max_width'] + || $imageInfo[1] > $options['max_height']) { + return MSZ_USER_AVATAR_ERROR_DIMENSIONS_TOO_LARGE; + } + + if (filesize($path) > $options['max_size']) { + return MSZ_USER_AVATAR_ERROR_DATA_TOO_LARGE; + } + + user_avatar_delete($userId); + + $fileName = sprintf(MSZ_USER_AVATAR_FORMAT, $userId); + $avatarPath = build_path( + create_directory(build_path(MSZ_STORAGE, 'avatars/original')), + $fileName + ); + + if (!copy($path, $avatarPath)) { + return MSZ_USER_AVATAR_ERROR_STORE_FAILED; + } + + return MSZ_USER_AVATAR_NO_ERRORS; +} + +function user_avatar_set_from_data(int $userId, string $data, array $options = []): int +{ + $tmp = tempnam(sys_get_temp_dir(), 'msz'); + + if ($tmp === false || !file_exists($tmp)) { + return MSZ_USER_AVATAR_ERROR_TMP_FAILED; + } + + chmod($tmp, 644); + file_put_contents($tmp, $data); + $result = user_avatar_set_from_path($userId, $tmp, $options); + safe_delete($tmp); + + return $result; +} diff --git a/src/Users/background.php b/src/Users/background.php new file mode 100644 index 00000000..5e339f7e --- /dev/null +++ b/src/Users/background.php @@ -0,0 +1,173 @@ + 'cover', + MSZ_USER_BACKGROUND_ATTACHMENT_STRETCH => 'stretch', + MSZ_USER_BACKGROUND_ATTACHMENT_TILE => 'tile', +]); + +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', +]); + +function user_background_settings_strings(int $settings, string $format = '%s'): array +{ + $arr = []; + + $attachment = $settings & 0x0F; + + if (array_key_exists($attachment, MSZ_USER_BACKGROUND_ATTACHMENTS_NAMES)) { + $arr[] = sprintf($format, MSZ_USER_BACKGROUND_ATTACHMENTS_NAMES[$attachment]); + } + + foreach (MSZ_USER_BACKGROUND_ATTRIBUTES_NAMES as $flag => $name) { + if (($settings & $flag) > 0) { + $arr[] = sprintf($format, $name); + } + } + + return $arr; +} + +function user_background_set_settings(int $userId, int $settings): void +{ + if ($userId < 1) { + 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(); +} + +function user_background_delete(int $userId): void +{ + $backgroundFileName = sprintf(MSZ_USER_BACKGROUND_FORMAT, $userId); + safe_delete(build_path(MSZ_STORAGE, 'backgrounds/original', $backgroundFileName)); +} + +define('MSZ_USER_BACKGROUND_TYPE_PNG', IMAGETYPE_PNG); +define('MSZ_USER_BACKGROUND_TYPE_JPG', IMAGETYPE_JPEG); +define('MSZ_USER_BACKGROUND_TYPES', [ + MSZ_USER_BACKGROUND_TYPE_PNG, + MSZ_USER_BACKGROUND_TYPE_JPG, +]); + +function user_background_is_allowed_type(int $type): bool +{ + return in_array($type, MSZ_USER_BACKGROUND_TYPES, true); +} + +define('MSZ_USER_BACKGROUND_OPTIONS', [ + 'max_width' => 3840, + 'max_height' => 2160, + 'max_size' => 1000000, +]); + +function user_background_default_options(): array +{ + return array_merge(MSZ_USER_BACKGROUND_OPTIONS, config_get_default([], 'Background')); +} + +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); + +function user_background_set_from_path(int $userId, string $path, array $options = []): int +{ + if (!file_exists($path)) { + return MSZ_USER_BACKGROUND_ERROR_FILE_NOT_FOUND; + } + + $options = array_merge(MSZ_USER_BACKGROUND_OPTIONS, $options); + + // 0 => width, 1 => height, 2 => type + $imageInfo = getimagesize($path); + + if ($imageInfo === false + || count($imageInfo) < 3 + || $imageInfo[0] < 1 + || $imageInfo[1] < 1) { + return MSZ_USER_BACKGROUND_ERROR_INVALID_IMAGE; + } + + if (!user_background_is_allowed_type($imageInfo[2])) { + return MSZ_USER_BACKGROUND_ERROR_PROHIBITED_TYPE; + } + + if ($imageInfo[0] > $options['max_width'] + || $imageInfo[1] > $options['max_height']) { + return MSZ_USER_BACKGROUND_ERROR_DIMENSIONS_TOO_LARGE; + } + + if (filesize($path) > $options['max_size']) { + return MSZ_USER_BACKGROUND_ERROR_DATA_TOO_LARGE; + } + + user_background_delete($userId); + + $fileName = sprintf(MSZ_USER_BACKGROUND_FORMAT, $userId); + $backgroundPath = build_path( + create_directory(build_path(MSZ_STORAGE, 'backgrounds/original')), + $fileName + ); + + if (!copy($path, $backgroundPath)) { + return MSZ_USER_BACKGROUND_ERROR_STORE_FAILED; + } + + return MSZ_USER_BACKGROUND_NO_ERRORS; +} + +function user_background_set_from_data(int $userId, string $data, array $options = []): int +{ + $tmp = tempnam(sys_get_temp_dir(), 'msz'); + + if ($tmp === false || !file_exists($tmp)) { + 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; +} diff --git a/src/Users/user.php b/src/Users/user.php index f67df362..76a528f0 100644 --- a/src/Users/user.php +++ b/src/Users/user.php @@ -130,291 +130,6 @@ function user_set_about_page(int $userId, string $content, int $parser = MSZ_PAR return $setAbout->execute() ? MSZ_USER_ABOUT_OK : MSZ_USER_ABOUT_UPDATE_FAILED; } -define('MSZ_USER_AVATAR_FORMAT', '%d.msz'); - -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), - ]; - - foreach ($deleteThis as $deleteAvatar) { - safe_delete($deleteAvatar); - } -} - -define('MSZ_USER_AVATAR_TYPE_PNG', IMAGETYPE_PNG); -define('MSZ_USER_AVATAR_TYPE_JPG', IMAGETYPE_JPEG); -define('MSZ_USER_AVATAR_TYPE_GIF', IMAGETYPE_GIF); -define('MSZ_USER_AVATAR_TYPES', [ - MSZ_USER_AVATAR_TYPE_PNG, - MSZ_USER_AVATAR_TYPE_JPG, - MSZ_USER_AVATAR_TYPE_GIF, -]); - -function user_avatar_is_allowed_type(int $type): bool -{ - return in_array($type, MSZ_USER_AVATAR_TYPES, true); -} - -define('MSZ_USER_AVATAR_OPTIONS', [ - 'max_width' => 1000, - 'max_height' => 1000, - 'max_size' => 500000, -]); - -function user_avatar_default_options(): array -{ - return array_merge(MSZ_USER_AVATAR_OPTIONS, config_get_default([], 'Avatar')); -} - -define('MSZ_USER_AVATAR_NO_ERRORS', 0); -define('MSZ_USER_AVATAR_ERROR_INVALID_IMAGE', 1); -define('MSZ_USER_AVATAR_ERROR_PROHIBITED_TYPE', 2); -define('MSZ_USER_AVATAR_ERROR_DIMENSIONS_TOO_LARGE', 3); -define('MSZ_USER_AVATAR_ERROR_DATA_TOO_LARGE', 4); -define('MSZ_USER_AVATAR_ERROR_TMP_FAILED', 5); -define('MSZ_USER_AVATAR_ERROR_STORE_FAILED', 6); -define('MSZ_USER_AVATAR_ERROR_FILE_NOT_FOUND', 7); - -function user_avatar_set_from_path(int $userId, string $path, array $options = []): int -{ - if (!file_exists($path)) { - return MSZ_USER_AVATAR_ERROR_FILE_NOT_FOUND; - } - - $options = array_merge(MSZ_USER_AVATAR_OPTIONS, $options); - - // 0 => width, 1 => height, 2 => type - $imageInfo = getimagesize($path); - - if ($imageInfo === false - || count($imageInfo) < 3 - || $imageInfo[0] < 1 - || $imageInfo[1] < 1) { - return MSZ_USER_AVATAR_ERROR_INVALID_IMAGE; - } - - if (!user_avatar_is_allowed_type($imageInfo[2])) { - return MSZ_USER_AVATAR_ERROR_PROHIBITED_TYPE; - } - - if ($imageInfo[0] > $options['max_width'] - || $imageInfo[1] > $options['max_height']) { - return MSZ_USER_AVATAR_ERROR_DIMENSIONS_TOO_LARGE; - } - - if (filesize($path) > $options['max_size']) { - return MSZ_USER_AVATAR_ERROR_DATA_TOO_LARGE; - } - - user_avatar_delete($userId); - - $fileName = sprintf(MSZ_USER_AVATAR_FORMAT, $userId); - $avatarPath = build_path( - create_directory(build_path(MSZ_STORAGE, 'avatars/original')), - $fileName - ); - - if (!copy($path, $avatarPath)) { - return MSZ_USER_AVATAR_ERROR_STORE_FAILED; - } - - return MSZ_USER_AVATAR_NO_ERRORS; -} - -function user_avatar_set_from_data(int $userId, string $data, array $options = []): int -{ - $tmp = tempnam(sys_get_temp_dir(), 'msz'); - - if ($tmp === false || !file_exists($tmp)) { - return MSZ_USER_AVATAR_ERROR_TMP_FAILED; - } - - chmod($tmp, 644); - file_put_contents($tmp, $data); - $result = user_avatar_set_from_path($userId, $tmp, $options); - safe_delete($tmp); - - return $result; -} - -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); - -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, -]); - -define('MSZ_USER_BACKGROUND_ATTACHMENTS_NAMES', [ - MSZ_USER_BACKGROUND_ATTACHMENT_COVER => 'cover', - MSZ_USER_BACKGROUND_ATTACHMENT_STRETCH => 'stretch', - MSZ_USER_BACKGROUND_ATTACHMENT_TILE => 'tile', -]); - -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', -]); - -function user_background_settings_strings(int $settings, string $format = '%s'): array -{ - $arr = []; - - $attachment = $settings & 0x0F; - - if (array_key_exists($attachment, MSZ_USER_BACKGROUND_ATTACHMENTS_NAMES)) { - $arr[] = sprintf($format, MSZ_USER_BACKGROUND_ATTACHMENTS_NAMES[$attachment]); - } - - foreach (MSZ_USER_BACKGROUND_ATTRIBUTES_NAMES as $flag => $name) { - if (($settings & $flag) > 0) { - $arr[] = sprintf($format, $name); - } - } - - return $arr; -} - -function user_background_set_settings(int $userId, int $settings): void -{ - if ($userId < 1) { - 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(); -} - -function user_background_delete(int $userId): void -{ - $backgroundFileName = sprintf(MSZ_USER_BACKGROUND_FORMAT, $userId); - safe_delete(build_path(MSZ_STORAGE, 'backgrounds/original', $backgroundFileName)); -} - -define('MSZ_USER_BACKGROUND_TYPE_PNG', IMAGETYPE_PNG); -define('MSZ_USER_BACKGROUND_TYPE_JPG', IMAGETYPE_JPEG); -define('MSZ_USER_BACKGROUND_TYPES', [ - MSZ_USER_BACKGROUND_TYPE_PNG, - MSZ_USER_BACKGROUND_TYPE_JPG, -]); - -function user_background_is_allowed_type(int $type): bool -{ - return in_array($type, MSZ_USER_BACKGROUND_TYPES, true); -} - -define('MSZ_USER_BACKGROUND_OPTIONS', [ - 'max_width' => 3840, - 'max_height' => 2160, - 'max_size' => 1000000, -]); - -function user_background_default_options(): array -{ - return array_merge(MSZ_USER_BACKGROUND_OPTIONS, config_get_default([], 'Background')); -} - -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); - -function user_background_set_from_path(int $userId, string $path, array $options = []): int -{ - if (!file_exists($path)) { - return MSZ_USER_BACKGROUND_ERROR_FILE_NOT_FOUND; - } - - $options = array_merge(MSZ_USER_BACKGROUND_OPTIONS, $options); - - // 0 => width, 1 => height, 2 => type - $imageInfo = getimagesize($path); - - if ($imageInfo === false - || count($imageInfo) < 3 - || $imageInfo[0] < 1 - || $imageInfo[1] < 1) { - return MSZ_USER_BACKGROUND_ERROR_INVALID_IMAGE; - } - - if (!user_background_is_allowed_type($imageInfo[2])) { - return MSZ_USER_BACKGROUND_ERROR_PROHIBITED_TYPE; - } - - if ($imageInfo[0] > $options['max_width'] - || $imageInfo[1] > $options['max_height']) { - return MSZ_USER_BACKGROUND_ERROR_DIMENSIONS_TOO_LARGE; - } - - if (filesize($path) > $options['max_size']) { - return MSZ_USER_BACKGROUND_ERROR_DATA_TOO_LARGE; - } - - user_background_delete($userId); - - $fileName = sprintf(MSZ_USER_BACKGROUND_FORMAT, $userId); - $backgroundPath = build_path( - create_directory(build_path(MSZ_STORAGE, 'backgrounds/original')), - $fileName - ); - - if (!copy($path, $backgroundPath)) { - return MSZ_USER_BACKGROUND_ERROR_STORE_FAILED; - } - - return MSZ_USER_BACKGROUND_NO_ERRORS; -} - -function user_background_set_from_data(int $userId, string $data, array $options = []): int -{ - $tmp = tempnam(sys_get_temp_dir(), 'msz'); - - if ($tmp === false || !file_exists($tmp)) { - 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; -} - // all the way down here bc of defines, this define is temporary define('MSZ_TMP_USER_ERROR_STRINGS', [ 'csrf' => "Couldn't verify you, please refresh the page and retry.",