<?php
session_start();

$CALL_SYSTEM				=	array();
$CALL_SYSTEM['BANNED'] 		= 	true;
$CALL_SYSTEM['CACHE'] 		= 	true;
$CALL_SYSTEM['REQUEST'] 	= 	true;
$CALL_SYSTEM['MESSAGE'] 	= 	true;
$CALL_SYSTEM['EXTRAFIELD']   =   true;

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

include('common.php');

define('CLASS_NAME','PowerBBRegisterMOD');

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

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

		if (!$PowerBB->_CONF['info_row']['reg_' . $PowerBB->_CONF['day']])
   		{
   			$PowerBB->functions->ShowHeader();
   			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_can_not_register_today']);
   		}


		/** Show register form **/
		if ($PowerBB->_GET['index'])
		{
			if ($PowerBB->_CONF['info_row']['reg_o']
				and !$PowerBB->_GET['agree'])
			{
				$this->_RegisterRules();
			}
			else
			{
				$this->_RegisterForm();
			}
		}
		/** **/
		  elseif ($PowerBB->_GET['checkname'])
		  {
		   $this->_CheckNameStart();
		  }
		  elseif ($PowerBB->_GET['checkemail'])
		  {
		   $this->_CheckEmailStart();
		  }
		/** Start registetr **/
		elseif ($PowerBB->_GET['start'])
		{
			$this->_RegisterStart();
		}
		/** **/
		else
		{
			$PowerBB->functions->ShowHeader();
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['path_not_true']);
		}


	}

	/**
	 * Print registeration rules
	 */
	function _RegisterRules()
	{
		global $PowerBB;

		$PowerBB->functions->ShowHeader();
		if ($PowerBB->_CONF['member_permission'])
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_are_already_registered']);
		}
		  $PowerBB->Powerparse->replace_smiles($PowerBB->_CONF['info_row']['rules']);
         $PowerBB->_CONF['info_row']['rules'] = $PowerBB->Powerparse->replace($PowerBB->_CONF['info_row']['rules']);
         $PowerBB->template->assign('rules',$PowerBB->_CONF['info_row']['rules']);

		$PowerBB->template->display('register_rules');
		$PowerBB->functions->GetFooter();
	}


	/**
	 * Show nice form for register :)
	 */
	function _RegisterForm()
	{
		global $PowerBB;

		$PowerBB->functions->ShowHeader();


		if ($PowerBB->_CONF['member_permission'])
		{
			$stop = ($PowerBB->_CONF['info_row']['ajax_register'] and !$PowerBB->_POST['ajax']) ? false : true;
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_are_already_registered']);
		}
				//getting extra fields
         $PowerBB->_CONF['template']['while']['extrafields']=$PowerBB->extrafield->getEmptyLoginFields();

		if ($PowerBB->_GET['invite'])
		{
         $PowerBB->_GET['invite']   =     $PowerBB->functions->CleanVariable($PowerBB->_GET['invite'],'intval');
         $MemberArr             =     array();
         $MemberArr['where']     =     array('id',$PowerBB->_GET['invite']);
         $GetInviterMemberInfo = $PowerBB->member->GetMemberInfo($MemberArr);
         $PowerBB->template->assign('invite',$GetInviterMemberInfo['username']);
		}

		$PowerBB->template->display('register');
        $PowerBB->functions->GetFooter();
	}

	/**
	 * Some checks then add the member to database
	 */
	function _RegisterStart()
	{
		global $PowerBB;

    	$stop = ($PowerBB->_CONF['info_row']['ajax_register'] and !$PowerBB->_POST['ajax']) ? false : true;
		if(is_numeric($PowerBB->_POST['username']))
		{
		  if ($PowerBB->_POST['ajax'])
		   {
			   $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_can_register_this_numbrs_name'],$stop,$stop);
			}
			else
      		{
      		   $PowerBB->functions->ShowHeader();
			   $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_can_register_this_numbrs_name']);
			   $PowerBB->functions->GetFooter();
		   }

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

		// Clean the username and email from white spaces
		$PowerBB->_POST['username'] 	= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['username'],'trim');
		$PowerBB->_POST['email'] 		= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['email'],'trim');

		$PowerBB->_POST['username'] 	= 	$PowerBB->functions->CleanVariable($PowerBB->_POST['username'],'sql');
		$PowerBB->_POST['email']   = 	$PowerBB->functions->CleanVariable($PowerBB->_POST['email'],'sql');
		$PowerBB->_POST['email_confirm']   = 	$PowerBB->functions->CleanVariable($PowerBB->_POST['email_confirm'],'sql');
       	$PowerBB->_POST['password_confirm']   = 	$PowerBB->functions->CleanVariable($PowerBB->_POST['password_confirm'],'sql');
      	$PowerBB->_POST['invite']   = 	$PowerBB->functions->CleanVariable($PowerBB->_POST['invite'],'sql');
        $PowerBB->_POST['code']   = 	$PowerBB->functions->CleanVariable($PowerBB->_POST['code'],'sql');
       	$PowerBB->_POST['year']   = 	$PowerBB->functions->CleanVariable($PowerBB->_POST['year'],'intval');
		$PowerBB->_POST['year']   = 	$PowerBB->functions->CleanVariable($PowerBB->_POST['year'],'sql');
      	$PowerBB->_POST['username']  =  $PowerBB->Powerparse->censor_words($PowerBB->_POST['username']);

		// Store the email provider in explode_email[1] and the name of email in explode_email[0]
		// That will be useful to ban email provider
		$explode_email = explode('@',$PowerBB->_POST['email']);

		// Well , we get the provider of email
		$EmailProvider = $explode_email[1];

		// Ensure all necessary information are valid
		if (empty($PowerBB->_POST['username'])
			or empty($PowerBB->_POST['password'])
			or empty($PowerBB->_POST['email']))
		{

		  if ($PowerBB->_POST['ajax'])
		   {
			   $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Please_fill_in_all_the_information'],$stop,$stop);
			}
			else
      		{
      		   $PowerBB->functions->ShowHeader();
			   $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Please_fill_in_all_the_information']);
			   $PowerBB->functions->GetFooter();
		   }

		}

		// Ensure the email is equal the confirm of email
		if ($PowerBB->_POST['email'] != $PowerBB->_POST['email_confirm'])
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['email_confirm_Incorrectly'],$stop,$stop);
		}

		// Ensure the password is equal the confirm of password
		if ($PowerBB->_POST['password'] != $PowerBB->_POST['password_confirm'])
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['password_confirm_Incorrectly'],$stop,$stop);
		}

		// Check if the email is valid, This line will prevent any false email
		if (!$PowerBB->functions->CheckEmail($PowerBB->_POST['email']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Please_enter_your_correct_email'],$stop,$stop);
		}

		// Ensure there is no person used the same username
		if ($PowerBB->member->IsMember(array('where' => array('username',$PowerBB->_POST['username']))))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['please_choose_another_name'],$stop,$stop);
		}

		// Ensure there is no person used the same email
		if ($PowerBB->member->IsMember(array('where' => array('email',$PowerBB->_POST['email']))))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['E-mail_is_registered_please_type_the_other']);
		}

		if ($PowerBB->banned->IsUsernameBanned(array('username'	=>	$PowerBB->_POST['username'])))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_can_not_register_this_name'],$stop,$stop);
		}

		if ($PowerBB->banned->IsEmailBanned(array('email'	=>	$PowerBB->_POST['email'])))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_can_not_register_this_e-mail'],$stop,$stop);
		}

		if ($PowerBB->banned->IsProviderBanned(array('provider'	=>	$EmailProvider)))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_can_not_register_this_provider_mail'],$stop,$stop);
		}

		if ($PowerBB->_POST['username'] == 'Guest')
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['can_not_register_Guest_name'],$stop,$stop);
		}

   		if (!isset($PowerBB->_POST['username']{$PowerBB->_CONF['info_row']['reg_less_num']}))
   		{
   			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Character_name_a_few_user'],$stop,$stop);
      	}

		   if(mb_strlen($PowerBB->_POST['username'], 'UTF-8') <= $PowerBB->_CONF['info_row']['reg_max_num'])
			{
			 // $this->_AjaxTxtRed($PowerBB->_CONF['template']['lang']['characters_Username_many']);
			}
			else
			{
			$this->_AjaxTxtRed($PowerBB->_CONF['template']['lang']['characters_Username_many']);
			}

      	if (isset($PowerBB->_POST['password']{$PowerBB->_CONF['info_row']['reg_pass_max_num']+1}))
      	{
            $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Character_pass_many'],$stop,$stop);
      	}

      	if (!isset($PowerBB->_POST['password']{$PowerBB->_CONF['info_row']['reg_pass_min_num']-1}))
      	{
        	$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Character_pass_few'],$stop,$stop);
      	}

      	// Ensure there is no the same Member invite
      	if ($PowerBB->_POST['invite'] =='')
      	{

        }
		else
   		{
			if (!$PowerBB->member->IsMember(array('where' => array('username',$PowerBB->_POST['invite']))))
			{
				$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['please_correct_inviter_name'],$stop,$stop);
			}
		}


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

		//getting extra fields
         $extraFields=$PowerBB->extrafield->getEmptyLoginFields();
		//checking if the extra fields are required
		 foreach($extraFields AS $field)
		 {
		$field['name']   = 	$PowerBB->functions->CleanVariable($field['name'],'sql');

			$FieldsArr = array();
			$FieldsArr['where'] = array('name',$field['name']);

			$FieldsInfo = $PowerBB->extrafield->GetFieldInfo($FieldsArr);
	   		if ($FieldsInfo['required'] == 'yes')
	   		{
		   		if (empty( $PowerBB->_POST[ $field['name_tag'] ] ))
		   		{
		          $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_do_not_enter'].' <i><b>'.$field['name'].'</b></i>',$stop,$stop);
		    	}
	      }

	    }

		if (strstr($PowerBB->_POST['username'],'"')
			or strstr($PowerBB->_POST['username'],"'")
			or strstr($PowerBB->_POST['username'],'>')
			or strstr($PowerBB->_POST['username'],'<')
			or strstr($PowerBB->_POST['username'],'*'))
      	{
      		$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['You_can_not_register_these_symbols'],$stop,$stop);
      	}

      	$md5_password = md5($PowerBB->_POST['password']);

      	//////////

      	// Get the information of default group to set username style cache

		$GrpArr 			= 	array();
		$GrpArr['where'] 	= 	array('id',$PowerBB->_CONF['info_row']['def_group']);

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

		// invite_num
    $invite = $PowerBB->_POST['invite'];
	$update = $PowerBB->DB->sql_query("UPDATE " . $PowerBB->table['member'] . " SET invite_num = invite_num + 1 WHERE username ='$invite'");

		$style = $GroupInfo['username_style'];
		$username_style_cache = str_replace('[username]',$PowerBB->_POST['username'],$style);

      	//////////

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

      	$InsertArr['field']['username'] 			= 	$PowerBB->_POST['username'];
      	$InsertArr['field']['password'] 			= 	$md5_password;
      	$InsertArr['field']['email'] 				= 	$PowerBB->_POST['email'];
      	$InsertArr['field']['usergroup'] 			= 	$PowerBB->_CONF['info_row']['def_group'];
      	$InsertArr['field']['user_gender'] 			= 	$PowerBB->_POST['gender'];
      	$InsertArr['field']['inviter'] 			    = 	$PowerBB->_POST['invite'];
      	$InsertArr['field']['register_date'] 		= 	$PowerBB->_CONF['now'];
      	$InsertArr['field']['lastvisit'] 		    = 	$PowerBB->_CONF['now'];
      	$InsertArr['field']['user_title'] 			= 	$PowerBB->_CONF['template']['lang']['member'];
      	$InsertArr['field']['style'] 				= 	$PowerBB->_CONF['info_row']['def_style'];
      	$InsertArr['field']['username_style_cache']	=	$username_style_cache;
      	$InsertArr['field']['bday_day']	            =	$PowerBB->_POST['day'];
      	$InsertArr['field']['bday_month']	        =	$PowerBB->_POST['month'];
      	$InsertArr['field']['bday_year']	        =	$PowerBB->_POST['year'];
      	      	//extra fields insertion
      	foreach($extraFields AS $field){
          $InsertArr['field'][ $field['name_tag'] ]     =   $PowerBB->functions->CleanVariable($PowerBB->_POST[ $field['name_tag'] ],'sql');
      	}
      	$InsertArr['get_id']						=	true;

      	$insert = $PowerBB->member->InsertMember($InsertArr);


      	if (!$PowerBB->_CONF['info_row']['ajax_register'])
      	{
      		if (!isset($PowerBB->_POST['ajax']))
      		{
         		$PowerBB->functions->ShowHeader($PowerBB->_CONF['template']['lang']['registrationprocess']);
      		}
      	}

      	// Ouf finally , but we still have work in this module
      	if ($insert)
      	{
      		$member_num = $PowerBB->member->GetMemberNumber(array('get_from'	=>	'cache'));

      		$PowerBB->cache->UpdateLastMember(array(	'username'		=>	$PowerBB->_POST['username'],
      													'id'			=>	$PowerBB->member->id,
      													'member_num'	=>	$member_num));

      		if ($PowerBB->_CONF['info_row']['def_group'] == 5)
      		{
      			$Adress	= 	$PowerBB->functions->GetForumAdress();
				$Code	=	$PowerBB->functions->RandomCode();

				$ActiveAdress = $Adress . 'index.php?page=active_member&index=1&id=' . $PowerBB->member->id . '&code=' . $Code;

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

				$ReqArr['field']['random_url'] 		= 	$Code;
				$ReqArr['field']['username'] 		= 	$PowerBB->_POST['username'];
				$ReqArr['field']['request_type'] 	= 	3;

				$InsertReq = $PowerBB->request->InsertRequest($ReqArr);

				if ($InsertReq)
				{
					$MessageInfArr 			= 	array();
					$MessageInfArr['where'] 	= 	array('id','4');

					$Massege_Info = $PowerBB->message->GetMessageInfo($MessageInfArr);

					$MsgArr = array();
					$MsgArr['text'] 		= 	$Massege_Info['text'];
					$MsgArr['active_url'] 	= 	$ActiveAdress;
					$MsgArr['username']		=	$PowerBB->_POST['username'];
					$MsgArr['title']		=	$PowerBB->_CONF['info_row']['title'];

					$Massege_Info['text'] = $PowerBB->message->MessageProccess($MsgArr);


				       if ($PowerBB->_CONF['info_row']['mailer']=='phpmail')
						{
					      $Send = $PowerBB->functions->mail($PowerBB->_POST['email'],$Massege_Info['title'],$Massege_Info['text'],$PowerBB->_CONF['info_row']['send_email']);
			            }
			           elseif ($PowerBB->_CONF['info_row']['mailer']=='smtp')
						{
							$to = $PowerBB->_POST['email'];
							$fromname = $PowerBB->_CONF['info_row']['title'];
							$message = $Massege_Info['text'];
							$subject = $Massege_Info['title'];
							$from = $PowerBB->_CONF['info_row']['send_email'];
                            $Send = $PowerBB->functions->send_this_smtp($to,$fromname,$message,$subject,$from);
						}

                  if (!$PowerBB->_CONF['info_row']['ajax_register'])
                  {
					$PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Registered_successfully_and_has_been_sent_to_your_e-mail_activation']);
					$PowerBB->functions->redirect('index.php?page=login&register_login=1&username='.$PowerBB->_POST['username'] .'&password='.$md5_password);
					$PowerBB->functions->GetFooter();
                   }


				}
			}
			else
      		{

      			if (!$PowerBB->_CONF['info_row']['ajax_register'])
      			{
      				if (!isset($PowerBB->_POST['ajax']))
      				{
                    $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Registered_successfully']);
      			    $PowerBB->functions->redirect('index.php?page=login&register_login=1&username='.$PowerBB->_POST['username'] .'&password='.$md5_password);
      			    $PowerBB->functions->GetFooter();
      				}
      			}

             }

      				if ($PowerBB->_CONF['info_row']['ajax_register'])
      				{
			            // $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['Registered_successfully']);
                        echo ('<SCRIPT LANGUAGE="JavaScript">window.location="index.php?page=login&register_login=1&username='.$PowerBB->_POST['username'].'&password='.$md5_password.'";</script>');

      				}


      	}
	}

		function _AjaxTxtGreen($msg,$color='green')
		{
		  global $PowerBB;
		  echo '<style type="text/css">.Ajaxwarning1 { font-family:tahoma; font-size:10px; background-color: #008000; color: #fff; border: 1px solid #6E704B; padding: 2px; -moz-border-radius: 5px; overflow: auto;}</style><span class="Ajaxwarning1" align="center">'.$msg.'</span>';
		  exit;
		}
		function _AjaxTxtRed($msg,$color='red')
		{
		  global $PowerBB;
		  echo '<style type="text/css">.Ajaxwarning2 { font-family:tahoma; font-size:10px; background-color: #BD2530; color: #fff; border: 1px solid #6E704B; padding: 2px; -moz-border-radius: 5px; overflow: auto;}</style><span class="Ajaxwarning2" align="center">'.$msg.'</span>';
		  exit;
		}
		  /**
		   * Some checks then add the member to database
		  */
		function _CheckNameStart()
		{
		  global $PowerBB;
				$stop = ($PowerBB->_CONF['info_row']['ajax_register'] and !$PowerBB->_POST['ajax']) ? false : true;
			  // Clean the username from white spaces
			if(is_numeric($PowerBB->_POST['username']))
			{
			$this->_AjaxTxtRed($PowerBB->_CONF['template']['lang']['You_can_register_this_numbrs_name']);
			}
			 $PowerBB->_POST['username']  =  $PowerBB->functions->CleanVariable($PowerBB->_POST['username'],'trim');
			 $PowerBB->_POST['username']  =  $PowerBB->Powerparse->censor_words($PowerBB->_POST['username']);

			if(!isset($PowerBB->_POST['username']{$PowerBB->_CONF['info_row']['reg_less_num']}))
			{
			$this->_AjaxTxtRed($PowerBB->_CONF['template']['lang']['Character_name_a_few_user']);
			}
		   if(mb_strlen($PowerBB->_POST['username'], 'UTF-8') <= $PowerBB->_CONF['info_row']['reg_max_num'])
			{
			 // $this->_AjaxTxtRed($PowerBB->_CONF['template']['lang']['characters_Username_many']);
			}
			else
			{
			$this->_AjaxTxtRed($PowerBB->_CONF['template']['lang']['characters_Username_many']);
			}
			if ($PowerBB->banned->IsUsernameBanned(array('username' => $PowerBB->_POST['username'])))
			{
			$this->_AjaxTxtRed($PowerBB->_CONF['template']['lang']['You_can_not_register_this_name']);
			}
			if ($PowerBB->_POST['username'] == 'Guest')
			{
			$this->_AjaxTxtRed($PowerBB->_CONF['template']['lang']['can_not_register_Guest_name']);
			}

			if (strstr($PowerBB->_POST['username'],'"')
			or strstr($PowerBB->_POST['username'],"'")
			or strstr($PowerBB->_POST['username'],'>')
			or strstr($PowerBB->_POST['username'],'<')
			or strstr($PowerBB->_POST['username'],'*'))
			{
			$this->_AjaxTxtRed($PowerBB->_CONF['template']['lang']['You_can_not_register_these_symbols']);
			}

			// Ensure there is no person used the same username
			if ($PowerBB->member->IsMember(array('where' =>
			array('username',$PowerBB->_POST['username']))))
			{
			$this->_AjaxTxtRed($PowerBB->_CONF['template']['lang']['please_choose_another_name']);
			}
			if (!$PowerBB->member->IsMember(array('where' => array('username',$PowerBB->_POST['username']))))
			{
			$this->_AjaxTxtGreen($PowerBB->_CONF['template']['lang']['You_can_register_this_name'],'green');
			$this->_AjaxTxtGreen('?','green');
			}

		}

		function _CheckEmailStart()
		{
		  global $PowerBB;
			$stop = ($PowerBB->_CONF['info_row']['ajax_register'] and!$PowerBB->_POST['ajax']) ? false : true;
		  // Clean the username from white spaces
		  $PowerBB->_POST['email']   =  $PowerBB->functions->CleanVariable($PowerBB->_POST['email'],'trim');
		  // Check if the email is valid, This line will prevent any false email
		  if(!$PowerBB->functions->CheckEmail($PowerBB->_POST['email']))
		  {
		   $this->_AjaxTxtRed($PowerBB->_CONF['template']['lang']['Please_enter_your_correct_email']);
		  }
		  // Ensure there is no person used the same email
		  if ($PowerBB->member->IsMember(array('where' =>array('email',$PowerBB->_POST['email']))))
		  {
		   $this->_AjaxTxtRed($PowerBB->_CONF['template']['lang']['E-mail_is_registered_please_type_the_other']);
		  }
		  if ($PowerBB->banned->IsEmailBanned(array('email' => $PowerBB->_POST['email'])))
		  {
		   $this->_AjaxTxtRed($PowerBB->_CONF['template']['lang']['You_can_not_register_this_e-mail']);
		  }


		   $this->_AjaxTxtGreen($PowerBB->_CONF['template']['lang']['You_can_register_this_email'],'green');
		   $this->_AjaxTxtGreen('?','green');
		}

}

?>
