ban.pl
129 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(tag br hr embolden italicize);
14
use Bc_chef qw(cookie_get);
15
use Bc_misc qw(
16
               get_param
17
               referrer
18
              );
19
use Bc_sql qw(sql_execute
20
              get_constant
21
              user_exists
22
              $QUERY_PAGE
23
              $QUERY_UID
24
              $LOGGEDIN
25
26
              $DB
27
             );
28
29
use Date qw(get_today);
30
use User qw(
31
            isUserAdmin
32
            isUserModerator
33
            $ISADMIN
34
            $ISMODERATOR
35
            $USER_DATA
36
           );
37
use Security qw(banned);
38
use Redir qw(
39
             notice_redir
40
             error_redir
41
            );
42
43
my $DEBUG = 0;
44
45
my $output;
46
47
if (not user_exists($LOGGEDIN) or banned($LOGGEDIN) or not isUserModerator($LOGGEDIN) or not referrer()) {
48
  my $msg =  "Access Denied";
49
  if ($DEBUG) { $msg .= " (ban.pl)"; }
50
  $output = error_redir("/", $msg);
51
} else {
52
  ############################################################
53
  my $uid = get_param($QUERY_UID);
54
  if ($DEBUG) {
55
    $output = pre_html_header();
56
    $output .= header("ban user - $uid", "", "", "", "to ban someone, or not", "");
57
    $output .= "debug mode enabled (referrer: " . referrer() . ")" . br;
58
    $output .= "no changes will be made" . hr . br;
59
  }
60
61
  if (user_exists($uid)) {
62
    # ensure user
63
    #   a) exists
64
    #   b) not an admin
65
    #   c) not a moderator
66
    #   d) not already banned
67
    if (banned($uid) or isUserAdmin($uid) or isUserModerator($uid)) {
68
      if (banned($uid) and get_param('u') eq 1) {
69
        if ($DEBUG) {
70
          $output .= embolden($uid) . " can be unbanned" . br;
71
        } else {
72
          my $bansql = "delete from bans where BID=" . $DB->quote($uid);
73
          if (sql_execute($bansql, "ban.pl")) {
74
            my $usersql = "update users set banned='1' where ID=" . $DB->quote($uid);
75
            if (sql_execute($usersql, "ban.pl")) {
76
              $output = notice_redir(referrer(), "$uid unbanned");
77
            } else {
78
              $output = error_redir(referrer(), "$uid cannot be unbanned");
79
            }
80
          } else {
81
            $output = error_redir(referrer(), "$uid cannot be unbanned");
82
          }
83
        }
84
      } else {
85
        if ($DEBUG) {
86
          $output .= embolden($uid) . "cannot be banned" . br;
87
        } else {
88
          $output = error_redir(referrer(), "$uid cannot be banned");
89
        }
90
      }
91
    } else {
92
      if ($DEBUG) {
93
        $output .= embolden($uid) . " can be banned" . br;
94
      } else {
95
        my $why = get_param("reason");
96
        if ($why) {
97
          my $bansql = "insert into bans (BID, ByID, dob, why) values (";
98
          $bansql .= $DB->quote(get_param($QUERY_UID)) . ", ";
99
          $bansql .= $DB->quote($LOGGEDIN) . ", ";
100
          $bansql .= $DB->quote(get_today("db", 1)) . ", ";
101
          $bansql .= $DB->quote($why);
102
          $bansql .= ")";
103
          if (sql_execute($bansql, "ban.pl")) {
104
            my $usersql = "update users set banned='2' where ID=" . $DB->quote($uid);
105
            if (sql_execute($usersql, "ban.pl"))
106
              { $output = notice_redir(referrer(), "$uid banned!"); } else
107
              { $output = error_redir(referrer(), "failed to ban!"); }
108
          } else {
109
            $output = error_redir(referrer(), "failed to ban!");
110
          }
111
        } else {
112
          $output = error_redir(referrer(), "gotta tell me why they're banned, silly!");
113
        }
114
      }
115
    }
116
  } else {
117
    if ($DEBUG) {
118
      $output .= "$uid does NOT exist";
119
    } else {
120
      $output = error_redir(referrer(), "can't ban $uid");
121
    }
122
  }
123
}
124
125
############################################################
126
127
print $output;
128
129
exit 1;