#!perl BEGIN { chdir('..'); } use strict; use warnings; use IO::File; use CGI ':standard'; use CGI::Carp 'fatalsToBrowser'; require "lib_pl"; our ($program, $submit_i, $file_i, $password_i, $common_conf); my $conf = read_data("$program.conf"); ldap_connect(); my $teachers = lookup_teachers(); my $id_name_ary = []; my $teacher_id_name = {}; for (@$teachers) { my $id = $_->{uid}; my $fullname = ldap_get_fullname($_, "uc_2ndname"); $teacher_id_name->{$id} = $fullname; push @$id_name_ary, $id, $fullname; } sub lookup_teachers { my @domains = @{ou_allowed_tree_to_ldap_domains($common_conf->{"ou.teachers"})}; my $users = []; for my $domain (@domains) { my $some_users = ldap_lookup_users_uid_name($domain); push @$users, @$some_users; } # get names for my $user (@$users) { ldap_name_attrs($user); $user->{fullname} = ldap_get_fullname($user); } # exclude those without a fullname @$users = grep {$_->{fullname} ne ""} @$users; # sort by surname @$users = sort { $a->{name2} cmp $b->{name2} } @$users; return $users; } my $target_dir = $conf->{directory}; my $happy_files = {map {$_, 1} split " ", $conf->{happy_files}}; hdr("Assignment Drop-box"); #d($teachers); #d($teacher_id_name); if (param()) { submitted(); } else { display_form(); } ftr(); sub display_form { my $dropdown = dropdown({abbrev=>1}, "teacher", $id_name_ary); print tbl_tsv(<{$extension}) { msg("This type of file is not allowed to be uploaded."); msg("Please complain to your sysadmin if you disagree."); $failed = 1; } } } } } my $abbrev = param("teacher"); my $teacher = $teacher_id_name->{$abbrev}; if (!$teacher) { msg("Please select a teacher from the list."); $failed = 1; } if (!$failed) { msg("Uploading to ${teacher}'s drop box. Please wait..."); $target_dir .= "\\$abbrev"; mkdir $target_dir; unless (chdir $target_dir) { msg("system error: cannot mkdir/chdir $target_dir: $!\n"); goto failed; } for (@files) { if (param($_)) { if (-e $filename{$_}) { already: msg("There is already a file uploaded with that name,", "please rename your file before uploading it."); goto failed; } my $file = slurp(param($_)); if (length($file) > $conf->{max_size_kb}*1024) { msg("This file is too big to upload."); goto failed; } # yeah, so there's a slight race condition if (-e $filename{$_}) { goto already; } belch($filename{$_}, $file); msg("file uploaded: $filename{$_}"); } } } if ($failed) { failed: msg("", "Press and try again."); } }