#
#C-BOARD Moyuku R}h
#  last modified 2011/05/03
#
#e֌W̊֐܂܂Ă܂B
#
#

package POST;


use File::Copy qw(move);
require './app/lib/emoji.pl'; require './app/lib/etc.pl';



#
#   eʕ\
#
sub input {
	do "$::FLAG{'template'}/psn.skin.pl";

	# ԐM̏ꍇ
	my($subject, $reference); my $body;
	if ($::FORM{'follow'}) {

		require './app/lib/file/msg.pl'; require './app/lib/htm.pl';
		require './app/cmd/message.pl';

		my $o_message = new FILE::MESSAGE($CNF::place{'data'}{'path'});
		(my $r_MSG = $o_message->one($::FORM{'follow'}))
			or &::error(\'ԐM̋Lǂݍ߂܂ł');
		local *MSG = $r_MSG;
		$o_message->close();

		# X
		if ($CNF::post{'resnumres'}{'mode'}) {
			if ($CNF::post{'resnumres'}{'max'} == 0)
			{
				# XS֎~
				&::error(\'ǗlɂԐM֎~Ă܂');
			}
			else
			{
				# X֎~ĩ݂X`FbN|t@CANZXj
				require './app/lib/file/tre.pl';
				my $o_tree = new FILE::TREE($CNF::place{'data'}{'path'});
				my $r_topic;
				$r_topic = $o_tree->one($MSG{'tree'});
				my $r_MSG_tpc = ${$r_topic}[0];
				${$r_MSG_tpc}{'reply'} = $#{$r_topic};
				local *MSG_tpc = $r_MSG_tpc;
				$o_tree->close;
				if ($MSG_tpc{'reply'} >= $CNF::post{'resnumres'}{'max'}) {
					&::error(\"ԐMLǗlݒ肵i$CNF::post{'resnumres'}{'max'}jzĂ܂");
				}
			}
		}
		if ($CNF::post{'quot'}{'number'}{'mode'}) {
			if ($MSG{'subject'} =~ /^Re\((\d+)\):(.*)$/) {
				$subject = 'Re(' . ($1 + 1) . "):$2";
			}
			elsif ($MSG{'subject'} =~ /^Re:(.*)$/) {
				$subject = "Re(1):$1";
			}
			else { $subject = "Re(1):$MSG{'subject'}";}
		}
		else {
			$subject = ($MSG{'subject'} =~ /^Re(?:\(\d+\))?:(.*)$/)
					 ? "Re:$1" : "Re:$MSG{'subject'}";
		}
		#Moyuku
		#$::FLAG{'mobile'}{'hdml'} && ($subject =~ s/\$/&dol;/g);

		# p̏ꍇ
		if (!$::FORM{'no_quot'}) {
			$body = $MSG{'body'}; chomp($body);
			$CNF::post{'emoji'}{'mode'} && &EMOJI::cancel(\$body);
			&HTML::escape(&ETC::option_unlink(&HTML::auto_unlink(\$body)));
			$body =~ s/^/$CNF::post{'quot'}{'icon'}/gmo;
			$CNF::post{'quot'}{'name'}{'mode'}
				&& ($body
					= join($MSG{'name'},	
					   @{$CNF::post{'quot'}{'name'}{'icon'}})
						. "\n" . $body);
		}
		
		# Qƕ
		$MSG{'typewriter'} && ($MSG{'body'} = "<tt>$MSG{'body'}</tt>");
		my $reference = $MSG{'body'};
		$r_reference
			= &DESIGN::reference(&MESSAGE::quot_color(\$reference));
	}


	# e͗̐ݒ
	my $upload_input;
	if ( $CNF::upload{'mode'} ) {
		 map($upload_input .= ${&DESIGN::upload_input}, (0 .. ($CNF::upload{'enable'}{'number'} - 1)));
	}
	
	$CNF::mail{'inform'}{'mode'}
		&& (my $r_inform_mail_input = &DESIGN::inform_mail_input);
	$CNF::mail{'owner'}{'mode'}
		&& (my $r_owner_mail_input = &DESIGN::owner_mail_input);
	$CNF::view{'genre'}{'mode'}
		&& (my $r_genre_input = &DESIGN::genre_input);
	$CNF::view{'tree'}{'sort'}{'mode'}
		&& $CNF::post{'sage'}{'mode'}
		&& (my $r_sage_input = &DESIGN::sage_input);
	$CNF::view{'body_color'}{'mode'}
		&& (my $r_body_color_input = &DESIGN::body_color_input);
	
	##Xp΍n
	#^CL[
	my $r_timekey = '';
	if( $::FORM{'cmd'} ne "mdt" ){
		$r_timekey = &make_timekey($CNF::antispam{'timekey'}{'idseed'});
	}
	#摜F
	my $r_numcrypted = '';
	if( $CNF::antispam{'imagekey'}{'use'}==1 && $::FORM{'cmd'} ne "mdt" ){
		$r_numcrypted = &make_imagekey_number( $CNF::antispam{'timekey'}{'idseed'} );
	}
	

	# \
	&BASE::head('psn');
	&DESIGN::post($r_genre_input, \$subject, \$body, \$upload_input,
		$r_sage_input, $r_inform_mail_input, $r_owner_mail_input, $r_reference,
		$r_body_color_input, $r_timekey, $r_numcrypted );
	&BASE::foot('psn');
}


#
#   摜F؃L[쐬
#
sub make_imagekey_number {
	my ( $idSeed ) = @_;

    $num = int( rand(9998) ) + 1;

	require './app/extlib/RC4.pm';

	my $rc4 = Crypt::RC4->new( $idSeed );
	my $crypted = $rc4->RC4( $num );
	
	$crypted =~ s/(.)/unpack('H2', $1)/eg;
	
	return $crypted;
}


