#webliberty::App::Diary.pm (2006/03/11)
#Copyright(C) 2002-2006 Knight, All rights reserved.

package webliberty::App::Diary;

use strict;
use base qw(webliberty::Basis Exporter);
use webliberty::String;
use webliberty::File;
use webliberty::Date;
use webliberty::Script;
use webliberty::App::Admin;

our @EXPORT = qw(error);

### コンストラクタ
sub new {
	my $class = shift;

	my $self = {
		init      => shift,
		config    => shift,
		query     => shift,
		agent     => undef,
		field     => undef,
		comment   => undef,
		tracktack => undef
	};
	bless $self, $class;

	my $i;

	open(FH, "$self->{init}->{data_dir}$self->{init}->{data_field}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_field}");
	while (<FH>) {
		chomp;

		$self->{field}->{$_} = ++$i;
	}
	close(FH);

	if (!$self->{query}->{mode}) {
		open(FH, "$self->{init}->{data_dir}$self->{init}->{data_comt_dir}$self->{init}->{data_comt_index}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_comt_dir}$self->{init}->{data_comt_index}");
		while (<FH>) {
			chomp;
			my($no, $pno, $stat, $date, $name, $subj, $host) = split(/\t/);

			$self->{comment}->{$pno}++;
		}
		close(FH);
	}

	if (!$self->{query}->{mode}) {
		open(FH, "$self->{init}->{data_dir}$self->{init}->{data_tb_dir}$self->{init}->{data_tb_index}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_tb_dir}$self->{init}->{data_tb_index}");
		while (<FH>) {
			chomp;
			my($no, $pno, $stat, $date, $blog, $title, $url) = split(/\t/);

			$self->{trackback}->{$pno}++;
		}
		close(FH);
	}

	return $self;
}

### 閲覧環境設定
sub set_agent {
	my $self  = shift;
	my $agent = shift;

	$self->{agent} = $agent;

	return;
}

### 基本情報作成
sub info {
	my $self = shift;

	my $page_ins = new webliberty::String($self->{query}->{page});
	$page_ins->create_number;

	my $info_path;
	if ($self->{init}->{script_file} =~ /([^\/\\]*)$/) {
		$info_path = "$self->{config}->{site_url}$1";
	}

	my $info_tbpath;
	if ($self->{init}->{tb_file} =~ /([^\/\\]*)$/) {
		$info_tbpath = "$self->{config}->{site_url}$1";
	}

	my $info_paintpath;
	if ($self->{init}->{paint_file} =~ /([^\/\\]*)$/) {
		$info_paintpath = "$self->{config}->{site_url}$1";
	}

	my($info_user, $info_user_start, $info_user_end);
	$info_user       = '';
	$info_user_start = '<!--';
	$info_user_end   = '-->';

	return(
		INFO_FILE        => $self->{init}->{script_file},
		INFO_TITLE       => $self->{config}->{site_title},
		INFO_BACK        => $self->{config}->{back_url},
		INFO_MOBILETITLE => $self->{config}->{mobile_site_title},
		INFO_MOBILEBACK  => $self->{config}->{mobile_back_url},
		INFO_DESCRIPTION => $self->{config}->{site_description},
		INFO_URL         => $self->{config}->{site_url},
		INFO_PATH        => $info_path,
		INFO_TBPATH      => $info_tbpath,
		INFO_PAINTPATH   => $info_paintpath,
		INFO_PAGE        => $page_ins->get_string,
		INFO_USER        => $info_user,
		INFO_USER_START  => $info_user_start,
		INFO_USER_END    => $info_user_end,
		INFO_TIMESTAMP   => time
	);
}

