3 * Bacula® - 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 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 $language = self::DEFAULT_LANGUAGE;
97 if ($this->isApplicationConfig() === true) {
98 $config = $this->getApplicationConfig();
99 if (array_key_exists('lang', $config['baculum'])) {
100 $language = $config['baculum']['lang'];
106 public function setLanguage($language) {
111 * Saving application configuration.
114 * @param array $config structure of config file params
115 * @return boolean true if config save is successfully, false if config save is failure
117 public function setApplicationConfig(array $config) {
118 $cfgFile = Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf');
119 return ($this->Application->getModule('misc')->writeINIFile($cfgFile, $config) != false);
123 * Getting application configuration.
126 * @return array application configuration
128 public static function getApplicationConfig() {
129 $cfgFile = Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf');
130 return Miscellaneous::parseINIFile($cfgFile);
134 * Checking if application configuration file exists.
137 * @return boolean true if file exists, otherwise false
139 public function isApplicationConfig() {
140 return file_exists(Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf'));
144 * Saving user to users configuration file.
147 * So far by webGUI is possible to set one user.
148 * For more users and restricted consoles, there is need to modify
149 * users and passwords file.
151 * TODO: Support for more than one user setting on webGUI.
154 * @param string $user username
155 * @param string $password user's password
156 * @param boolean $firstUsage determine if it is first saved user during first Baculum run
157 * @param mixed $oldUser previous username before change
158 * @return boolean true if user saved successfully, otherwise false
160 public function setUsersConfig($user, $password, $firstUsage = false, $oldUser = null) {
161 $usersFile = Prado::getPathOfNamespace(self::USERS_FILE, '.users');
162 $password = crypt($password, base64_encode($password));
163 if($firstUsage === true) {
164 $this->clearUsersConfig();
167 $users = $this->isUsersConfig() === true ? file($usersFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) : array();
170 for($i = 0; $i < count($users); $i++) {
171 // checking if user already exist in configuration file and if exist then update password
172 if(preg_match("/^{$user}\:/", $users[$i]) === 1) {
173 $users[$i] = "{$user}:{$password}";
179 if(!is_null($oldUser) && $oldUser !== $user) {
180 // delete old username with password from configuration file
181 for($j = 0; $j < count($users); $j++) {
182 if(preg_match("/^{$oldUser}\:/", $users[$j]) === 1) {
189 // add new user if does not exist
190 if($userExists === false) {
191 array_push($users, "{$user}:{$password}");
194 $usersToFile = implode("\n", $users);
195 $old_umask = umask(0);
197 $result = file_put_contents($usersFile, $usersToFile) !== false;
203 * Checking if users configuration file exists.
206 * @return boolean true if file exists, otherwise false
208 public function isUsersConfig() {
209 return file_exists(Prado::getPathOfNamespace(self::USERS_FILE, '.users'));
213 * Clear all content of users file.
216 * @return boolean true if file cleared successfully, otherwise false
218 private function clearUsersConfig() {
219 $usersFile = Prado::getPathOfNamespace(self::USERS_FILE, '.users');
220 $result = file_put_contents($usersFile, '') !== false;