# FILE: fcn-edit.pl
# DESCRIPTION: Edit existing post
#-------------------------------------------------------------------------------
# DISCUS COPYRIGHT NOTICE
#
# Discus is copyright (c) 2002 by DiscusWare, LLC, all rights reserved.
# The use of Discus is governed by the Discus License Agreement which is
# available from the Discus WWW site at:
#    http://www.discusware.com/discus/license
#
# Pursuant to the Discus License Agreement, this copyright notice may not be
# removed or altered in any way.
#-------------------------------------------------------------------------------

use strict;
use vars qw($GLOBAL_OPTIONS $DCONF $PARAMS);

###
### edit_post_form
###
### Presents a form to permit the editing of individual posts
###

sub edit_post_form {
	my ($topic, $page, $post, $arg) = @_;
	my $subst = {};
	my $page_ref = defined $arg->{page_ref} ? $arg->{page_ref} : GetPage($topic, $page);
	my @message = grep($_->{number} == $post, @{ $page_ref->{messages} });
	dreq("webtags", "profane");
	error_message(read_language()->{EDITING_POST_ERROR}, read_language()->{EDITING_POST_NO_EXIST}, 0, 1) if ref $message[0] ne "HASH";
	$subst->{general}->{topic} = $topic;
	$subst->{general}->{page} = $page;
	$subst->{general}->{post} = $post;
	$subst->{arg} = $arg;
	$subst->{general}->{skip_headers} = 1;
	$subst->{general}->{skip_nav_bar} = 1;
	$subst->{general}->{skip_bottom} = 1;
	$subst->{general}->{action} = defined $arg->{action} ? $arg->{action} : $PARAMS->{cgiurl};
	$subst->{general}->{formaction} = defined $arg->{formaction} ? $arg->{formaction} : "editpost";
	$subst->{head}->{param} = "Messages";
	$subst->{head}->{topic_number} = $topic;
	$subst->{messages} = \@message;
	my @prop = map { name => _genkey($_), value => $message[0]->{$_} }, grep(/^property_/, keys(%{ $message[0] }));
	$subst->{props} = \@prop;
	if ($arg->{text}) {
		$subst->{general}->{source} = $arg->{text};
		my $message = profanity_filter($arg->{text}, {subst => $subst, alternate_handler => \&edit_profanity_error, error_report => 1});
		my ($error, $text) = webtags($message, 1, 1, $arg->{is_moderator}, $arg->{is_superuser}, $topic);
		$subst->{messages}->[0]->{text} = $text;
		$subst->{general}->{error} = 1 if $error eq "!Error";
	} else {
		$subst->{general}->{source} = inverse_webtags($message[0]->{text}, $topic, undef);
	}
	return $subst if ($arg->{saver} && $arg->{makesubst} && ! $subst->{general}->{error});
	if ($DCONF->{pro}) {
		dreq("authwrap-PRO");
		$subst->{messages} = auth_wrap_attachments($subst->{messages}, $topic);
	}
	return $subst if ($arg->{makesubst} && ! $subst->{general}->{error});
	screen_out("editpost", $subst, $arg->{cookie_str});
}

sub _genkey {
	my ($in) = @_;
	return $' if $in =~ /^property_/;
	return $in;
}

###
### edit_profanity_error
###
### Alternate handler for profanity errors occurring when editing posts
###

sub edit_profanity_error {
	my ($message, $argument, $words) = @_;
	my $subst = $argument->{subst};
	my $error_message = read_language()->{PROFANITY_DETECTED_MESSAGE};
	my $l = join("<li>", sort keys(%{ $words })); $l = "<li>$l";
	$error_message =~ s/\%wordlist/$l/g;
	$subst->{messages}->[0]->{text} = $error_message;
	$subst->{general}->{error} = 1;
	screen_out("editpost", $subst, undef);
}

###
### save_edited_post
###
### Saves the text of an edited post and updates logs appropriately
###

