#!/usr/local/bin/perl # I wrote this for my own purposes, but feel free to use it and adapt it. If you don't know any # Perl or Unix at all, you might want to give this a miss. # # To install and configure: # 1. Change the first line to wherever Perl is on your server. # 2. Edit the variables to suit your own server and preferences. %labels and @essays are the same - # if someone can explain to me how to do it with just one array/hash, I'll be grateful. The script # is set up for a maximum of eight filename pairs, so if you want more, you'll need to edit the # "values" line in the radio-button group. # 3. Upload to your cgi-bin directory and make it executable. # 4. This one is a pain. Assuming you want this to be reasonably secure, you need to set up an htaccess # user name and password for each user. If you don't know # about htaccess, check out this site - http://www.freewebmasterhelp.com/tutorials/htaccess/ - or just # do a web search with keywords "htaccess tutorial OR howto". # # If you don't want to use htaccess, comment out the line "$name = $ENV{'REMOTE_USER'};" and uncomment # the lines lower down that start "h3("Your user name"),p,". Then find some other way to secure the page. use CGI qw/:standard/; use CGI::Carp('fatalsToBrowser'); $CGI::POST_MAX=1024 * 500; # Prevents really big uploads # Course-specific data - change this! $name = $ENV{'REMOTE_USER'}; # This grabs the user name from htaccess. If you're not using htaccess for user authentication # you should comment out this line and uncomment the lines lower down which print the box to # enter a user name. $program_url = 'http://lists.bilkent.edu.tr/~robin/cgibin/upload/101upload.cgi'; # URL of this program $filebase = '/home/robin/public_html/101/upload/'; # Directory to store files $filebase_url = 'http://lists.bilkent.edu.tr/~robin/101/upload/'; # URL of the above $css = 'http://www.bilkent.edu.tr/~robin/robin.css'; # URL of CSS stylesheet $coursepage = 'http://www.bilkent.edu.tr/~robin/101'; # URL of course page to return to %labels = ('101task1_draft', 'First essay draft','101task1_final', 'First essay final version','101task2_draft', 'Second essay draft','101task2_final', 'Second essay final version','101task3', 'Third essay'); # File names for essays. The first of each pair is the actual file name; the second is what will appear on the page. @essays = ('101task1_draft','101task1_final','101task2_draft','101task2_final','101task3'); print header; print start_html(-title=>"Upload files", -style=>{-src=>$css}); print_form() unless param; print_results() if param; make_index() if param; print end_html; close $file; sub print_form { print h1("Upload a file"); # Comment out the following line if you are not using htaccess to get the user name. print h2("Hello, $name"); print start_multipart_form(), # Uncomment this if you are not using htaccess to get the user name. # # h3("Your user name"),p, # textfield(-name=>'dir', # -size=>30, # -maxlength=>30), # p, h3("Which piece of work is this?"), radio_group(-name=>'essay', -values=>["@essays[0]","@essays[1]","@essays[2]","@essays[3]","@essays[4]"], -default=>"@essays[0]", -linebreak=>'true', -labels=>\%labels), h3("File to upload"), filefield(-name=>'upload',-size=>60), submit(-label=>'Upload File'), p("Note: by using this form, you are declaring that the essay you submit is entirely your own work, and that all ideas and sentences from sources have been properly cited. Drafts containing plagiarism will not be accepted, and final versions containing plagiarism will receive a zero, and could result in disciplinary action."), end_form; } sub print_results { my $length; my $file = param('upload'); # $name = param('name'); $essay = param('essay'); # Check that the user has selected a file to upload. if (!$file) { print h1("No file uploaded."); print h4 a({-href=>"$coursepage"},"Back to course page"); print h4 a({-href=>"$program_url"},"Upload another file"); return; } # Check that the user name has been entered (should be unnecessary # if you are using htaccess. if (!$name) { print h4("It looks like you forgot to enter your user name."); return; } # Check the type of file $type = uploadInfo($file)->{'Content-Type'}; if ($type eq 'text/html') { $ext = '.html'; } elsif ($type eq 'application/vnd.ms-word') { $ext = '.doc'; } elsif ($type eq 'application/msword'){ $ext = '.doc'; } elsif ($type eq 'text/x-tex') { $ext = '.tex'; } elsif ($type eq 'application/vnd.sun.xml.writer') { $ext = '.sxw'; } elsif ($type eq 'text/plain') { $ext = '.txt'; } elsif ($type eq 'application/rtf') { $ext = '.rtf'; } else { print "This is file is in a format I can't handle. I can deal with the following formats: plain text, HTML, OpenOffice Writer, MS Word, RTF, TeX/LaTeX. Please save it in one of these formats and try again."; return;} my $version = 1; while (-e "$filebase/$name$essay$version$ext") {$version++} open(FILE, ">$filebase/$name$essay$version$ext") or die $!; while(<$file>) { $length += length($_); print FILE $_; } print h1("File uploaded successfully!"); print p("Your file, $file, has been uploaded and renamed $name$essay$version$ext."); print p('File length: ',$length); restore_parameters(); print h4 a({-href=>"$coursepage"},"Back to course page"); print h4 a({-href=>"$program_url"},"Upload another file");} sub make_index { $i = 0; print h2("Files uploaded by $name"); opendir DIR, "$filebase"; while ( defined( $_ = readdir DIR ) ) {$i++; $files[$i] = $_; if (/$name/) { print "

'; print "$_"; print '

';} } }