]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Common/Portlets/NewHost.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / protected / Common / Portlets / NewHost.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2017 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.TActiveLabel');
24 Prado::using('System.Web.UI.ActiveControls.TActiveButton');
25 Prado::using('System.Web.UI.ActiveControls.TActiveTextBox');
26 Prado::using('System.Web.UI.TTemplateControl');
27 Prado::using('Application.Common.Class.OAuth2');
28 Prado::using('Application.Common.Portlets.PortletTemplate');
29
30 class NewHost extends PortletTemplate {
31
32         private $error = false;
33
34         private $show_buttons = true;
35
36         private $client_mode = false;
37
38         private $force_host_name;
39
40         private $api_required;
41
42         public function onLoad($param) {
43                 $host_name = $this->getForceHostName();
44                 if (!empty($host_name)) {
45                         $this->APIHostName->Text = $host_name;
46                         $this->APIHostName->setReadOnly(true);
47                 }
48         }
49
50         public function connectionAPITest($sender, $param) {
51                 $host = $this->APIAddress->Text;
52                 if (empty($host)) {
53                         $host = false;
54                 }
55                 $host_params = array(
56                         'protocol' => $this->APIProtocol->SelectedValue,
57                         'address' => $this->APIAddress->Text,
58                         'port' => $this->APIPort->Text,
59                         'url_prefix' => ''
60                 );
61
62                 if ($this->AuthBasic->Checked) {
63                         $host_params['auth_type'] = 'basic';
64                         $host_params['login'] = $this->APIBasicLogin->Text;
65                         $host_params['password'] = $this->APIBasicPassword->Text;
66                 } elseif ($this->AuthOAuth2->Checked) {
67                         $host_params['auth_type'] = 'oauth2';
68                         $host_params['client_id'] = $this->APIOAuth2ClientId->Text;
69                         $host_params['client_secret'] = $this->APIOAuth2ClientSecret->Text;
70                         $host_params['redirect_uri'] = $this->APIOAuth2RedirectURI->Text;
71                         $host_params['scope'] = $this->APIOAuth2Scope->Text;
72                 }
73                 $api = $this->getModule('api');
74
75                 // Catalog test
76                 $api->setHostParams($host, $host_params);
77                 $catalog = $api->get(array('catalog'), $host, false);
78
79                 // Console test
80                 $api->setHostParams($host, $host_params);
81                 $console = $api->set(array('console'), array('version'), $host, false);
82
83                 // Config test
84                 $api->setHostParams($host, $host_params);
85                 $config = $api->get(array('config'), $host, false);
86
87                 $is_catalog = (is_object($catalog) && $catalog->error === 0);
88                 $is_console = (is_object($console) && $console->error === 0);
89                 $is_config = (is_object($config) && $config->error === 0);
90
91                 $status_ok = true;
92                 if (in_array('catalog', $this->api_required)) {
93                         $status_ok = $is_catalog;
94                 }
95                 if ($status_ok && in_array('console', $this->api_required)) {
96                         $status_ok = $is_console;
97                 }
98                 if ($status_ok && in_array('config', $this->api_required)) {
99                         $status_ok = $is_config;
100                 }
101
102                 $this->APITestResultOk->Display = ($status_ok === true) ? 'Dynamic' : 'None';
103                 $this->APITestResultErr->Display = ($status_ok === false) ? 'Dynamic' : 'None';
104                 $this->APICatalogSupportYes->Display = ($is_catalog === true) ? 'Dynamic' : 'None';
105                 $this->APICatalogSupportNo->Display = ($is_catalog === false) ? 'Dynamic' : 'None';
106                 $this->APIConsoleSupportYes->Display = ($is_console === true) ? 'Dynamic' : 'None';
107                 $this->APIConsoleSupportNo->Display = ($is_console === false) ? 'Dynamic' : 'None';
108                 $this->APIConfigSupportYes->Display = ($is_config === true) ? 'Dynamic' : 'None';
109                 $this->APIConfigSupportNo->Display = ($is_config === false) ? 'Dynamic' : 'None';
110         }
111
112         public function addNewHost($sender, $param) {
113                 $cfg_host = array(
114                         'auth_type' => '',
115                         'login' => '',
116                         'password' => '',
117                         'client_id' => '',
118                         'client_secret' => '',
119                         'redirect_uri' => '',
120                         'scope' => ''
121                 );
122                 $cfg_host['protocol'] = $this->APIProtocol->Text;
123                 $cfg_host['address'] = $this->APIAddress->Text;
124                 $cfg_host['port'] = $this->APIPort->Text;
125                 $cfg_host['url_prefix'] = '';
126                 $cfg_host['login'] = $this->APIBasicLogin->Text;
127                 $cfg_host['password'] = $this->APIBasicPassword->Text;
128                 if ($this->AuthBasic->Checked == true) {
129                         $cfg_host['auth_type'] = 'basic';
130                         $cfg_host['login'] = $this->APIBasicLogin->Text;
131                         $cfg_host['password'] = $this->APIBasicPassword->Text;
132                 } elseif($this->AuthOAuth2->Checked == true) {
133                         $cfg_host['auth_type'] = 'oauth2';
134                         $cfg_host['client_id'] = $this->APIOAuth2ClientId->Text;
135                         $cfg_host['client_secret'] = $this->APIOAuth2ClientSecret->Text;
136                         $cfg_host['redirect_uri'] = $this->APIOAuth2RedirectURI->Text;
137                         $cfg_host['scope'] = $this->APIOAuth2Scope->Text;
138                 }
139                 $config = $this->getModule('host_config')->getConfig();
140                 $this->NewHostAddOk->Display = 'None';
141                 $this->NewHostAddError->Display = 'None';
142                 $this->NewHostAddExists->Display = 'None';
143                 $host_name = trim($this->APIHostName->Text);
144                 if (empty($host_name)) {
145                         $host_name = $cfg_host['address'];
146                 }
147                 if (!array_key_exists($host_name, $config)) {
148                         $config[$host_name] = $cfg_host;
149                         $res = $this->getModule('host_config')->setConfig($config);
150                         if ($res === true) {
151                                 $this->APIAddress->Text = '';
152                                 $this->APIPort->Text = '9096';
153                                 $this->APIBasicLogin->Text = '';
154                                 $this->APIBasicPassword->Text = '';
155                                 $this->APIHostName->Text = '';
156                                 $this->NewHostAddOk->Display = 'Dynamic';
157                                 $this->APITestResultOk->Display = 'None';
158                                 $this->APITestResultErr->Display = 'None';
159                                 $this->APICatalogSupportYes->Display = 'None';
160                                 $this->APICatalogSupportNo->Display = 'None';
161                                 $this->APIConsoleSupportYes->Display = 'None';
162                                 $this->APIConsoleSupportNo->Display = 'None';
163                                 $this->APIConfigSupportYes->Display = 'None';
164                                 $this->APIConfigSupportNo->Display = 'None';
165                                 $this->error = true;
166                         } else {
167                                 $this->NewHostAddError->Display = 'Dynamic';
168                         }
169                 } else {
170                         $this->NewHostAddExists->Display = 'Dynamic';
171                 }
172                 $this->onCallback($param);
173         }
174
175         public function setForceHostName($host_name) {
176                 $this->force_host_name = $host_name;
177         }
178
179         public function getForceHostName() {
180                 return $this->force_host_name;
181         }
182
183         public function setShowButtons($show) {
184                 $show = TPropertyValue::ensureBoolean($show);
185                 $this->show_buttons = $show;
186         }
187
188         public function getShowButtons() {
189                 return $this->show_buttons;
190         }
191
192         public function bubbleEvent($sender, $param) {
193                 if ($param instanceof TCommandEventParameter) {
194                         if ($this->error === true) {
195                                 $this->raiseBubbleEvent($this,$param);
196                         }
197                         return true;
198                 } else {
199                         return false;
200                 }
201         }
202
203         public function setAPIRequired($api_required) {
204                 $this->api_required = explode('|', $api_required);
205         }
206
207         public function getAPIRequired() {
208                 return $this->api_required;
209         }
210
211         public function setClientMode($client_mode) {
212                 $client_mode = TPropertyValue::ensureBoolean($client_mode);
213                 $this->client_mode = $client_mode;
214         }
215
216         public function getClientMode() {
217                 return $this->client_mode;
218         }
219
220         public function onCallback($param) {
221                 $this->raiseEvent('OnCallback', $this, $param);
222         }
223
224 }