Location: PHPKode > projects > WB News > base/SendFriend.php
<?php

/*========================================================*\
||########################################################||
||#                                                      #||
||#     WB News v2.0.0                                   #||
||# ---------------------------------------------------- #||
||#     Copyright (c) 2004-2008                          #||
||#     Created: 15th Jan 2008                           #||
||#     Filename: SendFriend.php                         #||
||#                                                      #||
||########################################################||
/*========================================================*/

/**
 *  @author $Author: pmcilwaine $
 *	@version $Id: SendFriend.php,v 1.1.2.4.2.1 2008/07/14 11:02:38 pmcilwaine Exp $
 */

require_once( $config["installdir"] . "/base/News.php" );

class SendFriend extends News
{

	var $newsid;

	function SendFriend( $newsid )
	{
		$this->newsid = $newsid;
		$this->News();
	}

	/**
	 *	Runs DisplayNewsArticle from News.php
	 *	@since 2.0
	 *	
	 */
	function ShowNews()
	{
		return $this->DisplayNewsArticle( $this->newsid );
	}

	/**
	 * Shows a form to be filled out to send to a friend. It also handles the post
	 * and sends off the sendEmail if it is Ok to be sent out.
	 * 
	 * @since 2.0
	 */
	function ShowForm()
	{
		if ( $this->config["systemstatus"] )
		{
			return;
		}
	
		global $auth;

		$myform = "send-friend";
		if ( $_SERVER["REQUEST_METHOD"] == "POST" && $_POST["form"] == $myform )
		{
			
			switch ( Submit() )
			{
			case "Send_Friend":
				
				$err_msg = array();
				
				$to_name = sanitize_post( "to_name" );
				$to_email = sanitize_post( "to_email" );
				$from_name = sanitize_post( "from_name" );
				$from_email = sanitize_post( "from_email" );
				$message = sanitize_post( "message", "multiline" );

				if ( !preg_match( "/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+[a-zA-Z0-9_-]$/", $to_email )  )
				{
					$err_msg["to_email"] = "Invalid email format";
				}

				if ( !preg_match( "/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+[a-zA-Z0-9_-]$/", $from_email ) )
				{
					$err_msg["from_email"] = "Invalid email format";
				}
				
				if ( "" == $to_name )
				{
					$err_msg["to_name"] = "To Name must not be empty";
				}
			
				if ( "" == $from_name )
				{
					$err_msg["from_name"] = "From Name must not be empty";
				}
			
				if ( "" == $message )
				{
					$err_msg["message"] = "Message must not be empty";
				}
			
				if ( count($err_msg) > 0 )
				{
					$_SESSION["formdata"] =& $_POST;
					$_SESSION["err_msg"][$myform] = $err_msg;
					break;
				}

				$this->to_name = $to_name;
				$this->to_email = $to_email;
				$this->from_name = $from_name;
				$this->from_email = $from_email;
				$this->message = $message;

				$this->sendEmail();

				$this->tmpl->SetFilename( BuildPath( "send-friend-sent.ihtml" ) );
				return $this->tmpl->GetHTML();
			}
			
		}

		$this->tmpl->SetFilename( BuildPath( "send-friend.ihtml" ) );

		$formdata =& $this->tmpl->AddParam( "formdata", array() );
		$this->tmpl->AddParam( "action", make_url_html() );

		$formdata["hidden"] = array(
			"form" => $myform
			);
		
		$formdata["to_name"] = NULL;
		$formdata["to_email"] = NULL;
		$formdata["from_name"] = NULL;
		$formdata["from_email"] = NULL;
		$formdata["message"] = NULL;
		
		$this->tmpl->AddParam( "buttons", "Send Friend" );
		
		if ( isset($_SESSION["formdata"]) )
		{
			if ( isset($_SESSION["err_msg"][$myform]) )
			{
				$this->tmpl->AddParam( "msg", $_SESSION["err_msg"][$myform] );
			}

			$formdata["to_name"] = sanitize_post_html( "to_name", NULL, $_SESSION["formdata"] );
			$formdata["to_email"] = sanitize_post_html( "to_email", NULL, $_SESSION["formdata"] );
			$formdata["from_name"] = sanitize_post_html( "from_name", NULL, $_SESSION["formdata"] );
			$formdata["from_email"] = sanitize_post_html( "from_email", NULL, $_SESSION["formdata"] );
			$formdata["message"] = sanitize_post_html( "message", "multiline", $_SESSION["formdata"] );

			unset( $_SESSION["formdata"], $_SESSION["err_msg"][$myform] );
		}
		
		return $this->tmpl->GetHTML();
	}
    
    /**
	 * Sends an email out to users friend
	 *
	 * @since version 1.0
	 * @return void
	 */
    function sendEmail()
    {
		$headers  = "MIME-Version: 1.0\r\n";
		$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
		$headers .= "From: " . $this->config['sitename'] . " <" . $this->config['adminemail'] . ">\r\n";
		$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";

		$msg = $this->config['sendtomsg'];

		$url = str_replace( "{newsid}", $this->newsid, $this->config["newsdisplay"] );
		$msg = str_replace( "{email}", $this->from_email, $msg);
		$msg = str_replace( "{url}", str_replace("&amp;", "&", $url), $msg );
		$msg = str_replace( "{usermsg}", $this->message, $msg);
		$msg = str_replace( "{adminemail}", $this->config['adminemail'], $msg );
		
		@mail( $sendto, "View News Article", stripslashes($msg), $headers);

		$new_id = $this->DB->NewID( SEQ_PREFIX . "seq_sendfriend" );
		$this->DB->query("INSERT INTO " . TBL_SEND . "
						(id, newsid, time, email_to, email_from, message, ipaddress)
						VALUES ('$new_id', '" . (int)$this->newsid . "', '" . time() . "', '" . $this->DB->escape($this->to_email) . "',
						'" . $this->DB->escape($this->from_email) . "', '" . $this->DB->escape($this->message) . "',
						'" . $_SERVER['REMOTE_ADDR'] . "')");
						
		return;
    }
	
}

return;

?>
Return current item: WB News