<?php

/*
// +--------------------------------------------------------------------------+
// | Teams anzeigen und verwalten                                             |
// | by truenoir                                                              |
// +--------------------------------------------------------------------------+
// | Obige Zeilen dürfen nicht entfernt werden!    Do not remove above lines! |
// +--------------------------------------------------------------------------+
*/

require "include/bittorrent.php";
dbconn();
loggedinorreturn();

function bark($msg="Zugriff verweigert")
{
  stderr("Fehler",$msg);
}

function checkleader($teamid)
{
  global $CURUSER;

  $ok=false;
  $res=mysql_query("select leader from teammembers where teamid=$teamid and userid=".sqlesc($CURUSER["id"]));
  if(mysql_num_rows($res)>0)
  {
    $r=mysql_fetch_assoc($res);
    if($r["leader"]=="yes") $ok=true;
  }
  return($ok);
}

$admin=get_user_class()>=UC_ADMINISTRATOR;
$mod=get_user_class()>=UC_MODERATOR;
$act=$_POST["act"];
if(!$act) $act=$_GET["act"];
if(!$act) bark("Falsche oder fehlende Parameter");

if($act=="neu")
{
  if(!$admin)
    bark();

  $name=sqlesc(trim($_POST["teamname"]));
  $banner=sqlesc(trim($_POST["banner"]));
  $typ=sqlesc(trim($_POST["typ"]));
  $suffix=sqlesc(trim($_POST["suffix"]));
  $row=mysql_fetch_row(mysql_query("select max(sort)+1 from teams"));
  if($row[0]<1)$row[0]=1;
  mysql_query("insert into teams (name,banner,typ,suffix,sort) values ($name,$banner,$typ,$suffix,$row[0])");
}
elseif($act=="edit")
{
  if(!$admin)
    bark();

  $teamid=0+$_POST["id"];
  $name=sqlesc(trim($_POST["teamname"]));
  $banner=sqlesc(trim($_POST["banner"]));
  $typ=sqlesc(trim($_POST["typ"]));
  $suffix=sqlesc(trim($_POST["suffix"]));
  $SQL="update teams set name=$name,banner=$banner,typ=$typ,suffix=$suffix where id=$teamid";
  mysql_query($SQL);
}
elseif($act=="leader")
{
  $teamid=sqlesc(0+$_GET["team"]);
  $userid=sqlesc(0+$_GET["user"]);

  if(!$mod&&!checkleader($teamid))
    bark();

  mysql_query("update teammembers set leader='no' where teamid=$teamid");
  mysql_query("update teammembers set leader='yes' where teamid=$teamid and userid=$userid");
}
elseif($act=="delmember")
{
  $teamid=sqlesc(0+$_GET["team"]);
  $userid=sqlesc(0+$_GET["user"]);

  if(!$mod&&!checkleader($teamid))
    bark();

  mysql_query("delete from teammembers where teamid=$teamid and userid=$userid");
}
elseif($act=="delete")
{
  if(!$admin)
    bark();

  $teamid=0+$_GET["team"];
  mysql_query("update torrents set team=0 where team=$teamid"); // Torrents zurücksetzen
  mysql_query("delete from teammembers where teamid=$teamid");
  mysql_query("delete from teams where id=$teamid");
}
elseif($act=="moveup"||$act=="movedown")
{
  if(!$admin)
    bark();

  $teamid=0+$_GET["team"];
  $sort=0+$_GET["sort"];
  $where="where sort ".($act=="moveup"?"<":">")." $sort order by sort ".($act=="moveup"?"desc":"")." limit 1";
  $data=mysql_fetch_assoc(mysql_query("select * from teams $where"));
  mysql_query("update teams set sort=$sort where id=$data[id]");
  mysql_query("update teams set sort=$data[sort] where id=$team");
}
elseif($act=="useradd")
{
  $destid=sqlesc(0+$_POST["destid"]);
  $teamid=sqlesc(0+$_POST["group"]);

  if(!checkleader($teamid))
    bark();

  mysql_query("insert into teammembers (teamid,userid,leader) values ($teamid,$destid,'no')");
  Header("Location: userdetails.php?id=$_POST[destid]");
  die();
}
Header("Location: teams.php");
?>