#
#   摜F؃L[`FbN
#
sub check_imagekey {
	my ( $imagekey, $num_crypted, $idSeed ) = @_;

	my @error;

	if ( length($imagekey) <= 0 ) {
		push(@error, "摜F؃L[͂Ă܂B");
		return(\@error);
	}

	require './app/extlib/RC4.pm';
	my $rc4 = Crypt::RC4->new( $idSeed );

	$num_crypted =~ s/([0-9A-Fa-f]{2})/pack('H2', $1)/eg;
	my $number = $rc4->RC4( $num_crypted );

	if ( $imagekey != $number ) {
		push(@error, "摜F؃L[v܂B");
	}

	#push(@error, "test " . $number . ' -> ' . $imagekey);

	return(\@error);
}


#
#   e
#   XpeȂǂ̊֌WŁAȂpɂɌĂ΂͂B
#
sub write {

	#
	require './app/lib/str.pl';


	# e\ǂ`FbN
	$CNF::post{'stop'}{'mode'} && &::error(\$CNF::post{'stop'}{'caption'});

	# bNJn
	&::lock('msg') or &::error(\'GĂď߂܂łB');

	# O
	my $r_error = &parameter;
	$r_error && &error($r_error);


	# e`FbN
	my $o_identify;
	($r_error, $o_identify) = &check;
	$r_error && &error($r_error, $o_identify);


	# vr[Ȃ\ďI
	if ($::FORM{'preview'}) { 
		#Ytt@C폜
		if ($::FLAG{'file'}) {
			foreach (@{$::FLAG{'file'}}) {
				unlink("$::SYS{'path_tmp'}/${$_}{'name'}");
			}
		}
		#bN
		&::unlock('msg');

		#\
		&preview; 
		return;
	}

	#G̏
	if ( $::FLAG{'mobile'}{'mode'} || $::FLAG{'pda'} ) {
		
		if ( $CNF::post{'emoji'}{'mode'} == 0 ) {
			&EMOJI::to_string(\$::FORM{'body'})
		} elsif ( $CNF::post{'emoji'}{'mode'} == 1 ) {
			&EMOJI::to_code(\$::FORM{'body'})		
		} else {
			
		}
		#$CNF::post{'emoji'}{'mode'}
		#	? &EMOJI::to_code(\$::FORM{'body'})
		#	: &EMOJI::to_string(\$::FORM{'body'});
	}




	# Ytt@C̏񏈗
	delete $::FORM{'file'};

	#̂炷iɃG[Ή̂߁j
	%FLAG2 = ();
	foreach (@{$::FLAG{'file'}}) {
		my %buf = %$_;
		push(@{$FLAG2{'file'}}, \%buf);
	}


	if ($::FLAG{'file'} && defined( $::FLAG{'file'}[-1] )) {

		$::FORM{'file'} = $FLAG2{'file'};
		foreach (@{$::FORM{'file'}}) {

			#(${$_}{'name'} =~ /\.(?:bmp|gif|jpe?g|png)$/i) or next;
			if (${$_}{'name'} =~ /\.(?:bmp|gif|jpe?g|png)$/i) {
				my $r_screen
					= &COMMON::get_image_size("$::SYS{'path_tmp'}/" . ${$_}{'name'});
				if (!$r_screen) {
					push(@error, '摜̃TCY̎擾Ɏs܂B'); last;
				}
				${$_}{'screen'} = join('*', @{$r_screen});
			}

			#t@CLԍ
			${$_}{'name'}  = $::FORM{'no'} . '_' . ${$_}{'name'};

		}
	}
			
#			print "Content-Type: text/html; charset=Shift_JIS\n\n";
#			#print "\@FLAG2";
#			print "FORM";
#			foreach (@{$::FORM{'file'}}) {
#				print ${$_}{'screen'}. "<br>\n";
#				print 	${$_}{'name'} . "<br>\n";
#			}
#			print "<br>";
#			
#			print "FLAG";
#			foreach (@{$::FLAG{'file'}}) {
#				print ${$_}{'screen'}. "<br>\n";
#				print 	${$_}{'name'} . "<br>\n";
#			}
#			print "<br>";
#			print "FLAG2";
#			foreach (@{$FLAG2{'file'}}) {
#				print ${$_}{'screen'}. "<br>\n";
#				print 	${$_}{'name'} . "<br>\n";
#			}
#			
#					
#			exit;

	# 
	my @total = &write_total; my @tree = &write_tree; 
	my @message = &write_message;
	my @file = $::FORM{'file'} ? &write_file : (0, 1);
	$SIG{'INT'} = $SIG{'HUP'} = $SIG{'QUIT'} = $SIG{'TERM'} = 'IGNORE';
	if ($total[1] && $tree[1] && $message[1] && $file[1]) {
		$total[0]->complete(); $tree[0]->complete();
		$message[0]->complete(); $file[0] && $file[0]->complete();
	}
	else {
		$total[0]->incomplete(); $tree[0]->incomplete();
		$message[0]->incomplete(); $file[0] && $file[0]->incomplete();
		&::error(\'L݂̏Ɏs܂B');
	}
	$o_identify && $o_identify->complete;


	#ۂ̃t@Cړ͏݌
	if ($::FLAG{'file'} && defined( $::FLAG{'file'}[-1] )) {
		foreach (@{$::FLAG{'file'}}) {
			#t@Cړ
			move( "$::SYS{'path_tmp'}/" . ${$_}{'name'}, "$CNF::place{'file'}{'path'}/" . $::FORM{'no'} . '_' . ${$_}{'name'} ) 
				or &::error(\'t@Cړł܂łB');
		}
	}

	#
	$CNF::rank{'frequency'}{'mode'} && &write_frequency;

	## L[[hL^
	#$CNF::post{'keyword'}{'mode'} && &keyword;

	# [M
	if ($CNF::mail{'mode'} != 2) {

		my %FRM2 = %::FORM; #QƂŔj󂳂̂Łcc
		$FRM2{'operator'} = -1;
		if ( $CNF::mail{'master'}{'mode'} ) {
			require './app/lib/mailer.pl';
			&MAILER::master_mail(\%FRM2);
		}

		%FRM2 = %::FORM;
		if ( $CNF::mail{'inform'}{'mode'} && $FRM2{'follow'} ) {
			require './app/lib/mailer.pl';
			&MAILER::response_mail(\%FRM2);
		}

		##
		#if ( $CNF::mail{'owner'}{'mode'} ) {
		#	require './app/lib/mailer.pl';	
		#	&MAILER::owner_mail
		#}

	}

	# LI[o[Âc[ߋO or 폜
	my $ret = 0;
	if ($total[1] > $CNF::post{'total'}) {
		require './app/cmd/delete.pl';
		$ret = &DELETE::last_tree(3);
		if ( $ret == 0 ) { &::error(\'ߋL̏Ɏs܂B'); }
	}

	# Ytt@C̗eʂI[o[班߂ɌÂYtt@C폜
	if ($file[0] && ($file[1] > $CNF::upload{'size'}{'total'})) {		
		require './app/cmd/delete.pl';
		$ret = &DELETE::last_file(($file[1] - $CNF::upload{'size'}{'total'}) * 2);
		if ( $ret == 0 ) { &::error(\'߃t@C̏Ɏs܂B'); }
	}

	# bN
	&::unlock('msg');

	# NbL[
	my @value = qw(email key mail_mode name inform_mail owner_mail web sage);
	$CNF::view{'genre'}{'cookie'}{'mode'} && push(@value, 'genre');
	$CNF::view{'body_color'}{'cookie'}{'mode'} && push(@value, 'body_color');
	my %value;
	map($value{$_} = $::FORM{$_}, (@value, grep(/^opt_/, keys %::FORM)));


	# Wv
	my $plfm = "";
	&BASE::location("$CNF::place{'this'}{'url'}?cmd=one;no=$::FORM{'no'};"
				  . "id=$::FORM{'id'}",
					{name => "$CNF::cookie{'name'}", value => \%value,
					 expire => $CNF::cookie{'expire'}});

}


