/**
  * Create a new run
  *
  * @param Request $r
  * @return array
  * @throws Exception
  * @throws InvalidDatabaseOperationException
  * @throws InvalidFilesystemOperationException
  */
 public static function apiCreate(Request $r)
 {
     // Init
     self::initializeGrader();
     // Authenticate user
     self::authenticateRequest($r);
     // Validate request
     self::validateCreateRequest($r);
     self::$log->info('New run being submitted!!');
     $response = array();
     if (self::$practice) {
         if (OMEGAUP_LOCKDOWN) {
             throw new ForbiddenAccessException('lockdown');
         }
         $submit_delay = 0;
         $contest_id = null;
         $test = 0;
     } else {
         //check the kind of penalty_type for this contest
         $penalty_type = $r['contest']->penalty_type;
         switch ($penalty_type) {
             case 'contest_start':
                 // submit_delay is calculated from the start
                 // of the contest
                 $start = $r['contest']->getStartTime();
                 break;
             case 'problem_open':
                 // submit delay is calculated from the
                 // time the user opened the problem
                 $opened = ContestProblemOpenedDAO::getByPK($r['contest']->getContestId(), $r['problem']->getProblemId(), $r['current_user_id']);
                 if (is_null($opened)) {
                     //holy moly, he is submitting a run
                     //and he hasnt even opened the problem
                     //what should be done here?
                     throw new NotAllowedToSubmitException('runEvenOpened');
                 }
                 $start = $opened->getOpenTime();
                 break;
             case 'none':
             case 'runtime':
                 //we dont care
                 $start = null;
                 break;
             default:
                 self::$log->error('penalty_type for this contests is not a valid option, asuming `none`.');
                 $start = null;
         }
         if (!is_null($start)) {
             //ok, what time is it now?
             $c_time = time();
             $start = strtotime($start);
             //asuming submit_delay is in minutes
             $submit_delay = (int) (($c_time - $start) / 60);
         } else {
             $submit_delay = 0;
         }
         $contest_id = $r['contest']->getContestId();
         $test = Authorization::IsContestAdmin($r['current_user_id'], $r['contest']) ? 1 : 0;
     }
     // Populate new run object
     $run = new Runs(array('user_id' => $r['current_user_id'], 'problem_id' => $r['problem']->getProblemId(), 'contest_id' => $contest_id, 'language' => $r['language'], 'source' => $r['source'], 'status' => 'new', 'runtime' => 0, 'penalty' => $submit_delay, 'memory' => 0, 'score' => 0, 'contest_score' => $contest_id != null ? 0 : null, 'submit_delay' => $submit_delay, 'guid' => md5(uniqid(rand(), true)), 'verdict' => 'JE', 'test' => $test));
     try {
         // Push run into DB
         RunsDAO::save($run);
         SubmissionLogDAO::save(new SubmissionLog(array('user_id' => $run->user_id, 'run_id' => $run->run_id, 'contest_id' => $run->contest_id, 'ip' => ip2long($_SERVER['REMOTE_ADDR']))));
         // Update submissions counter++
         $r['problem']->setSubmissions($r['problem']->getSubmissions() + 1);
         ProblemsDAO::save($r['problem']);
     } catch (Exception $e) {
         // Operation failed in the data layer
         throw new InvalidDatabaseOperationException($e);
     }
     try {
         // Create file for the run
         $filepath = RunController::getSubmissionPath($run);
         FileHandler::CreateFile($filepath, $r['source']);
     } catch (Exception $e) {
         throw new InvalidFilesystemOperationException($e);
     }
     // Call Grader
     try {
         self::$grader->Grade([$run->guid], false, false);
     } catch (Exception $e) {
         self::$log->error('Call to Grader::grade() failed:');
         self::$log->error($e);
     }
     if (self::$practice) {
         $response['submission_deadline'] = 0;
     } else {
         // Add remaining time to the response
         try {
             $contest_user = ContestsUsersDAO::getByPK($r['current_user_id'], $r['contest']->getContestId());
             if ($r['contest']->getWindowLength() === null) {
                 $response['submission_deadline'] = strtotime($r['contest']->getFinishTime());
             } else {
                 $response['submission_deadline'] = min(strtotime($r['contest']->getFinishTime()), strtotime($contest_user->getAccessTime()) + $r['contest']->getWindowLength() * 60);
             }
         } catch (Exception $e) {
             // Operation failed in the data layer
             throw new InvalidDatabaseOperationException($e);
         }
     }
     // Happy ending
     $response['guid'] = $run->getGuid();
     $response['status'] = 'ok';
     // Expire rank cache
     UserController::deleteProblemsSolvedRankCacheList();
     return $response;
 }
 /**
  * Update problem contents
  *
  * @param Request $r
  * @throws ApiException
  * @throws InvalidDatabaseOperationException
  */
 public static function apiUpdate(Request $r)
 {
     self::authenticateRequest($r);
     self::validateCreateOrUpdate($r, true);
     // Validate commit message.
     Validators::isStringNonEmpty($r['message'], 'message');
     // Update the Problem object
     $valueProperties = array('public', 'title', 'validator' => array('important' => true), 'time_limit' => array('important' => true), 'validator_time_limit' => array('important' => true), 'overall_wall_time_limit' => array('important' => true), 'extra_wall_time' => array('important' => true), 'memory_limit' => array('important' => true), 'output_limit' => array('important' => true), 'stack_limit' => array('important' => true), 'email_clarifications', 'source', 'order', 'languages');
     $problem = $r['problem'];
     $requiresRejudge = self::updateValueProperties($r, $problem, $valueProperties);
     $r['problem'] = $problem;
     $response = array('rejudged' => false);
     $problemDeployer = new ProblemDeployer($problem->alias, ProblemDeployer::UPDATE_CASES);
     // Insert new problem
     try {
         //Begin transaction
         ProblemsDAO::transBegin();
         if (isset($_FILES['problem_contents']) && FileHandler::GetFileUploader()->IsUploadedFile($_FILES['problem_contents']['tmp_name'])) {
             // DeployProblemZip requires alias => problem_alias
             $r['alias'] = $r['problem_alias'];
             $problemDeployer->deploy();
             if ($problemDeployer->hasValidator) {
                 $problem->validator = 'custom';
             } elseif ($problem->validator == 'custom') {
                 throw new ProblemDeploymentFailedException('problemDeployerValidatorRequired');
             }
             // This must come before the commit in case isSlow throws an exception.
             $problem->slow = $problemDeployer->isSlow($problem);
             // Calculate output limit.
             $output_limit = $problemDeployer->getOutputLimit();
             if ($output_limit != -1) {
                 $r['problem']->setOutputLimit($output_limit);
             }
             $response['uploaded_files'] = $problemDeployer->filesToUnzip;
             $problemDeployer->commit($r['message'], $r['current_user']);
             $requiresRejudge |= $problemDeployer->requiresRejudge;
         } else {
             $problem->slow = $problemDeployer->isSlow($problem);
         }
         // Save the contest object with data sent by user to the database
         ProblemsDAO::save($problem);
         //End transaction
         ProblemsDAO::transEnd();
     } catch (ApiException $e) {
         // Operation failed in the data layer, rollback transaction
         ProblemsDAO::transRollback();
         throw $e;
     } catch (Exception $e) {
         // Operation failed in the data layer, rollback transaction
         ProblemsDAO::transRollback();
         self::$log->error('Failed to update problem');
         self::$log->error($e);
         throw new InvalidDatabaseOperationException($e);
     } finally {
         $problemDeployer->cleanup();
     }
     if ($requiresRejudge == true && OMEGAUP_ENABLE_REJUDGE_ON_PROBLEM_UPDATE == true) {
         self::$log->info('Calling ProblemController::apiRejudge');
         try {
             self::apiRejudge($r);
             $response['rejudged'] = true;
         } catch (Exception $e) {
             self::$log->error('Best efort ProblemController::apiRejudge failed', $e);
         }
     }
     if ($r['redirect'] === true) {
         header('Location: ' . $_SERVER['HTTP_REFERER']);
     }
     // All clear
     $response['status'] = 'ok';
     // Invalidar problem statement cache @todo invalidar todos los lenguajes
     foreach ($problemDeployer->getUpdatedLanguages() as $lang) {
         Cache::deleteFromCache(Cache::PROBLEM_STATEMENT, $r['problem']->getAlias() . '-' . $lang . 'html');
         Cache::deleteFromCache(Cache::PROBLEM_STATEMENT, $r['problem']->getAlias() . '-' . $lang . 'markdown');
     }
     Cache::deleteFromCache(Cache::PROBLEM_SAMPLE, $r['problem']->getAlias() . '-sample.in');
     return $response;
 }