forked from tylerhall/php-growl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.growl.php
More file actions
124 lines (105 loc) · 4.19 KB
/
class.growl.php
File metadata and controls
124 lines (105 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?PHP
class Growl
{
const GROWL_PRIORITY_LOW = -2;
const GROWL_PRIORITY_MODERATE = -1;
const GROWL_PRIORITY_NORMAL = 0;
const GROWL_PRIORITY_HIGH = 1;
const GROWL_PRIORITY_EMERGENCY = 2;
private $appName;
private $address;
private $notifications;
private $password;
private $port;
public function __construct($address, $password = '', $app_name = 'PHP Growl')
{
$this->appName = utf8_encode($app_name);
$this->address = $address;
$this->notifications = array();
$this->password = $password;
$this->port = 9887;
if(@inet_pton($this->address) === false && ($tmp = dns_get_record($this->address, DNS_ANY)) !== false) {
$record_a = null;
$record_aaaa = null;
foreach($tmp as $v) {
if($record_a === null && $v['type'] == 'A') {
$record_a = $v['ip'];
}
if(defined('AF_INET6') && $record_aaaa === null && $v['type'] == 'AAAA') {
$record_aaaa = $v['ipv6'];
}
}
if($record_aaaa !== null) {
$this->address = $record_aaaa;
}
if($record_a !== null) {
$this->address = $record_a;
}
}
}
public function addNotification($name, $enabled = true)
{
$this->notifications[] = array('name' => utf8_encode($name), 'enabled' => $enabled);
}
public function register()
{
$data = '';
$defaults = '';
$num_defaults = 0;
for($i = 0; $i < count($this->notifications); $i++)
{
$data .= pack('n', strlen($this->notifications[$i]['name'])) . $this->notifications[$i]['name'];
if($this->notifications[$i]['enabled'])
{
$defaults .= pack('c', $i);
$num_defaults++;
}
}
// pack(Protocol version, type, app name, number of notifications to register)
$data = pack('c2nc2', 1, 0, strlen($this->appName), count($this->notifications), $num_defaults) . $this->appName . $data . $defaults;
$data .= pack('H32', md5($data . $this->password));
return $this->send($data);
}
public function notify($name, $title, $message, $priority = 0, $sticky = false)
{
$name = utf8_encode($name);
$title = utf8_encode($title);
$message = utf8_encode($message);
$priority = intval($priority);
$flags = ($priority & 7) * 2;
if($priority < 0) $flags |= 8;
if($sticky) $flags |= 256;
// pack(protocol version, type, priority/sticky flags, notification name length, title length, message length. app name length)
$data = pack('c2n5', 1, 1, $flags, strlen($name), strlen($title), strlen($message), strlen($this->appName));
$data .= $name . $title . $message . $this->appName;
$data .= pack('H32', md5($data . $this->password));
return $this->send($data);
}
private function send($data)
{
if(function_exists('socket_create') && function_exists('socket_sendto'))
{
if(strlen(inet_pton($this->address)) > 4 && defined('AF_INET6')) {
$sck = socket_create(AF_INET6, SOCK_DGRAM, SOL_UDP);
} elseif(strlen(inet_pton($this->address)) == 4) {
$sck = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
} else {
$error_str = "Error creating Socket";
if(strlen(inet_pton($this->address)) > 4 && !defined('AF_INET6')) {
$error_str = sprintf("IPv6 address used, but IPv6 not enabled with this php build.", $this->address);
}
throw new Exception($error_str);
}
socket_sendto($sck, $data, strlen($data), MSG_WAITALL, $this->address, $this->port);
return true;
}
elseif(function_exists('fsockopen'))
{
$fp = fsockopen('udp://' . $this->address, $this->port);
fwrite($fp, $data);
fclose($fp);
return true;
}
return false;
}
}