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

package webliberty::App::Mobile;

use strict;
use Jcode;
use base qw(webliberty::Basis);
use webliberty::Parser;
use webliberty::String;
use webliberty::App::Init;
use webliberty::App::Diary;

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

	my $self = {
		init    => shift,
		config  => shift,
		query   => shift,
		info    => undef,
		html    => undef,
		message => undef
	};
	bless $self, $class;

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

	if ($ENV{'HTTP_USER_AGENT'} =~ /J-PHONE/i) {
		$self->{info}->{method} = 'get';
	} else {
		$self->{info}->{method} = 'post';
	}

	return $self;
}

### メイン処理
sub run {
	my $self = shift;

	if ($self->{query}->{exec_regist}) {
		my $name_ins = new Jcode($self->{query}->{name}, 'sjis');
		my $mail_ins = new Jcode($self->{query}->{mail}, 'sjis');
		my $url_ins  = new Jcode($self->{query}->{url}, 'sjis');
		my $subj_ins = new Jcode($self->{query}->{subj}, 'sjis');
		my $text_ins = new Jcode($self->{query}->{text}, 'sjis');
		my $pwd_ins  = new Jcode($self->{query}->{pwd}, 'sjis');

		$self->{query}->{name} = $name_ins->utf8;
		$self->{query}->{mail} = $mail_ins->utf8;
		$self->{query}->{url}  = $url_ins->utf8;
		$self->{query}->{subj} = $subj_ins->utf8;
		$self->{query}->{text} = $text_ins->utf8;
		$self->{query}->{pwd}  = $pwd_ins->utf8;
	}

	$self->header;

	if ($self->{query}->{mode} eq 'admin') {
		if ($self->{query}->{admin_pwd}) {
			eval "use webliberty::App::Admin qw(check_password check_access check regist edit del);";
			if (!$self->check_password) {
				$self->error('パスワードが違います。');
			}

			if ($self->{query}->{work} eq 'new') {
				if ($self->{query}->{exec_regist}) {
					$self->check;
					$self->regist;
					$self->output_complete;
				} else {
					$self->output_admin_form;
				}
			} elsif ($self->{query}->{work} eq 'edit') {
				if ($self->{query}->{exec_regist}) {
					$self->check;
					$self->edit;
					$self->output_complete;
				} elsif ($self->{query}->{exec_del}) {
					$self->del;
					$self->output_complete;
				} elsif ($self->{query}->{edit}) {
					$self->output_admin_form;
				} else {
					$self->output_admin_edit;
				}
			} elsif ($self->{query}->{work} eq 'comment') {
				if ($self->{query}->{exec_regist}) {
					eval "use webliberty::App::Edit qw(edit);";
					$self->edit;
					$self->output_complete;
				} elsif ($self->{query}->{exec_del}) {
					eval "use webliberty::App::Edit qw(del);";
					$self->del;
					$self->output_complete;
				} elsif ($self->{query}->{edit}) {
					$self->output_comment;
				} else {
					$self->output_admin_comment;
				}
			} else {
				$self->error('不正なアクセスです。');
			}
		} else {
			$self->output_admin;
		}
	} elsif ($self->{query}->{mode} eq 'edit') {
		if ($self->{query}->{work}) {
			if ($self->{query}->{work} eq 'del') {
				eval "use webliberty::App::Edit qw(del);";
				$self->del;
				$self->output_complete;
			} elsif ($self->{query}->{work} eq 'edit') {
				eval "use webliberty::App::Comment qw(check);";
				eval "use webliberty::App::Edit qw(edit);";
				$self->edit;
				$self->output_complete;
			} else {
				$self->output_comment;
			}
		} else {
			$self->output_edit;
		}
	} elsif ($self->{query}->{mode} eq 'comment') {
		if ($self->{query}->{exec_regist}) {
			eval "use webliberty::App::Comment qw(check regist);";
			$self->check;
			$self->regist;
			$self->output_complete;
		} else {
			$self->output_comment;
		}
	} elsif ($self->{query}->{mode} eq 'comtnavi') {
		$self->output_comtnavi;
	} elsif ($self->{query}->{mode} eq 'tbnavi') {
		$self->output_tbnavi;
	} else {
		$self->output_list;
	}

	$self->footer;

	my($html, $length) = $self->get_mobile_data($self->{html});

	print "Content-Type: text/html; charset=Shift_JIS\n";
	print "Content-Length: $length\n\n";
	print $html;

	return;
}

