Projet

Général

Profil

Télécharger (3,29 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / growl.class @ 26d060bc

1
<?PHP
2
/*
3
	pfSense_MODULE:	notifications
4
*/
5

    
6
    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

    
14
        private $appName;
15
        private $address;
16
        private $notifications;
17
        private $password;
18
        private $port;
19

    
20
        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

    
29
        public function addNotification($name, $enabled = true)
30
        {
31
            $this->notifications[] = array('name' => utf8_encode($name), 'enabled' => $enabled);
32
        }
33

    
34
        public function register()
35
        {
36
            $data         = '';
37
            $defaults     = '';
38
            $num_defaults = 0;
39

    
40
            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

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

    
54
            return $this->send($data);
55
        }
56

    
57
        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

    
64
            $flags = ($priority & 7) * 2;
65
            if($priority < 0) $flags |= 8;
66
            if($sticky) $flags |= 1;
67

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

    
73
            return $this->send($data);
74
        }
75

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

    
98
            return false;
99
        }
100
    }
101

    
102
?>
(23-23/67)