/** * this function starts the request process * * @ return a array of request results */ public function run() { $res = array(); foreach ($this->handles as $handle) { $res[] = Request::custom($handle->method, $handle->target, $handle->header, $handle->content, $handle->authbool, $handle->sessiondelete); } /*// this var stores the number of running requests $running_handles = null; $res = array(); $maxx = count($this->handles); for($a = 0; $a<$maxx;$a++) $res[] = null; do { while (($execrun = curl_multi_exec($this->requests, $running_handles)) === CURLM_CALL_MULTI_PERFORM) { } if($execrun != CURLM_OK){ break; } // a request was just completed -- find out which one while(($done = curl_multi_info_read($this->requests))!== false) { $info = curl_getinfo($done['handle']); // if ($info['http_code'] == 200 || $info['http_code'] == 201 || $info['http_code'] == 404 || $info['http_code'] == 401) { $content = curl_multi_getcontent($done['handle']); $result = curl_getinfo($done['handle']); $header_size = curl_getinfo($done['handle'], CURLINFO_HEADER_SIZE); $result['headers'] = Request::http_parse_headers(substr($content, 0, $header_size)); $result['content'] = substr($content, $header_size); $result['status'] = curl_getinfo($done['handle'], CURLINFO_HTTP_CODE); $res[array_search($done['handle'], $this->handles)] = $result; if ($this->i < $maxx){ curl_multi_add_handle($this->requests,$this->handles[$this->i]); $this->i++; } // remove the curl handle that just completed curl_multi_remove_handle($this->requests, $done['handle']); } //Request_MultiRequest::Sleeper(10); } while ($running_handles); // close the multi curl object curl_multi_close($this->requests);*/ return $res; }
/** * Deletes a file. * * This function handles the reqest to the DBFile table to delete the information * belongs to this file as well as the request * to the filesystem to delete the file. * * @param string $lURL The address of the logic-controller. * @param array $header The header of the request. * @param array $file The file that should being deleted. */ public static function delete($lURL, $header, $file) { if (!empty($file)) { // requests to file-table of DB $URL = $lURL . '/DB/file/' . $file['fileId']; $answer = Request::custom('DELETE', $URL, $header, ""); // even if file has been deleted from db file table delete it from fs if ($answer['status'] >= 200 and $answer['status'] < 300) { // requests to filesystem $URL = $lURL . '/FS/' . $file['address']; $answer = Request::custom('DELETE', $URL, $header, ""); } } }
public static function createTests() { LFileTest::$Tester = new Testsystem(); $aufruf = function ($methode, $url, $data) { return Request::custom($methode, 'http://localhost/uebungsplattform/logic/LFile' . $url, array(), $data); }; $auswertungOK = function ($result, $methode, $url, $data, $fileDbGet, $fileDbPost, $fileFsPost) { PHPUnit_Framework_Assert::assertTrue(isset($result['content'])); PHPUnit_Framework_Assert::assertTrue(isset($result['status'])); PHPUnit_Framework_Assert::assertEquals(201, $result['status'], $data); $objData = File::decodeFile($data); $obj = File::decodeFile($result['content']); PHPUnit_Framework_Assert::assertEquals(is_array($objData), is_array($obj)); if (!is_array($obj)) { $obj = array($obj); } if (!is_array($objData)) { $objData = array($objData); } foreach ($obj as $res) { PHPUnit_Framework_Assert::assertEquals(null, $res->getBody()); PHPUnit_Framework_Assert::assertEquals(201, $res->getStatus(), $data); // prüfe Inhalt der Objekte } }; $auswertungWrongSyntax = function ($result, $methode, $url, $data, $fileDbGet, $fileDbPost, $fileFsPost) { PHPUnit_Framework_Assert::assertTrue(isset($result['content'])); PHPUnit_Framework_Assert::assertTrue(isset($result['status'])); PHPUnit_Framework_Assert::assertEquals(201, $result['status'], $data); $objData = File::decodeFile($data); $obj = File::decodeFile($result['content']); PHPUnit_Framework_Assert::assertEquals(is_array($objData), is_array($obj)); if (!is_array($obj)) { $obj = array($obj); } if (!is_array($objData)) { $objData = array($objData); } foreach ($obj as $res) { PHPUnit_Framework_Assert::assertEquals(null, $res->getBody()); PHPUnit_Framework_Assert::assertEquals(409, $res->getStatus(), $data); } }; $filesOK = array('{"fileId":"1_?aÖ","body":"f4","address":"a","hash":"hh"}', '{"body":"f4"}', '[{"fileId":"1_?aÖ","body":"f4","address":"a","hash":"hh"}]'); $filesWrongSyntax = array('{"fileId":"1_?aÖ","body":"f4","address"a}', '[{"fileId":"1_?aÖ","body":"f4","address":"a","hash":"h]', '{"fileId":"1_?aÖ",}', '{"address":"a"}', '{"hash":"hh"}', '{"displayName":"hh"}', '', '[]', '{}', null, array()); LFileTest::$Tester->situationHinzufuegen(new Situation('Funktioniert', $aufruf, $auswertungOK, new Sammlung('Methoden', 'POST'), new Sammlung('URL', '/file'), new Sammlung('Data', $filesOK), new Sammlung('fileDbGet', '{"fileId":"1_?aÖ","address":"a","hash":"hh"}'), new Sammlung('fileDbPost', '{"fileId":"1_?aÖ"}'), new Sammlung('fileFsPost', '{"address":"a","hash":"hh"}'))); LFileTest::$Tester->situationHinzufuegen(new Situation('SyntaxFehlerhaft', $aufruf, $auswertungWrongSyntax, new Sammlung('Methoden', 'POST'), new Sammlung('URL', '/file'), new Sammlung('Data', $filesWrongSyntax), new Sammlung('fileDbGet', null), new Sammlung('fileDbPost', null), new Sammlung('fileFsPost', null))); }
/** * Adds an exercise. * * Called when this component receives an HTTP POST request to * /exercise(/). * The request body should contain a JSON object representing an array of exercises */ public function addExercise() { $header = $this->app->request->headers->all(); $body = json_decode($this->app->request->getBody(), true); $allright = true; $result = array(); if (isset($body) == true && empty($body) == false) { foreach ($body as $subexercise) { // create exercise in DB $FileTypesArrayTemp = null; if (isset($subexercise['fileTypes'])) { $FileTypesArrayTemp = $subexercise['fileTypes']; unset($subexercise['fileTypes']); } $subexerciseJSON = json_encode($subexercise); $URL = $this->lURL . '/DB/exercise'; $method = 'POST'; if (isset($subexercise['id']) && $subexercise['id'] !== null) { $method = 'PUT'; $URL = $this->lURL . '/DB/exercise/' . $subexercise['id']; } $subexerciseAnswer = Request::custom($method, $URL, $header, $subexerciseJSON); if ($subexerciseAnswer['status'] == 201) { $subexerciseOutput = json_decode($subexerciseAnswer['content'], true); if (isset($subexercise['id'])) { $result[] = $subexercise; $subexerciseOutput = $subexercise; } else { $result[] = Exercise::decodeExercise($subexerciseAnswer['content']); } if (isset($subexerciseOutput['id'])) { $linkid = $subexerciseOutput['id']; } // create attachement in DB and FS if (isset($subexercise['attachments']) && !empty($subexercise['attachments'])) { foreach ($subexercise['attachments'] as &$attachment) { $attachment['exerciseId'] = $linkid; } $attachments = $subexercise['attachments']; $tempAttachments = array(); foreach ($attachments as $attachment) { $temp = Attachment::createAttachment(null, $attachment['exerciseId'], null, null); $temp->setFile($attachment); $tempAttachments[] = $temp; } $res = Request::routeRequest('POST', '/attachment', $header, Attachment::encodeAttachment($tempAttachments), $this->_postAttachment, 'attachment'); // checks the correctness of the query if ($res['status'] >= 200 && $res['status'] <= 299) { // ... } else { $allright = false; break; } } // create ExerciseFileTypes if (isset($FileTypesArrayTemp) && !empty($FileTypesArrayTemp)) { foreach ($FileTypesArrayTemp as $fileType) { $myExerciseFileType = ExerciseFileType::createExerciseFileType(NULL, $fileType['text'], $linkid); $myExerciseFileTypeJSON = ExerciseFileType::encodeExerciseFileType($myExerciseFileType); $URL = $this->lURL . "/DB/exercisefiletype"; $FileTypesAnswer = Request::custom('POST', $URL, $header, $myExerciseFileTypeJSON); if ($FileTypesAnswer['status'] != 201) { $allright = false; break; } } } if ($allright == false) { break; } } else { $allright = false; break; } } } if ($allright == true) { $this->app->response->setBody(Exercise::encodeExercise($result)); $this->app->response->setStatus(201); } else { $this->app->response->setStatus(409); } }
/** * Sends a new request. * * Called allways if the controller receives a request for * database, filesystem or a logic component. * Completes the URL, sends the new request and sets the response. * * @param array $string An array of strings that contains the URL * with which the controller has been called. * @param string $method The method of the received request. * @param string $URI The first part of the new request. * @param array $header The header of the received request. * @param string $body The body of the received request. */ public function sendNewRequest($string, $method, $URI, $header, $body) { // completes the URL by attach each field of $string to the given URL-part foreach ($string as $str) { $URI = $URI . '/' . $str; } // send the new request and set the response $answer = Request::custom($method, $URI, $header, $body); $this->app->response->setBody($answer['content']); $this->app->response->setStatus($answer['status']); if (isset($answer['headers']['Content-Type'])) { $this->app->response->headers->set('Content-Type', $answer['headers']['Content-Type']); } if (isset($answer['headers']['Content-Disposition'])) { $this->app->response->headers->set('Content-Disposition', $answer['headers']['Content-Disposition']); } }
/** * the getl function uses a list of links to find a * relevant component for the $data request * * @param $data a slim generated array of URI segments (String[]) */ public function getl($data) { Logger::Log('starts Controller routing', LogLevel::DEBUG); // if no URI is received, abort process if (count($data) == 0) { Logger::Log('Controller nothing to route', LogLevel::DEBUG); $this->_app->response->setStatus(409); $this->_app->stop(); return; } // get possible links $else = array(); $list = $this->_conf->getLinks(); foreach ($list as $links) { // determines all supported prefixes $possible = explode(',', $links->getPrefix()); if (in_array($data[0], $possible)) { // create a custom request $ch = Request::custom($this->_app->request->getMethod(), $links->getAddress() . $this->_app->request->getResourceUri(), array(), $this->_app->request->getBody()); // checks the answered status code if ($ch['status'] >= 200 && $ch['status'] <= 299) { // finished $this->_app->response->setStatus($ch['status']); $this->_app->response->setBody($ch['content']); if (isset($ch['headers']['Content-Type'])) { $this->_app->response->headers->set('Content-Type', $ch['headers']['Content-Type']); } if (isset($ch['headers']['Content-Disposition'])) { $this->_app->response->headers->set('Content-Disposition', $ch['headers']['Content-Disposition']); } Logger::Log('Controller prefix search done', LogLevel::DEBUG); $this->_app->stop(); return; } elseif ($ch['status'] == 401 || $ch['status'] == 404 || $ch['status'] == 406) { $this->_app->response->setStatus($ch['status']); $this->_app->response->setBody($ch['content']); if (isset($ch['headers']['Content-Type'])) { $this->_app->response->headers->set('Content-Type', $ch['headers']['Content-Type']); } if (isset($ch['headers']['Content-Disposition'])) { $this->_app->response->headers->set('Content-Disposition', $ch['headers']['Content-Disposition']); } $this->_app->stop(); return; } } elseif (in_array('', $possible)) { // if the prefix is not used, check if the link also // permits any questions and remember him in the $else list $else[] = $links; } } // if no possible link was found or every possible component // answered with a non "finished" status code, we will ask components // who are able to work with every prefix foreach ($else as $links) { // create a custom request $ch = Request::custom($this->_app->request->getMethod(), $links->getAddress() . $this->_app->request->getResourceUri(), array(), $this->_app->request->getBody()); // checks the answered status code if ($ch['status'] >= 200 && $ch['status'] <= 299) { // finished $this->_app->response->setStatus($ch['status']); $this->_app->response->setBody($ch['content']); if (isset($ch['headers']['Content-Type'])) { $this->_app->response->headers->set('Content-Type', $ch['headers']['Content-Type']); } if (isset($ch['headers']['Content-Disposition'])) { $this->_app->response->headers->set('Content-Disposition', $ch['headers']['Content-Disposition']); } Logger::Log('Controller blank search done', LogLevel::DEBUG); $this->_app->stop(); return; } elseif ($ch['status'] == 401 || $ch['status'] == 404 || $ch['status'] == 406) { $this->_app->response->setStatus($ch['status']); $this->_app->response->setBody($ch['content']); if (isset($ch['headers']['Content-Type'])) { $this->_app->response->headers->set('Content-Type', $ch['headers']['Content-Type']); } if (isset($ch['headers']['Content-Disposition'])) { $this->_app->response->headers->set('Content-Disposition', $ch['headers']['Content-Disposition']); } $this->_app->stop(); return; } } // no positive response or no operative link $this->_app->response->setStatus(409); }
/** * Edits an attachment. * * Called when this component receives an HTTP PUT request to * /attachment/attachment/$attachmentid(/). * The request body should contain a JSON object representing the * attachment's new attributes. * * @param string $attachmentid The id of the attachment that is being updated. */ public function editAttachment($attachmentid) { $header = $this->app->request->headers->all(); $body = $this->app->request->getBody(); $body = json_decode($body, true); if (isset($body['file']['body'])) { //getAttachment to get the file of the old Attachment $URL = $this->lURL . '/DB/attachment/attachment/' . $attachmentid; $answer = Request::custom('GET', $URL, $header, ""); $bodyOld = json_decode($answer['content'], true); //save the new file $body['file'] = LFileHandler::add($this->lURL, $header, $body['file']); // if file has not been saved if (empty($body['file'])) { $this->app->response->setStatus(409); } else { // if file has been saved //save the new information $URL = $this->lURL . '/DB/attachment/attachment/' . $attachmentid; $answer = Request::custom('PUT', $URL, $header, json_encode($body)); $this->app->response->setStatus($answer['status']); } // delete the old file LFileHandler::delete($this->lURL, $header, $bodyOld['file']); } else { // save the new information $URL = $this->lURL . '/DB/attachment/attachment/' . $attachmentid; $answer = Request::custom('PUT', $URL, $header, json_encode($body)); $this->app->response->setStatus($answer['status']); } }
/** * Returns all courses a given user belongs to. * * Called when this component receives an HTTP GET request to * /course/user/$userid(/). * * @param int $userid The id of the user. */ public function getCourses($userid) { $body = $this->app->request->getBody(); $header = $this->app->request->headers->all(); $URL = $this->lURL . '/DB/course/user/' . $userid; $answer = Request::custom('GET', $URL, $header, $body); $this->app->response->setStatus($answer['status']); $this->app->response->setBody($answer['content']); }
/** * Returns an exercise sheet with exercises. * * Called when this component receives an HTTP GET request to * /exercisesheet/exercisesheet/$sheetid(/). * * @param int $sheetid The id of the exercise sheet that should be returned. */ public function getExerciseSheetExercise($sheetid) { $URL = $this->lURL . '/DB/exercisesheet/exercisesheet/' . $sheetid . '/exercise'; $answer = Request::custom('GET', $URL, array(), ""); $sheet = json_decode($answer['content'], true); // sort exercises by link = exerercises and linkName = subexercise ascendingly if (isset($sheet['exercises']) && is_array($sheet['exercises'])) { $sheet['exercises'] = LArraySorter::orderBy($sheet['exercises'], 'link', SORT_ASC, 'linkName', SORT_ASC); } $sheet = json_encode($sheet); $this->app->response->setBody($sheet); $this->app->response->setStatus($answer['status']); }
/** * Function to auto allocate groups to tutors * * It takes two argument and returns a Status-Code. * * @param $courseid an integer identifies the course * @param $sheetid an integer identifies the exercisesheet */ public function autoAllocateByGroup($courseid, $sheetid) { $header = $this->app->request->headers->all(); $body = json_decode($this->app->request->getBody(), true); $error = false; $tutors = $body['tutors']; $submissions = array(); foreach ($body['unassigned'] as $submission) { $leaderId = $submission['leaderId']; $submissions[$leaderId][] = $submission; } //randomized allocation shuffle($tutors); $i = 0; $numberOfTutors = count($tutors); $markings = array(); foreach ($submissions as $submissionsByGroup) { foreach ($submissionsByGroup as $submission) { $newMarking = array('submission' => $submission, 'status' => 1, 'tutorId' => $tutors[$i]['tutorId']); //adds a submission to a tutor $markings[] = $newMarking; } if ($i < $numberOfTutors - 1) { $i++; } else { $i = 0; } } //requests to database $URL = $this->lURL . '/DB/marking'; /*foreach($markings as $marking){ $answer = Request::custom('POST', $URL, array(), json_encode($marking)); if ($answer['status'] >= 300){ $error = true; $errorstatus = $answer['status']; } }*/ $answer = Request::custom('POST', $URL, array(), json_encode($markings)); if ($answer['status'] >= 300) { $error = true; $errorstatus = $answer['status']; } // response if ($error == false) { $this->app->response->setStatus(201); $this->app->response->setBody(""); } else { $this->app->response->setStatus($errorstatus); $this->app->response->setBody("Warning: At least one group was not being allocated!"); } // $URL = $this->lURL.'/getsite/tutorassignment/course/' // .$courseid.'/exercisesheet/'.$sheetid; // $answer = Request::custom('GET', $URL, $header, ""); // // $this->app->response->setBody($answer['content']); }
/** * the getl function uses a list of links to find a * relevant component for the $data request * * @param $data a slim generated array of URI segments (String[]) */ public function getl($data) { Logger::Log('starts Controller routing', LogLevel::DEBUG); // if no URI is received, abort process if (count($data) == 0) { Logger::Log('Controller nothing to route', LogLevel::DEBUG); $this->_app->response->setStatus(409); $this->_app->stop(); return; } $URI = '/' . implode('/', $data); // get possible links $list = CConfig::getLinks($this->_conf->getLinks(), 'out'); foreach ($list as $links) { $componentURL = $links->getAddress(); $similar = Controller2::UrlAnd($componentURL, $URI); if ($similar != null) { $URI2 = substr($URI, Controller2::UrlAnd($componentURL, $URI)); $relevanz = explode(' ', $links->getRelevanz()); $found = false; foreach ($relevanz as $rel) { if ($rel == 'ALL') { $found = true; break; } $sub = strpos($rel, '_'); if ($sub !== false) { $method = substr($rel, 0, $sub); $path = substr($rel, $sub + 1); if (strtoupper($method) == strtoupper($this->_app->request->getMethod())) { $router = new \Slim\Router(); $route = new \Slim\Route($path, 'is_array'); $route->via(strtoupper($method)); $router->map($route); $routes = count($router->getMatchedRoutes(strtoupper($method), $URI2), true); if ($routes === 0) { continue; } $found = true; break; } } } if (!$found) { continue; } // create a custom request $ch = Request::custom($this->_app->request->getMethod(), $componentURL . substr($URI, $similar), array(), $this->_app->request->getBody()); // checks the answered status code if ($ch['status'] >= 200 && $ch['status'] <= 299) { // finished $this->_app->response->setStatus($ch['status']); $this->_app->response->setBody($ch['content']); if (isset($ch['headers']['Content-Type'])) { $this->_app->response->headers->set('Content-Type', $ch['headers']['Content-Type']); } if (isset($ch['headers']['Content-Disposition'])) { $this->_app->response->headers->set('Content-Disposition', $ch['headers']['Content-Disposition']); } Logger::Log('Controller2 search done', LogLevel::DEBUG); $this->_app->stop(); } } } // no positive response or no operative link $this->_app->response->setStatus(409); $this->_app->response->setBody(''); }
public function createSheetInfo($userid, $courseid) { // returns all possible exerciseTypes of the course $URL = $this->_getApprovalCondition->getAddress() . '/approvalcondition/course/' . $courseid; $answer = Request::custom('GET', $URL, array(), ''); $response['exerciseTypes'] = json_decode($answer['content'], true); // returns all exerciseTypes $URL = $this->_getExerciseType->getAddress() . '/exercisetype'; $answer = Request::custom('GET', $URL, array(), ''); $allexerciseTypes = json_decode($answer['content'], true); if (!empty($response['exerciseTypes'])) { foreach ($response['exerciseTypes'] as &$exerciseType) { foreach ($allexerciseTypes as &$allexerciseType) { if ($exerciseType['exerciseTypeId'] == $allexerciseType['id']) { $exerciseType['name'] = $allexerciseType['name']; } } } } else { unset($response['exerciseTypes']); } $this->flag = 1; // adds the user_course_data to the response $response['user'] = $this->userWithCourse($userid, $courseid); $this->app->response->setBody(json_encode($response)); }