Location: PHPKode > projects > Whiteboard > whiteboard1.0.3/program/restricted/submit.php
<?php
/* Whiteboard Courseware System v1.0.3
   Copyright (C) 2002-2003 Todd Templeton

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 2 of the License, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
   more details.

   You should have received a copy of the GNU General Public License along with
   this program; if not, write to the Free Software Foundation, Inc., 59 Temple
   Place, Suite 330, Boston, MA 02111-1307 USA

   Contact info: my paper address changes often, but you can contact me at
   hide@address.com
*/


define ("MODULE", "submit");
define ("PATHTOHTTPROOT", "../");

unset ($page);

require (PATHTOHTTPROOT . "include/include.php");
require (PATHTOHTTPROOT . "include/submitinclude.php");
require (PATHTOHTTPROOT . "include/gradesinclude.php");


/* recovers original filename from filename appended with "#" and UNIX
     timestamp
   returns original filename
   $filename is file system filename
*/
function realfilename ($filename) {
  $filename = explode ("#", $filename);
  $realfilename = "";
  for ($i = 0; $i < (count ($filename) - 1); $i++) {
    if ($i != 0)
      $realfilename .= "#";
    $realfilename .= $filename[$i];
  }
  return $realfilename;
}


/* prints required files table
   returns nothing
   $requiredfiles is an associative array indexed by filename containing
     true/false (indicating whether file has been submitted)
   $allowcompile is whether to show the "Run Script" button
*/
function requiredfilestable ($requiredfiles, $allowcompile) {
  $table['title'] = "Required Files";
  $table['numcolumns'] = 3;
  if ($allowcompile) {
    $table['form']['address'] = "submit.php";
    $table['form']['method'] = "POST";
    $table['form']['hidden'] = hiddenelements ();
    $table['form']['buttons'][0]['name'] = "action";
    $table['form']['buttons'][0]['value'] = "Run Script";
  }
  $table['headings'][0]['text'] = "File Name";
  $table['headings'][2]['text'] = "Status";
  if (is_array ($requiredfiles) && (count ($requiredfiles) > 0)) {
    ksort ($requiredfiles);
    reset ($requiredfiles);
    while ($elementinfo = each ($requiredfiles)) {
      $row[0]['text'] = $elementinfo['key'];
      $row[1]['text'] = "";
      $row[2]['align'] = "center";
      if ($elementinfo['value'])
        $row[2]['text'] = imagetag ("yes.gif");
      else
        $row[2]['text'] = imagetag ("no.gif");
      $table['rows'][] = $row;
    }
  }
  else {
    $table['rows'][0][0]['colspan'] = 3;
    $table['rows'][0][0]['text'] = "<em>No Required Files</em>";
  }
  prnt ("<tr><td>\n");
  prnt (printtable ($table));
  prnt ("</td></tr>\n");
}


/* prints file upload boxes
   returns nothing
   $numuploads is the number of upload boxes
*/
function fileuploadboxes ($numuploads) {
  global $submitinfo;
  $table['title'] = "File Upload";
  if ($numuploads < 2)
    $numcolumns = 1;
  else
    $numcolumns = 2;
  $table['numcolumns'] = $numcolumns;
  if ($numuploads > 0) {
    $table['form']['hidden'] = hiddenelements ();
    $button['name'] = "action";
    $button['value'] = "Submit";
    $table['form']['buttons'][] = $button;
    if ($submitinfo['morenexttime']) {
      $button['name'] = "action";
      $button['value'] = "More Next Time";
      $table['form']['buttons'][] = $button;
    }
    unset ($button['name']);
    $button['reset'] = true;
    $button['value'] = "Reset Form";
    $table['form']['buttons'][] = $button;
    $table['form']['address'] = "submit.php";
    $table['form']['method'] = "POST";
    $string = "<input type=\"file\" name=\"uploadfile[]\" size=20>";
    for ($i = 0; $i < $numuploads; $i++) {
      $row = $i / $numcolumns;
      $col = $i % $numcolumns;
      $table['rows'][$row][$col]['text'] = $string;
    }
  }
  else
    $table['rows'][0][0]['text'] = "<em>No File Uploads Permitted</em>";
  prnt ("<tr><td>\n");
  prnt (printtable ($table));
  prnt ("</td></tr>\n");
}


