[system] / trunk / webwork / system / cgi / cgi-scripts / profUploadClasslistFile.pl Repository:
ViewVC logotype

View of /trunk/webwork/system/cgi/cgi-scripts/profUploadClasslistFile.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (download) (as text) (annotate)
Fri Jun 15 21:06:18 2001 UTC (11 years, 11 months ago) by sam
File size: 7881 byte(s)
nothing should change

    1 #!/usr/local/bin/perl
    2 
    3 ## This file is profUploadClasslistFile.pl
    4 ## It provides access to utilities for uploading classlist files
    5 ##
    6 
    7 ####################################################################
    8 # Copyright @ 1995-1998 University of Rochester
    9 # All Rights Reserved
   10 ####################################################################
   11 
   12 use lib '.'; use webworkInit; # WeBWorKInitLine
   13 use CGI qw(:standard);
   14 use File::Copy;
   15 use Global;
   16 use Auth;
   17 use strict;
   18 
   19 my $query =new CGI;
   20 my %inputs = $query->Vars();
   21 # get information from CGI inputs  (see also below for additional information)
   22 
   23 my $Course      = $query->param('course');
   24 my $User      = $query->param('user');
   25 my $Session_key   = $query->param('key');
   26 
   27 my @array = %inputs;
   28 # print "inputs are @array";
   29 
   30 # verify that information has been received
   31 unless($Course && $User && $Session_key) {
   32     &wwerror("$0","The script did not receive the proper input data.","","");
   33 }
   34 
   35 
   36 
   37 
   38 # establish environment for this script
   39 
   40 &Global::getCourseEnvironment($inputs{'course'});
   41 
   42 
   43 my $cgiURL          = getWebworkCgiURL;
   44 my $databaseDirectory     = getCourseDatabaseDirectory;
   45 my $htmlURL             = getCourseHtmlURL;
   46 my $scriptDirectory       = getWebworkScriptDirectory;
   47 my $templateDirectory     = getCourseTemplateDirectory;
   48 my $DAT                     = getDat;
   49 my $dd                      = getDirDelim;
   50 
   51 require "${scriptDirectory}$Global::DBglue_pl";
   52 require "${scriptDirectory}$Global::FILE_pl";
   53 require "${scriptDirectory}$Global::HTMLglue_pl";
   54 
   55 # log access
   56 &Global::log_info('', query_string);
   57 
   58 
   59 my $permissionsFile = &Global::getCoursePermissionsFile($inputs{'course'});
   60 my $permissions = &get_permissions($inputs{'user'}, $permissionsFile);
   61 my $keyFile = &Global::getCourseKeyFile($inputs{'course'});
   62 
   63 #verify session key
   64 &verify_key($inputs{'user'}, $inputs{'key'}, $keyFile, $inputs{'course'});
   65 
   66 # verify permissions are correct
   67 if ($permissions != $Global::instructor_permissions ) {
   68     print "permissions = $permissions instructor_permissions = $Global::instructor_permissions\n";
   69     print &html_NO_PERMISSION;
   70     exit(0);
   71 }
   72 # get the rest of the information from the submitted form
   73 
   74 
   75 my $overWrite     = $inputs{'overWrite'};
   76 $overWrite = 0 unless defined $overWrite;
   77 my $format        = $inputs{'format'};
   78 my $backUp        = $inputs{'backUp'};
   79 $backUp = 0 unless defined $backUp;
   80 my $fileHandle    = $query->upload('upload');
   81 my $fileName;
   82 
   83 if ($fileHandle =~ /([^\/\\]+)$/) {
   84   $fileName = $1;
   85   $fileName =~ s/^\.+//;
   86 }
   87 else {
   88   wwerror($0, "The fileName $fileHandle is invalid.");
   89 }
   90 
   91 my $ext = 'lst';
   92 my $ext1 = 'html';
   93 if ($format eq 'tab') {$ext ='txt';}
   94 elsif ($format eq 'html') {$ext ='htm';}
   95 
   96 if ($format eq 'csv') {
   97     unless (($fileName =~ /\.lst$/) or ($fileName =~ /\.csv$/)) {
   98       wwerror($0, "The fileName $fileHandle is invalid. Only files with fileNames ending in .lst or .csv can be uploaded");
   99     }
  100 }
  101 
  102 elsif ($format eq 'tab') {
  103     unless ($fileName =~ /\.$ext$/){
  104       wwerror($0, "The fileName $fileHandle is invalid. Only files with fileNames ending in .txt can be uploaded");
  105     }
  106 }
  107 
  108 else {
  109     unless (($fileName =~ /\.$ext$/) or ($fileName =~ /.$ext1$/)){
  110       wwerror($0, "The fileName $fileHandle is invalid. Only files with fileNames ending in .$ext or in
  111       .$ext1 can be uploaded");
  112     }
  113 }
  114 
  115 ## get correct filename
  116 my $shortFileName = $fileName;
  117 $shortFileName      =~  s|\..*||;      ## remove extension
  118 my $saveFileName = "${shortFileName}\.lst";      ## make sure filename had lst extension
  119 
  120 ## check for overwrite mode
  121 if ((-e "${templateDirectory}$saveFileName") and ($overWrite != 1)) {
  122     &overWriteError();
  123     exit;
  124 }
  125 
  126 
  127     ## check for validity of format before saving file
  128 my $i=1;
  129 while(-e "${templateDirectory}temp$i.lst") {$i++;}      ##don't overwrite existing files
  130 my $tempFileName ="${templateDirectory}temp$i.lst";
  131 
  132 open(OUTFILE, ">$tempFileName") or wwerror ($0, "Can't open $tempFileName File for Writing.");
  133 
  134 ## read the file and save it to a temporary file.
  135 ## use bare block so that we can use  symbolic ref's under strict
  136     {
  137     no strict 'refs';
  138     $| = 1;
  139     my $Buffer;
  140     undef $Buffer;
  141     while (read($fileHandle,$Buffer,1024)) {
  142         print OUTFILE $Buffer;
  143     }
  144     close($fileHandle);
  145     close(OUTFILE);
  146 }
  147 
  148 
  149 ## convert file to comma delimited form if necessary
  150 if ($format eq 'tab') {
  151     open(INFILE, "$tempFileName") or wwerror ($0, "Can't open $tempFileName File for Reading.");
  152     my @stringArray = <INFILE>;
  153     close(INFILE);
  154     my $string = join '', @stringArray;
  155 
  156     #convert newlines of original machine to newline of webwork machine
  157     $string =~ s/\015?\012|\015/\n/g;
  158 
  159     $string =~ s|\t|,|g;
  160     $string =~ s|_TAB_|\t|g;
  161     open(OUTFILE, ">$tempFileName") or wwerror ($0, "Can't open $tempFileName File for Writing.");
  162     print OUTFILE $string;
  163     close(OUTFILE);
  164 }
  165 elsif ($format eq 'html') {
  166     open(INFILE, "$tempFileName") or wwerror ($0, "Can't open $tempFileName File for Reading.");
  167     my @stringArray = <INFILE>;
  168     close(INFILE);
  169     my $string = join '', @stringArray;
  170 
  171     #convert newlines of original machine to newline of webwork machine
  172     $string =~ s/\015?\012|\015/\n/g;
  173 
  174     my $workingString = &htmlPage2htmlTable($string);
  175     my $outString = &htmlTable2delim($workingString);
  176     open(OUTFILE, ">$tempFileName") or wwerror ($0, "Can't open $tempFileName File for Writing.");
  177     print OUTFILE $outString;
  178     close(OUTFILE);
  179 }
  180 
  181 my $msg = htmlCheckClasslistFile($Global::noOfFieldsInClasslist,$tempFileName);
  182             ## remove temp file
  183 if ($msg eq 'OK') {
  184     if ($backUp and (-e "${templateDirectory}$saveFileName")) {
  185         &backupFile("${templateDirectory}$saveFileName");   ##make up to three backups
  186     }
  187     copy("$tempFileName" , "${templateDirectory}$saveFileName") or &wwerror($0, "Could not write to the file
  188       ${templateDirectory}$saveFileName .  The uploaded file has been saved as $tempFileName");
  189   chmod($Global::classlist_file_permission, "${templateDirectory}$saveFileName") or
  190       &wwerror("$0","Can't do chmod($Global::classlist_file_permission, ${templateDirectory}$saveFileName)","","", query_string());
  191     chown(-1,$Global::numericalGroupID,"${templateDirectory}$saveFileName") or
  192       &wwerror("$0","Can't do chown(-1,$Global::numericalGroupID,${templateDirectory}$saveFileName)","","", query_string());
  193 
  194     unlink $tempFileName or &wwerror($0, "Could not remove the file $tempFileName.");
  195     &uploadSuccess("${templateDirectory}$saveFileName");
  196 }
  197 else {
  198     $msg .= "<BR>GO BACK AND CORRECT YOUR FILE $fileHandle. <BR><strong>FILE NOT SAVED.</strong>";
  199     unlink $tempFileName or &wwerror($0, "Could not remove the file $tempFileName. <BR> Also $msg");
  200     &wwerror("$0","$msg","","", query_string());
  201 }
  202 
  203 sub overWriteError {
  204     print"content-type: text/html\n\n<H2>No changes made:  You are not in overwrite mode</H2>\n";
  205     print "The file ${templateDirectory}$saveFileName already exists.  If you want to overwrite this file,
  206       use your browser's BacK Button to return to the previous page and check the overwrite check box or<BR>";
  207     print "<FORM METHOD=POST ACTION=\"${cgiURL}profClasslist.pl\"><P>";
  208     print &sessionKeyInputs(\%inputs);
  209     print <<"ENDOFHTML";
  210     <INPUT TYPE=SUBMIT VALUE="Return to Classlist Page">
  211     </FORM>
  212 ENDOFHTML
  213     print &htmlBOTTOM("profUploadClasslistFile.pl", \%inputs);
  214 }
  215 
  216 sub uploadSuccess {
  217   my ($savedFile) = @_;
  218   print"content-type: text/html\n\n<H2>Success, file saved. </H2>\n";
  219   print "Your file has been uploaded to the file:<BR>$savedFile";
  220   print "<FORM METHOD=POST ACTION=\"${cgiURL}profClasslist.pl\"><P>";
  221   print &sessionKeyInputs(\%inputs);
  222 print <<"ENDOFHTML";
  223 <INPUT TYPE=SUBMIT VALUE="Return to Classlist Page">
  224 </FORM>
  225 ENDOFHTML
  226   print &htmlBOTTOM("profUploadClasslistFile.pl", \%inputs);
  227 }
  228 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9