<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

if($_POST['delete']) {
  include("_mysql.php");
	include("_settings.php");
	include("_functions.php");
	if(!$userID) die('Not logged in!');

  $messageID = $_POST['messageID'];

	foreach($messageID as $id) {
	    safe_query("DELETE FROM ".PREFIX."messenger WHERE messageID='".$id."'");
	}
	die(redirect('index.php?site=messenger&action=outgoing','',0));
}
elseif($_POST['quickaction']) {
  include("_mysql.php");
	include("_settings.php");
	include("_functions.php");
	if(!$userID) die('Not logged in!');

	$quickactiontype = $_POST['quickactiontype'];
  $messageID = $_POST['messageID'];
	
   	if($quickactiontype=="viewed") {
    	foreach($messageID as $id) {
	    	safe_query("UPDATE ".PREFIX."messenger SET viewed='1' WHERE messageID='$id' AND userID='$userID'");
		}
	}
	elseif($quickactiontype=="notviewed") {
    	foreach($messageID as $id) {
	    	safe_query("UPDATE ".PREFIX."messenger SET viewed='0' WHERE messageID='$id' AND userID='$userID'");
		}
	}
	elseif($quickactiontype=="delete") {
    	foreach($messageID as $id) {
	    	safe_query("DELETE FROM ".PREFIX."messenger WHERE messageID='$id' AND touser='$userID'");
		}
	}
	die(redirect('index.php?site=messenger&action=incoming','',0));
}
elseif($_POST['send']) {
  include("_mysql.php");
	include("_settings.php");
	include("_functions.php");
	
  $touser = $_POST['touser'];
  if($touser[0] == "" AND $touser_filed != "*" AND getuserid($_POST['touser_field'])) $touser[0] = getuserid($_POST['touser_field']);

	if($_POST['eachmember']) {
		unset($touser);
		$ergebnis=safe_query("SELECT userID FROM ".PREFIX."user");
		while($ds=mysql_fetch_array($ergebnis)) {
			if(isclanmember($ds[userID])) $touser[]=$ds[userID];
		}
	}
	
	if($_POST['eachuser']) {
		unset($touser);
		$ergebnis=safe_query("SELECT userID FROM ".PREFIX."user");
		while($ds=mysql_fetch_array($ergebnis)) {
			$touser[]=$ds[userID];
		}
	}

	$date=time();
	$title = $_POST['title'];
	$message = $_POST['message'];
	if($touser[0] != "") {
	 foreach($touser as $id) {
		sendmessage($id,$title,$message,$userID);
   }
	}
	die(redirect('index.php?site=messenger&action=outgoing','',0));
}
elseif($_POST['reply']) {
  include("_mysql.php");
	include("_settings.php");
	include("_functions.php");
	if(!$userID) die('No access.');

  sendmessage($_POST['id'],$_POST['title'],$_POST['message'],$userID);

	die(redirect('index.php?site=messenger&action=outgoing','',0));
}

