#!/usr/local/bin/perl

#画像掲示板 ver2009/12/28
#コメント38種、画像・PDFを9枚アップ可能、自動サムネイル生成
#修正記事を最上段かそのままかの選択可
#サムネイルの形状を2種用意
#詳細表示のみ版
#記事交互に別要素1つ設定可能（シマシマ等）

$ver = '管理画面';

# 外部ファイル取り込み
use Jcode;
use Image::Magick;
require '../lib/cgi-lib.pl';

# 本体ファイルURL
$script = 'index.cgi';

# ログファイル
$logfile = 'index.dat';

# テンプレート
$tmpfile = '_index.html';

# 管理パスワード
$pass = 'gg47wst8';

# 画像アップディレクトリとそのURL
$imgdir = './index_img/';
$imgurl = 'http://www.moyai.or.jp/rapport/recruit/index_img/';

#フォームサイズ
$fcols = 60;
$frows =10;
$fsize = 60;

#画像数（max9）
$numupimg = 1;
$numupimg = $numupimg - 1;

# 投稿受理最大サイズ (bytes)
# → 例 : 102400 = 100KB
$cgi_lib'maxdata = 10480000;

# 画像ファイルの最大表示の大きさ（単位：ピクセル）
# → これを超える画像は縮小表示します
#拡大画像
$MaxW = 800;	# 横幅
$MaxH = 600;	# 縦幅
#サムネイル画像
$MaxW2 = 110;	# 横幅
$MaxH2 = 150;	# 縦幅

# 1ページあたり表示件数
$pagelog = 30;

# URLの自動リンク (0=no 1=yes)
$autolink = 1;

# 最大記事数
# → これを超える記事は古い順に自動削除されます
$max = 31;

# ページボタン (0=画像ボタン 1=フォームボタン)
$pageBtn = 1;

# 画像ボタンの定義
$nextImg = "";
$backImg = "";

#一覧表示にて、色をシマシマに等に使う
$bgcolor1 ="#ff0000";
$bgcolor2 ="#ffff00";

#項目名
$subname = "";		#固定
$datname = "日付";	#固定
$comname1 = "修正後の位置";	#固定
$comname2 = "";	#固定
$comname3 = "職種";
$comname4 = "応募資格";
$comname5 = "給与等";
$comname6 = "待遇・条件等";
$comname7 = "";
$comname8 = "";
$comname9 = "";
$comname10 = "";
$comname11 = "";
$comname12 = "";
$comname13 = "";
$comname14 = "";
$comname15 = "";
$comname16 = "";
$comname17 = "";
$comname18 = "";
$comname19 = "";
$comname20 = "";
$comname21 = "";
$comname22 = "";
$comname23 = "";
$comname24 = "";
$comname25 = "";
$comname26 = "";
$comname27 = "";
$comname28 = "";
$comname29 = "";
$comname30 = "";
$comname31 = "";
$comname32 = "";
$comname33 = "";
$comname34 = "";
$comname35 = "";
$comname36 = "";
$comname37 = "";
$comname38 = "";
$comname39 = "";
$comname40 = "";

#フォーム種（0=テキストフィールド、1=テキストエリア（改行反映））
$fsubname = 0;
$fdatname = 0;
$fcomname1 = 0;
$fcomname2 = 0;
$fcomname3 = 0;
$fcomname4 = 1;
$fcomname5 = 1;
$fcomname6 = 1;
$fcomname7 = 0;
$fcomname8 = 0;
$fcomname9 = 0;
$fcomname10 = 0;
$fcomname11 = 0;
$fcomname12 = 0;
$fcomname13 = 0;
$fcomname14 = 0;
$fcomname15 = 0;
$fcomname16 = 0;
$fcomname17 = 0;
$fcomname18 = 0;
$fcomname19 = 0;
$fcomname20 = 0;
$fcomname21 = 0;
$fcomname22 = 0;
$fcomname23 = 0;
$fcomname24 = 0;
$fcomname25 = 0;
$fcomname26 = 0;
$fcomname27 = 0;
$fcomname28 = 0;
$fcomname29 = 0;
$fcomname30 = 0;
$fcomname31 = 0;
$fcomname32 = 0;
$fcomname33 = 0;
$fcomname34 = 0;
$fcomname35 = 0;
$fcomname36 = 0;
$fcomname37 = 0;
$fcomname38 = 0;
$fcomname39 = 0;
$fcomname40 = 0;

#------------#
#  設定完了  #
#------------#

&decode;
if ($mode eq "admin") { &admin; }
elsif ($mode eq "checking") { &check; }
&logfile;

