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