<?php

// $Id: class.WbMailer.php 2064 2014-01-01 15:09:58Z darkviper $

/*

 Website Baker Project <http://www.websitebaker.org/>
 Copyright (C) 2004-2009, Ryan Djurovich

 Website Baker 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.

 Website Baker 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 Website Baker; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

class WbMailer extends PHPMailer
{
	// new websitebaker mailer class (subset of PHPMailer class)
	// setting default values 

	function __construct() {

        $oDb = WbDatabase::getInstance();
		// set mailer defaults (PHP mail function)
		$db_wbmailer_routine = "phpmail";
		$db_wbmailer_smtp_host = "";
		$db_wbmailer_default_sendername = "WB Mailer";
		$db_server_email = SERVER_EMAIL;

		// get mailer settings from database
		// $database = new database();
		$query = 'SELECT * FROM `'.$oDb->TablePrefix.'settings`';
		$oSettingSet = $oDb->doQuery($query);
		while($aSettings = $oSettingSet->fetchRow(MYSQL_ASSOC)) {
            switch ($aSettings['name']):
                case 'wbmailer_routine':
                    $db_wbmailer_routine = $aSettings['value']; break;
                case 'wbmailer_smtp_host':
                    $db_wbmailer_smtp_host = $aSettings['value']; break;
                case 'wbmailer_smtp_auth':
                    $db_wbmailer_smtp_auth = (bool)$aSettings['value']; break;
                case 'wbmailer_smtp_username':
                    $db_wbmailer_smtp_username = $aSettings['value']; break;
                case 'wbmailer_smtp_password':
                    $db_wbmailer_smtp_password = $aSettings['value']; break;
                case 'wbmailer_default_sendername':
                    $db_wbmailer_default_sendername = $aSettings['value']; break;
                case 'server_email':
                    $db_server_email = $aSettings['value']; break;
                default:
                    break;
            endswitch;
		}

		// set method to send out emails
		if($db_wbmailer_routine == "smtp" AND strlen($db_wbmailer_smtp_host) > 5) {
			// use SMTP for all outgoing mails send by Website Baker
			$this->IsSMTP();                                            
			$this->Host = $db_wbmailer_smtp_host;
			// check if SMTP authentification is required
			if ($db_wbmailer_smtp_auth == "true" && strlen($db_wbmailer_smtp_username) > 1 && strlen($db_wbmailer_smtp_password) > 1) {
				// use SMTP authentification
				$this->SMTPAuth = true;     	  								// enable SMTP authentification
				$this->Username = $db_wbmailer_smtp_username;  	// set SMTP username
				$this->Password = $db_wbmailer_smtp_password;	  // set SMTP password
			}
		} else {
			// use PHP mail() function for outgoing mails send by Website Baker
			$this->IsMail();
		}

		// set language file for PHPMailer error messages
		if(defined("LANGUAGE")) {
			$this->SetLanguage(strtolower(LANGUAGE),"language");    // english default (also used if file is missing)
		}

		// set default charset
		if(defined('DEFAULT_CHARSET')) { 
			$this->CharSet = DEFAULT_CHARSET; 
		} else {
			$this->CharSet='utf-8';
		}

		// set default sender name
		if($this->FromName == 'Root User') {
			if(isset($_SESSION['DISPLAY_NAME'])) {
				$this->FromName = $_SESSION['DISPLAY_NAME'];            // FROM NAME: display name of user logged in
			} else {
				$this->FromName = $db_wbmailer_default_sendername;			// FROM NAME: set default name
			}
		}
		/* 
			some mail provider (lets say mail.com) reject mails send out by foreign mail 
			relays but using the providers domain in the from mail address (e.g. myname@mail.com)
		*/
		$this->From = $db_server_email;                           // FROM MAIL: (server mail)

		// set default mail formats
		$this->IsHTML(true);                                        
		$this->WordWrap = 80;                                       
		$this->Timeout = 30;
	}
}
