<?php
/*
Plugin Name: Remote Growl
Plugin URI: http://selfdestruct.net/
Description: Send a Remote Growl notification on certain events
Version: 0.2alpha
Author: Toni Viemero
Author URI: http://selfdestruct.net/
*/

function md5_base64($data) {
    return 
preg_replace('/=+$/','',base64_encode(pack('H*',md5($data))));


function 
remotegrowl_connect($addr$port$timeout 5) {
    if (
is_resource($sock)) {
        @
socket_close($sock);
        
$sock null;
    }

    if (!
$addr || !$port) {
        die(
'Remote IP address or host name not specified');
    } elseif (
preg_match('/^d+.d+.d+.d+/'$addr)) {
        
$addr $addr;
    } else {
        
$addr = @gethostbyname($addr);
    }

    
$port    $port 65536;
    
$timeout $timeout;

    
$sock socket_create(AF_INETSOCK_DGRAMSOL_UDP);
    
socket_set_option($sockSOL_SOCKETSO_RCVTIMEO, array("sec" => $timeout"usec" => 0));
    
socket_set_option($sockSOL_SOCKETSO_SNDTIMEO, array("sec" => $timeout"usec" => 0));
    
socket_connect($sock$addr$port);

    if (!
$sock) {
        die(
'Unable to connect socket: 'socket_strerror(socket_last_error()));
    }

    return 
$sock;
}

function 
remotegrowl_disconnect($sock) {
    if (
is_resource($sock)) {
        @
socket_close($sock);
        
$sock null;
    }
}

function 
remotegrowl_write($type$header$message) {

    
$addr        get_option('remotegrowl_addr');
    
$port        get_option('remotegrowl_port');
    
$password    get_option('remotegrowl_password');
    
$application get_option('remotegrowl_application');

    
$sock remotegrowl_connect($addr$port);

    if (!
is_resource($sock)) {
        die(
'Socket not connected');
    }

    
$regmsg  'register|'$application .'|'$type;
    
$regmsg .= '$'md5_base64($regmsg $password);

    
socket_write($sock$regmsg);

    
$datagram 'notify|'$application .'|'$type .'|'$header .'|'$message;

    
$checksum '$' md5_base64($datagram $password);
    
socket_write($sock$datagram $checksum);

    
remotegrowl_disconnect($sock);

}

function 
remotegrowl_action_comment_post($comment_id) {
    global 
$wpdb;

    
$post_title $wpdb->get_var('SELECT p.post_title FROM '$wpdb->comments .' c, '$wpdb->posts .' p WHERE c.comment_ID = '$comment_id .' AND c.comment_post_ID = p.ID');

    
$type    'notify';
    
$header  __('New comment posted');
    
$message 'New comment posted to "'$post_title .'"';
    
remotegrowl_write($type$header$message);
}

function 
remotegrowl_action_publish_post($post_id) {
    global 
$wpdb;

    
$post_title $wpdb->get_var('SELECT post_title FROM '$wpdb->posts .' WHERE ID = '$post_id);

    
$type   'notify';
    
$header __('New blog entry');
    
$message 'New blog entry "'$post_title .'"';
    
remotegrowl_write($type$header$message);
}

function 
remotegrowl_action_user_register($user_id) {
    global 
$wpdb;

    
$user_login $wpdb->get_var('SELECT user_login FROM '$wpdb->users .' WHERE ID = '$user_id);

    
$type   'notify';
    
$header __('New user registered');
    
$message 'New user "'$user_login .'" registered';
    
remotegrowl_write($type$header$message);
}

function 
remotegrowl_action_wp_login($user_login) {
    
$type   'notify';
    
$header __('User logged in');
    
$message 'User "'$user_login .'" logged in';
    
remotegrowl_write($type$header$message);
}

function 
remotegrowl_action_search($string) {
    
$type   'notify';
    
$header __('New search query');
    
$message 'Someone just searched for "'$string .'"';
    
remotegrowl_write($type$header$message);
}

function 
add_remotegrowl_option_page() {
    if (
function_exists('add_submenu_page')) {
        
add_submenu_page('plugins.php''Remote Growl Options Page''Remote Growl'8basename(__FILE__), 'remotegrowl_options_page');
    }
}

function 
remotegrowl_options_page() {
    if (isset(
$_POST['update'])) {
        
$options = array('remotegrowl_addr',
                         
'remotegrowl_port',
                         
'remotegrowl_password',
                         
'remotegrowl_application'
                        
);

        foreach (
$options as $option) {
            
$value trim($_POST[$option]);
            
update_option($option$value);
        }

        echo 
"<div class='updated'><p><strong>Remote Growl options updated</strong></p></div>";
    }
    if (!
function_exists('socket_create')) { ?>
        <div class='error'><p><strong>Your PHP does not support sockets</strong></p></div>
    <?php
    
} else {
    
?>
        <form method="post" action="plugins.php?page=remotegrowl.php">
        <div class="wrap">
            <h2>Remote Growl Options</h2>
            <fieldset class='options'>
                <legend>Remote Growl Options</legend>
                <table class="editform" cellspacing="2" cellpadding="5" width="100%">
                    <tr>
                        <th valign="top" style="padding-top: 10px;">
                            <label for="remotegrowl_addr">Remote Growl IP or host:</label>
                        </th>
                        <td>
                            <?php
                            
echo "<input type='text' size='50' ";
                            echo 
"name='remotegrowl_addr' ";
                            echo 
"id='remotegrowl_addr' ";
                            echo 
"value='".get_option(remotegrowl_addr)."' />\n";
                            
?>
                            <p style="margin: 5px 10px;">For example <em>127.0.0.1</em> or <em>localhost</em></p>
                        </td>
                    </tr>
                    <tr>
                        <th valign="top" style="padding-top: 10px;">
                            <label for="remotegrowl_port>">Remote Growl port:</label>
                        </th>
                        <td>
                            <?php
                            
echo "<input type='text' size='50' ";
                            echo 
"name='remotegrowl_port' ";
                            echo 
"id='remotegrowl_port' ";
                            echo 
"value='".get_option(remotegrowl_port)."' />\n";
                            
?>
                            <p style="margin: 5px 10px;">For example <em>9955</em></p>
                        </td>
                    </tr>
                    <tr>
                        <th valign="top" style="padding-top: 10px;">
                            <label for="remotegrowl_password">Remote Growl password:</label>
                        </th>
                        <td>
                            <?php
                            
echo "<input type='text' size='50' ";
                            echo 
"name='remotegrowl_password' ";
                            echo 
"id='remotegrowl_password' ";
                            echo 
"value='".get_option(remotegrowl_password)."' />\n";
                            
?>
                            <p style="margin: 5px 10px;">For example <em>test</em></p>
                        </td>
                    </tr>
                    <tr>
                        <th valign="top" style="padding-top: 10px;">
                            <label for="remotegrowl_application">Application name:</label>
                        </th>
                        <td>
                            <?php
                            
echo "<input type='text' size='50' ";
                            echo 
"name='remotegrowl_application' ";
                            echo 
"id='remotegrowl_application' ";
                            echo 
"value='".get_option(remotegrowl_application)."' />\n";
                            
?>
                            <p style="margin: 5px 10px;">For example <em>WordPress</em></p>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" class="submit">
                            <input type='submit' name='update' value='Update Options' />
                        </td>
                    </tr>
                </table>
            </fieldset>
        </div>
        </form>
    <?php
    
}
}

add_action('admin_menu''add_remotegrowl_option_page');

add_action('comment_post''remotegrowl_action_comment_post');
add_action('publish_post''remotegrowl_action_publish_post');
add_action('user_register''remotegrowl_action_user_register');
add_action('wp_login''remotegrowl_action_wp_login');

if (isset(
$_GET['s'])) {
    
remotegrowl_action_search($_GET['s']);
}