### 記事フォーム作成
sub diary_form {
	my $self = shift;
	my($no, $id, $stat, $break, $comt, $tb, $field, $date, $name, $subj, $text, $color, $icon, $file, $host) = @_;

	my $no_ins    = new webliberty::String($no);
	my $id_ins    = new webliberty::String($id);
	my $stat_ins  = new webliberty::String($stat);
	my $break_ins = new webliberty::String($break);
	my $comt_ins  = new webliberty::String($comt);
	my $tb_ins    = new webliberty::String($tb);
	my $field_ins = new webliberty::String($field);
	my $date_ins  = new webliberty::String($date);
	my $name_ins  = new webliberty::String($name);
	my $subj_ins  = new webliberty::String($subj);
	my $text_ins  = new webliberty::String($text);
	my $icon_ins  = new webliberty::String($icon);
	my $file_ins  = new webliberty::String($file);
	my $host_ins  = new webliberty::String($host);

	$no_ins->create_number;
	$id_ins->create_line;
	$stat_ins->create_number;
	$break_ins->create_number;
	$comt_ins->create_number;
	$tb_ins->create_number;
	$field_ins->create_line;
	$date_ins->create_line;
	$name_ins->create_line;
	$subj_ins->create_line;
	$text_ins->create_text;
	$icon_ins->create_line;
	$file_ins->create_line;
	$host_ins->create_line;

	$id_ins->create_plain;
	$field_ins->create_plain;
	$date_ins->create_plain;
	$name_ins->create_plain;
	$subj_ins->create_plain;
	$text_ins->create_plain;
	$icon_ins->create_plain;
	$file_ins->create_plain;
	$host_ins->create_plain;

	my $form_stat;
	if ($stat_ins->get_string == 1) {
		$form_stat = ' checked="checked"';
	} else {
		$form_stat = '';
	}

	my $form_break;
	if ($break_ins->get_string == 1) {
		$form_break = ' checked="checked"';
	} else {
		$form_break = '';
	}

	my $form_comt;
	if ($comt_ins->get_string == 1) {
		$form_comt = ' checked="checked"';
	} else {
		$form_comt = '';
	}

	my $form_tb;
	if ($tb_ins->get_string == 1) {
		$form_tb = ' checked="checked"';
	} else {
		$form_tb = '';
	}

	my($form_field, $form_field_start, $form_field_end, $i);
	if (!$self->{config}->{use_field}) {
		$form_field_start = '<!--';
		$form_field_end   = '-->';
	}

	open(FH, "$self->{init}->{data_dir}$self->{init}->{data_field}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_field}");
	while (<FH>) {
		chomp;
		my($field, $child) = split(/<>/);

		$i++;

		if ($child) {
			$field = "└ $child";
		}

		if ($field_ins->get_string eq $_ or $field_ins->get_string == $i) {
			$form_field .= "<option value=\"$i\" lang=\"ja\" selected=\"selected\">$field</option>";
		} else {
			$form_field .= "<option value=\"$i\" lang=\"ja\">$field</option>";
		}
	}
	close(FH);

	if (!$form_field) {
		$form_field = '<option value="" lang="ja">分類が登録されていません</option>';
	} else {
		$form_field = "<option value=\"\" lang=\"ja\">選択してください</option>$form_field";
	}
	$form_field = "<select name=\"field\">$form_field</select>";

	my($form_date, $form_year, $form_month, $form_day, $form_hour, $form_minute);

	if ($date_ins->get_string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
		$form_year   = $1;
		$form_month  = $2;
		$form_day    = $3;
		$form_hour   = $4;
		$form_minute = $5;
	}

	if ($self->{agent} eq 'mobile') {
		$form_date .= "<input type=\"text\" name=\"year\" size=\"4\" value=\"$form_year\">年";
		$form_date .= "<input type=\"text\" name=\"month\" size=\"2\" value=\"$form_month\">月";
		$form_date .= "<input type=\"text\" name=\"day\" size=\"2\" value=\"$form_day\">日";
		$form_date .= "<input type=\"text\" name=\"hour\" size=\"2\" value=\"$form_hour\">時";
		$form_date .= "<input type=\"text\" name=\"minute\" size=\"2\" value=\"$form_minute\">分";
	} else {
		$form_date .= '<select name="year">';
		foreach ($form_year - 5 .. $form_year + 5) {
			if ($form_year == $_) {
				$form_date .= "<option value=\"$_\" lang=\"ja\" selected=\"selected\">$_年</option>";
			} else {
				$form_date .= "<option value=\"$_\" lang=\"ja\">$_年</option>";
			}
		}
		$form_date .= '</select>';

		$form_date .= '<select name="month">';
		foreach (1 .. 12) {
			my $month = sprintf("%02d", $_);
			if ($month == $form_month) {
				$form_date .= "<option value=\"$month\" lang=\"ja\" selected=\"selected\">$_月</option>";
			} else {
				$form_date .= "<option value=\"$month\" lang=\"ja\">$_月</option>";
			}
		}
		$form_date .= '</select>';

		$form_date .= '<select name="day">';
		foreach (1 .. 31) {
			my $day = sprintf("%02d", $_);
			if ($day == $form_day) {
				$form_date .= "<option value=\"$day\" lang=\"ja\" selected=\"selected\">$_日</option>";
			} else {
				$form_date .= "<option value=\"$day\" lang=\"ja\">$_日</option>";
			}
		}
		$form_date .= '</select>';

		$form_date .= '<select name="hour">';
		foreach (0 .. 23) {
			my $hour = sprintf("%02d", $_);
			if ($hour == $form_hour) {
				$form_date .= "<option value=\"$hour\" lang=\"ja\" selected=\"selected\">$_時</option>";
			} else {
				$form_date .= "<option value=\"$hour\" lang=\"ja\">$_時</option>";
			}
		}
		$form_date .= '</select>';

		$form_date .= '<select name="minute">';
		foreach (0 .. 59) {
			my $minute = sprintf("%02d", $_);
			if ($minute == $form_minute) {
				$form_date .= "<option value=\"$minute\" lang=\"ja\" selected=\"selected\">$_分</option>";
			} else {
				$form_date .= "<option value=\"$minute\" lang=\"ja\">$_分</option>";
			}
		}
		$form_date .= '</select>';
	}

	my($form_icon, $form_icon_start, $form_icon_end);
	if (!$self->{config}->{use_icon}) {
		$form_icon_start = '<!--';
		$form_icon_end   = '-->';
	}

	open(FH, "$self->{init}->{data_dir}$self->{init}->{data_icon}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_icon}");
	while (<FH>) {
		chomp;
		my($file, $name, $field, $user, $pwd) = split(/\t/);

		if ($icon_ins->get_string eq $file) {
			$form_icon .= "<option value=\"$file\" lang=\"ja\" selected=\"selected\">$name</option>";
		} else {
			$form_icon .= "<option value=\"$file\" lang=\"ja\">$name</option>";
		}
	}
	close(FH);

	if (!$form_icon) {
		$form_icon = '<option value="" lang="ja">アイコンが登録されていません</option>';
	} else {
		$form_icon = "<option value=\"\" lang=\"ja\">選択してください</option>$form_icon";
	}
	$form_icon = "<select name=\"icon\">$form_icon</select>";

	my($form_file1, $form_file2, $form_file3, $form_file4, $form_file5) = split(/<>/, $file_ins->get_string);
	my($form_file_start, $form_file_end);
	if (!$self->{config}->{use_file}) {
		$form_file_start = '<!--';
		$form_file_end   = '-->';
	}

	if ($self->{query}->{work} eq 'edit') {
		my($check1, $check2, $check3, $check4, $check5);

		if ($self->{query}->{delfile1}) {
			$check1 = ' checked="checked"';
		}
		if ($self->{query}->{delfile2}) {
			$check2 = ' checked="checked"';
		}
		if ($self->{query}->{delfile3}) {
			$check3 = ' checked="checked"';
		}
		if ($self->{query}->{delfile4}) {
			$check4 = ' checked="checked"';
		}
		if ($self->{query}->{delfile5}) {
			$check5 = ' checked="checked"';
		}

		if ($form_file1) {
			$form_file1 = "<input type=\"checkbox\" name=\"delfile1\" id=\"delfile1_checkbox\" value=\"on\"$check1> <label for=\"delfile1_checkbox\">$form_file1を削除</label>";
		}
		if ($form_file2) {
			$form_file2 = "<input type=\"checkbox\" name=\"delfile2\" id=\"delfile2_checkbox\" value=\"on\"$check2> <label for=\"delfile2_checkbox\">$form_file2を削除</label>";
		}
		if ($form_file3) {
			$form_file3 = "<input type=\"checkbox\" name=\"delfile3\" id=\"delfile3_checkbox\" value=\"on\"$check3> <label for=\"delfile3_checkbox\">$form_file3を削除</label>";
		}
		if ($form_file4) {
			$form_file4 = "<input type=\"checkbox\" name=\"delfile4\" id=\"delfile4_checkbox\" value=\"on\"$check4> <label for=\"delfile4_checkbox\">$form_file4を削除</label>";
		}
		if ($form_file5) {
			$form_file5 = "<input type=\"checkbox\" name=\"delfile5\" id=\"delfile5_checkbox\" value=\"on\"$check5> <label for=\"delfile5_checkbox\">$form_file5を削除</label>";
		}
	}

	my($form_tburl_start, $form_tburl_end);
	if (!$self->{config}->{use_file}) {
		$form_tburl_start = '<!--';
		$form_tburl_end   = '-->';
	}

	return(
		FORM_NO          => $no_ins->get_string,
		FORM_STAT        => $form_stat,
		FORM_BREAK       => $form_break,
		FORM_COMT        => $form_comt,
		FORM_TB          => $form_tb,
		FORM_FIELD       => $form_field,
		FORM_FIELD_START => $form_field_start,
		FORM_FIELD_END   => $form_field_end,
		FORM_DATE        => $form_date,
		FORM_NAME        => $name_ins->get_string,
		FORM_SUBJ        => $subj_ins->get_string,
		FORM_TEXT        => $text_ins->get_string,
		FORM_ICON        => $form_icon,
		FORM_ICON_START  => $form_icon_start,
		FORM_ICON_END    => $form_icon_end,
		FORM_FILE1       => $form_file1,
		FORM_FILE2       => $form_file2,
		FORM_FILE3       => $form_file3,
		FORM_FILE4       => $form_file4,
		FORM_FILE5       => $form_file5,
		FORM_FILE_START  => $form_file_start,
		FORM_FILE_END    => $form_file_end,
		FORM_HOST        => $host_ins->get_string,
		FORM_TBURL_START => $form_tburl_start,
		FORM_TBURL_END   => $form_tburl_end,
	);
}

