<?php
if ( 
		isset ( $_POST['full_name'] ) 
		&& !empty( $_POST['full_name'] ) 
		&& isset ( $_POST['username'] ) 
		&& !empty( $_POST['username'] ) 
		&& isset ( $_POST['email'] ) 
		&& !empty( $_POST['email'] ) 
		&& isset ( $_POST['password'] ) 
) {
	 
		
	  $_POST['code']           = sha1 ( $_SERVER['REMOTE_ADDR'] . microtime() . mt_rand ( 1,100000 ) )._Function::randomString( 40, TRUE, TRUE, TRUE );
	  $_POST['email']          = trim( $_POST['email']);
	  $_POST['full_name']      = _Function::spaces( trim( $_POST['full_name']) );
	  $_POST['username']       = _Function::spaces( trim( $_POST['username'] ) );
	  $_POST['password']       = _Function::spaces( trim( $_POST['password'] ) );
	  $admin                   = $obj->getSettings();
	  $emailAddress            = $_POST['email'];
	  $_POST['email_verify']   = $settings->email_verification;
		
		if ( $_POST['full_name'] == '' 
			|| mb_strlen( $_POST['full_name'], 'utf8' ) < 2 
			|| mb_strlen( $_POST['full_name'], 'utf8' )  > 20 
		) 
		{
			echo json_encode( array( 'res' => $_SESSION['LANG']['full_name_error'], 'focus' => 'full_name' ) );
			
		} else if ( preg_match( '/[^a-z0-9\_]/i',$_POST['username'] ) ) {
			
			echo json_encode( array( 'res' => $_SESSION['LANG']['username_not_valid'], 'focus' => 'username' ) );
			
		} else if ( strlen( $_POST['username'] ) < 1 || strlen( $_POST['username'] ) > 15 ) {
			
			echo json_encode( array( 'res' => $_SESSION['LANG']['username_not_valid'], 'focus' => 'username' ) );
			
		} else if ( !filter_var( $emailAddress, FILTER_VALIDATE_EMAIL ) ) {
			
			echo json_encode( array( 'res' => $_SESSION['LANG']['email_not_valid'], 'focus' => 'email' ) );
		   
		} else if ( mb_strlen( $_POST['password'], 'utf8' ) < 5 || mb_strlen( $_POST['password'], 'utf8' ) > 20 ) {
			
			echo json_encode( array( 'res' => $_SESSION['LANG']['password'], 'focus' => 'username' ) );
			
		}  else if ( $_POST['terms'] == '' ) {
			
			echo json_encode( array( 'res' => $_SESSION['LANG']['can_not_register'], ) );
		} else {
			
			/* INSERT DATABASE */
			$res = $obj->signUp();
			
			/* EMAIL TEMPLATE */
			
			$messageEmail = '
			<table width="550" cellpadding="0" cellspacing="0" style="font-family:Arial,Helvetica,sans-serif; font-size: 14px; color: #666;" align="center">
	<tbody>
		<tr>
			<td width="550" height="50" align="center" style="background: #fff;">
				<img style="width: 130px;" src="'. URL_BASE .'public/img/logo.png" />
			</td>
		</tr>
		
		<tr>
			<td width="558" align="center" style="background: #FFF; line-height: 18px; padding: 10px;  border-bottom: 1px solid #DDD; border-left: 1px solid #DDD; border-right: 1px solid #DDD;">
				<p style="margin-bottom: 10px;">
					Thanks for signing up on '. $admin->title .' <strong>"'. $_POST['full_name'] .'"</strong>
				</p>
				<p style="margin-bottom: 10px;">
					<a style="text-decoration: none; color: #FFF; padding: 5px 10px; background: #FF7000; border-radius: 3px; -webkit-border-radius: 3px;" rel="nofollow" href="'. URL_BASE .'?validate='. $_POST['code'].'" target="_blank">
					<strong>
						Click here to activate your account
					</strong></a>
				</p>
					<p style="text-align: center; font-size: 11px; padding: 5px 0 0 0;">
						© '.date('Y') .' '. $admin->title .'
					</p>
			</td>
		</tr>
	</tbody>
</table>';
			
			if( $res == 1 ) {
			
		  	echo json_encode( array( 'res' => $_SESSION['LANG']['sign_up_success'], 'success' => 1 ) );
			
			//<------- * Send Email * ------------>
			$mail = new PHPMailer;
			
			/*-------------
			 * SMTP 
			 *------------
			 */
			if( SMTP_USERNAME != '' ) {
				$mail->isSMTP();          // Set mailer to use SMTP
				$mail->Host = SMTP_HOST;  // Specify main and backup SMTP servers
				$mail->SMTPAuth = true;   // Enable SMTP authentication
				$mail->Username = SMTP_USERNAME; // SMTP username
				$mail->Password = SMTP_PASSWORD; // SMTP password
				$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
				$mail->Port = SMTP_PORT;  // TCP port to connect to
			}
					
			$mail->CharSet = 'UTF-8';
			$mail->Encoding = 'quoted-printable';
			$mail->From = EMAIL_ACTIVE_ACCOUNT;
			$mail->FromName = $admin->title;
			$mail->addAddress($_POST['email']);
			$mail->isHTML(true);
			$mail->Subject = 'Activate Account';
			$mail->Body    = $messageEmail;
			
			$mail->send();

		  } else if ( $res == 'unavailable' ) {
		  	
			echo json_encode( array( 'res' => $_POST['username'].' '.$_SESSION['LANG']['username_already_use'] ) );
	    } else {
	    	echo json_encode( array( 'res' => $_SESSION['LANG']['email_already_use'] ) );
	 }// ELSE
   }// ELSE
 } else {
	echo $_SESSION['LANG']['error'];
}

 ?>