acme: sed off more pedantically
[girocco/readme.git] / cgi / html.cgi
blob7e9e8f0276e043722995c090a1f43119cda83876
1 #!/usr/bin/perl
2 # (c) Petr Baudis <pasky@suse.cz>
3 # Portions Copyright (c) Kyle J. McKay <mackyle@gmail.com>
4 # GPLv2
6 use strict;
7 use warnings;
9 use Digest::MD5 qw(md5_hex);
11 use lib "__BASEDIR__";
12 use Girocco::CGI;
13 use Girocco::Config;
14 use Girocco::Util;
16 BEGIN {
17 eval {
18 require Digest::SHA;
19 Digest::SHA->import(
20 qw(sha1_hex)
21 );1} ||
22 eval {
23 require Digest::SHA1;
24 Digest::SHA1->import(
25 qw(sha1_hex)
26 );1} ||
27 eval {
28 require Digest::SHA::PurePerl;
29 Digest::SHA::PurePerl->import(
30 qw(sha1_hex)
31 );1} ||
32 die "One of Digest::SHA or Digest::SHA1 or Digest::SHA::PurePerl "
33 . "must be available\n";
36 # Ultra-trivial templating engine;
37 # /^@section=SECTION
38 # /^@heading=HEADING
39 # /^@header produces HTML header based on @section and @heading
40 # /@@gitweburl@@/ substitute for gitweburl configuration variable
41 # /@@base(gitweburl)@@/ substitute for base portion of gitweburl variable
42 # /@@path(gitweburl)@@/ substitute for path portion of gitweburl variable
43 # /@@server(gitweburl)@@/ replace scheme://host:port portion of gitweburl variable
44 # /@@md5(relpath)@@/ produces MD5 hex of file in DOCUMENT_ROOT at relpath
45 # /@@sha1(relpath)@@/ produces SHA1 hex of file in DOCUMENT_ROOT at relpath
46 # /@@blob(relpath)@@/ produces git hash-object -t blob of file in DOCUMENT_ROOT
47 # /@@ifmob@@...@@end@@/ remove unless mob defined
48 # /@@ifssh@@...@@end@@/ remove unless pushurl defined
49 # /@@ifhttps@@...@@end@@/ remove unless httpspushurl defined
50 # /@@ifcustom@@...@@end@@/ remove if pretrustedroot defined (i.e. not custom root)
51 # /@@ctr()@@/ replace with 1-based up-counting value
53 my $pathinfo = $ENV{PATH_INFO} || '';
54 my $docroot = $ENV{DOCUMENT_ROOT} || '';
55 $pathinfo =~ s,^/,,;
56 unless ($pathinfo) {
57 my $gcgi = Girocco::CGI->new('HTML Templater');
58 print "<p>Hi, this is your friendly HTML templater speaking. Pass me template name.</p>\n";
59 exit;
62 my $templatefd;
63 unless ($pathinfo !~ m#\./# and open($templatefd, '<', "$Girocco::Config::basedir/html/$pathinfo")) {
64 my $gcgi = Girocco::CGI->new('HTML Templater');
65 print "<p>Invalid template name.</p>\n";
66 exit;
69 if ($pathinfo =~ /\.(png|jpe?g|gif|svgz?)$/) {
70 my $kind = $1;
71 $kind = 'jpeg' if $kind eq 'jpg';
72 $kind = 'svg+xml' if $kind =~ /^svg/;
73 print "Content-type: image/$kind\n\n";
74 my $buf;
75 while (read($templatefd, $buf, 32768)) {
76 print $buf;
78 exit;
81 sub get_file_hash {
82 my ($hash, $fn) = @_;
83 return '' unless $docroot && $docroot ne '/' && -d $docroot;
84 return '' if $hash ne 'md5' && $hash ne 'sha1' && $hash ne 'blob';
85 return '' if !$fn || $fn =~ m|^[./]| || $fn =~ m|[.]/| || $fn =~ m|/[.]|;
86 return '' unless -f "$docroot/$fn";
87 open(HASHFILE, '<', "$docroot/$fn") or return '';
88 local $/;
89 undef $/;
90 my $contents = <HASHFILE>;
91 close(HASHFILE);
92 $hash eq 'blob' and $contents = sprintf("blob %u\0", length($contents)) . $contents;
93 return $hash eq 'md5' ? md5_hex($contents) : sha1_hex($contents);
96 my ($gcgi, $section, $heading);
98 my $template=join('', map(to_utf8($_, 1), <$templatefd>));
99 close $templatefd;
100 $template =~ s/@\@ifmob@\@(.*?)@\@end@\@/$Girocco::Config::mob?$1:''/ges;
101 $template =~ s/@\@ifssh@\@(.*?)@\@end@\@/$Girocco::Config::pushurl?$1:''/ges;
102 $template =~ s/@\@ifcustom@\@(.*?)@\@end@\@/!$Girocco::Config::pretrustedroot?$1:''/ges;
103 $template =~ s/@\@ifhttps@\@(.*?)@\@end@\@/$Girocco::Config::httpspushurl?$1:''/ges;
105 my $counter = 0;
107 foreach (split(/\n/, $template)) {
108 chomp;
109 if (s/^\@section=//) {
110 $section = $_;
111 next;
112 } elsif (s/^\@heading=//) {
113 $heading = $_;
114 next;
115 } elsif (s/^\@header//) {
116 $gcgi = Girocco::CGI->new($heading, $section);
117 print "<div class=\"htmlcgi\">";
118 next;
119 } else {
120 s/@@(\w+?)@@/${$Girocco::Config::{$1}}/ge;
121 s/@\@base\((\w+?)\)@@/url_base(${$Girocco::Config::{$1}})/ge;
122 s/@\@path\((\w+?)\)@@/url_path(${$Girocco::Config::{$1}})/ge;
123 s/@\@server\((\w+?)\)@@/url_server(${$Girocco::Config::{$1}})/ge;
124 s/@\@(md5|sha1|blob)\(([^()]+?)\)@@/get_file_hash($1,$2)/ge;
125 s/@\@ctr\(\)@@/++$counter/ge;
126 print "$_\n";
129 print "</div>";
130 $gcgi and $gcgi->srcname("html/$pathinfo");