<?php
/* ---------- ---------- ---------- ---------- *
                  Album Factory                
                                               
         利用規約を守ってご利用下さい。        
  http://www.coolandcool.net/pcg/notice.html   
                                               
          by CoolandCool (www.coolandcool.net) 
* ---------- ---------- ---------- ---------- */
$ver = "4.0";

mb_internal_encoding("SJIS");
mb_regex_encoding("SJIS");
include("setup.php");
require("exifReader.inc");
set_form();


session_start();
if ( $_REQUEST["usr"] and !$_SESSION["usr"] )   { $_SESSION["usr"]  = $_REQUEST["usr"];  }
if ( $_REQUEST["pass"] and !$_SESSION["pass"] ) { $_SESSION["pass"] = $_REQUEST["pass"]; }

$sec_data  = @file("./_log/SeCuRiTy.php") or out_error("かなりヤバイ！！");
$sec_vals  = mb_split("<>",   $sec_data[0]);
$deny_usr  = mb_split("<br>", $sec_vals[0]);
$deny_ip   = mb_split("<br>", $sec_vals[1]);
$deny_wrds = mb_split("<br>", $sec_vals[2]);

$cookie_data = mb_split( "<>", $_COOKIE["coolalbum"] );
$cookie_sec  = mb_split( "<>", $_COOKIE["coolusr"] );
foreach ( $deny_usr as $val ) {
	if ( $val != "" and $val == $cookie_data[0] ) { out_error("メンテナンス中です。"); }
}
foreach ( $deny_ip as $val ) {
	if ( $val != ""  and mb_strpos($_SERVER["REMOTE_ADDR"], $val) !== FALSE ) { out_error("メンテナンス中です。"); }
}
if ( $cookie_sec[0] ) { out_error("メンテナンス中です。"); }

if ( mb_strpos($_REQUEST["id"], "::") !== FALSE ) {
	if ( $_REQUEST["from"] !== "" ) { $_REQUEST["id"] = $_REQUEST["from"]; } else {
		out_error("<p align=center>絞り込み表示時には携帯用ページへ移行できません。<br>先に「*絞り込み解除」をクリックしてから携帯用ページに移動してください。<br><a href=\"javascript:history.go(-1)\">戻る</a></p>");
	}
}


# 設定ファイル読み込み
$config = @file("_log/config.php");
array_shift($config);
array_pop($config);
foreach ( $config as $val ) {
	$confg_val = split("\t", $val);
	if ( $_REQUEST["id"] == $confg_val[0] ) { break; }
}

if ( !$confg_val[1] ) { $confg_val[1] = "{$img_dir}/title_b.jpg"; }
if ( !$_REQUEST["id"] ) { $_REQUEST["mode"] = "title"; }



# アクセスログ保存
if ( mb_strpos($_SERVER["HTTP_REFERER"], $master_url) === FALSE ) {
	$time = time();
	$str  = "$time\t{$_SERVER["HTTP_REFERER"]}\t{$_SERVER["HTTP_USER_AGENT"]}\t{$_SERVER["REMOTE_ADDR"]}\t\n";
	$old_data = @file("./_log/accs.php");
	$data = @fopen("./_log/accs.php", "a+");
	@flock($data, LOCK_EX);
	if ( count($old_data) >= 400 ) {
		array_splice($old_data, 0, count($old_data) - 300);
		ftruncate($data, 0);
		foreach ( $old_data as $val ) {
			fputs($data, $val);
		}
	}
	fputs($data, $str);
	@flock($data, LOCK_UN);
	fclose($data);
}