### 管理者ページ
sub output_admin {
	my $self = shift;

	$self->{html} .= "■管理者用\n";
	$self->{html} .= "<hr>\n";
	$self->{html} .= "管理者パスワードを入力してください。\n";
	$self->{html} .= "<form action=\"$self->{info}->{script_url}\" method=\"$self->{info}->{method}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"mode\" value=\"admin\">\n";
	$self->{html} .= "作業内容<br>\n";
	$self->{html} .= "<select name=\"work\">\n";
	$self->{html} .= "<option value=\"new\">新規投稿</option>\n";
	$self->{html} .= "<option value=\"edit\">記事編集</option>\n";
	$self->{html} .= "<option value=\"comment\">コメント管理</option>\n";
	$self->{html} .= "</select><br>\n";
	$self->{html} .= "ﾊﾟｽﾜｰﾄﾞ<br>\n";
	$self->{html} .= "<input type=\"password\" name=\"admin_pwd\" size=\"6\"><br>\n";
	$self->{html} .= "<br>\n";
	$self->{html} .= "<input type=\"submit\" value=\"認証する\">\n";
	$self->{html} .= "</form>\n";
	$self->{html} .= "<hr>\n";
	$self->{html} .= "▲<a href=\"$self->{info}->{script_url}\">戻る</a>\n";

	return;
}

### 記事投稿フォーム
sub output_admin_form {
	my $self = shift;

	my $diary_ins = new webliberty::App::Diary($self->{init}, $self->{config}, $self->{query});
	$diary_ins->set_agent('mobile');

	my $init_ins = new webliberty::App::Init;
	my %label = %{$init_ins->get_label};

	my(%form, $form_label);
	if ($self->{query}->{work} eq 'edit') {
		if (!$self->{query}->{edit}) {
			$self->error('編集したい記事を選択してください。');
		}

		my $edit_date;
		open(FH, "$self->{init}->{data_dir}$self->{init}->{data_diary_dir}$self->{init}->{data_diary_index}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_diary_dir}$self->{init}->{data_diary_index}");
		while (<FH>) {
			chomp;
			my($date, $no, $id, $stat, $field, $name) = split(/\t/);

			if ($self->{query}->{edit} == $no) {
				$edit_date = $date;

				last;
			}
		}
		close(FH);

		my $edit_file;
		if ($edit_date =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
			$edit_file = "$self->{init}->{data_dir}$self->{init}->{data_diary_dir}$1$2\.$self->{init}->{data_ext}";
		}

		open(FH, "$edit_file") or $self->error("Read Error : $edit_file");
		while (<FH>) {
			chomp;
			my($no, $id, $stat, $break, $comt, $tb, $field, $date, $name, $subj, $text, $color, $icon, $file, $host) = split(/\t/);

			if ($self->{query}->{edit} == $no) {
				%form = $diary_ins->diary_form($no, $id, $stat, $break, $comt, $tb, $field, $date, $name, $subj, $text, $color, $icon, $file, $host);
			}
		}
		close(FH);

		$form_label = '記事編集';
	} else {
		my($sec, $min, $hour, $day, $mon, $year) = localtime(time);
		my $date = sprintf("%04d%02d%02d%02d%02d", $year + 1900, $mon + 1, $day, $hour, $min);

		%form = $diary_ins->diary_form('', '', 1, 1, '', '', '', $date, '', '', '', '', '', '', '');

		$form_label = '新規投稿';
	}

	$self->{html} .= "■$form_label\n";
	$self->{html} .= "<hr>\n";
	$self->{html} .= "<form action=\"$self->{info}->{script_url}\" method=\"$self->{info}->{method}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"admin_pwd\" value=\"$self->{query}->{admin_pwd}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"mode\" value=\"admin\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"work\" value=\"$self->{query}->{work}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"edit\" value=\"$form{'FORM_NO'}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"stat\" value=\"1\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"break\" value=\"1\">\n";
	$self->{html} .= "$label{'mobile_subj'}<br>\n";
	$self->{html} .= "<input type=\"text\" name=\"subj\" value=\"$form{'FORM_SUBJ'}\"><br>\n";
	$self->{html} .= "$label{'mobile_date'}<br>\n";
	$self->{html} .= "$form{'FORM_DATE'}<br>\n";
	$self->{html} .= "$label{'mobile_field'}<br>\n";
	$self->{html} .= "$form{'FORM_FIELD'}<br>\n";
	$self->{html} .= "$label{'mobile_text'}<br>\n";
	$self->{html} .= "<textarea name=\"text\" cols=\"14\" rows=\"5\">$form{'FORM_TEXT'}</textarea><br>\n";
	$self->{html} .= "<br>\n";
	$self->{html} .= "<input type=\"submit\" name=\"exec_regist\" value=\"投稿する\">\n";
	$self->{html} .= "</form>\n";
	$self->{html} .= "<hr>\n";
	$self->{html} .= "▲<a href=\"$self->{info}->{script_url}\">戻る</a>\n";

	return;
}

