Diff -- /var/www/vhosts/3dshawn.com/abforge.3dshawn.com/profile.cgi
Diff

/var/www/vhosts/3dshawn.com/abforge.3dshawn.com/profile.cgi

added on local at 2026-07-01 16:01:36

Added
+209
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to 67b290175e7d
Restore this content →
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.
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#======================================================================
12use strict;
13use warnings;
14
15use lib '/var/www/vhosts/3dshawn.com/abforge.3dshawn.com';
16use CGI;
17use MODS::Template;
18use MODS::DBConnect;
19use MODS::Login;
20use MODS::ABForge::Config;
21use MODS::ABForge::Wrapper;
22use MODS::ABForge::Billing;
23
24my $q = CGI->new;
25my $form = $q->Vars;
26my $auth = MODS::Login->new;
27my $wrap = MODS::ABForge::Wrapper->new;
28my $tfile = MODS::Template->new;
29my $db = MODS::DBConnect->new;
30my $cfg = MODS::ABForge::Config->new;
31my $bill = MODS::ABForge::Billing->new;
32my $DB = $cfg->settings('database_name');
33
34$|=1;
35my $userinfo = $auth->login_verify();
36if (!$userinfo) {
37 print "Status: 302 Found\nLocation: /login.cgi\n\n";
38 exit;
39}
40my $uid = $userinfo->{user_id};
41$uid =~ s/[^0-9]//g;
42
43my $dbh = $db->db_connect();
44
45sub _esc {
46 my $s = shift;
47 $s //= '';
48 $s =~ s/\\/\\\\/g;
49 $s =~ s/'/''/g;
50 return $s;
51}
52
53sub _h_attr {
54 my $s = shift;
55 $s //= '';
56 $s =~ s/&/&/g;
57 $s =~ s/</&lt;/g;
58 $s =~ s/>/&gt;/g;
59 $s =~ s/"/&quot;/g;
60 return $s;
61}
62
63# ---- POST handlers -------------------------------------------------
64my $saved_msg = '';
65
66if (($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}
91elsif (($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 --------------------------------------------
111my $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
120my @CURRENCIES = qw(USD EUR GBP CAD AUD JPY);
121my @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);
133my (@cur_opts, @tz_opts);
134my $cur_now = $u->{default_currency} || 'USD';
135my $tz_now = $u->{timezone} || 'UTC';
136foreach my $c (@CURRENCIES) {
137 push @cur_opts, { value => $c, selected_attr => ($c eq $cur_now ? ' selected' : '') };
138}
139foreach 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).
144my $active_sessions = 0;
145my $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)
155my @entitlements = $bill->user_feature_entitlements($db, $dbh, $DB, $uid);
156my $plan_label_for_features = (@entitlements && $entitlements[0]->{plan_label})
157 || 'Free';
158my @feature_rows;
159my %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);
166foreach 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
180my $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
200print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n";
201
202my $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});
Keyboard: j next diff k previous diff g top G bottom r restore c compile-check ? help