<?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;
$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");
$refid = $_POST['refid'];
$refid = str_replace("<", "", "$refid");
$refid = str_replace(">", "", "$refid");
$refid = str_replace("'", "", "$refid");
$refid = str_replace('"', "", "$refid");
$refid = str_replace("(", "", "$refid");
$refid = str_replace(")", "", "$refid");
$password = md5($_POST['pass1']);
$password = str_replace("<", "", "$password");
$password = str_replace(">", "", "$password");
$password = str_replace("'", "", "$password");
$password = str_replace('"', "", "$password");
$password = str_replace("(", "", "$password");
$password = str_replace(")", "", "$password");
$rpassword = md5($_POST['pass2']);
$rpassword = str_replace("<", "", "$rpassword");
$rpassword = str_replace(">", "", "$rpassword");
$rpassword = str_replace("'", "", "$rpassword");
$rpassword = str_replace('"', "", "$rpassword");
$rpassword = str_replace("(", "", "$rpassword");
$rpassword = str_replace(")", "", "$rpassword");
$country = $countryreg;
$country = str_replace("<", "", "$country");
$country = str_replace(">", "", "$country");
$country = str_replace("'", "", "$country");
$country = str_replace('"', "", "$country");
$country = str_replace("(", "", "$country");
$country = str_replace(")", "", "$country");