### 記事編集フォーム
sub output_admin_edit {
	my $self = shift;

	if (!$self->{message}) {
		$self->{message} = '投稿記事を編集・削除する事ができます。';
	}

	my $init_ins = new webliberty::App::Init;
	my %label = %{$init_ins->get_label};

	$self->{html} .= "■記事編集\n";
	$self->{html} .= "<hr>\n";
	$self->{html} .= "$self->{message}\n";
	$self->{html} .= "<form action=\"$self->{info}->{script_url}\" method=\"$self->{info}->{method}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"admin_pwd\" value=\"$self->{query}->{admin_pwd}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"mode\" value=\"admin\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"work\" value=\"edit\">\n";
	$self->{html} .= "$label{'mobile_no'}<br>\n";
	$self->{html} .= "<input type=\"text\" name=\"edit\" size=\"6\" value=\"\"><br>\n";
	$self->{html} .= "<input type=\"submit\" name=\"exec_form\" value=\"編集する\"><br>\n";
	$self->{html} .= "<br>\n";
	$self->{html} .= "$label{'mobile_no'}<br>\n";
	$self->{html} .= "<input type=\"text\" name=\"del\" size=\"6\" value=\"\"><br>\n";
	$self->{html} .= "<input type=\"submit\" name=\"exec_del\" value=\"削除する\">\n";
	$self->{html} .= "</form>\n";
	$self->{html} .= "<hr>\n";
	$self->{html} .= "▲<a href=\"$self->{info}->{script_url}\">戻る</a>\n";

	return;
}

