Initial checkin of Webmin
[webmin.git] / group_chooser.cgi
1 #!/usr/local/bin/perl
2 # group_chooser.cgi
3 # This CGI generated the HTML for choosing a group or list of groups.
4
5 require './web-lib.pl';
6 &init_config();
7 &ReadParse(undef, undef, 2);
8 %access = &get_module_acl();
9
10 # Build list of primary groups
11 setpwent();
12 while(@uinfo = getpwent()) {
13         push(@{$members{$uinfo[3]}}, $uinfo[0]);
14         }
15 endpwent() if ($gconfig{'os_type'} ne 'hpux');
16
17 if ($in{'multi'}) {
18         # selecting multiple groups.
19         if ($in{'frame'} == 0) {
20                 # base frame
21                 &PrintHeader();
22                 print "<script>\n";
23                 @ul = &split_quoted($in{'group'});
24                 $len = @ul;
25                 print "sel = new Array($len);\n";
26                 print "selr = new Array($len);\n";
27                 for($i=0; $i<$len; $i++) {
28                         print "sel[$i] = \"$ul[$i]\";\n";
29                         @ginfo = getgrnam($ul[$i]);
30                         if (@ginfo) {
31                                 @mems = &unique( split(/ /, $ginfo[3]),
32                                                  @{$members{$ginfo[2]}} );
33                                 if (@mems > 3) { @mems = (@mems[0..1], "..."); }
34                                 print "selr[$i] = \"",join(' ', @mems),"\";\n";
35                                 }
36                         else { print "selr[$i] = \"???\";\n"; }
37                         }
38                 print "</script>\n";
39                 print "<title>$text{'groups_title1'}</title>\n";
40                 print "<frameset cols='50%,50%'>\n";
41                 print "<frame src=\"/group_chooser.cgi?frame=1&multi=1\">\n";
42                 print "<frameset rows='*,50' frameborder=no>\n";
43                 print " <frame src=\"/group_chooser.cgi?frame=2&multi=1\">\n";
44                 print " <frame src=\"/group_chooser.cgi?frame=3&multi=1\" scrolling=no>\n";
45                 print "</frameset>\n";
46                 print "</frameset>\n";
47                 }
48         elsif ($in{'frame'} == 1) {
49                 # list of all groups to choose from
50                 &popup_header();
51                 print "<script>\n";
52                 print "function addgroup(u, r)\n";
53                 print "{\n";
54                 print "top.sel[top.sel.length] = u\n";
55                 print "top.selr[top.selr.length] = r\n";
56                 print "top.frames[1].location = top.frames[1].location\n";
57                 print "return false;\n";
58                 print "}\n";
59                 print "</script>\n";
60                 print "<font size=+1>$text{'groups_all'}</font>\n";
61                 print "<table width=100%>\n";
62                 foreach $u (&get_groups_list()) {
63                         if ($in{'group'} eq $u->[0]) { print "<tr $cb>\n"; }
64                         else { print "<tr>\n"; }
65                         print "<td width=20%><a href=\"\" onClick='return addgroup(\"$u->[0]\", \"$u->[3]\")'>$u->[0]</a></td>\n";
66                         print "<td>$u->[3]</td> </tr>\n";
67                         }
68                 print "</table>\n";
69                 &popup_footer();
70                 }
71         elsif ($in{'frame'} == 2) {
72                 # show chosen groups
73                 &popup_header();
74                 print "<font size=+1>$text{'groups_sel'}</font>\n";
75                 print <<'EOF';
76 <table width=100%>
77 <script>
78 function sub(j)
79 {
80 sel2 = new Array(); selr2 = new Array();
81 for(k=0,l=0; k<top.sel.length; k++) {
82         if (k != j) {
83                 sel2[l] = top.sel[k];
84                 selr2[l] = top.selr[k];
85                 l++;
86                 }
87         }
88 top.sel = sel2; top.selr = selr2;
89 location = location;
90 return false;
91 }
92 for(i=0; i<top.sel.length; i++) {
93         document.write("<tr>\n");
94         document.write("<td><a href=\"\" onClick='return sub("+i+")'>"+top.sel[i]+"</a></td>\n");
95         document.write("<td>"+top.selr[i]+"</td>\n");
96         }
97 </script>
98 </table>
99 EOF
100                 &popup_footer();
101                 }
102         elsif ($in{'frame'} == 3) {
103                 # output OK and Cancel buttons
104                 &popup_header();
105                 print "<script>\n";
106                 print "function qjoin(l)\n";
107                 print "{\n";
108                 print "rv = \"\";\n";
109                 print "for(i=0; i<l.length; i++) {\n";
110                 print "    if (rv != '') rv += ' ';\n";
111                 print "    if (l[i].indexOf(' ') < 0) rv += l[i];\n";
112                 print "    else rv += '\"'+l[i]+'\"'\n";
113                 print "    }\n";
114                 print "return rv;\n";
115                 print "}\n";
116                 print "</script>\n";
117                 print "<form>\n";
118                 print "<input type=button value=\"$text{'groups_ok'}\" ",
119                       " onClick='top.opener.ifield.value = qjoin(top.sel); ",
120                       "top.close()'>\n";
121                 print "<input type=button value=\"$text{'groups_cancel'}\" ",
122                       "onClick='top.close()'>\n";
123                 print "&nbsp;&nbsp;<input type=button value=\"$text{'groups_clear'}\" onClick='top.sel = new Array(); top.selr = new Array(); top.frames[1].location = top.frames[1].location'>\n";
124                 print "</form>\n";
125                 &popup_footer();
126                 }
127         }
128 else {
129         # selecting just one group .. display a list of all groups to
130         # choose from
131         &popup_header($text{'groups_title2'});
132         print "<script>\n";
133         print "function select(f)\n";
134         print "{\n";
135         print "top.opener.ifield.value = f;\n";
136         print "top.close();\n";
137         print "return false;\n";
138         print "}\n";
139         print "</script>\n";
140         print "<table width=100%>\n";
141         foreach $u (&get_groups_list()) {
142                 if ($in{'group'} eq $u->[0]) { print "<tr $cb>\n"; }
143                 else { print "<tr>\n"; }
144                 print "<td width=20%><a href=\"\" onClick='return select(\"$u->[0]\")'>$u->[0]</a></td>\n";
145                 print "<td>$u->[3]</td> </tr>\n";
146                 }
147         print "</table>\n";
148         &popup_footer();
149         }
150
151 sub get_groups_list
152 {
153 local(@ginfo, @groups, %gcan, %found);
154 if ($access{'gedit_mode'} == 2 || $access{'gedit_mode'} == 3) {
155         map { $gcan{$_}++ } split(/\s+/, $access{'gedit'});
156         }
157 setgrent();
158 while(@ginfo = getgrent()) {
159         @mems = &unique( split(/ /, $ginfo[3]), @{$members{$ginfo[2]}} );
160         if (@mems > 3) { @mems = (@mems[0..1], "..."); }
161         $ginfo[3] = join(' ', @mems);
162         if ($access{'gedit_mode'} == 0 ||
163             $access{'gedit_mode'} == 2 && $gcan{$ginfo[0]} ||
164             $access{'gedit_mode'} == 3 && !$gcan{$ginfo[0]} ||
165             $access{'gedit_mode'} == 4 &&
166                 (!$access{'gedit'} || $ginfo[2] >= $access{'gedit'}) &&
167                 (!$access{'gedit2'} || $ginfo[2] <= $access{'gedit2'})) {
168                 push(@groups, [ @ginfo ]) if (!$found{$ginfo[0]}++);
169                 }
170         }
171 endgrent() if ($gconfig{'os_type'} ne 'hpux');
172 return sort { $a->[0] cmp $b->[0] } @groups;
173 }
174
175 # split_quoted(string)
176 sub split_quoted
177 {
178 local @rv;
179 local $str = $_[0];
180 while($str =~ /^\s*(\S*"[^"]+"\S*)(.*)$/ || $str =~ /^\s*(\S+)(.*)$/) {
181         $str = $2;
182         local $g = $1;
183         $g =~ s/"//g;
184         push(@rv, $g);
185         }
186 return @rv;
187 }
188