Skip to content

Commit 29be9a5

Browse files
committed
update: Added table creation direct to database
1 parent 3a02720 commit 29be9a5

11 files changed

+95
-331
lines changed

FirewallClass.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,53 @@ class Firewall
1313
// retrieves user's IP address
1414
public static function getIP()
1515
{
16-
$ip = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);
17-
$ip = preg_replace("/[^0-9a-zA-Z\.\:]/", "", $ip);
16+
$ip = $_SERVER['REMOTE_ADDR'];
17+
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
18+
$ip = $_SERVER['HTTP_CLIENT_IP'];
19+
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
20+
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
21+
}
22+
$ip = preg_replace('/[^0-9a-zA-Z\.\:]/', '', $ip);
1823
return $ip;
1924
}
2025

21-
// set tables
26+
27+
// set tables - and creates if dont exists, using the db class
2228
public static function setTable($which, $table)
2329
{
30+
31+
$checkTable = function ($table, $target) {
32+
if (db::empty("DESCRIBE $table")) {
33+
$contents = @file_get_contents("./$target.sql");
34+
if ($contents) {
35+
$contents = preg_replace("/$target/", $table, $contents);
36+
db::execute($contents);
37+
}
38+
}
39+
};
40+
2441
switch ($which) {
25-
case "whiteListTable":
42+
case 'whiteListTable':
2643
self::$whiteListTable = $table;
44+
$checkTable($table, 'whiteListTable');
2745
break;
28-
case "blackListTable":
46+
case 'blackListTable':
2947
self::$blackListTable = $table;
48+
$checkTable($table, 'blackListTable');
3049
break;
31-
case "temporaryListTable":
50+
case 'temporaryListTable':
3251
self::$temporaryListTable = $table;
52+
$checkTable($table, 'temporaryListTable');
3353
break;
34-
case "minuteListTable":
54+
case 'minuteListTable':
3555
self::$minuteListTable = $table;
56+
$checkTable($table, 'temporaryListTable');
3657
break;
58+
default:
59+
throw new InvalidArgumentException('Invalid table name: ' . $which);
3760
}
38-
return new static();
61+
62+
return new self();
3963
}
4064

4165
// returns date now

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ You can do it inserting on the "Black List" table, or, if the visitor gets in te
3030

3131
* This class works well for static IPs;
3232
* Switching from wifi to phone's connection changes the device IP, therefore, if it was blocked before, since it is now a different IP, will be allowed to access what it was trying to (maybe use cookies to prevent this?).
33+
* Case there's no firewall tables on the database, the class now can create it, using the [DB Class](https://github.com/Matheus2212/php-database-class)
3334

3435
---
3536

blackListTable.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE TABLE `blackListTable` (
2+
`id` int(11) NOT NULL,
3+
`ip` varchar(64) NOT NULL,
4+
`created_at` datetime NOT NULL
5+
);
6+
7+
ALTER TABLE `blackListTable`
8+
ADD PRIMARY KEY (`id`);
9+
10+
ALTER TABLE `blackListTable`
11+
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
12+
COMMIT;

firewall_black_list.sql

Lines changed: 0 additions & 82 deletions
This file was deleted.

firewall_minute_list.sql

Lines changed: 0 additions & 67 deletions
This file was deleted.

firewall_temporary_list.sql

Lines changed: 0 additions & 110 deletions
This file was deleted.

0 commit comments

Comments
 (0)