/* moves text file, converts all newlines to UNIX newlines
   $oldpath is the old path
   $newpath is the new path
   $replacetabs is whether to replace tabs
*/
function movetextfile ($oldpath, $newpath, $replacetabs) {
  global $submitinfo;
  $tabreplace = "";
  for ($i = 0; $i < $submitinfo['tabspace']; $i++)
    $tabreplace .= " ";
  if (file_exists ($newfile)) {
    $errormsg = "Unable to create new file.";
    $detailedmsg = "'" . $newpath . "' already exists";
    fatalerror ($errormsg, $detailedmsg);
  }
  if (is_bool ($newfile = @fopen ($newpath, "w"))) {
    $errormsg = "Unable to create new file.";
    $detailedmsg = "trying to create '" . $newpath . "'";
    fatalerror ($errormsg, $detailedmsg);
  }
  $text = @file ($oldpath);
  $textlines = count ($text);
  for ($i = 0; $i < $textlines; $i++) {
    $text[$i] = str_replace ("\r\n", "\n", $text[$i]);
    $text[$i] = str_replace ("\r", "\n", $text[$i]);
    if ($replacetabs)
      $text[$i] = str_replace ("\t", $tabreplace, $text[$i]);
    if (!@fwrite ($newfile, $text[$i])) {
      $errormsg = "Unable to write to new file.";
      $detailedmsg = "trying to write to '" . $newpath . "'";
      fatalerror ($errormsg, $detailedmsg);
    }
  }
  @fclose ($newfile);
  if (!file_exists ($newpath)) {
    $errormsg = "Unable to move text file.";
    $detailedmsg = "trying to move '" . $oldpath . "' to '" . $newpath . "'";
    fatalerror ($errormsg, $detailedmsg);
  }
}


/* determines whether to replace tabs
   returns true/false
   $filename is a filename
   $notabreplacement is an associative array indexed by filename (may also be
     Perl-style regular expression); * means all (if only * set, don't replace
     any tabs; if * and others set, only replace tabs on ones that are set)
*/
function replacetabs ($filename, $notabreplacement) {
  $invert = false;
  $foundmatch = false;
  global $submitinfo;
  if (!isposinteger ($submitinfo['tabspace']))
    return false;
  if (!is_array ($notabreplacement))
    return true;
  if (isset ($notabreplacement["*"])) {
    if (count ($notabreplacement) == 1)
      return false;
    $invert = true;
  }
  if (isset ($notabreplacement[$filename]))
    $foundmatch = true;
  else {
    reset ($notabreplacement);
    while ($elementinfo = each ($notabreplacement)) {
      if ($elementinfo['key'] == "*")
	continue;
      if (preg_match ("/" . $elementinfo['key'] . "/", $filename)) {
        $foundmatch = true;
	break;
      }
    }
  }
  if (($foundmatch && !$invert) || (!$foundmatch && $invert))
    return false;
  return true;
}


