Polish code and divide functionality into libvirt-{domain|info|network} classes
[php-virt-control.git] / index.php
1 <?php
2         require('init.php');
3         $uri = array_key_exists('connection_uri', $_SESSION) ? $_SESSION['connection_uri'] : 'null';
4         $lg = array_key_exists('connection_logging', $_SESSION) ? $_SESSION['connection_logging'] : false;
5
6         if ($lg == '')
7                 $lg = false;
8
9         $errmsg = false;
10         if (!CONNECT_WITH_NULL_STRING && $uri == 'null')
11                 $uri = false;
12         if (isset($_SESSION['connection_credentials']))
13                 $lv = new Libvirt(
14                         $uri,
15                         $_SESSION['connection_credentials'][VIR_CRED_AUTHNAME],
16                         $_SESSION['connection_credentials'][VIR_CRED_PASSPHRASE],
17                         $lg,
18                         $lang_str
19                 );
20         else
21                 $lv = new Libvirt($uri, null, null, $lg, $lang_str);
22
23         $conns = array();
24         if (array_key_exists('connections', $_SESSION))
25                 $conns = $_SESSION['connections'];
26
27         if (array_key_exists('attach', $_GET) && ($_GET['attach'])) {
28                 $tmp = $db->list_connections(true);
29                 $rid = (int)$_GET['attach'];
30
31                 $new_uri = false;
32                 for ($i = 0; $i < sizeof($tmp); $i++) {
33                         if ($tmp[$i]['id'] == $rid) {
34                                 $id = $tmp[$i]['id'];
35                                 $hv = $tmp[$i]['hypervisor'];
36                                 $nm = $tmp[$i]['name'];
37                                 $rh = $tmp[$i]['remote'];
38                                 $rm = $tmp[$i]['method'];
39                                 $rp = $tmp[$i]['require_pwd'];
40                                 $un = $tmp[$i]['user'];
41                                 $pwd= $tmp[$i]['password'];
42                                 $hn = $tmp[$i]['host'];
43                                 $lg = $tmp[$i]['logfile'];
44                         }
45                 }
46
47                 if ($hv) {
48                         if ($lv->test_connection_uri($hv, $rh, $rm, $un, $pwd, $hn)) {
49                                 $new_uri = $lv->generate_connection_uri($hv, $rh, $rm, $un, $hn);
50                                 $new_conn = array();
51                                 $new_conn['connection_uri'] = $new_uri;
52                                 $new_conn['connection_name'] = $nm;
53                                 $new_conn['connection_logging'] = $lg;
54                                 $new_conn['id'] = $id;
55                                 if (isset($un) && isset($pwd))
56                                         $new_conn['connection_credentials'] = array(
57                                                 VIR_CRED_AUTHNAME => $un,
58                                                 VIR_CRED_PASSPHRASE => $pwd
59                                         );
60                         }
61                 }
62
63                 $skip = false;
64                 foreach ($conns as $item) {
65                         if ($item['connection_uri'] == $new_uri)
66                                 $skip = true;
67                 }
68
69                 if (!$skip) {
70                         $conns[] = $new_conn;
71                         $_SESSION['connections'] = $conns;
72                 }
73
74                 $_GET['connect'] = $rid;
75         }
76
77         if (array_key_exists('detach', $_GET) && ($_GET['detach'])) {
78                 $tmp = $db->list_connections(true);
79                 $rid = (int)$_GET['detach'];
80
81                 for ($i = 0; $i < sizeof($tmp); $i++) {
82                         if ($tmp[$i]['id'] == $rid) {
83                                 $id = $tmp[$i]['id'];
84
85                                 for ($j = 0; $j < sizeof($conns); $j++) {
86                                         if ($conns[$j]['id'] == $id)
87                                                 unset($conns[$j]);
88                                 }
89                         }
90                 }
91
92                 $_SESSION['connections'] = $conns;
93         }
94
95         if ($lg && LOGDIR)
96                 $lg = LOGDIR.'/'.$lg;
97
98         /* Get new MAC address in plain text - called by Ajax from pages/new-vm.php */
99         if (array_key_exists('get_mac', $_GET)) {
100                 die( $lv->generate_random_mac_addr() );
101         }
102         if (!$lv->enabled() || ($lv->get_last_error())) {
103                 $page = 'overview';
104                 $name = false;
105                 $errmsg = $lang->get('cannot-connect').' '.$lv->get_last_error();
106         }
107         else {
108                 $name = array_key_exists('name', $_GET) ? $_GET['name'] : false;
109                 $res = $lv->get_domain_by_name($name);
110                 $page = array_key_exists('page', $_GET) ? $_GET['page'] : 'overview';
111         }
112 ?>
113 <html>
114 <head>
115  <title>php-virt-control - <?php echo $lang->get('title-vmc') ?></title>
116  <link rel="STYLESHEET" type="text/css" href="html/main.css" />
117  <link rel="STYLESHEET" type="text/css" href="manager.css" />
118 </head>
119 <body>
120   <div id="header">
121     <div id="headerLogo"></div>
122   </div>
123
124 <?php
125         include('main-menu.php');
126         if ($name):
127 ?>
128         <h2 id="vm-name"><?php echo $lang->get('vm-title').' '.$name ?></h2>
129 <?php
130         include('menu.php');
131         if (File_Exists('./pages/details/'.$page.'.php'))
132                 include('./pages/details/'.$page.'.php');
133         else
134                 include('error.php');
135         else:
136         if (File_Exists('./pages/'.$page.'.php'))
137                 include('./pages/'.$page.'.php');
138         else
139                 include('error.php');
140         endif;
141 ?>
142
143 <?php
144         if (DEBUG) {
145                 echo '<div id="content">';
146                 echo '<div class="section">Debug - Libvirt-php resources</div>';
147
148                 $resources = $lv->print_resources();
149                 for ($i = 0; $i < sizeof($resources); $i++) {
150                         echo '<div class="item">';
151                         echo '        <div class="label">Resource #'.($i + 1).'</div>';
152                         echo '        <div class="value">'.$resources[$i].'</div>';
153                         echo '        <div class="nl">';
154                         echo '</div>';
155                 }
156
157                 echo '</div>';
158         }
159 ?>
160
161 </body>
162 </html>