<?php
	
	include_once "library.php";


            // command line invocation
            
    if (isset( $argv ))
    {
        if ($argv[1] == "sendit=true")
            $send_it = "true";
    }
    else
    	$send_it = val( $_GET, 'sendit' );

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

//	$cxn = start_code( true );


		/* kill the redirection of the email to the debugging address */
		/* if logged in as me */

//	session_destroy();

    $count = 0;
    
	$subject = "";
	$html_body = "";

	$today_date = date_create( date("Y-m-d") );
	$today_minus_30_days = clone $today_date;
		
	date_sub( $today_minus_30_days, date_interval_create_from_date_string( '30 days' ));
    $today_minus_30_days_str = $today_minus_30_days->format( "Y-m-d" );		


	$query = "SELECT * FROM profile WHERE deleted = 'no' and returned_mail = 'no' and email_address_valid = 'yes' and date_created > '".$today_minus_30_days_str."'";
	$query .= " AND id not in (SELECT sent_to_id FROM sys_message WHERE type = 'age_error_email') 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 );

		$date_of_birth = $data['date_of_birth'];
		$date_format = $data['date_format'];
		
		if ($date_of_birth != "" && $date_format != "")
		{
			$age = check_date( convert_date_of_birth(prep_for_sql( $date_of_birth ), str_replace( "'", "''", "yyyy/mm/dd"), $cxn ), $cxn);

			if ($age < 18 || $age > 110)
			{
				$domain = val( $_SERVER, "HTTP_HOST" );
	
				$subject = "Profile problem message from ".$_SESSION['sitename'];
	
				$text_body = "Dear ".name_case( $data['first_name'] ).",\n\n";
				$text_body .= "Thank you for joining ".$_SESSION['sitename'].".\n\n";
				$text_body .= "There is a problem with your profile.\n\n";
				$text_body .= "The system cannot calculate your age based on the Date of Birth and Date Format that you have selected.\n\n";
				$text_body .= "Your Date of Birth is recorded as ".$date_of_birth."\n\n";
				$text_body .= "Can you please log into the system, select 'Edit Your Profile', and enter your correct Date of Birth and Date Format. In particular can you please ensure that you select the correct Date Format that matches the Date of Birth that you have entered.\n\n\n";
				$text_body .= "Kind Regards\n\n";
				$text_body .= "Chat City\n\n";
				$text_body .= "www.chatcity1.net";
	
				$html_body = "Dear ".name_case( $data['first_name'] ).",<br><br>";
				$html_body .= "Thank you for joining ".$_SESSION['sitename'].".<br><br>";
				$html_body .= "There is a problem with your profile.<br><br>";
	
				$html_body .= "The system cannot calculate your age based on the Date of Birth and Date Format that you have selected.<br><br>";
				$html_body .= "Your Date of Birth is recorded as ".$date_of_birth."<br><br>";
				$html_body .= "Can you please log into the system, select 'Edit Your Profile', and enter your correct Date of Birth and Date Format. In particular can you please ensure that you select the correct Date Format that matches the Date of Birth that you have entered.<br><br><br>";
				$html_body .= "<br><br>Kind Regards<br><br>";
				$html_body .= "Chat City<br><br>";
				$html_body .= show_link( "www.chatcity1.net", "http://chatcity1.net" );
	
				if ($send_it == "true")
				{
					send_external( $data['id'], $subject, $text_body, $html_body, $cxn, $cxn_common, "live" );
	
	//				$query2 = "UPDATE profile SET photo_email_sent = 'yes' WHERE id = ".$data['id'];
	//				$result2 = run_query( $query2, $cxn );
	
					$query2 = "INSERT INTO sys_message (sent_to_id, type, sent) VALUES (".$data['id'].", 'age_error_email', '".$sent."')";
					$result2 = run_query( $query2, $cxn );
					
					$count++;
					$total_emails++;
				}
	
				echo black( "Age error email set to member ".$data['handle'].", DOB ".$date_of_birth.", date format ".$date_format.", age ".$age."<br>" );
			}
		}
	}


    echo $subject."<br><br>".$html_body;

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