v3.00 release
[perl/plp/.git] / plp.cgi
diff --git a/plp.cgi b/plp.cgi
index 2158b603ceae7a085721ab105e4255bc3a5ccbb5..6dbcee9f01abdd30336a0738f27d2e86b5d91c6b 100755 (executable)
--- a/plp.cgi
+++ b/plp.cgi
 #!/usr/local/bin/perl
+use v5.6.0;
+use PLP;
 
-use vars qw(%INTERNAL %get %post %fields %header %cookie %BLOCK $DEBUG $output);
-use strict;
+die 'Wrong module version' if $PLP::VERSION ne '3.00';
 
-sub SendHeaders(){
-    $INTERNAL{sentheaders} = 1;
-    print STDOUT "Content-Type: text/plain\n\n" if $DEBUG & 2;
-    print STDOUT map("$_: $header{$_}\n", keys %header), "\n";
-};
+use vars qw($DEBUG);
 
-sub rawprint(@){
-    print STDOUT (@_);
+use strict;
+{
+    $PLP::code = '';
+    $PLP::sentheaders = 0;
+    $PLP::inA = 0;
+    $PLP::inB = 0;
 }
 
-
-$ENV{PLP_VERSION} = '2.40';
 $DEBUG = 1;
+our $mod_perl = exists $ENV{MOD_PERL};
 
-# We put most everything in %INTERNAL, just so the user won't screw it.
-# We could also have used packages, but let's keep it simple.
-
-$INTERNAL{file} = $ENV{PATH_TRANSLATED};
-unless (-e $INTERNAL{file}){
-    $ENV{REDIRECT_STATUS} = '404';
-    print STDERR "PLP: Not found: $INTERNAL{file}\n";
-
-    #Change this if you have an error handling script.
-    print `/vhost/COMMON/err.cgi` || "Status: 404 Not found\n\nFile not found";
+{
+    my $file = $ENV{PATH_TRANSLATED};
+    $ENV{PLP_NAME} = $ENV{PATH_INFO};
+    my $path_info;
+    while (not -f $file) {
+        if (not $file =~ s/(\/+[^\/]*)$//) {
+           $ENV{REDIRECT_STATUS} = '404';
+           print STDERR "PLP: Not found: $file\n";
+
+           if ($mod_perl) {
+               Apache->request->uri($ENV{REQUEST_URI});
+               print STDOUT "Status: 404 Not Found";
+               Apache::exit();
+           } else {
+               print STDOUT "Status: 404 Not Found\n\nNot found: $ENV{REQUEST_URI}";
+               exit;
+           }
+       }
+       my $pi = $1;
+       $ENV{PLP_NAME} =~ s/\Q$pi\E$//;
+       $path_info = $pi . $path_info;
+    }
     
-    exit;
-}
-
-require plp;
-
-($INTERNAL{dir} = $INTERNAL{file}) =~ s{^(.*)/(.*?)$}[$1];
-$ENV{FILE_NAME} = $2;
-chdir $INTERNAL{dir};
-
-($ENV{PLP_NAME} = $ENV{REQUEST_URI}) =~ s/\?.*$//;
-
-
-$INTERNAL{qq} = "\10"; #^P
-$INTERNAL{q}  = "\17"; #^Q
-
-$INTERNAL{code} = ReadFile($INTERNAL{file});
-
-while ($INTERNAL{code} =~ /<\((.*?)\)>/ ){
-    (my $file = $1) =~ tr/[<>|//d;
-    $INTERNAL{code} =~ s//ReadFile($file)/e;
-}
-
-$INTERNAL{code} =~ s(<:)($INTERNAL{q};)g;
-$INTERNAL{code} =~ s(:>)(;\nprint q$INTERNAL{q})g;
-
-while ($INTERNAL{code} =~ /(<\[1(.*?)\]>(.*?)<\[2\]>(.*?)<\[3\]>)/s){
-    $BLOCK{"$2-1"} = $3;
-    $BLOCK{"$2-2"} = $4;
-    $INTERNAL{code} =~ s///;  #Redo last match
-}
-$INTERNAL{code} =~ s(\\\\\r?\n)()g;
-
-# This is bad and subject to removal.
-$INTERNAL{code} =~ s(<\[([^>]*?):(.*?)\]>)($BLOCK{"${1}-1"}$2$BLOCK{"${1}-2"})g;
-$INTERNAL{code} =~ s(<\[(?!/)(.*?)\]>)($BLOCK{"${1}-1"})g;
-$INTERNAL{code} =~ s(<\[/(.*?)\]>)($BLOCK{"${1}-2"})g;
+    if ($mod_perl) {
+       Apache->request->uri($ENV{REQUEST_URI});
+    }
 
-# This too is bad and subject to removal.
-$INTERNAL{code} =~ s(<{[ \08\09]*)($INTERNAL{q};print qq$INTERNAL{qq})g;
-$INTERNAL{code} =~ s([ \08\09]*}>)($INTERNAL{qq};print q$INTERNAL{q})g;
-$INTERNAL{code} = "print q$INTERNAL{q}$INTERNAL{code}$INTERNAL{q};";
+    if (not -r $file) {
+       if (exists $ENV{MOD_PERL}) {
+           print STDOUT "Status: 403 Forbidden";
+           Apache::exit();
+       } else {
+           print STDOUT "Status: 403 Forbidden\n\nForbidden: $ENV{REQUEST_URI}";
+           exit;
+       }
+    }
 
-$INTERNAL{code} =~ s{print qq$INTERNAL{qq}$INTERNAL{qq};}[]g;
-$INTERNAL{code} =~ s{print q$INTERNAL{q}$INTERNAL{q};}[]g;
+    delete @ENV{
+       qw(PATH_TRANSLATED SCRIPT_NAME SCRIPT_FILENAME PATH_INFO),
+        grep { /^REDIRECT_/ } keys %ENV
+    };
 
-tie %header, 'PLP::Headers';
-tie *PLPOUT, 'PLP::Print';
+    $ENV{PATH_INFO} = $path_info if defined $path_info;
+    $ENV{PLP_FILENAME} = $file;
+    (my $dir = $file) =~ s{/[^/]+$}[];
+    chdir $dir;
 
-# This is VERY bad, and will probably be removed. Use <: BEGIN { ... } 
-# :> instead
-while ($INTERNAL{code} =~ s/<_(.*?)_>//s){
-    $INTERNAL{pre} = $1;    
-    {
-       no strict;
-       eval $INTERNAL{pre};
-       if ($@ && $DEBUG & 1){
-           print "\nDebug:\n $@";
-       }
-    }
+    $PLP::code = PLP::source($file, 0);
+    tie *PLPOUT, 'PLP::Tie::Print';
+    select PLPOUT;
 }
 
-#$INTERNAL{headers}->();
-select PLPOUT;
 {
     no strict;
-    eval $INTERNAL{code};
-    SendHeaders() unless $INTERNAL{sentheaders};
+    PLP::Fields::doit();
+    {
+       package PLP::Script;
+       *headers = \%header;
+       *cookies = \%cookie;
+       PLP::Functions->import();
+       eval qq{package PLP::Script; $PLP::code};
+    }
+    select STDOUT;
+    undef *{"PLP::Script::$_"} for keys %PLP::Script::;
+    PLP::SendHeaders() unless $PLP::sentheaders;
     if ($@ && $DEBUG & 1){
-       print "<hr><b>Debug</b><br>", Entity($@);
+       print $header{'Content-Type'} =~ m!^text/html!i
+             ? ("<hr><b>Debug</b><br>", Entity($@))
+             : ("[Debug]\n", $@);
     }
 }
+