Vanilla commit.
[tinybbs.git] / ip_address.php
blob3f3af5768c5beb5b9f6515ecb81f96a41edc2180
1 <?php
3 require('includes/header.php');
5 if( ! $administrator && ! $moderator)
7 add_error('You are not wise enough.', true);
10 // Validate IP address.
11 if( ! filter_var($_GET['ip'], FILTER_VALIDATE_IP))
13 add_error('That is not a valid IP address.', true);
16 $ip_address = $_GET['ip'];
17 $hostname = gethostbyaddr($ip_address);
18 if($hostname === $ip_address)
20 $hostname = false;
23 $page_title = 'Information on IP address ' . $ip_address;
25 // If a ban request has been submitted...
26 if( ! empty($_POST['ban_length']))
28 if($_POST['ban_length'] == 'indefinite' | $_POST['ban_length'] == 'infinite')
30 $new_ban_expiry = 0;
32 else if(strtotime($_POST['ban_length']) > $_SERVER['REQUEST_TIME'])
34 $new_ban_expiry = strtotime($_POST['ban_length']);
36 else
38 add_error('Invalid ban length.');
41 if( ! $erred)
43 $ban_ip = $link->prepare('INSERT INTO ip_bans (ip_address, expiry, filed) VALUES (?, ?, UNIX_TIMESTAMP()) ON DUPLICATE KEY UPDATE expiry = ?, filed = UNIX_TIMESTAMP()');
44 $ban_ip->bind_param('sii', $ip_address, $new_ban_expiry, $new_ban_expiry);
45 $ban_ip->execute();
46 $ban_ip->close();
47 $_SESSION['notice'] = 'IP address banned.';
51 // Check for ban.
52 $check_ban = $link->prepare('SELECT filed, expiry FROM ip_bans WHERE ip_address = ?');
53 $check_ban->bind_param('s', $ip_address);
54 $check_ban->execute();
55 $check_ban->bind_result($ban_filed, $ban_expiry);
56 $check_ban->fetch();
57 $check_ban->close();
59 $banned = false;
60 if( ! empty($ban_filed))
62 if($ban_expiry == 0 || $ban_expiry > $_SERVER['REQUEST_TIME'])
64 $banned = true;
66 else // the ban has already expired
68 remove_ip_ban($ip_address);
72 // Get statistics.
73 $query = "SELECT count(*) FROM topics WHERE author_ip = '" . $link->real_escape_string($ip_address) . "';";
74 $query .= "SELECT count(*) FROM replies WHERE author_ip = '" . $link->real_escape_string($ip_address) . "';";
75 $query .= "SELECT count(*) FROM users WHERE ip_address = '" . $link->real_escape_string($ip_address) . "';";
77 $link->multi_query($query);
78 do
80 $result = $link->store_result();
81 while ($row = $result->fetch_row())
83 $queries[] = $row[0];
85 $result->free();
86 } while ($link->next_result());
88 $ip_num_topics = $queries[0];
89 $ip_num_replies = $queries[1];
90 $ip_num_ids = $queries[2];
92 print_errors();
94 echo '<p>This IP address (';
95 if($hostname)
97 echo 'host name <strong>' . $hostname . '</strong>';
99 else
101 echo 'no valid host name';
103 echo') is associated with <strong>' . $ip_num_ids . '</strong> ID' . ($ip_num_ids == 1 ? '' : 's') . ' and has been used to post <strong>' . $ip_num_topics . '</strong> existing topic' . ($ip_num_topics == 1 ? '' : 's') . ' and <strong>' . $ip_num_replies . '</strong> existing repl' . ($ip_num_replies == 1 ? 'y' : 'ies') . '.</p>';
104 if($banned)
106 echo '<p>It is currently <strong>banned</strong>. The ban was filed <span class="help" title="' . format_date($ban_filed) . '">' . calculate_age($ban_filed) . ' ago</span> and will ';
107 if($ban_expiry == 0)
109 echo 'last indefinitely';
111 else
113 echo 'expire in ' . calculate_age($ban_expiry);
115 echo '.</p>';
119 <form action="" method="post">
120 <div class="row">
121 <label for="ban_length" class="inline">Ban length</label>
122 <input type="text" name="ban_length" id="ban_length" value="<?php if( ! $banned) echo '1 week' ?>" class="inline" />
123 <input type="submit" value="<?php echo ($banned) ? 'Update ban length' : 'Ban' ?>" class="inline" />
124 <span class="unimportant">(A ban length of "indefinite" will never expire.)</span>
125 </div>
126 </form>
128 <ul class="menu">
129 <?php if($banned) echo '<li><a href="/unban_IP/' . $ip_address . '">Unban</a></li>' ?>
130 <li><a href="/delete_IP_IDs/<?php echo $ip_address ?>">Delete all IDs</a></li>
131 <li><a href="/nuke_IP/<?php echo $ip_address ?>">Delete all posts</a></li>
132 <li><a href="http://toolserver.org/~chm/whois.php?ip=<?php echo $ip_address ?>">Whois</a></li>
133 </ul>
136 <?php
138 if($ip_num_ids > 0)
140 echo '<h4 class="section">IDs</h4>';
142 $stmt = $link->prepare('SELECT uid, first_seen FROM users WHERE ip_address = ? ORDER BY first_seen DESC LIMIT 5000');
143 $stmt->bind_param('s', $ip_address);
144 $stmt->execute();
145 $stmt->bind_result($id, $id_first_seen);
147 $id_table = new table();
148 $columns = array
150 'ID',
151 'First seen ▼'
153 $id_table->define_columns($columns, 'ID');
155 while($stmt->fetch()) {
156 $values = array
158 '<a href="/profile/' . $id . '">' . $id . '</a>',
159 '<span class="help" title="' . format_date($id_first_seen) . '">' . calculate_age($id_first_seen) . '</span>'
162 $id_table->row($values);
164 $stmt->close();
165 echo $id_table->output();
168 require('includes/footer.php');