3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2015 Marcin Haba
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.
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.
17 * This notice must be preserved when any source code is
18 * conveyed and/or propagated.
20 * Bacula(R) is a registered trademark of Kern Sibbald.
23 Prado::using('System.Data.TDbConnection');
24 Prado::using('Application.Class.ConfigurationManager');
26 class Database extends TModule {
28 const DB_PARAMS_FILE = 'Application.Data.settings';
30 public static function getDBParams() {
31 $cfg = ConfigurationManager::getApplicationConfig();
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']);
37 * Get Data Source Name (DSN).
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');
45 * @param array $dbParams params of Data Source Name (DSN)
46 * @return string Data Source Name (DSN)
48 public static function getDsnByParams(array $dbParams) {
51 if(array_key_exists('path', $dbParams) && !empty($dbParams['path'])) {
52 $dsn[] = $dbParams['type'] . ':' . $dbParams['path'];
54 $dsn[] = $dbParams['type'] . ':' . 'dbname=' . $dbParams['name'];
56 if(array_key_exists('host', $dbParams)) {
57 $dsn[] = 'host=' . $dbParams['host'];
60 if(array_key_exists('port', $dbParams)) {
61 $dsn[] = 'port=' . $dbParams['port'];
65 $dsnFull = implode(';', $dsn);
70 * Checking connection to database.
73 * @param array $dbParams params to database connection
74 * @return bool if connection established then returns true, otherwise returns false.
76 public function testDbConnection(array $dbParams) {
77 $dsn = self::getDsnByParams($dbParams);
82 if(array_key_exists('login', $dbParams) && array_key_exists('password', $dbParams)) {
83 $connection = new TDBConnection($dsn, $dbParams['login'], $dbParams['password']);
85 $connection = new TDBConnection($dsn);
88 $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
89 $connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
90 $connection->Active = true;
92 $tablesFormat = $this->getTablesFormat($connection);
93 $isDatabase = (is_numeric($tablesFormat) === true && $tablesFormat > 0);
94 } catch (TDbException $e) {
98 if(array_key_exists('password', $dbParams)) {
99 // masking database password.
100 $dbParams['password'] = preg_replace('/.{1}/', '*', $dbParams['password']);
103 Prado::trace(sprintf('DBParams=%s, Connection=%s, TablesFormat=%s', print_r($dbParams, true), var_export($isDatabase, true), var_export($tablesFormat, true)), 'Execute');
109 * Get number of Bacula database tables format.
112 * @param TDBConnection $connection handler to database connection
113 * @return integer number of Bacula database tables format
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;
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'];
129 $db = new ActiveRecord();
130 $connection = $db->getDbConnection();
131 $connection->setActive(true);
132 $pdo = $connection->getPdoInstance();
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']));
153 return $dbsize['dbsize'];