<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

include_once('classes/core/CoreObject.class.php');
include_once('classes/system/Node.class.php');
include_once('classes/system/HtmlToText.class.php');

/**
 * Mail Class
 *
 * @author  cedseven
 * @package System
 */
class Mail extends CoreObject {

	var $subject;
	var $message;

	var $sender;
	// ['mail'] , ['firstname'], ['lastname']
	var $recipientsListTo;
	var $recipientsListCc;
	var $recipientsListBcc;
	// []['mail'] , []['firstname'], []['lastname']

	function Mail($subject='', $message='') {
	  $this->setSubject($subject);
	  $this->setMessage($message);
		$this->sender = array();
		$this->recipientsListTo = array();
		$this->recipientsListCc = array();
		$this->recipientsListBcc = array();
	}
	
	function setSubject($subject) {
	  $this->subject = $subject;
	}

	function setMessage($message) {
	  $this->message = $message;
	}

	function setSenderMail($mail) {
		if($this->isValidMail($mail)) {
			$this->sender['mail'] = $mail;
		}
	}

	function setSenderFirstname($firstname) {
		$this->sender['firstname'] = $this->toValidName($firstname);
	}

	function setSenderLastname($lastname) {
		$this->sender['lastname'] = $this->toValidName($lastname);
	}
	
	/**
	 *
	 * @private
	 */
 	function addRecipient($field, $mail, $firstname, $lastname) {
		if(!$this->isValidMail($mail)) {
			return false;
		}
		$firstname = $this->toValidName($firstname);
		$lastname  = $this->toValidName($lastname);

		switch($field) {
			case 'TO':
				$i = count($this->recipientsListTo);
				$this->recipientsListTo[$i]['mail'] 		 = $mail;
				$this->recipientsListTo[$i]['firstname'] = $firstname;
				$this->recipientsListTo[$i]['lastname']  = $lastname;
				break;
			case 'CC':
				$i = count($this->recipientsListCc);
				$this->recipientsListCc[$i]['mail'] 		 = $mail;
				$this->recipientsListCc[$i]['firstname'] = $firstname;
				$this->recipientsListCc[$i]['lastname']  = $lastname;
				break;
			case 'BCC':
				$i = count($this->recipientsListBcc);
				$this->recipientsListBcc[$i]['mail'] 		  = $mail;
				$this->recipientsListBcc[$i]['firstname'] = $firstname;
				$this->recipientsListBcc[$i]['lastname']  = $lastname;
				break;
			default:
				return false;
				break;
		}
		return true;
	}

	function addRecipientTO($mail, $firstname='', $lastname='') {
		return $this->addRecipient('TO', $mail, $firstname, $lastname);
	}

	function addRecipientCC($mail, $firstname='', $lastname='') {
		return $this->addRecipient('CC', $mail, $firstname, $lastname);
	}

	function addRecipientBCC($mail, $firstname='', $lastname='') {
		return $this->addRecipient('BCC', $mail, $firstname, $lastname);
	}

