Project

General

Profile

Download (17 KB) Statistics
| Branch: | Tag: | Revision:
1 9774f564 Colin Smith
<?php
2 09221bc3 Renato Botelho
/*
3 ac24dc24 Renato Botelho
 * notices.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 c5d81585 Renato Botelho
 * Copyright (c) 2005 Colin Smith (ethethlay@gmail.com)
7 38809d47 Renato Botelho do Couto
 * Copyright (c) 2005-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9 402c98a2 Reid Linnemann
 * Copyright (c) 2014-2023 Rubicon Communications, LLC (Netgate)
10 ac24dc24 Renato Botelho
 * All rights reserved.
11
 *
12 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15 ac24dc24 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
17 ac24dc24 Renato Botelho
 *
18 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23 9774f564 Colin Smith
 */
24
25
require_once("globals.inc");
26 419af712 Denny Page
require_once("functions.inc");
27 0c884086 jim-p
require_once("led.inc");
28 9774f564 Colin Smith
29 2568e151 Christian McDonald
$notice_path = g_get('tmp_path') . '/notices';
30 d5155a01 Viktor G
global $smtp_authentication_mechanisms;
31 c4249322 Phil Davis
$smtp_authentication_mechanisms = array(
32
	'PLAIN' => 'PLAIN',
33
	'LOGIN' => 'LOGIN');
34
/* Other SMTP Authentication Mechanisms that could be supported.
35
 * Note that MD5 is no longer considered secure.
36
 *	'GSSAPI' => 'GSSAPI ' . gettext("Generic Security Services Application Program Interface")
37
 *	'DIGEST-MD5' => 'DIGEST-MD5 ' . gettext("Digest access authentication")
38
 *	'MD5' => 'MD5'
39
 *	'CRAM-MD5' => 'CRAM-MD5'
40
*/
41 18c256c0 overtninja
global $pushover_sounds;
42
$pushover_sounds = array(
43 187da3ef overtninja
	'devicedefault' => 'Device Default',
44
	'pushover' => 'Pushover',
45 18c256c0 overtninja
	'bike' => 'Bike',
46
	'bugle' => 'Bugle',
47
	'cashregister' => 'Cash Register',
48
	'classical' => 'Classical',
49
	'cosmic' => 'Cosmic',
50
	'falling' => 'Falling',
51
	'gamelan' => 'Gamelan',
52 4864d7f6 Josh Soref
	'incoming' => 'Incoming',
53 18c256c0 overtninja
	'intermission' => 'Intermission',
54
	'magic' => 'Magic',
55
	'mechanical' => 'Mechanical',
56
	'pianobar' => 'Piano Bar',
57
	'siren' => 'Siren',
58
	'spacealarm' => 'Space Alarm',
59
	'tugboat' => 'Tug Boat',
60
	'alien' => 'Alien (long)',
61
	'climb' => 'Climb (long)',
62
	'persistent' => 'Persistent (long)',
63
	'echo' => 'Echo (long)',
64
	'updown' => 'Up Down (long)',
65
	'vibrate' => 'Vibrate only',
66 6b18e960 overtninja
	'none' => 'None (silent)');
67 9774f564 Colin Smith
68 91cae49d Scott Ullrich
/****f* notices/file_notice
69
 * NAME
70
 *   file_notice
71
 * INPUTS
72 642c6023 Phil Davis
 *	 $id, $notice, $category, $url, $priority, $local_only
73 91cae49d Scott Ullrich
 * RESULT
74 18c256c0 overtninja
 *   Files a notice and kicks off the various alerts, smtp, telegram, pushover, system log, LED's, etc.
75
 *   If $local_only is true then the notice is not sent to external places (smtp, telegram, pushover)
76 91cae49d Scott Ullrich
 ******/
