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