edit_config.pl
180 lines of code
1
#!/usr/local/bin/perl
2
3
# must have's!
4
use strict;
5
use warnings;
6
use CGI::Carp qw(fatalsToBrowser);
7
use DBI;
8
use URI::Escape;
9
10
use lib "/var/www/html/Pm";
11
12
use Html qw(pre_html_header header);
13
use Html2 qw(hr br embolden);
14
use Bc_chef qw(cookie_get);
15
use Bc_misc qw(get_param referrer);
16
use Bc_sql qw(
17
              get_constant
18
              sql_execute
19
              user_exists
20
              $QUERY_PAGE
21
              $QUERY_UID
22
              $LOGGEDIN
23
24
              $DB
25
             );
26
27
use User qw(isUserAdmin $USER_DATA);
28
use Redir qw(error_redir notice_redir);
29
use Security qw(banned);
30
31
my $DEBUG = 0;
32
33
34
if (not user_exists($LOGGEDIN) or banned($LOGGEDIN) or not isUserAdmin($LOGGEDIN)) {
35
  my $msg =  "Access Denied";
36
  if ($DEBUG) { $msg .= " (edit_config.pl)"; }
37
  print error_redir("/", $msg);
38
39
  exit 1;
40
}
41
42
my $output = "";
43
if ($DEBUG) { $output = header("edit config", "", "", "", "", "") . "DEBUG ENABLED!" . hr; }
44
45
############################################################
46
47
my $t = get_param("t"); # "tab" being edited
48
my $c = "bodies"; # "configuration name" to update
49
my $v = get_param("v"); # "value" of data to be updated
50
51
if ($t eq 'bodies') { $c = "bodies"; }
52
elsif ($t eq 'busts') { $c = "busts"; }
53
elsif ($t eq 'days') { $c = "days"; }
54
elsif ($t eq 'err') { $c = "errors"; }
55
elsif ($t eq 'eyes') { $c = "eyes"; }
56
elsif ($t eq 'sex') { $c = "genders"; }
57
elsif ($t eq 'hair') { $c = "hair"; }
58
elsif ($t eq 'height') { $c = "heights"; }
59
elsif ($t eq 'len') { $c = "lengths"; }
60
elsif ($t eq 'mon') { $c = "months"; }
61
elsif ($t eq 'orientation') { $c = "orientations"; }
62
elsif ($t eq 'sec') { $c = "sec_levels"; }
63
elsif ($t eq 'race') { $c = "races"; }
64
elsif ($t eq 'style') { $c = "styles"; }
65
elsif ($t eq 'weight') { $c = "weights"; }
66
elsif ($t eq 'zodiac') { $c = "zodiacs"; }
67
else {
68
  print error_redir(referrer(), "no such config: $t");
69
  exit 1;
70
}
71
72
if ($t and $v) {
73
  my $sql = "select name from sqlite_master where type='table' and name=" . $DB->quote($c);
74
  my $result = sql_execute($sql, "edit_config.pl");
75
  # now, each "config" table has two columns: an ID and a value
76
  my @items = split("\n", $v);
77
78
  if (ref $result eq "HASH") {
79
    if ($DEBUG) {
80
      $output .= "table $c exists" . br;
81
82
      my $dupes = 0;
83
      my %seen;
84
85
      foreach my $string (@items) {
86
        $string =~ s/\r$//;
87
        if ($seen{$string}) {
88
          $seen{$string} += 1;
89
          $dupes++;
90
        } else {
91
          $seen{$string} = 1;
92
        }
93
      }
94
95
      if ($dupes) {
96
        $output .= "duplicate entries detected" . br;
97
      } else {
98
        $output .= "no duplicate entries detected" . br;
99
        $output .= "delete from $c" . br;
100
        my $row = 1;
101
        foreach my $tvalue (sort @items) {
102
          my $insert = "insert into $c values ($row, " . $DB->quote($tvalue) . ")";
103
          $output .= "$insert" . br;
104
          $row++;
105
        }
106
      }
107
    } else {
108
      # not debugging
109
      # check for duplicates!
110
      my $dupes = 0;
111
      my %seen;
112
113
      foreach my $string (@items) {
114
        $string =~ s/\r$//;
115
        if ($seen{$string}) {
116
          $seen{$string} += 1;
117
          $dupes++;
118
        } else {
119
          $seen{$string} = 1;
120
        }
121
      }
122
123
      if ($dupes) {
124
        $output = error_redir(referrer(), "duplicate entries detected!");
125
      } else {
126
        my $d = "delete from $c";
127
        if (sql_execute($d, "edit config.pl")) {
128
          my $err = 0;
129
          my $row = 1;
130
          foreach my $tvalue (@items) {
131
            my $insert = "insert into $c values ($row, " . $DB->quote($tvalue) . ")";
132
            my $r = sql_execute($insert, "edit config.pl");
133
            if (not $r) {
134
              $output = error_redir(referrer(), "something didn't go right");
135
              $err = 1;
136
              last;
137
            } else {
138
              $row++;
139
            }
140
          }
141
142
          if (not $err) {
143
            my $url = referrer();
144
            $url =~ s/\&t=([a-z])+//i;
145
            $output = notice_redir("$url&t=$t", "$c updated");
146
          }
147
        } else {
148
          $output = error_redir(referrer(), "couldn't delete the data in $c");
149
        }
150
      }
151
    }
152
  } else {
153
    if ($DEBUG) {
154
      $output .= "table $c does not exist" . br;
155
      $output .= "c=$c" . br;
156
      $output .= "t=$t" . br;
157
      $output .= "v=$v" . br;
158
    } else {
159
      $output = error_redir(referrer(), "table $c does not exist");
160
    }
161
  }
162
# end if ($t or $v)
163
} else {
164
  if ($DEBUG) {
165
    $output .= "gotta gimme somethin to go on, bud, besides a fuckin toilet!" . br;
166
    $output .= "c=$c" . br;
167
    $output .= "t=$t" . br;
168
    $output .= "v=$v" . br;
169
  } else {
170
    $output = error_redir(referrer(), "gotta gimme somethin to work with, bud");
171
  }
172
173
# end else of if ($t or $v)
174
}
175
176
############################################################
177
178
print $output;
179
180
exit 1;