<?php

//	include_once "library.php";

	$send_it = val( $_GET, 'sendit' );

	if ($send_it <> "true")
		echo black( "TEST MODE<br><br>" );

//	$cxn = start_code( true );


	$today_date = date_create( date("Y-m-d") );

    $today_minus_365_days = clone $today_date;

	date_sub( $today_minus_365_days, date_interval_create_from_date_string( '365 days' ));
	$today_minus_365_days_str = $today_minus_365_days->format( "Y-m-d" );		

	$query = "SELECT * FROM profile WHERE ";
	$query .= " deleted = 'no' AND returned_mail = 'no' and email_address_valid = 'no' and date_created > '".$today_minus_365_days_str."'";
	$query .= " ORDER BY id DESC";

	$sent = date("Y-m-d H:i:s");

	$result = run_query( $query, $cxn );

	for ($i=0; $i < query_num_rows($result); $i++)
	{
		$data = get_row( $result );

		$member_id = $data['id'];
		$email_verification_code = $data['email_verification_code'];

		if (query_has_rows( "SELECT id FROM sys_message WHERE sent_to_id = ".$member_id." AND type = 'verification_email_resend'", $cxn ))
		{
			$data2 = get_first_row( "SELECT * FROM sys_message WHERE sent_to_id = ".$member_id." AND type = 'verification_email_resend' ORDER BY sent DESC", $cxn );
			$last_attempt = $data2['sent'];
			
			$query2 = "SELECT count(*) as num FROM sys_message WHERE sent_to_id = ".$member_id." AND type = 'verification_email_resend'";
			$result2 = run_query( $query2, $cxn );
			$data2 = get_row( $result2 );
			$num_attempts = $data2['num'];
			
		}
		else
		{
			$last_attempt = $data['date_created'];
			$num_attempts = 0;
		}

        
			// send one reminder only to reduce the chance of being maked as spam

		if ($num_attempts < 1 && ((time() - strtotime( $last_attempt )) > 3*24*60*60 ))
		{
			if ($send_it == "true")
			{
				send_verification_email( $member_id, $email_verification_code, $cxn, $cxn_common );

				run_query( "INSERT INTO sys_message (sent_to_id, type, sent) VALUES (".$member_id.", 'verification_email_resend', '".$sent."')", $cxn );
			}

			echo black( "Verification email reminder set to member ".$data['handle']."<br>" );
		}
	}

	echo black( "<br>Finished<br><br>" );
	
?>