diff --git a/public/settings.php b/public/settings.php index f5c225ae..b0c26b64 100644 --- a/public/settings.php +++ b/public/settings.php @@ -12,6 +12,60 @@ if ($settings_session === null) { return; } +$settings_profile_fields = [ + 'twitter' => [ + 'name' => 'Twitter', + 'regex' => '#^(?:https?://(?:www\.)?twitter.com/(?:\#!\/)?)?@?([A-Za-z0-9_]{1,20})/?$#u', + 'no-match' => 'Twitter field was invalid.', + ], + 'osu' => [ + 'name' => 'osu!', + 'regex' => '#^(?:https?://osu.ppy.sh/u(?:sers)?/)?([a-zA-Z0-9-\[\]_ ]{1,20})/?$#u', + 'no-match' => 'osu! field was invalid.', + ], + 'website' => [ + 'name' => 'Website', + 'type' => 'url', + 'regex' => '#^((?:https?)://.{1,240})$#u', + 'no-match' => 'Website field was invalid.', + ], + 'youtube' => [ + 'name' => 'Youtube', + 'regex' => '#^(?:https?://(?:www.)?youtube.com/(?:(?:user|c|channel)/)?)?(UC[a-zA-Z0-9-_]{1,22}|[a-zA-Z0-9-_%]{1,100})/?$#u', + 'no-match' => 'Youtube field was invalid.', + ], + 'steam' => [ + 'name' => 'Steam', + 'regex' => '#^(?:https?://(?:www.)?steamcommunity.com/(?:id|profiles)/)?([a-zA-Z0-9_-]{2,100})/?$#u', + 'no-match' => 'Steam field was invalid.', + ], + 'twitchtv' => [ + 'name' => 'Twitch.tv', + 'regex' => '#^(?:https?://(?:www.)?twitch.tv/)?([0-9A-Za-z_]{3,25})/?$#u', + 'no-match' => 'Twitch.tv field was invalid.', + ], + 'lastfm' => [ + 'name' => 'Last.fm', + 'regex' => '#^(?:https?://(?:www.)?last.fm/user/)?([a-zA-Z]{1}[a-zA-Z0-9_-]{1,14})/?$#u', + 'no-match' => 'Last.fm field was invalid.', + ], + 'github' => [ + 'name' => 'Github', + 'regex' => '#^(?:https?://(?:www.)?github.com/?)?([a-zA-Z0-9](?:[a-zA-Z0-9]|-(?=[a-zA-Z0-9])){0,38})/?$#u', + 'no-match' => 'Github field was invalid.', + ], + 'skype' => [ + 'name' => 'Skype', + 'regex' => '#^((?:live:)?[a-zA-Z][\w\.,\-_@]{1,100})$#u', + 'no-match' => 'Skype field was invalid.', + ], + 'discord' => [ + 'name' => 'Discord', + 'regex' => '#^(.{1,32}\#[0-9]{4})$#u', + 'no-match' => 'Discord field was invalid.', + ], +]; + $settings_user = $settings_session->user; $settings_mode = $_GET['m'] ?? null; @@ -46,216 +100,27 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { } if (isset($_POST['profile']) && is_array($_POST['profile'])) { - if (isset($_POST['profile']['twitter'])) { - $user_twitter = ''; + foreach ($settings_profile_fields as $name => $props) { + if (isset($_POST['profile'][$name])) { + $field_value = ''; - if (!empty($_POST['profile']['twitter'])) { - $twitter_regex = preg_match( - '#^(?:https?://(?:www\.)?twitter.com/(?:\#!\/)?)?@?([A-Za-z0-9_]{1,20})/?$#u', - $_POST['profile']['twitter'], - $twitter_matches - ); + if (!empty($_POST['profile'][$name])) { + $field_regex = preg_match( + $props['regex'], + $_POST['profile'][$name], + $field_matches + ); - if ($twitter_regex !== 1) { - $settings_errors[] = "Invalid Twitter field."; - break; + if ($field_regex !== 1) { + $settings_errors[] = $props['no-match']; + break; + } + + $field_value = $field_matches[1]; } - $user_twitter = $twitter_matches[1]; + $settings_user->{"user_{$name}"} = $field_value; } - - $settings_user->user_twitter = $user_twitter; - } - - if (isset($_POST['profile']['osu'])) { - $user_osu = ''; - - if (!empty($_POST['profile']['osu'])) { - $osu_regex = preg_match( - '#^(?:https?://osu.ppy.sh/u(?:sers)?/)?([a-zA-Z0-9-\[\]_ ]{1,20})/?$#u', - $_POST['profile']['osu'], - $osu_matches - ); - - if ($osu_regex !== 1) { - $settings_errors[] = "Invalid osu! field."; - break; - } - - $user_osu = $osu_matches[1]; - } - - $settings_user->user_osu = $user_osu; - } - - if (isset($_POST['profile']['website'])) { - $user_website = ''; - - if (!empty($_POST['profile']['website'])) { - $website_regex = preg_match( - '#^(?:https?)://(.{1,240})$#u', - $_POST['profile']['website'], - $website_matches - ); - - if ($website_regex !== 1) { - $settings_errors[] = "Invalid website field."; - break; - } - - $user_website = $website_matches[0]; - } - - $settings_user->user_website = $user_website; - } - - if (isset($_POST['profile']['youtube'])) { - $user_youtube = ''; - - if (!empty($_POST['profile']['youtube'])) { - $youtube_regex = preg_match( - '#^(?:https?://(?:www.)?youtube.com/(?:(?:user|c|channel)/)?)?' - . '(UC[a-zA-Z0-9-_]{1,22}|[a-zA-Z0-9-_%]{1,100})/?$#u', - $_POST['profile']['youtube'], - $youtube_matches - ); - - if ($youtube_regex !== 1) { - $settings_errors[] = "Invalid Youtube field."; - break; - } - - $user_youtube = $youtube_matches[1]; - } - - $settings_user->user_youtube = $user_youtube; - } - - if (isset($_POST['profile']['steam'])) { - $user_steam = ''; - - if (!empty($_POST['profile']['steam'])) { - $steam_regex = preg_match( - '#^(?:https?://(?:www.)?steamcommunity.com/(?:id|profiles)/)?([a-zA-Z0-9_-]{2,100})/?$#u', - $_POST['profile']['steam'], - $steam_matches - ); - - if ($steam_regex !== 1) { - $settings_errors[] = "Invalid Steam field."; - break; - } - - $user_steam = $steam_matches[1]; - } - - $settings_user->user_steam = $user_steam; - } - - if (isset($_POST['profile']['twitchtv'])) { - $user_twitchtv = ''; - - if (!empty($_POST['profile']['twitchtv'])) { - $twitchtv_regex = preg_match( - '#^(?:https?://(?:www.)?twitch.tv/)?([0-9A-Za-z_]{3,25})/?$#u', - $_POST['profile']['twitchtv'], - $twitchtv_matches - ); - - if ($twitchtv_regex !== 1) { - $settings_errors[] = "Invalid Twitch.TV field."; - break; - } - - $user_twitchtv = $twitchtv_matches[1]; - } - - $settings_user->user_twitchtv = $user_twitchtv; - } - - if (isset($_POST['profile']['lastfm'])) { - $user_lastfm = ''; - - if (!empty($_POST['profile']['lastfm'])) { - $lastfm_regex = preg_match( - '#^(?:https?://(?:www.)?last.fm/user/)?([a-zA-Z]{1}[a-zA-Z0-9_-]{1,14})/?$#u', - $_POST['profile']['lastfm'], - $lastfm_matches - ); - - if ($lastfm_regex !== 1) { - $settings_errors[] = "Invalid Last.fm field."; - break; - } - - $user_lastfm = $lastfm_matches[1]; - } - - $settings_user->user_lastfm = $user_lastfm; - } - - if (isset($_POST['profile']['github'])) { - $user_github = ''; - - if (!empty($_POST['profile']['github'])) { - $github_regex = preg_match( - '#^(?:https?://(?:www.)?github.com/?)?' - . '([a-zA-Z0-9](?:[a-zA-Z0-9]|-(?=[a-zA-Z0-9])){0,38})/?$#u', - $_POST['profile']['github'], - $github_matches - ); - - if ($github_regex !== 1) { - $settings_errors[] = "Invalid Github field."; - break; - } - - $user_github = $github_matches[1]; - } - - $settings_user->user_github = $user_github; - } - - if (isset($_POST['profile']['skype'])) { - $user_skype = ''; - - if (!empty($_POST['profile']['skype'])) { - $skype_regex = preg_match( - '#^((?:live:)?[a-zA-Z][\w\.,\-_@]{1,100})$#u', - $_POST['profile']['skype'], - $skype_matches - ); - - if ($skype_regex !== 1) { - $settings_errors[] = "Invalid Skype field."; - break; - } - - $user_skype = $skype_matches[1]; - } - - $settings_user->user_skype = $user_skype; - } - - if (isset($_POST['profile']['discord'])) { - $user_discord = ''; - - if (!empty($_POST['profile']['discord'])) { - $discord_regex = preg_match( - '#^(.{1,32}\#[0-9]{4})$#u', - $_POST['profile']['discord'], - $discord_matches - ); - - if ($discord_regex !== 1) { - $settings_errors[] = "Invalid Discord field."; - break; - } - - $user_discord = $discord_matches[1]; - } - - $settings_user->user_discord = $user_discord; } } @@ -335,6 +200,10 @@ $app->templating->vars(compact('settings_errors')); $app->templating->var('settings_title', $settings_modes[$settings_mode]); switch ($settings_mode) { + case 'account': + $app->templating->vars(compact('settings_profile_fields')); + break; + case 'sessions': $app->templating->var('user_sessions', $settings_user->sessions->reverse()); break; diff --git a/views/mio/settings/account.twig b/views/mio/settings/account.twig index 4e1a68bb..dbc5e60a 100644 --- a/views/mio/settings/account.twig +++ b/views/mio/settings/account.twig @@ -6,95 +6,16 @@