Rewritten auth on remote libvirt + several interface tweaks
authorOlivier Doucet <odoucet@php.net>
Fri, 3 Aug 2012 21:36:30 +0000 (23:36 +0200)
committerMichal Novotny <minovotn@redhat.com>
Mon, 6 Aug 2012 07:32:08 +0000 (09:32 +0200)
Signed-off-by: Michal Novotny <minovotn@redhat.com>

15 files changed:
AUTHORS
README
classes/database-mysql.php
classes/libvirt.php
html/generic.css
html/php-virt-control.css
index.php
init.php
main-menu.php
manager.css
menu.php
pages/details/overview.php
pages/domain-list.php
pages/network-list.php
pages/overview.php

diff --git a/AUTHORS b/AUTHORS
index 2129cf9..101793b 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -5,6 +5,11 @@ php-virt-control tool is maintained and developed by:
 
        Michal Novotny <minovotn@redhat.com> (or <mignov@gmail.com>)
 
+
+Some changes were made by:
+
+       Olivier Doucet <odoucet@php.net>
+
 The project is using libvirt-php extension for PHP to access the
 libvirt virtualization toolkit directly from the PHP scripts.
 
diff --git a/README b/README
index c467e45..27ba198 100644 (file)
--- a/README
+++ b/README
@@ -5,3 +5,9 @@ The php-virt-control tool is the virtual machine control tool for PHP.
 It depends on libvirt PHP bindings as the module for PHP. You can use
 the tool to manage your virtual guests you're having available on your
 libvirt connection(s).
+
+
+LICENSES
+--------
+Icons used are from http://www.icojoy.com/articles/49/ (license free)
+
index 069c5d1..21d4b69 100644 (file)
@@ -73,6 +73,7 @@
                                        'type tinyint(4) NOT NULL,'.
                                        'method varchar(3) NOT NULL,'.
                                        'require_pwd tinyint(4) NOT NULL,'.
+                                       'password varchar(255) NULL,'.
                                        'user varchar(255) NOT NULL,'.
                                        'host varchar(255) NOT NULL,'.
                                        'logfile varchar(255) NOT NULL,'.
                }
 
                /* Add/edit/remove functions */
