<?php
$page=$title="ثبت نام";
$description="در پرشین پیانو ثبت نام کرده و حساب کاربری خود را ایجاد کنید.";
$keywords="ثبت نام در پرشین پیانو, عضویت در پرشین پیانو, حساب کاربری پرشین پیانو";
$og_type='website';
$og_image='/images/og-logo.png';
$javascript='register.js';
include("includes/header.php"); 
echo('<br>');
if(isset($_POST['username'])&&isset($_POST['email'])){
	if(isset($_POST['accept_terms'])){
	$username=strip_tags($_POST['username']);
	$password=strip_tags($_POST['password']);
	$password_repeat=strip_tags($_POST['password_repeat']);
	$email=strip_tags($_POST['email']);
	if($username!=""&&$password!=""&&$password_repeat!=""&&
	   $email!=""){
		if(filter_var($email,FILTER_VALIDATE_EMAIL)){
		if($password==$password_repeat){
		$url = 'https://www.google.com/recaptcha/api/siteverify';
        $data = array('secret' => '6LcV4kMaAAAAADfHOutU8rl5LWaz68WVmJZWXmTo',
                      'response' => $_POST['g-recaptcha-response'], 'remoteip' => $_SERVER['REMOTE_ADDR']);
        $options = array(
            'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data)
            )
        );
        $context  = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        $data = json_decode($result);
        $score = $data->score;
        if($data->success==true&&$score>0.5){
        $statement=$link->prepare("SELECT username FROM users WHERE username='$username'");
		$statement->bind_param('s',$username);
		$statement->execute();
        $result=$statement->get_result();
	    if(!$row=$result->fetch_assoc()){
	    $statement=$link->prepare("SELECT username FROM users WHERE email=?");
		$statement->bind_param('s',$email);
		$statement->execute();
        $result=$statement->get_result();
	    if(!$row=$result->fetch_assoc()){
			if(strlen($username)<=35){
			if(strlen($password)<=20){
			if(strlen($email)<=30){
			if(strlen($_FILES['image']['name'])<=70){
			if($_FILES['image']['name']!=""){
			if($_FILES['image']['type']=='image/jpeg'||
			   $_FILES['image']['type']=='image/png'||
			   $_FILES['image']['type']=='image/bmp'){
			if($_FILES['image']['size']<=2097152){
				$targetfile=rand(1000000,9999999999)."-".$_FILES['image']['name'];
				move_uploaded_file($_FILES['image']['tmp_name'],
								  "images/users/".$targetfile);
				$file_ok=true;
			}else{
				echo("<span style='color:red'><center>حجم عکس انتخاب شده، بیش از 2 مگابایت است و در نتیجه مورد قبول نمی باشد.</center></span>");
			}
			}else{
				echo("<span style='color:red'><center>فرمت عکس آپلود شده مورد قبول نمی باشد. فقط فرمت های PNG ،BMP و JPG مورد قبول می باشند.</center></span>");
			}
			}else{
				$targetfile="user.png";
				$file_ok=true;
			}
			if(isset($file_ok)){
				include("includes/encryption.php");
				$password=encfunc('encrypt',$password);
				$statement=$link->prepare("INSERT INTO users (id,username,password,level,date,image,email,block,lastlogin)VALUES(0,?,?,0,'$date',?,?,0,
				'هرگز')");
				$statement->bind_param('ssss',$username, $password,$targetfile,$email);
		if($statement->execute()){
		$_SESSION['register']=true; ?>
		<script>location.replace('login');</script>
		<?php }else{
			echo("<span style='color:red'><center>در هنگام ثبت نام شما خطایی رخ داد. لطفا دوباره تلاش کنید.</center></span>");
		}
			}
			}else
				echo("<span style='color:red'><center>حداکثر اندازه ی نام عکس آپلود شده، 70 کاراکتر می باشد.</center></span>");
	        }else
				echo("<span style='color:red'><center>حداکثر اندازه ی ایمیل  وارد شده، 30 کاراکتر می باشد.</center></span>");
			}else
				echo("<span style='color:red'><center>حداکثر اندازه ی گذرواژه ی وارد شده، 20 کاراکتر می باشد.</center></span>");
			}else
				echo("<span style='color:red'><center>حداکثر اندازه ی نام کاربری وارد شده، 35 کاراکتر می باشد.</center></span>");
		}else{
			echo("<span style='color:red'><center>ایمیل وارد شده، قبلا توسط کاربر دیگری ثبت شده است. لطفا ایمیل دیگری را وارد کنید.</center></span>");
		}
        }else{
			echo("<span style='color:red'><center>نام کاربری وارد شده، قبلا توسط کاربر دیگری ثبت شده است. لطفا نام کاربری دیگری را انتخاب کنید.</center></span>");
		}
        }else{
		    echo("<span style='color:red'><center>در حال حاضر، امکان ثبت نام در پرشین پیانو برای شما وجود ندارد.</center></span>");
	    }
		}else{
			echo("<span style='color:red'><center>گذرواژه و تکرار آن با هم برابر نیستند.</center></span>");
		}
	}else{
		echo("<span style='color:red'><center>ایمیل وارد شده معتبر نمی باشد.</center></span>");
	}
	}else{
		echo("<span style='color:red'><center>لطفا برای ثبت نام همه ی بخش های ضروری (ستاره دار) را پرکنید.</center></span>");
	}
	}else
		echo("<span style='color:red'><center>برای ایجاد حساب کاربری، باید با قوانین پرشین پیانو موافقت کنید.</center></span>");
}
?>
<script>
   function submitregister(token) {
     document.getElementById("demo-form").submit();
   }
