2 # (c) Petr Baudis <pasky@suse.cz>
3 # Portions Copyright (c) Kyle J. McKay <mackyle@gmail.com>
9 use Digest
::MD5
qw(md5_hex);
28 require Digest
::SHA
::PurePerl
;
29 Digest
::SHA
::PurePerl
->import(
32 die "One of Digest::SHA or Digest::SHA1 or Digest::SHA::PurePerl "
33 . "must be available\n";
36 # Ultra-trivial templating engine;
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
51 my $pathinfo = $ENV{PATH_INFO
} || '';
52 my $docroot = $ENV{DOCUMENT_ROOT
} || '';
55 my $gcgi = Girocco
::CGI
->new('HTML Templater');
56 print "<p>Hi, this is your friendly HTML templater speaking. Pass me template name.</p>\n";
61 unless ($pathinfo !~ m
#\./# and open($templatefd, '<', "$Girocco::Config::basedir/html/$pathinfo")) {
62 my $gcgi = Girocco
::CGI
->new('HTML Templater');
63 print "<p>Invalid template name.</p>\n";
67 if ($pathinfo =~ /\.(png|jpe?g|gif|svgz?)$/) {
69 $kind = 'jpeg' if $kind eq 'jpg';
70 $kind = 'svg+xml' if $kind =~ /^svg/;
71 print "Content-type: image/$kind\n\n";
73 while (read($templatefd, $buf, 32768)) {
81 return '' unless $docroot && $docroot ne '/' && -d
$docroot;
82 return '' if $hash ne 'md5' && $hash ne 'sha1' && $hash ne 'blob';
83 return '' if !$fn || $fn =~ m
|^[./]| || $fn =~ m|[.]/| || $fn =~ m
|/[.]|;
84 return '' unless -f
"$docroot/$fn";
85 open(HASHFILE
, '<', "$docroot/$fn") or return '';
88 my $contents = <HASHFILE
>;
90 $hash eq 'blob' and $contents = sprintf("blob %u\0", length($contents)) . $contents;
91 return $hash eq 'md5' ? md5_hex
($contents) : sha1_hex
($contents);
94 my ($gcgi, $section, $heading);
96 my $template=join('', map(to_utf8
($_, 1), <$templatefd>));
98 $template =~ s/@\@ifmob@\@(.*?)@\@end@\@/$Girocco::Config::mob?$1:''/ges;
99 $template =~ s/@\@ifssh@\@(.*?)@\@end@\@/$Girocco::Config::pushurl?$1:''/ges;
100 $template =~ s/@\@ifhttps@\@(.*?)@\@end@\@/$Girocco::Config::httpspushurl?$1:''/ges;
102 foreach (split(/\n/, $template)) {
104 if (s/^\@section=//) {
107 } elsif (s/^\@heading=//) {
110 } elsif (s/^\@header//) {
111 $gcgi = Girocco
::CGI
->new($heading, $section);
112 print "<div class=\"htmlcgi\">";
115 s/@@(\w+?)@@/${$Girocco::Config::{$1}}/ge;
116 s/@\@base\((\w+?)\)@@/url_base(${$Girocco::Config::{$1}})/ge;
117 s/@\@path\((\w+?)\)@@/url_path(${$Girocco::Config::{$1}})/ge;
118 s/@\@server\((\w+?)\)@@/url_server(${$Girocco::Config::{$1}})/ge;
119 s/@\@(md5|sha1|blob)\(([^()]+?)\)@@/get_file_hash($1,$2)/ge;
124 $gcgi and $gcgi->srcname("html/$pathinfo");