<?php
//Starting session
session_start();
// ADDING PROTECTION TO SITE
include("../../functions.php");
include("../../configuration.php");
include("../../dbhandle.php");
include("../../security.php");
include("class.country.php");
//Load time function (in footer)
$time = microtime(true);
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
if($_COOKIE['tref'])
{
$ref = $_COOKIE['tref'];
} else {
$ref = $_GET['refid'];
setcookie("tref", $ref, time()+60*300);
}
$countryreg = ip_info("Visitor", "Country");
if (isset($countryreg)){
	$countryreg = $countryreg;
}else{
	$countryreg = $country_name;
}

if($countryreg == '') {
$countryreg = "United States";
}
// GETTING IP
$uip = getIP();

$session_username = mysql_real_escape_string($_SESSION['username']);
$session_password = mysql_real_escape_string($_SESSION['password']);

if($_SESSION['acctype'] == '1')  {

$startq = mysql_query("SELECT * FROM Accounts_Classic_Advertisers WHERE username='$session_username' AND password='$session_password'");
$startf = mysql_num_rows($startq);
$see = mysql_fetch_array($startq);


if ($startf == '0') {
$aloggedin = 0;
}
else
{
$aloggedin = 1;
}

// IF LOGGED IN ADVERTISER
if($aloggedin == '1') {
echo"<script type='text/javascript'>window.location= 'p.Dashboard'</script>";
}
}
else
{
$startq = mysql_query("SELECT * FROM Accounts_Classic_Publishers WHERE username='$session_username' AND password='$session_password'");
$startf = mysql_num_rows($startq);
$see = mysql_fetch_array($startq);

if ($startf == '0') {
$loggedin = 0;
}
else
{
$loggedin = 1;
}
// IF LOGGED IN PUBLISHER
if($loggedin == '1') {
echo"<script type='text/javascript'>window.location= 'p.Dashboard'</script>";
}
}
// ADDING MAIL FUNCTION
include("../../../data/plugins/phpmailer/PHPMailerAutoload.php");

if ( isset( $_POST[ 'field' ] ) )
{
    try
    {
        // Run CSRF check, on POST data, in exception mode, for 10 minutes, in one-time mode.
        NoCSRF::check( 'csrf_token', $_POST, true, 60*10, false );
        // form parsing, DB inserts, etc.
        // ...
        $result = 'CSRF check passed. Form parsed.';
    }
    catch ( Exception $e )
    {
        // CSRF attack detected
        echo"<script type='text/javascript'>javascript: window.history.go(-1);</script>";
    }
}

// DETECTING TIME AND DATE
$date = date('Y-m-d H:i:s');
$site = $_SERVER['HTTP_HOST'];

if($_POST['submit']) {
$username = $_POST['login_username'];
$username = str_replace("<", "", "$username");
$username = str_replace(">", "", "$username");
$username = str_replace("'", "", "$username");
$username = str_replace('"', "", "$username");
$username = str_replace("(", "", "$username");
$username = str_replace(")", "", "$username");
$email = $_POST['email'];
$email = str_replace("<", "", "$email");
$email = str_replace(">", "", "$email");
$email = str_replace("'", "", "$email");
$email = str_replace('"', "", "$email");
$email = str_replace("(", "", "$email");
$email = str_replace(")", "", "$email");

$username = AntiXSS::setEncoding($username, "UTF-8"); 
$email = AntiXSS::setEncoding($email, "UTF-8"); 
$newpass = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 10);
$newpassh = md5($newpass);
// IF USERNAME DOESN'T EXIST
$querysearch = mysql_query("SELECT * FROM Accounts_Classic_Publishers WHERE username='$username' AND email='$email'");
$queryrows = mysql_num_rows($querysearch);
if($queryrows != 0) {
	//SMTP needs accurate times, and the PHP time zone MUST be set
	//This should be done in your php.ini, but this is how to do it if you don't have access to that
		date_default_timezone_set('Etc/UTC');
		$mail = new PHPMailer();
		//$mail->isSMTP();
		$mail->SMTPDebug = 2;
		$mail->Debugoutput = 'html';
		$mail->Host = "$smtphost";
		$mail->Port = "$smtpport";
		$mail->SMTPAuth = "$smtpauth";
		$mail->SMTPDebug = 0;
		$mail->Username = "$smtpusername";
		$mail->Password = "$smtppass";
		$mail->setFrom("$frommail", "$title");
		$mail->addReplyTo("$replytoemail", "$title");
		$mail->addAddress("$email", "$title");
		$mail->Subject = "$title | Registration";
		$mail->isHTML(true);  
		$mail->Body = "Hi <b>$username</b> <br><br> We just got request to reset your password wia our portal. We successfully generated your your password. <br><br><br> New password is: $newpass <br><br><br> Best Regards,</br> $title Team";
		$mail->AltBody = "$title";
		if (!$mail->send()) {
			die('Error: #45679 - Email not sent');
		} else {
			mysql_query("UPDATE Accounts_Classic_Publishers SET password='$newpassh' WHERE username='$username' AND email='$email'");
			mysql_query("UPDATE Accounts_Classic_Advertisers SET password='$newpassh' WHERE username='$username' AND email='$email'");
			?>
								<form action='f.Home' method='post' name='frm'>
									<input type='hidden' name='success' value='checkmail'>
								</form>
								<script language="JavaScript">
									document.frm.submit();
								</script>
								<?php
		}
  }else{// COUNLDN'T COMPLETE >> FILL ALL FIELDS
	 							?>
								<form action='f.Home' method='post' name='frm'>
									<input type='hidden' name='error' value='notexist'>
								</form>
								<script language="JavaScript">
									document.frm.submit();
								</script>
								<?php
  }
}
?>