#!/usr/bin/perl -wTI.
#
# TWiki WikiClone (see wiki.pm for $wikiversion and other info)
#
# Based on parts of Ward Cunninghams original Wiki and JosWiki.
# Copyright (C) 1998 Markus Peter - SPiN GmbH (warpi@spin.de)
# Some changes by Dave Harris (drh@bhresearch.co.uk) incorporated
# Copyright (C) 1999-2000 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

use CGI;
use wiki;

$query = new CGI;

&main();

sub buildCategoryItem
{
    my ( $catitems, $ctext ) = @_;
    my $catname = "", $scatname = "", $catmodifier = "", $catvalue = "";
    my @cmd = split( /\|/, $catitems );
    my $src = "";
    my $len = @cmd;
    if( $len < "2" ) {
        return ( $catname, $catmodifier, $catvalue )
    }

    # check for CategoryName=CategoryValue parameter
    my $cvalue = $query->param( $cmd[1] );
    if( $cvalue ) {
        $src = "<!---->$cvalue<!---->";
    } elsif( $ctext ) {
        foreach( split( /\n/, $ctext ) ) {
            if( /$cmd[1]/ ) {
                $src = $_;
                last;
            }
        }
    }

    if( $cmd[0] eq "select" ) {
        $catname = $cmd[1];
        $scatname = $catname;
        $scatname =~ s/[^a-zA-z0-9]//g;
        my $size = $cmd[2];
        $catvalue = "<select name=\"$scatname\" size=\"$size\">";
        for( $i = 3; $i < $len; $i++ ) {
            my $value = $cmd[$i];
            my $svalue = $value;
            $svalue =~ s/[^a-zA-z0-9]//g;
            my $flag = "";
            if( $src =~ /$value/ ) {
                $flag = "selected";
            }
            $catvalue = "$catvalue\n<option value=\"$svalue\" $flag>$value";
        }
        $catvalue = "$catvalue\n</select>";

    } elsif( $cmd[0] eq "checkbox" ) {
        $catname = $cmd[1];
        $scatname = $catname;
        $scatname =~ s/[^a-zA-z0-9]//g;
        if( $cmd[2] eq "true" || $cmd[2] eq "1" ) {
            $i = $len - 4;
            $catmodifier = "$catmodifier\n<input type=\"button\" value=\" Set \" onClick=\"checkAll(this, 2, $i, true)\">&nbsp;";
            $catmodifier = "$catmodifier\n<input type=\"button\" value=\"Clear\" onClick=\"checkAll(this, 1, $i, false)\">&nbsp;";
        }
        $itemsPerLine = $cmd[3];
        $catvalue = "\n<table cellspacing=\"0\" cellpadding=\"0\"><tr>";
        for( $i = 4; $i < $len; $i++ ) {
            my $value = $cmd[$i];
            my $svalue = $value;
            $svalue =~ s/[^a-zA-z0-9]//g;
            my $flag = "";
            if( $src =~ /$value[^a-zA-Z0-9\.]/ ) {
                $flag = "checked";
            }
            $catvalue = "$catvalue\n<td><input type=\"checkbox\" name=\"$scatname$svalue\" $flag>$value &nbsp;&nbsp;</td>";
            if( ( $itemsPerLine > 0 ) && (($i-4) % $itemsPerLine == $itemsPerLine - 1 ) ) {
                $catvalue = "$catvalue\n</tr><tr>";
            }
        }
        $catvalue = "$catvalue\n</tr></table>\n";

    } elsif( $cmd[0] eq "radio" ) {
        $catname = $cmd[1];
        $scatname = $catname;
        $scatname =~ s/[^a-zA-z0-9]//g;
        $itemsPerLine = $cmd[2];
        $catvalue = "\n<table cellspacing=\"0\" cellpadding=\"0\"><tr>";
        for( $i = 3; $i < $len; $i++ ) {
            my $value = $cmd[$i];
            my $svalue = $value;
            $svalue =~ s/[^a-zA-z0-9]//g;
            my $flag = "";
            if( $src =~ /$value[^a-zA-Z0-9\.]/ ) {
                $flag = "checked";
            } elsif( ( $scatname eq "UseCategory" ) && 
                     ( ( $i == 3 && $ctext ) || ( $i == 4 && ! $ctext ) ) ) {
                $flag = "checked";
            }
            $catvalue = "$catvalue\n<td><input type=\"radio\" name=\"$scatname\" value=\"$svalue\" $flag>$value &nbsp;&nbsp;</td>";
            if( ( $itemsPerLine > 0 ) && (($i-3) % $itemsPerLine == $itemsPerLine - 1 ) ) {
                $catvalue = "$catvalue\n</tr><tr>";
            }
        }
        $catvalue = "$catvalue\n</tr></table>\n";

    } elsif( $cmd[0] eq "text" ) {
        $catname = $cmd[1];
        $scatname = $catname;
        $scatname =~ s/[^a-zA-z0-9]//g;
        $src =~ /<!---->(.*)<!---->/;
        if( $1 ) {
            $src = $1;
        } else {
            $src = "";
        }
        $catvalue = "<input type=\"text\" name=\"$scatname\" size=\"$cmd[2]\" value=\"$src\" >";
    }

    return ( $catname, $catmodifier, $catvalue )
}