#
#   p[^[Zbg
#
sub parameter {
	require './app/lib/file/msg.pl'; require './app/lib/htm.pl'; 
	my @error;

	# Lԍݒ
	my $o_message = new FILE::MESSAGE($CNF::place{'data'}{'path'});
	if (my $r_MSG = $o_message->read(0, 1)) {
		$o_message->close; $::FORM{'no'} = ${$r_MSG}{'no'} + 1;
	}
	elsif ($o_message->init()) {
		push(@error, 'Lԍ̎擾Ɏs܂B');
	}
	else { $::FORM{'no'} = 1;}

	# c[ԍݒ
	if ($::FORM{'follow'}) {
		$o_message = new FILE::MESSAGE($CNF::place{'data'}{'path'});
		(my $r_MSG = $o_message->one($::FORM{'follow'}))
			or push(@error, 'ԐM̋L܂łB');
		$::FORM{'tree'} = ${$r_MSG}{'tree'}; $o_message->close;
	}
	else { $::FORM{'tree'} = $::FORM{'no'};}

	# zXgݒ
	$::FORM{'host'} = &CGI::get_host(0);
	$::FORM{'ext_host'} = &CGI::get_host(1);
	$::FORM{'addr'} = $ENV{'REMOTE_ADDR'};

	# tݒ
	$::FORM{'date'} = &COMMON::date($CNF::dformat{'post'});

	# hcݒ
	$::FORM{'user_id'} = $::COOKIE{'user_id'};

	# ʔԍî܂܂ł͒̂ōj
	$::FORM{'ident'}
		= substr(&COMMON::sha1(\"$::FORM{'name'}\t$::FORM{'key'}"), 0, 10);

	# NCAgݒ
	$::FORM{'agent'} = $ENV{'HTTP_USER_AGENT'} || 'unknown';
	&COMMON::jsubstr(&HTML::escape(\$::FORM{'agent'}), 120);

	# Ԃݒ
	$::FORM{'time'} = $::FLAG{'time'};

	# gтǂݒ
	$::FLAG{'mobile'}{'mode'} && ($::FORM{'mobile'} = 1);

#	# Ytt@C̏ݒ
#	delete $::FORM{'file'};
#	if ($::FLAG{'file'}) {
#		(!$CNF::upload{'mode'} || !$::FLAG{'master'}
#			&& $CNF::upload{'mode'} != 2)
#			&& &::error(\'t@C̃Abv[h͋֎~Ă܂B');
#
#		$::FORM{'file'} = $::FLAG{'file'};
#		foreach (@{$::FORM{'file'}}) {
#			(${$_}{'name'} =~ /\.(?:bmp|gif|jpe?g|png)$/i) or next;
#			my $r_screen
#				= &COMMON::get_image_size("$CNF::place{'file'}{'path'}/" . ${$_}{'name'});
#			if (!$r_screen) {
#				push(@error, '摜̃TCY̎擾Ɏs܂B'); last;
#			}
#			${$_}{'screen'} = join('*', @{$r_screen});
#		}
#	}

	return(@error ? \@error : 0);
}


