Project

General

Profile

Download (3.18 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
                socket_sendto($sck, $data, strlen($data), 0x100, $this->address, $this->port);
78
                return true;
79
            }
80
            elseif(function_exists('fsockopen'))
81
            {
82
                $fp = fsockopen('udp://' . $this->address, $this->port);
83
                fwrite($fp, $data);
84
                fclose($fp);
85
                return true;
86
            }
87

    
88
            return false;
89
        }
90
    }
91

    
92
?>
(15-15/43)