### コメント管理フォーム
sub output_admin_comment {
	my $self = shift;

	if (!$self->{message}) {
		$self->{message} = 'コメントを編集・削除する事ができます。';
	}

	my $init_ins = new webliberty::App::Init;
	my %label = %{$init_ins->get_label};

	$self->{html} .= "■コメント管理\n";
	$self->{html} .= "<hr>\n";
	$self->{html} .= "$self->{message}\n";
	$self->{html} .= "<form action=\"$self->{info}->{script_url}\" method=\"$self->{info}->{method}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"admin_pwd\" value=\"$self->{query}->{admin_pwd}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"mode\" value=\"admin\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"work\" value=\"comment\">\n";
	$self->{html} .= "$label{'mobile_no'}<br>\n";
	$self->{html} .= "<input type=\"text\" name=\"edit\" size=\"6\" value=\"\"><br>\n";
	$self->{html} .= "<input type=\"submit\" name=\"exec_form\" value=\"編集する\"><br>\n";
	$self->{html} .= "<br>\n";
	$self->{html} .= "$label{'mobile_no'}<br>\n";
	$self->{html} .= "<input type=\"text\" name=\"del\" size=\"6\" value=\"\"><br>\n";
	$self->{html} .= "<input type=\"submit\" name=\"exec_del\" value=\"削除する\">\n";
	$self->{html} .= "</form>\n";
	$self->{html} .= "<hr>\n";
	$self->{html} .= "▲<a href=\"$self->{info}->{script_url}\">戻る</a>\n";

	return;
}

### コメント編集フォーム
sub output_edit {
	my $self = shift;

	if (!$self->{message}) {
		$self->{message} = '投稿記事を削除する事ができます。';
	}

	my $init_ins = new webliberty::App::Init;
	my %label = %{$init_ins->get_label};

	$self->{html} .= "■記事編集\n";
	$self->{html} .= "<hr>\n";
	$self->{html} .= "$self->{message}\n";
	$self->{html} .= "<form action=\"$self->{info}->{script_url}\" method=\"$self->{info}->{method}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"mode\" value=\"edit\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"work\" value=\"del\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"no\" value=\"$self->{query}->{no}\"><br>\n";
	$self->{html} .= "$label{'mobile_pwd'}<br>\n";
	$self->{html} .= "<input type=\"password\" name=\"pwd\" size=\"6\"><br>\n";
	$self->{html} .= "作業内容<br>\n";
	$self->{html} .= "<select name=\"work\">\n";
	$self->{html} .= "<option value=\"form\">記事編集</option>\n";
	$self->{html} .= "<option value=\"del\">記事削除</option>\n";
	$self->{html} .= "</select><br>\n";
	$self->{html} .= "<br>\n";
	$self->{html} .= "<input type=\"submit\" name=\"exec_del\" value=\"削除する\">\n";
	$self->{html} .= "</form>\n";
	$self->{html} .= "<hr>\n";
	$self->{html} .= "▲<a href=\"$self->{info}->{script_url}\">戻る</a>\n";

	return;
}

