Add external config support via BLOSXOM_CONFIG_DIR and BLOSXOM_CONFIG_FILE env variables.
[matthijs/upstream/blosxom.git] / blosxom.cgi
index 320238814f3b13092d44714c39a44326c5abe54f..6c51d0703815fd57b572fd1a8972787c7db874d1 100755 (executable)
@@ -67,7 +67,7 @@ $static_entries = 0;
 
 # --------------------------------
 
-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 !;
+use vars qw! $version $blog_title $blog_description $blog_language $datadir $url %template $template $depth $num_entries $file_extension $default_flavour $static_or_dynamic $config_dir $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 !;
 
 use strict;
 use FileHandle;
@@ -78,6 +78,32 @@ use CGI qw/:standard :netscape/;
 
 $version = "2.0.2";
 
+# Load configuration from $ENV{BLOSXOM_CONFIG_DIR}/blosxom.conf, if it exists
+my $blosxom_config;
+if ($ENV{BLOSXOM_CONFIG_FILE} && -r $ENV{BLOSXOM_CONFIG_FILE}) {
+  $blosxom_config = $ENV{BLOSXOM_CONFIG_FILE};
+  ($config_dir = $blosxom_config) =~ s! / [^/]* $ !!x;                          
+}
+else {
+  for my $blosxom_config_dir ($ENV{BLOSXOM_CONFIG_DIR}, '/etc/blosxom', '/etc') {
+    if (-r "$blosxom_config_dir/blosxom.conf") {
+      $config_dir = $blosxom_config_dir;
+      $blosxom_config = "$blosxom_config_dir/blosxom.conf";
+      last;
+    }
+  }
+}
+# Load $blosxom_config
+if ($blosxom_config) { 
+  if (-r $blosxom_config) {
+    eval { require $blosxom_config } or
+      warn "Error reading blosxom config file '$blosxom_config'" . ($@ ? ": $@" : '');
+  }
+  else {
+    warn "Cannot find or read blosxom config file '$blosxom_config'";
+  }
+}
+
 my $fh = new FileHandle;
 
 %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');
@@ -163,13 +189,23 @@ while (<DATA>) {
 
 # Plugins: Start
 if ( $plugin_dir and opendir PLUGINS, $plugin_dir ) {
-  foreach my $plugin ( grep { /^\w+$/ && -f "$plugin_dir/$_"  } sort readdir(PLUGINS) ) {
+  unshift @INC, $plugin_dir;
+  foreach my $plugin ( grep { /^[\w:]+$/ && -f "$plugin_dir/$_"  } sort readdir(PLUGINS) ) {
     next if ($plugin =~ /~$/);   # Ignore emacs backups
-    my($plugin_name, $off) = $plugin =~ /^\d*(\w+?)(_?)$/;
+    my($plugin_name, $off) = $plugin =~ /^\d*([\w:]+?)(_?)$/;
     my $on_off = $off eq '_' ? -1 : 1;
-    require "$plugin_dir/$plugin";
+    # Allow perl module plugins
+    if ($plugin =~ m/::/ && -z "$plugin_dir/$plugin") {
+      # For Blosxom::Plugin::Foo style plugins, we need to use a string require
+      eval "require $plugin_name";
+    }
+    else {
+      eval { require $plugin };
+    }
+    $@ and warn "error finding or loading blosxom plugin $plugin_name - skipping\n" and next;
     $plugin_name->start() and ( $plugins{$plugin_name} = $on_off ) and push @plugins, $plugin_name;
   }
+  shift @INC;
   closedir PLUGINS;
 }