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