This will probably become v2.1.2, so prepare the code for the release.
[matthijs/upstream/blosxom.git] / blosxom.cgi
1 #!/usr/bin/perl
2
3 # Blosxom
4 # Author: Rael Dornfest (2002-2003), The Blosxom Development Team (2005-2008)
5 # Version: 2.1.2 ($Id: blosxom.cgi,v 1.85 2008/10/02 01:09:41 xtaran Exp $)
6 # Home/Docs/Licensing: http://blosxom.sourceforge.net/
7 # Development/Downloads: http://sourceforge.net/projects/blosxom
8
9 package blosxom;
10
11 # --- Configurable variables -----
12
13 # What's this blog's title?
14 $blog_title = "My Weblog";
15
16 # What's this blog's description (for outgoing RSS feed)?
17 $blog_description = "Yet another Blosxom weblog.";
18
19 # What's this blog's primary language (for outgoing RSS feed)?
20 $blog_language = "en";
21
22 # What's this blog's text encoding ?
23 $blog_encoding = "UTF-8";
24
25 # Where are this blog's entries kept?
26 $datadir = "/Library/WebServer/Documents/blosxom";
27
28 # What's my preferred base URL for this blog (leave blank for automatic)?
29 $url = "";
30
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
34 $depth = 0;
35
36 # How many entries should I show on the home page?
37 $num_entries = 40;
38
39 # What file extension signifies a blosxom entry?
40 $file_extension = "txt";
41
42 # What is the default flavour?
43 $default_flavour = "html";
44
45 # Should I show entries from the future (i.e. dated after now)?
46 $show_future_entries = 0;
47
48 # --- Plugins (Optional) -----
49
50 # File listing plugins blosxom should load
51 # (if empty blosxom will load all plugins in $plugin_dir and $plugin_path directories)
52 $plugin_list = "";
53
54 # Where are my plugins kept?
55 $plugin_dir = "";
56
57 # Where should my plugins keep their state information?
58 $plugin_state_dir = "$plugin_dir/state";
59
60 # Additional plugins location
61 # List of directories, separated by ';' on windows, ':' everywhere else
62 $plugin_path = "";
63
64 # --- Static Rendering -----
65
66 # Where are this blog's static files to be created?
67 $static_dir = "/Library/WebServer/Documents/blog";
68
69 # What's my administrative password (you must set this for static rendering)?
70 $static_password = "";
71
72 # What flavours should I generate statically?
73 @static_flavours = qw/html rss/;
74
75 # Should I statically generate individual entries?
76 # 0 = no, 1 = yes
77 $static_entries = 0;
78
79 # Should I encode entities for xml content-types? (plugins can turn this off if they do it themselves)
80 $encode_xml_entities = 1;
81
82 # --------------------------------
83
84 use vars
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 !;
86
87 use strict;
88 use FileHandle;
89 use File::Find;
90 use File::stat;
91 use Time::Local;
92 use CGI qw/:standard :netscape/;
93
94 $version = "2.1.2";
95
96 # Load configuration from $ENV{BLOSXOM_CONFIG_DIR}/blosxom.conf, if it exists
97 my $blosxom_config;
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;
101 }
102 else {
103     for my $blosxom_config_dir ( $ENV{BLOSXOM_CONFIG_DIR}, '/etc/blosxom',
104         '/etc' )
105     {
106         if ( -r "$blosxom_config_dir/blosxom.conf" ) {
107             $config_dir     = $blosxom_config_dir;
108             $blosxom_config = "$blosxom_config_dir/blosxom.conf";
109             last;
110         }
111     }
112 }
113
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             . ( $@ ? ": $@" : '' );
120     }
121     else {
122         warn "Cannot find or read blosxom config file '$blosxom_config'";
123     }
124 }
125
126 my $fh = new FileHandle;
127
128 %month2num = (
129     nil => '00',
130     Jan => '01',
131     Feb => '02',
132     Mar => '03',
133     Apr => '04',
134     May => '05',
135     Jun => '06',
136     Jul => '07',
137     Aug => '08',
138     Sep => '09',
139     Oct => '10',
140     Nov => '11',
141     Dec => '12'
142 );
143 @num2month = sort { $month2num{$a} <=> $month2num{$b} } keys %month2num;
144
145 # Use the stated preferred URL or figure it out automatically. Set
146 # $url manually in the config section above if CGI.pm doesn't guess
147 # the base URL correctly, e.g. when called from a Server Side Includes
148 # document or so.
149 unless ($url) {
150     $url = url();
151
152     # Unescape %XX hex codes (from URI::Escape::uri_unescape)
153     $url =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;      
154
155     # Support being called from inside a SSI document
156     $url =~ s/^included:/http:/ if $ENV{SERVER_PROTOCOL} eq 'INCLUDED';
157
158     # Remove PATH_INFO if it is set but not removed by CGI.pm. This
159     # seems to happen when used with Apache's Alias directive or if
160     # called from inside a Server Side Include document. If that
161     # doesn't help either, set $url manually in the configuration.
162     $url =~ s/\Q$ENV{PATH_INFO}\E$// if defined $ENV{PATH_INFO};
163
164     # NOTE:
165     #
166     # There is one case where this code does more than necessary, too:
167     # If the URL requested is e.g. http://example.org/blog/blog and
168     # the base URL is correctly determined as http://example.org/blog
169     # by CGI.pm, then this code will incorrectly normalize the base
170     # URL down to http://example.org, because the same string as
171     # PATH_INFO is part of the base URL, too. But this is such a
172     # seldom case and can be fixed by setting $url in the config file,
173     # too.
174 }
175
176 # The only modification done to a manually set base URL is to strip
177 # a trailing slash if present.
178
179 $url =~ s!/$!!;
180
181 # Drop ending any / from dir settings
182 $datadir    =~ s!/$!!;
183 $plugin_dir =~ s!/$!!;
184 $static_dir =~ s!/$!!;
185
186 # Fix depth to take into account datadir's path
187 $depth += ( $datadir =~ tr[/][] ) - 1 if $depth;
188
189 if (    !$ENV{GATEWAY_INTERFACE}
190     and param('-password')
191     and $static_password
192     and param('-password') eq $static_password )
193 {
194     $static_or_dynamic = 'static';
195 }
196 else {
197     $static_or_dynamic = 'dynamic';
198     param( -name => '-quiet', -value => 1 );
199 }
200
201 # Path Info Magic
202 # Take a gander at HTTP's PATH_INFO for optional blog name, archive yr/mo/day
203 my @path_info = split m{/}, path_info() || param('path');
204 $path_info_full = join '/', @path_info;      # Equivalent to $ENV{PATH_INFO}
205 shift @path_info;
206
207 # Flavour specified by ?flav={flav} or index.{flav}
208 $flavour = '';
209 if (! ($flavour = param('flav'))) {
210     if ( $path_info[$#path_info] =~ /(.+)\.(.+)$/ ) {
211        $flavour = $2;
212         pop @path_info if $1 eq 'index';
213     }
214 }
215 $flavour ||= $default_flavour;
216
217 # Fix XSS in flavour name (CVE-2008-2236)
218 $flavour = blosxom_html_escape($flavour);
219
220 sub blosxom_html_escape {
221   my $string = shift;
222   my %escape = (
223                 '<' => '&lt;',
224                 '>' => '&gt;',
225                 '&' => '&amp;',
226                 '"' => '&quot;',
227                 "'" => '&apos;'
228                 );
229   my $escape_re = join '|' => keys %escape;
230   $string =~ s/($escape_re)/$escape{$1}/g;
231   $string;
232 }
233
234 # Global variable to be used in head/foot.{flavour} templates
235 $path_info = '';
236 # Add all @path_info elements to $path_info till we come to one that could be a year
237 while ( $path_info[0] && $path_info[0] !~ /^(19|20)\d{2}$/) {
238     $path_info .= '/' . shift @path_info;
239 }
240
241 # Pull date elements out of path
242 if ($path_info[0] && $path_info[0] =~ /^(19|20)\d{2}$/) {
243   $path_info_yr = shift @path_info;
244   if ($path_info[0] && 
245      ($path_info[0] =~ /^(0\d|1[012])$/ || 
246       exists $month2num{ ucfirst lc $path_info_mo })) {
247     $path_info_mo = shift @path_info;
248     # Map path_info_mo to numeric $path_info_mo_num
249     $path_info_mo_num = $path_info_mo =~ /^\d{2}$/
250       ? $path_info_mo
251       : $month2num{ ucfirst lc $path_info_mo };
252     if ($path_info[0] && $path_info[0] =~ /^[0123]\d$/) {
253       $path_info_da = shift @path_info;
254     }
255   }
256 }
257
258 # Add remaining path elements to $path_info
259 $path_info .= '/' . join('/', @path_info);
260
261 # Strip spurious slashes
262 $path_info =~ s!(^/*)|(/*$)!!g;
263
264 # Define standard template subroutine, plugin-overridable at Plugins: Template
265 $template = sub {
266     my ( $path, $chunk, $flavour ) = @_;
267
268     do {
269         return join '', <$fh>
270             if $fh->open("< $datadir/$path/$chunk.$flavour");
271     } while ( $path =~ s/(\/*[^\/]*)$// and $1 );
272
273     # Check for definedness, since flavour can be the empty string
274     if ( defined $template{$flavour}{$chunk} ) {
275         return $template{$flavour}{$chunk};
276     }
277     elsif ( defined $template{error}{$chunk} ) {
278         return $template{error}{$chunk};
279     }
280     else {
281         return '';
282     }
283 };
284
285 # Bring in the templates
286 %template = ();
287 while (<DATA>) {
288     last if /^(__END__)$/;
289     my ( $ct, $comp, $txt ) = /^(\S+)\s(\S+)(?:\s(.*))?$/ or next;
290     $txt =~ s/\\n/\n/mg;
291     $template{$ct}{$comp} .= $txt . "\n";
292 }
293
294 # Plugins: Start
295 my $path_sep = $^O eq 'MSWin32' ? ';' : ':';
296 my @plugin_dirs = split /$path_sep/, $plugin_path;
297 unshift @plugin_dirs, $plugin_dir;
298 my @plugin_list = ();
299 my %plugin_hash = ();
300
301 # If $plugin_list is set, read plugins to use from that file
302 if ( $plugin_list ) {
303     if ( -r $plugin_list and $fh->open("< $plugin_list") ) {
304         @plugin_list = map { chomp $_; $_ } grep { /\S/ && !/^#/ } <$fh>;
305         $fh->close;
306     }
307     else {
308         warn "unable to read or open plugin_list '$plugin_list': $!";
309         $plugin_list = '';
310     }
311 }
312
313 # Otherwise walk @plugin_dirs to get list of plugins to use
314 if ( ! @plugin_list && @plugin_dirs ) {
315     for my $plugin_dir (@plugin_dirs) {
316         next unless -d $plugin_dir;
317         if ( opendir PLUGINS, $plugin_dir ) {
318             for my $plugin (
319                 grep { /^[\w:]+$/ && !/~$/ && -f "$plugin_dir/$_" }
320                 readdir(PLUGINS) )
321             {
322
323                 # Ignore duplicates
324                 next if $plugin_hash{$plugin};
325
326                 # Add to @plugin_list and %plugin_hash
327                 $plugin_hash{$plugin} = "$plugin_dir/$plugin";
328                 push @plugin_list, $plugin;
329             }
330             closedir PLUGINS;
331         }
332     }
333     @plugin_list = sort @plugin_list;
334 }
335
336 # Load all plugins in @plugin_list
337 unshift @INC, @plugin_dirs;
338 foreach my $plugin (@plugin_list) {
339     my ( $plugin_name, $off ) = $plugin =~ /^\d*([\w:]+?)(_?)$/;
340     my $plugin_file = $plugin_list ? $plugin_name : $plugin;
341     my $on_off = $off eq '_' ? -1 : 1;
342
343     # Allow perl module plugins
344     # The -z test is a hack to allow a zero-length placeholder file in a 
345     #   $plugin_path directory to indicate an @INC module should be loaded
346     if ( $plugin =~ m/::/ && ( $plugin_list || -z $plugin_hash{$plugin} ) ) {
347
348      # For Blosxom::Plugin::Foo style plugins, we need to use a string require
349         eval "require $plugin_file";
350     }
351     else
352     { # we try first to load from $plugin_dir before attempting from $plugin_path
353         eval        { require "$plugin_dir/$plugin_file" }
354             or eval { require $plugin_file };
355     }
356
357     if ($@) {
358         warn "error finding or loading blosxom plugin '$plugin_name': $@";
359         next;
360     }
361     if ( $plugin_name->start() and ( $plugins{$plugin_name} = $on_off ) ) {
362         push @plugins, $plugin_name;
363     }
364
365 }
366 shift @INC foreach @plugin_dirs;
367
368 # Plugins: Template
369 # Allow for the first encountered plugin::template subroutine to override the
370 # default built-in template subroutine
371 foreach my $plugin (@plugins) {
372     if ( $plugins{$plugin} > 0 and $plugin->can('template') ) {
373         if ( my $tmp = $plugin->template() ) {
374             $template = $tmp;
375             last;
376         }
377     }
378 }
379
380 # Provide backward compatibility for Blosxom < 2.0rc1 plug-ins
381 sub load_template {
382     return &$template(@_);
383 }
384
385 # Define default entries subroutine
386 $entries = sub {
387     my ( %files, %indexes, %others );
388     find(
389         sub {
390             my $d;
391             my $curr_depth = $File::Find::dir =~ tr[/][];
392             return if $depth and $curr_depth > $depth;
393
394             if (
395
396                 # a match
397                 $File::Find::name
398                 =~ m!^$datadir/(?:(.*)/)?(.+)\.$file_extension$!
399
400                 # not an index, .file, and is readable
401                 and $2 ne 'index' and $2 !~ /^\./ and ( -r $File::Find::name )
402                 )
403             {
404
405                 # read modification time
406                 my $mtime = stat($File::Find::name)->mtime or return;
407
408                 # to show or not to show future entries
409                 return unless ( $show_future_entries or $mtime < time );
410
411                 # add the file and its associated mtime to the list of files
412                 $files{$File::Find::name} = $mtime;
413
414                 # static rendering bits
415                 my $static_file
416                     = "$static_dir/$1/index." . $static_flavours[0];
417                 if (   param('-all')
418                     or !-f $static_file
419                     or stat($static_file)->mtime < $mtime )
420                 {
421                     $indexes{$1} = 1;
422                     $d = join( '/', ( nice_date($mtime) )[ 5, 2, 3 ] );
423                     $indexes{$d} = $d;
424                     $indexes{ ( $1 ? "$1/" : '' ) . "$2.$file_extension" } = 1
425                         if $static_entries;
426                 }
427             }
428
429             # not an entries match
430             elsif ( !-d $File::Find::name and -r $File::Find::name ) {
431                 $others{$File::Find::name} = stat($File::Find::name)->mtime;
432             }
433         },
434         $datadir
435     );
436
437     return ( \%files, \%indexes, \%others );
438 };
439
440 # Plugins: Entries
441 # Allow for the first encountered plugin::entries subroutine to override the
442 # default built-in entries subroutine
443 foreach my $plugin (@plugins) {
444     if ( $plugins{$plugin} > 0 and $plugin->can('entries') ) {
445         if ( my $tmp = $plugin->entries() ) {
446             $entries = $tmp;
447             last;
448         }
449     }
450 }
451
452 my ( $files, $indexes, $others ) = &$entries();
453 %indexes = %$indexes;
454
455 # Static
456 if (    !$ENV{GATEWAY_INTERFACE}
457     and param('-password')
458     and $static_password
459     and param('-password') eq $static_password )
460 {
461
462     param('-quiet') or print "Blosxom is generating static index pages...\n";
463
464     # Home Page and Directory Indexes
465     my %done;
466     foreach my $path ( sort keys %indexes ) {
467         my $p = '';
468         foreach ( ( '', split /\//, $path ) ) {
469             $p .= "/$_";
470             $p =~ s!^/!!;
471             next if $done{$p}++;
472             mkdir "$static_dir/$p", 0755
473                 unless ( -d "$static_dir/$p" or $p =~ /\.$file_extension$/ );
474             foreach $flavour (@static_flavours) {
475                 $content_type
476                     = ( &$template( $p, 'content_type', $flavour ) );
477                 $content_type =~ s!\n.*!!s;
478                 my $fn = $p =~ m!^(.+)\.$file_extension$! ? $1 : "$p/index";
479                 param('-quiet') or print "$fn.$flavour\n";
480                 my $fh_w = new FileHandle "> $static_dir/$fn.$flavour"
481                     or die "Couldn't open $static_dir/$p for writing: $!";
482                 $output = '';
483                 if ( $indexes{$path} == 1 ) {
484
485                     # category
486                     $path_info = $p;
487
488                     # individual story
489                     $path_info =~ s!\.$file_extension$!\.$flavour!;
490                     print $fh_w &generate( 'static', $path_info, '', $flavour,
491                         $content_type );
492                 }
493                 else {
494
495                     # date
496                     local (
497                         $path_info_yr, $path_info_mo,
498                         $path_info_da, $path_info
499                     ) = split /\//, $p, 4;
500                     unless ( defined $path_info ) { $path_info = "" }
501                     print $fh_w &generate( 'static', '', $p, $flavour,
502                         $content_type );
503                 }
504                 $fh_w->close;
505             }
506         }
507     }
508 }
509
510 # Dynamic
511 else {
512     $content_type = ( &$template( $path_info, 'content_type', $flavour ) );
513     $content_type =~ s!\n.*!!s;
514
515     $content_type =~ s/(\$\w+(?:::\w+)*)/"defined $1 ? $1 : ''"/gee;
516     $header = { -type => $content_type };
517
518     print generate( 'dynamic', $path_info,
519         "$path_info_yr/$path_info_mo_num/$path_info_da",
520         $flavour, $content_type );
521 }
522
523 # Plugins: End
524 foreach my $plugin (@plugins) {
525     if ( $plugins{$plugin} > 0 and $plugin->can('end') ) {
526         $entries = $plugin->end();
527     }
528 }
529
530 # Generate
531 sub generate {
532     my ( $static_or_dynamic, $currentdir, $date, $flavour, $content_type )
533         = @_;
534
535     %files = %$files;
536     %others = ref $others ? %$others : ();
537
538     # Plugins: Filter
539     foreach my $plugin (@plugins) {
540         if ( $plugins{$plugin} > 0 and $plugin->can('filter') ) {
541             $entries = $plugin->filter( \%files, \%others );
542         }
543     }
544
545     my %f = %files;
546
547     # Plugins: Skip
548     # Allow plugins to decide if we can cut short story generation
549     my $skip;
550     foreach my $plugin (@plugins) {
551         if ( $plugins{$plugin} > 0 and $plugin->can('skip') ) {
552             if ( my $tmp = $plugin->skip() ) {
553                 $skip = $tmp;
554                 last;
555             }
556         }
557     }
558
559     # Define default interpolation subroutine
560     $interpolate = sub {
561         package blosxom;
562         my $template = shift;
563         # Interpolate scalars, namespaced scalars, and hash/hashref scalars
564         $template =~ s/(\$\w+(?:::\w+)*(?:(?:->)?{(['"]?)[-\w]+\2})?)/"defined $1 ? $1 : ''"/gee;
565         return $template;
566     };
567
568     unless ( defined($skip) and $skip ) {
569
570         # Plugins: Interpolate
571         # Allow for the first encountered plugin::interpolate subroutine to
572         # override the default built-in interpolate subroutine
573         foreach my $plugin (@plugins) {
574             if ( $plugins{$plugin} > 0 and $plugin->can('interpolate') ) {
575                 if ( my $tmp = $plugin->interpolate() ) {
576                     $interpolate = $tmp;
577                     last;
578                 }
579             }
580         }
581
582         # Head
583         my $head = ( &$template( $currentdir, 'head', $flavour ) );
584
585         # Plugins: Head
586         foreach my $plugin (@plugins) {
587             if ( $plugins{$plugin} > 0 and $plugin->can('head') ) {
588                 $entries = $plugin->head( $currentdir, \$head );
589             }
590         }
591
592         $head = &$interpolate($head);
593
594         $output .= $head;
595
596         # Stories
597         my $curdate = '';
598         my $ne      = $num_entries;
599
600         if ( $currentdir =~ /(.*?)([^\/]+)\.(.+)$/ and $2 ne 'index' ) {
601             $currentdir = "$1$2.$file_extension";
602             %f = ( "$datadir/$currentdir" => $files{"$datadir/$currentdir"} )
603                 if $files{"$datadir/$currentdir"};
604         }
605         else {
606             $currentdir =~ s!/index\..+$!!;
607         }
608
609         # Define a default sort subroutine
610         my $sort = sub {
611             my ($files_ref) = @_;
612             return
613                 sort { $files_ref->{$b} <=> $files_ref->{$a} }
614                 keys %$files_ref;
615         };
616
617      # Plugins: Sort
618      # Allow for the first encountered plugin::sort subroutine to override the
619      # default built-in sort subroutine
620         foreach my $plugin (@plugins) {
621             if ( $plugins{$plugin} > 0 and $plugin->can('sort') ) {
622                 if ( my $tmp = $plugin->sort() ) {
623                     $sort = $tmp;
624                     last;
625                 }
626             }
627         }
628
629         foreach my $path_file ( &$sort( \%f, \%others ) ) {
630             last if $ne <= 0 && $date !~ /\d/;
631             use vars qw/ $path $fn /;
632             ( $path, $fn )
633                 = $path_file =~ m!^$datadir/(?:(.*)/)?(.*)\.$file_extension!;
634
635             # Only stories in the right hierarchy
636             $path =~ /^$currentdir/
637                 or $path_file eq "$datadir/$currentdir"
638                 or next;
639
640             # Prepend a slash for use in templates only if a path exists
641             $path &&= "/$path";
642
643             # Date fiddling for by-{year,month,day} archive views
644             use vars
645                 qw/ $dw $mo $mo_num $da $ti $yr $hr $min $hr12 $ampm $utc_offset/;
646             ( $dw, $mo, $mo_num, $da, $ti, $yr, $utc_offset )
647                 = nice_date( $files{"$path_file"} );
648             ( $hr, $min ) = split /:/, $ti;
649             ( $hr12, $ampm ) = $hr >= 12 ? ( $hr - 12, 'pm' ) : ( $hr, 'am' );
650             $hr12 =~ s/^0//;
651             if ( $hr12 == 0 ) { $hr12 = 12 }
652
653             # Only stories from the right date
654             my ( $path_info_yr, $path_info_mo_num, $path_info_da )
655                 = split /\//, $date;
656             next if $path_info_yr     && $yr != $path_info_yr;
657             last if $path_info_yr     && $yr < $path_info_yr;
658             next if $path_info_mo_num && $mo ne $num2month[$path_info_mo_num];
659             next if $path_info_da     && $da != $path_info_da;
660             last if $path_info_da     && $da < $path_info_da;
661
662             # Date
663             my $date = ( &$template( $path, 'date', $flavour ) );
664
665             # Plugins: Date
666             foreach my $plugin (@plugins) {
667                 if ( $plugins{$plugin} > 0 and $plugin->can('date') ) {
668                     $entries
669                         = $plugin->date( $currentdir, \$date,
670                         $files{$path_file}, $dw, $mo, $mo_num, $da, $ti,
671                         $yr );
672                 }
673             }
674
675             $date = &$interpolate($date);
676
677             if ( $date && $curdate ne $date ) {
678                 $curdate = $date;
679                 $output .= $date;
680             }
681
682             use vars qw/ $title $body $raw /;
683             if ( -f "$path_file" && $fh->open("< $path_file") ) {
684                 chomp( $title = <$fh> );
685                 chomp( $body = join '', <$fh> );
686                 $fh->close;
687                 $raw = "$title\n$body";
688             }
689             my $story = ( &$template( $path, 'story', $flavour ) );
690
691             # Plugins: Story
692             foreach my $plugin (@plugins) {
693                 if ( $plugins{$plugin} > 0 and $plugin->can('story') ) {
694                     $entries = $plugin->story( $path, $fn, \$story, \$title,
695                         \$body );
696                 }
697             }
698
699             if ( $encode_xml_entities &&
700                  $content_type =~ m{\bxml\b} &&
701                  $content_type !~ m{\bxhtml\b} ) {
702                 # Escape special characters inside the <link> container
703
704                 # The following line should be moved more towards to top for
705                 # performance reasons -- Axel Beckert, 2008-07-22
706                 my $url_escape_re = qr([^-/a-zA-Z0-9:._]);
707
708                 $url   =~ s($url_escape_re)(sprintf('%%%02X', ord($&)))eg;
709                 $path  =~ s($url_escape_re)(sprintf('%%%02X', ord($&)))eg;
710                 $fn    =~ s($url_escape_re)(sprintf('%%%02X', ord($&)))eg;
711
712                 # Escape <, >, and &, and to produce valid RSS
713                 my %escape = (
714                     '<' => '&lt;',
715                     '>' => '&gt;',
716                     '&' => '&amp;',
717                     '"' => '&quot;',
718                     "'" => '&apos;'
719                 );
720                 my $escape_re = join '|' => keys %escape;
721                 $title =~ s/($escape_re)/$escape{$1}/g;
722                 $body  =~ s/($escape_re)/$escape{$1}/g;
723                 $url   =~ s/($escape_re)/$escape{$1}/g;
724                 $path  =~ s/($escape_re)/$escape{$1}/g;
725                 $fn    =~ s/($escape_re)/$escape{$1}/g;
726             }
727
728             $story = &$interpolate($story);
729
730             $output .= $story;
731             $fh->close;
732
733             $ne--;
734         }
735
736         # Foot
737         my $foot = ( &$template( $currentdir, 'foot', $flavour ) );
738
739         # Plugins: Foot
740         foreach my $plugin (@plugins) {
741             if ( $plugins{$plugin} > 0 and $plugin->can('foot') ) {
742                 $entries = $plugin->foot( $currentdir, \$foot );
743             }
744         }
745
746         $foot = &$interpolate($foot);
747         $output .= $foot;
748
749         # Plugins: Last
750         foreach my $plugin (@plugins) {
751             if ( $plugins{$plugin} > 0 and $plugin->can('last') ) {
752                 $entries = $plugin->last();
753             }
754         }
755
756     }    # End skip
757
758     # Finally, add the header, if any and running dynamically
759     $output = header($header) . $output
760         if ( $static_or_dynamic eq 'dynamic' and $header );
761
762     $output;
763 }
764
765 sub nice_date {
766     my ($unixtime) = @_;
767
768     my $c_time = CORE::localtime($unixtime);
769     my ( $dw, $mo, $da, $hr, $min, $sec, $yr )
770         = ( $c_time
771             =~ /(\w{3}) +(\w{3}) +(\d{1,2}) +(\d{2}):(\d{2}):(\d{2}) +(\d{4})$/
772         );
773     $ti = "$hr:$min";
774     $da = sprintf( "%02d", $da );
775     my $mo_num = $month2num{$mo};
776
777     my $offset
778         = timegm( $sec, $min, $hr, $da, $mo_num - 1, $yr - 1900 ) - $unixtime;
779     my $utc_offset = sprintf( "%+03d", int( $offset / 3600 ) )
780         . sprintf( "%02d", ( $offset % 3600 ) / 60 );
781
782     return ( $dw, $mo, $mo_num, $da, $ti, $yr, $utc_offset );
783 }
784
785 # Default HTML and RSS template bits
786 __DATA__
787 html content_type text/html; charset=$blog_encoding
788
789 html head <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
790 html head <html>
791 html head     <head>
792 html head         <meta http-equiv="content-type" content="$content_type" >
793 html head         <link rel="alternate" type="application/rss+xml" title="RSS" href="$url/index.rss" >
794 html head         <title>$blog_title $path_info_da $path_info_mo $path_info_yr</title>
795 html head     </head>
796 html head     <body>
797 html head         <div align="center">
798 html head             <h1>$blog_title</h1>
799 html head             <p>$path_info_da $path_info_mo $path_info_yr</p>
800 html head         </div>
801
802 html story         <div>
803 html story             <h3><a name="$fn">$title</a></h3>
804 html story             <div>$body</div>
805 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>
806 html story         </div>
807
808 html date         <h2>$dw, $da $mo $yr</h2>
809
810 html foot
811 html foot         <div align="center">
812 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>
813 html foot         </div>
814 html foot     </body>
815 html foot </html>
816
817 rss content_type text/xml; charset=$blog_encoding
818
819 rss head <?xml version="1.0" encoding="$blog_encoding"?>
820 rss head <rss version="2.0">
821 rss head   <channel>
822 rss head     <title>$blog_title</title>
823 rss head     <link>$url/$path_info</link>
824 rss head     <description>$blog_description</description>
825 rss head     <language>$blog_language</language>
826 rss head     <docs>http://blogs.law.harvard.edu/tech/rss</docs>
827 rss head     <generator>blosxom/$version</generator>
828
829 rss story   <item>
830 rss story     <title>$title</title>
831 rss story     <pubDate>$dw, $da $mo $yr $ti:00 $utc_offset</pubDate>
832 rss story     <link>$url/$yr/$mo_num/$da#$fn</link>
833 rss story     <category>$path</category>
834 rss story     <guid isPermaLink="false">$url$path/$fn</guid>
835 rss story     <description>$body</description>
836 rss story   </item>
837
838 rss date 
839
840 rss foot   </channel>
841 rss foot </rss>
842
843 error content_type text/html
844
845 error head <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
846 error head <html>
847 error head <head><title>Error: unknown Blosxom flavour "$flavour"</title></head>
848 error head     <body>
849 error head         <h1><font color="red">Error: unknown Blosxom flavour "$flavour"</font></h1>
850 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>
851
852 error story        <h3>$title</h3>
853 error story        <div>$body</div> <p><a href="$url/$yr/$mo_num/$da#fn.$default_flavour">#</a></p>
854
855 error date         <h2>$dw, $da $mo $yr</h2>
856
857 error foot     </body>
858 error foot </html>
859 __END__
860