<?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) 2011 iDocScript.com. All rights reserved.
|**************************************************************************************************/

include("include/config.php");
include("include/functions/import.php");
$thebaseurl = $config['baseurl'];

if ($_SESSION['USERID'] != "" && $_SESSION['USERID'] >= 0 && is_numeric($_SESSION['USERID']))
{
	$UID = intval(cleanit($_SESSION['USERID']));
    $getemail = $_SESSION['EMAIL'];
	
	if ($_SESSION[VERIFIED] == "1")
	{
		$emailverified = "<font color=\"#00FF00\">$lang[8]</font>";
		$ev1 = "1";
	}
	else
	{
		$emailverified = "<font color=\"#FF0000\">$lang[9]</font>";
		$ev1 = "0";
	}
	STemplate::assign('emailverified',$emailverified);
	STemplate::assign('ev1',$ev1);
		
	if($_REQUEST['change']!="")
	{
		$cleanmail = cleanit($_REQUEST['email']);
		if(!verify_valid_email($cleanmail))
		{
			$error = $lang['498'];
		}
		elseif ($getemail == $cleanmail)
		{
			$error = $lang['499'];
		}
		elseif (!verify_email_unique($cleanmail))
		{
			$error = $lang['500'];
		}
		
		elseif ($error == "")
		{
			$newemail = $cleanmail;
			$query="UPDATE members SET email='".mysql_real_escape_string($newemail)."', verified='0' WHERE USERID='".mysql_real_escape_string($UID)."'";
			$conn->execute($query);
			
			if(mysql_affected_rows()>=1)
			{
				$_SESSION['EMAIL'] = $newemail;
				$_SESSION['VERIFIED'] = "0";
				$verifycode = generateCode(5).time();
				
				$query = "SELECT USERID FROM members_verifycode WHERE USERID='".mysql_real_escape_string($UID)."'";
                $conn->execute($query);
				if(mysql_affected_rows()>=1)
				{				
                	$query = "UPDATE members_verifycode A, members B SET A.code='$verifycode' WHERE A.USERID=B.USERID AND A.USERID='".mysql_real_escape_string($UID)."'";
                	$conn->execute($query);
					if(mysql_affected_rows()>=1)
					{
						$proceedtoemail = true;
					}
					else
					{
						$proceedtoemail = false;
					}
				}
				else
				{
                	$query = "INSERT INTO members_verifycode SET USERID='".mysql_real_escape_string($UID)."', code='$verifycode'";
                	$conn->execute($query);
					if(mysql_affected_rows()>=1)
					{
						$proceedtoemail = true;
					}
					else
					{
						$proceedtoemail = false;
					}
				}
				
				if ($proceedtoemail)
				{
                	STemplate::assign('verifycode',$verifycode);
                	$sendto = $_SESSION['EMAIL'];
                	$sendername = $config['site_name'];
					STemplate::assign('sendername',$sendername);
                	$from = $config['site_email'];
                	$subject = $sendername." ".$lang['539'];
                	$sendmailbody = $lang['540']."<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="");
				}
				
				$message = $lang['501'];
			}
			else
			{
				$error = $lang['502'];
			}
		}
		else
		{
			$error = $lang['503'];
		}		
	}
	$templateselect = "changeemail.tpl";
}
else
{
	$redirect = base64_encode($thebaseurl."/changeemail.php");
	header("Location:$config[baseurl]/login.php?redirect=$redirect");exit;
}

$pagetitle = $lang['23'];
STemplate::assign('pagetitle',$pagetitle);

//TEMPLATES BEGIN
STemplate::assign('error',$error);
STemplate::assign('message',$message);
STemplate::display('header.tpl');
STemplate::display($templateselect);
STemplate::display('footer.tpl');
//TEMPLATES END
?>