| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | require_once(IA_ROOT_DIR.'common/db/job.php'); |
|---|
| 4 | require_once(IA_ROOT_DIR.'common/db/task.php'); |
|---|
| 5 | require_once(IA_ROOT_DIR.'common/db/round.php'); |
|---|
| 6 | |
|---|
| 7 | // Safe function to validate and submit a job. |
|---|
| 8 | // $args contains: |
|---|
| 9 | // task_id: Task to submit for. |
|---|
| 10 | // round_id: Round to submit for. Optional, if missing the job is sent |
|---|
| 11 | // to all parent rounds. |
|---|
| 12 | // compiler_id: c, cpp, fpc, or py. |
|---|
| 13 | // solution: A string with the file to submit. |
|---|
| 14 | // |
|---|
| 15 | // Returns an array of errors, or array() on success. |
|---|
| 16 | function safe_job_submit($args, $user) { |
|---|
| 17 | $errors = array(); |
|---|
| 18 | |
|---|
| 19 | // Validate task id. |
|---|
| 20 | $task = null; |
|---|
| 21 | if (!array_key_exists('task_id', $args)) { |
|---|
| 22 | $errors['task_id'] = "Lipseste id-ul task-ului."; |
|---|
| 23 | } else if (!is_task_id($args['task_id'])) { |
|---|
| 24 | $errors['task_id'] = "Id de task invalid."; |
|---|
| 25 | } else if (is_null($task = task_get($args['task_id']))) { |
|---|
| 26 | $errors['task_id'] = "Task-ul {$args['task_id']} nu exista."; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | // Validate round id. |
|---|
| 30 | $round = null; |
|---|
| 31 | if (!array_key_exists('round_id', $args)) { |
|---|
| 32 | $errors['round_id'] = "Nu ai specificat un concurs."; |
|---|
| 33 | } else if (!is_round_id($args['round_id'])) { |
|---|
| 34 | $errors['round_id'] = "Nu ai specificat un concurs corect."; |
|---|
| 35 | } else if (is_null($round = round_get($args['round_id']))) { |
|---|
| 36 | $errors['round_id'] = "Runda '{$args['round_id']}' nu exista."; |
|---|
| 37 | } |
|---|
| 38 | // Check if task is new and hasn't been added to any rounds |
|---|
| 39 | if (getattr($args, "round_id") == "" && |
|---|
| 40 | !task_get_submit_rounds($task["id"], $user) && |
|---|
| 41 | security_query($user, 'task-submit', $task)) { |
|---|
| 42 | unset($errors["round_id"]); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | // Validate compiler id |
|---|
| 46 | $valid_compilers = array('c', 'cpp', 'fpc', 'py'); |
|---|
| 47 | if (!array_key_exists('compiler_id', $args)) { |
|---|
| 48 | $errors['compiler_id'] = "Lipseste compilatorul."; |
|---|
| 49 | } else if (array_search($args['compiler_id'], $valid_compilers) === false) { |
|---|
| 50 | $errors['compiler_id'] = "Compilator invalid."; |
|---|
| 51 | } |
|---|
| 52 | // HACK: For the moment, only admin`s are allowed to submit Python jobs. |
|---|
| 53 | // TODO: Remove this once Python support is stable. |
|---|
| 54 | if ('py' == $args['compiler_id'] && !user_is_admin($user)) { |
|---|
| 55 | $errors['compiler_id'] = 'Deocamdata, numai administratorii pot ' |
|---|
| 56 | .'trimite surse Python.'; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | // Validate solution |
|---|
| 60 | if (!array_key_exists('solution', $args)) { |
|---|
| 61 | $errors['solution'] = "Lipseste fisierul solutie."; |
|---|
| 62 | } else if (!is_string($args['solution'])) { |
|---|
| 63 | $errors['solution'] = "Solution trebuie sa fie string."; |
|---|
| 64 | } else if (IA_SUBMISSION_MAXSIZE <= strlen($args['solution'])) { |
|---|
| 65 | $errors['solution'] = "Solutia depaseste dimensiunea maxima admisa:". |
|---|
| 66 | ((int)IA_SUBMISSION_MAXSIZE / 1024).'KB.'; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | // Check task submit security |
|---|
| 70 | if ($task && !security_query($user, 'task-submit', $task)) { |
|---|
| 71 | $errors[] = "Nu ai voie sa trimiti la acest task."; |
|---|
| 72 | } |
|---|
| 73 | if ($round && !security_query($user, 'round-submit', $round)) { |
|---|
| 74 | $errors[] = "Nu poti sa trimiti la aceasta runda."; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | // Only now create the job. |
|---|
| 78 | if (count($errors) === 0) { |
|---|
| 79 | job_create($args['task_id'], $args['round_id'], $user['id'], |
|---|
| 80 | $args['compiler_id'], $args['solution'], |
|---|
| 81 | getattr($args, 'remote_ip_info')); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | return $errors; |
|---|
| 85 | } |
|---|