sub save_edited_post {
	my ($topic, $page, $post, $text, $arg) = @_;
	my $a = $arg;
	$a->{makesubst} = 1;
	$a->{saver} = 1;
	$a->{text} = $text;
	if (($GLOBAL_OPTIONS->{admin_edit_note} && $arg->{is_superuser}) || (! $arg->{is_superuser} && $GLOBAL_OPTIONS->{mod_edit_note} && $arg->{is_moderator}) || ($GLOBAL_OPTIONS->{usr_edit_note} && ! $arg->{is_moderator})) {
		my $l = read_language()->{EDITING_POST_NOTE};
		$l =~ s/\%username/$arg->{username}/g;
		my $x_str = defined read_language()->{EDITING_POST_DATE_FORMAT} ? read_language()->{EDITING_POST_DATE_FORMAT} : "dateonly";
		$l =~ s/\%date/get_date_time($x_str)/ge;
		$a->{text} .= "\n\n$l";
	}
	my @old = ();
	my @new = ();
	my $subst = edit_post_form($topic, $page, $post, $a);
	my $page_ref = GetPage($topic, $page, { lock => 1 });
	my $text_save = "";
	foreach my $i (@{ $page_ref->{messages} }) {
		if ($i->{number} == $post) {
			$text_save = $i->{text};
			$i->{text} = $subst->{messages}->[0]->{text};
			last;
		}
	}
	my $new = attachment_scan($subst->{messages}->[0]->{text});
	my $old = attachment_scan($text_save);
	my %OA = map { $_->{filename}, 1 } @{$old};
	my %NA = map { $_->{filename}, 1 } @{$new};
	my @ag = (); my @nw = ();
	foreach my $knew (keys %NA) {
		next if $OA{$knew};
		push @nw, $knew;
	}
	foreach my $kold (keys %OA) {
		next if $NA{$kold};
		push @ag, $kold;
	}
	if (scalar(@nw) > 0) {
		unlock("$DCONF->{message_dir}/$topic/$page.$DCONF->{ext}");
		$subst->{general}->{attachadd} = 1;
		$subst->{general}->{error} = 1;
		my @li = ();
		foreach my $x (@nw) {
			push @li, { file => $x };
		}
		$subst->{list} = \@li;
		screen_out("editpost", $subst, undef);
	} elsif ($subst->{messages}->[0]->{text} =~ m|<img src="(.*?)"|is && $1 !~ m|^https?://.*$|i) {
		unlock("$DCONF->{message_dir}/$topic/$page.$DCONF->{ext}");
		$subst->{general}->{error} = 1;
		screen_out("editpost", $subst, undef);
	}
	$page_ref->{general}->{messages_raw} = 0;
	SetPage($page_ref, { unlock => 1 });
	if (scalar(@ag)) {
		my $dir = get_message_path($topic);
		unlink map { join("/", $dir, $_) } @ag;
	}
	dreq("fcn-logs");
	my $preview_storage = defined $DCONF->{log_preview_length} ? $DCONF->{log_preview_length} : 25;
	my $firstchars_1 = lc(escape(substr(remove_html($subst->{messages}->[0]->{text}), 0, $preview_storage)));
	my $firstchars_2 = lc(escape(substr(remove_html($text_save), 0, $preview_storage)));
	my $search_stop_1 = search_stop($subst->{messages}->[0]->{text});
	my $search_stop_2 = search_stop($text_save);
	if ($firstchars_1 ne $firstchars_2) {
		my $firstchars_NEW = escape(substr(remove_html($subst->{messages}->[0]->{text}), 0, $preview_storage));
		log_search_update_entry($topic, "log", [ { postindex => $post, firstchars => $firstchars_NEW } ]);
	}
	if ($search_stop_1 ne $search_stop_2) {
		log_search_update_entry($topic, "search", [ { postindex => $post, text => $search_stop_1 } ]);
	}
}

1;