/* submits files
   returns true/false (whether any new files successfully submitted)
   $filespath is the path to the user's submitted files
   $oldfilespath is the path to the user's deleted files
   $uploadfile is a flat array of upload file paths
   $uploadfile_name is a flat array of upload file names
   $usedspace is the current space used (pass by reference)
   $submitlogfile is the submit log file
   $mailtext is the current confirmation e-mail text (pass by reference)
*/
function submitfiles ($filespath, $oldfilespath, $uploadfile, $uploadfile_name, $usedspace, $submitlogfile, $notabreplacement, $mailtext) {
  global $submitinfo;
  global $assignmentinfo;
  global $time;
  global $dateandtime;
  global $netid;
  if (!is_array ($uploadfile))
    return false;
  $retval = false;
  for ($i = 0; $i < count ($uploadfile); $i++) {
    if (!isset ($uploadfile_name[$i]) || isemptystring ($uploadfile_name[$i]))
      continue;
    if (!is_file ($uploadfile[$i]))
      inputerror ("Uploaded file does not exist.");
    if (isset ($submittedfiles[$uploadfile_name[$i]])) {
      $errormsg = "Uploaded more than one file named";
      $errormsg .= " '" . $uploadfile_name[$i] . "'";
      registererror ($errormsg, 0);
      @unlink ($uploadfile[$i]);
      continue;
    }
    $filesize = filesize ($uploadfile[$i]);
    $oldfile = $filespath . "/" . $uploadfile_name[$i];
    if (file_exists ($oldfile)) {
      $oldfilesize = filesize ($oldfile);
      $oldfileexists = true;
    }
    else {
      $oldfilesize = 0;
      $oldfileexists = false;
    }
    if (isposinteger ($submitinfo['maxspace'])) {
      if (($usedspace + $filesize - $oldfilesize) > $submitinfo['maxspace']) {
        $errormsg = "Size of file '" . $uploadfile_name[$i] . "'";
        $errormsg .= " exceeds remaining space.";
        registererror ($errormsg, 0);
        @unlink ($uploadfile[$i]);
        continue;
      }
    }
    if ($oldfileexists) {
      if ($submitinfo['savedeletedfiles']) {
        $deletedfile = $oldfilespath . "/" . $uploadfile_name[$i] . "#" . $time;
        if (!movefile ($oldfile, $deletedfile)) {
	  $errormsg = "Unable to move existing file with same name to backup";
          $errormsg .= " directory.";
	  $detailedmsg = "trying to move '" . $oldfile . "'";
          $detailedmsg .= " to '" . $deletedfile . "'";
          fatalerror ($errormsg, $detailedmsg);
        }
        if (!$submitinfo['managedeletedfiles']) {
          $usedspace -= $oldfilesize;
          $oldfilesize = 0;
        }
      }
      else {
        if (!@unlink ($oldfile)) {
	  $errormsg = "Unable to remove existing file with same name.";
	  $detailedmsg = "trying to remove '" . $oldfile . "'";
	  fatalerror ($errormsg, $detailedmsg);
        }
      }
    }
    $newfile = $filespath . "/" . $uploadfile_name[$i];
    $uploadsuccessful = false;
    if (istextfile ($uploadfile[$i])) {
      $replacetabs = replacetabs ($uploadfile_name[$i], $notabreplacement);
      movetextfile ($uploadfile[$i], $newfile, $replacetabs);
      $filesize = @filesize ($filespath . "/" . $uploadfile_name[$i]);
    }
    else if (!@copy ($uploadfile[$i], $newfile)) {
      $errormsg = "Unable to upload file.";
      $detailedmsg = "trying to copy '" . $uploadfile[$i] . "'";
      $detailedmsg .= " to '" . $newfile . "'";
      fatalerror ($errormsg, $detailedmsg);
    }
    $usedspace += $filesize;
    if (!$submitinfo['managedeletedfiles'] && $oldfileexists)
      $usedspace -= $oldfilesize;
    $submittedfiles[$uploadfile_name[$i]] = true;
    if (!is_bool ($submitlogfile)) {
      $data = $dateandtime . ": " . $uploadfile_name[$i] . " submitted by ";
      $data .= "user " . $netid . " for";
      $data .= " assignment '" . $assignmentinfo['name'] . "'\n";
      if (!appendfile ($submitlogfile, $data)) {
	$errormsg = "Unable to write to log file.";
	$detailedmsg = "trying to write to '" . $submitlogfile . "'";
	fatalerror ($errormsg, $detailedmsg);
      }
    }
    $mailtext .= $uploadfile_name[$i] . "\t" . $filesize . " " . plural ($filesize, "byte", "bytes") . "\n";
    $retval = true;
    if (!@unlink ($uploadfile[$i])) {
      $errormsg = "Unable to delete temporary uploaded file.";
      $detailedmsg = "trying to delete '" . $uploadfile[$i] . "'";
      fatalerror ($errormsg, $detailedmsg);
    }
  }
  return $retval;
}


/* restores files
   returns true/false (whether any new files successfully submitted)
   $filespath is the path to the user's submitted files
   $oldfilespath is the path to the user's deleted files
   $selectedfiles is a flat array of filenames of deleted files
   $usedspace is the current space used (pass by reference)
   $submitlogfile is the submit log file
   $mailtext is the current confirmation e-mail text (pass by reference)
*/
function restorefiles ($filespath, $oldfilespath, $selectedfiles, $usedspace, $submitlogfile, $mailtext) {
  global $time;
  global $dateandtime;
  global $netid;
  global $submitinfo;
  global $assignmentinfo;
  $retval = false;
  for ($i = 0; $i < count ($selectedfiles); $i++) {
    if (!file_exists ($oldfilespath . "/" . $selectedfiles[$i])) {
      $errormsg = "Selected deleted file '" . $selectedfiles[$i] . "'";
      $errormsg .= " does not exist.";
      registererror ($errormsg, 0);
      continue;
    }
    $realfilename = realfilename ($selectedfiles[$i]);
    if (isset ($restoredfiles[$realfilename])) {
      $errormsg = "Trying to restore more than one file named";
      $errormsg .= " '" . $realfilename . "'";
      registererror ($errormsg, 0);
      continue;
    }
    $deletedfile = $oldfilespath . "/" . $selectedfiles[$i];
    $submittedfile = $filespath . "/" . $realfilename;
    $newdeletedfile = $oldfilespath . "/" . $realfilename . "#" . $time;
    $restoredfile = $filespath . "/" . $realfilename;
    $filesize = filesize ($deletedfile);
    $submittedfileexists = false;
    if (file_exists ($submittedfile)) {
      $submittedfileexists = true;
      $submittedfilesize = filesize ($filespath . "/" . $realfilename);
    }
    if ($submittedfileexists) {
      if (!movefile ($submittedfile, $newdeletedfile)) {
	$errormsg = "Unable to move existing file with same name to backup";
        $errormsg .= " directory.";
	$detailedmsg = "trying to move '" . $submittedfile . "'";
        $detailedmsg .= " to '" . $newdeletedfile . "'";
        fatalerror ($errormsg, $detailedmsg);
      }
    }
    if (!movefile ($deletedfile, $restoredfile)) {
      $errormsg = "Unable to restore file.";
      $detailedmsg = "trying to move '" . $deletedfile . "'";
      $detailedmsg .= " to '" . $restoredfile . "'";
      fatalerror ($errormsg, $detailedmsg);
    }
    if (!is_bool ($submitlogfile)) {
      $data = $dateandtime . ": " . $realfilename . " submitted by ";
      $data .= "user " . $netid . " for";
      $data .= " assignment '" . $assignmentinfo['name'] . "'\n";
      if (!appendfile ($submitlogfile, $data)) {
	$errormsg = "Unable to write to log file.";
	$detailedmsg = "trying to write to '" . $submitlogfile . "'";
	fatalerror ($errormsg, $detailedmsg);
      }
    }
    $mailtext .= $realfilename . "\t" . $filesize;
    $mailtext .= " " . plural ($filesize, "byte", "bytes") . "\n";
    $restoredfiles[$realfilename] = true;
    $retval = true;
  }
  return $retval;
}


