Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDOモジュール入れてるけどSQLite3クラスを使ってたのでPDOクラスを利用するように変更する #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ban4ipc
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ function ban4ip_banlist($BAN4IPD_CONF)
// BANデータベース内の全データを取得
$RESULT = $BAN4IPD_CONF['ban_db']->query("SELECT * FROM ban_tbl");
// データがあったら表示する
while ($DB_DATA = $RESULT->fetchArray(SQLITE3_ASSOC))
while ($DB_DATA = $RESULT->fetch(PDO::FETCH_ASSOC))
{
printf("%-39s %-16s %-10s %-6s %s",$DB_DATA['address'],$DB_DATA['service'],$DB_DATA['port'],$DB_DATA['rule'],date("Y/m/d H:i:s",$DB_DATA['unbandate']));
print "\n";
Expand Down
2 changes: 2 additions & 0 deletions ban4ipd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ log_file = "/var/log/ban4ipd.log"

; DB file in db_dir/
db_dir = "/var/lib/ban4ip"
db_count_pdo_dsn = "sqlite:/var/lib/ban4ip/count.db"
db_ban_pdo_dsn = "sqlite:/var/lib/ban4ip/ban.db"

; DB timeout[ms]
db_timeout = 5000
Expand Down
2 changes: 1 addition & 1 deletion ban4ipd_core.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ function ban4ip_end($signo)
else
{
// 該当データがあったらUNBANする
while ($DB_DATA = $RESULT->fetchArray(SQLITE3_ASSOC))
while ($DB_DATA = $RESULT->fetch(PDO::FETCH_ASSOC))
{
// UNBANする
$BAN4IPD_CONF['target_address'] = $DB_DATA['address'];
Expand Down
82 changes: 72 additions & 10 deletions ban4ipd_init.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,42 @@ function psearch($PP, $PATTERN)
// Check ban4ip SQLite3 DB
// ----------------------------------------------------------------------
// SQLite3データベース用のディレクトリを作成(エラー出力を抑制)
@mkdir($BAN4IPD_CONF['db_dir']);
// SQLite3データベース用のディレクトリがないなら
if (!is_dir($BAN4IPD_CONF['db_dir']))
if (!empty($BAN4IPD_CONF['db_dir']))
{
print $BAN4IPD_CONF['db_dir']." not found!?"."\n";
// 終わり
exit -1;
// PDO接続設定が片方でもないときだけでいい
if (empty($BAN4IPD_CONF['db_count_pdo_dsn']) || empty($BAN4IPD_CONF['db_ban_pdo_dsn']))
{
@mkdir($BAN4IPD_CONF['db_dir']);
// SQLite3データベース用のディレクトリがないなら
if (!is_dir($BAN4IPD_CONF['db_dir']))
{
print $BAN4IPD_CONF['db_dir']." not found!?"."\n";
// 終わり
exit -1;
}
}
}

// カウントデータベースに接続(無ければ新規に作成)
$BAN4IPD_CONF['count_db'] = new SQLite3($BAN4IPD_CONF['db_dir'].'/count.db');
if (!empty($BAN4IPD_CONF['db_count_pdo_dsn']))
{
try
{
$BAN4IPD_CONF['count_db'] = new PDO($BAN4IPD_CONF['db_count_pdo_dsn']);
}
catch(PDOException $e)
{
print $BAN4IPD_CONF['db_count_pdo_dsn']." not Connection!?"."\n";
print $e->getMessage()."\n";
// 終わり
exit -1;
}
}
else
{
// カウントデータベースに接続(無ければ新規に作成)
$BAN4IPD_CONF['count_db'] = new PDO('sqlite:' . $BAN4IPD_CONF['db_dir'].'/count.db');
}

// カウントデータベースに接続できなかったら
if ($BAN4IPD_CONF['count_db'] === FALSE)
Expand All @@ -187,11 +212,38 @@ function psearch($PP, $PATTERN)
// テーブルがなかったら作成
$BAN4IPD_CONF['count_db']->exec('CREATE TABLE IF NOT EXISTS count_tbl (address, service, registdate)');

// インデックスを作成する
try
{
$BAN4IPD_CONF['count_db']->exec('CREATE INDEX address_idx ON count_tbl (address)');
}
catch(PDOException $e)
{
// インデックス作成の際のエラーは許容する(重複エラーと思われるため)
}

// カウントデータベースのロックタイムアウト時間を少し長くする
$BAN4IPD_CONF['count_db']->busyTimeout($BAN4IPD_CONF['db_timeout']);
$BAN4IPD_CONF['count_db']->setAttribute(PDO::ATTR_TIMEOUT, $BAN4IPD_CONF['db_timeout']);

// BANデータベースに接続(無ければ新規に作成)
$BAN4IPD_CONF['ban_db'] = new SQLite3($BAN4IPD_CONF['db_dir'].'/ban.db');
if (!empty($BAN4IPD_CONF['db_ban_pdo_dsn']))
{
try
{
$BAN4IPD_CONF['ban_db'] = new PDO($BAN4IPD_CONF['db_ban_pdo_dsn']);
}
catch(PDOException $e)
{
print $BAN4IPD_CONF['db_ban_pdo_dsn']." not Connection!?"."\n";
print $e->getMessage()."\n";
// 終わり
exit -1;
}
}
else
{
$BAN4IPD_CONF['ban_db'] = new PDO('sqlite:' . $BAN4IPD_CONF['db_dir'].'/ban.db');
}

// BANデータベースに接続できなかったら
if ($BAN4IPD_CONF['ban_db'] === FALSE)
Expand All @@ -204,7 +256,17 @@ function psearch($PP, $PATTERN)
// テーブルがなかったら作成
$BAN4IPD_CONF['ban_db']->exec('CREATE TABLE IF NOT EXISTS ban_tbl (address, service, protcol, port, rule, unbandate)');

// インデックスを作成する
try
{
$BAN4IPD_CONF['ban_db']->exec('CREATE INDEX unbandate_idx ON ban_tbl (unbandate)');
}
catch(PDOException $e)
{
// インデックス作成の際のエラーは許容する(重複エラーと思われるため)
}

// カウントデータベースのロックタイムアウト時間を少し長くする
$BAN4IPD_CONF['ban_db']->busyTimeout($BAN4IPD_CONF['db_timeout']);
$BAN4IPD_CONF['ban_db']->setAttribute(PDO::ATTR_TIMEOUT, $BAN4IPD_CONF['db_timeout']);

?>
2 changes: 1 addition & 1 deletion ban4ipd_sub.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ function ban4ip_loop($TARGET_CONF)

// 対象IPアドレスの検出回数を取得
$RESULT_COUNT = 0;
while ($DB_DATA = $RESULT->fetchArray(SQLITE3_ASSOC))
while ($DB_DATA = $RESULT->fetch(PDO::FETCH_ASSOC))
{
$RESULT_COUNT ++;
}
Expand Down