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