Please Login First!";
exit(1);
}
if (isset($OJ_LANG)) {
require_once("../lang/$OJ_LANG.php");
}
require_once ("../include/const.inc.php");
?>
- Import Problem ...
- 如果导入失败,请参考 FAQ。
Return to Problem List
","",$Node->$TagName);
// $value=mb_ereg_replace("","",$value);
return $Node->$TagName->Chinese;
}
function getAttribute($Node, $TagName,$attribute) {
return $Node->children()->$TagName->attributes()->$attribute;
}
function hasRemoteProblem($remote_oj,$remote_id) {
if($remote_oj=="" || $remote_id=="")return false;
$md5 = md5($title);
$sql = "SELECT 1 FROM problem WHERE remote_oj=? and remote_id=?";
$result = pdo_query($sql,$remote_oj,$remote_id);
$rows_cnt = count($result);
//echo "row->$rows_cnt";
return ($rows_cnt>0);
}
function hasProblem($title) {
//return false;
$md5 = md5($title);
$sql = "SELECT 1 FROM problem WHERE md5(title)=?";
$result = pdo_query($sql, $md5);
$rows_cnt = count($result);
//echo "row->$rows_cnt";
return ($rows_cnt>0);
}
function mkpta($pid,$prepends,$node) {
$language_ext = $GLOBALS['language_ext'];
$OJ_DATA = $GLOBALS['OJ_DATA'];
foreach ($prepends as $prepend) {
$language = $prepend->attributes()->language;
$lang = getLang($language);
$file_ext = $language_ext[$lang];
$basedir = "$OJ_DATA/$pid";
$file_name = "$basedir/$node.$file_ext";
file_put_contents($file_name,$prepend);
}
}
function get_extension($file) {
$info = pathinfo($file);
return $info['extension'];
}
function import_fps($tempfile,$tempData) {
global $OJ_DATA,$OJ_SAE,$OJ_REDIS,$OJ_REDISSERVER,$OJ_REDISPORT,$OJ_REDISQNAME,$domain,$DOMAIN,$OJ_NAME;
$xmlDoc = simplexml_load_file($tempfile, 'SimpleXMLElement', LIBXML_PARSEHUGE);
$searchNodes = $xmlDoc->xpath("/DocumentElement");
$spid = 0;
foreach ($searchNodes as $searchNode) {
//echo $searchNode->title,"\n";
set_time_limit(60);
$title = $searchNode->Background->Chinese;
$time_limit = "1";
$unit = "s";
//echo $unit;
if ($unit=='ms')
$time_limit /= 1000;
$memory_limit = getAttribute($searchNode,"Datas",'MemoryLimit');
$unit = "kb";
if ($unit=='kb')
$memory_limit /= 1024;
$description = $searchNode->Description->Chinese;
$input = getValue($searchNode,'InputFormat');
$output = getValue($searchNode,'OutputFormat');
$sample_input = $searchNode->SampleInput->Sample1;
$sample_output = $searchNode->SampleOutput->Sample1;
//$test_input = getValue($searchNode,'test_input');
//$test_output = getValue($searchNode,'test_output');
$hint = getValue ($searchNode,'Hint');
$source = getValue ($searchNode,'Source');
$spj= 0;
$remote_oj= getValue ($searchNode,'remote_oj');
$remote_id= getValue ($searchNode,'remote_id');
if(hasRemoteProblem($remote_oj,$remote_id)){
$sql="update problem set title=?,time_limit=?,memory_limit=?,description=?,input=?,output=?,sample_input=?,sample_output=?,hint=?,source=?,spj=? where remote_oj=? and remote_id=?";
pdo_query($sql,$title, $time_limit, $memory_limit, $description, $input, $output, $sample_input, $sample_output, $hint, $source, $spj,$remote_oj,$remote_id);
}else{
$tail=0;
$ptitle = $title;
while (hasProblem($ptitle)) {
$tail++;
$ptitle = $title."_".$tail;
}
$title=$ptitle;
$pid = addproblem($title, $time_limit, $memory_limit, $description, $input, $output, $sample_input, $sample_output, $hint, $source, $spj, $OJ_DATA);
if($remote_oj!=""){
$sql="update problem set remote_oj=?,remote_id=? where problem_id=?";
pdo_query($sql,$remote_oj,$remote_id,$pid);
}
if ($spid==0)
$spid = $pid;
$sql = "INSERT INTO `privilege` (`user_id`,`rightstr`) VALUES(?,?)";
pdo_query($sql, $_SESSION[$OJ_NAME.'_'.'user_id'], "p$pid");
$_SESSION[$OJ_NAME.'_'."p$pid"] = true;
$basedir = "$OJ_DATA/$pid";
mkdir($basedir);
if (strlen($sample_input)) mkdata($pid,"sample.in",$sample_input,$OJ_DATA);
if (strlen($sample_output)) mkdata($pid,"sample.out",$sample_output,$OJ_DATA);
//if(!isset($OJ_SAE)||!$OJ_SAE){
$testinputs = $searchNode->Datas->Data;
$testno = 0;
foreach ($testinputs as $testNode) {
//if($testNode->nodeValue)
$output=$testNode['OutputFile'];
$input=$testNode['InputFile'];
$score=intval($testNode['Score']);
if($score<=0) $score=20;
//echo "$input-$output
";
rename($tempData."/$input",$basedir."/".$testno."[".$score."].in");
rename($tempData."/$output",$basedir."/".$testno."[".$score."].out");
$testno++;
}
unset($testinputs);
// }
$images = ($searchNode->children()->img);
$did = array();
$testno = 0;
foreach ($images as $img) {
//
$src = getValue($img,"src");
if (!in_array($src,$did)) {
$base64 = getValue($img,"base64");
$ext = pathinfo($src);
$ext = strtolower($ext['extension']);
if (!stristr(",jpeg,jpg,svg,png,gif,bmp",$ext)) {
$ext = "bad";
exit(1);
}
$testno++;
$ymd =$domain."/". date("Ymd");
$save_path = $ymd . "/";
//新文件名
$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $ext;
$newpath = $save_path."/$pid"."_".$testno."_".$new_file_name;
if ($OJ_SAE)
$newpath = "saestor://web/upload/".$newpath;
else
$newpath="../upload/".$newpath;
image_save_file($newpath,$base64);
$sql = "UPDATE problem SET description=replace(description,?,?) WHERE problem_id=?";
pdo_query($sql,$src,$newpath,$pid);
$sql = "UPDATE problem SET input=replace(input,?,?) WHERE problem_id=?";
pdo_query($sql,$src,$newpath,$pid);
$sql = "UPDATE problem SET output=replace(output,?,?) WHERE problem_id=?";
pdo_query($sql,$src,$newpath,$pid);
$sql = "UPDATE problem SET hint=replace(hint,?,?) WHERE problem_id=?";
pdo_query($sql,$src,$newpath,$pid);
array_push($did,$src);
}
}
if (!isset($OJ_SAE) || !$OJ_SAE) {
if ($spj==1) {
if($spjcode){
if($spjlang=="C++"){
$basedir = "$OJ_DATA/$pid";
$fp = fopen("$basedir/spj.cc","w");
fputs($fp, $spjcode);
fclose($fp);
//////system( " g++ -o $basedir/spj $basedir/spj.cc ");
}else{
$fp = fopen("$basedir/spj.c","w");
fputs($fp, $spjcode);
fclose($fp);
//////system( " gcc -o $basedir/spj $basedir/spj.c ");
}
if (!file_exists("$basedir/spj")) {
echo "you need to compile $basedir/spj.cc for spj[ g++ -o $basedir/spj $basedir/spj.cc ]
and rejudge $pid";
}
else {
//unlink("$basedir/spj.cc");
}
}
$basedir = "$OJ_DATA/$pid";
if($tpjcode){
if($tpjlang=="C++"){
$fp = fopen("$basedir/tpj.cc","w");
fputs($fp, $tpjcode);
fclose($fp);
//////system( " g++ -o $basedir/spj $basedir/spj.cc ");
}else{
$fp = fopen("$basedir/tpj.c","w");
fputs($fp, $spjcode);
fclose($fp);
//////system( " gcc -o $basedir/spj $basedir/spj.c ");
}
if (!file_exists("$basedir/tpj")) {
echo "you need to compile $basedir/tpj.cc for tpj[ g++ -o $basedir/tpj $basedir/tpj.cc ]
and rejudge $pid";
}
else {
//unlink("$basedir/spj.cc");
}
}
}
}
$solutions = $searchNode->children()->solution;
foreach ($solutions as $solution) {
$language = $solution->attributes()->language;
submitSolution($pid,$solution,$language);
}
unset($solutions);
$prepends = $searchNode->children()->prepend;
mkpta($pid,$prepends,"prepend");
$prepends = $searchNode->children()->template;
mkpta($pid,$prepends,"template");
$prepends = $searchNode->children()->append;
mkpta($pid,$prepends,"append");
}
}
unlink($tempfile);
if (isset($OJ_REDIS) && $OJ_REDIS) {
$redis = new Redis();
$redis->connect($OJ_REDISSERVER, $OJ_REDISPORT);
$sql = "SELECT solution_id FROM solution WHERE result=0 AND problem_id>0";
$result = pdo_query($sql);
foreach ($result as $row) {
echo $row['solution_id']."\n";
$redis->lpush($OJ_REDISQNAME,$row['solution_id']);
}
}
if ($spid>0) {
require_once("../include/set_get_key.php");
//echo "
Use these problems to create a contest.";
}
}
if ($_FILES ["fps"] ["error"] > 0) {
echo " - Error: ".$_FILES ["fps"] ["error"]."File size is too big, change in PHP.ini
";
}
else {
$tempfile = $_FILES ["fps"] ["tmp_name"];
if (get_extension( $_FILES ["fps"] ["name"])=="zip") {
echo " - zip file, only fps/xml files in root dir are supported";
$resource = zip_open($tempfile);
$i = 1;
$tempfile = tempnam("/tmp", "tyvj");
$tempData = tempnam("/tmp","tyvjData");
unlink($tempData);
mkdir($tempData);
while ($dir_resource = zip_read($resource)) {
if (zip_entry_open($resource,$dir_resource)) {
$file_name = $path.zip_entry_name($dir_resource);
$file_path = substr($file_name,0,strrpos($file_name, "/"));
if (!is_dir($file_name)) {
$file_size = zip_entry_filesize($dir_resource);
$file_content = zip_entry_read($dir_resource,$file_size);
if(get_extension(basename($file_name))=="xml"){
file_put_contents($tempfile,$file_content);
import_fps($tempfile,$tempData);
}else{
file_put_contents($tempData."/".basename($file_name),$file_content);
//echo basename($file_name)."
";
}
}
zip_entry_close($dir_resource);
}
}
zip_close($resource);
unlink ( $_FILES ["fps"] ["tmp_name"] );
}
else {
echo "zip only";
}
// echo "Upload: " . $_FILES ["fps"] ["name"] . "
";
// echo "Type: " . $_FILES ["fps"] ["type"] . "
";
// echo "Size: " . ($_FILES ["fps"] ["size"] / 1024) . " Kb
";
// echo "Stored in: " . $tempfile;
//$xmlDoc = new DOMDocument ();
//$xmlDoc->load ( $tempfile );
//$xmlcontent=file_get_contents($tempfile );
}
if (isset($OJ_UDP) && $OJ_UDP) {
$JUDGE_SERVERS = explode(",",$OJ_UDPSERVER);
$JUDGE_TOTAL = count($JUDGE_SERVERS);
$select = $insert_id%$JUDGE_TOTAL;
$JUDGE_HOST = $JUDGE_SERVERS[$select];
if (strstr($JUDGE_HOST,":")!==false) {
$JUDGE_SERVERS = explode(":",$JUDGE_HOST);
$JUDGE_HOST = $JUDGE_SERVERS[0];
$OJ_UDPPORT = $JUDGE_SERVERS[1];
}
if(isset($OJ_JUDGE_HUB_PATH))
send_udp_message($JUDGE_HOST, $OJ_UDPPORT, $OJ_JUDGE_HUB_PATH);
else
send_udp_message($JUDGE_HOST, $OJ_UDPPORT, 0);
}
?>