<?php
/**************************************************************************************************
|
| iDocScript - Document Sharing Script
| http://www.idocscript.com
| webmaster@idocscript.com
|
|**************************************************************************************************
|
| By using this software you agree that you have read and acknowledged our End-User License 
| Agreement available at http://www.idocscript.com/eula.html and to be bound by it.
|
| Copyright (c) iDocScript.com. All rights reserved.
|**************************************************************************************************/

include("include/config.php");
include("include/functions/import.php");
$thebaseurl = $config['baseurl'];
STemplate::assign('issignuppage',"1");

$redirect = base64_decode(cleanit($_REQUEST['redirect']));
$redirect = cleanit($redirect);
STemplate::assign('redirect',cleanit($_REQUEST['redirect']));

if ($_SESSION['USERID'] != "" && $redirect != "")
{
	header("Location:$redirect");exit;
}

if($_REQUEST['register']!="")
{
	$username = cleanit($_REQUEST['username']);
	$username = str_replace("(", "", $username);
	$username = str_replace(")", "", $username);
	$password = cleanit($_REQUEST['password']);
	$confirmpassword = cleanit($_REQUEST['confirmpassword']);
	$imagecode = cleanit($_REQUEST['imagecode']);
	$email = cleanit($_REQUEST['email']);
	$gender = intval(cleanit($_REQUEST['gender']));
	
	STemplate::assign('username',$username);
	STemplate::assign('password',$password);
	STemplate::assign('confirmpassword',$confirmpassword);
	STemplate::assign('email',$email);
	STemplate::assign('gender',$gender);
	
	if ($username == "")
	{
		$error=$lang['571'];
	}
	elseif (!verify_email_username($username))
	{
		$error = $langev['6'];
	}
	elseif(!verify_valid_email($email))
	{
		$error = $lang['498'];
	}
	elseif (!verify_email_unique($email))
	{
		$error = $lang['500'];
	}
	elseif ($password == "")
	{
		$error=$lang['573'];
	}
	elseif ($confirmpassword == "")
	{
		$error=$lang['574'];
	}
	elseif ($password != $confirmpassword)
	{
		$error=$lang['575'];
	}
	elseif($imagecode != $_SESSION[imagecode])
	{
		if ($config['enable_captcha'] != "0")
		{
			$error=$lang['576'];
		}
	}
	
	if ($error == "")
	{
		$md5pass = md5($password);
		if($config['man_mem_val'] == "1")
		{
			$addtosql = ", status='0'";
		}
		$query="INSERT INTO members SET email='".mysql_real_escape_string($email)."',username='".mysql_real_escape_string($username)."',gender='".mysql_real_escape_string($gender)."', password='".mysql_real_escape_string($md5pass)."', addtime='".time()."', lastlogin='".time()."', ip='".$_SERVER['REMOTE_ADDR']."', lip='".$_SERVER['REMOTE_ADDR']."' $addtosql";
		$result=$conn->execute($query);
		$userid = mysql_insert_id();
		
		if($userid != "" && is_numeric($userid) && $userid > 0)
		{
			$query="SELECT USERID,email,username,verified,familyfilter from members WHERE USERID='".mysql_real_escape_string($userid)."'";
			$result=$conn->execute($query);
			
			$SUSERID = $result->fields['USERID'];
			$SEMAIL = $result->fields['email'];
			$SUSERNAME = $result->fields['username'];
			$SVERIFIED = $result->fields['verified'];
			$SFAMILYFILTER = $result->fields['familyfilter'];
			if($config['man_mem_val'] != "1")
			{
				$_SESSION['USERID']=$SUSERID;
				$_SESSION['EMAIL']=$SEMAIL;
				$_SESSION['USERNAME']=$SUSERNAME;
				$_SESSION['VERIFIED']=$SVERIFIED;
				$_SESSION['FAMILYFILTER']=$SFAMILYFILTER;
			}
			
			if($_REQUEST["remember"]=="yes")
			{
				create_slrememberme();
			}
			
			// Generate Verify Code Begin
			$verifycode = generateCode(5).time();
			$query = "INSERT INTO members_verifycode SET USERID='".mysql_real_escape_string($SUSERID)."', code='$verifycode'";
            $conn->execute($query);
			if(mysql_affected_rows()>=1)
			{
				$proceedtoemail = true;
			}
			else
			{
				$proceedtoemail = false;
			}
			// Generate Verify Code End
			
			// Send Welcome E-Mail Begin
			if ($proceedtoemail)
			{
                STemplate::assign('verifycode',$verifycode);
                $sendto = $SEMAIL;
                $sendername = $config['site_name'];
				STemplate::assign('sendername',$sendername);
                $from = $config['site_email'];
				$subject = $lang['579']." ".$sendername;
				$sendmailbody = $lang['790']." ".$_SESSION['USERNAME'].",<br><br>";
				$sendmailbody .= $lang['638']."<br><br>";
				$sendmailbody .= "<a href='".$thebaseurl."/confirmemail/".$verifycode."'>".$thebaseurl."/confirmemail/".$verifycode."</a><br><br>";
				$sendmailbody .= $lang['578'].",<br>";
				$sendmailbody .= $sendername;
                mailme($sendto,$sendername,$from,$subject,$sendmailbody,$bcc="");
			}
			// Send Welcome E-Mail End
			
			if($config['man_mem_val'] != "1")
			{
				if($redirect == "")
				{
					header("Location:$config[baseurl]");exit;
				}
				else
				{
					header("Location:$redirect");exit;
				}
			}
			else
			{
				$showmessage = $langev['5'];
			}
		}
		else
		{
			$error=$lang['577'];
		}
	}
}

$pagetitle = $lang['102'];
STemplate::assign('pagetitle',$pagetitle);
STemplate::assign('message',$message);
STemplate::assign('error',$error);
STemplate::display('header.tpl');
STemplate::display('signup.tpl');
STemplate::display('footer.tpl');
?>