Diff -- /var/www/vhosts/3dshawn.com/repricer.3dshawn.com/_attic/marketplace_action.cgi
Diff

/var/www/vhosts/3dshawn.com/repricer.3dshawn.com/_attic/marketplace_action.cgi

added on local at 2026-07-01 21:47:33

Added
+115
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to 47587cffad97
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# RePricer - Marketplace push actions.
4#
5# Single POST chokepoint for cancel / retry on a marketplace_pushes
6# row. All actions are owner-scoped: the row's user_id must match the
7# logged-in user (or admin via impersonation).
8#
9# Actions:
10# act=cancel id=<push_id> -- delete the row (only if status=pending)
11# act=retry id=<push_id> -- flip status back to pending so the
12# worker re-attempts on its next sweep
13#======================================================================
14use strict;
15use warnings;
16
17use lib '/var/www/vhosts/3dshawn.com/repricer.3dshawn.com';
18use CGI;
19use MODS::DBConnect;
20use MODS::Login;
21use MODS::RePricer::Config;
22
23my $q = CGI->new;
24my $form = $q->Vars;
25my $auth = MODS::Login->new;
26my $db = MODS::DBConnect->new;
27my $cfg = MODS::RePricer::Config->new;
28my $DB = $cfg->settings('database_name');
29
30$| = 1;
31
32# POST-only. Drive-by GETs cannot mutate state.
33if (($ENV{REQUEST_METHOD} || '') ne 'POST') {
34 print "Status: 405 Method Not Allowed\nContent-Type: text/plain\n\nPOST required\n";
35 exit;
36}
37
38my $userinfo = $auth->login_verify();
39if (!$userinfo) {
40 print "Status: 302 Found\nLocation: /login.cgi\n\n";
41 exit;
42}
43require MODS::RePricer::Permissions;
44MODS::RePricer::Permissions->new->require_feature($userinfo, 'manage_marketplaces');
45my $uid = $userinfo->{user_id};
46$uid =~ s/[^0-9]//g;
47
48my $act = lc($form->{act} || '');
49$act =~ s/[^a-z_]//g;
50
51my $push_id = $form->{id} || 0;
52$push_id =~ s/[^0-9]//g;
53
54# Bail home if either is missing or invalid.
55if (!$act || !$push_id) {
56 print "Status: 302 Found\nLocation: /welcome.cgi\n\n";
57 exit;
58}
59
60my $dbh = $db->db_connect();
61
62# Guard against missing table on a fresh DB.
63my $have_tbl = $db->db_readwrite($dbh, qq~
64 SELECT COUNT(*) AS n FROM information_schema.tables
65 WHERE table_schema='$DB' AND table_name='marketplace_pushes'
66~, $ENV{SCRIPT_NAME}, __LINE__);
67unless ($have_tbl && $have_tbl->{n}) {
68 $db->db_disconnect($dbh);
69 print "Status: 302 Found\nLocation: /welcome.cgi\n\n";
70 exit;
71}
72
73# Verify ownership. The row must belong to the logged-in user. Admins
74# acting-as another user inherit that user's row visibility via the
75# impersonation swap in MODS::Login.
76my $row = $db->db_readwrite($dbh, qq~
77 SELECT id, user_id, status
78 FROM `${DB}`.marketplace_pushes
79 WHERE id='$push_id' AND user_id='$uid'
80 LIMIT 1
81~, $ENV{SCRIPT_NAME}, __LINE__);
82
83unless ($row && $row->{id}) {
84 $db->db_disconnect($dbh);
85 print "Status: 302 Found\nLocation: /welcome.cgi\n\n";
86 exit;
87}
88
89if ($act eq 'cancel') {
90 # Only cancel pending pushes -- once status=success the listing is
91 # live on the platform and "cancel" would not actually undo it.
92 if (($row->{status} || '') eq 'pending') {
93 $db->db_readwrite($dbh, qq~
94 DELETE FROM `${DB}`.marketplace_pushes
95 WHERE id='$push_id' AND user_id='$uid' AND status='pending'
96 ~, $ENV{SCRIPT_NAME}, __LINE__);
97 }
98}
99elsif ($act eq 'retry') {
100 # Retry: only valid on failed rows. Flip back to pending and the
101 # worker picks it up on its next sweep. We do not touch the error_
102 # message column so the history of the prior failure is preserved
103 # until the next attempt overwrites it.
104 if (($row->{status} || '') eq 'failed') {
105 $db->db_readwrite($dbh, qq~
106 UPDATE `${DB}`.marketplace_pushes
107 SET status='pending'
108 WHERE id='$push_id' AND user_id='$uid' AND status='failed'
109 ~, $ENV{SCRIPT_NAME}, __LINE__);
110 }
111}
112# Any other act value: no-op, just redirect home.
113
114$db->db_disconnect($dbh);
115print "Status: 302 Found\nLocation: /welcome.cgi\n\n";
Keyboard: j next diff k previous diff g top G bottom r restore c compile-check ? help