Created
June 8, 2011 16:55
-
-
Save MidnightLightning/1014819 to your computer and use it in GitHub Desktop.
PHP Growl notifier interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Interface with the growlnotify notification client | |
* | |
* The Growl notification system for Mac includes a command-line utility for scripts to send notifications through. | |
* 'growlnotify' needs to be explicitly installed from the "Extras" folder of the Growl installation disk before it can be used | |
* | |
* @author Brooks Boyd <boydb@midnightdesign.ws> | |
* @link http://growl.info/documentation/developer/growlnotifier.php Growlnotifier documentation | |
*/ | |
class Growler { | |
public $appName; | |
public $icon; | |
/** | |
* Set the name and icon for this script's notifications | |
* | |
* $param string $name The name of the application sending the Growl notification | |
* $param string $icon What icon to use for the notification. See {@link iconParse()} for how this string is interpreted | |
*/ | |
function __construct($name = "PHP Script", $icon = ".php") { | |
$this->appName = $name; | |
$this->icon = $icon; | |
} | |
/** | |
* Send notification | |
* | |
* @param string $title The first line of the notification, usually in bold | |
* @param string $msg The rest of the message body; can contain line breaks | |
* @param int $priority An optional integer from -2 (low priority) to 2 (high priority); defaults to zero (normal priority) | |
*/ | |
function notify($title = null, $msg = null, $priority = 0) { | |
if ($title === null || $msg === null) return; | |
$cmd = 'growlnotify '. | |
'--name '.escapeshellarg($this->appName).' '. | |
$this->iconParse().' '. | |
'--priority '.escapeshellarg(intval($priority)).' '. | |
'--message '.escapeshellarg($msg).' '. | |
'--title '.escapeshellarg($title); | |
exec($cmd); | |
} | |
/** | |
* Send notification to remote server | |
* | |
* @param string $host The remote server the message is being sent to | |
* @param string $title The first line of the notification, usually in bold | |
* @param string $msg The rest of the message body; can contain line breaks | |
* @param int $priority An optional integer from -2 (low priority) to 2 (high priority); defaults to zero (normal | |
* @param string $password Optional password, if the remote server is secured | |
* @param boolean $udp Send the notification via UDP instead of DO? | |
* @param integer $udp_port Specify the UDP port if different than the default | |
* @param string $udp_auth UDP authentication method; defaults to "MD5" | |
* @param boolean $udp_crypt Encrypt the UDP traffic? | |
*/ | |
function remote_notify($host, $title = null, $msg = null, $priority = 0, $password = null, $udp = false, $udp_port = null, $udp_auth = "MD5", $udp_crypt = false) { | |
if ($title == null || $msg === null) return; | |
$cmd = 'growlnotify '. | |
'--host '.escapeshellarg($host).' '. | |
'--name '.escapeshellarg($this->appName).' '. | |
$this->iconParse().' '. | |
'--priority '.escapeshellarg(intval($priority)).' '. | |
'--message '.escapeshellarg($msg).' '; | |
if ($password !== null) $cmd .= '--password '.escapeshellarg($password).' '; | |
if ($udp) { | |
// Send via UDP | |
$cmd .= '--udp --auth '.escapeshellarg($udp_auth).' '; | |
if ($udp_port !== null) $cmd .= '--port '.escapeshellarg($udp_port).' '; | |
if ($udp_crypt) $cmd .= '--crypt '; | |
} | |
$cmd .= '--title '.escapeshellarg($title); // Must be last | |
exec($cmd); | |
} | |
/** | |
* Figure out what sort of icon to use | |
* | |
* Determine if $this->icon is a reference to an extension, file, or application name. | |
* | |
* If $this->icon starts with a ".", it's an extension | |
* If $this->icon starts with a "/", it's a file path | |
* Otherwise, its the name of an application | |
*/ | |
private function iconParse() { | |
$i = $this->icon; | |
if ($i[0] === '.') { | |
// File extension | |
return '--icon '.escapeshellarg(substr($i,1)); | |
} elseif ($i[0] === '/') { | |
// File path; is it an image? | |
$ext = strtolower(pathinfo($i, PATHINFO_EXTENSION)); | |
$img_exts = array('jpg', 'jpeg', 'tif', 'gif', 'png', 'bmp'); | |
if (in_array($ext, $img_exts)) { | |
// This file is an image | |
return '--image '.escapeshellarg($i); | |
} else { | |
// This is a regular file; use the file's icon | |
return '--iconpath '.escapeshellarg($i); | |
} | |
} else { | |
// Assume this is an application's name | |
return '--appIcon '.escapeshellarg($i); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('../lib/simpletest/autorun.php'); // Simpletest PHP unit testing | |
require_once('growler.php'); // Growler class | |
class GrowlerNotification extends UnitTestCase { | |
function testNameIconConstruction() { | |
$g = new Growler('MyRandomApp', '.php'); | |
$this->assertEqual($g->appName, 'MyRandomApp'); | |
$this->assertEqual($g->icon, '.php'); | |
} | |
function testNotify() { | |
$g = new Growler('MyRandomApp', '.php'); | |
$g->notify('Test Notification', 'Baseline success'); | |
$this->pass('No errors'); | |
} | |
function testNoTitleOrMsg() { | |
$g = new Growler('MyRandomApp', '.php'); | |
$g->notify(); // Shouldn't do anything, but shouldn't throw an error either | |
$this->pass('No errors'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment