<?php
require_once("backend/functions.php");
dbconn();
loggedinonly();


if($CURUSER["edit_users"]!="yes")
	show_error_msg(T_("ACCESS_DENIED"),T_("YOU_DONT_HAVE_EDIT_USER_PERM"),1);

$action = $_POST["action"];

if (!$action)
	show_error_msg(T_("ERROR"), T_("TASK_NOT_FOUND"), 1);

if ($action == 'edituser'){
	$userid = $_POST["userid"];
	$title = sqlesc($_POST["title"]);
	$downloaded = strtobytes($_POST["downloaded"]);
	$uploaded = strtobytes($_POST["uploaded"]);
	$signature = sqlesc($_POST["signature"]);
	$avatar = strip_tags($_POST["avatar"]);
	$ip = sqlesc($_POST["ip"]);
	$class = (int) $_POST["class"];
	$donated = (float) $_POST["donated"];
	$password = $_POST["password"];
	$warned = sqlesc($_POST["warned"]);
	$forumbanned = sqlesc($_POST["forumbanned"]);
	$modcomment = sqlesc($_POST["modcomment"]);
	$enabled = sqlesc($_POST["enabled"]);
	$invites =(int) $_POST["invites"];
	$class = (int)$_POST["class"];
	$email = $_POST["email"];

	if (!is_valid_id($userid))
		show_error_msg(T_("EDITING_FAILED"), T_("INVALID_USERID"),1);




















	if (!validemail($email))
		show_error_msg(T_("EDITING_FAILED"), T_("EMAIL_ADDRESS_NOT_VALID"), 1);

    if ( $avatar != null )
    {    
        # Allowed Image Extenstions.
        $allowed_types = &$site_config["allowed_image_types"];    
              
        # We force http://
        if ( !preg_match( "#^\w+://#i", $avatar ) ) $avatar = "http://" . $avatar;

        # Clean Avatar Path.       
        $avatar = cleanstr( $avatar );
               
        # Validate Image.
        $im = @getimagesize( $avatar );
               
        if ( !$im[ 2 ] || !@array_key_exists( $im['mime'], $allowed_types ) )
             show_error_msg("Error", "The avatar url was determined to be of a invalid nature.", 1);
    }
    
    # Save New Avatar.
    $avatar = sqlesc($avatar);
          












//===| Start Donor status |===//
   if ($action == 'donor') {
$curusername = ($CURUSER['username']);
$curuserid = ($CURUSER['id']);
$username = sqlesc($_POST["username"]);
$userid = $_POST["userid"];
$donor = sqlesc($_POST["donor"]);
$donated = $_POST["donated"];
$total_donated = $_POST["total_donated"];
$donorlength = sqlesc($_POST["donorlength"]);
$donoruntil = get_date_time(gmtime() + $donorlength * 86400);
$days = $donorlength . " day" . ($donorlength > 1 ? "s" : "");

if ($_POST["donor"] == 'yes' && $_POST["donorlength"] == '') {
show_error_msg(T_("ERROR"), "You must specify the number of days!   [<a href=account-details.php?id=$userid><b>".T_("GO_BACK")."</b></a>]", 1);
}

if ($_POST["donor"] == 'yes' && $_POST["donated"] == '') {
show_error_msg(T_("ERROR"), "You must provide a Value for this action!   [<a href=account-details.php?id=$userid><b>".T_("GO_BACK")."</b></a>]", 1);
}

$modcomment = $row['modcomment'];
if ($_POST["donor"] != 'yes') {
$modcomment2 = gmdate("d-M-Y") . " - $curusername has donated to tracker \n";
} else {
$modcomment2 = gmdate("d-M-Y") . " - $curusername has added Donor status for $days\n- Value: $donated \n";
}
$modcomment = $modcomment2 . "" . $modcomment;
$modcom = sqlesc($modcomment);

if ($_POST["donor"] != 'yes') {
SQL_Query_exec("UPDATE users SET donor = $donor, donated = $donated, total_donated=total_donated+$donated, donoruntil = '0000-00-00 00:00:00', modcomment = CONCAT($modcom,modcomment) WHERE id = $userid");
$msg = sqlesc("Your donor status has been ADDED by ".$curusername."");
$added = sqlesc(get_date_time());
SQL_Query_exec("INSERT INTO messages (sender, receiver, msg, added) VALUES(0, $userid, $msg, $added)");
write_log("<a href=account-details.php?id=$curuserid><b>$curusername</b></a> has ADDED donor status for user <a href=account-details.php?id=$userid><b>$username</b></a>");
} else {
SQL_Query_exec("UPDATE users SET donor = $donor, donated = $donated, total_donated=total_donated+$donated, donoruntil = '$donoruntil', modcomment = CONCAT($modcom,modcomment) WHERE id = $userid");
$msg = sqlesc("You have received a DONOR STATUS from ".$curusername." for ".$days." \n REASON: You have donated ".$donated." euro , we appreciate your kindness to us.\nTHANKS!");
$added = sqlesc(get_date_time());
SQL_Query_exec("INSERT INTO messages (sender, receiver, msg, added) VALUES(0, $userid, $msg, $added)");
write_log("<a href=account-details.php?id=$curuserid><b>$curusername</b></a> has maked as donator user <a href=account-details.php?id=$userid><b>$username</b></a> for $days donated value is $donated");
}
header("Location: account-details.php?id=$userid");
die;
}
//====| End Donor status |====//





















	//change user class
	$res = SQL_Query_exec("SELECT class FROM users WHERE id=$userid");
	$arr = mysqli_fetch_row($res);
	$uc = $arr[0];

	// skip if class is same as current
	if ($uc != $class && $class > 0) {
		if ($userid == $CURUSER["id"]) {
			show_error_msg(T_("EDITING_FAILED"), T_("YOU_CANT_DEMOTE_YOURSELF"),1);
		} elseif ($uc >= get_user_class()) {
			show_error_msg(T_("EDITING_FAILED"), T_("YOU_CANT_DEMOTE_SOMEONE_SAME_LVL"),1);
		} else {
			@SQL_Query_exec("UPDATE users SET class=$class WHERE id=$userid");
			// Notify user
			$prodemoted = ($class > $uc ? "promoted" : "demoted");
			$msg = sqlesc("You have been $prodemoted to '" . get_user_class_name($class) . "' by " . $CURUSER["username"] . ".");
			$added = sqlesc(get_date_time());
			@SQL_Query_exec("INSERT INTO messages (sender, receiver, msg, added) VALUES(0, $userid, $msg, $added)");
		}
	}
	//continue updates


	SQL_Query_exec("UPDATE users SET email='$email', title=$title, downloaded='$downloaded', uploaded='$uploaded', signature=$signature, avatar=$avatar, ip=$ip, donated=$donated, forumbanned=$forumbanned, warned=$warned, modcomment=$modcomment, enabled=$enabled, invites=$invites WHERE id=$userid");

	write_log($CURUSER['username']." has edited user: $userid details");

	if ($_POST['resetpasskey']=='yes'){
		SQL_Query_exec("UPDATE users SET passkey='' WHERE id=$userid");
		//write_log($CURUSER['username']." ".T_("PASSKEY")." has been reset for: $userid");
	}

	$chgpasswd = $_POST['chgpasswd']=='yes' ? true : false;
	if ($chgpasswd) {
		$passreq = SQL_Query_exec("SELECT password FROM users WHERE id=$userid");
		$passres = mysqli_fetch_assoc($passreq);
		if($password != $passres['password']){
			$password = passhash($password);
			SQL_Query_exec("UPDATE users SET password='$password' WHERE id=$userid");
			write_log($CURUSER['username']." has changed password for user: $userid");
		}
	}
  
  header("Location: account-details.php?id=$userid");
  die;
}

if ($action == 'addwarning'){
	$userid = (int)$_POST["userid"];
	$reason = mysqli_real_escape_string($GLOBALS["DBconnector"],$_POST["reason"]);
	$expiry = (int)$_POST["expiry"];
	$type = mysqli_real_escape_string($GLOBALS["DBconnector"],$_POST["type"]);

	if (!is_valid_id($userid))
		show_error_msg(T_("EDITING_FAILED"), T_("INVALID_USERID"),1);

	if (!$reason || !$expiry || !$type){
		show_error_msg(T_("ERROR"), T_("MISSING_FORM_DATA").".", 1);
	}

	$timenow = get_date_time();

	$expiretime = get_date_time(gmtime() + (86400 * $expiry));

	$ret = SQL_Query_exec("INSERT INTO warnings (userid, reason, added, expiry, warnedby, type) VALUES ('$userid','$reason','$timenow','$expiretime','".$CURUSER['id']."','$type')");

	$ret = SQL_Query_exec("UPDATE users SET warned='yes' WHERE id='$userid'");

	$msg = sqlesc("You have been warned by " . $CURUSER["username"] . " - Reason: ".$reason." - Expiry: ".$expiretime."");
	$added = sqlesc(get_date_time());
	@SQL_Query_exec("INSERT INTO messages (sender, receiver, msg, added) VALUES(0, $userid, $msg, $added)");

	write_log($CURUSER['username']." has added a warning for user: <a href='account-details.php?id=$userid'>$userid</a>");
	header("Location: account-details.php?id=$userid");
	die;
}
































