/** * Validates a Create or Update Problem API request * * @param Request $r * @throws NotFoundException */ private static function validateCreateOrUpdate(Request $r, $is_update = false) { $is_required = true; // https://github.com/omegaup/omegaup/issues/739 if ($r['current_user']->username == 'omi') { throw new ForbiddenAccessException(); } // In case of update, params are optional if ($is_update) { $is_required = false; // We need to check problem_alias Validators::isStringNonEmpty($r['problem_alias'], 'problem_alias'); try { $r['problem'] = ProblemsDAO::getByAlias($r['problem_alias']); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } if (is_null($r['problem'])) { throw new NotFoundException('Problem not found'); } // We need to check that the user can actually edit the problem if (!Authorization::CanEditProblem($r['current_user_id'], $r['problem'])) { throw new ForbiddenAccessException(); } if ($r['problem']->deprecated) { throw new PreconditionFailedException('problemDeprecated'); } } else { Validators::isValidAlias($r['alias'], 'alias'); } Validators::isStringNonEmpty($r['title'], 'title', $is_required); Validators::isStringNonEmpty($r['source'], 'source', $is_required); Validators::isInEnum($r['public'], 'public', array('0', '1'), $is_required); Validators::isInEnum($r['validator'], 'validator', array('token', 'token-caseless', 'token-numeric', 'custom', 'literal'), $is_required); Validators::isNumberInRange($r['time_limit'], 'time_limit', 0, INF, $is_required); Validators::isNumberInRange($r['validator_time_limit'], 'validator_time_limit', 0, INF, $is_required); Validators::isNumberInRange($r['overall_wall_time_limit'], 'overall_wall_time_limit', 0, 60000, $is_required); Validators::isNumberInRange($r['extra_wall_time'], 'extra_wall_time', 0, 5000, $is_required); Validators::isNumberInRange($r['memory_limit'], 'memory_limit', 0, INF, $is_required); Validators::isNumberInRange($r['output_limit'], 'output_limit', 0, INF, $is_required); // HACK! I don't know why "languages" doesn't make it into $r, and I've spent far too much time // on it already, so I'll just leave this here for now... if (!isset($r['languages']) && isset($_REQUEST['languages'])) { $r['languages'] = implode(',', $_REQUEST['languages']); } elseif (isset($r['languages']) && is_array($r['languages'])) { $r['languages'] = implode(',', $r['languages']); } Validators::isValidSubset($r['languages'], 'languages', array('c', 'cpp', 'java', 'py', 'rb', 'pl', 'cs', 'pas', 'kp', 'kj', 'cat', 'hs', 'cpp11'), $is_required); }
/** * Validates a Create or Update Problem API request * * @param Request $r * @throws NotFoundException */ private static function validateCreateOrUpdate(Request $r, $is_update = false) { $is_required = true; // In case of update, params are optional if ($is_update) { $is_required = false; // We need to check problem_alias Validators::isStringNonEmpty($r["problem_alias"], "problem_alias"); try { $r["problem"] = ProblemsDAO::getByAlias($r["problem_alias"]); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } if (is_null($r["problem"])) { throw new NotFoundException("Problem not found"); } // We need to check that the user can actually edit the problem if (!Authorization::CanEditProblem($r["current_user_id"], $r["problem"])) { throw new ForbiddenAccessException(); } if ($r['problem']->deprecated) { throw new PreconditionFailedException('problemDeprecated'); } } else { Validators::isValidAlias($r['alias'], 'alias'); } Validators::isStringNonEmpty($r["title"], "title", $is_required); Validators::isStringNonEmpty($r["source"], "source", $is_required); Validators::isInEnum($r["public"], "public", array("0", "1"), $is_required); Validators::isInEnum($r["validator"], "validator", array("token", "token-caseless", "token-numeric", "custom", "literal"), $is_required); Validators::isNumberInRange($r["time_limit"], "time_limit", 0, INF, $is_required); Validators::isNumberInRange($r["validator_time_limit"], "validator_time_limit", 0, INF, $is_required); Validators::isNumberInRange($r["overall_wall_time_limit"], "overall_wall_time_limit", 0, 60000, $is_required); Validators::isNumberInRange($r["extra_wall_time"], "extra_wall_time", 0, 5000, $is_required); Validators::isNumberInRange($r["memory_limit"], "memory_limit", 0, INF, $is_required); Validators::isNumberInRange($r["output_limit"], "output_limit", 0, INF, $is_required); // HACK! I don't know why "languages" doesn't make it into $r, and I've spent far too much time // on it already, so I'll just leave this here for now... if (!isset($r["languages"]) && isset($_REQUEST["languages"])) { $r["languages"] = implode(",", $_REQUEST["languages"]); } else { if (isset($r["languages"]) && is_array($r["languages"])) { $r["languages"] = implode(",", $r["languages"]); } } Validators::isValidSubset($r["languages"], "languages", array('c', 'cpp', 'java', 'py', 'rb', 'pl', 'cs', 'pas', 'kp', 'kj', 'cat', 'hs', 'cpp11'), $is_required); }