added on local at 2026-07-01 21:47:18
| 1 | #!/usr/bin/perl | |
| 2 | #====================================================================== | |
| 3 | # WebSTLs - Save the per-user sidebar-collapsed preference. | |
| 4 | # | |
| 5 | # Called by the toggle button in the wrapper template (fetch POST with | |
| 6 | # keepalive:true). Writes user_settings.sidebar_collapsed for the | |
| 7 | # logged-in user. Idempotent -- safe to call repeatedly. | |
| 8 | #====================================================================== | |
| 9 | use strict; | |
| 10 | use warnings; | |
| 11 | ||
| 12 | use lib '/var/www/vhosts/3dshawn.com/repricer.3dshawn.com'; | |
| 13 | use CGI; | |
| 14 | use MODS::DBConnect; | |
| 15 | use MODS::Login; | |
| 16 | use MODS::RePricer::Config; | |
| 17 | ||
| 18 | my $q = CGI->new; | |
| 19 | my $form = $q->Vars; | |
| 20 | my $auth = MODS::Login->new; | |
| 21 | my $db = MODS::DBConnect->new; | |
| 22 | my $cfg = MODS::RePricer::Config->new; | |
| 23 | my $DB = $cfg->settings('database_name'); | |
| 24 | ||
| 25 | $| = 1; | |
| 26 | ||
| 27 | # POST only -- prevents drive-by GETs from changing user prefs. | |
| 28 | if (($ENV{REQUEST_METHOD} || '') ne 'POST') { | |
| 29 | print "Status: 405 Method Not Allowed\nContent-Type: text/plain\n\nPOST required\n"; | |
| 30 | exit; | |
| 31 | } | |
| 32 | ||
| 33 | my $userinfo = $auth->login_verify(); | |
| 34 | if (!$userinfo || !$userinfo->{user_id}) { | |
| 35 | print "Status: 401 Unauthorized\nContent-Type: text/plain\n\nlogin required\n"; | |
| 36 | exit; | |
| 37 | } | |
| 38 | ||
| 39 | my $uid = $userinfo->{user_id}; | |
| 40 | $uid =~ s/[^0-9]//g; | |
| 41 | ||
| 42 | my $dbh = $db->db_connect(); | |
| 43 | ||
| 44 | # Verify the table columns we want to write actually exist. The | |
| 45 | # wrapper renderer also guards this; a fresh DB without the migration | |
| 46 | # would otherwise 500 here too. sidebar_mobile_open was added later | |
| 47 | # than sidebar_collapsed -- accept it if present, otherwise silently | |
| 48 | # drop the mobile field. | |
| 49 | my $has_collapsed = $db->db_readwrite($dbh, qq~ | |
| 50 | SELECT COUNT(*) AS n FROM information_schema.columns | |
| 51 | WHERE table_schema='$DB' | |
| 52 | AND table_name='user_settings' | |
| 53 | AND column_name='sidebar_collapsed' | |
| 54 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 55 | my $has_mobile = $db->db_readwrite($dbh, qq~ | |
| 56 | SELECT COUNT(*) AS n FROM information_schema.columns | |
| 57 | WHERE table_schema='$DB' | |
| 58 | AND table_name='user_settings' | |
| 59 | AND column_name='sidebar_mobile_open' | |
| 60 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 61 | ||
| 62 | if (!$has_collapsed || !$has_collapsed->{n}) { | |
| 63 | $db->db_disconnect($dbh); | |
| 64 | print "Status: 503 Service Unavailable\nContent-Type: text/plain\n\nmigration pending\n"; | |
| 65 | exit; | |
| 66 | } | |
| 67 | ||
| 68 | # Build the UPSERT only over fields the client actually sent. A desktop | |
| 69 | # toggle does NOT touch the mobile column and vice versa, so each | |
| 70 | # breakpoint can drive its own state without stomping the other. | |
| 71 | my @ins_cols = ('user_id'); | |
| 72 | my @ins_vals = ("'$uid'"); | |
| 73 | my @upd_clause; | |
| 74 | ||
| 75 | if (exists $form->{sidebar_collapsed}) { | |
| 76 | my $v = ($form->{sidebar_collapsed} eq '1') ? 1 : 0; | |
| 77 | push @ins_cols, 'sidebar_collapsed'; | |
| 78 | push @ins_vals, "'$v'"; | |
| 79 | push @upd_clause, "sidebar_collapsed='$v'"; | |
| 80 | } | |
| 81 | if ($has_mobile && $has_mobile->{n} && exists $form->{sidebar_mobile_open}) { | |
| 82 | my $v = ($form->{sidebar_mobile_open} eq '1') ? 1 : 0; | |
| 83 | push @ins_cols, 'sidebar_mobile_open'; | |
| 84 | push @ins_vals, "'$v'"; | |
| 85 | push @upd_clause, "sidebar_mobile_open='$v'"; | |
| 86 | } | |
| 87 | ||
| 88 | if (@upd_clause) { | |
| 89 | push @upd_clause, 'updated_at=NOW()'; | |
| 90 | my $cols_sql = join(',', @ins_cols); | |
| 91 | my $vals_sql = join(',', @ins_vals); | |
| 92 | my $upd_sql = join(',', @upd_clause); | |
| 93 | $db->db_readwrite($dbh, qq~ | |
| 94 | INSERT INTO `${DB}`.user_settings ($cols_sql) | |
| 95 | VALUES ($vals_sql) | |
| 96 | ON DUPLICATE KEY UPDATE $upd_sql | |
| 97 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 98 | } | |
| 99 | ||
| 100 | $db->db_disconnect($dbh); | |
| 101 | ||
| 102 | print "Content-Type: text/plain\nCache-Control: no-store\n\nok\n"; |