#==========================================================================#
# ■ B.Forum Ver.1.01                                                      #
# ■ プラグインSDK(regist)                                                #
# □ Encode = EUC                                                          #
# □ Hiroaki,Sakuma (sakuma@beetas.org)                                    #
#                                                                          #
# このアプリケーションはオープンソースウェアです.                          #
# 無料で使用することができます.                                            #
# なお, 詳細な使用条件/最新情報については下記サイトをご覧下さい.           #
# http://www.beetas.org/                                                   #
#                                                                          #
# ------------------------------------------------------------------------ #
# Copyright 2001-2002 Hiroaki,Sakuma All Rights Reserved.                  #
# Copyright 2001-2002 BEETAS.org All Rights Reserved.                      #
#                                                                          #
#==========================================================================#
package regist;

use Exporter;
use strict;

@ISA = qw(Exporter);
@EXPORT = qw(version status require_min require_max main);

my (%in %SET);

sub version {
	# バージョン(要変更)
	$version = "Plugin SDK(regist) 1.01(0055-0058)";
	return $version;
}

sub status {
	return 1;
}

sub require_min {
	# 最低動作バージョン(要変更)
	$require_min = "0055";
	return $require_min;
}

sub require_max {
	# 最高動作バージョン(要変更)
	$require_max = "0058";
	return $require_max;
}

