]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Class/ConfigurationManager.php
baculum: Saving auth file for web server HTTP Basic auth
[bacula/bacula] / gui / baculum / protected / Class / ConfigurationManager.php
1 <?php
2 /**
3  * Bacula® - The Network Backup Solution
4  * Baculum - Bacula web interface
5  *
6  * Copyright (C) 2013-2014 Marcin Haba
7  *
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.
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  * Bacula® is a registered trademark of Kern Sibbald.
18  */
19
20 Prado::using('Application.Class.Miscellaneous');
21
22 class ConfigurationManager extends TModule
23 {
24
25         /**
26          * Location o application configuration file.
27          */
28         const CONFIG_FILE = 'Application.Data.settings';
29
30         /**
31          * Users login and password file for HTTP Basic auth.
32          */
33         const USERS_FILE = 'Application.Data.baculum';
34
35         /**
36          * PostgreSQL default params.
37          */
38         const PGSQL = 'pgsql';
39         const PGSQL_NAME = 'PostgreSQL';
40         const PGSQL_PORT = 5432;
41
42         /**
43          * MySQL default params.
44          */
45         const MYSQL = 'mysql';
46         const MYSQL_NAME = 'MySQL';
47         const MYSQL_PORT = 3306;
48
49         /**
50          * SQLite default params.
51          */
52         const SQLITE = 'sqlite';
53         const SQLITE_NAME = 'SQLite';
54         const SQLITE_PORT = null;
55
56         /**
57          * Default language for application.
58          */
59         const DEFAULT_LANGUAGE = 'en';
60
61         public function getDbNameByType($type) {
62                 switch($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;
67                 }
68                 return $dbName;
69         }
70
71         public function getPostgreSQLType() {
72                 return self::PGSQL;
73         }
74
75         public function getMySQLType() {
76                 return self::MYSQL;
77         }
78
79         public function getSQLiteType() {
80                 return self::SQLITE;
81         }
82
83         public function isPostgreSQLType($type) {
84                 return ($type === self::PGSQL);
85         }
86
87         public function isMySQLType($type) {
88                 return ($type === self::MYSQL);
89         }
90
91         public function isSQLiteType($type) {
92                 return ($type === self::SQLITE);
93         }
94
95         public function getLanguage() {
96                 return $this->Application->Parameters['language'];
97         }
98
99         public function setLanguage($language) {
100                 
101         }
102
103         /**
104          * Saving application configuration.
105          * 
106          * @access public
107          * @param array $config structure of config file params
108          * @return boolean true if config save is successfully, false if config save is failure
109          */
110         public function setApplicationConfig(array $config) {
111                 $cfgFile = Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf');
112                 return ($this->Application->getModule('misc')->writeINIFile($cfgFile, $config) != false);
113         }
114
115         /**
116          * Getting application configuration.
117          * 
118          * @access public
119          * @return array application configuration
120          */
121         public static function getApplicationConfig() {
122                 $cfgFile = Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf');
123                 return Miscellaneous::parseINIFile($cfgFile);
124         }
125
126         /**
127          * Checking if application configuration file exists.
128          * 
129          * @access public
130          * @return boolean true if file exists, otherwise false
131          */
132         public function isApplicationConfig() {
133                 return file_exists(Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf'));
134         }
135
136         /**
137          * Saving user to users configuration file.
138          *
139          * NOTE!
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.
143          *
144          * TODO: Support for more than one user setting on webGUI.
145          *
146          * @access public
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
152          */
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();
157                 }
158
159                 $users = $this->isUsersConfig() === true ? file($usersFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) : array();
160                 $userExists = false;
161
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}";
166                                 $userExists = true;
167                                 break;
168                         }
169                 }
170
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) {
175                                         unset($users[$j]);
176                                         break;
177                                 }
178                         }
179                 }
180
181                 // add new user if does not exist
182                 if($userExists === false) {
183                         array_push($users, "{$user}:{$password}");
184                 }
185
186                 $usersToFile = implode("\n", $users);
187                 $result = file_put_contents($usersFile, $usersToFile) !== false;
188                 return $result;
189         }
190
191         /**
192          * Checking if users configuration file exists.
193          *
194          * @access public
195          * @return boolean true if file exists, otherwise false
196          */
197         public function isUsersConfig() {
198                 return file_exists(Prado::getPathOfNamespace(self::USERS_FILE, '.users'));
199         }
200
201         /**
202          * Clear all content of users file.
203          *
204          * @access private
205          * @return boolean true if file cleared successfully, otherwise false
206          */
207         private function clearUsersConfig() {
208                 $usersFile = Prado::getPathOfNamespace(self::USERS_FILE, '.users');
209                 $result = file_put_contents($usersFile, '') !== false;
210                 return $result;
211         }
212 }
213 ?>