Handle hostnames with upper-case letters
[webmin.git] / ui-lib.pl
old mode 100644 (file)
new mode 100755 (executable)
index 8b14832..178677f
--- a/ui-lib.pl
+++ b/ui-lib.pl
@@ -1,9 +1,13 @@
+use vars qw($theme_no_table $ui_radio_selector_donejs $module_name 
+           $ui_multi_select_donejs);
+
 =head1 ui-lib.pl
 
 Common functions for generating HTML for Webmin user interface elements.
 Some example code :
 
- require '../ui-lib.pl';
+ use WebminCore;
+ init_config();
  ui_print_header(undef, 'My Module', '');
 
  print ui_form_start('save.cgi');
@@ -260,7 +264,7 @@ $rv .= "</tr>\n";
 return $rv;
 }
 
-=head2 ui_checked_columns_row(&columns, &tdtags, checkname, checkvalue, [checked?], [disabled])
+=head2 ui_checked_columns_row(&columns, &tdtags, checkname, checkvalue, [checked?], [disabled], [tags])
 
 Returns HTML for a row in a multi-column table, in which the first column 
 contains a checkbox. The parameters are :
@@ -277,15 +281,17 @@ contains a checkbox. The parameters are :
 
 =item disabled - Set to 1 if the checkbox should be disabled and thus un-clickable.
 
+=item tags - Extra HTML tags to include in the radio button.
+
 =cut
 sub ui_checked_columns_row
 {
 return &theme_ui_checked_columns_row(@_) if (defined(&theme_ui_checked_columns_row));
-my ($cols, $tdtags, $checkname, $checkvalue, $checked, $disabled) = @_;
+my ($cols, $tdtags, $checkname, $checkvalue, $checked, $disabled, $tags) = @_;
 my $rv;
 $rv .= "<tr $cb class='ui_checked_columns'>\n";
 $rv .= "<td class='ui_checked_checkbox' ".$tdtags->[0].">".
-       &ui_checkbox($checkname, $checkvalue, undef, $checked, undef, $disabled).
+       &ui_checkbox($checkname, $checkvalue, undef, $checked, $tags, $disabled).
        "</td>\n";
 my $i;
 for($i=0; $i<@$cols; $i++) {
@@ -304,7 +310,7 @@ $rv .= "</tr>\n";
 return $rv;
 }
 
-=head2 ui_radio_columns_row(&columns, &tdtags, checkname, checkvalue, [checked], [disabled])
+=head2 ui_radio_columns_row(&columns, &tdtags, checkname, checkvalue, [checked], [disabled], [tags])
 
 Returns HTML for a row in a multi-column table, in which the first
 column is a radio button. The parameters are :
@@ -321,11 +327,13 @@ column is a radio button. The parameters are :
 
 =item disabled - Set to 1 if the radio button should be disabled and thus un-clickable.
 
+=item tags - Extra HTML tags to include in the radio button.
+
 =cut
 sub ui_radio_columns_row
 {
 return &theme_ui_radio_columns_row(@_) if (defined(&theme_ui_radio_columns_row));
-my ($cols, $tdtags, $checkname, $checkvalue, $checked, $dis) = @_;
+my ($cols, $tdtags, $checkname, $checkvalue, $checked, $dis, $tags) = @_;
 my $rv;
 $rv .= "<tr $cb class='ui_radio_columns'>\n";
 $rv .= "<td class='ui_radio_radio' ".$tdtags->[0].">".
@@ -437,13 +445,15 @@ foreach my $r (@$data) {
                        # Checkbox in non-first column
                        push(@cols, &ui_checkbox($c->{'name'}, $c->{'value'},
                                                 $c->{'label'}, $c->{'checked'},
-                                                undef, $c->{'disabled'}));
+                                                $c->{'tags'},
+                                                $c->{'disabled'}));
                        }
                elsif ($c->{'type'} eq 'radio') {
                        # Radio button in non-first column
                        push(@cols, &ui_oneradio($c->{'name'}, $c->{'value'},
                                                 $c->{'label'}, $c->{'checked'},
-                                                undef, $c->{'disabled'}));
+                                                $c->{'tags'},
+                                                $c->{'disabled'}));
                        }
                elsif ($c->{'type'} eq 'group') {
                        # Header row that spans whole table
@@ -471,12 +481,14 @@ foreach my $r (@$data) {
        elsif ($c0->{'type'} eq 'checkbox') {
                $rv .= &ui_checked_columns_row(\@cols, \@rtds, $c0->{'name'},
                                               $c0->{'value'}, $c0->{'checked'},
-                                              $c0->{'disabled'});
+                                              $c0->{'disabled'},
+                                              $c0->{'tags'});
                }
        elsif ($c0->{'type'} eq 'radio') {
                $rv .= &ui_radio_columns_row(\@cols, \@rtds, $c0->{'name'},
                                             $c0->{'value'}, $c0->{'checked'},
-                                            $c0->{'disabled'});
+                                            $c0->{'disabled'},
+                                            $c0->{'tags'});
                }
        }
 