/* deletes files
   returns nothing
   $filespath is the path to selected files
   $backupfilespath is the path to which to back up deleted files (bool for
     none)
   $selectedfiles is a flat array of filenames in the $filespath directory
   $usedspace is the current space used (pass by reference)
   $submitlogfile is the submit log file
*/
function deletefiles ($filespath, $backupfilespath, $selectedfiles, $usedspace, $submitlogfile) {
  global $submitinfo;
  global $assignmentinfo;
  global $dateandtime;
  global $time;
  global $netid;
  for ($i = 0; $i < count ($selectedfiles); $i++) {
    $oldfile = $filespath . "/" . $selectedfiles[$i];
    if (!file_exists ($oldfile)) {
      $errormsg = "Selected file '" . $selectedfiles[$i] . "' does not exist.";
      registererror ($errormsg, 0);
      continue;
    }
    $filesize = filesize ($oldfile);
    if ($submitinfo['savedeletedfiles'] && !is_bool ($backupfilespath)) {
      $deletedfile = $backupfilespath . "/" . $selectedfiles[$i] . "#" . $time;
      if (!@copy ($oldfile, $deletedfile)) {
	$errormsg = "Selected file '" . $selectedfiles[$i] . "' could not be";
        $errormsg .= " backed up.";
	$detailedmsg = "trying to copy '" . $oldfile . "'";
        $detailedmsg .= " to '" . $newfile . "'";
        fatalerror ($errormsg, $detailedmsg);
      }
    }
    if (!@unlink ($oldfile)) {
      $errormsg = "Unable to delete selected file";
      $errormsg .= " '" . $selectedfiles[$i] . "'";
      $detailedmsg = "trying to delete '" . $oldfile . "'";
      fatalerror ($errormsg, $detailedmsg);
    }
    if (!is_bool ($submitlogfile)) {
      $data = $dateandtime . ": " . $selectedfiles[$i] . " deleted by ";
      $data .= "user " . $netid . " for";
      $data .= " assignment '" . $assignmentinfo['name'] . "'\n";
      if (!appendfile ($submitlogfile, $data)) {
	$errormsg = "Unable to write to log file.";
	$detailedmsg = "trying to write to '" . $submitlogfile . "'";
	fatalerror ($errormsg, $detailedmsg);
      }
    }
    if (!$submitinfo['managedeletedfiles'])
      $usedspace -= $filesize;
  }
}


/* checks number of characters per line in text files in a directory (registers
     errors to show up on page)
   returns nothing
   $dirpath is the directory path
   $maxcolumncount is the maximum number of characters per line allowed
*/
function checkfilescolumncount ($dirpath, $maxcolumncount) {
  if (is_bool ($dir = @opendir ($dirpath))) {
    $errormsg = "Unable to read submissions directory.";
    $detailedmsg = "trying to open directory '" . $dirpath . "'";
    fatalerror ($errosmg, $detailedmsg);
  }
  rewinddir($dir);
  while ($file = readdir($dir)) {
    if (is_file ($dirpath . "/" . $file) && istextfile ($dirpath . "/" . $file)) {
      $text = @file ($dirpath . "/" . $file);
      for ($i = 0; $i < count ($text); $i++) {
        if (strlen ($text[$i]) > $maxcolumncount) {
          $errormsg = $file . " has more than " . $maxcolumncount . " ";
          $errormsg .= plural ($maxcolumncount, "character", "characters");
          $errormsg .= " per line";
          registererror ($errormsg, 1);
          break;
        }
      }
    }
  }
}