#------------#
#  記事表示  #
#------------#
sub logfile {
	local($flag,$msg,$i,$next,$back,$loop,@head,@loop,@foot);

	# HTMLヘッダ
	print "Content-type: text/html\n\n";

	# テンプレート読み込み
	$loop="";
	@head=();
	@foot=();
	$flag=0;
	
	open(IN,"$tmpfile") || &error("Open Error: $tmpfile");

	while (<IN>) {
		push(@head,$_) if (!$flag);

		if (/<!-- line1 -->/) { $flag=1; }
		elsif (/<!-- line2 -->/) { $flag=2; }
		if ($flag == 1) { $loop .= $_; }
		elsif ($flag == 2) { push(@foot,$_); }
	}
	close(IN);

	# データ読み込み
	$number = -1 + $page;	#通し番号

	@loop=();
	$i=0;
	open(IN,"$logfile") || &error("Open Error: $logfile");
	while (<IN>) {
		$i++;
		next if ($i < $page + 1);
		next if ($i > $page + $pagelog);

		$msg = $loop;
		($no,$date,$sub,
		$com1,$com2,$com3,$com4,$com5,$com6,$com7,$com8,$com9,$com10,
		$com11,$com12,$com13,$com14,$com15,$com16,$com17,$com18,$com19,$com20,
		$com21,$com22,$com23,$com24,$com25,$com26,$com27,$com28,$com29,$com30,
		$com31,$com32,$com33,$com34,$com35,$com36,$com37,$com38,$com39,$com40,
		$t[0],$w[0],$h[0],$t[1],$w[1],$h[1],$t[2],$w[2],$h[2],$t[3],$w[3],$h[3],$t[4],$w[4],$h[4],
		$t[5],$w[5],$h[5],$t[6],$w[6],$h[6],$t[7],$w[7],$h[7],$t[8],$w[8],$h[8]) = split(/<>/);

		$number = $number + 1;

		# URLリンク
		#&auto_link($com3) if ($autolink);
		
		# 各種変換
		if ($date) { $date = "（".substr($date, 0, 10)."）"; }
				
		#シマシマ計算
		$value  = $number % 2;
		$bgcolor = $value == "0" ? $bgcolor1 : $bgcolor2;	#余りの有る無しが条件

		$msg =~ s/!bgcolor!/$bgcolor/i;
		$msg =~ s/!subject!/$sub/i;
		$msg =~ s/!date!/$date/i;
		$msg =~ s/!comment1!/$com1/i;
		$msg =~ s/!comment2!/$com2/i;
		$msg =~ s/!comment3!/$com3/i;
		$msg =~ s/!comment4!/$com4/i;
		$msg =~ s/!comment5!/$com5/i;
		$msg =~ s/!comment6!/$com6/i;
		$msg =~ s/!comment7!/$com7/i;
		$msg =~ s/!comment8!/$com8/i;
		$msg =~ s/!comment9!/$com9/i;
		$msg =~ s/!comment10!/$com10/i;
		$msg =~ s/!comment11!/$com11/i;
		$msg =~ s/!comment12!/$com12/i;
		$msg =~ s/!comment13!/$com13/i;
		$msg =~ s/!comment14!/$com14/i;
		$msg =~ s/!comment15!/$com15/i;
		$msg =~ s/!comment16!/$com16/i;
		$msg =~ s/!comment17!/$com17/i;
		$msg =~ s/!comment18!/$com18/i;
		$msg =~ s/!comment19!/$com19/i;
		$msg =~ s/!comment20!/$com20/i;
		$msg =~ s/!comment21!/$com21/i;
		$msg =~ s/!comment22!/$com22/i;
		$msg =~ s/!comment23!/$com23/i;
		$msg =~ s/!comment24!/$com24/i;
		$msg =~ s/!comment25!/$com25/i;
		$msg =~ s/!comment26!/$com26/i;
		$msg =~ s/!comment27!/$com27/i;
		$msg =~ s/!comment28!/$com28/i;
		$msg =~ s/!comment29!/$com29/i;
		$msg =~ s/!comment30!/$com30/i;
		$msg =~ s/!comment31!/$com31/i;
		$msg =~ s/!comment32!/$com32/i;
		$msg =~ s/!comment33!/$com33/i;
		$msg =~ s/!comment34!/$com34/i;
		$msg =~ s/!comment35!/$com35/i;
		$msg =~ s/!comment36!/$com36/i;
		$msg =~ s/!comment37!/$com37/i;
		$msg =~ s/!comment38!/$com38/i;
		$msg =~ s/!comment39!/$com39/i;
		$msg =~ s/!comment40!/$com40/i;

		# 画像
		foreach $i (0 .. $numupimg) {
			$j = $i + 1;
			$image = "!image-$j\!";

			if (!$t[$i]) { $msg =~ s/$image//i; next; }

			if ($h[$i] && $w[$i]) { $wh = "width=\"$w[$i]\" height=\"$h[$i]\""; }
			else { $wh=""; }

			if ($t[$i] eq ".pdf") {
				$msg =~ s|$image|<a href=\"$imgurl$no-$j.pdf\" target=\"_blank\"><img src=\"$imgurl$no-s$j.png\" alt=\"拡大\表\示\" /></a>|i;
			} else {
				# モーダルウィンドウ系スクリプトを使用する場合は要改変
				$msg =~ s|$image|<a href=\"$imgurl$no-$j$t[$i]\" rel=\"lytebox[$no]\"><img src=\"$imgurl$no-s$j$t[$i]\" alt=\"拡大\表\示\" $wh /></a>|i;
			}
		}

		$msg =~ s/<!-- line1 -->\n//g;
		push(@loop,$msg);
	}
	close(IN);

	# ページ繰越ボタン
	$next = $page + $pagelog;
	$back = $page - $pagelog;
	if ($back >= 0) {
		$bflag=1;
		if ($pageBtn) {
			$backBtn = "<form action=\"$script\" method=get class=\"btn_back\">\n";
			$backBtn .= "<input type=hidden name=page value=\"$back\" />\n";
			$backBtn .= "<input type=submit value=\"前ページへ\" class=\"submit\" /></form>\n";
		} else {
			$backBtn = "<a href=\"$script?page=$back\"><img src=\"$backImg\" border=0 alt=\"前ページへ\" /></a>\n";
		}
	} else {
		$bflag=0;
		$backBtn = "";
	}
	if ($next < $i) {
		$nflag=1;
		if ($pageBtn) {
			$nextBtn = "<form action=\"$script\" method=get class=\"btn_next\">\n";
			$nextBtn .= "<input type=hidden name=page value=\"$next\" />\n";
			$nextBtn .= "<input type=submit value=\"次ページへ\" class=\"submit\" /></form>\n";
		} else {
			$nextBtn = "<a href=\"$script?page=$next\"><img src=\"$nextImg\" border=0 alt=\"次ページへ\" /></a>\n";
		}
	} else {
		$nflag=0;
		$nextBtn = "";
	}

	# ヘッダ表示
	foreach (@head) {
		s|!back!\n|$backBtn|;
		s|!next!\n|$nextBtn|;
		s|<!-- line1 -->\n||;
		print;
	}

	print @loop;

	# フッタ表示
	foreach (@foot) {
		s|!back!\n|$backBtn|;
		s|!next!\n|$nextBtn|;
		s|<!-- line2 -->\n||;
		print;
	}
	exit;
}

#------------#
#  管理画面  #
#------------#
sub admin {
	local($no,$dat,$sub,$com1,$com2,$com3,$com4,$com5,$com6,$com7,$com8,$com9,$com10,
		$com11,$com12,$com13,$com14,$com15,$com16,$com17,$com18,$com19,$com20,
		$com21,$com22,$com23,$com24,$com25,$com26,$com27,$com28,$com29,$com30,
		$com31,$com32,$com33,$com34,$com35,$com36,$com37,$com38,$com39,$com40,
		$f,$del);

	# 認証
	if ($in{'pass'} ne $pass) { &error("パスワードが違います。"); }

	# 投稿フォーム
	if ($in{'job'} eq "form") {

		&form;

	# 投稿処理
	} elsif ($in{'job'} eq "form2") {

		local($no,$t1,$w1,$h1,$t2,$w2,$h2,$t3,$w3,$h3,$t4,$w4,$h4,$t5,$w5,$h5,
		$t6,$w6,$h6,$t7,$w7,$h7,$t8,$w8,$h8,$t9,$w9,$h9,@file);

		if (!$in{'comment3'}) { &error("$comname3が未入力です。"); }

		# タグ復元
		if ($in{'tag'} == 1) {
			$in{'sub'} = &tag($in{'sub'});
			$in{'date'} = &tag($in{'date'});
			$in{'comment1'} = &tag($in{'comment1'});
			$in{'comment2'} = &tag($in{'comment2'});
			$in{'comment3'} = &tag($in{'comment3'});
			$in{'comment4'} = &tag($in{'comment4'});
			$in{'comment5'} = &tag($in{'comment5'});
			$in{'comment6'} = &tag($in{'comment6'});
			$in{'comment7'} = &tag($in{'comment7'});
			$in{'comment8'} = &tag($in{'comment8'});
			$in{'comment9'} = &tag($in{'comment9'});
			$in{'comment10'} = &tag($in{'comment10'});
			$in{'comment11'} = &tag($in{'comment11'});
			$in{'comment12'} = &tag($in{'comment12'});
			$in{'comment13'} = &tag($in{'comment13'});
			$in{'comment14'} = &tag($in{'comment14'});
			$in{'comment15'} = &tag($in{'comment15'});
			$in{'comment16'} = &tag($in{'comment16'});
			$in{'comment17'} = &tag($in{'comment17'});
			$in{'comment18'} = &tag($in{'comment18'});
			$in{'comment19'} = &tag($in{'comment19'});
			$in{'comment20'} = &tag($in{'comment20'});
			$in{'comment21'} = &tag($in{'comment21'});
			$in{'comment22'} = &tag($in{'comment22'});
			$in{'comment23'} = &tag($in{'comment23'});
			$in{'comment24'} = &tag($in{'comment24'});
			$in{'comment25'} = &tag($in{'comment25'});
			$in{'comment26'} = &tag($in{'comment26'});
			$in{'comment27'} = &tag($in{'comment27'});
			$in{'comment28'} = &tag($in{'comment28'});
			$in{'comment29'} = &tag($in{'comment29'});
			$in{'comment30'} = &tag($in{'comment30'});
			$in{'comment31'} = &tag($in{'comment31'});
			$in{'comment32'} = &tag($in{'comment32'});
			$in{'comment33'} = &tag($in{'comment33'});
			$in{'comment34'} = &tag($in{'comment34'});
			$in{'comment35'} = &tag($in{'comment35'});
			$in{'comment36'} = &tag($in{'comment36'});
			$in{'comment37'} = &tag($in{'comment37'});
			$in{'comment38'} = &tag($in{'comment38'});
			$in{'comment39'} = &tag($in{'comment39'});
			$in{'comment40'} = &tag($in{'comment40'});
		}

		open(IN,"$logfile") || &error("Open Error: $logfile");
		@file = <IN>;
		close(IN);

		# 採番
		$maxno = 0;
		for( $i=0 ; $i <= $#file ; $i++ ) {
			($maxno2) = split(/<>/, $file[$i]);
			$maxno = $maxno2 if ($maxno2 > $maxno);
		}
		$no = $maxno + 1;

		# 画像アップ
		if ($in{'upfile1'} || $in{'upfile2'} || $in{'upfile3'} || $in{'upfile4'} || $in{'upfile5'} || $in{'upfile6'} || $in{'upfile7'} || $in{'upfile8'} || $in{'upfile9'}) {
			($t1,$w1,$h1,$t2,$w2,$h2,$t3,$w3,$h3,$t4,$w4,$h4,$t5,$w5,$h5,$t6,$w6,$h6,$t7,$w7,$h7,$t8,$w8,$h8,$t9,$w9,$h9) = &upload($no);
		}

		# 最大記事数調整
		while ($max-1 <= @file) {
			$del = pop(@file);
			local($no,$date,$sub,
			$com1,$com2,$com3,$com4,$com5,$com6,$com7,$com8,$com9,$com10,
			$com11,$com12,$com13,$com14,$com15,$com16,$com17,$com18,$com19,$com20,
			$com21,$com22,$com23,$com24,$com25,$com26,$com27,$com28,$com29,$com30,
			$com31,$com32,$com33,$com34,$com35,$com36,$com37,$com38,$com39,$com40,
			$t[0],$w[0],$h[0],$t[1],$w[1],$h[1],$t[2],$w[2],$h[2],$t[3],$w[3],$h[3],
			$t[4],$w[4],$h[4],$t[5],$w[5],$h[5],$t[6],$w[6],$h[6],$t[7],$w[7],$h[7],$t[8],$w[8],$h[8],$tag)
							= split(/<>/, $del);
			unlink("$imgdir$no-1$t[0]") if ($t[0]);
			unlink("$imgdir$no-s1$t[0]") if ($t[0]);
			unlink("$imgdir$no-s1.png") if ($t[0] eq ".pdf");
			unlink("$imgdir$no-2$t[1]") if ($t[1]);
			unlink("$imgdir$no-s2$t[1]") if ($t[1]);
			unlink("$imgdir$no-s2.png") if ($t[1] eq ".pdf");
			unlink("$imgdir$no-3$t[2]") if ($t[2]);
			unlink("$imgdir$no-s3$t[2]") if ($t[2]);
			unlink("$imgdir$no-s3.png") if ($t[2] eq ".pdf");
			unlink("$imgdir$no-4$t[3]") if ($t[3]);
			unlink("$imgdir$no-s4$t[3]") if ($t[3]);
			unlink("$imgdir$no-s4.png") if ($t[3] eq ".pdf");
			unlink("$imgdir$no-5$t[4]") if ($t[4]);
			unlink("$imgdir$no-s5$t[4]") if ($t[4]);
			unlink("$imgdir$no-s5.png") if ($t[4] eq ".pdf");
			unlink("$imgdir$no-6$t[5]") if ($t[5]);
			unlink("$imgdir$no-s6$t[5]") if ($t[5]);
			unlink("$imgdir$no-s6.png") if ($t[5] eq ".pdf");
			unlink("$imgdir$no-7$t[6]") if ($t[6]);
			unlink("$imgdir$no-s7$t[6]") if ($t[6]);
			unlink("$imgdir$no-s7.png") if ($t[6] eq ".pdf");
			unlink("$imgdir$no-8$t[7]") if ($t[7]);
			unlink("$imgdir$no-s8$t[7]") if ($t[7]);
			unlink("$imgdir$no-s8.png") if ($t[7] eq ".pdf");
			unlink("$imgdir$no-9$t[8]") if ($t[8]);
			unlink("$imgdir$no-s9$t[8]") if ($t[8]);
			unlink("$imgdir$no-s9.png") if ($t[8] eq ".pdf");
		}

		# 更新
		unshift(@file,"$no<>$in{'date'}<>$in{'sub'}<>$in{'comment1'}<>$in{'comment2'}<>$in{'comment3'}<>$in{'comment4'}<>$in{'comment5'}<>$in{'comment6'}<>$in{'comment7'}<>$in{'comment8'}<>$in{'comment9'}<>$in{'comment10'}<>$in{'comment11'}<>$in{'comment12'}<>$in{'comment13'}<>$in{'comment14'}<>$in{'comment15'}<>$in{'comment16'}<>$in{'comment17'}<>$in{'comment18'}<>$in{'comment19'}<>$in{'comment20'}<>$in{'comment21'}<>$in{'comment22'}<>$in{'comment23'}<>$in{'comment24'}<>$in{'comment25'}<>$in{'comment26'}<>$in{'comment27'}<>$in{'comment28'}<>$in{'comment29'}<>$in{'comment30'}<>$in{'comment31'}<>$in{'comment32'}<>$in{'comment33'}<>$in{'comment34'}<>$in{'comment35'}<>$in{'comment36'}<>$in{'comment37'}<>$in{'comment38'}<>$in{'comment39'}<>$in{'comment40'}<>$t1<>$w1<>$h1<>$t2<>$w2<>$h2<>$t3<>$w3<>$h3<>$t4<>$w4<>$h4<>$t5<>$w5<>$h5<>$t6<>$w6<>$h6<>$t7<>$w7<>$h7<>$t8<>$w8<>$h8<>$t9<>$w9<>$h9<>$in{'tag'}<>\n");
		open(OUT,">$logfile") || &error("Write Error: $logfile");
		print OUT @file;
		close(OUT);

	# 削除
	} elsif ($in{'job'} eq "dele" && $in{'no'}) {

		local($i,$j,$f);
		local(@new)=();
		open(IN,"$logfile") || &error("Open Error: $logfile");
		while (<IN>) {
			$f=0;
			($no,$date,$sub,
			$com1,$com2,$com3,$com4,$com5,$com6,$com7,$com8,$com9,$com10,
			$com11,$com12,$com13,$com14,$com15,$com16,$com17,$com18,$com19,$com20,
			$com21,$com22,$com23,$com24,$com25,$com26,$com27,$com28,$com29,$com30,
			$com31,$com32,$com33,$com34,$com35,$com36,$com37,$com38,$com39,$com40,
			$t[0],$w[0],$h[0],$t[1],$w[1],$h[1],$t[2],$w[2],$h[2],$t[3],$w[3],$h[3],$t[4],$w[4],$h[4],
			$t[5],$w[5],$h[5],$t[6],$w[6],$h[6],$t[7],$w[7],$h[7],$t[8],$w[8],$h[8],$tag) = split(/<>/);

			foreach $del ( split(/\0/, $in{'no'}) ) {
				if ($no == $del) {
					$f++;
					foreach $i (0 .. $numupimg) {
						$j = $i+1;
						unlink("$imgdir$no-$j$t[$i]");
						unlink("$imgdir$no-s$j$t[$i]");
						unlink("$imgdir$no-s$j.png") if ($t[$i] eq ".pdf");
					}
					last;
				}
			}
			if (!$f) { push(@new,$_); }
		}
		close(IN);

		# 更新
		open(OUT,">$logfile") || &error("Write Error: $logfile");
		print OUT @new;
		close(OUT);

	# 修正フォーム
	} elsif ($in{'job'} eq "edit" && $in{'no'}) {

		local(@no) = split(/\0/, $in{'no'});

		open(IN,"$logfile") || &error("Open Error: $logfile");
		while (<IN>) {
			($no,$dat,$sub,$com1,$com2,$com3,$com4,$com5,$com6,$com7,$com8,$com9,$com10,
			$com11,$com12,$com13,$com14,$com15,$com16,$com17,$com18,$com19,$com20,
			$com21,$com22,$com23,$com24,$com25,$com26,$com27,$com28,$com29,$com30,
			$com31,$com32,$com33,$com34,$com35,$com36,$com37,$com38,$com39,$com40,
			$t1,$w1,$h1,$t2,$w2,$h2,$t3,$w3,$h3,$t4,$w4,$h4,$t5,$w5,$h5,
			$t6,$w6,$h6,$t7,$w7,$h7,$t8,$w8,$h8,$t9,$w9,$h9,$tag) = split(/<>/);
			last if ($no[0] == $no);
		}
		close(IN);

		# 修正画面
		&form($no,$dat,$sub,$com1,$com2,$com3,$com4,$com5,$com6,$com7,$com8,$com9,$com10,
		$com11,$com12,$com13,$com14,$com15,$com16,$com17,$com18,$com19,$com20,
		$com21,$com22,$com23,$com24,$com25,$com26,$com27,$com28,$com29,$com30,
		$com31,$com32,$com33,$com34,$com35,$com36,$com37,$com38,$com39,$com40,
		$t1,$w1,$h1,$t2,$w2,$h2,$t3,$w3,$h3,$t4,$w4,$h4,$t5,$w5,$h5,
		$t6,$w6,$h6,$t7,$w7,$h7,$t8,$w8,$h8,$t9,$w9,$h9,$tag);

	# 修正実行
	} elsif ($in{'job'} eq "edit2" && $in{'no'}) {

		# タグ復元
		if ($in{'tag'} == 1) {
			$$in{'sub'} = &tag($in{'sub'});
			$in{'date'} = &tag($in{'date'});
			$in{'comment1'} = &tag($in{'comment1'});
			$in{'comment2'} = &tag($in{'comment2'});
			$in{'comment3'} = &tag($in{'comment3'});
			$in{'comment4'} = &tag($in{'comment4'});
			$in{'comment5'} = &tag($in{'comment5'});
			$in{'comment6'} = &tag($in{'comment6'});
			$in{'comment7'} = &tag($in{'comment7'});
			$in{'comment8'} = &tag($in{'comment8'});
			$in{'comment9'} = &tag($in{'comment9'});
			$in{'comment10'} = &tag($in{'comment10'});
			$in{'comment11'} = &tag($in{'comment11'});
			$in{'comment12'} = &tag($in{'comment12'});
			$in{'comment13'} = &tag($in{'comment13'});
			$in{'comment14'} = &tag($in{'comment14'});
			$in{'comment15'} = &tag($in{'comment15'});
			$in{'comment16'} = &tag($in{'comment16'});
			$in{'comment17'} = &tag($in{'comment17'});
			$in{'comment18'} = &tag($in{'comment18'});
			$in{'comment19'} = &tag($in{'comment19'});
			$in{'comment20'} = &tag($in{'comment20'});
			$in{'comment21'} = &tag($in{'comment21'});
			$in{'comment22'} = &tag($in{'comment22'});
			$in{'comment23'} = &tag($in{'comment23'});
			$in{'comment24'} = &tag($in{'comment24'});
			$in{'comment25'} = &tag($in{'comment25'});
			$in{'comment26'} = &tag($in{'comment26'});
			$in{'comment27'} = &tag($in{'comment27'});
			$in{'comment28'} = &tag($in{'comment28'});
			$in{'comment29'} = &tag($in{'comment29'});
			$in{'comment30'} = &tag($in{'comment30'});
			$in{'comment31'} = &tag($in{'comment31'});
			$in{'comment32'} = &tag($in{'comment32'});
			$in{'comment33'} = &tag($in{'comment33'});
			$in{'comment34'} = &tag($in{'comment34'});
			$in{'comment35'} = &tag($in{'comment35'});
			$in{'comment36'} = &tag($in{'comment36'});
			$in{'comment37'} = &tag($in{'comment37'});
			$in{'comment38'} = &tag($in{'comment38'});
			$in{'comment39'} = &tag($in{'comment39'});
			$in{'comment40'} = &tag($in{'comment40'});
		}

		# 画像アップ
		if ($in{'upfile1'} || $in{'upfile2'} || $in{'upfile3'} || $in{'upfile4'} || $in{'upfile5'} || $in{'upfile6'} || $in{'upfile7'} || $in{'upfile8'} || $in{'upfile9'}) {
			($t1,$w1,$h1,$t2,$w2,$h2,$t3,$w3,$h3,$t4,$w4,$h4,$t5,$w5,$h5,$t6,$w6,$h6,$t7,$w7,$h7,$t8,$w8,$h8,$t9,$w9,$h9) = &upload($in{'no'});
		}

		local(@new)=();
		open(IN,"$logfile") || &error("Open Error: $logfile");
		while (<IN>) {
			s/\n$//;
			($no,$dat,$sub,$com1,$com2,$com3,$com4,$com5,$com6,$com7,$com8,$com9,$com10,
		$com11,$com12,$com13,$com14,$com15,$com16,$com17,$com18,$com19,$com20,
		$com21,$com22,$com23,$com24,$com25,$com26,$com27,$com28,$com29,$com30,
		$com31,$com32,$com33,$com34,$com35,$com36,$com37,$com38,$com39,$com40,
		$t01,$w01,$h01,$t02,$w02,$h02,$t03,$w03,$h03,$t04,$w04,$h04,$t05,$w05,$h05,
		$t06,$w06,$h06,$t07,$w07,$h07,$t08,$w08,$h08,$t09,$w09,$h09,$tag) = split(/<>/);

			if ($in{'no'} == $no) {

				if ($in{'del1'}) {
					unlink("$imgdir$no-1$t01");
					unlink("$imgdir$no-s1$t01");
					unlink("$imgdir$no-s1.png");
					$t01 = $w01 = $h01 = "";
				}
				if ($in{'del2'}) {
					unlink("$imgdir$no-2$t02");
					unlink("$imgdir$no-s2$t02");
					unlink("$imgdir$no-s2.png");
					$t02 = $w02 = $h02 = "";
				}
				if ($in{'del3'}) {
					unlink("$imgdir$no-3$t03");
					unlink("$imgdir$no-s3$t03");
					unlink("$imgdir$no-s3.png");
					$t03 = $w03 = $h03 = "";
				}
				if ($in{'del4'}) {
					unlink("$imgdir$no-4$t04");
					unlink("$imgdir$no-s4$t04");
					unlink("$imgdir$no-s4.png");
					$t04 = $w04 = $h04 = "";
				}
				if ($in{'del5'}) {
					unlink("$imgdir$no-5$t05");
					unlink("$imgdir$no-s5$t05");
					unlink("$imgdir$no-s5.png");
					$t05 = $w05 = $h05 = "";
				}
				if ($in{'del6'}) {
					unlink("$imgdir$no-6$t06");
					unlink("$imgdir$no-s6$t06");
					unlink("$imgdir$no-s6.png");
					$t06 = $w06 = $h06 = "";
				}
				if ($in{'del7'}) {
					unlink("$imgdir$no-7$t07");
					unlink("$imgdir$no-s7$t07");
					unlink("$imgdir$no-s7.png");
					$t07 = $w07 = $h07 = "";
				}
				if ($in{'del8'}) {
					unlink("$imgdir$no-8$t08");
					unlink("$imgdir$no-s8$t08");
					unlink("$imgdir$no-s8.png");
					$t08 = $w08 = $h08 = "";
				}
				if ($in{'del9'}) {
					unlink("$imgdir$no-9$t09");
					unlink("$imgdir$no-s9$t09");
					unlink("$imgdir$no-s9.png");
					$t09 = $w09 = $h09 = "";
				}
				if ($t1) { $t01=$t1; $w01=$w1; $h01=$h1; }
				if ($t2) { $t02=$t2; $w02=$w2; $h02=$h2; }
				if ($t3) { $t03=$t3; $w03=$w3; $h03=$h3; }
				if ($t4) { $t04=$t4; $w04=$w4; $h04=$h4; }
				if ($t5) { $t05=$t5; $w05=$w5; $h05=$h5; }
				if ($t6) { $t06=$t6; $w06=$w6; $h06=$h6; }
				if ($t7) { $t07=$t7; $w07=$w7; $h07=$h7; }
				if ($t8) { $t08=$t8; $w08=$w8; $h08=$h8; }
				if ($t9) { $t09=$t9; $w09=$w9; $h09=$h9; }

				$_ = "$no<>$in{'date'}<>$in{'sub'}<>$in{'comment1'}<>$in{'comment2'}<>$in{'comment3'}<>$in{'comment4'}<>$in{'comment5'}<>$in{'comment6'}<>$in{'comment7'}<>$in{'comment8'}<>$in{'comment9'}<>$in{'comment10'}<>$in{'comment11'}<>$in{'comment12'}<>$in{'comment13'}<>$in{'comment14'}<>$in{'comment15'}<>$in{'comment16'}<>$in{'comment17'}<>$in{'comment18'}<>$in{'comment19'}<>$in{'comment20'}<>$in{'comment21'}<>$in{'comment22'}<>$in{'comment23'}<>$in{'comment24'}<>$in{'comment25'}<>$in{'comment26'}<>$in{'comment27'}<>$in{'comment28'}<>$in{'comment29'}<>$in{'comment30'}<>$in{'comment31'}<>$in{'comment32'}<>$in{'comment33'}<>$in{'comment34'}<>$in{'comment35'}<>$in{'comment36'}<>$in{'comment37'}<>$in{'comment38'}<>$in{'comment39'}<>$in{'comment40'}<>$t01<>$w01<>$h01<>$t02<>$w02<>$h02<>$t03<>$w03<>$h03<>$t04<>$w04<>$h04<>$t05<>$w05<>$h05<>$t06<>$w06<>$h06<>$t07<>$w07<>$h07<>$t08<>$w08<>$h08<>$t09<>$w09<>$h09<>$in{'tag'}<>";
			}
			if (($in{'no'} == $no) && ($in{'comment1'} eq "")) {	#更新後最上段へ
				unshift(@new,"$_\n");
			} else {
				push(@new,"$_\n");
			}
		}
		close(IN);

		# 更新
		open(OUT,">$logfile") || &error("Write Error: $logfile");
		print OUT @new;
		close(OUT);
	}

	&header;
	print <<"EOM";
<form action="$script">
<input type=submit value="新規書込み" class="submit">
<input type=hidden name=mode value="admin">
<input type=hidden name=pass value="$in{'pass'}">
<input type=hidden name=job value="form">
</form>
<ul>
<li>修正・削除の場合は、該当する記事にチェックを入れ、処理を選択して送信ボタンを押してください。
<li>削除は1度につき1件です。
<li>$max\件目以降の記事は自動的に削除されます。
</ul>
<form action="$script" method="POST">
<input type=hidden name=mode value="admin">
<input type=hidden name=pass value="$in{'pass'}">
処理：<select name=job>
<option value="edit">修正
<option value="dele">削除
</select>
<input type=submit value="送信する" class="submit">
EOM

	# ログ展開
	open(IN,"$logfile") || &error("Open Error: $logfile");
	while (<IN>) {
		($no,$dat,$sub,
		$com1,$com2,$com3,$com4,$com5,$com6,$com7,$com8,$com9,$com10,
		$com11,$com12,$com13,$com14,$com15,$com16,$com17,$com18,$com19,$com20,
		$com21,$com22,$com23,$com24,$com25,$com26,$com27,$com28,$com29,$com30,
		$com31,$com32,$com33,$com34,$com35,$com36,$com37,$com38,$com39,$com40,
		$t[0],$w[0],$h[0],$t[1],$w[1],$h[1],$t[2],$w[2],$h[2],$t[3],$w[3],$h[3],$t[4],$w[4],$h[4],
		$t[5],$w[5],$h[5],$t[6],$w[6],$h[6],$t[7],$w[7],$h[7],$t[8],$w[8],$h[8],$tag) = split(/<>/);

		$sub =~ s/<([^>]|\n)*>//g;
		$dat =~ s/<([^>]|\n)*>//g;
		$com3 =~ s/<([^>]|\n)*>//g;

		if (length($sub) > 100) {
			$sub = substr($sub,0,100);
			$sub .= "・・・";
		}
		if (length($com3) > 100) {
			$com3 = substr($com3,0,100);
			$com3 .= "・・・";
		}

		print "<hr><table border=\"0\" cellspacing=\"0\" cellpadding=\"5\"><tr>\n";
		print "<td align=\"left\" valign=\"top\" bgcolor=\"#cccccc\"><input type=checkbox name=no value=\"$no\"></td>\n";
		print "<td align=\"left\" valign=\"top\"><b>$com3</b><br><span style='font-size:11px'>$dat</span><br>\n";

		foreach $i (0 .. $numupimg) {
			$j = $i + 1;
			next if (!$t[$i]);

			print "[<a href=\"$imgurl$no-$j$t[$i]\" target=\"_blank\">ファイル$j</a>]\n";
		}
		print "</td></tr></table>\n";
	}
	close(IN);

	print "<hr></form>\n";
	print &HtmlBot;
	exit;
}

#----------------#
#  投稿フォーム  #
#----------------#
sub form {
	local($no,$dat,$sub,
		$com1,$com2,$com3,$com4,$com5,$com6,$com7,$com8,$com9,$com10,
		$com11,$com12,$com13,$com14,$com15,$com16,$com17,$com18,$com19,$com20,
		$com21,$com22,$com23,$com24,$com25,$com26,$com27,$com28,$com29,$com30,
		$com31,$com32,$com33,$com34,$com35,$com36,$com37,$com38,$com39,$com40,
		$t[0],$w[0],$h[0],$t[1],$w[1],$h[1],$t[2],$w[2],$h[2],$t[3],$w[3],$h[3],$t[4],$w[4],$h[4],
		$t[5],$w[5],$h[5],$t[6],$w[6],$h[6],$t[7],$w[7],$h[7],$t[8],$w[8],$h[8],$tag) = @_;

	if ($tag == 1) {
		# 改行はそのまま
		$sub =~ s/<br \/>/\r/ig;
		$dat =~ s/<br \/>/\r/ig;
		$com1 =~ s/<br \/>/\r/ig;
		$com2 =~ s/<br \/>/\r/ig;
		$com3 =~ s/<br \/>/\r/ig;
		$com4 =~ s/<br \/>/\r/ig;
		$com5 =~ s/<br \/>/\r/ig;
		$com6 =~ s/<br \/>/\r/ig;
		$com7 =~ s/<br \/>/\r/ig;
		$com8 =~ s/<br \/>/\r/ig;
		$com9 =~ s/<br \/>/\r/ig;
		$com10 =~ s/<br \/>/\r/ig;
		$com11 =~ s/<br \/>/\r/ig;
		$com12 =~ s/<br \/>/\r/ig;
		$com13 =~ s/<br \/>/\r/ig;
		$com14 =~ s/<br \/>/\r/ig;
		$com15 =~ s/<br \/>/\r/ig;
		$com16 =~ s/<br \/>/\r/ig;
		$com17 =~ s/<br \/>/\r/ig;
		$com18 =~ s/<br \/>/\r/ig;
		$com19 =~ s/<br \/>/\r/ig;
		$com20 =~ s/<br \/>/\r/ig;
		$com21 =~ s/<br \/>/\r/ig;
		$com22 =~ s/<br \/>/\r/ig;
		$com23 =~ s/<br \/>/\r/ig;
		$com24 =~ s/<br \/>/\r/ig;
		$com25 =~ s/<br \/>/\r/ig;
		$com26 =~ s/<br \/>/\r/ig;
		$com27 =~ s/<br \/>/\r/ig;
		$com28 =~ s/<br \/>/\r/ig;
		$com29 =~ s/<br \/>/\r/ig;
		$com30 =~ s/<br \/>/\r/ig;
		$com31 =~ s/<br \/>/\r/ig;
		$com32 =~ s/<br \/>/\r/ig;
		$com33 =~ s/<br \/>/\r/ig;
		$com34 =~ s/<br \/>/\r/ig;
		$com35 =~ s/<br \/>/\r/ig;
		$com36 =~ s/<br \/>/\r/ig;
		$com37 =~ s/<br \/>/\r/ig;
		$com38 =~ s/<br \/>/\r/ig;
		$com39 =~ s/<br \/>/\r/ig;
		$com40 =~ s/<br \/>/\r/ig;
		$checked = " checked";
	} else {
		# 改行は復元？
		$sub =~ s/<br \/>/\r/ig;
		$dat =~ s/<br \/>/\r/ig;
		$com1 =~ s/<br \/>/\r/ig;
		$com2 =~ s/<br \/>/\r/ig;
		$com3 =~ s/<br \/>/\r/ig;
		$com4 =~ s/<br \/>/\r/ig;
		$com5 =~ s/<br \/>/\r/ig;
		$com6 =~ s/<br \/>/\r/ig;
		$com7 =~ s/<br \/>/\r/ig;
		$com8 =~ s/<br \/>/\r/ig;
		$com9 =~ s/<br \/>/\r/ig;
		$com10 =~ s/<br \/>/\r/ig;
		$com11 =~ s/<br \/>/\r/ig;
		$com12 =~ s/<br \/>/\r/ig;
		$com13 =~ s/<br \/>/\r/ig;
		$com14 =~ s/<br \/>/\r/ig;
		$com15 =~ s/<br \/>/\r/ig;
		$com16 =~ s/<br \/>/\r/ig;
		$com17 =~ s/<br \/>/\r/ig;
		$com18 =~ s/<br \/>/\r/ig;
		$com19 =~ s/<br \/>/\r/ig;
		$com20 =~ s/<br \/>/\r/ig;
		$com21 =~ s/<br \/>/\r/ig;
		$com22 =~ s/<br \/>/\r/ig;
		$com23 =~ s/<br \/>/\r/ig;
		$com24 =~ s/<br \/>/\r/ig;
		$com25 =~ s/<br \/>/\r/ig;
		$com26 =~ s/<br \/>/\r/ig;
		$com27 =~ s/<br \/>/\r/ig;
		$com28 =~ s/<br \/>/\r/ig;
		$com29 =~ s/<br \/>/\r/ig;
		$com30 =~ s/<br \/>/\r/ig;
		$com31 =~ s/<br \/>/\r/ig;
		$com32 =~ s/<br \/>/\r/ig;
		$com33 =~ s/<br \/>/\r/ig;
		$com34 =~ s/<br \/>/\r/ig;
		$com35 =~ s/<br \/>/\r/ig;
		$com36 =~ s/<br \/>/\r/ig;
		$com37 =~ s/<br \/>/\r/ig;
		$com38 =~ s/<br \/>/\r/ig;
		$com39 =~ s/<br \/>/\r/ig;
		$com40 =~ s/<br \/>/\r/ig;
		$checked = "";
	}

	# パラメータ定義
	local($job) = $in{'job'} . '2';

	# 年月日を取得		#を外すと新規のみ
	#if ($dat eq "") {
		$ENV{'TZ'} = "JST-9";
		local($min, $hour,$mday,$mon,$year) = (localtime(time))[1..5];
		$dat = sprintf("%04d/%02d/%02d/%02d:%02d", $year+1900,$mon+1,$mday,$hour,$min);
	#}

	# フォーム表示
	&header;
	$fnumupimg = $numupimg + 1;
	if ($thumbtype) { $thumbtypecom = "ON"; } else { $thumbtypecom = "OFF"; }
	if ($updatetop) { $updatetopcom = "ON"; } else { $updatetopcom = "OFF"; }
	print <<"EOM";
<form>
<input type=button value="前画面に戻る" onClick="history.back()" class="submit">
</form>
<form action="$script" method="POST" enctype="multipart/form-data">
<input type=hidden name=mode value="admin">
<input type=hidden name=pass value="$in{'pass'}">
<input type=hidden name=job value="$job">
<input type=hidden name=no value="$no">
<input type=hidden name=tag value="1">
<table border=0 cellpadding=5 cellspacing=2>
EOM
if ($subname) {
	print "<tr><td bgcolor=#cccccc>$subname</td><td>\n";
	if ($fsubname) { print "<textarea name=sub cols=$fcols rows=$frows wrap=soft>$sub</textarea>\n"; }
	else { print "<input type=text name=sub value=\"$sub\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($datname) {
	print "<tr><td bgcolor=#cccccc>$datname</td><td>\n";
	if ($fdatname) { print "<textarea name=date cols=$fcols rows=$frows wrap=soft>$dat</textarea>\n"; }
	else { print "<input type=text name=date value=\"$dat\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname1) {
	print "<tr><td bgcolor=#cccccc>$comname1</td><td>\n";
	print "<input type=radio value=\"\" name=comment1";
	if ($com1 eq "") { print " checked"; }
	print ">最上段へ　<input type=radio value=\"1\" name=comment1";
	if ($com1 eq "1") { print " checked"; }
	print ">そのまま\n";
	print "</td></tr>\n";
}
if ($comname2) {
	print "<tr><td bgcolor=#cccccc>$comname2</td><td>\n";
	print "<input type=radio value=\"\" name=comment2";
	if ($com2 eq "") { print " checked"; }
	print ">縦横比はそのままで縮小　<input type=radio value=\"1\" name=comment2";
	if ($com2 eq "1") { print " checked"; }
	print ">正方形にトリミングして縮小<div style=\"height:10px; line-height:0; font-size:0em;\"></div><span style=\"font-size:11px; color:#666;\">※PDFの場合は、縦横比はそのままで縮小になります。<br>※修正時に変更する場合は画像の再アップが必要です。（再アップしたものだけに適用されます。）</span>\n";
	print "</td></tr>\n";
}
if ($comname3) {
	print "<tr><td bgcolor=#cccccc>$comname3</td><td>\n";
	if ($fcomname3) { print "<textarea name=comment3 cols=$fcols rows=$frows wrap=soft>$com3</textarea>\n"; }
	else { print "<input type=text name=comment3 value=\"$com3\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname4) {
	print "<tr><td bgcolor=#cccccc>$comname4</td><td>\n";
	if ($fcomname4) { print "<textarea name=comment4 cols=$fcols rows=$frows wrap=soft>$com4</textarea>\n"; }
	else { print "<input type=text name=comment4 value=\"$com4\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname5) {
	print "<tr><td bgcolor=#cccccc>$comname5</td><td>\n";
	if ($fcomname5) { print "<textarea name=comment5 cols=$fcols rows=$frows wrap=soft>$com5</textarea>\n"; }
	else { print "<input type=text name=comment5 value=\"$com5\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname6) {
	print "<tr><td bgcolor=#cccccc>$comname6</td><td>\n";
	if ($fcomname6) { print "<textarea name=comment6 cols=$fcols rows=$frows wrap=soft>$com6</textarea>\n"; }
	else { print "<input type=text name=comment6 value=\"$com6\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname7) {
	print "<tr><td bgcolor=#cccccc>$comname7</td><td>\n";
	if ($fcomname7) { print "<textarea name=comment7 cols=$fcols rows=$frows wrap=soft>$com7</textarea>\n"; }
	else { print "<input type=text name=comment7 value=\"$com7\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname8) {
	print "<tr><td bgcolor=#cccccc>$comname8</td><td>\n";
	if ($fcomname8) { print "<textarea name=comment8 cols=$fcols rows=$frows wrap=soft>$com8</textarea>\n"; }
	else { print "<input type=text name=comment8 value=\"$com8\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname9) {
	print "<tr><td bgcolor=#cccccc>$comname9</td><td>\n";
	if ($fcomname9) { print "<textarea name=comment9 cols=$fcols rows=$frows wrap=soft>$com9</textarea>\n"; }
	else { print "<input type=text name=comment9 value=\"$com9\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname10) {
	print "<tr><td bgcolor=#cccccc>$comname10</td><td>\n";
	if ($fcomname10) { print "<textarea name=comment10 cols=$fcols rows=$frows wrap=soft>$com10</textarea>\n"; }
	else { print "<input type=text name=comment10 value=\"$com10\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname11) {
	print "<tr><td bgcolor=#cccccc>$comname11</td><td>\n";
	if ($fcomname11) { print "<textarea name=comment11 cols=$fcols rows=$frows wrap=soft>$com11</textarea>\n"; }
	else { print "<input type=text name=comment11 value=\"$com11\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname12) {
	print "<tr><td bgcolor=#cccccc>$comname12</td><td>\n";
	if ($fcomname12) { print "<textarea name=comment12 cols=$fcols rows=$frows wrap=soft>$com12</textarea>\n"; }
	else { print "<input type=text name=comment12 value=\"$com12\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname13) {
	print "<tr><td bgcolor=#cccccc>$comname13</td><td>\n";
	if ($fcomname13) { print "<textarea name=comment13 cols=$fcols rows=$frows wrap=soft>$com13</textarea>\n"; }
	else { print "<input type=text name=comment13 value=\"$com13\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname14) {
	print "<tr><td bgcolor=#cccccc>$comname14</td><td>\n";
	if ($fcomname14) { print "<textarea name=comment14 cols=$fcols rows=$frows wrap=soft>$com14</textarea>\n"; }
	else { print "<input type=text name=comment14 value=\"$com14\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname15) {
	print "<tr><td bgcolor=#cccccc>$comname15</td><td>\n";
	if ($fcomname15) { print "<textarea name=comment15 cols=$fcols rows=$frows wrap=soft>$com15</textarea>\n"; }
	else { print "<input type=text name=comment15 value=\"$com15\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname16) {
	print "<tr><td bgcolor=#cccccc>$comname16</td><td>\n";
	if ($fcomname16) { print "<textarea name=comment16 cols=$fcols rows=$frows wrap=soft>$com16</textarea>\n"; }
	else { print "<input type=text name=comment16 value=\"$com16\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname17) {
	print "<tr><td bgcolor=#cccccc>$comname17</td><td>\n";
	if ($fcomname17) { print "<textarea name=comment17 cols=$fcols rows=$frows wrap=soft>$com17</textarea>\n"; }
	else { print "<input type=text name=comment17 value=\"$com17\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname18) {
	print "<tr><td bgcolor=#cccccc>$comname18</td><td>\n";
	if ($fcomname18) { print "<textarea name=comment18 cols=$fcols rows=$frows wrap=soft>$com18</textarea>\n"; }
	else { print "<input type=text name=comment18 value=\"$com18\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname19) {
	print "<tr><td bgcolor=#cccccc>$comname19</td><td>\n";
	if ($fcomname19) { print "<textarea name=comment19 cols=$fcols rows=$frows wrap=soft>$com19</textarea>\n"; }
	else { print "<input type=text name=comment19 value=\"$com19\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname20) {
	print "<tr><td bgcolor=#cccccc>$comname20</td><td>\n";
	if ($fcomname20) { print "<textarea name=comment20 cols=$fcols rows=$frows wrap=soft>$com20</textarea>\n"; }
	else { print "<input type=text name=comment20 value=\"$com20\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname21) {
	print "<tr><td bgcolor=#cccccc>$comname21</td><td>\n";
	if ($fcomname21) { print "<textarea name=comment21 cols=$fcols rows=$frows wrap=soft>$com21</textarea>\n"; }
	else { print "<input type=text name=comment21 value=\"$com21\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname22) {
	print "<tr><td bgcolor=#cccccc>$comname22</td><td>\n";
	if ($fcomname22) { print "<textarea name=comment22 cols=$fcols rows=$frows wrap=soft>$com22</textarea>\n"; }
	else { print "<input type=text name=comment22 value=\"$com22\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname23) {
	print "<tr><td bgcolor=#cccccc>$comname23</td><td>\n";
	if ($fcomname23) { print "<textarea name=comment23 cols=$fcols rows=$frows wrap=soft>$com23</textarea>\n"; }
	else { print "<input type=text name=comment23 value=\"$com23\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname24) {
	print "<tr><td bgcolor=#cccccc>$comname24</td><td>\n";
	if ($fcomname24) { print "<textarea name=comment24 cols=$fcols rows=$frows wrap=soft>$com24</textarea>\n"; }
	else { print "<input type=text name=comment24 value=\"$com24\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname25) {
	print "<tr><td bgcolor=#cccccc>$comname25</td><td>\n";
	if ($fcomname25) { print "<textarea name=comment25 cols=$fcols rows=$frows wrap=soft>$com25</textarea>\n"; }
	else { print "<input type=text name=comment25 value=\"$com25\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname26) {
	print "<tr><td bgcolor=#cccccc>$comname26</td><td>\n";
	if ($fcomname26) { print "<textarea name=comment26 cols=$fcols rows=$frows wrap=soft>$com26</textarea>\n"; }
	else { print "<input type=text name=comment26 value=\"$com26\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname27) {
	print "<tr><td bgcolor=#cccccc>$comname27</td><td>\n";
	if ($fcomname27) { print "<textarea name=comment27 cols=$fcols rows=$frows wrap=soft>$com27</textarea>\n"; }
	else { print "<input type=text name=comment27 value=\"$com27\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname28) {
	print "<tr><td bgcolor=#cccccc>$comname28</td><td>\n";
	if ($fcomname28) { print "<textarea name=comment28 cols=$fcols rows=$frows wrap=soft>$com28</textarea>\n"; }
	else { print "<input type=text name=comment28 value=\"$com28\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname29) {
	print "<tr><td bgcolor=#cccccc>$comname29</td><td>\n";
	if ($fcomname29) { print "<textarea name=comment29 cols=$fcols rows=$frows wrap=soft>$com29</textarea>\n"; }
	else { print "<input type=text name=comment29 value=\"$com29\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname30) {
	print "<tr><td bgcolor=#cccccc>$comname30</td><td>\n";
	if ($fcomname30) { print "<textarea name=comment30 cols=$fcols rows=$frows wrap=soft>$com30</textarea>\n"; }
	else { print "<input type=text name=comment30 value=\"$com30\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname31) {
	print "<tr><td bgcolor=#cccccc>$comname31</td><td>\n";
	if ($fcomname31) { print "<textarea name=comment31 cols=$fcols rows=$frows wrap=soft>$com31</textarea>\n"; }
	else { print "<input type=text name=comment31 value=\"$com31\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname32) {
	print "<tr><td bgcolor=#cccccc>$comname32</td><td>\n";
	if ($fcomname32) { print "<textarea name=comment32 cols=$fcols rows=$frows wrap=soft>$com32</textarea>\n"; }
	else { print "<input type=text name=comment32 value=\"$com32\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname33) {
	print "<tr><td bgcolor=#cccccc>$comname33</td><td>\n";
	if ($fcomname33) { print "<textarea name=comment33 cols=$fcols rows=$frows wrap=soft>$com33</textarea>\n"; }
	else { print "<input type=text name=comment33 value=\"$com33\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname34) {
	print "<tr><td bgcolor=#cccccc>$comname34</td><td>\n";
	if ($fcomname34) { print "<textarea name=comment34 cols=$fcols rows=$frows wrap=soft>$com34</textarea>\n"; }
	else { print "<input type=text name=comment34 value=\"$com34\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname35) {
	print "<tr><td bgcolor=#cccccc>$comname35</td><td>\n";
	if ($fcomname35) { print "<textarea name=comment35 cols=$fcols rows=$frows wrap=soft>$com35</textarea>\n"; }
	else { print "<input type=text name=comment35 value=\"$com35\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname36) {
	print "<tr><td bgcolor=#cccccc>$comname36</td><td>\n";
	if ($fcomname36) { print "<textarea name=comment36 cols=$fcols rows=$frows wrap=soft>$com36</textarea>\n"; }
	else { print "<input type=text name=comment36 value=\"$com36\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname37) {
	print "<tr><td bgcolor=#cccccc>$comname37</td><td>\n";
	if ($fcomname37) { print "<textarea name=comment37 cols=$fcols rows=$frows wrap=soft>$com37</textarea>\n"; }
	else { print "<input type=text name=comment37 value=\"$com37\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname38) {
	print "<tr><td bgcolor=#cccccc>$comname38</td><td>\n";
	if ($fcomname38) { print "<textarea name=comment38 cols=$fcols rows=$frows wrap=soft>$com38</textarea>\n"; }
	else { print "<input type=text name=comment38 value=\"$com38\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname39) {
	print "<tr><td bgcolor=#cccccc>$comname39</td><td>\n";
	if ($fcomname39) { print "<textarea name=comment39 cols=$fcols rows=$frows wrap=soft>$com39</textarea>\n"; }
	else { print "<input type=text name=comment39 value=\"$com39\" size=$fsize>\n"; }
	print "</td></tr>\n";
}
if ($comname40) {
	print "<tr><td bgcolor=#cccccc>$comname40</td><td>\n";
	if ($fcomname40) { print "<textarea name=comment40 cols=$fcols rows=$frows wrap=soft>$com40</textarea>\n"; }
	else { print "<input type=text name=comment40 value=\"$com40\" size=$fsize>\n"; }
	print "</td></tr>\n";
}

#local($i,$j);
#foreach $i (0 .. $numupimg) {
	#$j = $i+1;

	#print "<tr><td>ファイル$j</td><td><input type=file name=\"upfile$j\" size=45>";

	#if ($t[$i]) {
		#print "&nbsp;&nbsp;<input type=checkbox name=\"del$j\" value=1>削除\n";
		#print "[<a href=\"$imgurl$no-$j$t[$i]\" target=\"_blank\">ファイル$j</a>]";
	#}

	#print "</td></tr>\n";
#}

	print <<EOM;
</table>
<p>
<input type=submit value="送信する" class="submit">
</form>
EOM
	print &HtmlBot;
	exit;
}

#----------------#
#  デコード処理  #
#----------------#
sub decode {
	local($key,$val);

	&ReadParse;
	while ( ($key,$val) = each %in ) {

		if ($key !~ /^upfile/) {

			# utf8コード変換
			&Jcode::convert($val, 'utf8');

			# タグ処理
			$val =~ s/<>/&LT;&GT;/g;
			$val =~ s/&/&amp;/g;
			$val =~ s/"/&quot;/g;
			$val =~ s/</&lt;/g;
			$val =~ s/>/&gt;/g;

			# 改行処理
			$val =~ s/\r\n/<br \/>/g;
			$val =~ s/\r/<br \/>/g;
			$val =~ s/\n/<br \/>/g;
		}
		$in{$key} = $val;
	}
	$mode = $in{'mode'};
	$page = $in{'page'};
}

#--------------#
#  HTMLヘッダ  #
#--------------#
sub header {
	if ($headflag) { return; }

	print "Content-type: text/html\n\n";
	print <<"EOM";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja">
<head>
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=utf-8">
<title>$ver</title>
<link rel="stylesheet" type="text/css" href="../css/perl.css" media="all" />
</head>
<body bgcolor="#f0f0f0" text="#333333" style="margin:25px;">
EOM
	$headflag=1;
}

#--------------#
#  エラー処理  #
#--------------#
sub error {
	&header;
	print <<"EOM";
<div align="center">
<h3>エラーが発生しました。下記をご確認ください。</h3>
<font color="red">$_[0]</font>
</div>
EOM
	print &HtmlBot;
	exit;
}

#--------------------#
#  画像アップロード  #
#--------------------#
sub upload {
	local($no) = @_;
	local($tail,$fnam,$macbin,$f,$i,$flag,$imgfile,
		$upfile,$length,$W,$H,$W2,$H2,@tail,@fnam,@name,@upfile);

	# 画像処理
	$macbin=0;
	@tail=();
	@fnam=();
	@name=();
	foreach (@in) {
		my ($j,$i) = 0;
		if (/.*name=\"(.*)\";.*filename=\"(.*)\"/i) {
			$fnam = $2;
			$i = $1;
			$i =~ /upfile(\d)/i;
			$j = $1 - 1;
			$fnam =~ s/\r//g;
			$fnam =~ s/\n//g;
			$fnam[$j] = $fnam;
			$name[$j] = $i;
		}
		if (/(.*)Content-type:(.*)/i) {
			$tail = $2;
			$tail =~ s/\r//g;
			$tail =~ s/\n//g;
			$tail[$j] = $tail;
		}
		if (/application\/x-macbinary/i) { $macbin=1; }
	}

	# ファイル形式を認識
	$f=0;
	$i=0;
	@upfile=();
	foreach (0 .. $numupimg) {
		$i++;
		$flag=0;
		if ($tail[$_] =~ /image\/gif/i) { $tail=".gif"; $flag=1; }
		elsif ($tail[$_] =~ /image\/jpeg/i) { $tail=".jpg"; $flag=1; }
		elsif ($tail[$_] =~ /image\/x-png/i) { $tail=".png"; $flag=1; }
		elsif ($tail[$_] =~ /pdf/i) { $tail=".pdf"; $flag=1; }
		if (!$flag) {
			if ($fnam[$_] =~ /\.gif$/i) { $tail=".gif"; $flag=1; }
			elsif ($fnam[$_] =~ /\.jpe?g$/i) { $tail=".jpg"; $flag=1; }
			elsif ($fnam[$_] =~ /\.png$/i) { $tail=".png"; $flag=1; }
			elsif ($fnam[$_] =~ /\.pdf$/i) { $tail=".pdf"; $flag=1; }
		}

		if ($name[$_] eq "upfile$i") {
			$upfile = $in{"upfile$i"};
			$imgfile = "$imgdir/$no-$i$tail";
		}

		$flag = 0 unless($imgfile);
		
		# アップロード結果
		if ($flag) { $f++; }
		else { push(@upfile,("","","")); next; }

		# マックバイナリ対策
		if ($macbin) {
			$length = substr($upfile,83,4);
			$length = unpack("%N",$length);
			$upfile = substr($upfile,128,$length);
		}

		# データ書込み
		open(OUT,">$imgfile") || &error("ファイルのアップロードに失敗しました。");
		binmode(OUT);
		binmode(STDOUT);
		print OUT $upfile;
		close(OUT);

		chmod (0666,$imgfile);

		# 画像サイズ取得
		if ($tail eq ".jpg") { ($W, $H) = &j_size($imgfile); }
		elsif ($tail eq ".gif") { ($W, $H) = &g_size($imgfile); }
		elsif ($tail eq ".png") { ($W, $H) = &p_size($imgfile); }

		# 拡大用画像
		if (($W > $MaxW || $H > $MaxH) && ($tail ne ".pdf")) {
			$W2 = $MaxW / $W;
			$H2 = $MaxH / $H;
			if ($W2 < $H2) { $key = $W2; }
			else { $key = $H2; }
			$W = int ($W * $key) || 1;
			$H = int ($H * $key) || 1;
			
			# 拡大用画像生成（イメージマジック使用）
			$putsam = "$imgdir/$no-$i$tail";
			$tempimage = Image::Magick->new();
			$tempimage->Read($imgfile);
			$tempimage->Resize(width => $W,height => $H);
			$tempimage->Write($putsam);
		}
		# サムネイル画像
		if (($in{'comment2'} eq "") && ($tail ne ".pdf")) {	#トリミング指定可否
				$W2 = $MaxW2 / $W;
				$H2 = $MaxH2 / $H;
				if ($W2 < $H2) { $key = $W2; }
				else { $key = $H2; }
				$W3 = int ($W * $key) || 1;
				$H3 = int ($H * $key) || 1;
			
				# サムネイル生成（イメージマジック使用）
				$putsam = "$imgdir/$no-s$i$tail";
				$tempimage = Image::Magick->new();
				$tempimage->Read($imgfile);
				$tempimage->Resize(width => $W3,height => $H3);
				$tempimage->Write($putsam);
				
		} elsif ($tail ne ".pdf") {
				$W2 = $MaxW2 / $W;
				$H2 = $MaxH2 / $H;
				if ($W2 > $H2) {
					$key = $W2;
					$W3 = int ($W * $key) || 1;
					$H3 = int ($H * $key) || 1;
					$H4 = int (($H3-$MaxH2)/2);
				
					# サムネイル生成（イメージマジック使用）
					$putsam = "$imgdir/$no-s$i$tail";
					$tempimage = Image::Magick->new();
					$tempimage->Read($imgfile);
					$tempimage->Resize(width => $W3,height => $H3);
					$tempimage->Crop(width=> $MaxW2,height=> $MaxH2,x=> 0,y=> $H4);
					$tempimage->Write($putsam);
					$W3 = $MaxW2;
					$H3 = $MaxH2;
					
				} else {
					$key = $H2;
					$W3 = int ($W * $key) || 1;
					$H3 = int ($H * $key) || 1;
					$W4 = int (($W3-$MaxW2)/2);
					
					# サムネイル生成（イメージマジック使用）
					$putsam = "$imgdir/$no-s$i$tail";
					$tempimage = Image::Magick->new();
					$tempimage->Read($imgfile);
					$tempimage->Resize(width => $W3,height => $H3);
					$tempimage->Crop(width=> $MaxW2,height=> $MaxH2,x=>$W4,y=> 0);
					$tempimage->Write($putsam);
					$W3 = $MaxW2;
					$H3 = $MaxH2;

				}
		}
		# PDFサムネイル生成（イメージマジック使用）
		if ($tail eq ".pdf") {
			$imgfile = $imgfile .'[0]';
			$putsam = "$imgdir/$no-s$i.png";
			$tempimage = Image::Magick->new();
			$tempimage->Read($imgfile);
			$tempimage->Write($putsam);
			$tempimage2 = Image::Magick->new();
			$tempimage2->Read($putsam);
			$tempimage2= $tempimage2->Transform(geometry=>"$MaxW2 x $MaxH2");
			unlink("putsam");
			$tempimage2->Write($putsam);

		}

		push(@upfile,($tail,$W3,$H3));

	}
	return @upfile;
}

#------------------#
#  JPEGサイズ認識  #
#------------------#
sub j_size {
	local($jpeg) = @_;
	local($t, $m, $c, $l, $W, $H);

	open(JPEG, "$jpeg") || return (0,0);
	binmode JPEG;
	read(JPEG, $t, 2);
	while (1) {
		read(JPEG, $t, 4);
		($m, $c, $l) = unpack("a a n", $t);

		if ($m ne "\xFF") {
			$W = $H = 0;
			last;
		} elsif ((ord($c) >= 0xC0) && (ord($c) <= 0xC3)) {
			read(JPEG, $t, 5);
			($H, $W) = unpack("xnn", $t);
			last;
		} else {
			read(JPEG, $t, ($l - 2));
		}
	}
	close(JPEG);
	return ($W, $H);
}

#-----------------#
#  GIFサイズ認識  #
#-----------------#
sub g_size {
	local($gif) = @_;
	local($data);

	open(GIF,"$gif") || return (0,0);
	binmode(GIF);
	sysread(GIF,$data,10);
	close(GIF);

	if ($data =~ /^GIF/) { $data = substr($data,-4); }

	$W = unpack("v",substr($data,0,2));
	$H = unpack("v",substr($data,2,2));
	return ($W, $H);
}

#-----------------#
#  PNGサイズ認識  #
#-----------------#
sub p_size {
	local($png) = @_;
	local($data);

	open(PNG, "$png") || return (0,0);
	binmode(PNG);
	read(PNG, $data, 24);
	close(PNG);

	$W = unpack("N", substr($data, 16, 20));
	$H = unpack("N", substr($data, 20, 24));
	return ($W, $H);
}

#-----------------#
#  自動URLリンク  #
#-----------------#
sub auto_link {
	$_[0] =~ s/([^=^\"]|^)(https?\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/$1<a href=\"$2\" target=\"_blank\">$2<\/a>/g;
	$_[0] =~ s/([!#-9A-~\-\_]+\@[!#-9A-~\-\_\.]+)/<a href=\"mailto:$1\">$1<\/a>/g;
}

#------------#
#  タグ復元  #
#------------#
sub tag {
	local($_) = @_;

	s/&lt;/</g;
	s/&gt;/>/g;
	s/&amp;/&/g;
	s/&quot;/"/g;
	$_;
}

#------------------#
#  チェックモード  #
#------------------#
sub check {
	&header;
	print <<EOM;
<h2>Check Mode</h2>
<ul>
EOM

	# ログファイル
	if (-e $logfile) {
		print "<li>ログファイル：パスOK!\n";
		if (-r $logfile && -w $logfile) { print "<li>ログパーミッション：OK!\n"; }
		else { print "<li>ログパーミッションが不正です。\n"; }
	} else {
		print "<li>ログファイルのパスが不正です： $logfile\n";
	}

	# 画像ディレクトリ
	if (-d $imgdir) {
		print "<li>画像ディレクトリ：パスOK!\n";
		if (-r $imgdir && -w $imgdir && -x $imgdir) {
			print "<li>画像ディレクトリのパーミッション：OK!\n";
		} else {
			print "<li>画像ディレクトリのパーミッションが不正です。\n";
		}
	} else {
		print "<li>画像ディレクトリのパスが不正です： $imgdir\n";
	}

	print <<EOM;
<li>バージョン：$ver
</ul>
</body>
</html>
EOM
	exit;
}


__END__