@@ -484,7 +496,7 @@ $rv .= &ui_columns_end();
 return $rv;
 }
 
-=head2 ui_form_columns_table(cgi, &buttons, select-all, &otherlinks, &hiddens, &headings, width-percent, &data, &types, no-sort, title, empty-msg)
+=head2 ui_form_columns_table(cgi, &buttons, select-all, &otherlinks, &hiddens, &headings, width-percent, &data, &types, no-sort, title, empty-msg, form-no)
 
 Similar to ui_columns_table, but wrapped in a form. Parameters are :
 
@@ -506,7 +518,7 @@ sub ui_form_columns_table
 return &theme_ui_form_columns_table(@_)
        if (defined(&theme_ui_form_columns_table));
 my ($cgi, $buttons, $selectall, $others, $hiddens,
-       $heads, $width, $data, $types, $nosort, $title, $emptymsg) = @_;
+       $heads, $width, $data, $types, $nosort, $title, $emptymsg, $formno) = @_;
 my $rv;
 
 # Build links
@@ -529,8 +541,8 @@ if (@$data) {
                                }
                        }
                if ($cbname) {
-                       unshift(@leftlinks, &select_all_link($cbname),
-                                           &select_invert_link($cbname));
+                       unshift(@leftlinks, &select_all_link($cbname, $formno),
+                                   &select_invert_link($cbname, $formno));
                        }
                }
        }
@@ -704,12 +716,19 @@ options. May truncate values to 2 decimal points! The parameters are :
 
 =item disabled - Set to 1 if this text box should be disabled by default.
 
