<?php

	include "library.php";

	$cxn = start_code();

	
			/* get variables */

	$id = val( $_SESSION, "id" );

	$send = val( $_GET, "send" );
	$send_type = val( $_GET, "sendtype" );
	$send_to_id = val( $_GET, "sendtoid" );

	$returnpoint = val( $_SESSION, "returnpoint" );
	$startpos = val( $_SESSION, "startpos" );

	page_header( "", $cxn );

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

	crash_if_missing( $id, "id missing in send_message", $cxn );

	crash_if_missing( $send_to_id, "Send to id missing in send message", $cxn );

	crash_if_missing( $returnpoint, "return point missing in send message", $cxn );


	if ($send == "no")
		echo black( "Send cancelled<br><br>" );
	else
	{
		if ($send_type == "kiss")
		{
				/* check credits first so we can tell if they're needed */

			$credits_used = $_SESSION['credits_used'];
			
//			$credits_used = 0;

//			if (needs_credit( $id, $send_to_id, $cxn ))
//				$credits_used = 0.25;


				/* write the kiss to the database */

			$query = "INSERT INTO message (sender_id, receiver_id, type, sent, show_in_inbox) values (".$id.", ".$send_to_id.", 'kiss', '".$sent."', 'yes' )";
			$result = run_query( $query, $cxn );

			$query = "SELECT id as message_id FROM message WHERE sender_id = ".$id." AND receiver_id = ".$send_to_id." AND sent = '".$sent."'";
			$result = run_query( $query, $cxn );
			$data = get_row( $result );

			$message_id = $data['message_id'];


				/* send an email externally */

			$domain = val( $_SERVER, "HTTP_HOST" );

			$query = "SELECT handle FROM profile WHERE id = ".$id;
			$result = run_query( $query, $cxn );
			$data = get_row( $result );

			$subject = "You have recieved a kiss from ".$_SESSION['sitename']." member ".$data['handle'];

			$text_body = "You have recieved a kiss from ".$_SESSION['sitename']." member ".$data['handle']."\n\n";
			$text_body .= "To reply to this kiss, log into the system, select View A Profile, type in the handle and then click 'Reply to Kiss'";

			$html_body = "You have recieved a kiss from ".$_SESSION['sitename']." member <font color='#ff0000'>".$data['handle']."</font><br><br>";
			$html_body .= "To reply to this kiss click ".show_link( "reply", "http://".$domain."/display_profile.php", "showid", $id, "returnpoint", "main", "external", "true" )." and click on 'Reply to Kiss'";

			send_external( $send_to_id, $subject, $text_body, $html_body, $cxn );


				/* update the credits balance */

			if ($credits_used != 0)
			{
				$event_datetime = date("Y-m-d H:i:s");

				$query = "INSERT INTO credits (member_id, message_id, event, number, event_datetime) VALUES (".$id.", ".$message_id.", 'kiss sent', -0.25, '".$event_datetime."' )";
				$result = run_query( $query, $cxn );

				$query = "SELECT credits_balance FROM profile WHERE id = ".$id;
				$result = run_query( $query, $cxn );
				$data = get_row( $result );

				$credits_balance = $data['credits_balance'] - $credits_used;

				$query = "UPDATE profile SET credits_balance = ".$credits_balance." WHERE id = ".$id;
				$result = run_query( $query, $cxn );
			}

				/* show succeeded on screen */

			echo black( "Kiss sent<br><br>" );


				/* warning about sent to themselves */

			if ($id == $send_to_id)
			{
				echo black( "Please note that you have sent this kiss to yourself. " );
				echo black( "If you want to send a kiss to another member, go to " );
				echo black( "their profile and click 'send kiss'<br><br>" );
			}

				/* note credits used */

			if ($credits_used != 0)
			{
				if ($credits_used == 1)
					echo $credits_used." credit was used to send this kiss message<br><br>";
				else
					echo $credits_used." credits were used to send this kiss message<br><br>";

				if ($credits_balance == 1)
					echo black( "You have 1 credit left<br><br>" );
				else
					echo black( "You have ".$credits_balance." credits left<br><br>" );
			}
		}
		else
		if ($send_type == "email")
		{
//				/* check credits first so we can tell if they're needed */

			$credits_used = $_SESSION['credits_used'];


			$credits_used = 0;
			

//			if (needs_credit( $id, $send_to_id, $cxn ))
//				$credits_used = 1;


				/* send the message */

			$subject = val( $_POST, "subject" );
			$message = val( $_POST, "message" );

			if ($subject == "" && $message == "")
			{
				echo black( "This email does not have a subject or body text" );
				exit();
			}

			$sqlsubject = prep_for_sql( $subject );
			$sqlmessage = prep_for_sql( $message );

			$query = "SELECT handle FROM profile WHERE id = ".$id;
			$result = run_query( $query, $cxn );
			$data = get_row( $result );

				/* check for scam letters */

			if (strpos( strtolower( $message ), "$" ) !== false ||
				strpos( strtolower( $message ), "dollars" ) !== false)
			{
				$query = "UPDATE profile SET locked = 'yes' WHERE id = ".$id;
				$result = run_query( $query, $cxn );

				$query = "INSERT INTO account_activity (member_id, event, event_datetime) VALUES (".$id.", 'profile locked (scam detection)', '".date("Y-m-d H:i:s")."')";
				$result = run_query( $query, $cxn );
				
				send_email( "System", "markmcilroy@ozemail.com.au", "markmcilroy@ozemail.com.au", "System", "markmcilroy@ozemail.com.au", "Scam check profile ".$data['handle']." to ".$send_to_id, "", $sqlsubject.":".$sqlmessage );

				echo show_home_link();

				exit();
			}

			$handle = $data['handle'];

			$query = "INSERT INTO message (sender_id, receiver_id, type, subject, message, sent, show_in_inbox) values (".$id.", ".$send_to_id.", 'email', '".$sqlsubject."', '".$sqlmessage."', '".$sent."', 'yes' )";
			$result = run_query( $query, $cxn );

			$query = "SELECT id as message_id FROM message WHERE sender_id = ".$id." AND receiver_id = ".$send_to_id." AND sent = '".$sent."'";
			$result = run_query( $query, $cxn );
			$data = get_row( $result );

			$message_id = $data['message_id'];

			$domain = val( $_SERVER, "HTTP_HOST" );


				/* send an email externally */

			$text_body .= $message."\n\n\n";
			$text_body = "# This is a message from ".$_SESSION['sitename']." member ".$handle.". Please do not reply to this address.\n\n\n";
			$text_body .= "# To reply to this message log into the system at ".$domain." and navigate to the user's profile.\n";

			$html_body .= convert_to_html($message)."<br><br><br>";
			$html_body = "<br><br><font color='#999999'>This is a message from ".$_SESSION['sitename']." member<font color='#ff0000'> ".$handle." </font>.<br>";
			$html_body .= "<br><font color='#999999'>To reply to this message click ".show_link( "reply", "http://".$domain."/display_profile.php", "showid", $id, "returnpoint", "main", "external", "true" )."</font>";

			send_external( $send_to_id, $subject, $text_body, $html_body, $cxn );


				/* update the credits balance */

			if ($credits_used != 0)
			{
				$event_datetime = date("Y-m-d H:i:s");

				$query = "INSERT INTO credits (member_id, message_id, event, number, event_datetime) VALUES (".$id.", ".$message_id.", 'email sent', -1, '".$event_datetime."' )";
				$result = run_query( $query, $cxn );

				$query = "SELECT credits_balance FROM profile WHERE id = ".$id;
				$result = run_query( $query, $cxn );
				$data = get_row( $result );

				$credits_balance = $data['credits_balance'] - $credits_used;

				$query = "UPDATE profile SET credits_balance = ".$credits_balance." WHERE id = ".$id;
				$result = run_query( $query, $cxn );
			}


				/* show succeeded on screen */

			echo black( "Email sent<br><br>" );


				/* warning about messages sent to themselves */

			if ($id == $send_to_id)
			{
				echo black( "Please note that you have sent this message to yourself. " );
				echo black( "If you want to send a message to another member, go to " );
				echo black( "their profile and click 'send email'<br><br>" );
			}

				/* note credits used */

			if ($credits_used != 0)
			{
				if ($credits_used == 1)
					echo $credits_used." credit was used to send this email<br><br>";
				else
					echo $credits_used." credits were used to send this email<br><br>";

				if ($credits_balance == 1)
					echo black( "You have 1 credit left<br><br>" );
				else
					echo black( "You have ".$credits_balance." credits left<br><br>" );
			}

				/* chek for donation request */

//			donation_request( $id, $cxn );

		}
		else
			crash( "Internal error: invalid send type in send message", $cxn );

		$query = "UPDATE message SET has_been_replied_to = 'yes' WHERE sender_id = ".$send_to_id." AND receiver_id = ".$id;

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

	show_return_point( $returnpoint, $startpos, $send_to_id );

?>


