]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Pages/Home.php
baculum: Assign Baculum copyright to Kern Sibbald
[bacula/bacula] / gui / baculum / protected / Pages / Home.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('System.Web.UI.ActiveControls.TActiveButton');
24 Prado::using('System.Web.UI.ActiveControls.TActivePanel');
25 Prado::using('System.Web.UI.ActiveControls.TActiveDropDownList');
26 Prado::using('System.Web.UI.ActiveControls.TActiveCheckBox');
27 Prado::using('System.Web.UI.ActiveControls.TActiveLinkButton');
28
29 class Home extends BaculumPage
30 {
31         protected $app_config;
32
33         public $jobs;
34
35         public $openWindow = null;
36
37         public $initWindowId = null;
38
39         public $initElementId = null;
40
41         public $jobs_states = null;
42
43         public $dbtype = '';
44
45         public $windowIds = array('Storage', 'Client', 'Volume', 'Pool', 'Job', 'JobRun');
46
47         public $userPattern;
48
49         public function onPreInit($param) {
50                 parent::onPreInit($param);
51                 if (!$this->IsPostBack && !$this->IsCallBack) {
52                         /**
53                          * Reload page if refresh_page written in session.
54                          * Useful in login and re-login (back from wizards).
55                          * Otherwise HTTP Basic login prompt occurs.
56                          */
57                         if (array_key_exists('refresh_page', $_SESSION)) {
58                                 $refresh_page = $_SESSION['refresh_page'];
59                                 header('Location: ' . $refresh_page);
60                                 unset($_SESSION['refresh_page']);
61                                 exit();
62                         }
63                 }
64         }
65
66         public function onInit($param) {
67                 parent::onInit($param);
68                 $this->Application->getModule('users')->loginUser();
69
70                 if (!$this->IsPostBack && !$this->IsCallBack) {
71                         $this->getModule('api')->initSessionCache(true);
72                 }
73
74                 $isConfigExists = $this->getModule('configuration')->isApplicationConfig();
75                 if($isConfigExists === false) {
76                         $this->goToPage('ConfigurationWizard');
77                 }
78
79                 $this->userPattern = $this->getModule('configuration')->getUserPattern();
80                 $this->app_config = $this->getModule('configuration')->getApplicationConfig();
81
82                 $this->Users->Visible = $this->User->getIsAdmin();
83                 $this->SettingsWizardBtn->Visible = $this->User->getIsAdmin();
84                 $this->PoolBtn->Visible = $this->User->getIsAdmin();
85                 $this->VolumeBtn->Visible = $this->User->getIsAdmin();
86                 $this->ClearBvfsCache->Visible = $this->User->getIsAdmin();
87                 $this->Logging->Visible = $this->User->getIsAdmin();
88                 $this->BconsoleCustomPath->Text = $this->app_config['bconsole']['cfg_custom_path'];
89
90                 if(!$this->IsPostBack && !$this->IsCallBack) {
91                         $this->Logging->Checked = $this->getModule('logging')->isDebugOn();
92                 }
93
94                 if(!$this->IsPostBack && !$this->IsCallBack) {
95                         $directors = $this->getModule('api')->get(array('directors'))->output;
96                         if(!array_key_exists('director', $_SESSION) || $directors[0] != $_SESSION['director']) {
97                                 $_SESSION['director'] = $directors[0];
98                         }
99                         $this->Director->dataSource = array_combine($directors, $directors);
100                         $this->Director->SelectedValue = $_SESSION['director'];
101                         $this->Director->dataBind();
102                         if ($this->User->getIsAdmin() === true) {
103                                 $this->dbtype = $this->getModule('configuration')->getDbNameByType($this->app_config['db']['type']);
104                         }
105                         $this->setJobsStates();
106                         $this->setJobs();
107                         $this->setClients();
108                         $this->setUsers();
109                         $this->setWindowOpen();
110                 }
111         }
112
113         public function restore($sender, $param) {
114                 $this->goToPage('RestoreWizard');
115         }
116
117         public function configuration($sender, $param) {
118                 $this->goToPage('ConfigurationWizard');
119         }
120
121         public function director($sender, $param) {
122                 $_SESSION['director'] = $this->Director->SelectedValue;
123         }
124
125         public function setDebug($sender, $param) {
126                 if($this->User->getIsAdmin() === true) {
127                         $this->getModule('logging')->enableDebug($this->Logging->Checked);
128                         $this->goToDefaultPage();
129                 }
130         }
131
132         public function clearBvfsCache($sender, $param) {
133                 if($this->User->getIsAdmin() === true) {
134                         $this->getModule('api')->set(array('bvfs', 'clear'), array());
135                 }
136         }
137
138         public function getJobs() {
139                 return json_encode($this->jobs);
140         }
141
142         public function setJobsStates() {
143                 $jobs_summary = array(
144                         'ok' => array(),
145                         'error' => array(),
146                         'warning' => array(),
147                         'cancel' => array(),
148                         'running' => array()
149                 );
150                 $job_types = $jobs_summary;
151                 $job_states = array();
152
153                 $misc = $this->getModule('misc');
154                 foreach($job_types as $type => $arr) {
155                         $states = $misc->getJobStatesByType($type);
156                         foreach($states as $state => $desc) {
157                                 $desc['type'] = $type;
158                                 $jobs_states[$state] = $desc;
159                         }
160                 }
161
162                 $this->jobs_states = json_encode($jobs_states);
163         }
164
165         public function setJobs() {
166                 $this->jobs = $this->getModule('api')->get(array('jobs'));
167                 $jobs = array('@' => Prado::localize('select job'));
168                 foreach($this->jobs->output as $key => $job) {
169                         $jobs[$job->name] = $job->name;
170                 }
171                 $this->Jobs->dataSource = $jobs;
172                 $this->Jobs->dataBind();
173         }
174
175         public function setClients() {
176                 $clients_obj = $this->getModule('api')->get(array('clients'));
177                 $clients = array('@' => Prado::localize('select client'));
178                 foreach($clients_obj->output as $key => $client) {
179                         $clients[$client->clientid] = $client->name;
180                 }
181                 $this->Clients->dataSource = $clients;
182                 $this->Clients->dataBind();
183         }
184
185         public function setUsers() {
186                 if($this->User->getIsAdmin() === true) {
187                         $allUsers = $this->getModule('configuration')->getAllUsers();
188                         $users = array_keys($allUsers);
189                         sort($users);
190                         $this->UsersList->dataSource = $users;
191                         $this->UsersList->dataBind();
192                 }
193         }
194
195         public function userAction($sender, $param) {
196                 if($this->User->getIsAdmin() === true) {
197                         list($action, $param, $value) = explode(';', $param->CallbackParameter, 3);
198                         switch($action) {
199                                 case 'newuser':
200                                 case 'chpwd': {
201                                                 $admin = false;
202                                                 $valid = true;
203                                                 if ($param === $this->app_config['baculum']['login']) {
204                                                         $this->app_config['baculum']['password'] = $value;
205                                                         $valid = $this->getModule('configuration')->setApplicationConfig($this->app_config);
206                                                         $admin = true;
207                                                 }
208                                                 if ($valid === true) {
209                                                         $this->getModule('configuration')->setUsersConfig($param, $value);
210                                                 }
211                                                 if ($admin === true) {
212                                                         // if admin password changed then try to auto-login by async request
213                                                         $http_protocol = isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? 'https' : 'http';
214                                                         $this->getModule('configuration')->switchToUser(
215                                                                 $http_protocol,
216                                                                 $_SERVER['SERVER_NAME'],
217                                                                 $_SERVER['SERVER_PORT'],
218                                                                 $param,
219                                                                 $value
220                                                         );
221                                                         exit();
222                                                 } else {
223                                                         // if normal user's password changed then update users grid
224                                                         $this->setUsers();
225                                                 }
226                                         }
227                                         break;
228                                 case 'rmuser': {
229                                                 if ($param != $this->User->getName()) {
230                                                         $this->getModule('configuration')->removeUser($param);
231                                                         $this->setUsers();
232                                                 }
233                                         break;
234                                         }
235                         }
236                 }
237         }
238
239         public function setWindowOpen() {
240                 if (isset($this->Request['open']) && in_array($this->Request['open'], $this->windowIds) && $this->Request['open'] != 'JobRun') {
241                         $btn = $this->Request['open'] . 'Btn';
242                         $this->openWindow = $this->{$btn}->ClientID;
243                         if (isset($this->Request['id']) && (is_numeric($this->Request['id']))) {
244                                 $this->initWindowId = $this->Request['open'];
245                                 $this->initElementId = $this->Request['id'];
246                         }
247                 }
248         }
249
250         public function logout($sender, $param) {
251                 $cfg = $this->getModule('configuration');
252                 $http_protocol = isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? 'https' : 'http';
253                 $fake_pwd = $cfg->getRandomString();
254                 $cfg->switchToUser($http_protocol, $_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'], $this->User->getName(), $fake_pwd);
255                 exit();
256         }
257 }
258 ?>