Handle hostnames with upper-case letters
[webmin.git] / help.cgi
1 #!/usr/local/bin/perl
2 # help.cgi
3 # Displays help HTML for some module, with substitutions
4
5 BEGIN { push(@INC, ".."); };
6 use WebminCore;
7
8 $trust_unknown_referers = 1;
9 &init_config();
10 &error_setup($text{'help_err'});
11 $ENV{'PATH_INFO'} !~ /[\\\&\;\`\'\"\|\*\?\~\<\>\^\(\)\[\]\{\}\$\n\r]/ ||
12         &error($text{'help_epath'});
13 $ENV{'PATH_INFO'} =~ /^\/(\S+)\/(\S+)$/ || &error($text{'help_epath'});
14 $module = $1; $file = $2;
15
16 # if it ends with .gif assume it is a direct URL
17 if ($file =~ /\.(gif|jpg|jpeg|png)$/i) {
18         &redirect("$module/$file");
19         exit;
20 }
21
22 # read the help file
23 $path = &help_file($module, $file);
24 @st = stat($path);
25 open(HELP, $path) || &helperror(&text('help_efile', $path));
26 read(HELP, $help, $st[7]);
27 close(HELP);
28
29 # find and replace the <header> section
30 if ($help =~ s/<header>([^<]+)<\/header>//i) {
31         &popup_header($1);
32         print &ui_subheading($1);
33         }
34 else {
35         &helperror($text{'help_eheader'});
36         }
37
38 # replace any explicit use of <hr> with the ui-lib function
39 $uihr = &ui_hr();
40 $help =~ s/<hr>/$uihr/ig;
41
42 # find and replace the <footer> section
43 $help =~ s/<footer>/<p>/i;
44
45 # find and replace <include> directives
46 $help =~ s/<include\s+(\S+)>/inchelp($1)/ige;
47
48 # find and replace <if><else> directives
49 $help =~ s/<if\s+([^>]*)>([\000-\177]*?)<else>([\000-\177]*?)<\/if>/ifhelp($1, $2, $3)/ige;
50
51 # find and replace <if> directives
52 $help =~ s/<if\s+([^>]*)>([\000-\177]*?)<\/if>/ifhelp($1, $2)/ige;
53
54 # find and replace <exec> directives
55 $help =~ s/<exec\s+([^>]*)>/exechelp($1)/ige;
56
57 # output the HTML
58 print $help;
59 &popup_footer();
60
61 # inchelp(path)
62 sub inchelp
63 {
64 if ($_[0] =~ /^\/(\S+)\/(\S+)$/) {
65         # including something from another module..
66         }
67 else {
68         # including from this module
69         local $ipath = &help_file($module, $_[0]);
70         @st = stat($ipath);
71         open(INC, $ipath) ||
72                 return "<i>".&text('help_einclude', $_[0])."</i><br>\n";
73         read(INC, $inc, $st[7]);
74         close(INC);
75         return $inc;
76         }
77 }
78
79 # ifhelp(perl, text, [elsetext])
80 sub ifhelp
81 {
82 local $rv = eval $_[0];
83 if ($@) { return "<i>".&text('help_eif', $_[0], $@)."</i><br>\n"; }
84 elsif ($rv) { return $_[1]; }
85 else { return $_[2]; }
86 }
87
88 # exechelp(perl)
89 sub exechelp
90 {
91 local $rv = eval $_[0];
92 if ($@) { return "<i>".&text('help_eexec', $_[0], $@)."</i><br>\n"; }
93 else { return $rv; }
94 }
95
96 sub helperror
97 {
98 &header($text{'error'});
99 print "<center><h2>$text{'error'}</h2></center>\n";
100 print "<hr><p><b>",@_,"</b><p><hr>\n";
101 exit;
102 }
103