',', 2 => '|');
$quote_char = array(1 => '(none)', 2 => "'", 3 => '"');
$action = 'list';
if (e_QUERY)
{
$tmp = explode('-', e_QUERY); // Use '-' instead of '.' to avoid confusion with IP addresses
$action = $tmp[0];
$sub_action = varset($tmp[1], '');
if ($sub_action) $sub_action = preg_replace('/[^\w*@\.:]*/', '', urldecode($sub_action));
$id = intval(varset($tmp[2], 0));
unset($tmp);
}
if (isset($_POST['update_ban_prefs'])) // Update ban messages
{
$changed = FALSE;
foreach ($ipAdministrator->getValidReasonList() as $bt)
{
$i = abs($bt) + 1; // Forces a single-digit positive number for part of field name
$t1 = $tp->toDB(varset($_POST['ban_text_'.($i)],''));
$t2 = intval(varset($_POST['ban_time_'.($i)],0));
if (!isset($pref['ban_messages'][$bt]) || ($pref['ban_messages'][$bt] != $t1))
{
$pref['ban_messages'][$bt] = $t1;
$changed = TRUE;
}
if (!isset($pref['ban_durations'][$bt]) || ($pref['ban_durations'][$bt] != $t2))
{
$pref['ban_durations'][$bt] = $t2;
$changed = TRUE;
}
}
if ($changed)
{
// @todo write actual prefs changes to log file (different methods for prefs?)
save_prefs();
/*****************************************
Write messages and times to disc file
*****************************************/
$ipAdministrator->writeBanMessageFile();
banlist_adminlog('08','');
//$ns->tablerender(BANLAN_9, "
".BANLAN_33.'
');
$emessage->add(BANLAN_33, E_MESSAGE_SUCCESS);
}
}
$writeBanFile = FALSE;
if (isset($_POST['ban_ip']))
{
$_POST['ban_ip'] = trim($_POST['ban_ip']);
$new_ban_ip = preg_replace('/[^\w*@\.:]*/', '', urldecode($_POST['ban_ip']));
if ($new_ban_ip != $_POST['ban_ip'])
{
$message = BANLAN_27.' '.$new_ban_ip;
//$ns->tablerender(BANLAN_9, $message);
$emessage->add(BANLAN_33, $message);
$_POST['ban_ip'] = $new_ban_ip;
}
if (isset($_POST['entry_intent']) && (isset($_POST['add_ban']) || isset($_POST['update_ban'])) && $_POST['ban_ip'] != "" && strpos($_POST['ban_ip'], ' ') === false)
{
/* $_POST['entry_intent'] says why we're here:
'edit' - Editing blacklist
'add' - Adding to blacklist
'whedit' - Editing whitelist
'whadd' - Adding to whitelist
*/
if(e107::getIPHandler()->whatIsThis($new_ban_ip) == 'ip')
{
$new_ban_ip = e107::getIPHandler()->IPencode($new_ban_ip, TRUE); // Normalise numeric IP addresses (allow wildcards)
}
$new_vals = array('banlist_ip' => $new_ban_ip);
if (isset($_POST['add_ban']))
{
$new_vals['banlist_datestamp'] = time();
if ($_POST['entry_intent'] == 'add') $new_vals['banlist_bantype'] = eIPHandler::BAN_TYPE_MANUAL; // Manual ban
if ($_POST['entry_intent'] == 'whadd') $new_vals['banlist_bantype'] = eIPHandler::BAN_TYPE_WHITELIST;
}
$new_vals['banlist_admin'] = ADMINID;
$new_vals['banlist_reason'] = $tp->toDB(varset($_POST['ban_reason'], ''));
$new_vals['banlist_notes'] = $tp->toDB($_POST['ban_notes']);
if (isset($_POST['ban_time']) && is_numeric($_POST['ban_time']) && (($_POST['entry_intent']== 'edit') || ($_POST['entry_intent'] == 'add')))
{
$bt = intval($_POST['ban_time']);
$new_vals['banlist_banexpires'] = $bt ? time() + ($bt*60*60) : 0;
}
if (isset($_POST['add_ban']))
{ // Insert new value - can just pass an array
admin_update($sql->db_Insert('banlist', $new_vals), 'insert');
if ($_POST['entry_intent'] == 'add')
{
banlist_adminlog('01', $new_vals['banlist_ip']); // Write to banlist
}
else
{
banlist_adminlog('04', $new_vals['banlist_ip']); // Write to whitelist
}
}
else
{ // Update existing value
$qry = '';
$spacer = '';
foreach ($new_vals as $k => $v)
{
$qry .= $spacer."`{$k}`='$v'";
$spacer = ', ';
}
admin_update($sql->db_Update('banlist', $qry." WHERE banlist_ip='".$_POST['old_ip']."'"));
if ($_POST['entry_intent'] == 'edit')
{
banlist_adminlog('09',$new_vals['banlist_ip']);
}
else
{
banlist_adminlog('10',$new_vals['banlist_ip']);
}
}
unset($ban_ip);
$writeBanFile = TRUE;
}
}
// Remove a ban
if (($action == 'remove' || $action == 'whremove') && isset($_POST['ban_secure']))
{
$sql->db_Delete('generic', "gen_type='failed_login' AND gen_ip='{$sub_action}'");
admin_update($sql->db_Delete('banlist', "banlist_ip='{$sub_action}'"), 'delete');
if ($action == "remove")
{
$action = 'list';
banlist_adminlog('02', $sub_action);
}
else
{
$action = 'white';
banlist_adminlog('05', $sub_action);
}
$writeBanFile = TRUE;
}
// Update the ban expiry time/date - timed from now (only done on banlist)
if ($action == 'newtime')
{
$end_time = $id ? time() + ($id*60*60) : 0;
admin_update($sql->db_Update('banlist', 'banlist_banexpires='.intval($end_time)." WHERE banlist_ip='".$sub_action."'"));
banlist_adminlog('03', $sub_action);
$action = 'list';
$writeBanFile = TRUE;
}
if ($writeBanFile)
{
/************************************************
update list of banned IPs
*************************************************/
$ipAdministrator->writeBanListFiles('ip,htaccess');
if (!$ipAdministrator->doesMessageFileExist())
{
$ipAdministrator->writeBanMessageFile(); // Message file must exist - may not on fresh site
banlist_adminlog('08','');
$emessage->add(BANLAN_33, E_MESSAGE_SUCCESS);
}
}
/**
* @todo - eliminate extract();
*/
// Edit modes - get existing entry
if ($action == 'edit' || $action == 'whedit')
{
$sql->db_Select('banlist', '*', "banlist_ip='{$sub_action}'");
$row = $sql->db_Fetch();
extract($row); //FIXME - kill extract()
}
else
{
unset($banlist_ip, $banlist_reason);
if (e_QUERY && ($action == 'add' || $action == 'whadd') && strpos($_SERVER["HTTP_REFERER"], "userinfo"))
{
$banlist_ip = $sub_action;
}
}
/**
* Create dropdown with options for ban time - uses internal fixed list of reasonable values
*/
function ban_time_dropdown($click_js = '', $zero_text = BANLAN_21, $curval = -1, $drop_name = 'ban_time')
{
global $frm;
$intervals = array(0, 1, 2, 3, 6, 8, 12, 24, 36, 48, 72, 96, 120, 168, 336, 672);
$ret = $frm->select_open($drop_name, array('other' => $click_js, 'id' => false));
$ret .= $frm->option(' ', '');
foreach ($intervals as $i)
{
if ($i == 0)
{
$words = $zero_text ? $zero_text : BANLAN_21;
}
elseif (($i % 24) == 0)
{
$words = floor($i / 24).' '.BANLAN_23;
}
else
{
$words = $i.' '.BANLAN_24;
}
$ret .= $frm->option($words, $i, ($curval == $i));
}
$ret .= '';
return $ret;
}
/**
* Create generic dropdown from array of data
*/
function select_box($name, $data, $curval = FALSE)
{
global $frm;
$ret = $frm->select_open($name, array('class' => 'tbox', 'id' => false));
foreach ($data as $k => $v)
{
$ret .= $frm->option($v, $k, ($curval !== FALSE) && ($curval == $k));
}
$ret .= "\n";
return $ret;
}
/**
* Create dropdown with options for access counts before ban - uses internal fixed list of reasonable values
*/
function drop_box($box_name, $curval)
{
global $frm;
$opts = array(50, 100, 150, 200, 250, 300, 400, 500);
$ret = $frm->select_open($box_name, array('class' => 'tbox'));
foreach ($opts as $o)
{
$ret .= $frm->option($o, $o, ($curval == $o));
}
$ret .= "\n";
return $ret;
}
$text = '';
switch ($action)
{
case 'banlog' :
if(!getperms('0')) exit;
if (isset($_POST['delete_ban_log']))
{
$message = ($ipAdministrator->deleteLogFile() ? BANLAN_89 : BANLAN_90);
e107::getRender()->tablerender(BANLAN_88, "".$message."
");
}
$from = 0;
$amount = 20; // Number per page - could make configurable later if required
if ($sub_action) $from = intval($sub_action);
// @todo format form the 0.8 way
$text = "";
if (count($banLogEntries))
{
$text .= " ".str_replace('--NUM--', $num_entry, BANLAN_87);
}
e107::getRender()->tablerender("".BANLAN_86.'
', $text);
break;
case 'options' :
if (!getperms('0'))
exit();
if (isset($_POST['update_ban_options']))
{
$pref['enable_rdns'] = intval($_POST['ban_rdns_on_access']);
$pref['enable_rdns_on_ban'] = intval($_POST['ban_rdns_on_ban']);
$pref['ban_max_online_access'] = intval($_POST['ban_access_guest']).','.intval($_POST['ban_access_member']);
$pref['ban_retrigger'] = intval($_POST['ban_retrigger']);
$pref['ban_date_format'] = $tp->toDB($_POST['ban_date_format']);
save_prefs(); // @todo FIXME log detail of changes. Right prefs to use?
$emessage->add(LAN_SETSAVED, E_MESSAGE_SUCCESS);
}
if (isset($_POST['remove_expired_bans']))
{
$result = $sql->db_Delete('banlist',"`banlist_bantype` < ".eIPHandler::BAN_TYPE_WHITELIST." AND `banlist_banexpires` > 0 AND `banlist_banexpires` < ".time());
banlist_adminlog('12', $result);
$emessage->add(str_replace('--NUM--', $result, BANLAN_48), E_MESSAGE_SUCCESS);
}
list($ban_access_guest, $ban_access_member) = explode(',', varset($pref['ban_max_online_access'], '100,200'));
$ban_access_member = max($ban_access_guest, $ban_access_member);
$text = "
";
e107::getRender()->tablerender(BANLAN_72, $emessage->render().$text);
break;
case 'times' :
if (!getperms('0'))
exit();
$text = '';
if ((!isset($pref['ban_messages'])) || !is_array($pref['ban_messages']))
{
foreach ($ipAdministrator->getValidReasonList() as $bt)
{
$pref['ban_messages'][$bt] = '';
}
}
if ((!isset($pref['ban_durations'])) || !is_array($pref['ban_durations']))
{
foreach ($ipAdministrator->getValidReasonList() as $bt)
{
$pref['ban_durations'][$bt] = 0;
}
}
$text .= "
";
e107::getRender()->tablerender(BANLAN_77, $emessage->render().$text);
break;
case 'edit' : // Edit an existing ban
case 'add' : // Add a new ban
case 'whedit' : // Edit existing whitelist entry
case 'whadd' : // Add a new whitelist entry
if (!isset($banlist_reason)) $banlist_reason = '';
if (!isset($banlist_ip)) $banlist_ip = '';
if (!isset($banlist_notes)) $banlist_notes = '';
$page_title = array('edit' => BANLAN_60, 'add' => BANLAN_9, 'whedit' => BANLAN_59, 'whadd' => BANLAN_58);
$rdns_warn = varsettrue($pref['enable_rdns']) ? '' : ''.BANLAN_12.'
';
$next = ($action == 'whedit' || $action == 'whadd') ? '?white' : '?list';
// Edit/add form first
$text .= "
";
e107::getRender()->tablerender($page_title[$action], $emessage->render().$text);
break; // End of 'Add' and 'Edit'
case 'transfer' :
$message = '';
$error = false;
if (isset($_POST['ban_import']))
{ // Got a file to import
require_once(e_HANDLER.'upload_handler.php');
if (($files = process_uploaded_files(e_UPLOAD, FALSE, array('overwrite' => TRUE, 'max_file_count' => 1, 'file_mask' => 'csv'))) === FALSE)
{ // Invalid file
$error = true;
$message = BANLAN_47;
$emessage->add($message, E_MESSAGE_ERROR);
}
if(empty($files) || varsettrue($files[0]['error']))
{
$error = true;
if(varset($files[0]['message']))
$emessage->add($files[0]['message'], E_MESSAGE_ERROR);
}
if(!$error)
{ // Got a file of some sort
$message = process_csv(e_UPLOAD.$files[0]['name'],
intval(varset($_POST['ban_over_import'], 0)),
intval(varset($_POST['ban_over_expiry'], 0)),
$separator_char[intval(varset($_POST['ban_separator'], 1))],
$quote_char[intval(varset($_POST['ban_quote'], 3))]);
banlist_adminlog('07', 'File: '.e_UPLOAD.$files[0]['name'].'
'.$message);
}
}
$text = "
";
// Now do the import options
$text .= "
";
e107::getRender()->tablerender(BANLAN_35, $emessage->render().$text);
break; // End case 'transfer'
case 'list' :
case 'white' :
default :
if (($action != 'list') && ($action != 'white'))
$action = 'list';
$edit_action = ($action == 'list' ? 'edit' : 'whedit');
$del_action = ($action == 'list' ? 'remove' : 'whremove');
$col_widths = array('list' => array(10, 5, 35, 30, 10, 10), 'white' => array(15, 40, 35, 10));
$col_titles = array('list' => array(BANLAN_17, BANLAN_20, BANLAN_10, BANLAN_19, BANLAN_18, LAN_OPTIONS),
'white' => array(BANLAN_55, BANLAN_56, BANLAN_19, LAN_OPTIONS));
$no_values = array('list' => BANLAN_2, 'white' => BANLAN_54);
$col_defs = array('list' => array('banlist_datestamp' => 0, 'banlist_bantype' => 0, 'ip_reason' => BANLAN_7, 'banlist_notes' => 0, 'banlist_banexpires' => 0, 'ban_options' => 0),
'white' => array('banlist_datestamp' => 0, 'ip_reason' => BANLAN_57, 'banlist_notes' => 0, 'ban_options' => 0));
$text = "
";
e107::getRender()->tablerender(($action == 'list' ? BANLAN_3 : BANLAN_61), $emessage->render().$text);
// End of case 'list' and the default case
} // End switch ($action)
require_once(e_ADMIN.'footer.php');
/**
* Admin menu options
*/
function banlist_adminmenu()
{
$action = (e_QUERY) ? e_QUERY : 'list';
$var['list']['text'] = BANLAN_14; // List existing bans
$var['list']['link'] = e_SELF.'?list';
$var['list']['perm'] = '4';
$var['add']['text'] = BANLAN_25; // Add a new ban
$var['add']['link'] = e_SELF.'?add';
$var['add']['perm'] = '4';
$var['white']['text'] = BANLAN_52; // List existing whitelist entries
$var['white']['link'] = e_SELF.'?white';
$var['white']['perm'] = '4';
$var['whadd']['text'] = BANLAN_53; // Add a new whitelist entry
$var['whadd']['link'] = e_SELF.'?whadd';
$var['whadd']['perm'] = '4';
$var['transfer']['text'] = BANLAN_35;
$var['transfer']['link'] = e_SELF.'?transfer';
$var['transfer']['perm'] = '4';
if (getperms('0'))
{
$var['times']['text'] = BANLAN_15;
$var['times']['link'] = e_SELF.'?times';
$var['times']['perm'] = '0';
$var['options']['text'] = LAN_OPTIONS;
$var['options']['link'] = e_SELF.'?options';
$var['options']['perm'] = '0';
$var['banlog']['text'] = BANLAN_81;
$var['banlog']['link'] = e_SELF.'?banlog';
$var['banlog']['perm'] = '0';
}
e_admin_menu(BANLAN_16, $action, $var);
}
// Parse the date string used by the import/export - YYYYMMDD_HHMMSS
function parse_date($instr)
{
if (strlen($instr) != 15)
return 0;
return mktime(substr($instr, 9, 2), substr($instr, 11, 2), substr($instr, 13, 2), substr($instr, 4, 2), substr($instr, 6, 2), substr($instr, 0, 4));
}
// Process the imported CSV file, update the database, delete the file.
// Return a message
function process_csv($filename, $override_imports, $override_expiry, $separator = ',', $quote = '"')
{
$sql = e107::getDb();
$pref['ban_durations'] = e107::getPref('ban_durations');
$emessage = &eMessage::getInstance();
// echo "Read CSV: {$filename} separator: {$separator}, quote: {$quote} override imports: {$override_imports} override expiry: {$override_expiry}
";
// Renumber imported bans
if ($override_imports)
$sql->db_Update('banlist', "`banlist_bantype`=".eIPHandler::BAN_TYPE_TEMPORARY." WHERE `banlist_bantype` = ".eIPHandler::BAN_TYPE_IMPORTED);
$temp = file($filename);
$line_num = 0;
foreach ($temp as $line)
{ // Process one entry
$line = trim($line);
$line_num++;
if ($line)
{
$fields = explode($separator, $line);
$field_num = 0;
$field_list = array('banlist_bantype' => eIPHandler::BAN_TYPE_IMPORTED);
foreach ($fields as $f)
{
$f = trim($f);
if (substr($f, 0, 1) == $quote)
{
if (substr($f, -1, 1) == $quote)
{ // Strip quotes
$f = substr($f, 1, -1); // Strip off the quotes
}
else
{
$emessage->add(BANLAN_49.$line_num, E_MESSAGE_ERROR);
return BANLAN_49.$line_num;
}
}
// Now handle the field
$field_num++;
switch ($field_num)
{
case 1 : // IP address
$field_list['banlist_ip'] = e107::getIPHandler()->ipEncode($f);
break;
case 2 : // Original date of ban
$field_list['banlist_datestamp'] = parse_date($f);
break;
case 3 : // Expiry of ban - depends on $override_expiry
if ($override_expiry)
{
$field_list['banlist_banexpires'] = parse_date($f);
}
else
{ // Use default ban time from now
$field_list['banlist_banexpires'] = $pref['ban_durations'][eIPHandler::BAN_TYPE_IMPORTED] ? time() + (60*60*$pref['ban_durations'][eIPHandler::BAN_TYPE_IMPORTED]) : 0;
}
break;
case 4 : // Original ban type - we always ignore this and force to 'imported'
break;
case 5 : // Ban reason originally generated by E107
$field_list['banlist_reason'] = $f;
break;
case 6 : // Any user notes added
$field_list['banlist_notes'] = $f;
break;
default : // Just ignore any others
}
}
$qry = "REPLACE INTO `#banlist` (".implode(',', array_keys($field_list)).") values ('".implode("', '", $field_list)."')";
// echo count($field_list)." elements, query: ".$qry."
";
if (!$sql->db_Select_gen($qry))
{
$emessage->add(BANLAN_50.$line_num, E_MESSAGE_ERROR);
return BANLAN_50.$line_num;
}
}
}
// Success here - may need to delete old imported bans
if ($override_imports)
$sql->db_Delete('banlist', "`banlist_bantype` = ".eIPHandler::BAN_TYPE_TEMPORARY);
@unlink($filename); // Delete file once done
$emessage->add(str_replace('--NUM--', $line_num, BANLAN_51).$filename, E_MESSAGE_SUCCESS);
return str_replace('--NUM--', $line_num, BANLAN_51).$filename;
}
/**
* Log event to admin log
*
* @param string $msg_num - exactly two numeric characters corresponding to a log message
* @param string $woffle - information for the body of the log entre
*
* @return none
*/
function banlist_adminlog($msg_num = '00', $woffle = '')
{
e107::getAdminLog()->log_event('BANLIST_'.$msg_num, $woffle, E_LOG_INFORMATIVE, '');
}
/**
* Handle page DOM within the page header
*
* @return string JS source
*/
function headerjs()
{
require_once(e_HANDLER.'js_helper.php');
$ret = "
";
return $ret;
}
?>