wipe_pw_resets.pl
147 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 html_end);
13
use Redir;
14
use Bc_chef qw(cookie_get cookie_delete);
15
use Bc_misc qw(get_param new_id referrer);
16
use Bc_sql qw(sql_execute
17
              get_constant
18
19
              $DB
20
              $QUERY_UID
21
             );
22
use Date qw(get_today_cookie_style get_today add_date isBeforeDate);
23
use User qw(get_uid_byEmail get_user_stat isUserSuperAdmin);
24
25
my $DEBUG = 0;
26
27
my $output;
28
my $silent = 0;
29
30
if ($ARGV[0]) {
31
  my $handled = 0;
32
33
  if (not $handled and $ARGV[1]) {
34
    if (not $handled and $DEBUG and $ARGV[0] ne "system" and $ARGV[1] ne "debug") {
35
      $output = pre_html_header();
36
      $handled = 1;
37
    }
38
39
    if (not $handled and $ARGV[0] eq "system" and $ARGV[1] eq "debug") {
40
      $DEBUG = 1;
41
      $handled = 1;
42
    }
43
  }
44
45
  if (not $handled and $ARGV[0] eq "silent") {
46
    $silent = 1;
47
    $handled = 1;
48
  }
49
}
50
51
my $sql = "select * from pw_reset";
52
my $results = sql_execute($sql);
53
my $delCount = 0;
54
my $referrer = referrer();
55
56
if (not $referrer) { $referrer = "/"; }
57
58
if (not isUserSuperAdmin()) {
59
  if ($DEBUG)
60
    { $output .= "it does not appear that you are a super administrator<br>\n"; } else
61
    { $output = error_redir("/", "Access Denied"); }
62
} else {
63
64
  if ($DEBUG) { $output .= "it would appear that you are a super administrator<br>\n"; }
65
66
  if (ref $results eq "HASH") {
67
    if ($DEBUG) { $output .= "results is a hash<br>\n"; }
68
69
    my $notOld = 1;
70
    # if today > +3 days of $results->{date}
71
    my $expireDate = add_date($results->{date}, 4, "d");
72
    if (isBeforeDate($expireDate, get_today("db", 1))) {
73
      $notOld = 0;
74
    }
75
76
    if (not $notOld) {
77
      # it's old
78
      if ($DEBUG) { $output .= "pw reset request #$results->{ID} is old - $results->{date}!<br>\n"; }
79
80
      my $wipe_sql = "delete from pw_reset where code=" . $DB->quote($results->{code});
81
      my $wiped = sql_execute($wipe_sql);
82
      if ($wiped) {
83
        $delCount++;
84
        if ($DEBUG) {
85
          $output .= "it was deleted from the DB<br>\n";
86
        } else {
87
          $output = notice_redir($referrer, "Wiping of expired password reset requests completed!");
88
        }
89
      } else {
90
        if ($DEBUG) {
91
          $output .= "it was NOT deleted from the DB<br>\n";
92
        } else {
93
          $output = error_redir($referrer, "Wipe failed!");
94
        }
95
      }
96
    }
97
  } elsif (ref $results eq "ARRAY") {
98
    if ($DEBUG) { $output .= "results is an array<br>\n"; }
99
100
    foreach my $result (@$results) {
101
      my $notOld = 1;
102
      # if today > +3 days of $results->{date}
103
      my $expireDate = add_date($result->{date}, 4, "d");
104
105
      if (isBeforeDate($expireDate, get_today("db", 1))) { $notOld = 0; }
106
107
      if (not $notOld) {
108
        # it's old
109
        if ($DEBUG) { $output .= "pw reset request #$result->{ID} is old - $result->{date}!<br>\n"; }
110
111
        my $wipe_sql = "delete from pw_reset where code=" . $DB->quote($result->{code});
112
        my $wiped = sql_execute($wipe_sql);
113
        if ($wiped) {
114
          $delCount++;
115
          if ($DEBUG) { $output .= "it was deleted from the DB<br>\n"; }
116
        } else {
117
          if ($DEBUG) {
118
            $output .= "it was NOT deleted from the DB<br>\n";
119
          } else {
120
            $output = error_redir($referrer, "Wipe failed!");
121
          }
122
        }
123
      } else {
124
        # it's not old, skip this one
125
      }
126
    } # end of foreach loop
127
128
    if ($DEBUG) { $output .= "deleted $delCount request(s)<br>\n"; }
129
130
    if (not $DEBUG) { $output = notice_redir($referrer, "Wiped out $delCount expired password reset requests"); }
131
  } else {
132
    if ($DEBUG)
133
      { $output .= "invalid results!<br>\n"; } else
134
      { $output = error_redir($referrer, ""); }
135
  }
136
}
137
138
if ($DEBUG) { $output .= "deleted $delCount requests<br>\n"; }
139
140
if (not $DEBUG and $ARGV[0] eq "system") {
141
  $output = "Wiped $delCount requests!\n";
142
  $output .= "Done\n";
143
}
144
145
if (not $silent) { print $output; }
146
147
exit 1;