#!/usr/local/bin/perl
package TmmDiary;
use lib '.'; use TmmLib6;
$TL->setLogPath('error'); $TL->debugCgi(1); $TL->run(-warn => 1, -cgi => 1);
__DATA__;
use DbMysql;
$TL->startCgi(
-main => \&do_work,
);
sub do_work {
if(!defined($CGI->getForce('Command')))
{
$CGI->set(Command => 'DispNew');
}
$TL->dispatch($CGI->get('Command'),
{
DispNew => \&DispNew,
DispAll => \&DispAll,
DispDay => \&DispDay,
Admin => \&Admin,
Disp => \&Disp,
Tracer => \&Tracer,
})
or $TL->die("Command Error\n");
}
sub DispNew {
my $time = time;
my $lasttime = $time;
my $cookie = $TL->newCookie->getCookie('diaryinfo');
if(defined($cookie) && defined($cookie->{lastvisit}))
{
$lasttime = $cookie->{lastvisit};
$TL->log("last visit: $lasttime\n");
}
$TL->newCookie->setCookie('diaryinfo',
{ lastvisit => $time },
-expires => time + 60*60*24*90
);
my $t = $TL->newTemplate->loadTemplate('displist.html');
my $sth = $DB->prepare(q{
SELECT DATE_FORMAT(MAX(updatetime), '%b %e, %Y (%a) %k:%i') updatetime
FROM diary
});
$sth->execute;
my ($lastupdate) = $sth->fetchrow_array;
$t->node('head')->add({ LASTUPDATE => $lastupdate });
$sth = $DB->prepare(q{
SELECT id, subject, body,
createtime createtimets,
DATE_FORMAT(updatetime, '%b %e, %Y (%a) %k:%i') updatetime,
DATE_FORMAT(createtime, '%b %e, %Y (%a) %k:%i') createtime,
CASE WHEN FROM_UNIXTIME(?) < updatetime THEN 1
ELSE 0
END updateflag
FROM diary
WHERE createtime > now() - INTERVAL 3 DAY
AND FROM_UNIXTIME(?) < updatetime
ORDER BY createtimets DESC
});
$sth->execute($lasttime, $lasttime);
while(my $data = $sth->fetchrow_hashref)
{
if($data->{updateflag})
{
$t->node('list')->node('new')->add;
}
else
{
$t->node('list')->node('old')->add;
}
my $body = $data->{body};
$body =~ s/\n+$//;
$body =~ s/\n/
\n/g;
$t->node('list')->add({
LINK => $TL->newForm->set(
Command => 'Disp',
ID => $data->{id},
)->toLink,
SUBJECT => $data->{subject},
BODY => $body,
CREATETIME => $data->{createtime},
UPDATETIME => $data->{updatetime},
});
}
&DispDayList($t);
# $TL->setContentType('text/html; charset=UTF-8');
$TL->print($t->toStr);
}
sub DispAll {
my $t = $TL->newTemplate->loadTemplate('displist.html');
my $sth = $DB->prepare(q{
SELECT id, subject, body,
createtime createtimets,
DATE_FORMAT(updatetime, '%b %e, %Y (%a) %k:%i') updatetime,
DATE_FORMAT(createtime, '%b %e, %Y (%a) %k:%i') createtime
FROM diary
ORDER BY createtimets DESC
});
$sth->execute;
$t->node('newlink')->add;
while(my $data = $sth->fetchrow_hashref)
{
$t->node('list')->node('old')->add;
my $body = $data->{body};
$body =~ s/\n+$//;
$body =~ s/\n/
\n/g;
$t->node('list')->add({
LINK => $TL->newForm->set(
Command => 'Disp',
ID => $data->{id},
)->toLink,
SUBJECT => $data->{subject},
BODY => $body,
CREATETIME => $data->{createtime},
UPDATETIME => $data->{updatetime},
});
}
# $TL->setContentType('text/html; charset=UTF-8');
$TL->print($t->toStr);
}
sub DispDayList {
my $t = shift;
my $sth = $DB->prepare(q{
SELECT TO_DAYS(createtime) keydate,
YEAR(createtime) createyear,
MONTH(createtime) createmonth,
MONTHNAME(createtime) createmonthname,
DAYOFMONTH(createtime) createday,
COUNT(*) num
FROM diary
GROUP BY TO_DAYS(createtime)
ORDER BY createyear DESC, createmonth DESC, createday
});
$sth->execute;
my $month;
my $data;
while($data = $sth->fetchrow_hashref)
{
$t->node('kako')->node('month')->node('day')
->add({
DAY => $TL->newString($data->{createday})->h2z->get,
LINK => $TL->newForm->set(
Command => 'DispDay',
Keydate => $data->{keydate},
)->toLink,
NUM => $data->{num},
});
if(!ref($month)
or ($month->[0] ne $data->{createmonthname})
or ($month->[1] ne $data->{createyear}))
{
if(ref($month))
{
$t->node('kako')->node('month')
->add({
MONTH => $month->[0],
YEAR => $month->[1],
});
}
$month = [$data->{createmonthname}, $data->{createyear}];
}
}
if(ref($month))
{
$t->node('kako')->node('month')
->add({
MONTH => $month->[0],
YEAR => $month->[1],
});
}
$t->node('kako')->add;
}
sub DispDay {
my $t = $TL->newTemplate->loadTemplate('displist.html');
my $sth = $DB->prepare(q{
SELECT id, subject, body,
DATE_FORMAT(updatetime, '%b %e, %Y (%a) %k:%i') updatetime,
DATE_FORMAT(createtime, '%b %e, %Y (%a) %k:%i') createtime
FROM diary
WHERE TO_DAYS(createtime) = ?
});
$sth->execute($CGI->get('Keydate'));
$t->node('newlink')->add;
while(my $data = $sth->fetchrow_hashref)
{
$t->node('list')->node('old')->add;
my $body = $data->{body};
$body =~ s/\n+$//;
$body =~ s/\n/
\n/g;
$t->node('list')->add({
LINK => $TL->newForm->set(
Command => 'Disp',
ID => $data->{id},
)->toLink,
SUBJECT => $data->{subject},
BODY => $body,
CREATETIME => $data->{createtime},
UPDATETIME => $data->{updatetime},
});
}
&DispDayList($t);
# $TL->setContentType('text/html; charset=UTF-8');
$TL->print($t->toStr);
}
sub Disp {
my $t = $TL->newTemplate->loadTemplate('displist.html');
my $sth = $DB->prepare(q{
SELECT id, subject, body,
DATE_FORMAT(updatetime, '%b %e, %Y (%a) %k:%i') updatetime,
DATE_FORMAT(createtime, '%b %e, %Y (%a) %k:%i') createtime
FROM diary
WHERE id = ?
});
$sth->execute($CGI->get('ID'));
$t->node('newlink')->add;
while(my $data = $sth->fetchrow_hashref)
{
$t->node('list')->node('old')->add;
my $body = $data->{body};
$body =~ s/\n+$//;
$body =~ s/\n/
\n/g;
$t->node('list')->add({
LINK => $TL->newForm->set(
Command => 'Disp',
ID => $data->{id},
)->toLink,
SUBJECT => $data->{subject},
BODY => $body,
CREATETIME => $data->{createtime},
UPDATETIME => $data->{updatetime},
});
}
# $TL->setContentType('text/html; charset=UTF-8');
$TL->print($t->toStr);
}
sub Tracer {
my $t = $TL->readTextFile('tracer3.html');
# $TL->setContentType('text/html; charset=UTF-8');
$TL->print($t);
}
sub Admin {
if(!defined($CGI->get('pass'))
or ($CGI->get('pass') ne 'xxxxxx'))
{
# $TL->setContentType('text/html; charset=UTF-8');
$TL->printError(-title => 'Auth Error',
-error => '認証無いニョ');
return;
}
$SAVE->set(
Command => $CGI->get('Command'),
pass => $CGI->get('pass'),
);
if(!defined($CGI->getForce('Method')))
{
$CGI->set(Method => 'DispList');
}
$TL->dispatch($CGI->get('Method'),
{
DispList => \&AdminDispList,
Write => \&AdminWrite,
})
or $TL->die("Method Error\n");
}
sub AdminDispList {
my $t = $TL->newTemplate->loadTemplate('admindisplist.html');
my $sth = $DB->prepare(q{
SELECT id, subject, body,
createtime createtimets,
DATE_FORMAT(updatetime, '%b %e, %Y (%a) %k:%i') updatetime,
DATE_FORMAT(createtime, '%b %e, %Y (%a) %k:%i') createtime
FROM diary
WHERE createtime > now() - INTERVAL 3 DAY
ORDER BY createtimets DESC
});
$sth->execute;
while(my $data = $sth->fetchrow_hashref)
{
$t->node('list')->setForm(
$TL->newForm->set(
id => $data->{id},
subject => $data->{subject},
body => $data->{body},
)
);
$t->node('list')->add({
CREATETIME => $data->{createtime},
UPDATETIME => $data->{updatetime},
});
}
# $TL->setContentType('text/html; charset=UTF-8');
$TL->print($t->toStr);
}
sub AdminWrite {
my $body = $CGI->get('body');
$body = $TL->newTagCheck->setAllowTag(':HR:BR;B;STRONG;I;U;S;EM;A(HREF,TARGET);FONT(COLOR,SIZE)')->check($body);
$CGI->set(body => $body);
if($CGI->get('id') eq '')
{
my $sth = $DB->prepare(q{
INSERT INTO diary VALUES (NULL, NULL, NULL, ?, ?)
});
$sth->execute($CGI->getSliceValues(qw(subject body)));
}
else
{
my $sth = $DB->prepare(q{
UPDATE diary
SET subject = ?, body = ?
WHERE id = ?
});
$sth->execute($CGI->getSliceValues(qw(subject body id)));
}
$TL->location($TL->newForm->toLink);
}