]> git.sur5r.net Git - bacula/bacula/blob - gui/bacula-web/includes/bweb.inc.php
bacula-web: Removed useless code in bweb class
[bacula/bacula] / gui / bacula-web / includes / bweb.inc.php
1 <?php
2 /* 
3 +-------------------------------------------------------------------------+
4 | Copyright (C) 2004 Juan Luis Francés Jiménez                                                    |
5 | Copyright 2010-2011, Davide Franco                                              |
6 |                                                                         |
7 | This program is free software; you can redistribute it and/or           |
8 | modify it under the terms of the GNU General Public License             |
9 | as published by the Free Software Foundation; either version 2          |
10 | of the License, or (at your option) any later version.                  |
11 |                                                                         |
12 | This program is distributed in the hope that it will be useful,         |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
15 | GNU General Public License for more details.                            |
16 +-------------------------------------------------------------------------+ 
17 */
18 class Bweb extends DB 
19 {
20         public  $tpl;
21         public  $db_link;                                               // Database link
22         
23         private $config_file;                                   // Config filename
24         private $config;                                                // Loaded config from bacula.conf
25         private $catalogs = array();                    // Catalog array
26         public  $catalog_nb;
27         private $catalog_current_id;
28         private $bwcfg;
29
30     function __construct()
31         {             
32                 $this->bwcfg = new Config();
33                 
34                 // Loading configuration from config file
35                 $this->bwcfg->Load_Config();
36                 $this->catalog_nb = $this->bwcfg->Count_Catalogs();
37                 
38                 // Initialize smarty template classe
39                 $this->init_tpl();
40                 // Initialize smarty gettext function
41                 $this->init_gettext();
42                 
43                 // Check catalog id
44                 if( isset($_POST['catalog_id']) ) {
45                         $this->catalog_current_id = $_POST['catalog_id'];
46                         $_SESSION['catalog_id'] = $this->catalog_current_id;
47                 }
48                 elseif( isset( $_SESSION['catalog_id'] ) )
49                         $this->catalog_current_id = $_SESSION['catalog_id'];
50                 else {
51                         $this->catalog_current_id = 0;
52                         $_SESSION['catalog_id'] = $this->catalog_current_id;
53                 }
54
55                 $this->tpl->assign( 'catalog_current_id', $this->catalog_current_id );
56                 
57                 // Database connection
58                 try {
59                         $this->db_link = new CDB(       $this->bwcfg->getDSN($this->catalog_current_id), 
60                                                                                 $this->bwcfg->getUser($this->catalog_current_id), 
61                                                                                 $this->bwcfg->getPwd($this->catalog_current_id)  );
62                         $this->db_link->makeConnection();       
63         }catch( PDOException $e ) {
64                         CDBError::raiseError( $e );
65                 }               
66
67                 // Catalog selection            
68                 if( $this->catalog_nb > 1 ) {
69                         // Catalogs list
70                         $this->tpl->assign( 'catalogs', $this->bwcfg->Get_Catalogs() );                 
71                         // Catalogs count
72                         $this->tpl->assign( 'catalog_nb', $this->catalog_nb );
73                 }
74         }
75                 
76     // Initialize Smarty template classe
77         function init_tpl()
78         {
79                 $this->tpl = new Smarty();
80                 
81                 $this->tpl->compile_check       = true;
82                 $this->tpl->debugging           = false;
83                 $this->tpl->force_compile       = true;
84
85                 $this->tpl->template_dir        = "./templates";
86                 $this->tpl->compile_dir         = "./templates_c";
87         }
88         
89         function init_gettext()
90         {
91                 global $smarty_gettext_path;
92                 
93                 if ( function_exists("gettext") ) {
94                         require_once( BW_SMARTY_GETTEXT . "smarty_gettext.php" );     
95                         $this->tpl->register_block('t','smarty_translate');
96         
97                         $language = $this->bwcfg->Get_Config_Param("lang");
98                         $domain = "messages";   
99                         putenv("LANG=$language"); 
100                         setlocale(LC_ALL, $language);
101                         bindtextdomain($domain,"./locale");
102                         textdomain($domain);
103                 }
104                 else {
105                         function smarty_translate($params, $text, &$smarty) {
106                 return $text;
107                         }
108                         $smarty->register_block('t','smarty_translate');
109                 }
110         }
111         
112         function GetDbSize() 
113         {
114                 $db_size = 0;
115                 $query   = '';
116                 $result  = '';
117                 
118                 switch( $this->db_link->getDriver() )
119                 {
120                         case 'mysql':
121                                 $query  = "SELECT table_schema AS 'database', sum( data_length + index_length) AS 'dbsize' ";
122                                 $query .= "FROM information_schema.TABLES ";
123                                 $query .= "WHERE table_schema = 'bacula' ";
124                                 $query .= "GROUP BY table_schema";
125                         break;
126                         case 'pgsql':
127                                 $query  = "SELECT pg_database_size('bacula') AS dbsize";
128                         break;
129                         case 'sqlite':
130                                 // Not yet implemented
131                                 return "0 MB";
132                         break;
133                 }
134                 
135                 // Execute SQL statment
136                 try {
137                         $result  = $this->db_link->runQuery( $query );
138                         $db_size = $result->fetch();
139                         $db_size = CUtils::Get_Human_Size( $db_size['dbsize'] );
140                 }catch( PDOException $e) {
141                         CDBError::raiseError($e);
142                 }
143                 return $db_size;
144         } // end function GetDbSize()
145         
146         public function Get_Nb_Clients()
147         {
148                 $result    = '';
149                 $clients_nb = 0;
150                 $query     = "SELECT COUNT(*) AS nb_client FROM Client";
151                 
152                 try {
153                         $clients    = $this->db_link->runQuery( $query );
154                         $clients_nb = $clients->fetch();
155                 }catch( PDOException $e ) {
156                         CDBError::raiseError( $e );
157                 }
158                 
159                 return $clients_nb;
160         }
161   
162         // Return an array of volumes ordered by poolid and volume name
163         function GetVolumeList() 
164         {
165                         $pools        = '';
166                         $volumes      = '';
167                         $volumes_list = array();
168                         $query        = "";
169                         $debug        = false;
170                         
171                         // Get the list of pools id
172                         $query = "SELECT Pool.poolid, Pool.name FROM Pool ORDER BY Pool.poolid";
173                         
174                         try {
175                                 foreach( $this->getPools() as $pool ) {
176                                         switch( $this->db_link->getDriver() )
177                                         {
178                                                 case 'sqlite':
179                                                 case 'mysql':
180                                                         $query  = "SELECT Media.volumename, Media.volbytes, Media.volstatus, Media.mediatype, Media.lastwritten, Media.volretention
181                                                                         FROM Media LEFT JOIN Pool ON Media.poolid = Pool.poolid
182                                                                         WHERE Media.poolid = '". $pool['poolid'] . "' ORDER BY Media.volumename";
183                                                 break;
184                                                 case 'pgsql':
185                                                         $query  = "SELECT media.volumename, media.volbytes, media.volstatus, media.mediatype, media.lastwritten, media.volretention
186                                                                         FROM media LEFT JOIN pool ON media.poolid = pool.poolid
187                                                                     WHERE media.poolid = '". $pool['poolid'] . "' ORDER BY media.volumename";
188                                                 break;
189                                         } // end switch
190                                         
191                                         $volumes = $this->db_link->runQuery($query);
192                                 
193                                         if( !array_key_exists( $pool['name'], $volumes_list) )
194                                                 $volumes_list[ $pool['name'] ] = array();
195                                         
196                                         foreach( $volumes->fetchAll() as $volume ) {
197                                                 if( $volume['lastwritten'] != "0000-00-00 00:00:00" ) {
198                                                         
199                                                         // Calculate expiration date if the volume is Full
200                                                         if( $volume['volstatus'] == 'Full' ) {
201                                                                 $expire_date     = strtotime($volume['lastwritten']) + $volume['volretention'];
202                                                                 $volume['expire'] = strftime("%Y-%m-%d", $expire_date);
203                                                         }else {
204                                                                 $volume['expire'] = 'N/A';
205                                                         }
206                                                         
207                                                         // Media used bytes in a human format
208                                                         $volume['volbytes'] = CUtils::Get_Human_Size( $volume['volbytes'] );
209                                                 } else {
210                                                         $volume['lastwritten'] = "N/A";
211                                                         $volume['expire']      = "N/A";
212                                                         $volume['volbytes']       = "0 KB";
213                                                 }
214                                                 
215                                                 // Odd or even row
216                                                 if( count(  $volumes_list[ $pool['name'] ] ) % 2)
217                                                         $volume['class'] = 'odd';
218
219                                                 // Add the media in pool array
220                                                 array_push( $volumes_list[ $pool['name']], $volume);
221                                         } // end foreach volumes
222                                 } // end foreach pools
223                                 
224                         }catch(PDOException $e) {
225                                 CDBError::raiseError($e);
226                         }
227                         
228                         return $volumes_list;
229         } // end function GetVolumeList()
230         
231         public function countJobs( $start_timestamp, $end_timestamp, $status = 'ALL', $level = 'ALL', $jobname = 'ALL', $client = 'ALL' )
232         {
233                 $query                    = "";
234                 $where_interval   = "";
235                 $where_conditions = array();
236                 $result                   = '';
237                 
238                 // Calculate sql query interval
239                 $start_date             = date( "Y-m-d H:i:s", $start_timestamp);       
240                 $end_date               = date( "Y-m-d H:i:s", $end_timestamp);
241                 
242                 switch( $this->db_link->getDriver() )
243                 {
244                         case 'sqlite':
245                         case 'mysql':
246                                 $query                 .= "SELECT COUNT(*) AS job_nb FROM Job ";
247                                 $where_conditions[] = "(EndTime BETWEEN '$start_date' AND '$end_date')";
248                         break;
249                         case 'pgsql':
250                                 $query             .= "SELECT COUNT(*) AS job_nb FROM job ";
251                                 $where_conditions[] = "(EndTime BETWEEN timestamp '$start_date' AND timestamp '$end_date')";
252                         break;
253                 }
254                 
255                 if( $status != 'ALL' ) {
256                         switch( strtolower($status) )
257                         {
258                                 case 'running':
259                                         array_pop( $where_conditions );
260                                         $where_conditions[] = "JobStatus = 'R' ";
261                                 break;
262                                 case 'completed':
263                                         $where_conditions[] = "JobStatus = 'T' ";
264                                 break;
265                                 case 'failed':
266                                         $where_conditions[] = "JobStatus IN ('f','E') ";
267                                 break;
268                                 case 'canceled':
269                                         $where_conditions[] = "JobStatus = 'A' ";
270                                 break;
271                                 case 'waiting':
272                                         array_pop( $where_conditions );
273                                         $where_conditions[] = "Job.JobStatus IN ('F','S','M','m','s','j','c','d','t','p','C') ";
274                                 break;
275                         } // end switch
276                 }
277                 
278                 // Filter by status
279                 if( $level != 'ALL' )
280                         $where_conditions[] = "Level = '$level' ";
281                 
282                 // Construct SQL query
283                 foreach( $where_conditions as $k => $condition ) {
284                         if( $k == 0) {
285                                 $query .= "WHERE $condition ";
286                         }else
287                                 $query .= "AND $condition ";
288                 }
289                 
290                 // Execute the query
291                 try{
292                         $jobs   = $this->db_link->runQuery($query);
293                         $result = $jobs->fetch(); 
294                 }catch(PDOException $e) {
295                         CDBError::raiseError($e);
296                 }
297                 
298                 return $result['job_nb'];
299         }
300         
301         // Return the list of Pools in a array
302         public function getPools()
303         {
304                 $pools  = array();
305                 $result = '';
306                 
307                 switch( $this->db_link->getDriver() )
308                 {
309                         case 'sqlite':
310                         case 'mysql':
311                                 $query          = "SELECT name, poolid FROM Pool";
312                         break;
313                         case 'pgsql':
314                                 $query          = "SELECT name, poolid FROM pool";
315                         break;
316                 }
317                 try{
318                         $result = $this->db_link->runQuery($query);
319                         foreach( $result->fetchAll() as $pool )
320                                 $pools[] = $pool;
321                 }catch(PDOException $e) {
322                         CDBError::raiseError($e);
323                 }
324
325                 return $pools;
326         }
327         
328         public function Get_BackupJob_Names()
329         {
330                 $query          = '';
331                 $result         = '';
332                 $backupjobs = array();
333                 
334                 switch( $this->db_link->getDriver() )
335                 {
336                         case 'sqlite':
337                         case 'mysql':
338                                 $query          = "SELECT name FROM Job GROUP BY name ORDER BY name";
339                         break;
340                         case 'pgsql':
341                                 $query          = "SELECT name FROM Job GROUP BY name ORDER BY name";
342                         break;
343                 }
344                 try {
345                         $result = $this->db_link->runQuery($query);
346                         foreach( $result->fetchAll() as $jobname )
347                                 $backupjobs[] = $jobname['name'];
348                 }catch(PDOException $e) {
349                         CDBError::raiseError($e);
350                 }
351
352                 return $backupjobs;
353         }
354         
355         public function countVolumes( $pool_id = 'ALL' )
356         {
357                 $result = null;
358                 $nb_vol = null;
359                 $query  = '';
360
361                 switch( $this->db_link->getDriver() )
362                 {
363                         case 'sqlite':
364                         case 'mysql':
365                                 $query  = 'SELECT COUNT(*) as vols_count ';
366                                 $query .= 'FROM Media ';
367                                 if( $pool_id != 'ALL' )
368                                         $query .= ' WHERE Media.poolid = ' . $pool_id;
369                         break;
370                         case 'pgsql':
371                                 $query  = 'SELECT COUNT(*) as vols_count ';
372                                 $query .= 'FROM Media ';
373                                 if( $pool_id != 'ALL' )
374                                         $query .= ' WHERE media.poolid = ' . $pool_id;
375                         break;
376                 }
377                 
378                 // Execute sql query
379                 try {
380                         $result = $this->db_link->runQuery($query);
381                         $vols = $result->fetch();
382                 }catch( PDOException $e) {
383                         CDBError::raiseError($e);
384                 }
385                 
386                 return $vols['vols_count'];
387         }
388         
389         public function getStoredFiles( $start_timestamp, $end_timestamp, $job_name = 'ALL' )
390         {
391                 $query = "";
392                 $start_date = date( "Y-m-d H:i:s", $start_timestamp);   
393                 $end_date   = date( "Y-m-d H:i:s", $end_timestamp);     
394                 
395                 switch( $this->db_link->getDriver() )
396                 {
397                         case 'sqlite':
398                         case 'mysql':
399                                         $query = "SELECT SUM(JobFiles) AS stored_files FROM Job ";
400                                         $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' )";
401                         break;
402                         case 'pgsql':
403                                         $query = "SELECT SUM(JobFiles) AS stored_files FROM job ";
404                                         $query .= "WHERE ( endtime BETWEEN timestamp '$start_date' AND timestamp '$end_date' )";
405                         break;
406                 }
407                 
408                 if( $job_name != 'ALL' ) 
409                         $query .= " AND name = '$job_name'";
410                 
411                 // Execute query
412                 try {
413                         $result = $this->db_link->runQuery( $query );
414                         $result = $result->fetch();
415                 }catch( PDOException $e) {
416                         CDBError::raiseError($e);
417                 }
418                 
419                 if( isset($result['stored_files']) and !empty($result['stored_files']) )
420                         return $result['stored_files'];
421                 else
422                         return 0;
423         }
424         
425         // Function: getStoredBytes
426         // Parameters:
427         //              $start_timestamp:       start date in unix timestamp format
428         //              $end_timestamp:         end date in unix timestamp format
429         //              $job_name:                      optional job name
430         
431         public function getStoredBytes( $start_timestamp, $end_timestamp, $job_name = 'ALL' )
432         {
433                 $query                  = '';
434                 $result                 = '';
435                 $start_date             = date( "Y-m-d H:i:s", $start_timestamp);       
436                 $end_date               = date( "Y-m-d H:i:s", $end_timestamp); 
437                 
438                 switch( $this->db_link->getDriver() )
439                 {
440                         case 'sqlite':
441                         case 'mysql':
442                                 $query  = "SELECT SUM(JobBytes) as stored_bytes FROM Job ";
443                                 $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' )";
444                         break;
445                         case 'pgsql':
446                                 $query  = "SELECT SUM(jobbytes) as stored_bytes FROM job ";
447                                 $query .= "WHERE ( endtime BETWEEN timestamp '$start_date' AND timestamp '$end_date' )";
448                         break;
449                 }
450
451                 if( $job_name != 'ALL' ) 
452                         $query .= " AND name = '$job_name'";
453                 
454                 // Execute SQL statment
455                 try {
456                         $result = $this->db_link->runQuery( $query );
457                         $result = $result->fetch();
458                 }catch(PDOException $e) {
459                         CDBError::raiseError( $e );
460                 }
461                 
462                 if( isset($result['stored_bytes']) and !empty($result['stored_bytes']) )
463                         return $result['stored_bytes'];
464                 else
465                         return 0;
466         }
467 } // end class Bweb
468 ?>