<?

require "include/bittorrent.php";

dbconn ();

validcan ( false, 'can_editprof' );
////// KUBOK 5 /////
$kubok = sql_query ( "SELECT id FROM users WHERE kubok5 = 'yes'" ) or sqlerr ( __FILE__, __LINE__ );
while ( $kubok2 = mysql_fetch_array ( $kubok ) ) {
	$uid = $kubok2 ["id"];
	sql_query ( "UPDATE users SET kubok5 = 'no' WHERE id = '$uid'" ) or sqlerr ( __FILE__, __LINE__ );
}
$comm = sql_query ( "SELECT id, (SELECT COUNT(*) FROM comments WHERE users.id = comments.user) AS num_comm FROM users ORDER BY num_comm DESC LIMIT 1" ) or sqlerr ( __FILE__, __LINE__ );
$comm2 = mysql_fetch_array ( $comm );
$userid = $comm2 ["id"];
if ($userid) {
	$msg = sqlesc ( "Вы получили кубок лучшему комментатору" );
	$added = sqlesc ( get_date_time () );
	$subject = sqlesc ( "Вы получили кубок" );
	sql_query ( "UPDATE users SET kubok5 = 'yes' WHERE id = '$userid'" ) or sqlerr ( __FILE__, __LINE__ );
}
////// KUBOK 5 /////
//// KUBOK 3 /////
$kubok = sql_query ( "SELECT id FROM users WHERE kubok3 = 'yes'" ) or sqlerr ( __FILE__, __LINE__ );
while ( $kubok2 = mysql_fetch_array ( $kubok ) ) {
	$uid = $kubok2 ["id"];
	sql_query ( "UPDATE users SET kubok3 = 'no' WHERE id = '$uid'" ) or sqlerr ( __FILE__, __LINE__ );
}
$comm = sql_query ( "SELECT id, (SELECT COUNT(*) FROM torrents WHERE users.id = torrents.owner) AS num_comm FROM users ORDER BY num_comm DESC LIMIT 1" ) or sqlerr ( __FILE__, __LINE__ );
$comm2 = mysql_fetch_array ( $comm );
$userid = $comm2 ["id"];
if ($userid) {
	$msg = sqlesc ( "Вы получили кубок самому активному раздающему" );
	$added = sqlesc ( get_date_time () );
	$subject = sqlesc ( "Вы получили кубок" );
	sql_query ( "UPDATE users SET kubok3 = 'yes' WHERE id = '$userid'" ) or sqlerr ( __FILE__, __LINE__ );
}
///// KUBOK 3 ////
///// KUBOK 4 ////
$kubok = sql_query ( "SELECT id FROM users WHERE kubok4 = 'yes'" ) or sqlerr ( __FILE__, __LINE__ );
while ( $kubok2 = mysql_fetch_array ( $kubok ) ) {
	$uid = $kubok2 ["id"];
	sql_query ( "UPDATE users SET kubok4 = 'no' WHERE id = '$uid'" ) or sqlerr ( __FILE__, __LINE__ );
}
$comm = sql_query ( "SELECT id, owner, (SELECT COUNT(*) FROM comments WHERE torrents.id = comments.torrent) AS num_comm FROM torrents ORDER BY num_comm DESC LIMIT 1" ) or sqlerr ( __FILE__, __LINE__ );
$comm2 = mysql_fetch_array ( $comm );
$userid = $comm2 ["owner"];
if ($userid) {
	$msg = sqlesc ( "Вы получили кубок за самую обсуждаемую раздачу" );
	$added = sqlesc ( get_date_time () );
	$subject = sqlesc ( "Вы получили кубок" );
	sql_query ( "UPDATE users SET kubok4 = 'yes' WHERE id = '$userid'" ) or sqlerr ( __FILE__, __LINE__ );
}
///// KUBOK 4 ////
//// KUBOK 2 ////
$kubok = sql_query ( "SELECT id FROM users WHERE kubok2 = 'yes'" ) or sqlerr ( __FILE__, __LINE__ );
while ( $kubok2 = mysql_fetch_array ( $kubok ) ) {
	$uid = $kubok2 ["id"];
	sql_query ( "UPDATE users SET kubok2 = 'no' WHERE id = '$uid'" ) or sqlerr ( __FILE__, __LINE__ );
}
$comm = sql_query ( "SELECT owner, times_completed FROM torrents WHERE times_completed>0 ORDER BY times_completed DESC LIMIT 1" ) or sqlerr ( __FILE__, __LINE__ );
$comm2 = mysql_fetch_array ( $comm );
$userid = $comm2 ["owner"];
if ($userid) {
	$msg = sqlesc ( "Вы получили кубок за популярную раздачу" );
	$added = sqlesc ( get_date_time () );
	$subject = sqlesc ( "Вы получили кубок" );
	sql_query ( "UPDATE users SET kubok2 = 'yes' WHERE id = '$userid'" ) or sqlerr ( __FILE__, __LINE__ );
}
//// KUBOK 2 ////
//// KUBOK 6 ////
$kubok = sql_query ( "SELECT id FROM users WHERE kubok6 = 'yes'" ) or sqlerr ( __FILE__, __LINE__ );
while ( $kubok2 = mysql_fetch_array ( $kubok ) ) {
	$uid = $kubok2 ["id"];
	sql_query ( "UPDATE users SET kubok6 = 'no' WHERE id = '$uid'" ) or sqlerr ( __FILE__, __LINE__ );
}
$comm = sql_query ( "SELECT id, donated FROM users ORDER BY donated DESC LIMIT 1" ) or sqlerr ( __FILE__, __LINE__ );
$comm2 = mysql_fetch_array ( $comm );
$userid = $comm2 ["id"];
if ($userid) {
	$msg = sqlesc ( "Вы получили кубок лучшему меценату" );
	$added = sqlesc ( get_date_time () );
	$subject = sqlesc ( "Вы получили кубок" );
	sql_query ( "UPDATE users SET kubok6 = 'yes' WHERE id = '$userid'" ) or sqlerr ( __FILE__, __LINE__ );
}
//// KUBOK 6 ////
//// KUBOK 1 ////
$kubok = sql_query ( "SELECT id FROM users WHERE kubok1 = 'yes'" ) or sqlerr ( __FILE__, __LINE__ );
while ( $kubok2 = mysql_fetch_array ( $kubok ) ) {
	$uid = $kubok2 ["id"];
	sql_query ( "UPDATE users SET kubok1 = 'no' WHERE id = '$uid'" ) or sqlerr ( __FILE__, __LINE__ );
}
$comm = sql_query ( "SELECT t.owner, t.times_completed FROM torrents AS t LEFT JOIN ratings AS r ON r.rating>0 ORDER BY r.rating DESC LIMIT 1" ) or sqlerr ( __FILE__, __LINE__ );
$comm2 = mysql_fetch_array ( $comm );
$userid = $comm2 ["owner"];
if ($userid) {
	$msg = sqlesc ( "Вы получили кубок за лучшую раздачу" );
	$added = sqlesc ( get_date_time () );
	$subject = sqlesc ( "Вы получили кубок" );
	sql_query ( "UPDATE users SET kubok1 = 'yes' WHERE id = '$userid'" ) or sqlerr ( __FILE__, __LINE__ );
}
//// KUBOK 1 ////
location ( "index.php", 2 );
global $tracker_lang;
stdhead ( $tracker_lang ['success'] . "!" );
stdmsg ( $tracker_lang ['success'], "Вы успешно обновили кубки!", 'success' );
stdfoot ();
exit ();

?>