Ejemplo n.º 1
0
function processRemoveContestRequest($request)
{
    $prfx = DB_PREFIX;
    $user_row = RequestUtils::testSession($request->sessionID);
    $contest_id = $request->contestID;
    //simple security check
    if (!is_numeric($contest_id)) {
        throwBusinessLogicError(14);
    }
    if ($user_row['user_type'] !== 'SuperAdmin') {
        throwBusinessLogicError(0);
    } else {
        if ($contest_id === 0) {
            throwBusinessLogicError(16);
        }
    }
    //get all users of the contest
    $contest_user_rows = Data::getRows("SELECT id FROM {$prfx}user WHERE contest_id={$contest_id}");
    //compose where clause for delete query
    $where_user_id = "";
    while (list($user_id) = Data::getNextRow($contest_user_rows)) {
        $where_user_id .= 'user_id=' . $user_id . 'OR';
    }
    $where_user_id .= '0=1';
    Data::submitModificationQuery("DELETE FROM {$prfx}contest WHERE id={$contest_id}");
    Data::submitModificationQuery("DELETE FROM {$prfx}problem WHERE contest_id={$contest_id}");
    Data::submitModificationQuery("DELETE FROM {$prfx}problem_status WHERE {$where_user_id}");
    Data::submitModificationQuery("DELETE FROM {$prfx}session WHERE {$where_user_id}");
    Data::submitModificationQuery("DELETE FROM {$prfx}submission_history WHERE {$where_user_id}");
    Data::submitModificationQuery("DELETE FROM {$prfx}user WHERE contest_id={$contest_id}");
    return new AcceptedResponse();
}
Ejemplo n.º 2
0
function processGetUsersRequest($request)
{
    $user_row = RequestUtils::testSession($request->sessionID);
    $prfx = DB_PREFIX;
    $user_type = $user_row['user_type'];
    $contest_id = RequestUtils::getRequestedContest($request->contestID, $user_row['contest_id'], $user_type);
    //make superadmin possible to get users of zero-contest
    if ($user_type == 'SuperAdmin' && ($request->contestID == 0 || $request->contestID == -1)) {
        $contest_id = 0;
    }
    if ($contest_id < 0 || $user_type === 'Participant') {
        throwBusinessLogicError(0);
    }
    $rows = Data::getRows(sprintf("SELECT * FROM {$prfx}user WHERE contest_id={$contest_id}"));
    $res = new GetUsersResponse();
    $res->users = array();
    while ($row = Data::getNextRow($rows)) {
        $ud = new UserDescription();
        $ud->userID = (int) $row['id'];
        $ud->login = $row['login'];
        $ud->password = $row['password'];
        $ud->dataValue = Data::_unserialize($row['user_data'], array());
        $ud->userType = $row['user_type'];
        $res->users[] = $ud;
    }
    return $res;
}
Ejemplo n.º 3
0
 public static function hasRows($query)
 {
     $rows = Data::getRows($query);
     $row = Data::getNextRow($rows);
     if ($row) {
         return true;
     } else {
         return false;
     }
 }
Ejemplo n.º 4
0
function processAvailableContestsRequest($request)
{
    $prfx = DB_PREFIX;
    $res = new AvailableContestsResponse();
    $res->contests = array();
    $contest_rows = Data::getRows("SELECT * FROM {$prfx}contest");
    while ($row = Data::getNextRow($contest_rows)) {
        $c = Data::_unserialize($row['settings']);
        $c->contestID = (int) $row['id'];
        $res->contests[] = $c;
    }
    return $res;
}
Ejemplo n.º 5
0
 /**
  * Updates user result for a specified problem
  * @static
  * @param  $user_id id of the user
  * @param  $problem_id id of the problem
  * @param  $choice type of choice, Self of Last
  * @param  $transfer_settings settings of transfer
  * @param  $plugin plugin associated with the problem, needed only when $choice == Best to find best submission     
  * @param  $last_result last result of user if present
  * @return array array with results for results table
  */
 public static function getUserResults($user_id, $problem_id, $choice, $transfer_settings, $plugin, $last_result = null)
 {
     $prfx = DB_PREFIX;
     //get new result
     $all_results_request = "\n                    SELECT result, submission_time\n                    FROM {$prfx}submission_history\n                    WHERE user_id={$user_id}\n                    ORDER BY submission_time DESC\n        ";
     if ($choice === 'Last') {
         if ($last_result) {
             $new_result = $last_result;
         } else {
             //get all rows sorted with descending time, and then get first
             $row = Data::getRow($all_results_request);
             if (!$row) {
                 $new_result = array();
             } else {
                 $new_result = Data::_unserialize($row['result']);
             }
         }
     } else {
         // $choice === 'Best'
         $history_results = array();
         if ($last_result) {
             $history_results[] = $last_result;
         }
         $rows = Data::getRows($all_results_request);
         while ($row = Data::getNextRow($rows)) {
             $history_results[] = $row['result'];
         }
         $num_results = count($history_results);
         if ($num_results == 0) {
             $new_result = array();
         } else {
             $new_result = $history_results[$num_results - 1];
             for ($i = $num_results - 2; $i >= 0; $i--) {
                 if ($plugin->compareResults($history_results[$i], $new_result) === 1) {
                     $new_result = $history_results[$i];
                 }
             }
         }
     }
     $transfer = new ResultsTransfer($transfer_settings);
     return $transfer->convert($new_result);
 }
