// ЛПМ-ЧП ЛПННЕОФПЧ ОБ УФТБОЙГЕ
$PAGE_COMMENTS = 15;
// ДЙТЕЛФПТЙС У ЛБТФЙОЛБНЙ Ч ДОЕЧОЙЛЕ
$DIARYDIR = 'diary/';
// ДЙТЕЛФПТЙС У ЛБТФЙОЛБНЙ АЪЕТПЧ ДМС НЕИБОПЙДПЧ 2
$MECH2SHOTSDIR = "mech2/contest_shots/";
// ДЙТЕЛФПТЙС У ЛБТФЙОЛБНЙ ДМС ЬОГЙЛМПРЕДЙЙ
$MECH2PEDIADIR = "mech2/pedia_shots/";
function lang_en()
{
if (isset($_GET['l']) && $_GET['l']=='en') return 1;
if (!isset($_GET['l']) || (isset($_GET['l']) && $_GET['l']=='ru')) return 0;
}
// РПМХЮЙФШ ЧУЕ ЖБКМЩ ДЙТЕЛФПТЙЙ
function get_files($dirname)
{
// РПМХЮЙФШ УРЙУПЛ ФЕЛУФПЧ Ч ДЙТЕЛФПТЙЙ РТПЕЛФБ
$dir = @opendir($dirname);
if (!$dir)
{
echo iconv('koi8-r', 'cp1251', 'РХУФП!');
return;
}
$num = 0;
$arr = array();
while (($file = readdir($dir)) != false)
{
if ($file[0] == '.') continue;
$arr[$num] = $file;
$num++;
}
closedir($dir);
return $arr;
}
// РПМХЮЙФШ ЧУЕ УЛТЙОЫПФЩ ДЙТЕЛФПТЙЙ (ОЕ *_s.*)
function get_files_shots($dirname)
{
// РПМХЮЙФШ УРЙУПЛ ФЕЛУФПЧ Ч ДЙТЕЛФПТЙЙ РТПЕЛФБ
$dir = @opendir($dirname);
if (!$dir)
{
echo iconv('koi8-r', 'cp1251', 'РХУФП!');
return;
}
$num = 0;
$arr = array();
while (($file = readdir($dir)) != false)
{
if ($file[0] == '.' || strpos($file, "_s.")) continue;
$arr[$num] = $file;
$num++;
}
closedir($dir);
return $arr;
}
// ПВТБВПФЛБ УРЕГУЙНЧПМПЧ HTML РЕТЕД ЧЩЧПДПН УФТПЛЙ Ч HTML
function hq($s)
{
return htmlspecialchars($s);
}
// ДПВБЧМЕОЙЕ '\' Ч УФТПЛХ РЕТЕД ЪБОЕУЕОЙЕН Ч ВБЪХ
function sq($s)
{
return addslashes($s);
//return $s;
}
?>
// функции для сайта гонок
// вывести на экран скриншоты
function racing_random_shots($block, $page_lang)
{
$MAXSHOTS = 3;
$dirname = "pic/shots/";
// $arr = get_files_shots($dirname);
$arr = array('CEBA_aimracing_199.jpg', 'Slavon_aimracing_59.jpg',
'Vadim_aimracing_8.jpg');
$num = sizeof($arr);
if (!$num) return 0;
if ($num < $MAXSHOTS) $MAXSHOTS = $num;
$nn = array_rand($arr, $MAXSHOTS);
$dbh = db_connect();
if (!$dbh)
{
echo("Could not connect to base!");
return 0;
}
for ($i = 0; $i < $MAXSHOTS; $i++)
{
if ($MAXSHOTS == 1) $file = $arr[$nn];
else $file = $arr[$nn[$i]];
list ($name, $ext) = split('\.', $file);
$filebig = $file;
$file = $name.'_s.gif';
$q = "SELECT * FROM racing_shots WHERE filename = '$filebig'";
$res = db_query($dbh, $q);
if ($res && db_rows($res) == 1)
$desc = iconv('koi8-r', 'cp1251', db_get($res, 0, "description"));
else $desc = '';
$id = db_get($res, 0, "id");
$desc = preg_replace("/\n/", " ", $desc);
include $block;
}
db_disconnect($dbh);
}
// получить ID случайного скрина
function racing_random_shot()
{
global $dbh;
$dirname = 'pic/shots/';
$arr = get_files_shots($dirname);
if (!sizeof($arr)) return -1;
$file = $arr[array_rand($arr)];
list ($name, $ext) = split('\.', $file);
$filebig = $file;
$q = "SELECT * FROM racing_shots WHERE filename = '$filebig'";
$res = db_query($dbh, $q);
if (!$res || !db_rows($res)) return -1;
return db_get($res, 0, "id");
}
// скрин
function racing_shot($shotid)
{
// получить случайный скрин
if (!$shotid) $shotid = racing_random_shot();
if ($shotid <= 0) return;
global $dbh;
global $gpic;
$dirname = "pic/shots/";
$q = "SELECT * FROM racing_shots WHERE id = $shotid";
$res = db_query($dbh, $q);
$shotid = db_get($res, 0, "id");
$name = iconv('koi8-r', 'cp1251', db_get($res, 0, "name"));
$file = iconv('koi8-r', 'cp1251', db_get($res, 0, "filename"));
$desc = '';
$desc = iconv('koi8-r', 'cp1251', db_get($res, 0, "description"));
$desc = preg_replace("/\n/", " ", $desc);
// РПМХЮЕОЙЕ УМЕД Й РТЕД IDПЧ
/*
$previd = 0;
$nextid = 0;
$res = db_query($dbh, "SELECT id FROM mech2_shots WHERE id < $shotid ORDER BY id DESC LIMIT 1");
if (db_rows($res)) $previd = db_get($res, 0, "id");
$res = db_query($dbh, "SELECT id FROM mech2_shots WHERE id > $shotid ORDER BY id ASC LIMIT 1");
if (db_rows($res)) $nextid = db_get($res, 0, "id");
if ($a == 'shot')
*/
include 'blocks/shot';
}
// скрины гонок
function racing_shots($id)
{
global $dbh;
$dirname = "pic/shots/";
$q = "SELECT * FROM racing_shots ORDER BY filename ASC";
$res = db_query($dbh, $q);
if (!$res || !db_rows($res)) return;
$cnt = 0;
for ($i = 0; $i < db_rows($res); $i++)
{
$file = db_get($res, $i, "filename");
list ($name, $ext) = split('\.', $file);
$filebig = $file;
$file = $name.'_s.gif';
if (!file_exists($dirname.$filebig) ||
!file_exists($dirname.$file))
continue;
if (!$cnt) echo '
'."\n";
$shotid = db_get($res, $i, "id");
$name = iconv('koi8-r', 'cp1251', db_get($res, $i, "name"));
$desc = '';
$desc = iconv('koi8-r', 'cp1251', db_get($res, $i, "description"));
$desc = preg_replace("/\n/", " ", $desc);
include 'blocks/shotlist_item';
if ($cnt) echo "
\n";
$cnt = !$cnt;
}
}
// запись в лог
function mylog($msg)
{
global $logfile;
$ipaddr = $_SERVER['REMOTE_ADDR'];
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddr.= '-'.@$_SERVER['HTTP_X_FORWARDED_FOR'];
$time = strftime("%H:%M");
error_log("$time : $ipaddr : $msg\n", 3, $logfile);
}
// ====================================================================
// функция кодирования RC4
/**
* The symmetric encryption function
*
* @param string $pwd Key to encrypt with (can be binary of hex)
* @param string $data Content to be encrypted
* @param bool $ispwdHex Key passed is in hexadecimal or not
* @access public
* @return string
*/
function encrypt ($pwd, $data)
{
$key[] = '';
$box[] = '';
$cipher = '';
$pwd_length = strlen($pwd);
$data_length = strlen($data);
for ($i = 0; $i < 256; $i++)
{
$key[$i] = ord($pwd[$i % $pwd_length]);
$box[$i] = $i;
}
for ($j = $i = 0; $i < 256; $i++)
{
$j = ($j + $box[$i] + $key[$i]) & 255;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
/*$s = "";
for ($i = 0; $i < 256; $i++)
{
$v = $box[$i];
$s .= "[$i] = $v\n";
}
mylog($s);*/
$s = "";
for ($a = $j = $i = 0; $i < $data_length; $i++)
{
$a = ($a + 1) & 255;
$j = ($j + $box[$a]) & 255;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$k = $box[(($box[$a] + $box[$j]) & 255)];
$s .= "[$i] = $k\n";
$cipher .= chr((ord($data[$i]) ^ $k) & 255);
}
// mylog($s);
return $cipher;
}
// Декодирование данных
function racing_decode_data(&$data)
{
$data = @pack('H*', $data);
$data = encrypt("FlkVythfmYJFJGHhgdfjkbjGDfhbvпВромтиМСгшршопаРПглркЫроиШгп", $data);
//mylog($data);
$pos = strpos($data, ' ');
if ($pos === false)
{
return 0;
}
$in_CRC = substr($data, 0, $pos);
$data = substr($data, $pos + 1);
$CRC = sprintf('%d', crc32($data));
if ($CRC != $in_CRC)
{
mylog("InCRC = $in_CRC, CRC = $CRC");
return 0;
}
return 1;
}
// добавление записи в рекорды трасс
function racing_add_track_record($data, $isdemo = 0)
{
global $dbh;
$array = preg_split('/[=\n]/', $data);
array_shift($array);
array_pop($array);
for($i = 0; $i < sizeof($array); $i += 2)
{
$key = rtrim(trim($array[$i]));
$val = rtrim(trim($array[$i + 1]));
// mylog("[$key] => [$val]");
${$key} = $val;
}
// получение ID трека по его строковому ID
$res = db_query($dbh,
"SELECT * FROM racing_tracks WHERE stringid = '$TrackID'");
mylog("dbh:".$dbh);
if (!$res || !db_rows($res))
{
mylog("No such track [$TrackID]");
return 0;
}
$TrackID = db_get($res, 0, "id");
// получение ID глайдера по его строковому ID
$res = db_query($dbh,
"SELECT * FROM racing_gliders WHERE stringid = '$GliderID'");
if (!$res || !db_rows($res))
{
mylog("No such glider [$GliderID]");
return 0;
}
$GliderID = db_get($res, 0, "id");
$ip = $_SERVER['REMOTE_ADDR'];
$ip2 = @$_SERVER['HTTP_X_FORWARDED_FOR'];
if ($isdemo) $GUID = 'demo';
// проверка на существование такого рекорда
$res = db_query($dbh,
"SELECT * FROM racing_track_records ".
"WHERE TrackID = $TrackID AND CategoryID = $CategoryID AND ".
($isdemo ? "Name = '$Name'" : "GUID = '$GUID' ").
"AND Value = $Value");
if ($res && db_rows($res) > 0)
{
mylog("Trying to add same record.");
return 1;
}
$res = db_query($dbh,
"INSERT INTO racing_track_records (ipaddr, ipaddr2, date, Difficulty, TrackID, CategoryID, ".
"Name, GUID, Value, GliderID, BestLapTime, AverageSpeed, MaxSpeed, ".
"Killed, Outed, Accuracy, Rating) ".
"VALUES ('$ip', '$ip2', now(), $Difficulty, $TrackID, $CategoryID, '$Name', ".
"'$GUID', $Value, $GliderID, ".
"$BestLapTime, $AverageSpeed, $MaxSpeed, $Killed, $Outed, $Accuracy, ".
"$Rating)");
if (!$res)
{
mylog("Error adding record : ".mysql_error($dbh));
return 0;
}
// удаление всех рекордов игрока для данного сочетания кроме лучшего
$order = ($CategoryID == 0 ? "ASC" : "DESC");
$res = db_query($dbh, "SELECT ID FROM racing_track_records ".
"WHERE CategoryID = $CategoryID AND TrackID = $TrackID AND ".
($isdemo ? "Name = '$Name'" : "GUID = '$GUID' ").
"ORDER BY Value $order LIMIT 1");
if (!$res || !db_rows($res)) return 1;
$ID = db_get($res, 0, "id");
if ($ID > 0)
$res = db_query($dbh, "DELETE FROM racing_track_records ".
"WHERE CategoryID = $CategoryID AND TrackID = $TrackID AND ".
($isdemo ? "Name = '$Name'" : "GUID = '$GUID' ").
"AND ID <> $ID ");
return 1;
}
// добавление записи в рекорды чемпионатов
function racing_add_championship_record($data, $isdemo = 0)
{
global $dbh;
$array = preg_split('/[=\n]/', $data);
array_shift($array);
array_pop($array);
for($i = 0; $i < sizeof($array); $i += 2)
{
$key = rtrim(trim($array[$i]));
$val = rtrim(trim($array[$i + 1]));
// mylog("[$key] => [$val]");
${$key} = $val;
}
$ip = $_SERVER['REMOTE_ADDR'];
$ip2 = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$res = db_query($dbh,
"INSERT INTO racing_championship_records (ipaddr, ipaddr2, date, ".
"Difficulty, Name, GUID, ".
"Rating, TotalTime, AverageSpeed, MaxSpeed, Killed, Outed, Accuracy, ".
"WinCount, LossCount, Upgrade0, Upgrade1, Upgrade2, Upgrade3, Upgrade4) ".
"VALUES ('$ip', '$ip2', now(), $Difficulty, '$Name', '$GUID', $Rating, ".
"$TotalTime, $AverageSpeed, ".
"$MaxSpeed, $Killed, $Outed, $Accuracy, $WinCount, $LossCount, ".
"$Upgrade0, $Upgrade1, $Upgrade2, $Upgrade3, $Upgrade4) ");
if (!$res)
{
mylog("Error adding record : ".mysql_error($dbh));
return 0;
}
return 1;
}
// получение записей для данного трека
function racing_get_track_records($data)
{
global $dbh;
$array = preg_split('/[=\n]/', $data);
array_shift($array);
array_pop($array);
for($i = 0; $i < sizeof($array); $i += 2)
{
$key = rtrim(trim($array[$i]));
$val = rtrim(trim($array[$i + 1]));
// mylog("[$key] => [$val]");
${$key} = $val;
}
// получение ID трека по его строковому ID
$res = db_query($dbh,
"SELECT * FROM racing_tracks WHERE stringid = '$TrackID'");
if (!$res || !db_rows($res))
{
mylog("No such track [$TrackID]");
return 0;
}
$TrackID = db_get($res, 0, "id");
$str_order = ($CategoryID == 0 ? "ASC" : "DESC");
$res = db_query($dbh,
"SELECT Difficulty, CategoryID, racing_track_records.Name, ".
"racing_track_records.GUID, Value, ".
"racing_gliders.stringid AS GliderID, racing_gliders.name AS GliderName, ".
"BestLapTime, AverageSpeed, MaxSpeed, Killed, Outed, Accuracy, Rating ".
"FROM racing_track_records, racing_gliders ".
"WHERE racing_track_records.TrackID = $TrackID AND ".
"racing_track_records.GliderID = racing_gliders.ID AND ".
"racing_track_records.CategoryID = $CategoryID AND ".
"racing_track_records.Difficulty = $Difficulty ".
"ORDER BY Value $str_order ".
"LIMIT 100");
if (!$res)
{
mylog("Error getting records : ".mysql_error($dbh));
return 0;
}
echo db_rows($res)."\n";
while ($row = db_get_hash($res))
{
echo "Record\n";
// mylog("Record\n");
foreach (array_keys($row) as $key)
{
echo "$key=".$row[$key]."\n";
// mylog("$key=".$row[$key]);
}
}
return 1;
}
// получение записей для чемпионатов
function racing_get_championship_records($data)
{
global $dbh;
$array = preg_split('/[=\n]/', $data);
array_shift($array);
array_pop($array);
for($i = 0; $i < sizeof($array); $i += 2)
{
$key = rtrim(trim($array[$i]));
$val = rtrim(trim($array[$i + 1]));
// mylog("[$key] => [$val]");
${$key} = $val;
}
$res = db_query($dbh,
"SELECT Difficulty, racing_championship_records.Name, ".
"racing_championship_records.GUID, Rating, ".
"TotalTime, AverageSpeed, MaxSpeed, Killed, Outed, Accuracy, ".
"WinCount, LossCount, Upgrade0, Upgrade1, Upgrade2, Upgrade3, Upgrade4 ".
"FROM racing_championship_records ".
"WHERE Difficulty = $Difficulty ".
"ORDER BY Rating DESC ".
"LIMIT 100");
if (!$res)
{
mylog("Error getting records : ".mysql_error($dbh));
return 0;
}
echo db_rows($res)."\n";
// mylog(db_rows($res));
while ($row = db_get_hash($res))
{
echo "Record\n";
foreach (array_keys($row) as $key)
{
echo "$key=".$row[$key]."\n";
// mylog("$key=".$row[$key]);
}
}
return 1;
}
// получение записей для чемпионатов
function racing_championship_records($Difficulty)
{
global $dbh;
$res = db_query($dbh,
"SELECT Difficulty, racing_championship_records.Name, Rating, ".
"TotalTime, AverageSpeed, MaxSpeed, Killed, Outed, Accuracy, ".
"WinCount, LossCount, Upgrade0, Upgrade1, Upgrade2, Upgrade3, Upgrade4 ".
"FROM racing_championship_records ".
"WHERE Difficulty = $Difficulty ".
"ORDER BY Rating DESC ".
"LIMIT 100");
if (!$res) return 0;
for ($i = 0; $i < db_rows($res); $i++)
{
$name = db_get($res, $i, "name");
$rating = db_get($res, $i, "rating");
include 'blocks/cship_record';
}
return 1;
}
// вывести список доступных треков
function racing_track_options()
{
global $dbh;
$res = db_query($dbh, "SELECT * FROM racing_tracks ORDER BY Name");
if (!$res) return 0;
for ($i = 0; $i < db_rows($res); $i++)
{
$id = db_get($res, $i, "id");
$name = db_get($res, $i, "name");
echo "