From 3ab4fa63fc2e47b45f769577da02235562b4f310 Mon Sep 17 00:00:00 2001 From: mad Date: Sat, 18 Aug 2018 13:53:25 +0200 Subject: [PATCH] move new/editvm to ajax, check macaddress not in use already and a few fixes --- web/header.php | 2 +- web/index.php | 125 +++++++++++++++++++++++++++++++------------------ web/nlvmi.js | 4 +- 3 files changed, 82 insertions(+), 49 deletions(-) diff --git a/web/header.php b/web/header.php index 60b66ab..247753d 100644 --- a/web/header.php +++ b/web/header.php @@ -27,7 +27,7 @@ - +
usermanagement
"; } ?>logout
diff --git a/web/index.php b/web/index.php index 240cd07..d45ca54 100644 --- a/web/index.php +++ b/web/index.php @@ -93,6 +93,16 @@ if ($usersfound=="no"){ } } +function checkmacfree($vmname,$mac,$db_handle){ + $sql="SELECT vmname FROM vms WHERE (macaddr1='$mac' OR macaddr2='$mac' OR macaddr3='$mac') AND NOT vmname='$vmname'"; + //echo "na"; + $res = $db_handle->query($sql); + while ($row = $res->fetchArray(SQLITE3_ASSOC)){ + return "$row[vmname] uses the mac $mac already!"; + } + return 0; +} + //delete vms if (isset($_SESSION['username'])){ if ((isset($_POST['deletevm']))&&(isset($_POST['deletename']))){ @@ -166,6 +176,72 @@ if (isset($_SESSION['username'])){ } } +//insert or update vm 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")||($rkey!="rand")){ + if ((preg_match('/macaddr/', $rkey))&&($rvalue!="")){ + $cm = checkmacfree(clean($_REQUEST['vmname']), $rvalue, $db_handle); + if ($cm != "0") { + echo $cm; + exit; + } + } + $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; + $res = $db_handle->exec($sql); + echo "success"; + exit; + +// $mode=""; + } elseif ($_REQUEST['mode']=="editvm"){ +// $mode=""; + $sql="UPDATE vms SET "; + foreach(array_keys($_REQUEST) as $rkey){ + if (($rkey!="mode")&&($rkey!="editid")&&($rkey!="rand")){ + $rvalue = clean($_REQUEST[$rkey]); + if ((preg_match('/macaddr/', $rkey))&&($rvalue!="")){ + $cm = checkmacfree(clean($_REQUEST['vmname']), $rvalue, $db_handle); + if ($cm != "0") { + echo $cm; + exit; + } + } + if ($rvalue == "x86_64"){ + $rvalue = "qemu-system-x86_64"; + } + $sql .= " $rkey='$rvalue',"; + } + } + $sql = rtrim($sql, ','); + $eid=clean($_REQUEST['editid']); + $sql .= " WHERE id='$eid'"; +// if($db_handle->exec($sql)){ + $res = $db_handle->exec($sql); + echo "success"; + //echo $res; + exit; + //} + } + } + //ajax queries are all done, time for the header include('header.php'); @@ -277,7 +353,7 @@ if (isset($_SESSION['username'])){ $placehold['vncpasswort']="the password for VNC"; echo "

$formname


"; - echo "
"; + echo ""; $res = $db_handle->query('PRAGMA table_info(vms)'); while ($col = $res->fetchArray(SQLITE3_ASSOC)) { $arrColnames[]=$col['name']; @@ -375,7 +451,7 @@ if (isset($_SESSION['username'])){ } } } - echo "$editid
"; + echo "$editidsave vm"; } @@ -383,49 +459,6 @@ if (isset($_SESSION['username'])){ -//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); - $mode=""; - } 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")&&($mode!='usermgmt')){ if ($_SESSION['isadmin']=="1"){ @@ -452,7 +485,7 @@ if (isset($_SESSION['username'])){ echo " "; $server = gethostname(); if (preg_match('/stop/', $button)) { - echo "VNC"; + echo "VNC"; } echo "
"; } diff --git a/web/nlvmi.js b/web/nlvmi.js index 6b699a8..31474b4 100644 --- a/web/nlvmi.js +++ b/web/nlvmi.js @@ -48,9 +48,9 @@ $('#confirm-delete').on('show.bs.modal', function(e) { }); function submitbutton(formid){ - //console.log(formid); + console.log(formid); data = $('#'+ formid).serialize(); - //console.log(data); + console.log(data); $.ajax({ type: 'POST', url: "?rand="+ makeid(),