__('Delete', 'thold'),
2 => __('Duplicate', 'thold')
];
$assoc_actions = [
1 => __('Associate', 'thold'),
2 => __('Disassociate', 'thold')
];
// present a tabbed interface
$tabs_thold = [
'general' => __('General', 'thold'),
'hosts' => __('Devices', 'thold'),
'tholds' => __('Thresholds', 'thold'),
'templates' => __('Templates', 'thold')
];
$tabs_thold = api_plugin_hook_function('notify_list_tabs', $tabs_thold);
set_default_action();
switch (get_request_var('action')) {
case 'save':
form_save();
break;
case 'actions':
form_actions();
break;
case 'edit':
top_header();
edit();
bottom_footer();
break;
default:
top_header();
lists();
bottom_footer();
break;
}
/* --------------------------
The Save Function
-------------------------- */
/**
* Validates and saves the notification list edit form's submitted
* fields, rejecting the save if the same email address appears in both
* the To and BCC lists, then redirects back to the edit form for the
* saved (or original) list id. Called from this script's main
* request-dispatch switch when action=save.
*
* @return void
*/
function form_save() {
if (isset_request_var('save_component')) {
$save['id'] = get_filter_request_var('id');
$save['name'] = form_input_validate(get_nfilter_request_var('name'), 'name', '', false, 3);
$save['enabled'] = isset_request_var('enabled') ? 'on' : '';
$save['description'] = form_input_validate(get_nfilter_request_var('description'), 'description', '', false, 3);
$save['emails'] = form_input_validate(get_nfilter_request_var('emails'), 'emails', '', true, 3);
$save['bcc_emails'] = form_input_validate(get_nfilter_request_var('bcc_emails'), 'bcc_emails', '', true, 3);
$save['format_file'] = form_input_validate(get_nfilter_request_var('format_file'), 'format_file', '', true, 3);
$bccs = explode(',', $save['bcc_emails']);
$emails = explode(',', $save['emails']);
$bad = false;
foreach ($bccs as $email) {
if (array_search(trim($email), $emails, true) !== false) {
raise_message(
'duplicate_email',
__('ERROR: You can not have your Email in both the To and BCC lines. The Email below is invalid!
%s', $email, 'THOLD'),
MESSAGE_LEVEL_WARN
);
$_SESSION['sess_error_fields']['bcc_emails'] = 'bcc_emails';
$bad = true;
break;
}
}
if (!$bad) {
foreach ($emails as $email) {
if (array_search(trim($email), $bccs, true) !== false) {
raise_message(
'duplicate_email',
__('ERROR: You can not have your Email in both the To and BCC lines. The Email below is invalid!
%s', $email, 'THOLD'),
MESSAGE_LEVEL_WARN
);
$_SESSION['sess_error_fields']['bcc_emails'] = 'bcc_emails';
$bad = true;
break;
}
}
}
if (!is_error_message()) {
$id = sql_save($save, 'plugin_notification_lists');
if ($id) {
raise_message(1);
} else {
raise_message(2);
}
}
}
header('Location: notify_lists.php?header=false&action=edit&id=' . (empty($id) ? get_request_var('id') : $id));
}
/* ------------------------
The 'actions' function
------------------------ */
/**
* Handles the bulk-action confirmation page/submission for the
* notification lists view and its host/threshold/template association
* sub-views, validating the requested action against the combined set
* of list-level and association-level allowed actions before
* dispatching. Called from this script's main request-dispatch switch
* when action=actions.
*
* @return void
*
* @global array $actions Map of drp_action value => action label
* for list-level bulk actions.
* @global array $assoc_actions Map of drp_action value => action label
* for host/threshold/template association
* bulk actions.
*/
function form_actions() {
global $actions, $assoc_actions;
// ================= input validation =================
/*
* get_filter_request_var() stores the value as an int, so the comparison
* has to be made on strings for the strict check to mean anything.
*/
get_filter_request_var('drp_action');
$valid_actions = array_map('strval', array_keys($actions + $assoc_actions));
if (!in_array((string) get_request_var('drp_action'), $valid_actions, true)) {
raise_message(40);
header('Location: notify_lists.php?header=false');
exit;
}
// ====================================================
// if we are to save this form, instead of display it
if (isset_request_var('selected_items')) {
$selected_items = sanitize_unserialize_selected_items(get_nfilter_request_var('selected_items'));
if (isset_request_var('save_list')) {
if ($selected_items != false) {
if (get_request_var('drp_action') == '1') { // delete
/*
* Bind positionally on the values: sanitize_unserialize_selected_items()
* preserves the submitted array's keys, and a string key would be read
* as a named parameter.
*/
$ids = array_map('intval', array_values($selected_items));
$placeholders = implode(', ', array_fill(0, cacti_sizeof($ids), '?'));
/*
* Issued as SQL rather than through db_begin_transaction() and
* friends: Cacti's db_commit_transaction() gates the commit on
* SELECT @@in_transaction, which only MariaDB defines. On MySQL that
* query fails, the commit is skipped, and every write here is
* discarded when the connection closes.
*/
db_execute('START TRANSACTION');
/*
* The host reset deliberately omits the deleted = "" predicate its
* siblings carry: a soft-deleted device that is later restored must
* not come back pointing at a list that no longer exists.
*/
$statements = [
'DELETE FROM plugin_notification_lists WHERE id IN (' . $placeholders . ')',
'UPDATE host SET thold_send_email = 0 WHERE thold_send_email = 2 AND deleted = "" AND thold_host_email IN (' . $placeholders . ')',
'UPDATE host SET thold_send_email = 1 WHERE thold_send_email = 3 AND deleted = "" AND thold_host_email IN (' . $placeholders . ')',
'UPDATE host SET thold_host_email = 0 WHERE thold_host_email IN (' . $placeholders . ')',
'UPDATE thold_data SET notify_warning = 0 WHERE notify_warning IN (' . $placeholders . ')',
'UPDATE thold_data SET notify_alert = 0 WHERE notify_alert IN (' . $placeholders . ')',
'UPDATE thold_template SET notify_warning = 0 WHERE notify_warning IN (' . $placeholders . ')',
'UPDATE thold_template SET notify_alert = 0 WHERE notify_alert IN (' . $placeholders . ')'
];
$ok = true;
foreach ($statements as $sql) {
if (!db_execute_prepared($sql, $ids)) {
$ok = false;
break;
}
}
if ($ok) {
db_execute('COMMIT');
} else {
db_execute('ROLLBACK');
}
} elseif (get_request_var('drp_action') == '2') { // duplicate
$i = 1;
foreach ($selected_items as $item) {
// get list to be duplicated
$list = db_fetch_row_prepared('SELECT *
FROM plugin_notification_lists
WHERE id = ?',
[$item]);
// see if there is already a list with the new name
$exists = db_fetch_cell_prepared('SELECT COUNT(*)
FROM plugin_notification_lists
WHERE name = ?',
[get_nfilter_request_var('name')]);
if ($exists > 0) {
$name = get_nfilter_request_var('name') . ' (' . $i . ')';
$i ++;
} else {
$name = get_nfilter_request_var('name');
}
$save['id'] = 0;
$save['name'] = $name;
$save['description'] = $list['description'];
$save['emails'] = $list['emails'];
$save['bcc_emails'] = $list['bcc_emails'];
$id = sql_save($save, 'plugin_notification_lists');
if ($id) {
raise_message(1);
} else {
raise_message(2);
}
}
}
}
header('Location: notify_lists.php?header=false');
exit;
}
if (isset_request_var('save_associate')) {
if ($selected_items != false) {
get_filter_request_var('id');
get_filter_request_var('notification_action');
get_filter_request_var('notification_warning_action');
get_filter_request_var('notification_alert_action');
db_execute('START TRANSACTION');
$ok = true;
if (get_request_var('drp_action') == '1') { // associate
for ($i = 0; ($i < cacti_sizeof($selected_items)); $i++) {
// set the notification list
$ok = db_execute_prepared('UPDATE host
SET thold_host_email = ?
WHERE id = ?
AND deleted = ""',
[get_request_var('id'), $selected_items[$i]]) && $ok;
// set the global/list election
$ok = db_execute_prepared('UPDATE host
SET thold_send_email = ?
WHERE id = ?
AND deleted = ""',
[get_request_var('notification_action'), $selected_items[$i]]) && $ok;
if (get_request_var('notification_warning_action') > 0) {
// clear other settings
if (get_request_var('notification_warning_action') == 1) {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_data AS td
LEFT JOIN thold_template AS tt
ON td.thold_template_id = tt.id
SET td.notify_warning = ?
WHERE td.host_id = ?
AND (tt.notify_templated = "" OR tt.notify_templated IS NULL)',
[get_request_var('id'), $selected_items[$i]]) && $ok;
// clear other items
$ok = db_execute_prepared('UPDATE thold_data AS td
LEFT JOIN thold_template AS tt
ON td.thold_template_id = tt.id
SET td.notify_warning_extra = \'\'
WHERE td.host_id = ?
AND (tt.notify_templated = "" OR tt.notify_templated IS NULL)',
[$selected_items[$i]]) && $ok;
} else {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_data AS td
LEFT JOIN thold_template AS tt
ON td.thold_template_id = tt.id
SET td.notify_warning = ?
WHERE td.host_id = ?
AND (tt.notify_templated = "" OR tt.notify_templated IS NULL)',
[get_request_var('id'), $selected_items[$i]]) && $ok;
}
}
if (get_request_var('notification_alert_action') > 0) {
// clear other settings
if (get_request_var('notification_alert_action') == 1) {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_data AS td
LEFT JOIN thold_template AS tt
ON td.thold_template_id = tt.id
SET td.notify_alert = ?
WHERE td.host_id = ?
AND (tt.notify_templated = "" OR tt.notify_templated IS NULL)',
[get_request_var('id'), $selected_items[$i]]) && $ok;
// clear other items
$ok = db_execute_prepared('UPDATE thold_data AS td
LEFT JOIN thold_template AS tt
ON td.thold_template_id = tt.id
SET td.notify_extra = \'\'
WHERE host_id = ?
AND (tt.notify_templated = "" OR tt.notify_templated IS NULL)',
[$selected_items[$i]]) && $ok;
// remove legacy contacts
$ok = db_execute_prepared('DELETE pttc
FROM plugin_thold_threshold_contact AS pttc
INNER JOIN thold_data AS td
ON pttc.thold_id = td.id
LEFT JOIN thold_template AS tt
ON td.thold_template_id = tt.id
WHERE td.host_id = ?
AND (tt.notify_templated = "" OR tt.notify_templated IS NULL)',
[$selected_items[$i]]) && $ok;
} else {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_data AS td
LEFT JOIN thold_template AS tt
ON td.thold_template_id = tt.id
SET td.notify_alert = ?
WHERE td.host_id = ?
AND (tt.notify_templated = "" OR tt.notify_templated IS NULL)',
[get_request_var('id'), $selected_items[$i]]) && $ok;
}
}
if (!$ok) {
break;
}
}
} elseif (get_request_var('drp_action') == '2') { // disassociate
for ($i = 0; ($i < cacti_sizeof($selected_items)); $i++) {
// set the notification list
$ok = db_execute_prepared('UPDATE host
SET thold_host_email = 0
WHERE id = ?
AND deleted = ""',
[$selected_items[$i]]) && $ok;
// set the global/list election
$ok = db_execute_prepared('UPDATE host
SET thold_send_email = ?
WHERE id = ?
AND deleted = ""',
[get_request_var('notification_action'), $selected_items[$i]]) && $ok;
if (get_request_var('notification_warning_action') > 0) {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_data AS td
LEFT JOIN thold_template AS tt
ON td.thold_template_id = tt.id
SET td.notify_warning = 0
WHERE td.host_id = ?
AND (tt.notify_templated = "" OR tt.notify_templated IS NULL)
AND td.notify_warning = ?',
[$selected_items[$i], get_request_var('id')]) && $ok;
}
if (get_request_var('notification_alert_action') > 0) {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_data AS td
LEFT JOIN thold_template AS tt
ON td.thold_template_id = tt.id
SET td.notify_alert = 0
WHERE td.host_id = ?
AND (tt.notify_templated = "" OR tt.notify_templated IS NULL)
AND td.notify_alert = ?',
[$selected_items[$i], get_request_var('id')]) && $ok;
}
if (!$ok) {
break;
}
}
}
if ($ok) {
db_execute('COMMIT');
} else {
db_execute('ROLLBACK');
}
}
header('Location: notify_lists.php?header=false&action=edit&tab=hosts&id=' . get_request_var('id'));
exit;
}
if (isset_request_var('save_templates')) {
if ($selected_items != false) {
get_filter_request_var('id');
get_filter_request_var('notification_action');
get_filter_request_var('notification_warning_action');
get_filter_request_var('notification_alert_action');
db_execute('START TRANSACTION');
$ok = true;
$update_template = [];
if (get_request_var('drp_action') == '1') { // associate
for ($i = 0; ($i < cacti_sizeof($selected_items)); $i++) {
if (get_request_var('notification_warning_action') > 0) {
// clear other settings
if (get_request_var('notification_warning_action') == 1) {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_template
SET notify_warning = ?
WHERE id = ?',
[get_request_var('id'), $selected_items[$i]]) && $ok;
// clear other items
$ok = db_execute_prepared("UPDATE thold_template
SET notify_warning_extra = ''
WHERE id = ?",
[$selected_items[$i]]) && $ok;
} else {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_template
SET notify_warning = ?
WHERE id = ?',
[get_request_var('id'), $selected_items[$i]]) && $ok;
}
}
if (get_request_var('notification_alert_action') > 0) {
// clear other settings
if (get_request_var('notification_alert_action') == 1) {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_template
SET notify_alert = ?
WHERE id = ?',
[get_request_var('id'), $selected_items[$i]]) && $ok;
// clear other items
$ok = db_execute_prepared("UPDATE thold_template
SET notify_extra = ''
WHERE id = ?",
[$selected_items[$i]]) && $ok;
$ok = db_execute_prepared('DELETE FROM plugin_thold_template_contact
WHERE template_id = ?',
[$selected_items[$i]]) && $ok;
} else {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_template
SET notify_alert = ?
WHERE id = ?',
[get_request_var('id'), $selected_items[$i]]) && $ok;
}
}
$update_template[] = $selected_items[$i];
if (!$ok) {
break;
}
}
} elseif (get_request_var('drp_action') == '2') { // disassociate
for ($i = 0; ($i < cacti_sizeof($selected_items)); $i++) {
if (get_request_var('notification_warning_action') > 0) {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_template
SET notify_warning = 0
WHERE id = ?
AND notify_warning = ?',
[$selected_items[$i], get_request_var('id')]) && $ok;
}
if (get_request_var('notification_alert_action') > 0) {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_template
SET notify_alert = 0
WHERE id = ?
AND notify_alert = ?',
[$selected_items[$i], get_request_var('id')]) && $ok;
}
$update_template[] = $selected_items[$i];
if (!$ok) {
break;
}
}
}
if ($ok) {
db_execute('COMMIT');
// Propagate template changes to threshold instances after the
// notification assignment is committed so this cascade does not
// participate in the transaction boundary.
foreach ($update_template as $template_id) {
thold_template_update_thresholds($template_id);
}
} else {
db_execute('ROLLBACK');
}
}
header('Location: notify_lists.php?header=false&action=edit&tab=templates&id=' . get_request_var('id'));
exit;
}
if (isset_request_var('save_tholds')) {
if ($selected_items != false) {
get_filter_request_var('id');
get_filter_request_var('notification_action');
get_filter_request_var('notification_warning_action');
get_filter_request_var('notification_alert_action');
db_execute('START TRANSACTION');
$ok = true;
if (get_request_var('drp_action') == '1') { // associate
for ($i = 0; ($i < cacti_sizeof($selected_items)); $i++) {
if (get_request_var('notification_warning_action') > 0) {
// clear other settings
if (get_request_var('notification_warning_action') == 1) {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_data
SET notify_warning = ?
WHERE id = ?',
[get_request_var('id'), $selected_items[$i]]) && $ok;
// clear other items
$ok = db_execute_prepared("UPDATE thold_data
SET notify_warning_extra = ''
WHERE id = ?",
[$selected_items[$i]]) && $ok;
} else {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_data
SET notify_warning = ?
WHERE id = ?',
[get_request_var('id'), $selected_items[$i]]) && $ok;
}
}
if (get_request_var('notification_alert_action') > 0) {
// clear other settings
if (get_request_var('notification_alert_action') == 1) {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_data
SET notify_alert = ?
WHERE id = ?',
[get_request_var('id'), $selected_items[$i]]) && $ok;
// clear other items
$ok = db_execute_prepared("UPDATE thold_data
SET notify_extra = ''
WHERE id = ?",
[$selected_items[$i]]) && $ok;
$ok = db_execute_prepared('DELETE FROM plugin_thold_threshold_contact
WHERE thold_id = ?',
[$selected_items[$i]]) && $ok;
} else {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_data
SET notify_alert = ?
WHERE id = ?',
[get_request_var('id'), $selected_items[$i]]) && $ok;
}
}
if (!$ok) {
break;
}
}
} elseif (get_request_var('drp_action') == '2') { // disassociate
for ($i = 0; ($i < cacti_sizeof($selected_items)); $i++) {
if (get_request_var('notification_warning_action') > 0) {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_data
SET notify_warning = 0
WHERE id = ?
AND notify_warning = ?',
[$selected_items[$i], get_request_var('id')]) && $ok;
}
if (get_request_var('notification_alert_action') > 0) {
// set the notification list
$ok = db_execute_prepared('UPDATE thold_data
SET notify_alert = 0
WHERE id = ?
AND notify_alert = ?',
[$selected_items[$i], get_request_var('id')]) && $ok;
}
if (!$ok) {
break;
}
}
}
if ($ok) {
db_execute('COMMIT');
} else {
db_execute('ROLLBACK');
}
}
header('Location: notify_lists.php?header=false&action=edit&tab=tholds&id=' . get_request_var('id'));
exit;
} else {
api_plugin_hook_function('notify_list_save', $_POST);
header('Location: notify_lists.php?header=false&action=edit&tab=tholds&id=' . get_request_var('id'));
exit;
}
}
// setup some variables
$list = '';
$array = [];
$list_name = '';
if (isset_request_var('id')) {
$list_name = db_fetch_cell_prepared('SELECT name
FROM plugin_notification_lists
WHERE id = ?',
[get_filter_request_var('id')]);
}
if (isset_request_var('save_list')) {
// loop through each of the notification lists selected on the previous page and get more info about them
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
// ================= input validation =================
input_validate_input_number($matches[1]);
// ====================================================
$name = db_fetch_cell_prepared('SELECT name
FROM plugin_notification_lists
WHERE id = ?',
[$matches[1]]);
$list .= '
" . __('Click \'Continue\' to Delete Notification Lists(s). Any Device(s) or Threshold(s) associated with the List(s) will be reverted to the default.', 'thold') . "
";
html_end_box();
form_end();
bottom_footer();
} elseif (isset_request_var('save_templates')) {
// loop through each of the notification lists selected on the previous page and get more info about them
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
// ================= input validation =================
input_validate_input_number($matches[1]);
// ====================================================
$name = db_fetch_cell_prepared('SELECT name
FROM thold_template
WHERE id = ?',
[$matches[1]]);
$list .= '
";
html_end_box();
form_end();
bottom_footer();
} elseif (isset_request_var('save_tholds')) {
// loop through each of the notification lists selected on the previous page and get more info about them
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
// ================= input validation =================
input_validate_input_number($matches[1]);
// ====================================================
$name = db_fetch_cell_prepared('SELECT name_cache
FROM thold_data
WHERE id = ?',
[$matches[1]]);
$list .= '
";
html_end_box();
form_end();
bottom_footer();
} elseif (isset_request_var('save_associate')) {
// loop through each of the notification lists selected on the previous page and get more info about them
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
// ================= input validation =================
input_validate_input_number($matches[1]);
// ====================================================
$name = db_fetch_cell_prepared('SELECT description
FROM host WHERE id = ?',
[$matches[1]]);
$list .= '
" . __esc('Click \'Continue\' to Associate the Notification List \'%s\' with the Device(s) below.', $list_name, 'thold') . '
' . __('You may also Associate the Devices Thresholds as well. However, these Device Thresholds will allow the Threshold Notification List to be overwritten.', 'thold') . "
$list
" . __('Resulting Membership:', 'thold') . ' ';
form_dropdown('notification_action', [2 => __('Notification List Only', 'thold'), 3 => __('Notification and Global Lists', 'thold')], '', '', 2, '', '');
print '
" . __esc('Click \'Continue\' to Disassociate the Notification List \'%s\' from the Device(s) below.', $list_name, 'thold') . '
' . __('You may also Disassociate the Devices Thresholds as well. However, these Device Thresholds will allow the Threshold Notification List to be overwritten.', 'thold') . "
';
}
html_end_box(false);
if (cacti_sizeof($lists)) {
print $nav;
}
form_hidden_box('save_list', '1', '');
// draw the dropdown containing a list of available actions for this form
draw_actions_dropdown($actions);
form_end();
}