if ( $_REQUEST["view"] ) {
	$time = time(); $deny_flag = 0; $flag = 0;
	chck_accs();
	
	$chck_data = @file("./_log/acc_{$_REQUEST["view"]}.php");
	if ( !$chck_data ) { $chck_data = array(); }
	$accchck_data = array_reverse($chck_data);
	
	foreach ( $accchck_data as $vals ) {
		$val = mb_split("\t", $vals);
		if ( $val[0] == $_SERVER["REMOTE_ADDR"] ) { $flag ++; }
		if ( $time - $val[1] < $sec_vals[3] and $val[0] == $_SERVER["REMOTE_ADDR"] ) { $deny_flag ++; }
		if ( $deny_flag > 4 or $flag > 8 ) { break; }
	}
	
#	if ( $deny_flag > 4 ) { 
#		$cookie_data = "ocer<>$time<>";
#		setcookie("coolusr", $cookie_data, time() + 10*24*60*60); die();
#	}
	
	# ポイント処理
	$old_data = @file("./_log/acc_{$_REQUEST["view"]}.php");
	$data = @fopen("./_log/acc_{$_REQUEST["view"]}.php", "a+") or out_error("ポイント処理ミスった！");
	flock($data, LOCK_EX);
	if ( count($old_data) >= 100 ) {
		array_splice($old_data, 0, 50);
		ftruncate($data, 0);
		foreach ( $old_data as $val ) {
			fputs($data, $val);
		}
	}
	fputs($data, "{$_SERVER["REMOTE_ADDR"]}\t$time\n");
	flock($data, LOCK_UN);
	fclose($data);
	
	if ( $flag < 8 ) {
		$img_cnt = @file("_log/{$_REQUEST["view"]}.php");
		$data = @fopen("_log/{$_REQUEST["view"]}.php", "a+") or out_error("ポイント処理ミスった！");
		flock($data, LOCK_EX);
		$img_cnt[0] = trim($img_cnt[0]);
		if ( !$img_cnt[0] ) { $img_cnt[0] = "0"; }
		$img_cnt[0]++;
		ftruncate($data, 0);
		fputs($data, "$img_cnt[0]\n&pt=$img_cnt[0]");
		flock($data, LOCK_UN);
		fclose($data);
	}
	
	$data = @file("./_log/{$_REQUEST["ni"]}.php");
	foreach ( $data as $values ) {
		$value = mb_split("\t", $values);
		if ( $value[0] == $_REQUEST["view"] ) { break; }
	}
	
	
#__ EDITER__
$befores = mb_split(",", $before_exif);
$afters  = mb_split(",", $after_exif);
foreach ( $befores as $ed_key => $ed_val ) {
	for ( $ed_i = 12; $ed_i <= 37; $ed_i++ ) {
		if ( $ed_i != 21 and $ed_i != 28 and $ed_i != 31 and $ed_i != 32 ) {
			$value[$ed_i] = mb_eregi_replace($ed_val, $afters[$ed_key], $value[$ed_i]);
		}
	}
}
	
	
	if ( file_exists("{$img_dir}/{$_REQUEST["gn"]}/{$_REQUEST["view"]}m.jpg") ) { $thum_img = "{$img_dir}/{$_REQUEST["gn"]}/{$_REQUEST["view"]}m.jpg"; }
	else { $thum_img = "{$img_dir}/{$_REQUEST["gn"]}/{$_REQUEST["view"]}{$value[28]}.jpg"; }
	
	header("Content-type: image/jpeg");
	header("Expires: Thu, 01 Dec 1994 16:00:00 GMT");
	header("Last-Modified: ". gmdate("D, d M Y H:i:s"). " GMT");
	header("Cache-Control: no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	header("Content-Disposition: inline; filename=image.jpg");
	header("Content-length: " . filesize("$thum_img"));
	readfile("$thum_img");

/*
	print <<< EOD
<html>
<head>
<!-- 眉屈 -->
<Meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Album Factory</title>
</head><body>
<img src="{$thum_img}">
</body></html>
EOD;
*/
	exit;
}


else if ( $_REQUEST["mode"] == "title" ) {
	
	# カウンタ
	if ( !$_REQUEST["start_num"] ) {
		$cnt = @fopen("_cnt/title.txt", "a+") or out_error("カウンター処理ミスった！");
		flock($cnt, LOCK_EX);
		$count = fgets($cnt);
		if ( !$count ) { $count = "0"; }
		$count ++;
		ftruncate($cnt, 0);
		fputs($cnt, $count);
		flock($cnt, LOCK_UN);
		fclose($cnt);
	}
	
	# ページ処理
	$config = array_reverse($config);
	$pre_data = array();
	foreach ( $config as $key => $val ) {
		$value = mb_split("\t", $val);
		array_push($pre_data, "$value[22]<>$key");
	}
	natcasesort($pre_data);
	
	$data = array();
	foreach ( $pre_data as $val ) {
		$d_key = mb_split("<>", $val);
		array_push($data, $config[$d_key[1]]);
	}
	
	# $data = $config;
	# $data = array_reverse($data);
	$all_page = ceil( count($data) / $view_data );
	
	
	if ( $_REQUEST['start_num'] ) { $start_num = $_REQUEST['start_num']; }
	else { $start_num = 0; }
	$last_num = $start_num + $i_view_data - 1; #####
	if ( $last_num > count($data) - 1 ) { $last_num = count($data) - 1; }
	$next_num = $last_num + 1;
	if ( $next_num >= count($data) ) { $next_num = ""; }
	$prev_num = $start_num - $i_view_data; #####
	if ( $prev_num  < 0 ) { $prev_num = 0;  }
	if ( $start_num < 1 ) { $prev_num = ""; }
	$now_page = ( INT ) ( $start_num / $i_view_data ) + 1; #####
	if ( !$all_page ) { $now_page = "0"; }
	
	# テンプレートファイル読み込み
	$flag = "1"; $tmpl_file = "i_title";
	$tmpl = @file("_tmpl/".$tmpl_file.".htm") or out_error("テンプレートファイル処理ミスった！");
	
	foreach( $tmpl as $key => $val ) {
		if ( mb_ereg_match("(.*)<!--■■-->(.*)", $val) ) { $flag++; }
		if     ( $flag == "1" ) { $album_head .= $val; }
		elseif ( $flag == "2" ) { $album_main .= $val; }
		elseif ( $flag == "3" ) { $album_foot .= $val; }
	}
	
	if ( $prev_num !== "" ) {
		$prev_link  = "i.php?start_num={$prev_num}&mode=title";
	$album_head = mb_ereg_replace("■back■", $prev_link, $album_head);
	} else { $album_head = mb_ereg_replace("[^\n]*?■back■(.*?)\n", "", $album_head); }
	
	if ( $start_num > 0 ) {
		$top_link   = "i.php?start_num=0&mode=title";
		$album_head = mb_ereg_replace("■top■", $top_link, $album_head);
	} else { $album_head = mb_ereg_replace("[^\n]*?■top■(.*?)\n", "", $album_head); }
	
	if ( $next_num !== "" ) {
		$next_link  = "i.php?start_num={$next_num}&mode=title";
		$album_head = mb_ereg_replace("■next■", $next_link, $album_head);
	} else { $album_head = mb_ereg_replace("[^\n]*?■next■(.*?)\n", "", $album_head); }
	
	$album_head = mb_ereg_replace("■all_page■", $all_page, $album_head);
	$album_head = mb_ereg_replace("■now_page■", $now_page, $album_head);
	$album_head = mb_ereg_replace("■return■", $i_return_url, $album_head);
	$album_head = mb_ereg_replace("■count■",    $count, $album_head);
	$album_head = mb_ereg_replace("■fontsize■", $fontsize, $album_head);
	print $album_head;
	
	$m = 1;
	if ( $_REQUEST["view_line"] ) { $view_line = $_REQUEST["view_line"]; }
	
	for ( $i = $start_num; $i <= $last_num; $i++ ) {
		
		$album_body = $album_main;
		$value = mb_split("\t", $data[$i]);
		$date = date("Y/m/d G:i:s", $value[5]);
		$albumurl = "i.php?id={$value[0]}";
		if ( !$value[6] ) { $value[6] = $def_color; }
		
		$album_body = mb_ereg_replace("■albumurl■", $albumurl, $album_body);
		$album_body = mb_ereg_replace("■imgurl■",   $value[1],  $album_body);
		$album_body = mb_ereg_replace("■color■",    $value[6],  $album_body);
		$album_body = mb_ereg_replace("■date■",     $date,      $album_body);
		$album_body = mb_ereg_replace("■edit■",     "admin.php?id={$value[0]}", $album_body);
		$album_body = mb_ereg_replace("■title■",    $value[3],  $album_body);
		$album_body = mb_ereg_replace("■comment■",  $value[4], $album_body);
		$album_body = mb_ereg_replace("■srt■",      $value[12], $album_body);
		
		$topicdata = @file("./_log/{$value[0]}.php");
		if ( !$topicdata ) { $topicmany = 0; $topicdata = array(); } else { $topicmany = count($topicdata); }
		@natcasesort($topicdata);
		$dammy_num  = $topicmany - 1;
		$topic_vals = mb_split("\t", $topicdata[$dammy_num]);
		if ( $topic_vals[0] ) { $topic_date = date("Y/m/d G:i:s", $topic_vals[0]); }
		
		$topic_img = array();
		foreach ( $topicdata as $topic_vals ) {
			$topic_val = mb_split("\t", $topic_vals);
			if ( file_exists("{$img_dir}/{$value[2]}/{$topic_val[0]}i.jpg") ) { $thum_img = "{$img_dir}/{$value[2]}/{$topic_val[0]}i.jpg"; }
			else if ( file_exists("{$img_dir}/{$value[2]}/{$topic_val[0]}s.jpg") ) { $thum_img = "{$img_dir}/{$value[2]}/{$topic_val[0]}s.jpg"; }
			else if ( file_exists("{$img_dir}/{$value[2]}/{$topic_val[0]}m.jpg") ) { $thum_img = "{$img_dir}/{$value[2]}/{$topic_val[0]}m.jpg"; }
			else { $thum_img = "{$img_dir}/{$value[2]}/{$topic_val[0]}{$topic_val[28]}.jpg"; }
			array_push($topic_img, $thum_img);
		}
		
		$thum = $topic_img[rand(0,(count($topic_img)-1))];
		
		if ( $value[9] or $value[10] ) { $thum = "./img/mem.jpg"; }
		else if ( !$thum ) { $thum = "./img/noimage.jpg"; }
		
		
		$album_body = mb_ereg_replace("■topic■",      $topic_date, $album_body); # 最新記事
		$album_body = mb_ereg_replace("■topic_many■", $topicmany,  $album_body); # 記事数
		$album_body = mb_ereg_replace("■thumurl■",    $thum,  $album_body);      # 画像
		
		print $album_body;
	}

$album_foot = mb_ereg_replace("■all_page■", $all_page, $album_foot);
$album_foot = mb_ereg_replace("■now_page■", $now_page, $album_foot);
if ( $prev_num !== "" ) {
	$album_foot = mb_ereg_replace("■back■", $prev_link, $album_foot);
} else { $album_foot = mb_ereg_replace("[^\n]*?■back■(.*?)\n", "", $album_foot); }
if ( $start_num > 0 ) {
	$album_foot = mb_ereg_replace("■top■", $top_link, $album_foot);
} else { $album_foot = mb_ereg_replace("[^\n]*?■top■(.*?)\n", "", $album_foot); }
if ( $next_num !== "" ) {
	$album_foot = mb_ereg_replace("■next■", $next_link, $album_foot);
} else { $album_foot = mb_ereg_replace("[^\n]*?■next■(.*?)\n", "", $album_foot); }

print "$album_foot";
exit;
}


else if ( $_REQUEST["mode"] == "edit" ) {
	
	chck_accs();
	
	# 各データー読み込み
	if ( $_REQUEST["type"] == "main" ) {
		$data = @file("./_log/{$_REQUEST["id"]}.php");
		foreach ( $data as $value ) {
			$value = mb_split("\t", $value);
			if ( $value[0] == $_REQUEST["num"] ) { break; }
		}
		if ( file_exists("{$img_dir}/{$confg_val[2]}/{$value[0]}m.jpg") )
		 { $thum_img = "{$img_dir}/{$confg_val[2]}/{$value[0]}m.jpg"; }
		else { $thum_img = "{$img_dir}/{$confg_val[2]}/{$value[0]}.jpg"; }
		$name    = $value[2];
		$email   = $value[3];
		$url     = $value[5];
		$title   = $value[1];
		$comment = $value[6];
		$color   = $value[7];
		$cat_u   = $value[20];
	}
	else {
		$data = @file("./_res/{$_REQUEST["num"]}.php");
		foreach ( $data as $value ) {
			$value = mb_split("\t", $value);
			if ( $value[0] == $_REQUEST["date"] ) { break; }
		}
		$name    = $value[1];
		$email   = $value[2];
		$url     = $value[4];
		$comment = $value[5];
		$color   = $value[6];
	}
	
	$comment = mb_ereg_replace("<br>", "\n", $comment);
	
	# フォーム用意
	out_head();
	print <<< EOD
<form name="input" action="i.php" method="post">
<input type="hidden" name="mode" value="edit_regist">
<input type="hidden" name="id" value="{$_REQUEST["id"]}">
<input type="hidden" name="num" value="{$_REQUEST["num"]}">
<input type="hidden" name="date" value="{$_REQUEST["date"]}">
<input type="hidden" name="bn" value="{$_REQUEST["bn"]}">
<input type="hidden" name="bs" value="{$_REQUEST["bs"]}">
<input type="hidden" name="type" value="{$_REQUEST["type"]}">
+ 修正モード +<br>
名前:<input name="name" type="text" size="20" value="{$name}"><br>
メール:<input name="email" type="text" size="20" value="{$email}"><br>
URL:<input name="url" type="text" size="20" value="{$url}"><br>
EOD;
	
	if ( $_REQUEST["type"] == "main" ) { print "タイトル:<input name=\"title\" type=\"text\" size=\"20\" value=\"{$title}\"><br>"; }
	print "コメント:<input name=\"comment\" type=\"text\" size=\"20\" value=\"{$comment}\"><br>";
	
	if ( $_REQUEST["type"] == "main" ) {
		print "カテゴリ:<select name=\"cat\">";
		if ( $confg_val[8] ) { $cat = mb_split(",", $confg_val[8]); }
		foreach ( $cat as $val ) {
			if ( $val == $cat_u ) { print "<option value=\"$val\" selected>$val</option>\n"; }
			else { print "<option value=\"$val\">$val</option>\n"; }
		}
		print "</select><br>";
    }

	if ( $confg_val[7] ) { $admin["color"] = mb_split(",", $confg_val[7]); }
#	array_push ($admin["color"], "#AAAAAA");
	
	if ( $admin["color"][1] == "" ) { # 一色だけの場合はカラー選択非表示。
		print "<input type=hidden name=\"color\" value=\"{$admin["color"][0]}\">";
	} else {
		print "色:";
		foreach ( $admin["color"] as $key => $val ) {
			if ( $color == $val ) {
				print "<input name=\"color\" type=\"radio\" value=\"$val\" checked><font color=\"$val\">■</font>\n";
			} else if ( $val ) {
				print "<input name=\"color\" type=\"radio\" value=\"$val\"><font color=\"$val\">■</font>\n";
			}
		}
		print "<BR>";
	}
print "修正キー:<input name=\"editkey\" type=\"password\" size=\"10\" value=\"{$cookie_data[2]}\"><br>";
print "<input type=\"submit\" value=\"保存\">";
out_footer();
exit;
}


else if ( $_REQUEST["mode"] == "edit_regist" ) {
	
	chck_accs();
	
	# 各データー読み込み
	if ( $_REQUEST["type"] == "main" ) {
		$data = @file("./_log/{$_REQUEST["id"]}.php");
		foreach ( $data as $value ) {
			$value = mb_split("\t", $value);
			if ( $value[0] == $_REQUEST["num"] ) { break; }
		}
	$master_key = $value[4];
	}
	else {
		$data = @file("./_res/{$_REQUEST["num"]}.php");
		foreach ( $data as $value ) {
			$value = mb_split("\t", $value);
			if ( $value[0] == $_REQUEST["date"] ) { break; }
		}
	$master_key = $value[3];
	}
	if ( $srt_switch ) { $confg_val[2] = $sorted_dir["{$_REQUEST["num"]}"]; }
	if ( !$_REQUEST["editkey"] ) { out_error("修正キーが見えません。"); }
	if ( $_REQUEST["editkey"] != $master_key and $_REQUEST["editkey"] != $password and $_REQUEST["editkey"] != $confg_val[11] ) { out_error("修正キーがヤバいです。"); }
	
	$new_data = array();
	if ( $_REQUEST["type"] == "main" ) {
		foreach ( $data as $values ) {
			$value = mb_split("\t", $values);
			
			if ( $value[0] == $_REQUEST["num"] ) {
				
				$editdata = "{$value[0]}\t{$_REQUEST["title"]}\t{$_REQUEST["name"]}\t{$_REQUEST["email"]}\t{$value[4]}\t{$_REQUEST["url"]}\t{$_REQUEST["comment"]}\t{$_REQUEST["color"]}\t{$value[8]}\t{$value[9]}\t{$value[10]}\t{$value[11]}\t{$value[12]}\t{$value[13]}\t{$value[14]}\t{$value[15]}\t{$value[16]}\t{$value[17]}\t{$value[18]}\t{$value[19]}\t{$value[20]}\t{$_SERVER["REMOTE_ADDR"]}\t{$value[22]}\t{$value[23]}\t{$value[24]}\t{$value[25]}\t{$value[26]}\t{$value[27]}\t{$value[28]}\t{$value[29]}\t{$value[30]}\t{$value[31]}\t{$value[32]}\t\n"; //33
				
				if ( !$_REQUEST["del"] ) {
					array_push ($new_data, $editdata);
				} else { # 記事削除の場合 ####################
				
				
					$upinfo = @file("./_log/upinfo.php");
					if ( !$upinfo ) { $upinfo = array(); }
					$usr_file = @fopen("_log/upinfo.php", "a") or out_error("ファイルオープンエラー");
					flock($usr_file, LOCK_EX);
					ftruncate($usr_file, 0);
					foreach ( $upinfo as $val ) {
						$vals = mb_split("\t", $val);
						if ( $_REQUEST["num"] != $vals[0] ) { fputs($usr_file, $val); }
					}
					flock($usr_file, LOCK_UN);
					fclose($usr_file);
					
					
					
					$upinfo_data = @file("./_log/upinfo.php");
					$upinfo_data = array_reverse($upinfo_data);
					$info_img = "";
					$m = 1;
					foreach ( $upinfo_data as $val ) {
						$vals = split("\t",$val);
						if ( file_exists("{$vals[2]}/{$vals[0]}s.jpg") ) { $info_img .= "<a href=\"album.php?_v={$vals[0]}\"><img src=\"{$master_url}{$vals[2]}/{$vals[0]}s.jpg\" border=0></a>{$info_tab}"; }
						else { $info_img .= "<a href=\"album.php?_v={$vals[0]}\"><img src=\"{$master_url}{$vals[2]}/{$vals[0]}{$add_name}.jpg\" border=0></a>{$info_tab}"; }
						
						$m++;
						if ( $m > $info_many ) { break; }
					}
					$whatsupjs = @fopen("./_log/upinfo.js", "a");
					flock($whatsupjs, LOCK_EX);
					ftruncate($whatsupjs, 0);
					fputs($whatsupjs, "document.write('{$info_img}')");
					flock($whatsupjs, LOCK_UN);
					fclose($whatsupjs);
					
					
					foreach ( $upinfo_data as $val ) {
						$vals = split("\t",$val);
						if ( file_exists("{$vals[2]}/{$vals[0]}s.jpg") ) { $md3 .= ",{$vals[2]}/{$vals[0]}s.jpg"; }
						else { $md3 .= ",{$vals[2]}/{$vals[0]}{$vals[15]}.jpg"; }
						$md1 .= ",{$vals[1]}";
						$md2 .= ",{$vals[0]}";
						$md4 .= ",{$vals[3]}";
						$md5 .= ",{$vals[4]}";
						$md6 .= ",{$vals[7]}";
						$md7 .= ",{$vals[8]}";
					}
					$whatsup = @fopen("./_log/upinfod.php", "a");
					flock($whatsup, LOCK_EX);
					ftruncate($whatsup, 0);
					fputs($whatsup, "mid={$md1}&imid={$md2}&img_url={$md3}&ititle={$md4}&iname={$md5}&dscr={$md6}&date={$md7}&last=ok");
					flock($whatsup, LOCK_UN);
					fclose($whatsup);
				
					@unlink("{$img_dir}/{$confg_val[2]}/{$value[0]}{$value[28]}.jpg");
					@unlink("{$img_dir}/{$confg_val[2]}/{$value[0]}m.jpg");
					@unlink("{$img_dir}/{$confg_val[2]}/{$value[0]}s.jpg");
					@unlink("{$img_dir}/{$confg_val[2]}/{$value[0]}i.jpg");
					@unlink("./_log/{$value[0]}.php");
					@unlink("./_log/acc_{$value[0]}.php");
					@unlink("./_res/{$value[0]}.php");
					@unlink("./_res/{$value[0]}d.php");
				}
			}
			else { array_push ($new_data, $values); }
		}
		
		$file = @fopen("_log/{$_REQUEST["id"]}.php", "a") or out_error("書き込みミスったくさい！");
		flock($file, LOCK_EX);
		ftruncate($file, 0);
		foreach ( $new_data as $value ) {
			fputs($file, $value);
		}
		flock($file, LOCK_UN);
		fclose($file);
		header("Location: ./i.php?id={$_REQUEST["id"]}&start_num={$_REQUEST["bn"]}&srt={$_REQUEST["bs"]}");
		exit;
	}
	
	else { # レス記事処理
		if ( !$_REQUEST["name"] )    { out_error("名前がありませーん。"); }
		if ( !$_REQUEST["comment"] ) { out_error("コメントがなかったり。"); }
		foreach ($deny_wrds as $val) {
			if ( mb_strpos($_REQUEST["comment"], $val) !== FALSE ) { out_error("利用できない語句が含まれていました。"); }
		}
		foreach ( $data as $values ) {
			$value = mb_split("\t", $values);
			if ( $value[0] == $_REQUEST["date"] ) {
				$editdata = "{$value[0]}\t{$_REQUEST["name"]}\t{$_REQUEST["email"]}\t{$_REQUEST["editkey"]}\t{$_REQUEST["url"]}\t{$_REQUEST["comment"]}\t{$_REQUEST["color"]}\t{$_SERVER["REMOTE_ADDR"]}\t\n";
				if ( !$_REQUEST["del"] ) {
					array_push ($new_data, $editdata);
				}
			}
			else { array_push ($new_data, $values); }
		}
	$file = @fopen("_res/{$_REQUEST["num"]}.php", "a") or out_error("書き込みミスったくさい！！");
	flock($file, LOCK_EX);
	ftruncate($file, 0);
	foreach ( $new_data as $value ) {
		fputs($file, $value);
	}
	flock($file, LOCK_UN);
	fclose($file);
	header("Location: ./i.php?id={$_REQUEST["id"]}&mode=view_res&num={$_REQUEST["num"]}&bs={$_REQUEST["bs"]}&bn={$_REQUEST["bn"]}");
	exit;
	}
}


else if ( $_REQUEST["mode"] == "view_res" ) {
	
	chck_accs();
	
	$view_data = $i_res_view_data;
	
	$tmpl = @file("_tmpl/i_res.htm") or out_error("テンプレートファイルがうまく開けなかった！");
	
	$flag = 1;
	foreach( $tmpl as $key => $val ) {
		if ( mb_ereg_match("(.*)<!--■■-->(.*)", $val) ) { $flag++; }
		if     ( $flag == "1" ) { $res_head .= $val; }
		elseif ( $flag == "2" ) { $res_main .= $val; }
		elseif ( $flag == "3" ) { $res_foot .= $val; }
	}
	
	
	# データー読み込み
	$data = @file("./_log/{$_REQUEST["id"]}.php");
	$pre_data = array();
	$add_data = array();
	if ( !$data ) { $data = array(); }
	
	$num_val = 0;
	$art_num = array(); 
	foreach ( $data as $key => $val ) {
		$value = mb_split("\t", $val);
		$num_val++ ;
		$art_num[$value[0]] = $num_val; 
		
		# レス
		$rescnt = @file("./_res/{$value[0]}.php");
		# レス数
		if ( !$rescnt ) { $resmany = 0; } else { $resmany = count($rescnt); }
		# レス日時
		$damy_num = count($rescnt) - 1;
		$res_value = mb_split("\t", $rescnt[$damy_num]);
		# ポイント
		$point  = @file("./_log/{$value[0]}.php");
		
		if ( $_REQUEST["bs"] == "" ) { $_REQUEST["bs"] = $confg_val[12]; } #####
		
		if ( $_REQUEST["bs"] == "3" ) {
			array_push($pre_data, "$resmany<>$key");
		}
		else if ( $_REQUEST["bs"] == "2" ) {
			if ( $resmany > 0 ) { array_push($pre_data, "$res_value[0]<>$key"); }
			else { array_push($add_data, $val); }
		}
		else if ( $_REQUEST["bs"] == "1" ) {
			if ( !$point ) { $point[0] = "0"; }
			array_push($pre_data, "$point[0]<>$key");
		}
		else if ( $_REQUEST["bs"] == "4" ) {
			array_push($pre_data, "$value[20]<>$key");
		}
		else if ( $_REQUEST["bs"] == "6" ) {
			array_push($pre_data, "$value[0]<>$key");
		}
		else {
			array_push($pre_data, "$value[15]<>$key");
		}
	}
	
	natcasesort ($pre_data);
	if ( $_REQUEST["bs"] == "1" or $_REQUEST["bs"] == "2" or $_REQUEST["bs"] == "3" or $_REQUEST["bs"] == "5" or $_REQUEST["bs"] == "6" ) {
		$pre_data = array_reverse($pre_data, true);
	}
	
	$sort_data = array();
	foreach ( $pre_data as $val ) {
		$d_key = mb_split("<>", $val);
		array_push($sort_data, $data[$d_key[1]]);
	}
	
	if ( $add_data ) { 
		foreach ( $add_data as $val ) {
			array_push($sort_data, $val);
		}
	}
	
	$flag = 0;
	foreach ( $sort_data as $values ) {
		$value = mb_split("\t", $values);
		if ( $flag ) { $next_photo = $value[0]; $value = $rvalue; break; }
		if ( $value[0] == $_REQUEST["num"] ) { $rvalue = $value; $flag = 1; }
		if ( !$flag ) { $prev_photo = $value[0]; }
	}
	
	
#__ EDITER__
$befores = mb_split(",", $before_exif);
$afters  = mb_split(",", $after_exif);
foreach ( $befores as $ed_key => $ed_val ) {
	for ( $ed_i = 12; $ed_i <= 37; $ed_i++ ) {
		if ( $ed_i != 21 and $ed_i != 28 and $ed_i != 31 and $ed_i != 32 ) {
			$value[$ed_i] = mb_eregi_replace($ed_val, $afters[$ed_key], $value[$ed_i]);
		}
	}
}
	
	$res_data = @file("./_res/{$value[0]}.php");
	if ( !$res_data ) { $res_data = array(); }
	else { $res_data = array_reverse($res_data); } // 携帯表示の場合は新着コメントを上に。
	
	
	# ページ処理
	
	if ( $_REQUEST['start_num'] ) { $start_num = $_REQUEST['start_num']; }
	else { $start_num = 0; }
	$last_num = $start_num + $view_data - 1;
	if ( $last_num > count($res_data) - 1 ) { $last_num = count($res_data) - 1; }
	$next_num = $last_num + 1;
	if ( $next_num >= count($res_data) ) { $next_num = ""; }
	$prev_num = $start_num - $view_data;
	if ( $prev_num  < 0 ) { $prev_num = 0;  }
	if ( $start_num < 1 ) { $prev_num = ""; }
	$last_num_link = count($res_data) - $view_data;
	
	$now_page = ( INT ) ( $start_num / $view_data ) + 1;
	
	if ( $prev_num !== "" ) {
		$prev_link  = "i.php?start_num={$prev_num}&mode={$_REQUEST["mode"]}&id={$_REQUEST["id"]}&num={$_REQUEST["num"]}&bs={$_REQUEST["bs"]}&bn={$_REQUEST["bn"]}";
		$res_head = mb_ereg_replace("■back■", $prev_link, $res_head);
	}
	 else { $res_head = mb_ereg_replace("[^\n]*?■back■(.*?)\n", "", $res_head); }
	
	if ( $start_num > 0 ) {
		$top_link   = "i.php?start_num=0&mode={$_REQUEST["mode"]}&id={$_REQUEST["id"]}&num={$_REQUEST["num"]}&bs={$_REQUEST["bs"]}&bn={$_REQUEST["bn"]}";
		$res_head = mb_ereg_replace("■top■", $top_link, $res_head);
	}
	 else { $res_head = mb_ereg_replace("[^\n]*?■top■(.*?)\n", "", $res_head); }

	if ( $start_num < $last_num_link ) {
		$last_link   = "i.php?start_num=$last_num_link&mode={$_REQUEST["mode"]}&id={$_REQUEST["id"]}&num={$_REQUEST["num"]}&bs={$_REQUEST["bs"]}&bn={$_REQUEST["bn"]}";
		$res_head = mb_ereg_replace("■last■", $last_link, $res_head);
	}
	 else { $res_head = mb_ereg_replace("[^\n]*?■last■(.*?)\n", "", $res_head); }

	if ( $next_num !== "" ) {
		$next_link  = "i.php?start_num={$next_num}&mode={$_REQUEST["mode"]}&id={$_REQUEST["id"]}&num={$_REQUEST["num"]}&bs={$_REQUEST["bs"]}&bn={$_REQUEST["bn"]}";
		$res_head = mb_ereg_replace("■next■", $next_link, $res_head);
	}
	 else { $res_head = mb_ereg_replace("[^\n]*?■next■(.*?)\n", "", $res_head); }
	
	if ( !$value[3] ) {
		$res_head = mb_ereg_replace("(.*)?■email■(.*)?\n", "", $res_head, "s");
	}
	if ( !$value[5] ) {
		$res_head = mb_ereg_replace("(.*)?■url■(.*)?\n", "", $res_head, "s");
	}
	
	if ( file_exists("{$img_dir}/{$confg_val[2]}/{$value[0]}m.jpg") ) { $i_img = "{$img_dir}/{$confg_val[2]}/{$value[0]}m.jpg"; }
	else { $i_img = "{$img_dir}/{$confg_val[2]}/{$value[0]}{$value[28]}.jpg"; }
	$img = "i.php?view={$value[0]}";
	
	$thum_size = (INT) ( @filesize("{$img_dir}/{$confg_val[2]}/{$value[0]}i.jpg") / 1000 );
	$img_size  = (INT) ( @filesize($i_img) / 1000 );
	
	$date = date("Y/m/d G:i:s", $value[0]);
	
	if ( !$value[7] ) { $value[7] = $def_color; }
	if ( $title_model == 1 and !$value[1] and $value[12] ) { $value[1] = $value[12]; }
	else if ( $title_model == 2 and !$value[1] and $value[2] ) { $value[1] = $value[2]; }
	else if ( !$value[1] ) { $value[1] = "無題"; }
	$dammy = mb_split("\(", $value[18]);
	if ( $dammy[2] ) { $value[18] = $dammy[0] . "(" . $dammy[2]; }
	$point = @file("./_log/{$value[0]}.php");
	
	$res_head = mb_ereg_replace("■viewback■",  "i.php?id={$_REQUEST["id"]}&srt={$_REQUEST["bs"]}&start_num={$_REQUEST["bn"]}", $res_head);
	$res_head = mb_ereg_replace("■return■", $i_return_url, $res_head);
	$res_head = mb_ereg_replace("■fontsize■", $fontsize, $res_head);
	$res_head = mb_ereg_replace("■title■",    $value[1], $res_head);
	$res_head = mb_ereg_replace("■name■",     $value[2], $res_head);
	$res_head = mb_ereg_replace("■email■",    $value[3], $res_head);
	$res_head = mb_ereg_replace("■url■",      $value[5], $res_head);
	$res_head = mb_ereg_replace("■comment■",  $value[6], $res_head);
	$res_head = mb_ereg_replace("■color■",    $value[7], $res_head);
	$res_head = mb_ereg_replace("■width■",    $value[8], $res_head);
	$res_head = mb_ereg_replace("■height■",   $value[9], $res_head);
	$res_head = mb_ereg_replace("■s_width■",  $value[10], $res_head);
	$res_head = mb_ereg_replace("■s_height■", $value[11], $res_head);
	$res_head = mb_ereg_replace("■model■",    $value[12], $res_head);
	$res_head = mb_ereg_replace("■iso■",      $value[13], $res_head);
	$res_head = mb_ereg_replace("■fnumber■",  $value[14], $res_head);
	$res_head = mb_ereg_replace("■picdate■",  $value[15], $res_head);
	$res_head = mb_ereg_replace("■ftype■",    $value[16], $res_head);
	$res_head = mb_ereg_replace("■bright■",   $value[17], $res_head);
	$res_head = mb_ereg_replace("■ftime■",    $value[18], $res_head);
	$res_head = mb_ereg_replace("■white■",    $value[19], $res_head);
	$res_head = mb_ereg_replace("■cat■",      $value[20], $res_head);
	$res_head = mb_ereg_replace("■add1■",    $value[22], $res_head);
	$res_head = mb_ereg_replace("■add2■",    $value[23], $res_head);
	$res_head = mb_ereg_replace("■add3■",    $value[24], $res_head);
	$res_head = mb_ereg_replace("■add4■",    $value[25], $res_head);
	$res_head = mb_ereg_replace("■add5■",    $value[26], $res_head);
	$res_head = mb_ereg_replace("■add6■",    $value[27], $res_head);
	$res_head = mb_ereg_replace("■flength■",  $value[29], $res_head);
	$res_head = mb_ereg_replace("■lense■",    $value[30], $res_head);
	
	
	$res_head = mb_ereg_replace("■maker■",    $value[33], $res_head);
	$res_head = mb_ereg_replace("■flash■",    $value[34], $res_head);
	$res_head = mb_ereg_replace("■etype■",    $value[35], $res_head);
	$res_head = mb_ereg_replace("■sharpnessnse■",    $value[36], $res_head);
	$res_head = mb_ereg_replace("■jpegq■",    $value[37], $res_head);
	
	
	$reslink = "<a href=\"i.php?id={$_REQUEST["id"]}&mode=res_form&num={$value[0]}&bs={$_REQUEST["bs"]}&bn={$_REQUEST["bn"]}\">";
	$imglink = "album.php?id={$_REQUEST["id"]}&view={$value[0]}&fe={$find}";
	if ( !$value[2] ) { $value[2] = "誰か"; }
	if ( !$value[6] ) { $value[6] = "本文無し"; }
	
	$res_head = mb_ereg_replace("■date■",     $date,     $res_head);
	$res_head = mb_ereg_replace("■point■",    "<span title=\"写真表示回数\">{$point[0]}</span>", $res_head);
	$res_head = mb_ereg_replace("■reslink■", $reslink,  $res_head);
	$res_head = mb_ereg_replace("■topiclink■", $topiclink,  $res_head);
	
	$res_head = mb_ereg_replace("■thum_i■", "{$img_dir}/{$confg_val[2]}/{$value[0]}i.jpg",  $res_head);
	$res_head = mb_ereg_replace("■thum_size■", $thum_size, $res_head);
	
	$res_head = mb_ereg_replace("■img_size■", $img_size, $res_head);
	$res_head = mb_ereg_replace("■img_i■", $i_img, $res_head); // 画像パス（携帯用）
	$res_head = mb_ereg_replace("■imgurl■",   $imglink,  $res_head); // 画像アドレス（パソコン用）


	if ( $confg_val[17] ) {
		if ( file_exists("{$img_dir}/{$confg_val[2]}/{$value[0]}m.jpg") ) { $dmthum_img = "{$img_dir}/{$confg_val[2]}/{$value[0]}m.jpg"; }
		else { $dmthum_img = "{$img_dir}/{$confg_val[2]}/{$value[0]}{$value[28]}.jpg"; }
	$res_head = mb_ereg_replace("■imgiurl■", $dmthum_img, $res_head); // 画像アドレス（携帯用）
	$res_head = mb_ereg_replace("■thum_img■", $dmthum_img, $res_head); // 画像アドレス（携帯用）
	}
	else {
	$res_head = mb_ereg_replace("■imgiurl■", "i.php?gn={$confg_val[2]}&view={$value[0]}&ni={$_REQUEST["id"]}", $res_head); // 画像アドレス（携帯用）
	$res_head = mb_ereg_replace("■thum_img■", "i.php?gn={$confg_val[2]}&view={$value[0]}&ni={$_REQUEST["id"]}", $res_head); // 画像アドレス（携帯用）
	}
	
	if ( $next_photo ) {
		$res_head = mb_ereg_replace("■next_link■", "i.php?id={$_REQUEST["id"]}&mode=view_res&num={$next_photo}&bs={$_REQUEST["bs"]}&bn={$_REQUEST["bn"]}",  $res_head); }
	else { $res_head = mb_ereg_replace("(.*)?■next_link■(.*)?\n", "", $res_head, "s"); }
		
	if ( $prev_photo ) {
		$res_head = mb_ereg_replace("■prev_link■", "i.php?id={$_REQUEST["id"]}&mode=view_res&num={$prev_photo}&bs={$_REQUEST["bs"]}&bn={$_REQUEST["bn"]}",  $res_head); }
	else { $res_head = mb_ereg_replace("(.*)?■prev_link■(.*)?\n", "", $res_head, "s"); }
	
	print $res_head;
	
	for ( $i = $start_num; $i <= $last_num; $i++ ) {
		
		$res_body = $res_main;
		$res_value = mb_split("\t", $res_data[$i]);
		
		# メール
		if ( !$res_value[2] ) {
			$res_body = mb_ereg_replace("(.*)?■email■(.*)?\n", "", $res_body, s);
		}
		if ( !$res_value[4] ) {
			$res_body = mb_ereg_replace("(.*)?■url■(.*)?\n", "", $res_body, s);
		}
		$edit = "i.php?mode=edit&id={$_REQUEST[id]}&num={$value[0]}&date={$res_value[0]}&type=res";
		$date = date("Y/m/d G:i:s", $res_value[0]);
		if ( !$res_value[6] or $res_value[6] == "\n" ) { $res_value[6] = $def_color; }
		
		$res_body = mb_ereg_replace("■edit■",    $edit,         $res_body);
		$res_body = mb_ereg_replace("■date■",    $date,         $res_body);
		$res_body = mb_ereg_replace("■name■",    $res_value[1], $res_body);
		$res_body = mb_ereg_replace("■email■",   $res_value[2], $res_body);
		$res_body = mb_ereg_replace("■url■",     $res_value[4], $res_body);
		$res_body = mb_ereg_replace("■comment■", $res_value[5], $res_body);
		$res_body = mb_ereg_replace("■color■",   $res_value[6], $res_body);
		print $res_body;
	}
	
	
	$res_foot = mb_ereg_replace("■reslink■", $reslink,  $res_foot);
	$res_foot = mb_ereg_replace("■topiclink■", $topiclink,  $res_foot);
	
	if ( $next_photo ) {
		$res_foot = mb_ereg_replace("■next_link■", "i.php?id={$_REQUEST["id"]}&mode=view_res&num={$next_photo}&bs={$_REQUEST["bs"]}&bn={$_REQUEST["bn"]}",  $res_foot); }
	else { $res_foot = mb_ereg_replace("(.*)?■next_link■(.*)?\n", "", $res_foot, "s"); }
	
	if ( $prev_photo ) {
		$res_foot = mb_ereg_replace("■prev_link■", "i.php?id={$_REQUEST["id"]}&mode=view_res&num={$prev_photo}&bs={$_REQUEST["bs"]}&bn={$_REQUEST["bn"]}",  $res_foot); }
	else { $res_foot = mb_ereg_replace("(.*)?■prev_link■(.*)?\n", "", $res_foot, "s"); }
	
	$res_foot = mb_ereg_replace("■viewback■",     "i.php?id={$_REQUEST["id"]}&srt={$_REQUEST["bs"]}&start_num={$_REQUEST["bn"]}", $res_foot);
	
	if ( $prev_num !== "" ) {
		$res_foot = mb_ereg_replace("■back■", $prev_link, $res_foot);
	} else { $res_foot = mb_ereg_replace("[^\n]*?■back■(.*?)\n", "", $res_foot); }
	if ( $start_num > 0 ) {
		$res_foot = mb_ereg_replace("■top■", $top_link, $res_foot);
	} else { $res_foot = mb_ereg_replace("[^\n]*?■top■(.*?)\n", "", $res_foot); }

	if ( $start_num < $last_num_link ) {
		$res_foot = mb_ereg_replace("■last■", $last_link, $res_foot);
	}
	 else { $res_foot = mb_ereg_replace("[^\n]*?■last■(.*?)\n", "", $res_foot); }

	if ( $next_num !== "" ) {
		$res_foot = mb_ereg_replace("■next■", $next_link, $res_foot);
	} else { $res_foot = mb_ereg_replace("[^\n]*?■next■(.*?)\n", "", $res_foot); }
	
	print $res_foot;
	exit;
}


else if ( $_REQUEST["mode"] == "res_form" ) {
	
	chck_accs();
	
	
	# クッキー取得
	$cookie_data = mb_split( "<>", $_COOKIE["coolalbum"] );
	if ( !$cookie_data[4] ) { $cookie_data[4] = $color[0]; }
		if ( $_REQUEST["mode"] == 5 ) { # 再入力の場合
		$cookie_data[0] = $_REQUEST["name"];
		$cookie_data[1] = $_REQUEST["email"];
		$cookie_data[2] = $_REQUEST["editkey"];
		$cookie_data[3] = $_REQUEST["url"];
		$cookie_data[4] = $_REQUEST["color"];
	}
	
	
	out_head();
	
	print <<< EOD
	
<form name="input" action="i.php" method="post">
<input type="hidden" name="mode" value="res_regist">
<input type="hidden" name="id" value="{$_REQUEST["id"]}">
<input type="hidden" name="num" value="{$_REQUEST["num"]}">
<input type="hidden" name="bs" value="{$_REQUEST["bs"]}">
<input type="hidden" name="bn" value="{$_REQUEST["bn"]}">
+ コメント投稿 +<br>
名前:<input name="name" type="text" size="20" value="{$cookie_data[0]}"><br>
コメント:<input name="comment" type="text" size="20"><br>
メール:<input name="email" type="text" size="25" value="{$cookie_data[1]}"><br>
修正キー:<input name="editkey" type="password" size="10" value="{$cookie_data[2]}"><br>
<!-- URL<input name="url" type="text" size="25" value="{$cookie_data[3]}"><br> -->
EOD;
	
	if ( $confg_val[7] ) { $admin["color"] = mb_split(",", $confg_val[7]); }
#	array_push ($admin["color"], "#AAAAAA");
	if ( $admin["color"][1] == "" ) { # 一色だけの場合はカラー選択非表示。
		print "<input type=hidden name=\"color\" value=\"{$admin["color"][0]}\">";
	} else {
		print "色:";
		foreach ( $admin["color"] as $key => $val ) {
			if ( $val == "#FFFFFF" ) {
				print "<input name=\"color\" type=\"radio\" value=\"$val\" checked><font color=\"$val\">■</font>\n";
			} else if ( $val ) {
				print "<input name=\"color\" type=\"radio\" value=\"$val\"><font color=\"$val\">■</font>\n";
			}
		}
		print "<BR>";
	}
	print "修正キーを入力しておくと、あとから記事を修正することができます。<BR>";
	print "<input type=\"submit\" value=\"投稿\"></form>";
	
	out_footer();
	exit;
}


else if ( $_REQUEST["mode"] == "res_regist" ) {
	
	if ( !$_REQUEST["name"] )    { out_error("名前がありませーん。"); }
	if ( !$_REQUEST["comment"] ) { out_error("コメントがなかったり。"); }
	if ( !mb_ereg('[ぁ-ん]', $_REQUEST["comment"]) ) { out_error("ニホンゴスコシ？"); }
	foreach ($deny_wrds as $val) {
		if ( mb_strpos($_REQUEST["comment"], $val) !== FALSE ) { out_error("利用できない語句が含まれていました。"); }
	}
	
	$res_data = @file("./_res/{$_REQUEST["num"]}.php");
	if ( $res_data ) {
		$c_num = count($res_data) - 1;
		$chck = mb_split("\t", $res_data[$c_num]);
		if ( $_REQUEST["comment"] == $chck[5] ) { out_error("ちゃんと書き込まれてるヨ＝。"); }
	}
	
	$cookie_data = "{$_REQUEST["name"]}<>{$_REQUEST["email"]}<>{$_REQUEST["editkey"]}<>{$_REQUEST["url"]}<>{$_REQUEST["color"]}<>";
	setcookie("coolalbum", $cookie_data, time() + 30*24*60*60);
	
	$time = time();
	$save_data = "{$time}\t{$_REQUEST["name"]}\t{$_REQUEST["email"]}\t{$_REQUEST["editkey"]}\t{$_REQUEST["url"]}\t{$_REQUEST["comment"]}\t{$_REQUEST["color"]}\t{$_SERVER["REMOTE_ADDR"]}\t\n";
	$data = @fopen("_res/{$_REQUEST["num"]}.php", "a") or out_error("書き込みミスったくさい！！");
	flock($data, LOCK_EX);
	fputs($data, $save_data);
	flock($data, LOCK_UN);
	fclose($data);
	
	# 各データー読み込み
	$data = @file("./_log/{$_REQUEST["id"]}.php");
	foreach ( $data as $value ) {
		$value = mb_split("\t", $value);
		if ( $value[0] == $_REQUEST["num"] ) { break; }
	}
	
	if ( $_REQUEST["name"] != $value[2] ) {
		if ( mb_strpos($value[3], "docomo.ne.jp") !== FALSE or mb_strpos($value[3], "vodafone.ne.jp") !== FALSE or mb_strpos($value[3], "ezweb.ne.jp") !== FALSE ) {
			$mail_body = "AlbumFactoryのあなたの写真にコメントが付きました。\n{$master_url}i.php?id={$_REQUEST["id"]}&mode=view_res&num={$_REQUEST["num"]}&ck=26\nご確認下さい。\n\n* このメールは自動送信メールです。\nこのメールにご返信いただいても\nお答えできませんのでご注意下さい。\n";
		} else {
			$mail_body = "AlbumFactoryのあなたの写真にコメントが付きました。\n{$master_url}album.php?id={$_REQUEST["id"]}&mode=res_form&num={$_REQUEST["num"]}&ck=37\nご確認下さい。\n\n* このメールは自動送信メールです。\nこのメールにご返信いただいても\nお答えできませんのでご注意下さい。\n";
		}
		
		$mail_add = "From: {$master_mail}\nBcc: {$master_mail}";
		mb_send_mail($value[3], "AlbumFactoryコメント通知", $mail_body, $mail_add);
	} else {
		$mail_body = "AlbumFactoryにコメントが付きました。\n{$master_url}album.php?id={$_REQUEST["id"]}&mode=res_form&num={$_REQUEST["num"]}&ck=37\nご確認下さい。\n\n* このメールは自動送信メールです。\nこのメールにご返信いただいても\nお答えできませんのでご注意下さい。\n";
		$mail_add = "From: {$master_mail}\n";
		mb_send_mail($master_mail, "AlbumFactoryコメント通知", $mail_body, $mail_add);
	}
	
	header("Location: ./i.php?num={$_REQUEST["num"]}&mode=view_res&return=1&id={$_REQUEST["id"]}&bs={$_REQUEST["bs"]}&bn={$_REQUEST["bn"]}");
}


if ( !$_REQUEST["mode"] ) {
	
	if ( !$_REQUEST["id"] ) { header("Location: ./i.php?mode=title"); exit; }
	
	chck_accs();
	$view_data = $i_view_data;
	
	# カウンタ
	if ( mb_strpos($_REQUEST["id"], "::") === FALSE ) {
		$time = time();
		$no_cnt_flag = 0;
		$cnt_accs = @file("./_cnt/{$_REQUEST["id"]}accs.php");
		$cnt = @fopen("_cnt/{$_REQUEST["id"]}.txt", "a+") or out_error("カウンター処理ミスった！");
		@flock($cnt, LOCK_EX);
		$count = fgets($cnt);
		if ( !$count ) { $count = "0"; }
		
		foreach($cnt_accs as $accsvals) {
			$accsval = explode("\t", $accsvals);
			if ( $time - $accsval[0] < 12*60*60 and $accsval[1] == $_SERVER["REMOTE_ADDR"] ) { $no_cnt_flag = 1; break; }
		}
		
		if ( !$no_cnt_flag ) {
			$count ++;
			ftruncate($cnt, 0);
			fputs($cnt, $count);
		}
		@flock($cnt, LOCK_UN);
		fclose($cnt);
		
		if ( !$no_cnt_flag ) {
			$old_data = @file("./_cnt/{$_REQUEST["id"]}accs.php");
			$accs_data = @fopen("./_cnt/{$_REQUEST["id"]}accs.php", "a+");
			@flock($accs_data, LOCK_EX);
			if ( count($old_data) >= 500 ) {
				array_splice($old_data, 0, 200);
				ftruncate($accs_data, 0);
				foreach ( $old_data as $val ) {
					fputs($accs_data, $val);
				}
			}
			$str = "$time\t{$_SERVER["REMOTE_ADDR"]}\t\n";
			fputs($accs_data, $str);
			@flock($accs_data, LOCK_UN);
			fclose($accs_data);
		}
	}
	
	# データー読み込み
	$data = @file("./_log/{$_REQUEST["id"]}.php");
	$pre_data = array();
	$add_data = array();
	if ( !$data ) { $data = array(); }
	
	$num_val = 0;
	$art_num = array(); 
	foreach ( $data as $key => $val ) {
		$value = mb_split("\t", $val);
		$num_val++ ;
		$art_num[$value[0]] = $num_val; 
		
		# レス
		$rescnt = @file("./_res/{$value[0]}.php");
		# レス数
		if ( !$rescnt ) { $resmany = 0; } else { $resmany = count($rescnt); }
		# レス日時
		$damy_num = count($rescnt) - 1;
		$res_value = mb_split("\t", $rescnt[$damy_num]);
		# ポイント
		$point  = @file("./_log/{$value[0]}.php");
		if ( $_REQUEST["srt"] == "" ) { $_REQUEST["srt"] = $confg_val[12]; } #####
		
		if ( $_REQUEST["srt"] == "3" ) {
			array_push($pre_data, "$resmany<>$key");
		}
		else if ( $_REQUEST["srt"] == "2" ) {
			if ( $resmany > 0 ) { array_push($pre_data, "$res_value[0]<>$key"); }
			else { array_push($add_data, $val); }
		}
		else if ( $_REQUEST["srt"] == "1" ) {
			if ( !$point ) { $point[0] = "0"; }
			array_push($pre_data, "$point[0]<>$key");
		}
		else if ( $_REQUEST["srt"] == "4" ) {
			array_push($pre_data, "$value[20]<>$key");
		}
		else if ( $_REQUEST["srt"] == "6" ) {
			array_push($pre_data, "$value[0]<>$key");
		}
		else {
			array_push($pre_data, "$value[15]<>$key");
		}
	}
	
	natcasesort ($pre_data);
	if ( $_REQUEST["srt"] == "1" or $_REQUEST["srt"] == "2" or $_REQUEST["srt"] == "3" or $_REQUEST["srt"] == "5" or $_REQUEST["srt"] == "6" ) {
		$pre_data = array_reverse($pre_data, true);
	}
	
	$sort_data = array();
	foreach ( $pre_data as $val ) {
		$d_key = mb_split("<>", $val);
		array_push($sort_data, $data[$d_key[1]]);
	}
	
	if ( $add_data ) { 
		foreach ( $add_data as $val ) {
			array_push($sort_data, $val);
		}
	}
	
	$data = $sort_data;
	if ( $_REQUEST["_v"] ) {
		$_REQUEST['start_num'] = 0;
		foreach ( $data as $val ) {
			$value = mb_split("<>", $val);
			if ( $_REQUEST["_v"] == $value[0] ) { break; }
			$_REQUEST['start_num'] ++;
		}
	}
	$all_page = ceil ( count($data) / $view_data );
	
	
	# ページ処理
	if ( $_REQUEST['start_num'] ) { $start_num = $_REQUEST['start_num']; }
	else { $start_num = 0; }
	$last_num = $start_num + $view_data - 1;
	if ( $last_num > count($data) - 1 ) { $last_num = count($data) - 1; }
	$next_num = $last_num + 1;
	if ( $next_num >= count($data) ) { $next_num = ""; }
	$prev_num = $start_num - $view_data;
	if ( $prev_num  < 0 ) { $prev_num = 0;  }
	if ( $start_num < 1 ) { $prev_num = ""; }
	
	$now_page = ( INT ) ( $start_num / $view_data ) + 1;
	
	# テンプレートファイル読み込み
	$flag = "1";
	
	if ( file_exists("_tmpl/i".$_REQUEST["id"].".htm") ) { $tmpl_file = "i".$_REQUEST["id"]; }
	else { $tmpl_file = "i"; }
	
	$tmpl = @file("_tmpl/{$tmpl_file}.htm") or out_error("テンプレート処理ミスった！");
	
	foreach( $tmpl as $key => $val ) {
		if ( mb_ereg_match("(.*)<!--■■-->(.*)", $val) ) { $flag++; }
		if     ( $flag == "1" ) { $album_head .= $val; }
		elseif ( $flag == "2" ) { $album_main .= $val; }
		elseif ( $flag == "3" ) { $album_foot .= $val; }
	}
	
	if ( $prev_num !== "" ) {
		$prev_link  = "i.php?start_num={$prev_num}&id={$_REQUEST["id"]}&srt={$_REQUEST["srt"]}&tmpl={$_REQUEST["tmpl"]}&nc=1";
		$album_head = mb_ereg_replace("■back■", $prev_link, $album_head);
	}
	 else { $album_head = mb_ereg_replace("[^\n]*?■back■(.*?)\n", "", $album_head); }
	
	if ( $start_num > 0 ) {
		$top_link   = "i.php?start_num=0&id={$_REQUEST["id"]}&srt={$_REQUEST["srt"]}&tmpl={$_REQUEST["tmpl"]}&nc=1";
		$album_head = mb_ereg_replace("■top■", $top_link, $album_head);
	}
	 else { $album_head = mb_ereg_replace("[^\n]*?■top■(.*?)\n", "", $album_head); }

	if ( $next_num !== "" ) {
		$next_link  = "i.php?start_num={$next_num}&id={$_REQUEST["id"]}&srt={$_REQUEST["srt"]}&tmpl={$_REQUEST["tmpl"]}&nc=1";
		$album_head = mb_ereg_replace("■next■", $next_link, $album_head);
	}
	 else { $album_head = mb_ereg_replace("[^\n]*?■next■(.*?)\n", "", $album_head); }
	
	if ( $_REQUEST["srt"] == "1" ) { $srt_title = "ポイント順"; }
	else if ( $_REQUEST["srt"] == "2" ) { $srt_title = "新着コメント順"; }
	else if ( $_REQUEST["srt"] == "3" ) { $srt_title = "コメント数順"; }
	else if ( $_REQUEST["srt"] == "4" ) { $srt_title = "カテゴリ分類"; }
	else if ( $_REQUEST["srt"] == "5" ) { $srt_title = "撮影順(新→古)"; }
	else if ( $_REQUEST["srt"] == "6" ) { $srt_title = "新着写真順"; }
	else { $srt_title = "撮影順(古→新)"; }
	
	$album_head = mb_ereg_replace("■all_page■", $all_page, $album_head);
	$album_head = mb_ereg_replace("■now_page■", $now_page, $album_head);
	$album_head = mb_ereg_replace("■count■",    $count, $album_head);
	$album_head = mb_ereg_replace("■return■", $i_return_url, $album_head);
	$album_head = mb_ereg_replace("■id■",       $_REQUEST["id"], $album_head);
	$album_head = mb_ereg_replace("■sort■",     $srt_title, $album_head);
	$album_head = mb_ereg_replace("■input■",    "<a href=\"i.php?id={$_REQUEST["id"]}&mode=input\">", $album_head);
	
	$album_head = mb_ereg_replace("■srt_link1■", "<a href=\"i.php?id={$_REQUEST["id"]}&srt=0&tmpl={$_REQUEST["tmpl"]}\">", $album_head);
	$album_head = mb_ereg_replace("■srt_link2■", "<a href=\"i.php?id={$_REQUEST["id"]}&srt=2&tmpl={$_REQUEST["tmpl"]}\">", $album_head);
	$album_head = mb_ereg_replace("■srt_link3■", "<a href=\"i.php?id={$_REQUEST["id"]}&srt=3&tmpl={$_REQUEST["tmpl"]}\">", $album_head);
	$album_head = mb_ereg_replace("■srt_link4■", "<a href=\"i.php?id={$_REQUEST["id"]}&srt=1&tmpl={$_REQUEST["tmpl"]}\">", $album_head);
	$album_head = mb_ereg_replace("■srt_link5■", "<a href=\"i.php?id={$_REQUEST["id"]}&srt=4&tmpl={$_REQUEST["tmpl"]}\">", $album_head);
	$album_head = mb_ereg_replace("■srt_link6■", "<a href=\"i.php?id={$_REQUEST["id"]}&srt=5&tmpl={$_REQUEST["tmpl"]}\">", $album_head);
	$album_head = mb_ereg_replace("■srt_link7■", "<a href=\"i.php?id={$_REQUEST["id"]}&srt=6&tmpl={$_REQUEST["tmpl"]}\">", $album_head);
	$album_head = mb_ereg_replace("■fontsize■", $fontsize, $album_head);
	$album_head = mb_ereg_replace("■titleimg■", $confg_val[1], $album_head);
	$album_head = mb_ereg_replace("■title■",    $confg_val[3], $album_head);
	
	print $album_head;
	
	$m = 1;
	$find = time();
	
	for ( $i = $start_num; $i <= $last_num; $i++ ) {
		$album_body = $album_main;
		$value = mb_split("\t", $data[$i]);
		
		
		#__ EDITER__
$befores = mb_split(",", $before_exif);
$afters  = mb_split(",", $after_exif);
foreach ( $befores as $ed_key => $ed_val ) {
	for ( $ed_i = 12; $ed_i <= 37; $ed_i++ ) {
		if ( $ed_i != 21 and $ed_i != 28 and $ed_i != 31 and $ed_i != 32 ) {
			$value[$ed_i] = mb_eregi_replace($ed_val, $afters[$ed_key], $value[$ed_i]);
		}
	}
}
		
		if ( !$value[3] ) {
			$album_body = mb_ereg_replace("(.*)?■email■(.*)?\n", "", $album_body, "s");
		}
		if ( !$value[5] ) {
			$album_body = mb_ereg_replace("(.*)?■url■(.*)?\n", "", $album_body, "s");
		}
		
		if ( file_exists("{$img_dir}/{$confg_val[2]}/{$value[0]}m.jpg") ) { $i_img = "{$img_dir}/{$confg_val[2]}/{$value[0]}m.jpg"; }
		else { $i_img = "{$img_dir}/{$confg_val[2]}/{$value[0]}.jpg"; }
		$img = "i.php?view={$value[0]}";
		
		# ファイルサイズ計算
		$thum_size = (INT) ( @filesize("{$img_dir}/{$confg_val[2]}/{$value[0]}i.jpg") / 1000 );
		$img_size  = (INT) ( @filesize($i_img) / 1000 );
		$date = date("Y/m/d G:i:s", $value[0]);
		
		if ( !$value[7] ) { $value[7] = $def_color; }
		if ( $title_model == 1 and !$value[1] and $value[12] ) { $value[1] = $value[12]; }
		else if ( $title_model == 2 and !$value[1] and $value[2] ) { $value[1] = $value[2]; }
		else if ( !$value[1] ) { $value[1] = "無題"; }
		$dammy = mb_split("\(", $value[18]);
		if ( $dammy[2] ) { $value[18] = $dammy[0] . "(" . $dammy[2]; }
		# $edit = "i.php?mode=edit&id={$_REQUEST[id]}&num={$value[0]}&type=main&bs={$_REQUEST["srt"]}&bn={$_REQUEST["start_num"]}";
		
		$point  = @file("./_log/{$value[0]}.php");
		$rescnt = @file("./_res/{$value[0]}.php");
		if ( !$rescnt ) { $rescnt = 0; } else { $rescnt = count($rescnt); }
		
		if ( $_REQUEST["tmpl"] == "only" ) {
			$rate_width  = $only_max_width  / $value[8];
			$rate_height = $only_max_height / $value[9];
			if ( $rate_width < $rate_height ) { $rate = $rate_width; }
			else { $rate = $rate_height; }
			$value[8] = (int) ( $value[8] * $rate );
			$value[9] = (int) ( $value[9] * $rate );
		}
		
		$album_body = mb_ereg_replace("■num■",      $art_num[$value[0]], $album_body);
		$album_body = mb_ereg_replace("■vwidth■",    $view_width, $album_body);
		$album_body = mb_ereg_replace("■imgnum■",    $value[0], $album_body);
		$album_body = mb_ereg_replace("■title■",    $value[1], $album_body);
		$album_body = mb_ereg_replace("■name■",     $value[2], $album_body);
		$album_body = mb_ereg_replace("■email■",    $value[3], $album_body);
		$album_body = mb_ereg_replace("■url■",      $value[5], $album_body);
		$album_body = mb_ereg_replace("■comment■",  $value[6], $album_body);
		$album_body = mb_ereg_replace("■color■",    $value[7], $album_body);
		$album_body = mb_ereg_replace("■s_width■",  $value[8], $album_body);
		$album_body = mb_ereg_replace("■s_height■", $value[9], $album_body);
		$album_body = mb_ereg_replace("■width■",    $value[10], $album_body);
		$album_body = mb_ereg_replace("■height■",   $value[11], $album_body);
		$album_body = mb_ereg_replace("■model■",    $value[12], $album_body);
		$album_body = mb_ereg_replace("■iso■",      $value[13], $album_body);
		$album_body = mb_ereg_replace("■fnumber■",  $value[14], $album_body);
		$album_body = mb_ereg_replace("■picdate■",  $value[15], $album_body);
		$album_body = mb_ereg_replace("■ftype■",    $value[16], $album_body);
		$album_body = mb_ereg_replace("■bright■",   $value[17], $album_body);
		$album_body = mb_ereg_replace("■ftime■",    $value[18], $album_body);
		$album_body = mb_ereg_replace("■white■",    $value[19], $album_body);
		$album_body = mb_ereg_replace("■cat■",      $value[20], $album_body);
		$album_body = mb_ereg_replace("■add1■",     $value[22], $album_body);
		$album_body = mb_ereg_replace("■add2■",     $value[23], $album_body);
		$album_body = mb_ereg_replace("■add3■",     $value[24], $album_body);
		$album_body = mb_ereg_replace("■add4■",     $value[25], $album_body);
		$album_body = mb_ereg_replace("■add5■",     $value[26], $album_body);
		$album_body = mb_ereg_replace("■add6■",     $value[27], $album_body);
		$album_body = mb_ereg_replace("■flength■",  $value[29], $album_body);
		$album_body = mb_ereg_replace("■lense■",    $value[30], $album_body);
		
		$album_body = mb_ereg_replace("■maker■",    $value[33], $album_body);
		$album_body = mb_ereg_replace("■flash■",    $value[34], $album_body);
		$album_body = mb_ereg_replace("■etype■",    $value[35], $album_body);
		$album_body = mb_ereg_replace("■sharpnessnse■",    $value[36], $album_body);
		$album_body = mb_ereg_replace("■jpegq■",    $value[37], $album_body);
		
		$reslink = "<a href=\"i.php?id={$_REQUEST["id"]}&mode=res_form&num={$value[0]}&bs={$_REQUEST["srt"]}&bn={$_REQUEST["start_num"]}\">";
		$imglink = "album.php?id={$_REQUEST["id"]}&view={$value[0]}&fe={$find}";
		$topiclink = "<a href=\"i.php?id={$_REQUEST["id"]}&mode=view_res&num={$value[0]}&bs={$_REQUEST["srt"]}&bn={$_REQUEST["start_num"]}\">";
		if ( !$value[2] ) { $value[2] = "誰か"; }
		if ( !$value[6] ) { $value[6] = "本文無し"; }
		
		$album_body = mb_ereg_replace("■date■",    $date, $album_body);
		# $album_body = mb_ereg_replace("■edit■",    $edit, $album_body);
		$album_body = mb_ereg_replace("■point■",   $point[0], $album_body);
		$album_body = mb_ereg_replace("■rescnt■",  $rescnt, $album_body); // レス数
		$album_body = mb_ereg_replace("■reslink■", $reslink,  $album_body); // コメントリンク
		$album_body = mb_ereg_replace("■topiclink■", $topiclink,  $album_body); // 単体記事表示リンク
		
		$album_body = mb_ereg_replace("■thum_i■", "{$img_dir}/{$confg_val[2]}/{$value[0]}i.jpg",  $album_body);
		$album_body = mb_ereg_replace("■thum_size■", $thum_size, $album_body);
		
		$album_body = mb_ereg_replace("■img_size■", $img_size, $album_body);
		$album_body = mb_ereg_replace("■imgurl■",   $imglink,  $album_body); // 画像アドレス（パソコン用）
		$album_body = mb_ereg_replace("■imgiurl■", "i.php?gn={$confg_val[2]}&view={$value[0]}&ni={$_REQUEST["id"]}", $album_body); // 画像アドレス（携帯用）
		$album_body = mb_ereg_replace("■img_i■", $i_img, $album_body); // 画像パス（携帯用）
		
		print $album_body;
	}
	
	$album_foot = mb_ereg_replace("■all_page■", $all_page, $album_foot);
	$album_foot = mb_ereg_replace("■now_page■", $now_page, $album_foot);
	
	$album_foot = mb_ereg_replace("■srt_link1■", "<a href=\"i.php?id={$_REQUEST["id"]}&srt=0&tmpl={$_REQUEST["tmpl"]}\">", $album_foot);
	$album_foot = mb_ereg_replace("■srt_link2■", "<a href=\"i.php?id={$_REQUEST["id"]}&srt=2&tmpl={$_REQUEST["tmpl"]}\">", $album_foot);
	$album_foot = mb_ereg_replace("■srt_link3■", "<a href=\"i.php?id={$_REQUEST["id"]}&srt=3&tmpl={$_REQUEST["tmpl"]}\">", $album_foot);
	$album_foot = mb_ereg_replace("■srt_link4■", "<a href=\"i.php?id={$_REQUEST["id"]}&srt=1&tmpl={$_REQUEST["tmpl"]}\">", $album_foot);
	$album_foot = mb_ereg_replace("■srt_link5■", "<a href=\"i.php?id={$_REQUEST["id"]}&srt=4&tmpl={$_REQUEST["tmpl"]}\">", $album_foot);
	$album_foot = mb_ereg_replace("■srt_link6■", "<a href=\"i.php?id={$_REQUEST["id"]}&srt=5&tmpl={$_REQUEST["tmpl"]}\">", $album_foot);
	$album_foot = mb_ereg_replace("■srt_link7■", "<a href=\"i.php?id={$_REQUEST["id"]}&srt=6&tmpl={$_REQUEST["tmpl"]}\">", $album_foot);
	
	if ( $prev_num !== "" ) {
		$album_foot = mb_ereg_replace("■back■", $prev_link, $album_foot);
	} else { $album_foot = mb_ereg_replace("[^\n]*?■back■(.*?)\n", "", $album_foot); }
	if ( $start_num > 0 ) {
		$album_foot = mb_ereg_replace("■top■", $top_link, $album_foot);
	} else { $album_foot = mb_ereg_replace("[^\n]*?■top■(.*?)\n", "", $album_foot); }
	if ( $next_num !== "" ) {
		$album_foot = mb_ereg_replace("■next■", $next_link, $album_foot);
	} else { $album_foot = mb_ereg_replace("[^\n]*?■next■(.*?)\n", "", $album_foot); }
	
	print $album_foot;
	exit;
}



function chck_accs() {
	global $confg_val, $srt_switch;
	
	if ( $confg_val[9] and $confg_val[10] and $_REQUEST["mode"] != "title" and !$srt_switch ) {
		if ( !$_SESSION["pass"] or $_SESSION["pass"] != $confg_val[10] or $_SESSION["usr"] != $confg_val[9] ) {
		$_SESSION["usr"] = ""; $_SESSION["pass"] = "";
		
		out_head();
		print <<<EOD
<form action="i.php" method="post">
<input type="hidden" name="id" value="{$_REQUEST[id]}">
+ アクセス認証 +<br>
このアルバムを閲覧するには認証が必要です。<br>
ID：<input name="usr" type="text" size="10"><br>
PASS：<input name="pass" type="password" size="10"><br>
<input type="submit" value="認証">
</form>
EOD;
		out_footer();
		exit;
		}
	}
}

function out_head() {
	global $fontsize;
	print <<< EOD
<html>
<head>
<title>Album Factory</title>
</head>
<body>
EOD;
}

function out_footer() {
	global $copy;
	print "<BR>{$copy}";
	print "</body></html>";
}

function make_name($m) {
	srand( (double)microtime() * time() );
	$add_words = NULL;
	$f_words = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
	$f_word = preg_split("//", $f_words, 0, PREG_SPLIT_NO_EMPTY);
	for( $i =0 ; $i < $m; $i++ ) {
		$add_words .= $f_word[array_rand($f_word, 1)];
	}
	return($add_words);
}

function out_error($mssg) {
	out_head();
	print "<b>ヤバイ！！</b>";
	print "<BR>{$mssg}";
	out_footer();
	exit;
}

function set_form() {
	foreach ($_REQUEST as $key => $val ) {
		if ( mb_strpos($val, "file") === FALSE ) {
			if ( get_magic_quotes_gpc() ) { $val = stripslashes($val); }
			$val = htmlspecialchars($val, ENT_QUOTES);
			$val = mb_ereg_replace("\t", "&nsbp;&nsbp;&nsbp;&nsbp;", $val);
			$_REQUEST["$key"] = mb_ereg_replace("\r\n|\n|\r", "<br>", $val);
		}
	}
}
?>