77 642c6023 Phil Davis
function file_notice($id, $notice, $category = "General", $url = "", $priority = 1, $local_only = false) {
78 91cae49d Scott Ullrich
	/*
79
	 * $category - Category that this notice should be displayed under. This can be arbitrary,
80
	 * 	       but a page must be set to receive this messages for it to be displayed.
81
	 *
82
	 * $priority - A notice's priority. Higher numbers indicate greater severity.
83
	 *	       0 = informational, 1 = warning, 2 = error, etc. This may also be arbitrary,
84
	 */
85 9774f564 Colin Smith
	global $notice_path;
86 b37a2e8c Phil Davis
	if (!$queue = get_notices()) {
87
		$queue = array();
88
	}
89 9774f564 Colin Smith
	$queuekey = time();
90
	$toqueue = array(
91 e392cc2b Chris Buechler
				'id'		=> htmlentities($id),
92
				'notice'	=> htmlentities($notice),
93
				'url'		=> htmlentities($url),
94
				'category'	=> htmlentities($category),
95
				'priority'	=> htmlentities($priority),
96 9774f564 Colin Smith
			);
97 80b53805 Phil Davis
	while (isset($queue[$queuekey])) {
98
		$queuekey++;
99
	}
100 9774f564 Colin Smith
	$queue[$queuekey] = $toqueue;
101
	$queueout = fopen($notice_path, "w");
102 b37a2e8c Phil Davis
	if (!$queueout) {
103 6d44231f Phil Davis
		log_error(sprintf(gettext("Could not open %s for writing"), $notice_path));
104 34e1ff97 Scott Ullrich
		return;
105
	}
106 9774f564 Colin Smith
	fwrite($queueout, serialize($queue));
107
	fclose($queueout);
108 e8c516a0 Phil Davis
	log_error(sprintf(gettext("New alert found: %s"), $notice));
109 a0a125b9 Scott Ullrich
	/* soekris */
110 b37a2e8c Phil Davis
	if (file_exists("/dev/led/error")) {
111 5f9bcd7a Scott Ullrich
		exec("/bin/echo 1 > /dev/led/error");
112 b37a2e8c Phil Davis
	}
113 0c884086 jim-p
	/* wrap & alix */
114 1c9512f2 jim-p
	led_normalize();
115 0c884086 jim-p
	led_morse(1, 'sos');
116 642c6023 Phil Davis
	if (!$local_only) {
117 a2e35163 Phil Davis
		notify_all_remote($notice);
118 642c6023 Phil Davis
	}
119 e2c48999 Colin Smith
	return $queuekey;
120 9774f564 Colin Smith
}
121
122 91cae49d Scott Ullrich
/****f* notices/get_notices
123
 * NAME
124
 *   get_notices
125
 * INPUTS
126
 *	 $category
127
 * RESULT
128
 *   Returns a specific notices text
129
 ******/
130 9774f564 Colin Smith
function get_notices($category = "all") {
131 6955830f Ermal Lu?i
	global $g;
132
133 b37a2e8c Phil Davis
	if (file_exists("{$g['tmp_path']}/notices")) {
134 6955830f Ermal Lu?i
		$queue = unserialize(file_get_contents("{$g['tmp_path']}/notices"));
135 b37a2e8c Phil Davis
		if (!$queue) {
136
			return false;
137
		}
138
		if ($category != 'all') {
139
			foreach ($queue as $time => $notice) {
140
				if (strtolower($notice['category']) == strtolower($category)) {
141 9774f564 Colin Smith
					$toreturn[$time] = $notice;
142 b37a2e8c Phil Davis
				}
143 9774f564 Colin Smith
			}
144
			return $toreturn;
145
		} else {
146
			return $queue;
147
		}
148
	} else {
149
		return false;
150
	}
151
}
152
153 91cae49d Scott Ullrich
/****f* notices/close_notice
154
 * NAME
155
 *   close_notice
156
 * INPUTS
157
 *	 $id
158
 * RESULT
159
 *   Removes a notice from the list
160
 ******/
