<?php

global $headerinclude, $header, $theme, $footer, $lang;

// change this to the group ID of groups that should be able to see who banned a hacker. there is also commented support for unbanning from the ban page, but as I said, commented
$allowed = array(
  4, // Owner
  9, // Lead Admin
  3, // Admins
  6, // Global Mod
);

$bMod = false;

if($mybb->user['uid'] && in_array($mybb->user['usergroup'], $allowed)) {
  //error_no_permission();
  $bMod = true;
}

//$lang->load('modcp');

function s2p($steam_id){
  $steam_id=strtolower($steam_id);
  if (substr($steam_id,0,7)=='steam_0') {
    $tmp=explode(':',$steam_id);
    if ((count($tmp)==3) && is_numeric($tmp[1]) && is_numeric($tmp[2])){
      return str_replace(".0000000000", "", bcadd((($tmp[2]*2)+$tmp[1]),'76561197960265728'));
        }
    else return false;
  }
  else {
    return false;
  }
}

function dateDiff($time1, $time2, $precision = 6) {
    // If not numeric then convert texts to unix timestamps
    if (!is_int($time1)) {
      $time1 = strtotime($time1);
    }
    if (!is_int($time2)) {
      $time2 = strtotime($time2);
    }
 
    // If time1 is bigger than time2
    // Then swap time1 and time2
    if ($time1 > $time2) {
      $ttime = $time1;
      $time1 = $time2;
      $time2 = $ttime;
    }
 
    // Set up intervals and diffs arrays
    $intervals = array('year','month','day','hour','minute','second');
    $diffs = array();
 
    // Loop thru all intervals
    foreach ($intervals as $interval) {
      // Set default diff to 0
      $diffs[$interval] = 0;
      // Create temp time from time1 and interval
      $ttime = strtotime("+1 " . $interval, $time1);
      // Loop until temp time is smaller than time2
      while ($time2 >= $ttime) {
  $time1 = $ttime;
  $diffs[$interval]++;
  // Create new temp time from time1 and interval
  $ttime = strtotime("+1 " . $interval, $time1);
      }
    }
 
    $count = 0;
    $times = array();
    // Loop thru all diffs
    foreach ($diffs as $interval => $value) {
      // Break if we have needed precission
      if ($count >= $precision) {
  break;
      }
      // Add value and interval 
      // if value is bigger than 0
      if ($value > 0) {
  // Add s if value is not 1
  /*if ($value != 1) {
    $interval .= "s";
  }*/
  // Add value and interval to times array
  //$times[] = $value . " " . $interval;
  $times[] = $value.$interval;
  $count++;
      }
    }
 
    // Return string with times
    $tmp = implode(", ", $times);
    $tmp = str_replace("year", "yr", $tmp);
    $tmp = str_replace("month", "mn", $tmp);
    $tmp = str_replace("day", "d", $tmp);
    $tmp = str_replace("hour", "h", $tmp);
    $tmp = str_replace("minute", "m", $tmp);
    $tmp = str_replace("second", "s", $tmp);
    return $tmp;
  }

try {
  $frank = new PDO("mysql:host=localhost;dbname=solidser_bans", "solidser_bans", ".~u1=^b;Mh2{"); 
} catch (Exception $e) {
  
}

/*if($bMod && isset($_GET['id'])){
  $id = intval($_GET['id']);
  $exists = mysql_query("SELECT `steam` FROM `bans` WHERE `id` = '$id' AND `asteam` != 'STEAM_0:0:1'");
  $steam = mysql_result($exists, 0);
  if(mysql_num_rows($exists) > 0){
    mysql_query("DELETE FROM `bans` WHERE `id` = '".intval($_GET['id'])."'");
    mysql_query("INSERT INTO `log` ( time, message, server ) VALUES('".time()."', '[ADMIN] ".$mybb->user['username']." unbanned $steam', '0')");
  }
}*/