</script>
<p><center><div style="border: 1px black solid;width: <?php if($ismobile==false) echo('70'); else echo('95'); ?>%;border-radius: 25px;">
<h1><center>ثبت نام در پرشین پیانو</center></h1>
<p style="text-align: right;width: 97%">برای ثبت نام در پرشین پیانو، فرم زیر را پر کرده و روی دکمه ی ثبت نام کلیک کنید.</p>
<center><form action="register" method="post" enctype="multipart/form-data" name="register_form" onSubmit="event.preventDefault();register();" id='demo-form'>
<table width="<?php if($ismobile==false) echo('70'); else echo('90'); ?>%">
<tr>
	<td width="30%">نام کاربری<span style="color: red;font-family: vazir;font-size: 75%">*</span>:</td>
	<td><input type="text" name="username" style="font-family: vazir;font-size: 90%;width: 96%" spellcheck="false" autofocus></td>
</tr>
<tr>
	<td>گذرواژه<span style="color: red;font-family: vazir;font-size: 75%">*</span>:</td>
	<td><input type="password" name="password" style="font-family: vazir;font-size: 90%;width: 96%"></td>
</tr>
<tr>
	<td>تکرار گذرواژه<span style="color: red;font-family: vazir;font-size: 75%">*</span>:</td>
	<td><input type="password" name="password_repeat" style="font-family: vazir;font-size: 90%;width: 96%"></td>
</tr>
<tr>
	<td>ایمیل<span style="color: red;font-family: vazir;font-size: 75%">*</span>:</td>
	<td><input type="email" name="email" style="font-family: vazir;font-size: 90%;width: 96%" dir="ltr"></td>
</tr>
<tr>
	<td>عکس(اختیاری):</td>
	<td><input type="file" name="image" style="width: 100%"></td>
</tr>
<tr>
	<td colspan="2"><input type="checkbox" name="accept_terms">با تمامی <a href="terms" style="color: blue" target="_blank">قوانین پرشین پیانو</a> موافقت می کنم.</td>
</tr>
<tr style="text-align: left"><td colspan="2">
<button type="submit" class="g-recaptcha" 
        data-sitekey="6LcV4kMaAAAAAPEYeM7lrR08Nxo-Bmo3A3NcID0w" 
        data-callback='submitregister' 
        data-action='submit' onMouseOver="style='cursor:pointer;background-color: #34FF8A;font-size: 110%;font-family: vazir;width: <?php if($ismobile==false) echo('25'); else echo('31'); ?>%;border-radius: 5px;'" style="background-color: #33DC7B;font-size: 110%;font-family: vazir;width: <?php if($ismobile==false) echo('25'); else echo('31'); ?>%;border-radius: 5px;" onMouseOut="style='background-color: #33DC7B;font-size: 110%;font-family: vazir;width: <?php if($ismobile==false) echo('25'); else echo('31'); ?>%;border-radius: 5px;'">ثبت نام</button></td></tr>
</table>
</form><br>
حساب کاربری دارید؟ 
<a href="login" style="color: blue;"><b>وارد شوید.</b></a></center>
</center></div></center></p>
<?php include("includes/footer.php") ?>