elseif($userID) {

	if($_GET['userID'] OR $_POST['userID']) die('Not logged in.');
  $action = $_GET['action'];

  eval ("\$title_messenger = \"".gettemplate("title_messenger")."\";");
  echo $title_messenger;

	echo'<input type="button" value="Incoming" onClick="MM_goToURL(\'parent\',\'index.php?site=messenger&action=incoming\');return document.MM_returnValue"> 
		 <input type="button" value="Outgoing" onClick="MM_goToURL(\'parent\',\'index.php?site=messenger&action=outgoing\');return document.MM_returnValue"> 
		 <input type="button" value="New Message" onClick="MM_goToURL(\'parent\',\'index.php?site=messenger&action=newmessage\');return document.MM_returnValue">';

	if(!isset($action)) $action="incoming";

	if($action=="incoming") {
    	echo'<h2>incoming messages:</h2>';

    $page = $_GET['page'];
    $sort = $_GET['sort'];
    $type = $_GET['type'];
    if($_POST['entries']) $entries = $_POST['entries'];
    else $entries = $_GET['entries'];
	
		$alle = safe_query("SELECT messageID FROM ".PREFIX."messenger WHERE touser='$userID' AND userID='$userID'");
		$gesamt = mysql_num_rows($alle);
		$pages=1;
		if(!isset($page)) $page = 1;
		if(!isset($sort)) $sort="date";
		if(!isset($type)) $type = "DESC";
		
		if(isset($entries)) $max=$entries;
		else $max=$maxmessages;
				
    	for ($n=$max; $n<=$gesamt; $n+=$max) {
	    	if($gesamt>$n) $pages++;
		}
		 	
		if($pages>1) $page_link = makepagelink("index.php?site=messenger&action=incoming&sort=$sort&type=$type&entries=$max", $page, $pages);
		
		if ($page == "1") {
        	$ergebnis = safe_query("SELECT * FROM ".PREFIX."messenger WHERE touser='$userID' AND userID='$userID' ORDER BY $sort $type LIMIT 0,$max");
			if($type=="DESC") $n=$gesamt;
			else $n=1;
		}
		else {
	    	$start=$page*$max-$max;
        	$ergebnis = safe_query("SELECT * FROM ".PREFIX."messenger WHERE touser='$userID' AND userID='$userID' ORDER BY $sort $type LIMIT $start,$max");
			if($type=="DESC") $n = ($gesamt)-$page*$max+$max;
			else $n = ($gesamt+1)-$page*$max+$max;
		}
	
		$anz=mysql_num_rows($ergebnis);
		if($anz) {
		    if($type=="ASC") 
		        $sorter='<a href="index.php?site=messenger&action=incoming&page='.$page.'&sort='.$sort.'&type=DESC&entries='.$max.'">Sort:</a> <img src="images/icons/asc.gif" width="9" height="7" border="0">&nbsp;&nbsp;&nbsp;';
		    else 
		        $sorter='<a href="index.php?site=messenger&action=incoming&page='.$page.'&sort='.$sort.'&type=ASC&entries='.$max.'">Sort:</a> <img src="images/icons/desc.gif" width="9" height="7" border="0">&nbsp;&nbsp;&nbsp;';
					 
			echo'<table width="100%" border="0" cellspacing="0" cellpadding="0">
				  <tr><form method="post" action="index.php?site=messenger&action=incoming">
				    <td>'.$sorter.' '.$page_link.'</td>
				    <td align="right">messages/page <input type="text" name="entries" size="2" value="'.$max.'" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"> <input type="submit" value="show"></td>
				  </form></tr>
				</table>';

			
			eval ("\$pm_incoming_head = \"".gettemplate("pm_incoming_head")."\";");
      	    echo $pm_incoming_head;
			$n=1;
			while($ds=mysql_fetch_array($ergebnis)) {
				if($n%2) {
					$bg1=BG_1;
					$bg2=BG_2;
				}
				else {
					$bg1=BG_3;
					$bg2=BG_4;
				}
				$date=date("d.m.Y - H:i", $ds[date]);
				$icon='<img src="images/icons/pm_old.gif" width="14" height="12">';

				if(isignored($userID, $ds[fromuser])) $buddy='<a href="buddys.php?action=readd&id='.$ds[fromuser].'&userID='.$userID.'"><img src="images/icons/buddy_readd.gif" width="16" height="16" border="0" alt="back to buddy-list"></a>';
				elseif(isbuddy($userID, $ds[fromuser])) $buddy='<a href="buddys.php?action=ignore&id='.$ds[fromuser].'&userID='.$userID.'"><img src="images/icons/buddy_ignore.gif" width="16" height="16" border="0" alt="ignore user"></a>';
				else $buddy='<a href="buddys.php?action=add&id='.$ds[fromuser].'&userID='.$userID.'"><img src="images/icons/buddy_add.gif" width="16" height="16" border="0" alt="add to buddylist"></a>';
				
				if(isonline($ds[fromuser])=="offline") $statuspic='<img src="images/icons/offline.gif" width="7" height="7" alt="offline">';
			    else $statuspic='<img src="images/icons/online.gif" width="7" height="7" alt="online">';
				
				$sender='<a href="index.php?site=profile&id='.$ds[fromuser].'"><b>'.getnickname($ds[fromuser]).'</b></a>';
				if(isclanmember($ds[fromuser])) $member=' <img src="images/icons/member.gif" width="6" height="11" alt="Clanmember">';
				else $member='';
				
				$title='&#8226; <a href="index.php?site=messenger&action=show&id='.$ds[messageID].'">'.clearfromtags($ds[title]).'</a>';
				if(!$ds[viewed]) {
			    	$icon='<img src="images/icons/pm_new.gif" width="14" height="12">';
					$sender='<a href="index.php?site=profile&id='.$ds[fromuser].'"><b>'.getnickname($ds[fromuser]).'</b></a>';
					$title='&#8226; <a href="index.php?site=messenger&action=show&id='.$ds[messageID].'"><b>'.clearfromtags($ds[title]).'</b></a>';
				}
				eval ("\$pm_incoming_content = \"".gettemplate("pm_incoming_content")."\";");
      	        echo $pm_incoming_content;
				$n++;
			}
			eval ("\$pm_incoming_foot = \"".gettemplate("pm_incoming_foot")."\";");
      	    echo $pm_incoming_foot;
		}
		else echo'no incoming messages';
	}	
	elseif($action=="outgoing") {
    	echo'<h2>outgoing messages:</h2>';

    $page = $_GET['page'];
    $sort = $_GET['sorf'];
    $type = $_GET['type'];
    if($_POST['entries']) $entries = $_POST['entries'];
    else $entries = $_GET['entries'];

		$alle=safe_query("SELECT messageID FROM ".PREFIX."messenger WHERE fromuser='$userID' AND userID='$userID'");
		$gesamt = mysql_num_rows($alle);
		$pages=1;
		if(!isset($page)) $page = 1;
		if(!isset($sort)) $sort="date";
		if(!isset($type)) $type = "DESC";
		
		if(isset($entries)) $max=$entries;
		else $max=$maxmessages;
				
    	for ($n=$max; $n<=$gesamt; $n+=$max) {
	    	if($gesamt>$n) $pages++;
		}
		 	
		if($pages>1) $page_link = makepagelink("index.php?site=messenger&action=outgoing&entries=$max", $page, $pages);
		
		if ($page == "1") {
        	$ergebnis = safe_query("SELECT * FROM ".PREFIX."messenger WHERE fromuser='$userID' AND userID='$userID' ORDER BY $sort $type LIMIT 0,$max");
			if($type=="DESC") $n=$gesamt;
			else $n=1;
		}
		else {
	    	$start=$page*$max-$max;
        	$ergebnis = safe_query("SELECT * FROM ".PREFIX."messenger WHERE fromuser='$userID' AND userID='$userID' ORDER BY $sort $type LIMIT $start,$max");
			if($type=="DESC") $n = ($gesamt)-$page*$max+$max;
			else $n = ($gesamt+1)-$page*$max+$max;
		}
	
		$anz=mysql_num_rows($ergebnis);
		if($anz) {
		    if($type=="ASC") 
		        $sorter='<a href="index.php?site=messenger&action=outgoing&page='.$page.'&sort='.$sort.'&type=DESC&entries='.$max.'">Sort:</a> <img src="images/icons/asc.gif" width="9" height="7" border="0">&nbsp;&nbsp;&nbsp;';
  		    else 
		        $sorter='<a href="index.php?site=messenger&action=outgoing&page='.$page.'&sort='.$sort.'&type=ASC&entries='.$max.'">Sort:</a> <img src="images/icons/desc.gif" width="9" height="7" border="0">&nbsp;&nbsp;&nbsp;';

	    	echo'<table width="100%" border="0" cellspacing="0" cellpadding="0">
				  <tr><form method="post" action="index.php?site=messenger&action=outgoing">
				    <td>'.$sorter.' '.$page_link.'</td>
				    <td align="right">messages/page <input type="text" name="entries" size="2" value="'.$max.'" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"> <input type="submit" value="show"></td>
				  </form></tr>
				</table>';	
			
			eval ("\$pm_outgoing_head = \"".gettemplate("pm_outgoing_head")."\";");
      	    echo $pm_outgoing_head;
			$n=1;
			while($ds=mysql_fetch_array($ergebnis)) {
				if($n%2) {
					$bg1=BG_1;
					$bg2=BG_2;
				}
				else {
					$bg1=BG_3;
					$bg2=BG_4;
				}
				$date=date("d.m.Y - H:i", $ds[date]);
			
				if(isignored($userID, $ds[touser])) $buddy='<a href="buddys.php?action=readd&id='.$ds[touser].'&userID='.$userID.'"><img src="images/icons/buddy_readd.gif" width="16" height="16" border="0" alt="back to buddy-list"></a>';
				elseif(isbuddy($userID, $ds[touser])) $buddy='<a href="buddys.php?action=ignore&id='.$ds[touser].'&userID='.$userID.'"><img src="images/icons/buddy_ignore.gif" width="16" height="16" border="0" alt="ignore user"></a>';
				else $buddy='<a href="buddys.php?action=add&id='.$ds[touser].'&userID='.$userID.'"><img src="images/icons/buddy_add.gif" width="16" height="16" border="0" alt="add to buddylist"></a>';
				
				$receptionist='<a href="index.php?site=profile&id='.$ds[touser].'"><b>'.getnickname($ds[touser]).'</b></a>';
				if(isclanmember($ds[touser])) $member=' <img src="images/icons/member.gif" width="6" height="11" alt="Clanmember">';
				else $member='';
				if(isonline($ds[touser])=="offline") $statuspic='<img src="images/icons/offline.gif" width="7" height="7" alt="offline">';
			    else $statuspic='<img src="images/icons/online.gif" width="7" height="7" alt="online">';
				
				$title='&#8226; <a href="index.php?site=messenger&action=show&id='.$ds[messageID].'">'.clearfromtags($ds[title]).'</a>';
				$icon='<img src="images/icons/pm_old.gif" width="14" height="12">';
				eval ("\$pm_outgoing_content = \"".gettemplate("pm_outgoing_content")."\";");
      	        echo $pm_outgoing_content;
				$n++;
			}
			eval ("\$pm_outgoing_foot = \"".gettemplate("pm_outgoing_foot")."\";");
      	    echo $pm_outgoing_foot;
		}
		else echo'no outgoing messages';

	}
	elseif($action=="show") {

    $id = $_GET['id'];
		$ds=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."messenger WHERE messageID='$id'"));
		
		if($ds['touser']==$userID OR $ds['fromuser']==$userID) {
		  safe_query("UPDATE ".PREFIX."messenger SET viewed='1' WHERE messageID='$id'");
			$date=date("d.m.Y - H:i", $ds[date]);
			$sender='<a class="titlelink" href="index.php?site=profile&id='.$ds[fromuser].'">'.getnickname($ds[fromuser]).'</a>';
			$message = cleartext($ds[message]);
			$message = toggle($message, $ds[messageID]);
			$title = clearfromtags($ds[title]);
	
			$bg1=BG_1;
			eval ("\$pm_show = \"".gettemplate("pm_show")."\";");
      echo $pm_show;
    } else redirect('index.php?site=messenger','',0);
	}
	elseif($action=="touser") {
    $touser = $_GET['touser'];
   	$ergebnis=safe_query("SELECT * FROM ".PREFIX."messenger WHERE messageID='$id'");
		$ds=mysql_fetch_array($ergebnis);
	
		$tousernick=getnickname($touser);
	
		$bg1=BG_1;
		eval ("\$addbbcode = \"".gettemplate("addbbcode")."\";");
		eval ("\$pm_new_touser = \"".gettemplate("pm_new_touser")."\";");
    	echo $pm_new_touser;
	}
	elseif($action=="reply") {
    $id = $_GET['id'];
   	$ergebnis=safe_query("SELECT * FROM ".PREFIX."messenger WHERE messageID='$id'");
		$ds=mysql_fetch_array($ergebnis);
	
		$replytouser=$ds[fromuser];
		$tousernick=getnickname($replytouser);
		$date=date("d.m.Y - H:i", $ds[date]);
	
		$title=$ds[title];
		if(!preg_match("#Re\[(.*?)\]:#si", $title)) $title="Re[1]: ".$title;
		else {
		    preg_match_all("#Re\[(.*?)\]:#si", $title, $re);
			$rep=$re[1][0] + 1;
			$title = preg_replace("#\[(.*?)\]#si", "[$rep]", $title);
		}
		
		$message = toggle($ds[message], $ds[messageID]);
		$message='[QUOTE]'.$tousernick.' wrote:[HR]'.$message.'[/QUOTE]

';	
	
		$bg1=BG_1;
		eval ("\$addbbcode = \"".gettemplate("addbbcode")."\";");
		eval ("\$pm_reply = \"".gettemplate("pm_reply")."\";");
    	echo $pm_reply;
	}
	elseif($action=="newmessage") {
    	$ergebnis=safe_query("SELECT buddy FROM ".PREFIX."buddys WHERE userID='$userID'");
		while($ds=mysql_fetch_array($ergebnis)) {
	    	$user.='<option value="'.$ds[buddy].'">'.getnickname($ds[buddy]).'</option>';
		}
		if($user=="") $user='<option value="">no buddys</option>'; else $user  = '<option value="" selected>---</option>'.$user;
		$bg1=BG_1;
		
		if(isanyadmin($userID)) $admin='<b>Admin-Options:</b><br>
              send to each user 
              <input class="input" type="checkbox" name="eachuser" value="true">
              <br>
              send to each clanmember 
              <input class="input" type="checkbox" name="eachmember" value="true"> ';
		
		eval ("\$addbbcode = \"".gettemplate("addbbcode")."\";");
		eval ("\$pm_new = \"".gettemplate("pm_new")."\";");
    	echo $pm_new;
	}
}
else echo'<br><br>To use the messenger you have to be registered and logged in!<br><br>
	          &#8226; <b><a href="index.php?site=register">register now</a></b>';
?>
