]> git.deb.at Git - pkg/blosxom.git/blob - blosxom.cgi
allow newlines in DATA
[pkg/blosxom.git] / blosxom.cgi
1 #!/usr/bin/perl
2
3 # Blosxom
4 # Author: Rael Dornfest <rael@oreilly.com>
5 # Version: 2.0.1
6 # Home/Docs/Licensing: http://www.blosxom.com/
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 # Where are this blog's entries kept?
23 $datadir = "/Library/WebServer/Documents/blosxom";
24
25 # What's my preferred base URL for this blog (leave blank for automatic)?
26 $url = "";
27
28 # Should I stick only to the datadir for items or travel down the
29 # directory hierarchy looking for items?  If so, to what depth?
30 # 0 = infinite depth (aka grab everything), 1 = datadir only, n = n levels down
31 $depth = 0;
32
33 # How many entries should I show on the home page?
34 $num_entries = 40;
35
36 # What file extension signifies a blosxom entry?
37 $file_extension = "txt";
38
39 # What is the default flavour?
40 $default_flavour = "html";
41
42 # Should I show entries from the future (i.e. dated after now)?
43 $show_future_entries = 0;
44
45 # --- Plugins (Optional) -----
46
47 # Where are my plugins kept?
48 $plugin_dir = "";
49
50 # Where should my modules keep their state information?
51 $plugin_state_dir = "$plugin_dir/state";
52
53 # --- Static Rendering -----
54
55 # Where are this blog's static files to be created?
56 $static_dir = "/Library/WebServer/Documents/blog";
57
58 # What's my administrative password (you must set this for static rendering)?
59 $static_password = "";
60
61 # What flavours should I generate statically?
62 @static_flavours = qw/html rss/;
63
64 # Should I statically generate individual entries?
65 # 0 = no, 1 = yes
66 $static_entries = 0;
67
68 # --------------------------------
69
70 use vars qw! $version $blog_title $blog_description $blog_language $datadir $url %template $template $depth $num_entries $file_extension $default_flavour $static_or_dynamic $plugin_dir $plugin_state_dir @plugins %plugins $static_dir $static_password @static_flavours $static_entries $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 !;
71
72 use strict;
73 use FileHandle;
74 use File::Find;
75 use File::stat;
76 use Time::localtime;
77 use CGI qw/:standard :netscape/;
78
79 $version = "2.0";
80
81 my $fh = new FileHandle;
82
83 %month2num = (nil=>'00', Jan=>'01', Feb=>'02', Mar=>'03', Apr=>'04', May=>'05', Jun=>'06', Jul=>'07', Aug=>'08', Sep=>'09', Oct=>'10', Nov=>'11', Dec=>'12');
84 @num2month = sort { $month2num{$a} <=> $month2num{$b} } keys %month2num;
85
86 # Use the stated preferred URL or figure it out automatically
87 $url ||= url();
88 $url =~ s/^included:/http:/; # Fix for Server Side Includes (SSI)
89 $url =~ s!/$!!;
90
91 # Drop ending any / from dir settings
92 $datadir =~ s!/$!!; $plugin_dir =~ s!/$!!; $static_dir =~ s!/$!!;
93   
94 # Fix depth to take into account datadir's path
95 $depth and $depth += ($datadir =~ tr[/][]) - 1;
96
97 # Global variable to be used in head/foot.{flavour} templates
98 $path_info = '';
99
100 $static_or_dynamic = (!$ENV{GATEWAY_INTERFACE} and param('-password') and $static_password and param('-password') eq $static_password) ? 'static' : 'dynamic';
101 $static_or_dynamic eq 'dynamic' and param(-name=>'-quiet', -value=>1);
102
103 # Path Info Magic
104 # Take a gander at HTTP's PATH_INFO for optional blog name, archive yr/mo/day
105 my @path_info = split m{/}, path_info() || param('path'); 
106 shift @path_info;
107
108 while ($path_info[0] and $path_info[0] =~ /^[a-zA-Z].*$/ and $path_info[0] !~ /(.*)\.(.*)/) { $path_info .= '/' . shift @path_info; }
109
110 # Flavour specified by ?flav={flav} or index.{flav}
111 $flavour = '';
112
113 if ( $path_info[$#path_info] =~ /(.+)\.(.+)$/ ) {
114   $flavour = $2;
115   $1 ne 'index' and $path_info .= "/$1.$2";
116   pop @path_info;
117 } else {
118   $flavour = param('flav') || $default_flavour;
119 }
120
121 # Strip spurious slashes
122 $path_info =~ s!(^/*)|(/*$)!!g;
123
124 # Date fiddling
125 ($path_info_yr,$path_info_mo,$path_info_da) = @path_info;
126 $path_info_mo_num = $path_info_mo ? ( $path_info_mo =~ /\d{2}/ ? $path_info_mo : ($month2num{ucfirst(lc $path_info_mo)} || undef) ) : undef;
127
128 # Define standard template subroutine, plugin-overridable at Plugins: Template
129 $template = 
130   sub {
131     my ($path, $chunk, $flavour) = @_;
132
133     do {
134       return join '', <$fh> if $fh->open("< $datadir/$path/$chunk.$flavour");
135     } while ($path =~ s/(\/*[^\/]*)$// and $1);
136
137     return join '', ($template{$flavour}{$chunk} || $template{error}{$chunk} || '');
138   };
139 # Bring in the templates
140 %template = ();
141 while (<DATA>) {
142   last if /^(__END__)$/;
143   my($ct, $comp, $txt) = /^(\S+)\s(\S+)\s(.*)$/ or next;
144   $txt =~ s/\\n/\n/mg;
145   $template{$ct}{$comp} .= $txt . "\n";
146 }
147
148 # Plugins: Start
149 if ( $plugin_dir and opendir PLUGINS, $plugin_dir ) {
150   foreach my $plugin ( grep { /^\w+$/ && -f "$plugin_dir/$_"  } sort readdir(PLUGINS) ) {
151     next if ($plugin =~ /~$/);   # Ignore emacs backups
152     my($plugin_name, $off) = $plugin =~ /^\d*(\w+?)(_?)$/;
153     my $on_off = $off eq '_' ? -1 : 1;
154     require "$plugin_dir/$plugin";
155     $plugin_name->start() and ( $plugins{$plugin_name} = $on_off ) and push @plugins, $plugin_name;
156   }
157   closedir PLUGINS;
158 }
159
160 # Plugins: Template
161 # Allow for the first encountered plugin::template subroutine to override the
162 # default built-in template subroutine
163 my $tmp; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('template') and defined($tmp = $plugin->template()) and $template = $tmp and last; }
164
165 # Provide backward compatibility for Blosxom < 2.0rc1 plug-ins
166 sub load_template {
167   return &$template(@_);
168 }
169
170 # Define default entries subroutine
171 $entries =
172   sub {
173     my(%files, %indexes, %others);
174     find(
175       sub {
176         my $d; 
177         my $curr_depth = $File::Find::dir =~ tr[/][]; 
178         return if $depth and $curr_depth > $depth; 
179      
180         if ( 
181           # a match
182           $File::Find::name =~ m!^$datadir/(?:(.*)/)?(.+)\.$file_extension$!
183           # not an index, .file, and is readable
184           and $2 ne 'index' and $2 !~ /^\./ and (-r $File::Find::name)
185         ) {
186
187             # to show or not to show future entries
188             ( 
189               $show_future_entries
190               or stat($File::Find::name)->mtime < time 
191             )
192
193               # add the file and its associated mtime to the list of files
194               and $files{$File::Find::name} = stat($File::Find::name)->mtime
195
196                 # static rendering bits
197                 and (
198                   param('-all') 
199                   or !-f "$static_dir/$1/index." . $static_flavours[0]
200                   or stat("$static_dir/$1/index." . $static_flavours[0])->mtime < stat($File::Find::name)->mtime
201                 )
202                   and $indexes{$1} = 1
203                     and $d = join('/', (nice_date($files{$File::Find::name}))[5,2,3])
204   
205                       and $indexes{$d} = $d
206                         and $static_entries and $indexes{ ($1 ? "$1/" : '') . "$2.$file_extension" } = 1
207
208             } 
209             else {
210               !-d $File::Find::name and -r $File::Find::name and $others{$File::Find::name} = stat($File::Find::name)->mtime
211             }
212       }, $datadir
213     );
214
215     return (\%files, \%indexes, \%others);
216   };
217
218 # Plugins: Entries
219 # Allow for the first encountered plugin::entries subroutine to override the
220 # default built-in entries subroutine
221 my $tmp; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('entries') and defined($tmp = $plugin->entries()) and $entries = $tmp and last; }
222
223 my ($files, $indexes, $others) = &$entries();
224 %indexes = %$indexes;
225
226 # Static
227 if (!$ENV{GATEWAY_INTERFACE} and param('-password') and $static_password and param('-password') eq $static_password) {
228
229   param('-quiet') or print "Blosxom is generating static index pages...\n";
230
231   # Home Page and Directory Indexes
232   my %done;
233   foreach my $path ( sort keys %indexes) {
234     my $p = '';
235     foreach ( ('', split /\//, $path) ) {
236       $p .= "/$_";
237       $p =~ s!^/!!;
238       $done{$p}++ and next;
239       (-d "$static_dir/$p" or $p =~ /\.$file_extension$/) or mkdir "$static_dir/$p", 0755;
240       foreach $flavour ( @static_flavours ) {
241         my $content_type = (&$template($p,'content_type',$flavour));
242         $content_type =~ s!\n.*!!s;
243         my $fn = $p =~ m!^(.+)\.$file_extension$! ? $1 : "$p/index";
244         param('-quiet') or print "$fn.$flavour\n";
245         my $fh_w = new FileHandle "> $static_dir/$fn.$flavour" or die "Couldn't open $static_dir/$p for writing: $!";  
246         $output = '';
247         if ($indexes{$path} == 1) {
248           # category
249           $path_info = $p;
250           # individual story
251           $path_info =~ s!\.$file_extension$!\.$flavour!;
252           print $fh_w &generate('static', $path_info, '', $flavour, $content_type);
253         } else {
254           # date
255           local ($path_info_yr,$path_info_mo,$path_info_da, $path_info) = 
256               split /\//, $p, 4;
257           unless (defined $path_info) {$path_info = ""};
258           print $fh_w &generate('static', '', $p, $flavour, $content_type);
259         }
260         $fh_w->close;
261       }
262     }
263   }
264 }
265
266 # Dynamic
267 else {
268   my $content_type = (&$template($path_info,'content_type',$flavour));
269   $content_type =~ s!\n.*!!s;
270
271   $header = {-type=>$content_type};
272
273   print generate('dynamic', $path_info, "$path_info_yr/$path_info_mo_num/$path_info_da", $flavour, $content_type);
274 }
275
276 # Plugins: End
277 foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('end') and $entries = $plugin->end() }
278
279 # Generate 
280 sub generate {
281   my($static_or_dynamic, $currentdir, $date, $flavour, $content_type) = @_;
282
283   %files = %$files; %others = ref $others ? %$others : ();
284
285   # Plugins: Filter
286   foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('filter') and $entries = $plugin->filter(\%files, \%others) }
287
288   my %f = %files;
289
290   # Plugins: Skip
291   # Allow plugins to decide if we can cut short story generation
292   my $skip; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('skip') and defined($tmp = $plugin->skip()) and $skip = $tmp and last; }
293   
294   # Define default interpolation subroutine
295   $interpolate = 
296     sub {
297       package blosxom;
298       my $template = shift;
299       $template =~ 
300         s/(\$\w+(?:::)?\w*)/"defined $1 ? $1 : ''"/gee;
301       return $template;
302     };  
303
304   unless (defined($skip) and $skip) {
305
306     # Plugins: Interpolate
307     # Allow for the first encountered plugin::interpolate subroutine to 
308     # override the default built-in interpolate subroutine
309     my $tmp; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('interpolate') and defined($tmp = $plugin->interpolate()) and $interpolate = $tmp and last; }
310         
311     # Head
312     my $head = (&$template($currentdir,'head',$flavour));
313   
314     # Plugins: Head
315     foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('head') and $entries = $plugin->head($currentdir, \$head) }
316   
317     $head = &$interpolate($head);
318   
319     $output .= $head;
320     
321     # Stories
322     my $curdate = '';
323     my $ne = $num_entries;
324
325     if ( $currentdir =~ /(.*?)([^\/]+)\.(.+)$/ and $2 ne 'index' ) {
326       $currentdir = "$1$2.$file_extension";
327       $files{"$datadir/$1$2.$file_extension"} and %f = ( "$datadir/$1$2.$file_extension" => $files{"$datadir/$1$2.$file_extension"} );
328     } 
329     else { 
330       $currentdir =~ s!/index\..+$!!;
331     }
332
333     # Define a default sort subroutine
334     my $sort = sub {
335       my($files_ref) = @_;
336       return sort { $files_ref->{$b} <=> $files_ref->{$a} } keys %$files_ref;
337     };
338   
339     # Plugins: Sort
340     # Allow for the first encountered plugin::sort subroutine to override the
341     # default built-in sort subroutine
342     my $tmp; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('sort') and defined($tmp = $plugin->sort()) and $sort = $tmp and last; }
343   
344     foreach my $path_file ( &$sort(\%f, \%others) ) {
345       last if $ne <= 0 && $date !~ /\d/;
346       use vars qw/ $path $fn /;
347       ($path,$fn) = $path_file =~ m!^$datadir/(?:(.*)/)?(.*)\.$file_extension!;
348   
349       # Only stories in the right hierarchy
350       $path =~ /^$currentdir/ or $path_file eq "$datadir/$currentdir" or next;
351   
352       # Prepend a slash for use in templates only if a path exists
353       $path &&= "/$path";
354
355       # Date fiddling for by-{year,month,day} archive views
356       use vars qw/ $dw $mo $mo_num $da $ti $yr $hr $min $hr12 $ampm /;
357       ($dw,$mo,$mo_num,$da,$ti,$yr) = nice_date($files{"$path_file"});
358       ($hr,$min) = split /:/, $ti;
359       ($hr12, $ampm) = $hr >= 12 ? ($hr - 12,'pm') : ($hr, 'am'); 
360       $hr12 =~ s/^0//; $hr12 == 0 and $hr12 = 12;
361   
362       # Only stories from the right date
363       my($path_info_yr,$path_info_mo_num, $path_info_da) = split /\//, $date;
364       next if $path_info_yr && $yr != $path_info_yr; last if $path_info_yr && $yr < $path_info_yr; 
365       next if $path_info_mo_num && $mo ne $num2month[$path_info_mo_num];
366       next if $path_info_da && $da != $path_info_da; last if $path_info_da && $da < $path_info_da; 
367   
368       # Date 
369       my $date = (&$template($path,'date',$flavour));
370       
371       # Plugins: Date
372       foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('date') and $entries = $plugin->date($currentdir, \$date, $files{$path_file}, $dw,$mo,$mo_num,$da,$ti,$yr) }
373   
374       $date = &$interpolate($date);
375   
376       $curdate ne $date and $curdate = $date and $output .= $date;
377       
378       use vars qw/ $title $body $raw /;
379       if (-f "$path_file" && $fh->open("< $path_file")) {
380         chomp($title = <$fh>);
381         chomp($body = join '', <$fh>);
382         $fh->close;
383         $raw = "$title\n$body";
384       }
385       my $story = (&$template($path,'story',$flavour));
386   
387       # Plugins: Story
388       foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('story') and $entries = $plugin->story($path, $fn, \$story, \$title, \$body) }
389       
390       if ($content_type =~ m{\bxml\b}) {
391         # Escape <, >, and &, and to produce valid RSS
392         my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  
393         my $escape_re  = join '|' => keys %escape;
394         $title =~ s/($escape_re)/$escape{$1}/g;
395         $body =~ s/($escape_re)/$escape{$1}/g;
396       }
397   
398       $story = &$interpolate($story);
399     
400       $output .= $story;
401       $fh->close;
402   
403       $ne--;
404     }
405   
406     # Foot
407     my $foot = (&$template($currentdir,'foot',$flavour));
408   
409     # Plugins: Foot
410     foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('foot') and $entries = $plugin->foot($currentdir, \$foot) }
411   
412     $foot = &$interpolate($foot);
413     $output .= $foot;
414
415     # Plugins: Last
416     foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('last') and $entries = $plugin->last() }
417
418   } # End skip
419
420   # Finally, add the header, if any and running dynamically
421   $static_or_dynamic eq 'dynamic' and $header and $output = header($header) . $output;
422   
423   $output;
424 }
425
426
427 sub nice_date {
428   my($unixtime) = @_;
429   
430   my $c_time = ctime($unixtime);
431   my($dw,$mo,$da,$ti,$yr) = ( $c_time =~ /(\w{3}) +(\w{3}) +(\d{1,2}) +(\d{2}:\d{2}):\d{2} +(\d{4})$/ );
432   $da = sprintf("%02d", $da);
433   my $mo_num = $month2num{$mo};
434   
435   return ($dw,$mo,$mo_num,$da,$ti,$yr);
436 }
437
438
439 # Default HTML and RSS template bits
440 __DATA__
441 html content_type text/html
442
443 html head <html>
444 html head     <head>
445 html head         <link rel="alternate" type="type="application/rss+xml" title="RSS" href="$url/index.rss" />
446 html head         <title>$blog_title $path_info_da $path_info_mo $path_info_yr
447 html head         </title>
448 html head     </head>
449 html head     <body>
450 html head         <center>
451 html head             <font size="+3">$blog_title</font><br />
452 html head             $path_info_da $path_info_mo $path_info_yr
453 html head         </center>
454 html head         <p />
455
456 html story        <p>
457 html story            <a name="$fn"><b>$title</b></a><br />
458 html story            $body<br />
459 html story            <br />
460 html story            posted at: $ti | path: <a href="$url$path">$path </a> | <a href="$url/$yr/$mo_num/$da#$fn">permanent link to this entry</a>
461 html story        </p>
462
463 html date         <h3>$dw, $da $mo $yr</h3>
464
465 html foot
466 html foot         <p />
467 html foot         <center>
468 html foot             <a href="http://www.blosxom.com/"><img src="http://www.blosxom.com/images/pb_blosxom.gif" border="0" /></a>
469 html foot         </center>
470 html foot     </body>
471 html foot </html>
472
473 rss content_type text/xml
474
475 rss head <?xml version="1.0"?>
476 rss head <!-- name="generator" content="blosxom/$version" -->
477 rss head <!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">
478 rss head 
479 rss head <rss version="0.91">
480 rss head   <channel>
481 rss head     <title>$blog_title $path_info_da $path_info_mo $path_info_yr</title>
482 rss head     <link>$url</link>
483 rss head     <description>$blog_description</description>
484 rss head     <language>$blog_language</language>
485
486 rss story   <item>
487 rss story     <title>$title</title>
488 rss story     <link>$url/$yr/$mo_num/$da#$fn</link>
489 rss story     <description>$body</description>
490 rss story   </item>
491
492 rss date 
493
494 rss foot   </channel>
495 rss foot </rss>
496
497 error content_type text/html
498
499 error head <html>
500 error head <body>
501 error head     <p><font color="red">Error: 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.</font>
502
503
504 error story <p><b>$title</b><br />
505 error story $body <a href="$url/$yr/$mo_num/$da#fn.$default_flavour">#</a></p>
506
507 error date <h3>$dw, $da $mo $yr</h3>
508
509 error foot     </body>
510 error foot </html>
511 __END__