/* runs test script on submitted files (prints script output)
   returns nothing
   $scriptfile is the full path of the test script
   $filespath is the submitted files directory
*/
function compilesubmissions ($scriptfile, $filespath) {
  global $netid;
  if (is_bool ($temppath = tempnam (TEMPDIR, "cmple"))) {
    $errormsg = "Unable to create temp directory name.";
    $detailedmsg = "trying to create temp name in '" . TEMPDIR . "'.";
    fatalerror ($errormsg, $detailedmsg);
  }
  if (!@unlink ($temppath)) {
    $errormsg = "Unable to delete temp file.";
    $detailedmsg = "trying to delete '" . $temppath . "'";
    fatalerror ($errormsg, $detailedmsg);
  }
  if (!@mkdir ($temppath, 0770)) {
    $errormsg = "Unable to create temp directory.";
    $detailedmsg = "trying to create '" . $temppath . "'";
    fatalerror ($errormsg, $detailedmsg);
  }
  prnt ("<tr><td>\n");
  prnt ("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");
  prnt ("<tr><th align=\"left\" class=\"tabletitle\">Script Messages:</th></tr>\n");
  $command = $scriptfile . " " . escapeshellarg ($netid);
  $command .= " " .  escapeshellarg ($filespath);
  $command .= " " . escapeshellarg ($temppath);
  @exec ($command, $text);
  $textlines = count ($text);
  if ($textlines > 0) {
    prnt ("<tr><td><pre>\n");
    for ($i = 0; $i < $textlines; $i++) {
      $text[$i] = chop ($text[$i]);
      $text[$i] = htmlspecialchars ($text[$i], ENT_QUOTES);
      prnt ($text[$i] . "\n");
    }
    prnt ("</pre></td></tr>\n");
  }
  else
    prnt ("<tr><td class=\"tabledata\"><em>No Output</em></td></tr>\n");
  prnt ("</table>\n");
  prnt ("</td></tr>\n");
  $command = RMCOMMAND . " -rf ";
  $command .= escapeshellarg ($temppath);
  @exec ($command);
  if (file_exists ($temppath)) {
    $errormsg = "Unable to delete temp directory.";
    $detailedmsg = "trying to delete '" . $temppath . "'";
    fatalerror ($errormsg, $detailedmsg);
  }
}


if (isemptystring ($netid)) {
  $options = getdefaults ($courseconfigoptions);
  loginpage (courselink ("submit.php", false));
}
if (isemptystring ($course))
  inputerror ("Unknown course.");
if (!$priv['isstudent'])
  inputerror ("You do not have permission to access this module.");
$options = getcourseoptions ($department, $coursenumber);
if (!is_array ($options)) {
  $errormsg = "No course options available.";
  fatalerror ($errormsg, false);
}
$illegal = false;
if (!isset ($assignment) || isemptystring ($assignment))
  $illegal = true;
else if (!is_array ($assignmentinfo = getassignmentinfo ($assignment)))
  $illegal = true;
else if ($assignmentinfo['allow_submission'] == 0)
  $illegal = true;
if ($illegal) {
  $assignments = getassignments ();
  $categories = getcategories ();
  $orderedassignments = makeorderedassignments ($assignments, $categories);
  if (is_array ($orderedassignments)  && (count ($orderedassignments) == 1)) {
    $assignment = $orderedassignments[0]['assignment'];
    $assignmentinfo = $orderedassignments[0];
  }
  else {
    $page['title'] = "Assignment Submission";
    $page['browsertitle'] = "Assignment Submission";
    if (!is_array ($orderedassignments) || (count ($orderedassignments) <= 0))
      prnt ("<tr><td class=\"pagetext\">No Assignments</td></tr>\n");
    else
      assignmentbox ($orderedassignments);
    printpage ($page);
    exit ();
  }
}
if (!is_array ($submitinfo = getsubmitinfo ($assignment))) {
  $errormsg = "No submission options available.";
  $detailedmsg = "assignment '" . $assignment . "'";
  fatalerror ($errormsg, $detailedmsg);
}
/*managedeletedfiles option is dependent on savedeletedfiles option*/
if (!$submitinfo['savedeletedfiles'])
  $submitinfo['managedeletedfiles'] = false;