### コメント投稿フォーム
sub output_comment {
	my $self = shift;

	if ($self->{query}->{mode} eq 'admin') {
		$self->{query}->{no} = $self->{query}->{edit};
	}

	my $diary_ins = new webliberty::App::Diary($self->{init}, $self->{config}, $self->{query});
	$diary_ins->set_agent('mobile');

	my $init_ins = new webliberty::App::Init;
	my %label = %{$init_ins->get_label};

	my(%form, $form_label, $form_mode, $form_work, $diary_subj);

	if ($self->{query}->{mode} eq 'admin' or $self->{query}->{mode} eq 'edit') {
		my $edit_pno;

		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/);

			if ($self->{query}->{no} == $no) {
				$edit_pno = $pno;
				last;
			}
		}
		close(FH);

		my $flag;

		open(FH, "$self->{init}->{data_dir}$self->{init}->{data_comt_dir}$edit_pno\.$self->{init}->{data_ext}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_comt_dir}$edit_pno\.$self->{init}->{data_ext}");
		while (<FH>) {
			chomp;
			my($no, $pno, $stat, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $rank, $pwd, $host) = split(/\t/);

			if ($self->{query}->{no} == $no) {
				if ($self->{query}->{mode} ne 'admin') {
					my $pwd_ins = new webliberty::String($self->{query}->{pwd});
					if (!$pwd or !$pwd_ins->check_password($pwd)) {
						$self->error('削除キーが違います。');
					}
				}

				%form = $diary_ins->comment_form($no, $pno, $stat, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $rank, $self->{query}->{pwd}, $host);

				$flag = 1;

				last;
			}
		}
		close(FH);

		if (!$flag) {
			$self->error('指定された記事は存在しません。');
		}

		if ($self->{query}->{mode} eq 'admin') {
			$form_label = 'コメント編集';
			$form_mode  = 'admin';
			$form_work  = 'comment';
		} else {
			$form_label = 'コメント編集';
			$form_mode  = 'edit';
			$form_work  = 'edit';
		}
	} else {
		my($index_date, $index_no, $flag);

		open(FH, "$self->{init}->{data_dir}$self->{init}->{data_diary_dir}$self->{init}->{data_diary_index}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_diary_dir}$self->{init}->{data_diary_index}");
		while (<FH>) {
			chomp;
			my($date, $no, $id, $stat, $field, $name) = split(/\t/);

			if ($self->{query}->{no} == $no) {
				$index_date = $date;
				$index_no   = $no;

				$flag = 1;

				last;
			}
		}
		close(FH);

		if (!$flag) {
			$self->error('指定された記事は存在しません。');
		}

		my $data_file;
		if ($index_date =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
			$data_file = "$1$2\.$self->{init}->{data_ext}";
		}

		open(FH, "$self->{init}->{data_dir}$self->{init}->{data_diary_dir}$data_file") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_diary_dir}$data_file");
		while (<FH>) {
			chomp;
			my($no, $id, $stat, $break, $comt, $tb, $field, $date, $name, $subj, $text, $color, $icon, $file, $host) = split(/\t/);

			if ($index_no == $no) {
				$diary_subj = $subj;

				last;
			}
		}
		close(FH);

		%form = $diary_ins->comment_form($self->{query}->{no}, $self->{query}->{no}, '', '', '', '', '', "Re:$diary_subj", '', '', '', '', '', '', ''),

		$form_label = 'コメント投稿';
		$form_mode  = 'comment';
		$form_work  = 'regist';
	}

	$self->{html} .= "■$form_label\n";
	$self->{html} .= "<hr>\n";
	$self->{html} .= "<form action=\"$self->{info}->{script_url}\" method=\"$self->{info}->{method}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"mode\" value=\"$form_mode\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"work\" value=\"$form_work\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"key\" value=\"$form{'FORM_PWD'}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"no\" value=\"$form{'FORM_NO'}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"pno\" value=\"$form{'FORM_PNO'}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"date\" value=\"$form{'FORM_DATE'}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"subj\" value=\"$form{'FORM_SUBJ'}\">\n";
	$self->{html} .= "<input type=\"hidden\" name=\"host\" value=\"$form{'FORM_HOST'}\">\n";

	if ($self->{query}->{mode} eq 'admin') {
		$self->{html} .= "<input type=\"hidden\" name=\"admin_pwd\" value=\"$self->{query}->{admin_pwd}\">\n";
	}

	$self->{html} .= "$label{'mobile_name'}<br>\n";
	$self->{html} .= "<input type=\"text\" name=\"name\" value=\"$form{'FORM_NAME'}\"><br>\n";
	$self->{html} .= "$label{'mobile_mail'}<br>\n";
	$self->{html} .= "<input type=\"text\" name=\"mail\" value=\"$form{'FORM_MAIL'}\"><br>\n";
	$self->{html} .= "$label{'mobile_url'}<br>\n";
	$self->{html} .= "<input type=\"text\" name=\"url\" value=\"$form{'FORM_URL'}\"><br>\n";
	$self->{html} .= "$label{'mobile_text'}<br>\n";
	$self->{html} .= "<textarea name=\"text\" cols=\"14\" rows=\"5\">$form{'FORM_TEXT'}</textarea><br>\n";

	if ($self->{query}->{mode} ne 'admin') {
		$self->{html} .= "$label{'mobile_pwd'}<br>\n";
		$self->{html} .= "<input type=\"password\" name=\"pwd\" size=\"10\" value=\"$form{'FORM_PWD'}\"><br>\n";
	}

	$self->{html} .= "<br>\n";
	$self->{html} .= "<input type=\"submit\" name=\"exec_regist\" value=\"投稿する\">\n";
	$self->{html} .= "</form>\n";
	$self->{html} .= "<hr>\n";
	$self->{html} .= "▲<a href=\"$self->{info}->{script_url}\">戻る</a>\n";

	return;
}

