<?php
// -------------------------------------------------------------------------//
//			                 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
// ------------------------------------------------------------------------- //

include_once('../../mainfile.php');
include_once('class/pm.class.php');

if (!$xoopsUser) {
	$errormessage = _PM_SORRY.'<br />'._PM_PLZREG;
	redirect_header('user.php', 2, $errormessage);

exit();
}

// ------------------------------------------------------------------------- //

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

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

// ------------------------------------------------------------------------- //

if ( $HTTP_POST_VARS['op'] == 'submit' || $HTTP_POST_VARS['op'] == 'preview' ) {
	$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 ) {
	$message = _PM_USERNOEXIST.'<br />'._PM_PLZTRYAGAIN;
	redirect_header('javascript:history.go(-1)', 2, $message);
	exit();
	} else {
		$pm   = new PM();
		$type = $xoopsUser->isAdmin() ? 1 : 0;
		$pm->setVar('type'          , $type);
		$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('allow_html'    , $HTTP_POST_VARS['allow_html']);
		$pm->setVar('allow_smileys' , $HTTP_POST_VARS['allow_smileys']);
		$pm->setVar('allow_bbcode'  , $HTTP_POST_VARS['allow_bbcode']);
		$pm->setVar('from_userid'   , $xoopsUser->getVar('uid'));
		$pm_uname = XoopsUser::getUnameFromId($pm->getVar('to_userid'));

	if ( $HTTP_POST_VARS['op'] == 'preview' ) {
		include_once('../../header.php');
		themecenterposts($pm->getVar('subject', 'P'), $pm->getVar('msg_text', 'P'));
		OpenTable();

		echo '<h4>'._MI_PMSUBLINK_SEND.'</h4>';
		$subject = $pm->getVar('subject', 'F');
		$message = $pm->getVar('msg_text', 'F');
		include_once('include/messageform.inc.php');

		CloseTable();
		include_once('../../footer.php');
		} else {
			if ( !$pm->store() ) {
				redirect_header('javascript:history.go(-1)', 2, $pm->getErrors());
				exit();
				} else {
					redirect_header('index.php', 2, _PM_MESSAGEPOSTED);
					exit();
				}
		}
	}
	} elseif ( $reply == 1 || $send == 1 ) {
		$pm = new PM($msg_id);
		if ($reply == 1) {
			if ($pm->getVar('to_userid') != $xoopsUser->getVar('uid')) {
				redirect_header('javascript:history.go(-1)', 1);
				exit();
			}
			$pm->setVar('to_userid', $pm->getVar('from_userid'));
			$pm_uname = XoopsUser::getUnameFromId($pm->getVar('from_userid'));
			$subject  = 'Re: '.$pm->getVar('subject', 'F');
			$message  = "[quote]\n";
			$message .= sprintf(_PM_USERWROTE, $pm_uname);
			$message .= "\n\n".$pm->getVar('msg_text', 'F')."\n\n[/quote]\n\n";
			} elseif ($to_userid) {
				$pm->setVar('to_userid', $to_userid);
				$pm_uname = XoopsUser::getUnameFromId($to_userid);
			}
		include_once('../../header.php');
		OpenTable();

		echo '<h4>'._MI_PMSUBLINK_SEND.'</h4>';
		include_once('include/messageform.inc.php');

		CloseTable();
		include_once('../../footer.php');
		} else {
			redirect_header(XOOPS_URL.'/', 1);
			exit();
		}
?>