#
#   낢`FbN
#
sub check {
	my @error;

	# u[r[gbv
	if ( $::FORM{'comment'} && length($::FORM{'comment'}) > 0 ) {
		return(['sȓeƔ肳܂B']);
	}

	# ANZX`FbN
	if ($CNF::permission{'pst'}{'mode'}) {
		require './app/lib/prm.pl'; &PERMISSION::check('pst');
	}

	# e`FbN
	$CNF::post{'referer_check'}{'mode'} && !$::FLAG{'mobile'}{'mode'}
		&& !$::FLAG{'incm'}
		&& ($ENV{'HTTP_REFERER'} !~ /^\Q$CNF::place{'this'}{'url'}\E/)
		&& push(@error, 'esłB');
		
	# vLVoR`FbN
	($CNF::post{'no_proxy'}{'mode'}
		&& !&CGI::check_proxy(&CGI::get_host(1)))
		&& push(@error,
				'vLVT[o[oRĂ݂̏'
			  . '֎~Ă܂B');

	# ܂łŃG[ꍇ͏o
	@error && return(\@error);


	# eԃ`FbNiǗ҈ȊOj
	if ( $::FLAG{'master'} != 1 ) {
		@error = 
			@{ &check_timekey(
					$::FORM{'timekey'},
					$CNF::antispam{'timekey'}{'idseed'},
					$CNF::antispam{'timekey'}{'secmax'},
					$CNF::antispam{'timekey'}{'secmin'})
		};
		@error && return(\@error);	#OwU
	}


	# O`FbN
	my $name = $::FORM{'name'};
	my $master = $CNF::master{'name'};
	(!$::FLAG{'master'} && (&ETC::name_matching($name,$master,$CNF::master{'mode'})))
		&& return(['ǗlƓO͎g܂B']);

	# VKXbhă`FbN
	if (!$::FORM{'follow'}) {
		(!$::FLAG{'master'} && $CNF::permission{'pst'}{'master'}{'modenew'})
		&& return(['ǗlȊO͐VKXbh𓊍eł܂']);
	}

	# t@CYt`FbN
	if ( $CNF::upload{'mode'}==0 || ( !$::FLAG{'master'} && $CNF::upload{'mode'} != 2) )  {
		if ( $::FLAG{'file'} || $::FORM{'file'} ) {
			return (\'t@C̃Abv[h͋֎~Ă܂B');
		}
	}
	if ( $::FLAG{'file'} || $::FORM{'file'} ) {
		foreach (@{$::FLAG{'file'}}) {
			${$_}{'name'} =~ /(.*)\.(.*)$/;
			grep(  /$2/i, @{$CNF::upload{'enable'}{'ext'}} ) or return( ['̃^Cṽt@C̓Abv[h֎~Ă܂Bi'  . $2 . 'j']  ) ;
		}
	}


	# ܂łŃG[ꍇ͏o
	@error && return(\@error);


	# ef[^`FbN
	@error = @{&check_parameter}; 


	$::FLAG{'check'} = 1;

	# de`FbN
	my $o_identify;
	if (!$::FLAG{'master'} && !$::FORM{'preview'}
	 && ($::FORM{'cmd'} ne 'mdt')) {
		my $result; ($o_identify, $result) = &identify;
		($::FLAG{'debug'} == 2) && ($result = 2);
		if ($result == 0) {
			push(@error, 'e̍ēe͋֎~Ă܂B');
		}
		elsif ($result == 1) {
			push(@error,
				 "Ǘl($CNF::master{'name'})ȊO"
			   . "$CNF::post{'interval'}bȓ̍ēe͏o܂B");
		 }
	}
	
	return((@error ? \@error : 0), $o_identify);
}



