nlvmi/web/index.php

234 lines
9.6 KiB
PHP

<?php
function clean($str) {
$search = array('&' , '"' , "'" , '<' , '>' );
$replace = array('&amp;', '&quot;', '&#39;', '&lt;', '&gt;' );
$str = str_replace($search, $replace, $str);
return $str;
}
function checkvm($vmname) {
$shellout = shell_exec("/usr/bin/wrap-nlvmi checkvm $vmname bla");//){
if (preg_match_all('/running/', $shellout)){
return 0;
}
return 1;
}
//read config file
$configfile = new SplFileObject("/etc/nlvmi/nlvmi.conf");
while (!$configfile->eof()) {
$line = $configfile->fgets();
if (!preg_match('/^#/', $line)){
if (!preg_match('/^$/', $line)){
if (preg_match('/=/', $line)){
$configparam = explode('=', $line);
$p = rtrim($configparam[1]);
//echo "B$p";
$config[$configparam[0]]=str_replace('"', '', $p);
}
}
}
}
$file = null;
//check db connection
if ($config['DATABASETYPE']=="sqlite"){
$db_handle = new SQLite3($config['SQLITEFILE']);
$query = "SELECT name FROM sqlite_master WHERE type='table' AND name='vms';";
$db_handle->exec($query);
$result = $db_handle->query($query);
$row = $result->fetchArray();
if (!$row['name']=="vms"){
echo "DB connection failed!";
exit;
}
}
if ((isset($_POST['deletevm']))&&(isset($_POST['deletename']))){
$vmname = clean($_POST['deletename']);
$vmid = clean($_POST['deletevm']);
$sql="SELECT id FROM vms WHERE vmname='$vmname' AND id='$vmid'";
$res = $db_handle->query($sql);
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
$sqld="DELETE FROM vms WHERE id='$vmid'";
if ($db_handle->query($sqld)){
echo "VM $vmname deleted!";
}
}
}
include('header.php');
$mode="";
if (isset($_REQUEST['start'])){
$vmname = clean($_REQUEST['start']);
$sql="SELECT * FROM vms WHERE vmname='$vmname'";
$res = $db_handle->query($sql);
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
$shellout = shell_exec("/usr/bin/wrap-nlvmi start $vmname bla");
echo $shellout;
}
}
if (isset($_REQUEST['stop'])){
$vmname = clean($_REQUEST['stop']);
$sql="SELECT * FROM vms WHERE vmname='$vmname'";
$res = $db_handle->query($sql);
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
if(checkvm($vmname)){
$shellout = shell_exec("/usr/bin/wrap-nlvmi stop $vmname bla");
echo $shellout;
}
}
}
//form for edit and new vms
if (isset($_REQUEST['edit'])){
$mode="editvm";
$formname="editvm";
$eid=clean($_REQUEST['edit']);
$sql="SELECT * FROM vms WHERE id='$eid'";
$res = $db_handle->query($sql);
$editid = "<input type=hidden name=editid value=$eid>";
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
$e = $row;
}
} elseif(isset($_REQUEST['newvm'])) {
$formname="newvm";
$editid="";
$mode="newvm";
}
if (isset($formname)){
echo "<h2>$formname</h2></br>";
echo "<form name=$formname action=? method=post>";
$res = $db_handle->query('PRAGMA table_info(vms)');
while ($col = $res->fetchArray(SQLITE3_ASSOC)) {
$arrColnames[]=$col['name'];
}
for ($col=0; $col <count($arrColnames); $col++){
if ($arrColnames[$col]!="id"){
if (isset($e[$arrColnames[$col]])){
$val = $e[$arrColnames[$col]];
} else {
$val = "";
}
if ($arrColnames[$col] == "bootoption"){
echo "<div class=row><div class=col-md-1 style=text-align:right>bootoption</div><div class=col-md-2><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=bootoptionbutton data-toggle=dropdown aria-haspopup=true aria-expanded=false>C</button>";
echo "<div class=dropdown-menu aria-labelledby=bootoptionbutton>";
echo "<a class=dropdown-item href=\"javascript:dropdown('bootoptionbutton','c')\">c</a>";
echo "<a class=dropdown-item href=\"javascript:dropdown('bootoptionbutton','d')\">d</a>";
echo "</div></div></div></div><input type=hidden id=bootoptionbuttonh name=bootoption value=c>";
// echo "$arrColnames[$col] <select name=bootoption><option value=c>C</option><option value=d>D</option></select></br>";
} elseif ($arrColnames[$col] == "arch"){
echo "<div class=row><div class=col-md-1 style=text-align:right>arch</div><div class=col-md-2><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=archbutton data-toggle=dropdown aria-haspopup=true aria-expanded=false>x86_64</button>";
echo "<div class=dropdown-menu aria-labelledby=archbutton>";
echo "<a class=dropdown-item href=\"javascript:dropdown('archbutton','x86_64')\">x86_64</a>";
echo "</div></div></div></div><input type=hidden id=archbuttonh name=arch value=x86_64>";
// echo "$arrColnames[$col] <select name=arch><option value=qemu-system-x86_64>x86_64</option></select></br>";
} elseif ($arrColnames[$col] == "cdrom"){
echo "<div class=row><div class=col-md-1 style=text-align:right>cdrom</div><div class=col-md-2><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=cdrombutton data-toggle=dropdown aria-haspopup=true aria-expanded=false>$val</button>";
echo "<div class=dropdown-menu aria-labelledby=cdrombutton>";
$isodirs = explode('|', $config['ISODIRECTORY']);
foreach($isodirs as $isodir){
$files = array_diff(scandir($isodir), array('.', '..'));
$files = array_values(array_filter($files));
for ($f=0; $f<count($files); $f++){
if ((preg_match('/iso/', $files[$f]))||(preg_match('/ISO/', $files[$f]))){
echo "<a class=dropdown-item href=\"javascript:dropdown('cdrombutton','$isodir/$files[$f]')\">$isodir/$files[$f]</a>";
}
}
}
echo "</div></div></div></div><input type=hidden id=cdrombuttonh name=cdrom value=$val>";
} elseif (preg_match('/drive/', $arrColnames[$col])){
echo "<div class=row><div class=col-md-1 style=text-align:right>$arrColnames[$col]</div><div class=col-md-2><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=$arrColnames[$col]button data-toggle=dropdown aria-haspopup=true aria-expanded=false>$val</button>";
echo "<div class=dropdown-menu aria-labelledby=$arrColnames[$col]button>";
$vmdirs = explode('|', $config['VMDIRECTORY']);
foreach($vmdirs as $vmdir){
$files = array_diff(scandir($vmdir), array('.', '..'));
$files = array_values(array_filter($files));
for ($f=0; $f<count($files); $f++){
echo "<a class=dropdown-item href=\"javascript:dropdown('$arrColnames[$col]button','$vmdir/$files[$f]')\">$vmdir/$files[$f]</a>";
}
}
echo "</div></div></div></div><input type=hidden id=$arrColnames[$col]buttonh name=$arrColnames[$col] value=$val>";
} else {
echo "<div class=row><div class=col-md-1 style=text-align:right>$arrColnames[$col]</div><div class=col-md-2><input class=form-control type=text name=$arrColnames[$col] value=$val></div></div>";
}
}
}
echo "$editid<input type=hidden name=mode value=$formname><input class=\"btn btn-primary\" type=submit></form>";
}
//insert in db
if(isset($_REQUEST['mode'])){
if($_REQUEST['mode']=="newvm"){
$mode="newvm";
$sqls="INSERT INTO vms ";
$sqlc="(";
$sqlv=" VALUES (";
foreach(array_keys($_REQUEST) as $rkey){
if ($rkey!="mode"){
$rvalue = clean($_REQUEST[$rkey]);
if ($rvalue == "x86_64"){
$rvalue = "qemu-system-x86_64";
}
$sqlc .= "$rkey,";
$sqlv .= "'$rvalue',";
}
}
$sqlc = rtrim($sqlc, ',');
$sqlc.=")";
$sqlv = rtrim($sqlv, ',');
$sqlv.=")";
$sql = "$sqls$sqlc$sqlv";
echo $sql;
$result = $db_handle->exec($sql);
} elseif ($_REQUEST['mode']=="editvm"){
$mode="editvm";
$sql="UPDATE vms SET ";
foreach(array_keys($_REQUEST) as $rkey){
if (($rkey!="mode")&&($rkey!="editid")){
$rvalue = clean($_REQUEST[$rkey]);
if ($rvalue == "x86_64"){
$rvalue = "qemu-system-x86_64";
}
$sql .= " $rkey='$rvalue',";
}
}
$sql = rtrim($sql, ',');
$eid=clean($_REQUEST['editid']);
$sql .= " WHERE id='$eid'";
$result = $db_handle->exec($sql);
}
}
if($mode!="newvm"){
echo "<a class=\"btn btn-primary\" href=?newvm>Create a new VM</a></br>";
}
//getting vms from DB
$sql="SELECT * FROM vms";
$res = $db_handle->query($sql);
echo "<div class=row>";
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
if (checkvm($row['vmname'])){
$button = "start";
} else {
$button = "stop";
}
echo "<div class=col style=\"border:solid 1px;\">$row[vmname]</br><a class=\"btn btn-primary\" href=?edit=$row[id]>edit</a> ";
echo "<a href=?$button=$row[vmname] class=\"btn btn-primary\">$button</a> ";
echo "<button class=\"btn btn-default\" data-delete-text=\"Delete VM $row[vmname]!\" data-delete-vm=\"$row[id]\" data-delete-name=\"$row[vmname]\" data-toggle=\"modal\" data-target=\"#confirm-delete\">Delete VM</button> ";
$server = gethostname();
if ($button == "stop") {
echo "<a target=_blank href=novnc/vnc.html?path&host=$server class=\"btn btn-primary\">VNC</a></div>";
}
}
echo "</div></div></body></html>";
?>