Ejemplo n.º 6
0
function processAvailablePluginsRequest($request)
{
    //Uncomment to check permissions
    $user_row = RequestUtils::testSession($request->sessionID);
    if ($user_row['user_type'] !== 'SuperAdmin' && $user_row['user_type'] !== 'ContestAdmin') {
        throwBusinessLogicError(0);
    }
    $prfx = DB_PREFIX;
    if ($request->pluginSide === 'Client') {
        $table_name = $prfx . "client_plugin";
    } else {
        $table_name = $prfx . "server_plugin";
    }
    $rows = Data::getRows("SELECT * FROM {$table_name}");
    $res = new AvailablePluginsResponse();
    $res->aliases = array();
    $res->descriptions = array();
    while ($row = Data::getNextRow($rows)) {
        $res->aliases[] = $row['alias'];
        $res->descriptions[] = $row['description'];
    }
    return $res;
}
Ejemplo n.º 7
0
/**
 * creates queries to change problem set
 * @param $problems new problems
 * @param $contest_id contest id
 * @return array() list of temporary files. NULL if the existing file was used
 */
function queriesToAdjustProblems($problems, $contest_id)
{
    $prfx = DB_PREFIX;
    $changed_probs = array();
    //problems that will be changed by request
    $temp_probs = array();
    //find all contest problems
    $prob2settings = array();
    $res = Data::getRows(sprintf("SELECT * FROM {$prfx}problem WHERE contest_id=%s", Data::quote_smart($contest_id)));
    while ($row = Data::getNextRow($res)) {
        $prob2settings[$row['id']] = Data::_unserialize($row['contest_settings']);
    }
    $contest_pos = 1;
    foreach ($problems as $p) {
        $col_value = array();
        //new problem must have 1) data 2) settings
        //set contest id
        $col_value['contest_id'] = $contest_id;
        $col_value['contest_pos'] = $contest_pos++;
        if ($p->id != -1 && !isset($prob2settings[$p->id])) {
            throwBusinessLogicError(4);
        }
        //find problem file or make temporary if a new problem was sent
        if ($p->problem) {
            $problem_file = getTemporaryProblemFile();
            @file_put_contents($problem_file, $p->problem) or throwServerProblem(200, 'failed to write problem file: ' . $problem_file);
            $temp_probs[] = $problem_file;
        } else {
            if ($p->id < 0) {
                throwBusinessLogicError(1);
            }
            $problem_file = getProblemFile($p->id);
            $temp_probs[] = NULL;
        }
        $problem = new Problem($problem_file);
        //get server plugin
        //TODO improve security here
        $plugin_alias = $problem->getServerPlugin();
        require_once getServerPluginFile($plugin_alias);
        $plugin = new $plugin_alias($problem);
        //TODO consider calling updaters here instead of manual insertion of values
        //TODO recheck all values if new plugin specified
        $col_value['checker_columns'] = serialize($plugin->getColumnNames());
        $col_value['result_columns'] = serialize(array());
        //copy per contest settings
        if ($p->settings) {
            if ($p->id != -1) {
                $new_settings = $prob2settings[$p->id];
                copyValues($p->settings, $new_settings);
            } else {
                $new_settings = $p->settings;
            }
            $col_value['contest_settings'] = serialize($new_settings);
        } else {
            if ($p->id < 0) {
                throwBusinessLogicError(1);
            }
        }
        //query depends on whether we add or change a problem
        if ($p->id == -1) {
            Data::submitModificationQuery(Data::composeInsertQuery('problem', $col_value));
        } else {
            Data::submitModificationQuery(Data::composeUpdateQuery('problem', $col_value, "id='{$p->id}'"));
            $changed_probs[$p->id] = 1;
        }
    }
    //delete extra problems
    foreach (array_keys($prob2settings) as $id) {
        if (!isset($changed_probs[$id])) {
            Data::submitModificationQuery("DELETE FROM {$prfx}problem WHERE id='{$id}'");
        }
    }
    return $temp_probs;
}
Ejemplo n.º 8
0
function processGetContestDataRequest($request)
{
    $prfx = DB_PREFIX;
    $is_anonymous = is_null($request->sessionID);
    if (!$is_anonymous) {
        //get user_id or die, if session is invalid
        $userRow = RequestUtils::testSession($request->sessionID);
        $user_id = $userRow['id'];
        //authorize user for this operation
        // get contest ID
        $user_type = $userRow['user_type'];
        //compare requested contest and user contest
        $contest_id = RequestUtils::getRequestedContest($request->contestID, $userRow['contest_id'], $user_type);
    } else {
        $contest_id = $request->contestID;
    }
    if ($contest_id <= 0) {
        throwBusinessLogicError(0);
    }
    //create response
    $res = new GetContestDataResponse();
    //fill contest description with data
    //query db
    $row = Data::getRow(sprintf("SELECT * FROM {$prfx}contest WHERE id=%s", Data::quote_smart($contest_id))) or throwBusinessLogicError(14);
    //TODO remove this code duplication, the code is similar to AvailableContests.php
    $c = Data::_unserialize($row['settings']);
    $c->contestID = (int) $row['id'];
    $res->contest = $c;
    //fill problem data
    if ($is_anonymous) {
        return $res;
    }
    //query db to find out problems
    $problems_rows = Data::getRows(sprintf("SELECT * FROM {$prfx}problem WHERE contest_id=%s ORDER BY contest_pos ASC", Data::quote_smart($contest_id)));
    //fill problems data
    $res->problems = array();
    $info_type = $request->infoType;
    $extended_data = $request->extendedData;
    while ($row = Data::getNextRow($problems_rows)) {
        $pd = new ProblemDescription();
        $res->problems[] = $pd;
        $pd->id = (int) $row['id'];
        $pd->settings = Data::_unserialize($row['contest_settings']);
        //do we need any information
        if ($info_type == 'NoInfo') {
            continue;
        }
        //do we need to return some info for this problem
        if (!is_null($extended_data) && !in_array($pd->id, $extended_data)) {
            continue;
        }
        $problem = new Problem(getProblemFile($pd->id));
        if ($info_type !== 'NoInfo') {
            //fill extended data: statement or statementData and answerData
            if ($info_type === "ParticipantInfo") {
                $pd->problem = $problem->getParticipantVersion($user_id)->getProblemBytes();
            } elseif ($info_type === "AdminInfo") {
                if ($user_type === "Participant") {
                    throwBusinessLogicError(0);
                }
                $pd->problem = $problem->getProblemBytes();
            }
        }
    }
    return $res;
}
Ejemplo n.º 9
0
function processGetContestResultsRequest($request)
{
    $prfx = DB_PREFIX;
    //get $is_anonymous, $contest_id, $user_contest_row, $user_contest_start_time
    if (!is_null($request->sessionID)) {
        $is_anonymous = false;
        $user_contest_row = RequestUtils::testSession($request->sessionID);
        $contest_id = RequestUtils::getRequestedContest($request->contestID, $user_contest_row['contest_id'], $user_contest_row['user_type']);
        if ($contest_id < 0) {
            throwBusinessLogicError(14);
        }
        $user_contest_start_time = DateMySQLToPHP($user_contest_row['contest_start']);
        $user_contest_finish_time = DateMySQLToPHP($user_contest_row['contest_finish']);
    } else {
        $is_anonymous = true;
        $contest_id = $request->contestID;
        $user_contest_start_time = null;
        //contest was not started for anonymous
        $user_contest_finish_time = null;
        //and was not finished
    }
    //get $serialized_contest_settings
    $need_request_for_contest_data = $is_anonymous || $user_contest_row['user_type'] === 'SuperAdmin';
    if ($need_request_for_contest_data) {
        if ($contest_id === 0) {
            throwBusinessLogicError(14);
        }
        $contest_row = Data::getRow(sprintf("SELECT *\r\n             FROM {$prfx}contest\r\n             WHERE id=%s\r\n            ", Data::quote_smart($contest_id)));
        if (!$contest_row) {
            throwBusinessLogicError(14);
        }
        $serialized_contest_settings = $contest_row['settings'];
    } else {
        $serialized_contest_settings = $user_contest_row['settings'];
    }
    //get $contest_settings
    $contest_settings = Data::_unserialize($serialized_contest_settings);
    //get $is_admin
    $is_admin = !$is_anonymous && ($user_contest_row['user_type'] === 'SuperAdmin' || $user_contest_row['user_type'] === 'ContestAdmin');
    //get $permission
    $ctime = getCurrentContestTime($contest_settings, $user_contest_start_time, $user_contest_finish_time);
    if (!$is_admin) {
        if ($ctime['interval'] === 'before') {
            throwBusinessLogicError(19);
        }
        if ($ctime['interval'] === 'contest' && !$ctime['is_ending']) {
            $permission = $contest_settings->resultsAccessPolicy->contestPermission;
        } else {
            if ($ctime['is_ending']) {
                $permission = $contest_settings->resultsAccessPolicy->contestEndingPermission;
            } else {
                if ($ctime['interval'] === 'after' && !$ctime['is_ending']) {
                    $permission = $contest_settings->resultsAccessPolicy->afterContestPermission;
                }
            }
        }
    } else {
        $permission = 'FullAccess';
    }
    //test rights
    if ($permission === 'NoAccess') {
        throwBusinessLogicError(0);
    }
    if ($is_anonymous && $permission === "OnlySelfResults") {
        throwBusinessLogicError(0);
    }
    //get problem rows
    $all_problems_rows = Data::getRows(sprintf("SELECT *\r\n                                    FROM {$prfx}problem\r\n                                    WHERE {$prfx}problem.contest_id=%s\r\n                                    ORDER BY {$prfx}problem.contest_pos ASC", Data::quote_smart($contest_id)));
    //get users rows
    if ($permission === 'FullAccess') {
        $all_users_rows = Data::getRows(sprintf("SELECT *\r\n                                     FROM {$prfx}user\r\n                                     WHERE contest_id=%s", Data::quote_smart($contest_id)));
    } else {
        /* if $permission === 'OnlySelfResults'*/
        $all_users_rows = $user_contest_row;
    }
    //create result
    $result = new GetContestResultsResponse();
    //fill columns ids
    $result->headers = array();
    $result->minorHeaders = array();
    //the first column with 'user_id' and 'login'
    if ($is_admin) {
        $result->headers[] = 'admin info';
        $result->minorHeaders[] = array('id', 'login');
    }
    //column with participant data
    $result->headers[] = 'participant';
    //get participant subcolumns
    $data_subs = array();
    $contest_user_data = $contest_settings->data;
    if ($contest_user_data) {
        foreach ($contest_settings->data as $df) {
            if ($is_admin || $df->showInResult) {
                $data_subs[] = $df->data;
            }
        }
    }
    $result->minorHeaders[] = $data_subs;
    //columns with problems
    $problem_ids = array();
    $problem_cols = array();
    while ($problem_row = Data::getNextRow($all_problems_rows)) {
        $problem_ids[] = $problem_row['id'];
        $result->headers[] = $problem_row['name'];
        $col_names = Data::_unserialize($problem_row['column_names']);
        $result->minorHeaders[] = $col_names;
        $problem_cols[] = $col_names;
    }
    //fill results table
    $result->table = array();
    if ($permission === 'OnlySelfResults') {
        $result->table[] = getTableRow($user_contest_row, $is_admin, $problem_ids, $problem_cols, $contest_settings->data);
        $result->userLine = 0;
    } else {
        $ind = 0;
        $result->userLine = -1;
        while ($user_row = Data::getNextRow($all_users_rows)) {
            $result->table[] = getTableRow($user_row, $is_admin, $problem_ids, $problem_cols, $contest_settings->data);
            if ($user_row['id'] == $user_contest_row['id']) {
                $result->userLine = $ind;
            }
            $ind++;
        }
    }
    return $result;
}