#
#   ef[^`FbN
#     ̎ɂ(?)rihrł邱Ƃقǈӎ
#     Ă܂
#
	sub check_parameter {
		require './app/lib/htm.pl';
		my @error;

		my %item = qw(
		name O email [AhX subject 薼 body { key pX[h
		web z[y[WAhX
		);

		my $os;
		if ($CNF::post{'no_dependence'}{'mode'}) {
			if ($ENV{'HTTP_USER_AGENT'} =~ /Win/) { $os = 0;}
			elsif ($ENV{'HTTP_USER_AGENT'} =~ /Mac(?:intosh|_PowerPC)/) {
				$os = 1;
			}
		}
		
		# Xp΍
		if ( $::FLAG{'master'} != 1 ) {
			
			# 摜Fؔԍ
			if ( $CNF::antispam{'imagekey'}{'use'} == 1 ) {
				
				@error = 
					@{ &check_imagekey( $::FORM{'imagekey'}, $::FORM{'num_crypted'},  $CNF::antispam{'timekey'}{'idseed'} )	};
				@error && return(\@error);
				
			}
			
			# ept[L[[h
			if ( ( $::FLAG{'mobile'}{'mode'} != 1 && $CNF::antispam{'postword'}{'use'} == 1 ) 
					|| $CNF::antispam{'postword'}{'use'} == 2  ) {
				if ( $::FORM{'postword'} ne $CNF::antispam{'postword'}{'key'} ) {
					push(@error, "eL[[hv܂B");
				}
			}
		
		}
		

		# ܂łŃG[ꍇ͏o
		@error && return(\@error);


		#Xpe΍tB^[
		require './app/lib/filter.pl';
		my $err_msg = &FILTER::anti_spampost
						( $CNF::antispam{'filter'}{'level'},$::FORM{'name'},$::FORM{'subject'},$::FORM{'body'},
							$::FORM{'email'},$::FORM{'web'},$ENV{'REMOTE_ADDR'} );
		if ( length($err_msg) > 0 ) { 
			push(@error, $err_msg);
			if ( $::FLAG{'debug'} == 1 ) {
				#g[X
				$::FLAG{'debug_out'} .= $err_msg . "\n";
			} 
			
			return(\@error);
		}

		
		my $item;
		
		#s`FbN
		foreach $item (keys %item) {
			
			# ֎~`FbN
			foreach (@{$CNF::post{'disable'}{'word'}{'list'}}) {
				length($_) or next;
				
				if (index($::FORM{$item}, $_) != -1) {	# xdi蔲j
					push(@error, "$item{$item}ɋ֎~傪܂܂Ă܂B");
					last;
				}
			}
			
			# `FbN
			if ($::FORM{$item} =~ /^(?:@| |\n)*$/s) {
				$CNF::post{$item}{'indispensable'}
					&& push(@error, "$item{$item}LĂB");
			}
			(length($::FORM{$item}) > $CNF::post{$item}{'max'})
				&& push(@error,
						("$item{$item}܂B$CNF::post{$item}{'max'}"
					   . "ȓɂĂ"));
		}
		
		# 󔒗⊮
		my %empty = (	'name'    => $CNF::post{'name'}{'complement'},
				'subject' => $CNF::post{'subject'}{'complement'},
				'body'    => $CNF::post{'body'}{'complement'},
		);
		foreach (keys %item) {
			length($::FORM{$_}) or ($::FORM{$_} = $empty{$_});
		}
		
		# ܂łŃG[ꍇ͏o
		@error && return(\@error);

		
		&STRING::undef_table; &EMOJI::undef_table; my $param;
		foreach $param (grep(/^opt_/, keys %::FORM)) {
			if (!grep($param eq $_,
			          @{$CNF::post{'option'}{'param'}{'list'}})) {
				push(@error, "IvVp[^[($param)"
				           . "o^Ă܂B");
				next;
			}
			$::FORM{$_} =~ s/\n//g;
			(length($::FORM{$_}) > 100)
				&& push(@error, 'IvVp[^[܂B');
			&HTML::escape(\$::FORM{$_});
		}

		# [AhX`FbN
		length($::FORM{'email'})
			&& ($::FORM{'email'} !~ /^[\w\.\-]+\@[\w\.\-]+\.[a-zA-Z]{2,6}$/)
			&& push(@error, '[AhXsłB');

		# z[y[WAhX`FbN
		length($::FORM{'web'})
			&& ($::FORM{'web'} !~ /^s?https?:\/\/[!-~]+\.[!-~]+$/
			|| ($CNF::post{'url'}{'check'}{'mode'}
			&& !&HTML::check_url($::FORM{'web'})))
			&& push(@error, 'z[y[WAhXsłB');

		# pX[h`FbN
		($::FORM{'key'} =~ /[<>&"]/)
			&& &::error(\('pX[hɁu&lt;, &gt;, &amp;, &quot;v'
						. 'g܂B'));

		# WACR
		if (($::FLAG{'mobile'}{'mode'} != 1) && ($::FLAG{'pda'} != 1))
		{
			exists $CNF::view{'genre'}{'list'}{$::FORM{'genre'}}
			or delete $::FORM{'genre'};
		$CNF::view{'genre'}{'mode'} && $CNF::view{'genre'}{'require'}{'mode'}
			&& !length($::FORM{'genre'})
			&& push(@error,	"u$CNF::view{'genre'}{'menu'}vIĂB");
		}
		# {FIigс^oc`̏ꍇ̓`FbNȂj
		if (($::FLAG{'mobile'}{'mode'} != 1) && ($::FLAG{'pda'} != 1))
		{
			$CNF::view{'body_color'}{'mode'} && $CNF::view{'body_color'}{'require'}{'mode'}
				&& !length($::FORM{'body_color'})
				&& push(@error,	"u{FvIĂB");
		}

		# sȃp[^폜
		($CNF::mail{'inform'}{'mode'} && $::FORM{'email'})
			or delete $::FORM{'inform_mail'};
		($CNF::mail{'owner'}{'mode'} && $::FORM{'email'})
			or delete $::FORM{'owner_mail'};
		$::FORM{'email'} or delete $::FORM{'mail_mode'};

		# ܂łŃG[ꍇ͏o
		@error && return(\@error);


		#ꉞیŒu
		$::FORM{'email'} =~ s/</ /g;
		$::FORM{'email'} =~ s/>/ /g;
		$::FORM{'web'} =~ s/</ /g;
		$::FORM{'web'} =~ s/>/ /g;


		#ee`FbN
		foreach $item (keys %item) {

			# @ˑ`FbN
			(defined $os) && &STRING::no_dependence(\$::FORM{$item}, $os);

			# eC
			if ($item eq 'body') {

				($::FORM{$item} =~ /[!-~]{150}/)
					&& push(@error, "p͓KxɉsĂB");

				$::FORM{$item} =~ s/^(?:(?:@| )*\n)+//g;
				$::FORM{$item} =~ s/(?:(?:@| )*\n)+$//;
				$::FORM{$item} =~ s/(?:(?:@| )*\n){4,}/$1\n\n\n/g;


				#!Moyuku! ŊG̑ΉKv͂ȂB
				## ȀOɌgт̊G#????#R[hɕϊ
				## [̃I[gN΍sjiseus̕ϊ
				#if ( $::FLAG{'mobile'}{'mode'} ) {
				#	$CNF::post{'emoji'}{'mode'}
				#		? &EMOJI::to_code(\$::FORM{$item})
				#		: &EMOJI::to_string(\$::FORM{$item});
				#}

				if ($::FLAG{'master'} && $CNF::tag{'mode'} || !$::FLAG{'master'} && $CNF::tag{'mode'} == 2) {
					&HTML::check(\$::FORM{$item}, $CNF::tag{'enable'},
								   $CNF::tag{'java'}{'mode'},
								   $CNF::tag{'style'}{'mode'},
								   $CNF::post{'url'}{'target'},
								   $CNF::post{'url'}{'check'}{'mode'});
				} else {
					##ł̓Oɏł܂B
					#&HTML::auto_link(&HTML::escape(\$::FORM{$item}), $CNF::post{'url'}{'target'}, $CNF::post{'url'}{'check'}{'mode'});
					&HTML::escape(\$::FORM{$item});
				}
				
				#Oɂ͏܂Ȃ
				#if ($sharp) {
				#	&ETC::option_link(\$::FORM{$item}, $::FORM{'no'});
				#	$CNF::post{'emoji'}{'mode'}
				#		? &EMOJI::image(\$::FORM{$item},
				#						$CNF::place{'this'}{'path'}
				#					  . "?cmd=mji;id=$::FORM{'id'};name=")
				#		: &EMOJI::string(\$::FORM{$item});
				#}
				
			
			} else {
				#{ȊO
				( $::FLAG{'mobile'}{'mode'} ) && &EMOJI::to_string(\$::FORM{$item});
				
				$::FORM{$item} =~ s/\n//g; 
				$::FORM{$item} =~ s/^(?:@| )+//;
				$::FORM{$item} =~ s/(?:@| )+$/$1/;

				&HTML::escape(\$::FORM{$item});
			}



		}


		return(\@error);
	}


#
#   Í^CL[쐬
#
sub make_timekey {
	my $idSeed = shift;
	require './app/extlib/RC4.pm';

	my $rc4 = Crypt::RC4->new( $idSeed );

	my $timekey = $rc4->RC4( time() );
	$timekey =~ s/(.)/unpack('H2', $1)/eg;
	
	return $timekey;
}

#
#   e^CL[`FbN
#
sub check_timekey {
	my $postTimekey = shift;
	my $idSeed = shift;	my $secMax = shift;	my $secMin = shift;
	my @error;

	if ( length($postTimekey) <= 0 ) {
		push(@error, "sȓeƔ肳܂B");
		return(\@error);
	}

	require './app/extlib/RC4.pm';
	my $rc4 = Crypt::RC4->new( $idSeed );

	$postTimekey =~ s/([0-9A-Fa-f]{2})/pack('H2', $1)/eg;

	$start_time = $rc4->RC4( $postTimekey );

	if ( (time() - $start_time) <= 2 ) {
		eval{ sleep(10) };	#yieB
		push(@error, "sȓełB");
	} elsif ( (time() - $start_time) < $secMin ) {
		push(@error, "eɂԂAeXpƔ肳܂B");
	} elsif (  (time() - $start_time) > $secMax )  {
		push(@error, "e܂ł̐Ԃ𒴉߂Ă܂܂Bʓr̍ēe肢܂B");
	}

	#push(@error, "test " . $postTimekey . ' -> ' . $start_time . '  now='. time() );

	return(\@error);
}




#
#   G[
#
sub error {
	my $r_error = shift; 
	my $o_identify = shift;
	
	do "$::FLAG{'template'}/psn.skin.pl"; 
	require './app/lib/htm.pl';

	# ~
	$o_identify && $o_identify->incomplete;

	#ꎞt@C폜
	if ($::FLAG{'file'}) {
		foreach (@{$::FLAG{'file'}}) {
			unlink("$::SYS{'path_tmp'}/${$_}{'name'}");
		}
		undef $::FLAG{'file'};
	}

	# bN
	&::unlock('msg');

	# gт̏ꍇ
	$::FLAG{'mobile'}{'mode'} && &::error(\join("<br>\n", @{$r_error}));


	# p[^[Zbg
	$::FORM{'cmd'} = 'psn';
	
	#if ($::FLAG{'check'}) {
	#	$CNF::post{'emoji'}{'mode'} && &EMOJI::cancel(\$::FORM{'body'});
	#	&ETC::option_unlink(&HTML::auto_unlink(\$::FORM{'body'}));
	#}

	map(&HTML::escape(\$::FORM{$_}), keys %::FORM);
	foreach (qw(email key mail_mode name inform_mail owner_mail web genre sage body_color postword)) {
		$::COOKIE{$_} = $::FORM{$_};
	}
	delete $::FLAG{'check'};

	# ԐM̏ꍇ
	if ($::FORM{'follow'}) {
		require './app/lib/file/msg.pl'; 
		require './app/cmd/message.pl';	
		my $o_message = new FILE::MESSAGE($CNF::place{'data'}{'path'});
		(my $r_MSG = $o_message->one($::FORM{'follow'}))
			or &::error(\join("<br>\n&nbsp;",
			                  (@{$r_error},
			                   'ԐM̋Lǂݍ߂܂ł')));
		local *MSG = $r_MSG;

		# Qƕ
		&MESSAGE::quot_color(\$MSG{'body'}); $MSG{'body'} =~ s/^/@ /gm;
		$r_reference = &DESIGN::reference(\$MSG{'body'});
	}

	# e͗̐ݒ
	my $upload_input;
	$CNF::upload{'mode'}
		&& map($upload_input .= ${&DESIGN::upload_input},
			   (0 .. ($CNF::upload{'enable'}{'number'} - 1)));
	$CNF::mail{'inform'}{'mode'}
		&& (my $r_inform_mail_input = &DESIGN::inform_mail_input);
	$CNF::mail{'owner'}{'mode'}
		&& (my $r_owner_mail_input = &DESIGN::owner_mail_input);
	$CNF::view{'genre'}{'mode'}
		&& (my $r_genre_input = &DESIGN::genre_input);
	$CNF::view{'tree'}{'sort'}{'mode'}
		&& $CNF::post{'sage'}{'mode'}
		&& (my $r_sage_input = &DESIGN::sage_input);
	$CNF::view{'body_color'}{'mode'}
		&& (my $r_body_color_input = &DESIGN::body_color_input);
		
	##Xp΍n	
	#^CL[
	my $r_timekey = $::FORM{'timekey'};
	#摜F
	my $r_numcrypted = '';
	if( $CNF::antispam{'imagekey'}{'use'}==1 && $::FORM{'cmd'} ne "mdt" ){
		$r_numcrypted = &make_imagekey_number( $CNF::antispam{'timekey'}{'idseed'} );
	}


	#ʕ\
	&BASE::head('pst'); map(&::error(\$_), @{$r_error});
	print ${&DESIGN::line};
	&DESIGN::post($r_genre_input, \$::FORM{'subject'}, \$::FORM{'body'},
		\$upload_input, $r_sage_input, $r_inform_mail_input,
		$r_owner_mail_input, $r_reference,
		$r_body_color_input, $r_timekey, $r_numcrypted );
	&BASE::foot('pst'); exit;

}


#
#   vr[
#
	sub preview {
		require './app/cmd/message.pl';
		require './app/lib/htm.pl';
		
		do "$::FLAG{'template'}/psn.skin.pl"; 
		do "$::FLAG{'template'}/msg.skin.pl";


		# p[^[Zbg
		$::FORM{'cmd'} = 'psn';
		foreach (qw(email key mail_mode name inform_mail owner_mail web genre sage body_color postword imagekey)) {
			$::COOKIE{$_} = $::FORM{$_};
		}
		my $body = $::FORM{'body'};
		$::FORM{'file'}
			&& map($_{'size'} = &COMMON::size($_{'size'}), @{$::FORM{'file'}});

		# vr[\
		&BASE::head('pst'); $::FORM{'cmd'}="one"; &MESSAGE::all(\%::FORM);

		# p[^[C
		$::FORM{'body'} = $body;
		$CNF::post{'emoji'}{'mode'} && &EMOJI::cancel(\$::FORM{'body'});
		&ETC::option_unlink(&HTML::auto_unlink(\$::FORM{'body'}));
		map(&HTML::escape(\$::FORM{$_}), keys %::FORM);

		# ԐM̏ꍇ
		if ($::FORM{'follow'}) {
			require './app/lib/file/msg.pl';	
			my $o_message = new FILE::MESSAGE($CNF::place{'data'}{'path'});
			(my $r_MSG = $o_message->one($::FORM{'follow'}))
				or &::error(\'ԐM̋Lǂݍ߂܂ł');
			local *MSG = $r_MSG;

			# Qƕ
			&MESSAGE::quot_color(\$MSG{'body'}); $MSG{'body'} =~ s/^/@ /gm;
			$r_reference = &DESIGN::reference(\$MSG{'body'});
		}

		# e͗̐ݒ
		my $upload_input;
		$CNF::upload{'mode'}
			&& map($upload_input .= ${&DESIGN::upload_input},
				   (0 .. ($CNF::upload{'enable'}{'number'} - 1)));
		$CNF::mail{'inform'}{'mode'}
			&& (my $r_inform_mail_input = &DESIGN::inform_mail_input);
		$CNF::mail{'owner'}{'mode'}
			&& (my $r_owner_mail_input = &DESIGN::owner_mail_input);
		$CNF::view{'genre'}{'mode'}
			&& (my $r_genre_input = &DESIGN::genre_input);
		$CNF::view{'tree'}{'sort'}{'mode'}
			&& $CNF::post{'sage'}{'mode'}
			&& (my $r_sage_input = &DESIGN::sage_input);
		$CNF::view{'body_color'}{'mode'}
			&& (my $r_body_color_input = &DESIGN::body_color_input);
			
		##Xp΍n
		my $r_timekey = $::FORM{'timekey'};
		$r_numcrypted = $::FORM{'num_crypted'};		

		# ͗\
		&DESIGN::post($r_genre_input, \$::FORM{'subject'}, \$::FORM{'body'},
			\$upload_input, $r_sage_input, $r_inform_mail_input,
			$r_owner_mail_input, $r_reference,
			$r_body_color_input, $r_timekey, $r_numcrypted );


		&BASE::foot('pst');
	}


#
#   Gpbh
#
	sub emoji_pad {
		do "$::SYS{'path_skn'}/platforms/pc/emj.skin.pl"; 
		require './app/lib/htm.pl';
		
		&BASE::simple_header;
		
		&DESIGN::view;

	}


#
#   I[g[_[
#
sub AUTOLOAD {
	my $name = ($AUTOLOAD =~ /^POST::(.+)$/)[0];
	push(@{$::FLAG{'eval'}}, $AUTOLOAD);
	if (!defined $SUB{$name}) {
		&::error(\"`ĂȂ֐($AUTOLOAD)Ă΂܂B"); exit;
	}
	eval $SUB{$name}; length($@) && &::error(\"EVAL ERROR: $@ ($AUTOLOAD)");
	delete $SUB{$name}; goto &{'POST::' . $name};
}


%SUB = (




	identify => <<'__SUB__',
#
#   deh~
#
	sub identify {
		require './app/lib/file/psi.pl';
		my $digest = &COMMON::sha1(\"$::FORM{'subject'}\t$::FORM{'body'}");
		my $o_identify = new FILE::POST_IDENTIFY($CNF::place{'data'}{'path'});
		return($o_identify,
			   $o_identify->update({digest => $digest, host => $::FORM{'host'},
									time => $::FLAG{'time'}},
								   $CNF::post{'identify'}{'total'},
								   $CNF::post{'interval'}));
	}
__SUB__


	write_total => <<'__SUB__',
#
#   c[LL^
#
	sub write_total {
		require './app/lib/file/ttl.pl';
		my $o_total = new FILE::TOTAL($CNF::place{'data'}{'path'});
		return($o_total, $o_total->add($::FORM{'tree'} == $::FORM{'no'}));
	}
__SUB__

	write_tree => <<'__SUB__',
#
#   c[L^
#
	sub write_tree {
		require './app/lib/file/tre.pl'; my($item, %value);
		my @param = qw(date follow genre name no subject time);
		foreach $item (keys %::FORM) {
			($item =~ /^opt_\w+$/ || grep(($_ eq $item), @param))
				&& ($value{$item} = $::FORM{$item});
		}
		$value{'file'} = $::FORM{'file'} ? @{$::FORM{'file'}} : 0;
		my $o_tree = new FILE::TREE($CNF::place{'data'}{'path'});
#sage add
		if ($::FORM{'sage'}) {
			return($o_tree, $o_tree->write(\%value, $::FORM{'tree'},0));
		 } else {
			return($o_tree, $o_tree->write(\%value, $::FORM{'tree'},
					   $CNF::view{'tree'}{'sort'}{'mode'}));
		}
#		return($o_tree, $o_tree->write(\%value, $::FORM{'tree'},
#					   $CNF::view{'tree'}{'sort'}{'mode'}));
	}
__SUB__

	write_message => <<'__SUB__',
#
#   LL^
#
	sub write_message {
		my @param = qw(
		agent body date email follow genre host host_ext ident inform_mail
		owner_mail mail_mode mobile name no sage subject time tree typewriter
		user_id web body_color
		);
		my($item, %value);
		foreach $item (keys %::FORM) {
			($item =~ /^opt_\w+$/ || grep(($_ eq $item), @param))
				&& ($value{$item} = $::FORM{$item});
		}
		if ($::FORM{'file'}) {
			my(@file, $file);
			foreach $file (@{$::FORM{'file'}}) {
				my %file;
				foreach (keys %{$file}) { $file{$_} = ${$file}{$_};}
				$file{'size'} = &COMMON::size($file{'size'});
				push(@file, \%file);
			}
			$value{'file'} = \@file;
		}
		$value{'key'} = length($::FORM{'key'})
					  ? &COMMON::make_pass($::FORM{'key'}) : '';
		$::FORM{'key_val'} = $value{'key'};
		my $o_message = new FILE::MESSAGE($CNF::place{'data'}{'path'});
		return($o_message, $o_message->write(\%value));
	}
__SUB__

	write_file => <<'__SUB__',
#
#   Ytt@CL^
#
	sub write_file {
		require './app/lib/file/fle.pl';
		my $o_file = new FILE::FILE($CNF::place{'data'}{'path'});
		return($o_file, $o_file->write({no => $::FORM{'no'},
							file => $::FORM{'file'}}));
	}
__SUB__

	write_frequency => <<'__SUB__',
#
#   Ytt@CL^
#
	sub write_frequency {
		require './app/lib/file/frq.pl';
		my $o_frequency = new FILE::FREQUENCY($CNF::place{'data'}{'path'});
		$o_frequency->add($::FORM{'name'},
					  length($::FORM{'subject'} . $::FORM{'body'}));
	}
__SUB__

#	keyword => <<'__SUB__',
##
##   L[[hL^
##
#	sub keyword {
#		require './app/lib/file/kyw.pl';
#		my $str = "$::FORM{'subject'}\t$::FORM{'body'}";
#		my $o_keyword = new FILE::KEYWORD($CNF::place{'data'}{'path'});
#		$o_keyword->write($::FORM{'no'}, &STRING::keyword(\$str));
#		$o_keyword->complete();
#	}
#__SUB__


#	attach => <<'__SUB__',
##
##   Ytt@Cǉ
##
#	sub attach {
#		do "$::FLAG{'template'}/psn.skin.pl"; require './app/lib/htm.pl';
#		do "$::FLAG{'template'}/msg.skin.pl";
#
#		# Ytt@C폜
#		foreach (@{$::FORM{'file'}}) {
#			unlink("$CNF::place{'file'}{'path'}/${$_}{'name'}");
#		}
#		
#		# bN
#		&::unlock('msg');
#
#		# p[^[Zbg
#		$::FORM{'cmd'} = 'psn';
#		foreach (qw(email key mail_mode name inform_mail owner_mail web genre sage body_color)) {
#			$::COOKIE{$_} = $::FORM{$_};
#		}
#		my $body = $::FORM{'body'};
#		$::FORM{'file'}
#			&& map($_{'size'} = &COMMON::size($_{'size'}), @{$::FORM{'file'}});
#
#		## vr[\
#		#&BASE::head('pst'); &MESSAGE::all(\%::FORM);
#
#		# p[^[C
#		$::FORM{'body'} = $body;
#		$CNF::post{'emoji'}{'mode'} && &EMOJI::cancel(\$::FORM{'body'});
#		&ETC::option_unlink(&HTML::auto_unlink(\$::FORM{'body'}));
#		map(&HTML::escape(\$::FORM{$_}), keys %::FORM);
#
#		# ԐM̏ꍇ
#		if ($::FORM{'follow'}) {
#			require './app/lib/file/msg.pl'; 
#			require './app/cmd/msg.pl';	
#			my $o_message = new FILE::MESSAGE($CNF::place{'data'}{'path'});
#			(my $r_MSG = $o_message->one($::FORM{'follow'}))
#				or &::error(\'ԐM̋Lǂݍ߂܂ł');
#			local *MSG = $r_MSG;
#
#			# Qƕ
#			&MESSAGE::quot_color(\$MSG{'body'}); $MSG{'body'} =~ s/^/@ /gm;
#			$r_reference = &DESIGN::reference(\$MSG{'body'});
#		}
#
#		# e͗̐ݒ
#		my $upload_input;
#		$CNF::upload{'mode'}
#			&& map($upload_input .= ${&DESIGN::upload_input},
#				   (0 .. ($CNF::upload{'enable'}{'number'} - 1)));
#		$CNF::mail{'inform'}{'mode'}
#			&& (my $r_inform_mail_input = &DESIGN::inform_mail_input);
#		$CNF::mail{'owner'}{'mode'}
#			&& (my $r_owner_mail_input = &DESIGN::owner_mail_input);
#		$CNF::view{'genre'}{'mode'}
#			&& (my $r_genre_input = &DESIGN::genre_input);
#		$CNF::view{'tree'}{'sort'}{'mode'}
#			&& $CNF::post{'sage'}{'mode'}
#			&& (my $r_sage_input = &DESIGN::sage_input);
#		$CNF::view{'body_color'}{'mode'}
#			&& (my $r_body_color_input = &DESIGN::body_color_input);
#
#		#Xp΍n	
#		if($::FORM{'cmd'} ne "mdt"){&tohoho;}
#		my $r_timekey;
#		if($::FORM{'cmd'} ne "mdt"){
#			$r_timekey = &make_timekey($CNF::antispam{'timekey'}{'idseed'});
#		}
#		
#		# ͗\
#		&DESIGN::post($r_genre_input, \$::FORM{'subject'}, \$::FORM{'body'},
#			\$upload_input, $r_sage_input, $r_inform_mail_input,
#			$r_owner_mail_input, $r_reference,
#			$r_body_color_input, $r_timekey, $r_postkey_input, 
#			$upfile_list, $upfile_size,
#			$upfile_com, $upsize_com, $sub_com, $name_com, $email_com, $key_com,
#			$body_com, $web_com, $gen_com);
#
#
#		&BASE::foot('pst');
#	}
#__SUB__


);

1;

