| 1 |
|
|---|
| 2 |
# Blogsum |
|---|
| 3 |
# Copyright (c) 2009 Jason Dixon <jason@dixongroup.net> |
|---|
| 4 |
# All rights reserved. |
|---|
| 5 |
|
|---|
| 6 |
########################### |
|---|
| 7 |
# pragmas and vars # |
|---|
| 8 |
########################### |
|---|
| 9 |
use strict; |
|---|
| 10 |
use Blogsum::Config; |
|---|
| 11 |
my $database = $Blogsum::Config::database; |
|---|
| 12 |
my $blog_theme = $Blogsum::Config::blog_theme; |
|---|
| 13 |
my $blog_title = $Blogsum::Config::blog_title; |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
########################### |
|---|
| 17 |
# main execution # |
|---|
| 18 |
########################### |
|---|
| 19 |
my $cgi = CGI->new; |
|---|
| 20 |
my $dbh = DBI->connect("DBI:SQLite:dbname=$database", '', '', { RaiseError => 1 }) || die $DBI::errstr; |
|---|
| 21 |
my $template = HTML::Template->new(filename => "themes/${blog_theme}/admin.tmpl", die_on_bad_params => 0); |
|---|
| 22 |
$template->param( theme => $blog_theme ); |
|---|
| 23 |
my $view; |
|---|
| 24 |
|
|---|
| 25 |
if ($cgi->param('view')) { |
|---|
| 26 |
if ($cgi->param('view') eq 'moderate') { |
|---|
| 27 |
$view = 'moderate'; |
|---|
| 28 |
manage_comments(); |
|---|
| 29 |
} elsif ($cgi->param('view') eq 'edit') { |
|---|
| 30 |
$view = 'create'; |
|---|
| 31 |
edit_article(); |
|---|
| 32 |
} else { |
|---|
| 33 |
$view = 'administrate'; |
|---|
| 34 |
manage_articles(); |
|---|
| 35 |
} |
|---|
| 36 |
} else { |
|---|
| 37 |
$view = 'administrate'; |
|---|
| 38 |
manage_articles(); |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
$dbh->disconnect; |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
########################### |
|---|
| 45 |
# subfunctions # |
|---|
| 46 |
########################### |
|---|
| 47 |
|
|---|
| 48 |
sub manage_articles { |
|---|
| 49 |
|
|---|
| 50 |
my $article_id; |
|---|
| 51 |
my $status=2; |
|---|
| 52 |
|
|---|
| 53 |
if ($cgi->param('delete') =~ /\d+/) { |
|---|
| 54 |
$article_id = $cgi->param('delete'); |
|---|
| 55 |
$status=-1; |
|---|
| 56 |
} |
|---|
| 57 |
if ($cgi->param('draft') =~ /\d+/) { |
|---|
| 58 |
$article_id = $cgi->param('draft'); |
|---|
| 59 |
$status=0; |
|---|
| 60 |
} |
|---|
| 61 |
if ($cgi->param('publish') =~ /\d+/) { |
|---|
| 62 |
$article_id = $cgi->param('publish'); |
|---|
| 63 |
$status=1; |
|---|
| 64 |
} |
|---|
| 65 |
if ($status < 2) { |
|---|
| 66 |
my $stmt = "UPDATE articles SET enabled=? WHERE id=?"; |
|---|
| 67 |
my $sth = $dbh->prepare($stmt); |
|---|
| 68 |
$sth->execute($status, $article_id) || die $dbh->errstr; |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
if (@{get_comments()} > 0) { |
|---|
| 72 |
$template->param( comments_to_moderate => 1); |
|---|
| 73 |
} |
|---|
| 74 |
$template->param( view => $view, blog_title => $blog_title, articles => get_articles() ); |
|---|
| 75 |
print $cgi->header(), $template->output; |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
sub manage_comments { |
|---|
| 79 |
|
|---|
| 80 |
my $comment_id; |
|---|
| 81 |
my $status=2; |
|---|
| 82 |
|
|---|
| 83 |
if ($cgi->param('delete') =~ /\d+/) { |
|---|
| 84 |
$comment_id = $cgi->param('delete'); |
|---|
| 85 |
$status=-1; |
|---|
| 86 |
} |
|---|
| 87 |
if ($cgi->param('publish') =~ /\d+/) { |
|---|
| 88 |
$comment_id = $cgi->param('publish'); |
|---|
| 89 |
$status=1; |
|---|
| 90 |
} |
|---|
| 91 |
if ($status < 2) { |
|---|
| 92 |
my $stmt = "UPDATE comments SET enabled=? WHERE id=?"; |
|---|
| 93 |
my $sth = $dbh->prepare($stmt); |
|---|
| 94 |
$sth->execute($status, $comment_id) || die $dbh->errstr; |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
$template->param( view => $view, blog_title => $blog_title, comments => get_comments() ); |
|---|
| 98 |
print $cgi->header(), $template->output; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
sub edit_article { |
|---|
| 102 |
|
|---|
| 103 |
# preview, pass through all input |
|---|
| 104 |
if ($cgi->param('preview')) { |
|---|
| 105 |
my $uri = $cgi->param('uri') || $cgi->param('title') || undef; |
|---|
| 106 |
$uri =~ s/\ /\-/g if ($uri); |
|---|
| 107 |
$template->param( view => $view, blog_title => $blog_title, preview => 1, edit => 1 ); |
|---|
| 108 |
$template->param( id => $cgi->param('id') ) if ($cgi->param('id')); |
|---|
| 109 |
$template->param( title => $cgi->param('title') ) if ($cgi->param('title')); |
|---|
| 110 |
$template->param( uri => $uri ) if ($uri); |
|---|
| 111 |
$template->param( body => $cgi->param('body') ) if ($cgi->param('body')); |
|---|
| 112 |
$template->param( tags => $cgi->param('tags') ) if ($cgi->param('tags')); |
|---|
| 113 |
print $cgi->header(), $template->output; |
|---|
| 114 |
|
|---|
| 115 |
# save edits, with id (update) |
|---|
| 116 |
} elsif ($cgi->param('save') && $cgi->param('id')) { |
|---|
| 117 |
if ($cgi->param('title') && $cgi->param('uri') && $cgi->param('body')) { |
|---|
| 118 |
my $uri = $cgi->param('uri'); |
|---|
| 119 |
$uri =~ s/\ /\-/g; |
|---|
| 120 |
my $stmt = "UPDATE articles SET title=?, uri=?, body=?, tags=? WHERE id=?"; |
|---|
| 121 |
my $sth = $dbh->prepare($stmt); |
|---|
| 122 |
$sth->execute($cgi->param('title'), $uri, $cgi->param('body'), $cgi->param('tags'), $cgi->param('id')) || die $dbh->errstr; |
|---|
| 123 |
manage_articles(); |
|---|
| 124 |
# if missing data, push back to preview |
|---|
| 125 |
} else { |
|---|
| 126 |
$template->param( error => 'required fields: title, uri, body' ); |
|---|
| 127 |
$template->param( view => $view, blog_title => $blog_title, edit => 1 ); |
|---|
| 128 |
$template->param( id => $cgi->param('id') ) if ($cgi->param('id')); |
|---|
| 129 |
$template->param( title => $cgi->param('title') ) if ($cgi->param('title')); |
|---|
| 130 |
$template->param( uri => $cgi->param('uri') ) if ($cgi->param('uri')); |
|---|
| 131 |
$template->param( body => $cgi->param('body') ) if ($cgi->param('body')); |
|---|
| 132 |
$template->param( tags => $cgi->param('tags') ) if ($cgi->param('tags')); |
|---|
| 133 |
print $cgi->header(), $template->output; |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
# save new, no id (insert) |
|---|
| 137 |
} elsif ($cgi->param('save')) { |
|---|
| 138 |
if ($cgi->param('title') && $cgi->param('body')) { |
|---|
| 139 |
my $uri = $cgi->param('uri') || $cgi->param('title'); |
|---|
| 140 |
$uri =~ s/\ /\-/g; |
|---|
| 141 |
my $author = $ENV{'REMOTE_USER'} || 'author'; |
|---|
| 142 |
my $stmt = "INSERT INTO articles VALUES (NULL, datetime('now'), ?, ?, ?, ?, 0, ?)"; |
|---|
| 143 |
my $sth = $dbh->prepare($stmt); |
|---|
| 144 |
$sth->execute($cgi->param('title'), $uri, $cgi->param('body'), $cgi->param('tags'), $author) || die $dbh->errstr; |
|---|
| 145 |
manage_articles(); |
|---|
| 146 |
# if missing data, push back to preview |
|---|
| 147 |
} else { |
|---|
| 148 |
$template->param( error => 'required fields: title, body' ); |
|---|
| 149 |
$template->param( view => $view, blog_title => $blog_title, edit => 1 ); |
|---|
| 150 |
$template->param( id => $cgi->param('id') ) if ($cgi->param('id')); |
|---|
| 151 |
$template->param( title => $cgi->param('title') ) if ($cgi->param('title')); |
|---|
| 152 |
$template->param( uri => $cgi->param('uri') ) if ($cgi->param('uri')); |
|---|
| 153 |
$template->param( body => $cgi->param('body') ) if ($cgi->param('body')); |
|---|
| 154 |
$template->param( tags => $cgi->param('tags') ) if ($cgi->param('tags')); |
|---|
| 155 |
print $cgi->header(), $template->output; |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
# edit an existing |
|---|
| 159 |
} elsif ($cgi->param('id')) { |
|---|
| 160 |
my $query = "SELECT * FROM articles WHERE id=?"; |
|---|
| 161 |
my $sth = $dbh->prepare($query); |
|---|
| 162 |
$sth->execute($cgi->param('id')) || die $dbh->errstr; |
|---|
| 163 |
my $result = $sth->fetchrow_hashref; |
|---|
| 164 |
if ($result) { |
|---|
| 165 |
$template->param( view => $view, blog_title => $blog_title, edit => 1 ); |
|---|
| 166 |
$template->param( $result ); |
|---|
| 167 |
print $cgi->header(), $template->output; |
|---|
| 168 |
} else { |
|---|
| 169 |
$template->param( error => 'no results found' ); |
|---|
| 170 |
manage_articles(); |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
# brand new, show form |
|---|
| 174 |
} else { |
|---|
| 175 |
$template->param( view => $view, blog_title => $blog_title, edit => 1 ); |
|---|
| 176 |
print $cgi->header(), $template->output; |
|---|
| 177 |
} |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
sub get_articles { |
|---|
| 181 |
|
|---|
| 182 |
my $query = 'SELECT * FROM articles WHERE enabled !=-1 ORDER BY date DESC'; |
|---|
| 183 |
my $sth = $dbh->prepare($query); |
|---|
| 184 |
$sth->execute() || die $dbh->errstr; |
|---|
| 185 |
|
|---|
| 186 |
my @articles; |
|---|
| 187 |
while (my $result = $sth->fetchrow_hashref) { |
|---|
| 188 |
$result->{'date'} =~ /(\d{4})\-(\d{2})\-\d{2} \d{2}\:\d{2}\:\d{2}/; |
|---|
| 189 |
($result->{'year'}, $result->{'month'}) = ($1, $2); |
|---|
| 190 |
$result->{'date'} =~ s/(\d{4}\-\d{2}\-\d{2}) \d{2}\:\d{2}\:\d{2}/$1/; |
|---|
| 191 |
delete $result->{'enabled'} if ($result->{'enabled'} == 0); |
|---|
| 192 |
$result->{'theme'} = $blog_theme; |
|---|
| 193 |
push(@articles, $result); |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
return \@articles; |
|---|
| 197 |
} |
|---|
| 198 |
|
|---|
| 199 |
sub get_comments { |
|---|
| 200 |
|
|---|
| 201 |
my $query = 'SELECT a.title AS article_title, a.uri AS article_uri, a.date AS article_date, c.* FROM articles a, comments c WHERE a.id=c.article_id AND c.enabled=0 ORDER BY c.date DESC'; |
|---|
| 202 |
my $sth = $dbh->prepare($query); |
|---|
| 203 |
$sth->execute() || die $dbh->errstr; |
|---|
| 204 |
|
|---|
| 205 |
my @comments; |
|---|
| 206 |
while (my $result = $sth->fetchrow_hashref) { |
|---|
| 207 |
$result->{'article_date'} =~ /(\d{4})\-(\d{2})\-\d{2} \d{2}\:\d{2}\:\d{2}/; |
|---|
| 208 |
($result->{'article_year'}, $result->{'article_month'}) = ($1, $2); |
|---|
| 209 |
push(@comments, $result); |
|---|
| 210 |
} |
|---|
| 211 |
|
|---|
| 212 |
return \@comments; |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
|
|---|