]> git.deb.at Git - pkg/blosxom.git/blob - blosxom.cgi
997fcc74a4a00fdc80eb0cd82265e8c64f850643
[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(-path_info => 1);
88 $url =~ s/^included:/http:/ if $ENV{SERVER_PROTOCOL} eq 'INCLUDED';
89
90 # NOTE: Since v3.12, it looks as if CGI.pm misbehaves for SSIs and
91 # always appends path_info to the url. To fix this, we always
92 # request an url with path_info, and always remove it from the end of the
93 # string.
94 my $pi_len = length $ENV{PATH_INFO};
95 my $might_be_pi = substr($url, -$pi_len);
96 substr($url, -length $ENV{PATH_INFO}) = '' if $might_be_pi eq $ENV{PATH_INFO};
97
98 $url =~ s!/$!!;
99
100 # Drop ending any / from dir settings
101 $datadir =~ s!/$!!; $plugin_dir =~ s!/$!!; $static_dir =~ s!/$!!;
102   
103 # Fix depth to take into account datadir's path
104 $depth and $depth += ($datadir =~ tr[/][]) - 1;
105
106 # Global variable to be used in head/foot.{flavour} templates
107 $path_info = '';
108
109 $static_or_dynamic = (!$ENV{GATEWAY_INTERFACE} and param('-password') and $static_password and param('-password') eq $static_password) ? 'static' : 'dynamic';
110 $static_or_dynamic eq 'dynamic' and param(-name=>'-quiet', -value=>1);
111
112 # Path Info Magic
113 # Take a gander at HTTP's PATH_INFO for optional blog name, archive yr/mo/day
114 my @path_info = split m{/}, path_info() || param('path'); 
115 shift @path_info;
116
117 while ($path_info[0] and $path_info[0] =~ /^[a-zA-Z].*$/ and $path_info[0] !~ /(.*)\.(.*)/) { $path_info .= '/' . shift @path_info; }
118
119 # Flavour specified by ?flav={flav} or index.{flav}
120 $flavour = '';
121
122 if ( $path_info[$#path_info] =~ /(.+)\.(.+)$/ ) {
123   $flavour = $2;
124   $1 ne 'index' and $path_info .= "/$1.$2";
125   pop @path_info;
126 } else {
127   $flavour = param('flav') || $default_flavour;
128 }
129
130 # Strip spurious slashes
131 $path_info =~ s!(^/*)|(/*$)!!g;
132
133 # Date fiddling
134 ($path_info_yr,$path_info_mo,$path_info_da) = @path_info;
135 $path_info_mo_num = $path_info_mo ? ( $path_info_mo =~ /\d{2}/ ? $path_info_mo : ($month2num{ucfirst(lc $path_info_mo)} || undef) ) : undef;
136
137 # Define standard template subroutine, plugin-overridable at Plugins: Template
138 $template = 
139   sub {
140     my ($path, $chunk, $flavour) = @_;
141
142     do {
143       return join '', <$fh> if $fh->open("< $datadir/$path/$chunk.$flavour");
144     } while ($path =~ s/(\/*[^\/]*)$// and $1);
145
146     return join '', ($template{$flavour}{$chunk} || $template{error}{$chunk} || '');
147   };
148 # Bring in the templates
149 %template = ();
150 while (<DATA>) {
151   last if /^(__END__)$/;
152   my($ct, $comp, $txt) = /^(\S+)\s(\S+)\s(.*)$/ or next;
153   $txt =~ s/\\n/\n/mg;
154   $template{$ct}{$comp} .= $txt . "\n";
155 }
156
157 # Plugins: Start
158 if ( $plugin_dir and opendir PLUGINS, $plugin_dir ) {
159   foreach my $plugin ( grep { /^\w+$/ && -f "$plugin_dir/$_"  } sort readdir(PLUGINS) ) {
160     next if ($plugin =~ /~$/);   # Ignore emacs backups
161     my($plugin_name, $off) = $plugin =~ /^\d*(\w+?)(_?)$/;
162     my $on_off = $off eq '_' ? -1 : 1;
163     require "$plugin_dir/$plugin";
164     $plugin_name->start() and ( $plugins{$plugin_name} = $on_off ) and push @plugins, $plugin_name;
165   }
166   closedir PLUGINS;
167 }
168
169 # Plugins: Template
170 # Allow for the first encountered plugin::template subroutine to override the
171 # default built-in template subroutine
172 my $tmp; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('template') and defined($tmp = $plugin->template()) and $template = $tmp and last; }
173
174 # Provide backward compatibility for Blosxom < 2.0rc1 plug-ins
175 sub load_template {
176   return &$template(@_);
177 }
178
179 # Define default entries subroutine
180 $entries =
181   sub {
182     my(%files, %indexes, %others);
183     find(
184       sub {
185         my $d; 
186         my $curr_depth = $File::Find::dir =~ tr[/][]; 
187         return if $depth and $curr_depth > $depth; 
188      
189         if ( 
190           # a match
191           $File::Find::name =~ m!^$datadir/(?:(.*)/)?(.+)\.$file_extension$!
192           # not an index, .file, and is readable
193           and $2 ne 'index' and $2 !~ /^\./ and (-r $File::Find::name)
194         ) {
195
196             # to show or not to show future entries
197             ( 
198               $show_future_entries
199               or stat($File::Find::name)->mtime < time 
200             )
201
202               # add the file and its associated mtime to the list of files
203               and $files{$File::Find::name} = stat($File::Find::name)->mtime
204
205                 # static rendering bits
206                 and (
207                   param('-all') 
208                   or !-f "$static_dir/$1/index." . $static_flavours[0]
209                   or stat("$static_dir/$1/index." . $static_flavours[0])->mtime < stat($File::Find::name)->mtime
210                 )
211                   and $indexes{$1} = 1
212                     and $d = join('/', (nice_date($files{$File::Find::name}))[5,2,3])
213   
214                       and $indexes{$d} = $d
215                         and $static_entries and $indexes{ ($1 ? "$1/" : '') . "$2.$file_extension" } = 1
216
217             } 
218             else {
219               !-d $File::Find::name and -r $File::Find::name and $others{$File::Find::name} = stat($File::Find::name)->mtime
220             }
221       }, $datadir
222     );
223
224     return (\%files, \%indexes, \%others);
225   };
226
227 # Plugins: Entries
228 # Allow for the first encountered plugin::entries subroutine to override the
229 # default built-in entries subroutine
230 my $tmp; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('entries') and defined($tmp = $plugin->entries()) and $entries = $tmp and last; }
231
232 my ($files, $indexes, $others) = &$entries();
233 %indexes = %$indexes;
234
235 # Static
236 if (!$ENV{GATEWAY_INTERFACE} and param('-password') and $static_password and param('-password') eq $static_password) {
237
238   param('-quiet') or print "Blosxom is generating static index pages...\n";
239
240   # Home Page and Directory Indexes
241   my %done;
242   foreach my $path ( sort keys %indexes) {
243     my $p = '';
244     foreach ( ('', split /\//, $path) ) {
245       $p .= "/$_";
246       $p =~ s!^/!!;
247       $done{$p}++ and next;
248       (-d "$static_dir/$p" or $p =~ /\.$file_extension$/) or mkdir "$static_dir/$p", 0755;
249       foreach $flavour ( @static_flavours ) {
250         my $content_type = (&$template($p,'content_type',$flavour));
251         $content_type =~ s!\n.*!!s;
252         my $fn = $p =~ m!^(.+)\.$file_extension$! ? $1 : "$p/index";
253         param('-quiet') or print "$fn.$flavour\n";
254         my $fh_w = new FileHandle "> $static_dir/$fn.$flavour" or die "Couldn't open $static_dir/$p for writing: $!";  
255         $output = '';
256         if ($indexes{$path} == 1) {
257           # category
258           $path_info = $p;
259           # individual story
260           $path_info =~ s!\.$file_extension$!\.$flavour!;
261           print $fh_w &generate('static', $path_info, '', $flavour, $content_type);
262         } else {
263           # date
264           local ($path_info_yr,$path_info_mo,$path_info_da, $path_info) = 
265               split /\//, $p, 4;
266           unless (defined $path_info) {$path_info = ""};
267           print $fh_w &generate('static', '', $p, $flavour, $content_type);
268         }
269         $fh_w->close;
270       }
271     }
272   }
273 }
274
275 # Dynamic
276 else {
277   my $content_type = (&$template($path_info,'content_type',$flavour));
278   $content_type =~ s!\n.*!!s;
279
280   $header = {-type=>$content_type};
281
282   print generate('dynamic', $path_info, "$path_info_yr/$path_info_mo_num/$path_info_da", $flavour, $content_type);
283 }
284
285 # Plugins: End
286 foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('end') and $entries = $plugin->end() }
287
288 # Generate 
289 sub generate {
290   my($static_or_dynamic, $currentdir, $date, $flavour, $content_type) = @_;
291
292   %files = %$files; %others = ref $others ? %$others : ();
293
294   # Plugins: Filter
295   foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('filter') and $entries = $plugin->filter(\%files, \%others) }
296
297   my %f = %files;
298
299   # Plugins: Skip
300   # Allow plugins to decide if we can cut short story generation
301   my $skip; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('skip') and defined($tmp = $plugin->skip()) and $skip = $tmp and last; }
302   
303   # Define default interpolation subroutine
304   $interpolate = 
305     sub {
306       package blosxom;
307       my $template = shift;
308       $template =~ 
309         s/(\$\w+(?:::)?\w*)/"defined $1 ? $1 : ''"/gee;
310       return $template;
311     };  
312
313   unless (defined($skip) and $skip) {
314
315     # Plugins: Interpolate
316     # Allow for the first encountered plugin::interpolate subroutine to 
317     # override the default built-in interpolate subroutine
318     my $tmp; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('interpolate') and defined($tmp = $plugin->interpolate()) and $interpolate = $tmp and last; }
319         
320     # Head
321     my $head = (&$template($currentdir,'head',$flavour));
322   
323     # Plugins: Head
324     foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('head') and $entries = $plugin->head($currentdir, \$head) }
325   
326     $head = &$interpolate($head);
327   
328     $output .= $head;
329     
330     # Stories
331     my $curdate = '';
332     my $ne = $num_entries;
333
334     if ( $currentdir =~ /(.*?)([^\/]+)\.(.+)$/ and $2 ne 'index' ) {
335       $currentdir = "$1$2.$file_extension";
336       $files{"$datadir/$1$2.$file_extension"} and %f = ( "$datadir/$1$2.$file_extension" => $files{"$datadir/$1$2.$file_extension"} );
337     } 
338     else { 
339       $currentdir =~ s!/index\..+$!!;
340     }
341
342     # Define a default sort subroutine
343     my $sort = sub {
344       my($files_ref) = @_;
345       return sort { $files_ref->{$b} <=> $files_ref->{$a} } keys %$files_ref;
346     };
347   
348     # Plugins: Sort
349     # Allow for the first encountered plugin::sort subroutine to override the
350     # default built-in sort subroutine
351     my $tmp; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('sort') and defined($tmp = $plugin->sort()) and $sort = $tmp and last; }
352   
353     foreach my $path_file ( &$sort(\%f, \%others) ) {
354       last if $ne <= 0 && $date !~ /\d/;
355       use vars qw/ $path $fn /;
356       ($path,$fn) = $path_file =~ m!^$datadir/(?:(.*)/)?(.*)\.$file_extension!;
357   
358       # Only stories in the right hierarchy
359       $path =~ /^$currentdir/ or $path_file eq "$datadir/$currentdir" or next;
360   
361       # Prepend a slash for use in templates only if a path exists
362       $path &&= "/$path";
363
364       # Date fiddling for by-{year,month,day} archive views
365       use vars qw/ $dw $mo $mo_num $da $ti $yr $hr $min $hr12 $ampm /;
366       ($dw,$mo,$mo_num,$da,$ti,$yr) = nice_date($files{"$path_file"});
367       ($hr,$min) = split /:/, $ti;
368       ($hr12, $ampm) = $hr >= 12 ? ($hr - 12,'pm') : ($hr, 'am'); 
369       $hr12 =~ s/^0//; $hr12 == 0 and $hr12 = 12;
370   
371       # Only stories from the right date
372       my($path_info_yr,$path_info_mo_num, $path_info_da) = split /\//, $date;
373       next if $path_info_yr && $yr != $path_info_yr; last if $path_info_yr && $yr < $path_info_yr; 
374       next if $path_info_mo_num && $mo ne $num2month[$path_info_mo_num];
375       next if $path_info_da && $da != $path_info_da; last if $path_info_da && $da < $path_info_da; 
376   
377       # Date 
378       my $date = (&$template($path,'date',$flavour));
379       
380       # Plugins: Date
381       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) }
382   
383       $date = &$interpolate($date);
384   
385       $curdate ne $date and $curdate = $date and $output .= $date;
386       
387       use vars qw/ $title $body $raw /;
388       if (-f "$path_file" && $fh->open("< $path_file")) {
389         chomp($title = <$fh>);
390         chomp($body = join '', <$fh>);
391         $fh->close;
392         $raw = "$title\n$body";
393       }
394       my $story = (&$template($path,'story',$flavour));
395   
396       # Plugins: Story
397       foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('story') and $entries = $plugin->story($path, $fn, \$story, \$title, \$body) }
398       
399       if ($content_type =~ m{\bxml\b}) {
400         # Escape <, >, and &, and to produce valid RSS
401         my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  
402         my $escape_re  = join '|' => keys %escape;
403         $title =~ s/($escape_re)/$escape{$1}/g;
404         $body =~ s/($escape_re)/$escape{$1}/g;
405       }
406   
407       $story = &$interpolate($story);
408     
409       $output .= $story;
410       $fh->close;
411   
412       $ne--;
413     }
414   
415     # Foot
416     my $foot = (&$template($currentdir,'foot',$flavour));
417   
418     # Plugins: Foot
419     foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('foot') and $entries = $plugin->foot($currentdir, \$foot) }
420   
421     $foot = &$interpolate($foot);
422     $output .= $foot;
423
424     # Plugins: Last
425     foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('last') and $entries = $plugin->last() }
426
427   } # End skip
428
429   # Finally, add the header, if any and running dynamically
430   $static_or_dynamic eq 'dynamic' and $header and $output = header($header) . $output;
431   
432   $output;
433 }
434
435
436 sub nice_date {
437   my($unixtime) = @_;
438   
439   my $c_time = ctime($unixtime);
440   my($dw,$mo,$da,$ti,$yr) = ( $c_time =~ /(\w{3}) +(\w{3}) +(\d{1,2}) +(\d{2}:\d{2}):\d{2} +(\d{4})$/ );
441   $da = sprintf("%02d", $da);
442   my $mo_num = $month2num{$mo};
443   
444   return ($dw,$mo,$mo_num,$da,$ti,$yr);
445 }
446
447
448 # Default HTML and RSS template bits
449 __DATA__
450 html content_type text/html
451
452 html head <html>
453 html head     <head>
454 html head         <link rel="alternate" type="type="application/rss+xml" title="RSS" href="$url/index.rss" />
455 html head         <title>$blog_title $path_info_da $path_info_mo $path_info_yr
456 html head         </title>
457 html head     </head>
458 html head     <body>
459 html head         <center>
460 html head             <font size="+3">$blog_title</font><br />
461 html head             $path_info_da $path_info_mo $path_info_yr
462 html head         </center>
463 html head         <p />
464
465 html story        <p>
466 html story            <a name="$fn"><b>$title</b></a><br />
467 html story            $body<br />
468 html story            <br />
469 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>
470 html story        </p>
471
472 html date         <h3>$dw, $da $mo $yr</h3>
473
474 html foot
475 html foot         <p />
476 html foot         <center>
477 html foot             <a href="http://www.blosxom.com/"><img src="http://www.blosxom.com/images/pb_blosxom.gif" border="0" /></a>
478 html foot         </center>
479 html foot     </body>
480 html foot </html>
481
482 rss content_type text/xml
483
484 rss head <?xml version="1.0"?>
485 rss head <!-- name="generator" content="blosxom/$version" -->
486 rss head <!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">
487 rss head 
488 rss head <rss version="0.91">
489 rss head   <channel>
490 rss head     <title>$blog_title $path_info_da $path_info_mo $path_info_yr</title>
491 rss head     <link>$url</link>
492 rss head     <description>$blog_description</description>
493 rss head     <language>$blog_language</language>
494
495 rss story   <item>
496 rss story     <title>$title</title>
497 rss story     <link>$url/$yr/$mo_num/$da#$fn</link>
498 rss story     <description>$body</description>
499 rss story   </item>
500
501 rss date 
502
503 rss foot   </channel>
504 rss foot </rss>
505
506 error content_type text/html
507
508 error head <html>
509 error head <body>
510 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>
511
512
513 error story <p><b>$title</b><br />
514 error story $body <a href="$url/$yr/$mo_num/$da#fn.$default_flavour">#</a></p>
515
516 error date <h3>$dw, $da $mo $yr</h3>
517
518 error foot     </body>
519 error foot </html>
520 __END__