/*path to user's submitted files*/
$filespath = SUBMISSIONSDIR . "/" . $netid . "/" . $assignment;
$oldfilespath = $filespath . "/old";      /*path to user's deleted files*/
/*path to late log file*/
$latelogfilepath = COURSEDIR . "/" . "latesubmissions.log";
/*path to late log file*/
$assignmentlogfilepath = ASSIGNMENTSDIR . "/" . $assignment .  "/submit.log";
/*path to assignment compile script*/
$scriptfilepath = ASSIGNMENTSDIR . "/" . $assignment .  "/submit.scr";
/*comfirmation e-mail address*/
$mailaddress = $netid .  "@" . EMAILDOMAIN;
if (!is_dir (SUBMISSIONSDIR . "/" . $netid)) {
  if (!makedirectory (SUBMISSIONSDIR, $netid, 0770)) {
    $errormsg = "Unable to create user directory.";
    $detailedmsg = "trying to create";
    $detailedmsg .= " '" . SUBMISSIONSDIR . "/" . $netid . "'";
    fatalerror ($errormsg, $detailedmsg);
  }
}
/*determine if submitted files directory exists, create it if not*/
if (!is_dir ($filespath)) {
  if (!@mkdir ($filespath, 0770)) {
    $errormsg = "Unable to create submissions directory.";
    $detailedmsg = "trying to create '" . $filespath . "'";
    fatalerror ($errormsg, $detailedmsg);
  }
}
/*determine if deleted files directory exists, create it if not*/
if ($submitinfo['savedeletedfiles']) {
  if (!is_dir ($oldfilespath)) {
    if (!@mkdir ($oldfilespath, 0770)) {
      $errormsg = "Unable to created deleted files directory.";
      $detailedmsg = "trying to create '" . $oldfilespath . "'";
      fatalerror ($errormsg, $detailedmsg);
    }
  }
}
/*determine size of submitted files*/
$usedspace = dirsize ($filespath, false);
if (is_bool ($usedspace) && !$usedspace) {
  $errormsg = "Unable to determine size of submitted files.";
  $detailedmsg = "Trying to get size of '" . $filespath . "'";
  fatalerror ($errormsg, $detailedmsg);
}
/*add size of deleted files*/
if ($submitinfo['managedeletedfiles']) {
  $usedspace += dirsize ($oldfilespath, false);
  if (is_bool ($usedspace) && !$usedspace) {
    $errormsg = "Unable to determine size of deleted files.";
    $detailedmsg = "Trying to get size of '" . $oldfilespath . "'";
    fatalerror ($errormsg, $detailedmsg);
  }
}
/*determine if test script exists*/
if (is_executable ($scriptfilepath))
  $compilescriptexists = true;
else
  $compilescriptexists = false;
$islate = false;
$isingraceperiod = false;
$filedeadline = false;
if (isposnzinteger ($submitinfo['deadlineconv'])) {
  if ($time > $submitinfo['deadlineconv'])
    $islate = true;
  $filedeadline = $submitinfo['deadlineconv'];
  if (isposinteger ($submitinfo['graceperiodconv'])) {
    $filedeadline += $submitinfo['graceperiodconv'];
    if ($time <= $submitinfo['graceperiodconv'])
      $isingraceperiod = true;
  }
}
if ($islate && !$isingraceperiod && $submitinfo['loglatesubmissions']) {
  if (!is_writable ($latelogfilepath)) {
    if (!is_writable (COURSEDIR)) {
      $errormsg = "Late submissions log file";
      $errormsg = " does not exist and cannot be created.";
      $detailedmsg = "file '" . $latelogfilepath . "'";
      fatalerror ($errormsg, $detailedmsg);
    }
  }
  $submitlogfile = $latelogfilepath;
}
else if ($submitinfo['logsubmissions']) {
  if (!is_writable ($assignmentlogfilepath)) {
    if (!is_writable (ASSIGNMENTSDIR . "/" . $assignment)) {
      $errormsg = "Submissions log file";
      $errormsg = " does not exist and cannot be created.";
      $detailedmsg = "file '" . $assignmentlogfilepath . "'";
      fatalerror ($errormsg, $detailedmsg);
    }
  }
  $submitlogfile = $assignmentlogfilepath;
}
else
  $submitlogfile = false;