161 9774f564 Colin Smith
function close_notice($id) {
162
	global $notice_path;
163
	require_once("util.inc");
164 5f9bcd7a Scott Ullrich
	/* soekris */
165 b37a2e8c Phil Davis
	if (file_exists("/dev/led/error")) {
166 5f9bcd7a Scott Ullrich
		exec("/bin/echo 0 > /dev/led/error");
167 b37a2e8c Phil Davis
	}
168 0c884086 jim-p
	/* wrap & alix */
169
	led_normalize();
170 fab14377 Colin Smith
	$ids = array();
171 b37a2e8c Phil Davis
	if (!$notices = get_notices()) {
172
		return;
173
	}
174
	if ($id == "all") {
175 9774f564 Colin Smith
		unlink_if_exists($notice_path);
176
		return;
177
	}
178 b37a2e8c Phil Davis
	foreach (array_keys($notices) as $time) {
179
		if ($id == $time) {
180 9774f564 Colin Smith
			unset($notices[$id]);
181
			break;
182
		}
183
	}
184 b37a2e8c Phil Davis
	foreach ($notices as $key => $notice) {
185 9774f564 Colin Smith
		$ids[$key] = $notice['id'];
186
	}
187 b37a2e8c Phil Davis
	foreach ($ids as $time => $tocheck) {
188
		if ($id == $tocheck) {
189 9774f564 Colin Smith
			unset($notices[$time]);
190
			break;
191
		}
192
	}
193 b37a2e8c Phil Davis
	if (count($notices) != 0) {
194 cebc42e6 Colin Smith
		$queueout = fopen($notice_path, "w");
195 b37a2e8c Phil Davis
		fwrite($queueout, serialize($notices));
196
		fclose($queueout);
197 cebc42e6 Colin Smith
	} else {
198
		unlink_if_exists($notice_path);
199
	}
200 a0a125b9 Scott Ullrich
201 669e1adb Bill Marquette
	return;
202 9774f564 Colin Smith
}
203
204 91cae49d Scott Ullrich
/****f* notices/dump_xml_notices
205
 * NAME
206
 *   dump_xml_notices
207
 * INPUTS
208
 *	 NONE
209
 * RESULT
210
 *   Outputs notices in XML formatted text
211
 ******/
212 9774f564 Colin Smith
function dump_xml_notices() {
213 a4105aad Christian McDonald
	require_once("xmlparse.inc");
214 9774f564 Colin Smith
	global $notice_path, $listtags;
215
	$listtags[] = 'notice';
216 b37a2e8c Phil Davis
	if (!$notices = get_notices()) {
217
		return;
218
	}
219
	foreach ($notices as $time => $notice) {
220 9774f564 Colin Smith
		$notice['time'] = $time;
221
		$toput['notice'][] = $notice;
222
	}
223
	$xml = dump_xml_config($toput, 'notices');
224
	return $xml;
225
}
226
227 91cae49d Scott Ullrich
/****f* notices/are_notices_pending
228
 * NAME
229
 *   are_notices_pending
230
 * INPUTS
231
 *	 $category to check
232
 * RESULT
233
 *   returns true if notices are pending, false if they are not
234
 ******/