-               function add_connection($name, $hv, $type, $method, $require_pwd, $user, $host, $logfile) {
-                       if ($require_pwd)
+               function add_connection($name, $hv, $type, $method, $pwd, $user, $host, $logfile) {
+                       if (strlen($pwd) > 0)
                                $require_pwd = 1;
                        else
                                $require_pwd = 0;
 
-                       $qry = 'INSERT INTO '.$this->prefix.$this->tab_connections.'(name, hv, type, method, require_pwd, user, host, logfile) '.
-                               "VALUES('$name', '$hv', '$type', '$method', $require_pwd, '$user', '$host', '$logfile')";
+                       $qry = 'INSERT INTO '.$this->prefix.$this->tab_connections.'(name, hv, type, method, require_pwd, password, user, host, logfile) '.
+                               "VALUES('$name', '$hv', '$type', '$method', $require_pwd, '$pwd', '$user', '$host', '$logfile')";
 
                        if (!mysql_query($qry))
                                return false;
index 31ae525..d8bf892 100644 (file)
@@ -7,12 +7,12 @@
                private $lang_str;
                private $enabled = false;
 
-               function Libvirt($uri = false, $debug=false, $lang=false) {
+               function Libvirt($uri = false, $login = false, $pwd = false, $debug=false, $lang=false) {
                        if ($debug)
                                $this->set_logfile($debug);
                        if ($uri != false) {
                                $this->enabled = true;
-                               $this->connect($uri);
+                               $this->connect($uri, $login, $pwd);
                        }
                        $this->lang_str = $lang;
                }
                                return 'qemu+'.$remote_method.'://'.$remote_username.'@'.$remote_hostname.'/'.$append_type;
                }
 
-               function test_connection_uri($hv, $rh, $rm, $un, $rp, $hn, $session=false) {
+               function test_connection_uri($hv, $rh, $rm, $un, $pwd, $hn, $session=false) {
                        $uri = $this->generate_connection_uri($hv, $rh, $rm, $un, $hn, $session);
-                       if ($rp) {
-                               $credentials = array(VIR_CRED_AUTHNAME => $un, VIR_CRED_PASSPHRASE => $rp);
+                       if (strlen($pwd) > 0) {
+                               $credentials = array(VIR_CRED_AUTHNAME => $un, VIR_CRED_PASSPHRASE => $pwd);
                                $test = libvirt_connect($uri, false, $credentials);
                        }
                        else
                        return libvirt_print_binding_resources();
                }
 
-               function connect($uri = 'null') {
-                       $this->conn=libvirt_connect($uri, false);
+               function connect($uri = 'null', $login = false, $password = false) {
+                       if ($login !== false && $password !== false) {
+                               $this->conn=libvirt_connect($uri, false, array(VIR_CRED_AUTHNAME => $login, VIR_CRED_PASSPHRASE => $password));
+                       } else {
+                               $this->conn=libvirt_connect($uri, false);
+                       }
                        if ($this->conn==false)
                                return $this->_set_last_error();
                }
index dbf7b56..51a6dbf 100644 (file)
@@ -3,9 +3,14 @@ body {
   margin: 0em;
   padding: 0px;
   color: rgb(0,0,0);
-  font-family: Verdana, Arial, Helvetica, sans-serif;
-  font-size: 80%;
+  font-family: Arial, Helvetica, sans-serif;
   background: #ffffff;
+  font-size: 11px;
+}
+
+div, td, p {
+  font-family: Arial, Helvetica, sans-serif;
+  font-size: 11px;
 }
 
 p, ul, ol, dl {
index 704b4ca..3963e88 100644 (file)
@@ -33,15 +33,16 @@ img {
 }
 
 #content {
-    margin-left: 230px;
+    margin-left: 1em;
     margin-right: 1em;
     padding: 0px;
     padding-bottom: 1em;
+    float: left;
 }
 
 #menu {
     float: left;
-    width: 220px;
+    width: 200px;
     margin-bottom: 1em;
 }
 
index d14f8e0..59cb640 100644 (file)
--- a/index.php
+++ b/index.php
        $errmsg = false;
        if (!CONNECT_WITH_NULL_STRING && $uri == 'null')
                $uri = false;
+       if (isset($_SESSION['connection_credentials']))
+               $lv = new Libvirt(
+                       $uri, 
+                       $_SESSION['connection_credentials'][VIR_CRED_AUTHNAME], 
+                       $_SESSION['connection_credentials'][VIR_CRED_PASSPHRASE], 
+                       $lg, 
+                       $lang_str
+               );
 
-       $lv = new Libvirt($uri, $lg, $lang_str);
+       else
+               $lv = new Libvirt($uri, null, null, $lg, $lang_str);
 
        /* Get new MAC address in plain text - called by Ajax from pages/new-vm.php */
        if (array_key_exists('get_mac', $_GET)) {
                die( $lv->generate_random_mac_addr() );
        }
-
        if (!$lv->enabled() || ($lv->get_last_error())) {
                $page = 'overview';
                $name = false;
-               $errmsg = $lang->get('cannot_connect');
+               $errmsg = $lang->get('cannot_connect').' '.$lv->get_last_error();
        }
        else {
                $name = array_key_exists('name', $_GET) ? $_GET['name'] : false;
@@ -34,7 +42,8 @@
 <html>
 <head>
  <title>php-virt-control - <?php echo $lang->get('title_vmc') ?></title>
- <link rel="STYLESHEET" type="text/css" href="manager.css"> 
+ <link rel="STYLESHEET" type="text/css" href="html/main.css" />
+ <link rel="STYLESHEET" type="text/css" href="manager.css" /> 
 </head>
 <body>
   <div id="header">
index 2c37c58..0a2ac95 100644 (file)
--- a/init.php
+++ b/init.php
@@ -86,7 +86,7 @@
        if (!include('/etc/php-virt-control/connection.php'))
                $cstr = 'mysql:config/mysql-connection.php';
 
-       $cstr = $type.':/etc/php-virt-control/'.$config;
+       else $cstr = $type.':/etc/php-virt-control/'.$config;
 
        $db = getDBObject($cstr);
        $db->init();
index af7fa18..da15f5d 100644 (file)
@@ -7,4 +7,7 @@
     | <a href="?page=settings"><?php echo $lang->get('settings') ?></a>
     | <a href="?page=info"><?php echo $lang->get('info') ?></a>
     | <a href="?action=logout"><?php echo $lang->get('logout') ?></a>
+    <div style="float:right;text-align: right; width:220px;font-size:11px;font-style:italic">
+       <?php if (isset($uri)) echo "Connected to <br />".$uri; ?>
+    </div>
   </div>
index 98c69a8..4d2506a 100644 (file)
        background: url(graphics/php-virt-control.png);
 }
 
+table {
+       border-collapse:collapse;
+}
+table tr td {
+       padding-top: 5px;
+        padding-right: 5px;
+}
+
+.odd  {
+       background-color: #cccccc;
+}
+.even  {
+
+}
+
 #vm-name {
        padding-left: 10px;
 }
@@ -33,7 +48,7 @@
 }
 
 #menu a {
-       font-size: 20px;
+       font-size: 16px;
        padding: 10px;
        color: blue;
        text-decoration: none;
@@ -47,7 +62,7 @@
 }
 
 #main-menu a {
-        font-size: 20px;
+        font-size: 16px;
         padding: 10px;
         color: blue;
         text-decoration: none;
index f303ab2..5f33666 100644 (file)
--- a/menu.php
+++ b/menu.php
@@ -1,18 +1,18 @@
   <!-- MENU -->
   <div id="menu">
   <a href="?name=<?php echo $name ?>"><?php echo $lang->get('menu_overview') ?></a>
-  <!--
-  | <a href="?name=<?php echo $name ?>&amp;page=performance">Performance</a>
+  <!-- 
+  <br /> <a href="?name=<?php echo $name ?>&amp;page=performance">Performance</a>
   -->
-  | <a href="?name=<?php echo $name ?>&amp;page=processor"><?php echo $lang->get('menu_processor') ?></a>
-  | <a href="?name=<?php echo $name ?>&amp;page=memory"><?php echo $lang->get('menu_memory') ?></a>
-  | <a href="?name=<?php echo $name ?>&amp;page=boot-options"><?php echo $lang->get('menu_boot') ?></a>
-  | <a href="?name=<?php echo $name ?>&amp;page=disk-devices"><?php echo $lang->get('menu_disk') ?></a>
-  | <a href="?name=<?php echo $name ?>&amp;page=network-devices"><?php echo $lang->get('menu_network') ?></a>
-  | <a href="?name=<?php echo $name ?>&amp;page=multimedia-devices"><?php echo $lang->get('menu_multimedia') ?></a>
-  | <a href="?name=<?php echo $name ?>&amp;page=host-devices"><?php echo $lang->get('menu_hostdev') ?></a>
+  <br /> <a href="?name=<?php echo $name ?>&amp;page=processor"><?php echo $lang->get('menu_processor') ?></a>
+  <br /> <a href="?name=<?php echo $name ?>&amp;page=memory"><?php echo $lang->get('menu_memory') ?></a>
+  <br /> <a href="?name=<?php echo $name ?>&amp;page=boot-options"><?php echo $lang->get('menu_boot') ?></a>
+  <br /> <a href="?name=<?php echo $name ?>&amp;page=disk-devices"><?php echo $lang->get('menu_disk') ?></a>
+  <br /> <a href="?name=<?php echo $name ?>&amp;page=network-devices"><?php echo $lang->get('menu_network') ?></a>
+  <br /> <a href="?name=<?php echo $name ?>&amp;page=multimedia-devices"><?php echo $lang->get('menu_multimedia') ?></a>
+  <br /> <a href="?name=<?php echo $name ?>&amp;page=host-devices"><?php echo $lang->get('menu_hostdev') ?></a>
 <?php
   if (($lv->domain_is_running($res, $name) && ($lv->supports('screenshot'))))
-    echo '| <a href="?name='.$name.'&amp;page=screenshot">'.$lang->get('menu_screenshot').'</a>';
+    echo '<br /> <a href="?name='.$name.'&amp;page=screenshot">'.$lang->get('menu_screenshot').'</a>';
 ?>
   </div>
index a221d51..4d91d51 100644 (file)
@@ -68,7 +68,7 @@
     <div class="item">
       <div class="label"><?php echo $lang->get('description') ?>:</div>
       <div class="value">
-        <textarea rows="10" cols="60" name="description"><?php echo $desc ?></textarea>
+        <textarea rows="5" cols="60" name="description"><?php echo $desc ?></textarea>
       </div>
       <div class="nl" />
     </div>
index 5ffba2d..c62e7c7 100644 (file)
@@ -1,6 +1,5 @@
 <?php
   $action = array_key_exists('action', $_GET) ? $_GET['action'] : false;
-
   $msg = '';
   $frm = '';
   if ($action == 'domain-start') {
 
 <div class="section"><?php echo $lang->get('domain_list') ?></div>
 
-<table id="domain-list">
 <?php
-       if (verify_user($db, USER_PERMISSION_VM_CREATE)):
+        if (verify_user($db, USER_PERMISSION_VM_CREATE)):
 ?>
-  <tr>
-    <td colspan="2" align="left">
-      <a href="?page=new-vm"><?php echo $lang->get('create-new-vm') ?></a>
-    </td>
-  </tr>
 <?php
-       endif;
+        endif;
 ?>
+
+<div style="padding: 10px; font-size: 14px; font-weight: bold; width: 100%; border: 1px solid grey;margin-bottom: 10px;">
+<a href="?page=new-vm"><?php echo $lang->get('create-new-vm') ?></a>
+</div>
+
+<table id="domain-list">
   <tr>
     <th><?php echo $lang->get('name') ?></th>
     <th><?php echo $lang->get('arch') ?></th>
 
                                $running = $lv->domain_is_running($res, $name);
                                if (!$running) {
-                                       $actions  = '<a href="?page='.$page.'&amp;action=domain-start&amp;dom='.$name.'">'.$lang->get('dom_start').'</a> | ';
-                                       $actions .= '<a href="?page='.$page.'&amp;action=domain-dump&amp;dom='.$name.'">'.$lang->get('dom_dumpxml').'</a> | ';
+                                       $actions  = '<a href="?page='.$page.'&amp;action=domain-start&amp;dom='.$name.'"><img src="graphics/play.png" title="'.$lang->get('dom_start').'" /></a> ';
+                                       $actions .= '<a href="?page='.$page.'&amp;action=domain-dump&amp;dom='.$name.'"><img src="graphics/dump.png" title="'.$lang->get('dom_dumpxml').'" /></a> ';
                                        if (verify_user($db, USER_PERMISSION_VM_EDIT))
-                                               $actions .= '<a href="?page='.$page.'&amp;action=domain-edit&amp;dom='.$name.'">'.$lang->get('dom_editxml').'</a> | ';
+                                               $actions .= '<a href="?page='.$page.'&amp;action=domain-edit&amp;dom='.$name.'"><img src="graphics/edit.png" title="'.$lang->get('dom_editxml').'" /></a> ';
                                        if (verify_user($db, USER_PERMISSION_VM_DELETE))
-                                               $actions .= '<a href="?page='.$page.'&amp;action=domain-undefine&amp;dom='.$name.'">'.$lang->get('dom_undefine').'</a> | ';
+                                               $actions .= '<a href="?page='.$page.'&amp;action=domain-undefine&amp;dom='.$name.'"><img src="graphics/undefine.png" title="'.$lang->get('dom_undefine').'" /></a> ';
 
                                        $actions[ strlen($actions) - 2 ] = ' ';
                                        $actions = Trim($actions);
                                }
                                else {
-                                       $actions  = '<a href="?page='.$page.'&amp;action=domain-stop&amp;dom='.$name.'">'.$lang->get('dom_stop').'</a> | ';
-                                       $actions .= '<a href="?page='.$page.'&amp;action=domain-destroy&amp;dom='.$name.'">'.$lang->get('dom_destroy').'</a> | ';
-                                       $actions .= '<a href="?page='.$page.'&amp;action=domain-dump&amp;dom='.$name.'">'.$lang->get('dom_dumpxml').'</a> | ';
+                                       $actions  = '<a href="?page='.$page.'&amp;action=domain-stop&amp;dom='.$name.'"><img src="graphics/stop.png" title="'.$lang->get('dom_stop').'" /></a> ';
+                                       $actions .= '<a href="?page='.$page.'&amp;action=domain-destroy&amp;dom='.$name.'"><img src="graphics/destroy.png" title="'.$lang->get('dom_destroy').'" /></a> ';
+                                       $actions .= '<a href="?page='.$page.'&amp;action=domain-dump&amp;dom='.$name.'"><img src="graphics/dump.png" title="'.$lang->get('dom_dumpxml').'" /></a> ';
 
                                        if ($lv->supports('screenshot'))
-                                               $actions .= '<a href="?name='.$name.'&amp;page=screenshot">'.$lang->get('dom_screenshot').'</a> | ';
+                                               $actions .= '<a href="?name='.$name.'&amp;page=screenshot"><img src="graphics/screenshot.png" title="'.$lang->get('dom_screenshot').'" /></a>';
 
                                        $actions[ strlen($actions) - 2 ] = ' ';
                                        $actions = Trim($actions);
                                }
 
-                               echo "<tr>
-                                               <td class=\"name\">
+                               echo '<tr class="';
+                               if (($i % 2) == 0) echo 'odd'; 
+                               else echo 'even';
+                               echo '">';
+                               echo   "<td class=\"name\">
                                        ";
 
                                if (verify_user($db, USER_PERMISSION_VM_EDIT))
index 39dafec..52afa0a 100644 (file)
 
                                $actions = '';
                                if (!$active) {
-                                       $actions .= '<a href="?page='.$page.'&amp;action=net-start&amp;net='.$name.'">'.$lang->get('net_start').'</a> | ';
-                                       $actions .= '<a href="?page='.$page.'&amp;action=net-dumpxml&amp;net='.$name.'">'.$lang->get('net_dumpxml').'</a> | ';
+                                       $actions .= '<a href="?page='.$page.'&amp;action=net-start&amp;net='.$name.'"><img src="graphics/play.png" title="'.$lang->get('net_start').'" /></a> ';
+                                       $actions .= '<a href="?page='.$page.'&amp;action=net-dumpxml&amp;net='.$name.'"><img src="graphics/dump.png" title="'.$lang->get('net_dumpxml').'" /></a> ';
                                        if (verify_user($db, USER_PERMISSION_NETWORK_EDIT))
-                                               $actions .= '<a href="?page='.$page.'&amp;action=net-editxml&amp;net='.$name.'">'.$lang->get('net_editxml').'</a> | ';
+                                               $actions .= '<a href="?page='.$page.'&amp;action=net-editxml&amp;net='.$name.'"><img src="graphics/edit.png" title="'.$lang->get('net_editxml').'" /></a> ';
                                        if (verify_user($db, USER_PERMISSION_NETWORK_DELETE))
-                                               $actions .= '<a href="?page='.$page.'&amp;action=net-undefine&amp;net='.$name.'">'.$lang->get('net_undefine').'</a> | ';
+                                               $actions .= '<a href="?page='.$page.'&amp;action=net-undefine&amp;net='.$name.'"><img src="graphics/undefine.png" title="'.$lang->get('net_undefine').'" /></a> ';
 
                                        $actions[ strlen($actions) - 2 ] = ' ';
                                        $actions = Trim($actions);
                                }
                                else {
-                                       $actions  = '<a href="?page='.$page.'&amp;action=net-stop&amp;net='.$name.'">'.$lang->get('net_stop').'</a> | ';
-                                       $actions .= '<a href="?page='.$page.'&amp;action=net-dumpxml&amp;net='.$name.'">'.$lang->get('net_dumpxml').'</a> | ';
+                                       $actions  = '<a href="?page='.$page.'&amp;action=net-stop&amp;net='.$name.'"><img src="graphics/stop.png" title="'.$lang->get('net_stop').'" /></a> ';
+                                       $actions .= '<a href="?page='.$page.'&amp;action=net-dumpxml&amp;net='.$name.'"><img src="graphics/dump.png title="'.$lang->get('net_dumpxml').'" /></a> ';
 
                                        $actions[ strlen($actions) - 2 ] = ' ';
                                        $actions = Trim($actions);
                                }
 
-                               echo "<tr>
+                               echo "<tr class=";
+                               if(($i % 2) == 0) echo "odd"; else echo "even";
+                               echo ">
                                        <td class=\"name\">
                                            $name
                                        </td>
index 208f04b..42e8cbc 100644 (file)
@@ -84,6 +84,7 @@
                                $rm = $tmp[$i]['method'];
                                $rp = $tmp[$i]['require_pwd'];
                                $un = $tmp[$i]['user'];
+                               $pwd= $tmp[$i]['password'];
                                $hn = $tmp[$i]['host'];
                                $lg = $tmp[$i]['logfile'];
                        }
 
        $skip_rest = false;
        if ($hv) {
-               if ($lv->test_connection_uri($hv, $rh, $rm, $un, $rp, $hn)) {
+               if ($lv->test_connection_uri($hv, $rh, $rm, $un, $pwd, $hn)) {
                        $uri = $lv->generate_connection_uri($hv, $rh, $rm, $un, $hn);
                        $_SESSION['connection_uri'] = $uri;
                        $_SESSION['connection_logging'] = $lg;
+                       if (isset($un) && isset($pwd)) 
+                               $_SESSION['connection_credentials'] = array(
+                                       VIR_CRED_AUTHNAME => $un, 
+                                       VIR_CRED_PASSPHRASE => $pwd
+                               );
                        echo '<p>'.$lang->get('changed_uri').' <b>'.$uri.'</b></p>';
 
                        if ((array_key_exists('lvcname', $_POST)) && ($_POST['lvcname']))
-                               if ($db->add_connection($_POST['lvcname'], $hv, $rh, $rm, $rp, $un, $hn, $lg))
+                               if ($db->add_connection($_POST['lvcname'], $hv, $rh, $rm, $pwd, $un, $hn, $lg))
                                        echo '<p>'.$lang->get('conn_saved').'</p>';
 
                        echo '<a href="?">'.$lang->get('click_reload').'</a>';