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