4 # Author: Rael Dornfest (2002-2003), The Blosxom Development Team (2005-2008)
5 # Version: 2.1.0+dev ($Id: blosxom.cgi,v 1.80 2008/07/25 18:22:09 xtaran Exp $)
6 # Home/Docs/Licensing: http://blosxom.sourceforge.net/
7 # Development/Downloads: http://sourceforge.net/projects/blosxom
11 # --- Configurable variables -----
13 # What's this blog's title?
14 $blog_title = "My Weblog";
16 # What's this blog's description (for outgoing RSS feed)?
17 $blog_description = "Yet another Blosxom weblog.";
19 # What's this blog's primary language (for outgoing RSS feed)?
20 $blog_language = "en";
22 # What's this blog's text encoding ?
23 $blog_encoding = "UTF-8";
25 # Where are this blog's entries kept?
26 $datadir = "/Library/WebServer/Documents/blosxom";
28 # What's my preferred base URL for this blog (leave blank for automatic)?
31 # Should I stick only to the datadir for items or travel down the
32 # directory hierarchy looking for items? If so, to what depth?
33 # 0 = infinite depth (aka grab everything), 1 = datadir only, n = n levels down
36 # How many entries should I show on the home page?
39 # What file extension signifies a blosxom entry?
40 $file_extension = "txt";
42 # What is the default flavour?
43 $default_flavour = "html";
45 # Should I show entries from the future (i.e. dated after now)?
46 $show_future_entries = 0;
48 # --- Plugins (Optional) -----
50 # File listing plugins blosxom should load
51 # (if empty blosxom will load all plugins in $plugin_dir and $plugin_path directories)
54 # Where are my plugins kept?
57 # Where should my plugins keep their state information?
58 $plugin_state_dir = "$plugin_dir/state";
60 # Additional plugins location
61 # List of directories, separated by ';' on windows, ':' everywhere else
64 # --- Static Rendering -----
66 # Where are this blog's static files to be created?
67 $static_dir = "/Library/WebServer/Documents/blog";
69 # What's my administrative password (you must set this for static rendering)?
70 $static_password = "";
72 # What flavours should I generate statically?
73 @static_flavours = qw/html rss/;
75 # Should I statically generate individual entries?
79 # Should I encode entities for xml content-types? (plugins can turn this off if they do it themselves)
80 $encode_xml_entities = 1;
82 # --------------------------------
85 qw! $version $blog_title $blog_description $blog_language $blog_encoding $datadir $url %template $template $depth $num_entries $file_extension $default_flavour $static_or_dynamic $config_dir $plugin_list $plugin_path $plugin_dir $plugin_state_dir @plugins %plugins $static_dir $static_password @static_flavours $static_entries $path_info_full $path_info $path_info_yr $path_info_mo $path_info_da $path_info_mo_num $flavour $static_or_dynamic %month2num @num2month $interpolate $entries $output $header $show_future_entries %files %indexes %others $encode_xml_entities $content_type !;
92 use CGI qw/:standard :netscape/;
96 # Load configuration from $ENV{BLOSXOM_CONFIG_DIR}/blosxom.conf, if it exists
98 if ( $ENV{BLOSXOM_CONFIG_FILE} && -r $ENV{BLOSXOM_CONFIG_FILE} ) {
99 $blosxom_config = $ENV{BLOSXOM_CONFIG_FILE};
100 ( $config_dir = $blosxom_config ) =~ s! / [^/]* $ !!x;
103 for my $blosxom_config_dir ( $ENV{BLOSXOM_CONFIG_DIR}, '/etc/blosxom',
106 if ( -r "$blosxom_config_dir/blosxom.conf" ) {
107 $config_dir = $blosxom_config_dir;
108 $blosxom_config = "$blosxom_config_dir/blosxom.conf";
114 # Load $blosxom_config
115 if ($blosxom_config) {
116 if ( -r $blosxom_config ) {
117 eval { require $blosxom_config }
118 or warn "Error reading blosxom config file '$blosxom_config'"
119 . ( $@ ? ": $@" : '' );
122 warn "Cannot find or read blosxom config file '$blosxom_config'";
126 my $fh = new FileHandle;
143 @num2month = sort { $month2num{$a} <=> $month2num{$b} } keys %month2num;
145 # Use the stated preferred URL or figure it out automatically
146 $url ||= url( -path_info => 1 );
147 # Unescape %XX hex codes (from URI::Escape::uri_unescape)
148 $url =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
149 $url =~ s/^included:/http:/ if $ENV{SERVER_PROTOCOL} eq 'INCLUDED';
151 # NOTE: Since v3.12, it looks as if CGI.pm misbehaves for SSIs and
152 # always appends path_info to the url. To fix this, we always
153 # request an url with path_info, and always remove it from the end of the
155 my $pi_len = length $ENV{PATH_INFO};
156 my $might_be_pi = substr( $url, -$pi_len );
157 substr( $url, -length $ENV{PATH_INFO} ) = ''
158 if $might_be_pi eq $ENV{PATH_INFO};
162 # Drop ending any / from dir settings
164 $plugin_dir =~ s!/$!!;
165 $static_dir =~ s!/$!!;
167 # Fix depth to take into account datadir's path
168 $depth += ( $datadir =~ tr[/][] ) - 1 if $depth;
170 if ( !$ENV{GATEWAY_INTERFACE}
171 and param('-password')
173 and param('-password') eq $static_password )
175 $static_or_dynamic = 'static';
178 $static_or_dynamic = 'dynamic';
179 param( -name => '-quiet', -value => 1 );
183 # Take a gander at HTTP's PATH_INFO for optional blog name, archive yr/mo/day
184 my @path_info = split m{/}, path_info() || param('path');
185 $path_info_full = join '/', @path_info; # Equivalent to $ENV{PATH_INFO}
188 # Flavour specified by ?flav={flav} or index.{flav}
190 if (! ($flavour = param('flav'))) {
191 if ( $path_info[$#path_info] =~ /(.+)\.(.+)$/ ) {
193 pop @path_info if $1 eq 'index';
196 $flavour ||= $default_flavour;
198 # Global variable to be used in head/foot.{flavour} templates
200 # Add all @path_info elements to $path_info till we come to one that could be a year
201 while ( $path_info[0] && $path_info[0] !~ /^(19|20)\d{2}$/) {
202 $path_info .= '/' . shift @path_info;
205 # Pull date elements out of path
206 if ($path_info[0] && $path_info[0] =~ /^(19|20)\d{2}$/) {
207 $path_info_yr = shift @path_info;
209 ($path_info[0] =~ /^(0\d|1[012])$/ ||
210 exists $month2num{ ucfirst lc $path_info_mo })) {
211 $path_info_mo = shift @path_info;
212 # Map path_info_mo to numeric $path_info_mo_num
213 $path_info_mo_num = $path_info_mo =~ /^\d{2}$/
215 : $month2num{ ucfirst lc $path_info_mo };
216 if ($path_info[0] && $path_info[0] =~ /^[0123]\d$/) {
217 $path_info_da = shift @path_info;
222 # Add remaining path elements to $path_info
223 $path_info .= '/' . join('/', @path_info);
225 # Strip spurious slashes
226 $path_info =~ s!(^/*)|(/*$)!!g;
228 # Define standard template subroutine, plugin-overridable at Plugins: Template
230 my ( $path, $chunk, $flavour ) = @_;
233 return join '', <$fh>
234 if $fh->open("< $datadir/$path/$chunk.$flavour");
235 } while ( $path =~ s/(\/*[^\/]*)$// and $1 );
237 # Check for definedness, since flavour can be the empty string
238 if ( defined $template{$flavour}{$chunk} ) {
239 return $template{$flavour}{$chunk};
241 elsif ( defined $template{error}{$chunk} ) {
242 return $template{error}{$chunk};
249 # Bring in the templates
252 last if /^(__END__)$/;
253 my ( $ct, $comp, $txt ) = /^(\S+)\s(\S+)(?:\s(.*))?$/ or next;
255 $template{$ct}{$comp} .= $txt . "\n";
259 my $path_sep = $^O eq 'MSWin32' ? ';' : ':';
260 my @plugin_dirs = split /$path_sep/, $plugin_path;
261 unshift @plugin_dirs, $plugin_dir;
262 my @plugin_list = ();
263 my %plugin_hash = ();
265 # If $plugin_list is set, read plugins to use from that file
266 if ( $plugin_list ) {
267 if ( -r $plugin_list and $fh->open("< $plugin_list") ) {
268 @plugin_list = map { chomp $_; $_ } grep { /\S/ && !/^#/ } <$fh>;
272 warn "unable to read or open plugin_list '$plugin_list': $!";
277 # Otherwise walk @plugin_dirs to get list of plugins to use
278 if ( ! @plugin_list && @plugin_dirs ) {
279 for my $plugin_dir (@plugin_dirs) {
280 next unless -d $plugin_dir;
281 if ( opendir PLUGINS, $plugin_dir ) {
283 grep { /^[\w:]+$/ && !/~$/ && -f "$plugin_dir/$_" }
288 next if $plugin_hash{$plugin};
290 # Add to @plugin_list and %plugin_hash
291 $plugin_hash{$plugin} = "$plugin_dir/$plugin";
292 push @plugin_list, $plugin;
297 @plugin_list = sort @plugin_list;
300 # Load all plugins in @plugin_list
301 unshift @INC, @plugin_dirs;
302 foreach my $plugin (@plugin_list) {
303 my ( $plugin_name, $off ) = $plugin =~ /^\d*([\w:]+?)(_?)$/;
304 my $plugin_file = $plugin_list ? $plugin_name : $plugin;
305 my $on_off = $off eq '_' ? -1 : 1;
307 # Allow perl module plugins
308 # The -z test is a hack to allow a zero-length placeholder file in a
309 # $plugin_path directory to indicate an @INC module should be loaded
310 if ( $plugin =~ m/::/ && ( $plugin_list || -z $plugin_hash{$plugin} ) ) {
312 # For Blosxom::Plugin::Foo style plugins, we need to use a string require
313 eval "require $plugin_file";
316 { # we try first to load from $plugin_dir before attempting from $plugin_path
317 eval { require "$plugin_dir/$plugin_file" }
318 or eval { require $plugin_file };
322 warn "error finding or loading blosxom plugin '$plugin_name': $@";
325 if ( $plugin_name->start() and ( $plugins{$plugin_name} = $on_off ) ) {
326 push @plugins, $plugin_name;
330 shift @INC foreach @plugin_dirs;
333 # Allow for the first encountered plugin::template subroutine to override the
334 # default built-in template subroutine
335 foreach my $plugin (@plugins) {
336 if ( $plugins{$plugin} > 0 and $plugin->can('template') ) {
337 if ( my $tmp = $plugin->template() ) {
344 # Provide backward compatibility for Blosxom < 2.0rc1 plug-ins
346 return &$template(@_);
349 # Define default entries subroutine
351 my ( %files, %indexes, %others );
355 my $curr_depth = $File::Find::dir =~ tr[/][];
356 return if $depth and $curr_depth > $depth;
362 =~ m!^$datadir/(?:(.*)/)?(.+)\.$file_extension$!
364 # not an index, .file, and is readable
365 and $2 ne 'index' and $2 !~ /^\./ and ( -r $File::Find::name )
369 # read modification time
370 my $mtime = stat($File::Find::name)->mtime or return;
372 # to show or not to show future entries
373 return unless ( $show_future_entries or $mtime < time );
375 # add the file and its associated mtime to the list of files
376 $files{$File::Find::name} = $mtime;
378 # static rendering bits
380 = "$static_dir/$1/index." . $static_flavours[0];
383 or stat($static_file)->mtime < $mtime )
386 $d = join( '/', ( nice_date($mtime) )[ 5, 2, 3 ] );
388 $indexes{ ( $1 ? "$1/" : '' ) . "$2.$file_extension" } = 1
393 # not an entries match
394 elsif ( !-d $File::Find::name and -r $File::Find::name ) {
395 $others{$File::Find::name} = stat($File::Find::name)->mtime;
401 return ( \%files, \%indexes, \%others );
405 # Allow for the first encountered plugin::entries subroutine to override the
406 # default built-in entries subroutine
407 foreach my $plugin (@plugins) {
408 if ( $plugins{$plugin} > 0 and $plugin->can('entries') ) {
409 if ( my $tmp = $plugin->entries() ) {
416 my ( $files, $indexes, $others ) = &$entries();
417 %indexes = %$indexes;
420 if ( !$ENV{GATEWAY_INTERFACE}
421 and param('-password')
423 and param('-password') eq $static_password )
426 param('-quiet') or print "Blosxom is generating static index pages...\n";
428 # Home Page and Directory Indexes
430 foreach my $path ( sort keys %indexes ) {
432 foreach ( ( '', split /\//, $path ) ) {
436 mkdir "$static_dir/$p", 0755
437 unless ( -d "$static_dir/$p" or $p =~ /\.$file_extension$/ );
438 foreach $flavour (@static_flavours) {
440 = ( &$template( $p, 'content_type', $flavour ) );
441 $content_type =~ s!\n.*!!s;
442 my $fn = $p =~ m!^(.+)\.$file_extension$! ? $1 : "$p/index";
443 param('-quiet') or print "$fn.$flavour\n";
444 my $fh_w = new FileHandle "> $static_dir/$fn.$flavour"
445 or die "Couldn't open $static_dir/$p for writing: $!";
447 if ( $indexes{$path} == 1 ) {
453 $path_info =~ s!\.$file_extension$!\.$flavour!;
454 print $fh_w &generate( 'static', $path_info, '', $flavour,
461 $path_info_yr, $path_info_mo,
462 $path_info_da, $path_info
463 ) = split /\//, $p, 4;
464 unless ( defined $path_info ) { $path_info = "" }
465 print $fh_w &generate( 'static', '', $p, $flavour,
476 $content_type = ( &$template( $path_info, 'content_type', $flavour ) );
477 $content_type =~ s!\n.*!!s;
479 $content_type =~ s/(\$\w+(?:::\w+)*)/"defined $1 ? $1 : ''"/gee;
480 $header = { -type => $content_type };
482 print generate( 'dynamic', $path_info,
483 "$path_info_yr/$path_info_mo_num/$path_info_da",
484 $flavour, $content_type );
488 foreach my $plugin (@plugins) {
489 if ( $plugins{$plugin} > 0 and $plugin->can('end') ) {
490 $entries = $plugin->end();
496 my ( $static_or_dynamic, $currentdir, $date, $flavour, $content_type )
500 %others = ref $others ? %$others : ();
503 foreach my $plugin (@plugins) {
504 if ( $plugins{$plugin} > 0 and $plugin->can('filter') ) {
505 $entries = $plugin->filter( \%files, \%others );
512 # Allow plugins to decide if we can cut short story generation
514 foreach my $plugin (@plugins) {
515 if ( $plugins{$plugin} > 0 and $plugin->can('skip') ) {
516 if ( my $tmp = $plugin->skip() ) {
523 # Define default interpolation subroutine
526 my $template = shift;
527 # Interpolate scalars, namespaced scalars, and hash/hashref scalars
528 $template =~ s/(\$\w+(?:::\w+)*(?:(?:->)?{(['"]?)[-\w]+\2})?)/"defined $1 ? $1 : ''"/gee;
532 unless ( defined($skip) and $skip ) {
534 # Plugins: Interpolate
535 # Allow for the first encountered plugin::interpolate subroutine to
536 # override the default built-in interpolate subroutine
537 foreach my $plugin (@plugins) {
538 if ( $plugins{$plugin} > 0 and $plugin->can('interpolate') ) {
539 if ( my $tmp = $plugin->interpolate() ) {
547 my $head = ( &$template( $currentdir, 'head', $flavour ) );
550 foreach my $plugin (@plugins) {
551 if ( $plugins{$plugin} > 0 and $plugin->can('head') ) {
552 $entries = $plugin->head( $currentdir, \$head );
556 $head = &$interpolate($head);
562 my $ne = $num_entries;
564 if ( $currentdir =~ /(.*?)([^\/]+)\.(.+)$/ and $2 ne 'index' ) {
565 $currentdir = "$1$2.$file_extension";
566 %f = ( "$datadir/$currentdir" => $files{"$datadir/$currentdir"} )
567 if $files{"$datadir/$currentdir"};
570 $currentdir =~ s!/index\..+$!!;
573 # Define a default sort subroutine
575 my ($files_ref) = @_;
577 sort { $files_ref->{$b} <=> $files_ref->{$a} }
582 # Allow for the first encountered plugin::sort subroutine to override the
583 # default built-in sort subroutine
584 foreach my $plugin (@plugins) {
585 if ( $plugins{$plugin} > 0 and $plugin->can('sort') ) {
586 if ( my $tmp = $plugin->sort() ) {
593 foreach my $path_file ( &$sort( \%f, \%others ) ) {
594 last if $ne <= 0 && $date !~ /\d/;
595 use vars qw/ $path $fn /;
597 = $path_file =~ m!^$datadir/(?:(.*)/)?(.*)\.$file_extension!;
599 # Only stories in the right hierarchy
600 $path =~ /^$currentdir/
601 or $path_file eq "$datadir/$currentdir"
604 # Prepend a slash for use in templates only if a path exists
607 # Date fiddling for by-{year,month,day} archive views
609 qw/ $dw $mo $mo_num $da $ti $yr $hr $min $hr12 $ampm $utc_offset/;
610 ( $dw, $mo, $mo_num, $da, $ti, $yr, $utc_offset )
611 = nice_date( $files{"$path_file"} );
612 ( $hr, $min ) = split /:/, $ti;
613 ( $hr12, $ampm ) = $hr >= 12 ? ( $hr - 12, 'pm' ) : ( $hr, 'am' );
615 if ( $hr12 == 0 ) { $hr12 = 12 }
617 # Only stories from the right date
618 my ( $path_info_yr, $path_info_mo_num, $path_info_da )
620 next if $path_info_yr && $yr != $path_info_yr;
621 last if $path_info_yr && $yr < $path_info_yr;
622 next if $path_info_mo_num && $mo ne $num2month[$path_info_mo_num];
623 next if $path_info_da && $da != $path_info_da;
624 last if $path_info_da && $da < $path_info_da;
627 my $date = ( &$template( $path, 'date', $flavour ) );
630 foreach my $plugin (@plugins) {
631 if ( $plugins{$plugin} > 0 and $plugin->can('date') ) {
633 = $plugin->date( $currentdir, \$date,
634 $files{$path_file}, $dw, $mo, $mo_num, $da, $ti,
639 $date = &$interpolate($date);
641 if ( $date && $curdate ne $date ) {
646 use vars qw/ $title $body $raw /;
647 if ( -f "$path_file" && $fh->open("< $path_file") ) {
648 chomp( $title = <$fh> );
649 chomp( $body = join '', <$fh> );
651 $raw = "$title\n$body";
653 my $story = ( &$template( $path, 'story', $flavour ) );
656 foreach my $plugin (@plugins) {
657 if ( $plugins{$plugin} > 0 and $plugin->can('story') ) {
658 $entries = $plugin->story( $path, $fn, \$story, \$title,
663 if ( $encode_xml_entities &&
664 $content_type =~ m{\bxml\b} &&
665 $content_type !~ m{\bxhtml\b} ) {
666 # Escape special characters inside the <link> container
668 # The following line should be moved more towards to top for
669 # performance reasons -- Axel Beckert, 2008-07-22
670 my $url_escape_re = qr([^-/a-zA-Z0-9:._]);
672 $url =~ s($url_escape_re)(sprintf('%%%02X', ord($&)))eg;
673 $path =~ s($url_escape_re)(sprintf('%%%02X', ord($&)))eg;
674 $fn =~ s($url_escape_re)(sprintf('%%%02X', ord($&)))eg;
676 # Escape <, >, and &, and to produce valid RSS
684 my $escape_re = join '|' => keys %escape;
685 $title =~ s/($escape_re)/$escape{$1}/g;
686 $body =~ s/($escape_re)/$escape{$1}/g;
687 $url =~ s/($escape_re)/$escape{$1}/g;
688 $path =~ s/($escape_re)/$escape{$1}/g;
689 $fn =~ s/($escape_re)/$escape{$1}/g;
692 $story = &$interpolate($story);
701 my $foot = ( &$template( $currentdir, 'foot', $flavour ) );
704 foreach my $plugin (@plugins) {
705 if ( $plugins{$plugin} > 0 and $plugin->can('foot') ) {
706 $entries = $plugin->foot( $currentdir, \$foot );
710 $foot = &$interpolate($foot);
714 foreach my $plugin (@plugins) {
715 if ( $plugins{$plugin} > 0 and $plugin->can('last') ) {
716 $entries = $plugin->last();
722 # Finally, add the header, if any and running dynamically
723 $output = header($header) . $output
724 if ( $static_or_dynamic eq 'dynamic' and $header );
732 my $c_time = CORE::localtime($unixtime);
733 my ( $dw, $mo, $da, $hr, $min, $sec, $yr )
735 =~ /(\w{3}) +(\w{3}) +(\d{1,2}) +(\d{2}):(\d{2}):(\d{2}) +(\d{4})$/
738 $da = sprintf( "%02d", $da );
739 my $mo_num = $month2num{$mo};
742 = timegm( $sec, $min, $hr, $da, $mo_num - 1, $yr - 1900 ) - $unixtime;
743 my $utc_offset = sprintf( "%+03d", int( $offset / 3600 ) )
744 . sprintf( "%02d", ( $offset % 3600 ) / 60 );
746 return ( $dw, $mo, $mo_num, $da, $ti, $yr, $utc_offset );
749 # Default HTML and RSS template bits
751 html content_type text/html; charset=$blog_encoding
753 html head <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
756 html head <meta http-equiv="content-type" content="$content_type" >
757 html head <link rel="alternate" type="application/rss+xml" title="RSS" href="$url/index.rss" >
758 html head <title>$blog_title $path_info_da $path_info_mo $path_info_yr</title>
761 html head <div align="center">
762 html head <h1>$blog_title</h1>
763 html head <p>$path_info_da $path_info_mo $path_info_yr</p>
767 html story <h3><a name="$fn">$title</a></h3>
768 html story <div>$body</div>
769 html story <p>posted at: $ti | path: <a href="$url$path">$path</a> | <a href="$url/$yr/$mo_num/$da#$fn">permanent link to this entry</a></p>
772 html date <h2>$dw, $da $mo $yr</h2>
775 html foot <div align="center">
776 html foot <a href="http://blosxom.sourceforge.net/"><img src="http://blosxom.sourceforge.net/images/pb_blosxom.gif" alt="powered by blosxom" border="0" width="90" height="33" ></a>
781 rss content_type text/xml; charset=$blog_encoding
783 rss head <?xml version="1.0" encoding="$blog_encoding"?>
784 rss head <rss version="2.0">
786 rss head <title>$blog_title</title>
787 rss head <link>$url/$path_info</link>
788 rss head <description>$blog_description</description>
789 rss head <language>$blog_language</language>
790 rss head <docs>http://blogs.law.harvard.edu/tech/rss</docs>
791 rss head <generator>blosxom/$version</generator>
794 rss story <title>$title</title>
795 rss story <pubDate>$dw, $da $mo $yr $ti:00 $utc_offset</pubDate>
796 rss story <link>$url/$yr/$mo_num/$da#$fn</link>
797 rss story <category>$path</category>
798 rss story <guid isPermaLink="true">$url$path/$fn</guid>
799 rss story <description>$body</description>
807 error content_type text/html
809 error head <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
811 error head <head><title>Error: unknown Blosxom flavour "$flavour"</title></head>
813 error head <h1><font color="red">Error: unknown Blosxom flavour "$flavour"</font></h1>
814 error head <p>I'm afraid this is the first I've heard of a "$flavour" flavoured Blosxom. Try dropping the "/+$flavour" bit from the end of the URL.</p>
816 error story <h3>$title</h3>
817 error story <div>$body</div> <p><a href="$url/$yr/$mo_num/$da#fn.$default_flavour">#</a></p>
819 error date <h2>$dw, $da $mo $yr</h2>