/** * Handles requests to POST /api/user to update information about the active user. */ public function post() { $message = null; $message = ProtocolMessage::unserialize($this->request->post['_proto'], 'UserRequestProtocolMessage'); // Check: the message is modifiying the requesting user. if ($message->userId != $this->user->id) { return 400; } // Check: XSRF token is correct. if (!$this->utility->checkXsrfToken($this->session, $message->xsrfToken)) { $this->response->status = 401; $this->response->json(['STATUS' => 'FAILURE', 'MESSAGE' => 'XSRF token is invalid.'], true); return $this->response; } // Update database to reflect information sent in the request. $this->user->setProperty(SchedulePlannerProtocolMessageUtility::TOUR_PROPERTY, $message->hasSeenTour === true); $this->user->setProperty(SchedulePlannerProtocolMessageUtility::DISCLAIMER_PROPERTY, $message->hasAgreedToDisclaimer === true); if (is_integer($message->lastSeenVersion) && $message->lastSeenVersion >= 0) { $this->user->setProperty(SchedulePlannerProtocolMessageUtility::LAST_SEEN_VERSION_PROPERTY, $message->lastSeenVersion); } $this->db->prepare("DELETE FROM `playgrounds` WHERE `userid` = ?")->execute($this->user->id); $q = $this->db->prepare("INSERT INTO `playgrounds` (`userid`, `courseid`) VALUES (?, ?);"); foreach ($message->playground->courses as $course) { $q->execute($this->user->id, $course->courseId); } $this->db->prepare("DELETE FROM `schedules` WHERE `userid` = ?")->execute($this->user->id); $q = $this->db->prepare("INSERT INTO `schedules` (`userid`, `courseid`, `year`) VALUES (?, ?, 0);"); foreach ($message->schedule->courses as $course) { $q->execute($this->user->id, $course->courseId); } $this->response->json(['STATUS' => 'OK'], true); }
/** * Handles GET requests to /api/courses as defined in webapp.php. */ public function get() { // Attempt to use the cached version for efficiency (if it exists). $file = new File(FILE_ROOT . '/cache/courses.json'); if ($file->exists && $file->isReadable) { return $file; } // Otherwise, calculate the response directly. $response = $this->utility->createCoursesResponse(null); $this->response->json(ProtocolMessage::serialize($response), true); // "')]}\n" }
protected static function debugFormatInt64($value) { if (bccomp($value, "-20000000000000") <= 0 or bccomp($value, "20000000000000") >= 0) { return ProtocolMessage::debugFormatFixed64($value); } return strval($value); }
$cdp->run(); $utility = new SchedulePlannerProtocolMessageUtility(App::getDatabase()); $response = $utility->createCoursesResponse(null); $json = ProtocolMessage::serialize($response); try { $file = new File(FILE_ROOT . '/cache/courses.json'); $file->content = "')]}\n" . $json; } catch (FileException $e) { fprintf(STDOUT, "Failed to write file cache, continuing.\n"); } fprintf(STDOUT, "Done!\n"); } else { fprintf(STDOUT, "Unrecognized command.\n"); } }); /** * Regenerates the cache of course data to reflect database modifications. * php server.php recache */ CLIApplication::listen('recache', function ($args) { $utility = new SchedulePlannerProtocolMessageUtility(App::getDatabase()); $response = $utility->createCoursesResponse(null); $json = ProtocolMessage::serialize($response); try { $file = new File(FILE_ROOT . '/cache/courses.json'); $file->content = "')]}\n" . $json; } catch (FileException $e) { fprintf(STDOUT, "Failed to write file cache, continuing.\n"); } fprintf(STDOUT, "Done!\n"); });
/** @override*/ public function validate() { return parent::validate() && is_integer($this->courseId) && $this->courseId > 0; }