added on local at 2026-07-01 21:47:30
| 1 | #!/usr/bin/perl | |
| 2 | #====================================================================== | |
| 3 | # RePricer -- buyer login. | |
| 4 | # | |
| 5 | # GET /buyer_login.cgi?id=<storefront_id> → render form | |
| 6 | # POST /buyer_login.cgi → authenticate + set cookie | |
| 7 | #====================================================================== | |
| 8 | use strict; | |
| 9 | use warnings; | |
| 10 | ||
| 11 | use lib '/var/www/vhosts/3dshawn.com/repricer.3dshawn.com'; | |
| 12 | use CGI; | |
| 13 | use MODS::Template; | |
| 14 | use MODS::DBConnect; | |
| 15 | use MODS::RePricer::Config; | |
| 16 | use MODS::RePricer::BuyerAuth; | |
| 17 | use MODS::RePricer::Themes; | |
| 18 | ||
| 19 | my $q = CGI->new; | |
| 20 | my $form = $q->Vars; | |
| 21 | my $tfile = MODS::Template->new; | |
| 22 | my $db = MODS::DBConnect->new; | |
| 23 | my $cfg = MODS::RePricer::Config->new; | |
| 24 | my $auth = MODS::RePricer::BuyerAuth->new; | |
| 25 | my $themes = MODS::RePricer::Themes->new; | |
| 26 | my $DB = $cfg->settings('database_name'); | |
| 27 | ||
| 28 | $| = 1; | |
| 29 | ||
| 30 | my $sid = $form->{id} || $form->{storefront_id} || 0; | |
| 31 | $sid =~ s/[^0-9]//g; | |
| 32 | ||
| 33 | my $dbh = $db->db_connect(); | |
| 34 | my $store; | |
| 35 | if ($sid) { | |
| 36 | $store = $db->db_readwrite($dbh, | |
| 37 | qq~SELECT id, name, subdomain, theme_id FROM `${DB}`.storefronts WHERE id='$sid' LIMIT 1~, | |
| 38 | $ENV{SCRIPT_NAME}, __LINE__); | |
| 39 | } | |
| 40 | unless ($store && $store->{id}) { | |
| 41 | $db->db_disconnect($dbh); | |
| 42 | print "Status: 302 Found\nLocation: /\n\n"; | |
| 43 | exit; | |
| 44 | } | |
| 45 | ||
| 46 | my $err = ''; | |
| 47 | my $email_in = ''; | |
| 48 | ||
| 49 | if (($ENV{REQUEST_METHOD} || '') eq 'POST') { | |
| 50 | $email_in = $form->{email} || ''; | |
| 51 | my $pw = $form->{password} || ''; | |
| 52 | ||
| 53 | my ($buyer, $cookie) = $auth->login($db, $dbh, $DB, $store->{id}, $email_in, $pw); | |
| 54 | if (ref($buyer) eq 'HASH' && $buyer->{error}) { | |
| 55 | $err = $buyer->{error}; | |
| 56 | } elsif ($buyer && $buyer->{id}) { | |
| 57 | # Link this visitor's tracking session to the buyer account so | |
| 58 | # the admin visitor list + chat can flag them as a Customer / | |
| 59 | # Lead and show their order count + LTV. | |
| 60 | my $track_token = ''; | |
| 61 | my $raw_cookie = $ENV{HTTP_COOKIE} || ''; | |
| 62 | if ($raw_cookie =~ /(?:^|;\s*)repricer_track=([a-f0-9-]{36})/) { | |
| 63 | $track_token = $1; | |
| 64 | } | |
| 65 | if ($track_token) { | |
| 66 | require MODS::RePricer::Tracking; | |
| 67 | my $tr = MODS::RePricer::Tracking->new; | |
| 68 | $tr->link_session_to_buyer($db, $dbh, $DB, $track_token, $buyer->{id}); | |
| 69 | } | |
| 70 | ||
| 71 | $db->db_disconnect($dbh); | |
| 72 | print "Status: 302 Found\n"; | |
| 73 | print "Set-Cookie: $cookie\n" if $cookie; | |
| 74 | print "Location: /my_orders.cgi?id=$store->{id}\n\n"; | |
| 75 | exit; | |
| 76 | } else { | |
| 77 | $err = 'login failed'; | |
| 78 | } | |
| 79 | } | |
| 80 | ||
| 81 | my $theme = $themes->by_id($store->{theme_id}); | |
| 82 | my $css_vars = $themes->css_vars($theme); | |
| 83 | ||
| 84 | $db->db_disconnect($dbh); | |
| 85 | ||
| 86 | require MODS::RePricer::StoreChrome; | |
| 87 | my $chrome_dbh = $db->db_connect(); | |
| 88 | my $chrome_html = MODS::RePricer::StoreChrome->header_html({ | |
| 89 | store_id => $store->{id}, | |
| 90 | store_name => $store->{name} || $store->{subdomain}, | |
| 91 | is_buyer_in => 0, | |
| 92 | cart_count => 0, | |
| 93 | db => $db, | |
| 94 | dbh => $chrome_dbh, | |
| 95 | DB => $DB, | |
| 96 | }); | |
| 97 | $db->db_disconnect($chrome_dbh); | |
| 98 | ||
| 99 | my $tvars = { | |
| 100 | theme_css_vars => $css_vars, | |
| 101 | store_id => $store->{id}, | |
| 102 | store_name => _h($store->{name} || $store->{subdomain}), | |
| 103 | store_chrome_html => $chrome_html, | |
| 104 | email_value => _h($email_in), | |
| 105 | has_error => $err ? 1 : 0, | |
| 106 | error_message => _h($err), | |
| 107 | signup_href => "/buyer_signup.cgi?id=$store->{id}", | |
| 108 | back_href => "/store.cgi?id=$store->{id}", | |
| 109 | }; | |
| 110 | ||
| 111 | print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n"; | |
| 112 | print join('', $tfile->template('repricer_buyer_login.html', $tvars, undef)); | |
| 113 | ||
| 114 | sub _h { | |
| 115 | my $s = shift; | |
| 116 | $s //= ''; | |
| 117 | $s =~ s/&/&/g; | |
| 118 | $s =~ s/</</g; | |
| 119 | $s =~ s/>/>/g; | |
| 120 | $s =~ s/"/"/g; | |
| 121 | return $s; | |
| 122 | } |