From 24ac40972221300abfeee9f7ad39b5ec11632914 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 22 Nov 2012 12:12:26 +0000 Subject: switch to path_info uri --- index.cgi | 126 ++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 73 insertions(+), 53 deletions(-) (limited to 'index.cgi') diff --git a/index.cgi b/index.cgi index d23a588..c79f761 100755 --- a/index.cgi +++ b/index.cgi @@ -1,6 +1,7 @@ -# Blogsum +# Blogsum NG # +# Copyright (c) 2012 Dimitri Sokolyuk # Copyright (c) 2010 DixonGroup Consulting # All rights reserved. # @@ -61,18 +62,32 @@ $page_not_found_error ||= '404 page not found'; # main execution # ########################### -my %dispatch = ( - 'article' => \&output_article, - 'sitemap' => \&output_sitemap, - 'rss' => \&output_rss, -); my $cgi = CGI->new; -$cgi->charset('UTF-8'); my $dbh = DBI->connect("DBI:SQLite:dbname=$database", '', '', { RaiseError => 1 }) || die $DBI::errstr; my $template = HTML::Template->new(filename => "themes/${blog_theme}/index.tmpl", die_on_bad_params => 0); -my $view = $cgi->param('view'); -$view = 'article' unless exists $dispatch{$view}; -$dispatch{$view}(); + +my $path = $cgi->path_info(); +my $output = \&output_article; +my @args; + +if ( $path =~ /^\/(\d{4})\/(\d{2})\/([^\/]+)$/ ) { + @args = ( year => $1, month => $2, uri => $3 ); +} elsif ( $path =~ m/^\/(\d{4})\/(\d{2})\/?$/ ) { + @args = ( year => $1, month => $2 ); +} elsif ( $path =~ m/^\/(\d{4})\/?$/ ) { + @args = ( year => $1 ); +} elsif ( $path =~ m/^\/Page\/(\d+)$/ ) { + @args = ( page => $1 ); +} elsif ( $path =~ m/^\/Tags\/(\w+)$/ ) { + @args = ( tag => $1 ); +} elsif ( $path =~ m/^\/rss\.xml$/ ) { + $output = \&output_rss; +} elsif ( $path =~ m/^\/sitemap\.xml$/ ) { + $output = \&output_sitemap; +} + +$cgi->charset('UTF-8'); +$output->( @args ); $dbh->disconnect; ########################### @@ -80,12 +95,14 @@ $dbh->disconnect; ########################### sub output_article { - read_comment() if $comments_allowed; - my $articles = get_articles(); - my $archives = get_archives(); - + my %args = @_; + my $articles = get_articles( %args ); + my $archives = get_archives( %args ); my $tagcloud = get_tag_cloud(); my @status = (); + + read_comment() if $comments_allowed; + $template->param( script_name => $cgi->script_name() ); $template->param( archives => $archives ); $template->param( tagcloud => $tagcloud ); @@ -98,7 +115,7 @@ sub output_article { $template->param( google_webmaster_id => $google_webmaster_id ); if (@{$articles}) { $template->param( articles => $articles ); - if ($cgi->param('uri') && $comments_allowed) { + if ($comments_allowed && $args{'uri'}) { $template->param( comment_form => 1 ); $template->param( comment_max_length => $comment_max_length ); $template->param( id => $articles->[0]->{'id'} ); @@ -123,7 +140,7 @@ sub output_rss { language => 'en', ); - my $articles = get_articles(); + my $articles = get_articles( more => 1 ); for my $item (@{$articles}) { my $link = $blog_url . $item->{'year'} . '/' . $item->{'month'} . '/' . $item->{'uri'}; $rss->add_item ( @@ -148,8 +165,7 @@ sub output_sitemap { priority => 1.0, ); - $articles_per_page = -1; - my $articles = get_articles(); + my $articles = get_articles( nolimit => 1 ); for my $item (@{$articles}) { my $link = $blog_url . $item->{'year'} . '/' . $item->{'month'} . '/' . $item->{'uri'}; $map->add ( @@ -171,36 +187,34 @@ sub output_sitemap { } sub get_articles { + my %args = @_; my $page = 1; my $offset = 0; my $limit_clause; my $where_clause; - my $j = 0; + my $full_text = 0; my $show_comments = 0; - $articles_per_page = ($articles_per_page > 0) ? $articles_per_page : -1; - if ($cgi->param('page') && POSIX::isdigit($cgi->param('page'))) { - $page = $cgi->param('page'); + $articles_per_page = -1 if $args{'nolimit'}; + + if ($args{'page'}) { + $page = $args{'page'}; $offset = ($page - 1) * $articles_per_page; } - if (($cgi->param('year') =~ /\d{4}/)&& (1900 < $cgi->param('year')) && ($cgi->param('year') < 2036)) { - $where_clause .= 'WHERE date LIKE \'%' . $cgi->param('year'); - $j++; - if (($cgi->param('month') =~ /\d{2}/) && (0 < $cgi->param('month')) && ($cgi->param('month') <= 12)) { - $where_clause .= '-' . $cgi->param('month') . '%\' AND enabled=1 '; - $j++; - if ($cgi->param('uri') =~ /\w+/) { + if ($args{'year'} && 1900 < $args{'year'} && $args{'year'} < 2036) { + $where_clause .= 'WHERE date LIKE \'%' . $args{'year'}; + if ($args{'month'} && 0 < $args{'month'} && $args{'month'} <= 12) { + $where_clause .= '-' . $args{'month'} . '%\' AND enabled=1 '; + if ($args{'uri'}) { $where_clause .= 'AND uri=? AND enabled=1 '; - $j++; $show_comments=1; } } else { $where_clause .= "\%' AND enabled=1 "; } - } elsif ($cgi->param('tag')) { + } elsif ($args{'tag'}) { $where_clause .= "WHERE (tags LIKE ? OR author LIKE ?) AND enabled=1 "; - $j++; } elsif ($cgi->param('id')) { $where_clause .= 'WHERE id=? AND enabled=1 '; @@ -209,15 +223,24 @@ sub get_articles { } else { $where_clause .= 'WHERE enabled=1 '; $limit_clause = " LIMIT $articles_per_page OFFSET $offset"; + + # XXX + my $query2 = 'SELECT count(*) as total FROM articles WHERE enabled=1'; + my $sth2 = $dbh->prepare($query2); + $sth2->execute || die $dbh->errstr; + my $article_count = $sth2->fetchrow_hashref->{'total'}; + + $template->param( page_prev => ($page - 1) ) if $article_count > ($offset - $articles_per_page); + $template->param( page_next => ($page + 1) ) if $article_count > ($offset + $articles_per_page); } my $query = 'SELECT *, strftime("%s", date) AS epoch FROM articles ' . $where_clause . 'ORDER BY date DESC' . $limit_clause; my $sth = $dbh->prepare($query); - if ($j == 3) { - $sth->execute($cgi->param('uri')) || die $dbh->errstr; - } elsif ($cgi->param('tag')) { - my $search_tag = '%' . $cgi->param('tag') . '%'; + if ($args{'uri'}) { + $sth->execute($args{'uri'}) || die $dbh->errstr; + } elsif ($args{'tag'}) { + my $search_tag = '%' . $args{'tag'} . '%'; $sth->execute($search_tag, $search_tag) || die $dbh->errstr; } elsif ($cgi->param('id')) { $sth->execute($cgi->param('id')) || die $dbh->errstr; @@ -230,7 +253,7 @@ sub get_articles { $result->{'date'} =~ /(\d{4})\-(\d{2})\-\d{2} \d{2}\:\d{2}\:\d{2}/; ($result->{'year'}, $result->{'month'}) = ($1, $2); # cut off readmore if we're on the front page - if (($result->{'body'} =~ //) && ($j < 3) && ($cgi->param('view') ne 'rss')) { + if ($result->{'body'} =~ // && !($args{'uri'} || $args{'more'})) { $result->{'body'} =~ /(.*)\<\!\-\-readmore\-\-\>/s; $result->{'body'} = $1; $result->{'readmore'}++; @@ -244,22 +267,16 @@ sub get_articles { push(@articles, $result); } - my $query2 = 'SELECT count(*) as total FROM articles WHERE enabled=1'; - my $sth2 = $dbh->prepare($query2); - $sth2->execute || die $dbh->errstr; - my $article_count = $sth2->fetchrow_hashref->{'total'}; - $template->param( page_prev => ($page - 1) ) if (($j == 0) && ($article_count > ($offset - $articles_per_page))); - $template->param( page_next => ($page + 1) ) if (($j == 0) && ($article_count > ($offset + $articles_per_page))); - return (\@articles); } sub get_archives { + my %args = @_; my %history; my @archives; my @archives_compressed; - my $current_year = $cgi->param('year') || ((localtime)[5] + 1900); - my $current_month = $cgi->param('month') || ((localtime)[4] + 1); + my $current_year = $args{'year'} || ((localtime)[5] + 1900); + my $current_month = $args{'month'} || ((localtime)[4] + 1); my %months = ( '01' => 'January', '02' => 'February', @@ -343,12 +360,15 @@ sub format_tags { } sub read_comment { - if ($cgi->param('recaptcha_challenge_field') && $cgi->param('recaptcha_response_field') && $cgi->param('comment') && $cgi->param('id')) { + if ($cgi->param('recaptcha_challenge_field') && + $cgi->param('recaptcha_response_field') && + $cgi->param('comment') && + $cgi->param('id')) { # test our captcha my $result = verify_captcha( privatekey => $captcha_seckey, - remoteip => $ENV{'REMOTE_ADDR'}, + remoteip => $cgi->remote_addr(), challenge => $cgi->param('recaptcha_challenge_field'), response => $cgi->param('recaptcha_response_field') ); @@ -361,7 +381,7 @@ sub read_comment { my $comment_email = $cgi->param('email') ? substr($cgi->param('email'), 0, 100) : undef; my $comment_url = $cgi->param('url') ? substr($cgi->param('url'), 0, 100) : undef; my $comment_body = substr(HTML::Entities::encode($cgi->param('comment'), "<>&"), 0, $comment_max_length); - my $remoteip = $ENV{'REMOTE_ADDR'}; + my $remoteip = $cgi->remote_addr(); $sth->execute($cgi->param('id'), $comment_name, $comment_email, $comment_url, $comment_body) || die $dbh->errstr; $template->param( message => 'comment awaiting moderation, thank you' ); @@ -369,8 +389,8 @@ sub read_comment { # send email notification my $smtp = Net::SMTP->new($smtp_server); my $date = POSIX::strftime("%a, %d %b %Y %H:%M:%S %z (%Z)", localtime); - $smtp->mail($ENV{USER}); - $smtp->to("$blog_owner\n"); + $smtp->mail($blog_owner); + $smtp->to($blog_owner); $smtp->data(); $smtp->datasend("From: $smtp_sender\n"); $smtp->datasend("To: $blog_owner\n"); @@ -404,8 +424,8 @@ sub read_comment { } } - # present the challenge - $template->param( captcha_api_server => $captcha_api_server, captcha_pubkey => $captcha_pubkey ); + $template->param( captcha_api_server => $captcha_api_server ); + $template->param( captcha_pubkey => $captcha_pubkey ); } sub verify_captcha { -- cgit v1.2.3