235 34e1ff97 Scott Ullrich
function are_notices_pending($category = "all") {
236
	global $notice_path;
237 b37a2e8c Phil Davis
	if (file_exists($notice_path)) {
238 34e1ff97 Scott Ullrich
		return true;
239
	}
240
	return false;
241 6e2ec568 Scott Ullrich
}
242
243 dc5fdeea PiBa-NL
function notices_sendqueue() {
244
	global $g;
245
	$nothing_done_count = 0;
246
	$messagequeue = array();
247 179377b0 robjarsen
248 dc5fdeea PiBa-NL
	while(true) {
249
		$nothing_done_count++;
250 4864d7f6 Josh Soref
		$smtpcount = 0;
251 dc5fdeea PiBa-NL
		$messages = array();
252 c5faa351 jim-p
		if (file_exists("{$g['vardb_path']}/notifyqueue.messages") &&
253
		    is_writable("{$g['vardb_path']}/notifyqueue.messages")) {
254
			$notifyqueue_lck = lock("notifyqueue", LOCK_EX);
255 dc5fdeea PiBa-NL
			$messages = unserialize(file_get_contents("{$g['vardb_path']}/notifyqueue.messages"));
256
			$messagequeue = $messages;
257
			$messages['mails']['item'] = array(); // clear all items to be send
258 c5faa351 jim-p
			$ret = file_put_contents("{$g['vardb_path']}/notifyqueue.messages", serialize($messages));
259
			if ($ret === false) {
260
				log_error("ERROR: Failed to write notify message queue!");
261
				return;
262
			}
263 dc5fdeea PiBa-NL
			unset($messages);
264 c5faa351 jim-p
		} else {
265
			/* Queue does not exist or is not writable, so no action can be taken
266
			 * https://redmine.pfsense.org/issues/14031
267
			 */
268
			return;
269 dc5fdeea PiBa-NL
		}
270
		// clear lock before trying to send messages, so new one's can be added
271
		unlock($notifyqueue_lck);
272 179377b0 robjarsen
273 dc5fdeea PiBa-NL
		if (is_array($messagequeue['mails']['item'])) {
274
			$smtpmessage = "";
275
			foreach($messagequeue['mails']['item'] as $mail) {
276
				switch ($mail['type']) {
277 179377b0 robjarsen
					case 'mail':
278 4864d7f6 Josh Soref
						$smtpcount++;
279 dc5fdeea PiBa-NL
						$smtpmessage .= "\r\n" . date("G:i:s",$mail['time']) . " " . $mail['msg'];
280
						break;
281
					default:
282
						break;
283
				}
284
			}
285
			if (!empty($smtpmessage)) {
286 4864d7f6 Josh Soref
				$smtpmessageheader = sprintf(gettext("Notifications in this message: %s"), $smtpcount);
287 d0d6d27f jim-p
				$smtpmessageheader .= "\n" . str_repeat('=', strlen($smtpmessageheader)) . "\n";
288 dc5fdeea PiBa-NL
				$nothing_done_count = 0;
289 d0d6d27f jim-p
				notify_via_smtp($smtpmessageheader . $smtpmessage, true);
290 dc5fdeea PiBa-NL
			}
291
		}
292
		if ($nothing_done_count > 6) {
293
			break;
294
		} else {
295
			sleep(10);
296
		}
297
	}
298
}
299
300
function notify_via_queue_add($message, $type='mail') {
301
	global $g;
302
	$mail = array();
303
	$mail['time'] = time();
304
	$mail['type'] = $type;
305
	$mail['msg'] = $message;
306
	$notifyqueue_lck = lock("notifyqueue", LOCK_EX);
307
	$messages = array();
308
	if (file_exists("{$g['vardb_path']}/notifyqueue.messages")) {
309 c5faa351 jim-p
		if (!is_writable("{$g['vardb_path']}/notifyqueue.messages")) {
310
			/* Cannot write to notify queue, so exit early
311
			 * https://redmine.pfsense.org/issues/14031
312
			 */
313
			return;
314
		}
315 dc5fdeea PiBa-NL
		$messages = unserialize(file_get_contents("{$g['vardb_path']}/notifyqueue.messages"));
316
	}
317 c5faa351 jim-p
	if (is_array($messages)) {
318 dc5fdeea PiBa-NL
		$messages['mails']['item'][] = $mail;
319 c5faa351 jim-p
		$ret = file_put_contents("{$g['vardb_path']}/notifyqueue.messages", serialize($messages));
320
		if ($ret === false) {
321
			log_error("ERROR: Failed to write notify message queue!");
322
			return;
323
		}
324 dc5fdeea PiBa-NL
	}
325
	unset($messages);
326 179377b0 robjarsen
327 dc5fdeea PiBa-NL
	mwexec_bg('/usr/local/bin/notify_monitor.php');
328
	unlock($notifyqueue_lck);
329
}
330
331 91cae49d Scott Ullrich
/****f* notices/notify_via_smtp
332 b19369b7 Scott Ullrich
 * NAME
333
 *   notify_via_smtp
334
 * INPUTS
335
 *	 notification string to send as an email
336
 * RESULT
337
 *   returns true if message was sent
338
 ******/
