' ); $replace = array('&', '"', ''', '<', '>' ); $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 = ""; while ($row = $res->fetchArray(SQLITE3_ASSOC)){ $e = $row; } } elseif(isset($_REQUEST['newvm'])) { $formname="newvm"; $editid=""; $mode="newvm"; } if (isset($formname)){ echo "

$formname


"; echo "
"; $res = $db_handle->query('PRAGMA table_info(vms)'); while ($col = $res->fetchArray(SQLITE3_ASSOC)) { $arrColnames[]=$col['name']; } for ($col=0; $col
bootoption
"; // echo "$arrColnames[$col]
"; } elseif ($arrColnames[$col] == "arch"){ echo "
arch
"; // echo "$arrColnames[$col]
"; } elseif ($arrColnames[$col] == "cdrom"){ echo "
cdrom
"; } elseif (preg_match('/format/', $arrColnames[$col])) { echo "
$arrColnames[$col]
"; } elseif (preg_match('/tapdev/', $arrColnames[$col])) { echo "
$arrColnames[$col]
"; } elseif (preg_match('/macaddr/', $arrColnames[$col])) { echo "
$arrColnames[$col]
"; } elseif (preg_match('/brdev/', $arrColnames[$col])) { echo "
$arrColnames[$col]
"; } elseif (preg_match('/drive/', $arrColnames[$col])){ echo "
$arrColnames[$col]
"; } else { echo "
$arrColnames[$col]
"; } } } echo "$editid"; } //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")&&($mode!="editvm")){ echo "
Create a new VM

"; //getting vms from DB $sql="SELECT * FROM vms"; $res = $db_handle->query($sql); echo "
"; while ($row = $res->fetchArray(SQLITE3_ASSOC)){ if (checkvm($row['vmname'])){ $button = "start"; $buttonc = "btn-success"; } else { $button = "stop"; $buttonc = "btn-warning"; } echo "
$row[vmname]
edit "; echo "$button "; echo " "; $server = gethostname(); if (preg_match('/stop/', $button)) { echo "VNC
"; } } } echo "
"; ?>