added on local at 2026-07-01 16:01:36
| 1 | #!/usr/bin/perl | |
| 2 | #====================================================================== | |
| 3 | # ABForge -- /profile.cgi (account identity) | |
| 4 | # | |
| 5 | # Owns: display name, email, default currency, time zone, password | |
| 6 | # change link, 2FA, email verification, active sessions, account close, | |
| 7 | # and (owner only) "Available Features" + Team Members links. | |
| 8 | # | |
| 9 | # Notification toggles and other UI prefs live on /preferences.cgi. | |
| 10 | # Replaces the old tabbed /settings.cgi. | |
| 11 | #====================================================================== | |
| 12 | use strict; | |
| 13 | use warnings; | |
| 14 | ||
| 15 | use lib '/var/www/vhosts/3dshawn.com/abforge.3dshawn.com'; | |
| 16 | use CGI; | |
| 17 | use MODS::Template; | |
| 18 | use MODS::DBConnect; | |
| 19 | use MODS::Login; | |
| 20 | use MODS::ABForge::Config; | |
| 21 | use MODS::ABForge::Wrapper; | |
| 22 | use MODS::ABForge::Billing; | |
| 23 | ||
| 24 | my $q = CGI->new; | |
| 25 | my $form = $q->Vars; | |
| 26 | my $auth = MODS::Login->new; | |
| 27 | my $wrap = MODS::ABForge::Wrapper->new; | |
| 28 | my $tfile = MODS::Template->new; | |
| 29 | my $db = MODS::DBConnect->new; | |
| 30 | my $cfg = MODS::ABForge::Config->new; | |
| 31 | my $bill = MODS::ABForge::Billing->new; | |
| 32 | my $DB = $cfg->settings('database_name'); | |
| 33 | ||
| 34 | $|=1; | |
| 35 | my $userinfo = $auth->login_verify(); | |
| 36 | if (!$userinfo) { | |
| 37 | print "Status: 302 Found\nLocation: /login.cgi\n\n"; | |
| 38 | exit; | |
| 39 | } | |
| 40 | my $uid = $userinfo->{user_id}; | |
| 41 | $uid =~ s/[^0-9]//g; | |
| 42 | ||
| 43 | my $dbh = $db->db_connect(); | |
| 44 | ||
| 45 | sub _esc { | |
| 46 | my $s = shift; | |
| 47 | $s //= ''; | |
| 48 | $s =~ s/\\/\\\\/g; | |
| 49 | $s =~ s/'/''/g; | |
| 50 | return $s; | |
| 51 | } | |
| 52 | ||
| 53 | sub _h_attr { | |
| 54 | my $s = shift; | |
| 55 | $s //= ''; | |
| 56 | $s =~ s/&/&/g; | |
| 57 | $s =~ s/</</g; | |
| 58 | $s =~ s/>/>/g; | |
| 59 | $s =~ s/"/"/g; | |
| 60 | return $s; | |
| 61 | } | |
| 62 | ||
| 63 | # ---- POST handlers ------------------------------------------------- | |
| 64 | my $saved_msg = ''; | |
| 65 | ||
| 66 | if (($form->{action} || '') eq 'save_account') { | |
| 67 | my $name = $form->{display_name} // ''; | |
| 68 | my $email = $form->{email} // ''; | |
| 69 | my $cur = uc($form->{default_currency} // 'USD'); | |
| 70 | my $tz = $form->{timezone} // 'UTC'; | |
| 71 | ||
| 72 | $name =~ s/^\s+|\s+$//g; | |
| 73 | $email =~ s/^\s+|\s+$//g; | |
| 74 | $cur = substr($cur, 0, 3); | |
| 75 | $tz = substr($tz, 0, 64); | |
| 76 | ||
| 77 | if ($name eq '' || $email !~ /\@/) { | |
| 78 | $saved_msg = 'Please provide a display name and a valid email address.'; | |
| 79 | } else { | |
| 80 | $db->db_readwrite($dbh, qq~ | |
| 81 | UPDATE `${DB}`.users | |
| 82 | SET display_name='~ . _esc($name) . qq~', | |
| 83 | email='~ . _esc($email) . qq~', | |
| 84 | default_currency='~ . _esc($cur) . qq~', | |
| 85 | timezone='~ . _esc($tz) . qq~' | |
| 86 | WHERE id='$uid' | |
| 87 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 88 | $saved_msg = 'Profile saved.'; | |
| 89 | } | |
| 90 | } | |
| 91 | elsif (($form->{action} || '') eq 'resend_verification') { | |
| 92 | require MODS::ABForge::EmailVerify; | |
| 93 | my $u_check = $db->db_readwrite($dbh, qq~ | |
| 94 | SELECT id, email, display_name, email_verified_at | |
| 95 | FROM `${DB}`.users WHERE id='$uid' LIMIT 1 | |
| 96 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 97 | if ($u_check && $u_check->{id} && !$u_check->{email_verified_at}) { | |
| 98 | my $r = MODS::ABForge::EmailVerify->send_for_user( | |
| 99 | $db, $dbh, $DB, | |
| 100 | { id => $u_check->{id}, email => $u_check->{email}, display_name => $u_check->{display_name} }, | |
| 101 | ); | |
| 102 | $saved_msg = ($r && $r->{ok}) | |
| 103 | ? 'Verification email sent. Check your inbox (and spam folder).' | |
| 104 | : 'Could not send verification email: ' . ($r->{error} || 'unknown error'); | |
| 105 | } else { | |
| 106 | $saved_msg = 'Your email is already verified.'; | |
| 107 | } | |
| 108 | } | |
| 109 | ||
| 110 | # ---- Load current state -------------------------------------------- | |
| 111 | my $u = $db->db_readwrite($dbh, qq~ | |
| 112 | SELECT display_name, email, default_currency, timezone, | |
| 113 | two_factor_enabled, | |
| 114 | email_verified_at, | |
| 115 | DATE_FORMAT(email_verified_at, '%b %e, %Y') AS verified_on | |
| 116 | FROM `${DB}`.users | |
| 117 | WHERE id='$uid' | |
| 118 | ~, $ENV{SCRIPT_NAME}, __LINE__) || {}; | |
| 119 | ||
| 120 | my @CURRENCIES = qw(USD EUR GBP CAD AUD JPY); | |
| 121 | my @TIMEZONES = ( | |
| 122 | 'UTC', | |
| 123 | 'America/Los_Angeles', | |
| 124 | 'America/Denver', | |
| 125 | 'America/Chicago', | |
| 126 | 'America/New_York', | |
| 127 | 'Europe/London', | |
| 128 | 'Europe/Berlin', | |
| 129 | 'Europe/Paris', | |
| 130 | 'Asia/Tokyo', | |
| 131 | 'Australia/Sydney', | |
| 132 | ); | |
| 133 | my (@cur_opts, @tz_opts); | |
| 134 | my $cur_now = $u->{default_currency} || 'USD'; | |
| 135 | my $tz_now = $u->{timezone} || 'UTC'; | |
| 136 | foreach my $c (@CURRENCIES) { | |
| 137 | push @cur_opts, { value => $c, selected_attr => ($c eq $cur_now ? ' selected' : '') }; | |
| 138 | } | |
| 139 | foreach my $t (@TIMEZONES) { | |
| 140 | push @tz_opts, { value => $t, selected_attr => ($t eq $tz_now ? ' selected' : '') }; | |
| 141 | } | |
| 142 | ||
| 143 | # Active sessions (this account's; visitor sessions live in their own table). | |
| 144 | my $active_sessions = 0; | |
| 145 | my $r_sess = $db->db_readwrite($dbh, qq~ | |
| 146 | SELECT COUNT(*) AS n FROM `${DB}`.user_sessions | |
| 147 | WHERE user_id='$uid' | |
| 148 | AND revoked_at IS NULL | |
| 149 | AND expires_at > NOW() | |
| 150 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 151 | $active_sessions = $r_sess->{n} if $r_sess && defined $r_sess->{n}; | |
| 152 | ||
| 153 | # ---- Plan entitlement state for the "Available Features" panel ----- | |
| 154 | # (owner-only -- the template guards on $is_owner) | |
| 155 | my @entitlements = $bill->user_feature_entitlements($db, $dbh, $DB, $uid); | |
| 156 | my $plan_label_for_features = (@entitlements && $entitlements[0]->{plan_label}) | |
| 157 | || 'Free'; | |
| 158 | my @feature_rows; | |
| 159 | my %ICON_SVG = ( | |
| 160 | send => '<path d="M22 2 11 13"/><path d="m22 2-7 20-4-9-9-4 20-7Z"/>', | |
| 161 | store => '<path d="m3 9 1-5h16l1 5"/><path d="M5 9v11a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V9"/>', | |
| 162 | activity => '<path d="M22 12h-4l-3 9L9 3l-3 9H2"/>', | |
| 163 | chart => '<path d="M3 3v18h18"/><path d="m7 14 4-4 4 4 5-5"/>', | |
| 164 | people => '<path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/>', | |
| 165 | ); | |
| 166 | foreach my $f (@entitlements) { | |
| 167 | push @feature_rows, { | |
| 168 | key => $f->{key}, | |
| 169 | name => $f->{name}, | |
| 170 | blurb => $f->{blurb}, | |
| 171 | icon_svg => $ICON_SVG{ $f->{icon} } || $ICON_SVG{chart}, | |
| 172 | included => $f->{included} ? 1 : 0, | |
| 173 | locked => $f->{locked} ? 1 : 0, | |
| 174 | module_href => '/' . ($f->{module} || 'dashboard') . '.cgi', | |
| 175 | }; | |
| 176 | } | |
| 177 | ||
| 178 | $db->db_disconnect($dbh); | |
| 179 | ||
| 180 | my $tvars = { | |
| 181 | user_id => $uid, | |
| 182 | saved_msg => $saved_msg, | |
| 183 | has_saved_msg => $saved_msg ? 1 : 0, | |
| 184 | ||
| 185 | display_name => _h_attr($u->{display_name}), | |
| 186 | email => _h_attr($u->{email}), | |
| 187 | currency_options => \@cur_opts, | |
| 188 | timezone_options => \@tz_opts, | |
| 189 | ||
| 190 | email_verified => $u->{email_verified_at} ? 1 : 0, | |
| 191 | email_verified_label => $u->{email_verified_at} ? ('Verified ' . ($u->{verified_on} || '')) : 'Not verified yet', | |
| 192 | two_factor_enabled => $u->{two_factor_enabled} ? 1 : 0, | |
| 193 | active_session_count => $active_sessions, | |
| 194 | ||
| 195 | plan_label_for_features => $plan_label_for_features, | |
| 196 | features => \@feature_rows, | |
| 197 | has_features => scalar(@feature_rows) ? 1 : 0, | |
| 198 | }; | |
| 199 | ||
| 200 | print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n"; | |
| 201 | ||
| 202 | my $body = join('', $tfile->template('abforge_profile.html', $tvars, $userinfo)); | |
| 203 | ||
| 204 | $wrap->render({ | |
| 205 | userinfo => $userinfo, | |
| 206 | page_key => 'my_profile', | |
| 207 | title => 'My Profile', | |
| 208 | body => $body, | |
| 209 | }); |