sub main
{
    my $thePathInfo = $query->path_info(); 
    my $theRemoteUser = $query->remote_user();
    my $theTopic = $query->param( 'topic' ) || "";
    my $theUrl = $query->url;

    ( $topic, $webName, $dummy, $userName ) = 
	&wiki::initialize( $thePathInfo, $theRemoteUser, $theTopic, $theUrl );
    $dummy = "";  # to suppress warning

    my $saveCmd = $query->param( 'cmd' ) || "";
    my $breakLock = $query->param( 'breaklock' ) || "";
    my $onlyWikiName = $query->param( 'onlywikiname' ) || "";
    my $tmpl = "";
    my $tcat = "";
    my $icat = "";
    my $text = "";
    my $before = "";
    my $atext = "";
    my $ctext = "";
    my $after = "";
    my $catname = "";
    my $catmod = "";
    my $catvalue = "";
    my $extra = "";
    my $wikiUserName = &wiki::userToWikiName( $userName );

    if( ! &wiki::webExists( $webName ) ) {
        print "Content-type: text/html\n\n";
	$tmpl= &wiki::readTemplate( "noweb" );
	$tmpl = &wiki::handleCommonTags( $tmpl, $topic );
	print $tmpl;
        return;
    }

    if( ( $onlyWikiName ) && ( ! &wiki::isWikiName( $topic ) ) && ( ! &wiki::topicExists( $webName, $topic ) ) ) {
        # do not allow non-wikinames, redirect to view topic
        print $query->redirect( &wiki::getViewUrl( $webName, $topic ) );
	return;
    }

    # check access permission
    if( &wiki::topicExists( $webName, $topic ) ) {
	$text = &wiki::readTopic( $topic );
    }
    if( ! &wiki::checkAccessPermission( "change", $wikiUserName, $text, $topic, $webName ) ) {
        # user has not permission to change the topic
        my $url = &wiki::getOopsUrl( $webName, $topic, "oopsaccesschange" );
        print $query->redirect( $url );
        return;
    }
    if( ( $saveCmd ) &&
        ( ! &wiki::userIsInGroup( $wikiUserName, "TWikiAdminGroup" ) ) ) {
        # user has no permission to execute undocumented cmd=... parameter
        my $url = &wiki::getOopsUrl( $webName, $topic, "oopsaccessgroup", "$wiki::mainWebname.TWikiAdminGroup" );
        print $query->redirect( $url );
        return;
    }

    my( $lockUser, $lockTime ) = &wiki::topicIsLocked( $topic );
    if( ( ! $breakLock ) && ( $lockUser ) ) {
        # warn user that other person is editing this topic
        $lockUser = &wiki::userToWikiName( $lockUser );
        use integer;
        $lockTime = ( $lockTime / 60 ) + 1; # convert to minutes
        my $editLock = $wiki::editLockTime / 60;
        # PTh 20 Jun 2000: changed to getOopsUrl
        my $url = &wiki::getOopsUrl( $webName, $topic, "oopslocked",
            $lockUser, $editLock, $lockTime );
        print $query->redirect( $url );
	return;
    }
    &wiki::lockTopic( $topic );

    print "Content-type: text/html\n\n";

    $tmpl= &wiki::readTemplate( "edit", $topic );
    $icat= &wiki::readTemplate( "twikicatitems" );
    if( ! &wiki::topicExists( $webName, $topic ) ) {
        $text= &wiki::readTemplate( "notedited" );
        $extra = "(not exist)";

        my $foo = &wiki::getLocaldate();
        $text =~ s/%DATE%/$foo/go;
        $text =~ s/%USERNAME%/$wikiUserName/go;
    }

    if( $icat ) {
        # extract category section and build category form elements
        ( $before, $ctext, $after) = split( /<!--TWikiCat-->/, $text);
        # cut TWikiCat part
        $text = $before;
        if( ! $ctext ) { $ctext = ""; }
        if( $after ) {
            $text .= $after;
        }

        $tcat = &wiki::readTemplate( "twikicatedit" );
        if( ! $tcat ) {
            $tcat = &wiki::readTemplate( "twikicatview" );
        }
        my $line = "", $ttext = "", $temp = "";
        ( $before, $line, $after) = split( /%REPEAT%/, $tcat );
        foreach( split( /\n/, $icat ) ) {
            ( $catname, $catmod, $catvalue ) = buildCategoryItem( $_, $ctext );
            if( $catname ) {
                $catname = &wiki::getRenderedVersion( $catname );
                $temp = $line;
                $temp =~ s/%CATNAME%/$catname/go;
                $temp =~ s/%CATMODIFIER%/$catmod/go;
                $temp =~ s/%CATVALUE%/$catvalue/go;
                $ttext = "$ttext$temp";
            }
        }
        $tcat = "$before$ttext$after";
    }

    if( $saveCmd ne "repRev" ) {
        # cut attachment section if not in debug mode
        ( $before, $atext, $after) = split( /<!--TWikiAttachment-->/, $text );
        $text = $before;
        if( $after ) {
            $text .= $after;
        }
    }

    $text =~ s/&/&amp\;/go;
    $text =~ s/</&lt\;/go;
    $text =~ s/>/&gt\;/go;
    $text =~ s/\t/   /go;

    if( $wiki::doLogTopicEdit ) {
        # write log entry
        &wiki::writeLog( "edit", "$webName.$topic", $extra );
    }

    if( $saveCmd ) {
        $tmpl =~ s/\(edit\)/\(edit cmd=$saveCmd\)/go;
    }
    $tmpl =~ s/%CMD%/$saveCmd/go;
    $tmpl = &wiki::handleCommonTags( $tmpl, $topic );

    $tmpl =~ s/%TEXT%/$text/go;
    $tmpl =~ s/%TWIKICAT%/$tcat/go;

    print $tmpl;
}
