generated from NJUPTAAA/NOJ_Extension_Babel_Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubmitter.php
102 lines (93 loc) · 3.19 KB
/
Submitter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace App\Babel\Extension\uva;
use App\Babel\Submit\Curl;
use App\Models\CompilerModel;
use App\Models\JudgerModel;
use App\Models\OJModel;
use Illuminate\Support\Facades\Validator;
use Requests;
use Log;
class Submitter extends Curl
{
protected $sub;
public $post_data = [];
protected $oid;
protected $selectedJudger;
public function __construct(&$sub, $all_data)
{
$this->sub = &$sub;
$this->post_data = $all_data;
$judger = new JudgerModel();
$this->oid = OJModel::oid('uva');
if (is_null($this->oid)) {
throw new Exception("Online Judge Not Found");
}
$judger_list = $judger->list($this->oid);
$this->selectedJudger = $judger_list[array_rand($judger_list)];
}
private function _login()
{
$response = $this->grab_page([
'site' => "https://onlinejudge.org/",
'oj' => 'uva',
'handle' => $this->selectedJudger['handle'],
]);
if (strpos($response, 'Logout') === false) {
$post_data = [
'username' => $this->selectedJudger["handle"],
'passwd' => $this->selectedJudger["password"],
'remember' => 'yes',
];
$inputs = preg_match_all('/<input type="\w*" name="(op2|lang|force_session|return|message|loginfrom|cbsecuritym3|\w[0-9a-z]{32})" value="(.*?)" \/>/', $response, $matches);
for ($i = 0; $i < $inputs; ++$i) {
$post_data[$matches[1][$i]] = $matches[2][$i];
}
$this->post_data([
'site' => 'https://onlinejudge.org/index.php?option=com_comprofiler&task=login',
'data' => $post_data,
'oj' => 'uva',
'ret' => false,
'handle' => $this->selectedJudger['handle'],
]);
}
}
private function _submit()
{
$params = [
'problemid' => $this->post_data['iid'],
'language' => $this->post_data['lang'],
'code' => $this->post_data['solution'],
];
$response = $this->post_data([
'site' => "https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=25&page=save_submission",
'data' => $params,
'oj' => 'uva',
'ret' => true,
'returnHeader' => true,
'handle' => $this->selectedJudger['handle'],
]);
$this->sub['jid'] = $this->selectedJudger["jid"];
if (preg_match('/Submission\+received\+with\+ID\+(\d+)/', $response, $match)) {
$this->sub['remote_id'] = $match[1];
} else {
sleep(1);
throw new \Exception("Submission error");
// $this->sub['verdict'] = 'Submission Error';
}
}
public function submit()
{
$validator = Validator::make($this->post_data, [
'pid' => 'required|integer',
'coid' => 'required|integer',
'iid' => 'required|integer',
'solution' => 'required',
]);
if ($validator->fails()) {
$this->sub['verdict'] = "System Error";
return;
}
$this->_login();
$this->_submit();
}
}