# Plugin for TWiki Collaboration Platform, http://TWiki.org/ # # Copyright (C) 2000-2003 Andrea Sterbini, a.sterbini@flashnet.it # Copyright (C) 2001-2003 Peter Thoeny, peter@thoeny.com # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details, published at # http://www.gnu.org/copyleft/gpl.html # # ========================= # # This Plugin is meant to improve upon SessionPlugin. Do not install # them both at the same time. However, never fear, this plugin has # absorbed all of the functionality of SessionPlugin. It simply # has been implemented more cleanly and does not require silly # things like logon scripts (though they are certainly still # supported). # # Each plugin is a package that may contain these functions: VERSION: # # initPlugin ( $topic, $web, $user, $installWeb ) 1.000 # initializeUserHandler ( $loginName, $url, $pathInfo ) 1.010 # registrationHandler ( $web, $wikiName, $loginName ) 1.010 # commonTagsHandler ( $text, $topic, $web ) 1.000 # startRenderingHandler ( $text, $web ) 1.000 # outsidePREHandler ( $text ) 1.000 # insidePREHandler ( $text ) 1.000 # endRenderingHandler ( $text ) 1.000 # beforeEditHandler ( $text, $topic, $web ) 1.010 # afterEditHandler ( $text, $topic, $web ) 1.010 # beforeSaveHandler ( $text, $topic, $web ) 1.010 # writeHeaderHandler ( $query ) 1.010 Use only in one Plugin # redirectCgiQueryHandler ( $query, $url ) 1.010 Use only in one Plugin # getSessionValueHandler ( $key ) 1.010 Use only in one Plugin # setSessionValueHandler ( $key, $value ) 1.010 Use only in one Plugin # # ========================= package TWiki::Plugins::SessionPlugin; # ========================= use vars qw( $web $topic $user $installWeb $VERSION $pluginName $doneInit $debug $fixUrls $query $session $sessionId $authUser ); use strict; # Use CGI to get cookie use CGI; # Use CGI::Session to handle session information. # # Considered using Apache::Session or PHP::Session, but CGI::Session # seemed the "most" platform independent. :) use CGI::Session; $VERSION = '1.010'; $pluginName = 'SessionPlugin'; # Name of this Plugin # Init must be called first in order to be able to # setup username information soon enough. _init(); # ========================= sub initPlugin { ( $topic, $web, $user, $installWeb ) = @_; # check for Plugins.pm versions if( $TWiki::Plugins::VERSION < 1 ) { TWiki::Func::writeWarning( "Version mismatch between $pluginName and Plugins.pm" ); return 0; } _init(); # Get plugin debug flag $debug = TWiki::Func::getPreferencesFlag( "\U$pluginName\E_DEBUG" ); # Get whether or not URLs should have sessions tagged onto the end of them $fixUrls = TWiki::Prefs::getPreferencesValue( "\U$pluginName\E_FIXURLS" ); # Plugin correctly initialized TWiki::Func::writeDebug( "- TWiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK" ) if $debug; return 1; } # ========================= sub initializeUserHandler { ### my ( $loginName, $url, $pathInfo ) = @_; # do not uncomment, use $_[0], $_[1]... instead TWiki::Func::writeDebug( "- ${pluginName}::initializeUserHandler( $_[0], $_[1] )" ) if $debug; # Allows a plugin to set the username based on cookies. Called by TWiki::initialize. # Return the user name, or "guest" if not logged in. # New hook in TWiki::Plugins $VERSION = '1.010' return $authUser; } # ========================= sub DISABLE_commonTagsHandler { ### my ( $text, $topic, $web ) = @_; # do not uncomment, use $_[0], $_[1]... instead TWiki::Func::writeDebug( "- ${pluginName}::commonTagsHandler( $_[2].$_[1] )" ) if $debug; # This is the place to define customized tags and variables # Called by sub handleCommonTags, after %INCLUDE:"..."% # do custom extension rule, like for example: # $_[0] =~ s/%XYZ%/&handleXyz()/ge; # $_[0] =~ s/%XYZ{(.*?)}%/&handleXyz($1)/ge; } # ========================= sub DISABLE_startRenderingHandler { ### my ( $text, $web ) = @_; # do not uncomment, use $_[0], $_[1] instead TWiki::Func::writeDebug( "- ${pluginName}::startRenderingHandler( $_[1] )" ) if $debug; # This handler is called by getRenderedVersion just before the line loop # do custom extension rule, like for example: # $_[0] =~ s/old/new/g; } # ========================= sub DISABLE_outsidePREHandler { ### my ( $text ) = @_; # do not uncomment, use $_[0] instead ##TWiki::Func::writeDebug( "- ${pluginName}::outsidePREHandler( $renderingWeb.$topic )" ) if $debug; # This handler is called by getRenderedVersion, once per line, before any changes, # for lines outside
 and  tags. 
    # Use it to define customized rendering rules.
    # Note: This is an expensive function to comment out.
    # Consider startRenderingHandler instead

    # do custom extension rule, like for example:
    # $_[0] =~ s/old/new/g;
}

# =========================
sub DISABLE_insidePREHandler
{
### my ( $text ) = @_;   # do not uncomment, use $_[0] instead

    ##TWiki::Func::writeDebug( "- ${pluginName}::insidePREHandler( $web.$topic )" ) if $debug;

    # This handler is called by getRenderedVersion, once per line, before any changes,
    # for lines inside 
 and  tags. 
    # Use it to define customized rendering rules.
    # Note: This is an expensive function to comment out.
    # Consider startRenderingHandler instead

    # do custom extension rule, like for example:
    # $_[0] =~ s/old/new/g;
}

