<?php

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

$CALL_SYSTEM					=	array();
$CALL_SYSTEM['SUBJECT'] 		= 	true;
$CALL_SYSTEM['SECTION'] 		= 	true;
$CALL_SYSTEM['TOOLBOX'] 		= 	true;
$CALL_SYSTEM['ICONS'] 			= 	true;

define('JAVASCRIPT_PowerCode',true);

include('common.php');

define('CLASS_NAME','PowerBBChatMOD');

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

		/** Go to Chat site **/
		if ($PowerBB->_GET['chat'])
		{
			$this->_AddchatMessage();
		}
		elseif ($PowerBB->_GET['start'])
		{
			$this->_StartchatMessage();
		}

	}

	/**
	 * add chat message
	 */
	function _AddchatMessage()
	{
		global $PowerBB;


		/** member can't use the chat system if his posts was less than 20 posts **/

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

		$member = $PowerBB->member->GetMemberInfo($MemberArr);
		if ($member['posts'] < $PowerBB->_CONF['info_row']['chat_num_mem_posts'])
		{
          $PowerBB->template->assign('num_mem_posts',true);
		}
		if ($PowerBB->_CONF['group_info']['banned'] == 1)
		{
			// Stop the page with small massege
            $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['suspended_member']);
        }

		$SmlArr 					= 	array();
		$SmlArr['order'] 			=	array();
		$SmlArr['order']['field']	=	'id';
		$SmlArr['order']['type']	=	'ASC';
		$SmlArr['limit']			=	$PowerBB->_CONF['info_row']['smiles_nm'];
		$SmlArr['proc'] 			= 	array();
		$SmlArr['proc']['*'] 		= 	array('method'=>'clean','param'=>'html');

		$PowerBB->_CONF['template']['while']['SmileRows'] = $PowerBB->icon->GetSmileList($SmlArr);

        $PowerBB->template->display('add_chat_message');

	}

	function _StartchatMessage()
	{

		global $PowerBB;

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

		/** member can't use the chat system if his posts was less than 20 posts **/
		$MemberArr 				= 	array();
		$MemberArr['where']		=	array('username',$PowerBB->_CONF['member_row']['username']);

		$member = $PowerBB->member->GetMemberInfo($MemberArr);
		if ($member['posts'] < $PowerBB->_CONF['info_row']['chat_num_mem_posts'])
		{
         $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Member_can_not_use_the_chat_system_posts_less']);

		}


			if (empty($PowerBB->_POST['text']))
			{
				$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Please_write_the_message']);
			}

			if ($PowerBB->_CONF['info_row']['chat_hide_country'] == 1)
			{

			if (empty($PowerBB->_POST['country']))
			{
	         $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Please_write_the_country']);
			}
			}


            $PowerBB->Powerparse->replace_smiles($PowerBB->_POST['text']);
            $PowerBB->_POST['text'] = str_ireplace('{39}',"'",$PowerBB->_POST['text']);
            $PowerBB->_POST['text'] = str_ireplace('cookie','',$PowerBB->_POST['text']);


             // Filter Words
	        $censorwords = preg_split('#[ \r\n\t]+#', $PowerBB->_CONF['info_row']['censorwords'], -1, PREG_SPLIT_NO_EMPTY);
            $PowerBB->_POST['country'] = str_ireplace($censorwords,'', $PowerBB->_POST['country']);
            $PowerBB->_POST['text'] = str_ireplace($censorwords,'', $PowerBB->_POST['text']);
            $PowerBB->_POST['text'] = str_ireplace('&amp;','&',$PowerBB->_POST['text']);
            $PowerBB->_POST['text'] = str_ireplace('<br>','',$PowerBB->_POST['text']);
            $PowerBB->_POST['text'] = str_ireplace('</p>','',$PowerBB->_POST['text']);
            $PowerBB->_POST['text'] = str_ireplace('<p>','',$PowerBB->_POST['text']);
            $PowerBB->_POST['text'] = str_ireplace('XSS','',$PowerBB->_POST['text']);
            $PowerBB->_POST['text'] = str_ireplace('write','',$PowerBB->_POST['text']);
            $PowerBB->_POST['text'] = str_ireplace('document','',$PowerBB->_POST['text']);
            $PowerBB->_POST['text'] = str_ireplace('&quot;','',$PowerBB->_POST['text']);
            $PowerBB->_POST['country'] = str_ireplace('&amp;','&',$PowerBB->_POST['country']);
            $PowerBB->_POST['country'] = str_ireplace('<br>','',$PowerBB->_POST['country']);
            $PowerBB->_POST['country'] = str_ireplace('</p>','',$PowerBB->_POST['country']);
            $PowerBB->_POST['country'] = str_ireplace('<p>','',$PowerBB->_POST['country']);
            $PowerBB->_POST['country'] = str_ireplace('&quot;','',$PowerBB->_POST['country']);
            $PowerBB->_POST['country'] = str_ireplace('http://','',$PowerBB->_POST['country']);
            $PowerBB->_POST['country'] = str_ireplace('www','',$PowerBB->_POST['country']);
            $PowerBB->_POST['country'] = str_ireplace('com','',$PowerBB->_POST['country']);
            $PowerBB->_POST['country'] = str_ireplace('net','',$PowerBB->_POST['country']);
            $PowerBB->_POST['country'] = str_ireplace('org;','',$PowerBB->_POST['country']);
            $PowerBB->_POST['country'] = str_ireplace('iframe;','',$PowerBB->_POST['country']);
            $PowerBB->_POST['text'] = str_ireplace('iframe;','',$PowerBB->_POST['text']);
            //
        	   $PowerBB->_POST['country'] = 	$PowerBB->functions->CleanVariable($PowerBB->_POST['country'],'html');
               //$PowerBB->_POST['text'] =  $PowerBB->functions->CleanVariable($PowerBB->_POST['text'],'html');
		       // Kill SQL Injection
		        $PowerBB->_POST['country'] = $PowerBB->functions->CleanVariable($PowerBB->_POST['country'],'sql');
		        $PowerBB->_POST['text'] = $PowerBB->functions->CleanVariable($PowerBB->_POST['text'],'sql');

            $TextPost = utf8_decode($PowerBB->_POST['text']);
    		if (isset($TextPost{$PowerBB->_CONF['info_row']['chat_num_characters']}))
    		{
                 $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['message_large_number_of_characters']);
             }

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

			$ChatArr['field']['country'] 	    = 	$PowerBB->functions->CleanVariable($PowerBB->_POST['country'],'html');
			$ChatArr['field']['message'] 		= 	$PowerBB->_POST['text'];
			$ChatArr['field']['username'] 		= 	$PowerBB->_CONF['member_row']['username'];
			$ChatArr['field']['user_id'] 		= 	$PowerBB->_CONF['member_row']['id'];

			$insert = $PowerBB->chat->InsertChat($ChatArr);


		$TotleCahtArr 					= 	array();
		$TotleCahtArr['order']			=	array();
		$TotleCahtArr['order']['field']	=	'id';
		$TotleCahtArr['order']['type']	=	'DESC';
       if ($PowerBB->chat->GetChatNumber($TotleCahtArr) > $PowerBB->_CONF['info_row']['chat_message_num'])
        {
			$LastChatArr 						= 	array();
			$LastChatArr['order'] 				= 	array();
			$LastChatArr['order']['field'] 		= 	'id';
			$LastChatArr['order']['type']	 	= 	' ASC';
			$LastChatArr['limit'] 				= 	'0,1';

			$PowerBB->_CONF['template']['LastChat'] = $PowerBB->chat->GetChatInfo($LastChatArr);

			$DelArr 			= 	array();
			$DelArr['where'] 	= 	array('id',$PowerBB->_CONF['template']['LastChat']['id']);

			$del = $PowerBB->chat->DeleteChat($DelArr);

		}


			if ($insert)
			{
            echo ('<SCRIPT LANGUAGE="JavaScript">window.opener.location="index.php";window.close(true)</script>');

			}



	}

}

?>
