]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Web/Class/WebConfig.php
baculum: Fix multiple directors support
[bacula/bacula] / gui / baculum / protected / Web / Class / WebConfig.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2016 Kern Sibbald
7  *
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.
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  * This notice must be preserved when any source code is
18  * conveyed and/or propagated.
19  *
20  * Bacula(R) is a registered trademark of Kern Sibbald.
21  */
22
23 Prado::using('Application.Common.Class.ConfigFileModule');
24
25 /**
26  * Manage webGUI configuration.
27  * Module is responsible for get/set webGUI config data.
28  *
29  * @author Marcin Haba <marcin.haba@bacula.pl>
30  */
31 class WebConfig extends ConfigFileModule {
32
33         /**
34          * Web config file path
35          */
36         const CONFIG_FILE_PATH = 'Application.Web.Config.settings';
37
38         /**
39          * Web config file format
40          */
41         const CONFIG_FILE_FORMAT = 'ini';
42
43         /**
44          * Default application language
45          */
46         const DEFAULT_LANGUAGE = 'en';
47
48         /**
49          * These options are obligatory for web config.
50          */
51         private $required_options = array(
52                 'baculum' => array('login', 'password', 'debug', 'lang')
53         );
54
55         /**
56          * Get (read) web config.
57          *
58          * @access public
59          * @param string $section config section name
60          * @return array config
61          */
62         public function getConfig($section = null) {
63                 $config = $this->readConfig(self::CONFIG_FILE_PATH, self::CONFIG_FILE_FORMAT);
64                 if ($this->validateConfig($config) === true) {
65                         if (!is_null($section)) {
66                                 $config = array_key_exists($section, $this->required_options) ? $config[$section] : array();
67                         }
68                 } else {
69                         $config = array();
70                 }
71                 return $config;
72         }
73
74         /**
75          * Set (save) web config.
76          *
77          * @access public
78          * @param array $config config
79          * @return boolean true if config saved successfully, otherwise false
80          */
81         public function setConfig(array $config) {
82                 $result = false;
83                 if ($this->validateConfig($config) === true) {
84                         $result = $this->writeConfig($config, self::CONFIG_FILE_PATH, self::CONFIG_FILE_FORMAT);
85                 }
86                 return $result;
87         }
88         
89         /**
90          * Validate web config.
91          * Config validation should be used as early as config data is available.
92          * Validation is done in read/write config methods.
93          *
94          * @access private
95          * @param array $config config
96          * @return boolean true if config valid, otherwise false
97          */
98         private function validateConfig(array $config = array()) {
99                 $is_valid = $this->isConfigValid($this->required_options, $config, self::CONFIG_FILE_FORMAT, self::CONFIG_FILE_PATH);
100                 return $is_valid;
101         }
102
103         /**
104          * Get web administrator name.
105          * Web interface supports one admin and many normal users.
106          *
107          * @return string web admin name
108          */
109         public function getWebAdmin() {
110                 $config = $this->getConfig();
111                 $web_admin = $config['baculum']['login'];
112                 return $web_admin;
113         }
114
115         /**
116          * Get application language short name.
117          * If no language set then returned is default language.
118          *
119          * @access public
120          * @return string lanuage short name
121          */
122         public function getLanguage() {
123                 $language = null;
124                 $config = $this->getConfig();
125                 if (array_key_exists('baculum', $config) && array_key_exists('lang', $config['baculum'])) {
126                         $language = $config['baculum']['lang'];
127                 }
128                 if (is_null($language)) {
129                         $language = self::DEFAULT_LANGUAGE;
130                 }
131                 return $language;
132         }
133 }
134 ?>