﻿#!/usr/local/bin/perl

#
# TWISTERSMAP SCRIPT
# Script written by SayQoo
# mail@yumekana.com
#
# <利用規定>
#  当スクリプトの著作権は、せいくうにあります。
#  当スクリプトの著作権表示を変更、または削除することはできません。
#
########################### TWISTERSMAP SCRIPT ###########################

require './jcode.pl';
require './cgi-lib.pl';

#********** 設定項目 **********
		#★ タイトル名
$title = "ツイスターズマップ";
		#★ 大会年度
$holdyy = "2012";
		#★ トップページＵＲＬ
$topurl = "https://www.twisters.jp/twisters2012/";
		#★ トップページタイトル
$toptitle = "ツイスターズ2012in福岡";
		#★ トップページへ画像（相対パス）
$totoppic = "./totop.jpg";
		#★ トップページ画像（相対パス）
$toppic = "./twistersmap.jpg";
		#★ バナー画像（相対パス）
$bannerpic = "./banner.jpg";
		#★ ロックファイル
$lockfile = "lockfile_map";
		#★ ツイスターズＭＡＰトップ
$formfile = "./twistersmap.cgi";
		#★ このＣＧＩファイル名
$cgifile = "./twistersmap.cgi";
		#★ ログファイル
$logfile = "./twistersmap.log";
		#★ 画像の最大サイズ
