<?php
$druh = "banlist";
include "head.php";
$menu2 = include_menu2($druh);
include_head($lang['set_banlist_title'], $druh);

if (!isset($_GET['action'])) {$_GET['action'] = "default";};
switch ($_GET['action']) {

case "add" :

$ip = mysql_real_escape_string($_POST['ip']);
$druh = mysql_real_escape_string($_POST['druh']);
if (empty($_POST['duvod'])) {$duvod = $lang['set_banlist_empty_reason'];} else {$duvod = $_POST['duvod'];};
$duvod = mysql_real_escape_string($duvod);

$query = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_banlist WHERE ip='".$ip."'");
$result = mysql_fetch_array($query);

 if (empty($ip) or empty($druh) or $result['pocet'] != 0 or $ip == $_SERVER['REMOTE_ADDR']) {
     echo '<h1>'.$lang['set_banlist_ban_ip'].'</h1><br />';
     echo '<div class="oznameni_div">';
     echo '<div class="chyba">'.$lang['set_banlist_error'].'</div>';
     if (empty($ip)) {echo '<p>'.$lang['set_banlist_er_1'].'</p>';};
     if (empty($druh)) {echo '<p>'.$lang['set_banlist_er_2'].'</p>';};
     if ($result['pocet'] != 0) {echo '<p>'.$lang['set_banlist_er_3'].'</p>';};
     if ($ip == $_SERVER['REMOTE_ADDR']) {echo '<p>'.$lang['set_banlist_er_6'].'</p>';};
     echo '</div>';
     
     include_end();
     exit();
 };
 
    mysql_query("INSERT INTO jtcms_banlist (ip,datum,druh,duvod) VALUES ('".$ip."',NOW(),'".$druh."','".$duvod."')");
    Header("Location: set_banlist.php?work=added&druh=".$druh."&ip=".rawurlencode($ip));
    echo '<script type="text/javascript">location.replace("set_banlist.php?work=added&druh='.$druh.'&ip='.rawurlencode($ip).'")</script>';  

break;

case "edit" :

$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM jtcms_banlist WHERE id='".$id."' LIMIT 1");
$radku = mysql_num_rows($query);
$result = mysql_fetch_array($query);

echo '<h1>'.$lang['set_banlist_edit_ban'].'</h1><br />';

 if (empty($id) or $radku == 0) {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['set_banlist_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['set_banlist_er_4'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['set_banlist_er_5'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  }; 

 if (!empty($_POST)) {
    $ip = mysql_real_escape_string($_POST['ip']);
    $druh = mysql_real_escape_string($_POST['druh']);
    if (empty($_POST['duvod'])) {$duvod = "Důvod banu nebyl vyplněn.";} else {$duvod = $_POST['duvod'];};
    $duvod = mysql_real_escape_string($duvod);
    $id = mysql_real_escape_string($_GET['id']);
    
    if (!empty($ip) && !empty($druh) && !empty($id)) {
    
    mysql_query("UPDATE jtcms_banlist SET ip='".$ip."', druh='".$druh."', duvod='".$duvod."' WHERE id='".$id."' LIMIT 1");
    Header("Location: set_banlist.php?work=edited&ip=".rawurlencode($ip));
    echo '<script type="text/javascript">location.replace("set_banlist.php?work=edited&ip='.rawurlencode($ip).'")</script>';
    
    } else {
      echo '<div class="oznameni_div">';
      echo '<div class="chyba">'.$lang['set_banlist_error'].'</div>';
      if (empty($id)) {echo '<p>'.$lang['set_banlist_er_4'].'</p>';};
      if (empty($ip)) {echo '<p>'.$lang['set_banlist_er_1'].'</p>';};
      if (empty($druh)) {echo '<p>'.$lang['set_banlist_er_2'].'</p>';};
      echo '</div>';
    };
    };
?>

<div class="tac">
<form method="post" action="set_banlist.php?action=edit&amp;id=<?php echo $id; ?>" id="banlist_form">
<label for="banlist_ip"><?php echo $lang['set_banlist_ip']; ?></label><input type="text" name="ip" id="banlist_ip" class="input"<?php if (empty($_POST['ip'])) {echo ' value="'.$result['ip'].'"';} else {echo ' value="'.$_POST['ip'].'"';}; ?> />
<select name="druh" size="1" class="input">
<option value="1"<?php if (empty($_POST['druh'])) {if ($result['druh'] == 1) {echo ' selected';};} else {if ($_POST['druh'] == 1) {echo ' selected';};}; ?> /><?php echo $lang['set_banlist_sel_1']; ?>
<option value="2"<?php if (empty($_POST['druh'])) {if ($result['druh'] == 2) {echo ' selected';};} else {if ($_POST['druh'] == 2) {echo ' selected';};}; ?>  /><?php echo $lang['set_banlist_sel_2']; ?>
</select>
<br /><br />
<label for="banlist_duvod"><?php echo $lang['set_banlist_form_reason']; ?></label><br />
<textarea name="duvod" id="banlist_duvod" wrap="hard"><?php if (empty($_POST['duvod'])) {echo $result['duvod'];} else {echo $_POST['duvod'];}; ?></textarea>
<br /><br />
<input type="submit" name="submit" value="<?php echo $lang['set_banlist_ban_ip']; ?>" class="submit" />
</form>
</div>

<?php
break;

case "delete" :

$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM jtcms_banlist WHERE id='".$id."' LIMIT 1");
$radku = mysql_num_rows($query);
$result = mysql_fetch_array($query);
$ip = $result['ip'];

 if (empty($id) or $radku == 0) {
    echo '<h1>'.$lang['set_banlist_delete_ban'].'</h1><br />';
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['set_banlist_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['set_banlist_er_4'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['set_banlist_er_5'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  }; 
  
    mysql_query("DELETE FROM jtcms_banlist WHERE id='".$id."' LIMIT 1");
    Header("Location: set_banlist.php?work=deleted&ip=".rawurlencode($ip));
    echo '<script type="text/javascript">location.replace("set_banlist.php?work=deleted&ip='.rawurlencode($ip).'")</script>'; 

break;

default :

echo '<h1>Blokování IP adres</h1><br />';

if (!isset($_GET['work'])) {$_GET['work'] = "default";};
switch ($_GET['work']) {

case "added" : 
echo '<div class="oznameni_div">';
echo '<p>'.$lang['set_banlist_work_1'].' <span class="blue">'.$_GET['ip'].'</span>';
if ($_GET['druh'] == 1) {echo ' '.$lang['set_banlist_work_2'];}; 
if ($_GET['druh'] == 2) {echo ' '.$lang['set_banlist_work_3'];};
echo '</p>';
echo '</div>';
break;

case "edited" :
echo '<div class="oznameni_div">';
echo '<p>'.$lang['set_banlist_work_4'].' <span class="blue">'.$_GET['ip'].'</span> '.$lang['set_banlist_work_5'].'</p>';
echo '</div>';
break;

case "deleted" :
echo '<div class="oznameni_div">';
echo '<p>'.$lang['set_banlist_work_4'].' <span class="blue">'.$_GET['ip'].'</span> '.$lang['set_banlist_work_6'].'</p>';
echo '</div>';
break;

};
?>

<div class="tac">
<form method="post" action="set_banlist.php?action=add" id="banlist_form">
<label for="banlist_ip"><?php echo $lang['set_banlist_ip']; ?></label><input type="text" name="ip" id="banlist_ip" class="input"<?php if (!empty($_GET['ip'])) {echo ' value="'.$_GET['ip'].'"';}; ?> />
<select name="druh" size="1" class="input">
<option value="1" /><?php echo $lang['set_banlist_sel_1']; ?>
<option value="2" /><?php echo $lang['set_banlist_sel_2']; ?>
</select>
<br /><br />
<label for="banlist_duvod"><?php echo $lang['set_banlist_form_reason']; ?></label><br />
<textarea name="duvod" id="banlist_duvod" wrap="hard"><?php if (!empty($_GET['duvod'])) {echo $_GET['duvod'];}; ?></textarea>
<br /><br />
<input type="submit" name="submit" value="<?php echo $lang['set_banlist_ban_ip']; ?>" class="submit" />
</form>
</div>
<br /><br />

<?php 
$query = mysql_query("SELECT * FROM jtcms_banlist ORDER by id DESC");
$radku = mysql_num_rows($query);
?>
<table class="tabulka_banlist">
<tr class="prvni_tr">
  <td class="td1 first">&nbsp;<?php echo $lang['set_banlist_ip2']; ?></td>
  <td class="td2"><?php echo $lang['set_banlist_reason']; ?></td>
  <td class="td3"><?php echo $lang['set_banlist_type']; ?></td>
  <td class="td4 last"><img src="stuff/img/icons/date.png" alt="<?php echo $lang['set_banlist_date']; ?>" title="<?php echo $lang['set_banlist_date']; ?>" /></td>
</tr>
<?php
if ($radku == 0) {
  echo '<tr><td colspan="5" style="text-align: center; padding: 5px;">'.$lang['set_banlist_no_bans'].'</td></tr>';
} else {
  while ($result = mysql_fetch_array($query)) {
    echo '<tr>';
    echo '<td>&nbsp;'.$result['ip'].'</td>';
    echo '<td>'.$result['duvod'].'</td>';
    echo '<td>';
      if ($result['druh'] == 1) {echo $lang['set_banlist_sel_1'];} else {echo $lang['set_banlist_sel_2'];};
    echo '</td>';
    echo '<td>';
      echo '<img src="stuff/img/icons/date.png" alt="'.$lang['set_banlist_date'].'" title="'.$lang['set_banlist_date'].' '.$result['datum'].'" />';
      echo '<a href="set_banlist.php?action=edit&amp;id='.$result['id'].'" title="'.$lang['set_banlist_edit_ban'].'"><img src="stuff/img/icons/edit.png" alt="'.$lang['set_banlist_edit_ban'].'" /></a>';
      echo '<img src="stuff/img/icons/trash.png" alt="'.$lang['set_banlist_delete_ban'].'" class="smaz" title="'.$lang['set_banlist_delete_ban'].'" onclick="smaz_ban(\''.$result['id'].'\',\''.$result['ip'].'\')" />';
    echo '</td>';
    echo '</tr>';
    echo "\n";
  };
};
?>
</table>
<script type="text/javascript">
function smaz_ban(id, ip){
jConfirm("<?php echo $lang['set_banlist_really_del_1']; ?> "+ip+"?", "<?php echo $lang['set_banlist_really_del_headline']; ?>", function(r) {
    if (r == true) {location.replace("set_banlist.php?action=delete&id="+id);};
});
};
</script>
<?php
};
include_end();
?>