<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //

$xoopsOption['pagetype'] = "pmsg";
include_once("mainfile.php");
include_once(XOOPS_ROOT_PATH."/class/xoopspm.php");

if ( !$xoopsUser ) {
	redirect_header("user.php",0);
	exit();
} else {
	if ( !empty($HTTP_POST_VARS['delete']) ) {
		$pm = new XoopsPM(intval($HTTP_POST_VARS['msg_id']));
		if ( !$pm->delete()) {
			exit();
		} else { 
			redirect_header("viewpmsg.php",1,_PM_DELETED);
			exit();
		}
	}
	$start = !empty($HTTP_GET_VARS['start']) ? intval($HTTP_GET_VARS['start']) : 0;
	$total_messages = !empty($HTTP_GET_VARS['total_messages']) ? intval($HTTP_GET_VARS['total_messages']) : 0;
	include("header.php");
	$pm_arr = XoopsPM::getAllPM(array("to_userid=".$xoopsUser->getVar("uid").""), true, "msg_time", "ASC", 1, $start);
	OpenTable();
	echo "<div><h4>". _PM_PRIVATEMESSAGE."</h4></div><br /><a href='userinfo.php?uid=". $xoopsUser->getVar("uid") ."'>". _PM_PROFILE ."</a>&nbsp;<span style='font-weight:bold;'>&raquo;&raquo;</span>&nbsp;<a href='viewpmsg.php'>". _PM_INBOX ."</a>&nbsp;<span style='font-weight:bold;'>&raquo;&raquo;</span><br /><table border='0' cellpadding='1' cellpadding='0' valign='top' width='100%'><tr><td><table border='0' cellpadding='3' cellpadding='1' width='100%'><tr class='bg2' align='left'><td width='20%' colspan='2' align='left'><span class='fg2'><b>". _PM_FROM ."</b></span></td></tr>\n";
	if ( empty($pm_arr) ) {
		echo "<tr><td class='bg3' colspan='2' align='center'>"._PM_YOUDONTHAVE."</td></tr>\n";
	} else {
		if ( !$pm_arr[0]->setRead()) {
			//echo "failed";
		}
		echo "<tr class='bg3' align='left'>\n";
		$poster = new XoopsUser($pm_arr[0]->getVar("from_userid"));
		if ( !$poster->isActive() ) {
			$poster = false;
		}
		echo "<td valign='top'>";
		if ( $poster != false ) { // we need to do this for deleted users
		    	echo "<b>".$poster->getVar("uname")."</b><br />\n";
			if ( $poster->getVar("user_avatar") != "" ) {
				echo "<img src='images/avatar/".$poster->getVar("user_avatar")."' alt='' /><br />\n";
			}
			if ( $poster->getVar("user_from") != "" ) {
				echo _PM_FROMC."".$poster->getVar("user_from")."<br /><br />\n";
			}
			if ( $poster->isOnline() ) {
			echo "<span style='color:#ee0000;font-weight:bold;'>"._ONLINE."</span><br /><br />\n";
			}
		} else {
			echo $xoopsConfig['anonymous']; // we need to do this for deleted users
		}
		echo "</td><td><img src='images/subject/".$pm_arr[0]->getVar("msg_image", "E")."' alt='' />&nbsp;"._PM_SENTC."".formatTimestamp($pm_arr[0]->getVar("msg_time"))."&nbsp;&nbsp;&nbsp";
		echo "<hr /><b>".$pm_arr[0]->getVar("subject")."</b><br /><br />\n";
		echo $pm_arr[0]->getVar("msg_text") . "<br /><br />";
        	echo "<hr />\n";
		if ( $poster != false ) {
			echo "<a href='userinfo.php?uid=".$poster->getVar("uid")."'><img src='images/icons/profile.gif' alt='"._PM_PROFILE."' /></a>\n";
			if ( $poster->getVar("user_viewemail") == 1 ) {
				echo "<a href='mailto:".$poster->getVar("email", "E")."'><img src='".XOOPS_URL."/images/icons/email.gif' alt='";
				printf(_PM_SENDEMAIL,$poster->getVar("uname"));
				echo "' /></a>\n";
			}
			$posterurl = $poster->getVar("url");
            		if ( $posterurl != "" ) {
				echo "<a href='".$posterurl."' target='_blank'><img src='images/icons/www.gif' alt='www' /></a>\n";
			}
            		if ( $poster->getVar("user_icq") != "" ) {
				echo "<a href='http://wwp.icq.com/scripts/search.dll?to=".$poster->getVar("user_icq", "E")."'><img src='".XOOPS_URL."/images/icons/icq_add.gif' alt='"._PM_ADDTOLIST."' /></a>\n";
			}
			if ( $poster->getVar("user_aim") != "" ) {
				echo "<a href='aim:goim?screenname=".$poster->getVar("user_aim", "E")."&amp;message=Hi+".$poster->getVar("user_aim", "E")."+Are+you+there?'><img src='".XOOPS_URL."/images/icons/aim.gif' alt='aim' /></a>\n";
			}
			if ( $poster->getVar("user_yim") != "" ) {
				echo "<a href='http://edit.yahoo.com/config/send_webmesg?.target=".$poster->getVar("user_yim","E")."&.src=pg'><img src='".XOOPS_URL."/images/icons/yim.gif' alt='yim' /></a>\n";
			}
			if ( $poster->getVar("user_msnm") != "" ) {
				echo "<a href='".XOOPS_URL."/userinfo.php?uid=".$poster->getVar("uid")."'><img src='".XOOPS_URL."/images/icons/msnm.gif' alt='msnm' /></a>\n";
			}
		} else {
			echo "&nbsp;";
		}
        	echo "</td></tr>\n";
		echo "<tr class='bg1' align='right'><td width='20%' colspan='2' align='right' class='fg2'>";
		$previous = $start - 1;
        	$next = $start + 1;

        	if ( $previous >= 0 ) { 
			echo "<a href='readpmsg.php?start=".$previous."&amp;total_messages=".$total_messages."'>"._PM_PREVIOUS."</a> | ";
		} else {
			echo _PM_PREVIOUS." | ";
		}
		if ( $next < $total_messages ) {
			echo "<a href='readpmsg.php?start=".$next."&amp;total_messages=".$total_messages."'>"._PM_NEXT."</a>";
		} else {
			echo ""._PM_NEXT."";
		}
		echo "</td></tr>\n";
        	echo "<tr class='bg2' align='left'><td width='20%' colspan='2' align='left'><form action='readpmsg.php' method='post' name='delete".$pm_arr[0]->getVar("msg_id")."'>";
		// we dont want to reply to a deleted user!
		if ( $poster != false ) {
			echo "<a href='#' onclick='javascript:openWithSelfMain(\"".XOOPS_URL."/pmlite.php?reply=1&amp;msg_id=".$pm_arr[0]->getVar("msg_id")."\",\"pmlite\",360,290);'><img src='".XOOPS_URL."/images/icons/reply.gif' alt='"._REPLY."' /></a>\n";
		}
		echo "<input type='hidden' name='delete' value='1' />";
		echo "<input type='hidden' name='msg_id' value='".$pm_arr[0]->getVar("msg_id")."' />";
		echo "<a href='#".$pm_arr[0]->getVar("msg_id")."' onclick='javascript:document.delete".$pm_arr[0]->getVar("msg_id").".submit();'><img src='".XOOPS_URL."/images/icons/delete.gif' alt='"._DELETETHISPOST."' /></a>";
		echo "</form></td></tr>\n";
	}
	echo "</table></td></tr></table>";
	CloseTable();
	include("footer.php");
}
?>