Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

One instance per app & IPv6 support

Thanks to:
>
snoj@a605678
e32519bc3
>
ryanzec/php-growl@add39f0
e58af70edcbf
  • Loading branch information...
commit ed61bdbbe8eeccf590c0e0782c5aad08d0cebd03 1 parent 589f204
@toin0u toin0u authored committed
Showing with 68 additions and 17 deletions.
  1. +7 −5 README.markdown
  2. +38 −12 class.growl.php
  3. +23 −0 test.php
View
12 README.markdown
@@ -2,18 +2,20 @@ PHP-Growl is an implementation of the [Growl notification system](http://growl.i
As a proof of concept example, I've written a [Mint](http://haveamint.com) [Pepper](http://haveamint.com/peppermill/) plugin that notifies you whenever certain events (like new referrals, unique visitors, etc) happen on your website. It's a slick way to stay on top of who is visiting your site. You can [download Growl Pepper here](http://www.haveamint.com/peppermill/pepper/49/growl/).
- <?PHP
- $growl = new Growl($ip_address);
+ <?php
+
+ $connection = array('address' => '127.0.0.1', 'password' => 'password');
+ $growl = new Growl();
// Adding and registering your notifications with Growl
// only needs to be done once per computer. Growl will
// remember your app after this.
$growl->addNotification('Notification Name');
$growl->addNotification('Another Notification');
- $growl->register();
+ $growl->register($connection);
// Send a notification
- $growl->notify('Notification Name', 'Some Title', 'Some message to display');
+ $growl->notify($connection, 'Notification Name', 'Some Title', 'Some message to display');
// Send a second notification
- $growl->notify('Another Notification', 'Another Title', 'Something useful I hope.');
+ $growl->notify($connection, 'Another Notification', 'Another Title', 'Something useful I hope.');
View
50 class.growl.php
@@ -1,11 +1,12 @@
-<?PHP
+<?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;
+ 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;
private $appName;
private $address;
@@ -13,12 +14,12 @@ class Growl
private $password;
private $port;
- public function __construct($address, $password = '', $app_name = 'PHP Growl')
+ public function __construct($app_name = 'PHP Growl')
{
$this->appName = utf8_encode($app_name);
- $this->address = $address;
+ $this->address = null;
$this->notifications = array();
- $this->password = $password;
+ $this->password = null;
$this->port = 9887;
}
@@ -27,8 +28,10 @@ public function addNotification($name, $enabled = true)
$this->notifications[] = array('name' => utf8_encode($name), 'enabled' => $enabled);
}
- public function register()
+ public function register($connection)
{
+ $this->setConnectionData($connection);
+
$data = '';
$defaults = '';
$num_defaults = 0;
@@ -50,8 +53,10 @@ public function register()
return $this->send($data);
}
- public function notify($name, $title, $message, $priority = 0, $sticky = false)
+ public function notify($connection, $name, $title, $message, $priority = 0, $sticky = false)
{
+ $this->setConnectionData($connection);
+
$name = utf8_encode($name);
$title = utf8_encode($title);
$message = utf8_encode($message);
@@ -73,8 +78,11 @@ private function send($data)
{
if(function_exists('socket_create') && function_exists('socket_sendto'))
{
- $sck = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
+ $sck = ( strlen(inet_pton($this->address)) > 4 && defined('AF_INET6') )
+ ? socket_create(AF_INET6, SOCK_DGRAM, SOL_UDP)
+ : socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_sendto($sck, $data, strlen($data), 0x100, $this->address, $this->port);
+ $this->resetConnectionData();
return true;
}
elseif(function_exists('fsockopen'))
@@ -82,9 +90,27 @@ private function send($data)
$fp = fsockopen('udp://' . $this->address, $this->port);
fwrite($fp, $data);
fclose($fp);
+ $this->resetConnectionData();
return true;
}
return false;
}
+
+ private function setConnectionData($connection)
+ {
+ if(empty($connection['address']))
+ {
+ throw new Exception('Address Missing', 'Unable to send notification without ip address.');
+ }
+
+ $this->address = $connection['address'];
+ $this->password = (!empty($connection['password'])) ? $connection['password'] : '';
+ }
+
+ private function resetConnectionData()
+ {
+ $this->address = null;
+ $this->password = null;
+ }
}
View
23 test.php
@@ -0,0 +1,23 @@
+<?php
+
+require_once 'class.growl.php';
+
+$connection = array('address' => '127.0.0.1', 'password' => 'password');
+$growl = new Growl();
+
+// Adding and registering your notifications with Growl
+// only needs to be done once per computer. Growl will
+// remember your app after this.
+$growl->addNotification('Notification Name');
+$growl->addNotification('Another Notification');
+$growl->register($connection);
+
+// Send a notification
+$growl->notify($connection, 'Notification Name', 'Some Title', 'Some message to display');
+
+// Send a second notification
+$priority = 2; #GROWL_PRIORITY_EMERGENCY
+$sticky = true;
+$growl->notify($connection, 'Another Notification', 'Another Title', 'Something useful I hope.', $priority, $sticky);
+
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.