Project

General

Profile

Download (2.65 KB) Statistics
| Branch: | Tag: | Revision:
1
<?PHP
2
	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

    
10
		private $appName;
11
		private $address;
12
		private $notifications;
13
		private $password;
14
		private $port;
15

    
16
		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

    
25
		public function addNotification($name, $enabled = true)
26
		{
27
			$this->notifications[] = array('name' => utf8_encode($name), 'enabled' => $enabled);
28
		}
29

    
30
		public function register()
31
		{
32
			$data         = '';
33
			$defaults     = '';
34
			$num_defaults = 0;
35

    
36
			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

    
46
			// 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

    
50
			return $this->send($data);
51
		}
52

    
53
		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

    
60
			$flags = ($priority & 7) * 2;
61
			if ($priority < 0) $flags |= 8;
62
			if ($sticky) $flags |= 1;
63

    
64
			// 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

    
69
			return $this->send($data);
70
		}
71

    
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
			}
93

    
94
			return false;
95
		}
96
	}
97

    
98
?>
(23-23/65)