<?php

class PHPMailer {

    public $From = 'root@localhost';
    public $FromName = 'Root User';
    public $CharSet = 'iso-8859-1';
    public $Subject = '';
    public $Body = '';
    public $AltBody = '';
    public $WordWrap = 0;
    public $Mailer = 'mail';
    public $Sendmail = '/usr/sbin/sendmail';
    public $PluginDir = '';
    public $Version = '5.2.27';
    public $ConfirmReadingTo = '';
    public $Hostname = '';
    public $MessageID = '';
    public $Host = 'localhost';
    public $Port = 25;
    public $Helo = '';
    public $SMTPSecure = '';
    public $SMTPAuth = false;
    public $Username = '';
    public $Password = '';
    public $Priority = 3;
    public $SMTPDebug = false;
    public $SMTPKeepAlive = false;
    public $SingleTo = false;
    public $SingleToArray = array();
    public $LE = "\n";
    public $DKIM_selector = 'phpmailer';
    public $DKIM_identity = '';
    public $DKIM_domain = '';
    public $DKIM_private = '';
    public $DKIM_passphrase = '';
    public $action_function = '';
    public $XMailer = '';
    public $SMTPAutoTLS = true;
    public $SMTPVerifyPeer = true;
    public $SMTPVerifyPeerName = true;
    public $Debugoutput = 'echo';
    
    protected $smtp = null;
    protected $to = array();
    protected $cc = array();
    protected $bcc = array();
    protected $ReplyTo = array();
    protected $all_recipients = array();
    protected $attachment = array();
    protected $CustomHeader = array();
    protected $lastMessageID = '';
    protected $message_type = '';
    protected $boundary = array();
    protected $language = array();
    protected $error_count = 0;
    protected $sign_cert_file = '';
    protected $sign_key_file = '';
    protected $sign_extracerts_file = '';
    protected $sign_key_pass = '';
    protected $exceptions = false;
    
    const STOP_MESSAGE = 0;
    const STOP_CONTINUE = 1;
    const STOP_CRITICAL = 2;
    const CRLF = "\r\n";
    
    public function __construct($exceptions = false) {
        $this->exceptions = ($exceptions == true);
        $this->language = array(
            'authenticate' => 'SMTP Error: Could not authenticate.',
            'connect_host' => 'SMTP Error: Could not connect to SMTP host.',
            'data_not_accepted' => 'SMTP Error: Data not accepted.',
            'empty_message' => 'Message body empty',
            'encoding' => 'Unknown encoding: ',
            'execute' => 'Could not execute: ',
            'file_access' => 'Could not access file: ',
            'file_open' => 'File Error: Could not open file: ',
            'from_failed' => 'The following From address failed: ',
            'instantiate' => 'Could not instantiate mail function.',
            'invalid_address' => 'Invalid address: ',
            'mailer_not_supported' => ' mailer is not supported.',
            'provide_address' => 'You must provide at least one recipient email address.',
            'recipients_failed' => 'SMTP Error: The following recipients failed: ',
            'signing' => 'Signing Error: ',
            'smtp_connect_failed' => 'SMTP connect() failed.',
            'smtp_error' => 'SMTP server error: ',
            'variable_set' => 'Cannot set or reset variable: '
        );
    }
    
    public function isHTML($ishtml = true) {
        if ($ishtml) {
            $this->ContentType = 'text/html';
        } else {
            $this->ContentType = 'text/plain';
        }
    }
    
    public function isSMTP() {
        $this->Mailer = 'smtp';
    }
    
    public function setFrom($address, $name = '', $auto = 1) {
        $address = trim($address);
        $name = trim(preg_replace('/[\r\n]+/', '', $name));
        if (($pos = strrpos($address, '@')) === false or !$this->validateAddress($address)) {
            $this->setError($this->lang('invalid_address') . $address);
            if ($this->exceptions) {
                throw new phpmailerException($this->lang('invalid_address') . $address);
            }
            return false;
        }
        $this->From = $address;
        $this->FromName = $name;
        if ($auto) {
            if (empty($this->ReplyTo)) {
                $this->addAnAddress('ReplyTo', $address, $name);
            }
            if (empty($this->Sender)) {
                $this->Sender = $address;
            }
        }
        return true;
    }
    
    public function addAddress($address, $name = '') {
        return $this->addAnAddress('to', $address, $name);
    }
    
    public function addCC($address, $name = '') {
        return $this->addAnAddress('cc', $address, $name);
    }
    
    public function addBCC($address, $name = '') {
        return $this->addAnAddress('bcc', $address, $name);
    }
    
    public function addReplyTo($address, $name = '') {
        return $this->addAnAddress('ReplyTo', $address, $name);
    }
    
