added on local at 2026-07-01 21:47:31
| 1 | #!/usr/bin/perl | |
| 2 | #====================================================================== | |
| 3 | # RePricer -- buyer signup. | |
| 4 | # | |
| 5 | # GET /buyer_signup.cgi?id=<storefront_id> → render form | |
| 6 | # POST /buyer_signup.cgi → create account + login | |
| 7 | # | |
| 8 | # On success: 302 to /my_orders.cgi?id=<storefront_id> with the | |
| 9 | # session cookie set. On failure: re-renders the form with an error. | |
| 10 | #====================================================================== | |
| 11 | use strict; | |
| 12 | use warnings; | |
| 13 | ||
| 14 | use lib '/var/www/vhosts/3dshawn.com/repricer.3dshawn.com'; | |
| 15 | use CGI; | |
| 16 | use MODS::Template; | |
| 17 | use MODS::DBConnect; | |
| 18 | use MODS::RePricer::Config; | |
| 19 | use MODS::RePricer::BuyerAuth; | |
| 20 | use MODS::RePricer::Themes; | |
| 21 | ||
| 22 | my $q = CGI->new; | |
| 23 | my $form = $q->Vars; | |
| 24 | my $tfile = MODS::Template->new; | |
| 25 | my $db = MODS::DBConnect->new; | |
| 26 | my $cfg = MODS::RePricer::Config->new; | |
| 27 | my $auth = MODS::RePricer::BuyerAuth->new; | |
| 28 | my $themes = MODS::RePricer::Themes->new; | |
| 29 | my $DB = $cfg->settings('database_name'); | |
| 30 | ||
| 31 | $| = 1; | |
| 32 | ||
| 33 | my $sid = $form->{id} || $form->{storefront_id} || 0; | |
| 34 | $sid =~ s/[^0-9]//g; | |
| 35 | ||
| 36 | my $dbh = $db->db_connect(); | |
| 37 | ||
| 38 | # Resolve the storefront so we can theme the page + redirect back. | |
| 39 | my $store; | |
| 40 | if ($sid) { | |
| 41 | $store = $db->db_readwrite($dbh, | |
| 42 | qq~SELECT id, name, subdomain, theme_id FROM `${DB}`.storefronts WHERE id='$sid' LIMIT 1~, | |
| 43 | $ENV{SCRIPT_NAME}, __LINE__); | |
| 44 | } | |
| 45 | unless ($store && $store->{id}) { | |
| 46 | $db->db_disconnect($dbh); | |
| 47 | print "Status: 302 Found\nLocation: /\n\n"; | |
| 48 | exit; | |
| 49 | } | |
| 50 | ||
| 51 | my $err = ''; | |
| 52 | my $email_in = ''; | |
| 53 | my $name_in = ''; | |
| 54 | ||
| 55 | if (($ENV{REQUEST_METHOD} || '') eq 'POST') { | |
| 56 | $email_in = $form->{email} || ''; | |
| 57 | $name_in = $form->{display_name} || ''; | |
| 58 | my $pw = $form->{password} || ''; | |
| 59 | ||
| 60 | my $result = $auth->signup($db, $dbh, $DB, $store->{id}, $email_in, $pw, $name_in); | |
| 61 | if (ref($result) eq 'HASH' && $result->{error}) { | |
| 62 | $err = $result->{error}; | |
| 63 | } else { | |
| 64 | # Auto-login the new account. | |
| 65 | my ($buyer, $cookie) = $auth->login($db, $dbh, $DB, $store->{id}, $email_in, $pw); | |
| 66 | ||
| 67 | # Link the visitor's existing tracking session to the new | |
| 68 | # buyer_account so admin sees them flip from Guest -> Lead. | |
| 69 | if ($buyer && $buyer->{id}) { | |
| 70 | my $track_token = ''; | |
| 71 | my $raw_cookie = $ENV{HTTP_COOKIE} || ''; | |
| 72 | if ($raw_cookie =~ /(?:^|;\s*)repricer_track=([a-f0-9-]{36})/) { | |
| 73 | $track_token = $1; | |
| 74 | } | |
| 75 | if ($track_token) { | |
| 76 | require MODS::RePricer::Tracking; | |
| 77 | my $tr = MODS::RePricer::Tracking->new; | |
| 78 | $tr->link_session_to_buyer($db, $dbh, $DB, $track_token, $buyer->{id}); | |
| 79 | } | |
| 80 | ||
| 81 | # Backfill any historic buyer_credit_ledger rows that were | |
| 82 | # issued to this email while it was unregistered, so the new | |
| 83 | # account inherits the credit balance instantly. Self-guards | |
| 84 | # against the table being absent. | |
| 85 | eval { | |
| 86 | require MODS::RePricer::BuyerCredits; | |
| 87 | my $bc = MODS::RePricer::BuyerCredits->new; | |
| 88 | $bc->link_email_to_account($db, $dbh, $DB, | |
| 89 | $store->{id}, $buyer->{id}, $email_in); | |
| 90 | }; | |
| 91 | } | |
| 92 | ||
| 93 | $db->db_disconnect($dbh); | |
| 94 | print "Status: 302 Found\n"; | |
| 95 | print "Set-Cookie: $cookie\n" if $cookie; | |
| 96 | print "Location: /my_orders.cgi?id=$store->{id}\n\n"; | |
| 97 | exit; | |
| 98 | } | |
| 99 | } | |
| 100 | ||
| 101 | my $theme = $themes->by_id($store->{theme_id}); | |
| 102 | my $css_vars = $themes->css_vars($theme); | |
| 103 | ||
| 104 | $db->db_disconnect($dbh); | |
| 105 | ||
| 106 | require MODS::RePricer::StoreChrome; | |
| 107 | my $chrome_dbh = $db->db_connect(); | |
| 108 | my $chrome_html = MODS::RePricer::StoreChrome->header_html({ | |
| 109 | store_id => $store->{id}, | |
| 110 | store_name => $store->{name} || $store->{subdomain}, | |
| 111 | is_buyer_in => 0, | |
| 112 | cart_count => 0, | |
| 113 | db => $db, | |
| 114 | dbh => $chrome_dbh, | |
| 115 | DB => $DB, | |
| 116 | }); | |
| 117 | $db->db_disconnect($chrome_dbh); | |
| 118 | ||
| 119 | my $tvars = { | |
| 120 | theme_css_vars => $css_vars, | |
| 121 | store_id => $store->{id}, | |
| 122 | store_name => _h($store->{name} || $store->{subdomain}), | |
| 123 | store_chrome_html => $chrome_html, | |
| 124 | email_value => _h($email_in), | |
| 125 | name_value => _h($name_in), | |
| 126 | has_error => $err ? 1 : 0, | |
| 127 | error_message => _h($err), | |
| 128 | login_href => "/buyer_login.cgi?id=$store->{id}", | |
| 129 | back_href => "/store.cgi?id=$store->{id}", | |
| 130 | }; | |
| 131 | ||
| 132 | print "Content-Type: text/html; charset=utf-8\nCache-Control: no-cache\n\n"; | |
| 133 | print join('', $tfile->template('repricer_buyer_signup.html', $tvars, undef)); | |
| 134 | ||
| 135 | sub _h { | |
| 136 | my $s = shift; | |
| 137 | $s //= ''; | |
| 138 | $s =~ s/&/&/g; | |
| 139 | $s =~ s/</</g; | |
| 140 | $s =~ s/>/>/g; | |
| 141 | $s =~ s/"/"/g; | |
| 142 | return $s; | |
| 143 | } |