Source for file Growl.php
Documentation is available at Growl.php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2006, Bertrand Mansion |
// | All rights reserved. |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// +-----------------------------------------------------------------------+
// | Author: Bertrand Mansion <golgote@mamasam.com> |
// +-----------------------------------------------------------------------+
require_once 'Net/Growl/Application.php';
* Version of the Growl protocol used in this package
if (!defined('GROWL_PROTOCOL_VERSION')) define('GROWL_PROTOCOL_VERSION', 1 );
* Packet of type Registration
if (!defined('GROWL_TYPE_REGISTRATION')) define('GROWL_TYPE_REGISTRATION', 0 );
* Packet of type Notification
if (!defined('GROWL_TYPE_NOTIFICATION')) define('GROWL_TYPE_NOTIFICATION', 1 );
* Current number of notification being displayed on user desktop
$GLOBALS['_NET_GROWL_NOTIFICATION_COUNT'] = 0;
* Maximum number of notification to be displayed on user desktop
if (!isset ($GLOBALS['_NET_GROWL_NOTIFICATION_LIMIT'])) {
$GLOBALS['_NET_GROWL_NOTIFICATION_LIMIT'] = 0;
* Sends notifications to {@link http://growl.info Growl}
* This package makes it possible to easily send a notification from
* your PHP script to {@link http://growl.info Growl}.
* Growl is a global notification system for Mac OS X.
* Any application can send a notification to Growl, which will display
* an attractive message on your screen. Growl currently works with a
* growing number of applications.
* The class provides the following capabilities:
* - Register your PHP application in Growl.
* - Let Growl know what kind of notifications to expect.
* - Set a maximum number of notifications to be displayed.
* @author Bertrand Mansion <golgote@mamasam.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://growl.info Growl Homepage
* This is usually a Net_Growl_Application object but can really be
* any other object as long as Net_Growl_Application methods are
* Application is registered
var $_isRegistered = false;
* Net_Growl connection options
'port' => GROWL_UDP_PORT ,
* Makes sure there is only one Growl connection open.
* @return object Net_Growl
function &singleton($appName, $notifications, $password = '', $options = array ())
$obj = new Net_Growl($appName, $notifications, $password, $options);
* This method instantiate a new Net_Growl object and opens a socket connection
* to the specified Growl socket server. Currently, only UDP is supported by Growl.
* The constructor registers a shutdown function {@link Net_Growl::_Net_Growl()}
* that closes the socket if it is open.
* require_once 'Net/Growl.php';
* $notifications = array('Errors', 'Messages');
* $growl = new Net_Growl('My application', $notification);
* $growl->notify( 'Messages',
* 'My notification title',
* 'My notification description');
* @param mixed Can be a Net_Growl_Application object or the application name string
* @param array Array of notifications
* @param string Optional password for Growl
* @param array Array of options : 'host', 'port', 'protocol' for Growl socket server
function Net_Growl(&$application, $notifications = array (), $password = '', $options = array ())
foreach ($options as $k => $v) {
if (isset ($this->_options[$k])) {
$this->_options[$k] = $v;
$this->_application = & $application;
* Limit the number of notifications
* This method limits the number of notifications to be displayed on
* the Growl user desktop. By default, there is no limit. It is used
* mostly to prevent problem with notifications within loops.
* @param int Maximum number of notifications
$GLOBALS['_NET_GROWL_NOTIFICATION_LIMIT'] = $max;
* Returns the registered application object
* @return object Application
* @see Net_Growl_Application
return $this->_application;
* Build, encode end send the registration packet
* @return true|PEAR_Error
if (!isset ($this->_socket)) {
$socket = $this->_options['protocol']. '://'. $this->_options['host'];
$this->_socket = fsockopen($socket, $this->_options['port'], $errno, $errstr);
if ($this->_socket === false ) {
return PEAR ::raiseError ($errstr);
$appName = utf8_encode($this->_application->getGrowlName ());
$password = $this->_application->getGrowlPassword ();
$nameEnc = $defaultEnc = '';
$nameCnt = $defaultCnt = 0;
$notifications = $this->_application->getGrowlNotifications ();
foreach($notifications as $name => $options) {
if (is_array($options) && !empty ($options['enabled'])) {
$defaultEnc .= pack('c', $nameCnt);
$data .= $appName . $nameEnc . $defaultEnc;
$checksum = pack('H32', md5($data . $password));
$checksum = pack('H32', md5($data));
return PEAR ::raiseError ('Could not send registration to Growl Server.');
$this->_isRegistered = true;
* Sends a notification to Growl
* Growl notifications have a name, a title, a description and
* a few options, depending on the kind of display plugin you use.
* The bubble plugin is recommended, until there is a plugin more
* appropriate for these kind of notifications.
* The current options supported by most Growl plugins are:
* array('priority' => 0, 'sticky' => false)
* - sticky: whether the bubble stays on screen until the user clicks on it.
* - priority: a number from -2 (low) to 2 (high), default is 0 (moderate).
* @param object Application object
* @param bool Whether to send the registration to the server
* @return true|PEAR_Error
function notify($name, $title, $description = '', $options = array ())
if ($GLOBALS['_NET_GROWL_NOTIFICATION_LIMIT'] > 0 &&
$GLOBALS['_NET_GROWL_NOTIFICATION_COUNT'] >= $GLOBALS['_NET_GROWL_NOTIFICATION_LIMIT']) {
if (!$this->_isRegistered && ($res = $this->_sendRegister ()) !== true ) {
$appName = utf8_encode($this->_application->getGrowlName ());
$password = $this->_application->getGrowlPassword ();
$priority = isset ($options['priority']) ? $options['priority'] : 0;
$flags = ($priority & 7 ) * 2;
if (isset ($options['sticky']) && $options['sticky'] === true ) {
$data .= $name . $title . $description . $appName;
$checksum = pack('H32', md5($data . $password));
$checksum = pack('H32', md5($data));
return PEAR ::raiseError ('Could not send notification to Growl Server.');
++ $GLOBALS['_NET_GROWL_NOTIFICATION_COUNT'];
* Automatically closes the socket if it is open.
Documentation generated on Mon, 11 Mar 2019 14:38:44 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|