* WolfPanel is licensed under a
 * Creative Commons Attribution-NonCommercial 4.0 International License.
 * 
 * You should have received a copy of the license along with this
 * work. If not, see <http://creativecommons.org/licenses/by-nc/4.0/>. 
 */
include "../core.php";
// Admin Check
$user = UserManager::getLocalUser();
if (!$user->isAdmin()) {
    PageManager::displayErrorPage("access");
    return;
}
$id = isset($_GET["serverid"]) && is_numeric($_GET["serverid"]) ? $_GET["serverid"] : -1;
$port = isset($_GET["port"]) && is_numeric($_GET["port"]) ? $_GET["port"] : -1;
if (!PhysicalServerManager::existsById($id)) {
    Page::renderJson(array("status" => "error", "message" => "Server doesnt exist"));
    return;
}
if ($port < Core::getConfig("port_min") || $port > Core::getConfig("port_max")) {
    Page::renderJson(array("status" => "error", "message" => "Invalid Port"));
    return;
}
$server = new PhysicalServer($id);
if ($server->isPortFree($port)) {
    Page::renderJson(array("status" => "success", "message" => "Port is available"));
    return;
} else {
    Page::renderJson(array("status" => "error", "message" => "Port is not available"));
    return;
}