### 記事表示
sub output_list {
	my $self = shift;

	if ($self->{query}->{no}) {
		$self->{html} .= "■記事表示\n";
	} else {
		$self->{html} .= "■タイトル一覧\n";
	}
	$self->{html} .= "<hr>\n";

	my($index_size, $index_date, $index_no);

	open(FH, "$self->{init}->{data_dir}$self->{init}->{data_diary_dir}$self->{init}->{data_diary_index}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_diary_dir}$self->{init}->{data_diary_index}");
	while (<FH>) {
		chomp;
		my($date, $no, $id, $stat, $field, $name) = split(/\t/);

		if ($stat and $self->{query}->{no} == $no) {
			$index_date = $date;
			$index_no   = $no;

			last;
		} elsif ($stat and $index_size == $self->{query}->{page} * $self->{config}->{mobile_page_size}) {
			$index_date = $date;
			$index_no   = $no;
		}

		if ($stat and (!$self->{query}->{field} or $self->{field}->{$self->{query}->{field}} eq $field)) {
			$index_size++;
		}
	}
	close(FH);

	my $start_file;
	if ($index_date =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
		$start_file = "$self->{init}->{data_dir}$self->{init}->{data_diary_dir}$1$2\.$self->{init}->{data_ext}";
	}

	my $diary_ins = new webliberty::App::Diary($self->{init}, $self->{config}, $self->{query});
	$diary_ins->set_agent('mobile');
	my %info = $diary_ins->info;

	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($dir_flag, $file_flag, $comt_flag, $i);

	foreach my $entry (@dir) {
		if ($entry !~ /^\d\d\d\d\d\d\.$self->{init}->{data_ext}$/) {
			next;
		}
		if ($start_file eq "$self->{init}->{data_dir}$self->{init}->{data_diary_dir}$entry") {
			$dir_flag = 1;
		}
		if ($dir_flag) {
			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;
				}

				if ($no == $index_no) {
					$file_flag = 1;
				}
				if ($file_flag) {
					if ($self->{query}->{field} and $self->{field}->{$self->{query}->{field}} ne $field) {
						next;
					}
					if (!$self->{query}->{field} or $self->{field}->{$self->{query}->{field}} eq $field) {
						$i++;
					}

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

					if ($self->{query}->{no} and !$self->{query}->{comment}) {
						$self->{html} .= "▽$article{'ARTICLE_SUBJ'}\n";
						if ($article{'ARTICLE_FILES'}) {
							$self->{html} .= "<p>$article{'ARTICLE_FILES'}</p>\n";
						}
						$self->{html} .= "$article{'ARTICLE_TEXT'}\n";
						$self->{html} .= "$article{'ARTICLE_DATE'}(No.$article{'ARTICLE_NO'})\n";
					} else {
						$self->{html} .= "▼<a href=\"$self->{info}->{script_url}?no=$no\">$article{'ARTICLE_SUBJ'}</a>-$article{'ARTICLE_DATE'}<br>\n";
					}

					if ($self->{query}->{no} or $i >= $self->{config}->{mobile_page_size}) {
						$dir_flag  = 0;
						$file_flag = 0;
						$comt_flag = $comt;
						last;
					}
				}
			}
			close(FH);
		}
	}

	if ($self->{query}->{no} and -e "$self->{init}->{data_dir}$self->{init}->{data_comt_dir}$self->{query}->{no}\.$self->{init}->{data_ext}") {
		if (!$self->{query}->{comment}) {
			$self->{html} .= "<br>\n";
			$self->{html} .= "---------------\n";
		}

		open(FH, "$self->{init}->{data_dir}$self->{init}->{data_comt_dir}$self->{query}->{no}\.$self->{init}->{data_ext}") or $self->error("Read Error : $self->{init}->{data_dir}$self->{init}->{data_comt_dir}$self->{query}->{no}\.$self->{init}->{data_ext}");
		while (<FH>) {
			chomp;
			my($no, $pno, $stat, $field, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $rank, $pwd, $host) = split(/\t/);

			my %article = $diary_ins->comment_article($no, $pno, $stat, $field, $date, $name, $mail, $url, $subj, $text, $color, $icon, $file, $rank, $pwd, $host);

			if ($self->{query}->{comment} == $no) {
				$self->{html} .= "<br>\n";
				$self->{html} .= "---------------<br>\n";
				$self->{html} .= "▽$article{'ARTICLE_SUBJ'}<br>\n";
				$self->{html} .= "投稿者:$article{'ARTICLE_NAME'}\n";
				$self->{html} .= "$article{'ARTICLE_TEXT'}\n";
				$self->{html} .= "$article{'ARTICLE_DATE'}<br>\n";
				$self->{html} .= "<a href=\"$self->{info}->{script_url}?mode=edit&amp;no=$no\">記事編集・削除</a><br>\n";
				$self->{html} .= "---------------\n";
			} else {
				$self->{html} .= "<br>\n";
				$self->{html} .= "▼<a href=\"$self->{info}->{script_url}?no=$pno&amp;comment=$no\">$article{'ARTICLE_SUBJ'}</a>-$article{'ARTICLE_NAME'}\n";
			}
		}
		close(FH);
	} else {
		$self->{html} .= "<br>\n";

		if ($self->{query}->{page} > 0) {
			$self->{html} .= "▼<a href=\"$self->{info}->{script_url}?page=" . ($self->{query}->{page} - 1) . "\">前のページ</a><br>";
		}
		if (int(($index_size - 1) / $self->{config}->{mobile_page_size}) > $self->{query}->{page}) {
			$self->{html} .= "▼<a href=\"$self->{info}->{script_url}?page=" . ($self->{query}->{page} + 1) . "\">次のページ</a><br>";
		}
	}

	if ($self->{query}->{no} and $comt_flag) {
		$self->{html} .= "<hr>\n";
		$self->{html} .= "▼<a href=\"$self->{info}->{script_url}?mode=comment&amp;no=$self->{query}->{no}\">コメント投稿</a><br>\n";
		$self->{html} .= "<hr>\n";
		$self->{html} .= "▲<a href=\"$self->{info}->{script_url}\">戻る</a>\n";
	}
	if (!$self->{query}->{mode} and !$self->{query}->{no}) {
		$self->{html} .= "<hr>\n";
		$self->{html} .= "▼<a href=\"$self->{info}->{script_url}?mode=comtnavi\">最近のコメント</a><br>\n";
		$self->{html} .= "▼<a href=\"$self->{info}->{script_url}?mode=tbnavi\">最近のトラックバック</a><br>\n";
		$self->{html} .= "▼<a href=\"$self->{info}->{script_url}?mode=admin\">管理者用</a>\n";
		$self->{html} .= "<hr>\n";
		$self->{html} .= "▲<a href=\"$info{'INFO_MOBILEBACK'}\">戻る</a>\n";
	}

	return;
}

