/** * Processes a submissions * * Called when this component receives an HTTP POST request to * /submission(/). */ public function postSubmission() { $this->app->response->setStatus(201); $body = $this->app->request->getBody(); $submissions = Submission::decodeSubmission($body); // always been an array $arr = true; if (!is_array($submissions)) { $submissions = array($submissions); $arr = false; } $res = array(); foreach ($submissions as $submission) { $fail = false; $process = new Process(); $process->setRawSubmission($submission); $eid = $submission->getExerciseId(); // load processor data from database $result = Request::routeRequest('GET', '/process/exercise/' . $eid, array(), '', $this->_processorDb, 'process'); $processors = null; if ($result['status'] >= 200 && $result['status'] <= 299) { $processors = Process::decodeProcess($result['content']); } else { if ($result['status'] != 404) { $submission->addMessage("Interner Fehler"); $res[] = $submission; $this->app->response->setStatus(409); continue; } } $result2 = Request::routeRequest('GET', '/exercisefiletype/exercise/' . $eid, array(), '', $this->_getExerciseExerciseFileType, 'exercisefiletype'); $exerciseFileTypes = null; if ($result2['status'] >= 200 && $result2['status'] <= 299) { $exerciseFileTypes = ExerciseFileType::decodeExerciseFileType($result2['content']); if (!is_array($exerciseFileTypes)) { $exerciseFileTypes = array($exerciseFileTypes); } $filePath = null; if ($submission->getFile() != null) { $file = $submission->getFile()->getBody(true); if ($file !== null) { $fileHash = sha1($file); $filePath = '/tmp/' . $fileHash; if ($submission->getFile()->getDisplayName() != null) { LProcessor::generatepath($filePath); file_put_contents($filePath . '/' . $submission->getFile()->getDisplayName(), $file); $filePath .= '/' . $submission->getFile()->getDisplayName(); } else { LProcessor::generatepath($filePath); file_put_contents($filePath . '/tmp', $file); $filePath .= '/tmp'; } } } // check file type if ($filePath != null) { $found = false; $types = array(); $mimeType = MimeReader::get_mime($filePath); $foundExtension = isset(pathinfo($filePath)['extension']) ? pathinfo($filePath)['extension'] : '-'; foreach ($exerciseFileTypes as $type) { $types[] = $type->getText(); $type = explode(' ', str_replace('*', '', $type->getText())); //echo MimeReader::get_mime($filePath); if (strpos($mimeType, $type[0]) !== false && (!isset($type[1]) || '.' . $foundExtension == $type[1])) { $found = true; break; } } if (!$found && count($exerciseFileTypes) > 0) { $submission->addMessage("falscher Dateityp \nGefunden: " . $mimeType . " ." . $foundExtension . "\nErlaubt: " . implode(',', $types)); $res[] = $submission; $this->app->response->setStatus(409); unlink($filePath); continue; } unlink($filePath); } } else { if ($result2['status'] != 404) { $submission->addMessage("Interner Fehler"); $res[] = $submission; $this->app->response->setStatus(409); continue; } } // process submission if ($processors !== null) { if (!is_array($processors)) { $processors = array($processors); } foreach ($processors as $pro) { $component = $pro->getTarget(); if ($process->getExercise() === null) { $process->setExercise($pro->getExercise()); } $process->setParameter($pro->getParameter()); $process->setAttachment($pro->getAttachment()); $process->setTarget($pro->getTarget()); $process->setWorkFiles($pro->getWorkFiles()); //echo Process::encodeProcess($process)."_______";// return; $result = Request::post($component->getAddress() . '/process', array(), Process::encodeProcess($process)); //echo $result['content'].'_______'; if ($result['status'] >= 200 && $result['status'] <= 299) { $process = Process::decodeProcess($result['content']); } else { $fail = true; $submission->addMessage("Beim Verarbeiten der Einsendung ist ein Fehler aufgetreten"); if (isset($result['content'])) { $content = Process::decodeProcess($result['content']); $submission->setStatus($content->getStatus()); $submission->addMessages($content->getMessages()); } break; } } } if ($fail) { if (isset($submission)) { $submission->setFile(null); } $res[] = $submission; $this->app->response->setStatus(409); continue; } // upload submission $uploadSubmission = $process->getSubmission(); if ($uploadSubmission === null) { $uploadSubmission = $process->getRawSubmission(); if ($uploadSubmission->getFile() != null) { $file = $uploadSubmission->getFile(); if ($file->getDisplayName() == null) { $file->setDisplayName($submission->getExerciseName()); } } } if ($uploadSubmission !== null) { ///echo Submission::encodeSubmission($uploadSubmission);return; $result = Request::routeRequest('POST', '/submission', array(), Submission::encodeSubmission($uploadSubmission), $this->_submission, 'submission'); //var_dump($result);return; // checks the correctness of the query if ($result['status'] >= 200 && $result['status'] <= 299) { $queryResult = Submission::decodeSubmission($result['content']); $uploadSubmission->setId($queryResult->getId()); $uploadSubmission->setFile($queryResult->getFile()); if ($process->getMarking() !== null) { $process->getMarking()->setSubmission($queryResult); } } else { $uploadSubmission->addMessage("Beim Speichern der Einsendung ist ein Fehler aufgetreten."); //var_dump($uploadSubmission); return; if (isset($result['content'])) { $content = Submission::decodeSubmission($result['content']); $uploadSubmission->setStatus($content->getStatus()); $uploadSubmission->addMessages($content->getMessages()); } $uploadSubmission->setStatus(409); $res[] = $uploadSubmission; $this->app->response->setStatus(409); continue; } } // upload marking if ($process->getMarking() !== null) { //echo Marking::encodeMarking($process->getMarking()); $result = Request::routeRequest('POST', '/marking', array(), Marking::encodeMarking($process->getMarking()), $this->_marking, 'marking'); // checks the correctness of the query if ($result['status'] >= 200 && $result['status'] <= 299) { $queryResult = Marking::decodeMarking($result['content']); } else { $uploadSubmission->addMessage("Beim Speichern der Korrektur ist ein Fehler aufgetreten"); if (isset($result['content'])) { $content = Marking::decodeMarking($result['content']); $uploadSubmission->addMessages($content->getMessages()); } $res[] = $uploadSubmission; $this->app->response->setStatus(409); continue; } } $rr = $process->getSubmission(); if ($rr === null) { $rr = $process->getRawSubmission(); } $res[] = $rr; } if (!$arr && count($res) == 1) { $this->app->response->setBody(Submission::encodeSubmission($res[0])); } else { $this->app->response->setBody(Submission::encodeSubmission($res)); } }
/** * Deletes a file. * * Called when this component receives an HTTP DELETE request to * /file/$a/$b/$c/$file. * * @param string $hash The hash of the file which should be deleted. */ public function deleteFile($callName, $input, $params = array()) { $path = array(self::getBaseDir(), $params['a'], $params['b'], $params['c'], $params['file']); $filePath = implode('/', array_slice($path, 0)); if (strlen($filePath) > 0 && file_exists($this->config['DIR']['files'] . '/' . $filePath)) { // after the successful deletion, we want to return the file data $file = new File(); $file->setAddress($filePath); $file->setFileSize(filesize($this->config['DIR']['files'] . '/' . $filePath)); $file->setHash(sha1_file($this->config['DIR']['files'] . '/' . $filePath)); $file->setMimeType(MimeReader::get_mime($this->config['DIR']['files'] . '/' . $filePath)); // removes the file unlink($this->config['DIR']['files'] . '/' . $filePath); // the removing/unlink process failed, if the file still exists. if (file_exists($this->config['DIR']['files'] . '/' . $filePath)) { return Model::isProblem(new File()); } // the file is removed return Model::isCreated($file); } else { // file does not exist return Model::isProblem(new File()); } }
include_once dirname(__FILE__) . '/../Assistants/Language.php'; include_once dirname(__FILE__) . '/../Assistants/MimeReader.php'; global $globalUserData; Authentication::checkRights(PRIVILEGE_LEVEL::TUTOR, $cid, $uid, $globalUserData); $langTemplate = 'TutorUpload_Controller'; Language::loadLanguageFile('de', $langTemplate, 'json', dirname(__FILE__) . '/'); if (isset($_POST['action']) && $_POST['action'] == 'TutorUpload') { if (isset($_FILES['MarkingFile'])) { $file = $_FILES['MarkingFile']; $error = $file['error']; if ($error == 0) { $filePath = $file['tmp_name']; $displayName = $file['name']; $type = $file["type"]; // checks file ending if (MimeReader::get_mime($filePath) == "application/zip") { // creates the JSON object containing the file $file = new File(); $file->setBody(Reference::createReference($filePath)); $file->setTimeStamp(time()); $file->setDisplayName($displayName); $file = File::encodeFile($file); // sends the JSON object to the logic $URI = $logicURI . "/tutor/user/{$uid}/course/{$cid}"; $error = http_post_data($URI, $file, true, $message); if ($message == "201" || $message == "200") { $successmsg = Language::Get('main', 'sucessFileUpload', $langTemplate); $notifications[] = MakeNotification('success', $successmsg); } else { $errors = @json_decode($error); if ($errors !== null) {