. //////////////////////////////////////////////////////////////////////////////// // Last modified 16/oct/2017 by cassio@ime.usp.br function makeurlhttps($siteurl) { if(substr($siteurl,0,7) == 'http://') $siteurl = substr($siteurl,7); if(substr($siteurl,0,8) != 'https://') $siteurl = 'https://' . $siteurl; if(substr($siteurl,strlen($siteurl)-1,1) != '/') $siteurl .= '/'; return $siterul; } function scoretransfer($putname, $localsite, $timeo=5) { $ds = DIRECTORY_SEPARATOR; if($ds=="") $ds = "/"; if(is_readable('/etc/boca.conf')) { $pif=parse_ini_file('/etc/boca.conf'); $bocaproxy = @trim($pif['proxy']); if($bocaproxy != "" && substr($bocaproxy,0,6) != 'tcp://') $bocaproxy = 'tcp://' . $bocaproxy; $bocaproxylogin = @trim($pif['proxylogin']); $bocaproxypass = @trim($pif['proxypassword']); if($bocaproxylogin != "") $bocaproxypass = base64_encode($bocaproxylogin . ":" . $bocaproxypass); } else { $bocaproxy = ""; $bocaproxypass = ""; } $privatedir = $_SESSION['locr'] . $ds . "private"; // as of aug/2017, let's only do transfers with the mainsite as specified at the contestmainsiteurl // if(!is_readable($privatedir . $ds . 'remotescores' . $ds . "otherservers")) return; // $remotesite = @file($privatedir . $ds . 'remotescores' . $ds . "otherservers"); $remotesite = array(); $superlfile = $privatedir . $ds . "score_localsite_" . $localsite . "_x.dat"; $localfile = "score_site" . $localsite . "_" . $localsite . "_x.dat"; $contest=$_SESSION["usertable"]["contestnumber"]; if($contest != '' && ($ct = DBContestInfo($contest)) != null) { if(trim($ct['contestmainsiteurl']) != '') { $tmp = explode(' ',$ct['contestmainsiteurl']); if(count($tmp) >= 3) { $remotesite[count($remotesite)] = $ct['contestmainsiteurl']; } } } for($i = 0; $i < count($remotesite); $i++) { $sitedata = explode(' ', $remotesite[$i]); if(count($sitedata) < 3) continue; $siteurl = $sitedata[0]; if(strpos($siteurl,'#') !== false) continue; LOGError("scoretransfer: found site $siteurl"); $siteurl = makeurlhttps($siteurl); // LOGError("url=" .$siteurl . "index.php?getsessionid=1"); $opts = array(); $opts['http']['timeout'] = $timeo; $context = stream_context_create($opts); if(($sess = @file_get_contents($siteurl . "index.php?getsessionid=1", 0, $context))===false) { LOGError("scoretransfer: timeout at get session id for $siteurl"); continue; } // LOGError("sess=$sess pass=" . trim($sitedata[2]) . " hash=" . myhash(trim($sitedata[2]))); $user = trim($sitedata[1]); $res = myhash( myhash (trim($sitedata[2])) . $sess); // LOGError("url=" . $siteurl . "index.php?name=${user}&password=${res}&action=transfer"); $opts = array( 'http' => array( 'method' => 'GET', 'request_fulluri' => true, 'header' => 'Cookie: PHPSESSID=' . $sess ) ); if($bocaproxy != "") $opts['http']['proxy'] = $bocaproxy; if($bocaproxypass != "") $opts['http']['header'] .= "\r\nProxy-Authorization: Basic " . $bocaproxypass; $opts['http']['timeout'] = $timeo; $context = stream_context_create($opts); try { $ok = @file_get_contents($siteurl . "index.php?name=${user}&password=${res}&action=transfer", 0, $context); } catch(Exception $e) { $ok = false; } if($ok===false) { LOGError("scoretransfer: timeout at login for $siteurl"); continue; } // LOGError("ok=" . $ok); if(substr($ok,strlen($ok)-strlen('TRANSFER OK'),strlen('TRANSFER OK')) == 'TRANSFER OK') { if(($res = @file_get_contents($siteurl . "scoretable.php?remote=-42", 0, $context))===false) { LOGError("scoretransfer: timeout at get score for $siteurl"); continue; } @file_put_contents($privatedir . $ds . 'remotescores' . $ds . 'tmp.zip', $res); if(is_readable($privatedir . $ds . 'remotescores' . $ds . 'tmp.zip')) { $zip = new ZipArchive; if ($zip->open($privatedir . $ds . 'remotescores' . $ds . 'tmp.zip') === true) { cleardir($privatedir . $ds . 'remotescores' . $ds . 'tmp'); @mkdir($privatedir . $ds . 'remotescores' . $ds . 'tmp'); $zip->extractTo($privatedir . $ds . 'remotescores' . $ds . 'tmp'); foreach(glob($privatedir . $ds . 'remotescores' . $ds . 'tmp' . $ds . '*.dat') as $file) { @chown($file,"www-data"); @chmod($file,0660); $bn = basename($file); if($bn == $localfile) @rename($file, $privatedir . $ds . 'remotescores' . $ds . "score_site" . $localsite . "__y.dat"); else @rename($file, $privatedir . $ds . 'remotescores' . $ds . $bn); } $zip->close(); LOGError("scoretransfer: download OK"); } else { LOGError("scoretransfer: download failed (2)"); } cleardir($privatedir . $ds . 'remotescores' . $ds . 'tmp'); @unlink($privatedir . $ds . 'remotescores' . $ds . 'tmp.zip'); } else { LOGError("scoretransfer: download failed (3)"); } } else { LOGError("scoretransfer: download failed (1)"); } if(is_readable($putname)) { $data = @file_get_contents($putname); $data_url = http_build_query(array('data' => $data, )); $opts = array( 'http' => array( 'method' => 'POST', 'request_fulluri' => true, 'header' => 'Cookie: PHPSESSID=' . $sess . "\r\nContent-Type: application/x-www-form-urlencoded", 'content' => $data_url ) ); if($bocaproxy != "") $opts['http']['proxy'] = $bocaproxy; if($bocaproxypass != "") $opts['http']['header'] .= "\r\nProxy-Authorization: Basic " . $bocaproxypass; $opts['http']['timeout'] = $timeo; $context = stream_context_create($opts); try { $s = @file_get_contents($siteurl . "site/putfile.php", 0, $context); } catch(Exception $e) { $s = false; } if($s===false) { LOGError("scoretransfer: timeout at upload for $siteurl"); } else { if(strpos($s,'SCORE UPLOADED OK') !== false) LOGError("scoretransfer: upload OK"); else LOGError("scoretransfer: upload failed (" . $s . ")"); } } if(is_readable($superlfile)) { $data = @file_get_contents($superlfile); $data_url = http_build_query(array('data' => $data, )); $opts = array( 'http' => array( 'method' => 'POST', 'request_fulluri' => true, 'header' => 'Cookie: PHPSESSID=' . $sess . "\r\nContent-Type: application/x-www-form-urlencoded", 'content' => $data_url ) ); if($bocaproxy != "") $opts['http']['proxy'] = $bocaproxy; if($bocaproxypass != "") $opts['http']['header'] .= "\r\nProxy-Authorization: Basic " . $bocaproxypass; $opts['http']['timeout'] = $timeo; $context = stream_context_create($opts); try { $s = @file_get_contents($siteurl . "site/putfilesuper.php", 0, $context); } catch(Exception $e) { $s=false; } if($s===false) { LOGError("scoretransfer: timeout at full upload for $siteurl"); continue; } else { if(strpos($s,'SCORE UPLOADED OK') !== false) LOGError("scoretransfer: upload full OK"); else LOGError("scoretransfer: upload full failed (" . $s . ")"); } } } } function getMainXML($contest,$timeo=5) { $ds = DIRECTORY_SEPARATOR; if($ds=="") $ds = "/"; if(is_readable('/etc/boca.conf')) { $pif=parse_ini_file('/etc/boca.conf'); $bocaproxy = @trim($pif['proxy']); if($bocaproxy != "" && substr($bocaproxy,0,6) != 'tcp://') $bocaproxy = 'tcp://' . $bocaproxy; $bocaproxylogin = @trim($pif['proxylogin']); $bocaproxypass = @trim($pif['proxypassword']); if($bocaproxylogin != "") $bocaproxypass = base64_encode($bocaproxylogin . ":" . $bocaproxypass); } else { $bocaproxy = ""; $bocaproxypass = ""; } $privatedir = $_SESSION['locr'] . $ds . "private"; $c = DBConnect(); if ($c==null) return false; $r = DBExec($c, "select * from contesttable where contestnumber=$contest"); if (DBnLines($r)==0) { echo "Unable to find the contest $contest in the database.\n"; LOGError("Unable to find the contest $contest in the database."); return false; } $ct = DBRow($r,0); $localsite = $ct["contestlocalsite"]; $mainsite = $ct["contestmainsite"]; if(trim($ct['contestmainsiteurl']) == '') { return false; } $sitedata = explode(' ',$ct['contestmainsiteurl']); if(count($sitedata) < 3) { LOGError("getMainXML: invalid mainsiteurl entry"); return false; } if(count($sitedata) == 3) { $updatetime=0; } else $updatetime=trim($sitedata[3]); $siteurl = $sitedata[0]; LOGInfo("getMainXML: site $siteurl"); $siteurl = makeurlhttps($siteurl); // LOGError("url=" .$siteurl . "index.php?getsessionid=1"); $opts = array(); $opts['http']['timeout'] = $timeo; $context = stream_context_create($opts); echo "
";
echo "Connecting to ". $siteurl . "\n";
try {
$sess = @file_get_contents($siteurl . "index.php?getsessionid=1", 0, $context);
} catch(Exception $e) {
$sess=false;
}
if($sess===false) {
echo "timeout at connection\n";
LOGError("getMainXML: timeout at get session id for $siteurl");
return false;
}
$user = trim($sitedata[1]);
$res = myhash( myhash (trim($sitedata[2])) . $sess);
$opts = array(
'http' => array(
'method' => 'GET',
'request_fulluri' => true,
'header' => 'Cookie: PHPSESSID=' . $sess
)
);
if($bocaproxy != "")
$opts['http']['proxy'] = $bocaproxy;
if($bocaproxypass != "")
$opts['http']['header'] .= "\r\nProxy-Authorization: Basic " . $bocaproxypass;
$opts['http']['timeout'] = $timeo;
$context = stream_context_create($opts);
echo "Authorizing\n";
try {
$ok = @file_get_contents($siteurl . "index.php?name=${user}&password=${res}&action=transfer", 0, $context);
} catch(Exception $e) {
$ok=false;
}
if($ok===false) {
echo "timeout at authorization\n";
LOGError("getMainXML: timeout at login for $siteurl");
return false;
}
$ti = mytime();
// LOGError("ok=" . $ok);
if(substr($ok,strlen($ok)-strlen('TRANSFER OK'),strlen('TRANSFER OK')) == 'TRANSFER OK') {
echo "Generating local data\n";
$data = encryptData(generateSiteXML($contest, $localsite, $updatetime-30),myhash(trim($sitedata[2])));
$data_url = http_build_query(array('xml' => $data, 'updatetime' => ($updatetime-30)
));
$opts = array(
'http' => array(
'method' => 'POST',
'request_fulluri' => true,
'header' => 'Cookie: PHPSESSID=' . $sess . "\r\nContent-Type: application/x-www-form-urlencoded",
'content' => $data_url
)
);
if($bocaproxy != "")
$opts['http']['proxy'] = $bocaproxy;
if($bocaproxypass != "")
$opts['http']['header'] .= "\r\nProxy-Authorization: Basic " . $bocaproxypass;
$opts['http']['timeout'] = $timeo;
$context = stream_context_create($opts);
echo "Transferring data\n";
try {
$s = @file_get_contents($siteurl . "site/getsite.php", 0, $context);
} catch(Exception $e) {
$s=false;
}
if($s===false) {
echo "timeout at transferring\n";
LOGError("getMainXML: timeout at transfer for $siteurl");
return false;
}
$chstr = "\n";
if(strpos($s,$chstr) !== false) {
echo "Transfer succeeded\n";
LOGInfo("xmltransfer: OK");
} else {
echo "Transfer error (" . $s . ")\n";
LOGError("xmltransfer: failed (" . $s . ")");
}
echo "Processing received data\n";
$s = substr($s, strpos($s, $chstr) + strlen($chstr));
// LOGError("string: " . substr($s,0,50));
$s = decryptData($s,myhash (trim($sitedata[2])),'xml from main not ok');
if(strtoupper(substr($s,0,5)) != "