]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Pages/ConfigurationWizard.php
baculum: Update copyright dates
[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-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('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 = $_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                 $cfgData['baculum']['lang'] = $_SESSION['language'];
107                 $ret = $this->getModule('configuration')->setApplicationConfig($cfgData);
108                 if($ret === true) {
109                         if($this->getModule('configuration')->isUsersConfig() === true) { // version with users config file, so next is try to auto-login
110                                 $previousUser = ($this->firstRun === false) ? $this->applicationConfig['baculum']['login'] : null;
111                                 $this->getModule('configuration')->setUsersConfig($cfgData['baculum']['login'], $cfgData['baculum']['password'], $this->firstRun, $previousUser);
112                                 // Automatic login after finish wizard.
113                                 $http_protocol = isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? 'https' : 'http';
114                                 $urlPrefix = $this->Application->getModule('friendly-url')->getUrlPrefix();
115                                 $location = sprintf("%s://%s:%s@%s:%d%s", $http_protocol, $cfgData['baculum']['login'], $cfgData['baculum']['password'], $_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'], $urlPrefix);
116                                 header("Location: $location");
117                                 exit();
118                         } else { // standard version (user defined auth method)
119                                 $this->goToDefaultPage();
120                         }
121                 }
122         }
123
124         public function setDBType($sender, $param) {
125                 $db = $this->DBType->SelectedValue;
126                 $this->setLogin($db);
127                 $this->setPassword($db);
128                 $this->setIP($db);
129                 $this->setDefaultPort($db);
130                 $this->setDBPath($db);
131         }
132
133         public function setLogin($db) {
134                 $this->Login->Enabled = ($this->getModule('configuration')->isSQLiteType($db) === false);
135         }
136
137         public function setPassword($db) {
138                 $this->Password->Enabled = ($this->getModule('configuration')->isSQLiteType($db) === false);
139         }
140
141         public function setIP($db) {
142                 $this->IP->Enabled = ($this->getModule('configuration')->isSQLiteType($db) === false);
143         }
144
145         public function setDefaultPort($db) {
146                 $configuration = $this->Application->getModule('configuration');
147                 $port = null;
148                 if($configuration->isPostgreSQLType($db) === true) {
149                         $port = $configuration::PGSQL_PORT;
150                 } elseif($configuration->isMySQLType($db) === true) {
151                         $port = $configuration::MYSQL_PORT;
152                 } elseif($configuration->isSQLiteType($db) === true) {
153                         $port = $configuration::SQLITE_PORT;
154                 }
155
156                 $prevPort = $this->Port->getViewState('port');
157
158                 if(is_null($port)) {
159                         $this->Port->Text = '';
160                         $this->Port->Enabled = false;
161                 } else {
162                         $this->Port->Enabled = true;
163                         $this->Port->Text = (empty($prevPort)) ? $port : $prevPort;
164                 }
165                 $this->Port->setViewState('port', '');
166         }
167
168         public function setDBPath($db) {
169                 if($this->getModule('configuration')->isSQLiteType($db) === true) {
170                         $this->DBPath->Enabled = true;
171                         $this->DBPathField->Display = 'Fixed';
172                 } else {
173                         $this->DBPath->Enabled = false;
174                         $this->DBPathField->Display = 'Hidden';
175                 }
176         }
177
178         public function setLang($sender, $param) {
179                 $_SESSION['language'] = $sender->SelectedValue;
180                 $this->goToPage('ConfigurationWizard');
181         }
182
183         public function validateAdministratorPassword($sender, $param) {
184                 $sender->Display = ($this->RetypePasswordRequireValidator->IsValid === true && $this->RetypePasswordRegexpValidator->IsValid === true) ? 'Dynamic' : 'None';
185                 $param->IsValid = ($param->Value === $this->PanelPassword->Text);
186         }
187
188          public function renderPanel($sender, $param) {
189                 $this->LoginValidator->Display = ($this->Login->Enabled === true) ? 'Dynamic' : 'None';
190                 $this->PortValidator->Display = ($this->Port->Enabled === true) ? 'Dynamic' : 'None';
191                 $this->IPValidator->Display = ($this->IP->Enabled === true) ? 'Dynamic' : 'None';
192                 $this->DBPathValidator->Display = ($this->DBPath->Enabled === true) ? 'Dynamic' : 'None';
193                 $this->DbTestResultOk->Display = 'None';
194                 $this->DbTestResultErr->Display = 'None';
195                 $this->Step2Content->render($param->NewWriter);
196         }
197         
198         public function connectionDBTest($sender, $param) {
199                 $validation = false;
200                 $configuration = $this->getModule('configuration');
201                 $dbParams = array();
202                 $dbParams['type'] = $this->DBType->SelectedValue;
203                 if($configuration->isMySQLType($dbParams['type']) === true || $configuration->isPostgreSQLType($dbParams['type']) === true) {
204                         $dbParams['name'] = $this->DBName->Text;
205                         $dbParams['login'] = $this->Login->Text;
206                         $dbParams['password'] = $this->Password->Text;
207                         $dbParams['host'] = $this->IP->Text;
208                         $dbParams['port'] = $this->Port->Text;
209                         $validation = true;
210                 } elseif($configuration->isSQLiteType($dbParams['type']) === true && !empty($this->DBPath->Text)) {
211                         $dbParams['path'] = $this->DBPath->Text;
212                         $validation = true;
213                 }
214                 
215                 $isValidate = ($validation === true) ? $this->getModule('db')->testDbConnection($dbParams) : false;
216                 $this->DbTestResultOk->Display = ($isValidate === true) ? 'Dynamic' : 'None';
217                 $this->DbTestResultErr->Display = ($isValidate === false) ? 'Dynamic' : 'None';
218                 $this->Step2Content->render($param->NewWriter);
219
220         }
221
222         public function connectionBconsoleTest($sender, $param) {
223                 $result = $this->getModule('bconsole')->testBconsoleCommand(array('version'), $this->BconsolePath->Text, $this->BconsoleConfigPath->Text, $this->UseSudo->Checked)->exitcode;
224                 $isValidate = ($result === 0);
225                 $this->BconsoleTestResultOk->Display = ($isValidate === true) ? 'Dynamic' : 'None';
226                 $this->BconsoleTestResultErr->Display = ($isValidate === false) ? 'Dynamic' : 'None';
227                 $this->Step4Content->render($param->NewWriter);
228         }
229 }
230 ?>