3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2016 Kern Sibbald
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.
24 * Base API database module.
25 * Every API module that use database connection should inherit this class.
27 * @author Marcin Haba <marcin.haba@bacula.pl>
30 Prado::using('Application.Common.Class.Errors');
31 Prado::using('Application.API.Class.BException');
32 Prado::using('Application.API.Class.APIConfig');
33 Prado::using('System.Data.ActiveRecord.TActiveRecord');
35 class APIDbModule extends TActiveRecord {
38 * Get Data Source Name (DSN).
40 * For SQLite params are:
41 * array('type' => 'type', 'path' => '/some/system/path');
42 * For others params are:
43 * array('type' => 'type', 'name' => 'name', 'host' => 'IP or hostname', 'port' => 'database port');
46 * @param array $db_params database connection params
47 * @return string Data Source Name (DSN)
49 public static function getDsn(array $db_params) {
50 $dsn_params = array();
52 if(array_key_exists('path', $db_params) && !empty($db_params['path'])) {
53 $dsn_params[] = $db_params['type'] . ':' . $db_params['path'];
55 $dsn_params[] = $db_params['type'] . ':' . 'dbname=' . $db_params['name'];
57 if(array_key_exists('ip_addr', $db_params)) {
58 $dsn_params[] = 'host=' . $db_params['ip_addr'];
61 if(array_key_exists('port', $db_params)) {
62 $dsn_params[] = 'port=' . $db_params['port'];
66 $dsn = implode(';', $dsn_params);
70 public function getDbConnection() {
71 $config = new APIConfig();
72 $db_params = $config->getConfig('db');
73 $db_connection = self::getAPIDbConnection($db_params);
74 return $db_connection;
78 * Get API catalog database connection.
81 * @param array database parameters from api config
82 * @param bool force connection try (used when db_params are not saved yet)
83 * @return object TDbConnection instance or null if errors occured during connecting
84 * @throws BCatalogExcemption if cataloga access is not supported
86 public static function getAPIDbConnection(array $db_params, $force = false) {
87 $db_connection = null;
88 if ((array_key_exists('enabled', $db_params) && $db_params['enabled'] === '1') || $force === true) {
89 $dsn = self::getDsn($db_params);
90 $db_connection = null;
91 if (array_key_exists('login', $db_params) && array_key_exists('password', $db_params)) {
92 $db_connection = new TDbConnection($dsn, $db_params['login'], $db_params['password']);
94 $db_connection = new TDbConnection($dsn);
96 $db_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
97 $db_connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
99 throw new BCatalogException(
100 DatabaseError::MSG_ERROR_DATABASE_ACCESS_NOT_SUPPORTED,
101 DatabaseError::ERROR_DATABASE_ACCESS_NOT_SUPPORTED
104 return $db_connection;
107 public function getColumnValue($column_name) {
108 // column name to lower due to not correct working PDO::CASE_LOWER for SQLite database
109 $column_name = strtolower($column_name);
110 $value = parent::getColumnValue($column_name);