if ($action == 'donater_fix')
{
$clid = (int) $_POST["userid"];
if ($CURUSER["class"] < "6") //===| Only allow Owners
show_error_msg(T_("ERROR"), "Only the Owner can do this! &nbsp [<a href=account-details.php?id=$clid><b>".T_("GO_BACK")."</b></a>]", 1);

$payment_amount = mysqli_real_escape_string($GLOBALS["DBconnector"],$_POST["money"]);
$txn_id = mysqli_real_escape_string($GLOBALS["DBconnector"],$_POST["txn_id"]);
$reason = mysqli_real_escape_string($GLOBALS["DBconnector"],$_POST["reason"]);

if (!is_valid_id($clid)) show_error_msg(T_("EDITING_FAILED"), T_("INVALID_USER_ID"), 1);
if (!$txn_id || !$payment_amount) { show_error_msg(T_("ERROR"), T_("MISSING_FORM_DATA") . ".", 1); }

$donated_time = get_date_time();

//==| set donor time amounts
if ($payment_amount == 5)  { $days = 1;  } //--| 1 month
elseif ($payment_amount == 10) { $days = 2;  } //--| 2 months
elseif ($payment_amount == 15) { $days = 91;  } //--| 3 months
elseif ($payment_amount == 20) { $days = 122; } //--| 4 months
elseif ($payment_amount == 30) { $days = 183; } //--| 6 months
elseif ($payment_amount == 40) { $days = 244; } //--| 8 months
elseif ($payment_amount == 50) { $days = 305; } //--| 10 months
elseif ($payment_amount == 60) { $days = 366; } //--| 12 months
else { $days = 6 * $payment_amount; } //--|6 days per amount unit

//==| set donor bonus points
if ($payment_amount == 5)  { $bonuspoints = 500;  } //--| 500 points
elseif ($payment_amount == 10) { $bonuspoints = 1000; } //--| 1000 points
elseif ($payment_amount == 15) { $bonuspoints = 1500; } //--| 1500 points
elseif ($payment_amount == 20) { $bonuspoints = 2000; } //--| 2000 points
elseif ($payment_amount == 30) { $bonuspoints = 3000; } //--| 3000 points
elseif ($payment_amount == 40) { $bonuspoints = 4000; } //--| 4000 points
elseif ($payment_amount == 50) { $bonuspoints = 5000; } //--| 5000 points
elseif ($payment_amount == 60) { $bonuspoints = 6000; } //--| 6000 points
else { $bonuspoints = 100 * $payment_amount; } //--| 100 points per amount unit

//==| set invites upgrade
if ($payment_amount == 5)  { $invites = 5; } //--| 5 Invites
elseif ($payment_amount == 10) { $invites = 10; } //--| 10 Invites
elseif ($payment_amount == 15) { $invites = 15; } //--| 15 Invites
elseif ($payment_amount == 20) { $invites = 20; } //--| 20 Invites
elseif ($payment_amount == 30) { $invites = 30; } //--| 30 Invites
elseif ($payment_amount == 40) { $invites = 40; } //--| 40 Invites
elseif ($payment_amount == 50) { $invites = 50; } //--| 50 Invites
elseif ($payment_amount == 60) { $invites = 60; } //--| 60 Invites
else { $invites = 1 * $payment_amount; } //--| 1 Invite per amount unit

if (!$site_config["ratiofree_enable"]) {
//==| set donor upload upgrade
if ($payment_amount == 5)  { $donb = 107374182400;  } //--| 100 GB
elseif ($payment_amount == 10) { $donb = 214748364800;  } //--| 200 GB
elseif ($payment_amount == 15) { $donb = 322122547200;  } //--| 300 GB
elseif ($payment_amount == 20) { $donb = 429496729600;  } //--| 400 GB
elseif ($payment_amount == 30) { $donb = 644245094400;  } //--| 600 GB
elseif ($payment_amount == 40) { $donb = 858993459200;  } //--| 800 GB
elseif ($payment_amount == 50) { $donb = 1073741824000; } //--| 1000 GB
elseif ($payment_amount == 60) { $donb = 1288490188800; } //--| 1200 GB
else { $donb = 21474836480 * $payment_amount; } //--| 20 GB per amount unit
} else {
//==| set donor seedtime upgrade
if ($payment_amount == 5)  { $donb = 4320000;  } //--| 50 Days
elseif ($payment_amount == 10) { $donb = 8640000;  } //--| 100 Days
elseif ($payment_amount == 15) { $donb = 12960000; } //--| 150 Days
elseif ($payment_amount == 20) { $donb = 17280000; } //--| 200 Days
elseif ($payment_amount == 30) { $donb = 25920000; } //--| 300 Days
elseif ($payment_amount == 40) { $donb = 34560000; } //--| 400 Days
elseif ($payment_amount == 50) { $donb = 43200000; } //--| 500 Days
elseif ($payment_amount == 60) { $donb = 51840000; } //--| 600 Days
else { $donb = 864000 * $payment_amount; } //--| 10 Days per amount unit
}

//==| get user information from DB
if ($clid > 0) {
$query = "SELECT * FROM users WHERE id='" . $clid . "'";
$result = SQL_Query_exec($query);
$row = mysqli_fetch_array($result);
$username = $row['username'];
$old = date($row['donoruntil']);
$class = $row['class'];
$modcomment = $row['modcomment'];
$invitations = ($row['invites'] + $invites);
$seedpoints = ($row['seedbonus'] + $bonuspoints);
if (!$site_config["ratiofree_enable"]) {
$dona = $row['uploaded'];
} else {
$dona = $row['seedtime'];
}
}
else
{
$username = 'Guest' . rand(1000, 9999);
$class = 1;
}

//==| Get action!
if ($clid > 0)
{
//--| set class to VIP if is lower
if ($class <= 3) { $class = 3; }

$r = @SQL_Query_exec("SELECT * FROM users WHERE id=$clid");
$user = mysqli_fetch_array($r) or show_error_msg(T_("ERROR"), "No user with this ID.", 1);

//--| reset download upload post and reset to zero H&R's
$query = "UPDATE users SET donor='yes', class=$class, warned ='no', invites=$invitations, seedbonus=$seedpoints WHERE id='" . $clid . "'";
$result = SQL_Query_exec("$query");

//--| get donor time if already some left from before
if ($old <> "0000-00-00 00:00:00") {
$old1 = strtotime($old) - gmtime();
$donateuntil = get_date_time((gmtime() + $days * 86400) + $old1);
}
else {
$donateuntil = get_date_time(gmtime() + $days * 86400);
}

//--| add Upload bonus for amount given
$bonus = $donb + $dona;
$donordif = strtotime($donateuntil) - gmtime();
if (!$site_config["ratiofree_enable"]) {
$don_bonus = mksize($donb);
} else {
$don_bonus = mkprettytime($donb);
}

//--| set the names for donor time amounts
if ($days == 30) { $time = 1 . " month"; }
elseif ($days == 61) { $time = 2 . " months"; }
elseif ($days == 91) { $time = 3 . " months"; }
elseif ($days == 122) { $time = 4 . " months"; }
elseif ($days == 183) { $time = 6 . " months"; }
elseif ($days == 244) { $time = 8 . " months"; }
elseif ($days == 305) { $time = 10 . " months"; }
elseif ($days == 366) { $time = 1 ." year"; }
elseif ($days == 1) { $time = 1 . " day"; }
else { $time = $days ." days"; }

//--| add to mod comment for staff
$modcomment = $row['modcomment'];
if (!$site_config["ratiofree_enable"]) {
$modcomment3 = gmdate("d-M-Y") . " | Donor status granted for: $time | $bonuspoints Points granted | $invites Invites granted | $don_bonus Upload granted | Reason: $reason\n";
} else {
$modcomment3 = gmdate("d-M-Y") . " | Donor status granted for: $time | $bonuspoints Points granted | $invites Invites granted | $don_bonus Seed Time granted | Reason: $reason\n";
}
$modcomment = $modcomment3 . "" . $modcomment;
$modcom = sqlesc($modcomment);

//--| update users total and bonus totals
if (!$site_config["ratiofree_enable"]) {
$query = "UPDATE users SET uploaded='" . $bonus . "', modcomment = CONCAT($modcom,modcomment), donoruntil='$donateuntil', donated=$payment_amount, total_donated=total_donated+$payment_amount WHERE id='" . $clid . "'";
} else {
$query = "UPDATE users SET seedtime='" . $bonus . "', modcomment = CONCAT($modcom,modcomment), donoruntil='$donateuntil', donated=$payment_amount, total_donated=total_donated+$payment_amount WHERE id='" . $clid . "'";
}
$result = SQL_Query_exec($query);
$newclass_name = get_user_class_name('3');

//--| send message to user that donated
if (!$site_config["ratiofree_enable"]) {
$msg = sqlesc("Dear $username, thank you for your support to $site_config[SITENAME]. Your donation helps us to pay the bills and keep the site online.\nAs donor you have received now $bonuspoints Bonus Points, $don_bonus upload, $invites invites, the status of " . $newclass_name . " for $time and not least, our respect!\nAs long as you are VIP, all torrents are free for you, and you do not have to care for ratio or for the rules of hit and run.\nThanks again and enjoy of the new VIP status  :ok: \n\n$site_config[SITENAME] Staff\n");
} else {
$msg = sqlesc("Dear $username, thank you for your support to $site_config[SITENAME]. Your donation helps us to pay the bills and keep the site online.\nAs donor you have received now $bonuspoints Bonus Points, $don_bonus seed time, $invites invites, the status of " . $newclass_name . " for $time and not least, our respect!\nAs long as you are VIP, all torrents are free for you, and you do not have to care for the rules of hit and run.\nThanks again and enjoy of the new VIP status  :ok: \n\n$site_config[SITENAME] Staff\n");
}
$subject = sqlesc("Thank You for Your Donation!");
$added = sqlesc(get_date_time());
if ($clid > 0)
SQL_Query_exec("INSERT INTO messages (sender, subject, receiver, msg, added) VALUES (0, $subject, $clid, $msg, $added)");
}

//--| send message to site owners
$sysop = SQL_Query_exec("SELECT id FROM users WHERE class>7");
       while ($sysopid = mysqli_fetch_assoc($sysop)) {
$subject = "New Donation Manually Credited";
$sysopmsg = $subject." | User: $username | Amount: $payment_amount Euro | Transaction ID: $txn_id | New Class: $newclass_name";
$sysopmsg .= "\nNew Modcomment: ".$modcomment3;
SQL_Query_exec("INSERT INTO messages (poster, sender, receiver, added, subject, msg) VALUES(0, 0, " . $sysopid['id'] . ", '" . get_date_time() . "', " . sqlesc($subject) . ", " . sqlesc($sysopmsg) . ")");
       }

//--| update donation totals for counter
$added = sqlesc(get_date_time());
$month1 = sqlesc(gmdate("m"));
$year1 = sqlesc(gmdate("Y"));
$query = "INSERT INTO funds (cash,user,added,month,year) VALUES ($payment_amount, $clid, $added,$month1,$year1)";
$result = SQL_Query_exec($query);
if (!$site_config["ratiofree_enable"]) {
$modcomment4 = gmdate("d-M-Y") . " | Donor status granted for: <b>$time</b> | <b>$bonuspoints</b> Points granted | <b>$invites</b> invites granted | <b>$don_bonus</b> Upload granted | Reason: $reason \n";
} else {
$modcomment4 = gmdate("d-M-Y") . " | Donor status granted for: <b>$time</b> | <b>$bonuspoints</b> Points granted | <b>$invites</b> invites granted | <b>$don_bonus</b> Seed Time granted | Reason: $reason \n";
}
write_log($CURUSER['username']." has manual donation for user: <a href='account-details.php?id=$clid'><b>$username</b></a> | $modcomment4");

autolink("account-details.php?id=$clid","Donation Added For $username Class: $newclass_name");
die;
}





























if ($action == "deleteaccount"){
    
    if ($CURUSER["delete_users"] != "yes")//only allow admins to delete users
		show_error_msg(T_("ERROR"), T_("TASK_ADMIN"),1);

	$userid = (int)$_POST["userid"];
	$username = sqlesc($_POST["username"]);
	$delreason = sqlesc($_POST["delreason"]);

	if (!is_valid_id($userid))
		show_error_msg(T_("FAILED"), T_("INVALID_USERID"),1);

    if ($CURUSER["id"] == $userid) 
        show_error_msg(T_("ERROR"), "You cannot delete yourself.", 1);
        
	if (!$delreason){
		show_error_msg(T_("ERROR"), T_("MISSING_FORM_DATA"), 1);
	}

	deleteaccount($userid);

	write_log($CURUSER['username']." has deleted account: $username");

	show_error_msg(T_("COMPLETED"), T_("USER_DELETE"), 1);
	die;
}

?>