<?php
	FNTH_class( "Template" );
	require_once( $GLOBALS["site_root"] . "/includes/models/SystemParameters.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/3rd/class.phpmailer.php" );

	class MailModel {
		protected $aParams = array();
		protected $aHeaders = array();
		protected $sSubject = "";
		protected $sBody = "";
		protected $bHtml = false;
		protected $bPlain = true;

		protected $sLanguage = "hu";

		public $sFrom = "";
		public $sFromEmail = "";

		public $sReplyTo = "";
		public $sReplyToEmail = "";

		protected $aMimePrefs = array(
					"input-charset" => "UTF-8",
					"output-charset" => "UTF-8" );
		protected $cSubject;
		protected $cBody;
		protected $cHeaders;
		protected $sBoundary = "";

		public function setHtml( $v ) { $this->bHtml = $v; }
		public function setPlain( $v ) { $this->bPlain = $v; }

		public function clearParams() { $this->aParams = array(); }
		public function setParam( $k , $v ) { $this->aParams[$k] = $v; }
		public function setHeader( $k , $v ) { $this->aHeaders[$k] = $v; }
		public function getHeader( $k ) { return $this->aHeaders[$k]; }
		public function setSubject( $v ) { $this->sSubject = $v; }
		public function getSubject() { return $this->sSubject; }
		public function setBody( $v ) { $this->sBody = $v; }
		public function getBody() { return $this->sBody; }

		public function __construct( $lang = "hu" ) {
			$this->sLanguage = $lang;
			$test = (strpos( $_SERVER["SERVER_NAME"] , "darkside" )!==false);
			if ($test) {
				$this->sFrom = __("admin_mail_from_name","attr",$this->sLanguage);
				$this->sFromEmail = "info@darksidehq.media-online.hu";
			} else {
				$this->sFrom = __("admin_mail_from_name","attr",$this->sLanguage);
				$this->sFromEmail = __("admin_mail_from_email","attr",$this->sLanguage);
			}
		}

		public function fetchBody() {
			$t = new FNTHTemplate;
			$t->bSuppressErrors = false;
			$t->setParams( $this->aParams );
			$query = sprintf( "select * from mail_templates where mail_template_id=%d" , $this->iTemplateId );
			$row = FNTH_db_get_row( $query );
			$this->setBody( $row["template_content"] );
			return $t->renderFromString( $this->sBody );
		}

		protected function createPHPMailer() {
			$phpm = new PHPMailer();
			$durva_hack = true;
			// durva hack
			if ($durva_hack) {
				$this->sFromEmail = "info@forditmx2.media-online.hu";
				$phpm->Mailer="smtp";
				$phpm->Host = "89.135.88.132";
				$phpm->Helo = "forditmx2.media-online.hu";
			}
			// durva hack end
			$phpm->CharSet = "UTF-8";
			return $phpm;
		}

		public function sendMail( $email = "lorenzo@fotonsoft.hu" ) {
			$t = new FNTHTemplate;
			$t->setParams( $this->aParams );
			$this->cSubject = $t->renderFromString( $this->sSubject );
			$this->cBody = $t->renderFromString( $this->sBody );


			$test = (strpos( $_SERVER["SERVER_NAME"] , "darkside" )!==false);
			if ($test) {
				switch ($email) {
				case "b@lint.hu":
				case "ba@media-online.hu":
				case "andras.balint@media-online.hu":
				case "balint.andras@media-online.hu":
				case "andras.balint@mediaonline.hu":
				case "balint.andras@mediaonline.hu":
				case "balintand@gmail.com":
				case "info@fordit.hu":
				case "hh@media-online.hu":
				case "aniko.horvath@media-online.hu":
				case "horvath.aniko@media-online.hu":
				case "lorenzo@urbanfolklor.hu":
				case "lori@urbanfolklor.hu":
				case "lorenzo.darkside@gmail.com":
				case "h_aniko@freemail.hu":
				case "mb20@freemail.hu":
				case "gergely.szentirmai@ees.hu":
				case "szentirmai.gergely@ees.hu":
				case "barna.deri@ees.hu":
				case "deri.barna@ees.hu":
					break;
				case "lorenzo@fotonsoft.hu":
					$email = "lorenzo@fotonsoft.hu";
					break;
				default:
					if (strpos( $email , "@deathstar.media-online.hu" )!==false) break;
					if (strpos( $email , "@deathstar.mediaonline.hu" )!==false) break;
					$email = "";	// lorenzo@fotonsoft.hu";
					break;
				}

				// teszt emailre kuldjuk, ha nem rendes
				if (!strlen( $email )) {
					$sm = new SystemParametersModel();
					$email = $sm->getVar( "test_email_address" );
				}
			}

			// original email (test + real)
			if (strlen( $email )) {
				$phpm = $this->createPHPMailer();
				// ebben a sorrendben (replyto, address, from) normal
				// ebben a sorrendben (from,address,replyto) mi is kapunk masolatot, ha kell
				if (strlen( $this->sReplyTo ) && strlen( $this->sReplyToEmail )) $phpm->addReplyTo( $this->sReplyToEmail , $this->sReplyTo );
				$phpm->addAddress( $email );
				$phpm->setFrom( $this->sFromEmail , $this->sFrom );
				$phpm->Subject = $this->cSubject;
				$phpm->AltBody = strip_tags( $this->cBody );
				$phpm->MsgHTML( $this->cBody );
				$phpm->Send();
			}

			if (!$test) {
				// eles eseten a test copyja
				$sm = new SystemParametersModel();
				$email = $sm->getVar( "test_email_address" );
				if (!strlen( $email )) return;
				// test email
				$this->cSubject = "SERVER: ".$t->renderFromString( $this->sSubject );
				$phpm = $this->createPHPMailer();
				// ebben a sorrendben (replyto, address, from) normal
				// ebben a sorrendben (from,address,replyto) mi is kapunk masolatot, ha kell
				if (strlen( $this->sReplyTo ) && strlen( $this->sReplyToEmail )) $phpm->addReplyTo( $this->sReplyToEmail , $this->sReplyTo );
				$phpm->addAddress( $email );
				$phpm->setFrom( $this->sFromEmail , $this->sFrom );
				$phpm->Subject = $this->cSubject;
				$phpm->AltBody = strip_tags( $this->cBody );
				$phpm->MsgHTML( $this->cBody );
				$phpm->Send();
			}
		}
	}

?>