339 48b86f62 jim-p
function notify_via_smtp($message, $force = false) {
340 8273ea35 Scott Ullrich
	global $config, $g;
341 b37a2e8c Phil Davis
	if (platform_booting()) {
342 8273ea35 Scott Ullrich
		return;
343 b37a2e8c Phil Davis
	}
344 b19369b7 Scott Ullrich
345 b37a2e8c Phil Davis
	if (isset($config['notifications']['smtp']['disable']) && !$force) {
346 48b86f62 jim-p
		return;
347 b37a2e8c Phil Davis
	}
348 48b86f62 jim-p
349 acc9c73e Renato Botelho
	/* Do NOT send the same message twice, except if $force is true */
350
	if (!$force && file_exists("/var/db/notices_lastmsg.txt")) {
351 2c7ac8dc Scott Ullrich
		$lastmsg = trim(file_get_contents("/var/db/notices_lastmsg.txt"));
352 b37a2e8c Phil Davis
		if ($lastmsg == $message) {
353 2c7ac8dc Scott Ullrich
			return;
354 b37a2e8c Phil Davis
		}
355 2c7ac8dc Scott Ullrich
	}
356 743c30df Scott Ullrich
357 7c845149 jim-p
	/* Store last message sent to avoid spamming */
358 dc5fdeea PiBa-NL
	@file_put_contents("/var/db/notices_lastmsg.txt", $message);
359
	if (!$force) {
360
		notify_via_queue_add($message, 'mail');
361
		$ret = true;
362
	} else {
363
		$ret = send_smtp_message($message, "{$config['system']['hostname']}.{$config['system']['domain']} - Notification", $force);
364
	}
365 7c845149 jim-p
366 dc5fdeea PiBa-NL
	return $ret;
367 7c845149 jim-p
}
368
369 02376f6f Phil Davis
function send_smtp_message($message, $subject = "(no subject)", $force = false) {
370 7c845149 jim-p
	global $config, $g;
371 d4c5ada9 Renato Botelho
	require_once("Mail.php");
372 4bbb3155 Scott Ullrich
373 b37a2e8c Phil Davis
	if (isset($config['notifications']['smtp']['disable']) && !$force) {
374 02376f6f Phil Davis
		return;
375 b37a2e8c Phil Davis
	}
376 02376f6f Phil Davis
377 b37a2e8c Phil Davis
	if (!$config['notifications']['smtp']['ipaddress']) {
378 7c845149 jim-p
		return;
379 b37a2e8c Phil Davis
	}
380 7c845149 jim-p
381 b37a2e8c Phil Davis
	if (!$config['notifications']['smtp']['notifyemailaddress']) {
382 7c845149 jim-p
		return;
383 b37a2e8c Phil Davis
	}
384 7c845149 jim-p
385 1e45d13f Christian McDonald
	$to = config_get_path('notifications/smtp/notifyemailaddress');
386 b19369b7 Scott Ullrich
387 d4c5ada9 Renato Botelho
	if (empty($config['notifications']['smtp']['username']) ||
388
	    empty($config['notifications']['smtp']['password'])) {
389
		$auth = false;
390
		$username = '';
391
		$password = '';
392
	} else {
393
		$auth = isset($config['notifications']['smtp']['authentication_mechanism'])
394
		    ? $config['notifications']['smtp']['authentication_mechanism']
395
		    : 'PLAIN';
396 1e45d13f Christian McDonald
		$username = config_get_path('notifications/smtp/username');
397
		$password = config_get_path('notifications/smtp/password');
398 d4c5ada9 Renato Botelho
	}
399
400
	$params = array(
401
		'host' => (isset($config['notifications']['smtp']['ssl'])
402
		    ? 'ssl://'
403
		    : '')
404
		    . $config['notifications']['smtp']['ipaddress'],
405
		'port' => empty($config['notifications']['smtp']['port'])
406
		    ? 25
407
		    : $config['notifications']['smtp']['port'],
408
		'auth' => $auth,
409
		'username' => $username,
410
		'password' => $password,
411
		'localhost' => $config['system']['hostname'] . "." .
412
		    $config['system']['domain'],
413 c8c46e5a Renato Botelho
		'timeout' => !empty($config['notifications']['smtp']['timeout'])
414
		    ? $config['notifications']['smtp']['timeout']
415
		    : 20,
416 d4c5ada9 Renato Botelho
		'debug' => false,
417
		'persist' => false
418
	);
419
420 7da466e1 jim-p
	if ($config['notifications']['smtp']['sslvalidate'] == "disabled") {
421 93166bdc Viktor G
		$params['socket_options'] = array(
422
			'ssl' => array(
423
				'verify_peer_name' => false,
424
				'verify_peer' => false
425
		));
426 7da466e1 jim-p
	}
427
428 b37a2e8c Phil Davis
	if ($config['notifications']['smtp']['fromaddress']) {
429 1e45d13f Christian McDonald
		$from = config_get_path('notifications/smtp/fromaddress');
430 d4c5ada9 Renato Botelho
	} else {
431
		$from = "pfsense@{$config['system']['hostname']}.{$config['system']['domain']}";
432 72306d5a Scott Ullrich
	}
433 b19369b7 Scott Ullrich
434
	$headers = array(
435 d4c5ada9 Renato Botelho
		"From"    => $from,
436
		"To"      => $to,
437
		"Subject" => $subject,
438
		"Date"    => date("r")
439 b19369b7 Scott Ullrich
	);
440
441 87d9798f jim-p
	$error_text = 'Could not send the message to %1$s -- Error: %2$s';
442
	try {
443
		$smtp =& Mail::factory('smtp', $params);
444
		$mail = @$smtp->send($to, $headers, $message);
445
446
		if (PEAR::isError($mail)) {
447
			$err_msg = sprintf(gettext($error_text),
448
			    $to, $mail->getMessage());
449
		}
450
	} catch (Exception $e) {
451
		$err_msg = sprintf(gettext($error_text), $to, $e->getMessage());
452
	}
453 d4c5ada9 Renato Botelho
454 87d9798f jim-p
	if (!empty($err_msg)) {
455 d4c5ada9 Renato Botelho
		log_error($err_msg);
456
		return($err_msg);
457 f9fb2569 Scott Ullrich
	}
458 d4c5ada9 Renato Botelho
459
	log_error(sprintf(gettext("Message sent to %s OK"), $to));
460
	return;
461 b19369b7 Scott Ullrich
}
462 ded59fb6 Kapmeister
/****f* notices/notify_via_telegram
463
 * NAME
464
 *   notify_via_telegram
465
 * INPUTS
466
 *	 notification string to send to Telegram via API
467
 * RESULT
468
 *   returns NULL if message was sent
469
 ******/
