added on local at 2026-07-01 16:01:36
| 1 | #!/usr/bin/perl | |
| 2 | #====================================================================== | |
| 3 | # ABForge -- /preferences.cgi (notifications + UI preferences) | |
| 4 | # | |
| 5 | # Owns email notification toggles. Profile/identity (name, email, | |
| 6 | # currency, time zone, 2FA, sessions, account close) lives on | |
| 7 | # /profile.cgi. Replaces the old tabbed /settings.cgi. | |
| 8 | #====================================================================== | |
| 9 | use strict; | |
| 10 | use warnings; | |
| 11 | ||
| 12 | use lib '/var/www/vhosts/3dshawn.com/abforge.3dshawn.com'; | |
| 13 | use CGI; | |
| 14 | use MODS::Template; | |
| 15 | use MODS::DBConnect; | |
| 16 | use MODS::Login; | |
| 17 | use MODS::ABForge::Config; | |
| 18 | use MODS::ABForge::Wrapper; | |
| 19 | ||
| 20 | my $q = CGI->new; | |
| 21 | my $form = $q->Vars; | |
| 22 | my $auth = MODS::Login->new; | |
| 23 | my $wrap = MODS::ABForge::Wrapper->new; | |
| 24 | my $tfile = MODS::Template->new; | |
| 25 | my $db = MODS::DBConnect->new; | |
| 26 | my $cfg = MODS::ABForge::Config->new; | |
| 27 | my $DB = $cfg->settings('database_name'); | |
| 28 | ||
| 29 | $|=1; | |
| 30 | my $userinfo = $auth->login_verify(); | |
| 31 | if (!$userinfo) { | |
| 32 | print "Status: 302 Found\nLocation: /login.cgi\n\n"; | |
| 33 | exit; | |
| 34 | } | |
| 35 | my $uid = $userinfo->{user_id}; | |
| 36 | $uid =~ s/[^0-9]//g; | |
| 37 | ||
| 38 | my $dbh = $db->db_connect(); | |
| 39 | ||
| 40 | # ---- Notification prefs schema (mirrored into JSON in user_settings) - | |
| 41 | my @NOTIF_KEYS = qw( | |
| 42 | sale_alerts | |
| 43 | platform_health | |
| 44 | review_digest | |
| 45 | ab_winners | |
| 46 | weekly_summary | |
| 47 | marketing | |
| 48 | ); | |
| 49 | my %NOTIF_DEFAULT = ( | |
| 50 | sale_alerts => 1, | |
| 51 | platform_health => 1, | |
| 52 | review_digest => 1, | |
| 53 | ab_winners => 1, | |
| 54 | weekly_summary => 1, | |
| 55 | marketing => 0, | |
| 56 | ); | |
| 57 | ||
| 58 | sub _esc { | |
| 59 | my $s = shift; | |
| 60 | $s //= ''; | |
| 61 | $s =~ s/\\/\\\\/g; | |
| 62 | $s =~ s/'/''/g; | |
| 63 | return $s; | |
| 64 | } | |
| 65 | ||
| 66 | sub _parse_notif_json { | |
| 67 | my ($raw) = @_; | |
| 68 | my %out = %NOTIF_DEFAULT; | |
| 69 | return \%out unless $raw && $raw =~ /\S/; | |
| 70 | foreach my $k (@NOTIF_KEYS) { | |
| 71 | if ($raw =~ /"\Q$k\E"\s*:\s*(true|false|0|1)/i) { | |
| 72 | my $v = lc $1; | |
| 73 | $out{$k} = ($v eq 'true' || $v eq '1') ? 1 : 0; | |
| 74 | } | |
| 75 | } | |
| 76 | return \%out; | |
| 77 | } | |
| 78 | ||
| 79 | sub _build_notif_json { | |
| 80 | my ($prefs) = @_; | |
| 81 | my @parts; | |
| 82 | foreach my $k (@NOTIF_KEYS) { | |
| 83 | my $v = $prefs->{$k} ? 1 : 0; | |
| 84 | push @parts, qq{"$k":$v}; | |
| 85 | } | |
| 86 | return '{' . join(',', @parts) . '}'; | |
| 87 | } | |
| 88 | ||
| 89 | # ---- POST handler -------------------------------------------------- | |
| 90 | my $saved_msg = ''; | |
| 91 | ||
| 92 | if (($form->{action} || '') eq 'save_notifications') { | |
| 93 | my %prefs; | |
| 94 | foreach my $k (@NOTIF_KEYS) { | |
| 95 | $prefs{$k} = ($form->{"notif_$k"} ? 1 : 0); | |
| 96 | } | |
| 97 | my $json = _build_notif_json(\%prefs); | |
| 98 | ||
| 99 | $db->db_readwrite($dbh, qq~ | |
| 100 | INSERT INTO `${DB}`.user_settings | |
| 101 | SET user_id='$uid', | |
| 102 | notification_prefs='~ . _esc($json) . qq~', | |
| 103 | marketing_opt_in='~ . ($prefs{marketing} ? 1 : 0) . qq~' | |
| 104 | ON DUPLICATE KEY UPDATE | |
| 105 | notification_prefs=VALUES(notification_prefs), | |
| 106 | marketing_opt_in=VALUES(marketing_opt_in) | |
| 107 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 108 | $saved_msg = 'Notification preferences saved.'; | |
| 109 | } | |
| 110 | ||
| 111 | # ---- Load current state -------------------------------------------- | |
| 112 | my $s = $db->db_readwrite($dbh, qq~ | |
| 113 | SELECT notification_prefs, marketing_opt_in | |
| 114 | FROM `${DB}`.user_settings | |
| 115 | WHERE user_id='$uid' | |
| 116 | ~, $ENV{SCRIPT_NAME}, __LINE__) || {}; | |
| 117 | my $prefs = _parse_notif_json($s->{notification_prefs}); | |
| 118 | $prefs->{marketing} = $s->{marketing_opt_in} ? 1 : 0 if exists $s->{marketing_opt_in}; | |
| 119 | ||
| 120 | $db->db_disconnect($dbh); | |
| 121 | ||
| 122 | my $tvars = { | |
| 123 | user_id => $uid, | |
| 124 | saved_msg => $saved_msg, | |
| 125 | has_saved_msg => $saved_msg ? 1 : 0, | |
| 126 | ||
| 127 | notif_sale_alerts_on => $prefs->{sale_alerts} ? 'on' : '', | |
| 128 | notif_platform_health_on => $prefs->{platform_health} ? 'on' : '', | |
| 129 | notif_review_digest_on => $prefs->{review_digest} ? 'on' : '', | |
| 130 | notif_ab_winners_on => $prefs->{ab_winners} ? 'on' : '', | |
| 131 | notif_weekly_summary_on => $prefs->{weekly_summary} ? 'on' : '', | |
| 132 | notif_marketing_on => $prefs->{marketing} ? 'on' : '', | |
| 133 | ||
| 134 | notif_sale_alerts_checked => $prefs->{sale_alerts} ? 'checked' : '', | |
| 135 | notif_platform_health_checked => $prefs->{platform_health} ? 'checked' : '', | |
| 136 | notif_review_digest_checked => $prefs->{review_digest} ? 'checked' : '', | |
| 137 | notif_ab_winners_checked => $prefs->{ab_winners} ? 'checked' : '', | |
| 138 | notif_weekly_summary_checked => $prefs->{weekly_summary} ? 'checked' : '', | |
| 139 | notif_marketing_checked => $prefs->{marketing} ? 'checked' : '', | |
| 140 | }; | |
| 141 | ||
| 142 | print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n"; | |
| 143 | ||
| 144 | my $body = join('', $tfile->template('abforge_preferences.html', $tvars, $userinfo)); | |
| 145 | ||
| 146 | $wrap->render({ | |
| 147 | userinfo => $userinfo, | |
| 148 | page_key => 'preferences', | |
| 149 | title => 'Preferences', | |
| 150 | body => $body, | |
| 151 | }); |