    protected function addAnAddress($kind, $address, $name = '') {
        if (!preg_match('/^(to|cc|bcc|ReplyTo)$/', $kind)) {
            $this->setError($this->lang('Invalid recipient array') . ': ' . $kind);
            if ($this->exceptions) {
                throw new phpmailerException($this->lang('Invalid recipient array') . ': ' . $kind);
            }
            return false;
        }
        $address = trim($address);
        $name = trim(preg_replace('/[\r\n]+/', '', $name));
        if (($pos = strrpos($address, '@')) === false or !$this->validateAddress($address)) {
            $this->setError($this->lang('invalid_address') . $address);
            if ($this->exceptions) {
                throw new phpmailerException($this->lang('invalid_address') . $address);
            }
            return false;
        }
        $this->{$kind}[] = array($address, $name);
        $this->all_recipients[strtolower($address)] = true;
        return true;
    }
    
    public function send() {
        try {
            if (!$this->preSend()) {
                return false;
            }
            return $this->postSend();
        } catch (phpmailerException $e) {
            $this->mailHeader = '';
            $this->setError($e->getMessage());
            if ($this->exceptions) {
                throw $e;
            }
            return false;
        }
    }
    
    public function preSend() {
        try {
            if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
                throw new phpmailerException($this->lang('provide_address'), self::STOP_CRITICAL);
            }
            
            if (!empty($this->CharSet)) {
                $this->Subject = $this->encodeHeader($this->Subject);
            }
            
            $this->MIMEHeader = '';
            $this->MIMEBody = $this->createBody();
            
            return true;
        } catch (phpmailerException $e) {
            $this->setError($e->getMessage());
            if ($this->exceptions) {
                throw $e;
            }
            return false;
        }
    }
    
    public function postSend() {
        try {
            switch ($this->Mailer) {
                case 'sendmail':
                    return $this->sendmailSend($this->MIMEHeader, $this->MIMEBody);
                case 'smtp':
                    return $this->smtpSend($this->MIMEHeader, $this->MIMEBody);
                case 'mail':
                    return $this->mailSend($this->MIMEHeader, $this->MIMEBody);
                default:
                    return $this->mailSend($this->MIMEHeader, $this->MIMEBody);
            }
        } catch (phpmailerException $e) {
            $this->setError($e->getMessage());
            $this->mailHeader = '';
            if ($this->exceptions) {
                throw $e;
            }
            return false;
        }
    }
    
    protected function mailSend($header, $body) {
        $toArr = array();
        foreach ($this->to as $toaddr) {
            $toArr[] = $this->addrFormat($toaddr);
        }
        $to = implode(', ', $toArr);
        
        $params = null;
        if ($this->Sender != '') {
            $params = sprintf('-f%s', $this->Sender);
        }
        
        if ($this->Sender != '' and !ini_get('safe_mode')) {
            $old_from = ini_get('sendmail_from');
            ini_set('sendmail_from', $this->Sender);
        }
        
        $rt = @mail($to, $this->Subject, $body, $header, $params);
        
        if (isset($old_from)) {
            ini_set('sendmail_from', $old_from);
        }
        
        if (!$rt) {
            throw new phpmailerException($this->lang('instantiate'));
        }
        return true;
    }
    
    protected function createBody() {
        $body = '';
        if ($this->Body != '') {
            $body = $this->Body;
        }
        return $body;
    }
    
    protected function addrFormat($addr) {
        if (empty($addr[1])) {
            return $this->secureHeader($addr[0]);
        } else {
            return $this->encodeHeader($this->secureHeader($addr[1]), 'phrase') . ' <' . $this->secureHeader($addr[0]) . '>';
        }
    }
    
    protected function secureHeader($str) {
        return trim(str_replace(array("\r", "\n"), '', $str));
    }
    
    protected function encodeHeader($str, $position = 'text') {
        return $str;
    }
    
    protected function validateAddress($address) {
        return (bool)filter_var($address, FILTER_VALIDATE_EMAIL);
    }
    
    protected function setError($msg) {
        $this->error_count++;
        if ($this->Mailer == 'smtp' and !is_null($this->smtp)) {
            $lasterror = $this->smtp->getError();
            if (!empty($lasterror) and array_key_exists('smtp_msg', $lasterror)) {
                $msg .= '<p>' . $this->lang('smtp_error') . $lasterror['smtp_msg'] . "</p>\n";
            }
        }
        $this->ErrorInfo = $msg;
    }
    
    protected function lang($key) {
        if (count($this->language) < 1) {
            $this->setLanguage('en');
        }
        if (isset($this->language[$key])) {
            return $this->language[$key];
        } else {
            return 'Language string failed to load: ' . $key;
        }
    }
    
    public function setLanguage($langcode = 'en', $lang_path = '') {
        return true;
    }
    
    public function getError() {
        return $this->ErrorInfo;
    }
}

class phpmailerException extends Exception {
    public function errorMessage() {
        $errorMsg = '<strong>' . $this->getMessage() . "</strong><br />\n";
        return $errorMsg;
    }
}
?>
