Location: PHPKode > scripts > AntiSpam Feedback > antispam-feedback/asf_controller.php
<?PHP
/*
* AntiSpam Feedback
*
* @package AntiSpam Feedback
* @author $Author: sheiko $  
* @version $Id: asf_controller.php, v 1.0 sheiko Exp $ 
* @since v.1.0 
* @copyright (c) Dmitry Sheiko http://www.cmsdevelopment.com 
*/ 

define("ASF_CONFIRM_URL", "http://site.com/asf_confirm.php");
define("HEADERS", "MIME-Version: 1.0\r\nContent-type: text/html; charset=UTF-8\r\n"); 
define("LETTER_STYLE", "<style>BODY { padding: 10px; }\r\nBODY, TD {font-family: Arial, Tahoma; font-size: 12px; } </style>");

/**
* AntiSpam Feedback Controller Class 
* @package AntiSpam Feedback
* @author $Author: sheiko $ 
*/

class NotificationController {

	var $MessageID= "";
	var $MessageBODY= "";
	/** 
    * Class Constructor
    * @return object
    */ 	
	function NotificationController() {
		if(!isset($_POST["email"])) $this->respond("Invalid POST");
		if(!isset($_POST["gencode"]))  $this->respond("Invalid POST");
		session_start();
		$this->MessageID = time();
		$this->MessageBODY = $this->getMessageBody();
		$this->deleteExpiredMessages();
		$this->MessageToQueue();
		$this->SendNotification();
		$this->respond("Your message was suggested to send"); 
		return $this;
	}
	
	/** 
    * Respond to AJAX and finish processing
    * 
    * @param string
    */ 			
	function respond($message) {
		ob_end_clean();
		$out = '{
				"errormsg" : "",
				"content" : "'.$message.'"
				}';
		header("Content-type: text/html; charset=UTF-8");
		print $out;		
		exit;
	}

	/** 
    * Delete expired messages
    * 
    */ 				
	function deleteExpiredMessages() {
		$d = dir(realpath(dirname(__FILE__))); 
		while (false !== ($entry = $d->read())) { 
			if(preg_match("/.msg$/", $entry)) {
				$time = preg_replace("/.msg$/", "", $entry);
				if($time<time()-60*60*24*7) @unlink($entry);
			}
		} 
		$d->close(); 		
	}
	/** 
    * Get message body
    * 
    * @return sting
    */ 				
	function getMessageBody() {
		if(md5($_POST["gencode"])!==$_SESSION["Md5OfGenCode"]) $this->respond("Incorrect confirmation code");
		unset($_POST["gencode"]);
		$text = "<table border=\"0\" width=\"100%\">";
		foreach($_POST as $key => $val)  {
			$val = nl2br(htmlspecialchars(stripslashes($val)));
			$text .= "<tr><td valign=\"top\">{$key}</td><td width=\"100%\">{$val}</td></tr>";
		}
		$text .= "</table>";
		return $text;
	}

	/** 
    * Put the message into queue
    * 
    */ 				
	function MessageToQueue() {
		if($fp=@fopen($this->MessageID.".msg", "w")) {
			fwrite($fp, $this->MessageBODY );
			fclose($fp);				
		} else $this->respond("Can not create TMP-file. Set apropriate access permissions to the folder");
	}
	
	/** 
    * Send notification
    * 
    */ 				
	function SendNotification() {
		if(!@mail(	$_POST["email"], 
					"Please, confirm if you really want to send this message", 
					LETTER_STYLE."Please, go <a href=\"".ASF_CONFIRM_URL."?msgid={$this->MessageID}\">".ASF_CONFIRM_URL."?msgid={$this->MessageID}</a> if you really want to send this message<br /><br />
					{$this->MessageBODY}<br /><br />If you do not confirm till ".date("r", time()+60*60*24*7)." the message will be deleted"
					, HEADERS)) $this->respond("Can not send notification. Please, try again.");
	}	
}

ob_start();
$ctrl = new NotificationController();
?>
Return current item: AntiSpam Feedback