Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

SSI Emulation Library

by EvanK (Chaplain)
on Jan 13, 2001 at 10:37 UTC ( #51570=sourcecode: print w/ replies, xml ) Need Help??

Category: Web Stuff
Author/Contact Info EvanK <evank@scriptionsoft.com>
Description: An alternative to using big clumsy modules when you need to emulate ssi in perl scripts...it's been written to theoretically work on both win32 and *nix systems, though I've only gotten to test it on windows. works fine for me though. any comments and feedback welcome.
## Usage:
#
# EvalExecs($html_to_parse) <== Evaluates <!--#exec -->'s
#
# EvalIncludes($html_to_parse) <== Evaluates <!--#include -->'s
#

use Cwd;

sub EvalExecs {

my($cwd,$path,$html,$action,$mode,$file,$exec);

$cwd = getcwd();
if($^O =~ m/win/) {
    if(substr($cwd,-1,1) eq "\\") {$cwd = substr($cwd,0,(length($cwd)-
+1))}
} else {
    $cwd =~ s/\\/\//g;
    if(substr($cwd,-1,1) eq "/") {$cwd = substr($cwd,0,(length($cwd)-1
+))}
}
$path = $ENV{'DOCUMENT_ROOT'};
if($^O =~ m/win/) {
    if(substr($path,-1,1) eq "\\") {$path = substr($path,0,(length($pa
+th)-1))}
} else {
    $path =~ s/\\/\//g;
    if(substr($path,-1,1) eq "/") {$path = substr($path,0,(length($pat
+h)-1))}
}

$html = $_[0];

while($html =~ m/<!--#exec(.+)-->/) {
    $html =~ s/<__exec(.+)__>/<_INVALID_exec$1_INVALID_>/;
    $html =~ s/<!--#exec(.+)-->/<__exec$1__>/;
    $action = $1;

    if($action =~ m/cmd=/) {

        $mode = 1;
        if($action =~ m/cmd="(.+)"/) {
            $file = $1;
        }
        elsif($action =~ m/cmd='(.+)'/) {
            $file = $1;
        }

    } elsif($action =~ m/cgi=/) {

        $mode = 2;
        if($action =~ m/cgi="(.+)"/) {
            $file = $1;
        }
        elsif($action =~ m/cgi='(.+)'/) {
            $file = $1;
        }

    }

    if(!$file) {next}

    if($mode == 1) {
        $exec = `$file`;
    } else {
        $exec = `perl $file`;
    }

    $html =~ s/<__exec(.+)__>/$exec/g;
    return $html;
}
$html =~ s/<_INVALID_exec(.+)_INVALID_>/<!--#exec$1-->/g;
}

sub EvalIncludes {

my($cwd,$path,$html,$action,$mode,$file,$final,$include,@temp,@dir,@in
+clude);

$cwd = getcwd();
if($^O =~ m/win/) {
    if(substr($cwd,-1,1) eq "\\") {$cwd = substr($cwd,0,(length($cwd)-
+1))}
} else {
    $cwd =~ s/\\/\//g;
    if(substr($cwd,-1,1) eq "/") {$cwd = substr($cwd,0,(length($cwd)-1
+))}
}
$path = $ENV{'DOCUMENT_ROOT'};
if($^O =~ m/win/) {
    if(substr($path,-1,1) eq "\\") {$path = substr($path,0,(length($pa
+th)-1))}
} else {
    $path =~ s/\\/\//g;
    if(substr($path,-1,1) eq "/") {$path = substr($path,0,(length($pat
+h)-1))}
}

$html = $_[0];

while($html =~ m/<!--#include(.+)-->/) {
    $html =~ s/<__include(.+)__>/<_INVALID_include$1_INVALID_>/;
    $html =~ s/<!--#include(.+)-->/<__include$1__>/;
    $action = $1;

    if($action =~ m/file=/) {

        $mode = 1;
        if($action =~ m/file="(.+)"/) {
            $file = $1;
        }
        elsif($action =~ m/file='(.+)'/) {
            $file = $1;
        }

    } elsif($action =~ m/virtual=/) {

        $mode = 2;
        if($action =~ m/virtual="(.+)"/) {
            $file = $1;
        }
        elsif($action =~ m/virtual='(.+)'/) {
            $file = $1;
        }

    }

    if(!$file) {next}

    if($mode == 1) {
        if(-e "$cwd/$file") {$final = "$cwd/$file"}
        else {
            opendir(MAIN,"$cwd");
            @temp = readdir(MAIN);
            closedir(MAIN);

            foreach $listed (@temp) {
                if((-d "$cwd/$listed" && ($listed ne "." && $listed ne
+ "..")) && (-e "$cwd/$listed/$file")) {$final = "$cwd/$listed/$file";
+ goto LAST}
                elsif(-d "$cwd/$listed" && ($listed ne "." && $listed 
+ne "..")) {push(@dirs,$listed)}
            }

            foreach $dir (@dirs) {
                opendir(DIR,"$cwd/$dir");
                @temp = readdir(DIR);
                closedir(DIR);

                foreach $listed (@temp) {
                    if(-d "$cwd/$dir/$listed" && -e "$cwd/$dir/$listed
+/$file") {$final = "$cwd/$dir/$listed/$file"; goto LAST}
                    elsif(-d "$cwd/$dir/$listed") {push(@dirs,"$dir/$l
+isted")}
                }
            }
        }
        LAST: if(!$final) {next}
    } else {
        $final = "$path/$file";
    }

    if(@include) {
        foreach $i (0..$#include) {
            shift(@include);
        }
    }

    open(INCLUDE,"$final");
    @include = <INCLUDE>;
    close(INCLUDE);

    $last = $include[-1];
    $include = '';

    foreach $i (@include) {
        chomp($i);
        if($i eq $last) {$include .= "$i";} else {$include .= "$i\n";}
    }

    $html =~ s/<__include(.+)__>/$include/g;
    return $html;
}
$html =~ s/<_INVALID_include(.+)_INVALID_>/<!--#include$1-->/g;
}

Comment on SSI Emulation Library
Download Code
Re: SSI Emulation Library
by chipmunk (Parson) on Jan 13, 2001 at 21:06 UTC
    Three points, after a quick scan through your code:

    Using $^O =~ /win/ to detect Windows systems will fail to detect 'MSWin32' as a Windows system, and will falsely detect 'Darwin'. This was an issue for the CGI module on p5p recently; the recommended solution was $^O =~ /^MSWin/i.

    Removing a trailing slash from a path can be done more easily with a regex than with substr:

    $cwd = getcwd(); $path = $ENV{'DOCUMENT_ROOT'}; if ($^O =~ /^MSWin/i) { $cwd =~ s,\\$,,; $path =~ s,\\$,,; } else { $cwd =~ s,/$,,; $path =~ s,/$,,; }
    However, / can be used as a directory separator in Perl on Windows, and there are other operating systems, besides Unix and Windows, and they don't all use slash or backslash as the directory separator. File::Spec would be a portable way of dealing with this issue.

    Finally... Have you tested your code with HTML that contains multiple SSI directives on the same line?

      First off, thanks for pointing out the /win/ thing. And about the fore- and backslashes, I only designed it for windows and *nix, but I'll look into that...and I thought the while loop would take care of multiple directives in one line...

      ______________________________________________
      When I get a little money, I buy books. If I have any left over, I buy food and clothes. -Erasmus

Re: SSI Emulation Library
by dws (Chancellor) on Jan 14, 2001 at 11:43 UTC
    chipmonk makes a very good point about multiple SSI directives on one line. You might consider doing something like: $html =~ s/<!--#(.+?)-->/&expand_directive($1)/ge;
    For some hints on fleshing out the other SSI directives, take a look at my SSI script. You're welcome to borrow the code.

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://51570]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2014-07-13 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (249 votes), past polls