<?php
session_start();
(!defined('IN_PowerBB')) ? die() : '';
include('common.php');
define('CLASS_NAME','PowerBBTopicAddMOD');
class PowerBBTopicAddMOD
{
	var $SectionInfo;
	var $SectionGroup;

	function run()
	{
		global $PowerBB;

		$this->_CommonCode();

		if ($PowerBB->_GET['index'])
		{
			$this->_Index();
		}
		elseif ($PowerBB->_GET['start'])
		{
			$this->_Start();
		}
		else
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
		}

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


	function _CommonCode()
	{
		global $PowerBB;

		//////////

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

		if (empty($PowerBB->_GET['id']))
		{
			$PowerBB->functions->ShowHeader($PowerBB->_CONF['template']['lang']['Mistake']);

			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
		}

		//////////

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

		$this->SectionInfo = $PowerBB->section->GetSectionInfo($SecArr);

		if (!$this->SectionInfo)
		{
			$PowerBB->functions->ShowHeader($PowerBB->_CONF['template']['lang']['Mistake']);

			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Section_does_not_exist']);
		}

		// Kill XSS
		$PowerBB->functions->CleanVariable($this->SectionInfo,'html');
		// Kill SQL Injection
		$PowerBB->functions->CleanVariable($this->SectionInfo,'sql');

		//////////

		/** Get section's group information and make some checks **/
		$SecGroupArr 						= 	array();
		$SecGroupArr['where'] 				= 	array();

		$SecGroupArr['where'][0]			=	array();
		$SecGroupArr['where'][0]['name'] 	= 	'section_id';
		$SecGroupArr['where'][0]['oper']	=	'=';
		$SecGroupArr['where'][0]['value'] 	= 	$this->SectionInfo['id'];

		$SecGroupArr['where'][1]			=	array();
		$SecGroupArr['where'][1]['con']		=	'AND';
		$SecGroupArr['where'][1]['name']	=	'group_id';
		$SecGroupArr['where'][1]['oper']	=	'=';
		$SecGroupArr['where'][1]['value']	=	$PowerBB->_CONF['group_info']['id'];

		// Finally get the permissions of group
		$this->SectionGroup = $PowerBB->group->GetSectionGroupInfo($SecGroupArr);

		//////////

		// The visitor can't show this section , so stop the page
		if (!$this->SectionGroup['view_section']
			or !$this->SectionGroup['write_subject'])
		{
			$PowerBB->functions->ShowHeader($PowerBB->_CONF['template']['lang']['Mistake']);
          if (!$PowerBB->_CONF['member_permission'])
              {
              $PowerBB->template->display('login');
              $PowerBB->functions->error_stop();
			}
	        else
	        {
	        $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Sorry_you_can_not_write_in_this_section']);
	        }
	     }

		if ($PowerBB->_CONF['group_info']['view_subject'] == 0)
		{
          if (!$PowerBB->_CONF['member_permission'])
              {
              $PowerBB->template->display('login');
              $PowerBB->functions->error_stop();
			}
	        else
	        {
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Sorry_you_can_not_write_in_this_section']);
	        }
		}

		if (!empty($this->SectionInfo['section_password'])
			and !$PowerBB->_CONF['group_info']['admincp_allow'])
		{
			// The visitor don't give me password , so require it
     		if (empty($PowerBB->_GET['password']))
        	{
               $PowerBB->functions->ShowHeader($PowerBB->_CONF['template']['lang']['password_forum']);
      			$PowerBB->template->display('forum_password');
      			$PowerBB->functions->stop();
     		}
     		// The visitor give me password , so check
     		elseif (!empty($PowerBB->_GET['password']))
     		{
     			$PassArr = array();

     			// Section id
     			$PassArr['id'] 	= $this->SectionInfo['id'];

     			// The password to check
     			$PassArr['password'] = base64_decode($PowerBB->_GET['password']);

     			$IsTruePassword = $PowerBB->section->CheckPassword($PassArr);

     			// Stop ! it's don't true password
     			if (!$IsTruePassword)
     			{
     				$PowerBB->functions->ShowHeader($PowerBB->_CONF['template']['lang']['Mistake']);

     				$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['PasswordIsnotTrue']);
     			}

     			$PowerBB->_CONF['template']['password'] = '&amp;password=' . $PowerBB->_GET['password'];
     		}
     	}

     	//////////

     	$PowerBB->template->assign('section_info',$this->SectionInfo);

