]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/API/Class/Database.php
Update some old copyrights
[bacula/bacula] / gui / baculum / protected / API / Class / Database.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2016 Kern Sibbald
7  *
8  * The main author of Baculum is Marcin Haba.
9  * The original author of Bacula is Kern Sibbald, with contributions
10  * from many others, a complete list can be found in the file AUTHORS.
11  *
12  * You may use this file and others of this release according to the
13  * license defined in the LICENSE file, which includes the Affero General
14  * Public License, v3.0 ("AGPLv3") and some additional permissions and
15  * terms pursuant to its AGPLv3 Section 7.
16  *
17  * This notice must be preserved when any source code is
18  * conveyed and/or propagated.
19  *
20  * Bacula(R) is a registered trademark of Kern Sibbald.
21  */
22  
23 Prado::using('System.interfaces');
24 Prado::using('Application.Common.Class.Errors');
25 Prado::using('Application.API.Class.BException');
26 Prado::using('Application.API.Class.APIModule');
27 Prado::using('Application.API.Class.APIDbModule');
28
29 class Database extends APIModule {
30
31         public $ID;
32
33         /**
34          * Supported database types
35          */
36         const PGSQL_TYPE = 'pgsql';
37         const MYSQL_TYPE = 'mysql';
38         const SQLITE_TYPE = 'sqlite';
39
40         /**
41          * Check/test connection to database.
42          * 
43          * @access public
44          * @param array $db_params params to database connection
45          * @return array true if test passed, otherwise false
46          * @throws BCatalogException if problem with connection to database
47          */
48         public function testDbConnection(array $db_params) {
49                 $is_connection = false;
50                 $tables_format = null;
51
52                 try {
53                         $connection = APIDbModule::getAPIDbConnection($db_params, true);
54                         $connection->setActive(true);
55                         $tables_format = $this->getTablesFormat($connection);
56                         $is_connection = (is_numeric($tables_format) === true && $tables_format > 0);
57                 } catch (TDbException $e) {
58                         throw new BCatalogException(
59                                 DatabaseError::MSG_ERROR_DB_CONNECTION_PROBLEM . ' ' . $e->getErrorMessage(),
60                                 DatabaseError::ERROR_DB_CONNECTION_PROBLEM
61                         );
62                 }
63
64                 if(array_key_exists('password', $db_params)) {
65                         // mask database password.
66                         $db_params['password'] = preg_replace('/.{1}/', '*', $db_params['password']);
67                 }
68
69                 $logmsg = 'DBParams=%s, Connection=%s, TablesFormat=%s';
70                 $msg = sprintf($logmsg, print_r($db_params, true), var_export($is_connection, true), var_export($tables_format, true));
71                 $this->getModule('logging')->log(
72                         __FUNCTION__,
73                         $msg,
74                         Logging::CATEGORY_APPLICATION,
75                         __FILE__,
76                         __LINE__
77                 );
78                 return $is_connection;
79         }
80
81         /**
82          * Test connection to the Catalog using params from config.
83          *
84          * @access public
85          * @return bool true if connection established, otherwise false
86          */
87         public function testCatalog() {
88                 $result = false;
89                 $api_config = $this->getModule('api_config')->getConfig();
90                 if (array_key_exists('db', $api_config)) {
91                         $result = $this->testDbConnection($api_config['db']);
92                 } else {
93                         throw new BCatalogException(
94                                 DatabaseError::MSG_ERROR_DATABASE_ACCESS_NOT_SUPPORTED,
95                                 DatabaseError::ERROR_DATABASE_ACCESS_NOT_SUPPORTED
96                         );
97                 }
98                 return $result;
99         }
100
101         /**
102          * Get Catalog database tables format
103          * 
104          * @access private
105          * @param TDBConnection $connection handler to database connection
106          * @return mixed Catalog database tables format or null
107          */
108         private function getTablesFormat(TDBConnection $connection) {
109                 $query = 'SELECT versionid FROM Version';
110                 $command = $connection->createCommand($query);
111                 $row = $command->queryRow();
112                 $tables_format = array_key_exists('versionid', $row) ? $row['versionid'] : null;
113                 return $tables_format;
114         }
115
116         public function getDatabaseSize() {
117                 $db_params = $this->getModule('api_config')->getConfig('db');
118
119                 $connection = APIDbModule::getAPIDbConnection($db_params);
120                 $connection->setActive(true);
121                 $pdo = $connection->getPdoInstance();
122
123                 $size = 0;
124                 switch ($db_params['type']) {
125                         case self::PGSQL_TYPE: {
126                                 $sql = "SELECT pg_database_size('{$db_params['name']}') AS dbsize";
127                                 $result = $pdo->query($sql);
128                                 $row = $result->fetch();
129                                 $size = $row['dbsize'];
130                                 break;
131                         }
132                         case self::MYSQL_TYPE: {
133                                 $sql = "SELECT Sum(data_length + index_length) AS dbsize FROM information_schema.tables";
134                                 $result = $pdo->query($sql);
135                                 $row = $result->fetch();
136                                 $size = $row['dbsize'];
137                                 break;
138                         }
139                         case self::SQLITE_TYPE: {
140                                 $sql = "PRAGMA page_count";
141                                 $result = $pdo->query($sql);
142                                 $page_count = $result->fetch();
143                                 $sql = "PRAGMA page_size";
144                                 $result = $pdo->query($sql);
145                                 $page_size = $result->fetch();
146                                 $size = ($page_count['page_count'] * $page_size['page_size']);
147                                 break;
148                         }
149                 }
150                 $dbsize = array('dbsize' => $size, 'dbtype' => $db_params['type']);
151                 $pdo = null;
152                 return $dbsize;
153         }
154 }
155 ?>