S2OJ/web/app/models/UOJSubmissionLikeTrait.php

302 lines
7.9 KiB
PHP
Raw Normal View History

2022-11-06 02:26:21 +00:00
<?php
trait UOJSubmissionLikeTrait {
use UOJDataTrait;
/**
* @var UOJProblem|UOJContestProblem
*/
public $problem = null;
public static function getAndRememberSubmissionLanguage(array $content) {
$language = '/';
foreach ($content['config'] as $row) {
if (strEndWith($row[0], '_language')) {
$language = $row[1];
break;
}
}
if ($language != '/') {
Cookie::set('uoj_preferred_language', $language, time() + 60 * 60 * 24 * 365, '/');
}
return $language;
}
public function setProblem(array $cfg = []) {
$cfg += ['problem' => 'auto'];
$problem = $cfg['problem'] === 'auto' ? UOJProblem::query($this->info['problem_id']) : $cfg['problem'];
if (!($problem instanceof UOJProblem && $problem->info['id'] == $this->info['problem_id'])) {
return false;
}
if (isset($this->info['contest_id'])) {
if (!($problem instanceof UOJContestProblem && $problem->contest->info['id'] == $this->info['contest_id'])) {
$problem = new UOJContestProblem($problem->info, UOJContest::query($this->info['contest_id']));
if (!$problem->valid()) {
return false;
}
}
} else {
if ($problem instanceof UOJContestProblem) {
$problem = new UOJProblem($problem->info);
}
}
$this->problem = $problem;
return true;
}
public function userIsSubmitter(array $user = null) {
return $user && $this->info['submitter'] === $user['username'];
}
public function userCanView(array $user = null, array $cfg = []) {
$cfg += ['ensure' => false];
2022-11-11 23:10:34 +00:00
if (!$this->problem->userCanView($user) && !$this->userIsSubmitter($user)) {
$cfg['ensure'] && UOJResponse::page403();
return false;
} elseif (!$this->info['is_hidden']) {
2022-11-06 02:26:21 +00:00
return true;
} elseif ($this->userCanManageProblemOrContest($user)) {
return true;
} else {
$cfg['ensure'] && UOJResponse::page404();
return false;
}
}
public function userCanManageProblemOrContest(array $user = null) {
if (!$this->problem instanceof UOJContestProblem && $this->problem->userCanManage($user)) {
2022-11-06 02:26:21 +00:00
return true;
} elseif ($this->problem instanceof UOJContestProblem && $this->problem->contest->userCanManage($user)) {
return true;
} else {
return false;
}
}
public function userCanDelete(array $user = null) {
return isSuperUser($user);
}
public function publicStatus() {
return explode(', ', $this->info['status'])[0];
}
public function isWaiting() {
$status = $this->publicStatus();
return $status === 'Waiting' || $status === 'Waiting Rejudge';
}
public function hasJudged() {
return $this->publicStatus() === 'Judged';
}
public function userPermissionCodeCheck(array $user = null, $perm_code) {
switch ($perm_code) {
case 'SELF':
return $this->userIsSubmitter($user);
default:
return $this->problem->userPermissionCodeCheck($user, $perm_code);
}
}
public function viewerCanSeeStatusDetailsHTML(array $user = null) {
return $this->userIsSubmitter($user) && !$this->hasJudged();
}
public function getStatusDetailsHTML() {
return getSubmissionStatusDetailsHTML($this->publicStatus(), $this->info['status_details']);
}
public function getUri() {
return $this->info['id'];
}
public function getLink() {
2023-02-17 02:57:55 +00:00
return '<a class="text-decoration-none" href="' . HTML::url($this->getUri()) . '">#' . $this->info['id'] . '</a>';
2022-11-06 02:26:21 +00:00
}
public function getResult($key = null) {
if (!isset($this->info['result'])) {
return null;
}
$result = json_decode($this->info['result'], true);
if ($key === null) {
return $result;
}
return isset($result[$key]) ? $result[$key] : null;
}
public function getContent($key = null) {
if (!isset($this->info['content'])) {
return null;
}
$content = json_decode($this->info['content'], true);
if ($key === null) {
return $content;
}
return isset($content[$key]) ? $content[$key] : null;
}
public function echoContent($cfg = []) {
$cfg += [
'list_group' => false,
];
2022-11-06 02:26:21 +00:00
$content = $this->getContent();
if (!$content) {
return false;
}
$card_class = 'card mb-3';
$card_header_class = 'card-header fw-bold';
$card_body_class = 'card-body';
$card_footer_class = 'card-footer';
if ($cfg['list_group']) {
$card_class = 'list-group-item';
$card_header_class = 'fw-bold mb-2';
$card_body_class = '';
$card_footer_class = 'text-end mt-2';
}
if ($content['remote_submission_id']) {
echo <<<EOD
<div class="{$card_class}">
<div class="{$card_header_class}">
远程提交
</div>
<div class="{$card_body_class}">
远程提交 ID: {$content['remote_submission_id']}
<br>
源代码请在「详细信息」选项卡查看。
</div>
</div>
EOD;
return true;
}
2022-11-06 02:26:21 +00:00
$zip_file = new ZipArchive();
2022-11-11 23:10:34 +00:00
if ($zip_file->open(UOJContext::storagePath() . $content['file_name'], ZipArchive::RDONLY) !== true) {
2022-11-06 02:26:21 +00:00
echo <<<EOD
<div class="{$card_class}">
<div class="{$card_header_class}">
提交内容
</div>
<div class="{$card_body_class} text-danger">
木有
</div>
</div>
EOD;
2022-11-06 02:26:21 +00:00
return false;
}
2022-11-11 23:10:34 +00:00
2022-11-06 02:26:21 +00:00
$config = [];
foreach ($content['config'] as $val) {
$config[$val[0]] = $val[1];
}
2022-11-11 23:10:34 +00:00
2022-11-06 02:26:21 +00:00
foreach ($this->problem->getSubmissionRequirement() as $req) {
if ($req['type'] == "source code") {
$file_content = $zip_file->getFromName("{$req['name']}.code");
if ($file_content === false) {
$file_content = '';
}
if (isset($config["{$req['name']}_language"])) {
$file_language = $config["{$req['name']}_language"];
} else {
$file_language = '?';
}
$file_content = uojTextEncode($file_content, array('allow_CR' => true, 'html_escape' => true));
2022-11-11 23:10:34 +00:00
$footer_text = UOJLocale::get('problems::source code') . ': ';
2022-11-06 02:26:21 +00:00
$footer_text .= UOJLang::getLanguageDisplayName($file_language);
$sh_class = UOJLang::getLanguagesCSSClass($file_language);
echo <<<EOD
<div class="{$card_class}">
<div class="{$card_header_class}">
{$req['name']}
</div>
<div class="{$card_body_class} copy-button-container">
<pre class="mb-0"><code class="$sh_class bg-light rounded p-3">{$file_content}\n</code></pre>
</div>
<div class="{$card_footer_class}">$footer_text</div>
</div>
EOD;
2022-11-06 02:26:21 +00:00
} elseif ($req['type'] == "text") {
$file_content = $zip_file->getFromName("{$req['file_name']}", 504);
if ($file_content === false) {
$file_content = '';
}
$file_content = strOmit($file_content, 500);
$file_content = uojTextEncode($file_content, array('allow_CR' => true, 'html_escape' => true));
$footer_text = UOJLocale::get('problems::text file');
echo <<<EOD
<div class="{$card_class}">
<div class="{$card_header_class}">
{$req['file_name']}
</div>
<div class="{$card_body_class}">
<pre class="mb-0 bg-light rounded p-3">\n{$file_content}\n</pre>
</div>
<div class="{$card_footer_class}">{$footer_text}</div>
</div>
EOD;
2022-11-06 02:26:21 +00:00
}
}
$zip_file->close();
return true;
}
protected function echoStatusBarTDBase($name, array $cfg) {
switch ($name) {
case 'id':
echo $this->getLink();
break;
case 'problem':
if ($this->problem) {
echo $this->problem->getLink(isset($cfg['problem_title']) ? $cfg['problem_title'] : []);
} else {
echo '<span class="text-danger">?</span>';
}
break;
case 'submitter':
case 'owner':
case 'hacker':
echo UOJUser::getLink($this->info[$name]);
2022-11-06 02:26:21 +00:00
break;
case 'used_time':
if ($cfg['show_actual_score']) {
2022-11-11 23:10:34 +00:00
echo $this->info['used_time'] . 'ms';
2022-11-06 02:26:21 +00:00
} else {
echo '/';
}
break;
case 'used_memory':
if ($cfg['show_actual_score']) {
2022-11-11 23:10:34 +00:00
echo $this->info['used_memory'] . 'kb';
2022-11-06 02:26:21 +00:00
} else {
echo '/';
}
break;
case 'tot_size':
if ($this->info['tot_size'] < 1024) {
echo $this->info['tot_size'] . 'b';
} else {
echo sprintf("%.1f", $this->info['tot_size'] / 1024) . 'kb';
}
break;
case 'submit_time':
case 'judge_time':
2022-11-11 23:10:34 +00:00
echo '<small>', $this->info[$name], '</small>';
2022-11-06 02:26:21 +00:00
break;
default:
echo '?';
break;
}
}
}