### コメント一覧
sub output_comtnavi {
	my $self = shift;

	$self->{html} .= "■最近のコメント\n";
	$self->{html} .= "<hr>\n";

	my $diary_ins = new webliberty::App::Diary($self->{init}, $self->{config}, $self->{query});
	$diary_ins->set_agent('mobile');
	my %info = $diary_ins->info;

	my $i;

	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/);

		$i++;
		if ($i > 5) {
			last;
		}

		my %article = $diary_ins->comment_article($no, $pno, $stat, $date, $name, '', '', $subj, '', '', '', '', '', '', $host);

		$self->{html} .= "▼<a href=\"$self->{info}->{script_url}?no=$pno&amp;comment=$no\">$article{'ARTICLE_SUBJ'}</a>-$article{'ARTICLE_NAME'}($article{'ARTICLE_DATE'})<br>\n";
	}
	close(FH);

	$self->{html} .= "<hr>\n";
	$self->{html} .= "▲<a href=\"$self->{info}->{script_url}\">戻る</a>\n";

	return;
}

### トラックバック一覧
sub output_tbnavi {
	my $self = shift;

	$self->{html} .= "■最近のトラックバック\n";
	$self->{html} .= "<hr>\n";

	my $diary_ins = new webliberty::App::Diary($self->{init}, $self->{config}, $self->{query});
	$diary_ins->set_agent('mobile');
	my %info = $diary_ins->info;

	my $i;

	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/);

		$i++;
		if ($i > $self->{config}->{tblist_size}) {
			last;
		}

		my %article = $diary_ins->trackback_article($no, $pno, $stat, $date, $blog, $title, $url, '');

		$self->{html} .= "▼<a href=\"$article{'TRACKBACK_URL'}\">$article{'TRACKBACK_TITLE'}</a>-$article{'TRACKBACK_BLOG'}($article{'TRACKBACK_DATE'})<br>\n";
	}
	close(FH);

	$self->{html} .= "<hr>\n";
	$self->{html} .= "▲<a href=\"$self->{info}->{script_url}\">戻る</a>\n";

	return;
}

