<?php
// $Id: pmlite.php,v 1.1 2004/07/11 10:29:14 Farsus Exp $
// -------------------------------------------------------------------------//
//			       RUNCMS                                                   //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Original Author: The Xoops Project
// Author Website : http://www.xoops.org
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //

$xoopsOption['pagetype']   = 'pmsg';
$xoopsOption['page_style'] = 0;

include_once("mainfile.php");
include_once(XOOPS_ROOT_PATH."/class/xoopspm.php");
include_once(XOOPS_ROOT_PATH.'/class/xoopsform/formdhtmltextarea.php');

$reply     = !empty($HTTP_GET_VARS['reply'])     ? 1 : 0;
$send      = !empty($HTTP_GET_VARS['send'])      ? 1 : 0;
$send2     = !empty($HTTP_GET_VARS['send2'])     ? 1 : 0;
$to_userid = !empty($HTTP_GET_VARS['to_userid']) ? intval($HTTP_GET_VARS['to_userid']) : 0;
$msg_id    = !empty($HTTP_GET_VARS['msg_id'])    ? intval($HTTP_GET_VARS['msg_id']) : 0;

if ( empty($HTTP_GET_VARS['refresh'] ) && $HTTP_POST_VARS['op'] != "submit" ) {
	$jump = "pmlite.php?refresh=".time()."";
	if ( $send == 1 ) {
		$jump .= "&amp;send=".$send."";
		} elseif ( $send2 == 1 ) {
			$jump .= "&amp;send2=".$send2."&amp;to_userid=".$to_userid."";
			} elseif ( $reply == 1 ) {
				$jump .= "&amp;reply=".$reply."&amp;msg_id=".$msg_id."";
			}
	echo "<html><head><meta http-equiv='Refresh' content='0; url=".$jump."' /></head><body></body></html>";
	exit();
}

xoops_header();

if ($xoopsUser) {

$count_arr      =& XoopsPM::getAllPM(array("to_userid='".$xoopsUser->getVar("uid")."'"), false);
$total_messages = count($count_arr);

if ( !empty($xoopsConfig['max_pms']) && ($total_messages >= intval($xoopsConfig['max_pms'])) ) {
	?>
	<table align="center" cellpadding="5"><tr>
	<td align="center"><h4><?php echo _ERROR;?></h4></td>
	</tr><tr>
	<td><?php echo _PM_FULL;?></td>
	</tr><tr>
	<td align="center"><input type='button' class='button' name='cancel' value='<?php echo _CLOSE;?>' onclick='javascript:window.close();' /></td>
	</tr></table>
	<?php
	xoops_footer(0);
	exit();
}

if ( isset($HTTP_POST_VARS['op']) && $HTTP_POST_VARS['op'] == "submit" ) {
	$res = $db->query("SELECT COUNT(*) FROM ".$db->prefix("users")." WHERE uid=".intval($HTTP_POST_VARS['to_userid'])."");
	list($count) = $db->fetch_row($res);

if ( $count != 1 ) {
	echo "<br /><br /><div align='center'><h4>"._PM_USERNOEXIST."<br />";
	echo _PM_PLZTRYAGAIN."</h4><br />";
	echo "[ <a href='javascript:history.go(-1)'>"._GOBACK."</a> ]</div>";
	} else {
		$pm = new XoopsPM();
		$pm->setVar("subject"    , $HTTP_POST_VARS['subject']);
		$pm->setVar("msg_text"   , $HTTP_POST_VARS['message']);
		$pm->setVar("to_userid"  , $HTTP_POST_VARS['to_userid']);
		$pm->setVar("from_userid", $xoopsUser->getVar('uid'));

	if ( !$pm->store() ) {
		echo $pm->getErrors();
		echo "<br /><a href='javascript:history.go(-1)'>"._GOBACK."</a>";
		} else {
			echo "<br /><br /><div align='center'><h4>"._PM_MESSAGEPOSTED."</h4><br /><a href=\"javascript:window.opener.location='".XOOPS_URL."/viewpmsg.php';window.close();\">"._PM_CLICKHERE."</a><br /><br /><a href=\"javascript:window.close();\">"._PM_ORCLOSEWINDOW."</a></div>";
		}
	}

	} elseif ( $reply == 1 || $send == 1 || $send2 == 1 ) {

	if ($reply == 1) {
		$pm = new XoopsPM($msg_id);
		if ($pm->getVar("to_userid") != $xoopsUser->getVar("uid")) {
			echo '<script type="text/javascript">self.close();</script>';
			exit();
		}
		$pm_uname   = XoopsUser::getUnameFromId($pm->getVar("from_userid"));
		$replytext  = "[quote]\n";
		$replytext .= sprintf(_PM_USERWROTE, $pm_uname);
		$replytext .= "\n\n".$pm->getVar("msg_text", "E")."\n\n[/quote]\n\n";
	}

echo "
<form action='pmlite.php' method='post' onsubmit='return setRequired(\"subject\", \"message\");'><br />
<table width='100%' align='center'><tr align='left'>
<td class='bg3' nowrap><b>"._PM_TO."<b></td>";

if ($reply == 1) {
	echo "<td class='bg1'><input type='hidden' name='to_userid' value='".$pm->getVar("from_userid")."' />".$pm_uname."</td>";
	} elseif ($send2 == 1) {
		$to_username = XoopsUser::getUnameFromId($to_userid);
		echo "<td class='bg1'><input type='hidden' name='to_userid' value='".$to_userid."' />".$to_username."</td>";
		} else {
			echo "<td class='bg1'><select class='select' name='to_userid'>";
			$result = $db->query("SELECT uid, uname FROM ".$db->prefix("users")." WHERE level > 0 ORDER BY uname");
			while ( list($ftouid, $ftouname) = $db->fetch_row($result) ) {
				echo "<option value='".$ftouid."'>".$myts->makeTboxData4Show($ftouname)."</option>";
			}
			echo "</select></td>";
		}

	echo "</tr><tr align='left'><td class='bg3' nowrap><b>"._PM_SUBJECTC."<b></td>";
	if ($reply == 1) {
		echo "<td class='bg1'><input type='text' class='text' name='subject' id='subject' value='Re: ".$pm->getVar("subject", "E")."' size='30' maxlength='100' /></td>";
		} else {
			echo "<td class='bg1'><input type='text' class='text' name='subject' id='subject' size='30' maxlength='100' /></td>";
		}
	echo "
	</tr><tr align='left' valign='top'>
	<td class='bg3' nowrap><b>"._PM_MESSAGEC."</b><br /><br /></td>
	<td class='bg1'>";

	if ($reply == 1) {
		$message = $replytext;
	}

	$desc  = new XoopsFormDhtmlTextArea('', 'message', $message, 10, 50);
	echo $desc->render();

	echo "
	</td>
	</tr><tr>
	<td class='bg1' colspan='2' align='center'>
	<input type='hidden' name='op' value='submit' />
	<input type='submit' class='button' name='submit' value='"._SUBMIT."' />
	<input type='reset' class='button' value='"._PM_CLEAR."' />
	<input type='button' class='button' name='cancel' value='"._CANCEL."' onclick='javascript:window.close();' />
	</td>
	</tr></table></form>";
	}
	} else {
		echo '<script type="text/javascript">self.close();</script>';
		exit();
	}

xoops_footer(0);
?>