$notabreplacement = false;
if (is_array ($submitinfo['notabreplacementconv'])) {
  if (count ($submitinfo['notabreplacementconv']) > 0) {
    reset ($submitinfo['notabreplacementconv']);
    while ($elementinfo = each ($submitinfo['notabreplacementconv'])) {
      if (!isemptystring ($elementinfo['value']))
        $notabreplacement[$elementinfo['value']] = true;
    }
  }
}
$requiredfiles = false;
if (is_array ($submitinfo['requiredfilesconv'])) {
  if (count ($submitinfo['requiredfilesconv']) > 0) {
    reset ($submitinfo['requiredfilesconv']);
    while ($elementinfo = each ($submitinfo['requiredfilesconv'])) {
      if (!isemptystring ($elementinfo['value']))
        $requiredfiles[$elementinfo['value']] = false;
    }
  }
}
/*begin confirmation e-mail*/
$mailsubject = $course . ", " . semester2string ($semester);
$mailsubject .= ": '" . $assignmentinfo['name'] . "'";
$mailsubject .= " File Submission Confirmation";
if (!isset ($numuploads) || !isposinteger ($numuploads))
  $numuploads = $submitinfo['defaultuploads'];
$sendmail = false;
$mailtext = "NetID:\t" . $netid . "\nCourse:\t" . $course . "\n";
$mailtext .= "Date:\t" . $dateandtime;
if ($islate && (!$isingraceperiod || $submitinfo['lateingraceperiod']))
  $mailtext .= " (late)";
$mailtext .= "\n\n";
if (!isemptystring ($submitinfo['mailmessage']))
  $mailtext .= "\n" . $submitinfo['mailmessage'] . "\n\n";
$mailtext .= "\nNewly submitted files for '" . $assignmentinfo['name'] . "':\n";
/*delete selected files*/
if ($action == "Delete") {
  if (defined ("DEMOMODE") && DEMOMODE && !$priv['isnonstudent'])
    inputerror ("File deletion is disabled.");
  if ($semester != $currentsemester)
    inputerror ("Submission changes only allowed for current semester.");
  deletefiles ($filespath, $oldfilespath, $selectedfiles, &$usedspace, $submitlogfile);
}
/*permanently delete selected deleted files*/
if ($action == "Permanent Delete") {
  if (defined ("DEMOMODE") && DEMOMODE && !$priv['isnonstudent'])
    inputerror ("Permanent file deletion is disabled.");
  if (!$submitinfo['managedeletedfiles'])
    inputerror ("Assignment does not allow users to manage deleted files.");
  if ($semester != $currentsemester)
    inputerror ("Submission changes only allowed for current semester.");
  deletefiles ($oldfilespath, false, $selectedfiles, &$usedspace, false);
}
/*restore selected deleted files*/
if ($action == "Restore") {
  if (defined ("DEMOMODE") && DEMOMODE && !$priv['isnonstudent'])
    inputerror ("File restoration is disabled.");
  if (!$submitinfo['managedeletedfiles'])
    inputerror ("Assignment does not allow users to manage deleted files.");
  if ($semester != $currentsemester)
    inputerror ("Submission changes only allowed for current semester.");
  $sendmail = restorefiles ($filespath, $oldfilespath, $selectedfiles, &$usedspace, $submitlogfile, &$mailtext);
}
/*submit files*/
if (($action == "Submit") || ($action == "More Next Time")) {
  if (defined ("DEMOMODE") && DEMOMODE && !$priv['isnonstudent'])
    inputerror ("File submission is disabled.");
  if ($submitinfo['defaultuploads'] <= 0)
    inputerror ("No file uploads permitted for this assignment.");
  if ($action == "More Next Time") {
    if (!$submitinfo['morenexttime'])
      inputerror ("Assignment does not allow 'More Next Time'.");
    $numuploads += $submitinfo['defaultuploads'];
  }
  if ($semester != $currentsemester)
    inputerror ("Submission changes only allowed for current semester.");
  $sendmail = submitfiles ($filespath, $oldfilespath, $uploadfile, $uploadfile_name, &$usedspace, $submitlogfile, $notabreplacement, &$mailtext);
}
if (isposinteger ($submitinfo['maxcolumncount']))
  checkfilescolumncount ($filespath, $submitinfo['maxcolumncount']);
$filesexist = false;
unset ($submittedfiles);
$allrequiredfilessubmitted = dirfilesinfo ($filespath, &$submittedfiles, false, &$mailtext, &$requiredfiles, $filedeadline);
if (is_array ($submittedfiles) && (count ($submittedfiles) > 0))
  $filesexist = true;
