forked from futurepw/oj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit.php
232 lines (204 loc) · 6.72 KB
/
submit.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
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
<?php session_start();
if (!isset($_SESSION['user_id'])){
require_once("oj-header.php");
echo "<a href='loginpage.php'>$MSG_Login</a>";
require_once("oj-footer.php");
exit(0);
}
require_once("include/db_info.inc.php");
require_once("include/const.inc.php");
$now=strftime("%Y-%m-%d %H:%M",time());
$user_id=$_SESSION['user_id'];
if (isset($_POST['cid'])){
$pid=intval($_POST['pid']);
$cid=intval($_POST['cid']);
$sql="SELECT `problem_id` from `contest_problem`
where `num`='$pid' and contest_id=$cid";
}else{
$id=intval($_POST['id']);
$sql="SELECT `problem_id` from `problem` where `problem_id`='$id' and problem_id not in (select distinct problem_id from contest_problem where `contest_id` IN (
SELECT `contest_id` FROM `contest` WHERE
(`end_time`>'$now' or private=1)and `defunct`='N'
))";
if(!isset($_SESSION['administrator']))
$sql.=" and defunct='N'";
}
//echo $sql;
$res=mysql_query($sql);
if ($res&&mysql_num_rows($res)<1&&!isset($_SESSION['administrator'])&&!((isset($cid)&&$cid<=0)||(isset($id)&&$id<=0))){
mysql_free_result($res);
$view_errors= "Where do find this link? No such problem.<br>";
require("template/".$OJ_TEMPLATE."/error.php");
exit(0);
}
mysql_free_result($res);
$test_run=false;
if (isset($_POST['id'])) {
$id=intval($_POST['id']);
$test_run=($id<=0);
}else if (isset($_POST['pid']) && isset($_POST['cid'])&&$_POST['cid']!=0){
$pid=intval($_POST['pid']);
$cid=intval($_POST['cid']);
$test_run=($cid<0);
if($test_run) $cid=-$cid;
// check user if private
$sql="SELECT `private` FROM `contest` WHERE `contest_id`='$cid' AND `start_time`<='$now' AND `end_time`>'$now'";
$result=mysql_query($sql);
$rows_cnt=mysql_num_rows($result);
if ($rows_cnt!=1){
echo "You Can't Submit Now Because Your are not invited by the contest or the contest is not running!!";
mysql_free_result($result);
require_once("oj-footer.php");
exit(0);
}else{
$row=mysql_fetch_array($result);
$isprivate=intval($row[0]);
mysql_free_result($result);
if ($isprivate==1&&!isset($_SESSION['c'.$cid])){
$sql="SELECT count(*) FROM `privilege` WHERE `user_id`='$user_id' AND `rightstr`='c$cid'";
$result=mysql_query($sql) or die (mysql_error());
$row=mysql_fetch_array($result);
$ccnt=intval($row[0]);
mysql_free_result($result);
if ($ccnt==0&&!isset($_SESSION['administrator'])){
$view_errors= "You are not invited!\n";
require("template/".$OJ_TEMPLATE."/error.php");
exit(0);
}
}
}
$sql="SELECT `problem_id` FROM `contest_problem` WHERE `contest_id`='$cid' AND `num`='$pid'";
$result=mysql_query($sql);
$rows_cnt=mysql_num_rows($result);
if ($rows_cnt!=1){
$view_errors= "No Such Problem!\n";
require("template/".$OJ_TEMPLATE."/error.php");
mysql_free_result($result);
exit(0);
}else{
$row=mysql_fetch_object($result);
$id=intval($row->problem_id);
if($test_run) $id=-$id;
mysql_free_result($result);
}
}else{
$id=0;
/*
$view_errors= "No Such Problem!\n";
require("template/".$OJ_TEMPLATE."/error.php");
exit(0);
*/
$test_run=true;
}
$language=intval($_POST['language']);
if ($language>count($language_name) || $language<0) $language=0;
$language=strval($language);
$source=$_POST['source'];
$input_text=$_POST['input_text'];
if(get_magic_quotes_gpc()){
$source=stripslashes($source);
$input_text=stripslashes($input_text);
}
$input_text=preg_replace ( "(\r\n)", "\n", $input_text );
$source=mysql_real_escape_string($source);
$input_text=mysql_real_escape_string($input_text);
$source_user=$source;
if($test_run) $id=-$id;
//use append Main code
$prepend_file="$OJ_DATA/$id/prepend.$language_ext[$language]";
if(isset($OJ_APPENDCODE)&&$OJ_APPENDCODE&&file_exists($prepend_file)){
$source=mysql_real_escape_string(file_get_contents($prepend_file)."\n").$source;
}
$append_file="$OJ_DATA/$id/append.$language_ext[$language]";
if(isset($OJ_APPENDCODE)&&$OJ_APPENDCODE&&file_exists($append_file)){
$source.=mysql_real_escape_string("\n".file_get_contents($append_file));
}
//end of append
if($test_run) $id=0;
$len=strlen($source);
//echo $source;
setcookie('lastlang',$language,time()+360000);
$ip=$_SERVER['REMOTE_ADDR'];
if ($len<2){
$view_errors="Code too short!<br>";
require("template/".$OJ_TEMPLATE."/error.php");
exit(0);
}
if ($len>65536){
$view_errors="Code too long!<br>";
require("template/".$OJ_TEMPLATE."/error.php");
exit(0);
}
// last submit
$now=strftime("%Y-%m-%d %X",time()-1);
$sql="SELECT `in_date` from `solution` where `user_id`='$user_id' and in_date>'$now' order by `in_date` desc limit 1";
$res=mysql_query($sql);
if (0&&mysql_num_rows($res)==1){
//$row=mysql_fetch_row($res);
//$last=strtotime($row[0]);
//$cur=time();
//if ($cur-$last<10){
$view_errors="You should not submit more than twice in 10 seconds.....<br>";
require("template/".$OJ_TEMPLATE."/error.php");
exit(0);
//}
}
if((~$OJ_LANGMASK)&(1<<$language)){
$store_id=0;
if(isset($_SESSION['store_id'])) $store_id=$_SESSION['store_id'];
if (!isset($pid)){
$sql="INSERT INTO solution(problem_id,user_id,in_date,language,ip,code_length)
VALUES('$id','$user_id',NOW(),'$language','$ip','$len')";
}else{
$sql="INSERT INTO solution(problem_id,user_id,in_date,language,ip,code_length,contest_id,num)
VALUES('$id','$user_id',NOW(),'$language','$ip','$len','$cid','$pid')";
}
mysql_query($sql);
$insert_id=mysql_insert_id();
$sql="INSERT INTO `source_code_user`(`solution_id`,`source`)VALUES('$insert_id','$source_user')";
mysql_query($sql);
$sql="INSERT INTO `source_code`(`solution_id`,`source`)VALUES('$insert_id','$source')";
mysql_query($sql);
if($test_run){
$sql="INSERT INTO `custominput`(`solution_id`,`input_text`)VALUES('$insert_id','$input_text')";
mysql_query($sql);
}
//echo $sql;
}
$statusURI=strstr($_SERVER['REQUEST_URI'],"submit",true)."status.php";
if (isset($cid))
$statusURI.="?cid=$cid";
$sid="";
if (isset($_SESSION['user_id'])){
$sid.=session_id().$_SERVER['REMOTE_ADDR'];
}
if (isset($_SERVER["REQUEST_URI"])){
$sid.=$statusURI;
}
// echo $statusURI."<br>";
$sid=md5($sid);
$file = "cache/cache_$sid.html";
//echo $file;
if($OJ_MEMCACHE){
$mem = new Memcache;
if($OJ_SAE)
$mem=memcache_init();
else{
$mem->connect($OJ_MEMSERVER, $OJ_MEMPORT);
}
$mem->delete($file,0);
}
else if(file_exists($file))
unlink($file);
//echo $file;
$statusURI="status.php?user_id=".$_SESSION['user_id'];
if (isset($cid))
$statusURI.="&cid=$cid";
if(!$test_run)
header("Location: $statusURI");
else{
?>
<script>window.parent.setTimeout("fresh_result('<?php echo $insert_id;?>')",2000);</script>
<?php
}
?>