v2.40 release
[perl/plp/.git] / plp.cgi
1 #!/usr/local/bin/perl
2
3 use vars qw(%INTERNAL %get %post %fields %header %cookie %BLOCK $DEBUG $output);
4 use strict;
5
6 sub SendHeaders(){
7     $INTERNAL{sentheaders} = 1;
8     print STDOUT "Content-Type: text/plain\n\n" if $DEBUG & 2;
9     print STDOUT map("$_: $header{$_}\n", keys %header), "\n";
10 };
11
12 sub rawprint(@){
13     print STDOUT (@_);
14 }
15
16
17 $ENV{PLP_VERSION} = '2.40';
18 $DEBUG = 1;
19
20 # We put most everything in %INTERNAL, just so the user won't screw it.
21 # We could also have used packages, but let's keep it simple.
22
23 $INTERNAL{file} = $ENV{PATH_TRANSLATED};
24 unless (-e $INTERNAL{file}){
25     $ENV{REDIRECT_STATUS} = '404';
26     print STDERR "PLP: Not found: $INTERNAL{file}\n";
27
28     #Change this if you have an error handling script.
29     print `/vhost/COMMON/err.cgi` || "Status: 404 Not found\n\nFile not found";
30     
31     exit;
32 }
33
34 require plp;
35
36 ($INTERNAL{dir} = $INTERNAL{file}) =~ s{^(.*)/(.*?)$}[$1];
37 $ENV{FILE_NAME} = $2;
38 chdir $INTERNAL{dir};
39
40 ($ENV{PLP_NAME} = $ENV{REQUEST_URI}) =~ s/\?.*$//;
41
42
43 $INTERNAL{qq} = "\10"; #^P
44 $INTERNAL{q}  = "\17"; #^Q
45
46 $INTERNAL{code} = ReadFile($INTERNAL{file});
47
48 while ($INTERNAL{code} =~ /<\((.*?)\)>/ ){
49     (my $file = $1) =~ tr/[<>|//d;
50     $INTERNAL{code} =~ s//ReadFile($file)/e;
51 }
52
53 $INTERNAL{code} =~ s(<:)($INTERNAL{q};)g;
54 $INTERNAL{code} =~ s(:>)(;\nprint q$INTERNAL{q})g;
55
56 while ($INTERNAL{code} =~ /(<\[1(.*?)\]>(.*?)<\[2\]>(.*?)<\[3\]>)/s){
57     $BLOCK{"$2-1"} = $3;
58     $BLOCK{"$2-2"} = $4;
59     $INTERNAL{code} =~ s///;  #Redo last match
60 }
61 $INTERNAL{code} =~ s(\\\\\r?\n)()g;
62
63 # This is bad and subject to removal.
64 $INTERNAL{code} =~ s(<\[([^>]*?):(.*?)\]>)($BLOCK{"${1}-1"}$2$BLOCK{"${1}-2"})g;
65 $INTERNAL{code} =~ s(<\[(?!/)(.*?)\]>)($BLOCK{"${1}-1"})g;
66 $INTERNAL{code} =~ s(<\[/(.*?)\]>)($BLOCK{"${1}-2"})g;
67
68 # This too is bad and subject to removal.
69 $INTERNAL{code} =~ s(<{[ \08\09]*)($INTERNAL{q};print qq$INTERNAL{qq})g;
70 $INTERNAL{code} =~ s([ \08\09]*}>)($INTERNAL{qq};print q$INTERNAL{q})g;
71 $INTERNAL{code} = "print q$INTERNAL{q}$INTERNAL{code}$INTERNAL{q};";
72
73 $INTERNAL{code} =~ s{print qq$INTERNAL{qq}$INTERNAL{qq};}[]g;
74 $INTERNAL{code} =~ s{print q$INTERNAL{q}$INTERNAL{q};}[]g;
75
76 tie %header, 'PLP::Headers';
77 tie *PLPOUT, 'PLP::Print';
78
79 # This is VERY bad, and will probably be removed. Use <: BEGIN { ... } 
80 # :> instead
81 while ($INTERNAL{code} =~ s/<_(.*?)_>//s){
82     $INTERNAL{pre} = $1;    
83     {
84         no strict;
85         eval $INTERNAL{pre};
86         if ($@ && $DEBUG & 1){
87             print "\nDebug:\n $@";
88         }
89     }
90 }
91
92 #$INTERNAL{headers}->();
93 select PLPOUT;
94 {
95     no strict;
96     eval $INTERNAL{code};
97     SendHeaders() unless $INTERNAL{sentheaders};
98     if ($@ && $DEBUG & 1){
99         print "<hr><b>Debug</b><br>", Entity($@);
100     }
101 }