Declaring this state as bugfix release 2.1.1, the "never trust a dot zero 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.1 ($Id: blosxom.cgi,v 1.83 2008/07/30 22:27:02 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.1";
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 # Global variable to be used in head/foot.{flavour} templates
218 $path_info = '';
219 # Add all @path_info elements to $path_info till we come to one that could be a year
220 while ( $path_info[0] && $path_info[0] !~ /^(19|20)\d{2}$/) {
221     $path_info .= '/' . shift @path_info;
222 }
223
224 # Pull date elements out of path
225 if ($path_info[0] && $path_info[0] =~ /^(19|20)\d{2}$/) {
226   $path_info_yr = shift @path_info;
227   if ($path_info[0] && 
228      ($path_info[0] =~ /^(0\d|1[012])$/ || 
229       exists $month2num{ ucfirst lc $path_info_mo })) {
230     $path_info_mo = shift @path_info;
231     # Map path_info_mo to numeric $path_info_mo_num
232     $path_info_mo_num = $path_info_mo =~ /^\d{2}$/
233       ? $path_info_mo
234       : $month2num{ ucfirst lc $path_info_mo };
235     if ($path_info[0] && $path_info[0] =~ /^[0123]\d$/) {
236       $path_info_da = shift @path_info;
237     }
238   }
239 }
240
241 # Add remaining path elements to $path_info
242 $path_info .= '/' . join('/', @path_info);
243
244 # Strip spurious slashes
245 $path_info =~ s!(^/*)|(/*$)!!g;
246
247 # Define standard template subroutine, plugin-overridable at Plugins: Template
248 $template = sub {
249     my ( $path, $chunk, $flavour ) = @_;
250
251     do {
252         return join '', <$fh>
253             if $fh->open("< $datadir/$path/$chunk.$flavour");
254     } while ( $path =~ s/(\/*[^\/]*)$// and $1 );
255
256     # Check for definedness, since flavour can be the empty string
257     if ( defined $template{$flavour}{$chunk} ) {
258         return $template{$flavour}{$chunk};
259     }
260     elsif ( defined $template{error}{$chunk} ) {
261         return $template{error}{$chunk};
262     }
263     else {
264         return '';
265     }
266 };
267
268 # Bring in the templates
269 %template = ();
270 while (<DATA>) {
271     last if /^(__END__)$/;
272     my ( $ct, $comp, $txt ) = /^(\S+)\s(\S+)(?:\s(.*))?$/ or next;
273     $txt =~ s/\\n/\n/mg;
274     $template{$ct}{$comp} .= $txt . "\n";
275 }
276
277 # Plugins: Start
278 my $path_sep = $^O eq 'MSWin32' ? ';' : ':';
279 my @plugin_dirs = split /$path_sep/, $plugin_path;
280 unshift @plugin_dirs, $plugin_dir;
281 my @plugin_list = ();
282 my %plugin_hash = ();
283
284 # If $plugin_list is set, read plugins to use from that file
285 if ( $plugin_list ) {
286     if ( -r $plugin_list and $fh->open("< $plugin_list") ) {
287         @plugin_list = map { chomp $_; $_ } grep { /\S/ && !/^#/ } <$fh>;
288         $fh->close;
289     }
290     else {
291         warn "unable to read or open plugin_list '$plugin_list': $!";
292         $plugin_list = '';
293     }
294 }
295
296 # Otherwise walk @plugin_dirs to get list of plugins to use
297 if ( ! @plugin_list && @plugin_dirs ) {
298     for my $plugin_dir (@plugin_dirs) {
299         next unless -d $plugin_dir;
300         if ( opendir PLUGINS, $plugin_dir ) {
301             for my $plugin (
302                 grep { /^[\w:]+$/ && !/~$/ && -f "$plugin_dir/$_" }
303                 readdir(PLUGINS) )
304             {
305
306                 # Ignore duplicates
307                 next if $plugin_hash{$plugin};
308
309                 # Add to @plugin_list and %plugin_hash
310                 $plugin_hash{$plugin} = "$plugin_dir/$plugin";
311                 push @plugin_list, $plugin;
312             }
313             closedir PLUGINS;
314         }
315     }
316     @plugin_list = sort @plugin_list;
317 }
318
319 # Load all plugins in @plugin_list
320 unshift @INC, @plugin_dirs;
321 foreach my $plugin (@plugin_list) {
322     my ( $plugin_name, $off ) = $plugin =~ /^\d*([\w:]+?)(_?)$/;
323     my $plugin_file = $plugin_list ? $plugin_name : $plugin;
324     my $on_off = $off eq '_' ? -1 : 1;
325
326     # Allow perl module plugins
327     # The -z test is a hack to allow a zero-length placeholder file in a 
328     #   $plugin_path directory to indicate an @INC module should be loaded
329     if ( $plugin =~ m/::/ && ( $plugin_list || -z $plugin_hash{$plugin} ) ) {
330
331      # For Blosxom::Plugin::Foo style plugins, we need to use a string require
332         eval "require $plugin_file";
333     }
334     else
335     { # we try first to load from $plugin_dir before attempting from $plugin_path
336         eval        { require "$plugin_dir/$plugin_file" }
337             or eval { require $plugin_file };
338     }
339
340     if ($@) {
341         warn "error finding or loading blosxom plugin '$plugin_name': $@";
342         next;
343     }
344     if ( $plugin_name->start() and ( $plugins{$plugin_name} = $on_off ) ) {
345         push @plugins, $plugin_name;
346     }
347
348 }
349 shift @INC foreach @plugin_dirs;
350
351 # Plugins: Template
352 # Allow for the first encountered plugin::template subroutine to override the
353 # default built-in template subroutine
354 foreach my $plugin (@plugins) {
355     if ( $plugins{$plugin} > 0 and $plugin->can('template') ) {
356         if ( my $tmp = $plugin->template() ) {
357             $template = $tmp;
358             last;
359         }
360     }
361 }
362
363 # Provide backward compatibility for Blosxom < 2.0rc1 plug-ins
364 sub load_template {
365     return &$template(@_);
366 }
367
368 # Define default entries subroutine
369 $entries = sub {
370     my ( %files, %indexes, %others );
371     find(
372         sub {
373             my $d;
374             my $curr_depth = $File::Find::dir =~ tr[/][];
375             return if $depth and $curr_depth > $depth;
376
377             if (
378
379                 # a match
380                 $File::Find::name
381                 =~ m!^$datadir/(?:(.*)/)?(.+)\.$file_extension$!
382
383                 # not an index, .file, and is readable
384                 and $2 ne 'index' and $2 !~ /^\./ and ( -r $File::Find::name )
385                 )
386             {
387
388                 # read modification time
389                 my $mtime = stat($File::Find::name)->mtime or return;
390
391                 # to show or not to show future entries
392                 return unless ( $show_future_entries or $mtime < time );
393
394                 # add the file and its associated mtime to the list of files
395                 $files{$File::Find::name} = $mtime;
396
397                 # static rendering bits
398                 my $static_file
399                     = "$static_dir/$1/index." . $static_flavours[0];
400                 if (   param('-all')
401                     or !-f $static_file
402                     or stat($static_file)->mtime < $mtime )
403                 {
404                     $indexes{$1} = 1;
405                     $d = join( '/', ( nice_date($mtime) )[ 5, 2, 3 ] );
406                     $indexes{$d} = $d;
407                     $indexes{ ( $1 ? "$1/" : '' ) . "$2.$file_extension" } = 1
408                         if $static_entries;
409                 }
410             }
411
412             # not an entries match
413             elsif ( !-d $File::Find::name and -r $File::Find::name ) {
414                 $others{$File::Find::name} = stat($File::Find::name)->mtime;
415             }
416         },
417         $datadir
418     );
419
420     return ( \%files, \%indexes, \%others );
421 };
422
423 # Plugins: Entries
424 # Allow for the first encountered plugin::entries subroutine to override the
425 # default built-in entries subroutine
426 foreach my $plugin (@plugins) {
427     if ( $plugins{$plugin} > 0 and $plugin->can('entries') ) {
428         if ( my $tmp = $plugin->entries() ) {
429             $entries = $tmp;
430             last;
431         }
432     }
433 }
434
435 my ( $files, $indexes, $others ) = &$entries();
436 %indexes = %$indexes;
437
438 # Static
439 if (    !$ENV{GATEWAY_INTERFACE}
440     and param('-password')
441     and $static_password
442     and param('-password') eq $static_password )
443 {
444
445     param('-quiet') or print "Blosxom is generating static index pages...\n";
446
447     # Home Page and Directory Indexes
448     my %done;
449     foreach my $path ( sort keys %indexes ) {
450         my $p = '';
451         foreach ( ( '', split /\//, $path ) ) {
452             $p .= "/$_";
453             $p =~ s!^/!!;
454             next if $done{$p}++;
455             mkdir "$static_dir/$p", 0755
456                 unless ( -d "$static_dir/$p" or $p =~ /\.$file_extension$/ );
457             foreach $flavour (@static_flavours) {
458                 $content_type
459                     = ( &$template( $p, 'content_type', $flavour ) );
460                 $content_type =~ s!\n.*!!s;
461                 my $fn = $p =~ m!^(.+)\.$file_extension$! ? $1 : "$p/index";
462                 param('-quiet') or print "$fn.$flavour\n";
463                 my $fh_w = new FileHandle "> $static_dir/$fn.$flavour"
464                     or die "Couldn't open $static_dir/$p for writing: $!";
465                 $output = '';
466                 if ( $indexes{$path} == 1 ) {
467
468                     # category
469                     $path_info = $p;
470
471                     # individual story
472                     $path_info =~ s!\.$file_extension$!\.$flavour!;
473                     print $fh_w &generate( 'static', $path_info, '', $flavour,
474                         $content_type );
475                 }
476                 else {
477
478                     # date
479                     local (
480                         $path_info_yr, $path_info_mo,
481                         $path_info_da, $path_info
482                     ) = split /\//, $p, 4;
483                     unless ( defined $path_info ) { $path_info = "" }
484                     print $fh_w &generate( 'static', '', $p, $flavour,
485                         $content_type );
486                 }
487                 $fh_w->close;
488             }
489         }
490     }
491 }
492
493 # Dynamic
494 else {
495     $content_type = ( &$template( $path_info, 'content_type', $flavour ) );
496     $content_type =~ s!\n.*!!s;
497
498     $content_type =~ s/(\$\w+(?:::\w+)*)/"defined $1 ? $1 : ''"/gee;
499     $header = { -type => $content_type };
500
501     print generate( 'dynamic', $path_info,
502         "$path_info_yr/$path_info_mo_num/$path_info_da",
503         $flavour, $content_type );
504 }
505
506 # Plugins: End
507 foreach my $plugin (@plugins) {
508     if ( $plugins{$plugin} > 0 and $plugin->can('end') ) {
509         $entries = $plugin->end();
510     }
511 }
512
513 # Generate
514 sub generate {
515     my ( $static_or_dynamic, $currentdir, $date, $flavour, $content_type )
516         = @_;
517
518     %files = %$files;
519     %others = ref $others ? %$others : ();
520
521     # Plugins: Filter
522     foreach my $plugin (@plugins) {
523         if ( $plugins{$plugin} > 0 and $plugin->can('filter') ) {
524             $entries = $plugin->filter( \%files, \%others );
525         }
526     }
527
528     my %f = %files;
529
530     # Plugins: Skip
531     # Allow plugins to decide if we can cut short story generation
532     my $skip;
533     foreach my $plugin (@plugins) {
534         if ( $plugins{$plugin} > 0 and $plugin->can('skip') ) {
535             if ( my $tmp = $plugin->skip() ) {
536                 $skip = $tmp;
537                 last;
538             }
539         }
540     }
541
542     # Define default interpolation subroutine
543     $interpolate = sub {
544         package blosxom;
545         my $template = shift;
546         # Interpolate scalars, namespaced scalars, and hash/hashref scalars
547         $template =~ s/(\$\w+(?:::\w+)*(?:(?:->)?{(['"]?)[-\w]+\2})?)/"defined $1 ? $1 : ''"/gee;
548         return $template;
549     };
550
551     unless ( defined($skip) and $skip ) {
552
553         # Plugins: Interpolate
554         # Allow for the first encountered plugin::interpolate subroutine to
555         # override the default built-in interpolate subroutine
556         foreach my $plugin (@plugins) {
557             if ( $plugins{$plugin} > 0 and $plugin->can('interpolate') ) {
558                 if ( my $tmp = $plugin->interpolate() ) {
559                     $interpolate = $tmp;
560                     last;
561                 }
562             }
563         }
564
565         # Head
566         my $head = ( &$template( $currentdir, 'head', $flavour ) );
567
568         # Plugins: Head
569         foreach my $plugin (@plugins) {
570             if ( $plugins{$plugin} > 0 and $plugin->can('head') ) {
571                 $entries = $plugin->head( $currentdir, \$head );
572             }
573         }
574
575         $head = &$interpolate($head);
576
577         $output .= $head;
578
579         # Stories
580         my $curdate = '';
581         my $ne      = $num_entries;
582
583         if ( $currentdir =~ /(.*?)([^\/]+)\.(.+)$/ and $2 ne 'index' ) {
584             $currentdir = "$1$2.$file_extension";
585             %f = ( "$datadir/$currentdir" => $files{"$datadir/$currentdir"} )
586                 if $files{"$datadir/$currentdir"};
587         }
588         else {
589             $currentdir =~ s!/index\..+$!!;
590         }
591
592         # Define a default sort subroutine
593         my $sort = sub {
594             my ($files_ref) = @_;
595             return
596                 sort { $files_ref->{$b} <=> $files_ref->{$a} }
597                 keys %$files_ref;
598         };
599
600      # Plugins: Sort
601      # Allow for the first encountered plugin::sort subroutine to override the
602      # default built-in sort subroutine
603         foreach my $plugin (@plugins) {
604             if ( $plugins{$plugin} > 0 and $plugin->can('sort') ) {
605                 if ( my $tmp = $plugin->sort() ) {
606                     $sort = $tmp;
607                     last;
608                 }
609             }
610         }
611
612         foreach my $path_file ( &$sort( \%f, \%others ) ) {
613             last if $ne <= 0 && $date !~ /\d/;
614             use vars qw/ $path $fn /;
615             ( $path, $fn )
616                 = $path_file =~ m!^$datadir/(?:(.*)/)?(.*)\.$file_extension!;
617
618             # Only stories in the right hierarchy
619             $path =~ /^$currentdir/
620                 or $path_file eq "$datadir/$currentdir"
621                 or next;
622
623             # Prepend a slash for use in templates only if a path exists
624             $path &&= "/$path";
625
626             # Date fiddling for by-{year,month,day} archive views
627             use vars
628                 qw/ $dw $mo $mo_num $da $ti $yr $hr $min $hr12 $ampm $utc_offset/;
629             ( $dw, $mo, $mo_num, $da, $ti, $yr, $utc_offset )
630                 = nice_date( $files{"$path_file"} );
631             ( $hr, $min ) = split /:/, $ti;
632             ( $hr12, $ampm ) = $hr >= 12 ? ( $hr - 12, 'pm' ) : ( $hr, 'am' );
633             $hr12 =~ s/^0//;
634             if ( $hr12 == 0 ) { $hr12 = 12 }
635
636             # Only stories from the right date
637             my ( $path_info_yr, $path_info_mo_num, $path_info_da )
638                 = split /\//, $date;
639             next if $path_info_yr     && $yr != $path_info_yr;
640             last if $path_info_yr     && $yr < $path_info_yr;
641             next if $path_info_mo_num && $mo ne $num2month[$path_info_mo_num];
642             next if $path_info_da     && $da != $path_info_da;
643             last if $path_info_da     && $da < $path_info_da;
644
645             # Date
646             my $date = ( &$template( $path, 'date', $flavour ) );
647
648             # Plugins: Date
649             foreach my $plugin (@plugins) {
650                 if ( $plugins{$plugin} > 0 and $plugin->can('date') ) {
651                     $entries
652                         = $plugin->date( $currentdir, \$date,
653                         $files{$path_file}, $dw, $mo, $mo_num, $da, $ti,
654                         $yr );
655                 }
656             }
657
658             $date = &$interpolate($date);
659
660             if ( $date && $curdate ne $date ) {
661                 $curdate = $date;
662                 $output .= $date;
663             }
664
665             use vars qw/ $title $body $raw /;
666             if ( -f "$path_file" && $fh->open("< $path_file") ) {
667                 chomp( $title = <$fh> );
668                 chomp( $body = join '', <$fh> );
669                 $fh->close;
670                 $raw = "$title\n$body";
671             }
672             my $story = ( &$template( $path, 'story', $flavour ) );
673
674             # Plugins: Story
675             foreach my $plugin (@plugins) {
676                 if ( $plugins{$plugin} > 0 and $plugin->can('story') ) {
677                     $entries = $plugin->story( $path, $fn, \$story, \$title,
678                         \$body );
679                 }
680             }
681
682             if ( $encode_xml_entities &&
683                  $content_type =~ m{\bxml\b} &&
684                  $content_type !~ m{\bxhtml\b} ) {
685                 # Escape special characters inside the <link> container
686
687                 # The following line should be moved more towards to top for
688                 # performance reasons -- Axel Beckert, 2008-07-22
689                 my $url_escape_re = qr([^-/a-zA-Z0-9:._]);
690
691                 $url   =~ s($url_escape_re)(sprintf('%%%02X', ord($&)))eg;
692                 $path  =~ s($url_escape_re)(sprintf('%%%02X', ord($&)))eg;
693                 $fn    =~ s($url_escape_re)(sprintf('%%%02X', ord($&)))eg;
694
695                 # Escape <, >, and &, and to produce valid RSS
696                 my %escape = (
697                     '<' => '&lt;',
698                     '>' => '&gt;',
699                     '&' => '&amp;',
700                     '"' => '&quot;',
701                     "'" => '&apos;'
702                 );
703                 my $escape_re = join '|' => keys %escape;
704                 $title =~ s/($escape_re)/$escape{$1}/g;
705                 $body  =~ s/($escape_re)/$escape{$1}/g;
706                 $url   =~ s/($escape_re)/$escape{$1}/g;
707                 $path  =~ s/($escape_re)/$escape{$1}/g;
708                 $fn    =~ s/($escape_re)/$escape{$1}/g;
709             }
710
711             $story = &$interpolate($story);
712
713             $output .= $story;
714             $fh->close;
715
716             $ne--;
717         }
718
719         # Foot
720         my $foot = ( &$template( $currentdir, 'foot', $flavour ) );
721
722         # Plugins: Foot
723         foreach my $plugin (@plugins) {
724             if ( $plugins{$plugin} > 0 and $plugin->can('foot') ) {
725                 $entries = $plugin->foot( $currentdir, \$foot );
726             }
727         }
728
729         $foot = &$interpolate($foot);
730         $output .= $foot;
731
732         # Plugins: Last
733         foreach my $plugin (@plugins) {
734             if ( $plugins{$plugin} > 0 and $plugin->can('last') ) {
735                 $entries = $plugin->last();
736             }
737         }
738
739     }    # End skip
740
741     # Finally, add the header, if any and running dynamically
742     $output = header($header) . $output
743         if ( $static_or_dynamic eq 'dynamic' and $header );
744
745     $output;
746 }
747
748 sub nice_date {
749     my ($unixtime) = @_;
750
751     my $c_time = CORE::localtime($unixtime);
752     my ( $dw, $mo, $da, $hr, $min, $sec, $yr )
753         = ( $c_time
754             =~ /(\w{3}) +(\w{3}) +(\d{1,2}) +(\d{2}):(\d{2}):(\d{2}) +(\d{4})$/
755         );
756     $ti = "$hr:$min";
757     $da = sprintf( "%02d", $da );
758     my $mo_num = $month2num{$mo};
759
760     my $offset
761         = timegm( $sec, $min, $hr, $da, $mo_num - 1, $yr - 1900 ) - $unixtime;
762     my $utc_offset = sprintf( "%+03d", int( $offset / 3600 ) )
763         . sprintf( "%02d", ( $offset % 3600 ) / 60 );
764
765     return ( $dw, $mo, $mo_num, $da, $ti, $yr, $utc_offset );
766 }
767
768 # Default HTML and RSS template bits
769 __DATA__
770 html content_type text/html; charset=$blog_encoding
771
772 html head <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
773 html head <html>
774 html head     <head>
775 html head         <meta http-equiv="content-type" content="$content_type" >
776 html head         <link rel="alternate" type="application/rss+xml" title="RSS" href="$url/index.rss" >
777 html head         <title>$blog_title $path_info_da $path_info_mo $path_info_yr</title>
778 html head     </head>
779 html head     <body>
780 html head         <div align="center">
781 html head             <h1>$blog_title</h1>
782 html head             <p>$path_info_da $path_info_mo $path_info_yr</p>
783 html head         </div>
784
785 html story         <div>
786 html story             <h3><a name="$fn">$title</a></h3>
787 html story             <div>$body</div>
788 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>
789 html story         </div>
790
791 html date         <h2>$dw, $da $mo $yr</h2>
792
793 html foot
794 html foot         <div align="center">
795 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>
796 html foot         </div>
797 html foot     </body>
798 html foot </html>
799
800 rss content_type text/xml; charset=$blog_encoding
801
802 rss head <?xml version="1.0" encoding="$blog_encoding"?>
803 rss head <rss version="2.0">
804 rss head   <channel>
805 rss head     <title>$blog_title</title>
806 rss head     <link>$url/$path_info</link>
807 rss head     <description>$blog_description</description>
808 rss head     <language>$blog_language</language>
809 rss head     <docs>http://blogs.law.harvard.edu/tech/rss</docs>
810 rss head     <generator>blosxom/$version</generator>
811
812 rss story   <item>
813 rss story     <title>$title</title>
814 rss story     <pubDate>$dw, $da $mo $yr $ti:00 $utc_offset</pubDate>
815 rss story     <link>$url/$yr/$mo_num/$da#$fn</link>
816 rss story     <category>$path</category>
817 rss story     <guid isPermaLink="false">$url$path/$fn</guid>
818 rss story     <description>$body</description>
819 rss story   </item>
820
821 rss date 
822
823 rss foot   </channel>
824 rss foot </rss>
825
826 error content_type text/html
827
828 error head <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
829 error head <html>
830 error head <head><title>Error: unknown Blosxom flavour "$flavour"</title></head>
831 error head     <body>
832 error head         <h1><font color="red">Error: unknown Blosxom flavour "$flavour"</font></h1>
833 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>
834
835 error story        <h3>$title</h3>
836 error story        <div>$body</div> <p><a href="$url/$yr/$mo_num/$da#fn.$default_flavour">#</a></p>
837
838 error date         <h2>$dw, $da $mo $yr</h2>
839
840 error foot     </body>
841 error foot </html>
842 __END__
843