### コメントフォーム作成
sub comment_form {
	my $self = shift;
	my($no, $pno, $stat, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $rank, $pwd, $host) = @_;

	my $no_ins    = new webliberty::String($no);
	my $pno_ins   = new webliberty::String($pno);
	my $stat_ins  = new webliberty::String($stat);
	my $date_ins  = new webliberty::String($date);
	my $name_ins  = new webliberty::String($name);
	my $mail_ins  = new webliberty::String($mail);
	my $url_ins   = new webliberty::String($url);
	my $subj_ins  = new webliberty::String($subj);
	my $text_ins  = new webliberty::String($text);
	my $icon_ins  = new webliberty::String($icon);
	my $file_ins  = new webliberty::String($file);
	my $rank_ins  = new webliberty::String($rank);
	my $pwd_ins   = new webliberty::String($pwd);
	my $host_ins  = new webliberty::String($host);

	$no_ins->create_number;
	$pno_ins->create_number;
	$stat_ins->create_number;
	$date_ins->create_line;
	$name_ins->create_line;
	$mail_ins->create_line;
	$url_ins->create_line;
	$subj_ins->create_line;
	$text_ins->create_text;
	$icon_ins->create_line;
	$file_ins->create_line;
	$rank_ins->create_line;
	$pwd_ins->create_line;
	$host_ins->create_line;

	$date_ins->create_plain;
	$name_ins->create_plain;
	$mail_ins->create_plain;
	$url_ins->create_plain;
	$subj_ins->create_plain;
	$text_ins->create_plain;
	$icon_ins->create_plain;
	$file_ins->create_plain;
	$rank_ins->create_plain;
	$pwd_ins->create_plain;
	$host_ins->create_plain;

	if (!$url_ins->get_string) {
		$url_ins->set_string('http://');
	}

	my($form_icon, $form_icon_start, $form_icon_end);
	if (!$self->{config}->{use_icon}) {
		$form_icon_start = '<!--';
		$form_icon_end   = '-->';
	}

	open(FH, "$self->{init}->{data_dir}$self->{init}->{data_icon}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_icon}");
	while (<FH>) {
		chomp;
		my($file, $name, $field, $user, $pwd) = split(/\t/);

		if ($icon_ins->get_string eq $file) {
			$form_icon .= "<option value=\"$file\" lang=\"ja\" selected=\"selected\">$name</option>";
		} else {
			$form_icon .= "<option value=\"$file\" lang=\"ja\">$name</option>";
		}
	}
	close(FH);

	if (!$form_icon) {
		$form_icon = '<option value="" lang="ja">アイコンが登録されていません</option>';
	} else {
		$form_icon = "<option value=\"\" lang=\"ja\">選択してください</option>$form_icon";
	}
	$form_icon = "<select name=\"icon\">$form_icon</select>";

	return(
		FORM_NO         => $no_ins->get_string,
		FORM_PNO        => $pno_ins->get_string,
		FORM_DATE       => $date_ins->get_string,
		FORM_NAME       => $name_ins->get_string,
		FORM_MAIL       => $mail_ins->get_string,
		FORM_URL        => $url_ins->get_string,
		FORM_SUBJ       => $subj_ins->get_string,
		FORM_TEXT       => $text_ins->get_string,
		FORM_FILE       => $file_ins->get_string,
		FORM_ICON       => $form_icon,
		FORM_ICON_START => $form_icon_start,
		FORM_ICON_END   => $form_icon_end,
		FORM_PWD        => $pwd_ins->get_string,
		FORM_HOST       => $host_ins->get_string
	);
}