# =========================
sub endRenderingHandler
{
### my ( $text ) = @_;   # do not uncomment, use $_[0] instead

    TWiki::Func::writeDebug( "- ${pluginName}::endRenderingHandler( $web.$topic )" ) if $debug;

    # This handler is called by getRenderedVersion just after the line loop, that is,
    # after almost all XHTML rendering of a topic.  tags are removed after this.

    $_[0] =~ s/%SESSIONID%/$sessionId/ge;

    # Backwards compatibility with old SessionPlugin
    # this MUST render after TigerSkinPlugin commonTagsHandler does TIGERLOGON
    $_[0] =~ s/%SESSIONLOGON%/&_dispLogon()/geo;
    $_[0] =~ s/%SKINSELECT%/&_skinSelect()/geo;

}

# =========================
sub DISABLE_beforeEditHandler
{
### my ( $text, $topic, $web ) = @_;   # do not uncomment, use $_[0], $_[1]... instead

    TWiki::Func::writeDebug( "- ${pluginName}::beforeEditHandler( $_[2].$_[1] )" ) if $debug;

    # This handler is called by the edit script just before presenting the edit text
    # in the edit box. Use it to process the text before editing.
    # New hook in TWiki::Plugins $VERSION = '1.010'

}

# =========================
sub DISABLE_afterEditHandler
{
### my ( $text, $topic, $web ) = @_;   # do not uncomment, use $_[0], $_[1]... instead

    TWiki::Func::writeDebug( "- ${pluginName}::afterEditHandler( $_[2].$_[1] )" ) if $debug;

    # This handler is called by the preview script just before presenting the text.
    # New hook in TWiki::Plugins $VERSION = '1.010'

}

# =========================
sub DISABLE_beforeSaveHandler
{
### my ( $text, $topic, $web ) = @_;   # do not uncomment, use $_[0], $_[1]... instead

    TWiki::Func::writeDebug( "- ${pluginName}::beforeSaveHandler( $_[2].$_[1] )" ) if $debug;

    # This handler is called by TWiki::Store::saveTopic just before the save action.
    # New hook in TWiki::Plugins $VERSION = '1.010'

}

# =========================
sub writeHeaderHandler
{
### my ( $query ) = @_;   # do not uncomment, use $_[0] instead

    TWiki::Func::writeDebug( "- ${pluginName}::writeHeaderHandler( query )" ) if $debug;

    # This handler is called by TWiki::writeHeader, just prior to writing header. 
    # Return a single result: A string containing HTTP headers, delimited by CR/LF
    # and with no blank lines. Plugin generated headers may be modified by core
    # code before they are output, to fix bugs or manage caching. Plugins should no
    # longer write headers to standard output.
    # Use only in one Plugin.
    # New hook in TWiki::Plugins $VERSION = '1.010'

# This is the ideal solution, but the autoloaded header.al
# file has a hard time being found. This wouldn't be a problem
# on machines where CGI::Session is installed in the standard
# Perl directories or if PERL5LIB was set, but in the default
# TWiki environment where twiki/lib could be the only interesting
# Perl library around, it makes things more complicated.
#    return $session->header();

# So instead we just do exactly what $session->header() does
# internally. (and be sure we set path correctly)
    my $cookie = new CGI::Cookie(-name=>'CGISESSID', -value=>$session->id, -path=>"/");
    return $query->header(-cookie=>$cookie);

}

# =========================
sub getSessionValueHandler
{
### my ( $key ) = @_;   # do not uncomment, use $_[0] instead

    TWiki::Func::writeDebug( "- ${pluginName}::getSessionValueHandler( $_[0] )" ) if $debug;

    # This handler is called by TWiki::getSessionValue. Return the value of a key.
    # Use only in one Plugin.
    # New hook in TWiki::Plugins $VERSION = '1.010'

    return $session->param( $_[0] );

}

# =========================
sub setSessionValueHandler
{
### my ( $key, $value ) = @_;   # do not uncomment, use $_[0], $_[1] instead

    TWiki::Func::writeDebug( "- ${pluginName}::setSessionValueHandler( $_[0], $_[1] )" ) if $debug;

    # This handler is called by TWiki::setSessionValue. 
    # Use only in one Plugin.
    # New hook in TWiki::Plugins $VERSION = '1.010'

    if( defined( $session->param( $_[0], $_[1] ) ) )
    {
        return 1;
    }

    return undef;

}

# =========================


# ============= PRIVATE member functions

sub _init
{
    return if( $doneInit );

    $doneInit = 1;

    $query = TWiki::Func::getCgiQuery();
    return 0 if( ! $query );

    # Initialize the session
    $session = new CGI::Session("driver:File", $query, {Directory=>'/tmp'});

    # Get the sessionId
    $sessionId = $session->id();
    TWiki::Func::writeDebug( "- TWiki::Plugins::${pluginName}::_init using sessionId = $sessionId" ) if $debug;

    # See whether the user was logged in (first webserver, then session, then default)
    $authUser = $ENV{REMOTE_USER} ||
                $session->param( "AUTHUSER" ) ||
                TWiki::Func::getDefaultUserName(); 

    # Save the user's information again if they do not appear to be a guest 
    if( TWiki::Func::getDefaultUserName() ne $authUser )
    {
        $session->param( "AUTHUSER", $authUser );
    }

    # See whether user has decided to set a stickskin
    my $stickskin = $query->param( 'stickskin' );

    # If a stickskin has been selected, save it in the session
    if( $stickskin ) {
        $session->param( "SKIN", $stickskin );
    }

}

# =========================
sub _dispLogon
{
    my $logon = "Logon>>";

    return $logon;
}

# =========================
sub _skinSelect
{
    my $html = "\n";
    return $html;
}

# =========================

1;