+=item tags - Additional HTML attributes for the <input> tag.
+
+=item defaultunits - Units mode selected by default
+
 =cut
 sub ui_bytesbox
 {
-my ($name, $bytes, $size, $dis) = @_;
+my ($name, $bytes, $size, $dis, $tags, $defaultunits) = @_;
 my $units = 1;
-if ($bytes >= 10*1024*1024*1024*1024) {
+if ($bytes eq '' && $defaultunits) {
+       $units = $defaultunits;
+       }
+elsif ($bytes >= 10*1024*1024*1024*1024) {
        $units = 1024*1024*1024*1024;
        }
 elsif ($bytes >= 10*1024*1024*1024) {
@@ -729,7 +748,7 @@ if ($bytes ne "") {
        $bytes =~ s/\.00$//;
        }
 $size = &ui_max_text_width($size || 8);
-return &ui_textbox($name, $bytes, $size, $dis)." ".
+return &ui_textbox($name, $bytes, $size, $dis, undef, $tags)." ".
        &ui_select($name."_units", $units,
                 [ [ 1, "bytes" ],
                   [ 1024, "kB" ],
@@ -763,7 +782,7 @@ return "<input class='ui_upload' type=file name=\"".&quote_escape($name)."\" ".
        ($tags ? " ".$tags : "").">";
 }
 
-=head2 ui_password(name, value, size, [disabled?], [maxlength])
+=head2 ui_password(name, value, size, [disabled?], [maxlength], [tags])
 
 Returns HTML for a password text input. Parameters are the same as ui_textbox,
 and behaviour is identical except that the user's input is not visible.
@@ -772,13 +791,14 @@ and behaviour is identical except that the user's input is not visible.
 sub ui_password
 {
 return &theme_ui_password(@_) if (defined(&theme_ui_password));
-my ($name, $value, $size, $dis, $max) = @_;
+my ($name, $value, $size, $dis, $max, $tags) = @_;
 $size = &ui_max_text_width($size);
 return "<input class='ui_password' ".
        "type=password name=\"".&quote_escape($name)."\" ".
        "value=\"".&quote_escape($value)."\" ".
        "size=$size ".($dis ? "disabled=true" : "").
        ($max ? " maxlength=$max" : "").
+       " ".$tags.
        ">";
 }
 
@@ -901,13 +921,13 @@ if (defined($opts_title)) {
        }
 $rv .= "<tr class='ui_multi_select_row'>";
 $rv .= "<td>".&ui_select($name."_opts", [ ], $leftover,
-                        $size, 0, 0, $dis, $wstyle)."</td>\n";
-$rv .= "<td>".&ui_button("->", undef, $dis,
+                        $size, 1, 0, $dis, $wstyle)."</td>\n";
+$rv .= "<td>".&ui_button("->", $name."_add", $dis,
                 "onClick='multi_select_move(\"$name\", form, 1)'")."<br>".
-             &ui_button("<-", undef, $dis,
+             &ui_button("<-", $name."_remove", $dis,
                 "onClick='multi_select_move(\"$name\", form, 0)'")."</td>\n";
 $rv .= "<td>".&ui_select($name."_vals", [ ], $values,
-                        $size, 0, 0, $dis, $wstyle)."</td>\n";
+                        $size, 1, 0, $dis, $wstyle)."</td>\n";
 $rv .= "</tr></table>\n";
 $rv .= &ui_hidden($name, join("\n", map { $_->[0] } @$values));
 return $rv;
@@ -933,15 +953,27 @@ var opts_idx = opts.selectedIndex;
 var vals_idx = vals.selectedIndex;
 if (dir == 1 && opts_idx >= 0) {
        // Moving from options to selected list
-       var o = opts.options[opts_idx];
-       vals.options[vals.options.length] = new Option(o.text, o.value);
-       opts.remove(opts_idx);
+       for(var i=0; i<opts.options.length; i++) {
+               var o = opts.options[i];
+               if (o.selected) {
+                       vals.options[vals.options.length] =
+                               new Option(o.text, o.value);
+                       opts.remove(i);
+                       i--;
+                       }
+               }
        }
 else if (dir == 0 && vals_idx >= 0) {
        // Moving the other way
-       var o = vals.options[vals_idx];
-       opts.options[opts.options.length] = new Option(o.text, o.value);
-       vals.remove(vals_idx);
+       for(var i=0; i<vals.options.length; i++) {
+               var o = vals.options[i];
+               if (o.selected) {
+                       opts.options[opts.options.length] =
+                               new Option(o.text, o.value);
+                       vals.remove(i);
+                       i--;
+                       }
+               }
        }
 // Fill in hidden field
 var hid = f.elements[name];
@@ -1174,11 +1206,13 @@ a field with radio buttons next to it. The parameters are :
 
 =item max - Optional maximum allowed input length, in characters.
 
+=item tags - Additional HTML attributes for the text box
+
 =cut
 sub ui_opt_textbox
 {
 return &theme_ui_opt_textbox(@_) if (defined(&theme_ui_opt_textbox));
-my ($name, $value, $size, $opt1, $opt2, $dis, $extra, $max) = @_;
+my ($name, $value, $size, $opt1, $opt2, $dis, $extra, $max, $tags) = @_;
 my $dis1 = &js_disable_inputs([ $name, @$extra ], [ ]);
 my $dis2 = &js_disable_inputs([ ], [ $name, @$extra ]);
 my $rv;
@@ -1189,7 +1223,9 @@ $rv .= &ui_radio($name."_def", $value eq '' ? 1 : 0,
 $rv .= "<input class='ui_opt_textbox' name=\"".&quote_escape($name)."\" ".
        "size=$size value=\"".&quote_escape($value)."\" ".
        ($value eq "" || $dis ? "disabled=true" : "").
-       ($max ? " maxlength=$max" : "").">\n";
+       ($max ? " maxlength=$max" : "").
+       " ".$tags.
+       ">\n";
 return $rv;
 }
 
@@ -1416,12 +1452,35 @@ if (!$tconfig{'nohr'} && !$tconfig{'nobottomhr'}) {
 return $rv;
 }
 
-=head2 ui_print_header(subtext, args...)
+=head2 ui_print_header(subtext, image, [help], [config], [nomodule], [nowebmin], [rightside], [head-stuff], [body-stuff], [below])
 
 Print HTML for a header with the post-header line. The args are the same
-as those passed to header(), defined in web-lib-funcs.pl. The exception is
-the additional subtext parameter, which is for optional HTML to display
-just below the header.
+as those passed to header(), defined in web-lib-funcs.pl, with the addition
+of the subtext parameter :
+
+=item subtext - Text to display below the title
+
+=item title - The text to show at the top of the page
+
+=item image - An image to show instead of the title text. This is typically left blank.
+
+=item help - If set, this is the name of a help page that will be linked to in the title.
+
+=item config - If set to 1, the title will contain a link to the module's config page.
+
+=item nomodule - If set to 1, there will be no link in the title section to the module's index.
+
+=item nowebmin - If set to 1, there will be no link in the title section to the Webmin index.
+
+=item rightside - HTML to be shown on the right-hand side of the title. Can contain multiple lines, separated by <br>. Typically this is used for links to stop, start or restart servers.
+
+=item head-stuff - HTML to be included in the <head> section of the page.
+
+=item body-stuff - HTML attributes to be include in the <body> tag.
+
+=item below - HTML to be displayed below the title. Typically this is used for application or server version information.
+
+
 
 =cut
 sub ui_print_header
@@ -1802,8 +1861,8 @@ if (!$main::ui_hidden_start_donejs++) {
        }
 
 # Build list of tab titles and names
-my $tabnames = "[".join(",", map { "\"".&html_escape($_->[0])."\"" } @$tabs)."]";
-my $tabtitles = "[".join(",", map { "\"".&html_escape($_->[1])."\"" } @$tabs)."]";
+my $tabnames = "[".join(",", map { "\"".&quote_escape($_->[0])."\"" } @$tabs)."]";
+my $tabtitles = "[".join(",", map { "\"".&quote_escape($_->[1])."\"" } @$tabs)."]";
 $rv .= "<script>\n";
 $rv .= "document.${name}_tabnames = $tabnames;\n";
 $rv .= "document.${name}_tabtitles = $tabtitles;\n";
@@ -1822,7 +1881,7 @@ $rv .= "</td></tr>\n";
 $rv .= "<tr>\n";
 $rv .= "<td bgcolor=#ffffff width=1><img src=$imgdir/1x1.gif></td>\n";
 foreach my $t (@$tabs) {
-       if ($t ne $tabs[0]) {
+       if ($t ne $$tabs[0]) {
                # Spacer
                $rv .= "<td width=2 bgcolor=#ffffff class='ui_tab_spacer'>".
                       "<img src=$imgdir/1x1.gif></td>\n";
@@ -1965,10 +2024,13 @@ return $max && $w > $max ? $max : $w;
 =head2 ui_radio_selector(&opts, name, selected)
 
 Returns HTML for a set of radio buttons, each of which shows a different
-block of HTML when selected. &opts is an array ref to arrays containing
-[ value, label, html ]
+block of HTML when selected. The parameters are :
 
-XXX
+=item opts - An array ref to arrays containing [ value, label, html ]
+
+=item name - HTML name for the radio buttons
+
+=item selected - Value for the initially selected button.
 
 =cut
 sub ui_radio_selector
@@ -2015,11 +2077,23 @@ EOF
 
 ####################### grid layout functions
 
-=head2 ui_grid_table(&elements, columns, [width-percent], [tds], [tabletags], [title])
+=head2 ui_grid_table(&elements, columns, [width-percent], [&tds], [tabletags], [title])
 
 Given a list of HTML elements, formats them into a table with the given
 number of columns. However, themes are free to override this to use fewer
-columns where space is limited.
+columns where space is limited. Parameters are :
+
+=item elements - An array reference of table elements, each of which can be any HTML you like.
+
+=item columns - Desired number of columns in the grid.
+
+=item width-percent - Optional desired width as a percentage.
+
+=item tds - Array ref of HTML attributes for <td> tags in the tables.
+
+=item tabletags - HTML attributes for the <table> tag.
+
+=item title - Optional title to add to the top of the grid.
 
 =cut
 sub ui_grid_table
@@ -2057,24 +2131,33 @@ if (defined($title)) {
 return $rv;
 }
 
-=head2 ui_radio_table(name, selected, &rows)
+=head2 ui_radio_table(name, selected, &rows, [no-bold])
 
 Returns HTML for a table of radio buttons, each of which has a label and
-some associated inputs to the right.
+some associated inputs to the right. The parameters are :
+
+=item name - Unique name for this table, which is also the radio buttons' name.
+
+=item selected - Value for the initially selected radio button.
+
+=item rows - Array ref of array refs, one per button. The elements of each are the value for this option, a label, and option additional HTML to appear next to it.
+
+=item no-bold - When set to 1, labels in the table will not be bolded
 
 =cut
 sub ui_radio_table
 {
 return &theme_ui_radio_table(@_) if (defined(&theme_ui_radio_table));
-my ($name, $sel, $rows) = @_;
+my ($name, $sel, $rows, $nobold) = @_;
 return "" if (!@$rows);
 my $rv = "<table class='ui_radio_table'>\n";
 foreach my $r (@$rows) {
        $rv .= "<tr>\n";
-       $rv .= "<td valign=top".(defined($r->[2]) ? "" : " colspan=2").
-              "><b>".
-              &ui_oneradio($name, $r->[0], $r->[1], $r->[0] eq $sel).
-              "</b></td>\n";
+       $rv .= "<td valign=top".(defined($r->[2]) ? "" : " colspan=2").">".
+              ($nobold ? "" : "<b>").
+              &ui_oneradio($name, $r->[0], $r->[1], $r->[0] eq $sel, $r->[3]).
+              ($nobold ? "" : "</b>").
+              "</td>\n";
        if (defined($r->[2])) {
                $rv .= "<td valign=top>".$r->[2]."</td>\n";
                }
@@ -2086,7 +2169,15 @@ return $rv;
 
 =head2 ui_up_down_arrows(uplink, downlink, up-show, down-show)
 
-Returns HTML for moving some objects in a table up or down
+Returns HTML for moving some objects in a table up or down. The parameters are :
+
+=item uplink - URL for the up-arrow link.
+
+=item downlink - URL for the down-arrow link.
+
+=item up-show - Set to 1 if the up-arrow should be shown, 0 if not.
+
+=item down-show - Set to 1 if the down-arrow should be shown, 0 if not.
 
 =cut
 sub ui_up_down_arrows
@@ -2114,7 +2205,7 @@ return $mover;
 
 =head2 ui_hr
 
-Returns a horizontal row tag
+Returns a horizontal row tag, typically just an <hr>
 
 =cut
 sub ui_hr
@@ -2125,7 +2216,7 @@ return "<hr>\n";
 
 =head2 ui_nav_link(direction, url, disabled)
 
-Returns an arrow icon linking to provided url
+Returns an arrow icon linking to the provided url.
 
 =cut
 sub ui_nav_link
@@ -2143,10 +2234,22 @@ else {
        }
 }
 
-=head2 ui_confirmation_form(cgi, message, &hiddens, [&buttons], [&otherinputs], [extra-warning])
+=head2 ui_confirmation_form(cgi, message, &hiddens, [&buttons], [otherinputs], [extra-warning])
 
 Returns HTML for a form asking for confirmation before performing some
-action, such as deleting a user.
+action, such as deleting a user. The parameters are :
+
+=item cgi - Script to which the confirmation form submits, like delete.cgi.
+
+=item message - Warning message for the user to see.
+
+=item hiddens - Array ref of two-element array refs, containing hidden form field names and values.
+
+=item buttons - Array ref of two-element array refs, containing form button names and labels.
+
+=item otherinputs - HTML for extra inputs to include in ther form.
+
+=item extra-warning - An additional separate warning message to show.
 
 =cut
 sub ui_confirmation_form
@@ -2172,10 +2275,10 @@ return $rv;
 
 ####################### javascript functions
 
-=head2 js_disable_inputs
+=head2 js_disable_inputs(&disable-inputs, &enable-inputs, [tag])
 
-js_disable_input(&disable-inputs, &enable-inputs, [tag])
-Returns Javascript to disable some form elements and enable others
+Returns Javascript to disable some form elements and enable others. Mainly
+for internal use.
 
 =cut
 sub js_disable_inputs
@@ -2205,7 +2308,23 @@ return $_[2] ? "$_[2]='$rv'" : $rv;
 
 Returns HTML for moving left and right in some large list, such as an inbox
 or database table. If only 5 parameters are given, no far links are included.
-If any link is undef, that array will be greyed out.
+If any link is undef, that array will be greyed out. The parameters are :
+
+=item message - Text or display between arrows.
+
+=item inputs - Additional HTML inputs to show after message.
+
+=item cgi - Optional CGI for form wrapping arrows to submit to.
+
+=item left-link - Link for left-facing arrow.
+
+=item right-link - Link for right-facing arrow.
+
+=item far-left-link - Link for far left-facing arrow, optional.
+
+=item far-right-link - Link for far right-facing arrow, optional.
+
+=item below - HTML to display below the arrows.
 
 =cut
 sub ui_page_flipper
@@ -2218,22 +2337,24 @@ $rv .= &ui_form_start($cgi) if ($cgi);
 # Far left link, if needed
 if (@_ > 5) {
        if ($farleft) {
-               $rv .= "<a href='$farleft'><img src=/images/first.gif ".
+               $rv .= "<a href='$farleft'>".
+                      "<img src=$gconfig{'webprefix'}/images/first.gif ".
                       "border=0 align=middle></a>\n";
                }
        else {
-               $rv .= "<img src=/images/first-grey.gif ".
+               $rv .= "<img src=$gconfig{'webprefix'}/images/first-grey.gif ".
                       "border=0 align=middle></a>\n";
                }
        }
 
 # Left link
 if ($left) {
-       $rv .= "<a href='$left'><img src=/images/left.gif ".
+       $rv .= "<a href='$left'>".
+              "<img src=$gconfig{'webprefix'}/images/left.gif ".
               "border=0 align=middle></a>\n";
        }
 else {
-       $rv .= "<img src=/images/left-grey.gif ".
+       $rv .= "<img src=$gconfig{'webprefix'}/images/left-grey.gif ".
               "border=0 align=middle></a>\n";
        }
 
@@ -2243,22 +2364,24 @@ $rv .= " ".$inputs if ($inputs);
 
 # Right link
 if ($right) {
-       $rv .= "<a href='$right'><img src=/images/right.gif ".
+       $rv .= "<a href='$right'>".
+              "<img src=$gconfig{'webprefix'}/images/right.gif ".
               "border=0 align=middle></a>\n";
        }
 else {
-       $rv .= "<img src=/images/right-grey.gif ".
+       $rv .= "<img src=$gconfig{'webprefix'}/images/right-grey.gif ".
               "border=0 align=middle></a>\n";
        }
 
 # Far right link, if needed
 if (@_ > 5) {
        if ($farright) {
-               $rv .= "<a href='$farright'><img src=/images/last.gif ".
+               $rv .= "<a href='$farright'>".
+                      "<img src=$gconfig{'webprefix'}/images/last.gif ".
                       "border=0 align=middle></a>\n";
                }
        else {
-               $rv .= "<img src=/images/last-grey.gif ".
+               $rv .= "<img src=$gconfig{'webprefix'}/images/last-grey.gif ".
                       "border=0 align=middle></a>\n";
                }
        }
@@ -2271,7 +2394,7 @@ return $rv;
 
 =head2 js_checkbox_disable(name, &checked-disable, &checked-enable, [tag])
 
-MISSING DOCUMENTATION
+For internal use only.
 
 =cut
 sub js_checkbox_disable
@@ -2289,7 +2412,7 @@ return $_[3] ? "$_[3]='$rv'" : $rv;
 
 =head2 js_redirect(url, [window-object])
 
-Returns HTML to trigger a redirect to some URL
+Returns HTML to trigger a redirect to some URL.
 
 =cut
 sub js_redirect
@@ -2298,6 +2421,11 @@ my ($url, $window) = @_;
 if (defined(&theme_js_redirect)) {
        return &theme_js_redirect(@_);
        }
+$window ||= "window";
+if ($url =~ /^\//) {
+       # If the URL is like /foo , add webprefix
+       $url = $gconfig{'webprefix'}.$url;
+       }
 return "<script>${window}.location = '".&quote_escape($url)."';</script>\n";
 }