Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
brianherbert committed Oct 23, 2010
0 parents commit a489ecd
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 0 deletions.
106 changes: 106 additions & 0 deletions controllers/admin/growl_settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Growl Settings Controller
*
* PHP version 5
* LICENSE: This source file is subject to LGPL license
* that is available through the world-wide-web at the following URI:
* http://www.gnu.org/copyleft/lesser.html
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi - http://source.ushahididev.com
* @module Growl Settings Controller
* @copyright Ushahidi - http://www.ushahidi.com
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
*
*/

class Growl_Settings_Controller extends Admin_Controller
{
public function index()
{
$this->template->this_page = 'addons';

// Standard Settings View
$this->template->content = new View("admin/plugins_settings");
$this->template->content->title = "Growl Settings";

// Settings Form View
$this->template->content->settings_form = new View("growl/admin/growl_settings");

// setup and initialize form field names
$form = array
(
'ips' => '',
'passwords' => ''
);
// Copy the form as errors, so the errors will be stored with keys
// corresponding to the form field names
$errors = $form;
$form_error = FALSE;
$form_saved = FALSE;

// check, has the form been submitted, if so, setup validation
if ($_POST)
{
// Instantiate Validation, use $post, so we don't overwrite $_POST
// fields with our own things
$post = new Validation($_POST);

// Add some filters
$post->pre_filter('trim', TRUE);

// Add some rules, the input field, followed by a list of checks, carried out in order

$post->add_rules('ips','required', 'length[4,20]');
$post->add_rules('passwords', 'required', 'length[1,50]');

// Test to see if things passed the rule checks
if ($post->validate())
{
// Yes! everything is valid
$growl = new Growl_Model(1);
$growl->ips = $post->ips;
$growl->passwords = $post->passwords;
$growl->save();

// Everything is A-Okay!
$form_saved = TRUE;

// repopulate the form fields
$form = arr::overwrite($form, $post->as_array());

}

// No! We have validation errors, we need to show the form again,
// with the errors
else
{
// repopulate the form fields
$form = arr::overwrite($form, $post->as_array());

// populate the error fields, if any
$errors = arr::overwrite($errors, $post->errors('settings'));
$form_error = TRUE;
}
}
else
{
// Retrieve Current Settings
$growl = ORM::factory('growl', 1);

$form = array
(
'ips' => $growl->ips,
'passwords' => $growl->passwords
);
}

// Pass the $form on to the settings_form variable in the view
$this->template->content->settings_form->form = $form;

// Other variables
$this->template->content->errors = $errors;
$this->template->content->form_error = $form_error;
$this->template->content->form_saved = $form_saved;
}
}
129 changes: 129 additions & 0 deletions hooks/_growl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Growl Hook - Load All Events
*
* PHP version 5
* LICENSE: This source file is subject to LGPL license
* that is available through the world-wide-web at the following URI:
* http://www.gnu.org/copyleft/lesser.html
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi - http://source.ushahididev.com
* @module Growl Hook
* @copyright Ushahidi - http://www.ushahidi.com
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
*/

// This class was heavily inspired by and largely copied from Tyler Hall's proof of concept on http://github.com/tylerhall/php-growl

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;

private $appName;
private $address;
private $notifications;
private $password;
private $port;
private $title;
private $message;

/**
* Registers the main event add method
*/
public function __construct($message='Default Message')
{
$settings = ORM::factory('growl')->find(1);
$ips = $settings->ips;
$pws = $settings->passwords;

$this->appName = Kohana::config('settings.site_name');
$this->address = $ips;
$this->notifications = array();
$this->password = $pws;
$this->port = 9887;
$this->message = $message;

// Events
//echo Event::add('ushahidi_action.report_add', array($this, 'growlit'));
$this->growlit();
}

public function growlit(){
$this->addNotification('Ushahidi');
$this->register();
$this->notify('Ushahidi',$this->appName,$this->message,0,true);
}

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

public function register()
{
$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++;
}
}

// 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));
$this->send($data);

return true;
}

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 |= 256;

// 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;
}
}

//new growl;
42 changes: 42 additions & 0 deletions hooks/growl_events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Growl Events Hook - Load All Events
*
* PHP version 5
* LICENSE: This source file is subject to LGPL license
* that is available through the world-wide-web at the following URI:
* http://www.gnu.org/copyleft/lesser.html
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi - http://source.ushahididev.com
* @module Growl Hook
* @copyright Ushahidi - http://www.ushahidi.com
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
*/

class growl_events {

public function __construct()
{
Event::add('ushahidi_action.report_add', array($this, 'report_add'));
Event::add('ushahidi_action.comment_add', array($this, 'comment_add'));
Event::add('ushahidi_action.report_delete ', array($this, 'report_delete'));
}

public function report_add(){
$incident = Event::$data;
new growl('New Report: '.$incident->incident_title);
}

public function comment_add(){
$comment = Event::$data;
new growl('New Comment on Incident ID '.$comment->incident_id.': '.$comment->comment_author.' ('.$comment->comment_email.') - '.$comment->comment_description);
}

public function report_delete(){
// Doesn't work.
new growl('Report Deleted');
}

}

new growl_events;
51 changes: 51 additions & 0 deletions libraries/growl_install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Performs install/uninstall methods for the Growl plugin
*
* PHP version 5
* LICENSE: This source file is subject to LGPL license
* that is available through the world-wide-web at the following URI:
* http://www.gnu.org/copyleft/lesser.html
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi - http://source.ushahididev.com
* @module Growl Installer
* @copyright Ushahidi - http://www.ushahidi.com
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
*/

class Growl_Install {

/**
* Constructor to load the shared database library
*/
public function __construct()
{
$this->db = Database::instance();
}

/**
* Creates the required database tables for the Growl plugin
*/
public function run_install()
{
// Create the database tables.
// Also include table_prefix in name
$this->db->query('CREATE TABLE `'.Kohana::config('database.default.table_prefix').'growl` (
`id` tinyint(4) NOT NULL,
`ips` text CHARACTER SET latin1 NOT NULL,
`passwords` text CHARACTER SET latin1 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;');

// Create settings row
$this->db->query('INSERT INTO `'.Kohana::config('database.default.table_prefix').'growl` (`id`) VALUES (1)');
}

/**
* Deletes the database tables for the Growl module
*/
public function uninstall()
{
$this->db->query('DROP TABLE `'.Kohana::config('database.default.table_prefix').'growl`');
}
}
20 changes: 20 additions & 0 deletions models/growl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Model for Growl
*
* PHP version 5
* LICENSE: This source file is subject to LGPL license
* that is available through the world-wide-web at the following URI:
* http://www.gnu.org/copyleft/lesser.html
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi - http://source.ushahididev.com
* @module Growl Model
* @copyright Ushahidi - http://www.ushahidi.com
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
*/

class Growl_Model extends ORM
{
// Database table name
protected $table_name = 'growl';
}
20 changes: 20 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== About ===
name: Growl
website: http://www.ushahidi.com
description: Allows you to push notifications to Growl via Ushahidi
version: 0.1
requires: 2.0
tested up to: 2.0
author: Brian Herbert
author website: http://www.brianherbert.com

== Description ==
This plugin allows you to receive Growl notifications when certain events happen on your deployment.

== Installation ==
1. Copy the entire /growl/ directory into your /plugins/ directory.
2. Activate the plugin.

== Changelog ==
v0.1 - October 23, 2010
* Initial build
Loading

0 comments on commit a489ecd

Please sign in to comment.