<?php
/************************************************************/
/*	                     RUNCMS                             */
/* Relaible - 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('header.php');
include_once('class/pm.class.php');
include_once(XOOPS_ROOT_PATH."/class/fileupload.php");
include_once(XOOPS_ROOT_PATH.'/class/xoopsmailer.php');
include_once('cache/config.php');

Global $xoopsUser;

if ($HTTP_POST_VARS['msg_replay'] == 1) {
		    $pm = new PM(intval($HTTP_POST_VARS['msg_id1']));
			$pm->setReplay();
			//$msg_id = 0;
		}

if ($HTTP_POST_VARS['submit']) {
    $op = "submit";
}
if ($HTTP_POST_VARS['preview']) {
    $op = "preview";
}

$to_userid = !empty($HTTP_POST_VARS['to_userid']) ? $HTTP_POST_VARS['to_userid'] : $HTTP_GET_VARS['to_userid'];
$send = $HTTP_POST_VARS['send'];

$pm = new PM();
$pm_arr =& PM::getAllPM(array("to_userid='".$to_userid."'"), true);
$total_messages = count($pm_arr);

if ( !empty($pmConfig['max_pms']) && ($total_messages >= intval($pmConfig['max_pms'])) ) {
	redirect_header('index.php', 2, _PM_FULL);
	exit();
}
$pm = new PM();
$pm_arr1 =& PM::getAllPM(array("to_userid='".$xoopsUser->getVar("uid")."'"), true);
$total_messages1 = count($pm_arr1);

if ( !empty($pmConfig['max_pms']) && ($total_messages1 >= intval($pmConfig['max_pms'])) ) {
	redirect_header('index.php', 2, _PM_FULL1);
	exit();
}

$upload = new fileupload();
// ------------------------------------------------------------------------- //

$reply     = !empty($HTTP_GET_VARS['reply'])     ? 1 : 0;
$msg_id    = !empty($HTTP_GET_VARS['msg_id'])    ? intval($HTTP_GET_VARS['msg_id']) : 0;
if ($send == "") {
    $send      = !empty($HTTP_GET_VARS['send'])      ? 1 : 0;
}

if ($to_userid == "") {
	$to_userid = !empty($HTTP_GET_VARS['to_userid']) ? intval($HTTP_GET_VARS['to_userid']) : 0;
}
// ------------------------------------------------------------------------- //

if ( $op == 'submit' || $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);
		if ($HTTP_POST_VARS['msg_image'] == "") {
		    $HTTP_POST_VARS['msg_image'] = 'icon1.gif';
		}
		//*******************
	$attachment_info='';
		if ($msg_attachment)
		{	
			$upload->set_upload_dir('cache/files', 'msg_attachment');
			$upload->set_basename($xoopsUser->getVar('uid').'_'.time());
			$result = $upload->upload();
 			$attach_errors = $upload->errors(0);
		if ($result)
		{
 			$attachment_filename=$result['msg_attachment']['realname'];
			$attachment_pseudoname=$result['msg_attachment']['basename'].$result['msg_attachment']['extension'];
			$attachment_size=$result['msg_attachment']['size'];
			$attachment_info=$attachment_filename.'|'.$attachment_pseudoname.'|'.$attachment_size.'|0';
		}
		if ($attach_errors != "" && !strpos($attach_errors, _ULC_FILE))
		{
			redirect_header("javascript:history.go(-'2')", 5, "<b>Error</b>: ".$attach_errors."<br />");
			exit();
		}
	}
		//******************
		if ($HTTP_POST_VARS['subject'] == "") {
		    $subject = "Post";
		} else {
		$subject = $HTTP_POST_VARS['subject'];
		}
		
		$pm->setVar('msg_image'     , $HTTP_POST_VARS['msg_image']);
		$pm->setVar('subject'       , $subject);
		$pm->setVar('msg_attachment', $attachment_info);
		$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->setVar('msg_replay'    , 0);
		$pm_uname = XoopsUser::getUnameFromId($pm->getVar('to_userid'));
		
		
	if ( $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 {
				//***********************************
				$fromusername = XoopsUser::getUnameFromId($pm->getVar('from_userid'));
				$username = XoopsUser::getUnameFromId($pm->getVar('to_userid'));
				
				$to_user = $pm->getVar('to_userid');
				$sql = "SELECT msg_mail FROM ".$db->prefix("private_msgs_config")." WHERE msg_uid=".$to_user."";
				$result = $db->query($sql);
				$mailsend = $db->fetch_row($result);
				if ($mailsend[0] == 1 and $pmConfig['sendmail'] == 1) {
					$sql = "SELECT email FROM ".$db->prefix("users")." WHERE uid=".$to_user."";
    				$result = $db->query($sql);
					$usermail = $db->fetch_row($result);
					$mailbody = _PM_MAILBODY." ".XOOPS_URL;
					$xoopsMailer =& getMailer();
					$xoopsMailer->useMail();
					$xoopsMailer->setTemplate("messages.tpl");
					$xoopsMailer->setTemplateDir(XOOPS_ROOT_PATH."/modules/messages");
					$xoopsMailer->assign("SITENAME", $meta['title']);
					$xoopsMailer->assign("ADMINMAIL", $xoopsConfig['adminmail']);
					$xoopsMailer->assign("SITEURL", XOOPS_URL."/");
					$xoopsMailer->assign("FROMUSERNAME", $fromusername);
					$xoopsMailer->assign("USERNAME", $username);
					$xoopsMailer->setToEmails($usermail);
					$xoopsMailer->setFromEmail($mail);
					$xoopsMailer->setFromName($meta['title']);
					$xoopsMailer->setSubject(_PM_MAILSUBJECT);
					$xoopsMailer->send();
				}
				//**************************************
					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'));
			if (stristr($pm->getVar('subject', 'F'),'Re:')) {
			    $subject  = $pm->getVar('subject', 'F');
			} else {
			$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();
		}
?>
