Implement MySQL db to store connections
authorMichal Novotny <minovotn@redhat.com>
Wed, 10 Aug 2011 13:57:26 +0000 (15:57 +0200)
committerMichal Novotny <minovotn@redhat.com>
Wed, 10 Aug 2011 13:57:26 +0000 (15:57 +0200)
Signed-off-by: Michal Novotny <minovotn@redhat.com>

16 files changed:
classes/database-file.php
classes/database-mysql.php [new file with mode: 0644]
classes/database.php
classes/language.php
classes/libvirt.php
data/mysql_conn.php [new file with mode: 0644]
error-connection-db.php [new file with mode: 0644]
functions.php
index.php
init.php
lang/cs.php
lang/en.php
main-menu.php
pages/new-net.php
pages/new-vm.php
pages/overview.php

index f6674c7..c690466 100644 (file)
@@ -39,6 +39,7 @@
 
                /* Parse function */
                function parse_data() {
+                       $id = 0;
                        rewind($this->fp);
 
                        $in_tab = false;
@@ -69,7 +70,9 @@
                                if ($in_tab) {
                                        if ($tab_name == 'connections') {
                                                $tmp = explode(',', $s);
+                                               $id++;
                                                $e = array(
+                                                               'id'            => $id,
                                                                'name'          => Trim($tmp[0]),
                                                                'hypervisor'    => Trim($tmp[1]),
                                                                'remote'        => Trim($tmp[2]),
diff --git a/classes/database-mysql.php b/classes/database-mysql.php
new file mode 100644 (file)
index 0000000..186822c
--- /dev/null
@@ -0,0 +1,136 @@
+<?php
+       /*
+               Database table structures:
+
+               CREATE TABLE IF NOT EXISTS `connections` (
+                 `id` int(11) NOT NULL AUTO_INCREMENT,
+                 `name` varchar(255) NOT NULL,
+                 `hv` varchar(5) NOT NULL,
+                 `type` tinyint(4) NOT NULL,
+                 `method` varchar(3) NOT NULL,
+                 `require_pwd` tinyint(4) NOT NULL,
+                 `user` varchar(255) NOT NULL,
+                 `host` varchar(255) NOT NULL,
+                 `logfile` varchar(255) NOT NULL,
+                 PRIMARY KEY (`id`)
+               ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Table of connection for php-virt-control';
+
+       */
+       class DatabaseMySQL extends Database {
+               private $server;
+               private $user;
+               private $password;
+               private $dbname;
+               private $prefix;
+               private $tab_connections = 'connections';
+               private $connections = array();
+               private $db;
+
+               function DatabaseMySQL($data) {
+                       if (!File_Exists($data))
+                               return $this->set_db_fatal('no-datafile');
+
+                       $server   = false;
+                       $user     = false;
+                       $password = false;
+                       $dbname   = false;
+                       $prefix   = false;
+
+                       include($data);
+
+                       if (!$server)
+                               return $this->set_db_fatal('no-server');
+                       if (!$user)
+                               return $this->set_db_fatal('no-user');
+                       if (!$password)
+                               return $this->set_db_fatal('no-password');
+                       if (!$dbname)
+                               return $this->set_db_fatal('no-dbname');
+                       if (!$prefix)
+                               $prefix = '';
+
+                       $this->server = $server;
+                       $this->user = $user;
+                       $this->password = $password;
+                       $this->dbname = $dbname;
+                       $this->prefix = $prefix;
+
+                       $this->connect();
+               }
+
+               function connect() {
+                       $this->db = mysql_connect($this->server, $this->user, $this->password);
+                       if (!$this->db)
+                               return $this->set_db_fatal('db-failure-connect');
+
+                       if (!mysql_select_db($this->dbname, $this->db))
+                               return $this->set_db_fatal('db-failure-select');
+                       
+
+                       return true;
+               }
+
+               function close() {
+                       mysql_close($this->db);
+                       return true;
+               }
+
+               function refresh() {
+                       $res = mysql_query('SELECT * FROM '.$this->prefix.$this->tab_connections);
+                       if (!$res)
+                               return false;
+
+                       $this->connections = array();
+                       while ($rec = mysql_fetch_assoc($res)) {
+                               $rec['hypervisor'] = $rec['hv'];
+                               $rec['remote'] = ($rec['type'] == 1) ? 1 : 0;
+                       
+                               $this->connections[] = $rec;
+                       }
+
+                       return true;
+               }
+
+               /* Listing functions */
+               function list_connections($refresh=false) {
+                       if ($refresh)
+                               $this->refresh();
+
+                       return $this->connections;
+               }
+
+               /* Add/edit/remove functions */
+               function add_connection($name, $hv, $type, $method, $require_pwd, $user, $host, $logfile) {
+                       if ($require_pwd)
+                               $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')";
+
+                       if (!mysql_query($qry))
+                               return false;
+
+                       return mysql_insert_id();
+               }
+
+               function edit_connection($id, $name, $hv, $type, $method, $require_pwd, $user, $host, $logfile) {
+                       if ($require_pwd)
+                               $require_pwd = 1;
+                       else
+                               $require_pwd = 0;
+
+                       $qry = 'UPDATE '.$this->prefix.$this->tab_connections." SET name = '$name', hv = '$hv', type = '$type', method = '$method', ".
+                               "require_pwd = $require_pwd, user = '$user', host = '$host', logfile = '$logfile' WHERE id = $id";
+
+                       return mysql_query($qry) ? true : false;
+               }
+
+               function remove_connection($id) {
+                       $qry = 'DELETE FROM '.$this->prefix.$this->tab_connections.' WHERE id = '.$id;
+
+                       return mysql_query($qry) ? true : false;
+               }
+       }
+?>
index 0d68e9d..e1b7b83 100644 (file)
@@ -2,11 +2,30 @@
        class Database {
                var $unimpl = 'Function is not implemented';
                var $log = array();
+               private $fatal = false;
 
                function Database($type) {
                        $this->connect();
                }
 
+               function set_db_fatal($func) {
+                       $this->fatal = true;
+                       $this->err('connect', $func);
+
+                       return false;
+               }
+
+               function has_fatal_error() {
+                       return $this->fatal;
+               }
+
+               function get_fatal_error() {
+                       if (!$this->fatal)
+                               return false;
+
+                       return $this->log[0]['msg'];
+               }
+
                function err($func, $msg) {
                        $this->log[] = array('func' => $func, 'msg' => $msg);
 
index e11127d..073d7b9 100644 (file)
@@ -15,7 +15,7 @@
                }
 
                function get($ident) {
-                       return array_key_exists($ident, $this->trans) ? $this->trans[$ident] : false;
+                       return array_key_exists($ident, $this->trans) ? $this->trans[$ident] : $ident;
                }
 
                function initlang() {
                                        'Yes' => 'Yes',
                                        'No' => 'No',
                                        'error_page_title' => 'Error',
+                                       'error_connection_db_label' => 'DB Error',
+                                       'error_connection_db_text' => 'The connection to your database has failed. Please check your configuration (please see init.php script and alter settings).',
+                                       'no-datafile' => 'Data file is missing.',
+                                       'no-server' => 'Server definition is missing in the data file.',
+                                       'no-user' => 'User definition is missing in the data file.',
+                                       'no-password' => 'Password definition is missing in the data file.',
+                                       'no-dbname' => 'Database name definition is missing in the data file.',
+                                       'db-failure-connect' => 'Cannot connect to database. Please check your configuration.',
+                                       'db-failure-select' => 'Cannot select database name. Please check your configuration.',
                                        'error_page_label' => 'Page not found:',
                                        'error_page_text' => 'The page you were looking for was not found. Please report to the administrator and try again later.',
                                        'error_missing_label' => 'Missing dependency:',
index 32c1058..adb8f00 100644 (file)
                                return 'qemu+'.$remote_method.'://'.$remote_username.'@'.$remote_hostname.'/'.$append_type;
                }
 
+               function test_connection_uri($hv, $rh, $rm, $un, $rp, $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);
+                               $test = libvirt_connect($uri, false, $credentials);
+                       }
+                       else
+                               $test = libvirt_connect($uri);
+                       $ok = is_resource($test);
+                       unset($test);
+
+                       return $ok;
+               }
+
                function print_resources() {
                        return libvirt_print_binding_resources();
                }
diff --git a/data/mysql_conn.php b/data/mysql_conn.php
new file mode 100644 (file)
index 0000000..1a957d9
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+       $server = 'localhost';
+       $user = 'root';
+       $password = 'password';
+       $dbname = 'php-virt-control';
+       $prefix = '';
+?>
diff --git a/error-connection-db.php b/error-connection-db.php
new file mode 100644 (file)
index 0000000..1f0a81c
--- /dev/null
@@ -0,0 +1,26 @@
+
+<html> 
+<head> 
+ <title>php-virt-control - <?= $lang->get('title_vmc') ?></title> 
+ <link rel="STYLESHEET" type="text/css" href="manager.css"> 
+</head> 
+<body> 
+  <div id="header"> 
+    <div id="headerLogo"></div> 
+  </div> 
+  <!-- CONTENTS --> 
+  <div id="content"> 
+
+    <div class="section"><?= $lang->get('error_page_title') ?></div>
+    <div class="item">
+      <div class="label"><?= $lang->get('error_connection_db_label') ?></div>
+      <div class="value"><?= $lang->get('error_connection_db_text').' '.$lang->get($db->get_fatal_error()) ?></div>
+      <div class="nl" />
+    </div>
+    </form> 
+  </div> 
+</body> 
+</html> 
index 2216c55..1adff79 100644 (file)
@@ -2,8 +2,14 @@
        function getDBObject($uri) {
                $tmp = explode(':', $uri);
 
-               if (Trim($tmp[0]) == 'file')
-                       return new DatabaseFile( Trim($tmp[1]) );
+               $proto = Trim($tmp[0]);
+               $pdata = Trim($tmp[1]);
+
+               if ($proto == 'file')
+                       return new DatabaseFile( $pdata );
+               else
+               if ($proto == 'mysql')
+                       return new DatabaseMySQL( $pdata );
 
                return false;
        }
index 99e4928..f42132c 100644 (file)
--- a/index.php
+++ b/index.php
                include('error.php');
        endif;
 ?>
+
+<?
+       if (DEBUG) {
+               echo '<div id="content">';
+               echo '<div class="section">Debug - Libvirt-php resources</div>';
+
+               $resources = $lv->print_resources();
+               for ($i = 0; $i < sizeof($resources); $i++) {
+                       echo '<div class="item">';
+                       echo '        <div class="label">Resource #'.($i + 1).'</div>';
+                       echo '        <div class="value">'.$resources[$i].'</div>';
+                       echo '        <div class="nl">';
+                       echo '</div>';
+               }
+
+               echo '</div>';
+       }
+?>
+
 </body>
 </html>
index b179d93..97fefb1 100644 (file)
--- a/init.php
+++ b/init.php
@@ -1,4 +1,5 @@
 <?php
+       define('DEBUG', true);
        define('LOGDIR', getcwd().'/logs');
        define('LIBVIRT_PHP_REQ_VERSION', '0.4.3');
        define('PHPVIRTCONTROL_VERSION', '0.0.2');
@@ -35,6 +36,7 @@
        require('classes/language.php');
        require('classes/database.php');
        require('classes/database-file.php');
+       require('classes/database-mysql.php');
 
        $lang = new Language($lang_str);
 
                exit;
        }
 
-       $db = getDBObject('file:data/test.dat');
+       //$db = getDBObject('file:data/test.dat');
+       $db = getDBObject('mysql:data/mysql_conn.php');
+       if ($db->has_fatal_error()) {
+               include('error-connection-db.php');
+               exit;
+       }
 ?>
index 9c4ee7e..7f99f09 100644 (file)
                                        'Yes' => 'Ano',
                                        'No' => 'Ne',
                                        'error_page_title' => 'Chyba',
+                                       'error_connection_db_label' => 'Chyba databáze',
+                                       'error_connection_db_text' => 'Připojení k Vaší databázi se nezdařilo. Prosím zkontrolujte Vaši konfiguraci (změňte nastavení v souboru init.php).',
+                                       'no-datafile' => 'Datový soubor neexistuje.',
+                                       'no-server' => 'Definice serveru v datovém souboru neexistuje.',
+                                       'no-user' => 'Definice uživatele v datovém souboru neexistuje.',
+                                       'no-password' => 'Definice hesla v datovém souboru neexistuje.',
+                                       'no-dbname' => 'Definice jména databáze v datovém souboru neexistuje.',
+                                       'db-failure-connect' => 'Nezdařilo se připojit k databázi. Prosím zkontrolujte konfiguraci.',
+                                       'db-failure-select' => 'Nezdařilo se přepnout databázi. Prosím zkontrolujte konfiguraci.',
                                        'error_page_label' => 'Stránka nenalezena:',
                                        'error_page_text' => 'Stránka, kterou hledáte, nebyla nalezena. Prosím reportujte tuto chybu správci a zkuste znovu později.',
                                        'error_missing_label' => 'Chybějící závislost:',
index a340550..bb7e0da 100644 (file)
                                        'Yes' => 'Yes',
                                        'No' => 'No',
                                        'error_page_title' => 'Error',
+                                       'error_connection_db_label' => 'DB Error',
+                                       'error_connection_db_text' => 'The connection to your database has failed. Please check your configuration (please see init.php script and alter settings).',
+                                       'no-datafile' => 'Data file is missing.t',
+                                       'no-server' => 'Server definition is missing in the data file.',
+                                       'no-user' => 'User definition is missing in the data file.',
+                                       'no-password' => 'Password definition is missing in the data file.',
+                                       'no-dbname' => 'Database name definition is missing in the data file.',
+                                       'db-failure-connect' => 'Cannot connect to database. Please check your configuration.',
+                                       'db-failure-select' => 'Cannot select database name. Please check your configuration.',
                                        'error_page_label' => 'Page not found:',
                                        'error_page_text' => 'The page you were looking for was not found. Please report to the administrator and try again later.',
                                        'error_missing_label' => 'Missing dependency:',
index 61a681f..2a9309d 100644 (file)
@@ -3,6 +3,6 @@
     <a href="?"><?= $lang->get('main_menu') ?></a>
     | <a href="?page=domain-list"><?= $lang->get('domain_list') ?></a>
     | <a href="?page=network-list"><?= $lang->get('network_list') ?></a>
-    | <a href="?page=info"><?= $lang->get('info') ?></a>
     | <a href="?page=settings"><?= $lang->get('settings') ?></a>
+    | <a href="?page=info"><?= $lang->get('info') ?></a>
   </div>
index 3bf5acd..4502c18 100644 (file)
 
 <input type="hidden" name="sent" value="1" />
 </form>
+</table>
 
 <?php
   endif;
index 7225dcf..67a8b89 100644 (file)
     <input type="submit" value=" <?= $lang->get('create-vm') ?> " />
     </td>
 </tr>
-
+</table>
 <input type="hidden" name="sent" value="1" />
 </form>
 
index c92938d..ba8e595 100644 (file)
@@ -37,6 +37,7 @@
                $user = $tmp[$i]['user'];
                $host = $tmp[$i]['host'];
                $logfile = $tmp[$i]['logfile'];
+               $id = $tmp[$i]['id'];
 
                echo '<tr align="center">
                        <td>'.$name.'</td>
@@ -45,8 +46,8 @@
                        <td>'.($host ? $host : '-').'</td>
                         <td>'.($logfile ? $logfile : '-').'</td>
                        <td>
-                               <a href="?connect='.($i + 1).'">'.$lang->get('connect').'</a> |
-                               <a href="?remove_conn='.($i + 1).'">'.$lang->get('conn_remove').'</a>
+                               <a href="?connect='.$id.'">'.$lang->get('connect').'</a> |
+                               <a href="?remove_conn='.$id.'">'.$lang->get('conn_remove').'</a>
                        </td>
                       </tr>';
        }
 
        if (array_key_exists('connect', $_GET)) {
                $tmp = $db->list_connections();
-               $i = (int)$_GET['connect'] - 1;
-
-                $hv = $tmp[$i]['hypervisor'];
-                $rh = $tmp[$i]['remote'];
-                $rm = $tmp[$i]['method'];
-               $rp = $tmp[$i]['require_pwd'];
-                $un = $tmp[$i]['user'];
-                $hn = $tmp[$i]['host'];
-                $lg = $tmp[$i]['logfile'];
+               $rid = (int)$_GET['connect'];
+
+               for ($i = 0; $i < sizeof($tmp); $i++) {
+                       if ($tmp[$i]['id'] == $rid) {
+                               $id = $tmp[$i]['id'];
+                               $hv = $tmp[$i]['hypervisor'];
+                               $rh = $tmp[$i]['remote'];
+                               $rm = $tmp[$i]['method'];
+                               $rp = $tmp[$i]['require_pwd'];
+                               $un = $tmp[$i]['user'];
+                               $hn = $tmp[$i]['host'];
+                               $lg = $tmp[$i]['logfile'];
+                       }
+               }
                
                unset($tmp);
        }
 
+       $skip_rest = false;
        if ($hv) {
-               $uri = $lv->generate_connection_uri($hv, $rh, $rm, $un, $hn);
-               if ($rp) {
-                       $credentials = array(VIR_CRED_AUTHNAME => $un, VIR_CRED_PASSPHRASE => $rp);
-                       $test = libvirt_connect($uri, false, $credentials);
-               }
-               else
-                       $test = libvirt_connect($uri);
-               $ok = is_resource($test);
-               unset($test);
-
-               if ($ok) {
+               if ($lv->test_connection_uri($hv, $rh, $rm, $un, $rp, $hn)) {
+                       $uri = $lv->generate_connection_uri($hv, $rh, $rm, $un, $hn);
                        $_SESSION['connection_uri'] = $uri;
                        $_SESSION['connection_logging'] = $lg;
                        echo '<p>'.$lang->get('changed_uri').' <b>'.$uri.'</b></p>';
                                        echo '<p>'.$lang->get('conn_saved').'</p>';
 
                        echo '<a href="?">'.$lang->get('click_reload').'</a>';
-                       die('</div>');
+                       $skip_rest = true;
                }
                else {
                        echo '<p>'.$lang->get('conn_failed').': '.$uri.'</p>';
        $ds = ($rh) ? 'table-row' : 'none';
 ?>
 
+<?php
+       if (!$skip_rest):
+?>
 <p />
 
 <script language="javascript">
 </table>
 </form>
 
+<?php
+       endif;
+?>
+
 </div>