echo "Using existing block for IPs\n";
                 $sBlock = new Block($sFeathurBlockData->data["0"]["id"]);
             }
         }
         if (!($sIPExists = $database->CachedQuery("SELECT * FROM `ipaddresses` WHERE `ip_address` = :IP", array('IP' => $sIP["ipaddress"])))) {
             echo "Adding IP to database.\n";
             $sIPAdd = new IP(0);
             $sIPAdd->uIPAddress = $sIP["ipaddress"];
             $sIPAdd->uVPSId = $sVPS->sId;
             $sIPAdd->uBlockId = $sBlock->sId;
             $sIPAdd->InsertIntoDatabase();
         } else {
             echo "Using existing IP.\n";
             $sIPAdd = new IP($sIPExists->data[0]["id"]);
             $sIPAdd->uVPSId = $sVPS->sId;
             $sIPAdd->InsertIntoDatabase();
         }
         $sIPTotal++;
     }
 }
 $sVPS->uPrimaryIP = $sValue["mainipaddress"];
 $sVPS->InsertIntoDatabase();
 echo "Moving VPS configs to new ID...\n";
 $sCommandList = "vzctl chkpnt {$sValue["ctid"]} --dumpfile /tmp/Dump.{$sValue["ctid"]};";
 $sCommandList .= "mv /etc/vz/conf/{$sValue["ctid"]}.conf /etc/vz/conf/{$sVPS->sContainerId}.conf;";
 $sCommandList .= "mv /vz/private/{$sValue["ctid"]} /vz/private/{$sVPS->sContainerId};";
 $sCommandList .= "mv /vz/root/{$sValue["ctid"]} /vz/root/{$sVPS->sContainerId};";
 $sCommandList .= "vzctl restore {$sVPS->sContainerId} --dumpfile /tmp/Dump.{$sValue["ctid"]};";
 $sServerConnect = $sServer->server_connect($sServer);
 $sUpdate = $sServerConnect->exec($sCommandList);
 $sStatus = $sServerConnect->exec("vzctl status {$sVPS->sContainerId};");
 public function database_openvz_assignip($sUser, $sVPS, $sRequested)
 {
     $sUserPermissions = $sUser->sPermissions;
     $sVPSUser = new User($sVPS->sUserId);
     if ($sUserPermissions == 7) {
         global $database;
         $sServer = new Server($sVPS->sServerId);
         $sSSH = Server::server_connect($sServer);
         $sIP = $sRequested["GET"]["ip"];
         $sCheckExisting = $database->CachedQuery("SELECT * FROM ipaddresses WHERE `ip_address` = :IPAddress", array('IPAddress' => $sIP));
         if (!empty($sCheckExisting)) {
             foreach ($sCheckExisting->data as $value) {
                 if (empty($value["vps_id"])) {
                     $sIPList[] = array("id" => $value["id"], "ip_address" => $value["ip_address"]);
                 }
                 $sTotalIPs++;
             }
         }
         $sAvailableIPs = count($sIPList);
         if ($sAvailableIPs == 1) {
             $sUpdate = $database->CachedQuery("UPDATE ipaddresses SET `vps_id` = :VPSId WHERE `id` = :Id", array('VPSId' => $sVPS->sId, 'Id' => $sIPList[0]["id"]));
             $sIPs = $database->CachedQuery("SELECT * FROM ipaddresses WHERE `vps_id` = :VPSId", array('VPSId' => $sVPS->sId));
             foreach ($sIPs->data as $subvalue) {
                 $sCommand .= "vzctl set {$sVPS->sContainerId} --ipadd {$subvalue["ip_address"]} --save;";
                 $sTotal++;
             }
             $sLog[] = array("command" => $sCommand, "result" => $sSSH->exec($sCommand));
             $sVariable = array("ip" => urlencode($sIP));
             return $sArray = array("json" => 1, "type" => "success", "result" => "The IP {$sIP} was added to this VPS.", "reload" => 1);
         } elseif (empty($sTotalIPs) && empty($sAvailableIPs)) {
             $sIPExpand = explode(".", $sIP);
             if ($sFindBlock = $database->CachedQuery("SELECT * FROM ipaddresses WHERE `ip_address` LIKE :IPAddress", array('IPAddress' => "%{$sIPExpand[0]}.{$sIPExpand[1]}.{$sIPExpand[2]}%"))) {
                 $sIPAdd = new IP(0);
                 $sIPAdd->uIPAddress = $sIP;
                 $sIPAdd->uVPSId = $sVPS->sId;
                 $sIPAdd->uBlockId = $sFindBlock->data[0]["block_id"];
                 $sIPAdd->InsertIntoDatabase();
                 $sIPs = $database->CachedQuery("SELECT * FROM ipaddresses WHERE `vps_id` = :VPSId", array('VPSId' => $sVPS->sId));
                 foreach ($sIPs->data as $subvalue) {
                     $sCommand .= "vzctl set {$sVPS->sContainerId} --ipadd {$subvalue["ip_address"]} --save;";
                     $sTotal++;
                 }
                 $sLog[] = array("command" => $sCommand, "result" => $sSSH->exec($sCommand));
                 $sVariable = array("ip" => urlencode($sIP));
                 return $sArray = array("json" => 1, "type" => "success", "result" => "The IP {$sIP} was added to this VPS.", "reload" => 1);
             } else {
                 $sIPAdd = new IP(0);
                 $sIPAdd->uIPAddress = $sIP;
                 $sIPAdd->uVPSId = $sVPS->sId;
                 $sIPAdd->uBlockId = 0;
                 $sIPAdd->InsertIntoDatabase();
                 $sIPs = $database->CachedQuery("SELECT * FROM ipaddresses WHERE `vps_id` = :VPSId", array('VPSId' => $sVPS->sId));
                 foreach ($sIPs->data as $subvalue) {
                     $sCommand .= "vzctl set {$sVPS->sContainerId} --ipadd {$subvalue["ip_address"]} --save;";
                     $sTotal++;
                 }
                 $sLog[] = array("command" => $sCommand, "result" => $sSSH->exec($sCommand));
                 $sVariable = array("ip" => urlencode($sIP));
                 return $sArray = array("json" => 1, "type" => "success", "result" => "The IP {$sIP} was added to this VPS.", "reload" => 1);
             }
         } elseif (!empty($sTotalIPs) && $sAvailableIPs != 1) {
             return $sArray = array("json" => 1, "type" => "error", "result" => "The IP you gave is assigned to a VPS!");
         } else {
             return $sArray = array("json" => 1, "type" => "error", "result" => "An unknown error occured!");
         }
     } else {
         return $sArray = array("json" => 1, "type" => "error", "result" => "Insufficient permissions for this action.");
     }
 }
 public function database_kvm_assignip($sUser, $sVPS, $sRequested)
 {
     $sUserPermissions = $sUser->sPermissions;
     $sVPSUser = new User($sVPS->sUserId);
     if ($sUserPermissions == 7) {
         global $database;
         $sIP = $sRequested["GET"]["ip"];
         $sCheckExisting = $database->CachedQuery("SELECT * FROM ipaddresses WHERE `ip_address` = :IPAddress", array('IPAddress' => $sIP));
         if (!empty($sCheckExisting)) {
             foreach ($sCheckExisting->data as $value) {
                 if (empty($value["vps_id"])) {
                     $sIPList[] = array("id" => $value["id"], "ip_address" => $value["ip_address"]);
                 }
                 $sTotalIPs++;
             }
         }
         $sAvailableIPs = count($sIPList);
         if ($sAvailableIPs == 1) {
             $sUpdate = $database->CachedQuery("UPDATE ipaddresses SET `vps_id` = :VPSId WHERE `id` = :Id", array('VPSId' => $sVPS->sId, 'Id' => $sIPList[0]["id"]));
             return true;
         } elseif (empty($sTotalIPs) && empty($sAvailableIPs)) {
             $sIPExpand = explode(".", $sIP);
             if ($sFindBlock = $database->CachedQuery("SELECT * FROM ipaddresses WHERE `ip_address` LIKE :IPAddress", array('IPAddress' => "%{$sIPExpand[0]}.{$sIPExpand[1]}.{$sIPExpand[2]}%"))) {
                 $sIPAdd = new IP(0);
                 $sIPAdd->uIPAddress = $sIP;
                 $sIPAdd->uVPSId = $sVPS->sId;
                 $sIPAdd->uBlockId = $sFindBlock->data[0]["block_id"];
                 $sIPAdd->InsertIntoDatabase();
                 return true;
             } else {
                 $sIPAdd = new IP(0);
                 $sIPAdd->uIPAddress = $sIP;
                 $sIPAdd->uVPSId = $sVPS->sId;
                 $sIPAdd->uBlockId = 0;
                 $sIPAdd->InsertIntoDatabase();
                 return true;
             }
         } elseif (!empty($sTotalIPs) && $sAvailableIPs != 1) {
             return $sArray = array("json" => 1, "type" => "error", "result" => "The IP you gave is assigned to a VPS!");
         } else {
             return $sArray = array("json" => 1, "type" => "error", "result" => "An unknown error occured!");
         }
     } else {
         return $sArray = array("json" => 1, "type" => "error", "result" => "Insufficient permissions for this action.");
     }
 }
 public static function add_ipv4_range($sRequested)
 {
     global $database;
     if (filter_var($sRequested["GET"]["start"], FILTER_VALIDATE_IP) && filter_var($sRequested["GET"]["end"], FILTER_VALIDATE_IP)) {
         if (is_numeric($sRequested["GET"]["pool"])) {
             $sStart = ip2long($sRequested["GET"]["start"]);
             $sEnd = ip2long($sRequested["GET"]["end"]);
             $sTotalIPs = $sEnd - $sStart;
             if ($sTotalIPs < 256 && $sTotalIPs > 0) {
                 $sCurrent = $sStart;
                 while ($sCurrent <= $sEnd) {
                     if (!($sIPExists = $database->CachedQuery("SELECT * FROM `ipaddresses` WHERE `ip_address` = :IP", array('IP' => long2ip($sCurrent))))) {
                         $sIP = new IP(0);
                         $sIP->uIPAddress = long2ip($sCurrent);
                         $sIP->uVPSId = 0;
                         $sIP->uBlockId = $sRequested["GET"]["pool"];
                         $sIP->InsertIntoDatabase();
                         unset($sIP);
                     }
                     $sCurrent++;
                 }
                 return $sSuccess = array("content" => "The IPs have been added to the pool.");
             } else {
                 return $sError = array("red" => "You must specify a valid range or IP!");
             }
         } else {
             return $sError = array("red" => "Invalid block. Please reload and try again!");
         }
     } else {
         return $sError = array("red" => "You must specify a valid range or IP!");
     }
 }