#!/usr/bin/perl -w
#
# 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

#This is a hack to the standard "save" script until Plugins are allowed
#to handle form processing

# It still needs to do the same kind of general checks that "save" does.

#Version history...
#30 Nov 2001: Initial version
#4 Dec 2001: 1.01 release, changed name to CommentPlugin,
#            added $button var, changed textarea WRAP setting to "soft"
#24 Feb 2002: added a few more user requests, made "English" text configurable
#5 March 2002: Bug fixes
#6 March 2002: Hopefully the LAST bug fix for savecomment for now...
#              formattedPost was processing TWiki variables incorrectly
#9 Dec 2002: Changed to strict mode
#            updated library includes to match beta TWiki - JonLambert
#11 Dec 2002: Added check for locked pages
#            will redirect to a new oopslockedcomment.tmpl - JonLambert
#15 July 2003: passing comments to oops template - to copy-paste if BACK is broken


# Set library paths in @INC, at compile time
BEGIN { unshift @INC, '.'; require 'setlib.cfg'; }

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use TWiki;
use TWiki::Plugins::CommentPlugin;

use strict;

use vars qw( $query );

$query= new CGI;

&main();

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

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

    my $wikiUserName = &TWiki::userToWikiName( $userName );

    if( ! &TWiki::Store::webExists( $webName ) ) {
        my $url = &TWiki::getOopsUrl( $webName, $topic, "oopsnoweb" );
        TWiki::redirect( $query, $url );
        return;
    }

    my( $mirrorSiteName, $mirrorViewURL ) = &TWiki::readOnlyMirrorWeb( $webName );
    if( $mirrorSiteName ) {
        my $url = &TWiki::getOopsUrl( $webName, $topic, "oopsmirror", $mirrorSiteName, $mirrorViewURL );
        print $query->redirect( $url );
        return;
    }

    # check access permission using POST
    if( ! &TWiki::Access::checkAccessPermission( "post", $wikiUserName, "", $topic, $webName ) ) {
        my $url = &TWiki::getOopsUrl( $webName, $topic, "oopsaccesschange" );
        TWiki::redirect( $query, $url );
        return;
    }

    # get all text as entered, so user can copy-paste them somewhere if save failed
    my $comment_all = "URL: ".  $query->param('comment_url'). '<br />'.
                      "Link: ". $query->param('comment_link'). '<br />'.
                      "Text: ". $query->param('comment'). '<br />';

    # check for locks and if so redirect - JonLambert
    my( $lockUser, $lockTime ) = &TWiki::Store::topicIsLockedBy( $webName, $topic );
    if( $lockUser ) {
        # warn user that other person is editing this topic
        $lockUser = &TWiki::userToWikiName( $lockUser );
        use integer;
        $lockTime = ( $lockTime / 60 ) + 1; # convert to minutes
        my $editLock = $TWiki::editLockTime / 60;
        # PTh 20 Jun 2000: changed to getOopsUrl
        my $url = &TWiki::getOopsUrl( $webName, $topic, "oopslockedcomment",
            $lockUser, $editLock, $lockTime, $comment_all );
        TWiki::redirect( $query, $url );
        return;
    }

    #now just read in the topic file, format comments as mode requires, and save
    my ($meta, $text) =  &TWiki::Store::readTopic( $webName, $topic );
    $text = TWiki::Plugins::CommentPlugin::saveComment($query, $wikiUserName, $text); # will format text for save
    # will return (1) UNDEF if no save needed, (2) empty string if format error
    
    TWiki::redirect( $query, &TWiki::getViewUrl( "", $topic ) ) if not defined $text;

    my $error = '';
    $error = "COMMENT format error: cannot save $comment_all " unless $text;

    my $unlock = "on";
    my $dontNotify = "";
    my $saveCmd = "";
    
    $error = &TWiki::Store::saveTopic( $webName, $topic, $text, $meta, $saveCmd, $unlock, $dontNotify ) unless $error;
    if( $error ) {
        # S. Knutson 30 Nov 2000: error happened (probably from RCS), show it
        my $url = &TWiki::getOopsUrl( $webName, $topic, "oopssaveerr", $error );
        TWiki::redirect( $query, $url );
    } else {
        TWiki::redirect( $query, &TWiki::getViewUrl( "", $topic ) );
    }
}