$maxW = 240;
$maxH = 320;
$maxD = int ($cgi_lib'maxdata / 1024);
		#★ クッキーのKEY
$cookey = 'MAP';
		#★ 性別テーブル
@sex = ("男性","女性");
		#★ 県テーブル
@pref = ("海外","北海道","青森県","岩手県","宮城県","秋田県","山形県","福島県",
        "茨城県","栃木県","群馬県","埼玉県","千葉県","東京都","神奈川県",
        "山梨県","長野県","新潟県","富山県","石川県","福井県",
        "岐阜県","静岡県","愛知県","三重県",
        "滋賀県","京都府","大阪府","兵庫県","奈良県","和歌山県",
        "鳥取県","島根県","岡山県","広島県","山口県",
        "香川県","徳島県","愛媛県","高知県",
        "福岡県","佐賀県","長崎県","熊本県","大分県","宮崎県","鹿児島県",
        "沖縄県");
		#★ methodの形式 (POST or GET)
$method = 'POST';
		#★ 管理者用パスワード (英数字で8文字以内）
$password = 'jou';
		#★ 時間の設定
$now = time;
		#★ アクセス拒否するホスト名
@kick = ("*.host.ne.jp","*.host.com");

#********** MAIN **********

&KICK_HOST;
&DECODE;
&IN;
&TIME;
$navi = "■<a href=\"$topurl\">$toptitle</a>";
if ($mode eq "regist") {
	&INPUT;
} elsif ($mode eq "modify") {
	&INPUT;
} elsif ($mode eq "save") {
	&CHK;
	&SAVE;
	&VIEW;
} elsif ($mode eq "search") {
	&SEARCH;
} elsif ($mode eq "list") {
	&LIST;
} elsif ($mode eq "view") {
	&VIEW;
} elsif (($mode eq "modpass")or($mode eq "delpass")) {
	&PASS;
} elsif ($mode eq "delchk") {
	&DELCHK;
} elsif ($mode eq "delete") {
	&DELETE;
} elsif ($mode eq "top") {
	print "Location: $formfile\n\n"; exit;
} else {
	&MAP;
}
exit;

#********** IN SUB **********

sub IN {
	$mode = $in{'mode'}; $id = $in{'id'}; $nickname = $in{'nickname'}; $sex = $in{'sex'};
	$name = $in{'name'}; $yyyy = $in{'yyyy'}; $mm = $in{'mm'}; $dd = $in{'dd'};
	$zip = $in{'zip'}; $pref = $in{'pref'}; $address = $in{'address'};
	$tel = $in{'tel'}; $url = $in{'url'}; $email = $in{'email'};
	$passwd = $in{'passwd'}; $passwdchk = $in{'passwdchk'};
	$delimg = $in{'delimg'}; $filename = $in{'filename'}; $comment = $in{'comment'};

	$performer = $in{'performer'}; $presenter = $in{'presenter'};
	$decorator = $in{'decorator'}; $lecture = $in{'lecture'}; $other = $in{'other'};
	if ($performer eq "yes") { $detail = "パフォーマー"; } else { $detail = ""; };
	if ($presenter eq "yes") {
		if ($detail eq "") {
			 $detail = "プレゼンター";
		} else {
			$detail = "$detail プレゼンター";
		}
	}
	if ($decorator eq "yes") {
		if ($detail eq "") {
			 $detail = "デコレーター";
		} else {
			$detail = "$detail デコレーター";
		}
	}
	if ($lecture eq "yes") {
		if ($detail eq "") {
			 $detail = "バルーン教室";
		} else {
			$detail = "$detail バルーン教室";
		}
	}
	if ($other eq "yes") {
		if ($detail eq "") {
			 $detail = "その他";
		} else {
			$detail = "$detail その他";
		}
	}
}

#********** MAP SUB **********

sub MAP {
	for ($count = 0; $count <= 47; $count++) {
		$map1[$count] = 0;
	}
	open(DB,"$logfile") || &ERROR("オープンできません。 : $logfile");
	@lines = <DB>; close(DB);
	$all = @lines;
	foreach (@lines) {
		($id2,$nickname2,$sex2,$name2,$yyyy2,$mm2,$dd2,$zip2,$pref2,$z) = split(/<>/, $_);
		$map1[$pref2]++;
	}
	for ($count = 0; $count <= 47; $count++) {
		if ($map1[$count] eq 0) {
			$map2[$count] = "map7";
		} elsif ($map1[$count] > 5) {
			$map2[$count] = "map2";
		} else {
			$map2[$count] = "map4";
		}
	}
	&HEADER;
	print <<"_PRINT_";
現在の登録者数は　<span class="pink">$all</span>名　です。
<table class="map" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td colspan="18">　</td>
</tr>
<tr>
<td rowspan="15">　</td>
<td rowspan="3">　</td>
<td class="map7">　</td>
<td class="left" colspan="12">登録なし</td>
<td class="$map2[1]" colspan="2">
<a href="$cgifile?mode=list&pref=1" title="北海道">北海道</a>
</td>
<td rowspan="9">　</td>
</tr>
<tr>
<td class="map4">　</td>
<td class="left" colspan="12">１～５人</td>
<td colspan="2">　</td>
</tr>
<tr>
<td class="map2">　</td>
<td class="left" colspan="12">６人以上</td>
<td class="$map2[2]" colspan="2">
<a href="$cgifile?mode=list&pref=2" title="青森">青森</a>
</td>
</tr>
<tr>
<td colspan="14" rowspan="2">　</td>
<td class="$map2[5]">
<a href="$cgifile?mode=list&pref=5" title="秋田">秋<br>田</a>
</td>
<td class="$map2[3]">
<a href="$cgifile?mode=list&pref=3" title="岩手">岩<br>手</a>
</td>
</tr>
<tr>
<td class="$map2[6]" rowspan="2">
<a href="$cgifile?mode=list&pref=6" title="山形">山<br>形</a>
</td>
<td class="$map2[4]" rowspan="2">
<a href="$cgifile?mode=list&pref=4" title="宮城">宮<br>城</a>
</td>
</tr>
<tr>
<td colspan="10" rowspan="2">　</td>
<td class="$map2[19]" rowspan="2">
<a href="$cgifile?mode=list&pref=19" title="石川">石<br>川</a>
</td>
<td colspan="3">　</td>
</tr>
<tr>
<td class="$map2[18]">
<a href="$cgifile?mode=list&pref=18" title="富山">富山</a>
</td>
<td class="$map2[17]" colspan="2">
<a href="$cgifile?mode=list&pref=17" title="新潟">新潟</a>
</td>
<td class="$map2[7]" colspan="2">
<a href="$cgifile?mode=list&pref=7" title="福島">福島</a>
</td>
</tr>
<tr>
<td colspan="9">　</td>
<td class="$map2[20]" colspan="2">
<a href="$cgifile?mode=list&pref=20" title="福井">福井</a>
</td>
<td class="$map2[21]" rowspan="2">
<a href="$cgifile?mode=list&pref=21" title="岐阜">岐阜</a>
</td>
<td class="$map2[16]" rowspan="2">
<a href="$cgifile?mode=list&pref=16" title="長野">長野</a>
</td>
<td class="$map2[10]">
<a href="$cgifile?mode=list&pref=10" title="群馬">群馬</a>
</td>
<td class="$map2[9]">
<a href="$cgifile?mode=list&pref=9" title="栃木">栃木</a>
</td>
<td class="$map2[8]" rowspan="2">
<a href="$cgifile?mode=list&pref=8" title="茨城">茨<br>城</a>
</td>
</tr>
<tr>
<td colspan="4" rowspan="2">　</td>
<td class="$map2[35]" rowspan="2">
<a href="$cgifile?mode=list&pref=35" title="山口">山<br>口</a>
</td>
<td class="$map2[32]">
<a href="$cgifile?mode=list&pref=32" title="島根">島根</a>
</td>
<td class="$map2[31]">
<a href="$cgifile?mode=list&pref=31" title="鳥取">鳥取</a>
</td>
<td class="$map2[28]" rowspan="2">
<a href="$cgifile?mode=list&pref=28" title="兵庫">兵<br>庫</a>
</td>
<td class="$map2[26]">
<a href="$cgifile?mode=list&pref=26" title="京都">京都</a>
</td>
<td class="$map2[25]" colspan="2">
<a href="$cgifile?mode=list&pref=25" title="滋賀">滋賀</a>
</td>
<td class="$map2[11]" colspan="2">
<a href="$cgifile?mode=list&pref=11" title="埼玉">埼玉</a>
</td>
</tr>
<tr>
<td class="$map2[34]">
<a href="$cgifile?mode=list&pref=34" title="広島">広島</a>
</td>
<td class="$map2[33]">
<a href="$cgifile?mode=list&pref=33" title="岡山">岡山</a>
</td>
<td class="$map2[27]">
<a href="$cgifile?mode=list&pref=27" title="大阪">大<br>阪</a>
</td>
<td class="$map2[29]">
<a href="$cgifile?mode=list&pref=29" title="奈良">奈<br>良</a>
</td>
<td class="$map2[24]" rowspan="2">
<a href="$cgifile?mode=list&pref=24" title="三重">三<br>重</a>
</td>
<td class="$map2[23]">
<a href="$cgifile?mode=list&pref=23" title="愛知">愛知</a>
</td>
<td class="$map2[15]">
<a href="$cgifile?mode=list&pref=15" title="山梨">山梨</a>
</td>
<td class="$map2[13]" colspan="2">
<a href="$cgifile?mode=list&pref=13" title="東京">東京</a>
</td>
<td class="$map2[12]" rowspan="2">
<a href="$cgifile?mode=list&pref=12" title="千葉">千<br>葉</a>
</td>
<td>　</td>
</tr>
<tr>
<td class="$map2[42]" rowspan="2">
<a href="$cgifile?mode=list&pref=42" title="長崎">長<br>崎</a>
</td>
<td class="$map2[41]" rowspan="2">
<a href="$cgifile?mode=list&pref=41" title="佐賀">佐<br>賀</a>
</td>
<td class="$map2[40]" colspan="2">
<a href="$cgifile?mode=list&pref=40" title="福岡">福岡</a>
</td>
<td rowspan="4">　</td>
<td colspan="3">　</td>
<td class="$map2[30]" colspan="2">
<a href="$cgifile?mode=list&pref=30" title="和歌山">和歌山</a>
</td>
<td>　</td>
<td class="$map2[22]">
<a href="$cgifile?mode=list&pref=22" title="静岡">静岡</a>
</td>
<td class="$map2[14]" colspan="2">
<a href="$cgifile?mode=list&pref=14" title="神奈川">神奈川</a>
</td>
<td>　</td>
</tr>
<tr>
<td class="$map2[43]" rowspan="2">
<a href="$cgifile?mode=list&pref=43" title="熊本">熊<br>本</a>
</td>
<td class="$map2[44]">
<a href="$cgifile?mode=list&pref=44" title="大分">大分</a>
</td>
<td class="$map2[38]">
<a href="$cgifile?mode=list&pref=38" title="愛媛">愛媛</a>
</td>
<td class="$map2[36]">
<a href="$cgifile?mode=list&pref=36" title="香川">香川</a>
</td>
<td colspan="10" rowspan="4">　</td>
</tr>
<tr>
<td colspan="2" rowspan="2">　</td>
<td class="$map2[45]">
<a href="$cgifile?mode=list&pref=45" title="宮崎">宮崎</a>
</td>
<td class="$map2[39]">
<a href="$cgifile?mode=list&pref=39" title="高知">高知</a>
</td>
<td class="$map2[37]">
<a href="$cgifile?mode=list&pref=37" title="徳島">徳島</a>
</td>
</tr>
<tr>
<td class="$map2[46]" colspan="2">
<a href="$cgifile?mode=list&pref=46" title="鹿児島">鹿児島</a>
</td>
<td colspan="2">　</td>
</tr>
<tr>
<td colspan="2">　<br></td>
<td colspan="14">　</td>
</tr>
<tr>
<td>　</td>
<td class="$map2[47]" colspan="2">
<a href="$cgifile?mode=list&pref=47" title="沖縄">沖縄</a>
</td>
<td colspan="12">　</td>
<td class="$map2[0]" colspan="2">
<a href="$cgifile?mode=list&pref=0" title="海外">海外</a>
</td>
<td>　</td>
</tr>
<tr>
<td colspan="18">　</td>
</tr>
</table>
<br>
ツイストバルーンが好きな方ならレベルに関係なくどなたでも登録できます。<br>
必須項目を入力するだけでも登録できるので、気楽に登録して下さい。<br>
そして、バルーン仲間をどんどん増やしましょう。<br>
<br>
<div class="button">
<div id="button1">
<a href="$cgifile?mode=regist" title="登録">登　録</a>
</div>
<div id="button2">
<a href="$cgifile?mode=search" title="検索">検　索</a>
</div>
</div>
<br>
_PRINT_
	&FOOTER;
}

#********** INPUT SUB **********

sub INPUT {
	#変更の場合のデータを取得する
	if (($id ne "")&&($mode eq "modify")) {
		if ($passwd eq "") { &ERROR("パスワードが入力されていません。"); }
		open(DB,"$logfile")|| &ERROR("オープンできません。 : $logfile");
		seek(DB,0,0); @line=<DB>; close(DB);
		$flag = 0;
		foreach $line(@line) {
			($id2,$nickname2,$sex2,$name2,$yyyy2,$mm2,$dd2,$zip2,$pref2,$address2,$tel2,$url2,$email2,$performer2,$presenter2,$decorator2,$lecture2,$other2,$detail2,$comment2,$tail2,$w2,$h2,$passwd2,$host2,$addr2,$agent2,$date2,$time2,$z) = split(/<>/, $line);
			if ($id eq $id2){
				if ((&MATCH_PASS("$passwd","$passwd2") ne 'OK')&&($passwd ne $password)) { &ERROR("パスワードが間違っています。");}
				$flag = 1;
				$comment3 = $comment2;
				$comment3 =~s/<br>/\n/g;
				last;
			}
		}
		if ($flag eq 0) { &ERROR("該当するデータが見つかりません。"); }
		&SAVE_COOKIE;
		$label = "変更";
	} else {
		$id2=$nickname2=$sex2=$name2=$yyyy2=$mm2=$dd2=$zip2=$pref2=$address2=$tel2=$url2=$email2=$performer2=$presenter2=$decorator2=$lecture2=$other2=$detail2=$comment2=$tail2=$w2=$h2=$passwd2="";
		$label = "登録";
	}
	&HEADER;
	print <<"_PRINT_";
<form method="$method" action="$cgifile" enctype="multipart/form-data">
<table class="map" cellspacing="0" cellpadding="5">
<caption>$labelフォーム</caption>
<tbody>
<tr>
<td class="input" nowrap>
ニックネーム（本名可）
</td>
<td class="input2" nowrap>
<input name="nickname" size="40" value="$nickname2"><span class="red">（必須）</span>
</td>
</tr>
<tr>
<td class="input" nowrap>
性別
</td>
<td class="input2" nowrap>
_PRINT_
	if ($sex2 eq 0) {
		print "<input type=\"radio\" name=\"sex\" value=\"0\" checked>男　<input type=\"radio\" name=\"sex\" value=\"1\">女";
	} elsif ($sex2 eq 1) {
		print "<input type=\"radio\" name=\"sex\" value=\"0\">男　<input type=\"radio\" name=\"sex\" value=\"1\" checked>女";
	} else {
		print "<input type=\"radio\" name=\"sex\" value=\"0\">男　<input type=\"radio\" name=\"sex\" value=\"1\">女";
	}
	print <<"_PRINT_";
　<span class="red">（必須）</span>
</td>
</tr>
<tr>
<td class="input" nowrap>
本名
</td>
<td class="input2" nowrap>
<input name="name" size="40" value="$name2">
</td>
</tr>
_PRINT_

	if (($mode eq "modify")&&(-e "./images/$id2$tail2")&&($tail2 eq ".gif" || $tail2 eq ".jpg" || $tail2 eq ".png")) {
		if ($w2 eq 0 || $w2 eq "") { $w2 = $maxW; }
		if ($h2 eq 0 || $h2 eq "") { $h2 = $maxH; }
		print <<"_PRINT_";
<tr>
<td class="input" nowrap rowspan="2">
画　像
</td>
<td class="input2">
<img src="./images/$id2$tail2" width=$w2 height=$h2><br>
<input type="checkbox" name="delimg" value="yes">画像を削除
</td>
</tr>
<tr>
<td class="input2">
<input type="file" name="filename" size="60">
</td>
</tr>
<tr>
<td nowrap>　</td>
<td class="left" nowrap>
 ↑変更する場合のみご記入下さい。
</td>
</tr>
_PRINT_
	} else {
		print <<"_PRINT_";
<tr>
<td class="input" nowrap>
画　像
</td>
<td class="input2">
<input type="file" name="filename" size="60">
</td>
</tr>
_PRINT_
	}
	print <<"_PRINT_";
<tr>
<td class="input" nowrap>
誕生日
</td>
<td class="input2" nowrap>
<select name="yyyy">
_PRINT_
	if ($yyyy2 >= 1916 and $yyyy2 <= 2000) {
		print "<option value=\"\">----\n";
	} else {
		print "<option value=\"\" selected>----\n";
	}
	for ($count = 1916; $count <= 2000; $count++) {
		if ($yyyy2 eq $count) {
			print "<option value=\"$count\" selected>$count\n";
		} else {
			print "<option value=\"$count\">$count\n";
		}
	}
	print "</select>年　\n";
	print "<select name=\"mm\">\n";
	if ($mm2 >= 1 and $mm2 <= 12) {
		print "<option value=\"\">--\n";
	} else {
		print "<option value=\"\" selected>--\n";
	}
	for ($count = 1; $count <= 12; $count++) {
		if ($mm2 eq $count) {
			print "<option value=\"$count\" selected>$count\n";
		} else {
			print "<option value=\"$count\">$count\n";
		}
	}
	print "</select>月　\n";
	print "<select name=\"dd\">\n";
	if ($dd2 >= 1 and $dd2 <= 31) {
		print "<option value=\"\">--\n";
	} else {
		print "<option value=\"\" selected>--\n";
	}
	for ($count = 1; $count <= 31; $count++) {
		if ($dd2 eq $count) {
			print "<option value=\"$count\" selected>$count\n";
		} else {
			print "<option value=\"$count\">$count\n";
		}
	}
	print <<"_PRINT_";
</select>日
</td>
</tr>
<tr>
<td class="input" nowrap>
〒
</td>
<td class="input2" nowrap>
<input name="zip" size="10" value="$zip2">
</td>
</tr>
<tr>
<td class="input" nowrap>
県
</td>
<td class="input2" nowrap>
<select size="1" name="pref">
_PRINT_
	for ($count = 1; $count <= 47; $count++) {
		if ($pref2 eq $count) {
			print "<option value=\"$count\" selected>$pref[$count]\n";
		} else {
			print "<option value=\"$count\">$pref[$count]\n";
		}
	}
	if ($pref2 eq 0) {
		print "<option value=\"0\" selected>$pref[0]\n";
	} else {
		print "<option value=\"0\">$pref[0]\n";
	}
	print <<"_PRINT_";
</select>
<span class="red">（必須）</span>
</td>
</tr>
<tr>
<td class="input" nowrap>
住所
</td>
<td class="input2" nowrap>
<input name="address" size="70" value="$address2">
</td>
</tr>
<tr>
<td class="input" nowrap>
電話番号
</td>
<td class="input2" nowrap>
<input name="tel" size="18" value="$tel2">
</td>
</tr>
<tr>
<td class="input" nowrap>
ＵＲＬ
</td>
<td class="input2" nowrap>
<input name="url" size="60" value="http://$url2"><br>
 ↑半角英数字で正しくご記入下さい。
</td>
</tr>
<tr>
<td class="input" nowrap>
E-mail
</td>
<td class="input2" nowrap>
<input name="email" size="60" value="$email2"><br>
 ↑半角英数字で正しくご記入下さい。
</td>
</tr>
<tr>
<td class="input" nowrap>
活動内容
</td>
<td class="input2" nowrap>
_PRINT_
	if ($performer2 eq "yes") {
		print "<input type=\"checkbox\" name=\"performer\" value=\"yes\" checked>パフォーマー　　\n";
	} else {
		print "<input type=\"checkbox\" name=\"performer\" value=\"yes\">パフォーマー　　\n";
	}
	if ($presenter2 eq "yes") {
		print "<input type=\"checkbox\" name=\"presenter\" value=\"yes\" checked>プレゼンター　　\n";
	} else {
		print "<input type=\"checkbox\" name=\"presenter\" value=\"yes\">プレゼンター　　\n";
	}
	if ($decorator2 eq "yes") {
		print "<input type=\"checkbox\" name=\"decorator\" value=\"yes\" checked>デコレーター<br>\n";
	} else {
		print "<input type=\"checkbox\" name=\"decorator\" value=\"yes\">デコレーター<br>\n";
	}
	if ($lecture2 eq "yes") {
		print "<input type=\"checkbox\" name=\"lecture\" value=\"yes\" checked>バルーン教室　　\n";
	} else {
		print "<input type=\"checkbox\" name=\"lecture\" value=\"yes\">バルーン教室　　\n";
	}
	if ($other2 eq "yes") {
		print "<input type=\"checkbox\" name=\"other\" value=\"yes\" checked>趣味・その他\n";
	} else {
		print "<input type=\"checkbox\" name=\"other\" value=\"yes\">趣味・その他\n";
	}
	print <<"_PRINT_";
<br>
  ↑該当する活動内容にチェックを付けて下さい。<span class="red">（必須）</span>
</td>
</tr>
<tr>
<td class="input" nowrap>
ＰＲ
</td>
<td class="input2" nowrap>
<TEXTAREA cols="60" name="comment" rows="5">$comment3</TEXTAREA><br>
  ↑自己紹介、宣伝、その他何かございましたらお書き下さい。
</td>
</tr>
<tr>
<td class="input" nowrap>
パスワード
</td>
<td class="input2" nowrap>
<input type="password" name="passwd" size="16" maxlength="8">
（英数字で8文字以内）<span class="red">（必須）</span>
</td>
</tr>
<tr>
<td class="input" nowrap>
パスワード（確認用）
</td>
<td class="input2" nowrap>
<input type="password" name="passwdchk" size="16" maxlength="8">
（上と同じパスワードを入力）<span class="red">（必須）</span>
</td>
</tr>
<tr>
<td class="left" colspan="2">
<ul>
<li>タグは使用できません。</li>
<li>半角ｶﾀｶﾅは使用できません。</li>
<li>画像は横 $maxWピクセル、縦 $maxHピクセルを超えると縮小表\示されます。</li>
<li>$maxD KBを超える画像はアップロードできません。</li>
</ul>
<br>
<span class="red">＜必ずお読み下さい＞</span>
<ul>
<li>すべての項目を入力する必要はありません。</li>
<li>本フォームより入力いただいた個人情報はパスワード以外全て公開されます。<br>
公開したくない項目については<span class="red">入力しない</span>ようお願いします。</li>
<li>入力いただいた個人情報については、この$title以外で利用することはありません。</li>
<li>バルーン関係の場合のみ、商用利用・営利目的の登録、大歓迎です。<br>
ショップをお持ちの方、仕事の依頼をお受けしている方どんどん宣伝してください。</li>
<li>パスワードは忘れないようお願いします。こちらから教えることは致しません。</li>
</ul>
</td>
</tr>
</table>
<br>
<input type="hidden" name="mode" value="save">
<input type="hidden" name="id" value="$id2">
<input type="submit" value="　登録　">　
<input type="button" value="　戻る　" onClick="history.go(-1)">
</form>
_PRINT_
	&FOOTER;
}

#********** CHK SUB **********

sub CHK {
	if ($nickname eq "") { &ERROR("名前が入力されていません。"); }
	if ($sex eq "") { &ERROR("性別が選択されていません。"); }
	if ($pref eq "") { &ERROR("都道府県が選択されていません。"); }
	if (($email ne "")&&($email !~ /(.*)\@(.*)\.(.*)/)) { &ERROR("メールアドレスの入力が不正です。"); }
	if ($detail eq "") { &ERROR("活動内容が選択されていません。"); }
	if (length($name) > 20) { &ERROR("名前が長すぎます。20文字以内で入力して下さい。"); }
	if (length($nickname) > 20) { &ERROR("ニックネームが長すぎます。20文字以内で入力して下さい。"); }
	if (length($email) > 50) { &ERROR("メールアドレスが長すぎます。50文字以内で入力して下さい。"); }
	if ($passwd eq "") { &ERROR("パスワードが設定されていません。"); }
	if ($passwd ne $passwdchk) { &ERROR("パスワードが合っていません。"); }
}

#********** SAVE SUB **********

sub SAVE {
	$comment =~ s/\r\n/<br>/g;
	$comment =~ s/\r/<br>/g;
	$comment =~ s/\n/<br>/g;
	$url =~ s/^http\:\/\///;
	$agent = &GET_AGENT;
	&LOCK;
	open(DB,"$logfile") || &ERROR("オープンできません。 : $logfile");
	@lines = <DB>; close(DB);
	($id2,$nickname2,$sex2,$name2,$yyyy2,$mm2,$dd2,$zip2,$pref2,$address2,$tel2,$url2,$email2,$performer2,$presenter2,$decorator2,$lecture2,$other2,$detail2,$comment2,$tail2,$w2,$h2,$passwd2,$host2,$addr2,$agent2,$date2,$time2,$z) = split(/<>/, @lines[0]);
	#if (($host2 eq $host)&&($time2+300 > $now)) {
	#	&ERROR("５分以内の連続\申\し込み処理は禁止です");
	#}
	if ($id eq "") {
		$id = $now;
		if ($id eq $id2) { &ERROR("登録に失敗しました。もう一度やり直してください。"); }
	} elsif (($filename)or($delimg eq "yes")) {
		if (-e "./images/$id\.jpg") { unlink("./images/$id\.jpg"); }
		if (-e "./images/$id\.png") { unlink("./images/$id\.png"); }
		if (-e "./images/$id\.gif") { unlink("./images/$id\.gif"); }
		$tail=$w=$h="";
	}
	if ($filename) { &UPLOAD; }
	$passwd3 = &MAKE_PASS($passwd);
	local($flag) = 0;
	foreach (@lines) {
		($id2,$nickname2,$sex2,$name2,$yyyy2,$mm2,$dd2,$zip2,$pref2,$address2,$tel2,$url2,$email2,$performer2,$presenter2,$decorator2,$lecture2,$other2,$detail2,$comment2,$tail2,$w2,$h2,$passwd2,$host2,$addr2,$agent2,$date2,$time2,$z) = split(/<>/, $_);
		if ($id eq $id2) {
			&LOAD_COOKIE;
			if (($c_id ne $id2)or($c_passwd ne $passwd2)) {
				&ERROR("パスワードが正しくありません。");
			}
			$flag = 1;
			if (($filename eq "")and($delimg ne "yes")and($tail2 ne "")) {
				$tail = $tail2;
				$w = $w2;
				$h = $h2;
			}
			push (@lines2,"$id<>$nickname<>$sex<>$name<>$yyyy<>$mm<>$dd<>$zip<>$pref<>$address<>$tel<>$url<>$email<>$performer<>$presenter<>$decorator<>$lecture<>$other<>$detail<>$comment<>$tail<>$w<>$h<>$passwd3<>$host<>$addr<>$agent<>$date<>$now<>\n");
		} else {
			push (@lines2,$_);
		}
	}
	if ($flag eq 0) {
		push (@lines2,"$id<>$nickname<>$sex<>$name<>$yyyy<>$mm<>$dd<>$zip<>$pref<>$address<>$tel<>$url<>$email<>$performer<>$presenter<>$decorator<>$lecture<>$other<>$detail<>$comment<>$tail<>$w<>$h<>$passwd3<>$host<>$addr<>$agent<>$date<>$now<>\n");
	}
	open(DB,">$logfile") || &ERROR("書き込みできません。 : $logfile");
	print DB @lines2; close(DB);

	&UNLOCK;

	#&OK("登録手続きが無事終了しました。");
}

#********** UPLOAD SUB **********

sub UPLOAD {
	local($macbin, $fname, $flag, $w2, $h2, $rate);

	$macbin=0;
	foreach (@in) {
		if (/(.*)Content-type:(.*)/i) { $tail=$2; }
		if (/(.*)filename=\"(.*)\"/i) { $fname=$2; }
		if (/application\/x-macbinary/i) { $macbin=1; }
	}
	$tail =~ s/\r//g;
	$tail =~ s/\n//g;

	$flag=0;
	if ($tail =~ /image\/gif/i) { $tail=".gif"; $flag=1; }
	if ($tail =~ /image\/p?jpeg/i) { $tail=".jpg"; $flag=1; }
	if ($tail =~ /image\/x-png/i) { $tail=".png"; $flag=1; }
	if (!$flag) {
		if ($fname =~ /\.gif$/i) { $tail=".gif"; $flag=1; }
		if ($fname =~ /\.jpe?g$/i) { $tail=".jpg"; $flag=1; }
		if ($fname =~ /\.png$/i) { $tail=".png"; $flag=1; }
	}
	if (!$flag || !$fname) {
		&ERROR("画像のアップロードに失敗しました。");
	}
	if ($macbin) {
		$length = substr($filename,83,4);
		$length = unpack("%N",$length);
		$filename = substr($filename,128,$length);
	}
	$imgfile = "images/$id$tail";
	if (!open(OUT,"> $imgfile")) {
		&ERROR("画像のアップロードに失敗しました。");
	}
	binmode(OUT);
	binmode(STDOUT);
	print OUT $filename;
	close(OUT);
	chmod(0666,$imgfile);
	($w, $h) = &SIZEGET($imgfile);
	if ($w > $maxW || $h > $maxH) {
		$w2 = $maxW / $w;
		$h2 = $maxH / $h;
		if ($w2 < $h2) { $rate = $w2; } else { $rate = $h2; }
		$w = int($w * $rate);
		if ($w < 1) { $w = 1; }
		$h = int($h * $rate);
		if ($h < 1) { $h = 1; }
	}
}

#********** SEARCH SUB **********

sub SEARCH {
	&HEADER;
	print <<"_PRINT_";
<form method="GET" action="$cgifile">
<table class="map" cellspacing="0" cellpadding="5">
<tbody>
<caption>検索フォーム</caption>
<tr>
<td class="input" nowrap>
ニックネーム
</td>
<td class="input2" nowrap>
<input name="nickname" size="40" value="$nickname2">（あいまい検索可）
</td>
</tr>
<tr>
<td class="input" nowrap>
性別
</td>
<td class="input2" nowrap>
<select size="1" name="sex">
<option value="" selected>選択なし
<option value="0">$sex[0]
<option value="1">$sex[1]
</select>
</td>
</tr>
<tr>
<td class="input" nowrap>
県
</td>
<td class="input2" nowrap>
<select size="1" name="pref">
<option value="" selected>選択なし
_PRINT_
	for ($count = 1; $count <= 47; $count++) {
		print "<option value=\"$count\">$pref[$count]\n";
	}
	print <<"_PRINT_";
<option value="0">$pref[0]
</select>
</td>
</tr>
<tr>
<td class="input" nowrap>
活動内容
</td>
<td class="input2" nowrap>
<select size="1" name="detail">
<option value="" selected>選択なし
<option value="1">パフォーマー
<option value="2">プレゼンター
<option value="3">デコレーター
<option value="4">バルーン教室
<option value="5">趣味・その他
</select>
</td>
</tr>
</table>
<br>
<input type="hidden" name="mode" value="list">
<input type="submit" value="　検索　">　
<input type="button" value="　戻る　" onClick="history.go(-1)">
</form>
_PRINT_
	&FOOTER;
}

#********** VIEW SUB **********

sub VIEW{
	if ($id eq "") { &ERROR("データが見つかりませんでした。"); }
	open(DB,"$logfile") || &ERROR("オープンできません。 : $logfile");
	@lines = <DB>; close(DB);
	local($flag) = 0;
	foreach (@lines) {
		($id2,$nickname2,$sex2,$name2,$yyyy2,$mm2,$dd2,$zip2,$pref2,$address2,$tel2,$url2,$email2,$performer2,$presenter2,$decorator2,$lecture2,$other2,$detail2,$comment2,$tail2,$w2,$h2,$passwd2,$host2,$addr2,$agent2,$date2,$time2,$z) = split(/<>/, $_);
		if ($id eq $id2) {
			$flag = 1;
			last;
		}
	}
	if ($flag eq 0) { &ERROR("データが見つかりませんでした。"); }
	&HEADER;
	if ($mode eq "save") {
		print <<"_PRINT_";
<br>
以下の内容で登録しました。<br>
<br>
_PRINT_
	}
	print <<"_PRINT_";
<table class="line" cellpadding="5" cellspacing="0" width="700">
<tbody>
<caption>$nickname2</caption>
<tr>
<td class="detail" nowrap width="130">
ニックネーム
</td>
<td class="left" width="230">
$nickname2
</td>
<td class="pic" nowrap width="240" rowspan="7">
_PRINT_
	if ((-e "./images/$id2$tail2")&&($tail2 eq ".gif" || $tail2 eq ".jpg" || $tail2 eq ".png")) {
		if ($w2 eq 0 || $w2 eq "") { $w2 = $maxW; }
		if ($h2 eq 0 || $h2 eq "") { $h2 = $maxH; }
		print "<img src=\"./images/$id2$tail2\" width=$w2 height=$h2>\n";
	} else {
		print "　\n";
	}
	print <<"_PRINT_";
</td>
</tr>
<tr>
<td class="detail" nowrap>
性別
</td>
<td class="left" nowrap>
$sex[$sex2]
</td>
</tr>
<tr>
<td class="detail" nowrap>
本名
</td>
<td class="left">
$name2　
</td>
</tr>
<tr>
<td class="detail" nowrap>
誕生日
</td>
<td class="left" nowrap>
_PRINT_
	$ymd = "";
	if ($yyyy2 ne "") { $ymd = ("$yyyy2" . "年　"); }
	if ($mm2 ne "") { $ymd = ("$ymd" . "$mm2" . "月　"); }
	if ($dd2 ne "") { $ymd = ("$ymd" . "$dd2" . "日　"); }
	if ($ymd eq "") { $ymd = "　"; }
	print <<"_PRINT_";
$ymd
</td>
</tr>
<tr>
<td class="detail" nowrap>
〒
</td>
<td class="left">
$zip2　
</td>
</tr>
<tr>
<td class="detail">
住所
</td>
<td class="left">
$pref[$pref2]　
$address2　
</td>
</tr>
<tr>
<td class="detail" nowrap>
電話番号
</td>
<td class="left">
$tel2　
</td>
</tr>
<tr>
<td class="detail" nowrap>
ＵＲＬ
</td>
<td class="left" colspan="2">
_PRINT_
	if ($url2 ne "") {
		print "<a href=\"http://$url2\" target=\"_blank\">http://$url2</a>";
	} else {
		print "　";
	}
	print <<"_PRINT_";
</td>
</tr>
<tr>
<td class="detail" nowrap>
E-mail
</td>
<td class="left" colspan="2">
_PRINT_
	if ($email2 ne "") {
		print "<a href=\"mailto:$email2\">$email2</a>";
	} else {
		print "　";
	}
	print <<"_PRINT_";
</td>
</tr>
<tr>
<td class="detail" nowrap>
活動内容
</td>
<td class="left" colspan="2">
$detail2　
</td>
</tr>
<tr>
<td class="detail" nowrap>
ＰＲ
</td>
<td class="left" colspan="2">
$comment2　
</td>
</tr>
<tr>
<td class="right" colspan="3">
[<a href="$cgifile?mode=modpass&id=$id2" title="変更">変更</a>]　
[<a href="$cgifile?mode=delpass&id=$id2" title="削除">削除</a>]　
</td>
</tr>
</table>
_PRINT_
	if ($mode eq "save") {
		print <<"_PRINT_";
<form method="post" action="$cgifile">
<input type="hidden" name="mode" VALUE="top">
<input type="submit" VALUE="　ＯＫ　">
</form>
_PRINT_
	} else {
		print <<"_PRINT_";
<form>
<input type="button" value="　戻る　" onClick="history.go(-1)">
</form>
_PRINT_
	}
	&FOOTER;
	exit;
}

#********** LIST SUB **********

sub LIST{
	open(DB,"$logfile") || &ERROR("オープンできません。 : $logfile");
	@lines = <DB>; close(DB);
	local($flag) = 0;

	&HEADER;
	foreach (@lines) {
		($id2,$nickname2,$sex2,$name2,$yyyy2,$mm2,$dd2,$zip2,$pref2,$address2,$tel2,$url2,$email2,$performer2,$presenter2,$decorator2,$lecture2,$other2,$detail2,$comment2,$tail2,$w2,$h2,$passwd2,$host2,$addr2,$agent2,$date2,$time2,$z) = split(/<>/, $_);
		if (($nickname eq "") or ($nickname2 =~ /$nickname/)) {
			if (($pref eq "") or ($pref2 eq $pref)) {
				if (($sex eq "") or ($sex2 eq $sex)) {
					if (($detail eq "")
					  or (($detail eq 1) and ($performer2 eq "yes"))
                                          or (($detail eq 2) and ($presenter2 eq "yes"))
                                          or (($detail eq 3) and ($decorator2 eq "yes"))
                                          or (($detail eq 4) and ($lecture2 eq "yes"))
                                          or (($detail eq 5) and ($other2 eq "yes"))) {
						&DETAIL;
						$flag = 1;
					}
				}
			}
		}
	}
	if ($flag eq 0) {
		print "<br>\n該当の登録がありません<br><br>\n";
	}
	print "<form>\n";
	print "<input type=\"button\" value=\"　戻る　\" onClick=\"history.go(-1)\">\n";
	print "</form>\n";
	&FOOTER;
	exit;
}

#********** DETAIL SUB **********

sub DETAIL {
	$comment2 = &ALINK($comment2);
print <<"_PRINT_";
<br>
<table class="line" cellspacing="0" cellpadding="5" width="600">
<tr>
<td class="detail">
ニックネーム
</td>
<td class="left">
$nickname2
</td>
<td class="detail">
性別
</td>
<td class="left">
$sex[$sex2]
</td>
<td class="detail">
出身県
</td>
<td class="left">
$pref[$pref2]
</td>
<td>
[ <a href="$cgifile?mode=view&id=$id2">詳細</a> ]
</td>
</tr>
<tr>
<td class="detail">
活動内容
</td>
<td class="left" colspan="6">
$detail2
</td>
</tr>
</table>
_PRINT_
}

#********** PASS SUB **********

sub PASS {
	if ($id eq "") { &ERROR("該当するデータが見つかりません。"); }
	&HEADER;
	print <<"_PRINT_";
<form method="$method" action="$cgifile">
<table class="line" cellpadding="20" cellspacing="0">
<tbody>
<caption>パスワードの入力</caption>
<tr>
<td nowrap>
パスワード：
<input type="password" name="passwd" size="16" maxlength="8">
</td>
</tr>
</table>
<br>
_PRINT_
	if ($mode eq "modpass") {
		print "<input type=\"hidden\" name=\"mode\" value=\"modify\">";
	} else {
		print "<input type=\"hidden\" name=\"mode\" value=\"delchk\">";
	}
print <<"_PRINT_";
<input type="hidden" name="id" value="$id">
<input type="submit" value="　確認　">　
<input type="button" value="　戻る　" onClick="history.go(-1)">
</form>
_PRINT_
	&FOOTER;
}

#********** DELCHK SUB **********

sub DELCHK {
	if ($passwd eq "") { &ERROR("パスワードが入力されていません。"); }
	if ($id eq "") { &ERROR("該当するデータが見つかりません。"); }
	open(DB,"$logfile")|| &ERROR("オープンできません。 : $logfile");
	seek(DB,0,0); @line=<DB>; close(DB);
	$flag = 0;
	foreach $line(@line) {
		($id2,$nickname2,$sex2,$name2,$yyyy2,$mm2,$dd2,$zip2,$pref2,$address2,$tel2,$url2,$email2,$performer2,$presenter2,$decorator2,$lecture2,$other2,$detail2,$comment2,$tail2,$w2,$h2,$passwd2,$host2,$addr2,$agent2,$date2,$time2,$z) = split(/<>/, $line);
		if ($id eq $id2){
			$flag = 1;
			if ((&MATCH_PASS("$passwd","$passwd2") ne 'OK')&&($passwd ne $password)) { &ERROR("パスワードが間違っています。");}
			last;
		}
	}
	if ($flag eq 0) { &ERROR("該当するデータが見つかりません。"); }
	&SAVE_COOKIE;
	&HEADER;
print <<"_PRINT_";
<hr>
<br><br>
$nickname2 のデータを削除します。<br>
<br>
よろしいですか？
<form method="post" action="$cgifile">
<input type="hidden" name="mode" VALUE="delete">
<input type="hidden" name="id" VALUE="$id">
<input type="submit" VALUE="　削除　">　
<input type="button" value="　戻る　" onClick="history.go(-1)">
</form>
_PRINT_
	&FOOTER;
}

#********** DELETE SUB **********

sub DELETE {
	&LOCK;
	open(DB,"$logfile") || &ERROR("オープンできません。 : $logfile");
	@lines = <DB>; close(DB);
	if ($id eq "") { &ERROR("該当のデータが見つかりません。"); }
	if (-e "./images/$id\.jpg") { unlink("./images/$id\.jpg"); }
	if (-e "./images/$id\.png") { unlink("./images/$id\.png"); }
	if (-e "./images/$id\.gif") { unlink("./images/$id\.gif"); }
	local($flag) = 0;
	foreach (@lines) {
		($id2,$nickname2,$sex2,$name2,$yyyy2,$mm2,$dd2,$zip2,$pref2,$address2,$tel2,$url2,$email2,$performer2,$presenter2,$decorator2,$lecture2,$other2,$detail2,$comment2,$tail2,$w2,$h2,$passwd2,$host2,$addr2,$agent2,$date2,$time2,$z) = split(/<>/, $_);
		if ($id eq $id2) {
			&LOAD_COOKIE;
			if (($c_id ne $id2)or($c_passwd ne $passwd2)) {
				&ERROR("パスワードが正しくありません。");
			}
			$flag = 1;
		} else {
			push (@lines2,$_);
		}
	}
	if ($flag eq 0) { &ERROR("該当のデータが見つかりません。"); }
	open(DB,">$logfile") || &ERROR("書き込みできません。 : $logfile");
	print DB @lines2; close(DB);

	&UNLOCK;

	&OK("削除手続きが無事終了しました。");
}

#********** OK SUB **********

sub OK{
	&HEADER;
print <<"_PRINT_";
<hr>
<p>$_[0]</p>
<form method="post" action="$cgifile">
<input type="hidden" name="mode" VALUE="top">
<input type="submit" VALUE="　ＯＫ　">
</form>
_PRINT_
	&FOOTER;
	exit;
}


#********** ERROR SUB **********

sub ERROR {
	&UNLOCK($lockfile);
	&HEADER;
print <<"_PRINT_";
<hr>
<p class="red">エラー！！</p>
<p>$_[0]</p>
<form>
<input type="button" value="　戻る　" onClick="history.go(-1)">
</form>
_PRINT_
	&FOOTER;
	exit;
}

#********** HEADER SUB **********

sub HEADER {
	print "Content-type: text/html\n\n";
print <<"_PRINT_";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html lang="ja">
<head>
<title>$toptitle</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta name="keywords" content="balloon,バルーン,風船,ツイスト">
<meta name="description" content="$toptitle">
<link rel="stylesheet" type="text/css" href="./style.css" media="all">
<link rel="contents" href="index.html" title="メイン">
</head>
<body>
<a name="top"></a>
<center>
<div class="navi">
<a href="$topurl"><img src="$totoppic" alt="" border="0"></a>
</div>
<div class="title">
<a href="$formfile"><img src="$toppic" alt="" border="0"></a>
</div>
<div class="contents">
_PRINT_
}

#********** FOOTER SUB **********

sub FOOTER {
print <<"_PRINT_";
<hr>
<p class="footer">
<table class="banner" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="left">
当サイトはリンクフリーです。<br>
（バナーはご自由にお使いください）
</td>
<td class="pic">
<img src="$bannerpic" alt="">
</td>
</tr>
<tr>
<td class="left" colspan="2">
リンクはトップページにお願い致します。　<a href="$topurl">$topurl</a><br>
当サイトの著作権はツイスターズ$holdyy実行委員会が所有します。<br>
掲載作品写真の著作権は作者が所有します。<br>
直リンク・無断転載は禁止です。
</td>
</tr>
</tbody>
</table>
</p>
<p class="copyright">
Script by <span class="blue">SayQoo</span>
</p>
</div>
<address>
(C)Japan Balloon Twisters Convention $holdyy All Rights Reserved.
</address>
</center>
</body>
</html>
_PRINT_
}

#********** DECODE SUB **********

sub DECODE {
	local($name,$value);
	&ReadParse;
	while (($name,$value) = each(%in)) {
		if ($name ne "filename") {
			&jcode'convert(*value,'sjis');
			$value =~ s/</&lt;/g;
			$value =~ s/>/&gt;/g;
		}
		$in{$name} = $value;
	}
}

#********** LOAD_COOKIE SUB **********

sub LOAD_COOKIE {
	$cookie = $ENV{'HTTP_COOKIE'};
	@pairs = split(/;/, $cookie);
	foreach $pair (@pairs) {
		local($key,$value) = split(/=/, $pair);
		$key =~ s/\s//g;
		$DUMMY{$key} = $value;
	}
	@pairs = split(/,/, $DUMMY{$cookey});
	foreach $pair (@pairs) {
		local($key,$value) = split(/:/, $pair);
		$COOKIE{$key} = $value;
	}
	$c_id  = $COOKIE{'id'};
	$c_passwd  = $COOKIE{'passwd'};
}

#********** SAVE_COOKIE SUB **********

sub SAVE_COOKIE {
	#保存日数は1日間
	($secg,$ming,$hourg,$mdayg,$mong,$yearg,$wdayg,$ydayg,$isdstg) = gmtime(time + 1*24*60*60);
	@mong  = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	@weekg = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
	$date_gmt = sprintf("%s, %02d\-%s\-%04d %02d\:%02d\:%02d GMT",$weekg[$wdayg],$mdayg,$mong[$mong],$yearg+1900,$hourg,$ming,$secg);
	$cook = "id\:$id2\,passwd\:$passwd2";
	print "Set-Cookie: $cookey\=$cook; expires=$date_gmt\n";
}

#********** TIME SUB **********

sub TIME {
	$ENV{'TZ'} = "JST-9";
	$now = time;
	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	$year += 1900; $mon++;
	$week = ('日','月','火','水','木','金','土') [$wday];
	$japandate = "$year年$mon月$mday日($week曜日) $hour時$min分";
	if ($mon  < 10) { $mon  = "0$mon";  }
	if ($mday < 10) { $mday = "0$mday"; }
	if ($hour < 10) { $hour = "0$hour"; }
	if ($min  < 10) { $min  = "0$min";  }
	$week = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat') [$wday];
	$date = "$year\/$mon\/$mday($week) $hour\:$min";
}

#********** SIZEGET SUB **********

sub SIZEGET {
	local($fname, $d, $m, $c, $l, $w, $h);

	$fname = "@_";

	if ($fname =~ /\.jpg$/i) {
		open(JPEG, "$fname") || return (0,0);
		binmode JPEG;
		read(JPEG, $d, 2);
		while (1) {
			read(JPEG, $d, 4);
			($m, $c, $l) = unpack("a a n", $d);

			if ($m ne "\xFF") {
				$w = $h = 0; last;
			} elsif ((ord($c) >= 0xC0) && (ord($c) <= 0xC3)) {
				read(JPEG, $d, 5);
				($h, $w) = unpack("xnn", $d);
				last;
			} else {
				read(JPEG, $d, ($l - 2));
			}
		}
		close(JPEG);
		return ($w, $h);
	} elsif ($fname =~ /\.gif$/i) {
		open(GIF,"$fname") || return (0,0);
		binmode(GIF);
		sysread(GIF,$d,10);
		close(GIF);
		if ($d =~ /^GIF/) { $d = substr($d,-4); }
		$w = unpack("v",substr($d,0,2));
		$h = unpack("v",substr($d,2,2));
		return ($w, $h);
	} elsif ($fname =~ /\.png$/i) {
		open(PNG, "$fname") || return (0,0);
		binmode(PNG);
		read(PNG, $d, 24);
		close(PNG);
		$w = unpack("N", substr($d, 16, 20));
		$h = unpack("N", substr($d, 20, 24));
		return ($w, $h);
	}
}

#********** LOCK SUB **********

sub LOCK {
	$c = 0;
	while(-f "$lockfile") {
		$c++;
		if ($c >= 20) { unlink($lockfile); &ERROR('BUSY');}
		sleep(1);
	}
	open(LOCK,">$lockfile");
	close(LOCK);
}

#********** UNLOCK SUB **********

sub UNLOCK {
	if (-e $lockfile) { unlink($lockfile); }
}

#********** ALINK SUB **********

sub ALINK {
	local($text) = $_[0];
	&jcode'convert(*text,'sjis');
	$text =~ s/(http:\/\/[\w\.\~\-\/\?\&\+\=\:\@\%\#]+)/<A HREF=\"$1\" TARGET=\"blank\">$1<\/A>/ig;
	$text =~ s/(ftp:\/\/[\w\.\~\-\/]+)/<A HREF=\"$1\" TARGET=_top>$1<\/A>/ig;
	$text =~ s/([\w\.\-]+)\@([\w\.\-]+)/<A HREF=\"mailto\:$1\@$2\">$1\@$2<\/A>/ig;
	&jcode'convert(*text,'sjis');
	return $text;
}

#********** MAKE_PASS SUB **********

sub MAKE_PASS {
	local($form_pass) = @_;
	srand();
	$xx = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./";
	$salt = substr($xx, int(rand(64)), 1);
	$salt .= substr($xx, int(rand(64)), 1);
	return crypt($form_pass, $salt);
}

#********** MATCH_PASS SUB **********

sub MATCH_PASS {
	local($form_pass,$log_pass) = @_;
	if ($log_pass =~ /^\$1\$/) { $pass_key = 3; }
	else { $pass_key = 0; }
	if (crypt($form_pass, substr($log_pass,$pass_key,2)) eq "$log_pass") {
		return 'OK';
	} else {
		return 'NG';
	}
}

#********** KICK_HOST SUB **********

sub KICK_HOST {
	$host = $ENV{'REMOTE_HOST'};
	$addr = $ENV{'REMOTE_ADDR'};
	if ($host eq $addr) { $host = gethostbyaddr(pack('C4',split(/\./,$host)),2); }
	local($match) = 0;
	foreach (@kick) {
		$_ =~ s/\*/\.\*/g;
		if ($host =~ /$_/) { $match=1; last; }
		if ($addr =~ /$_/) { $match=1; last; }
	}
	if ($match) { &ERROR("このホストはアクセスできません"); }
}

#********** GET_HOST SUB **********

sub GET_HOST {
	local($host) = $ENV{'REMOTE_HOST'};
	if ($host eq '') { $host = $ENV{'REMOTE_ADDR'}; }
	return $host;
}

#********** GET_AGENT SUB **********

sub GET_AGENT {
	local($agent) = $ENV{'HTTP_USER_AGENT'};
	return $agent;
}

#********** DEBUG SUB **********

sub DEBUG {
	open (DB,"debug.log") ; seek(DB,0,0); @debug = <DB>; close(DB);
	(@debug < 50) || (@debug = @debug[0 .. 48]);
	local($debugpoint) = @_;
	unshift(@debug,"$debugpoint\n");
	open(DB,">debug.log") ;
		eval 'flock(DB,2);'; seek(DB,0,0); print DB @debug; eval 'flock(DB,8);';
	close(DB);
}