     	//////////
	}

	function _preview()
	{
		global $PowerBB;

		$PowerBB->functions->GetEditorTools();

     	$PowerBB->template->assign('id',$PowerBB->_GET['id']);

     	// Instead of send a whole version of $this->SectionGroup to template engine
     	// We just send options which we really need, we use this way to save memory
     	$PowerBB->template->assign('upload_attach',$this->SectionGroup['upload_attach']);

		////////

		$Admin = $PowerBB->functions->ModeratorCheck($PowerBB->_GET['id']);

		$PowerBB->template->assign('Admin',$Admin);

		////////
        $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);

        $PowerBB->template->assign('SectionGroup',$this->SectionGroup);
        // 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('new_topic');
	}

	function _Index()
	{
		global $PowerBB;

		$PowerBB->functions->ShowHeader();

		$PowerBB->functions->GetEditorTools();

     	$PowerBB->template->assign('id',$PowerBB->_GET['id']);



     	// Instead of send a whole version of $this->SectionGroup to template engine
     	// We just send options which we really need, we use this way to save memory
     	$PowerBB->template->assign('upload_attach',$this->SectionGroup['upload_attach']);

		////////

		$Admin = $PowerBB->functions->ModeratorCheck($PowerBB->_GET['id']);

		$PowerBB->template->assign('Admin',$Admin);

		////////
        $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);
         $PowerBB->template->assign('SectionGroup',$this->SectionGroup);
		$SecInfoArr 			= 	array();
		$SecInfoArr['where'] 	= 	array('id',$PowerBB->_GET['id']);

		$section_info = $PowerBB->section->GetSectionInfo($SecInfoArr);
		if ($section_info['parent'] == '0')
        {
       	$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['no_newthread_in_section_main']);
	    }

	    $checked 			= 	'no_icon';
	    $PowerBB->template->assign('checked',$checked);

        // 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('new_topic');

	}

	function _empty_bac()
	{
		global $PowerBB;

		$PowerBB->functions->GetEditorTools();

     	$PowerBB->template->assign('id',$PowerBB->_GET['id']);



     	// Instead of send a whole version of $this->SectionGroup to template engine
     	// We just send options which we really need, we use this way to save memory
     	$PowerBB->template->assign('upload_attach',$this->SectionGroup['upload_attach']);

		////////

		$Admin = $PowerBB->functions->ModeratorCheck($PowerBB->_GET['id']);

		$PowerBB->template->assign('Admin',$Admin);

		////////
        $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);
         $PowerBB->template->assign('SectionGroup',$this->SectionGroup);
          $previewtext = $PowerBB->_POST['text'];
          $previewtext = $PowerBB->Powerparse->replace($previewtext);
          $previewtext = str_replace("\'","'",$previewtext);
          $PowerBB->Powerparse->replace_smiles($previewtext);
          $PowerBB->template->assign('preview',$previewtext);
          $PowerBB->_POST['text'] = str_replace("\'","'",$PowerBB->_POST['text']);
          $PowerBB->template->assign('prev',$PowerBB->_POST['text']);
          $PowerBB->template->assign('title_prev',$PowerBB->_POST['title']);
          $PowerBB->template->assign('describe_prev',$PowerBB->_POST['describe']);
          $PowerBB->template->assign('prefix_subject_prev',$PowerBB->_POST['prefix_subject']);
     	$PowerBB->template->display('new_topic');
	}


	function _Start()
	{
		global $PowerBB;

		$PowerBB->_POST['title'] 	= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['title'],'trim');
		$PowerBB->_POST['text'] 	= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['text'],'trim');

		// Kill SQL Injection
		$PowerBB->_POST['title'] 	= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['title'],'sql');
        $PowerBB->_POST['text'] 	= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['text'],'sql');
        $PowerBB->_POST['describe'] 	= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['describe'],'sql');

	   if ($PowerBB->_POST['preview'])
       {
    	    $PowerBB->functions->ShowHeader($PowerBB->_CONF['template']['lang']['add_new_topic']);
            $previewtext = $PowerBB->_POST['text'];
            $previewtext = $PowerBB->Powerparse->replace($previewtext);
           $PowerBB->template->assign('preview',stripslashes($previewtext));

            $PowerBB->Powerparse->replace_smiles($previewtext);
            $PowerBB->template->assign('prev',stripslashes($PowerBB->_POST['text']));
            $PowerBB->template->assign('title_prev',stripslashes($PowerBB->_POST['title']));
            $PowerBB->template->assign('describe_prev',stripslashes($PowerBB->_POST['describe']));
            $PowerBB->template->assign('view_preview',stripslashes($PowerBB->_POST['text']));
            $PowerBB->template->assign('prefix_subject_prev',$PowerBB->_POST['prefix_subject']);

			$this->_preview();


        }
       else
        {

                   $PowerBB->functions->ShowHeader($PowerBB->_CONF['template']['lang']['add_new_topic']);

				if (empty($PowerBB->_POST['title']))
				{
                    $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['no_title']);
                      $this->_empty_bac();
					$PowerBB->functions->error_stop();

				}

				if (empty($PowerBB->_POST['text']))
				{
					$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['no_text']);
				}
				$writer = $PowerBB->_CONF['member_row']['username'];
                $last_subject_write_time = $PowerBB->DB->sql_query("SELECT  *   FROM " . $PowerBB->table['subject'] . " WHERE writer= '$writer' ORDER BY id desc");
                $last_write_time = $PowerBB->DB->sql_fetch_array($last_subject_write_time);
	            if ((time() - $PowerBB->_CONF['info_row']['floodctrl']) <= $last_write_time['native_write_time'])
	            {
				$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['floodctrl_subjects'],$stop,$stop);
                     $this->_empty_bac();
				$PowerBB->functions->error_stop();
				}

				if ($last_write_time['title'] == $PowerBB->_POST['title'])
				{
   			        $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['duplicatethread']);
		     	    $PowerBB->functions->redirect('index.php?page=forum&amp;show=1&amp;id=' . $this->SectionInfo['id'] . $PowerBB->_CONF['template']['password'],'8');
		     		$PowerBB->functions->error_stop();
				}

			if ($PowerBB->_POST['poll'])
	    		{

                   // Filter Words
                   $PowerBB->_POST['question'] = $PowerBB->functions->CleanVariable($PowerBB->_POST['question'],'html');
                   $PowerBB->_POST['question'] = $PowerBB->functions->CleanVariable($PowerBB->_POST['question'],'sql');

	  		   if (empty($PowerBB->_POST['question']))
				{
					$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['fill_in_question']);
	                 $PowerBB->template->assign('question',$PowerBB->_POST['question']);
	                    $this->_empty_bac();
						$PowerBB->functions->error_stop();
				 }

	    			 $Answer = $PowerBB->_POST['answer'];
			      foreach ($Answer as $Answer_x)
			      {

                   // Filter Answer Words
                   $Answer_x = $PowerBB->functions->CleanVariable($Answer_x,'sql');
                   $Answer_x = $PowerBB->functions->CleanVariable($Answer_x,'html');

                   $SubjectInfov['title'] = $PowerBB->Powerparse->censor_words($SubjectInfov['title']);

		      		if (empty($Answer_x))
					{
						$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['fill_in_answer']);
	                    $PowerBB->template->assign('question',$PowerBB->_POST['question']);
		                $this->_empty_bac();
						$PowerBB->functions->error_stop();
					}

			      }


			}

			if ($PowerBB->_POST['tag'])
	    		{

	    			 $Tags = $PowerBB->_POST['tags'];
			      foreach ($Tags as $Tags_x)
			      {

		      		if (empty($Tags_x))
					{
						$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['fill_in_tags']);
		                $this->_empty_bac();
						$PowerBB->functions->error_stop();
					}

			      }


			}


					/*$IsFlood = $PowerBB->subject->IsFlood(array('last_time'=>$PowerBB->_CONF['member_row']['lastpost_time']));

					if ($IsFlood)
					{
						$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['floodctrl_subjects']);
					}*/
                    $TitlePost = utf8_decode($PowerBB->_POST['title']);
		     		if (isset($TitlePost{$PowerBB->_CONF['info_row']['post_title_max']}))
		     		{
                        $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['post_text_max_subjects']);
                         $this->_empty_bac();
		       			$PowerBB->functions->error_stop();
		    		}

		        	if  (!isset($TitlePost{$PowerBB->_CONF['info_row']['post_title_min']}))
		     		{
                        $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['post_text_min_subjects']);
                         $this->_empty_bac();
		      			$PowerBB->functions->error_stop();
		     		}

				if (!$PowerBB->_CONF['group_info']['admincp_allow'])
				{
                    $TextPost = utf8_decode($PowerBB->_POST['text']);
                    $TextPost = preg_replace('#\[IMG\](.*)\[/IMG\]#siUe', '', $TextPost);
		       	 	if (isset($TextPost{$PowerBB->_CONF['info_row']['post_text_max']}))
		     		{
                        $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['post_text_max']);
                         $this->_empty_bac();
		      			$PowerBB->functions->error_stop();
		     		}

		     		if (!isset($TextPost{$PowerBB->_CONF['info_row']['post_text_min']}))
		     		{
		     			 $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['post_text_min']);
                         $this->_empty_bac();
		      			$PowerBB->functions->error_stop();
		     		}

		     		if (!$PowerBB->_CONF['member_permission'])
		            {
			     		if(md5($PowerBB->_POST['code']) != $_SESSION['key'])
				        {
		                $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Code_that_you_enter_the_wrong'],$stop,$stop);
			            }

			           if (empty($PowerBB->_POST['guest_name']))
						{
		     				   $PowerBB->functions->ShowHeader($PowerBB->_CONF['template']['lang']['Mistake']);
		                       $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_do_not_type_your_name']);
						}
		            }

		     	}
                   // Filter Words
                   $PowerBB->_POST['text'] = str_replace('target="_blank" ','',$PowerBB->_POST['text']);
                   //


		$SecInfoArr 			= 	array();
		$SecInfoArr['where'] 	= 	array('id',$this->SectionInfo['id']);

		$section_info = $PowerBB->section->GetSectionInfo($SecInfoArr);

		     	$SubjectArr 								= 	array();
		     	$SubjectArr['get_id']						=	true;
		     	$SubjectArr['field']						=	array();
		     	$SubjectArr['field']['title'] 				= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['title'],'html');
		     	if (!$PowerBB->_CONF['member_permission'])
				{
		     	$SubjectArr['field']['text'] 			= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['text'].'[color=#4000BF][i][guest_name]'.$PowerBB->_CONF['template']['lang']['LastsPostsWriter'].$PowerBB->_POST['guest_name'].'[/guest_name][/i][/color]','html');
				$SubjectArr['field']['writer'] 				= 	$PowerBB->_CONF['template']['lang']['Guestp'];
				}
		     	else
		     	{
		     	$SubjectArr['field']['text'] 				= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['text'],'html');
				$SubjectArr['field']['writer'] 				= 	$PowerBB->_CONF['rows']['member_row']['username'];
				}
		     	$SubjectArr['field']['section'] 			= 	$this->SectionInfo['id'];
		     	$SubjectArr['field']['write_time'] 			= 	$PowerBB->_CONF['now'];
		     	$SubjectArr['field']['icon'] 				= 	$PowerBB->_POST['icon'];
		     	$SubjectArr['field']['subject_describe'] 	= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['describe'],'html');
		     	$SubjectArr['field']['native_write_time'] 	= 	$PowerBB->_CONF['now'];
		     	$SubjectArr['field']['sec_subject'] 		= 	$section_info['sec_section'];
		     	$SubjectArr['field']['prefix_subject'] 		= 	$PowerBB->_POST['prefix_subject'];
		     	if ($PowerBB->_POST['poll'])
		     	{
		     	$SubjectArr['field']['poll_subject'] 		= 	1;
		     	}
		     	$SubjectArr['field']['attach_subject'] 		= 	0;
		     	$SubjectArr['field']['tags_cache']			=	$PowerBB->functions->CleanVariable($PowerBB->_POST['tags'],'sql');

		     	if (($PowerBB->_CONF['member_row']['review_subject'] or $PowerBB->_CONF['group_info']['review_subject'] or $this->SectionInfo['review_subject'])
					and !$PowerBB->_CONF['group_info']['admincp_allow'])
				{
					$SubjectArr['field']['review_subject'] = 1;
				}


		     	if ($PowerBB->_POST['stick'])
		     	{
		     		$SubjectArr['field']['stick'] = 1;
		     	}

		     	if ($PowerBB->_POST['close'])
		     	{
		     		$SubjectArr['field']['close'] = 1;
		     	}

		     	$Insert = $PowerBB->subject->InsertSubject($SubjectArr);


		     	if ($Insert)
		     	{
		     		//////////

		     		if ($PowerBB->_POST['poll'])
		     		{

		     			if (isset($PowerBB->_POST['question'])
		     				and isset($PowerBB->_POST['answer'][0])
		     				and isset($PowerBB->_POST['answer'][1]))
		     			{
		     				$answers_number = 2;

		     				if ($PowerBB->_POST['poll_answers_count'] > 0)
		     				{
		     					$answers_number = $PowerBB->_POST['poll_answers_count'];
		     				}

		     				$answers = array();

		     				$x = 0;

		     				while ($x < $answers_number)
		     				{
		     					// The text of the answer
		     					$answers[$x][0] = $PowerBB->_POST['answer'][$x];
								$PowerBB->functions->CleanVariable($PowerBB->_POST['answer'][$x],'html');
								$PowerBB->functions->CleanVariable($PowerBB->_POST['answer'][$x],'sql');
								$PowerBB->_POST['answer'][$x] = str_replace('SCRIPT','',$PowerBB->_POST['answer'][$x]);
								$PowerBB->_POST['answer'][$x] = $PowerBB->functions->CleanVariable($PowerBB->_POST['answer'][$x],'sql');


		     					// The result
		     					$answers[$x][1] = 0;

		     					$x += 1;
		     				}

		     				$PollArr 				= 	array();
		     				$PollArr['field']	=	array();
		     				$PollArr['field']['qus'] 	    = 	$PowerBB->functions->CleanVariable($PowerBB->_POST['question'],'html');
		     				$PollArr['field']['answers'] 	= 	$PowerBB->_POST['answer'];
		     				$PollArr['field']['subject_id']	=	$PowerBB->subject->id;

		     				$InsertPoll = $PowerBB->poll->InsertPoll($PollArr);
		     			}
		     		}


				    //////////

		     		// Set tags for the subject
                       //$PowerBB->_POST['tags'] 	= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['tags'],'sql');
		     		$tags_size = sizeof($PowerBB->_POST['tags']);

		     		if ($tags_size > 0
		     			and strlen($PowerBB->_POST['tags'][0]) > 0)
		     		{
		     			foreach ($PowerBB->_POST['tags'] as $tag)
		     			{
                       $tag 	= 	$PowerBB->functions->CleanVariable($tag,'sql');

		     				$CheckArr 			= 	array();
		     				$CheckArr['where'] 	= 	array('tag',$tag);

		     				$tag_id = 1;

		     				$Tag = $PowerBB->tag->GetTagInfo($CheckArr);

		     				if (!$Tag)
		     				{
		     					$InsertArr 					=	array();
		     					$InsertArr['field']			=	array();
		     					$InsertArr['field']['tag']	=	$tag;
		     					$InsertArr['get_id']		=	true;

		     					$insert = $PowerBB->tag->InsertTag($InsertArr);

		     					$tag_id = $PowerBB->tag->id;

		     					unset($InsertArr);
		     				}
		     				else
		     				{
		     					$UpdateArr 			= 	array();
		     					$UpdateArr['field']	=	array();

		     					$UpdateArr['field']['number'] 	= 	$Tag['num'] + 1;
		     					$UpdateArr['where']				=	array('id',$Tag['id']);

		     					$update = $PowerBB->tag->UpdateTag($UpdateArr);

		     					$tag_id = $Tag['id'];
		     				}

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

		     				$InsertArr['field']['tag_id'] 			= 	$tag_id;
		     				$InsertArr['field']['subject_id'] 		=	$PowerBB->subject->id;
		     				$InsertArr['field']['tag'] 				= 	$PowerBB->functions->CleanVariable($tag,'html');
		     				$InsertArr['field']['subject_title'] 	= 	$PowerBB->_POST['title'];

		     				// Note, this function is from tag system not subject system
		     				$insert = $PowerBB->tag->InsertSubject($InsertArr);
		     			}
		     		}

                    if ($PowerBB->_CONF['info_row']['add_tags_automatic'] == '1')
		     		{
                        //add tags Automatic from subject title
						$excludedWords = array();
						$doubleAr = $PowerBB->_POST['title'];
						$censorwords = preg_split('/\s+/s', $doubleAr, -1, PREG_SPLIT_NO_EMPTY);
						$excludedWords = array_merge($excludedWords, $censorwords);
						unset($censorwords);

						// Trim current exclusions
						for ($x = 0; $x < count($excludedWords); $x++)
						{
						$excludedWords[$x] = trim($excludedWords[$x]);
						if (@strlen($excludedWords[$x]) > 3)
						{
						$Insert2Arr 					=	array();
						$Insert2Arr['field']			=	array();
						$Insert2Arr['field']['tag']	=	$excludedWords[$x];
						$Insert2Arr['get_id']		=	true;

						$insert2 = $PowerBB->tag->InsertTag($Insert2Arr);

						$tag_id = $PowerBB->tag->id;

						unset($InsertArr);
                        $excludedWords[$x] 	= 	$excludedWords[$x];

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

				     	$InsertArr['field']['tag_id'] 			= 	$tag_id;
						$InsertArr['field']['subject_id'] 		=	$PowerBB->subject->id;
						$InsertArr['field']['tag'] 				= 	$PowerBB->functions->CleanVariable($excludedWords[$x],'html');
						$InsertArr['field']['subject_title'] 	= 	$PowerBB->_POST['title'];
						// Note, this function is from tag system not subject system
						$insert = $PowerBB->tag->InsertSubject($InsertArr);
                       }
						}
                    }
					//////////

		     		// Upload files

				   $GetAttachArr 					= 	array();
				   $GetAttachArr['where'] 			= 	array('subject_id','-'.$PowerBB->_CONF['member_row']['id']);
				   $Attachinfo = $PowerBB->attach->GetAttachInfo($GetAttachArr);

		     		if ($Attachinfo)
		     		{

						$SubjectArr 							= 	array();
						$SubjectArr['field'] 					= 	array();
						$SubjectArr['field']['attach_subject'] 	= 	'1';
						$SubjectArr['where'] 					= 	array('id',$PowerBB->subject->id);

						$update = $PowerBB->subject->UpdateSubject($SubjectArr);

					//	Update All Attach
					 $member_id_Attach = '-'.$PowerBB->_CONF['member_row']['id'];
                     $getAttach = $PowerBB->DB->sql_query("SELECT  *   FROM " . $PowerBB->table['attach'] . " WHERE subject_id = '$member_id_Attach' ");
                     while ($getAttach_row = $PowerBB->DB->sql_fetch_array($getAttach))
                      {
						// Count a new download
						$UpdateArr 						= 	array();
						$UpdateArr['field'] 			= 	array();
						$UpdateArr['field']['subject_id'] 	= 	$PowerBB->subject->id;
						$UpdateArr['where'] 			= 	array('id',$getAttach_row['id']);

                 		$update = $PowerBB->attach->UpdateAttach($UpdateArr);
                     }
		     		}

		     		//////////

		     		if ($this->SectionGroup['no_posts'])
		     		{
		     			$posts = $PowerBB->_CONF['member_row']['posts'] + 1;
		     		}
		     		else
		     		{
		     			$posts = $PowerBB->_CONF['member_row']['posts'];
		     		}

		     		if ($PowerBB->_CONF['group_info']['usertitle_change'])
		     		{
		    	 		$UsertitleArr 			= 	array();
		     			$UsertitleArr['where'] 	= 	array('posts',$posts);

		     			$UserTitle = $PowerBB->usertitle->GetUsertitleInfo($UsertitleArr);

		     			if ($UserTitle != false)
		     			{
		     				$usertitle = $UserTitle['usertitle'];
		     			}
		     		}

		     		//////////

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

		     		$MemberArr['field']['posts']			=	$posts;
		     		$MemberArr['field']['lastpost_time'] 	=	$PowerBB->_CONF['now'];
		     		$MemberArr['field']['user_title']		=	(isset($usertitle)) ? $usertitle : null;
		     		$MemberArr['where']						=	array('id',$PowerBB->_CONF['member_row']['id']);

		   			$UpdateMember = $PowerBB->member->UpdateMember($MemberArr);


		     		// The overall number of subjects
		     		$UpdateSubjectNumber = $PowerBB->cache->UpdateSubjectNumber(array('subject_num'	=>	$PowerBB->_CONF['info_row']['subject_number']));

		     		//////////
		     		if (!$PowerBB->_CONF['member_permission'])
					{
	                $writer = 	$PowerBB->_CONF['template']['lang']['Guestp'];
					}
			     	else
			     	{
		            $writer = 	$PowerBB->_CONF['member_row']['username'];
					}

		     	if (($PowerBB->_CONF['member_row']['review_subject'] or $PowerBB->_CONF['group_info']['review_subject'] or $this->SectionInfo['review_subject'])
					and !$PowerBB->_CONF['group_info']['admincp_allow'])
				{
					$review_subject = 1;
				}


		     		// The number of section's subjects number
		     		$UpdateArr 					= 	array();
		     		$UpdateArr['field']			=	array();

		     		$UpdateArr['field']['subject_num'] 	= 	$this->SectionInfo['subject_num'] + 1;
					$UpdateArr['field']['last_writer'] 		= 	$writer;
		     		$UpdateArr['field']['last_subject'] 		= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['title'],'html');
		     		$UpdateArr['field']['last_subjectid'] 	= 	$PowerBB->subject->id;
		     		$UpdateArr['field']['last_date'] 	= 	$PowerBB->_CONF['now'];
		     		$UpdateArr['field']['last_time'] 	= 	$PowerBB->_CONF['now'];
		     		$UpdateArr['field']['icon'] 		= 	$PowerBB->_POST['icon'];
		     		$UpdateArr['field']['last_berpage_nm'] 		= 	'0';
		     		$UpdateArr['where']					= 	array('id',$this->SectionInfo['id']);

		     		$UpdateSubjectNumber = $PowerBB->section->UpdateSection($UpdateArr);

		     		// Free memory
		     		unset($UpdateArr);

		     		//////////

		     		// Update section's cache
	           $UpdateSectionCache = $PowerBB->functions->UpdateSectionCache($this->SectionInfo['id']);

		     		//////////

                    $get_section_parent = $PowerBB->DB->sql_query("SELECT  *   FROM " . $PowerBB->table['section'] . " WHERE parent = " . $this->SectionInfo['parent'] . " ");
	                $Inf_row = $PowerBB->DB->sql_fetch_array($get_section_parent);
                        if ($Inf_row)
						{

	                           //////////
				     		if (!$PowerBB->_CONF['member_permission'])
							{
			                $writer = 	$PowerBB->_CONF['template']['lang']['Guestp'];
							}
					     	else
					     	{
				            $writer = 	$PowerBB->_CONF['member_row']['username'];
							}

				     		$UpdateLastprantArr = array();
				     		$UpdateLastprantArr['field']			=	array();
							$UpdateLastprantArr['field']['last_writer'] 		= 	$writer;
				     		$UpdateLastprantArr['field']['last_subject'] 		= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['title'],'html');
				     		$UpdateLastprantArr['field']['last_subjectid'] 	= 	$PowerBB->subject->id;
				     		$UpdateLastprantArr['field']['last_date'] 	= 	$PowerBB->_CONF['now'];
				     		$UpdateLastprantArr['field']['last_time'] 	= 	$PowerBB->_CONF['now'];
				     		$UpdateLastprantArr['field']['icon'] 		= 	$PowerBB->_POST['icon'];
		     		        $UpdateLastprantArr['field']['last_berpage_nm'] 		= 	'0';
				     		$UpdateLastprantArr['where'] 		        = 	array('id',$Inf_row['parent']);

				     		// Update Last subject's information
				     		$UpdateprantLast = $PowerBB->section->UpdateSection($UpdateLastprantArr);
							unset($UpdateLastprantArr);


				     	}

               $get_sections_parent = $PowerBB->DB->sql_query("SELECT  *   FROM " . $PowerBB->table['section'] . " WHERE parent = " . $this->SectionInfo['parent'] . " ");
	                $Inf_rows = $PowerBB->DB->sql_fetch_array($get_section_parent);
                        if ($Inf_rows)
						{

	                           //////////
				     		if (!$PowerBB->_CONF['member_permission'])
							{
			                $writer = 	$PowerBB->_CONF['template']['lang']['Guestp'];
							}
					     	else
					     	{
				            $writer = 	$PowerBB->_CONF['member_row']['username'];
							}

				     		$UpdateLastprantArr = array();
				     		$UpdateLastprantArr['field']			=	array();
							$UpdateLastprantArr['field']['last_writer'] 		= 	$writer;
				     		$UpdateLastprantArr['field']['last_subject'] 		= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['title'],'html');
				     		$UpdateLastprantArr['field']['last_subjectid'] 	= 	$PowerBB->subject->id;
				     		$UpdateLastprantArr['field']['last_date'] 	= 	$PowerBB->_CONF['now'];
				     		$UpdateLastprantArr['field']['last_time'] 	= 	$PowerBB->_CONF['now'];
				     		$UpdateLastprantArr['field']['icon'] 		= 	$PowerBB->_POST['icon'];
		     		        $UpdateLastprantArr['field']['last_berpage_nm'] 		= 	'0';
				     		$UpdateLastprantArr['where'] 		        = 	array('parent',$Inf_rows['id']);

				     		// Update Last subject's information
				     		$UpdateprantLast = $PowerBB->section->UpdateSection($UpdateLastprantArr);
							unset($UpdateLastprantArr);

					     		// Update section's cache
					     		$UpdateArr 				= 	array();
					     		$UpdateArr['parent'] 	= 	$Inf_rows['parent'];

					     		$update_cache = $PowerBB->section->UpdateSectionsCache($UpdateArr);

					     		unset($UpdateArr);

				     		if ($UpdateprantLast)
							{

		                           //////////
					     		if (!$PowerBB->_CONF['member_permission'])
								{
				                $writer = 	$PowerBB->_CONF['template']['lang']['Guestp'];
								}
						     	else
						     	{
					            $writer = 	$PowerBB->_CONF['member_row']['username'];
								}

					     		$UpdateLastprantArr = array();
					     		$UpdateLastprantArr['field']			=	array();
								$UpdateLastprantArr['field']['last_writer'] 		= 	$writer;
					     		$UpdateLastprantArr['field']['last_subject'] 		= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['title'],'html');
					     		$UpdateLastprantArr['field']['last_subjectid'] 	= 	$PowerBB->subject->id;
					     		$UpdateLastprantArr['field']['last_date'] 	= 	$PowerBB->_CONF['now'];
					     		$UpdateLastprantArr['field']['last_time'] 	= 	$PowerBB->_CONF['now'];
					     		$UpdateLastprantArr['field']['icon'] 		= 	$PowerBB->_POST['icon'];
		     		            $UpdateLastprantArr['field']['last_berpage_nm'] 		= 	'0';
					     		$UpdateLastprantArr['where'] 		        = 	array('id',$Inf_rows['parent']);

					     		// Update Last subject's information
					     		$UpdateprantLast = $PowerBB->section->UpdateSection($UpdateLastprantArr);
								unset($UpdateLastprantArr);

						     		// Update section's cache
						     		$UpdateArr 				= 	array();
						     		$UpdateArr['parent'] 	= 	$Inf_rows['parent'];

						     		$update_cache = $PowerBB->section->UpdateSectionsCache($UpdateArr);


						     		unset($UpdateArr);
						     }

				     	}

		     		//////////
                    //Enabled to be notified by the existence of new replies
                    if ($PowerBB->_CONF['info_row']['allowed_emailed'] == '1')
		     		{

				     if ($PowerBB->_POST['emailed'])
				     {
				     	$EmailedArr 								= 	array();
				     	$EmailedArr['get_id']						=	true;
				     	$EmailedArr['field']						=	array();
				     	$EmailedArr['field']['user_id'] 			= 	$PowerBB->_CONF['member_row']['id'];
				     	$EmailedArr['field']['subject_id'] 			= 	$PowerBB->subject->id;
				     	$EmailedArr['field']['subject_title'] 		= 	$PowerBB->_POST['title'];

				     	$Insert = $PowerBB->emailed->InsertEmailed($EmailedArr);
		             }

	               //Send email notification to all participants in this department with a new topic
                   $SectionInfoid = $this->SectionInfo['id'];
                    $Adress = $PowerBB->functions->GetForumAdress();
                    $charset                =   $PowerBB->_CONF['info_row']['charset'];
	                 $PowerBB->_POST['message'] = $PowerBB->Powerparse->replace($PowerBB->_POST['message']);
	                 $starthtml = '<html dir=\"$charset\"><body>';
	                 $Endhtml = '</body></html>';
                        $br = '<br>';
                        $br = $PowerBB->Powerparse->replace($br);
                        $title = $PowerBB->_CONF['template']['lang']['New_Topic'] ;
			         	$Form_Massege = $PowerBB->_CONF['template']['lang']['Peace_be_upon_you']  . $PowerBB->_CONF['member_row']['username'].$PowerBB->_CONF['template']['lang']['Has_written_a_new_topic'] .
			                           $PowerBB->_CONF['template']['lang']['Please_login_on_the_following_link_to_access_the_subject']. $Adress . 'index.php?page=topic&show=1&id=' . $PowerBB->subject->id . $PowerBB->_CONF['template']['lang']['greetings_Management_Forum'] . $PowerBB->_CONF['info_row']['title'] .'<br>' . $Adress . 'index.php';

                     $getmember_query = $PowerBB->DB->sql_query("SELECT * FROM " . $PowerBB->table['emailed'] . " WHERE section_id = '$SectionInfoid'");
					if ($PowerBB->emailed->IsEmailed(array('where' => array('section_id',$SectionInfoid))))
					{

                      while ($getmember_row = $PowerBB->DB->sql_fetch_array($getmember_query))
                      {
                       		$MemArr 			= 	array();
							$MemArr['where'] 	= 	array('id',$getmember_row['user_id']);

							$MemInfo = $PowerBB->member->GetMemberInfo($MemArr);

                        $username = $PowerBB->_CONF['template']['lang']['hello_your']  . $MemInfo['username'].'<br>';

                       if (!$PowerBB->_CONF['member_row']['username'] == $MemInfo['username'])
					   {

			         	if ($PowerBB->_CONF['info_row']['mailer']=='phpmail')
						{
                       $Send = $PowerBB->functions->mail($MemInfo['email'],$title.':'.$PowerBB->_POST['title'],$starthtml.$username.$Form_Massege.$Endhtml,$PowerBB->_CONF['info_row']['send_email']);
			            }
						elseif ($PowerBB->_CONF['info_row']['mailer']=='smtp')
						{
						$to = $MemInfo['email'];
						$fromname = $PowerBB->_CONF['info_row']['title'];
						$message = $starthtml.$username.$Form_Massege.$Endhtml;
						$subject = $PowerBB->_POST['title'];
						$from = $PowerBB->_CONF['info_row']['send_email'];
                        $Send = $PowerBB->functions->send_this_smtp($to,$fromname,$message,$subject,$from);

						}
                       }


                      }
                    }

                  }
                   $subjecttitle =  $PowerBB->_POST['title'];
                   $subjecttitle = str_replace("\'", "'", $subjecttitle);
                   $subjecttitle = str_replace("\\'", "'", $subjecttitle);

				   if ($this->SectionInfo['parent']<1)
						{
													$SectionCache = $this->SectionInfo['id'];

								$Section_Rnum = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT section FROM " . $PowerBB->table['reply'] . " WHERE section = " . $SectionCache . " AND delete_topic<>1 AND review_reply<>1"));
								$Section_Snum = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT section FROM " . $PowerBB->table['subject'] . " WHERE section = " . $SectionCache . " AND delete_topic<>1 AND review_subject<>1 "));


								$subject_num = $Section_Snum ;
								$reply_num   = $Section_Rnum;

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

								$UpdatesrFormSecArr['field']['reply_num'] 	= 	$reply_num;
								$UpdatesrFormSecArr['field']['subject_num']  = 	$subject_num;

								$UpdatesrFormSecArr['where'] 		        = 	array('id',$SectionCache);

								// Update reply_num & subject_num
								$UpdaterSFormSec = $PowerBB->section->UpdateSection($UpdatesrFormSecArr);
                                 $UpdateSectionCache1 = $PowerBB->functions->UpdateSectionCache($SectionCache);
						}
						else
						{
						$SectionCache = $this->SectionInfo['parent'];


								$S_R = $PowerBB->DB->sql_query("SELECT * FROM " . $PowerBB->table['section'] . " WHERE parent = ".$SectionCache." ");
								while ($f_S_R = $PowerBB->DB->sql_fetch_array($S_R))
								{
								$Section_Rnum = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT section FROM " . $PowerBB->table['reply'] . " WHERE section = " . $SectionCache . " AND delete_topic<>1 AND review_reply<>1"));
								$Section_Snum = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT section FROM " . $PowerBB->table['subject'] . " WHERE section = " . $SectionCache . " AND delete_topic<>1 AND review_subject<>1 "));

								$Rnum = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT section FROM " . $PowerBB->table['reply'] . " WHERE section = " . $f_S_R['id'] . " AND delete_topic<>1 AND review_reply<>1"));
								$Snum = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT section FROM " . $PowerBB->table['subject'] . " WHERE section = " . $f_S_R['id'] . " AND delete_topic<>1 AND review_subject<>1 "));

								$subject_num = $Section_Snum + $Snum;
								$reply_num   = $Section_Rnum + $Rnum;

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

								$UpdatesrFormSecArr['field']['reply_num'] 	= 	$reply_num;
								$UpdatesrFormSecArr['field']['subject_num']  = 	$subject_num;

								$UpdatesrFormSecArr['where'] 		        = 	array('id',$SectionCache);

								// Update reply_num & subject_num
								$UpdaterSFormSec = $PowerBB->section->UpdateSection($UpdatesrFormSecArr);
                                 $UpdateSectionCache1 = $PowerBB->functions->UpdateSectionCache($SectionCache);
				               }
						}

		     		$LastArr = array();

		     		$LastArr['writer'] 		= 	$PowerBB->_CONF['member_row']['username'];
		     		$LastArr['title'] 		= 	$PowerBB->_POST['title'];
		     		$LastArr['subject_id'] 	= 	$PowerBB->subject->id;
		     		$LastArr['date'] 		= 	$PowerBB->_CONF['date'];
		     		$LastArr['last_time'] 		= 	$PowerBB->_CONF['date'];
		     		$LastArr['icon'] 		= 	$PowerBB->_POST['icon'];
		     		$LastArr['last_reply'] 		= 	'0';

		     		$LastArr['where'] 		= 	(!$this->SectionInfo['parent']) ? array('id',$this->SectionInfo['id']) : array('id',$this->SectionInfo['parent']);

     		// Update Last subject's information
     		$UpdateLast = $PowerBB->section->UpdateLastSubject($LastArr);

			     	if (($PowerBB->_CONF['member_row']['review_subject'] or $PowerBB->_CONF['group_info']['review_subject'] or $this->SectionInfo['review_subject'])
						and !$PowerBB->_CONF['group_info']['admincp_allow'])
					{
		     			$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Waiting_approved_by_management']);
		     			$PowerBB->functions->redirect('index.php?page=forum&amp;show=1&amp;id=' . $this->SectionInfo['id'] . $PowerBB->_CONF['template']['password'],'8');

					}
		     		else
		     		{
			     			$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['subjects_Add_successfully1'].' ' . $PowerBB->Powerparse->censor_words($subjecttitle) . ' '.$PowerBB->_CONF['template']['lang']['subjects_Add_successfully2']);
			     			$PowerBB->functions->redirect('index.php?page=topic&amp;show=1&amp;id=' . $PowerBB->subject->id . $PowerBB->_CONF['template']['password']);
		     		}

		     		//////////
		     	}

        }
	}

}

?>
