<?php /* -*- mode: php; tab-width: 4; c-basic-offset: 4; c-set-style: k&r -*- */
/*
 * Copyright (c) 2010 TrueStep
 * Author: Rory Arms - http://www.TrueStep.com/
 * Name: pi.netvibes_dashboard.php
 * Btime: 2009-12-11
 * Description: Plugin to generate NetVibes Premium Dashboard <iframe>
 * Tested with: EE-1.6.8/PHP-5.2.6
 * Tag syntax: {exp:netvibes_dashboard}
 * $Id: pi.netvibes_dashboard.php,v 1.2 2010/04/22 03:08:47 rorya Exp $
 */

//ini_set('display_errors', 1);
//error_reporting(E_ALL);

// EE plugin properties
$plugin_info = array(
                     
'pi_name' => 'Netvibes Premium Dashboard',
                     
'pi_version' => '1.0',
                     
'pi_author' => 'TrueStep',
                     
'pi_author_url' => 'http://www.TrueStep.com/',
                     
'pi_description' => 'Generates NetVibes Premium Dashboard &lt;iframe&gt;, based on members\' email address',
                     
'pi_usage' => Netvibes_dashboard::usage()
                     );

class 
Netvibes_dashboard 
{
    
// EE required vars
    
var $return_data "";

    
// app vars
    
var $debug;
    var 
$plugin_version "1.0";
    var 
$plugin_name "Netvibes_dashboard";
    var 
$date;

    function 
Netvibes_dashboard() {
        global 
$TMPL$SESS$IN;

        
// initialize
        //
        // debug toggle, overrides output with debug info
        // make sure this is set to FALSE, in production
        
$this->debug FALSE;

        
$unix_time time();
        
// init date with ISO-8601 format
        
$this->date date("Y-m-d\TH:i:sO");


        
// plugin parameters

        // required
        
$nv_public_page $TMPL->fetch_param('public_page');
        
$nv_api_key $TMPL->fetch_param('api_key');
        
$nv_secret_key $TMPL->fetch_param('secret_key');
        
$user_email $TMPL->fetch_param('user_email');

        
// optional
        
$id $TMPL->fetch_param('id');
        
$mode $TMPL->fetch_param('mode');
        
$start_tab $TMPL->fetch_param('start_tab');
        
$iframe_height $TMPL->fetch_param('height');
        
$opt_attribs $TMPL->fetch_param('opt_attribs');
        
$testmode $TMPL->fetch_param('testmode');
        
$verbose $TMPL->fetch_param('verbose');
        
$search_key $TMPL->fetch_param('search_key');

        
// setup diagnostics paramater defaults
        //

        // param: testmode
        
if ($testmode === "on") {
            
$verbose TRUE;
        } else if (
$testmode === "off")
            
$testmode NULL;

        
// param: verbose
        
if (! $verbose === "true")
            
$verbose NULL;

        
// required parameter defaults
        //

        // param: public_page
        
if (! $nv_public_page) {
            if (
$verbose) {
                
$this->return_data "<!-- netvibes_dashboard: public_page parameter required -->";
                return;
            }
        }

        
// param: api_key
        
if (! $nv_api_key) {
            if (
$verbose) {
                
$this->return_data "<!-- netvibes_dashboard: api_key paramater is required  -->";
                return;
            }
        }

        
// param: secret_key
        
if (! $nv_secret_key) {
            if (
$verbose) {
                
$this->return_data "<!-- netvibes_dashboard: secret_key paramater is required  -->";
                return;
            }
        }
        
// process any optional parameters
        //

        // param: id
        
if (! $id) {
            
//standard NetVibes id
            
$id "nvFrame";
        } else if (
$id == "") {
            
$id "nvFrame";
        }

        
// param: mode
        
if ($mode) {
            
// non-sso is an alias for no-sso
            
if ($mode == "non-sso") {
                
$mode "no-sso";
            }
        }

        
// param: user_email
        // if email user_email parameter is not supplied, use the logged in user's email
        
if (($mode == "sso") && (! $user_email)) {
            
$user_email $SESS->userdata['email'];
            if (
$user_email == "") {
                if (
$verbose)
                    
$this->return_data "<!-- netvibes_dashboard: Not logged in. No dashboard displayed -->";
                return;
            }
        }

        
// param: height
        
if (! $iframe_height)
            
$iframe_height 1300;

        
// param: opt_attribs
        
if ($opt_attribs) {
            if (! 
$opt_attribs == "") {
                
$opt_attribs " " $opt_attribs;
            } else {
                
$opt_attribs "";
            }
        } else {
            
$opt_attribs "";
        }

        
// param: start_tab
        
if ($start_tab) {
            if (
$start_tab == "") {
                
$start_tab "";
            } else {
                
$start_tab "#"$start_tab;
            }
        } else {
            
$start_tab "";
        }

        if (! 
$search_key) {
            
$search_key "search";
        }

        
// main

        // search query, if provided
        
$search_val $IN->GBL($search_key'GET');

        
// trim email address
        
$user_email trim($user_email);

        if (! 
$mode || $mode == "sso") {
            
// sso mode

            // create Premium Dashboard signature
            
if ($search_val) {
                
$payload "api_key${nv_api_key}search${search_val}timestamp${unix_time}user_email${user_email}${nv_secret_key}";
            } else {
                
$payload "api_key${nv_api_key}timestamp${unix_time}user_email${user_email}${nv_secret_key}";
            }

            
// generate sha1 checksum of payload
            
$signature sha1($payload);

            
$mode "sso";
            if (
$search_val) {
                
$iframe_sso_url htmlentities("http://www.netvibes.com/partner/${nv_public_page}.php?api_key=${nv_api_key}&search=${search_val}&timestamp=${unix_time}&user_email=${user_email}&sign=${signature}${start_tab}");
            } else {
                
$iframe_sso_url htmlentities("http://www.netvibes.com/partner/${nv_public_page}.php?api_key=${nv_api_key}&timestamp=${unix_time}&user_email=${user_email}&sign=${signature}${start_tab}");
            }

            
$iframe_html "<iframe id=\"${id}\" name=\"${id}\" height=\"${iframe_height}\" scrolling=\"no\" src=\"${iframe_sso_url}\"${opt_attribs}>
  [Sorry, your browser does not support iframes]
</iframe>"
;
        } else {
            
// no-sso mode
            
$mode "no-sso";

            if (
$search_val) {
                
$iframe_url htmlentities("http://www.netvibes.com/partner/premium.php?universe=${nv_public_page}&search=${search_val}${start_tab}");
            } else {
                
$iframe_url htmlentities("http://www.netvibes.com/partner/premium.php?universe=${nv_public_page}${start_tab}");
            }

            
$iframe_html "<iframe id=\"${id}\" name=\"${id}\" height=\"${iframe_height}\" scrolling=\"no\" src=\"${iframe_url}\"${opt_attribs}>
  [Sorry, your browser does not support iframes]
</iframe>"
;
        }

        if (
$this->debug) {
            print(
"-- "$this->plugin_name " debug --<br />\n");
            print(
"user_email: "$user_email ."<br />\n");
            print(
"payload: "$payload ."<br />\n");
            print(
"signature: "$signature ."<br />\n");
            print(
"url: "$iframe_url ."<br />\n");
        }

        
// generate Netvibes Premium Dashboard compliant <iframe> tag 
        
if ($testmode)
            
$this->return_data "<!-- " $this->plugin_name " testmode: \"on\"; mode: \"" $mode "\" -->\n<!-- " $iframe_html "\n-->";
        else
            
$this->return_data $iframe_html;

    }

    
// public
    // 3-segment tag: {exp:netvibes_dashboard:version}
    // returns int (plugin version)
    
function version() {
        return 
$this->plugin_version;
    }

    
// public
    
function usage() {
        
ob_start(); 
        
?>
The Netvibes_dashboard Plugin 
                 
Generates the Netvibes <iframe> needed to handle a NetVibes Premium Dashboard
service. By default, the email used with Netvibes is the email in the profile 
of the currently logged on (in EE) user. Supports both SSO and no-SSO modes of 
operation.

Usage:

{exp:netvibes_dashboard public_page="my_company" api_key="af8adac8807092b1e1e21662dda56142f3aa5b99" secret_key="3Ebac988343201cf3c7336684a3656ff3c2cd3d4"}

Parameters:

public_page - the name of the Netvibes public page. [required]
api_key - Netvibes API key [required]
secret_key - Netvibes secret key [required]
mode - Allowed values: sso|no-sso. Default: sso
user_email - email address to use with Netvibes. When not specified, logged in email address is used key [required w/ mode="sso"]
start_tab - the label of the tab to open when the Dashboard loads Default: off
height - the initial height of the iframe, in pixels Default: 1300
opt_attribs - set other, optional <iframe> tag attributes, manually. Default: off
testmode - Wrap <iframe> in HTML comment. Allowed values off|on. Default: off
verbose - Show errors in HTML comments. Allowed values true|false. Default: false
search_key - Only used if template that uses this template specifies a GET search query. If this is specified, it overrides the query string key name to use for search. Default: search

        <?php
        $buffer 
ob_get_contents();
        
ob_end_clean(); 

        return 
$buffer;
    } 
// end usage()
}