1<?php
2
3
4// Pandora FMS - http://pandorafms.com
5// ==================================================
6// Copyright (c) 2005-2009 Artica Soluciones Tecnologicas
7// Please see http://pandorafms.org for full contribution list
8
9// This program is free software; you can redistribute it and/or
10// modify it under the terms of the GNU General Public License
11// as published by the Free Software Foundation for version 2.
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16
17include_once("include/functions_modules.php");
18include_once("include/functions_categories.php");
19
20function prepend_table_simple ($row, $id = false) {
21	global $table_simple;
22
23	if ($id)
24		$data = array ($id => $row);
25	else
26		$data = array ($row);
27
28	$table_simple->data = array_merge ($data, $table_simple->data);
29}
30
31function push_table_simple ($row, $id = false) {
32	global $table_simple;
33
34	if ($id)
35		$data = array ($id => $row);
36	else
37		$data = array ($row);
38
39	$table_simple->data = array_merge ($table_simple->data, $data);
40}
41
42function prepend_table_advanced ($row, $id = false) {
43	global $table_advanced;
44
45	if ($id)
46		$data = array ($id => $row);
47	else
48		$data = array ($row);
49
50	$table_advanced->data = array_merge ($data, $table_advanced->data);
51}
52
53function push_table_advanced ($row, $id = false) {
54	global $table_advanced;
55
56	if ($id)
57		$data = array ($id => $row);
58	else
59		$data = array ($row);
60
61	$table_advanced->data = array_merge ($table_advanced->data, $data);
62}
63
64function add_component_selection ($id_network_component_type) {
65	global $table_simple;
66
67	$data = array ();
68	$data[0] = __('Using module component').' ';
69	$data[0] .= ui_print_help_icon ('network_component', true);
70
71	$component_groups = network_components_get_groups ($id_network_component_type);
72	$data[1] = '<span id="component_group" class="left">';
73	$data[1] .= html_print_select ($component_groups,
74		'network_component_group', '', '', '--'.__('Manual setup').'--', 0,
75		true, false, false);
76	$data[1] .= '</span>';
77	$data[1] .= html_print_input_hidden ('id_module_component_type', $id_network_component_type, true);
78	$data[1] .= '<span id="no_component" class="invisible error">';
79	$data[1] .= __('No component was found');
80	$data[1] .= '</span>';
81	$data[1] .= '<span id="component" class="invisible right">';
82	$data[1] .= html_print_select (array (), 'network_component', '', '',
83		'---'.__('Manual setup').'---', 0, true);
84	$data[1] .= '</span>';
85	$data[1] .= ' <span id="component_loading" class="invisible">';
86	$data[1] .= html_print_image('images/spinner.png', true);
87	$data[1] .= '</span>';
88
89	$table_simple->colspan['module_component'][1] = 3;
90	$table_simple->rowstyle['module_component'] = 'background-color: #cfcfcf';
91
92	prepend_table_simple ($data, 'module_component');
93}
94
95require_once ('include/functions_network_components.php');
96enterprise_include_once('include/functions_policies.php');
97
98// If code comes from policies disable export select
99global $__code_from;
100
101$disabledBecauseInPolicy = false;
102$disabledTextBecauseInPolicy = '';
103
104$page = get_parameter('page', '');
105if (strstr($page, "policy_modules") === false && $id_agent_module) {
106	if ($config['enterprise_installed'])
107		$disabledBecauseInPolicy = policies_is_module_in_policy($id_agent_module) && policies_is_module_linked($id_agent_module);
108	else
109		$disabledBecauseInPolicy = false;
110	if ($disabledBecauseInPolicy)
111		$disabledTextBecauseInPolicy = 'disabled = "disabled"';
112}
113
114$update_module_id = (int) get_parameter_get ('update_module');
115$table_simple = new stdClass();
116$table_simple->id = 'simple';
117$table_simple->width = '100%';
118$table_simple->class = 'databox';
119$table_simple->data = array ();
120$table_simple->colspan = array ();
121$table_simple->style = array ();
122$table_simple->style[0] = 'font-weight: bold; width: 26%;';
123$table_simple->style[1] = 'width: 40%';
124$table_simple->style[2] = 'font-weight: bold;';
125
126#$table_simple->colspan[4][1] = 3;
127$table_simple->colspan[5][1] = 3;
128$table_simple->colspan[6][1] = 3;
129
130$table_simple->data[0][0] = __('Name');
131$table_simple->data[0][1] = html_print_input_text_extended ('name',
132	io_safe_output($name), 'text-name', '', 45, 100, $disabledBecauseInPolicy, '', 'autocomplete="off"', true);
133//$table_simple->data[0][1] = html_print_input_text ('name',
134//	io_safe_output($name), '', 45, 100, true, $disabledBecauseInPolicy);
135
136if (!empty($id_agent_module) && isset($id_agente)) {
137	$table_simple->data[0][1] .= '&nbsp;<b>' . __('ID') . '</b>&nbsp;&nbsp;' . $id_agent_module . ' ';
138
139	$table_simple->data[0][1] .= '&nbsp;<a href="index.php?sec=gagente&tab=module&sec2=godmode/agentes/configurar_agente&id_agente='.$id_agente.'&delete_module='.$id_agent_module.'"
140		onClick="if (!confirm(\' '.__('Are you sure?').'\')) return false;">';
141	$table_simple->data[0][1] .= html_print_image ('images/cross.png', true,
142		array ('title' => __('Delete module')));
143	$table_simple->data[0][1] .= '</a> ';
144}
145
146$disabled_enable = 0;
147$policy_link = db_get_value('policy_linked', 'tagente_modulo',
148	'id_agente_modulo', $id_agent_module);
149
150if ($policy_link != 0) {
151	$disabled_enable = 1;
152}
153$table_simple->data[0][2] = __('Disabled');
154$table_simple->data[0][3] = html_print_checkbox ("disabled", 1, $disabled, true, $disabled_enable);
155
156$table_simple->data[1][0] = __('Type').' ' . ui_print_help_icon ('module_type', true);
157$table_simple->data[1][0] .= html_print_input_hidden ('id_module_type_hidden', $id_module_type, true);
158
159if (isset($id_agent_module)) {
160	if ($id_agent_module) {
161		$edit = false;
162	}
163	else {
164		$edit = true;
165	}
166}
167else {
168	//Run into a policy
169	$edit = true;
170}
171
172if (!$edit) {
173	$sql = sprintf ('SELECT id_tipo, nombre
174			FROM ttipo_modulo
175			WHERE id_tipo = %s
176			ORDER BY descripcion',
177			$id_module_type);
178
179	$type_names = db_get_all_rows_sql($sql);
180
181	$type_names_hash = array();
182	foreach ($type_names as $tn) {
183		$type_names_hash[$tn['id_tipo']] = $tn['nombre'];
184	}
185
186	$table_simple->data[1][1] = '<em>'.modules_get_moduletype_description ($id_module_type).' ('.$type_names_hash[$id_module_type].')</em>';
187	$table_simple->data[1][1] .= html_print_input_hidden('type_names',base64_encode(io_json_mb_encode($type_names_hash)),true);
188}
189else {
190	if (isset($id_module_type)) {
191		$idModuleType = $id_module_type;
192	}
193	else {
194		$idModuleType = '';
195	}
196
197	$sql = sprintf ('SELECT id_tipo, descripcion
198		FROM ttipo_modulo
199		WHERE categoria IN (%s)
200		ORDER BY descripcion',
201		implode (',', $categories));
202
203	$table_simple->data[1][1] = html_print_select_from_sql ($sql, 'id_module_type',
204		$idModuleType, '', '', '', true, false, false, $disabledBecauseInPolicy, false, false, 100);
205
206	// Store the relation between id and name of the types on a hidden field
207	$sql = sprintf ('SELECT id_tipo, nombre
208			FROM ttipo_modulo
209			WHERE categoria IN (%s)
210			ORDER BY descripcion',
211			implode (',', $categories));
212
213	$type_names = db_get_all_rows_sql($sql);
214
215	$type_names_hash = array();
216	foreach ($type_names as $tn) {
217		$type_names_hash[$tn['id_tipo']] = $tn['nombre'];
218	}
219
220	$table_simple->data[1][1] .= html_print_input_hidden('type_names',base64_encode(io_json_mb_encode($type_names_hash)),true);
221}
222
223$table_simple->data[1][2] = __('Module group');
224$table_simple->data[1][3] = html_print_select_from_sql ('SELECT id_mg, name FROM tmodule_group ORDER BY name',
225	'id_module_group', $id_module_group, '', __('Not assigned'), '0',
226	true, false, true, $disabledBecauseInPolicy);
227
228if($disabledBecauseInPolicy){
229 	$table_simple->data[1][3] .= html_print_input_hidden ('id_module_group', $id_module_group, true);
230}
231
232$table_simple->data[2][0] = __('Warning status').' ' . ui_print_help_icon ('warning_status', true);
233
234$table_simple->data[2][1] = '';
235
236if (!modules_is_string_type($id_module_type) || $edit) {
237	$table_simple->data[2][1] .= '<span id="minmax_warning"><em>'.__('Min. ').'</em>';
238	$table_simple->data[2][1] .= html_print_input_text ('min_warning', $min_warning,
239		'', 10, 255, true, $disabledBecauseInPolicy);
240	$table_simple->data[2][1] .= '<br /><em>'.__('Max.').'</em>';
241	$table_simple->data[2][1] .= html_print_input_text ('max_warning', $max_warning,
242		'', 10, 255, true, $disabledBecauseInPolicy).'</span>';
243}
244if (modules_is_string_type($id_module_type) || $edit) {
245	$table_simple->data[2][1] .= '<span id="string_warning"><em>'.__('Str.').'</em>';
246	$table_simple->data[2][1] .= html_print_input_text ('str_warning', $str_warning,
247		'', 10, 255, true, $disabledBecauseInPolicy).'</span>';
248}
249
250$table_simple->data[2][1] .= '<br /><em>'.__('Inverse interval').'</em>';
251$table_simple->data[2][1] .= html_print_checkbox ("warning_inverse", 1,
252	$warning_inverse, true);
253$table_simple->data[2][2] = __('Critical status').' ' . ui_print_help_icon ('critical_status', true);
254$table_simple->data[2][3] = '';
255
256if (!modules_is_string_type($id_module_type) || $edit) {
257	$table_simple->data[2][3] .= '<span id="minmax_critical"><em>'.__('Min. ').'</em>';
258	$table_simple->data[2][3] .= html_print_input_text ('min_critical', $min_critical,
259		'', 10, 255, true, $disabledBecauseInPolicy);
260	$table_simple->data[2][3] .= '<br /><em>'.__('Max.').'</em>';
261	$table_simple->data[2][3] .= html_print_input_text ('max_critical', $max_critical,
262		'', 10, 255, true, $disabledBecauseInPolicy).'</span>';
263}
264if (modules_is_string_type($id_module_type) || $edit) {
265	$table_simple->data[2][3] .= '<span id="string_critical"><em>'.__('Str.').'</em>';
266	$table_simple->data[2][3] .= html_print_input_text ('str_critical', $str_critical,
267		'', 10, 255, true, $disabledBecauseInPolicy).'</span>';
268}
269
270$table_simple->data[2][3] .= '<br /><em>'.__('Inverse interval').'</em>';
271$table_simple->data[2][3] .= html_print_checkbox ("critical_inverse", 1, $critical_inverse, true);
272
273/* FF stands for Flip-flop */
274$table_simple->data[3][0] = __('FF threshold').' ' . ui_print_help_icon ('ff_threshold', true);
275$table_simple->colspan[3][1] = 3;
276
277$table_simple->data[3][1] = html_print_radio_button ('each_ff', 0, '', $each_ff, true) . ' ' . __('All state changing') . ' : ';
278$table_simple->data[3][1] .= html_print_input_text ('ff_event', $ff_event, '', 5
279, 15, true, $disabledBecauseInPolicy) . '<br />';
280$table_simple->data[3][1] .= html_print_radio_button ('each_ff', 1, '', $each_ff, true) . ' ' . __('Each state changing') . ' : ';
281$table_simple->data[3][1] .= __('To normal');
282$table_simple->data[3][1] .= html_print_input_text ('ff_event_normal', $ff_event_normal, '', 5, 15, true, $disabledBecauseInPolicy) . ' ';
283$table_simple->data[3][1] .= __('To warning');
284$table_simple->data[3][1] .= html_print_input_text ('ff_event_warning', $ff_event_warning, '', 5, 15, true, $disabledBecauseInPolicy) . ' ';
285$table_simple->data[3][1] .= __('To critical');
286$table_simple->data[3][1] .= html_print_input_text ('ff_event_critical', $ff_event_critical, '', 5, 15, true, $disabledBecauseInPolicy);
287$table_simple->data[4][0] = __('Historical data');
288if($disabledBecauseInPolicy) {
289	// If is disabled, we send a hidden in his place and print a false checkbox because HTML dont send disabled fields and could be disabled by error
290	$table_simple->data[4][1] = html_print_checkbox ("history_data_fake", 1, $history_data, true, $disabledBecauseInPolicy);
291	$table_simple->data[4][1] .= '<input type="hidden" name="history_data" value="'.(int)$history_data.'">';
292}
293else {
294	$table_simple->data[4][1] = html_print_checkbox ("history_data", 1, $history_data, true, $disabledBecauseInPolicy);
295}
296
297/* Advanced form part */
298$table_advanced = new stdClass();
299$table_advanced->id = 'advanced';
300$table_advanced->width = '100%';
301$table_advanced->class = 'databox filters';
302$table_advanced->data = array ();
303$table_advanced->style = array ();
304$table_advanced->style[0] =
305	$table_advanced->style[3] =
306	$table_advanced->style[5] = 'font-weight: bold;';
307$table_advanced->colspan = array ();
308
309$table_advanced->data[0][0] = __('Description');
310$table_advanced->colspan[0][1] = 6;
311$table_advanced->data[0][1] = html_print_textarea ('description', 2, 65,
312	$description, $disabledTextBecauseInPolicy, true);
313
314$table_advanced->data[1][0] = __('Custom ID');
315$table_advanced->colspan[1][1] = 2;
316$table_advanced->data[1][1] = html_print_input_text ('custom_id', $custom_id,
317	'', 20, 65, true);
318
319$table_advanced->data[1][3] = __('Unit');
320$table_advanced->data[1][4] = html_print_input_text ('unit', $unit,
321	'', 20, 65, true);
322$table_advanced->colspan[1][4] = 3;
323
324$module_id_policy_module = 0;
325if (isset($module['id_policy_module'])) {
326	$module_id_policy_module = $module['id_policy_module'];
327}
328
329// In the data modules, the interval is not in seconds. It is a factor
330// to be multiplied for the agent interval
331if ($moduletype == MODULE_DATA) {
332	$table_advanced->data[2][0] = __('Interval');
333	$table_advanced->colspan[2][1] = 2;
334	$interval_factor = 1;
335	if (isset($id_agente)) {
336		$agent_interval = agents_get_interval ($id_agente);
337		$interval_factor = $interval / $agent_interval;
338		$table_advanced->data[2][1] = human_time_description_raw($interval) . ' (' . sprintf(__('Agent interval x %s'), $interval_factor) . ') ' . ui_print_help_icon ('module_interval_factor', true);
339	}
340	else {
341		$table_advanced->data[2][1] = sprintf(__('Agent interval x %s'), $interval_factor) . ui_print_help_icon ('module_interval_factor', true);
342	}
343
344	if ($__code_from == 'policies') {
345		// If is the policy form, module_interval will store the factor (not the seconds).
346		// So server will transform it to interval in seconds
347		$table_advanced->data[2][1] = sprintf(__('Default') . ': 1' , $interval_factor) . ui_print_help_icon ('module_interval_factor', true);
348		$table_advanced->data[2][1] .= html_print_input_hidden('module_interval', $interval_factor, true);
349	}
350	else if ($module_id_policy_module != 0) {
351		$table_advanced->data[2][1] .= ui_print_help_tip(__('The policy modules of data type will only update their intervals when policy is applied.'), true);
352	}
353
354	// If it is a non policy form, the module_interval will not provided and will
355	// be taken the agent interval (this code is at configurar_agente.php)
356}
357else {
358	$table_advanced->data[2][0] = __('Interval') . ui_print_help_icon ('module_interval', true);
359	$table_advanced->colspan[2][1] = 2;
360	$table_advanced->data[2][1] = html_print_extended_select_for_time ('module_interval' , $interval, '', '', '0', false, true, false, false);
361}
362
363$table_advanced->data[2][1] .= html_print_input_hidden ('moduletype', $moduletype, true);
364
365$table_advanced->data[2][3] = __('Post process').' ' . ui_print_help_icon ('postprocess', true);
366$table_advanced->data[2][4] =
367	html_print_extended_select_for_post_process('post_process',
368		$post_process, '', '', '0', false, true, false, true,
369		$disabledBecauseInPolicy);
370$table_advanced->colspan[2][4] = 3;
371
372$table_advanced->data[3][0] = __('Min. Value');
373$table_advanced->colspan[3][1] = 2;
374
375$table_advanced->data[3][1] = html_print_input_text ('min', $min, '', 5, 15, true, $disabledBecauseInPolicy). ' ' . ui_print_help_tip (__('Any value below this number is discarted.'), true);
376$table_advanced->data[3][3] = __('Max. Value');
377$table_advanced->data[3][4] = html_print_input_text ('max', $max, '', 5, 15, true, $disabledBecauseInPolicy). ' ' . ui_print_help_tip (__('Any value over this number is discarted.'), true);
378$table_advanced->colspan[3][4] = 3;
379
380$table_advanced->data[4][0] = __('Export target');
381// Default text message for export target select and disabled option
382$none_text = __('None');
383$disabled_export = false;
384
385if ($__code_from == 'policies') {
386	$none_text = __('Not needed');
387	$disabled_export = true;
388}
389$table_advanced->data[4][1] = html_print_select_from_sql ('SELECT id, name FROM tserver_export ORDER BY name',
390	'id_export', $id_export, '', $none_text, '0', true, false, false, $disabled_export).ui_print_help_tip (__('In case you use an Export server you can link this module and export data to one these.'), true);
391$table_advanced->colspan[4][1] = 2;
392
393// Code comes from module_editor
394if ($__code_from == 'modules') {
395	$throw_unknown_events_check =
396		!modules_is_disable_type_event($id_agent_module, EVENTS_GOING_UNKNOWN);
397}
398else {
399	global $__id_pol_mod;
400
401	$throw_unknown_events_check =
402		!policy_module_is_disable_type_event($__id_pol_mod, EVENTS_GOING_UNKNOWN);
403}
404$table_advanced->data[4][3] = __('Throw unknown events');
405$table_advanced->data[4][4] = html_print_checkbox('throw_unknown_events',
406	1, $throw_unknown_events_check, true);
407$table_advanced->colspan[4][4] = 3;
408
409$table_advanced->data[5][0] = __('FF interval') . ' ' . ui_print_help_icon ('ff_interval', true);
410$table_advanced->data[5][1] = html_print_input_text ('module_ff_interval', $ff_interval,
411	'', 5, 10, true, $disabledBecauseInPolicy) .
412	ui_print_help_tip (__('Module execution flip flop time interval (in secs).'), true);
413$table_advanced->colspan[5][1] = 2;
414
415$table_advanced->data[5][3] = __('FF timeout') . ' ' . ui_print_help_icon ('ff_timeout', true);
416
417$module_type_name = modules_get_type_name($id_module_type);
418$table_advanced->data[5][4] = '';
419if (preg_match ('/async/', $module_type_name) || $edit) {
420	$table_advanced->data[5][4] .= '<span id="ff_timeout">' . html_print_input_text ('ff_timeout', $ff_timeout,
421		'', 5, 10, true, $disabledBecauseInPolicy).ui_print_help_tip (__('Timeout in secs from start of flip flop counting. If this value is exceeded, FF counter is reset. Set to 0 for no timeout.'), true) . '</span>';
422}
423if (!preg_match ('/async/', $module_type_name) || $edit) {
424	$table_advanced->data[5][4] .= '<span id="ff_timeout_disable">' . __('Disabled') . ui_print_help_tip (__('This value can be set only in the async modules.'), true) . '</span>';
425}
426$table_advanced->colspan[5][4] = 3;
427
428/* Tags */
429// This var comes from module_manager_editor.php or policy_modules.php
430global $__code_from;
431$table_advanced->data[6][0] =  __('Tags available');
432// Code comes from module_editor
433if ($__code_from == 'modules') {
434	$__table_modules = 'ttag_module';
435	$__id_where = 'b.id_agente_modulo';
436	$__id = (int)$id_agent_module;
437
438	$__sql = " AND b.id_policy_module = 0";
439	$__sql_policy = " AND b.id_policy_module != 0";
440
441}
442else {
443	// Code comes from policy module editor
444
445	global $__id_pol_mod;
446	$__table_modules= 'ttag_policy_module';
447	$__id_where = 'b.id_policy_module';
448	$__id = $__id_pol_mod;
449
450	$__sql = "";
451
452}
453
454if (!tags_has_user_acl_tags($config["id_user"])) {
455	$table_advanced->data[6][1] = html_print_select_from_sql (
456		"SELECT id_tag, name
457		FROM ttag
458		WHERE id_tag NOT IN (
459			SELECT a.id_tag
460			FROM ttag a, $__table_modules b
461			WHERE a.id_tag = b.id_tag AND $__id_where = $__id )
462			ORDER BY name", 'id_tag_available[]', '', '','','',
463		true, true, false, false, 'width: 200px', '5');
464}
465else {
466	$user_tags = tags_get_user_tags($config["id_user"], "AW");
467	if (!empty($user_tags)) {
468		$id_user_tags = array_keys($user_tags);
469
470		$table_advanced->data[6][1] = html_print_select_from_sql (
471			"SELECT id_tag, name
472			FROM ttag
473			WHERE id_tag IN (" . implode(',',  $id_user_tags) . ") AND
474				id_tag NOT IN (
475				SELECT a.id_tag
476				FROM ttag a, $__table_modules b
477				WHERE a.id_tag = b.id_tag AND $__id_where = $__id )
478				ORDER BY name", 'id_tag_available[]', '', '','','',
479			true, true, false, false, 'width: 200px', '5');
480	}
481	else {
482		$table_advanced->data[6][1] = html_print_select_from_sql (
483			"SELECT id_tag, name
484			FROM ttag
485			WHERE id_tag NOT IN (
486				SELECT a.id_tag
487				FROM ttag a, $__table_modules b
488				WHERE a.id_tag = b.id_tag AND $__id_where = $__id )
489				ORDER BY name", 'id_tag_available[]', '', '','','',
490			true, true, false, false, 'width: 200px', '5');
491	}
492}
493$table_advanced->data[6][2] = html_print_image('images/darrowright.png', true, array('id' => 'right', 'title' => __('Add tags to module'))); //html_print_input_image ('add', 'images/darrowright.png', 1, '', true, array ('title' => __('Add tags to module')));
494$table_advanced->data[6][2] .= '<br><br><br><br>' . html_print_image('images/darrowleft.png', true, array('id' => 'left', 'title' => __('Delete tags to module'))); //html_print_input_image ('add', 'images/darrowleft.png', 1, '', true, array ('title' => __('Delete tags to module')));
495
496$table_advanced->data[6][3] = '<b>' . __('Tags selected') . '</b>';
497$table_advanced->data[6][4] = html_print_select_from_sql (
498	"SELECT a.id_tag, name
499	FROM ttag a, $__table_modules b
500	WHERE a.id_tag = b.id_tag AND $__id_where = $__id
501		$__sql
502	ORDER BY name",
503	'id_tag_selected[]', '', '','','', true, true, false,
504	false, 'width: 200px', '5');
505
506if ($__code_from == 'modules') {
507	$table_advanced->data[6][5] = '<b>' . __('Tags from policy') . '</b>';
508	$table_advanced->data[6][6] = html_print_select_from_sql (
509		"SELECT a.id_tag, name
510		FROM ttag a, $__table_modules b
511		WHERE a.id_tag = b.id_tag AND $__id_where = $__id
512			$__sql_policy
513		ORDER BY name",
514		'id_tag_policy[]', '', '','','', true, true, false,
515		false, 'width: 200px', '5');
516}
517
518$table_advanced->data[7][0] = __('Quiet');
519$table_advanced->data[7][0] .= ui_print_help_tip(
520	__('The module still stores data but the alerts and events will be stop'), true);
521$table_advanced->colspan[7][1] = 7;
522$table_advanced->data[7][1] = html_print_checkbox('quiet_module', 1,
523	$quiet_module, true);
524
525$table_advanced->data[8][0] = __('Critical instructions') .
526	ui_print_help_tip(__("Instructions when the status is critical"), true);
527$table_advanced->data[8][1] = html_print_textarea ('critical_instructions', 2, 65, $critical_instructions, '', true);
528$table_advanced->colspan[8][1] = 6;
529
530$table_advanced->data[9][0] = __('Warning instructions') .
531	ui_print_help_tip(__("Instructions when the status is warning"), true);
532$table_advanced->data[9][1] = html_print_textarea ('warning_instructions', 2, 65, $warning_instructions, '', true);
533$table_advanced->colspan[9][1] = 6;
534
535$table_advanced->data[10][0] = __('Unknown instructions'). ui_print_help_tip(__("Instructions when the status is unknown"), true);
536$table_advanced->data[10][1] = html_print_textarea ('unknown_instructions', 2, 65, $unknown_instructions, '', true);
537$table_advanced->colspan[10][1] = 6;
538
539if (isset($id_agente) && $moduletype == MODULE_DATA) {
540	$table_advanced->data[11][0] = __('Cron') .
541		ui_print_help_tip (__('If cron is set the module interval is ignored and the module runs on the specified date and time'), true);
542	$table_advanced->data[11][1] = html_print_extended_select_for_cron ($hour, $minute, $mday, $month, $wday, true, true);
543	$table_advanced->colspan[11][1] = 6;
544	}
545else {
546	$table_advanced->data[11][0] = __('Cron') .
547		ui_print_help_tip (__('If cron is set the module interval is ignored and the module runs on the specified date and time'), true);
548	$table_advanced->data[11][1] = html_print_extended_select_for_cron ($hour, $minute, $mday, $month, $wday, true, false);
549	$table_advanced->colspan[11][1] = 6;
550}
551
552$table_advanced->data[12][0] = __('Timeout');
553$table_advanced->data[12][1] = html_print_input_text ('max_timeout', $max_timeout, '', 5, 10, true). ' ' . ui_print_help_tip (__('Seconds that agent will wait for the execution of the module.'), true);
554$table_advanced->data[12][2] = '';
555$table_advanced->data[12][3] = __('Retries');
556$table_advanced->data[12][4] = html_print_input_text ('max_retries', $max_retries, '', 5, 10, true). ' ' . ui_print_help_tip (__('Number of retries that the module will attempt to run.'), true);
557$table_advanced->colspan[12][4] = 3;
558
559if (check_acl ($config['id_user'], 0, "PM")) {
560	$table_advanced->data[13][0] = __('Category');
561	$table_advanced->data[13][1] = html_print_select(
562		categories_get_all_categories('forselect'), 'id_category', $id_category, '', __('None'), 0, true);
563	$table_advanced->colspan[13][1] = 6;
564}
565else {
566	// Store in a hidden field if is not visible to avoid delete the value
567	$table_advanced->data[12][4] .= html_print_input_hidden ('id_category', $id_category, true);
568}
569
570/* Advanced form part */
571$table_macros = new stdClass();
572$table_macros->id = 'module_macros';
573$table_macros->width = '100%';
574$table_macros->class = 'databox filters';
575$table_macros->data = array ();
576$table_macros->style = array ();
577$table_macros->style[0] = 'font-weight: bold;';
578$table_macros->style[2] = 'font-weight: bold;';
579$table_macros->style[5] = 'width: 10px';
580$table_macros->colspan = array ();
581
582$macro_count = 0;
583if (isset($module_macros)) {
584	if (is_array($module_macros)) {
585		foreach ($module_macros as $macro_name => $macro_value) {
586			$table_macros->data[$macro_count][0] = __('Name');
587			$table_macros->data[$macro_count][1] = html_print_input_text ('module_macro_names[]', $macro_name, '', 50, 60, true);
588			$table_macros->data[$macro_count][2] = __('Value');
589			$table_macros->data[$macro_count][3] = html_print_input_text ('module_macro_values[]', $macro_value, '', 50, 60, true);
590			$table_macros->data[$macro_count][4] = '<a href="javascript: delete_macro(' . $macro_count . ');">' . html_print_image('images/cross.png', true) . '</a>';
591			$macro_count++;
592		}
593	}
594}
595$table_macros->data[$macro_count][0] = '<span>'.__('Custom macros').'</span> <a href="javascript:add_macro();">'.html_print_image('images/add.png',true).'</a>';
596$table_macros->colspan[$macro_count][0] = 5;
597$macro_count++;
598
599html_print_input_hidden ('module_macro_count', $macro_count);
600
601/* Advanced form part */
602// Add relationships
603$table_new_relations = new stdClass();
604$table_new_relations->id = 'module_new_relations';
605$table_new_relations->width = '100%';
606$table_new_relations->class = 'databox filters';
607$table_new_relations->data = array ();
608$table_new_relations->style = array ();
609$table_new_relations->style[0] = 'width: 10%; font-weight: bold;';
610$table_new_relations->style[1] = 'width: 25%; text-align: center;';
611$table_new_relations->style[2] = 'width: 10%; font-weight: bold;';
612$table_new_relations->style[3] = 'width: 25%; text-align: center;';
613$table_new_relations->style[4] = 'width: 30%; text-align: center;';
614
615$table_new_relations->data[0][0] = __('Agent');
616$params = array();
617$params['return'] = true;
618$params['show_helptip'] = true;
619$params['input_name'] = 'autocomplete_agent_name';
620$params['use_hidden_input_idagent'] = true;
621$params['print_hidden_input_idagent'] = true;
622$params['hidden_input_idagent_id'] = 'hidden-autocomplete_id_agent';
623$params['javascript_function_action_after_select_js_call'] = 'change_modules_autocomplete_input();';
624$table_new_relations->data[0][1] = ui_print_agent_autocomplete_input($params);
625$table_new_relations->data[0][2] = __('Module');
626$table_new_relations->data[0][3] = "<div id='module_autocomplete'></div>";
627$table_new_relations->data[0][4] = html_print_button (__('Add relationship'), 'add_relation', false, 'javascript: add_new_relation();', 'class="sub add"', true);
628$table_new_relations->data[0][4] .= "&nbsp;&nbsp;<div id='add_relation_status' style='display: inline;'></div>";
629
630// Relationship list
631$table_relations = new stdClass();
632$table_relations->id = 'module_relations';
633$table_relations->width = '100%';
634$table_relations->class = 'databox data';
635$table_relations->head = array ();
636$table_relations->data = array ();
637$table_relations->rowstyle = array ();
638$table_relations->rowstyle[-1] = 'display: none;';
639$table_relations->style = array ();
640$table_relations->style[2] = 'width: 10%; text-align: center;';
641$table_relations->style[3] = 'width: 10%; text-align: center;';
642
643$table_relations->head[0] = __('Agent');
644$table_relations->head[1] = __('Module');
645$table_relations->head[2] = __('Changes'). ui_print_help_tip(__("Activate this to prevent the relation from being updated or deleted"), true);
646$table_relations->head[3] = __('Delete');
647
648// Create an invisible row to use their html to add new rows
649$table_relations->data[-1][0] = "";
650$table_relations->data[-1][1] = "";
651$table_relations->data[-1][2] = '<a id="disable_updates_button" class="alpha50" href="">' . html_print_image('images/lock.png', true) . '</a>';
652$table_relations->data[-1][3] = '<a id="delete_relation_button" href="">' . html_print_image('images/cross.png', true) . '</a>';
653
654$module_relations = modules_get_relations(array('id_module' => $id_agent_module));
655if (!$module_relations) {
656	$module_relations = array();
657}
658
659$relations_count = 0;
660foreach ($module_relations as $key => $module_relation) {
661
662	if ($module_relation['module_a'] == $id_agent_module) {
663		$module_id = $module_relation['module_b'];
664		$agent_id = modules_give_agent_id_from_module_id ($module_relation['module_b']);
665	}
666	else {
667		$module_id = $module_relation['module_a'];
668		$agent_id = modules_give_agent_id_from_module_id ($module_relation['module_a']);
669	}
670
671	$agent_name = ui_print_agent_name ($agent_id, true);
672
673	$module_name = modules_get_agentmodule_name($module_id);
674	if (empty($module_name) || $module_name == 'false') {
675		$module_name = $module_id;
676	}
677
678	if ($module_relation['disable_update']) {
679		$disabled_update_class = "";
680	}
681	else {
682		$disabled_update_class = "alpha50";
683	}
684
685	// Agent name
686	$table_relations->data[$relations_count][0] = $agent_name;
687	// Module name
688	$table_relations->data[$relations_count][1] =
689		"<a href='index.php?sec=gagente&sec2=godmode/agentes/configurar_agente&id_agente=" . $agent_id .
690		"&tab=module&edit_module=1&id_agent_module=" . $module_id . "'>" .
691		ui_print_truncate_text($module_name, 'module_medium', true, true, true, '[&hellip;]') . "</a>";
692	// Lock relationship updates
693	$table_relations->data[$relations_count][2] =
694		'<a id="disable_updates_button" class="' . $disabled_update_class .
695		'"href="javascript: change_lock_relation(' . $relations_count . ', ' . $module_relation['id'] . ');">' .
696		html_print_image('images/lock.png', true) . '</a>';
697	// Delete relationship
698	$table_relations->data[$relations_count][3] =
699		'<a id="delete_relation_button" href="javascript: delete_relation(' . $relations_count . ', ' . $module_relation['id'] . ');">' .
700		html_print_image('images/cross.png', true) . '</a>';
701	$relations_count++;
702}
703
704html_print_input_hidden ('module_relations_count', $relations_count);
705
706ui_require_jquery_file('json');
707
708?>
709
710<script type="text/javascript">
711/* <![CDATA[ */
712$(document).ready (function () {
713	$("#right").click (function () {
714		jQuery.each($("select[name='id_tag_available[]'] option:selected"), function (key, value) {
715			tag_name = $(value).html();
716			if (tag_name != <?php echo "'".__('None')."'"; ?>) {
717				id_tag = $(value).attr('value');
718				$("select[name='id_tag_selected[]']").append($("<option></option>").val(id_tag).html('<i>' + tag_name + '</i>'));
719				$("#id_tag_available").find("option[value='" + id_tag + "']").remove();
720				$("#id_tag_selected").find("option[value='']").remove();
721				if($("#id_tag_available option").length == 0) {
722					$("select[name='id_tag_available[]']").append($("<option></option>").val('').html('<i><?php echo __('None'); ?></i>'));
723				}
724			}
725		});
726	});
727
728	$("#left").click (function () {
729		jQuery.each($("select[name='id_tag_selected[]'] option:selected"), function (key, value) {
730				tag_name = $(value).html();
731				if (tag_name != <?php echo "'".__('None')."'"; ?>) {
732					id_tag = $(value).attr('value');
733					$("select[name='id_tag_available[]']").append($("<option>").val(id_tag).html('<i>' + tag_name + '</i>'));
734					$("#id_tag_selected").find("option[value='" + id_tag + "']").remove();
735					$("#id_tag_available").find("option[value='']").remove();
736					if($("#id_tag_selected option").length == 0) {
737						$("select[name='id_tag_selected[]']").append($("<option></option>").val('').html('<i><?php echo __('None'); ?></i>'));
738					}
739				}
740		});
741	});
742
743	$("#submit-updbutton").click(function () {
744		$('#id_tag_selected option').map(function() {
745			$(this).prop('selected', true);
746		});
747	});
748
749	$("#submit-crtbutton").click(function () {
750		$('#id_tag_selected option').map(function() {
751			$(this).prop('selected', true);
752		});
753	});
754
755	$("#id_module_type").change(function () {
756		var type_selected = $(this).val();
757		var type_names = jQuery.parseJSON(Base64.decode($('#hidden-type_names').val()));
758
759		var type_name_selected = type_names[type_selected];
760
761		if (type_name_selected.match(/_string$/) == null) {
762			// Numeric types
763			$('#string_critical').hide();
764			$('#string_warning').hide();
765			$('#minmax_critical').show();
766			$('#minmax_warning').show();
767		}
768		else {
769			// String types
770			$('#string_critical').show();
771			$('#string_warning').show();
772			$('#minmax_critical').hide();
773			$('#minmax_warning').hide();
774		}
775
776		if (type_name_selected.match(/async/) == null) {
777			$('#ff_timeout').hide();
778			$('#ff_timeout_disable').show();
779		}
780		else {
781			$('#ff_timeout').show();
782			$('#ff_timeout_disable').hide();
783		}
784	});
785
786	$("#id_module_type").trigger('change');
787
788	// Prevent the form submission when the user hits the enter button from the relationship autocomplete inputs
789	$("#text-autocomplete_agent_name").keydown(function(event) {
790		if(event.keyCode == 13) { // key code 13 is the enter button
791			event.preventDefault();
792		}
793	});
794
795	//validate post_process. Change ',' by '.'
796	$("#submit-updbutton").click (function () {
797		validate_post_process();
798	});
799	$("#submit-crtbutton").click (function () {
800		validate_post_process();
801	});
802
803});
804
805// Add a new module macro
806function add_macro () {
807	var macro_count = parseInt($("#hidden-module_macro_count").val());
808	var delete_icon = '<?php html_print_image ("images/cross.png", false) ?>';
809
810	// Add inputs for the new macro
811	$("#module_macros").append('<tr id="module_macros-' + macro_count + '" class="datos2"><td style=" font-weight: bold; vertical-align: top;" class="datos2">Name</td> \
812	<td style="" class="datos2"><input type="text" name="module_macro_names[]" value="" id="text-module_macro_names[]" size="50" maxlength="60"></td> \
813	<td style="font-weight: bold; vertical-align: top;" class="datos2">Value</td> \
814	<td style="" class="datos2"><input type="text" name="module_macro_values[]" value="" id="text-module_macro_values[]" size="50" maxlength="60"></td> \
815	<td style="" class="datos2"><a href="javascript: delete_macro(' + macro_count + ');">' + delete_icon + '</a></td></tr>');
816
817	// Update the macro count
818	$("#hidden-module_macro_count").val(macro_count + 1);
819}
820
821// Delete an existing module macro
822function delete_macro (num) {
823	if ($("#module_macros-" + num).length) {
824		$("#module_macros-" + num).remove();
825	}
826
827	// Do not decrease the macro counter or new macros may overlap existing ones!
828}
829
830
831/* Relationship javascript */
832
833// Change the modules autocomplete input depending on the result of the agents autocomplete input
834function change_modules_autocomplete_input () {
835	var id_agent = parseInt($("#hidden-autocomplete_id_agent").val());
836	var module_autocomplete = $("#module_autocomplete");
837	var load_icon = '<?php html_print_image ("images/spinner.gif", false) ?>';
838	var error_icon = '<?php html_print_image ("images/error_red.png", false) ?>';
839
840	if (!module_autocomplete.hasClass('working')) {
841		module_autocomplete.addClass('working');
842		module_autocomplete.html(load_icon);
843
844		$.ajax({
845			type: "POST",
846			url: "ajax.php",
847			dataType: "html",
848			data: {
849				page: "include/ajax/module",
850				get_module_autocomplete_input: true,
851				id_agent: id_agent
852			},
853			success: function (data) {
854				module_autocomplete.removeClass('working');
855				if (data) {
856					module_autocomplete.html(data);
857					// Prevent the form submission when the user hits the enter button from the relationship autocomplete inputs
858					$("#text-autocomplete_module_name").keydown(function(event) {
859						if(event.keyCode == 13) { // key code 13 is the enter button
860							event.preventDefault();
861						}
862					});
863				}
864				else {
865					module_autocomplete.html(error_icon);
866				}
867			},
868			error: function (data) {
869				module_autocomplete.removeClass('working');
870				module_autocomplete.html(error_icon);
871			}
872		});
873	}
874}
875
876// Add a new relation
877function add_new_relation () {
878	var module_a_id = parseInt($("#hidden-id_agent_module").val());
879	var module_b_id = parseInt($("#hidden-autocomplete_module_name_hidden").val());
880	var module_b_name = $("#text-autocomplete_module_name").val();
881	var agent_b_name = $("#text-autocomplete_agent_name").val();
882	var hiddenRow = $("#module_relations--1");
883	var button = $("#button-add_relation");
884	var iconPlaceholder = $("#add_relation_status");
885	var load_icon = '<?php html_print_image ("images/spinner.gif", false, array("style"=>"vertical-align:middle;")) ?>';
886	var suc_icon = '<?php html_print_image ("images/ok.png", false, array("style"=>"vertical-align:middle;")) ?>';
887	var error_icon = '<?php html_print_image ("images/error_red.png", false, array("style"=>"vertical-align:middle;")) ?>';
888
889
890	if (!button.hasClass('working')) {
891		button.addClass('working');
892		iconPlaceholder.html(load_icon);
893
894		$.ajax({
895			type: "POST",
896			url: "ajax.php",
897			dataType: "json",
898			data: {
899				page: "include/ajax/module",
900				add_module_relation: true,
901				id_module_a: module_a_id,
902				id_module_b: module_b_id,
903				name_module_b: module_b_name
904			},
905			success: function (data) {
906				button.removeClass('working');
907				if (data === false) {
908					iconPlaceholder.html(error_icon);
909					setTimeout( function() { iconPlaceholder.html(''); }, 2000);
910				}
911				else {
912					iconPlaceholder.html(suc_icon);
913					setTimeout( function() { iconPlaceholder.html(''); }, 2000);
914
915					// Add the new row
916					var relationsCount = parseInt($("#hidden-module_relations_count").val());
917
918					var rowClass = "datos";
919					if (relationsCount % 2 != 0) {
920						rowClass = "datos2";
921					}
922
923					var rowHTML = '<tr id="module_relations-' + relationsCount + '" class="' + rowClass + '">' +
924										'<td id="module_relations-' + relationsCount + '-0"><b>' + agent_b_name + '</b></td>' +
925										'<td id="module_relations-' + relationsCount + '-1">' + module_b_name + '</td>' +
926										'<td id="module_relations-' + relationsCount + '-2" style="width: 10%; text-align: center;">' +
927											'<a id="disable_updates_button" class="alpha50" href="javascript: change_lock_relation(' + relationsCount + ', ' + data + ');">' +
928												'<?php echo html_print_image("images/lock.png", true); ?>' +
929											'</a>' +
930										'</td>' +
931										'<td id="module_relations-' + relationsCount + '-3" style="width: 10%; text-align: center;">' +
932											'<a id="delete_relation_button" href="javascript: delete_relation(' + relationsCount + ', ' + data +  ');">' +
933												'<?php echo html_print_image("images/cross.png", true); ?>' +
934											'</a>' +
935										'</td>' +
936									'</tr>';
937					$("#module_relations").find("tbody").append(rowHTML);
938
939					$("#hidden-module_relations_count").val(relationsCount + 1);
940					$("#text-autocomplete_module_name").val('');
941				}
942			},
943			error: function (data) {
944				button.removeClass('working');
945				iconPlaceholder.html(error_icon);
946				setTimeout( function() { iconPlaceholder.html(''); }, 2000);
947			}
948		});
949	}
950}
951
952// Delete an existing relation
953function change_lock_relation (num_row, id_relation) {
954	var row = $("#module_relations-" + num_row);
955	var button = row.find("#disable_updates_button");
956	var oldSrc = button.find("img").prop("src");
957	var isEnabled = button.hasClass('alpha50');
958
959	if (row.length > 0 && !button.hasClass('working')) {
960		button.addClass('working');
961		button.removeClass('alpha50');
962		button.find("img").prop("src", 'images/spinner.gif');
963
964		$.ajax({
965			type: "POST",
966			url: "ajax.php",
967			dataType: "json",
968			data: {
969				page: "include/ajax/module",
970				change_module_relation_updates: true,
971				id_relation: id_relation
972			},
973			success: function (data) {
974				if (data === false) {
975					button.addClass('alpha50');
976				}
977				button.removeClass('working');
978				button.find("img").prop("src", oldSrc);
979			},
980			error: function (data) {
981				if (isEnabled) {
982					button.addClass('alpha50');
983				}
984				button.removeClass('working');
985				button.find("img").prop("src", oldSrc);
986			}
987		});
988	}
989}
990
991// Delete an existing relation
992function delete_relation (num_row, id_relation) {
993	var row = $("#module_relations-" + num_row);
994	var button = row.find("#delete_relation_button");
995	var oldSrc = button.find("img").prop("src");
996
997	if (row.length > 0 && !button.hasClass('working')) {
998		button.addClass('working');
999		button.find("img").prop("src", 'images/spinner.gif');
1000
1001		$.ajax({
1002			type: "POST",
1003			url: "ajax.php",
1004			dataType: "json",
1005			data: {
1006				page: "include/ajax/module",
1007				remove_module_relation: true,
1008				id_relation: id_relation
1009			},
1010			success: function (data) {
1011				button.removeClass('working');
1012				button.find("img").prop("src", oldSrc);
1013				if (data === true) {
1014					row.remove();
1015				}
1016			},
1017			error: function (data) {
1018				button.removeClass('working');
1019				button.find("img").prop("src", oldSrc);
1020			}
1021		});
1022	}
1023}
1024
1025function validate_post_process() {
1026	var post_process = $("#text-post_process").val();
1027	var new_post_process = post_process.replace(',', '.');
1028
1029	$("#text-post_process").val(new_post_process);
1030}
1031
1032/* End of relationship javascript */
1033
1034/* ]]> */
1035</script>
1036