### 記事データ作成
sub diary_article {
	my $self = shift;
	my($no, $id, $stat, $break, $comt, $tb, $field, $date, $name, $subj, $text, $color, $icon, $file, $host) = @_;

	my $no_ins    = new webliberty::String($no);
	my $id_ins    = new webliberty::String($id);
	my $stat_ins  = new webliberty::String($stat);
	my $break_ins = new webliberty::String($break);
	my $comt_ins  = new webliberty::String($comt);
	my $tb_ins    = new webliberty::String($tb);
	my $field_ins = new webliberty::String($field);
	my $date_ins  = new webliberty::String($date);
	my $name_ins  = new webliberty::String($name);
	my $subj_ins  = new webliberty::String($subj);
	my $text_ins  = new webliberty::String($text);
	my $icon_ins  = new webliberty::String($icon);
	my $file_ins  = new webliberty::String($file);
	my $host_ins  = new webliberty::String($host);

	$no_ins->create_number;
	$id_ins->create_line;
	$stat_ins->create_number;
	$break_ins->create_number;
	$comt_ins->create_number;
	$tb_ins->create_number;
	$field_ins->create_line;
	$date_ins->create_line;
	$name_ins->create_line;
	$subj_ins->create_line;
	$text_ins->create_text;
	$icon_ins->create_line;
	$file_ins->create_line;
	$host_ins->create_line;

	if ($stat_ins->get_string == 1) {
		$stat_ins->set_string('公開');
	} else {
		$stat_ins->set_string('下書き');
	}

	my($article_new_start, $article_new_end);
	if (time - $date_ins->get_string > 60 * 60 * 24 * $self->{config}->{new_days}) {
		$article_new_start = '<!--';
		$article_new_end   = '-->';
	}

	my($article_year, $article_month, $article_day, $article_hour, $article_minute, $article_week, $article_date_start, $article_date_end);
	if ($date_ins->get_string =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
		my $day_ins  = new webliberty::Date;
		my $week = $day_ins->get_week($1, $2, $3);

		my @week = ('日', '月', '火', '水', '木', '金', '土');

		$article_year   = $1;
		$article_month  = $2;
		$article_day    = $3;
		$article_hour   = $4;
		$article_minute = $5;
		$article_week   = $week[$week];

		if ($self->{agent} eq 'mobile') {
			$date_ins->set_string("$article_month/$article_day-$article_hour:$article_minute");
		} else {
			$date_ins->set_string("$article_year年$article_month月$article_day日($article_week)$article_hour時$article_minute分");
		}
	} else {
		$article_date_start = '<!--';
		$article_date_end   = '-->';
	}

	if ($field_ins->get_string =~ /^(.+)<>(.+)$/) {
		$field_ins->set_string("$1::$2");
	} elsif (!$field_ins->get_string) {
		$field_ins->set_string('未分類');
	}
	my($article_field_start, $article_field_end);
	if (!$self->{config}->{use_field}) {
		$article_field_start = '<!--';
		$article_field_end   = '-->';
	}

	if (!$name_ins->get_string) {
		$name_ins->set_string('管理者');
	}

	if (!$subj_ins->get_string) {
		$subj_ins->set_string('No Title');
	}

	if (!$break_ins->get_string) {
		$text_ins->replace_string('<br />', "\n");
	}

	$text_ins->permit_html;

	if ($self->{config}->{autolink_mode}) {
		$text_ins->create_link;
	}
	if ($text_ins->get_string =~ /\$PAINT(\d+)/) {
		my $info_paint_path;
		if ($self->{init}->{data_dir} =~ /([^\/\\]*\/)$/) {
			$info_paint_path = "$self->{config}->{site_url}$1$self->{init}->{paint_dir}";
		}

		opendir(DIR, "$self->{init}->{data_dir}$self->{init}->{paint_dir}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{paint_dir}");
		my @files = sort { $a <=> $b } grep { m/\d+\.\w+/g } readdir(DIR);
		close(DIR);

		my $i = $files[$#files] + 0;

		foreach (1 .. $files[$#files]) {
			if ($text_ins->get_string =~ /\$PAINT$i/) {
				my $file_ins  = new webliberty::File("$self->{init}->{data_dir}$self->{init}->{paint_dir}$files[$i-1]");
				my $file_name = $file_ins->get_name . '.' . $file_ins->get_ext;
				my($width, $height) = $file_ins->get_size;

				if ($self->{agent} eq 'mobile') {
					$text_ins->replace_string('\$PAINT' . $i, "<a href=\"$info_paint_path$files[$i-1]\">ペイントファイル $files[$i-1]</a>");
				} else {
					$text_ins->replace_string('\$PAINT' . $i, "<img src=\"$info_paint_path$files[$i-1]\" alt=\"ペイントファイル $files[$i-1]\" width=\"$width\" height=\"$height\" />");
				}
			}

			$i--;
		}
	}

	my($article_file1, $article_file2, $article_file3, $article_file4, $article_file5) = split(/<>/, $file_ins->get_string);

	my($article_icon_start, $article_icon_end);
	if ($icon_ins->get_string) {
		$icon_ins->set_string("<img src=\"$self->{init}->{data_dir}$self->{init}->{data_icon_dir}" . $icon_ins->get_string . "\" alt=\"アイコン\" />");
	} else {
		$article_icon_start = '<!--';
		$article_icon_end   = '-->';
	}

	my($article_files, $article_file_start, $article_file_end);

	my $info_path;
	if ($self->{init}->{script_file} =~ /([^\/\\]*)$/) {
		$info_path = "$self->{config}->{site_url}$1";
	}

	my($info_upfile_path, $info_thumbnail_path);
	if ($self->{init}->{data_dir} =~ /([^\/\\]*\/)$/) {
		$info_upfile_path    = "$self->{config}->{site_url}$1$self->{init}->{data_upfile_dir}";
		$info_thumbnail_path = "$self->{config}->{site_url}$1$self->{init}->{data_thumbnail_dir}";
	}

	if ($article_file1 or $article_file2 or $article_file3 or $article_file4 or $article_file5) {
		my $i;

		foreach ($article_file1, $article_file2, $article_file3, $article_file4, $article_file5) {
			my $file_ins = new webliberty::File("$self->{init}->{data_dir}$self->{init}->{data_upfile_dir}$_");
			my($width, $height) = $file_ins->get_size;

			my $flag;
			if ($width > $self->{config}->{img_maxwidth}) {
				$height = int($height / ($width / $self->{config}->{img_maxwidth}));
				$width  = $self->{config}->{img_maxwidth};

				$flag = 1;
			}

			my($file_path, $file);
			if ($self->{config}->{thumbnail_mode} and -e "$self->{init}->{data_dir}$self->{init}->{data_thumbnail_dir}$_") {
				$file_path = $info_thumbnail_path;
			} else {
				$file_path = $info_upfile_path;
			}
			if ($_) {
				if ($self->{agent} eq 'mobile' and $width > 0 and $height > 0) {
					$file = "<a href=\"$file_path$_\">アップロードファイル $_</a><br />";
				} elsif ($flag and $width > 0 and $height > 0) {
					$file = "<a href=\"$info_path?mode=image&amp;file=$_\"><img src=\"$file_path$_\" alt=\"アップロードファイル $_\" width=\"$width\" height=\"$height\" /></a>";
				} elsif ($width > 0 and $height > 0) {
					$file = "<img src=\"$file_path$_\" alt=\"アップロードファイル $_\" width=\"$width\" height=\"$height\" />";
				} else {
					$file = "<a href=\"$info_upfile_path$_\">アップロードファイル $_</a><br />";
				}
			}

			$i++;

			if ($text_ins->get_string =~ /\$FILE$i/) {
				$text_ins->replace_string('\$FILE' . $i, $file);
			} else {
				$article_files .= $file;
			}
		}
	}
        if (!$article_files) {
		$article_file_start = '<!--';
		$article_file_end   = '-->';
	}

	if ($article_file1) {
		$article_file1 = "<a href=\"$info_path$article_file1\">$article_file1</a>";
	}
	if ($article_file2) {
		$article_file2 = "<a href=\"$info_path$article_file2\">$article_file2</a>";
	}
	if ($article_file3) {
		$article_file3 = "<a href=\"$info_path$article_file3\">$article_file3</a>";
	}
	if ($article_file4) {
		$article_file4 = "<a href=\"$info_path$article_file4\">$article_file4</a>";
	}
	if ($article_file5) {
		$article_file5 = "<a href=\"$info_path$article_file5\">$article_file5</a>";
	}

	if ($text_ins->get_string =~ /[^-]*(-----)[^-]*/) {
		my $spliter = $1;

		my $info_path;
		if ($self->{init}->{script_file} =~ /([^\/\\]*)$/) {
			$info_path = "$self->{config}->{site_url}$1";
		}

		if ($self->{query}->{mode}) {
			if ($self->{agent} eq 'mobile') {
				$text = (split(/$spliter/, $text_ins->get_string, 2))[1];
				$text_ins->set_string("<a href=\"$info_path?no=$no\">前に戻る</a>$text");
			} else {
				$text_ins->replace_string($spliter, '');
			}
		} else {
			$text = (split(/$spliter/, $text_ins->get_string, 2))[0];
			if ($self->{agent} eq 'mobile') {
				$text_ins->set_string("$text<a href=\"$info_path?mode=continue&amp;no=$no\">続きを読む</a>");
			} else {
				$text_ins->set_string("$text<a href=\"$info_path?mode=comment&amp;no=$no\">続きを読む</a>");
			}
		}
	}

	if ($break_ins->get_string) {
		$text_ins->replace_string('<br /><br />', '</p><p>');
		$text_ins->set_string('<p>' . $text_ins->get_string . '</p>');
	}

	my $article_url;
	if ($no_ins->get_string) {
		if ($self->{config}->{html_archive_mode}) {
			if ($self->{init}->{archive_dir} =~ /([^\/\\]*\/)$/) {
				$article_url = "$self->{config}->{site_url}$1" . $no_ins->get_string . "\.$self->{init}->{archive_ext}";
			}
		} else {
			if ($self->{init}->{script_file} =~ /([^\/\\]*)$/) {
				$article_url = "$self->{config}->{site_url}$1?no=" . $no_ins->get_string;
			}
		}
	} else {
		$article_url = $self->{config}->{site_url};
	}

	my($article_comment, $article_comment_start, $article_comment_end);
	if ($self->{comment}->{$no_ins->get_string}) {
		$article_comment = $self->{comment}->{$no_ins->get_string};
	} else {
		$article_comment = 0;
	}
	if ($self->{query}->{mode} or !$comt_ins->get_string) {
		$article_comment_start = '<!--';
		$article_comment_end   = '-->';
	}

	my($article_trackback, $article_trackback_start, $article_trackback_end);
	if ($self->{trackback}->{$no_ins->get_string}) {
		$article_trackback = $self->{trackback}->{$no_ins->get_string};
	} else {
		$article_trackback = 0;
	}
	if ($self->{query}->{mode} or !$tb_ins->get_string) {
		$article_trackback_start = '<!--';
		$article_trackback_end   = '-->';
	}

	my $app_ins = new webliberty::App::Admin($self->{init}, $self->{config}, $self->{query});

	my($article_admin_start, $article_admin_end);
	if ($self->{query}->{mode} or *STDOUT ne "*main::STDOUT" or !$app_ins->check_password or !$no_ins->get_string) {
		$article_admin_start = '<!--';
		$article_admin_end   = '-->';
	}

	return(
		ARTICLE_NO              => $no_ins->get_string,
		ARTICLE_STAT            => $stat_ins->get_string,
		ARTICLE_FIELD           => $field_ins->get_string,
		ARTICLE_FIELD_START     => $article_field_start,
		ARTICLE_FIELD_END       => $article_field_end,
		ARTICLE_FNO             => $self->{field}->{$field},
		ARTICLE_DATE            => $date_ins->get_string,
		ARTICLE_DATE_START      => $article_date_start,
		ARTICLE_DATE_END        => $article_date_end,
		ARTICLE_YEAR            => $article_year,
		ARTICLE_MONTH           => $article_month,
		ARTICLE_DAY             => $article_day,
		ARTICLE_HOUR            => $article_hour,
		ARTICLE_MINUTE          => $article_minute,
		ARTICLE_WEEK            => $article_week,
		ARTICLE_NAME            => $name_ins->get_string,
		ARTICLE_SUBJ            => $subj_ins->get_string,
		ARTICLE_TEXT            => $text_ins->get_string,
		ARTICLE_ICON            => $icon_ins->get_string,
		ARTICLE_ICON_START      => $article_icon_start,
		ARTICLE_ICON_END        => $article_icon_end,
		ARTICLE_FILE1           => $article_file1,
		ARTICLE_FILE2           => $article_file2,
		ARTICLE_FILE3           => $article_file3,
		ARTICLE_FILE4           => $article_file4,
		ARTICLE_FILE5           => $article_file5,
		ARTICLE_FILES           => $article_files,
		ARTICLE_FILE_START      => $article_file_start,
		ARTICLE_FILE_END        => $article_file_end,
		ARTICLE_HOST            => $host_ins->get_string,
		ARTICLE_URL             => $article_url,
		ARTICLE_NEW_START       => $article_new_start,
		ARTICLE_NEW_END         => $article_new_end,
		ARTICLE_COMMENT         => $article_comment,
		ARTICLE_COMMENT_START   => $article_comment_start,
		ARTICLE_COMMENT_END     => $article_comment_end,
		ARTICLE_TRACKBACK       => $article_trackback,
		ARTICLE_TRACKBACK_START => $article_trackback_start,
		ARTICLE_TRACKBACK_END   => $article_trackback_end,
		ARTICLE_ADMIN_START     => $article_admin_start,
		ARTICLE_ADMIN_END       => $article_admin_end
	);
}

### コメントデータ作成
sub comment_article {
	my $self = shift;
	my($no, $pno, $stat, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $rank, $pwd, $host) = @_;

	my $no_ins    = new webliberty::String($no);
	my $pno_ins   = new webliberty::String($pno);
	my $stat_ins  = new webliberty::String($stat);
	my $date_ins  = new webliberty::String($date);
	my $name_ins  = new webliberty::String($name);
	my $mail_ins  = new webliberty::String($mail);
	my $url_ins   = new webliberty::String($url);
	my $subj_ins  = new webliberty::String($subj);
	my $text_ins  = new webliberty::String($text);
	my $color_ins = new webliberty::String($color);
	my $icon_ins  = new webliberty::String($icon);
	my $file_ins  = new webliberty::String($file);
	my $rank_ins  = new webliberty::String($rank);
	my $pwd_ins   = new webliberty::String($pwd);
	my $host_ins  = new webliberty::String($host);

	$no_ins->create_number;
	$pno_ins->create_number;
	$stat_ins->create_number;
	$date_ins->create_line;
	$name_ins->create_line;
	$mail_ins->create_line;
	$url_ins->create_line;
	$subj_ins->create_line;
	$text_ins->create_text;
	$color_ins->create_line;
	$icon_ins->create_line;
	$file_ins->create_line;
	$rank_ins->create_line;
	$pwd_ins->create_line;
	$host_ins->create_line;

	if ($stat_ins->get_string == 1) {
		$stat_ins->set_string('承認');
	} else {
		$stat_ins->set_string('未承認');
	}

	my($article_new_start, $article_new_end);
	if (time - $date_ins->get_string > 60 * 60 * 24 * $self->{config}->{new_days}) {
		$article_new_start = '<!--';
		$article_new_end   = '-->';
	}

	my($sec, $min, $hour, $day, $mon, $year, $week) = localtime($date_ins->get_string);
	my @week = ('日', '月', '火', '水', '木', '金', '土');

	my $article_year   = sprintf("%02d", $year + 1900);
	my $article_month  = sprintf("%02d", $mon + 1);
	my $article_day    = sprintf("%02d", $day);
	my $article_hour   = sprintf("%02d", $hour);
	my $article_minute = sprintf("%02d", $min);
	my $article_week   = $week[$week];

	if ($self->{agent} eq 'mobile') {
		$date_ins->set_string("$article_month/$article_day-$article_hour:$article_minute");
	} else {
		$date_ins->set_string("$article_year年$article_month月$article_day日($article_week)$article_hour時$article_minute分");
	}

	if (!$subj_ins->get_string) {
		$subj_ins->set_string('No Title');
	}
	if ($url_ins->get_string eq 'http://') {
		$url_ins->set_string('');
	}

	my($article_mail_start, $article_mail_end);
	if ($mail_ins->get_string) {
		$article_mail_start = '<a href="mailto:' . $mail_ins->get_string . '">';
		$article_mail_end   = '</a>';
	} else {
		$article_mail_start = '<!--';
		$article_mail_end   = '-->';
	}

	my($article_url_start, $article_url_end);
	if ($url_ins->get_string) {
		$article_url_start = '<a href="' . $url_ins->get_string . '">';
		$article_url_end   = '</a>';
	} else {
		$article_url_start = '<!--';
		$article_url_end   = '-->';
	}

	$text_ins->replace_string('<br /><br />', '</p><p>');
	$text_ins->set_string('<p>' . $text_ins->get_string . '</p>');

	if ($self->{config}->{quotation_color}) {
		$text = $text_ins->get_string;

		my $quotation_color = $self->{config}->{quotation_color};

		$text =~ s/([\>]|^)(&gt;|＞)([^<]*)/$1<span style=\"color:$quotation_color;\">$2$3<\/span>/g;

		$text_ins->set_string($text);
	}
	if ($self->{config}->{autolink_mode}) {
		$text_ins->create_link;
	}

	my($article_icon_start, $article_icon_end);
	if ($icon_ins->get_string) {
		$icon_ins->set_string("<img src=\"$self->{init}->{data_dir}$self->{init}->{data_icon_dir}" . $icon_ins->get_string . "\" alt=\"アイコン\" />");
	} else {
		$article_icon_start = '<!--';
		$article_icon_end   = '-->';
	}

	my($article_edit_start, $article_edit_end);
	if ($self->{query}->{exec_preview}) {
		$article_edit_start = '<!--';
		$article_edit_end   = '-->';
	}

	return(
		ARTICLE_NO         => $no_ins->get_string,
		ARTICLE_PNO        => $pno_ins->get_string,
		ARTICLE_STAT       => $stat_ins->get_string,
		ARTICLE_DATE       => $date_ins->get_string,
		ARTICLE_YEAR       => $article_year,
		ARTICLE_MONTH      => $article_month,
		ARTICLE_DAY        => $article_day,
		ARTICLE_HOUR       => $article_hour,
		ARTICLE_MINUTE     => $article_minute,
		ARTICLE_WEEK       => $article_week,
		ARTICLE_NAME       => $name_ins->get_string,
		ARTICLE_MAIL       => $mail_ins->get_string,
		ARTICLE_MAIL_START => $article_mail_start,
		ARTICLE_MAIL_END   => $article_mail_end,
		ARTICLE_URL        => $url_ins->get_string,
		ARTICLE_URL_START  => $article_url_start,
		ARTICLE_URL_END    => $article_url_end,
		ARTICLE_SUBJ       => $subj_ins->get_string,
		ARTICLE_TEXT       => $text_ins->get_string,
		ARTICLE_COLOR      => $color_ins->get_string,
		ARTICLE_ICON       => $icon_ins->get_string,
		ARTICLE_ICON_START => $article_icon_start,
		ARTICLE_ICON_END   => $article_icon_end,
		ARTICLE_FILE       => $file_ins->get_string,
		ARTICLE_RANK       => $rank_ins->get_string,
		ARTICLE_PWD        => $pwd_ins->get_string,
		ARTICLE_HOST       => $host_ins->get_string,
		ARTICLE_NEW_START  => $article_new_start,
		ARTICLE_NEW_END    => $article_new_end,
		ARTICLE_EDIT_START => $article_edit_start,
		ARTICLE_EDIT_END   => $article_edit_end
	);
}

### トラックバックデータ作成
sub trackback_article {
	my $self = shift;
	my($no, $pno, $stat, $date, $blog, $title, $url, $excerpt) = @_;

	my $no_ins      = new webliberty::String($no);
	my $pno_ins     = new webliberty::String($pno);
	my $stat_ins    = new webliberty::String($stat);
	my $date_ins    = new webliberty::String($date);
	my $blog_ins    = new webliberty::String($blog);
	my $title_ins   = new webliberty::String($title);
	my $url_ins     = new webliberty::String($url);
	my $excerpt_ins = new webliberty::String($excerpt);

	$no_ins->create_number;
	$pno_ins->create_number;
	$stat_ins->create_number;
	$date_ins->create_line;
	$blog_ins->create_line;
	$title_ins->create_line;
	$url_ins->create_line;
	$excerpt_ins->create_line;

	if ($stat_ins->get_string == 1) {
		$stat_ins->set_string('承認');
	} else {
		$stat_ins->set_string('未承認');
	}

	my($sec, $min, $hour, $day, $mon, $year, $week) = localtime($date_ins->get_string);
	my @week = ('日', '月', '火', '水', '木', '金', '土');

	my $article_year   = sprintf("%02d", $year + 1900);
	my $article_month  = sprintf("%02d", $mon + 1);
	my $article_day    = sprintf("%02d", $day);
	my $article_hour   = sprintf("%02d", $hour);
	my $article_minute = sprintf("%02d", $min);
	my $article_week   = $week[$week];

	if ($self->{agent} eq 'mobile') {
		$date_ins->set_string("$article_month/$article_day-$article_hour:$article_minute");
	} else {
		$date_ins->set_string("$article_year年$article_month月$article_day日($article_week)$article_hour時$article_minute分");
	}

	$excerpt_ins->replace_string('&amp;', '&');
	$excerpt_ins->replace_string('&lt;', '<');
	$excerpt_ins->replace_string('&gt;', '>');
	$excerpt_ins->replace_string('<[^>]*>', '');
	$excerpt_ins->replace_string('<', '&lt;');
	$excerpt_ins->replace_string('>', '&gt;');
	$excerpt_ins->replace_string('&', '&amp');

	if ($excerpt_ins->check_length > 300) {
		$excerpt_ins->set_string(substr($excerpt_ins->get_string, 0, 300) . ' …');
	}

	return(
		TRACKBACK_NO      => $no_ins->get_string,
		TRACKBACK_PNO     => $pno_ins->get_string,
		TRACKBACK_STAT    => $stat_ins->get_string,
		TRACKBACK_DATE    => $date_ins->get_string,
		TRACKBACK_YEAR    => $article_year,
		TRACKBACK_MONTH   => $article_month,
		TRACKBACK_DAY     => $article_day,
		TRACKBACK_HOUR    => $article_hour,
		TRACKBACK_MINUTE  => $article_minute,
		TRACKBACK_WEEK    => $article_week,
		TRACKBACK_BLOG    => $blog_ins->get_string,
		TRACKBACK_TITLE   => $title_ins->get_string,
		TRACKBACK_URL     => $url_ins->get_string,
		TRACKBACK_EXCERPT => $excerpt_ins->get_string
	);
}

### データ更新
sub update {
	my $self = shift;

	if ($self->{config}->{html_index_mode} or $self->{config}->{html_archive_mode}) {
		require webliberty::App::List;
		my $stdout = *STDOUT;

		if ($self->{config}->{html_archive_mode}) {
			foreach (split(/\n/, $self->{query}->{no})) {
				my $dammy;
				$dammy->{query}->{no} = $_;

				open(HTML, ">$self->{init}->{archive_dir}$dammy->{query}->{no}\.$self->{init}->{archive_ext}") or $self->error("Write Error : $self->{init}->{archive_dir}$dammy->{query}->{no}\.$self->{init}->{archive_ext}");
				*STDOUT = *HTML;
				my $app_ins = new webliberty::App::List($self->{init}, $self->{config}, $dammy->{query});
				$app_ins->run;
				close(HTML);

				if ($self->{init}->{chmod_mode}) {
					if ($self->{init}->{suexec_mode}) {
						chmod(0604, "$self->{init}->{archive_dir}$dammy->{query}->{no}\.$self->{init}->{archive_ext}") or $self->error("Chmod Error : $self->{init}->{archive_dir}$dammy->{query}->{no}\.$self->{init}->{archive_ext}");
					} else {
						chmod(0606, "$self->{init}->{archive_dir}$dammy->{query}->{no}\.$self->{init}->{archive_ext}") or $self->error("Chmod Error : $self->{init}->{archive_dir}$dammy->{query}->{no}\.$self->{init}->{archive_ext}");
					}
				}
			}
		}

		if ($self->{config}->{html_index_mode}) {
			open(HTML, ">$self->{init}->{html_file}") or $self->error("Write Error : $self->{init}->{html_file}");
			*STDOUT = *HTML;
			my $app_ins = new webliberty::App::List($self->{init}, $self->{config});
			$app_ins->run;
			close(HTML);
		}

		*STDOUT = $stdout;
	}

	if ($self->{config}->{thumbnail_mode}) {
		require webliberty::Thumbnail;
		my $thumbnail_ins = new webliberty::Thumbnail;
		my($flag, $message) = $thumbnail_ins->create_thumbnail(
			file_dir      => "$self->{init}->{data_dir}$self->{init}->{data_upfile_dir}",
			thumbnail_dir => "$self->{init}->{data_dir}$self->{init}->{data_thumbnail_dir}",
			img_max_width => $self->{config}->{img_maxwidth},
			limit         => 10
		);
		if (!$flag) {
			$self->error($message);
		}
	}

	if ($self->{config}->{js_title_mode} or $self->{config}->{js_text_size}) {
		opendir(DIR, "$self->{init}->{data_dir}$self->{init}->{data_diary_dir}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_diary_dir}");
		my @dir = sort { $b <=> $a } readdir(DIR);
		closedir(DIR);

		my $info_archive_path;
		if ($self->{config}->{html_archive_mode}) {
			if ($self->{init}->{archive_dir} =~ /([^\/\\]*\/)$/) {
				$info_archive_path = "$self->{config}->{site_url}$1";
			}
		} else {
			if ($self->{init}->{script_file} =~ /([^\/\\]*)$/) {
				$info_archive_path = "$self->{config}->{site_url}$1";
			}
		}

		my($js_title_data, $js_text_data, $i);

		foreach my $entry (@dir) {
			if ($entry !~ /^\d\d\d\d\d\d\.$self->{init}->{data_ext}$/) {
				next;
			}
			open(FH, "$self->{init}->{data_dir}$self->{init}->{data_diary_dir}$entry") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_diary_dir}$entry");
			while (<FH>) {
				chomp;
				my($no, $id, $stat, $break, $comt, $tb, $field, $date, $name, $subj, $text, $color, $icon, $file, $host) = split(/\t/);

				if (!$stat) {
					next;
				}

				$i++;
				if ($i > $self->{config}->{js_title_size} and $i > $self->{config}->{js_text_size}) {
					last;
				}

				if ($date =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
					$date = "$1年$2月$3日";
				}

				my $link;
				if ($self->{config}->{html_archive_mode}) {
					$link = "$info_archive_path$no\.$self->{init}->{archive_ext}";
				} else {
					$link = "$info_archive_path?no=$no";
				}

				my %article = $self->diary_article($no, $id, $stat, $break, $comt, $tb, $field, $date, $name, $subj, $text, $color, $icon, $file, $host);

				if ($self->{config}->{js_title_size}) {
					$js_title_data .= "$date ... <a href=\"$link\">$subj</a><br />\n";
				}
				if ($self->{config}->{js_text_size}) {
					$js_text_data  .= "<p><a href=\"$link\">$subj</a> ... $date</p>$article{'ARTICLE_TEXT'}\n";
				}
			}
			close(FH);
		}

		my $script_ins = new webliberty::Script;

		if ($self->{config}->{js_title_size}) {
			my($flag, $message) = $script_ins->create_jscript(
				file     => $self->{init}->{js_title_file},
				contents => $js_title_data
			);
			if (!$flag) {
				$self->error($message);
			}
		}
		if ($self->{config}->{js_text_size}) {
			my($flag, $message) = $script_ins->create_jscript(
				file     => $self->{init}->{js_text_file},
				contents => $js_text_data
			);
			if (!$flag) {
				$self->error($message);
			}
		}
	}

	return;
}

### エラー出力
sub error {
	my $self    = shift;
	my $message = shift;

	if (open(FH, "$self->{init}->{skin_dir}$self->{init}->{skin_error}")) {
		print $self->header;
		while (<FH>) {
			s/\$\{INFO_ERROR\}/$message/g;
			print;
		}
	} else {
		$self->SUPER::error($message);
	}

	exit;
}

1;
