<?php
/**
 * WebEngine CMS
 * https://webenginecms.org/
 * 
 * @version 1.2.6
 * @author Lautaro Angelico <http://lautaroangelico.com/>
 * @copyright (c) 2013-2025 Lautaro Angelico, All Rights Reserved
 * 
 * Licensed under the MIT license
 * http://opensource.org/licenses/MIT
 */
?>
<h1 class="page-header">Last 50 Bans</h1>
<?php
	$database = Connection::Database('Platform');
	$accountDatabase = Connection::Database('Me_MuOnline');
	$banRealm = NexoraRealm::active();
	
	if(isset($_POST['liftban'])) {
		try {
			if(!Validator::UnsignedNumber($_POST['liftban'])) throw new Exception("Invalid ban id.");
			
			// Retrieve Ban Information
			$banInfo = $database->query_fetch_single("SELECT * FROM ".WEBENGINE_BAN_LOG." WHERE id = ? AND realm = ?", array($_POST['liftban'], $banRealm));
			if(!is_array($banInfo)) throw new Exception("Ban ID does not exist.");
			
			// Check account status
			//if($common->accountOnline($banInfo['account_id'])) throw new Exception("The account is online.");
			
			// Unban Account
			$unban = $accountDatabase->query("UPDATE "._TBL_MI_." SET "._CLMN_BLOCCODE_." = 0 WHERE "._CLMN_USERNM_." = ?", array($banInfo['account_id']));
			if(!$unban) throw new Exception("Could not update account information (unban).");
			
			// Remove Ban log
			$database->query("DELETE FROM ".WEBENGINE_BAN_LOG." WHERE account_id = ? AND realm = ?", array($banInfo['account_id'], $banRealm));
			$database->query("DELETE FROM ".WEBENGINE_BANS." WHERE account_id = ? AND realm = ?", array($banInfo['account_id'], $banRealm));
			
			NexoraAudit::log('admin.account_unbanned', 'account', $banInfo['account_id'], array('ban_type'=>$banInfo['ban_type'],'realm'=>$banRealm), array('banned'=>false,'realm'=>$banRealm));
			message('success', 'Account ban lifted');
		} catch(Exception $ex) {
			message('error', $ex->getMessage());
		}
	}
?>
<div class="row">
	<div class="col-md-12">
		<div class="panel-body">
			<!-- Nav tabs -->
			<ul class="nav nav-tabs">
				<li class="active"><a href="#temp" data-toggle="tab" aria-expanded="true">Temporal</a>
				</li>
				<li class=""><a href="#perm" data-toggle="tab" aria-expanded="false">Permanent</a>
				</li>
			</ul>

			<!-- Tab panes -->
			<div class="tab-content">
				<div class="tab-pane fade active in" id="temp"><br />
				<?php
					$tBans = $database->query_fetch("SELECT TOP 25 * FROM ".WEBENGINE_BAN_LOG." WHERE ban_type = ? AND realm = ? ORDER BY id DESC", array("temporal", $banRealm));
					if(is_array($tBans)) {
						echo '<table class="table table-condensed">';
							echo '<thead>';
							echo '<tr>';
								echo '<th>Account</th>';
								echo '<th>Banned By</th>';
								echo '<th>Date</th>';
								echo '<th>Days</th>';
								echo '<th>Reason</th>';
								echo '<th></th>';
							echo '</tr>';
							echo '</thead>';
							echo '<tbody>';
							foreach($tBans as $temporalBan) {
								echo '<tr>';
									echo '<td>'.$temporalBan['account_id'].'</td>';
									echo '<td>'.$temporalBan['banned_by'].'</td>';
									echo '<td>'.date("Y-m-d H:i", $temporalBan['ban_date']).'</td>';
									echo '<td>'.$temporalBan['ban_days'].'</td>';
									echo '<td>'.$temporalBan['ban_reason'].'</td>';
									echo '<td style="text-align:right;"><form method="post" action="'.admincp_base('latestbans').'" style="display:inline"><input type="hidden" name="liftban" value="'.(int)$temporalBan['id'].'"><button type="submit" class="btn btn-danger btn-xs">Lift Ban</button></form></td>';
								echo '</tr>';
							}
						echo '</tbody>';
						echo '</table>';
					} else {
						message('warning', 'There are no temporal bans logged.', ' ');
					}
				?>
				</div>
				<div class="tab-pane fade" id="perm"><br />
				<?php
					$pBans = $database->query_fetch("SELECT TOP 25 * FROM ".WEBENGINE_BAN_LOG." WHERE ban_type = ? AND realm = ? ORDER BY id DESC", array("permanent", $banRealm));
					if(is_array($pBans)) {
						echo '<table class="table table-condensed">';
							echo '<thead>';
							echo '<tr>';
								echo '<th>Account</th>';
								echo '<th>Banned By</th>';
								echo '<th>Date</th>';
								echo '<th>Reason</th>';
								echo '<th></th>';
							echo '</tr>';
							echo '</thead>';
							echo '<tbody>';
							foreach($pBans as $permanentBan) {
								echo '<tr>';
									echo '<td>'.$permanentBan['account_id'].'</td>';
									echo '<td>'.$permanentBan['banned_by'].'</td>';
									echo '<td>'.date("Y-m-d H:i", $permanentBan['ban_date']).'</td>';
									echo '<td>'.$permanentBan['ban_reason'].'</td>';
									echo '<td style="text-align:right;"><form method="post" action="'.admincp_base('latestbans').'" style="display:inline"><input type="hidden" name="liftban" value="'.(int)$permanentBan['id'].'"><button type="submit" class="btn btn-danger btn-xs">Lift Ban</button></form></td>';
								echo '</tr>';
							}
						echo '</tbody>';
						echo '</table>';
					} else {
						message('warning', 'There are no permanent bans logged.', ' ');
					}
				?>
				</div>
			</div>
		</div>
	</div>
</div>