sub main ($$$@) {

	# 変更の必要なし
	my $head = $_[0];
	my $foot = $_[1];
	%in = %{$_[2]};
	splice (@_,0,3);
	%SET = %Bforum::SET;



	# ここから変更

	if (&Bforum::limit) {
		&Bforum::form;
		undef @_;
		return;
	} else {

		my ($tmp1,$tmp2,$tmp3,$tmp4,$tmp5,$tmp6);
		my (@tmp7,@tmp8);
		my (@index,@list);

		foreach (split(',',$SET{'INDISPENSABLE'})) {
			if (!$in{$_} && !$in{('nk.' . $_)}) {
				$error{$_} = 1;
			}
		}

		if (($in{'email'} && $in{'email'} !~ /\S+\@\S+\.\S+/) || ($in{'nk.email'} && $in{'nk.email'} !~ /\S+\@\S+\.\S+/)) { $error{'email'} = 1; }

		if ($SET{'FILE_UPLOAD_DO'} ne 'N') {
			foreach (keys %Bforum::files) {
				if ($SET{('FILE_UPLOAD_LIMIT_' . uc($_))} && $SET{('FILE_UPLOAD_LIMIT_' . uc($_))} < length((split(/[\n|\r|(\r\n)]{2}/,$Bforum::files{$_}))[1])) {
					$error{$_} = 1;
					$Bforum::notice .= &Bforum::_open(5,"$SET{'USER_DIR'}/$in{'id'}/BF.error$Bforum::browser->{browser}","$SET{'USER_DIR'}/BF.error$Bforum::browser->{browser}") || "<b>ファイル容量が設定値を超えています</b>\n";
				} elsif ($SET{'FILE_UPLOAD_LIMIT'} && $SET{'FILE_UPLOAD_LIMIT'} < length((split(/[\n|\r|(\r\n)]{2}/,$files{$_}))[1])) {
					$error{$_} = 1;
					$Bforum::notice .= &Bforum::_open(5,"$SET{'USER_DIR'}/$in{'id'}/BF.error$Bforum::browser->{browser}","$SET{'USER_DIR'}/BF.error$Bforum::browser->{browser}") || "<b>ファイル容量が設定値を超えています</b>\n";
				}
			}
		}

		if (%error) {
			$Bforum::notice .= &Bforum::_open(0,"$SET{'USER_DIR'}/$in{'id'}/BF.error$Bforum::browser->{browser}","$SET{'USER_DIR'}/BF.error$Bforum::browser->{browser}") || "<b>エラーの為書き込めませんでした</b>\n";
			undef $Bforum::in{'passwd'};
			&Bforum::form;
		} else {

			&Bforum::_analysis(\%check,&Bforum::_open(undef,"$SET{'USER_DIR'}/$in{'id'}/last"));
			$check{'message'} =~ s/\n<br>\n/\n/gio;
			chomp ($check{'message'});
			if ($in{'author'} eq $check{'author'} && $in{'title'} eq $check{'title'} && $in{'message'} eq $check{'message'}) {
				if ($in{'t'}) {
					print "Location: $cgi?id=$in{'id'}&t=$in{'t'}&v=$check{'no'}\n\n";
				} else {
					print "Location: $cgi?$in{'id'}\n\n";
				}
				exit(0);
			}

			if ($in{'confirm'} && !%Bforum::files) { &Bforum::confirm; }

			foreach (sort keys %in) {
				$_ =~ s/nk\.//o;
				if ($_ eq 'f' || $_ eq 'id' || $_ eq 'message' || $_ eq 't' || $_ eq 'v' || $_ eq 'cc') { next; }
				if ($_ eq 'passwd') {
				}
				my ($tmp1);
				$tmp1 = lc ($_);
				$tmp1 = ucfirst ($tmp1);
				$log .= "$tmp1: $in{$_}\n";
			}

			$host = $ENV{'REMOTE_HOST'};
			$addr = $ENV{'REMOTE_ADDR'};
			if ($host eq $addr || $host eq '') { $host = gethostbyaddr(pack("C4",split('.',$addr)),2); }
			if ($host eq '') { $host = $addr; }
			$time = time;

			if (!$in{'t'}) {
				$Bforum::no = 1;
			} else {
				$Bforum::no = $num + 2;
			}

			foreach (keys %files) {
				$tmp5 = $_;
				$tmp5 =~ s/(\W)/'%' . unpack("H2",$1)/ego;
				$in{'files'} .= "$tmp5, ";
			}


			$log .= "No: $Bforum::no\n";
			$log .= "Host: $host\n";
			$log .= "Agent: $ENV{'HTTP_USER_AGENT'}\n";
			$log .= "Version: $Bforum::version(Revision:$Bforum::revision)\n";
			$log .= "Date: $time\n";
			$log .= "Files: $in{'files'}\n";
			$log .= "Referer: $in{'v'}\n";
			$log .= "\n$in{'message'}\n";

			if(&_lock) {

				if (!$in{'t'}) {
					$in{'t'} = time;
					&Bforum::_write("$SET{'USER_DIR'}/$in{'id'}/$in{'t'}/1",$log);

				} else {

					&Bforum::_write("$SET{'USER_DIR'}/$in{'id'}/$in{'t'}/$Bforum::no",$log);

					@list = &Bforum::_open('file',"$SET{'USER_DIR'}/$in{'id'}/$in{'t'}/list",">$SET{'USER_DIR'}/$in{'id'}/$in{'t'}/list");

					if ($in{'v'} && $in{'v'} ne '1') {
						$tmp1 = 0;
						$tmp2 = 0;
						foreach (@list) {
							$tmp2 ++;
							@tmp7 = split (/<0>/,$_);
							if ($tmp7[0] eq $in{'v'}) {
								$tmp7[1] ++;
								splice (@list,$tmp2,undef,"$Bforum::no<0>$tmp7[1]<0>\n");
								last;
							}
						}
					} else {
						unshift (@list,"$Bforum::no<0>0<0>\n");
					}

					&Bforum::_write("$SET{'USER_DIR'}/$in{'id'}/$in{'t'}/list",@list);

				}

				@index = &Bforum::_open('file',"$SET{'USER_DIR'}/$in{'id'}/index");
				if (!@index) { &Bforum::error; }

				if ($SET{'INDEX_SORT_DO'} ne 'N' || $Bforum::no eq '1') {
					$tmp3 = 0;
					foreach (@index) {
						$tmp4 = $_;
						chomp($tmp4);
						if ($tmp4 eq $in{'t'}) {
							splice (@index,$tmp3,1);
							last;
						}
						undef $tmp4;
						$tmp3 ++;
					}
					unshift (@index,"$in{'t'}\n");
				}

				if ($SET{'FILE_UPLOAD_DO'} ne 'N' && %files) {
					umask (000);
					mkdir ("$SET{'USER_DIR'}/$in{'id'}/files",0777);
					mkdir ("$SET{'USER_DIR'}/$in{'id'}/files/$in{'t'}",0777);
					mkdir ("$SET{'USER_DIR'}/$in{'id'}/files/$in{'t'}/$Bforum::no",0777);
					foreach (keys %Bforum::files) {
						$tmp6 = $_;
						$tmp6 =~ s/(\W)/'%' . unpack("H2",$1)/ego;
						open (FILE,">$SET{'USER_DIR'}/$in{'id'}/files/$in{'t'}/$Bforum::no/$tmp6");
						binmode (FILE);
						print FILE $Bforum::files{$_};
						close (FILE);
					}
				}

				&Bforum::_write("$SET{'USER_DIR'}/$in{'id'}/index",@index);
				&Bforum::_write("$SET{'USER_DIR'}/$in{'id'}/last",$log);

				if ($in{'passwd'}) {
					if (&Bforum::_use("MD5")) {
						@tmp8 = ('a' .. 'z','A' .. 'Z',0 .. 9);
						&Bforum::_write("$SET{'USER_DIR'}/$in{'id'}/$in{'t'}/.passwd",&Bforum::_open(undef,"$SET{'USER_DIR'}/$in{'id'}/$in{'t'}/.passwd") . "$Bforum::no:crypt:" . crypt($in{'passwd'},($tmp8[(int(rand($#tmp8)))] . $tmp8[(int(rand($#tmp8)))])) . "\n");
					} else {
						&Bforum::_write("$SET{'USER_DIR'}/$in{'id'}/$in{'t'}/.passwd",&Bforum::_open(undef,"$SET{'USER_DIR'}/$in{'id'}/$in{'t'}/.passwd") . "$Bforum::no:md5:" . MD5->hexhash($in{'passwd'}) . "\n");
					}
				}

				&Bforum::_flush("$SET{'USER_DIR'}/$in{'id'}/last");

				&Bforum::_cache_flush;

				&Bforum::_write("$SET{'USER_DIR'}/$in{'id'}/0",$log);
				&Bforum::_flush("$SET{'USER_DIR'}/$in{'id'}/0");
				&Bforum::mknmz;
				if (-f "$SET{'USER_DIR'}/$in{'id'}/0") { unlink ("$SET{'USER_DIR'}/$in{'id'}/0") || &Bforum::error; }

				&Bforum::status;
				&Bforum::_unlock;

				if ($SET{'SENDMAIL_DO'} ne 'N') {
					$in{'url'} = $ENV{'HTTP_HOST'} . $cgi . "?id=$in{'id'}&t=$in{'t'}&v=$Bforum::no";
					&Bforum::sendmail(&Bforum::_open(undef,"$SET{'USER_DIR'}/$in{'id'}/BF.email$Bforum::browser->{browser}","$SET{'USER_DIR'}/BF.email$Bforum::browser->{browser}"),\%in);
				}

				&Bforum::do($regist);
				&Bforum::change($regist,\%regist);

				$print{'jump'} = "$cgi?id=$in{'id'}&t=$in{'t'}&v=$Bforum::no";

				&Bforum::change($regist,\%print);
				undef %print;

				print &Bforum::_set_cookie;
				$Bforum::print .= $Bforum::regist;

				$Bforum::do{'nohead'} = 1;
				$Bforum::do{'nocache'} = 1;

				return "";

			} else {
				&Bforum::form;
				undef @_;
				return;
			}
			exit(0);
		}
	}

}

sub about {
	close (NULL);
	&Bforum::about((caller)[2] . '@' . &version);
}

1;

