3 * Bacula® - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2014 Marcin Haba
8 * The main author of Baculum is Marcin Haba.
9 * The main author of Bacula is Kern Sibbald, with contributions from many
10 * 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 * Bacula® is a registered trademark of Kern Sibbald.
20 Prado::using('Application.Class.Miscellaneous');
22 class ConfigurationManager extends TModule
26 * Location o application configuration file.
28 const CONFIG_FILE = 'Application.Data.settings';
31 * Users login and password file for HTTP Basic auth.
33 const USERS_FILE = 'Application.Data.baculum';
36 * PostgreSQL default params.
38 const PGSQL = 'pgsql';
39 const PGSQL_NAME = 'PostgreSQL';
40 const PGSQL_PORT = 5432;
43 * MySQL default params.
45 const MYSQL = 'mysql';
46 const MYSQL_NAME = 'MySQL';
47 const MYSQL_PORT = 3306;
50 * SQLite default params.
52 const SQLITE = 'sqlite';
53 const SQLITE_NAME = 'SQLite';
54 const SQLITE_PORT = null;
57 * Default language for application.
59 const DEFAULT_LANGUAGE = 'en';
61 public function getDbNameByType($type) {
63 case self::PGSQL: $dbName = self::PGSQL_NAME; break;
64 case self::MYSQL: $dbName = self::MYSQL_NAME; break;
65 case self::SQLITE: $dbName = self::SQLITE_NAME; break;
66 default: $dbName = null; break;
71 public function getPostgreSQLType() {
75 public function getMySQLType() {
79 public function getSQLiteType() {
83 public function isPostgreSQLType($type) {
84 return ($type === self::PGSQL);
87 public function isMySQLType($type) {
88 return ($type === self::MYSQL);
91 public function isSQLiteType($type) {
92 return ($type === self::SQLITE);
95 public function getLanguage() {
96 return $this->Application->Parameters['language'];
99 public function setLanguage($language) {
104 * Saving application configuration.
107 * @param array $config structure of config file params
108 * @return boolean true if config save is successfully, false if config save is failure
110 public function setApplicationConfig(array $config) {
111 $cfgFile = Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf');
112 return ($this->Application->getModule('misc')->writeINIFile($cfgFile, $config) != false);
116 * Getting application configuration.
119 * @return array application configuration
121 public static function getApplicationConfig() {
122 $cfgFile = Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf');
123 return Miscellaneous::parseINIFile($cfgFile);
127 * Checking if application configuration file exists.
130 * @return boolean true if file exists, otherwise false
132 public function isApplicationConfig() {
133 return file_exists(Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf'));
137 * Saving user to users configuration file.
140 * So far by webGUI is possible to set one user.
141 * For more users and restricted consoles, there is need to modify
142 * users and passwords file.
144 * TODO: Support for more than one user setting on webGUI.
147 * @param string $user username
148 * @param string $password user's password
149 * @param boolean $firstUsage determine if it is first saved user during first Baculum run
150 * @param mixed $oldUser previous username before change
151 * @return boolean true if user saved successfully, otherwise false
153 public function setUsersConfig($user, $password, $firstUsage = false, $oldUser = null) {
154 $usersFile = Prado::getPathOfNamespace(self::USERS_FILE, '.users');
155 if($firstUsage === true) {
156 $this->clearUsersConfig();
159 $users = $this->isUsersConfig() === true ? file($usersFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) : array();
162 for($i = 0; $i < count($users); $i++) {
163 // checking if user already exist in configuration file and if exist then update password
164 if(preg_match("/^{$user}\:/", $users[$i]) === 1) {
165 $users[$i] = "{$user}:{$password}";
171 if(!is_null($oldUser) && $oldUser !== $user) {
172 // delete old username with password from configuration file
173 for($j = 0; $j < count($users); $j++) {
174 if(preg_match("/^{$oldUser}\:/", $users[$j]) === 1) {
181 // add new user if does not exist
182 if($userExists === false) {
183 array_push($users, "{$user}:{$password}");
186 $usersToFile = implode("\n", $users);
187 $result = file_put_contents($usersFile, $usersToFile) !== false;
192 * Checking if users configuration file exists.
195 * @return boolean true if file exists, otherwise false
197 public function isUsersConfig() {
198 return file_exists(Prado::getPathOfNamespace(self::USERS_FILE, '.users'));
202 * Clear all content of users file.
205 * @return boolean true if file cleared successfully, otherwise false
207 private function clearUsersConfig() {
208 $usersFile = Prado::getPathOfNamespace(self::USERS_FILE, '.users');
209 $result = file_put_contents($usersFile, '') !== false;