added on local at 2026-07-01 22:09:44
| 1 | #!/usr/bin/perl | |
| 2 | #====================================================================== | |
| 3 | # ShopCart -- "permanent delete" / empty trash. | |
| 4 | # | |
| 5 | # POST params: | |
| 6 | # all=1 -> purge every removed model the user owns | |
| 7 | # model_id=N -> purge just that one (must be status='removed') | |
| 8 | # | |
| 9 | # "Purge" here means: set models.purged_at = NOW(). The row stays in | |
| 10 | # the database -- nothing is destroyed. Every user-facing query filters | |
| 11 | # WHERE purged_at IS NULL, so the model becomes invisible to the | |
| 12 | # creator (and stayed invisible to buyers since the soft-delete step). | |
| 13 | # Admin tools (no purged_at filter) can still inspect / recover it. | |
| 14 | # | |
| 15 | # Returns JSON { success: 1, purged: <count> } or { success: 0, error }. | |
| 16 | #====================================================================== | |
| 17 | use strict; | |
| 18 | use warnings; | |
| 19 | ||
| 20 | use lib '/var/www/vhosts/3dshawn.com/shop.3dshawn.com'; | |
| 21 | use CGI; | |
| 22 | use MODS::DBConnect; | |
| 23 | use MODS::Login; | |
| 24 | use MODS::ShopCart::Config; | |
| 25 | ||
| 26 | $| = 1; | |
| 27 | ||
| 28 | my $q = CGI->new; | |
| 29 | my $db = MODS::DBConnect->new; | |
| 30 | my $auth = MODS::Login->new; | |
| 31 | my $config = MODS::ShopCart::Config->new; | |
| 32 | my $DB = $config->settings('database_name'); | |
| 33 | ||
| 34 | print "Content-Type: application/json\nCache-Control: no-store, no-cache, must-revalidate, max-age=0\nPragma: no-cache\nExpires: 0\n\n"; | |
| 35 | ||
| 36 | our $DONE = 0; | |
| 37 | END { | |
| 38 | if (!$DONE) { | |
| 39 | print qq~{"success":0,"error":"server aborted -- check MODS/DB_ERRORS.log"}~; | |
| 40 | } | |
| 41 | } | |
| 42 | ||
| 43 | sub fail { | |
| 44 | my ($msg) = @_; | |
| 45 | $msg =~ s/"/\\"/g; | |
| 46 | print qq~{"success":0,"error":"$msg"}~; | |
| 47 | $DONE = 1; | |
| 48 | exit; | |
| 49 | } | |
| 50 | ||
| 51 | my $userinfo = $auth->login_verify(); | |
| 52 | fail('not authenticated') unless $userinfo && $userinfo->{user_id}; | |
| 53 | require MODS::ShopCart::Permissions; | |
| 54 | fail('not authorized') unless MODS::ShopCart::Permissions->new->has_feature($userinfo, 'delete_models'); | |
| 55 | ||
| 56 | my $uid = $userinfo->{user_id}; | |
| 57 | $uid =~ s/[^0-9]//g; | |
| 58 | fail('bad user id') unless $uid; | |
| 59 | ||
| 60 | my $bulk = $q->param('all') ? 1 : 0; | |
| 61 | my $model_id = $q->param('model_id') || ''; | |
| 62 | $model_id =~ s/[^0-9]//g; | |
| 63 | fail('need all=1 or model_id=N') unless $bulk || $model_id; | |
| 64 | ||
| 65 | my $dbh = $db->db_connect(); | |
| 66 | my $purged = 0; | |
| 67 | ||
| 68 | if ($bulk) { | |
| 69 | # Count first so the response can tell the user how many got purged. | |
| 70 | my $r = $db->db_readwrite($dbh, qq~ | |
| 71 | SELECT COUNT(*) AS n FROM `${DB}`.models | |
| 72 | WHERE user_id='$uid' AND status='removed' AND purged_at IS NULL | |
| 73 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 74 | $purged = $r && $r->{n} ? $r->{n} : 0; | |
| 75 | ||
| 76 | $db->db_readwrite($dbh, qq~ | |
| 77 | UPDATE `${DB}`.models | |
| 78 | SET purged_at = NOW() | |
| 79 | WHERE user_id='$uid' AND status='removed' AND purged_at IS NULL | |
| 80 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 81 | } else { | |
| 82 | # Verify ownership AND that the model is currently in trash before | |
| 83 | # purging. A user shouldn't be able to purge a draft or published | |
| 84 | # model accidentally -- only items that are already removed. | |
| 85 | my $row = $db->db_readwrite($dbh, qq~ | |
| 86 | SELECT user_id, status, purged_at FROM `${DB}`.models | |
| 87 | WHERE id='$model_id' LIMIT 1 | |
| 88 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 89 | unless ($row && $row->{user_id} && $row->{user_id} eq $uid) { | |
| 90 | $db->db_disconnect($dbh); | |
| 91 | fail('model not yours'); | |
| 92 | } | |
| 93 | unless ($row->{status} && $row->{status} eq 'removed') { | |
| 94 | $db->db_disconnect($dbh); | |
| 95 | fail('only items in trash can be purged'); | |
| 96 | } | |
| 97 | if ($row->{purged_at}) { | |
| 98 | # Already purged -- treat as success (idempotent). | |
| 99 | $db->db_disconnect($dbh); | |
| 100 | print qq~{"success":1,"purged":0,"already":1}~; | |
| 101 | $DONE = 1; | |
| 102 | exit; | |
| 103 | } | |
| 104 | $db->db_readwrite($dbh, qq~ | |
| 105 | UPDATE `${DB}`.models | |
| 106 | SET purged_at = NOW() | |
| 107 | WHERE id='$model_id' AND user_id='$uid' LIMIT 1 | |
| 108 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 109 | $purged = 1; | |
| 110 | } | |
| 111 | ||
| 112 | $db->db_disconnect($dbh); | |
| 113 | ||
| 114 | print qq~{"success":1,"purged":$purged}~; | |
| 115 | $DONE = 1; | |
| 116 | exit; |