$oldfilesexist = false;
unset ($deletedfiles);
if ($submitinfo['managedeletedfiles']) {
  dirfilesinfo ($oldfilespath, &$deletedfiles, true, false, false, false);
  if (is_array ($deletedfiles) && (count ($deletedfiles) > 0))
    $oldfilesexist = true;
}
/*send confirmation e-mail*/
if ($sendmail && $submitinfo['sendmail']) {
  if (!@mail ($mailaddress, $mailsubject, $mailtext)) {
    $errormsg = "Unable to send email confirmation.";
    fatalerror ($errormsg, false);
  }
}
$page['title'] = "Assignment Submission";
$page['subtitle'] = $assignmentinfo['name'];
$page['browsertitle'] = $assignmentinfo['name'] . " - Assignment Submission";
$scripts = "";
if ($filesexist) {
  $scripts .= "function selectall () {\n";
  $scripts .= "  for (var i = 0; i < document.submittedfilesform.elements.length; i++) {\n";
  $scripts .= "    var currentelement = document.submittedfilesform.elements[i];\n";
  $scripts .= "    if (currentelement.type == 'checkbox')\n";
  $scripts .= "      currentelement.checked = true;\n";
  $scripts .= "  }\n";
  $scripts .= "}\n";
}
if ($oldfilesexist) {
  if ($filesexist)
    $scripts .= "\n";
  $scripts .= "function selectallold () {\n";
  $scripts .= "  for (var i = 0; i < document.deletedfilesform.elements.length; i++) {\n";
  $scripts .= "    var currentelement = document.deletedfilesform.elements[i];\n";
  $scripts .= "    if (currentelement.type == 'checkbox')\n";
  $scripts .= "      currentelement.checked = true;\n";
  $scripts .= "  }\n";
  $scripts .= "}\n";
}
if (!isemptystring ($scripts))
  $page['scripts'] = $scripts;
$topdata['field'] = "User:";
$topdata['data'] = $netid;
$page['topdata'][] = $topdata;
if (isposnzinteger ($submitinfo['deadlineconv'])) {
  $topdata['field'] = "Deadline:";
  if ($islate && (!$isingraceperiod || $submitinfo['lateingraceperiod'])) {
    $topdata['data'] = "<font color=\"" . $options['errorcolor'] . "\">";
    $topdata['data'] .= htmlsafe ($submitinfo['deadline']);
    $topdata['data'] .= " (late)</font>";
  }
  else
    $topdata['data'] = htmlsafe ($submitinfo['deadline']);
  $page['topdata'][] = $topdata;
}
if (isposinteger ($submitinfo['maxspace'])) {
  $topdata['field'] = "Size Limit:";
  $topdata['data'] = $submitinfo['maxspace'] . " ";
  $topdata['data'] .= plural ($submitinfo['maxspace'], "byte", "bytes");
  if ($filesexist || $oldfilesexist) 
    $topdata['data'] .= " (" . $usedspace . " " . plural ($usedspace, "byte", "bytes") . " used)";
  else
    $topdata['data'] .= " (0 bytes used)";
  $page['topdata'][] = $topdata;
}
else {
  $topdata['field'] = "Space Used:";
  if ($filesexist || $oldfilesexist) 
    $topdata['data'] = $usedspace . " " . plural ($usedspace, "byte", "bytes");
  else
    $topdata['data'] = "0 bytes";
  $page['topdata'][] = $topdata;
}
$page['message'] = $submitinfo['message'];
unset ($usedspace);
/*test program using provided script*/
if ($compilescriptexists && ($action == "Run Script") && $allrequiredfilessubmitted) {
  if (defined ("DEMOMODE") && DEMOMODE && !$priv['isnonstudent'])
    inputerror ("Submission testing is disabled.");
  compilesubmissions ($scriptfilepath, $filespath);
  prnt (EMPTYTABLEROW . "\n");
}
/*print "Submitted Files" table*/
filetable ($submittedfiles, 0, $semester == $currentsemester, false, false);
/*print "Deleted Files" table*/
if ($submitinfo['managedeletedfiles']) {
  prnt (EMPTYTABLEROW . "\n");
  filetable ($deletedfiles, 1, $semester == $currentsemester, false, false);
}
prnt (EMPTYTABLEROW . "\n");
if (is_bool ($requiredfiles))
  $allrequiredfilessubmitted = true;
$allowcompile = $allrequiredfilessubmitted && $compilescriptexists;
$allowcompile = $allowcompile && $filesexist;
if (defined ("DEMOMODE") && DEMOMODE && !$priv['isnonstudent'])
  $allowcompile = false;
/*print "Required Files" table*/
requiredfilestable ($requiredfiles, $allowcompile);
/*print upload boxes*/
if ($semester == $currentsemester) {
  prnt (EMPTYTABLEROW . "\n");
  fileuploadboxes ($numuploads);
}
printpage ($page);
?>
Return current item: Whiteboard