#!/usr/bin/perl -wT # # TWiki KupuEditorAddOn add-on for TWiki # Copyright (C) 2004 Damien Mandrioli and Romain Raugi # # Based on parts of TWiki. # Copyright (C) 1999-2004 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. BEGIN { # Set default current working directory (needed for mod_perl) if( $ENV{"SCRIPT_FILENAME"} && $ENV{"SCRIPT_FILENAME"} =~ /^(.+)\/[^\/]+$/ ) { chdir $1; } # Set library paths in @INC, at compile time unshift @INC, '.'; require 'setlib.cfg'; } use strict; use CGI::Carp qw( fatalsToBrowser ); use CGI; use TWiki; =pod ---++ _saveAttachment( $query, $userName, $webName, $topic, $meta, $content ) Save attachment. =cut sub _saveAttachment { my ( $query, $userName, $webName, $topic, $meta, $content ) = @_; my $comment = $query->param('comment'); my $hide = $query->param('hide'); my $file = $query->param('file'); my $upload_filehandle = $query->upload('file'); my $saveCmd = ""; my $dontLogSave = ""; my $doUnlock = ""; my $dontNotify = "1"; # Upload # Temporary name my $name = $file; $name =~ s/.*[\/\\](.*)/$1/; my $rnd = rand(10); my $tmpFilename = "$TWiki::pubDir/kupu_ul_$rnd.tmp"; # Write file content open UPLOADFILE, ">$tmpFilename" or return ( $meta, $content ); binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE; # Attribute setting $hide = "h" if ( $hide ); # Test existence my $size = -s $tmpFilename; my $error = undef; if ( -e $tmpFilename && $size ) { # Save Attachment $error = &TWiki::Store::saveAttachment( $webName, $topic, $content, $saveCmd, $name, $dontLogSave, $doUnlock, $dontNotify, $comment, $tmpFilename ); # Delete temp file unlink $tmpFilename; return ( $meta, $content ) if ( $error ); } else { return ( $meta, $content ); } # Create properties &TWiki::Attach::updateAttachment( "", $name, $file, $size, "", $userName, $comment, $hide, $meta ) if ( ! $error ); # Save topic $error = &TWiki::Store::saveTopic( $webName, $topic, $content, $meta, $saveCmd, $doUnlock, $dontNotify ); return ( $meta, $content ); } my $query = new CGI; # Initialization my $thePathInfo = $query->path_info(); my $theRemoteUser = $query->remote_user(); my $theTopic = $query->param( 'topic' ); my $theUrl = $query->url; my ( $topic, $webName, $scriptUrlPath, $userName ) = TWiki::initialize( $thePathInfo, $theRemoteUser, $theTopic, $theUrl, $query ); # Attachments my $attachments_list = ""; my $host = $TWiki::defaultUrlHost; my $pub = $TWiki::pubUrlPath; $host =~ s/^(.*)\/$/$1/; $pub =~ s/^\/(.*)$/$1/; my $attachurl = "$host\/$pub\/$webName\/$topic\/"; # Test topic existence if( &TWiki::Store::topicExists( $webName, $topic ) ) { # Obtain topic my ( $meta, $content ) = &TWiki::Store::readTopic( $webName, $topic ); # Test permissions my $wikiUserName = &TWiki::userToWikiName( $userName ); if ( &TWiki::Access::checkAccessPermission( "view", $wikiUserName, $content, $topic, $webName ) ) { # Save new attachment if necessary my $file = $query->param('file'); if ( $file ) { ( $meta, $content ) = &_saveAttachment( $query, $userName, $webName, $topic, $meta, $content ); } # Display existing attachments my @attachments = $meta->find( "FILEATTACHMENT" ); foreach my $attachment (@attachments) { my $name = $attachment->{"name"}; my $fileIcon = &TWiki::Attach::filenameToIcon( $name ); $attachments_list .= "