]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Class/Database.php
a2fdfd71a922e610e93bbb6a3bbc0c617404a797
[bacula/bacula] / gui / baculum / protected / Class / Database.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2015 Marcin Haba
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.Data.TDbConnection');
24 Prado::using('Application.Class.ConfigurationManager');
25
26 class Database extends TModule {
27
28         const DB_PARAMS_FILE = 'Application.Data.settings';
29
30         public static function getDBParams() {
31                 $cfg = ConfigurationManager::getApplicationConfig();
32                 $dbParams = array();
33                 return array('type' => $cfg['db']['type'], 'name' => $cfg['db']['name'], 'path' => $cfg['db']['path'], 'host' => $cfg['db']['ip_addr'], 'port' => $cfg['db']['port'], 'login' => $cfg['db']['login'], 'password' => $cfg['db']['password']);
34         }
35
36         /**
37          * Get Data Source Name (DSN).
38          * 
39          * For SQLite params are:
40          *      array('dbtype' => 'type', 'dbpath' => 'localization');
41          * For others params are:
42          *      array('dbType' => 'type', 'dbname' => 'name', 'host' => 'IP or hostname', 'port' => 'database port');
43          * 
44          * @access public
45          * @param array $dbParams params of Data Source Name (DSN)
46          * @return string Data Source Name (DSN)
47          */
48         public static function getDsnByParams(array $dbParams) {
49                 $dsn = array();
50
51                 if(array_key_exists('path', $dbParams) && !empty($dbParams['path'])) {
52                         $dsn[] = $dbParams['type'] . ':' . $dbParams['path'];
53                 } else {
54                         $dsn[] =  $dbParams['type'] . ':' . 'dbname=' . $dbParams['name'];
55
56                         if(array_key_exists('host', $dbParams)) {
57                                 $dsn[] = 'host=' . $dbParams['host'];
58                         }
59
60                         if(array_key_exists('port', $dbParams)) {
61                                 $dsn[] = 'port=' . $dbParams['port'];
62                         }
63                 }
64
65                 $dsnFull = implode(';', $dsn);
66                 return $dsnFull;
67         }
68
69         /**
70          * Checking connection to database.
71          * 
72          * @access public
73          * @param array $dbParams params to database connection
74          * @return bool if connection established then returns true, otherwise returns false.
75          */
76         public function testDbConnection(array $dbParams) {
77                 $dsn = self::getDsnByParams($dbParams);
78                 $isDatabase = false;
79                 $tablesFormat = null;
80
81                 try {
82                         if(array_key_exists('login', $dbParams) && array_key_exists('password', $dbParams)) {
83                                 $connection = new TDBConnection($dsn, $dbParams['login'], $dbParams['password']);
84                         } else {
85                                 $connection = new TDBConnection($dsn);
86                         }
87
88                         $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
89                         $connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
90                         $connection->Active = true;
91
92                         $tablesFormat = $this->getTablesFormat($connection);
93                         $isDatabase = (is_numeric($tablesFormat) === true && $tablesFormat > 0);
94                 } catch (TDbException $e) {
95                         $isDatabase = false;
96                 }
97
98                 if(array_key_exists('password', $dbParams)) {
99                         // masking database password.
100                         $dbParams['password'] = preg_replace('/.{1}/', '*', $dbParams['password']);
101                 }
102
103                 Prado::trace(sprintf('DBParams=%s, Connection=%s, TablesFormat=%s', print_r($dbParams, true), var_export($isDatabase, true), var_export($tablesFormat, true)), 'Execute');
104
105                 return $isDatabase;
106         }
107
108         /**
109          * Get number of Bacula database tables format.
110          * 
111          * @access private
112          * @param TDBConnection $connection handler to database connection
113          * @return integer number of Bacula database tables format
114          */
115         private function getTablesFormat(TDBConnection $connection) {
116                 $query = 'SELECT versionid FROM Version';
117                 $command = $connection->createCommand($query);
118                 $result = $command->queryRow();
119                 $ret = (array_key_exists('versionid', $result) === true) ? $result['versionid'] : false;
120                 return $ret;
121         }
122
123         public function getDatabaseSize() {
124                 $configuration = $this->Application->getModule('configuration');
125                 $db_params = $this->getDBParams();
126                 $dbtype = $db_params['type'];
127                 $dbname = $db_params['name'];
128
129                 $db = new ActiveRecord();
130                 $connection = $db->getDbConnection();
131                 $connection->setActive(true);
132                 $pdo = $connection->getPdoInstance();
133
134                 $dbsize = 0;
135                 if ($configuration->isPostgreSQLType($dbtype)) {
136                         $sql = "SELECT pg_database_size('$dbname') AS dbsize";
137                         $result = $pdo->query($sql);
138                         $dbsize = $result->fetch();
139                 } else if ($configuration->isMySQLType($dbtype)) {
140                         $sql = "SELECT Sum(data_length + index_length) AS dbsize FROM information_schema.tables";
141                         $result = $pdo->query($sql);
142                         $dbsize = $result->fetch();
143                 } else if ($configuration->isSQLiteType($dbtype)) {
144                         $sql = "PRAGMA page_count";
145                         $result = $pdo->query($sql);
146                         $page_count = $result->fetch();
147                         $sql = "PRAGMA page_size";
148                         $result = $pdo->query($sql);
149                         $page_size = $result->fetch();
150                         $dbsize = array('dbsize' => ($page_count['page_count'] * $page_size['page_size']));
151                 }
152                 $pdo = null;
153                 return $dbsize['dbsize'];
154         }
155 }
156 ?>