<?php

namespace Mailer;

class EMailClient extends \PHPMailer\PHPMailer\PHPMailer {
  
  

  
  

    // --- Statische Methoden ---
  
  public static function getMailerBoxSignature($mailbox = null ) { 
    
    
    // --- BLOCK: main ---
    
		return \FOP\Database::query('SELECT signature FROM fop_mail_host_box WHERE '.qi('ID').' = ' . \FOP\Database::quote($mailbox) . ' LIMIT 1')->fetchColumn();
	
    
  }

  public static function getMailerBox($useSend = null ) { 
    
    
    // --- BLOCK: main ---
    
		$mailbox = null;
		switch ($useSend) {
			
		}
		if (!$mailbox) {
			$mailbox = \FOP\Database::query('SELECT '.qi('ID').' FROM fop_mail_host_box WHERE zustand IN (1, 3)AND fopuser = '.\FOP\Database::sessionVarSQL('user').' LIMIT 1')->fetchColumn();
		}
		if (!$mailbox) {
			$mailbox = \FOP\Database::query('SELECT mailbox FROM fop_mail_host_box_share WHERE type = 2 AND fopuser = '.\FOP\Database::sessionVarSQL('user').' LIMIT 1')->fetchColumn();
		}
		if (!$mailbox) {
			$mailbox = \FOP\Database::query('SELECT '.qi('ID').' FROM fop_mail_host_box WHERE zustand IN (1, 3) LIMIT 1')->fetchColumn();
		}
		return $mailbox;
	
    
  }

  public static function getMailer($mailbox = null ) { 
    
    
    // --- BLOCK: main-create ---
    $mail = new self($mailbox);
    // --- BLOCK: main-return ---
    return $mail;
    
  }


    // --- Eigenschaften ---
  
  private $mailbox ;
  private $hoststr ;
  private $sentFolder ;
  private $latestuid ;

    // --- Methoden ---
  
  public function send() {
    
    
     
    
		$result = parent::send();
		if ($result) {
			// save in Sent Folder
			// TODO: Diversify folder; new column in fop_mail_host_box: sent_dir
			if (isset($this->sentFolder)) {
				$mbox = \Tables\Fop_mail_host_box_dir::imap_open($this->mailbox, $this->sentFolder, '');
				$result = imap_append($mbox, $this->hoststr . $this->sentFolder, $this->getSentMIMEMessage(), '\Seen');
				/* TODO: code disabled because it is not allowed inside an other transaction
				// Sync Sent Folder
				$postbox = array('type' => 'fop_mail_host_box',
					'id' => $this->mailbox,
					'dir' => $this->sentFolder,
					'dirFull' => $this->hoststr . $this->sentFolder,
					'check' => imap_mailboxmsginfo($mbox),
					'syncdate' => 0);
				\FOP\Imap::iterateMails($postbox, $mbox, $this->latestuid);
				*/
				imap_close($mbox);
			}
		}
		return $result;
	
    
  }
  public function redirect() {
    
    
     
    
		header('Location: ' . \TablesRouter\Fop_mail_host_box_draft::createURL2([
			'std_mailbox' => $this->mailbox,
			'to' => $this->to[0][0], // [email, name]
			'column_subject' => $this->Subject,
			'column_content' => $this->Body,
			'attachment' => implode(',', array_map(function ($attachment) {
				if ($attachment[5] /* isStringAttachment */) {
					return \FOP\Files::uploadFromString($attachment[0], $attachment[1]);
				} else {
					return \FOP\Files::uploadFromFile($attachment[0], $attachment[1]);
				}
			}, $this->attachment))
		]));
		die();
	
    
  }
  public function __construct($mailbox) {
    
    
     
    
		parent::__construct();

		if (!$mailbox) $mailbox = self::getMailerBox();
		$res = \FOP\Database::query('SELECT
			fop_mail_host_box.hoststr,
			fop_mail_host_box.from_address,
			fop_mail_host_box.user,
			fop_mail_host_box.password,
			fop_mail_host_box.from_name,
			fop_mail_host.smtp_host,
			fop_mail_host.smtp_port,
			fop_mail_host.smtp_auth,
			(SELECT imap_name FROM fop_mail_host_box_dir WHERE '.qi('ID').' = fop_mail_host_box.sent_dir) AS folder,
			(SELECT latestuid FROM fop_imaplistener WHERE mailboxid = fop_mail_host_box.'.qi('ID').' AND directorystr = (SELECT imap_name FROM fop_mail_host_box_dir WHERE '.qi('ID').' = fop_mail_host_box.sent_dir) LIMIT 1) AS latestuid
			FROM fop_mail_host_box LEFT JOIN fop_mail_host ON email_server = fop_mail_host.'.qi('ID').'
			WHERE fop_mail_host_box.'.qi('ID').' = ' . intval($mailbox) . ' LIMIT 1')->fetch(\PDO::FETCH_ASSOC);
		if(!$res) throw new \FOP\AccessDeniedException('noMailboxError');

		$this->mailbox = $mailbox;
		$this->hoststr = $res['hoststr'];
		$this->sentFolder = $res['folder'];
		$this->latestuid = $res['latestuid'];
		$this->CharSet = 'UTF-8';
		$this->IsSMTP();
		$this->Host = $res['smtp_host'];
		$this->Port = $res['smtp_port'];
		$this->SMTPAuth = true;
		$this->Username = $res['user'];
		$this->Password = \Types\Password::decryptSymmetric($res['password']);
		
		if ($res['smtp_auth'] == 'starttls') $this->SMTPSecure = 'tls';
		else if ($res['smtp_auth'] == 'ssl') $this->SMTPSecure = 'ssl';

		$this->From = $res['from_address'];
		$this->FromName = $res['from_name'];
	
    
  }
}



