' ); $replace = array('&', '"', ''', '<', '>' ); $str = str_replace($search, $replace, $str); return $str; } //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; } } include('header.php'); $mode=""; //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 "
bootoption
"; // echo "$arrColnames[$col]
"; } elseif ($arrColnames[$col] == "cdrom"){ echo "
cdrom
"; } else { if (isset($e[$arrColnames[$col]])){ $val = $e[$arrColnames[$col]]; } else { $val = ""; } 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]); $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]); $sql .= " $rkey='$rvalue',"; } } $sql = rtrim($sql, ','); $eid=clean($_REQUEST['editid']); $sql .= " WHERE id='$eid'"; $result = $db_handle->exec($sql); } } if($mode!="newvm"){ 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)){ echo "
$row[vmname]
edit
"; } echo "
"; ?>