<?php

(!defined('IN_PowerBB')) ? die() : '';

$CALL_SYSTEM					=	array();
$CALL_SYSTEM['PM'] 				= 	true;
$CALL_SYSTEM['ICONS'] 			= 	true;
$CALL_SYSTEM['TOOLBOX'] 		= 	true;
$CALL_SYSTEM['FILESEXTENSION'] 	= 	true;
$CALL_SYSTEM['ATTACH'] 			= 	true;
$CALL_SYSTEM['MODERATORS'] 	= 	true;
$CALL_SYSTEM['USERRATING'] 	= 	true;

define('JAVASCRIPT_PowerCode',true);

include('common.php');

define('CLASS_NAME','PowerBBPrivateMassegeShowMOD');

class PowerBBPrivateMassegeShowMOD
{
	function run()
	{
		global $PowerBB;

		if (!$PowerBB->_CONF['info_row']['pm_feature'])
		{
            $PowerBB->functions->ShowHeader();
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['no_pm']);
		}

		/** Can't use the private massege system **/
		if (!$PowerBB->_CONF['rows']['group_info']['use_pm'])
		{
            $PowerBB->functions->ShowHeader();
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Cant_use_pm']);
		}
		/** **/

		/** Visitor can't use the private massege system **/
		if (!$PowerBB->_CONF['member_permission'])
		{
            $PowerBB->functions->ShowHeader();
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Cant_see_pm']);
		}
		/** **/

		/** Read a massege **/
		if ($PowerBB->_GET['show'])
		{
			$this->_ShowMassege();
		}
		/** **/

