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