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

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

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

Added
+116
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to 7f72cf0cd345
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 -- "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#======================================================================
17use strict;
18use warnings;
19
20use lib '/var/www/vhosts/3dshawn.com/repricer.3dshawn.com';
21use CGI;
22use MODS::DBConnect;
23use MODS::Login;
24use MODS::RePricer::Config;
25
26$| = 1;
27
28my $q = CGI->new;
29my $db = MODS::DBConnect->new;
30my $auth = MODS::Login->new;
31my $config = MODS::RePricer::Config->new;
32my $DB = $config->settings('database_name');
33
34print "Content-Type: application/json\nCache-Control: no-cache\n\n";
35
36our $DONE = 0;
37END {
38 if (!$DONE) {
39 print qq~{"success":0,"error":"server aborted -- check MODS/DB_ERRORS.log"}~;
40 }
41}
42
43sub fail {
44 my ($msg) = @_;
45 $msg =~ s/"/\\"/g;
46 print qq~{"success":0,"error":"$msg"}~;
47 $DONE = 1;
48 exit;
49}
50
51my $userinfo = $auth->login_verify();
52fail('not authenticated') unless $userinfo && $userinfo->{user_id};
53require MODS::RePricer::Permissions;
54fail('not authorized') unless MODS::RePricer::Permissions->new->has_feature($userinfo, 'delete_models');
55
56my $uid = $userinfo->{user_id};
57$uid =~ s/[^0-9]//g;
58fail('bad user id') unless $uid;
59
60my $bulk = $q->param('all') ? 1 : 0;
61my $model_id = $q->param('model_id') || '';
62$model_id =~ s/[^0-9]//g;
63fail('need all=1 or model_id=N') unless $bulk || $model_id;
64
65my $dbh = $db->db_connect();
66my $purged = 0;
67
68if ($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
114print qq~{"success":1,"purged":$purged}~;
115$DONE = 1;
116exit;
Keyboard: j next diff k previous diff g top G bottom r restore c compile-check ? help