<?php
// INCLUDE HEADER IN PAGE
include(getcwd().'/data/templates/backend/superhero/other_header.php');
// GET CURRENT DATE AND TIME
$date = date('Y-m-d H:i:s');
?>
<?php
if(mysql_real_escape_string($_POST['sendgift'])) {
	$top 				= $_POST['userviews'];
	$top_username 	= $_POST['username'];
	$top_id 			= $_POST['userid'];
	$giftnum 		= $_POST['giftnum'];
	$query 			= "SELECT * FROM Accounts_Classic_Publishers WHERE id='$top_id' AND username='$top_username'";
	$result 			= mysql_query($query) or die('Error, query failed');
	while($row = mysql_fetch_array($result)){
		$email = $row['email'];
		$titleemail = $title.' - You are TOP user';
		$message = 'Hi '.$top_username.'. We are from Title team. You just got ranked in our web, you got '.$top.' views in last 7 days. We are very happy that we have members like you, so we gave you gift as a balance to your account in amount '.$giftnum.'.Which is biggest amount which we had this week. We want to give you BIG thanks, and good luck with next earnings. Thanks Title Team';
		//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 = 0;
														//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";
														//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 = "$titleemail";
														//Set the HTML body
															$mail->isHTML(true);  
														//Read an HTML message body from an external file, convert referenced images to embedded,
															$mail->Body = "$message";
														//Replace the plain text body with one created manually
															$mail->AltBody = "$title";
							
														//send the message, check for errors
															if (!$mail->send()) {
																$success = 0;
																$error = 1;
																$errortext = 'Error: #45679 - Bad connection/authorization - Email not sent';
															} else {
																$success = 1;
																$error = 0;
															}
	}
	if($success == 1) {
	?>
	<div style="margin-top: -80px;" class="alert alert-success" role="alert">
		<strong>Well done!</strong> Thanks letter was sent to email (<?php echo $email;?>) of user <?php echo $top_username; ?>. To user account was added <?php echo $giftnum;?> <?php echo $acu['ISO'];?>.
	</div>
	<?php
	}
	if($error == 1) {
	?>
	<div style="margin-top: -80px;" class="alert alert-danger" role="alert">
		<strong>Sorry!</strong> Problem in connection/authorization to your SMTP server.
	</div>
	<?php
	}
	$edit = mysql_query("UPDATE Accounts_Classic_Publishers SET balance=balance + '$giftnum' WHERE id='$top_id' AND username='$top_username'");
}

if(mysql_real_escape_string($_POST['sendthanks'])) {
	$top 				= $_POST['userviews'];
	$top_username 	= $_POST['username'];
	$top_id 			= $_POST['userid'];
	$query 			= "SELECT * FROM Accounts_Classic_Publishers WHERE id='$top_id' AND username='$top_username'";
	$result 			= mysql_query($query) or die('Error, query failed');
	while($row = mysql_fetch_array($result)){
		$email = $row['email'];
		$titleemail = $title.' - You are TOP user';
		$message = 'Hi '.$top_username.'. We are from Title team. You just got ranked in our web, you got '.$top.' views in last 7 days. Which is biggest amount which we had this week. We want to give you BIG thanks, and good luck with next earnings. Thanks Title Team';
		//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 = 0;
														//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";
														//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 = "$titleemail";
														//Set the HTML body
															$mail->isHTML(true);  
														//Read an HTML message body from an external file, convert referenced images to embedded,
															$mail->Body = "$message";
														//Replace the plain text body with one created manually
															$mail->AltBody = "$title";
							
														//send the message, check for errors
															if (!$mail->send()) {
																$success = 0;
																$error = 1;
																$errortext = 'Error: #45679 - Bad connection/authorization - Email not sent';
															} else {
																$success = 1;
																$error = 0;
															}
	}
	if($success == 1) {
	?>
	<div style="margin-top: -80px;" class="alert alert-success" role="alert">
		<strong>Well done!</strong> Thanks letter was sent to email (<?php echo $email;?>) of user <?php echo $top_username; ?>.
	</div>
	<?php
	}
	if($error == 1) {
	?>
	<div style="margin-top: -80px;" class="alert alert-danger" role="alert">
		<strong>Sorry!</strong> Problem in connection/authorization to your SMTP server.
	</div>
	<?php
	}
}
?>
  <div class="container">
    <div class="text-center">
      <h1><?php echo $title; ?>
      	<small>/Active is: Admin Account
			</small>
		</h1>
 	 </div>
 	 </br>
<?php
	$query = "SELECT * FROM Accounts_Classic_Publishers";
	$result = mysql_query($query) or die('Error, query failed');
	$top = '0';
	while($row = mysql_fetch_array($result)){
		$actual_id = $row['id'];
		$actual_username = $row['username'];
		$actual_data1 = $row['date1'];
		$actual_data2 = $row['date2'];
		$actual_data3 = $row['date3'];
		$actual_data4 = $row['date4'];
		$actual_data5 = $row['date5'];
		$actual_data6 = $row['date6'];
		$actual_data7 = $row['date7'];
		$actual_sum = $actual_data1 + $actual_data2 + $actual_data3 + $actual_data4 + $actual_data5 + $actual_data6 + $actual_data7;
		if($actual_sum > $top) {
			$top = $actual_sum;
			$top_username = $actual_username;
			$top_id = $actual_id;
		}
	}
?>
<div class="jumbotron">
  <h1><?php echo $top_username;?></h1>
  <p><?php echo $top_username;?> is TOP publisher of this week, be thankful and send to his email thanks letter or/and give him gift.</p>
  <p><?php echo $top_username;?> received <?php echo $top;?> views in last 7 days.</p>
  <form method='post'>
  	<input class='btn btn-primary' type='hidden' name='userid' value='<?php echo $top_id;?>'>
  	<input class='btn btn-primary' type='hidden' name='username' value='<?php echo $top_username;?>'>
  	<input class='btn btn-primary' type='hidden' name='userviews' value='<?php echo $top;?>'>
  	<div class="col-sm-6">
          <input type='submit' name='sendthanks' value='Send thanks letter' class='btn btn-primary'>
   </div>
   <div class="col-sm-6">
         <center>
         <div class='form-group has-success'>
				<div class="input-group">
               <span class="input-group-addon"><?php echo $acu['ISO'];?></span>
               <input type='text' name='giftnum' placeholder='Gift value' class='form-control'>
               <span class="input-group-btn">
               	<input type='submit' name='sendgift' value='Send Gift & Thanks letter' class='btn btn-primary'>
            	</span>
           	</div>
			</div>
			</center>
   </div></br></br></br></br>
  </form>
</div>
</div>

<?php
include(getcwd().'/data/templates/backend/superhero/other_footer.php');
?>