	function send() {
		if(isset($this->sender['mail']) && (count($this->recipientsListTo) > 0)) {

			// check sender email address validity
			if(!$this->isValidMail($this->sender['mail'])) {
			  return -1;
			}
			// check recipients email address validity
			foreach($this->recipientsListTo as $index=>$recipient) {
				if(!$this->isValidMail($recipient['mail'])) {
				  return -1;
				}
			}
			foreach($this->recipientsListCc as $index=>$recipient) {
				if(!$this->isValidMail($recipient['mail'])) {
				  return -1;
				}
			}
			foreach($this->recipientsListBcc as $index=>$recipient) {
				if(!$this->isValidMail($recipient['mail'])) {
				  return -1;
				}
			}
			
			if(strlen($this->sender['firstname']) > 0 || strlen($this->sender['lastname']) > 0) {
				$header  = "From: \"".$this->sender['firstname']." ".$this->sender['lastname']."\" <".$this->sender['mail'].">\n";
			}
			else {
				$header  = "From: ".$this->sender['mail']."\n";
			}
			$header .= "Return-Path: ".$this->sender['mail']."\n";
			$header .= "Reply-to: ".$this->sender['mail']."\n";

			foreach($this->recipientsListTo as $index=>$recipient) {
				if(strlen($recipient['firstname']) > 0 || strlen($recipient['lastname']) > 0) {
					$header  = "To: \"".$recipient['firstname']." ".$recipient['lastname']."\" <".$recipient['mail'].">\n";
				}
				else {
					$header .= "To: ".$recipient['mail']."\n";
				}
			}
			foreach($this->recipientsListCc as $index=>$recipient) {
				if(strlen($recipient['firstname']) > 0 || strlen($recipient['lastname']) > 0) {
					$header  = "Cc: \"".$recipient['firstname']." ".$recipient['lastname']."\" <".$recipient['mail'].">\n";
				}
				else {
					$header .= "Cc: ".$recipient['mail']."\n";
				}
			}
			foreach($this->recipientsListBcc as $index=>$recipient) {
				if(strlen($recipient['firstname']) > 0 || strlen($recipient['lastname']) > 0) {
					$header  = "Bcc: \"".$recipient['firstname']." ".$recipient['lastname']."\" <".$recipient['mail'].">\n";
				}
				else {
					$header .= "Bcc: ".$recipient['mail']."\n";
				}
			}

			// create a boundary
			$boundary = md5(uniqid(time()));

			// need a html version and a text version
			if(!$this->isHtmlEncoded($this->message)) {
				$messageHtml = $this->htmlEncode($this->message);
				$messageTxt = $this->message;
			}
			else {
				$messageHtml = $this->message;
				$messageTxt = $this->convertHtmlToText($this->message);
			}

			$messageHtml = "<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"></head><body>".$messageHtml."</body></html>";

			$subject = $this->convertHtmlToText($this->subject);
			$subject = substr(Node::convertUTFStringToASCII($subject), 0, 150);

			$header .= "MIME-Version: 1.0\n";
			$header .= "Content-Type: multipart/alternative; boundary=\"".$boundary."\"\n\n";

			$body = "--".$boundary."\n";
			$body .= "Content-Type: text/plain; charset=iso-8859-1\n";
			$body .= "Content-Transfer-Encoding: 7bit\n\n";
			$body .= $messageTxt."\n";
			$body .= "--".$boundary."\n";
			$body .= "Content-Type: text/html; charset=UTF-8\n";
			$body .= "Content-Transfer-Encoding: 7bit\n\n";
			$body .= $messageHtml."\n";
			$body .= "--".$boundary."--\n";
			
			// todo : recipients in header may not be correctly handled by the smtp server
			//mail('', $subject, $body, $header);
			mail($this->recipientsListTo[0]['mail'], $subject, $body, $header);
			return 1;
		}
		return 0;
	}

  function toValidName($name) {
		$name = strtolower(substr($name, 0, 25));
		$name = Node::convertUTFStringToFileName($name);
  	return ucfirst($name);
  }

  function isValidMail($mail) {
  	$regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
  	if(eregi($regexp, $mail)) {
  		return true;
  	}
  	return false;
  }

	function isHtmlEncoded($string) {
		$htmlSpecialCharsTable = array("<br>", "<br />", "</a>", "</span>", "</div>", "&nbsp;", "&#156;","&#159;","&#160;","&#192;","&#193;","&#194;","&#195;","&#196;","&#197;","&#198;","&#199;","&#200;","&#201;","&#202;","&#203;","&#204;","&#205;","&#206;","&#207;","&#209;","&#210;","&#211;","&#212;","&#213;","&#214;","&#216;","&#217;","&#218;","&#219;","&#220;","&#223;","&#224;","&#225;","&#226;","&#227;","&#228;","&#229;","&#230;","&#231;","&#232;","&#233;","&#234;","&#235;","&#236;","&#237;","&#238;","&#239;","&#241;","&#242;","&#243;","&#244;","&#245;","&#246;","&#248;","&#249;","&#250;","&#251;","&#252;","&#255;","&#260;","&#261;","&#262;","&#263;","&#280;","&#281;","&#321;","&#322;","&#323;","&#324;","&#338;","&#346;","&#347;","&#377;","&#378;","&#379;","&#380;","&Agrave;","&Aacute;","&Acirc;","&Atilde;","&Auml;","&Aring;","&Aelig;","&Ccedil;","&Egrave;","&Eacute;","&Ecirc;","&Euml;","&Igrave;","&Iacute;","&Icirc;","&Iuml;","&Ntilde;","&Ograve;","&Oacute;","&Ocirc;","&Otilde;","&Ouml;","&Oslash;","&Ugrave;","&Uacute;","&Ucirc;","&Uuml;","&szlig;","&agrave;","&aacute;","&acirc;","&atilde;","&auml;","&aring;","&aelig;","&ccedil;","&egrave;","&eacute;","&ecirc;","&euml;","&igrave;","&iacute;","&icirc;","&iuml;","&ntilde;","&ograve;","&oacute;","&ocirc;","&otilde;","&ouml;","&oslash;","&ugrave;","&uacute;","&ucirc;","&uuml;","&yuml;");
		foreach($htmlSpecialCharsTable as $clue) {
			if(stristr($string, $clue)) {
				return true;
			}
		}
		return false;
	}
	
	function htmlEncode($string) {
		return htmlentities($string, ENT_QUOTES, "UTF-8");
	}
	
	function convertHtmlToText($html) {
  	$htmlToText = &new HtmlToText($html);
		return $htmlToText->convert();
	}
}
?>