|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Http\Controllers\VirtualJudge\CodeForces; |
|
3
|
|
|
|
|
4
|
|
|
use App\Http\Controllers\VirtualJudge\Curl; |
|
5
|
|
|
use App\Models\JudgerModel; |
|
6
|
|
|
use App\Models\ProblemModel; |
|
7
|
|
|
use App\Models\ResponseModel; |
|
8
|
|
|
use App\Models\SubmissionModel; |
|
9
|
|
|
use Illuminate\Support\Facades\Validator; |
|
10
|
|
|
|
|
11
|
|
|
class CodeForces extends Curl |
|
12
|
|
|
{ |
|
13
|
|
|
protected $sub; |
|
14
|
|
|
public $post_data=[]; |
|
15
|
|
|
protected $selectedJudger; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(& $sub, $all_data) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->sub=& $sub; |
|
20
|
|
|
$this->post_data=$all_data; |
|
21
|
|
|
$judger=new JudgerModel(); |
|
22
|
|
|
$judger_list=$judger->list(2); |
|
23
|
|
|
$this->selectedJudger=$judger_list[array_rand($judger_list)]; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
private function codeForcesLogin() |
|
27
|
|
|
{ |
|
28
|
|
|
$response=$this->grab_page('http://codeforces.com', 'codeforces', [], $this->selectedJudger["handle"]); |
|
29
|
|
|
if (!(strpos($response, 'Logout')!==false)) { |
|
30
|
|
|
$response=$this->grab_page('http://codeforces.com/enter', 'codeforces', [], $this->selectedJudger["handle"]); |
|
31
|
|
|
|
|
32
|
|
|
$exploded=explode("name='csrf_token' value='", $response); |
|
33
|
|
|
$token=explode("'/>", $exploded[2])[0]; |
|
34
|
|
|
|
|
35
|
|
|
$params=[ |
|
36
|
|
|
'csrf_token' => $token, |
|
37
|
|
|
'action' => 'enter', |
|
38
|
|
|
'ftaa' => '', |
|
39
|
|
|
'bfaa' => '', |
|
40
|
|
|
'handleOrEmail' => $this->selectedJudger["handle"], //I wanna kill for handleOrEmail |
|
41
|
|
|
'password' => $this->selectedJudger["password"], |
|
42
|
|
|
'remember' => true, |
|
43
|
|
|
]; |
|
44
|
|
|
$this->login('http://codeforces.com/enter', http_build_query($params), 'codeforces', false, $this->selectedJudger["handle"]); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function codeForcesSubmit() |
|
49
|
|
|
{ |
|
50
|
|
|
// $this->sub['language']=substr($this->post_data["lang"], 2, 50); |
|
51
|
|
|
|
|
52
|
|
|
$submissionModel=new SubmissionModel(); |
|
53
|
|
|
$s_num=$submissionModel->countSolution($this->post_data["solution"]); |
|
54
|
|
|
$space=''; |
|
55
|
|
|
for ($i=0; $i<$s_num; $i++) { |
|
56
|
|
|
$space.=' '; |
|
57
|
|
|
} |
|
58
|
|
|
$contestId=$this->post_data["cid"]; |
|
|
|
|
|
|
59
|
|
|
$submittedProblemIndex=$this->post_data["iid"]; |
|
60
|
|
|
$var=substr($this->post_data["lang"], 0, 2); |
|
61
|
|
|
$programTypeId=$var; |
|
62
|
|
|
if ($var[0]==0) { |
|
63
|
|
|
$programTypeId=$var[1]; |
|
64
|
|
|
} |
|
65
|
|
|
$source=($space.chr(10).$this->post_data["solution"]); |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
$response=$this->grab_page("codeforces.com/contest/{$this->post_data['cid']}/submit", "codeforces", [], $this->selectedJudger["handle"]); |
|
69
|
|
|
|
|
70
|
|
|
$exploded=explode("name='csrf_token' value='", $response); |
|
71
|
|
|
$token=explode("'/>", $exploded[2])[0]; |
|
72
|
|
|
|
|
73
|
|
|
$params=[ |
|
74
|
|
|
'csrf_token' => $token, |
|
75
|
|
|
'action' => 'submitSolutionFormSubmitted', |
|
76
|
|
|
'ftaa' => '', |
|
77
|
|
|
'bfaa' => '', |
|
78
|
|
|
'submittedProblemIndex' => $submittedProblemIndex, |
|
79
|
|
|
'programTypeId' => $programTypeId, |
|
80
|
|
|
'source' => $source, |
|
81
|
|
|
'tabSize' => 4, |
|
82
|
|
|
'sourceFile' => '', |
|
83
|
|
|
]; |
|
84
|
|
|
$response=$this->post_data("codeforces.com/contest/{$this->post_data['cid']}/submit?csrf_token=".$token, http_build_query($params), "codeforces", true, true, true, false, [], $this->selectedJudger["handle"]); |
|
85
|
|
|
$this->sub["jid"]=$this->selectedJudger["jid"]; |
|
86
|
|
|
if (strpos($response, 'alert("Source code hasn\'t submitted because of warning, please read it.");') !== false) { |
|
87
|
|
|
$this->sub['verdict']='Compile Error'; |
|
88
|
|
|
preg_match('/<div class="roundbox " style="font-size:1.2rem;margin:0.5em 0;padding:0.5em;text-align:left;background-color:#eca;">[\s\S]*?<div class="roundbox-rb"> <\/div>([\s\S]*?)<div/', $response, $match); |
|
89
|
|
|
$warning = str_replace('Press button to submit the solution.', '', $match[1]); |
|
90
|
|
|
$this->sub['compile_info']=trim($warning); |
|
91
|
|
|
} elseif (substr_count($response, 'My Submissions')!=2) { |
|
92
|
|
|
file_put_contents(base_path('storage/logs/'.time().'.html'),$response); |
|
93
|
|
|
// Forbidden? |
|
94
|
|
|
$exploded=explode('<span class="error for__source">', $response); |
|
95
|
|
|
if(!isset($exploded[1])){ |
|
96
|
|
|
$this->sub['verdict']="Submission Error"; |
|
97
|
|
|
}else{ |
|
98
|
|
|
$this->sub['compile_info']=explode("</span>", $exploded[1])[0]; |
|
99
|
|
|
$this->sub['verdict']="Submission Error"; |
|
100
|
|
|
} |
|
101
|
|
|
} else { |
|
102
|
|
|
preg_match('/submissionId="(\d+)"/', $response, $match); |
|
103
|
|
|
$this->sub['remote_id']=$match[1]; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function submit() |
|
108
|
|
|
{ |
|
109
|
|
|
$validator=Validator::make($this->post_data, [ |
|
110
|
|
|
'pid' => 'required|integer', |
|
111
|
|
|
'cid' => 'required|integer', |
|
112
|
|
|
'coid' => 'required|integer', |
|
113
|
|
|
'iid' => 'required', |
|
114
|
|
|
'solution' => 'required', |
|
115
|
|
|
]); |
|
116
|
|
|
|
|
117
|
|
|
if ($validator->fails()) { |
|
118
|
|
|
$this->sub['verdict']="Submission Error"; |
|
119
|
|
|
return; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$this->codeForcesLogin(); |
|
123
|
|
|
$this->codeForcesSubmit(); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|