		$PowerBB->functions->GetFooter();
	}

	/**
	 * Get a massege information to show it
	 */
	function _ShowMassege()
	{
		global $PowerBB;

		$PowerBB->functions->ShowHeader();

		if (empty($PowerBB->_GET['id']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
		}

		$PowerBB->_GET['id'] = $PowerBB->functions->CleanVariable($PowerBB->_GET['id'],'intval');

		$MsgArr 			= 	array();
		$MsgArr['id'] 		= 	$PowerBB->_GET['id'];

		$MassegeInfo = $PowerBB->pm->GetMessageInfo($MsgArr);
       /*
		if ($MassegeInfo['user_to'] != $PowerBB->_CONF['member_row']['username'] or $MassegeInfo['user_from'] != $PowerBB->_CONF['member_row']['username'])
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Letter_requested_does_not_exist']);
		}
       */

		$PrivateMsgArr 			= 	array();
		$PrivateMsgArr['id'] 		= 	$PowerBB->_GET['id'];
		$PrivateMsgArr['username'] = 	$PowerBB->_CONF['member_row']['username'];

		$PowerBB->_CONF['template']['MassegeRow'] = $PowerBB->pm->GetPrivateMassegeInfo($PrivateMsgArr);

		if (!$PowerBB->_CONF['template']['MassegeRow'])
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Letter_requested_does_not_exist']);
		}

		if ($PowerBB->_CONF['template']['MassegeRow']['user_to'] != $PowerBB->_CONF['member_row']['username'] AND $PowerBB->_CONF['template']['MassegeRow']['user_from'] != $PowerBB->_CONF['member_row']['username'])
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Letter_requested_does_not_exist']);
		}


		$PowerBB->functions->CleanVariable($PowerBB->_CONF['template']['MassegeRow'],'html');
		$PowerBB->functions->CleanVariable($PowerBB->_CONF['template']['MassegeRow'],'sql');

		$get_list  = 'id,username,user_sig,user_country,user_gender,register_date';
		$get_list .= ',posts,user_title,avater_path,away,away_msg,hide_online,invite_num';

		$SenderArr = array();

		$SenderArr['get'] 		= 	$get_list;
		$SenderArr['where']		=	array('username',$PowerBB->_CONF['template']['MassegeRow']['user_from']);

		$PowerBB->_CONF['template']['Info'] = $PowerBB->member->GetMemberInfo($SenderArr);

		$PowerBB->functions->CleanVariable($PowerBB->_CONF['template']['Info'],'html');


   		// Get username style
		$MemberArr 			= 	array();
		$MemberArr['where'] 	= 	array('username',$PowerBB->_CONF['template']['MassegeRow']['user_from']);

		$StyleMemberInfo = $PowerBB->member->GetMemberInfo($MemberArr);

	  	$GrpArr 			= 	array();
		$GrpArr['where'] 	= 	array('id',$StyleMemberInfo['usergroup']);

		$GroupStyleInfo = $PowerBB->group->GetGroupInfo($GrpArr);

	  	$GroupStyleInfo['username_style'] = str_ireplace('[username]',$PowerBB->_CONF['template']['MassegeRow']['user_from'],$GroupStyleInfo['username_style']);

       $PowerBB->template->assign('username_sender',$GroupStyleInfo['username_style']);
      ////////
         // replace away msg
		   $PowerBB->_CONF['template']['Info']['away_msg'] = $PowerBB->Powerparse->replace($PowerBB->_CONF['template']['Info']['away_msg']);
           $PowerBB->Powerparse->replace_smiles($PowerBB->_CONF['template']['Info']['away_msg']);

		// feltr away msg

        //

		$send_text = $PowerBB->_CONF['template']['MassegeRow']['text'];

		if (is_numeric($PowerBB->_CONF['template']['Info']['register_date']))
		{
			$PowerBB->_CONF['template']['Info']['register_date'] = $PowerBB->functions->date($PowerBB->_CONF['template']['Info']['register_date']);
		}

		 // Moderator And admin Check for View the Icons Editing and Deletion
		$ModArr 			= 	array();
		$ModArr['where'] 	= 	array('username',$PowerBB->_CONF['member_row']['username']);

		$PowerBB->_CONF['template']['while']['ModeratorsList'] = $PowerBB->moderator->GetModeratorList($ModArr);

		if (is_array($PowerBB->_CONF['template']['while']['ModeratorsList'])
			and sizeof($PowerBB->_CONF['template']['while']['ModeratorsList']) > 0)
		{
			$PowerBB->template->assign('mod_toolbar',0);
		}
		else
		{
			$PowerBB->template->assign('mod_toolbar',1);
		}


		$PowerBB->_CONF['template']['MassegeRow']['title']	=	str_replace($PowerBB->_CONF['template']['lang']['Reply_pm'],'',$PowerBB->_CONF['template']['MassegeRow']['title']);
		$PowerBB->_CONF['template']['MassegeRow']['text'] 	=	$PowerBB->Powerparse->replace($PowerBB->_CONF['template']['MassegeRow']['text']);
        $censorwords = preg_split('#[ \r\n\t]+#', $PowerBB->_CONF['info_row']['censorwords'], -1, PREG_SPLIT_NO_EMPTY);
       $PowerBB->_CONF['template']['MassegeRow']['text'] = str_ireplace($censorwords,'**', $PowerBB->_CONF['template']['MassegeRow']['text']);
       $PowerBB->_CONF['template']['MassegeRow']['text'] = str_replace('&amp;','&',$PowerBB->_CONF['template']['MassegeRow']['text']);
       $PowerBB->_CONF['template']['MassegeRow']['text'] = str_ireplace('{39}',"'",$PowerBB->_CONF['template']['MassegeRow']['text']);
       $PowerBB->_CONF['template']['MassegeRow']['text'] = str_ireplace('cookie','**',$PowerBB->_CONF['template']['MassegeRow']['text']);
       $PowerBB->_CONF['template']['MassegeRow']['text'] = str_replace("\'","'",$PowerBB->_CONF['template']['MassegeRow']['text']);

		$PowerBB->Powerparse->replace_smiles($PowerBB->_CONF['template']['MassegeRow']['text']);

		if (is_numeric($PowerBB->_CONF['template']['MassegeRow']['date']))
		{
			$MassegeDate = $PowerBB->functions->date($PowerBB->_CONF['template']['MassegeRow']['date']);

			$PowerBB->_CONF['template']['MassegeRow']['date'] = $MassegeDate;
		}

		$AttachArr 				= 	array();
		$AttachArr['where'] 	= 	array('pm_id',$PowerBB->_GET['id']);

		// Get the attachment information
		$PowerBB->_CONF['template']['while']['AttachList'] = $PowerBB->attach->GetAttachList($AttachArr);

		if ($PowerBB->_CONF['template']['while']['AttachList'] != false)
		{
			$PowerBB->template->assign('ATTACH_SHOW',true);

			$PowerBB->functions->CleanVariable($PowerBB->_CONF['template']['while']['AttachList'],'html');
		}

		// The writer signture isn't empty
		if (!empty($PowerBB->_CONF['template']['Info']['user_sig']))
		{
			// So , use the PowerCode in it
			$PowerBB->_CONF['template']['Info']['user_sig'] = $PowerBB->Powerparse->replace($PowerBB->_CONF['template']['Info']['user_sig']);
			$PowerBB->Powerparse->replace_smiles($PowerBB->_CONF['template']['Info']['user_sig']);
			$PowerBB->_CONF['template']['show_sig'] = 1;
		}
        $br = '
        ';
		$censorwords = preg_split('#[ \r\n\t]+#', $PowerBB->_CONF['info_row']['censorwords'], -1, PREG_SPLIT_NO_EMPTY);
		$send_text = str_ireplace($censorwords,'**', $send_text);
		$send_text = str_ireplace('{39}',"'",$send_text);
        $send_text = str_ireplace('cookie','**',$send_text);
        $send_text = str_ireplace('<br />',$br,$send_text);

		$PowerBB->template->assign('send_title',$PowerBB->_CONF['template']['lang']['Reply_pm'] . $PowerBB->_CONF['template']['MassegeRow']['title']);
		$PowerBB->template->assign('send_text','[quote]' . $send_text . '[/quote]');
		if ($PowerBB->_CONF['template']['MassegeRow']['folder'] == 'sent')
		{
       		$PowerBB->template->assign('to',$PowerBB->_CONF['template']['MassegeRow']['user_to']);
		}
		else
		{
		$PowerBB->template->assign('to',$PowerBB->_CONF['template']['MassegeRow']['user_from']);
		}
				// feltr pm Text
        $censorwords = preg_split('#[ \r\n\t]+#', $PowerBB->_CONF['info_row']['censorwords'], -1, PREG_SPLIT_NO_EMPTY);
        $PowerBB->_CONF['template']['MassegeRow']['title'] = str_ireplace($censorwords,'**', $PowerBB->_CONF['template']['MassegeRow']['title']);
        $PowerBB->_CONF['template']['MassegeRow']['text'] = str_ireplace($censorwords,'**', $PowerBB->_CONF['template']['MassegeRow']['text']);

        $PowerBB->_CONF['template']['MassegeRow']['text'] = str_ireplace('{39}',"'",$PowerBB->_CONF['template']['MassegeRow']['text']);
        $PowerBB->_CONF['template']['MassegeRow']['text'] = str_ireplace('cookie','**',$PowerBB->_CONF['template']['MassegeRow']['text']);
        $PowerBB->_CONF['template']['MassegeRow']['title'] = str_ireplace('cookie','**',$PowerBB->_CONF['template']['MassegeRow']['title']);
        $PowerBB->_CONF['template']['MassegeRow']['title'] = str_ireplace('{39}',"'",$PowerBB->_CONF['template']['MassegeRow']['title']);


		$PowerBB->functions->GetEditorTools();

		$cache = unserialize(base64_decode($PowerBB->_CONF['member_row']['style_cache']));
        $image_path = $PowerBB->_CONF['rows']['style']['image_path'];

        $MemberArr 			= 	array();
		$MemberArr['where'] 	= 	array('username',$PowerBB->_CONF['template']['MassegeRow']['user_from']);

		$MemberInfo = $PowerBB->member->GetMemberInfo($MemberArr);

		$CheckOnline = ($MemberInfo['logged'] < $PowerBB->_CONF['timeout']) ? false : true;

		($CheckOnline) ? $PowerBB->template->assign('status',"<img title='online' src='$image_path/icon_user_online.gif'>") : $PowerBB->template->assign('status',"<img title='offline' src='$image_path/icon_user_offline.gif'>");

		if (!$PowerBB->_CONF['template']['MassegeRow']['user_read'])
		{
			$ReadArr 						= 	array();
			$ReadArr['where'] 				= 	array();

			$ReadArr['where'][0] 			= 	array();
			$ReadArr['where'][0]['name'] 	= 	'id';
			$ReadArr['where'][0]['oper'] 	= 	'=';
			$ReadArr['where'][0]['value'] 	= 	$PowerBB->_GET['id'];

			$Read = $PowerBB->pm->MakeMassegeRead($ReadArr);

			if ($Read)
			{
				$NumArr 				= 	array();
				$NumArr['username'] 	= 	$PowerBB->_CONF['member_row']['username'];

				$Number = $PowerBB->pm->NewMessageNumber($NumArr);

				$CacheArr 					= 	array();
				$CacheArr['field']			=	array();

				$CacheArr['field']['unread_pm'] 	= 	$Number;
				$CacheArr['where'] 					= 	array('username',$PowerBB->_CONF['member_row']['username']);

				$Cache = $PowerBB->member->UpdateMember($CacheArr);
			}
		}

		////////////////////
		$ExArr 						= 	array();
		$ExArr['order']				=	array();
		$ExArr['order']['field']	=	'id';
		$ExArr['order']['type']		=	'DESC';
		$ExArr['proc'] 				= 	array();
		$ExArr['proc']['*'] 		= 	array('method'=>'clean','param'=>'html');

		$PowerBB->_CONF['template']['while']['ExList'] = $PowerBB->extension->GetExtensionList($ExArr);

		//get user rating
		$RatingArr 							= 	array();
		$RatingArr['proc'] 					= 	array();
		$RatingArr['proc']['*'] 			= 	array('method'=>'clean','param'=>'html');

		$RatingArr['where']					=	array();
		$RatingArr['where'][0]				=	array();
		$RatingArr['where'][0]['name']		=	'posts';
		$RatingArr['where'][0]['oper']		=	'>';
		$RatingArr['where'][0]['value']		=	$PowerBB->_CONF['template']['Info']['posts'];

		$RatingInfo = $PowerBB->userrating->GetUserRatingInfo($RatingArr);
		$PowerBB->template->assign('RatingInfo',$RatingInfo);

		//get user title
		$UsertitleArr 							= 	array();
		$UsertitleArr['proc'] 					= 	array();
		$UsertitleArr['proc']['*'] 			= 	array('method'=>'clean','param'=>'html');

		$UsertitleArr['where']					=	array();
		$UsertitleArr['where'][0]				=	array();
		$UsertitleArr['where'][0]['name']		=	'posts';
		$UsertitleArr['where'][0]['oper']		=	'>';
		$UsertitleArr['where'][0]['value']		=	$PowerBB->_CONF['template']['Info']['posts'];

		$UsertitleInfo = $PowerBB->usertitle->GetUsertitleInfo($UsertitleArr);
		$PowerBB->template->assign('Usertitle',$UsertitleInfo['usertitle']);
		$GrpArr 			= 	array();
		$GrpArr['where'] 	= 	array('id',$PowerBB->_CONF['template']['Info']['usergroup']);

		$GroupInfo = $PowerBB->group->GetGroupInfo($GrpArr);
        $PowerBB->template->assign('GroupInfo',$GroupInfo);

		if ($PowerBB->_GET['page'] == 'pm_show')
		{
			$PowerBB->template->assign('address_bar_pm_send_SHOW',true);
		}

        // show Custom_bbcode List
		$Custom_bbcodeArr 					= 	array();
		$Custom_bbcodeArr['order']			=	array();
		$Custom_bbcodeArr['order']['field']	=	'id';
		$Custom_bbcodeArr['order']['type']	=	'DESC';
		$Custom_bbcodeArr['proc'] 			= 	array();
		$Custom_bbcodeArr['proc']['*'] 		= 	array('method'=>'clean','param'=>'html');

		$PowerBB->_CONF['template']['while']['Custom_bbcodesList'] = $PowerBB->custom_bbcode->GetCustom_bbcodeList($Custom_bbcodeArr);



		$PowerBB->template->display('pm_show');
	}
}

?>
