-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem_export_xml.php
More file actions
232 lines (213 loc) · 6.72 KB
/
problem_export_xml.php
File metadata and controls
232 lines (213 loc) · 6.72 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
<?@session_start ();
require_once ("../include/db_info.inc.php");
require_once("../include/check_post_key.php");
if(isset($OJ_LANG)){
require_once("../lang/$OJ_LANG.php");
}
function getTestFileIn($pid, $testfile,$OJ_DATA) {
if ($testfile != "")
return file_get_contents ( "$OJ_DATA/$pid/" . $testfile . ".in" );
else
return "";
}
function getTestFileOut($pid, $testfile,$OJ_DATA) {
if ($testfile != "")
return file_get_contents ( );
else
return "";
}
function printTestCases($pid,$OJ_DATA){
$ret = "";
$pdir = opendir ( "$OJ_DATA/$pid/" );
while ( $file = readdir ( $pdir ) ) {
$pinfo = pathinfo ( $file );
if (isset($pinfo ['extension'])
&&$pinfo ['extension'] == "in"
&& $pinfo ['basename'] != "sample.in") {
$ret = basename ( $pinfo ['basename'], "." . $pinfo ['extension'] );
$outfile="$OJ_DATA/$pid/" . $ret . ".out";
$infile="$OJ_DATA/$pid/" . $ret . ".in";
if(file_exists($infile)){
echo "<test_input><![CDATA[".file_get_contents ($infile)."]]></test_input>\n";
}if(file_exists($outfile)){
echo "<test_output><![CDATA[".file_get_contents ($outfile)."]]></test_output>\n";
}
// break;
}
}
closedir ( $pdir );
return $ret;
}
class Solution{
var $language="";
var $source_code="";
}
function getSolution($pid){
$ret=new Solution();
require("../include/db_info.inc.php");
if(isset($OJ_LANG)){
require("../lang/$OJ_LANG.php");
}
require("../include/const.inc.php");
$con = mysql_pconnect($DB_HOST,$DB_USER,$DB_PASS);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_query("set names utf8",$con);
mysql_set_charset("utf8",$con);
mysql_select_db($DB_NAME,$con);
$sql = "select `solution_id`,`language` from solution where problem_id=$pid and result=4 order by language";
// echo $sql;
$result = mysql_query($sql,$con ) ;
if($result&&$row = mysql_fetch_row ( $result) ){
$solution_id=$row[0];
$ret->language=$language_name[$row[1]];
mysql_free_result($result);
$sql = "select source from source_code where solution_id=$solution_id";
$result = mysql_query ( $sql ) or die ( mysql_error () );
if($row = mysql_fetch_object ( $result) ){
$ret->source_code=$row->source;
}
mysql_free_result($result);
}
mysql_close($con);
return $ret;
}
function fixurl($img_url){
$img_url=htmlspecialchars_decode( $img_url);
if (substr($img_url,0,7)!="http://"){
if(substr($img_url,0,1)=="/"){
$ret='http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$img_url;
}else{
$path= dirname($_SERVER['PHP_SELF']);
$ret='http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$path."/../".$img_url;
}
}else{
$ret=$img_url;
}
return $ret;
}
function image_base64_encode($img_url){
$img_url=fixurl($img_url);
$handle = @fopen($img_url, "rb");
if($handle){
$contents = stream_get_contents($handle);
$encoded_img= base64_encode($contents);
fclose($handle);
return $encoded_img;
}else
return false;
}
function getImages($content){
preg_match_all("<[iI][mM][gG][^<>]+[sS][rR][cC]=\"?([^ \"\>]+)/?>",$content,$images);
return $images;
}
function fixcdata($content){
return str_replace("]]>","]]]]><![CDATA[>",$content);
}
function fixImageURL(&$html,&$did){
$images=getImages($html);
$imgs=array_unique($images[1]);
foreach($imgs as $img){
$html=str_replace($img,fixurl($img),$html);
//print_r($did);
if(!in_array($img,$did)){
$base64=image_base64_encode($img);
if($base64){
echo "<img><src><![CDATA[";
echo fixurl($img);
echo "]]></src><base64><![CDATA[";
echo $base64;
echo "]]></base64></img>";
}
array_push($did,$img);
}
}
}
if (! isset ( $_SESSION ['administrator'] )) {
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
echo "<a href='../loginpage.php'>Please Login First!</a>";
exit ( 1 );
}
if (isset($_POST ['do'])||isset($_GET['cid'])) {
if(isset($_POST ['in'])&&strlen($_POST ['in'])>0){
$in=mysql_real_escape_string ( $_POST ['in'] );
$sql = "select * from problem where problem_id in($in)";
$filename="-$in";
}else if (isset($_GET['cid'])){
$cid=intval( $_GET['cid'] );
$sql= "select title from contest where contest_id='$cid'";
$result = mysql_query ( $sql ) or die ( mysql_error () );
$row = mysql_fetch_object ( $result );
$filename='-'.$row->title;
mysql_free_result ( $result );
$sql = "select * from problem where problem_id in(select problem_id from contest_problem where contest_id=$cid)";
}else{
$start = intval ( $_POST ['start'] );
$end = intval ( $_POST ['end'] );
$sql = "select * from problem where problem_id>=$start and problem_id<=$end";
$filename="-$start-$end";
}
//echo $sql;
$result = mysql_query ( $sql ) or die ( mysql_error () );
if (isset($_POST ['submit'])&&$_POST ['submit'] == "Export")
header ( 'Content-Type: text/xml' );
else {
header ( "content-type: application/file" );
header ( "content-disposition: attachment; filename=\"fps-".$_SESSION['user_id'].$filename.".xml\"" );
}
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
?>
<fps version="1.1" url="http://code.google.com/p/freeproblemset/">
<generator name="HUSTOJ" url="http://code.google.com/p/hustoj/"/>
<?
while ( $row = mysql_fetch_object ( $result ) ) {
?>
<item>
<title><![CDATA[<?=$row->title?>]]></title>
<time_limit><![CDATA[<?=$row->time_limit?>]]></time_limit>
<memory_limit><![CDATA[<?=$row->memory_limit?>]]></memory_limit>
<?
$did=array();
fixImageURL($row->description,$did);
fixImageURL($row->input,$did);
fixImageURL($row->output,$did);
fixImageURL($row->hint,$did);
?>
<description><![CDATA[<?=$row->description?>]]></description>
<input><![CDATA[<?=$row->input?>]]></input>
<output><![CDATA[<?=$row->output?>]]></output>
<sample_input><![CDATA[<?=$row->sample_input?>]]></sample_input>
<sample_output><![CDATA[<?=$row->sample_output?>]]></sample_output>
<?php printTestCases($row->problem_id,$OJ_DATA)?>
<hint><![CDATA[<?=$row->hint?>]]></hint>
<source><![CDATA[<?=fixcdata($row->source)?>]]></source>
<?
$solution=getSolution($row->problem_id);
if ($solution->language){?>
<solution language="<?=$solution->language?>"><![CDATA[<?=fixcdata($solution->source_code)?>]]></solution>
<?}?>
<?
if($row->spj!=0){
$filec="$OJ_DATA/".$row->problem_id."/spj.c";
$filecc="$OJ_DATA/".$row->problem_id."/spj.cc";
if(file_exists( $filec )){
echo "<spj language=\"C\"><![CDATA[";
echo fixcdata(file_get_contents ($filec ));
echo "]]></spj>";
}
elseif(file_exists( $filecc )){
echo "<spj language=\"C++\"><![CDATA[";
echo fixcdata(file_get_contents ($filecc ));
echo "]]></spj>";
}
}
?>
</item>
<?
}
mysql_free_result ( $result );
echo "</fps>";
}
?>