470
471 1c2926e6 Kapmeister
function notify_via_telegram($message, $force = false) {
472 51a12198 Kapmeister
	global $config;
473 9948b5b5 Kapmeister
	if ((!isset($config['notifications']['telegram']['enabled']) && (!$force))
474 c4c42f28 Kapmeister
	    || !$config['notifications']['telegram']['api']
475
	    || !$config['notifications']['telegram']['chatid']) {
476 9948b5b5 Kapmeister
		if ($force) {
477
			return gettext("Unable to test Telegram notification without both API Key & Chat ID set");
478 c3d53b06 Kapmeister
		}
479 51a12198 Kapmeister
		return;
480
	}
481 c3d53b06 Kapmeister
482 51a12198 Kapmeister
	$url = "https://api.telegram.org/bot{$config['notifications']['telegram']['api']}/sendMessage?";
483
	$data = array(
484
		"chat_id" => $config['notifications']['telegram']['chatid'],
485
		"text" => "{$config['system']['hostname']}.{$config['system']['domain']}\n{$message}"
486
	);
487 b9fbc36a Viktor G
	$result = json_decode(curl_post_notification($url . http_build_query($data)), true);
488 9948b5b5 Kapmeister
	if (is_array($result)) {
489 c3d53b06 Kapmeister
		if ($result['ok']) {
490
			unset($err_msg);
491
		} else {
492 9948b5b5 Kapmeister
			$err_msg = sprintf(gettext("Failed to send Telegram notification. Error received was :{$result['error_code']}: {$result['description']}"));
493 c3d53b06 Kapmeister
			log_error($err_msg);
494
		}
495
	} else {
496 9948b5b5 Kapmeister
		$err_msg = gettext("API to Telegram did not return data in expected format!");
497 51a12198 Kapmeister
		log_error($err_msg);
498
	}
499
	return $err_msg;
500 ded59fb6 Kapmeister
}
501
502 18c256c0 overtninja
/****f* notices/notify_via_pushover
503
 * NAME
504
 *   notify_via_pushover
505
 * INPUTS
506
 *	 notification string to send to Pushover via API
507
 * RESULT
508
 *   returns NULL if message was sent
509
 ******/
