function problem_display($row)
{
    global $cache;
    $cache_id = problem_cache_id($row['contest_id'], $row['prob_id']) . '.htm';
    if ($data = $cache->get($cache_id)) {
        echo $data;
    } else {
        $prob =& problem_load($row);
        $data = "<script type=\"text/javascript\">var problemVersion = {$row['version']};</script>";
        // Display heading
        $data .= '<div id="problem_title">';
        $data .= 'Problem: <b>' . $row['prob_id'] . '</b>';
        $data .= '</div>';
        $data .= '<div id="problem">';
        $data .= '<div id="problem_header">';
        $data .= '<b>Summary:</b> ' . $row['summary'];
        $data .= '<br /><b>Weight:</b> ' . $row['weight'];
        $data .= '<br /><b>Time limit:</b> ' . $row['time_limit'] . ' second(s)';
        $data .= '<br /><b>Memory limit:</b> ' . $row['mem_limit'] . ' MB';
        $data .= '<br /><b>Test cases:</b> ' . count($prob->tests);
        $data .= '</div>';
        // Display problem description (body)
        $data .= "<div id=\"problem_body\">\n";
        $data .= "<b>Description:</b><br /><div id=\"problem_body_text\">\n";
        $data .= $prob->body;
        $data .= "</div></div>\n";
        // Display the formatting constraints
        $data .= "<div id=\"problem_constraints\">\n";
        $data .= "<b>Formatting/Constraints:</b>\n";
        $data .= "<ul>\n";
        foreach ($prob->constraints as $type => $points) {
            $data .= '<li><i>' . ucfirst(strtolower($type)) . ":</i>\n<ol>\n";
            foreach ($points as $point) {
                $data .= "<li>{$point}</li>\n";
            }
            $data .= "</ol>\n</li>\n";
        }
        $data .= "</ul>\n</div>\n";
        // Display the examples
        $data .= '<div id="problem_examples">';
        $data .= "<b>Examples:</b>\n";
        $data .= "<ol>\n";
        foreach ($prob->examples as $id => $text) {
            $data .= "<li>\n";
            $data .= "<b>Input:</b> <pre>";
            $data .= html_strip($prob->tests[$id]->getInput());
            $data .= "</pre>\n";
            $data .= "<b>Output:</b> <pre>";
            $data .= html_strip($prob->tests[$id]->getOutput());
            $data .= "</pre>\n";
            $data .= "<b>Analysis:</b> <div class=\"analysis\">\n";
            $data .= $text;
            $data .= "</div>\n";
            $data .= "</li>\n";
        }
        if (count($prob->examples) == 0) {
            $data .= "None.\n";
        }
        $data .= "</ol></div>\n";
        $data .= "</div>\n";
        $cache->save($data, $cache_id);
        echo $data;
    }
}
function test_cases(&$problem, &$solution, $tests = null, $verbose = true)
{
    global $cfg;
    $buf = '';
    $passed = '';
    // Get valid extensions for language
    $lang = $solution['language'];
    require_once $cfg['dir']['languages'] . '/' . $solution['language'] . '.php';
    $extn_func = 'lang_' . $lang . '_extensions';
    $extns = $extn_func();
    $temp_dir = tempnam('/tmp', 'ogs');
    unlink($temp_dir);
    mkdir($temp_dir);
    $source_file = $temp_dir . '/test.' . $extns[0];
    $exec_file = tempnam($temp_dir, 'ogs');
    // Load problem
    $prob = problem_load($problem);
    // Make the source code file
    $handle = fopen($source_file, 'w');
    $header_func = 'lang_' . $lang . '_header';
    fwrite($handle, $header_func());
    fwrite($handle, "\n");
    fwrite($handle, $solution['source']);
    fclose($handle);
    // Compile in appropriate language
    dump("<pre class=\"testing\">");
    dump("Compiling [language={$lang}]...\n");
    $compile_func = 'lang_' . $lang . '_compile';
    if (!$compile_func($source_file, $exec_file)) {
        // Error in compilation, error in $exec_file
        $error = file_get_contents($exec_file);
        dump("</pre>\n<pre class=\"test_error\">");
        dump("<b>Compilation Error:</b>\n<br />{$error}");
        dump("</pre>\n");
    } else {
        dump("Verifying binary...<b>OK</b>");
        dump("</pre>\n");
        dump("<pre class=\"testing\">");
        $count = 0;
        // test case counter
        $correct = 0;
        // correct test cases
        // Check for test case filter
        $custom = false;
        if ($tests == null) {
            $tests_to_do =& $prob->tests;
        } else {
            if (is_array($tests)) {
                $tests_to_do = array();
                /*
                			foreach ($tests as $id) {
                				array_push($tests_to_do, $prob->tests[$id]);
                			}*/
                /*The following code pushes all tests into $tests, modified for TCL*/
                /* Does not modify the calling function, just makes the params obsolete */
                $tests2 = array_keys($prob->tests);
                foreach ($tests2 as $id) {
                    array_push($tests_to_do, $prob->tests[$id]);
                }
            } else {
                //Custom testing
                $custom = true;
                $tests_to_do = array(new TestCase(1, null, null));
                $tests_to_do[0]->input = $tests;
                $tests_to_do[0]->output = '';
            }
        }
        foreach ($tests_to_do as $test_case) {
            dump("Test case {$count}: ");
            // Determine limits
            $time_limit = $problem['time_limit'];
            $mem_limit = $problem['mem_limit'];
            if ($test_case->time_limit != null) {
                $time_limit = $test_case->time_limit;
            }
            if ($test_case->mem_limit != null) {
                $mem_limit = $test_case->mem_limit;
            }
            // Special case for Java
            if ($lang == 'java') {
                $mem_limit += $cfg['tester']['java_mem_overhead'];
            }
            // Execute and get output in $output
            $output =& $test_case->getOutput();
            $test_input =& $test_case->getInput();
            $ret = system_gauge($exec_file, $test_input, $output, $time_limit, $mem_limit, $exec_time);
            if ($custom == true && $ret != 1) {
                dump("<b>done</b>\n");
                dump("</pre>\n<pre class=\"test_success\">");
                dump("<b>Execution time:</b> {$exec_time} seconds\n");
                dump("<b>Program output:</b>\n");
                // Print output returned
                dump($output);
                dump("(Please verify correctness of output yourself)\n");
            } else {
                if ($ret == 0) {
                    dump("<b>OK</b> ({$exec_time} seconds)\n");
                    ++$correct;
                    $passed .= '1';
                } else {
                    if ($ret == 1) {
                        // Execution failed, error msg in $output
                        dump("<b>failed </b>");
                        dump("\t(Reason: {$output})\n");
                        $passed .= '0';
                    } else {
                        if ($ret == 2) {
                            dump("<b>failed</b>");
                            /*Remove the comments if you wish to show which cases failed*/
                            /*
                                            if (!$verbose) {
                                                dump("\t(Reason: Output Incorrect)\n");
                                            } else {
                                                dump("\t(Reason: Output incorrect, shown BELOW)\n");
                            					dump("-------------- Input used --------------\n");
                            					dump($test_input);
                            					
                            					dump("\n-------------- Your output --------------\n");
                            					dump($output);
                            					if ($output{strlen($output)-1} != "\n") {
                            						dump("\n");
                            					}
                            					
                            					dump("-------------- Expected output --------------\n");
                            					$output =& $test_case->getOutput();
                            					dump($output);
                            					if ($output{strlen($output)-1} != "\n")
                            						dump("\n");
                            					dump("\n");
                                            }*/
                            /* And comment the following line */
                            dump("\t(Reason: Output Incorrect)\n");
                            $passed .= '0';
                        }
                    }
                }
            }
            ++$count;
        }
        dump("</pre>\n");
        if ($custom == false) {
            if ($correct == $count) {
                $status = 'test_success';
            } else {
                if ($correct > 0) {
                    $status = 'test_warning';
                } else {
                    $status = 'test_error';
                }
            }
            dump("<pre class=\"{$status}\">");
            dump("<b>Total cases passed: {$correct}/{$count}</b>");
            dump("</pre>\n");
        }
    }
    // Remove temprary files
    @unlink($source_file);
    @unlink($exec_file);
    @rmdir($temp_dir);
    if ($count == 0) {
        return 0;
    }
    if ($correct == $count) {
        return array(1, $passed);
    } else {
        return array($correct * 1.0 / $count, $passed);
    }
}
require_once $cfg['dir']['languages'] . '/' . $soln['language'] . '.php';
$func = 'lang_' . $soln['language'] . '_description';
echo '<b>Language:</b> ' . $func();
echo '<pre class="solution_code">';
echo html_escape($soln['source']);
echo '</pre>';
ob_end_flush();
if ($contest['end_future'] != '1' && ($contest['tested'] == 1 || auth_user_in_group('Administrators'))) {
    // Show test-case passing summary
    require_once 'problem.php';
    require_once 'HTML/Table.php';
    $table = new HTML_Table();
    $table->addRow(array('Test Case', 'Passed', 'Input'), null, 'TH');
    $res =& db_query('problem_by_id', array($_GET['prob_id'], $_GET['id']));
    $res->fetchInto($problem);
    $prob = problem_load($problem);
    $show_eg = strlen($soln['passed']) == count($prob->tests);
    $i = '0';
    foreach ($prob->tests as $id => $case) {
        if (!$show_eg && array_key_exists($id, $prob->examples)) {
            continue;
        }
        $passed = "no";
        if ($soln['passed'][$i] == '1') {
            $passed = "yes";
        }
        $table->addRow(array($i, $passed, '<pre>' . $case->getInput() . '</pre>'));
        ++$i;
    }
    $table->altRowAttributes(1, null, array("class" => "altrow"));
    echo '<div class="overflow">' . $table->toHtml() . '</div>';
function submit_perform($contest_id, $team_id, &$problem, &$solution, $mode = null, $custom = null)
{
    global $cfg;
    require_once 'tester.php';
    $prob = problem_load($problem);
    // Compile and Test first
    if ($custom != null) {
        $ret = test_cases($problem, $solution, $custom);
    } else {
        if ($mode == 'practice') {
            $ret = test_cases($problem, $solution);
        } else {
            // only test examples
            $ret = test_cases($problem, $solution, array_keys($prob->examples));
        }
    }
    $result = $ret[0];
    if ($mode == 'submit') {
        // Submit after testing
        if ($result == 1) {
            echo "<pre class=\"testing\">\n";
            echo "Checking for previous submission(s)...";
            $res = db_query('solution_by_id', array($contest_id, $team_id, $problem['prob_id']));
            $res->fetchInto($row);
            $res->free();
            $initial = $problem['weight'];
            if ($row['submits'] == '0') {
                echo "none\n";
            } else {
                echo "found\n";
                echo "Resubmission count...{$row['submits']}\n";
                $initial1 = score_resubmit_penalty($initial, $row['submits']);
                $percent = (1 - $initial1 / $initial) * 100;
                echo "Cumulative penalty...{$percent}%\n";
                $initial = $initial1;
            }
            $res = db_query('contest_time', $contest_id);
            $res->fetchInto($row2);
            $score = score_calc($initial, $row['elapsed2'], $row2['time']);
            db_query('submit_solution', array($solution['language'], $solution['source'], $score, $contest_id, $team_id, $problem['prob_id']));
            // Now that user has submitted, we clear his draft
            db_query('delete_draft_by_user', $_SESSION['user_id']);
            echo "</pre>\n<pre class=\"test_success\">";
            echo "<b>Solution to problem </b><i>{$problem['prob_id']}</i><b> submitted for ";
            printf("%.3f points!</b>\n", $score);
            echo "Note: The above score is not final. The actual score will be determined by the system tests.";
            echo "</pre>\n";
        } else {
            echo "<pre class=\"test_error\"><b>Solution failed to pass all tests. Refusing to submit!</b></pre>";
        }
    }
}