<?php
// ======================================================================== //
//                                                                          //
//   KooBI- Webportalsystem                                                 //
//   SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS                     //
//                                                                          //
//   ====================================================================   //
//                                                                          //   
//   dream4 | webservices - Am Michelsgrund 7 - 69469 Weinheim - Germany    //    
//   tel. +49(0) 6201 - 980442 / fax. +49(0) 6201 - 64809                   //
//                                                                          //
// ======================================================================== //
if(!checkactive('pn')) include(dirname(__FILE__)."/lib/notactive.php" );
$boxtype = "pagecomments";
$sname = simpletitle($lang['personalmessage']);
$ok = 1;
$pnin = 1;

if(isset($_REQUEST['pp']) && $_REQUEST['pp']!=""){
	$limit = $_REQUEST['pp'];
	} else {
	$limit = 10;
}

if((!permission("canpn")) || ($MAXPN==0)){
	$nometa = 1;
	$EOUT = stderror("nopermission", $lang['nopermission_t'], str_replace("__URL__", "index.php", $lang['redirect']), "index.php", 1);
	$error = 1;
	$NOOUT = 1;
	
	} else {
	
	// PN LÖSCHEN
	if(isset($_REQUEST['del']) && $_REQUEST['del']!="") {
		reset ($_POST);
		while (list($key, $val) = each($_POST)) {
			if (substr($key,0,3)=="pn_") {
				$aktid = str_replace("pn_","",$key);
				$sql =& new dbc("DELETE FROM ".PREFIX."_pn WHERE pnid='".$aktid."'");
			}
		}
		header("Location:index.php?p=pn&goto=".$_REQUEST['goto']."&area=".$_REQUEST['area']."");
	}
	
	if(!isset($_REQUEST['action']) && $_REQUEST['action']==""){
		
		$goto = $_REQUEST['goto']=="outbox" ? "outbox" : "inbox";
		$tofrom = $goto=="inbox" ? "to_uid" : "from_uid";
		
		$send_recieve_text = $goto=="inbox" ? $lang['recieve_dt'] : $lang['send_dt'];
		$text_fromto = $goto=="inbox" ? $lang['from_t'] : $lang['to_t'];
		
		
		// Aufsteigende & Absteigende Sortierung
		if(!isset($_REQUEST['sort']) && $_REQUEST['sort']==""){
			$sort = "DESC";
		} else {
			$sort = $_REQUEST['sort'];
			if(($sort != "ASC") && ($sort != "DESC")) $sort = "DESC";
		}
		
		
		// Sortierung nach Betreff, Autor, Gelesen und Ungelesen
		if(!isset($_REQUEST['porder']) && $_REQUEST['porder']==""){
			$porder = "pntime";
		} else {
			$porder = $_REQUEST['porder'];
			if(($porder != "pntime") && ($porder != "topic") && ($porder != "uid") && ($porder != "readed") && ($porder != "notreaded")) $porder = "pntime";
		}
		
		if(($goto=="inbox") && ($porder=="uid")) {
			$porder = "from_uid";
			$inbox_uid = " selected";
		}
		if(($goto=="outbox") && ($porder=="uid")) {
			$porder = "to_uid";
			$outbox_uid = " selected";
		}
		
		if($porder=="pntime"){
			$porder = "pntime";
			$pntime_sel = " selected";
		}
		
		if($porder=="topic"){
			$porder = "topic";
			$topic_sel = " selected";
		}
		
		if($porder=="readed"){
			$porder = "is_readed='yes'";
			$readed_sel = " selected";
		}
		
		if($porder=="notreaded"){
			$porder = "is_readed='no'";
			$notreaded_sel = " selected";
		}
		
		// Sortierungsoptionen dem Template zuweisen...
		$sel_topic_read_unread	 = '<option value="pntime" ' . $pntime_sel . '>'.$lang['bydate'].'</option>';
		$sel_topic_read_unread	.= '<option value="topic" ' . $topic_sel . '>'.$lang['bytopic'].'</option>';
		$sel_topic_read_unread	.= '<option value="uid" ' . $outbox_uid . $inbox_uid . '>'.$lang['byauthor'].'</option>';
		$sel_topic_read_unread	.= '<option value="readed" ' . $readed_sel . ' >'.$lang['byreaded'].'</option>';
		$sel_topic_read_unread	.= '<option value="notreaded" ' . $notreaded_sel . '>'.$lang['byunreaded'].'</option>';
		
		$tmpl->assign('sel_topic_read_unread', $sel_topic_read_unread) ;
		
		$sql =& new dbc("SELECT * FROM ".PREFIX."_pn WHERE $tofrom='".$UID."' AND typ='".$goto."'  ORDER BY $porder $sort");
		$pnin = $sql->numrows();
		
		$seiten = ceil($pnin / $limit);
		$a = prepage() * $limit - $limit;
		
		$sql =& new dbc("SELECT * FROM ".PREFIX."_pn WHERE $tofrom='".$UID."' AND typ='".$goto."'  ORDER BY $porder $sort limit  $a,$limit");
		
		
		// Normale oder Katalogisierte Ansicht...
		if(($_COOKIE['listpn']=="katalog" ) || (!$_COOKIE['listpn'])){
			$LISTPN = 1;
			setcookie("listpn", "katalog", time()+(365*24*3600));
		}
		
		if(isset($_REQUEST['switchto']) && $_REQUEST['switchto']!=""){
			if($_REQUEST['switchto'] == "katalog"){
				$LISTPN = 1;
				setcookie("listpn", "katalog", time()+(365*24*3600));
				$switchto = "katalog";
			}
			if(isset($_REQUEST['switchto']) && $_REQUEST['switchto']=="norm"){
				$LISTPN = -1;
				setcookie("listpn", "norm", time()+(365*24*3600));
				$switchto = "norm";
			}
		}
		
		
		$entry_array = array();
		$table_data = array();
		if($LISTPN==1){
			while ($row = $sql->fetchrow()) {
				$sql2 =& new dbc("SELECT uname,uid FROM ".PREFIX."_user WHERE uid='".$row->from_uid."' ");
				$row2 = $sql2->fetchrow();
				$sql2->close();
				
				if($goto=="inbox"){
					$theuserid = $row2->uid;
					$theusername = $row2->uname;
				}else{
					$sql_emp =& new dbc("SELECT uname,uid FROM ".PREFIX."_user WHERE uid='".$row->to_uid."' ");
					$row_emp = $sql_emp->fetchrow();
					$theuserid = $row->to_uid;
					$theusername = $row_emp->uname;
				}
				
				$entry_array[] = array(
				'timestamp' => $row->pntime,
				'data'      => array(
				'title'	=> stripslashes($row->topic),
				'pntime' => mygmt($row->pntime),
				'pnday' => mygmt($row->pntime),
				'von'	=> $theusername,
				'goto'	=> $goto,
				'pnid'	=> $row->pnid,
				'icon' => isreaded($row->pnid),
				'toid' => "index.php?p=user&amp;id=".$theuserid."&amp;area=".$area."",
				'mlink'	=> "index.php?p=pn&amp;action=message&amp;id=".$row->pnid."&amp;goto=".$goto."&amp;area=".$area.""
				)
				);
			}
			
			$last = 0;
			
			$ts = array();
			$ts[0] = array(
			'anfang' => mktime(0,0,0,date("m"),date("d")+MYGMT,date("Y")),
			'ende'	 => mktime(23,59,59,date("m"),date("d")+MYGMT,date("Y"))
			);
			
			$last = mktime(0,0,0,date("m"),date("d")+MYGMT,date("Y"));
			$wochentag = date("w") + 1;
			
			for ($i = 1; $i < $wochentag; $i++) {
				$a = $wochentag - $i;
				if (date("d")-$i > 0) {
					$last -= 86400;
					$ts[$a] = array(
					'anfang' => $last,
					'ende'	 => $last+86399
					);
				}
			}
			
			$ts[-2] = array(
			'anfang'	=> $last-(7 * 86400),
			'ende'		=> $last
			);
			
			$last -= 7 * 86400;
			
			$ts[-1] = array(
			'anfang'	=> 0,
			'ende'		=> $last
			);
			
			
			
			$wochentage = explode(",",$lang['days_name']);
			
			while(list($key,$val)=each($ts)) {
				if ($key==0) {
					$t = $lang['pn_today'];
					$d = ", " . date("d.m.Y", $val['anfang']);
				} elseif ($key==-1) {
					$t = $lang['later'];
					$d = "";
				} elseif ($key==-2) {
					$t = $lang['lastweek'];
					$d = "";
				} else {
					$t = $wochentage[$key-1];
					$d = ", " . date("d.m.Y", $val['anfang']);
				}
				
				$mys = 0;
				
				reset($entry_array);
				while(list($k,$v) = each($entry_array)) {
					if($v['timestamp'] > $val['anfang'] && $v['timestamp'] < $val['ende']) {
						$mys++;
					}
				}
				
				if($mys > 0) {
					$a = 0;
					reset($entry_array);
					while(list($k,$v) = each($entry_array)) {
						if($v['timestamp'] > $val['anfang'] && $v['timestamp'] < $val['ende']) {
							$a++;
							if($a==1) {
								
								unset($position,$plusminus,$disp);
								$position = strpos($_COOKIE["pn"], "pn" . $goto.$key);
								
								if ( is_numeric($position) ) {
									$disp= "none";
									$plusminus = "plus.gif";
								} else {
									$plusminus = "minus.gif";
								}
								$toggle = "<img hspace=\"2\" class=\"absmiddle\" border=\"0\" id=\"pn_".$goto.$key."\" src=\"templates/".$THEME."/img/page/".$plusminus."\" 
								onmouseover=\"this.style.cursor = 'pointer'\"
								onclick=\"MWJ_changeDisplay('pn".$goto.$key."', MWJ_getStyle( 'pn".$goto.$key."', 'display' ) ? '' : 'none');
								koobi4_toggleImage('pn_".$goto.$key."', this.src);
								koobi4_setCookie('pn', 'pn".$goto.$key."');\"
								alt=\"\" />";
								
								$v['data']['tbody_start'] = "<tr><td class=\"toggletr\" colspan=\"4\">".$toggle.$t.$d."</td></tr><tbody id='pn".$goto.$key."' name='pn".$goto.$key."' style=\"display: ".$disp.";\">";
							}
							if($a==$mys) {
								$v['data']['tbody_end'] = "</tbody>";
							}
							
							array_push($table_data, $v['data']);
							
						}
					}
				}
				
			}
			
		} else {
			while ($row = $sql->fetchrow()) {
				$sql2 =& new dbc("SELECT uname,uid FROM ".PREFIX."_user WHERE uid='".$row->from_uid."' ");
				$row2 = $sql2->fetchrow();
				$sql2->close();
				
				if ($goto=="inbox") {
					$theuserid = $row2->uid;
					$theusername = $row2->uname;
				} else {
					$sql_emp =& new dbc("SELECT uname,uid FROM ".PREFIX."_user WHERE uid='".$row->to_uid."' ");
					$row_emp = $sql_emp->fetchrow();
					$theuserid = $row->to_uid;
					$theusername = $row_emp->uname;
				}
				array_push($table_data, array(
				'timestamp' => $row->pntime,
				'title'	=> stripslashes($row->topic),
				'pntime' => mygmt($row->pntime),
				'pnday' => mygmt($row->pntime),
				'von'	=> $theusername,
				'pnid'	=> $row->pnid,
				'goto'	=> $goto,
				'icon' => isreaded($row->pnid),
				'toid' => "index.php?p=user&amp;id=".$theuserid."&area=".$area."",
				'mlink'	=> "index.php?p=pn&amp;action=message&amp;id=".$row->pnid."&amp;goto=".$goto."&area=".$area.""
				)
				);
			}
		}
		
		
		
		
		
		
		// Selektion für gelesene und ungelesene Nachrichten wieder ändern...
		if (isset($_REQUEST['porder']) && $_REQUEST['porder']=="readed") {
			$porder = "readed";
		}
		if (isset($_REQUEST['porder']) && $_REQUEST['porder']=="notreaded") {
			$porder = "notreaded";
		}
		if (isset($_REQUEST['porder']) && $_REQUEST['porder']=="uid") {
			$porder = "uid";
		}
		
		// Navigation erzeugen
		if($pnin > $limit){
			$nav = pagenav($seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?p=pn&goto=".$goto."&sort=".$sort."&porder=".$porder."&pp=".$limit."&page={s}&switchto=".$switchto."&area=".$area."\">{t}</a> ");
			$tmpl->assign("nav", $nav) ;
		}
		
		// Anzahlwahl + Auf oder Absteigend
		for ($i = 10; $i <= 50; $i+=10) {
			unset($thisselect);
			if (isset($_REQUEST['pp']) && $_REQUEST['pp']==$i) {
				$thisselect = "selected";
			}
			$pp_l .= '<option value="'.$i.'" '.$thisselect.'>'.$i.' '.$lang['eachpage'].'</option>';
		}
		$tmpl->assign('pp_l', $pp_l) ;
		
		if(!$page){
			$page = 1;
		} else {
			$page = $_REQUEST['page'];
		}
		if(isset($_REQUEST['sort']) && $_REQUEST['sort']=="DESC"){ // ABSTEIGEND
			$tmpl->assign('sel1', "selected");
			$tmpl->assign('sel2', "");
		}
		if(isset($_REQUEST['sort']) && $_REQUEST['sort']=="ASC"){ // AUFSTEIGEND
			$tmpl->assign('sel2', "selected");
			$tmpl->assign('sel1', "");
		}
		$tmpl->assign('page', $page);
		
		
		// Normale Ansicht & Katalogisierte Ansicht
		$link_the_normmodus = "index.php?p=pn&goto=".$goto."&sort=".$sort."&porder=".$porder."&pp=".$limit."&page=".$page."&switchto=norm&area=".$area."";
		$link_the_katmodus = "index.php?p=pn&goto=".$goto."&sort=".$sort."&porder=".$porder."&pp=".$limit."&page=".$page."&switchto=katalog&area=".$area."";
		$tmpl->assign('normmodus_link', $link_the_normmodus) ;
		$tmpl->assign('katmodus_link', $link_the_katmodus) ;
		$tmpl->assign('lang', $lang);
		
		
		// PROZENT EINGANG / AUSGANG
		$onepn = 100 / $MAXPN;
		$allpn = $onepn * $pnin;
		$inoutwidth = round($allpn/1.005, 3);
		$inoutpercent = round($allpn, 0);
		if($pnin==$MAXPN){$warningpnfull = $lang['warningpnfull'];}
		
		if($goto=="inbox") {
			$tmpl->assign('selin', "selected");
			$tmpl->assign("view", "inbox");
		}
		
		if($goto=="outbox") {
			$tmpl->assign('selout', "selected");
			$tmpl->assign("view", "outbox");
		}
	}
	
	if(!isset($_REQUEST['action']) && $_REQUEST['action']==""){
		$desclink = "index.php?p=pn&goto=".$goto."&sort=DESC&pp=".$_REQUEST['pp']."&page=".prepage()."&area=".$area."";
		$asclink = "index.php?p=pn&goto=".$goto."&sort=ASC&pp=".$_REQUEST['pp']."&page=".prepage()."&area=".$area."";
		
		
		
		$tmpl->assign('send_recieve_text', $send_recieve_text);
		$tmpl->assign('title_t', $lang['title_t']);
		$tmpl->assign('from_t', $text_fromto);
		$tmpl->assign('action', $lang['action']);
		$tmpl->assign('title', $lang['personalmessage']);
		$tmpl->assign('content', $content);
		$tmpl->assign('delmarked', $lang['delmarked']);
		$tmpl->assign('goto', $goto);
		$tmpl->assign('pndel_confirm', $lang['pndel_confirm']);
		$tmpl->assign('inoutwidth', $inoutwidth);
		$tmpl->assign('inoutpercent', $inoutpercent);
		$tmpl->assign('pnioutnall', $pnin);
		$tmpl->assign('pnmax', str_replace("__MAXPN__",$MAXPN,$lang['pninoutstatus']));
		$tmpl->assign('warningpnfull', $warningpnfull);
		$tmpl->assign('sortdesc', $desclink);
		$tmpl->assign('sortasc', $asclink);
		$tmpl->assign('dlpnas', $lang['dlpn']);
		$tmpl->assign('pndl_text', "index.php?p=pn&goto=".$goto."&download=1&type=text&area=".$area."");
		$tmpl->assign('pndl_html', "index.php?p=pn&goto=".$goto."&download=1&type=html&area=".$area."");
		$tmpl->assign('pndl_text_link', $lang['dlpn_text']);
		$tmpl->assign('pndl_html_link', $lang['dlpn_html']);
		
		// Subtemplate
		$tmpl->assign('outin', 1);
		$tmpl->assign('neu', 0);
		
		// Wenn keine PN...
		if($pnin) {
			$tmpl->assign('table_data', $table_data);
		} else {
			$tmpl->assign('nopns', $lang['nopns']);
			$tmpl->assign('nomessages', 1);
			$tmpl->assign('outin', 0);
		}
		
		// PN herunterladen
		if(isset($_REQUEST['download']) && $_REQUEST['download']==1){
			$sql =& new dbc("SELECT * FROM ".PREFIX."_pn WHERE $tofrom='".$UID."' AND typ='".$goto."'  ORDER BY pntime ".$sort."");
			while ($row = $sql->fetchrow()) {
				$message = str_replace("\n", "\r\n", str_replace("\r\n", "\n", $row->message));
				$message = kstripslashes($message);
				
				if($goto=="inbox"){
					$pninout = $lang['inbox'];
					$fname = $theusername;
					$tname = $UNAME;
				}else{
					$pninout = $lang['outbox'];
					$fname = $UNAME;
					$tname = $theusername;
				}
				
				if($_REQUEST['type']=="text") {
					
					$dlmessage .= "===============================================================================\r\n";
					$dlmessage .= "".$lang['from_t'].":\t" . $fname ."\r\n";
					$dlmessage .= "".$lang['to_t'].":\t" . $tname . "\r\n";
					$dlmessage .= "".$lang['date'].":\t" . date('d-m-Y H:i', $row->pntime) . "\r\n";
					$dlmessage .= "".$lang['topic_t'].":\t" . $row->topic . "\r\n";
					$dlmessage .= "-------------------------------------------------------------------------------\r\n";
					$dlmessage .=  wordwrap( $message, 90, "\r\n", 1) . "\r\n\r\n";
					$end = ".txt";
					
				} else {
					
					$dlmessage .= '<style><!-- TD{FONT-SIZE: 11px;FONT-FAMILY:   Verdana, Arial, Helvetica, sans-serif;} --></style>';
					$dlmessage .= '<table width="100%"  border="1" cellpadding="3" cellspacing="0" bordercolor="#333333"><tr><td bgcolor="#F4F4F4">';
					$dlmessage .= "<B>".$lang['from_t']."</B>:\t" . $fname ."\r<br>";
					$dlmessage .= "<B>".$lang['to_t']."</B>:\t" . $tname . "\r<br>";
					$dlmessage .= "<B>".$lang['date']."</B>:\t" . date('d-m-Y H:i', $row->pntime) . "\r<br>";
					$dlmessage .= "<B>".$lang['topic_t']."</B>:\t" . $row->topic . "\r<br>";
					$dlmessage .= '</td></tr><tr><td>';
					$dlmessage .=  nl2br(kspecialchars(wordwrap( $message, 90, "\r\n", 1))) ."";
					$dlmessage .= '</td></tr></table><br>';
					$end = ".htm";
				}
			}
			downloadfile($dlmessage, $pninout ."-". $lang['personalmessage'].date("d-m-Y") . $end, 'text/plain');
		}
		
	}
	
	// NACHRICHT ANSEHEN
	if(isset($_REQUEST['action']) && $_REQUEST['action']=="message"){
		
		$goto = $_REQUEST['goto']=="inbox" ? "inbox" : "outbox";
		$tofrom = $goto=="inbox" ? "to_uid" : "from_uid";
		$sql =& new dbc("SELECT * FROM ".PREFIX."_pn WHERE pnid='".$_REQUEST['id']."' AND $tofrom='".$UID."' AND typ='".$goto."'");
		$num = $sql->numrows();
		$row = $sql->fetchrow();
		$sql->close();
		
		// WENN UNGÜLTIGE ID ODER PN NICHT == USER-ID
		if(!$num){
			$q = "SELECT * FROM ".PREFIX."_pn WHERE pnid='".$_REQUEST['id']."' AND $tofrom='".$UID."' AND typ='".$goto."'";
			$tmpl->assign('showmessage', 0);
			
			$EOUT = msg("error_once", "error_wrongpnid", str_replace("__URL__", "index.php?p=pn&goto=".$_REQUEST['goto']."", $lang['redirect']), "index.php?p=pn&goto=".$_REQUEST['goto']."");
			$NOOUT = 1;
			
			$nometa = 1;
			$ok = 0;
		}
		
		if($ok==1):
		$pn_id = $_REQUEST['id'];
		if(isset($_REQUEST['do']) && $_REQUEST['do']=="del"){
			$sql =& new dbc("DELETE FROM ".PREFIX."_pn WHERE pnid='".$pn_id."'");
			header("Location:index.php?p=pn&goto=".$_REQUEST['goto']."&area=".$_REQUEST['area']."");
			
		}
		
		if($goto == "inbox"){
			$sql =& new dbc("UPDATE ".PREFIX."_pn SET is_readed='yes' WHERE pnid='".$_REQUEST['id']."'");
			
			$sql =& new dbc("SELECT pntime,topic FROM ".PREFIX."_pn WHERE pnid='".$_REQUEST['id']."'");
			$row_subid = $sql->fetchrow();
			
			$sql =& new dbc("UPDATE ".PREFIX."_pn SET is_readed='yes' WHERE pntime='".$row_subid->pntime."' AND topic='".$row_subid->topic."'");
			
		}
		
		if($_POST['goto'] == "inbox"){
			$sqlid = $row->from_uid;
			$tfrlink = "index.php?p=user&amp;id=".$UID."&area=".$area."";
		}elseif($_POST['goto'] == "outbox"){
			$sqlid = $row->to_uid;
			$tfrlink = "index.php?p=user&amp;id=".$row_u->uid."&area=".$area."";
		}else{
			$sqlid = $row->from_uid;
		}
		
		if(!isset($_REQUEST['goto']) && ($_REQUEST['goto']=="")):
		$goto = "inbox";
		else: $goto = $_REQUEST['goto']; endif;
		
		$text_fromto = $goto=="inbox" ? $lang['from_t'] : $lang['to_t'];
		
		$message = $row->message;
		
		
		
		if($row->smilies=="yes") {
			$tmpl->assign('message', dosmilies(kcodes($message)));
		} else {
			$tmpl->assign('message', kcodes($message));
		}
		
		
		
		$sql =& new dbc("SELECT uid,uname,user_regdate,user_posts,ugroup FROM ".PREFIX."_user WHERE uid='".$sqlid."'");
		$row_u = $sql->fetchrow();
		$sql->close();
		
		$sql =& new dbc("SELECT * FROM ".PREFIX."_user LEFT JOIN ".PREFIX."_usergroup USING (ugroup) WHERE (uid='".$row_u->uid."') LIMIT 1");
		$row_u2 = $sql->fetchrow();
		$sql->close();
		
		
		$pnlink = "index.php?p=pn&action=message&id=".$pn_id."&goto=".$goto."&do=del&area=".$area."&area=".$area."";
		$forwardlink = "index.php?p=pn&action=new&forward=1&id=".$pn_id."&subject=".base64_encode($row->topic)."&aut=".base64_encode($row_u->uname)."&date=".base64_encode(mygmt($row->pntime))."&area=".$area."";
		$relink = "index.php?p=pn&action=new&forward=2&id=".$pn_id."&subject=".base64_encode($row->topic)."&aut=".base64_encode($row_u->uname)."&date=".base64_encode(mygmt($row->pntime))."&area=".$area."";
		
		if($_POST['goto'] == "inbox"){
			$tfrlink = "index.php?p=user&amp;id=".$row_u->uid."";
			$group = fetchgroupname($row_u->ugroup);
		} else {
			$tfrlink = "index.php?p=user&amp;id=".$row_u->uid."";
			$group = fetchgroupname($row_u2->ugroup);
		}
		
		if($goto=="inbox") {
			$tmpl->assign('answerok', 1);
		}
		
		$tmpl->assign('delpn', $pnlink);
		$tmpl->assign('forwardlink', $forwardlink);
		$tmpl->assign('relink', $relink);
		$tmpl->assign('delpn_t', $lang['confirmdelpn']);
		$tmpl->assign('membersince_date', mygmt($row_u->user_regdate));
		$tmpl->assign('membersince', $lang['membersince']);
		$tmpl->assign('posts_num', $row_u->user_posts);
		$tmpl->assign('groupname', $group);
		$tmpl->assign('posts', $lang['posts']);
		$tmpl->assign('group_t', $lang['group_t']);
		$tmpl->assign('pntitle', oform($row->topic));
		$tmpl->assign('send_dt', $lang['send_dt']);
		$tmpl->assign('pntime',mygmt($row->pntime));
		$tmpl->assign('pn_t_once',$lang['pn_t_once']);
		$tmpl->assign('tofromname', $row_u->uname);
		$tmpl->assign('tofromname_link', $tfrlink);
		$tmpl->assign('sendtime_t', $lang['sendtime_t']);
		$tmpl->assign('pndate', mygmt($row->pntime));
		$tmpl->assign('message_t', $lang['message_t']);
		$tmpl->assign('topic_t', $lang['topic_t']);
		$tmpl->assign('to_t', $text_fromto);
		$tmpl->assign('showmessage', 1);
		endif;
	}
	
	// NEUE NACHRICHT
	if(isset($_REQUEST['action']) && $_REQUEST['action'] == "new") {
		$sql =& new dbc("SELECT typ FROM ".PREFIX."_pn WHERE typ='outbox' and from_uid='".$UID."'");
		$num = $sql->numrows();
		
		
		if($num == $MAXPN || $num >= $MAXPN){
			$EOUT = msg("error_once", "newpn_full", str_replace("__URL__", "index.php?p=pn&goto=inbox&area=".$area."", $lang['redirect']), "index.php?p=pn&goto=inbox&area=".$area."");
			$NOOUT = 1;
			$nometa = 1;
			$sname = $lang['personalmessage'];
		$ok = 0;}
		
		
		// Vorschau
		if(isset($_POST['send']) && $_POST['send']=="1"){
			
			
			$message = $_POST['text'];
			
			if($_POST['parseurl']=="yes") {
				$message = parseurl($message);
			}
			
			if($_POST['use_smilies'] == "yes") {
				$tmpl->assign('preview_text', dosmilies(kcodes($message)));
			} else {
				$tmpl->assign('preview_text', kcodes($message));
			}
			
			$text = "";
			$tmpl->assign('tofromname', oform($_POST['tofromname']));
			$tmpl->assign('title', oform($_POST['title']));
			$tmpl->assign('text', kspecialchars($_POST['text']));
			$tmpl->assign('preview', 1);
			
		}
		
		include(BASEDIR."/inc/functions_post_array.php");
		if(isset($_POST['send']) && $_POST['send']=="2"){
			
			$error = "";
			
			if(empty($_POST['tofromname'])): $pnerror .= emsg("error_norecipient"); endif;
			if(empty($_POST['title'])): $pnerror .= emsg("error_notopic"); endif;
			if(empty($_POST['text'])): $pnerror .= emsg("error_notext"); endif;
			if(strlen($_POST['text']) > $MAXPNLENTH): $pnerror .= emsg("error_tolong"); endif;
			
			$tmpl->assign('tofromname', oform($_POST['tofromname']));
			$tmpl->assign('title', oform($_POST['title']));
			$tmpl->assign('text', oform($_POST['text']));
			
			
			
			
			// CHECK OB USER EXISTIERT
			$sql =& new dbc("SELECT uname,uid,user_canpn FROM ".PREFIX."_user WHERE uname='".oform($_POST['tofromname'])."'");
			$num = $sql->numrows();
			$row = $sql->fetchrow();
			
			if(!$pnerror)if(!$num)$pnerror .= emsg("error_usernotindb");
			if(!$pnerror)if($_POST['tofromname']==$UNAME) $pnerror .= emsg("error_usersameasuname");
			
			
			// CHECK OB ABSENDER IN IGNORIERLISTE DES EMPFÄNGERS STEHT
			$sql_ignore =& new dbc("SELECT ignore_uid,uid FROM ".PREFIX."_ignore WHERE uid='".$row->uid."' AND ignore_uid='".$UID."' ");
			$num_ignore = $sql_ignore->numrows();
			if(!$pnerror)if($num_ignore)$pnerror .= emsg("error_userignore");
			
			
			// CHECK OB NACHRICHTENBOX VOLL IST
			if(!$pnerror){
				$sql2 =& new dbc("SELECT * FROM ".PREFIX."_user LEFT JOIN ".PREFIX."_usergroup USING (ugroup) WHERE ( uid='".$row->uid."') LIMIT 1");
				$row2 = $sql2->fetchrow();
				$USERARRAY2 = $row2;
				$usermaxpn = $row2->groupmaxpn;
				
				$sql3 =& new dbc("SELECT * FROM ".PREFIX."_pn WHERE to_uid='".$row->uid."' AND typ='inbox'");
				$numuserpn = $sql3->numrows();
				
				if($numuserpn >= $usermaxpn): $pnerror .= emsg("error_messageboxfull"); endif;
			}
			
			// CHECK OB USER PN'S EMPFANGEN MÖCHTE
			if(!$pnerror){
				if($row->user_canpn=="no"): $pnerror .= emsg("error_userwantsnopn"); endif;
			}
			
			// CHECK OB USER DIE GRUPPENRECHTE BESITZT, PN'S ZU EMPFANGEN
			if(!$pnerror){
				$USERRIGHTS2 = explode("|", $USERARRAY2->groupperms);
				foreach ( $USERRIGHTS2  AS $VAL2 ) $PERMS2[$VAL2]=1;
				if(!permission("canpn")): $pnerror .= emsg("error_userpnnorights"); endif;
			}
			
			
			$text = phpdecode(substr($_POST['text'],0, $MAXPNLENTH));
			if($_POST['parseurl']=="yes") {
				$text = parseurl($text);
			}
			
			if(!$pnerror){
				$sql =& new dbc("INSERT INTO ".PREFIX."_pn (smilies,pnid,to_uid,from_uid,topic,message,is_readed,pntime,typ) VALUES ('".$_POST['use_smilies']."','','".$row->uid."','".$UID."','".iform($_POST['title'],"255","")."','".$text."','no','".time()."','inbox')");
				
				if(isset($_REQUEST['savecopy']) && $_REQUEST['savecopy']=="yes"){
					$sql =& new dbc("INSERT INTO ".PREFIX."_pn (smilies,pnid,to_uid,from_uid,topic,message,is_readed,pntime,typ) VALUES ('".$_POST['use_smilies']."','','".$row->uid."','".$UID."','".iform($_POST['title'],"255","")."','".$text."','no','".time()."','outbox')");
				}
				
				// MAILBENACHRICHTIGUNG
				if($USERARRAY2->pntomail=="yes"){
					$body = $lang['new_pn_emailbody'];
					$body = str_replace("__USER__", $USERARRAY2->uname, $body);
					$body = str_replace("__AUTOR__", UNAME, $body);
					$body = str_replace("__LINK__", "http://" . $_SERVER['HTTP_HOST'].str_replace("/index.php","",$_SERVER['PHP_SELF'])."/index.php?p=pn&goto=inbox", $body);
					$body = str_replace("\n","\n\n", $body);
					
					@mail($USERARRAY2->email, $lang['subjectnewpnemail'], stripslashes($body),
					"From: $pref[sendemail]\r\n"
					."Reply-To: $pref[sendemail]\r\n"
					."X-Mailer: PHP/" . phpversion());
					
				}
				
				
				$EOUT = msg("personalmessage", "personalmessage_ok_t", str_replace("__URL__", "index.php?p=pn&goto=outbox", $lang['redirect']), "index.php?p=pn&goto=outbox");
				$nometa = 1;
				$ok = 0;
				$sname = $lang['personalmessage'];
			}
		}
		
		
		if($pnerror) {
			$tmpl->assign('title_error', $lang['error']);
			$tmpl->assign('iserror', 1);
			$tmpl->assign('error', $pnerror);
		}
		
		
		
		$tmpl->assign('listfonts',  fontdropdown());
		$tmpl->assign('sizedropdown',  sizedropdown());
		$tmpl->assign('colordropdown',  colordropdown());
		$tmpl->assign('max_post_length', $MAXPNLENTH);
		$tmpl->assign('listemos', listsmilies());
		$tmpl->assign('area', $area);
		$tmpl->assign('newpn_t', str_replace("__ZEICHEN__", $MAXPNLENTH ,$lang['newpn_t']));
		$tmpl->assign('newpn_error', $pnerror);
		$tmpl->assign('lang', $lang);
		
		if(isset($_REQUEST['to']) && $_REQUEST['to']!=""){
			$tmpl->assign('tofromname', base64_decode($_REQUEST['to']));
		}
		
		
		if(isset($_REQUEST['forward']) && $_REQUEST['forward']!=""){
			
			$fwre = $_REQUEST['forward']=="1" ? "Fw: " : "Re: ";
			$fromto = $_REQUEST['forward']=="1" ? "from_uid" : "to_uid";
			
			$sql =& new dbc("SELECT message FROM ".PREFIX."_pn WHERE pnid='".$_REQUEST['id']."' AND $fromto='".$UID."'");
			$row = $sql->fetchrow();
			$sql->close();
			
			$qtext = $row->message;
			$qtext = str_replace("\r\n", "\r\n", $qtext);
			$qtext = kstripslashes(phpencode($qtext));
			
			$qtext = "\r\n\r\n" . $lang['originalmessage'] . "\r\n" . $lang['from_t'].": ".base64_decode($_REQUEST['aut'])."\n". $lang['topic_t'] .": ".base64_decode($_REQUEST['subject'])."\n".$lang['date'].": ".base64_decode($_REQUEST['date'])."\r\n\n". $qtext . "";
			
			$tmpl->assign('tofromname', base64_decode($_REQUEST['aut']));
			$tmpl->assign('title', $fwre.base64_decode($_REQUEST['subject']));
			$tmpl->assign('text', $qtext);
		}
		
		if(SMILIES == 1) $tmpl->assign('smilie', 1);
		
		$tmpl->assign('outin', 0);
		$tmpl->assign('neu', 1);
		
	}
	$tmpl->assign('area', $area);
	$tmpl->assign('lang', $lang);
	
	if($ok == 1) {
		$tmpl->assign("content", parsetrue( "container/".container("pn"), $lang['personalmessage'], $tmpl->fetch("user/pn.tpl")));
	} else {
		$tmpl->assign("content", $EOUT);
	}
	
}
?>