// IF ALL DATA IS OK
if($username&&$password&&$rpassword&&$email&&$country) {

  // IF PASSWORDS IN FORMS ARE SAME
  if($password == $rpassword) {
						// PROTECTION XSS
						$username = AntiXSS::setEncoding($username, "UTF-8"); 
						// IF USERNAME DOESN'T EXIST
						$querysearch = mysql_query("SELECT * FROM Accounts_Classic_Publishers WHERE username='$username'");
						$queryrows = mysql_num_rows($querysearch);
						// IF EMAIL DOESN'T EXIST
						$querysearchE = mysql_query("SELECT * FROM Accounts_Classic_Publishers WHERE email='$email'");
						$queryrowsE = mysql_num_rows($querysearchE);
						// IF USERNAME DOESN'T EXIST
						$querysearchadv = mysql_query("SELECT * FROM Accounts_Classic_Advertisers WHERE username='$username'");
						$queryrowsadv = mysql_num_rows($querysearchadv);
						// IF EMAIL DOESN'T EXIST
						$querysearchadvE = mysql_query("SELECT * FROM Accounts_Classic_Advertisers WHERE email='$email'");
						$queryrowsadvE = mysql_num_rows($querysearchadvE);
						// IF USERNAME NOT FOUND >> NOT EXIST YET
						if($queryrows == 0 && $queryrowsE == 0 && $queryrowsadv == 0 && $queryrowsadvE == 0) {
											// PROTECTIONS XSS
											$username = AntiXSS::setEncoding($username, "UTF-8"); 
											$password = AntiXSS::setEncoding($password, "UTF-8"); 
											$email = AntiXSS::setEncoding($email, "UTF-8"); 
											$refid = AntiXSS::setEncoding($refid, "UTF-8"); 
											$country = AntiXSS::setEncoding($country, "UTF-8"); 
											$date = AntiXSS::setEncoding($date, "UTF-8"); 
											$uip = AntiXSS::setEncoding($uip, "UTF-8");
											$hash = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 45);
											$api = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 45);
											// INSERTING NEW USER TO DB
														//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');
							
														//Create a new PHPMailer instance
															$mail = new PHPMailer();
														//Tell PHPMailer to use SMTP
															$mail->isSMTP();
														//Enable SMTP debugging
														// 0 = off (for production use)
														// 1 = client messages
														// 2 = client and server messages
															$mail->SMTPDebug = 2;
														//Ask for HTML-friendly debug output
															$mail->Debugoutput = 'html';
														//Set the hostname of the mail server
															$mail->Host = "$smtphost";
														//Set the SMTP port number - likely to be 25, 465 or 587
															$mail->Port = "$smtpport";
														//Whether to use SMTP authentication
															$mail->SMTPAuth = "$smtpauth";
															$mail->SMTPDebug = 0;
														//Username to use for SMTP authentication
															$mail->Username = "$smtpusername";
														//Password to use for SMTP authentication
															$mail->Password = "$smtppass";
														//Set who the message is to be sent from
															$mail->setFrom("$frommail", "$title");
														//Set an alternative reply-to address
															$mail->addReplyTo("$replytoemail", "$title");
														//Set who the message is to be sent to
															$mail->addAddress("$email", "$title");
														//Set the subject line
															$mail->Subject = "$title | Registration";
														//Set the HTML body
															$mail->isHTML(true);  
														//Read an HTML message body from an external file, convert referenced images to embedded,
															$mail->Body = "Hi <b>$username</b> <br><br> You just created new account on our $title advertising platform. <br> To complete your registration move to this site and activate account:<br><br> {$mainurl}activation.{$hash} <br><br><br> Best Regards,</br> $title Team";
														//Replace the plain text body with one created manually
															$mail->AltBody = "$title";
							
														//send the message, check for errors
															if (!$mail->send()) {
																// ERROR MESSAGE
																die('Error: #45679 - Email not sent|Publisher|' . $smtphost . '|' . $smtpport . '|' . $smtpauth . '|' . $smtpusername . '|' . $smtppass . '|' . $frommail . '|' . $replytoemail . '|' . $email . '|');
															} else {
																// SUCCESS MESSAGE
																$addaccount = mysql_query("INSERT INTO Accounts_Classic_Publishers VALUES('','$username','$password','$email','','','','','','$refid','0','0.00','0.00','0.00','0','','','','','','$country','$date','$uip','$uip','','','','','','','0','0','0','0','0','0','0','0','0','0','0','0','0','0','$hash','$api')");
															}
											$qstats = mysql_query("UPDATE Web_Stats SET reg7=reg7 + '1' WHERE id='1'");
											if($refid != '') {						
												$qqq = mysql_query("UPDATE Accounts_Classic_Publishers SET totalrefs=totalrefs + '1' WHERE username='$refid'");
											}
														//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');
							
														//Create a new PHPMailer instance
															$mail = new PHPMailer();
														//Tell PHPMailer to use SMTP
															$mail->isSMTP();
														//Enable SMTP debugging
														// 0 = off (for production use)
														// 1 = client messages
														// 2 = client and server messages
															$mail->SMTPDebug = 2;
														//Ask for HTML-friendly debug output
															$mail->Debugoutput = 'html';
														//Set the hostname of the mail server
															$mail->Host = "$smtphost";
														//Set the SMTP port number - likely to be 25, 465 or 587
															$mail->Port = "$smtpport";
														//Whether to use SMTP authentication
															$mail->SMTPAuth = "$smtpauth";
															$mail->SMTPDebug = 0;
														//Username to use for SMTP authentication
															$mail->Username = "$smtpusername";
														//Password to use for SMTP authentication
															$mail->Password = "$smtppass";
														//Set who the message is to be sent from
															$mail->setFrom("$frommail", "$title");
														//Set an alternative reply-to address
															$mail->addReplyTo("$replytoemail", "$title");
														//Set who the message is to be sent to
															$mail->addAddress("$email", "$title");
														//Set the subject line
															$mail->Subject = "$title | Registration";
														//Set the HTML body
															$mail->isHTML(true);  
														//Read an HTML message body from an external file, convert referenced images to embedded,
															$mail->Body = "Hi <b>$username</b> <br><br> You just created new account on our $title advertising platform. <br> To complete your registration move to this site and activate account:<br><br> {$mainurl}activation.{$hash} <br><br><br> Best Regards,</br> $title Team";
														//Replace the plain text body with one created manually
															$mail->AltBody = "$title";
							
														//send the message, check for errors
															if (!$mail->send()) {
																// ERROR MESSAGE
																die('Email not sent');
															} else {
																// INSERTING NEW USER TO DB
																$addaccount = mysql_query("INSERT INTO Accounts_Classic_Advertisers VALUES('','$username','$password','$email','','','','','','$refid','0.00','0.00','0','','','','','','$country','$date','$uip','$uip','','','','','','','0','0','0','0','0','0','0','$hash','$api')");
																$qstats = mysql_query("UPDATE Web_Stats SET areg7=areg7 + '1' WHERE id='1'");
																// SUCCESS MESSAGE
																?>
																<form action='f.Home' method='post' name='frm'>
																	<input type='hidden' name='success' value='accountsignedup'>
																</form>
																<script language="JavaScript">
																	document.frm.submit();
																</script>
																<?php
															}
											if($refid != '') {
													$qqq = mysql_query("UPDATE Accounts_Classic_Publishers SET totalrefs=totalrefs + '1' WHERE username='$refid'");
											}
						
						}else{// USERNAME EXIST
								?>
								<form action='f.Home' method='post' name='frm'>
									<input type='hidden' name='error' value='userexist'>
								</form>
								<script language="JavaScript">
									document.frm.submit();
								</script>
								<?php
						}
   }else{// PASSWORDS AREN'T SAME IN FORM
	  							?>
								<form action='f.Home' method='post' name='frm'>
									<input type='hidden' name='error' value='repeatpassword'>
								</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='fillall'>
								</form>
								<script language="JavaScript">
									document.frm.submit();
								</script>
								<?php
  }
}
?>