Changeset 7683


Ignore:
Timestamp:
2004-07-01T13:35:46+12:00 (20 years ago)
Author:
jrm21
Message:

better code for reading in the config file, so not just unix-specific.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/perllib/plugins/DBPlug.pm

    r7681 r7683  
    3434package DBPlug;
    3535
     36use strict;
     37no strict 'refs'; # allow variable as a filehandle
     38
    3639use BasPlug;
    3740use unicode;
     
    4144
    4245sub BEGIN {
    43     @ISA = ('BasPlug');
     46    @DBPlug::ISA = ('BasPlug');
    4447}
    4548
     
    138141        return 0;
    139142    }
    140     undef $/;       # don't use \n as line separator
    141     my $conf = <CONF>;  # reads in whole file
    142     $/ = "\n";      # set line separator back
     143    my $line;
     144    my $statement="";
     145    my $callback="";
     146    while (defined($line=<CONF>)) {
     147    chomp $line;
     148    $line =~ s/\s*#.*$//mg; # remove comments
     149    $statement .= $line;
     150    if ($statement =~ m/;\s*$/) { # ends with ";"
     151        # check that it is safe
     152        # assignment
     153        if ($statement =~ m~\$(\w+)\s* = \s*
     154        (\d     # digits
     155         | ".*?(?<!\\)" # " up to the next " not preceded by a \
     156         | '.*?(?<!\\)' # ' up to the next ' not preceded by a \
     157        )\s*;~x) {      # /x means ignore comments and whitespace in rx
     158        # evaluate the assignment
     159        if (!eval "\$$1=$2;") {
     160            my $err=$@;
     161            chomp $err;
     162            $err =~ s/\.$//; # remove a trailing .
     163            print $outhandle "$err (in $filename)\n";
     164            return 0; # there was an error reading the config file
     165        }
     166        } elsif ($callback) {
     167        # add this line to the callback function
     168        $callback .= $statement;
     169        } elsif ($statement =~ /sub text_callback/) {
     170        # this is the start of the callback function definition
     171        $callback = $statement;
     172        }
     173        $statement = "";
     174    } elsif ($statement =~ /\}\s*$/ && $callback) { # ends with {
     175        $callback .= $statement ; $statement = "";
     176    }
     177    }
    143178    close CONF;
    144     eval $conf;     # execute the perl commands in that file
     179    if ($callback) {
     180    # try to check that the function is "safe"
     181    if ($callback =~ /\b(?:system|open|pipe|readpipe|qx|kill|eval|do|use|require|exec|fork)\b/ ||
     182        $callback =~ /[\`]|\|\-/) {
     183        # no backticks or functions that start new processes allowed
     184        print STDERR "DBPlug: bad fuction in callback\n";
     185        return 0;
     186    }
     187    }
    145188
    146189    if (!defined($db)) {
Note: See TracChangeset for help on using the changeset viewer.