forked from yzyDavid/CProgrammingLanguage
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10-77-submitter-2014.php
More file actions
executable file
·461 lines (445 loc) · 15.7 KB
/
10-77-submitter-2014.php
File metadata and controls
executable file
·461 lines (445 loc) · 15.7 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/usr/bin/php
<?php
/**
*
* CProgramming website submitter
* @author Senorsen
* @description sen@senorsen.com , please see license
* @link http://www.senorsen.com
* @version 2014-04-21 Mon 22:40:32
* @version 2014-04-22 Tue 02:01:49
* @version 2014-04-22 Tue 12:35:48
*
**/
$tmp_path = '/tmp/'; // must with a suffix slash ('/')
$tmp_file = tempnam($tmp_path, "SEN_CP_TMP");
$server_host = "10.77.30.139";
$server = "http://$server_host/";
if ($argc == 1) {
echo "Usage: ./submitter.php YOUR_USER_ID [YOUR_PASSWORD]\n";
echo "Put me in Richard1122's CProgram...-ZJU directory.\n";
echo "Not tested since 2014-09\n";
echo "Author: Senorsen. Without any copyright, but follow the LICENSE.\n";
echo "\n";
exit(0);
}
if ($argc == 2) {
$stuid = $passwd = $argv[1];
}
if ($argc == 3) {
$stuid = $argv[1];
$passwd = $argv[2];
}
echo "CProgramming Submitter by Senorsen - sen@senorsen.com\n";
echo "Please see the LICENSE.\n";
$login_ret = login($stuid, $passwd);
getannounce($login_ret);
$classes = getclass();
echo "\n";
echo "+---------------------------------------------+\n";
foreach ($classes as $key => $value) {
printf("ID: %2d %5s %10s %13s %12s\n", $key, $value->id, $value->no, $value->name, $value->teacher);
}
echo "+---------------------------------------------+\n";
sleep(1);
foreach ($classes as $key => $value) {
echo "Try $key-$value->id : ";
$lianxis = getlianxi($value->id);
if ($lianxis == FALSE) {
continue;
}
echo "Your practices: \n";
foreach ($lianxis as $kk => $vv) {
printf("ID: %2d %4s %s\t %s %s\n", $kk, $vv->id, $vv->no, $vv->from, $vv->to);
}
sleep(2);
foreach ($lianxis as $kk => $vv) {
echo "Try practice $kk-$vv->id ($vv->no) :\n";
sleep(0.5);
$problemset = getproblemset($vv->id);
$nas = array();
foreach ($problemset as $vvv) {
$acnastr = $vvv->score == 100 ? 'AC' : 'NA';
$acnaclr = $acnastr == 'AC' ? 32 : 31;
echo exec("echo \"[\\033[0;$acnaclr;49m$acnastr\\033[0;0;0m]\"");
printf(" %05d %3d %s\n", $vvv->id, $vvv->score, $vvv->title);
if ($acnastr == 'NA') {
array_push($nas, $vvv);
}
}
echo "Proceed with file copy? (to homework_{$vv->id}_$stuid/) [y/N]:\n";
if (inputstr() == 'y') {
$p_file_copy = 1;
$loop_arr = $problemset;
@mkdir("homework_{$vv->id}_$stuid");
} else {
$p_file_copy = 0;
$loop_arr = $nas;
}
foreach ($loop_arr as $vvv) {
echo "Fetch for $vvv->vid-$vvv->id : $vvv->title ";
$problem_desc = htmltotext(getproblem($vvv->link));
echo ".\n";
$src_flag = 0;
ready_for_upload:
if (!file_exists('CProgrammingLanguage-BHH-ZJU/' . $vvv->id . '/' . $vvv->id . '.c')) {
$src_flag = 1;
@mkdir('CProgrammingLanguage-BHH-ZJU/' . $vvv->id);
$desc_searches = array('/\)\{/', "/\\)\n\\{/", '/"stdio\.h"/');
$desc_replaces = array(') {', ') {', '<stdio.h>');
$problem_desc = preg_replace($desc_searches, $desc_replaces, $problem_desc);
file_put_contents("CProgrammingLanguage-BHH-ZJU/$vvv->id/README.txt", $vvv->title . "\n\n" . $problem_desc . "\n");
echo "Terrible >_< File not exist.\n";
echo "So you should solve this problem: (press Enter/Return key to see the problem)\n";
waitforret();
preg_match('/(\#include[\s\S]+)$/', $problem_desc, $matches_src);
$demo_src = $matches_src[1];
// Hey it should be 小森森/Senorsen's code style!!
file_put_contents("CProgrammingLanguage-BHH-ZJU/$vvv->id/{$vvv->id}_not_finished.c", $demo_src);
echo $problem_desc . "\n";
echo "=v= When you finished this code at CProgrammingLanguage-BHH-ZJU/$vvv->id/{$vvv->id}_not_finished.c, \n";
echo " Work hard to HIT your Enter/Return key!\n";
echo "Find CProgrammingLanguage-BHH-ZJU/$vvv->id/{$vvv->id}_not_finished.c, and it will be renamed automatically.\n";
waitforret();
rename("CProgrammingLanguage-BHH-ZJU/$vvv->id/{$vvv->id}_not_finished.c", "CProgrammingLanguage-BHH-ZJU/$vvv->id/$vvv->id.c");
goto ready_for_upload;
} else {
echo "Uploading CProgrammingLanguage-BHH-ZJU/$vvv->id/$vvv->id.c, please wait...\n";
if (FALSE == uploadsrc($vvv->id, "CProgrammingLanguage-BHH-ZJU/$vvv->id/$vvv->id.c")) {
echo "Error: cannot upload. \n";
echo "fatal error.\n";
echo "If this problem persists, please contact me.\n";
exit(4);
} else {
$timer = 10;
while ($timer) {
$timer--;
$problem_result = getresult();
echo "Code: $problem_result\n";
if ($problem_result == '运行完毕') break;
sleep(1);
}
if ($problem_result != '运行完毕') {
echo "Your source code is incorrect? \n";
echo "Try to correct it, and then press Enter/Return to retry.\n";
waitforret();
goto ready_for_upload;
}
if ($src_flag) {
echo "It seems that my repo does not have your code.\n";
echo "Would you like to share (pull request) this to me?\n";
echo "Pulling request of .c code is welcomed here!\n";
}
echo "$vvv->id $vvv->title Solved, AC!!\n";
if ($p_file_copy) {
copy("CProgrammingLanguage-BHH-ZJU/$vvv->id/$vvv->id.c", "homework_{$vv->id}_$stuid/$vvv->id.c");
}
sleep(1);
}
}
}
echo "Contest $vv->id OK!\n";
if (count($nas) > 0) {
$problemset = getproblemset($vv->id);
foreach ($problemset as $vvv) {
$acnastr = $vvv->score == 100 ? 'AC' : 'NA';
$acnaclr = $acnastr == 'AC' ? 32 : 31;
echo exec("echo \"[\\033[0;$acnaclr;49m$acnastr\\033[0;0;0m]\"");
printf(" %05d %3d %s\n", $vvv->id, $vvv->score, $vvv->title);
}
}
echo "Press Enter/Return to continue, or 'exit' and RET for exit!\n";
if (waitforret()) {
bye();
exit(0);
}
}
echo "Everything is OK!\n";
echo "Author: Senorsen sen@senorsen.com\n";
}
bye();
function bye()
{
global $tmp_file;
echo "Bye~~\n";
unlink($tmp_file);
}
function uploadsrc($prob_id, $file)
{
$cfile = new CURLFile($file, 'application/octet-stream', "$prob_id.c");
$data = array(
'MAX_FILE_SIZE' => '81920000',
'prob_id' => $prob_id,
'langn' => 0,
'flagn' => 1,
'sub1' => 'Senorsen',
'userfile' => $cfile,
);
$ret = curlf('student/submit_process.php', $data, 1);
echo $ret;
if (preg_match('/解答已经提交/', $ret)) {
preg_match('/url=(.+?)"/', $ret, $matches);
sleep(2);
curlf('student/' . $matches[1], null, 1);
return TRUE;
} else {
return FALSE;
}
}
function getresult()
{
$run_id = 0;
$ret = curlf("student/status.php?run_id=$run_id&lang=0&flag=1", null, 1);
preg_match_all('/<td align=center(.+?)\/td>/', $ret, $matches);
$arr = array('运行中', '运行超时', '编译错误', '运行完毕');
foreach ($arr as $value) {
if (preg_match('/' . $value . '/', $matches[1][9])) return $value;
}
return 'UNKNOWN';
}
function getproblem($link)
{
$ret = curlf('student/' . $link, null, 1);
preg_match('/<code>([\s\S]+?)<\/code>/', $ret, $matches);
$desc = $matches[1];
return $desc;
}
function getproblemset($id)
{
$data = "contest_id=$id&Submit=listproblem";
$ret = curlf('student/listproblem.php', $data, 1);
if (!preg_match('/题号/', $ret)) {
echo "Error: cannot get problemset.\n";
echo "If this problem persists, please contact me.\n";
sleeptogether();
return FALSE;
} else {
preg_match_all('/<td align=center >(\d+)/', $ret, $matches_number);
preg_match_all('/<td align=center>(\d+)/', $ret, $matches_score);
preg_match_all('/<a.+?href="show_problem.php\?cid=\d+\&pid=\d+\&pno=\d+\&lang=\d+">([\s\S]+?)<\/a><\/td>/', $ret, $matches_title);
preg_match_all('/<a.+?href="(show_problem.php\?cid=\d+\&pid=\d+\&pno=\d+\&lang=\d+)">/', $ret, $matches_link);
$matches_number = $matches_number[1];
$matches_score = $matches_score[1];
$matches_title = $matches_title[1];
$matches_link = $matches_link[1];
$problems = array();
$i = 0;
$j = 0;
foreach ($matches_number as $key => $value) {
$key = intval($key);
$row = (object)array('id' => intval($value), 'score' => @intval($matches_score[$key]));
$problems[$j++] = $row;
$i++;
}
$i = 0;
foreach ($matches_link as $key => $value) {
$key = intval($key);
$problems[$i]->title = trim($matches_title[$key]);
$problems[$i]->link = $matches_link[$key];
$problems[$i]->vid = $i;
$problems[$i]->contest_id = $id;
$i++;
}
return $problems;
}
}
function getlianxi($id)
{
global $server;
$data = "c_choose=$id&submit1=submit";
$ret = curlf('student/contestchoose.php?et=1', $data, 1);
if (!preg_match('/训练场次列表/', $ret)) {
echo $ret;
echo "Error: Cannot get practices.\n";
echo "If this problem persists, please contact me.\n";
sleeptogether();
return FALSE;
} else {
$lianxis = array();
preg_match_all('/<div align="center">(.*?)<\/div>/', $ret, $matches);
$matches = $matches[1];
for ($i = 0; $i < 5; $i++) unset($matches[$i]);
foreach ($matches as $key => $value) {
$key = intval($key);
if (preg_match('/<input type="radio"/', $value)) {
array_push($lianxis, (object)array(
'id' => $matches[$key + 1],
'no' => $matches[$key + 2],
'from' => $matches[$key + 3],
'to' => $matches[$key + 4]
));
}
}
return $lianxis;
}
}
function sleeptogether()
{
echo "Now we sleep for 4s: ";
for ($i = 1; $i <= 4; $i++) {
sleep(1);
echo $i . " ";
}
echo ".\n";
}
function getannounce($str)
{
global $stuid;
$ret = curlf('student/teacherannounce.php?userid=' . $stuid, null, 1);
// echo "LENGTH" . strlen($ret);
// 105 is empty
if (strlen($ret) <= 85) {
echo "***ATTENTION***: You have some announcement in the website.\n";
echo "If you think this important, please have a look \n";
echo " at the C Programming Practice website.\n";
sleep(2);
}
}
function getclass()
{
$str = curlf('student/studentfirst.php?et=1', null, 1);
$classes = array();
preg_match_all('/<div align="center">(.*?)<\/div>/', $str, $matches);
for ($i = 0; $i < 5; $i++) {
unset($matches[1][$i]);
}
foreach ($matches as $key => $value) {
if ($key == 0) continue;
array_push($classes, (object)array(
'id' => $value[6],
'no' => $value[7],
'name' => $value[8],
'teacher' => $value[9],
));
}
return $classes;
}
function login($stuid, $passwd)
{
global $tmp_file, $server_host;
echo "Try login with stuid: $stuid ";
$data = array(
'user_id' => $stuid,
'passwd' => $passwd,
'submit1' => 'login',
);
$ret = curlf('student/studentcheck.php', $data, 2);
if ($ret === FALSE) {
echo "x\n";
echo "Login failed, please check your network connection.\n";
echo "If this problem persists, please contact me >_<\n";
exit(2);
} else {
echo " ... [ok]\n";
if (preg_match('/frameborder="NO" name="topFrame"/', $ret)) {
// 登录成功
$topstu = curlf('student/topstu.php', null, 1);
preg_match('/.+?(\w+)\[(.+?)\]<\/b> 您好,感谢登陆使用!/', $topstu, $matches);
echo "Login success! $matches[1] - $matches[2] \n";
file_put_contents($tmp_file, file_get_contents($tmp_file) . "$server_host\tFALSE\t/\tFALSE\t0\tuser_id\t$stuid\n");
return $ret;
} else {
echo "Login faild, please check your stuid & passwd.\n";
echo "If this problem persists, please contact me >_<\n";
exit(1);
}
}
}
function curlf($url, $data = null, $cookie_jar_type = 0, $other_headers = array(), $timeout = 30)
{
global $tmp_file, $server;
$url = $server . $url;
$ua = "Senorsen's CProgramming file submitter";
$url = mb_convert_encoding($url, 'utf-8', 'gbk');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout / 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if (!empty($ua)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(array("User-Agent: $ua", "X-Forwarded-For: 10.77.30.31"), $other_headers));
}
// cookie_jar_type:
// 0 for doing nothing
// 1 for read
// 2 for write
if ($cookie_jar_type == 1) {
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmp_file);
}
if ($cookie_jar_type == 2) {
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmp_file);
}
if (!is_null($data)) {
//POST
if (is_string($data)) {
$data = mb_convert_encoding($data, 'gbk', 'utf-8');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else if (is_array($data)) {
convert_arr_to_gbk($data);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
}
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$str = curl_exec($ch);
curl_close($ch);
if (!is_null($str)) {
$str = mb_convert_encoding($str, 'utf-8', 'gbk');
} else {
$str = FALSE;
}
return $str;
}
function convert_arr_to_gbk(&$obj)
{
// note that xiao sensen won't pass me an unchangable variable.
if (is_object($obj) || is_array($obj)) {
// hey but I cannot convert the key.
foreach ($obj as $key => &$value) {
convert_arr_to_gbk($value);
}
return $obj;
} else if (is_string($obj)) {
return ($obj = mb_convert_encoding($obj, 'gbk', 'utf-8'));
}
}
function htmltotext($str)
{
$search = array('/(<br>|<br \/>)/',
'/&(quot|#34);/', // 替换 HTML 实体
'/&(amp|#38);/',
'/&(lt|#60);/',
'/&(gt|#62);/',
'/&(nbsp|#160);/',
'/&(iexcl|#161);/',
'/&(cent|#162);/',
'/&(pound|#163);/',
'/&(copy|#169);/',
'/&#(\\d+);/',
'/' . "\x0d" . '/'
); // 作为 PHP 代码运行
$replace = array("\n",
"\"",
"&",
"<",
">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
chr(1),
''
);
return preg_replace($search, $replace, $str);
}
function waitforret()
{
if ('exit' == inputstr()) return TRUE;
return FALSE;
}
function inputstr()
{
return trim(fgets(STDIN));
}