diff --git a/pandora_console/godmode/agentes/planned_downtime.list.php b/pandora_console/godmode/agentes/planned_downtime.list.php index cdd8bae876..a40a6346b0 100755 --- a/pandora_console/godmode/agentes/planned_downtime.list.php +++ b/pandora_console/godmode/agentes/planned_downtime.list.php @@ -1,27 +1,53 @@ '; } @@ -63,12 +90,15 @@ ui_print_page_header( $id_downtime = (int) get_parameter('id_downtime', 0); $stop_downtime = (bool) get_parameter('stop_downtime'); -// STOP DOWNTIME -if ($stop_downtime) { +// STOP DOWNTIME. +if ($stop_downtime === true) { $downtime = db_get_row('tplanned_downtime', 'id', $id_downtime); - // Check AD permission on the downtime - if (empty($downtime) || (! check_acl($config['id_user'], $downtime['id_group'], 'AD') && ! check_acl($config['id_user'], $downtime['id_group'], 'AW'))) { + // Check AD permission on the downtime. + if (empty($downtime) === true + || ((bool) check_acl($config['id_user'], $downtime['id_group'], 'AD') === false + && (bool) check_acl($config['id_user'], $downtime['id_group'], 'AW') === false) + ) { db_pandora_audit( 'ACL Violation', 'Trying to access downtime scheduler' @@ -80,7 +110,9 @@ if ($stop_downtime) { $result = planned_downtimes_stop($downtime); if ($result === false) { - ui_print_error_message(__('An error occurred stopping the scheduled downtime')); + ui_print_error_message( + __('An error occurred stopping the scheduled downtime') + ); } else { echo $result['message']; } @@ -88,11 +120,14 @@ if ($stop_downtime) { $delete_downtime = (int) get_parameter('delete_downtime'); // DELETE WHOLE DOWNTIME! -if ($delete_downtime) { +if (empty($delete_downtime) === false) { $downtime = db_get_row('tplanned_downtime', 'id', $id_downtime); - // Check AD permission on the downtime - if (empty($downtime) || (! check_acl($config['id_user'], $downtime['id_group'], 'AD') && ! check_acl($config['id_user'], $downtime['id_group'], 'AW'))) { + // Check AD permission on the downtime. + if (empty($downtime) === true + || ((bool) check_acl($config['id_user'], $downtime['id_group'], 'AD') === false + && (bool) check_acl($config['id_user'], $downtime['id_group'], 'AW') === false) + ) { db_pandora_audit( 'ACL Violation', 'Trying to access downtime scheduler' @@ -102,10 +137,13 @@ if ($delete_downtime) { } // The downtime shouldn't be running!! - if ($downtime['executed']) { + if ((bool) $downtime['executed'] === true) { ui_print_error_message(__('This scheduled downtime is running')); } else { - $result = db_process_sql_delete('tplanned_downtime', ['id' => $id_downtime]); + $result = db_process_sql_delete( + 'tplanned_downtime', + ['id' => $id_downtime] + ); ui_print_result_message( $result, @@ -119,15 +157,25 @@ if ($delete_downtime) { $offset = (int) get_parameter('offset'); $filter_params = []; -$search_text = $filter_params['search_text'] = (string) get_parameter('search_text'); -$date_from = $filter_params['date_from'] = (string) get_parameter('date_from'); -$date_to = $filter_params['date_to'] = (string) get_parameter('date_to'); -$execution_type = $filter_params['execution_type'] = (string) get_parameter('execution_type'); -$show_archived = $filter_params['archived'] = (bool) get_parameter('archived'); -$agent_id = $filter_params['agent_id'] = (int) get_parameter('agent_id'); -$agent_name = $filter_params['agent_name'] = (string) (!empty($agent_id) ? get_parameter('agent_name') : ''); -$module_id = $filter_params['module_id'] = (int) get_parameter('module_name_hidden'); -$module_name = $filter_params['module_name'] = (string) (!empty($module_id) ? get_parameter('module_name') : ''); +$search_text = (string) get_parameter('search_text'); +$date_from = (string) get_parameter('date_from'); +$date_to = (string) get_parameter('date_to'); +$execution_type = (string) get_parameter('execution_type'); +$show_archived = (bool) get_parameter('archived'); +$agent_id = (int) get_parameter('agent_id'); +$agent_name = (string) ((empty($agent_id) === false) ? get_parameter('agent_name') : ''); +$module_id = (int) get_parameter('module_name_hidden'); +$module_name = (string) ((empty($module_id) === false) ? get_parameter('module_name') : ''); + +$filter_params['search_text'] = $search_text; +$filter_params['date_from'] = $date_from; +$filter_params['date_to'] = $date_to; +$filter_params['execution_type'] = $execution_type; +$filter_params['archived'] = $show_archived; +$filter_params['agent_id'] = $agent_id; +$filter_params['agent_name'] = $agent_name; +$filter_params['module_id'] = $module_id; +$filter_params['module_name'] = $module_name; $filter_params_str = http_build_query($filter_params); @@ -141,11 +189,32 @@ $table_form->data = []; $row = []; // Search text. -$row[] = __('Search').' '.html_print_input_text('search_text', $search_text, '', 50, 250, true); +$row[] = __('Search').' '.html_print_input_text( + 'search_text', + $search_text, + '', + 50, + 250, + true +); // Dates. -$date_inputs = __('From').' '.html_print_input_text('date_from', $date_from, '', 10, 10, true); +$date_inputs = __('From').' '.html_print_input_text( + 'date_from', + $date_from, + '', + 10, + 10, + true +); $date_inputs .= ' '; -$date_inputs .= __('To').' '.html_print_input_text('date_to', $date_to, '', 10, 10, true); +$date_inputs .= __('To').' '.html_print_input_text( + 'date_to', + $date_to, + '', + 10, + 10, + true +); $row[] = $date_inputs; $table_form->data[] = $row; @@ -157,9 +226,24 @@ $execution_type_fields = [ 'once' => __('Once'), 'periodically' => __('Periodically'), ]; -$row[] = __('Execution type').' '.html_print_select($execution_type_fields, 'execution_type', $execution_type, '', __('Any'), '', true, false, false); +$row[] = __('Execution type').' '.html_print_select( + $execution_type_fields, + 'execution_type', + $execution_type, + '', + __('Any'), + '', + true, + false, + false +); // Show past downtimes. -$row[] = __('Show past downtimes').' '.html_print_checkbox('archived', 1, $show_archived, true); +$row[] = __('Show past downtimes').' '.html_print_checkbox( + 'archived', + 1, + $show_archived, + true +); $table_form->data[] = $row; @@ -178,48 +262,84 @@ $agent_input = __('Agent').' '.ui_print_agent_autocomplete_input($params); $row[] = $agent_input; // Module. -$row[] = __('Module').' '.html_print_autocomplete_modules('module_name', $module_name, false, true, '', [], true); +$row[] = __('Module').' '.html_print_autocomplete_modules( + 'module_name', + $module_name, + false, + true, + '', + [], + true +); -$row[] = html_print_submit_button(__('Search'), 'search', false, 'class="sub search"', true); +$row[] = html_print_submit_button( + __('Search'), + 'search', + false, + 'class="sub search"', + true +); $table_form->data[] = $row; // End of table filter. // Useful to know if the user has done a form filtering. $filter_performed = false; +$downtimes = []; $groups = users_get_groups(false, $access); -if (!empty($groups)) { +if (empty($groups) === false) { $where_values = '1=1'; $groups_string = implode(',', array_keys($groups)); - $where_values .= " AND id_group IN ($groups_string)"; + $where_values .= sprintf(' AND id_group IN (%s)', $groups_string); // WARNING: add $filter_performed = true; to any future filter. - if (!empty($search_text)) { + if (empty($search_text) === false) { $filter_performed = true; - - $where_values .= " AND (name LIKE '%$search_text%' OR description LIKE '%$search_text%')"; + $where_values .= sprintf( + ' AND (name LIKE "%%s%" OR description LIKE "%%s%")', + $search_text, + $search_text + ); } - if (!empty($execution_type)) { + if (empty($execution_type) === false) { $filter_performed = true; - - $where_values .= " AND type_execution = '$execution_type'"; + $where_values .= sprintf(' AND type_execution = "%s"', $execution_type); } - if (!empty($date_from)) { + if (empty($date_from) === false) { $filter_performed = true; - - $where_values .= " AND (type_execution = 'periodically' OR (type_execution = 'once' AND date_from >= '".strtotime("$date_from 00:00:00")."'))"; + $where_values .= sprintf( + ' AND (type_execution = "periodically" + OR (type_execution = "once" + AND date_from >= "%s") + )', + strtotime($date_from.' 00:00:00') + ); } - if (!empty($date_to)) { + if (empty($date_to) === false) { $filter_performed = true; - - $periodically_monthly_w = "type_periodicity = 'monthly' - AND ((periodically_day_from <= '".date('d', strtotime($date_from))."' AND periodically_day_to >= '".date('d', strtotime($date_to))."') - OR (periodically_day_from > periodically_day_to - AND (periodically_day_from <= '".date('d', strtotime($date_from))."' OR periodically_day_to >= '".date('d', strtotime($date_to))."')))"; + $periodically_monthly_w = sprintf( + 'type_periodicity = "monthly" AND ( + ( + periodically_day_from <= "%s" + AND periodically_day_to >= "%s" + ) + OR ( + periodically_day_from > periodically_day_to + AND ( + periodically_day_from <= "%s" + OR periodically_day_to >= "%s" + ) + ) + )', + date('d', strtotime($date_from)), + date('d', strtotime($date_to)), + date('d', strtotime($date_from)), + date('d', strtotime($date_to)) + ); $periodically_weekly_days = []; $date_from_aux = strtotime($date_from); @@ -229,7 +349,7 @@ if (!empty($groups)) { while ($date_from_aux <= $date_end && $days_number < 7) { $weekday_actual = strtolower(date('l', $date_from_aux)); - $periodically_weekly_days[] = "$weekday_actual = 1"; + $periodically_weekly_days[] = $weekday_actual.' = 1'; $date_from_aux = ($date_from_aux + SECONDS_1DAY); $days_number++; @@ -237,36 +357,60 @@ if (!empty($groups)) { $periodically_weekly_w = "type_periodicity = 'weekly' AND (".implode(' OR ', $periodically_weekly_days).')'; - $periodically_w = "type_execution = 'periodically' AND (($periodically_monthly_w) OR ($periodically_weekly_w))"; + $periodically_w = sprintf( + 'type_execution = "periodically" AND ((%s) OR (%s))', + $periodically_monthly_w, + $periodically_weekly_w + ); - $once_w = "type_execution = 'once' AND date_to <= '".strtotime("$date_to 23:59:59")."'"; + $once_w = sprintf( + 'type_execution = "once" AND date_to <= "%s"', + strtotime($date_to.' 23:59:59') + ); - $where_values .= " AND (($periodically_w) OR ($once_w))"; + $where_values .= sprintf( + ' AND ((%s) OR (%s))', + $periodically_w, + $once_w + ); } - if (!$show_archived) { + if ($show_archived === false) { $filter_performed = true; - - $where_values .= " AND (type_execution = 'periodically' OR (type_execution = 'once' AND date_to >= '".time()."'))"; + $where_values .= sprintf( + ' AND (type_execution = "periodically" + OR (type_execution = "once" + AND date_to >= "%s"))', + time() + ); } - if (!empty($agent_id)) { + if (empty($agent_id) === false) { $filter_performed = true; - - $where_values .= " AND id IN (SELECT id_downtime FROM tplanned_downtime_agents WHERE id_agent = $agent_id)"; + $where_values .= sprintf( + ' AND id IN (SELECT id_downtime FROM tplanned_downtime_agents WHERE id_agent = %d)', + $agent_id + ); } - if (!empty($module_id)) { + if (empty($module_id) === false) { $filter_performed = true; - - $where_values .= " AND (id IN (SELECT id_downtime - FROM tplanned_downtime_modules - WHERE id_agent_module = $module_id) - OR id IN (SELECT id_downtime - FROM tplanned_downtime_agents tpda, tagente_modulo tam - WHERE tpda.id_agent = tam.id_agente - AND tam.id_agente_modulo = $module_id - AND tpda.all_modules = 1))"; + $where_values .= sprintf( + ' AND (id IN ( + SELECT id_downtime + FROM tplanned_downtime_modules + WHERE id_agent_module = %d) + OR id IN ( + SELECT id_downtime + FROM tplanned_downtime_agents tpda, tagente_modulo tam + WHERE tpda.id_agent = tam.id_agente + AND tam.id_agente_modulo = %d + AND tpda.all_modules = 1 + ) + )', + $module_id, + $module_id + ); } // Columns of the table tplanned_downtime. @@ -296,68 +440,41 @@ if (!empty($groups)) { 'id_user', ]; - switch ($config['dbtype']) { - case 'mysql': - case 'postgresql': - $columns_str = implode(',', $columns); - $sql = "SELECT $columns_str - FROM tplanned_downtime - WHERE $where_values - ORDER BY type_execution DESC, date_from DESC - LIMIT ".$config['block_size']." - OFFSET $offset"; - break; + $columns_str = implode(',', $columns); + $sql = sprintf( + 'SELECT %s + FROM tplanned_downtime + WHERE %s + ORDER BY type_execution DESC, date_from DESC + LIMIT %d + OFFSET %d', + $columns_str, + $where_values, + $config['block_size'], + $offset + ); - case 'oracle': - // Oracle doesn't have TIME type, so we should transform the DATE value - $new_time_from = "TO_CHAR(periodically_time_from, 'HH24:MI:SS') AS periodically_time_from"; - $new_time_to = "TO_CHAR(periodically_time_to, 'HH24:MI:SS') AS periodically_time_to"; - - $time_from_key = array_search('periodically_time_from', $columns); - $time_to_key = array_search('periodically_time_to', $columns); - - if ($time_from_key !== false) { - $columns[$time_from_key] = $new_time_from; - } - - if ($time_to_key !== false) { - $columns[$time_to_key] = $new_time_to; - } - - $columns_str = implode(',', $columns); - - $set = []; - $set['limit'] = $config['block_size']; - $set['offset'] = $offset; - - $sql = "SELECT $columns_str - FROM tplanned_downtime - WHERE $where_values - ORDER BY type_execution DESC, date_from DESC"; - - $sql = oracle_recode_query($sql, $set); - break; - } - - $sql_count = "SELECT COUNT(id) AS num - FROM tplanned_downtime - WHERE $where_values"; + $sql_count = sprintf( + 'SELECT COUNT(id) AS num + FROM tplanned_downtime + WHERE %s', + $where_values + ); $downtimes = db_get_all_rows_sql($sql); $downtimes_number_res = db_get_all_rows_sql($sql_count); - $downtimes_number = $downtimes_number_res != false ? $downtimes_number_res[0]['num'] : 0; -} else { - $downtimes = []; + $downtimes_number = ($downtimes_number_res !== false) ? $downtimes_number_res[0]['num'] : 0; } +$url_list = 'index.php?sec=extensions&sec2=godmode/agentes/planned_downtime.list'; +$url_editor = 'index.php?sec=extensions&sec2=godmode/agentes/planned_downtime.editor'; // No downtimes cause the user has not anyone. -if (!$downtimes && !$filter_performed) { +if ($downtimes === false && $filter_performed === false) { include_once $config['homedir'].'/general/first_task/planned_downtime.php'; -} -// No downtimes cause the user performed a search. -else if (!$downtimes) { +} else if ($downtimes === false) { + // No downtimes cause the user performed a search. // Filter form. - echo "
'; @@ -367,22 +484,25 @@ else if (!$downtimes) { echo ' '; -} -// Has downtimes. -else { - echo "'; - ui_pagination($downtimes_number, "index.php?sec=extensions&sec2=godmode/agentes/planned_downtime.list&$filter_params_str", $offset); + ui_pagination( + $downtimes_number, + $url_list.'&'.$filter_params_str, + $offset + ); // User groups with AR, AD or AW permission. $groupsAD = users_get_groups($config['id_user'], $access); @@ -403,7 +523,9 @@ else { $table->head['configuration'] = __('Configuration'); $table->head['running'] = __('Running'); - if ($write_permisson || $manage_permisson) { + if ($write_permisson === true + || $manage_permisson === true + ) { $table->head['stop'] = __('Stop downtime'); $table->head['copy'] = __('Copy'); $table->head['edit'] = __('Edit'); @@ -414,7 +536,9 @@ else { $table->align['group'] = 'center'; $table->align['running'] = 'center'; - if ($write_permisson || $manage_permisson) { + if ($write_permisson === true + || $manage_permisson === true + ) { $table->align['stop'] = 'center'; $table->align['edit'] = 'center'; $table->align['delete'] = 'center'; @@ -430,7 +554,7 @@ else { WHERE id_downtime = '.$downtime['id'] ); - $data['name'] = $downtime['name']." ($total)"; + $data['name'] = $downtime['name'].' ('.$total.')'; $data['description'] = $downtime['description']; $data['group'] = ui_print_group_icon($downtime['id_group'], true); @@ -451,7 +575,7 @@ else { $data['configuration'] = reporting_format_planned_downtime_dates($downtime); - if ($downtime['executed'] == 0) { + if ((int) $downtime['executed'] === 0) { $data['running'] = html_print_image( 'images/pixel_red.png', true, @@ -474,43 +598,105 @@ else { } // If user have writting permissions. - if (in_array($downtime['id_group'], $groupsAD)) { - // Stop button - if ($downtime['type_execution'] == 'once' && $downtime['executed'] == 1) { - if (check_acl_restricted_all($config['id_user'], $downtime['id_group'], 'AW') - || check_acl_restricted_all($config['id_user'], $downtime['id_group'], 'AD') + if (in_array($downtime['id_group'], $groupsAD) === true) { + // Stop button. + if ($downtime['type_execution'] === 'once' + && (int) $downtime['executed'] === 1 + ) { + if ((bool) check_acl_restricted_all($config['id_user'], $downtime['id_group'], 'AW') === true + || (bool) check_acl_restricted_all($config['id_user'], $downtime['id_group'], 'AD') === true ) { - $data['stop'] = ''.html_print_image('images/cancel.png', true, ['title' => __('Stop downtime')]); + $url_list_params = $url_list.'&stop_downtime=1&id_downtime='.$downtime['id'].'&'.$filter_params_str; + $data['stop'] = ''; + $data['stop'] .= html_print_image( + 'images/cancel.png', + true, + ['title' => __('Stop downtime')] + ); } else { - $data['stop'] = html_print_image('images/cancel.png', true, ['title' => __('Stop downtime')]); + $data['stop'] = html_print_image( + 'images/cancel.png', + true, + ['title' => __('Stop downtime')] + ); } } else { $data['stop'] = ''; } // Edit & delete buttons. - if ($downtime['executed'] == 0) { - if (check_acl_restricted_all($config['id_user'], $downtime['id_group'], 'AW') - || check_acl_restricted_all($config['id_user'], $downtime['id_group'], 'AD') + if ((int) $downtime['executed'] === 0) { + if ((bool) check_acl_restricted_all($config['id_user'], $downtime['id_group'], 'AW') === true + || (bool) check_acl_restricted_all($config['id_user'], $downtime['id_group'], 'AD') === true ) { // Copy. - $data['copy'] = ''.html_print_image('images/copy.png', true, ['title' => __('Copy'), 'class' => 'invert_filter']).''; + $data['copy'] = ''; + $data['copy'] .= html_print_image( + 'images/copy.png', + true, + [ + 'title' => __('Copy'), + 'class' => 'invert_filter', + ] + ); + $data['copy'] .= ''; + // Edit. - $data['edit'] = ''.html_print_image('images/config.png', true, ['title' => __('Update'), 'class' => 'invert_filter']).''; + $data['edit'] = ''; + $data['edit'] .= html_print_image( + 'images/config.png', + true, + [ + 'title' => __('Update'), + 'class' => 'invert_filter', + ] + ); + $data['edit'] .= ''; + // Delete. - $data['delete'] = ''.html_print_image('images/cross.png', true, ['title' => __('Delete'), 'class' => 'invert_filter']); + $url_delete = $url_list.'&delete_downtime=1&id_downtime='.$downtime['id'].'&'.$filter_params_str; + $data['delete'] = ''; + $data['delete'] .= html_print_image( + 'images/cross.png', + true, + [ + 'title' => __('Delete'), + 'class' => 'invert_filter', + ] + ); + $data['delete'] .= ''; } else { $data['edit'] = ''; $data['delete'] = ''; } - } else if ($downtime['executed'] == 1 && $downtime['type_execution'] == 'once') { - if (check_acl_restricted_all($config['id_user'], $downtime['id_group'], 'AW') - || check_acl_restricted_all($config['id_user'], $downtime['id_group'], 'AD') + } else if ((int) $downtime['executed'] === 1 + && $downtime['type_execution'] === 'once' + ) { + if ((bool) check_acl_restricted_all($config['id_user'], $downtime['id_group'], 'AW') === true + || (bool) check_acl_restricted_all($config['id_user'], $downtime['id_group'], 'AD') === true ) { // Copy. - $data['copy'] = ''.html_print_image('images/copy.png', true, ['title' => __('Copy'), 'class' => 'invert_filter']).''; + $data['copy'] = ''; + $data['copy'] .= html_print_image( + 'images/copy.png', + true, + [ + 'title' => __('Copy'), + 'class' => 'invert_filter', + ] + ); + $data['copy'] .= ''; // Edit. - $data['edit'] = ''.html_print_image('images/config.png', true, ['title' => __('Update'), 'class' => 'invert_filter']).''; + $data['edit'] = ''; + $data['edit'] .= html_print_image( + 'images/config.png', + true, + [ + 'title' => __('Update'), + 'class' => 'invert_filter', + ] + ); + $data['edit'] .= ''; // Delete. $data['delete'] = __('N/A'); } else { @@ -529,7 +715,9 @@ else { $data['delete'] = ''; } - if (!empty($malformed_downtimes_exist) && isset($malformed_downtimes[$downtime['id']])) { + if (empty($malformed_downtimes_exist) === false + && isset($malformed_downtimes[$downtime['id']]) === true + ) { $next_row_num = count($table->data); $table->cellstyle[$next_row_num][0] = 'color: red'; $table->cellstyle[$next_row_num][1] = 'color: red'; @@ -542,7 +730,17 @@ else { } html_print_table($table); - ui_pagination($downtimes_number, "index.php?sec=extensions&sec2=godmode/agentes/planned_downtime.list&$filter_params_str", $offset, 0, false, 'offset', true, 'pagination-bottom'); + ui_pagination( + $downtimes_number, + $url_list.'&'.$filter_params_str, + $offset, + 0, + false, + 'offset', + true, + 'pagination-bottom' + ); + echo ' '; // Create button. - if ($write_permisson) { + if ($write_permisson === true) { echo ' '; - echo ''; } echo ''; } -ui_require_jquery_file('ui.datepicker-'.get_user_language(), 'include/javascript/i18n/'); +ui_require_jquery_file( + 'ui.datepicker-'.get_user_language(), + 'include/javascript/i18n/' +); ?>