<?php

	include "library.php";

	$cxn = start_code();

	$cxn_common = db_common_login();


	
			/* 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, $cxn_common );

	$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 );

	$ip_address = get_ip_address();	
	
	if ($send == "no")
		echo black( "Send cancelled<br><br>" );
	else
	{
		if ($send_type == "contact_request")
		{
				/* 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 contact_request to the database */

			$query = "INSERT INTO message (sender_id, receiver_id, type, sent, show_in_inbox, ip_address) values (".$id.", ".$send_to_id.", 'contact_request', '".$sent."', 'yes', '".$ip_address."' )";
			$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'];
    
            $query2 = "SELECT * FROM profile WHERE id = ".$id;
			$result2 = run_query( $query2, $cxn );
			$data_from = get_row( $result2 );
            

				/* 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 = "Contact Request message from ".$_SESSION['sitename']." member ".$data['handle'];

			$text_body = "You have received a Contact Request message from ".$_SESSION['sitename']." member '".$data['handle']."'\n\n";
			
			if ($data_from['sex'] == "Male")
    			$text_body = "If you are interested in contacting him, please log into the site and send him an email.\n\n";			
    		else
    			$text_body = "If you are interested in contacting her, please log into the site and send her an email.\n\n";			
			

			$html_body = "You have received a Contact Request message from ".$_SESSION['sitename']." member '".$data['handle']."'<br><br>";

			if ($data_from['sex'] == "Male")
    			$html_body .= "If you are interested in contacting him, please log into the site and send him an email at ".show_link( "reply", val( $_SESSION, "http_protocol" )."://".$domain."/display_profile.php", "showid", $id, "returnpoint", "main", "external", "true" );
            else
    			$html_body .= "If you are interested in contacting her, please log into the site and send her an email at ".show_link( "reply", val( $_SESSION, "http_protocol" )."://".$domain."/display_profile.php", "showid", $id, "returnpoint", "main", "external", "true" );
			
//			$html_body .= "To reply to this kiss click ".show_link( "reply", val( $_SESSION, "http_protocol" )."://".$domain."/display_profile.php", "showid", $id, "returnpoint", "main", "external", "true", "kissreply", "true" )." and click on 'Reply to Kiss'";

			send_external( $send_to_id, $subject, $text_body, $html_body, $cxn, $cxn_common, "live" );


				/* 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.", 'contact request 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( "<br>Contact Request sent<br><br><br>" );


				/* warning about sent to themselves */

			if ($id == $send_to_id)
			{
				echo black( "Please note that you have sent this contact request to yourself. " );
				echo black( "If you want to send a contact request to another member, go to " );
				echo black( "their profile and click 'Send Contact Request'<br><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'];


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


				/* send the message */

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

            $send_email_system_password = val( $_POST, "send_email_system_password" );
            
            
            if ($send_email_system_password != "1234abcd%^%")
            {
                echo "Internal error, incorrect system password.<br><br>";
                
                echo show_home_link();
                
                exit();
            }
            else
            {
    			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 );
    
    
    				// check for the email being a business proposal
    				
    			$points = 0;

    			if (strpos( $message, "000" ) !== false)
    				$points += 1;

    			if (strpos( $message, "important" ) !== false)
    				$points += 1;
    
    			if (strpos( $message, "business" ) !== false)
    				$points += 1;
    				
    			if (strpos( $message, "bank" ) !== false)
    				$points += 1;
    
    			if (strpos( $message, "deposit" ) !== false)
    				$points += 1;
    
    			if (strpos( $message, "money" ) !== false)
    				$points += 1;
    
    			if (strpos( $message, "agreement" ) !== false)
    				$points += 1;
    
    			if (strpos( $message, "invest" ) !== false)
    				$points += 1;
    
    			if (strpos( $message, "illion" ) !== false)
    				$points += 1;
    
    			if (strlen( $message ) >= 1300)
    				$points += 1;
    			
    			if ($points >= 4)
    			{
    				echo black( "Our system has assessed your message as a business proposal.<br><br>" );
    				echo black( "Please note that it is against the site Terms of Service to send business proposals to other members.<br><br>" );
    				echo black( "Your message will not be sent.<br><br><br>" );
    				echo show_home_link();
    				exit();
    			}
    
    
    			$query = "SELECT handle FROM profile WHERE id = ".$id;
    			$result = run_query( $query, $cxn );
    			$data = get_row( $result );
    

    			$handle = $data['handle'];
    
    			$query = "INSERT INTO message (sender_id, receiver_id, type, subject, message, sent, show_in_inbox, ip_address) values (".$id.", ".$send_to_id.", 'email', '".$sqlsubject."', '".$sqlmessage."', '".$sent."', 'yes', '".$ip_address."' )";
    			$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";
    			$text_body .= "\n\nIf you want to block further communication with this member you can do that on the member's profile.";
    
    //			$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", val( $_SESSION, "http_protocol" )."://".$domain."/display_profile.php", "showid", $id, 
    			
    			$html_body = convert_to_html($message)."<br><br><br>";
    			$html_body .= "<br><br><br><span style=\"color: #999999\">This is a message from ".$_SESSION['sitename']." member</span><span style=\"color: #dd0000\"> '".$handle."'.</span><br>";
    			$html_body .= "<br><span style=\"color: #999999\">To reply to this message click</span> ".show_link( "reply", val( $_SESSION, "http_protocol" )."://".$domain."/display_profile.php", "showid", $id, "returnpoint", "main", "external", "true" );
    
    			$html_body .= "<br><br><span style=\"color: #999999\">If you want to block further communication with this member you can do that on the same 'reply' page.</span>";
    			
    			send_external( $send_to_id, $subject, $text_body, $html_body, $cxn, $cxn_common, "live" );
    
    
    				/* 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( "<br>Email sent<br><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 remaining<br><br>" );
    			}
    
    				/* check 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 );
	}


            // slow the system down to discourage members from sending large numbers of emails, there's no other effective way of stopping them.
//    sleep( 2 );
    
    
	show_return_point( $returnpoint, $startpos, $send_to_id );


		// adsense ads are not permitted on control-flow, non-content pages.
		
//	if (! low_res_screen())
//	{
//		echo "<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>";
//	
////		show_double_ads( $cxn );
//		
//		show_ads( $cxn );		
//	}
//	else
//	{
//		echo "<br><br><br><br><br><br><br><br><br><br><br>";
//	
//		
////		show_ads_large_square( $cxn );
//		
//		show_ads_narrow_banner();		
//	}
	
	
?>