/** * Add contest to a group scoreboard * * @param Request $r */ public static function apiAddContest(Request $r) { self::validateGroupScoreboardAndContest($r); Validators::isInEnum($r["only_ac"], "only_ac", array(0, 1)); Validators::isNumber($r["weight"], "weight"); try { $groupScoreboardContest = new GroupsScoreboardsContests(array("group_scoreboard_id" => $r["scoreboard"]->group_scoreboard_id, "contest_id" => $r["contest"]->contest_id, "only_ac" => $r["only_ac"], "weight" => $r["weight"])); GroupsScoreboardsContestsDAO::save($groupScoreboardContest); self::$log->info("Contest " . $r["contest_alias"] . "added to scoreboard " . $r["scoreboard_alias"]); } catch (Exception $ex) { throw new InvalidDatabaseOperationException($ex); } return array("status" => "ok"); }
/** * Add contest to a group scoreboard * * @param Request $r */ public static function apiAddContest(Request $r) { self::validateGroupScoreboardAndContest($r); Validators::isInEnum($r['only_ac'], 'only_ac', array(0, 1)); Validators::isNumber($r['weight'], 'weight'); try { $groupScoreboardContest = new GroupsScoreboardsContests(array('group_scoreboard_id' => $r['scoreboard']->group_scoreboard_id, 'contest_id' => $r['contest']->contest_id, 'only_ac' => $r['only_ac'], 'weight' => $r['weight'])); GroupsScoreboardsContestsDAO::save($groupScoreboardContest); self::$log->info('Contest ' . $r['contest_alias'] . 'added to scoreboard ' . $r['scoreboard_alias']); } catch (Exception $ex) { throw new InvalidDatabaseOperationException($ex); } return array('status' => 'ok'); }
/** * Validator for List API * * @param Request $r * @throws ForbiddenAccessException * @throws InvalidDatabaseOperationException * @throws NotFoundException */ private static function validateList(Request $r) { // Defaults for offset and rowcount if (!isset($r['offset'])) { $r['offset'] = 0; } if (!isset($r['rowcount'])) { $r['rowcount'] = 100; } if (!Authorization::IsSystemAdmin($r['current_user_id'])) { throw new ForbiddenAccessException('userNotAllowed'); } Validators::isNumber($r['offset'], 'offset', false); Validators::isNumber($r['rowcount'], 'rowcount', false); Validators::isInEnum($r['status'], 'status', array('new', 'waiting', 'compiling', 'running', 'ready'), false); Validators::isInEnum($r['verdict'], 'verdict', array('AC', 'PA', 'WA', 'TLE', 'MLE', 'OLE', 'RTE', 'RFE', 'CE', 'JE', 'NO-AC'), false); // Check filter by problem, is optional if (!is_null($r['problem_alias'])) { Validators::isStringNonEmpty($r['problem_alias'], 'problem'); try { $r['problem'] = ProblemsDAO::getByAlias($r['problem_alias']); } catch (Exception $e) { // Operation failed in the data layer throw new InvalidDatabaseOperationException($e); } if (is_null($r['problem'])) { throw new NotFoundException('problemNotFound'); } } Validators::isInEnum($r['language'], 'language', array('c', 'cpp', 'cpp11', 'java', 'py', 'rb', 'pl', 'cs', 'pas', 'kp', 'kj', 'cat', 'hs'), false); // Get user if we have something in username if (!is_null($r['username'])) { try { $r['user'] = UserController::resolveUser($r['username']); } catch (NotFoundException $e) { // If not found, simply ignore it $r['username'] = null; $r['user'] = null; } } }
/** * Validator for List API * * @param Request $r * @throws ForbiddenAccessException * @throws InvalidDatabaseOperationException * @throws NotFoundException */ private static function validateList(Request $r) { // Defaults for offset and rowcount if (!isset($r["offset"])) { $r["offset"] = 0; } if (!isset($r["rowcount"])) { $r["rowcount"] = 100; } if (!Authorization::IsSystemAdmin($r["current_user_id"])) { throw new ForbiddenAccessException("userNotAllowed"); } Validators::isNumber($r["offset"], "offset", false); Validators::isNumber($r["rowcount"], "rowcount", false); Validators::isInEnum($r["status"], "status", array('new', 'waiting', 'compiling', 'running', 'ready'), false); Validators::isInEnum($r["verdict"], "verdict", array("AC", "PA", "WA", "TLE", "MLE", "OLE", "RTE", "RFE", "CE", "JE", "NO-AC"), false); // Check filter by problem, is optional if (!is_null($r["problem_alias"])) { Validators::isStringNonEmpty($r["problem_alias"], "problem"); try { $r["problem"] = ProblemsDAO::getByAlias($r["problem_alias"]); } catch (Exception $e) { // Operation failed in the data layer throw new InvalidDatabaseOperationException($e); } if (is_null($r["problem"])) { throw new NotFoundException("problemNotFound"); } } Validators::isInEnum($r["language"], "language", array('c', 'cpp', 'cpp11', 'java', 'py', 'rb', 'pl', 'cs', 'pas', 'kp', 'kj', 'cat', 'hs'), false); // Get user if we have something in username if (!is_null($r["username"])) { try { $r["user"] = UserController::resolveUser($r["username"]); } catch (NotFoundException $e) { // If not found, simply ignore it $r["username"] = null; $r["user"] = null; } } }
/** * Given a contest_alias, sets the recommended flag on/off. * Only omegaUp admins can call this API. * * @param Request $r * @return array */ public static function apiSetRecommended(Request $r) { self::authenticateRequest($r); if (!Authorization::IsSystemAdmin($r['current_user_id'])) { throw new ForbiddenAccessException('userNotAllowed'); } // Validate & get contest_alias try { $r['contest'] = ContestsDAO::getByAlias($r['contest_alias']); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } if (is_null($r['contest'])) { throw new NotFoundException('contestNotFound'); } // Validate value param Validators::isInEnum($r['value'], 'value', array('0', '1')); $r['contest']->recommended = $r['value']; try { ContestsDAO::save($r['contest']); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } return array('status' => 'ok'); }
/** * Updates problem statement only * * @param Request $r * @return array * @throws ApiException * @throws InvalidDatabaseOperationException */ public static function apiUpdateStatement(Request $r) { self::authenticateRequest($r); self::validateCreateOrUpdate($r, true); // Validate statement Validators::isStringNonEmpty($r['statement'], 'statement'); Validators::isStringNonEmpty($r['message'], 'message'); // Check that lang is in the ISO 639-1 code list, default is "es". $iso639_1 = array('ab', 'aa', 'af', 'ak', 'sq', 'am', 'ar', 'an', 'hy', 'as', 'av', 'ae', 'ay', 'az', 'bm', 'ba', 'eu', 'be', 'bn', 'bh', 'bi', 'bs', 'br', 'bg', 'my', 'ca', 'ch', 'ce', 'ny', 'zh', 'cv', 'kw', 'co', 'cr', 'hr', 'cs', 'da', 'dv', 'nl', 'dz', 'en', 'eo', 'et', 'ee', 'fo', 'fj', 'fi', 'fr', 'ff', 'gl', 'ka', 'de', 'el', 'gn', 'gu', 'ht', 'ha', 'he', 'hz', 'hi', 'ho', 'hu', 'ia', 'id', 'ie', 'ga', 'ig', 'ik', 'io', 'is', 'it', 'iu', 'ja', 'jv', 'kl', 'kn', 'kr', 'ks', 'kk', 'km', 'ki', 'rw', 'ky', 'kv', 'kg', 'ko', 'ku', 'kj', 'la', 'lb', 'lg', 'li', 'ln', 'lo', 'lt', 'lu', 'lv', 'gv', 'mk', 'mg', 'ms', 'ml', 'mt', 'mi', 'mr', 'mh', 'mn', 'na', 'nv', 'nd', 'ne', 'ng', 'nb', 'nn', 'no', 'ii', 'nr', 'oc', 'oj', 'cu', 'om', 'or', 'os', 'pa', 'pi', 'fa', 'pl', 'ps', 'pt', 'qu', 'rm', 'rn', 'ro', 'ru', 'sa', 'sc', 'sd', 'se', 'sm', 'sg', 'sr', 'gd', 'sn', 'si', 'sk', 'sl', 'so', 'st', 'es', 'su', 'sw', 'ss', 'sv', 'ta', 'te', 'tg', 'th', 'ti', 'bo', 'tk', 'tl', 'tn', 'to', 'tr', 'ts', 'tt', 'tw', 'ty', 'ug', 'uk', 'ur', 'uz', 've', 'vi', 'vo', 'wa', 'cy', 'wo', 'fy', 'xh', 'yi', 'yo', 'za', 'zu'); Validators::isInEnum($r['lang'], 'lang', $iso639_1, false); if (is_null($r['lang'])) { $r['lang'] = UserController::getPreferredLanguage($r); } $problemDeployer = new ProblemDeployer($r['problem_alias'], ProblemDeployer::UPDATE_STATEMENTS); try { $problemDeployer->updateStatement($r['lang'], $r['statement']); $problemDeployer->commit("{$r['lang']}.markdown: {$r['message']}", $r['current_user']); // Invalidar problem statement cache Cache::deleteFromCache(Cache::PROBLEM_STATEMENT, $r['problem']->getAlias() . '-' . $r['lang'] . '-' . 'html'); Cache::deleteFromCache(Cache::PROBLEM_STATEMENT, $r['problem']->getAlias() . '-' . $r['lang'] . '-' . 'markdown'); Cache::deleteFromCache(Cache::PROBLEM_SAMPLE, $r['problem']->getAlias() . '-sample.in'); } catch (ApiException $e) { throw $e; } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } finally { $problemDeployer->cleanup(); } // All clear $response['status'] = 'ok'; return $response; }
/** * Validate update API request * * @param Request $r * @throws InvalidDatabaseOperationException * @throws ForbiddenAccessException */ private static function validateUpdate(Request $r) { Validators::isNumber($r["clarification_id"], "clarificaion_id"); Validators::isStringNonEmpty($r["answer"], "answer", false); Validators::isInEnum($r["public"], "public", array('0', '1'), false); Validators::isStringNonEmpty($r["message"], "message", false); // Check that clarification exists try { $r['clarification'] = ClarificationsDAO::GetByPK($r["clarification_id"]); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } if (!Authorization::CanEditClarification($r["current_user_id"], $r["clarification"])) { throw new ForbiddenAccessException(); } }
/** * Updates problem statement only * * @param Request $r * @return array * @throws ApiException * @throws InvalidDatabaseOperationException */ public static function apiUpdateStatement(Request $r) { self::authenticateRequest($r); self::validateCreateOrUpdate($r, true); // Validate statement Validators::isStringNonEmpty($r["statement"], "statement"); Validators::isStringNonEmpty($r["message"], "message"); // Check that lang is in the ISO 639-1 code list, default is "es". $iso639_1 = array("ab", "aa", "af", "ak", "sq", "am", "ar", "an", "hy", "as", "av", "ae", "ay", "az", "bm", "ba", "eu", "be", "bn", "bh", "bi", "bs", "br", "bg", "my", "ca", "ch", "ce", "ny", "zh", "cv", "kw", "co", "cr", "hr", "cs", "da", "dv", "nl", "dz", "en", "eo", "et", "ee", "fo", "fj", "fi", "fr", "ff", "gl", "ka", "de", "el", "gn", "gu", "ht", "ha", "he", "hz", "hi", "ho", "hu", "ia", "id", "ie", "ga", "ig", "ik", "io", "is", "it", "iu", "ja", "jv", "kl", "kn", "kr", "ks", "kk", "km", "ki", "rw", "ky", "kv", "kg", "ko", "ku", "kj", "la", "lb", "lg", "li", "ln", "lo", "lt", "lu", "lv", "gv", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mh", "mn", "na", "nv", "nd", "ne", "ng", "nb", "nn", "no", "ii", "nr", "oc", "oj", "cu", "om", "or", "os", "pa", "pi", "fa", "pl", "ps", "pt", "qu", "rm", "rn", "ro", "ru", "sa", "sc", "sd", "se", "sm", "sg", "sr", "gd", "sn", "si", "sk", "sl", "so", "st", "es", "su", "sw", "ss", "sv", "ta", "te", "tg", "th", "ti", "bo", "tk", "tl", "tn", "to", "tr", "ts", "tt", "tw", "ty", "ug", "uk", "ur", "uz", "ve", "vi", "vo", "wa", "cy", "wo", "fy", "xh", "yi", "yo", "za", "zu"); Validators::isInEnum($r["lang"], "lang", $iso639_1, false); if (is_null($r["lang"])) { $r['lang'] = UserController::getPreferredLanguage($r); } $problemDeployer = new ProblemDeployer($r['problem_alias'], ProblemDeployer::UPDATE_STATEMENTS); try { $problemDeployer->updateStatement($r['lang'], $r['statement']); $problemDeployer->commit("{$r['lang']}.markdown: {$r['message']}", $r['current_user']); // Invalidar problem statement cache Cache::deleteFromCache(Cache::PROBLEM_STATEMENT, $r["problem"]->getAlias() . "-" . $r["lang"] . "-" . "html"); Cache::deleteFromCache(Cache::PROBLEM_STATEMENT, $r["problem"]->getAlias() . "-" . $r["lang"] . "-" . "markdown"); Cache::deleteFromCache(Cache::PROBLEM_SAMPLE, $r["problem"]->getAlias() . "-sample.in"); } catch (ApiException $e) { throw $e; } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } finally { $problemDeployer->cleanup(); } // All clear $response["status"] = "ok"; return $response; }