gc.sh: attempt to recover from a stale gc.pid.lock file
[girocco.git] / cgi / html.cgi
blob0186fc8cf43d66f306daa449f25238a8e906566d
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 ".";
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 # /@@ifmob@@...@@end@@/ remove unless mob defined
47 # /@@ifssh@@...@@end@@/ remove unless pushurl defined
48 # /@@ifhttps@@...@@end@@/ remove unless httpspushurl defined
50 my $pathinfo = $ENV{PATH_INFO} || '';
51 my $docroot = $ENV{DOCUMENT_ROOT} || '';
52 $pathinfo =~ s,^/,,;
53 unless ($pathinfo) {
54 my $gcgi = Girocco::CGI->new('HTML Templater');
55 print "<p>Hi, this is your friendly HTML templater speaking. Pass me template name.</p>\n";
56 exit;
59 unless ($pathinfo !~ m#\./# and open(TEMPLATE, "$Girocco::Config::basedir/html/$pathinfo")) {
60 my $gcgi = Girocco::CGI->new('HTML Templater');
61 print "<p>Invalid template name.</p>\n";
62 exit;
65 if ($pathinfo =~ /\.png$/) {
66 print "Content-type: image/png\n\n";
67 print while (<TEMPLATE>);
68 exit;
71 sub get_file_hash {
72 my ($hash, $fn) = @_;
73 return '' unless $docroot && $docroot ne '/' && -d $docroot;
74 return '' if $hash ne 'md5' && $hash ne 'sha1';
75 return '' if !$fn || $fn =~ m|^[./]| || $fn =~ m|[.]/| || $fn =~ m|/[.]|;
76 return '' unless -f "$docroot/$fn";
77 open(HASHFILE, "$docroot/$fn") or return '';
78 local $/;
79 undef $/;
80 my $contents = <HASHFILE>;
81 close(HASHFILE);
82 return $hash eq 'md5' ? md5_hex($contents) : sha1_hex($contents);
85 my ($gcgi, $section, $heading);
87 my $template=join('', <TEMPLATE>);
88 close TEMPLATE;
89 $template =~ s/@\@ifmob@\@(.*?)@\@end@\@/$Girocco::Config::mob?$1:''/ges;
90 $template =~ s/@\@ifssh@\@(.*?)@\@end@\@/$Girocco::Config::pushurl?$1:''/ges;
91 $template =~ s/@\@ifhttps@\@(.*?)@\@end@\@/$Girocco::Config::httpspushurl?$1:''/ges;
93 foreach (split(/\n/, $template)) {
94 chomp;
95 if (s/^\@section=//) {
96 $section = $_;
97 next;
98 } elsif (s/^\@heading=//) {
99 $heading = $_;
100 next;
101 } elsif (s/^\@header//) {
102 $gcgi = Girocco::CGI->new($heading, $section);
103 print "<div class=\"htmlcgi\">";
104 next;
105 } else {
106 s/@@(\w+?)@@/${$Girocco::Config::{$1}}/ge;
107 s/@\@base\((\w+?)\)@@/url_base(${$Girocco::Config::{$1}})/ge;
108 s/@\@path\((\w+?)\)@@/url_path(${$Girocco::Config::{$1}})/ge;
109 s/@\@server\((\w+?)\)@@/url_server(${$Girocco::Config::{$1}})/ge;
110 s/@\@(md5|sha1)\(([^()]+?)\)@@/get_file_hash($1,$2)/ge;
111 print "$_\n";
114 print "</div>";
115 $gcgi and $gcgi->srcname("html/$pathinfo");