Apache-Blog

 view release on metacpan or  search on metacpan

Blog.pm  view on Meta::CPAN


sub handle_older {
	my $r = shift;

	# display all the entries

	# is there a template?
	my $template = dirname($r->filename)."/older.html";

	if (!-e $template) {
		return DECLINED;
	} # end if

	$template = HTML::Template->new( filename => $template, die_on_bad_params=>0 );


	my @out_entries = Apache::Blog::Entry->get_all( dirname($r->filename) );

	$template->param( older_entries => \@out_entries );

	my $total_words = 0;
	$total_words += $_->wc for @out_entries;

	$template->param( total_words => $total_words );
	

	$r->content_type( 'text/html' );
	$r->send_http_header;
	$r->print( $template->output );
	
	
	return OK;
} # end handle_directory

sub handle_comment {
	my $r = shift;
	my $apr = Apache::Request->new($r);

	my $name = $apr->param('name');
	my $comment = $apr->param('comment');
	my $filename = $apr->param('filename');

	# if the comment directory doesn't exist, we should create it
	my $comment_dir = dirname($r->filename)."/$filename-comment";
	if (!-d $comment_dir) {
		# looks like perl 5.6.1 doesn't need the permissions bit
		# on mkdir, but perl 5.005_03 does. great fun when your
		# perl -c is 5.6.1, but your mod_perl is 5.005_03.
		# perhaps this should tell me something about my
		# development environment. perhaps i shouldn't be so
		# liberal here with the permissions either.
		unless (mkdir($comment_dir, 0755)) {
			$r->log_reason("Can't create $comment_dir: $!");
			return SERVER_ERROR;
		} # end mkdir
	} # end no directory

	# need a filename. we start at 1 and move upwards. there's
	# almost certainly a race condition here, but this is written
	# for my site where i get about one comment a week. if yours is
	# so busy you're worried about this breaking, then feel free to
	# fix it.
	my @existing_files = glob("$comment_dir/*");
	my $new_basename = scalar(@existing_files) + 1;

	open (COMMENT, ">$comment_dir/$new_basename");
	print COMMENT "$name\n";
	print COMMENT scalar(localtime)."\n";
	print COMMENT $comment;
	close COMMENT;

	# not quite sure what this will do if the user is being a bitch
	# and have proxied away the referer header. this could be
	# construed as a bug.
	$r->header_out( 'Location' => $r->header_in( 'Referer' ));

	return 302;
} # end handle_comment


1;

__END__

=head1 NAME

Apache::Blog - mod_perl weblog handler

=head1 SYNOPSIS

In httpd.conf

  Alias /blog/ /home/daniel/blog/
  <Location /blog>
    SetHandler perl-script
    PerlHandler +Apache::Blog
  </Location>

=head1 DESCRIPTION

Apache::Blog is a simple handler for online diaries. At the moment it
works on the one-entry-one-page paradigm, but would be easy to apapt to
multiple entries per page if this is prefered. In the future this will
be a configuration option.

It is inspired by the service offered at http://www.diaryland.com/

It uses HTML::Template, so it is easy to design new layouts. There are
some samples included with the distribution.

All diary entries are stored as plain text files, there's no database
stuff going on here. This is to make it simple to add entries - any
editor can be used to write entries.

To use, decide on a directory which is to hold your weblog entries,
and set PerlHandler like in the example. The alias isn't nessasary,
but I like it that way.

Also in that directory need to be two template files, one called
entry-template.html, and one called older.html.



( run in 0.812 second using v1.01-cache-2.11-cpan-39bf76dae61 )