]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Web/Portlets/BaculaConfigDirectives.php
baculum: Fix numeric password setting bug reported by Heitor Faria
[bacula/bacula] / gui / baculum / protected / Web / Portlets / BaculaConfigDirectives.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.TActiveRepeater');
24 Prado::using('System.Web.UI.ActiveControls.TActiveLabel');
25 Prado::using('Application.Web.Portlets.BConditional');
26 Prado::using('Application.Web.Portlets.DirectiveListTemplate');
27 Prado::using('Application.Web.Portlets.DirectiveBoolean');
28 Prado::using('Application.Web.Portlets.DirectiveComboBox');
29 Prado::using('Application.Web.Portlets.DirectiveInteger');
30 Prado::using('Application.Web.Portlets.DirectiveText');
31 Prado::using('Application.Web.Portlets.DirectiveTimePeriod');
32 Prado::using('Application.Web.Portlets.DirectiveRunscript');
33 Prado::using('Application.Web.Portlets.DirectiveMessages');
34 Prado::using('Application.IO.TTextWriter');
35
36 class BaculaConfigDirectives extends DirectiveListTemplate {
37
38         const SHOW_ALL_DIRECTIVES = 'ShowAllDirectives';
39
40         private $show_all_directives = false;
41
42         public $resource_names = array();
43
44         private $directive_types = array(
45                 'DirectiveBoolean',
46                 'DirectiveComboBox',
47                 'DirectiveInteger',
48                 'DirectiveText',
49                 'DirectiveTimePeriod'
50         );
51
52         private $directive_list_types = array(
53                 'DirectiveFileSet',
54                 'DirectiveSchedule',
55                 'DirectiveMessages',
56                 'DirectiveRunscript'
57         );
58
59         private function getConfigData($host, array $parameters) {
60                 $default_params = array('config');
61                 $params = array_merge($default_params, $parameters);
62                 $result = $this->Application->getModule('api')->get($params, $host, false);
63                 $config = array();
64                 if (is_object($result) && $result->error === 0 && (is_object($result->output) || is_array($result->output))) {
65                         $config = $result->output;
66                 }
67                 return $config;
68         }
69
70         public function loadConfig() {
71                 $load_values = $this->getLoadValues();
72
73                 $host = $this->getHost();
74                 $component_type = $this->getComponentType();
75                 $component_name = $this->getComponentName();
76                 $resource_type = $this->getResourceType();
77                 $resource_name = $this->getResourceName();
78                 $directives = array();
79                 $parent_directives = array();
80                 $config = new stdClass;
81                 if ($load_values === true) {
82                         $config = $this->getConfigData($host, array(
83                                 $component_type,
84                                 $resource_type,
85                                 $resource_name
86                         ));
87                         if ($resource_type === 'Job' && property_exists($config, 'JobDefs')) {
88                                 $parent_directives = $this->getConfigData($host, array(
89                                         $component_type,
90                                         'JobDefs',
91                                         $config->JobDefs
92                                 ));
93                         }
94                 }
95                 $data_desc = $this->Application->getModule('data_desc');
96                 $resource_desc = $data_desc->getDescription($component_type, $resource_type);
97                 foreach ($resource_desc as $directive_name => $directive_desc) {
98                         $in_config = false;
99                         if ($load_values === true) {
100                                 $in_config = property_exists($config, $directive_name);
101                         }
102
103                         $directive_value = null;
104                         if ($in_config === true && $load_values === true) {
105                                 $directive_value = $config->{$directive_name};
106                         }
107
108                         $default_value = null;
109                         $data = null;
110                         $field_type = 'TextBox';
111                         $resource = null;
112                         $required = false;
113                         // @TODO: Add support for all directive properties defined in description file
114                         if (is_object($directive_desc)) {
115                                 if (property_exists($directive_desc, 'Required')) {
116                                         $required = $directive_desc->Required;
117                                         if ($load_values === true && array_key_exists($directive_name, $parent_directives)) {
118                                                 // values can be taken from JobDefs
119                                                 $required = false;
120                                         }
121                                 }
122                                 if (property_exists($directive_desc, 'DefaultValue')) {
123                                         $default_value = $directive_desc->DefaultValue;
124                                 }
125                                 if (property_exists($directive_desc, 'Data')) {
126                                         $data = $directive_desc->Data;
127                                 }
128                                 if (property_exists($directive_desc, 'FieldType')) {
129                                         $field_type = $directive_desc->FieldType;
130                                 }
131                                 if (property_exists($directive_desc, 'Resource')) {
132                                         $resource = $directive_desc->Resource;
133                                 }
134                         }
135
136                         if (!is_array($directive_value) && !is_object($directive_value)) {
137                                 $directive_value = array($directive_value);
138                         }
139                         if (is_object($directive_value)) {
140                                 $directive_value = (array)$directive_value;
141                         }
142                         foreach ($directive_value as $key => $value) {
143                                 $directive = array(
144                                         'host' => $host,
145                                         'component_type' => $component_type,
146                                         'component_name' => $component_name,
147                                         'resource_type' => $resource_type,
148                                         'resource_name' => $resource_name,
149                                         'directive_name' => $directive_name,
150                                         'directive_value' => $value,
151                                         'default_value' => $default_value,
152                                         'required' => $required,
153                                         'data' => $data,
154                                         'resource' => $resource,
155                                         'field_type' => $field_type,
156                                         'label' => $directive_name,
157                                         'in_config' => $in_config,
158                                         'show' => (($in_config || !$load_values) || $this->getShowAllDirectives())
159                                 );
160                                 array_push($directives, $directive);
161                         }
162                 }
163                 $config = $this->getConfigData($host, array($component_type));
164                 for ($i = 0; $i < count($config); $i++) {
165                         $resource_type = $this->getConfigResourceType($config[$i]);
166                         $resource_name = property_exists($config[$i]->{$resource_type}, 'Name') ? $config[$i]->{$resource_type}->Name : '';
167                         if (!array_key_exists($resource_type, $this->resource_names)) {
168                                 $this->resource_names[$resource_type] = array();
169                         }
170                         array_push($this->resource_names[$resource_type], $resource_name);
171                 }
172                 $this->setResourceNames($this->resource_names);
173
174                 $this->RepeaterDirectives->DataSource = $directives;
175                 $this->RepeaterDirectives->dataBind();
176         }
177
178         public function loadDirectives($sender, $param) {
179                 $show_all_directives = !$this->getShowAllDirectives();
180                 $this->setShowAllDirectives($show_all_directives);
181                 $this->loadConfig();
182         }
183
184         public function createDirectiveElement($sender, $param) {
185                 $load_values = $this->getLoadValues();
186                 for ($i = 0; $i < count($this->directive_types); $i++) {
187                         $control = $this->getChildControl($param->Item, $this->directive_types[$i]);
188                         if (is_object($control)) {
189                                 $control->setHost($param->Item->DataItem['host']);
190                                 $control->setComponentType($param->Item->DataItem['component_type']);
191                                 $control->setComponentName($param->Item->DataItem['component_name']);
192                                 $control->setResourceType($param->Item->DataItem['resource_type']);
193                                 $control->setResourceName($param->Item->DataItem['resource_name']);
194                                 $control->setDirectiveName($param->Item->DataItem['directive_name']);
195                                 $control->setDirectiveValue($param->Item->DataItem['directive_value']);
196                                 $control->setDefaultValue($param->Item->DataItem['default_value']);
197                                 $control->setRequired($param->Item->DataItem['required']);
198                                 $control->setData($param->Item->DataItem['data']);
199                                 $control->setResource($param->Item->DataItem['resource']);
200                                 $control->setLabel($param->Item->DataItem['label']);
201                                 $control->setInConfig($param->Item->DataItem['in_config']);
202                                 $show_all_directives = ($param->Item->DataItem['in_config'] || !$load_values || $this->getShowAllDirectives());
203                                 $control->setShow($show_all_directives);
204                                 $control->setResourceNames($this->resource_names);
205                                 break;
206                         }
207                 }
208                 for ($i = 0; $i < count($this->directive_list_types); $i++) {
209                         $control = $this->getChildControl($param->Item, $this->directive_list_types[$i]);
210                         if (is_object($control)) {
211                                 $control->setHost($param->Item->DataItem['host']);
212                                 $control->setComponentType($param->Item->DataItem['component_type']);
213                                 $control->setComponentName($param->Item->DataItem['component_name']);
214                                 $control->setResourceType($param->Item->DataItem['resource_type']);
215                                 $control->setResourceName($param->Item->DataItem['resource_name']);
216                                 $control->setDirectiveName($param->Item->DataItem['directive_name']);
217                                 $control->setData($param->Item->DataItem['directive_value']);
218                                 $control->setLoadValues($this->getLoadValues());
219                                 $control->setResourceNames($this->resource_names);
220                                 $control->raiseEvent('OnDirectiveListLoad', $this, null);
221                         }
222                 }
223         }
224
225
226         public function saveResource($sender, $param) {
227                 $directives = array();
228                 $host = $this->getHost();
229                 $component_type = $this->getComponentType();
230                 $resource_type = $this->getResourceType();
231                 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type);
232                 for ($i = 0; $i < count($this->directive_types); $i++) {
233                         $controls = $this->RepeaterDirectives->findControlsByType($this->directive_types[$i]);
234                         for ($j = 0; $j < count($controls); $j++) {
235                                 $parent_name = $controls[$j]->getParentName();
236                                 if (!is_null($parent_name)) {
237                                         continue;
238                                 }
239                                 $directive_name = $controls[$j]->getDirectiveName();
240                                 $directive_value = $controls[$j]->getDirectiveValue();
241                                 $default_value = $resource_desc[$directive_name]->DefaultValue;
242                                 $in_config = $controls[$j]->getInConfig();
243                                 if (is_null($directive_value)) {
244                                         // skip not changed values that don't exist in config
245                                         continue;
246                                 }
247                                 if ($this->directive_types[$i] === 'DirectiveBoolean') {
248                                         settype($default_value, 'bool');
249                                 } elseif ($this->directive_types[$i] === 'DirectiveInteger') {
250                                         settype($directive_value, 'int');
251                                 }
252                                 if ($directive_value === $default_value && $in_config === false) {
253                                         // value the same as default value, skip it
254                                         continue;
255                                 }
256                                 $directives[$directive_name] = $directive_value;
257                         }
258                 }
259                 for ($i = 0; $i < count($this->directive_list_types); $i++) {
260                         $controls = $this->RepeaterDirectives->findControlsByType($this->directive_list_types[$i]);
261                         for ($j = 0; $j < count($controls); $j++) {
262                                 $directive_name = $controls[$j]->getDirectiveName();
263                                 $directive_value = $controls[$j]->getDirectiveValue();
264                                 if (is_null($directive_value)) {
265                                         continue;
266                                 }
267                                 if (!array_key_exists($directive_name, $directives)) {
268                                         $directives[$directive_name] = array();
269                                 }
270                                 if (is_array($directive_value)) {
271                                         if ($this->directive_list_types[$i] === 'DirectiveMessages') {
272                                                 $directives = array_merge($directives, $directive_value);
273                                         } elseif ($this->directive_list_types[$i] === 'DirectiveRunscript') {
274                                                 if (!isset($directives[$directive_name])) {
275                                                         $directives[$directive_name] = array();
276                                                 }
277                                                 $directives[$directive_name] = array_merge($directives[$directive_name], $directive_value[$directive_name]);
278                                         } elseif (array_key_exists($directive_name, $directive_value)) {
279                                                 $directives[$directive_name][] = $directive_value[$directive_name];
280                                         } elseif (count($directive_value) > 0) {
281                                                 $directives[$directive_name][] = $directive_value;
282                                         }
283                                 } else {
284                                         $directives[$directive_name] = $directive_value;
285                                 }
286                         }
287                 }
288                 $load_values = $this->getLoadValues();
289                 if ($load_values === true) {
290                         $resource_name = $this->getResourceName();
291                 } else {
292                         $resource_name = array_key_exists('Name', $directives) ? $directives['Name'] : '';
293                 }
294
295                 $params = array(
296                         'config',
297                         $component_type,
298                         $resource_type,
299                         $resource_name
300                 );
301                 $result = $this->Application->getModule('api')->set($params, array('config' => serialize($directives)), $host, false);
302                 if ($result->error === 0) {
303                         $this->SaveDirectiveOk->Display = 'Dynamic';
304                         $this->SaveDirectiveError->Display = 'None';
305                         $this->SaveDirectiveErrMsg->Text = '';
306                 } else {
307                         $this->SaveDirectiveOk->Display = 'None';
308                         $this->SaveDirectiveError->Display = 'Dynamic';
309                         $this->SaveDirectiveErrMsg->Display = 'Dynamic';
310                         $this->SaveDirectiveErrMsg->Text = "Error {$result->error}: {$result->output}";
311                 }
312         }
313
314         public function setShowAllDirectives($show_all_directives) {
315                 $this->setViewState(self::SHOW_ALL_DIRECTIVES, $show_all_directives);
316         }
317
318         public function getShowAllDirectives() {
319                 return $this->getViewState(self::SHOW_ALL_DIRECTIVES, false);
320         }
321 }
322 ?>