### 処理完了画面
sub output_complete {
	my $self = shift;

	if (!$self->{message}) {
		$self->{message} = '処理が完了しました。';
	}

	$self->{html} .= "■処理完了\n";
	$self->{html} .= "<hr>\n";
	$self->{html} .= "$self->{message}<br>\n";
	$self->{html} .= "<hr>\n";
	$self->{html} .= "▲<a href=\"$self->{info}->{script_url}\">戻る</a>\n";

	return;
}

### ヘッダー
sub header {
	my $self = shift;

	my $diary_ins = new webliberty::App::Diary($self->{init}, $self->{config}, $self->{query});
	$diary_ins->set_agent('mobile');
	my %info = $diary_ins->info;

	$self->{html}  = "<html>\n";
	$self->{html} .= "<head>\n";
	$self->{html} .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Shift_JIS\">\n";
	$self->{html} .= "<title>$info{'INFO_MOBILETITLE'}</title>\n";
	$self->{html} .= "</head>\n";
	$self->{html} .= "<body text=\"#000000\" bgcolor=\"#FFFFFF\" link=\"#0000FF\" vlink=\"#0000FF\">\n";
	$self->{html} .= "<center>$info{'INFO_MOBILETITLE'}</center>\n";
	$self->{html} .= "<hr>\n";

	return;
}

### フッター
sub footer {
	my $self = shift;

	$self->{html} .= "<hr>\n";
	$self->{html} .= "<center><a href=\"http://www.web-liberty.net/mobile/\">Web Liberty</a></center>\n";
	$self->{html} .= "</body>\n";
	$self->{html} .= "</html>\n";

	return;
}

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

	$self->header;
	$self->{html} .= "ｴﾗｰ:<br>$message\n";
	$self->footer;

	my($html, $length) = $self->get_mobile_data($self->{html});

	print "Content-Type: text/html; charset=Shift_JIS\n";
	print "Content-Length: $length\n\n";
	print $html;

	exit;
}

### 携帯用データ作成
sub get_mobile_data {
	my $self = shift;
	my $data = shift;

	$data =~ s/\xEF\xBD\x9E/\a/g;
	my $jcode_ins = new Jcode($data, 'utf8');
	$data = $jcode_ins->sjis;
	$data =~ s/\a/\x81\x60/g;

	return($data, length($data));
}

1;
