Project

General

Profile

Download (2.69 KB) Statistics
| Branch: | Tag: | Revision:
1 2b4aace0 Scott Ullrich
<?PHP
2 523855b0 Scott Ullrich
/*
3
	pfSense_MODULE:	notifications
4
*/
5
6 9ba87997 Phil Davis
	class Growl
7
	{
8
		const GROWL_PRIORITY_LOW = -2;
9
		const GROWL_PRIORITY_MODERATE = -1;
10
		const GROWL_PRIORITY_NORMAL = 0;
11
		const GROWL_PRIORITY_HIGH = 1;
12
		const GROWL_PRIORITY_EMERGENCY = 2;
13 2b4aace0 Scott Ullrich
14 9ba87997 Phil Davis
		private $appName;
15
		private $address;
16
		private $notifications;
17
		private $password;
18
		private $port;
19 2b4aace0 Scott Ullrich
20 9ba87997 Phil Davis
		public function __construct($address, $password = '', $app_name = 'PHP-Growl')
21
		{
22
			$this->appName       = utf8_encode($app_name);
23
			$this->address       = $address;
24
			$this->notifications = array();
25
			$this->password      = $password;
26
			$this->port          = 9887;
27
		}
28 2b4aace0 Scott Ullrich
29 9ba87997 Phil Davis
		public function addNotification($name, $enabled = true)
30
		{
31
			$this->notifications[] = array('name' => utf8_encode($name), 'enabled' => $enabled);
32
		}
33 2b4aace0 Scott Ullrich
34 9ba87997 Phil Davis
		public function register()
35
		{
36
			$data         = '';
37
			$defaults     = '';
38
			$num_defaults = 0;
39 2b4aace0 Scott Ullrich
40 9ba87997 Phil Davis
			for ($i = 0; $i < count($this->notifications); $i++)
41
			{
42
				$data .= pack('n', strlen($this->notifications[$i]['name'])) . $this->notifications[$i]['name'];
43
				if ($this->notifications[$i]['enabled'])
44
				{
45
					$defaults .= pack('c', $i);
46
					$num_defaults++;
47
				}
48
			}
49 2b4aace0 Scott Ullrich
50 9ba87997 Phil Davis
			// pack(Protocol version, type, app name, number of notifications to register)
51
			$data  = pack('c2nc2', 1, 0, strlen($this->appName), count($this->notifications), $num_defaults) . $this->appName . $data . $defaults;
52
			$data .= pack('H32', md5($data . $this->password));
53 2b4aace0 Scott Ullrich
54 9ba87997 Phil Davis
			return $this->send($data);
55
		}
56 2b4aace0 Scott Ullrich
57 9ba87997 Phil Davis
		public function notify($name, $title, $message, $priority = 0, $sticky = false)
58
		{
59
			$name     = utf8_encode($name);
60
			$title    = utf8_encode($title);
61
			$message  = utf8_encode($message);
62
			$priority = intval($priority);
63 2b4aace0 Scott Ullrich
64 9ba87997 Phil Davis
			$flags = ($priority & 7) * 2;
65
			if ($priority < 0) $flags |= 8;
66
			if ($sticky) $flags |= 1;
67 2b4aace0 Scott Ullrich
68 9ba87997 Phil Davis
			// pack(protocol version, type, priority/sticky flags, notification name length, title length, message length. app name length)
69
			$data = pack('c2n5', 1, 1, $flags, strlen($name), strlen($title), strlen($message), strlen($this->appName));
70
			$data .= $name . $title . $message . $this->appName;
71
			$data .= pack('H32', md5($data . $this->password));
72 2b4aace0 Scott Ullrich
73 9ba87997 Phil Davis
			return $this->send($data);
74 575a42f5 Ermal Lu?i
		}
75 9ba87997 Phil Davis
76
		private function send($data)
77
		{
78
			if (function_exists('socket_create') && function_exists('socket_sendto'))
79
			{
80
				$sck = @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
81
				if ($sck) {
82
					socket_sendto($sck, $data, strlen($data), 0x100, $this->address, $this->port);
83
					return true;
84
				}
85
			}
86
			elseif (function_exists('fsockopen'))
87
			{
88
				if ($this->address) {
89
					$fp = @fsockopen('udp://' . $this->address, $this->port);
90
					if ($fp) {
91
						fwrite($fp, $data);
92
						fclose($fp);
93
						return true;
94
					}
95
				}
96 575a42f5 Ermal Lu?i
			}
97 2b4aace0 Scott Ullrich
98 9ba87997 Phil Davis
			return false;
99
		}
100
	}
101 2b4aace0 Scott Ullrich
102 9734b054 Scott Ullrich
?>