2004-11-23 23:26:30 +00:00
|
|
|
# Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
|
|
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2016-08-16 14:59:13 +02:00
|
|
|
# the Free Software Foundation; either version 3 of the License, or
|
2004-11-23 23:26:30 +00:00
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2016-08-16 14:59:13 +02:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2004-11-23 23:26:30 +00:00
|
|
|
|
2015-03-27 03:01:01 +02:00
|
|
|
use strict;
|
2015-08-18 10:48:03 +02:00
|
|
|
use v5.10;
|
2015-03-27 03:01:01 +02:00
|
|
|
|
2014-08-21 22:23:23 +02:00
|
|
|
AddModuleDescription('html-template.pl', 'HTML Templates');
|
2004-11-23 23:26:30 +00:00
|
|
|
|
2004-11-23 00:33:47 +00:00
|
|
|
# The entire mechanism of how pages are built is now upside down.
|
|
|
|
|
# Instead of writing code that assembles pages, we load templates,
|
|
|
|
|
# that refer to pieces of code.
|
|
|
|
|
#
|
|
|
|
|
# This is the beginning of PHP-in-Perl. :(
|
|
|
|
|
|
2015-04-10 13:31:28 +03:00
|
|
|
our ($q, %Action, $DataDir, $UseCache, $LastUpdate);
|
|
|
|
|
our ($HtmlTemplateDir);
|
2004-11-23 23:26:30 +00:00
|
|
|
|
|
|
|
|
$HtmlTemplateDir = "$DataDir/templates";
|
|
|
|
|
|
2015-04-11 23:41:33 +03:00
|
|
|
*BrowsePage = \&DoHtmlTemplate;
|
2004-11-23 00:33:47 +00:00
|
|
|
|
|
|
|
|
# replace all actions with DoHtmlTemplate!
|
|
|
|
|
foreach my $key (keys %Action) {
|
|
|
|
|
$Action{$key} = \&DoHtmlTemplate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub DoHtmlTemplate {
|
|
|
|
|
my ($id, $raw, $comment, $status) = @_;
|
|
|
|
|
if ($q->http('HTTP_IF_MODIFIED_SINCE')
|
|
|
|
|
and $q->http('HTTP_IF_MODIFIED_SINCE') eq gmtime($LastUpdate)
|
|
|
|
|
and GetParam('cache', $UseCache) >= 2) {
|
|
|
|
|
print $q->header(-status=>'304 NOT MODIFIED');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2004-11-23 23:26:30 +00:00
|
|
|
OpenPage($id) if $id;
|
2004-11-23 00:33:47 +00:00
|
|
|
print GetHttpHeader('text/html');
|
2004-11-23 23:26:30 +00:00
|
|
|
print GetHtmlTemplate();
|
2004-11-23 00:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
2004-11-23 22:10:16 +00:00
|
|
|
# Some subroutines from the script need a wrapper in order to return a
|
|
|
|
|
# string instead of printing directly.
|
2004-11-23 23:26:30 +00:00
|
|
|
|
2004-11-23 22:10:16 +00:00
|
|
|
sub HtmlTemplateRc {
|
2015-07-03 14:32:33 +02:00
|
|
|
my $result = ToString(sub { DoRc(\&GetRcHtml) });
|
2004-11-23 22:10:16 +00:00
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Processing instructions are processed as Perl code, and its result
|
|
|
|
|
# is substituted. Examples:
|
2004-11-23 00:33:47 +00:00
|
|
|
#
|
|
|
|
|
# <?&foo?> -- This will call the subroutine &foo. It's return value
|
|
|
|
|
# will be substituted for the processing instruction.
|
|
|
|
|
#
|
|
|
|
|
# <?$foo?> -- This substitutes the value of variable $foo.
|
|
|
|
|
#
|
|
|
|
|
# Since the processing instruction is valid XHTML, the template should
|
|
|
|
|
# be valid XHTML as well.
|
2004-11-21 19:29:52 +00:00
|
|
|
|
2004-11-23 23:26:30 +00:00
|
|
|
sub GetHtmlTemplate {
|
|
|
|
|
my $template = shift || GetActionHtmlTemplate();
|
|
|
|
|
my $html = ReadFileOrDie($template);
|
|
|
|
|
$html =~ s/<\?(.*?)\?>/HtmlTemplateEval($1)/egs;
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
2004-11-23 00:33:47 +00:00
|
|
|
|
2004-11-23 23:26:30 +00:00
|
|
|
sub HtmlTemplateEval {
|
|
|
|
|
my $code = shift;
|
|
|
|
|
my $result = eval($code) || $@;
|
|
|
|
|
}
|
2004-11-21 19:29:52 +00:00
|
|
|
|
2004-11-23 23:26:30 +00:00
|
|
|
sub GetActionHtmlTemplate {
|
|
|
|
|
my $action = GetParam('action', 'browse');
|
|
|
|
|
# return browse.de.html, or browse.html, or error.html, or report an error...
|
|
|
|
|
foreach my $f ((map { "$action.$_" } HtmlTemplateLanguage()), $action, "error") {
|
2016-06-27 12:05:37 +02:00
|
|
|
return "$HtmlTemplateDir/$f.html" if IsFile("$HtmlTemplateDir/$f.html");
|
2004-11-23 22:10:16 +00:00
|
|
|
}
|
2004-11-23 23:26:30 +00:00
|
|
|
ReportError(Tss('Could not find %1.html template in %2', $action, $HtmlTemplateDir),
|
|
|
|
|
'500 INTERNAL SERVER ERROR');
|
|
|
|
|
}
|
2004-11-23 22:10:16 +00:00
|
|
|
|
2004-11-23 23:26:30 +00:00
|
|
|
sub HtmlTemplateLanguage {
|
|
|
|
|
my $requested_language = $q->http('Accept-language');
|
|
|
|
|
my @languages = split(/ *, */, $requested_language);
|
|
|
|
|
my %Lang = ();
|
2015-04-28 00:03:11 +03:00
|
|
|
foreach (@languages) {
|
2004-11-23 23:26:30 +00:00
|
|
|
my $qual = 1;
|
|
|
|
|
$qual = $1 if (/q=([0-9.]+)/);
|
|
|
|
|
$Lang{$qual} = $1 if (/^([-a-z]+)/);
|
2004-11-23 00:33:47 +00:00
|
|
|
}
|
2004-11-23 23:26:30 +00:00
|
|
|
return map { $Lang{$_} } sort { $b <=> $a } keys %Lang;
|
2004-11-21 19:29:52 +00:00
|
|
|
}
|