]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Pages/ConfigurationWizard.php
f541cbeec221ea127d56bee09bc19f4522d84b4f
[bacula/bacula] / gui / baculum / protected / Pages / ConfigurationWizard.php
1 <?php
2 /*
3  * Bacula(R) - 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 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('System.Web.UI.ActiveControls.TActiveDropDownList');
24 Prado::using('System.Web.UI.ActiveControls.TActiveTextBox');
25 Prado::using('System.Web.UI.ActiveControls.TActivePanel');
26 Prado::using('System.Web.UI.ActiveControls.TActiveLabel');
27 Prado::using('System.Web.UI.ActiveControls.TActiveButton');
28 Prado::using('System.Web.UI.ActiveControls.TActiveImage');
29 Prado::using('System.Web.UI.ActiveControls.TActiveTableCell');
30
31 class ConfigurationWizard extends BaculumPage
32 {
33         public $firstRun;
34         public $applicationConfig;
35         public $userPattern;
36
37         const DEFAULT_DB_NAME = 'bacula';
38         const DEFAULT_DB_LOGIN = 'bacula';
39         const DEFAULT_BCONSOLE_BIN = '/usr/sbin/bconsole';
40         const DEFAULT_BCONSOLE_CONF = '/etc/bacula/bconsole.conf';
41         const DEFAULT_BCONSOLE_CONF_CUSTOM = '/etc/bacula/bconsole-{user}.conf';
42
43         public function onInit($param) {
44                 parent::onInit($param);
45                 $this->Lang->SelectedValue = $_SESSION['language'];
46                 $config = $this->getModule('configuration');
47                 $this->firstRun = !$config->isApplicationConfig();
48                 $this->applicationConfig = $config->getApplicationConfig();
49                 $this->userPattern = $config->getUserPattern();
50                 if($this->firstRun === false && $this->User->getIsAdmin() === false) {
51                         die('Access denied.');
52                 }
53         }
54
55         public function onLoad($param) {
56                 parent::onLoad($param);
57                 $this->Licence->Text = $this->getModule('misc')->getLicence();
58                 $this->Port->setViewState('port', $this->Port->Text);
59                 if(!$this->IsPostBack && !$this->IsCallBack) {
60                         if($this->firstRun === true) {
61                                 $this->DBName->Text = self::DEFAULT_DB_NAME;
62                                 $this->Login->Text = self::DEFAULT_DB_LOGIN;
63                                 $this->BconsolePath->Text = self::DEFAULT_BCONSOLE_BIN;
64                                 $this->BconsoleConfigPath->Text = self::DEFAULT_BCONSOLE_CONF;
65                                 $this->BconsoleConfigCustomPath->Text = self::DEFAULT_BCONSOLE_CONF_CUSTOM;
66                         } else {
67                                 $this->DBType->SelectedValue = $this->getPage()->applicationConfig['db']['type'];
68                                 $this->DBName->Text = $this->applicationConfig['db']['name'];
69                                 $this->Login->Text = $this->applicationConfig['db']['login'];
70                                 $this->Password->Text = $this->applicationConfig['db']['password'];
71                                 $this->IP->Text = $this->applicationConfig['db']['ip_addr'];
72                                 $this->Port->Text = $this->applicationConfig['db']['port'];
73                                 $this->Port->setViewState('port', $this->applicationConfig['db']['port']);
74                                 $this->DBPath->Text = $this->applicationConfig['db']['path'];
75                                 $this->BconsolePath->Text = $this->applicationConfig['bconsole']['bin_path'];
76                                 $this->BconsoleConfigPath->Text = $this->applicationConfig['bconsole']['cfg_path'];
77                                 $this->BconsoleConfigCustomPath->Text = array_key_exists('cfg_custom_path', $this->applicationConfig['bconsole']) ? $this->applicationConfig['bconsole']['cfg_custom_path'] : self::DEFAULT_BCONSOLE_CONF_CUSTOM;
78                                 $this->UseSudo->Checked = $this->getPage()->applicationConfig['bconsole']['use_sudo'] == 1;
79                                 $this->PanelLogin->Text = $this->applicationConfig['baculum']['login'];
80                                 $this->PanelPassword->Text = $this->applicationConfig['baculum']['password'];
81                                 $this->RetypePanelPassword->Text = $this->applicationConfig['baculum']['password'];
82                         }
83                 }
84         }
85
86         public function NextStep($sender, $param) {
87         }
88         
89         public function PreviousStep($sender, $param) {
90         }
91
92         public function wizardStop($sender, $param) {
93                 $this->goToDefaultPage();
94         }
95
96         public function wizardCompleted() {
97                 $cfgData = array('db' => array(), 'bconsole' => array(), 'baculum' => array());
98                 $cfgData['db']['type'] = $this->DBType->SelectedValue;
99                 $cfgData['db']['name'] = $this->DBName->Text;
100                 $cfgData['db']['login'] = $this->Login->Text;
101                 $cfgData['db']['password'] = $this->Password->Text;
102                 $cfgData['db']['ip_addr'] = $this->IP->Text;
103                 $cfgData['db']['port'] = $this->Port->Text;
104                 $cfgData['db']['path'] = $this->Application->getModule('configuration')->isSQLiteType($cfgData['db']['type']) ? $this->DBPath->Text : '';
105                 $cfgData['bconsole']['bin_path'] = $this->BconsolePath->Text;
106                 $cfgData['bconsole']['cfg_path'] = $this->BconsoleConfigPath->Text;
107                 $cfgData['bconsole']['cfg_custom_path'] = $this->BconsoleConfigCustomPath->Text;
108                 $cfgData['bconsole']['use_sudo'] = (integer)($this->UseSudo->Checked === true);
109                 $cfgData['baculum']['login'] = $this->PanelLogin->Text;
110                 $cfgData['baculum']['password'] = $this->PanelPassword->Text;
111                 $cfgData['baculum']['debug'] = isset($this->applicationConfig['baculum']['debug']) ? $this->applicationConfig['baculum']['debug'] : "0";
112                 $cfgData['baculum']['lang'] = $_SESSION['language'];
113                 $ret = $this->getModule('configuration')->setApplicationConfig($cfgData);
114                 if($ret === true) {
115                         if($this->getModule('configuration')->isUsersConfig() === true) { // version with users config file, so next is try to auto-login
116                                 $previousUser = ($this->firstRun === false) ? $this->applicationConfig['baculum']['login'] : null;
117                                 $this->getModule('configuration')->setUsersConfig($cfgData['baculum']['login'], $cfgData['baculum']['password'], $this->firstRun, $previousUser);
118
119                                 // Automatic login after finish wizard.
120                                 $http_protocol = isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? 'https' : 'http';
121                                 $this->getModule('configuration')->switchToUser($http_protocol, $_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'], $cfgData['baculum']['login'], $cfgData['baculum']['password']);
122                                 exit();
123                         } else { // standard version (user defined auth method)
124                                 $this->goToDefaultPage();
125                         }
126                 }
127         }
128
129         public function setDBType($sender, $param) {
130                 $db = $this->DBType->SelectedValue;
131                 $this->setLogin($db);
132                 $this->setPassword($db);
133                 $this->setIP($db);
134                 $this->setDefaultPort($db);
135                 $this->setDBPath($db);
136         }
137
138         public function setLogin($db) {
139                 $this->Login->Enabled = ($this->getModule('configuration')->isSQLiteType($db) === false);
140         }
141
142         public function setPassword($db) {
143                 $this->Password->Enabled = ($this->getModule('configuration')->isSQLiteType($db) === false);
144         }
145
146         public function setIP($db) {
147                 $this->IP->Enabled = ($this->getModule('configuration')->isSQLiteType($db) === false);
148         }
149
150         public function setDefaultPort($db) {
151                 $configuration = $this->Application->getModule('configuration');
152                 $port = null;
153                 if($configuration->isPostgreSQLType($db) === true) {
154                         $port = $configuration::PGSQL_PORT;
155                 } elseif($configuration->isMySQLType($db) === true) {
156                         $port = $configuration::MYSQL_PORT;
157                 } elseif($configuration->isSQLiteType($db) === true) {
158                         $port = $configuration::SQLITE_PORT;
159                 }
160
161                 $prevPort = $this->Port->getViewState('port');
162
163                 if(is_null($port)) {
164                         $this->Port->Text = '';
165                         $this->Port->Enabled = false;
166                 } else {
167                         $this->Port->Enabled = true;
168                         $this->Port->Text = (empty($prevPort)) ? $port : $prevPort;
169                 }
170                 $this->Port->setViewState('port', '');
171         }
172
173         public function setDBPath($db) {
174                 if($this->getModule('configuration')->isSQLiteType($db) === true) {
175                         $this->DBPath->Enabled = true;
176                         $this->DBPathField->Display = 'Fixed';
177                 } else {
178                         $this->DBPath->Enabled = false;
179                         $this->DBPathField->Display = 'Hidden';
180                 }
181         }
182
183         public function setLang($sender, $param) {
184                 $_SESSION['language'] = $sender->SelectedValue;
185                 $this->goToPage('ConfigurationWizard');
186         }
187
188         public function validateAdministratorPassword($sender, $param) {
189                 $sender->Display = ($this->RetypePasswordRequireValidator->IsValid === true && $this->RetypePasswordRegexpValidator->IsValid === true) ? 'Dynamic' : 'None';
190                 $param->IsValid = ($param->Value === $this->PanelPassword->Text);
191         }
192
193          public function renderPanel($sender, $param) {
194                 $this->LoginValidator->Display = ($this->Login->Enabled === true) ? 'Dynamic' : 'None';
195                 $this->PortValidator->Display = ($this->Port->Enabled === true) ? 'Dynamic' : 'None';
196                 $this->IPValidator->Display = ($this->IP->Enabled === true) ? 'Dynamic' : 'None';
197                 $this->DBPathValidator->Display = ($this->DBPath->Enabled === true) ? 'Dynamic' : 'None';
198                 $this->DbTestResultOk->Display = 'None';
199                 $this->DbTestResultErr->Display = 'None';
200                 $this->Step2Content->render($param->NewWriter);
201         }
202         
203         public function connectionDBTest($sender, $param) {
204                 $validation = false;
205                 $configuration = $this->getModule('configuration');
206                 $dbParams = array();
207                 $dbParams['type'] = $this->DBType->SelectedValue;
208                 if($configuration->isMySQLType($dbParams['type']) === true || $configuration->isPostgreSQLType($dbParams['type']) === true) {
209                         $dbParams['name'] = $this->DBName->Text;
210                         $dbParams['login'] = $this->Login->Text;
211                         $dbParams['password'] = $this->Password->Text;
212                         $dbParams['host'] = $this->IP->Text;
213                         $dbParams['port'] = $this->Port->Text;
214                         $validation = true;
215                 } elseif($configuration->isSQLiteType($dbParams['type']) === true && !empty($this->DBPath->Text)) {
216                         $dbParams['path'] = $this->DBPath->Text;
217                         $validation = true;
218                 }
219                 
220                 $isValidate = ($validation === true) ? $this->getModule('db')->testDbConnection($dbParams) : false;
221                 $this->DbTestResultOk->Display = ($isValidate === true) ? 'Dynamic' : 'None';
222                 $this->DbTestResultErr->Display = ($isValidate === false) ? 'Dynamic' : 'None';
223                 $this->Step2Content->render($param->NewWriter);
224
225         }
226
227         public function connectionBconsoleTest($sender, $param) {
228                 $result = $this->getModule('bconsole')->testBconsoleCommand(array('version'), $this->BconsolePath->Text, $this->BconsoleConfigPath->Text, $this->UseSudo->Checked)->exitcode;
229                 $isValidate = ($result === 0);
230                 $this->BconsoleTestResultOk->Display = ($isValidate === true) ? 'Dynamic' : 'None';
231                 $this->BconsoleTestResultErr->Display = ($isValidate === false) ? 'Dynamic' : 'None';
232         }
233 }
234 ?>