Project

General

Profile

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