//$statement = $frank->query("SELECT `id`, `nick`, `steam`, `unban_time`, `reason`, `anick`, `asteam` FROM `bans` WHERE `asteam` != 'STEAM_0:0:1' ORDER BY `unban_time`=0 ASC, UNIX_TIMESTAMP()<=`unban_time` ASC, `unban_time` ASC");
$statement = $frank->query("SELECT `id`, `nick`, `steam`, `unban_time`, `reason`, `anick`, `asteam` FROM `bans` WHERE (`unban_time` > UNIX_TIMESTAMP() OR `unban_time` = 0) AND `asteam` != 'STEAM_0:0:1' ORDER BY `unban_time` = 0 ASC, `unban_time` ASC");

$btr = "";

if($statement->rowCount() > 0) {
  $num = 1;
  foreach($statement->fetchAll() as $objBan)
  {
    $strNick = $objBan['nick'];
    $strSteam = $objBan['steam'];
    $iTime = intval($objBan['unban_time']);
    $strReason = $objBan['reason'];
    $strANick = $objBan['anick'];
    $strASteam = $objBan['asteam'];

    $unban = "";

    if($iTime == 0) {
      $unban = "<strong>Never</strong>"; // price said the ! makes it seem like I am yelling at him :(
    }
    else {
      $unban_time = $iTime - time();
      if ($unban_time < 0) {
        $unban = "<strong><a title='This player has not rejoined since their ban expired'>Unbanned!</a></strong>";
      }
      else {
        $unban = dateDiff($iTime, time());
      }
    }

    $iFound = preg_match("/.*?(Hack)/is", $strReason, $iFound);

    if($iFound === 1)
    {
	  // the steamid here says "if the admin that banned them is STEAMID then show that admin banned the hacker anyway
      if($strASteam !== "STEAM_0:0:19878867" && $bMod === false)
      {
        $strANick = "Hidden";
        $strASteam = "STEAM_0:0:1";
      }
    }

    $i = 1;
    if($num % 2 == 0) {
      $i = 2;
    }
    $extra = "";
    /*if($bMod){
      $extra = '<a href="/misc.php?page=bans&id='.$ban['id'].'"><img src="/images/exclamation.png" border="0" title="Unban '.$nick.'"></a> ';
    }*/
    $btr .= '<tr>
    <td class="trow'.$i.'">'.$strNick.'</td>
    <td class="trow'.$i.'">
      <a href="http://steamcommunity.com/profiles/'.s2p($strSteam).'" target="_blank">'.$strSteam.'</a>
    </td>
    <td class="trow'.$i.'">'.$extra.$unban.'</td>
    <td class="trow'.$i.'">'.$strReason.'</td>
    <td class="trow'.$i.'"><a href="http://steamcommunity.com/profiles/'.s2p($strASteam).'" target="_blank">'.$strANick.'</a></td>';
    $btr .= '</tr>';
    $num = $num + 1;
  }
}
else {
  $btr = '<tr><td class="trow1" colspan="5" align="center">There are no bans</td></tr>';  
}

$content = "";

$link = "1";
$word = "hide";
if($extra != "") {
  $link = "0";
  $word = "show";
}
$content = '<table border="0" cellspacing="1" cellpadding="4" class="tborder">
  <tr><td class="thead" colspan="5"><strong>Server Bans</strong> &bull; '.number_format($statement->rowCount()).' bans</td></tr>
  <tr>
  <td class="tcat" style="max-width: 15%; width: 15%;"><span class="smalltext"><strong>Name</strong></span></td>
  <td class="tcat"><span class="smalltext"><strong>SteamID</strong></span></td>
  <td class="tcat"><span class="smalltext"><strong>Unban</strong></span></td>
  <td class="tcat" style="max-width: 50%; width: 50%;"><span class="smalltext"><strong>Reason</strong></span></td>
  <td class="tcat"><span class="smalltext"><strong>Admin</strong></span></td>
  </tr>
  {$btr}
  </table>';

$template='<html>
<head>
<title>'.$pages['name'].'</title>
{$headerinclude}
</head>
<body id="bans">
{$header}
'.$content.'
{$footer}
</body>
</html>';

$template=str_replace("\'", "'", addslashes($template));

add_breadcrumb($pages['name']);

eval("\$page=\"".$template."\";");

output_page($page);
?>