]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Class/ConfigurationManager.php
baculum: Update copyright dates
[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-2015 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                 $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'];
101                         }
102                 }
103                 return $language;
104         }
105
106         public function setLanguage($language) {
107                 
108         }
109
110         /**
111          * Saving application configuration.
112          * 
113          * @access public
114          * @param array $config structure of config file params
115          * @return boolean true if config save is successfully, false if config save is failure
116          */
117         public function setApplicationConfig(array $config) {
118                 $cfgFile = Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf');
119                 return ($this->Application->getModule('misc')->writeINIFile($cfgFile, $config) != false);
120         }
121
122         /**
123          * Getting application configuration.
124          * 
125          * @access public
126          * @return array application configuration
127          */
128         public static function getApplicationConfig() {
129                 $cfgFile = Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf');
130                 return Miscellaneous::parseINIFile($cfgFile);
131         }
132
133         /**
134          * Checking if application configuration file exists.
135          * 
136          * @access public
137          * @return boolean true if file exists, otherwise false
138          */
139         public function isApplicationConfig() {
140                 return file_exists(Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf'));
141         }
142
143         /**
144          * Saving user to users configuration file.
145          *
146          * NOTE!
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.
150          *
151          * TODO: Support for more than one user setting on webGUI.
152          *
153          * @access public
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
159          */
160         public function setUsersConfig($user, $password, $firstUsage = false, $oldUser = null) {
161                 $usersFile = Prado::getPathOfNamespace(self::USERS_FILE, '.users');
162                 if($firstUsage === true) {
163                         $this->clearUsersConfig();
164                 }
165
166                 $users = $this->isUsersConfig() === true ? file($usersFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) : array();
167                 $userExists = false;
168
169                 for($i = 0; $i < count($users); $i++) {
170                         // checking if user already exist in configuration file and if exist then update password
171                         if(preg_match("/^{$user}\:/", $users[$i]) === 1) {
172                                 $users[$i] = "{$user}:{$password}";
173                                 $userExists = true;
174                                 break;
175                         }
176                 }
177
178                 if(!is_null($oldUser) && $oldUser !== $user) {
179                         // delete old username with password from configuration file
180                         for($j = 0; $j < count($users); $j++) {
181                                 if(preg_match("/^{$oldUser}\:/", $users[$j]) === 1) {
182                                         unset($users[$j]);
183                                         break;
184                                 }
185                         }
186                 }
187
188                 // add new user if does not exist
189                 if($userExists === false) {
190                         array_push($users, "{$user}:{$password}");
191                 }
192
193                 $usersToFile = implode("\n", $users);
194                 $result = file_put_contents($usersFile, $usersToFile) !== false;
195                 return $result;
196         }
197
198         /**
199          * Checking if users configuration file exists.
200          *
201          * @access public
202          * @return boolean true if file exists, otherwise false
203          */
204         public function isUsersConfig() {
205                 return file_exists(Prado::getPathOfNamespace(self::USERS_FILE, '.users'));
206         }
207
208         /**
209          * Clear all content of users file.
210          *
211          * @access private
212          * @return boolean true if file cleared successfully, otherwise false
213          */
214         private function clearUsersConfig() {
215                 $usersFile = Prado::getPathOfNamespace(self::USERS_FILE, '.users');
216                 $result = file_put_contents($usersFile, '') !== false;
217                 return $result;
218         }
219 }
220 ?>