GlobalBan is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    GlobalBan is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GlobalBan.  If not, see <http://www.gnu.org/licenses/>.
*/
require_once ROOTDIR . "/include/database/class.BanQueries.php";
$banQueries = new BanQueries();
$bannedUsers = $banQueries->downloadActiveBans(true, false);
// Cycle through each user and create the keygroup
foreach ($bannedUsers as $bannedUser) {
    $now = time();
    // In seconds
    $expireDateSeconds = $bannedUser->getExpireDateSeconds();
    $banTime = 0;
    // Determine how much longer the ban is based on the expire date
    if ($expireDateSeconds > 0) {
        $banTime = $now - $expireDateSeconds;
    }
    if ($banTime > -1) {
        echo "banid " . floor($banTime / 60) . " " . $bannedUser->getSteamId() . "\n";
    }
}
function UpdateBanWebpage($postId, $banId, $config)
{
    $banQueries = new BanQueries();
    $banQueries->updateBanWebpage($config->e107Url . "e107_plugins/forum/forum_viewtopic.php?" . $postId, $banId);
}
function kickUser($steamId, $serverId, $message, $messageTOserver)
{
    // Leave this in to be compatible with the alternate thread version
    $kick = "kickid";
    $command = $kick . " \"" . $steamId . "\" " . $message;
    echo $command;
    // This will send an RCON command to the server
    $serverQueries = new ServerQueries();
    $server = $serverQueries->getServer($serverId);
    $r = new rcon($server->getIp(), $server->getPort(), $server->getRcon());
    if ($r->isValid()) {
        $r->Auth();
        $r->kickUser($steamId, $message);
        $r->sendRconCommand("banid 5 " . $steamId);
        $r->sendRconCommand("es_msg " . $messageTOserver);
    }
    $banQueries2 = new BanQueries();
    $banQueries2->updateKickCounter($steamId);
}
$comments = $_POST['comments'];
// comments
$bannedPost = $_POST['bannedPost'];
// Link to Post about ban
$ModifiedBy = $_POST['ModifiedBy'];
$fullPowerLevelEditUser = $_POST['fullPowerLevelEditUser'];
// Make sure the user is an UNBU member, admin, or ban manager
if ($member || $admin || $banManager || $fullPower) {
    $allowedToBan = true;
}
if ($allowedToBan) {
    // Make sure special chars for MySQL are escaped
    $bannedUser = addslashes($bannedUser);
    $bannedUser = str_ireplace(array("\"", "\r\n", "\n", "\r", ";"), "", $bannedUser);
    // Remove ; to prevent kick command inyection like name; quit or ; _restart
    $banQueries = new BanQueries();
    $lengthQueries = new LengthQueries();
    $length = $lengthQueries->getBanLength($lengthId);
    // Banned user information
    $bannedUserO = $banQueries->getBannedUser($banId);
    if ($fullPower || $banManager || $bannedUserO->getBanner() == $_SESSION['name'] && !empty($_SESSION['name']) && ($admin || $member) || $bannedUserO->getBannerSteamId() == $_SESSION['steamId'] && !empty($_SESSION['steamId']) && ($admin || $member)) {
        // We are banning an IP
        if (isset($_POST['banIp'])) {
            $banQueries->addIpBan($_POST['ip']);
            header("Location: index.php?page=updateBan&banId=" . $banId);
        } else {
            if (isset($_POST['updateBan'])) {
                // We are updating ban information
                if ($config->enableSmfIntegration) {
                    $username = $user_info['username'];
                } else {
     }
 }
 // The XML form was submitted
 if (isset($_POST['submitXMLImport'])) {
     $reason = $_POST['reasonXML'];
     // Reason Id
     $reason = $_POST['reason'];
     // Reason Id
     $tempName = $_FILES['file']['tmp_name'];
     // Temp name of when it is uploaded
     $count = 0;
     $alreadyAdded = 0;
     $alreadyDs = "";
     $failed = 0;
     if (is_uploaded_file($tempName)) {
         $banQueries = new BanQueries();
         $xml = "";
         $f = fopen($tempName, 'r');
         while ($data = fread($f, 4096)) {
             $xml .= $data;
         }
         fclose($f);
         preg_match_all("/\\<Ban\\>(.*?)\\<\\/Ban\\>/s", $xml, $banBlocks);
         foreach ($banBlocks[1] as $block) {
             preg_match_all("/\\<SteamID\\>(.*?)\\<\\/SteamID\\>/", $block, $steamId);
             preg_match_all("/\\<IP\\>(.*?)\\<\\/IP\\>/", $block, $ip);
             preg_match_all("/\\<Name\\>(.*?)\\<\\/Name\\>/", $block, $name);
             preg_match_all("/\\<Length\\>(.*?)\\<\\/Length\\>/", $block, $length);
             preg_match_all("/\\<TimeScale\\>(.*?)\\<\\/TimeScale\\>/", $block, $timeScale);
             preg_match_all("/\\<AddDate\\>(.*?)\\<\\/AddDate\\>/", $block, $addDate);
             preg_match_all("/\\<ExpireDate\\>(.*?)\\<\\/ExpireDate\\>/", $block, $expireDate);
}
if ($saveSQL == 1) {
    $saveSQL = true;
} else {
    $saveSQL = false;
}
if ($saveSQL) {
    $filename = $config->siteName . "_banned_users.xml";
    header("Content-type: text/xml charset=UTF-8");
} else {
    $filename = $config->siteName . "_banned_users.cfg";
    header("Content-type: text/plain");
}
$header = "Content-Disposition: attachment; filename=\"" . $filename . "\"";
header($header);
$banQueries = new BanQueries();
$bannedUsers = $banQueries->downloadActiveBans($all, $demosOnly);
function strleft($s1, $s2)
{
    return substr($s1, 0, strpos($s1, $s2));
}
function selfURL()
{
    $s = empty($_SERVER["HTTPS"]) ? '' : $_SERVER["HTTPS"] == "on" ? "s" : "";
    $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/") . $s;
    $port = $_SERVER["SERVER_PORT"] == "80" ? "" : ":" . $_SERVER["SERVER_PORT"];
    return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
}
$url = selfURL();
$url = substr($url, 0, strrpos($url, "/")) . "/";
if ($saveSQL) {
} else {
    $bansFilter = "";
}
if (!empty($_GET['bri'])) {
    $bansReason_id = $_GET['bri'];
} else {
    $bansReason_id = "";
}
if (!empty($_GET['ba'])) {
    $bansAdmin = $_GET['ba'];
} else {
    $bansAdmin = "";
}
$lan_file = ROOTDIR . '/languages/' . $LANGUAGE . '/lan_banlist.php';
include file_exists($lan_file) ? $lan_file : ROOTDIR . "/languages/English/lan_banlist.php";
$banQueries = new BanQueries();
// Ban delete process
if ($fullPower) {
    // A full power admin executed a ban delete
    if (!empty($_GET['process']) && !empty($_GET['BanId'])) {
        if ($_GET['process'] == "delete") {
            if ($_GET['BanId'] != null || $_GET['BanId'] != "") {
                $deleteBannedUser = $banQueries->getBannedUser($_GET['BanId']);
                unBanUser($deleteBannedUser->getSteamId(), $deleteBannedUser->getIp());
                $banQueries->deleteBan($deleteBannedUser->getSteamId());
            }
        }
    }
}
function unBanUser($steamId, $bannedIP)
{
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GlobalBan.  If not, see <http://www.gnu.org/licenses/>.
*/
require_once ROOTDIR . "/include/database/class.BanQueries.php";
$ip = $_GET['ip'];
$active = $_GET['active'];
// Switch the current active state to the other state
// off becomes on, and on becomes off
if ($active == 0) {
    $active = 1;
} else {
    $active = 0;
}
$banQueries = new BanQueries();
// Must be logged in to change the status
if ($member || $admin || $banManager || $fullPower) {
    $banQueries->updateBanIpActiveStatus($active, $ip);
}
// Send back the new active state
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
echo "<?xml version=\"1.0\" ?>";
?>
<root>
  <ip><?php 
echo $ip;
?>
</ip>
	<update><?php 
if ($member || $admin || $banManager || $fullPower) {
    require_once ROOTDIR . "/include/database/class.ServerQueries.php";
    require_once ROOTDIR . "/include/database/class.ReasonQueries.php";
    require_once ROOTDIR . "/include/database/class.BanQueries.php";
    include_once ROOTDIR . "/include/database/class.LengthQueries.php";
    require_once ROOTDIR . "/include/database/class.UserQueries.php";
    // User specific queries
    require_once ROOTDIR . "/include/objects/class.User.php";
    // User class to store user info
    include_once ROOTDIR . "/include/objects/class.Length.php";
    $lan_file = ROOTDIR . '/languages/' . $LANGUAGE . '/lan_updateBan.php';
    include file_exists($lan_file) ? $lan_file : ROOTDIR . "/languages/English/lan_updateBan.php";
    // Initialize Objects
    $serverQueries = new ServerQueries();
    $reasonQueries = new ReasonQueries();
    $banQueries = new BanQueries();
    $lengthQueries = new LengthQueries();
    $userQuery = new UserQueries();
    $userEdit = new User();
    // Ban ID
    $banId = $_GET['banId'];
    // Get the list of servers
    $serverList = $serverQueries->getServers();
    // List of Admins
    $banAmins = $reasonQueries->getAdminsList();
    // List of Reasons
    $banReasons = $reasonQueries->getReasonList();
    // Banned user information
    $bannedUser = $banQueries->getBannedUser($banId);
    // List of Ban Lengths
    $banLengths = $lengthQueries->getLengthList();
}
if (!empty($_GET['sd'])) {
    $sortDirection = $_GET['sd'];
    // Start Range
} else {
    $sortDirection = "DESC";
}
if (!empty($_POST['searchText'])) {
    $searchText = $_POST['searchText'];
    // Start Range
} else {
    $searchText = "";
}
$lan_file = ROOTDIR . '/languages/' . $LANGUAGE . '/lan_ipbanlist.php';
include file_exists($lan_file) ? $lan_file : ROOTDIR . "/languages/English/lan_ipbanlist.php";
$banQueries = new BanQueries();
// Count how many bans exist in the database
$ipCount = $banQueries->getNumberOfIpBans($banManager, $fullPower, $searchText);
$bannedIps = $banQueries->getIpBanList($banManager, $fullPower, $startRange, $ipCount, $sortBy, $sortDirection, $searchText);
?>
<div id="search" align="right">
  <form action="" method="post">
  <input name="searchText" id="searchText" type="text" value="<?php 
echo $searchText;
?>
" size="40" maxLength="40"/>
  <input type="submit" value="<?php 
echo $LANIPBAN_011;
?>
">
  </form>
    along with GlobalBan.  If not, see <http://www.gnu.org/licenses/>.
*/
require_once ROOTDIR . "/include/database/class.BanQueries.php";
require_once ROOTDIR . "/include/database/class.ServerQueries.php";
require_once ROOTDIR . "/include/class.rcon.php";
// require_once(ROOTDIR."/include/objects/class.BannedUser.php");
$id = $_POST['id'];
$active = $_POST['active'];
// Switch the current active state to the other state
// off becomes on, and on becomes off
if ($active == 0) {
    $active = 1;
} else {
    $active = 0;
}
$banQueries = new BanQueries();
// Must be logged in to change the status
if ($member || $admin || $banManager || $fullPower) {
    $banQueries->updateBanActiveStatus($active, $id);
    if ($active == 0) {
        //$bannedUser = new BannedUser();
        $bannedUser = $banQueries->getBannedUser($id);
        unBanUser($bannedUser->getSteamId(), $bannedUser->getIp());
    }
}
function unBanUser($steamId, $bannedIP)
{
    // This will send an RCON command to the server
    $serverQueries = new ServerQueries();
    // Get the list of servers
    $servers = $serverQueries->getServers();
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GlobalBan.  If not, see <http://www.gnu.org/licenses/>.
*/
require_once ROOTDIR . "/include/database/class.BanQueries.php";
$id = $_GET['id'];
$pending = $_GET['pending'];
// Switch the current active state to the other state
// off becomes on, and on becomes off
if ($pending == 0) {
    $pending = 1;
} else {
    $pending = 0;
}
$banQueries = new BanQueries();
// Must be logged in to change the status
if ($member || $admin || $banManager || $fullPower) {
    $banQueries->updateBanPendingStatus($pending, $id);
}
// Send back the new active state
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
echo "<?xml version=\"1.0\" ?>";
?>
<root>
  <id><?php 
echo $id;
?>
</id>
	<update><?php 
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GlobalBan.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * This is a standalone page to save a file.  All globals/constants on the index.php
 * page should be declared here so that no errors occur.
 */
define("ROOTDIR", dirname(__FILE__));
// Global Constant of root directory
require_once ROOTDIR . "/config/class.Config.php";
$config = new Config();
// All configuration variables are contained in this object
include_once ROOTDIR . "/include/database/class.BanQueries.php";
require_once ROOTDIR . "/include/objects/class.Ip.php";
$filename = $config->siteName . "_banned_ip.cfg";
$header = "Content-Disposition: attachment; filename=\"" . $filename . "\"";
header("Content-type: text/plain");
header($header);
$banQueries = new BanQueries();
$bannedIps = $banQueries->downloadActiveIps();
// Empty line after banid print is needed to force a new line
foreach ($bannedIps as $bannedIp) {
    ?>
addip 0 <?php 
    echo $bannedIp->getIp();
    ?>

<?php 
}