Browse files

Code cleanup.

  • Loading branch information...
1 parent c26035d commit 4cf0bfd07d5241f139ceedc0882ba02eabfd8cef @tylerhall committed Nov 27, 2008
Showing with 83 additions and 89 deletions.
  1. +83 −89 class.growl.php
View
172 class.growl.php
@@ -1,96 +1,90 @@
<?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;
+ 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;
- public $appName = 'PHP Growl';
- public $address;
- public $password;
- public $port = 9887;
- public $notifications = array();
+ private $appName;
+ private $address;
+ private $notifications;
+ private $password;
+ private $port;
- function __construct($app_name = null)
- {
- if(isset($app_name))
- $this->appName = utf8_encode($app_name);
- }
-
- function setAddress($address, $password = '')
- {
- $this->address = $address;
- $this->password = $password;
- }
-
- function addNotification($name, $enabled = true)
- {
- if($name != '')
- $this->notifications[] = array('name' => utf8_encode($name), 'enabled' => $enabled);
- }
-
- function register($address = null, $password = '')
- {
- if(isset($address))
- {
- $this->address = $address;
- $this->password = $password;
- }
-
- $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++;
- }
- }
+ 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;
+ }
- // 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));
+ public function addNotification($name, $enabled = true)
+ {
+ $this->notifications[] = array('name' => utf8_encode($name), 'enabled' => $enabled);
+ }
- $this->send($data);
- }
-
- 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 |= 1;
+ public function register()
+ {
+ $data = '';
+ $defaults = '';
+ $num_defaults = 0;
- // 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));
+ 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++;
+ }
+ }
- $this->send($data);
- }
-
- function send($data)
- {
- if(function_exists('socket_create') && function_exists('socket_sendto'))
- {
- $sck = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
- socket_sendto($sck, $data, strlen($data), 0x100, $this->address, $this->port);
- }
- elseif(function_exists('fsockopen'))
- {
- $fp = fsockopen('udp://' . $this->address, $this->port);
- fwrite($fp, $data);
- fclose($fp);
- }
- }
- }
+ // 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 |= 1;
+
+ // 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'))
+ {
+ $sck = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
+ socket_sendto($sck, $data, strlen($data), 0x100, $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;
+ }
+ }

0 comments on commit 4cf0bfd

Please sign in to comment.