<?php
ob_start();
require_once("init.php");
if(!permission("adminuser")){
	echo error($langadmin['noperms']);
	exit();
}
$num = 1;
$ok = 1;

$limit = ($_REQUEST['pp']!='') ? $_REQUEST['pp'] : 15;

define("USER","YES");

$tmpl = new Koobi("templates/" . ADMINTHEME . "/user/");
$tmpl->assign("lang", $lang);
$tmpl->assign("langadmin", $langadmin);
$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign('pref', $pref);

if($_REQUEST['subaction'] == "userfiles"){
	switch($_REQUEST['action']){
		
		// neu anlegen
		case 'new':
		$timetill = mktime(0, 0, 0, $_REQUEST['m_n'], $_REQUEST['d_n'], $_REQUEST['y_n']);
		foreach($_POST['downloadcat'] as $dlcat){
			
		$sql = new dbc("INSERT INTO ".PREFIX."_downloadcat_restricted (
		id,
		uid,
		catid,
		time_till
		) VALUES (
		'',
		'$_REQUEST[uid]',
		'$dlcat',
		'$timetill'
		)
		");
		}
		
		header("Location:user.php?subaction=userfiles&uid=$_REQUEST[uid]");
		break;
		
		
		case 'save':
		// loeschen
		if($_POST['del'] >= 1){
			foreach ($_POST['del'] as $id => $del) {
				if( $del != "")  {
					$result = new dbc("DELETE FROM  ".PREFIX."_downloadcat_restricted  WHERE id = '$id'");
				}
			}
			header("Location:user.php?subaction=userfiles&uid=$_REQUEST[uid]");
		}
		
		// aktualisieren
		if($_POST['y'] >= 1){
			foreach ($_POST['y'] as $id => $y) {
				if( $y != "")  {
					$time_till = mktime(0, 0, 0, $_REQUEST['m'][$id], $_REQUEST['d'][$id], $_REQUEST['y'][$id]);
					$sql = new dbc("UPDATE ".PREFIX."_downloadcat_restricted set time_till='$time_till' WHERE id = '$id'");
				}
			}
		}
		header("Location:user.php?subaction=userfiles&uid=$_REQUEST[uid]");
		break;
		
		
		case '':
		case 'show':
		
		// download- kategorien (beschraenkt) fuer user anzeigen
		$userfilecat = array();
		$sql = new dbc("SELECT * FROM " . PREFIX . "_downloadcat_restricted WHERE uid = '$_REQUEST[uid]'");
		while($row = $sql->fetchrow()){
			$sql_2 = new dbc("SELECT catname FROM " . PREFIX . "_downloadcat WHERE catid = '$row->catid'");
			$row_2 = $sql_2->fetchrow();
			$row->dlcatname = $row_2->catname;
			
			$row->date_day = date("d",$row->time_till);
			$row->date_month = date("m",$row->time_till);
			$row->date_year = date("Y",$row->time_till);
			
			array_push($userfilecat, $row);
		}
		
		// kategorien
		$cat_sel= array();
		getDownloadcategs_simple(0, "", $cat_sel, $area);
		$tmpl->assign('cat_sel', $cat_sel);
		$tmpl->assign('userfilecat', $userfilecat);
		$tmpl->display('userfiles.tpl');
		break;
	}
	} else {
	
	
	if(isset($_REQUEST['action'])){
		if(!permission("adminuseredit")) { echo error($langadmin['noperms']); exit();}
		if($_REQUEST['action']=="edit"){
			
			// Benutzer speichern
			if($_REQUEST['send']==1){
				$ok = 1;
				
				// Eingaben prüfen
				$sql =& new dbc("SELECT uid FROM " . PREFIX . "_user WHERE uname='".escs(ltrim(rtrim($_POST['uname'])))."' AND uid!='".$_REQUEST['id']."'");
				$c= $sql->numrows();
				if($c > 0) {$errormsg .= "<li class=errorfont>".$langadmin['unameexists']."</li>"; $ok=-1;}
				
				// Benutzername prüfen
				if(empty($_REQUEST['uname'])) {$errormsg .= "<li class=errorfont>".$langadmin['unoname']."</li>"; $ok=-1;}
				$pname = str_replace(array("[","]",":","(",")","=","-","*","+"), "", ltrim(rtrim($_POST['uname'])));
				if(ereg("[^ ._A-Za-zÀ-ÖØ-öø-ÿ0-9-]", $pname)){
					$errormsg .= "<li class=errorfont>".$langadmin['unamwrongchars']."</li>";
				$ok = -1;}
				
				// Email prüfen
				if(empty($_REQUEST['email'])) {$errormsg .= "<li class=errorfont>".$langadmin['unomail']."</li>"; $ok=-1;}
				if(!ereg("^[_A-Za-zÀ-ÖØ-öø-ÿ0-9-]+(\.[_A-Za-zÀ-ÖØ-öø-ÿ0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$", $_REQUEST['email'])){
					$errormsg .= "<li class=errorfont>".$langadmin['unomailwrong']."</li>";
				$ok = -1;}
				
				// Existiert Email schon?
				$sql =& new dbc("SELECT uid FROM " . PREFIX . "_user WHERE email='".escs($_REQUEST['email'])."' AND uid!='".$_REQUEST['id']."'");
				$c1= $sql->numrows();
				if($c1 > 0) {$errormsg .= "<li class=errorfont>".$langadmin['umailexists']."</li>"; $ok=-1;}
				
				
				$q = "UPDATE ".PREFIX."_user SET ";
				if( (!empty($_REQUEST['dd'])) && (!empty($_REQUEST['mm'])) && (!empty($_REQUEST['yyyy'])) ) {
					$q .= "user_birthday='".$_REQUEST['dd'].".".$_REQUEST['mm'].".".$_REQUEST['yyyy']."', ";
				}
				
				if( !empty($_REQUEST['pass']) ) {
					$q .= "pass='".ltrim(rtrim(md5($_REQUEST['pass'])))."',";
				}
				
				$q .= "uname='".$_REQUEST['uname']."', ";
				$q .= "email='".$_REQUEST['email']."', ";
				$q .= "ugroup='".$_REQUEST['group']."', ";
				$q .= "url='".httptolink($_REQUEST['url'])."', ";
				$q .= "user_aim='".$_REQUEST['user_aim']."', ";
				$q .= "user_icq='".$_REQUEST['user_icq']."', ";
				$q .= "user_sig='".iform($_REQUEST['user_sig'],"5000","")."', ";
				$q .= "invisible='".$_REQUEST['invisible']."', ";
				$q .= "user_canpn='".$_REQUEST['user_canpn']."', ";
				$q .= "pntomail='".$_REQUEST['pntomail']."', ";
				$q .= "user_interests='".iform($_REQUEST['user_interests'],"5000","")."', ";
				$q .= "group_id_misc='" . @implode(";", $_REQUEST['group_misc']) . "', ";
				$q .= "title = '".$_REQUEST['title']."', ";
				$q .= "name =  '".$_REQUEST['name']."', ";
				$q .= "lastname =  '".$_REQUEST['lastname']."', ";
				$q .= "street = '".$_REQUEST['street']."', ";
				$q .= "zip = '".$_REQUEST['zip']."', ";
				$q .= "user_from = '".$_REQUEST['user_from']."', ";
				$q .= "phone = '".$_REQUEST['phone']."', ";
				$q .= "fax = '".$_REQUEST['fax']."', ";
				$q .= "phone_mobile = '".$_REQUEST['phone_mobile']."' ";
				$q .= "WHERE uid='".$_REQUEST['id']."'";
				
				if($ok==1){
					$sql =& new dbc("$q");
					
					if(!empty($_REQUEST['emailtext'])){
						@mail("".$_REQUEST['email']."", kstripslashes($_REQUEST['subject']) , kstripslashes($_REQUEST['emailtext']),
						"From: ".$pref['sendemail']."\nReply-To: ".$pref['company_email_reply']."\nX-Mailer: PHP/" . phpversion());
					}
					
					header("Location:user.php");
					
				}
				
				
			}
			
			// PN löschen START
			if($_REQUEST['do']=="delpn"){
				if($_REQUEST['what']=="in"){
					$sql =& new dbc("DELETE FROM ".PREFIX."_pn WHERE to_uid='".$_REQUEST['id']."' AND typ='inbox'");
				}else{
					$sql =& new dbc("DELETE FROM ".PREFIX."_pn WHERE from_uid='".$_REQUEST['id']."' AND typ='outbox'");
				}
			}
			// PN löschen ENDE
			$sql =& new dbc("SELECT * FROM ".PREFIX."_user WHERE uid='".$_REQUEST['id']."'");
			$row = $sql->fetchrow();
			
			$sqlg = new dbc("SELECT * FROM ".PREFIX."_usergroup  WHERE ugroup!='2'");
			$selugroup = array();
			while ($rowg = $sqlg->fetchrow()) {
				array_push($selugroup, $rowg);
			}
			
			$birth = explode(".",$row->user_birthday);
			$sql2 = new dbc("SELECT pnid FROM ".PREFIX."_pn WHERE to_uid='".$_REQUEST['id']."' AND typ='inbox'");
			$pnin = $sql2->numrows();
			
			$sql3 =& new dbc("SELECT pnid FROM ".PREFIX."_pn WHERE from_uid='".$_REQUEST['id']."' AND typ='outbox'");
			$pnout = $sql3->numrows();
			
			// BENUTZER BEARBEITEN 
			$tmpl->assign("ok", $ok);
			$tmpl->assign("form", "user.php?action=edit&id=".$_REQUEST['id']."");
			$tmpl->assign("hiddenpage", $_REQUEST['page']);
			$tmpl->assign("errormsg", $errormsg);
			$tmpl->assign("row", $row);
			$tmpl->assign("rowg", $rowg);
			$tmpl->assign("birth", $birth);
			$tmpl->assign("pnin", $pnin);
			$tmpl->assign("pnout", $pnout);
			$tmpl->assign("selugroup", $selugroup);
			$tmpl->assign("onkeypress", "if(event.keyCode < 48 || event.keyCode > 57) { event.returnValue = false; } else {if(event.which < 48 || event.which > 57) return false;}");
			$tmpl->display("user_editnew.tpl");
		}
		
		// ==============================================================
		// <<-- Neuen Benutzer anlegen -->>
		// ==============================================================
		if($_REQUEST['action']=="new"){
			if($_REQUEST['send']==1){
				$ok = 1;
				
				// Eingaben prüfen
				$sql =& new dbc("SELECT uid FROM " . PREFIX . "_user WHERE uname='".escs(ltrim(rtrim($_POST['uname'])))."' AND uid!='".$_REQUEST['id']."'");
				$c= $sql->numrows();
				if($c > 0) {$errormsg .= "<li class=errorfont>".$langadmin['unameexists']."</li>"; $ok=-1;}
				
				// Benutzername prüfen
				if(empty($_REQUEST['uname'])) {$errormsg .= "<li class=errorfont>".$langadmin['unoname']."</li>"; $ok=-1;}
				$pname = str_replace(array("[","]",":","(",")","=","-","*","+"), "", ltrim(rtrim($_POST['uname'])));
				if(ereg("[^ ._A-Za-zÀ-ÖØ-öø-ÿ0-9-]", $pname)){
					$errormsg .= "<li class=errorfont>".$langadmin['unamwrongchars']."</li>";
				$ok = -1;}
				
				// Ist Benutzername in Bannliste eingetragen?
				if(bannedname(ltrim(rtrim($_REQUEST['uname'])))){$errormsg .= "<li class=errorfont>".$langadmin['banned']."</li>"; $ok=-1;}
				
				
				// Kennwort prüfen
				if(empty($_REQUEST['pass'])) { $errormsg .= "<li class=errorfont>".$langadmin['nopass']."</li>"; $ok=-1;}
				
				
				// Email prüfen
				if(empty($_REQUEST['email'])) {$errormsg .= "<li class=errorfont>".$langadmin['unomail']."</li>"; $ok=-1;}
				if(!ereg("^[_A-Za-zÀ-ÖØ-öø-ÿ0-9-]+(\.[_A-Za-zÀ-ÖØ-öø-ÿ0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$", $_REQUEST['email'])){
					$errormsg .= "<li class=errorfont>".$langadmin['unomailwrong']."</li>";
				$ok = -1;}
				
				// Existiert Email schon?
				$sql =& new dbc("SELECT uid FROM " . PREFIX . "_user WHERE email='".escs($_REQUEST['email'])."' AND uid!='".$_REQUEST['id']."'");
				$c1= $sql->numrows();
				if($c1 > 0) {$errormsg .= "<li class=errorfont>".$langadmin['umailexists']."</li>"; $ok=-1;}
				
				// Ist Emailin Bannliste eingetragen?
				if(bannedemail(ltrim(rtrim($_REQUEST['email'])))){$errormsg .= "<li class=errorfont>".$langadmin['isbanned_email']."</li>"; $ok=-1;}
				
				// Wurde Gruppe gewählt?
				if(empty($_REQUEST['group'])) { $errormsg .= "<li class=errorfont>".$langadmin['noselgroup']."</li>"; $ok=-1;}
				
				// Wenn alles io ist, Benutzer anlegen...
				if($ok==1){
					
					$q = "INSERT INTO ".PREFIX."_user (
					uid, 
					ugroup,
					uname,
					email,
					url,
					pass,
					user_birthday,
					user_icq,
					user_aim,
					user_yim,
					user_msnm,
					user_sig,
					user_interests,
					user_canpn,
					invisible,
					pntomail,
					user_regdate,
					title,
					name,
					lastname,
					street,
					zip,
					user_from,
					phone,
					fax,
					phone_mobile
					) VALUES (
					'',
					'".$_REQUEST['group']."',
					'".ltrim(rtrim($_REQUEST['uname']))."',
					'".$_REQUEST['email']."',
					'".$_REQUEST['url']."',
					'".md5($_REQUEST['pass'])."',
					'".$_REQUEST['dd'].".".$_REQUEST['mm'].".".$_REQUEST['yyyy']."',
					'".$_REQUEST['user_icq']."',
					'".$_REQUEST['user_aim']."',
					'".$_REQUEST['user_yim']."',
					'".$_REQUEST['user_msnm']."',
					'".$_REQUEST['user_sig']."',
					'".$_REQUEST['user_interests']."',
					'".$_REQUEST['user_canpn']."',
					'".$_REQUEST['invisible']."',
					'".$_REQUEST['pntomail']."',
					'".time()."',
					'".$_REQUEST['title']."',
					'".$_REQUEST['name']."',
					'".$_REQUEST['lastname']."',
					'".$_REQUEST['street']."',
					'".$_REQUEST['zip']."',
					'".$_REQUEST['user_from']."',
					'".$_REQUEST['phone']."',
					'".$_REQUEST['fax']."',
					'".$_REQUEST['phone_mobile']."'
					)";
					
					$sql = new dbc($q);
					
					$message = $langadmin['mailnewaccount'];
					$message = str_replace("##USERNAME##", ltrim(rtrim($_REQUEST['uname'])), $message);
					$message = str_replace("##PASSWORD##", $_REQUEST['pass'], $message);
					$message = str_replace("##HOMEPAGE##", "http://" . $_SERVER['HTTP_HOST'].str_replace("/admin/user.php","",$_SERVER['PHP_SELF']), $message);
					$message = str_replace("##HPNAME##", $pref['hpname'], $message);
					
					$body = str_replace("\n","\n\n", $message);
					
					@mail($_REQUEST['email'], $langadmin['subjectaccount'], stripslashes($body),
					"From: $pref[sendemail]\r\n"
					."Reply-To: $pref[sendemail]\r\n"
					."X-Mailer: PHP/" . phpversion());
			
					header("Location:user.php");
				}
			}
			
			$sqlg = new dbc("SELECT * FROM ".PREFIX."_usergroup  WHERE ugroup!='2'");
			$selugroup = array();
			while ($rowg = $sqlg->fetchrow()) {
				array_push($selugroup, $rowg);
			}
			
			$tmpl->assign("ok", $ok);
			$tmpl->assign("form", "user.php?action=new&send=1");
			$tmpl->assign("hiddenpage", $_REQUEST['page']);
			$tmpl->assign("errormsg", $errormsg);
			$tmpl->assign("row", $row);
			$tmpl->assign("rowg", $rowg);
			$tmpl->assign("birth", $birth);
			$tmpl->assign("pnin", $pnin);
			$tmpl->assign("pnout", $pnout);
			$tmpl->assign("selugroup", $selugroup);
			$tmpl->assign("onkeypress", "if(event.keyCode < 48 || event.keyCode > 57) { event.returnValue = false; } else {if(event.which < 48 || event.which > 57) return false;}");
			$tmpl->display("user_editnew.tpl");
		} 
		// =====================================
		// <<-- BENUTZER SUCHEN -->>
		// =====================================
		if ($_REQUEST['subaction'] == "searchuser") {
			$tmpl->assign("lang", $lang);
			$tmpl->assign("langadmin", $langadmin);
			
			$sqlg = new dbc("SELECT * FROM " . PREFIX . "_usergroup  WHERE ugroup!='2'");
			$groups = array();
			while ($rowg = $sqlg->fetchrow()) {
				if($_REQUEST['group'] == $rowg->ugroup){
					$rowg->opt_selected = "selected";
				}
				
				array_push($groups, $rowg);
			}
			
			$tmpl->assign("groups", $groups);
			$tmpl->assign("year", date("Y")+1);
			$tmpl->display("user_search.tpl");
		}
		
		
		
		
		// Verschiebung in andere Gruppe
		if($_REQUEST['action']=="moveto"){
			
			// Endgültig verschieben
			if($_REQUEST['subaction']=="movetofinal"){
				$g = $_REQUEST['group'];
				reset ($_POST);
				while(list($key,$val) = each($_POST)) {
					if (substr($key,0,4)=="usr_") {
						$aktid = str_replace("usr_","",$key);
						
						$sql = new dbc("UPDATE ".PREFIX."_user SET ugroup='".$g."' WHERE uid='".escs($aktid)."' AND uid!='".$UID."'");
						
					}
				}
				
				if((isset($_REQUEST['redir'])) && (isset($_REQUEST['smethod']))){
					$page = $_REQUEST['page']+1;
					header("Location:user.php?do=searchuser&ismore=1&smethod=1&redir=".$_REQUEST['redir']."&page=".$page."&typ=1");
				} else {
					header("Location:user.php");
				}
			}
			
			
			// Nachfrage
			$user = array();
			reset ($_POST);
			while(list($key,$val) = each($_POST)) {
				if (substr($key,0,5)=="user_") {
					$aktid = str_replace("user_","",$key);
					$sql =& new dbc("SELECT uid,uname FROM ".PREFIX."_user WHERE uid='".$aktid."'");
					$row=$sql->fetchrow();
					
					array_push($user, $row);
				}
			}
			
			if($_REQUEST['mode']==7){
				$aktid = str_replace("usr_","",$_REQUEST['utd']);
				$sql = new dbc("SELECT uid,uname FROM ".PREFIX."_user WHERE uid='".$aktid."'");
				$rowu = $sql->fetchrow();
				
			}
			
			$sqlg = new dbc("SELECT * FROM " . PREFIX . "_usergroup  WHERE ugroup!='2'");
			$groups = array();
			while ($rowg = $sqlg->fetchrow()) {
				array_push($groups, $rowg);
			}
			
			if(!isset($_REQUEST['page'])){
				$page = 1; 
			} else { 
				$page = $_REQUEST['page'];
			}
			
			$tmpl->assign("groups", $groups);
			$tmpl->assign("user", $user);
			$tmpl->assign("aktid", $aktid);
			$tmpl->assign("row", $row);
			$tmpl->assign("page", $page);
			$tmpl->assign("rowu", $rowu);
			$tmpl->assign("rowg", $rowg);
			$tmpl->display("user_movefinal.tpl");
		}
		
		// Aktivieren
		if($_REQUEST['action']=="activate"){
			
			// Endgültig aktivieren
			if($_REQUEST['subaction']=="activatefinal"){
				
				reset ($_POST);
				while(list($key,$val) = each($_POST)) {
					if (substr($key,0,4)=="usr_") {
						$aktid = str_replace("usr_","",$key);
						
						
						// Mail mit Aktivierungstext senden?
						if($_REQUEST['smail']==1){
							$sql = new dbc("SELECT email,uname FROM ".PREFIX."_user WHERE uid='".$aktid."'");
							$row = $sql->fetchrow();
							
							$message = kstripslashes($_REQUEST['emailtext']);
							$message = str_replace("##HOMEPAGE##","http://" . $_SERVER['HTTP_HOST'].str_replace("/admin/user.php","",$_SERVER['PHP_SELF']),$message);
							$message = str_replace("##USERNAME##",$row->uname,$message);
							$message = str_replace("##EMAIL##",$row->email,$message);
							$message = str_replace("##HPNAME##",$pref['hpname'],$message);
							
							$body = str_replace("\n","\n\n", $message);
							
							@mail($row->email, stripslashes($_REQUEST['subject']), stripslashes($body),
							"From: $pref[sendemail]\r\n"
							."Reply-To: $pref[sendemail]\r\n"
							."X-Mailer: PHP/" . phpversion());
							
						}
						
						// Endgültig aktivieren...
						$sql =& new dbc("UPDATE ".PREFIX."_user SET status='1' WHERE uid='".escs($aktid)."'");
					}
				}
				if((isset($_REQUEST['redir'])) && (isset($_REQUEST['smethod']))){
					$page = $_REQUEST['page']+1;
					header("Location:user.php?do=searchuser&ismore=1&smethod=1&redir=".$_REQUEST['redir']."&page=".$page."&typ=1");
				} else {
					header("Location:user.php");
				}
			}
			
			// Aktivierung-Nachfrage
			$user = array();
			reset ($_POST);
			while(list($key,$val) = each($_POST)) {
				if (substr($key,0,5)=="user_") {
					$aktid = str_replace("user_","",$key);
					$sql =& new dbc("SELECT uid,uname FROM ".PREFIX."_user WHERE uid='".$aktid."'");
					$row=$sql->fetchrow();
					
					array_push($user, $row);
				}
			}
			
			if($_REQUEST['mode']==7){
				$aktid = str_replace("usr_","",$_REQUEST['utd']);
				$sql = new dbc("SELECT uid,uname FROM ".PREFIX."_user WHERE uid='".$aktid."'");
				$rowu = $sql->fetchrow();
				
			}
			
			if(!isset($_REQUEST['page'])){
				$page = 1; 
			} else { 
				$page = $_REQUEST['page']; 
			}
			
			$tmpl->assign("groups", $groups);
			$tmpl->assign("user", $user);
			$tmpl->assign("aktid", $aktid);
			$tmpl->assign("row", $row);
			$tmpl->assign("page", $page);
			$tmpl->assign("rowu", $rowu);
			$tmpl->assign("rowg", $rowg);
			$tmpl->assign("textdelaccount", nl2br($langadmin['textdelaccount']));
			$tmpl->display("user_activate.tpl");
			
			
			
			
		}
		
		// Deaktivieren
		if($_REQUEST['action']=="deactivate"){
			reset ($_POST);
			while(list($key,$val) = each($_POST)) {
				if (substr($key,0,5)=="user_") {
					$aktid = str_replace("user_","",$key);
					$sql =& new dbc("UPDATE ".PREFIX."_user SET status='0' WHERE uid='".$aktid."'  AND uid!='".$UID."'");
				}
			}
			
			if($_REQUEST['mode']==7){
				$aktid = str_replace("usr_","",$_REQUEST['utd']);
				$sql =& new dbc("SELECT uname FROM ".PREFIX."_user WHERE uid='".$aktid."'");
				$row=$sql->fetchrow();
				$sql =& new dbc("UPDATE ".PREFIX."_user SET status='0' WHERE uid='".$aktid."'  AND uid!='".$UID."'");
			}
			header("Location:user.php");
			
		}
		
		// Löschen...
		if($_REQUEST['action']=="del"){
			
			// Bestätigung vor der Löschung...
			if($_REQUEST['subaction']=="delfinal"){
				reset ($_POST);
				while(list($key,$val) = each($_POST)) {
					if (substr($key,0,4)=="del_") {
						$aktid = str_replace("del_","",$key);
						
						// Mail mit Grund senden?
						if(!empty($_REQUEST['emailtext'])){
							$sql =& new dbc("SELECT email,uname FROM ".PREFIX."_user WHERE uid='".$aktid."'");
							$row = $sql->fetchrow();
							
							$message = kstripslashes($_REQUEST['emailtext']);
							$message = str_replace("##HOMEPAGE##","http://" . $_SERVER['HTTP_HOST'].str_replace("/admin/user.php","",$_SERVER['PHP_SELF']),$message);
							$message = str_replace("##USERNAME##",$row->uname,$message);
							$message = str_replace("##EMAIL##",$row->email,$message);
							$message = str_replace("##HPNAME##",$pref['hpname'],$message);
							
							$body = str_replace("\n","\n\n", $message);
							
							@mail($row->email, stripslashes($_REQUEST['subject']) , stripslashes($body),
							"From: $pref[sendemail]\r\n"
							."Reply-To: $pref[sendemail]\r\n"
							."X-Mailer: PHP/" . phpversion());
							
						}
						
						// Endgültig löschen...
						$sql_first = new dbc("SELECT uid FROM ".PREFIX."_user WHERE uname = 'Gast' and user_regdate < 2");
						$row_first = $sql_first->fetchrow();
						
						if($row_first->uid != ''){
							$sql_1 = new dbc("
								UPDATE 
								   ".PREFIX."_f_post 
								SET 
								   uid = '$row_first->uid'
								WHERE 
								   uid = '$aktid'
								");
								
							$sql_2 = new dbc("
								UPDATE 
								   ".PREFIX."_f_topic
								SET 
								   uid = '$row_first->uid'
								WHERE 
								   uid = '$aktid'
								");
								
						}
						
						$sql = new dbc("DELETE FROM ".PREFIX."_user WHERE uid='".escs($aktid)."' AND uid!='".escs($UID)."'");
						
					}
				}
				if((isset($_REQUEST['redir'])) && (isset($_REQUEST['smethod']))){
					$page = $_REQUEST['page']+1;
					header("Location:user.php?do=searchuser&ismore=1&smethod=1&redir=".$_REQUEST['redir']."&page=".$page."&typ=1");
				} else {
					header("Location:user.php");
				}
				
			}
			
			
			$user = array();
			reset ($_POST);
			while(list($key,$val) = each($_POST)) {
				if (substr($key,0,5)=="user_") {
					$aktid = str_replace("user_","",$key);
					$sql =& new dbc("SELECT uid,uname FROM ".PREFIX."_user WHERE uid='".$aktid."'");
					$row=$sql->fetchrow();
					
					array_push($user, $row);
				}
			}
			
			if($_REQUEST['mode']==7){
				$aktid = str_replace("usr_","",$_REQUEST['utd']);
				$sql = new dbc("SELECT uid,uname FROM ".PREFIX."_user WHERE uid='".$aktid."'");
				$rowu = $sql->fetchrow();
				
			}
			
			$sqlg = new dbc("SELECT * FROM " . PREFIX . "_usergroup  WHERE ugroup!='2'");
			$groups = array();
			while ($rowg = $sqlg->fetchrow()) {
				array_push($groups, $rowg);
			}
			
			if(!isset($_REQUEST['page'])){
				$page = 1; 
			} else { 
				$page = $_REQUEST['page'];
			}
			
			$tmpl->assign("groups", $groups);
			$tmpl->assign("user", $user);
			$tmpl->assign("aktid", $aktid);
			$tmpl->assign("row", $row);
			$tmpl->assign("page", $page);
			$tmpl->assign("rowu", $rowu);
			$tmpl->assign("rowg", $rowg);
			$tmpl->display("user_delete.tpl");
			
			
			
		}
	}
	
	
	
	
	
	if (!isset($_REQUEST['action'])) {
		if ($_REQUEST['do'] != "searchuser") {
			if ($_REQUEST['smethod'] != 1) {
				$sqlg =& new dbc("SELECT * FROM ".PREFIX."_usergroup  WHERE ugroup != '2'");
				$guests = array();
				while ($rowg = $sqlg->fetchrow()) {
					array_push($guests, $rowg);
				}
				$tmpl->assign("guests", $guests);
			}
		}
		
		if ($_REQUEST['smethod'] != 1) {
			if (isset($_REQUEST['group']) && $_REQUEST['group']!="") {
				$z1 = "AND (ugroup='".$_REQUEST['group']."')";
				if ( $_REQUEST['group'] == "all" ) {
					$z1 = "";
				}
			}
			
			if ($_REQUEST['ud'] == "ASC") {
				$ascdesc = "ASC";
				
			} else {
				$_REQUEST['ud'] = "DESC";
				$ascdesc = "DESC";
			}
			
			if ($_REQUEST['selby'] == "username") {
				$sortby = " ORDER BY uname";
			} elseif ($_REQUEST['selby'] == "posts") {
				$sortby = " ORDER BY user_posts";
			} elseif ($_REQUEST['selby'] == "joindate") {
				$sortby = " ORDER BY user_regdate";
			} elseif ($_REQUEST['selby'] == "user_lastonline") {
				$sortby = " ORDER BY user_lastonline";
			} else {
				$sortby = " ORDER BY user_regdate";
			}
			
			if (isset($_REQUEST['suname']) && $_REQUEST['suname']!="") {
				if ($_REQUEST['sc'] == "like") {
					$searchc = "%";
				}
				$seename = " AND uname like '" . $_REQUEST['suname'] . "$searchc'";
			}
			
			if ($_REQUEST['show'] == "notactive") {
				$showq = "AND (status!='1')";
				$seename = "";
				$z1 = "";
			}
			$spq = "(uid!='0' $seename)";
		}
		
		
		
		if ($_REQUEST['smethod'] == 1) {
			if ($_REQUEST['sc'] == "like") {
				$searchc = "%";
			}
			if ($_REQUEST['suname'] == "") {
				$searchc = "";
			}
			
			$spq = " (uname like '".$_REQUEST['suname']."$searchc')";
			$spg_1 = " OR (ugroup='".$_REQUEST['group']."')";
			$ascdesc = " ORDER by user_regdate DESC ";
			
			
			if ($_REQUEST['suname'] != "") {
				$spq = "";
				if ($_REQUEST['group'] != "all") {
					$spg_1 = "((uname like '".$_REQUEST['suname']."$searchc')) AND ((ugroup='".$_REQUEST['group']."'))";
				} else {
					$spg_1 = "(uname like '".$_REQUEST['suname']."$searchc')";
				}
			} else {
				$spg_1 = "";
			}
		}
		
		if ($_REQUEST['sumail'] != "" || $_REQUEST['sumail_parent'] != "") {
			unset($spq,$spg_1,$showq,$z1);
			$parent_mail = explode("@", $_REQUEST['sumail_parent']);
			$parent_mail = "%".$parent_mail[1]."%";
			
			$spq = " (email='".$_REQUEST['sumail']."') ";
			
			$lengthpmail = strlen($parent_mail);
			if (($parent_mail != "") && ($lengthpmail > 4)) {
				$spq .= " OR (email like '".$parent_mail."') ";
			}
		}
		if (strlen($_REQUEST['homepage']) > 4) {
			$spq .= " OR (url like '%".$_REQUEST['homepage']."%') ";
		}
		if (!empty($_REQUEST['user_icq'])) $spq .= " OR (user_icq='".$_REQUEST['user_icq']."') ";
		if (!empty($_REQUEST['user_aim'])) $spq .= " OR (user_aim='".$_REQUEST['user_aim']."') ";
		if (!empty($_REQUEST['user_yim'])) $spq .= " OR (user_yim='".$_REQUEST['user_yim']."') ";
		if (!empty($_REQUEST['user_msnm'])) $spq .= " OR (user_msnm='".$_REQUEST['user_msnm']."') ";
		if ((!empty($_REQUEST['before_month'])) && (!empty($_REQUEST['before_year'])) && (!empty($_REQUEST['before_day'])) && (!empty($_REQUEST['after_month'])) && (!empty($_REQUEST['after_year'])) && (!empty($_REQUEST['after_day']))) $spq .= " OR ((user_regdate<".mktime(0,0,0,$_REQUEST['before_month'],$_REQUEST['before_day'],$_REQUEST['before_year']).")) && ((user_regdate>".mktime(0,0,0,$_REQUEST['after_month'],$_REQUEST['after_day'],$_REQUEST['after_year']).")) ";
		if (!empty($_REQUEST['countb'])) $spq .= " OR (user_posts>".$_REQUEST['countb'].") ";
		if (!empty($_REQUEST['counts'])) $spq .= " OR (user_posts<".$_REQUEST['counts'].") ";
		if ((!empty($_REQUEST['pbefore_month'])) && (!empty($_REQUEST['pbefore_year'])) && (!empty($_REQUEST['pbefore_day']))) $spq .= " OR ((user_lastpost<".mktime(0,0,0,$_REQUEST['pbefore_month'],$_REQUEST['pbefore_day'],$_REQUEST['pbefore_year']).") && (user_lastpost>0)) ";
		if ((!empty($_REQUEST['pafter_month'])) && (!empty($_REQUEST['pafter_year'])) && (!empty($_REQUEST['pafter_day']))) $spq .= " OR ((user_lastpost>".mktime(0,0,0,$_REQUEST['pafter_month'],$_REQUEST['pafter_day'],$_REQUEST['pafter_year']).") && (user_lastpost>0)) ";
		if (!empty($_REQUEST['loggedfor']))  $spq .= " OR (user_lastonline<".mktime(0, 0, 0, date("m"), date ("d")-$_REQUEST['loggedfor'], date("Y")).") ";
		
		if (isset($_REQUEST['redir'])) {
			$spq = base64_decode($_REQUEST['redir']);
			$spq =  " (uname like '') OR (user_posts < 1) ";
		}
		if ($_REQUEST['typ'] == 1) {
			$limit = 15;
			$morelimit = 15;
			$morecron = 15;
		}
		
		$sql2 = new dbc("SELECT uid FROM ".PREFIX."_user WHERE $spq $spg_1 $showq $z1  AND user_regdate > 1 $sortby $ascdesc");
		$num = $sql2->numrows();
		$tmpl->assign("number", $num);
		
		$seiten = ceil($num / $limit);
		$a = prepage() * $limit - $limit;
		
		if ($_REQUEST['modedel'] == 1) {
			$a = 0;
		}
		
		$sql = new dbc("SELECT * FROM ".PREFIX."_user WHERE $spq $spg_1 $showq $z1 AND user_regdate > 1   $sortby  $ascdesc  limit  $a,$limit");
		$erg = $sql->numrows();
		
		
		
		if ((isset($_REQUEST['ismore'])) && ($erg < 1)) {
			echo messagenorm($langadmin['cronok'],"user.php");
			exit();
		}
		
		if ($erg < 1) {
			echo error($langadmin['norows']);
			exit();
		}
		
		$user = array();
		while ($row = $sql->fetchrow()) {
		
			$update = new dbc("UPDATE ".PREFIX."_user SET  user_posts='".numposts($row->uid)."' WHERE uid = '$row->uid'");
			
			$row->fgroupname = fetchgroupname($row->ugroup);
			$row->ulast = mygmt($row->user_lastonline);
			$row->regdate = mygmt($row->user_regdate);
			array_push($user, $row);
		}
		
		if ($crontext) {
			if($num > $morelimit){
				$crt = '
				<input name="limit" type="hidden" id="limit" value="'.$morelimit.'">
				<input name="typ" type="hidden" id="typ" value="1">
				';
			}
		}
		if ($morecron) { 
			$crontext = 1; 
			$crt = '
			<input name="redir" type="hidden" id="redir" value='.base64_encode($spq).'">								
			<input name="smethod" type="hidden" id="smethod" value="1">';
			$crtbutt = base64_encode($spq);
		}
		
		$tmpl->assign("crt", $crt);
		$tmpl->assign("user", $user);
		$tmpl->assign("smethod", $_REQUEST['smethod']);
		$tmpl->assign("tformat", $TFORMAT);
		
		if (($_REQUEST['smethod']!=13) && ($num>$limit)) {
			$tmpl->assign("navi", pagenav($seiten, $lang['aktpos'], " <a class=\"pnav\" href=\"user.php?
			group="	. $_REQUEST['group'] . "
			&ud=" . $_REQUEST['ud'] . "
			&selby=" . $_REQUEST['selby'] . "
			&show=" . $_REQUEST['show'] . "
			&countb=" . $_REQUEST["countb"] . "
			&sumail=" . $_REQUEST["sumail"] . "
			&sumail_parent=" . $_REQUEST["sumail_parent"] . "
			&homepage=" . $_REQUEST["homepage"] . "
			&user_icq=" . $_REQUEST["user_icq"] . "
			&user_aim=" . $_REQUEST["user_aim"] . "
			&user_yim=" . $_REQUEST["user_yim"] . "
			&user_msnm=" . $_REQUEST["user_msnm"] . "
			&after_day=" . $_REQUEST["after_day"] . "
			&after_month=" . $_REQUEST["after_month"] . "
			&after_year=" . $_REQUEST["after_year"] . "
			&before_day=" . $_REQUEST["before_day"] . "
			&before_month=" . $_REQUEST["before_month"] . "
			&before_year=" . $_REQUEST["before_year"] . "
			&counts=" . $_REQUEST["counts"] . "
			&pbefore_day=" . $_REQUEST["pbefore_day"] . "
			&pbefore_month=" . $_REQUEST["pbefore_month"] . "
			&pbefore_year=" . $_REQUEST["pbefore_year"] . "
			&pafter_day=" . $_REQUEST["pafter_day"] . "
			&pafter_month=" . $_REQUEST["pafter_month"] . "
			&pafter_year=" . $_REQUEST["pafter_year"] . "
			&loggedfor=" . $_REQUEST["loggedfor"] . "
			&smethod=" . $_REQUEST["smethod"] . "
			&pp=$limit
			&page={s}\">{t}</a> "));
		}
		
		$tmpl->display("user_overview.tpl");
	}
}
?>
