Permalink
Browse files

Ticket #3734: Convert growl related code to use pear-Net_Growl port

  • Loading branch information...
rbgarga committed Aug 11, 2016
1 parent 50950d0 commit e7cf30eb6fcd0cc2ec20dd267775b03db2b3667f
Showing with 97 additions and 42 deletions.
  1. +81 −32 src/etc/inc/notices.inc
  2. +16 −10 src/usr/local/www/system_advanced_notifications.php
View
@@ -393,13 +393,18 @@ function send_smtp_message($message, $subject = "(no subject)", $force = false)
* returns true if message was sent
******/
function notify_via_growl($message, $force=false) {
require_once("growl.class");
require_once("Net/Growl/Autoload.php");
global $config, $g;
if (isset($config['notifications']['growl']['disable']) && !$force) {
return;
}
if (empty($config['notifications']['growl']['ipaddress'])) {
return;
}
/* Do NOT send the same message twice */
if (file_exists("/var/db/growlnotices_lastmsg.txt")) {
$lastmsg = trim(file_get_contents("/var/db/growlnotices_lastmsg.txt"));
@@ -408,26 +413,43 @@ function notify_via_growl($message, $force=false) {
}
}
$ip = $config['notifications']['growl']['ipaddress'];
if (!is_ipaddr($ip) && !(dns_check_record($ip, A) || dns_check_record($ip, AAAA))) {
$err_msg = gettext(
"Growl IP Address is invalid. Check the setting in System Advanced Notifications.");
log_error($err_msg);
return($err_msg);
}
$hostname = $config['system']['hostname'] . "." . $config['system']['domain'];
$growl_ip = $config['notifications']['growl']['ipaddress'];
$growl_password = $config['notifications']['growl']['password'];
$growl_name = $config['notifications']['growl']['name'];
$growl_notification = $config['notifications']['growl']['notification_name'];
if (!empty($growl_ip)) {
if (is_ipaddr($growl_ip) || dns_check_record($growl_ip, A) || dns_check_record($growl_ip, AAAA)) {
$growl = new Growl($growl_ip, $growl_password, $growl_name);
$growl->notify("{$growl_notification}", gettext(sprintf("%s (%s) - Notification", $g['product_name'], $hostname)), "{$message}");
} else {
// file_notice to local only to prevent file_notice from calling back to growl in a loop
file_notice("growl", gettext("Growl IP Address is invalid. Check the setting in System Advanced Notifications."), "General", "", 1, true);
}
$password = $config['notifications']['growl']['password'];
$name = $config['notifications']['growl']['name'];
$notification = $config['notifications']['growl']['notification_name'];
$options = array(
'host' => $ip,
'protocol' => 'gntp',
'timeout' => 20
);
try {
$growl = Net_Growl::singleton($name, null, $password, $options);
//$name = 'GROWL_NOTIFY_STATUS';
$title = sprintf(gettext("%s (%s) - Notification"), $g['product_name'], $hostname);
$growl->publish($name, $title, $message);
} catch (Net_Growl_Exception $e) {
$err_msg = sprintf(gettext(
'Could not send Growl notification to %1$s -- Error: %2$s'),
$ip, $e->getMessage());
log_error($err_msg);
return($err_msg);
}
/* Store last message sent to avoid spamming */
$fd = fopen("/var/db/growlnotices_lastmsg.txt", "w");
fwrite($fd, $message);
fclose($fd);
@file_put_contents("/var/db/growlnotices_lastmsg.txt", $message);
return;
}
/****f* notices/register_via_growl
@@ -439,23 +461,50 @@ function notify_via_growl($message, $force=false) {
* none
******/
function register_via_growl() {
require_once("growl.class");
require_once("Net/Growl/Autoload.php");
global $config;
$growl_ip = $config['notifications']['growl']['ipaddress'];
$growl_password = $config['notifications']['growl']['password'];
$growl_name = $config['notifications']['growl']['name'];
$growl_notification = $config['notifications']['growl']['notification_name'];
if (!empty($growl_ip)) {
if (is_ipaddr($growl_ip) || dns_check_record($growl_ip, A) || dns_check_record($growl_ip, AAAA)) {
$growl = new Growl($growl_ip, $growl_password, $growl_name);
$growl->addNotification($growl_notification);
$growl->register();
} else {
// file_notice to local only to prevent file_notice from calling back to growl in a loop
file_notice("growl", gettext("Growl IP Address is invalid. Check the setting in System Advanced Notifications."), "General", "", 1, true);
}
if (empty($config['notifications']['growl']['ipaddress'])) {
return;
}
$ip = $config['notifications']['growl']['ipaddress'];
if (!is_ipaddr($ip) && !(dns_check_record($ip, A) || dns_check_record($ip, AAAA))) {
$err_msg = gettext(
"Growl IP Address is invalid. Check the setting in System Advanced Notifications.");
log_error($err_msg);
return($err_msg);
}
$name = $config['notifications']['growl']['name'];
$password = $config['notifications']['growl']['password'];
$app = new Net_Growl_Application(
$name,
null,
$password
);
$options = array(
'host' => $ip,
'protocol' => 'gntp',
'timeout' = 20
);
try {
$growl = Net_Growl::singleton($app, null, null, $options);
$growl->register();
} catch (Net_Growl_Exception $e) {
$err_msg = sprintf(gettext(
'Could not send register Growl on %1$s -- Error: %2$s'),
$ip, $e->getMessage());
log_error($err_msg);
return($err_msg);
}
return;
}
/* Notify via remote methods only - not via GUI. */
@@ -47,7 +47,7 @@
if ($config['notifications']['growl']['name']) {
$pconfig['name'] = $config['notifications']['growl']['name'];
} else {
$pconfig['name'] = 'PHP-Growl';
$pconfig['name'] = 'pfSense-Growl';
}
@@ -160,7 +160,13 @@
if (isset($config['notifications']['growl']['ipaddress'])) {
unlink_if_exists($g['vardb_path'] . "/growlnotices_lastmsg.txt");
register_via_growl();
notify_via_growl(sprintf(gettext("This is a test message from %s. It is safe to ignore this message."), $g['product_name']), true);
$test_result = notify_via_growl(sprintf(gettext("This is a test message from %s. It is safe to ignore this message."), $g['product_name']), true);
if (empty($test_result)) {
$test_result = gettext("Growl testing notification successfully sent");
$test_class = 'success';
} else {
$test_class = 'danger';
}
}
}
@@ -169,12 +175,12 @@
if (file_exists("/var/db/notices_lastmsg.txt")) {
unlink("/var/db/notices_lastmsg.txt");
}
$smtp_test_result = notify_via_smtp(sprintf(gettext("This is a test message from %s. It is safe to ignore this message."), $g['product_name']), true);
if (empty($smtp_test_result)) {
$smtp_test_result = gettext("SMTP testing e-mail successfully sent");
$smtp_test_class = 'success';
$test_result = notify_via_smtp(sprintf(gettext("This is a test message from %s. It is safe to ignore this message."), $g['product_name']), true);
if (empty($test_result)) {
$test_result = gettext("SMTP testing e-mail successfully sent");
$test_class = 'success';
} else {
$smtp_test_class = 'danger';
$test_class = 'danger';
}
}
}
@@ -186,8 +192,8 @@
print_input_errors($input_errors);
}
if ($smtp_test_result) {
print_info_box($smtp_test_result, $smtp_test_class);
if ($test_result) {
print_info_box($test_result, $test_class);
}
$tab_array = array();
@@ -216,7 +222,7 @@
'Registration Name',
'text',
$pconfig['name'],
['placeholder' => 'PHP-Growl']
['placeholder' => 'pfSense-Growl']
))->setHelp('Enter the name to register with the Growl server.');
$section->addInput(new Form_Input(

0 comments on commit e7cf30e

Please sign in to comment.