<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.2 www.exoops.info 2006/03 
// $Id: pmlite.php,v 1.12 2003/07/09 23:27:07 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// 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($_GET['reply'])     ? 1 : 0;
$send      = !empty($_GET['send'])      ? 1 : 0;
$send2     = !empty($_GET['send2'])     ? 1 : 0;
$to_userid = !empty($_GET['to_userid']) ? intval($_GET['to_userid']) : 0;
$msg_id    = !empty($_GET['msg_id'])    ? intval($_GET['msg_id']) : 0;
if ( empty($_GET['refresh'] ) && $_POST['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($bcoosConfig['max_pms']) && ($total_messages >= intval($bcoosConfig['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($_POST['op']) && $_POST['op'] == "submit" ) {
	$res = $db->query("SELECT COUNT(*) FROM ".$db->prefix("users")." WHERE uid=".intval($_POST['to_userid'])."");
	list($count) = $db->fetchRow($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", $_POST['subject']);
		$pm->setVar("msg_text", $_POST['message']);
		$pm->setVar("to_userid", $_POST['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->fetchRow($result) ) {
				echo "<option value='".$ftouid."'>".$myts->makeTboxData4Show($ftouname)."</option>";
			}
			echo "</select></td>";
		}
	echo "</tr><tr align='left'><td class='bg3' nowrap><b>"._SUBJECT.": <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);
?>