510
511
function notify_via_pushover($message, $force = false) {
512
	global $config;
513
	if ((!isset($config['notifications']['pushover']['enabled']) && (!$force))
514
	    || !$config['notifications']['pushover']['apikey']
515
	    || !$config['notifications']['pushover']['userkey']) {
516
		if ($force) {
517
			return gettext("Unable to test Pushover notification without both API Key & User Key set");
518
		}
519
		return;
520
	}
521 187da3ef overtninja
	if (strcasecmp($config['notifications']['pushover']['sound'], 'devicedefault') == 0) {
522 88774881 Christian McDonald
		config_del_path('notifications/pushover/sound');
523 187da3ef overtninja
	}
524 18c256c0 overtninja
525
	$url = "https://api.pushover.net/1/messages.json";
526
	$data = array(
527
		"token" => $config['notifications']['pushover']['apikey'],
528
		"user" => $config['notifications']['pushover']['userkey'],
529
		"sound" => $config['notifications']['pushover']['sound'],
530
		"priority" => $config['notifications']['pushover']['priority'],
531
		"retry" => $config['notifications']['pushover']['retry'],
532
		"expire" => $config['notifications']['pushover']['expire'],
533
		"message" => "{$config['system']['hostname']}.{$config['system']['domain']}\n{$message}"
534
	);
535 b9fbc36a Viktor G
	$result = json_decode(curl_post_notification($url, $data), true);
536 18c256c0 overtninja
	if (is_array($result)) {
537
		if ($result['status']) {
538
			unset($err_msg);
539
		} else {
540 4002dc3f overtninja
			$err_msg = sprintf(gettext("Failed to send Pushover notification. Error received was: %s"), $result['errors']['0']);
541 18c256c0 overtninja
			log_error($err_msg);
542
		}
543
	} else {
544 6b18e960 overtninja
		$err_msg = gettext("Pushover API server did not return data in expected format!");
545 18c256c0 overtninja
		log_error($err_msg);
546
	}
547
	return $err_msg;
548
}
549
550 b9fbc36a Viktor G
/****f* notices/notify_via_slack
551
 * NAME
552
 *   notify_via_slack
553
 * INPUTS
554
 *	 notification string to send to Slack via API
555
 * RESULT
556
 *   returns NULL if message was sent
557
 ******/
558
559
function notify_via_slack($message, $force = false) {
560
	global $config;
561
	if ((!isset($config['notifications']['slack']['enabled']) && (!$force))
562
	    || !$config['notifications']['slack']['api']
563
	    || !$config['notifications']['slack']['channel']) {
564
		if ($force) {
565
			return gettext("Unable to test Slack notification without both API Key & Channel set");
566
		}
567
		return;
568
	}
569
570
	$url = "https://slack.com/api/chat.postMessage";
571
	$data = array(
572
		"token" => $config['notifications']['slack']['api'],
573
		"channel" => "#" . $config['notifications']['slack']['channel'],
574
		"text" => $message,
575
		"username" => "{$config['system']['hostname']}.{$config['system']['domain']}"
576
	);
577
	$result = json_decode(curl_post_notification($url, $data), true);
578
	if (is_array($result)) {
579
		if ($result['ok']) {
580
			unset($err_msg);
581
		} else {
582
			$err_msg = sprintf(gettext("Failed to send Slack notification. Error received was: %s"), $result['error']);
583
			log_error($err_msg);
584
		}
585
	} else {
586
		$err_msg = gettext("Slack API server did not return data in expected format!");
587
		log_error($err_msg);
588
	}
589
	return $err_msg;
590
}
591
592
function curl_post_notification($url, $data = array()) {
593 18c256c0 overtninja
	$conn = curl_init($url);
594 b9fbc36a Viktor G
	if (!empty($data)) {
595 4dde40ec Viktor G
		curl_setopt($conn, CURLOPT_POSTFIELDS, $data);
596 b9fbc36a Viktor G
	}
597 18c256c0 overtninja
	curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, true);
598
	curl_setopt($conn, CURLOPT_FRESH_CONNECT,  true);
599 4dde40ec Viktor G
	curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);
600 75a3b0de Viktor G
	set_curlproxy($conn);
601 b9fbc36a Viktor G
	$curl_post_result = curl_exec($conn);
602 18c256c0 overtninja
	curl_close($conn);
603 b9fbc36a Viktor G
	return $curl_post_result;	//json encoded
604 18c256c0 overtninja
}
605
606 53842a6c jim-p
/* Notify via remote methods only - not via GUI. */
607
function notify_all_remote($msg) {
608
	notify_via_smtp($msg);
609 ded59fb6 Kapmeister
	notify_via_telegram($msg);
610 18c256c0 overtninja
	notify_via_pushover($msg);
611 b9fbc36a Viktor G
	notify_via_slack($msg);
612 53842a6c jim-p
}
613
614 6955830f Ermal Lu?i
?>