<?php

class EMailMessageToModelAdapter {
	
	public static function fromEMailToModel(EMailMessage $msg) {
		
		$mail = new EMails();
		
		$mail->isNewRecord = TRUE;
		$mail->userId = 1;
		$mail->timestamp = $msg->timestamp();
		$mail->subject = $msg->subject();
			
		$mail->from_personal = $msg->from_personal();
		$mail->to_personal = $msg->to_personal();
		
		$mail->body_raw = $msg->getBody();
		
		/*$convert = imap_mime_header_decode(@$header->from[0]->personal);
		$model->from_personal = utf8_encode(@$convert[0]->text);
		print_r($convert);
		$model->from_host = @$header->from[0]->host;
		$model->from_mailbox = @$header->from[0]->mailbox;
			
		$model->to_host = @$header->to[0]->host;
		$convert = imap_mime_header_decode(@$header->to[0]->personal);
		print_r($convert);
		$model->to_personal = utf8_encode(@$convert[0]->text);
		$model->to_mailbox = @$header->to[0]->mailbox;
			
		$model->body_raw = $raw_body;*/
		
		
		return $mail;
		
	}
	
}


class EMailMessage {
	
	private $mHeader;
	private $mBody;
	
	public function setBody($body) {
		$this->mBody = $body;
	}
	
	public function setHeader($header) {
		$this->mHeader = $header;
	}
	
	public function getHeader() {
		return $this->mHeader;
	}
	
	public function getBody() {
		return $this->mBody;
	}	
	
	public function timestamp() {
		return $this->mHeader->udate;
	}
	
	public function subject() {
		return $this->_conv(@$this->mHeader->subject);
	}
	
	public function from_personal() {
		return $this->_conv(@$this->mHeader->from[0]->personal);
	}
	
	public function to_personal() {
		return $this->_conv(@$this->mHeader->to[0]->personal);
	}
	
	
	private function _conv($header) {
		
		$conv = imap_mime_header_decode($header);
		$header = "";
		foreach ($conv as $obj) {
			if ($obj->charset == "default") {
				$header.= $obj->text;
			} else if ( $obj->charset == "UTF-8" ) {
				$header.= $obj->text;
			} else {
				$header.= iconv($obj->charset, "UTF-8", $obj->text);
			}
		}
				
		return $header;
	}
		
}

class ImapConnector {

	private $mServer;
	private $mUserName;
	private $mPassword;
	private $imap = true;
	private $use_ssl = true;
	private $checkcert = true;
	
	private $currentFolder;
	
	private $serverConnectionString;
	
	private $mImapConnection = null;
	
	public function __construct($server, $user, $pass, $imap = true, $use_ssl = true, $checkcert = true) {
		
		$this->mServer   = $server;
		$this->mUserName = $user;
		$this->mPassword = $pass;
		$this->imap      = $imap;
		$this->use_ssl   = $use_ssl;
		$this->checkcert = $checkcert;
		
	}
		
	public function connect() {
		
		$server = "{".$this->mServer;
		
		if ($this->imap) {
			$server .= "/imap";
		} else {
			$server .= "/pop3";
		}
		
		if ($this->use_ssl) {
			$server .= "/ssl";
			if (!$this->checkcert) {
				$server .= "/novalidate-cert";
			}
		}
		$server .= "}".$this->currentFolder;
		$this->serverConnectionString = $server;
		$this->mImapConnection = imap_open($server, $this->mUserName, $this->mPassword);
		
		if (!$this->mImapConnection) {
			return false;
		}
		
		return true;
	}
	
	public function cd($folder) {
		
		$this->currentFolder = $folder;		
		$this->disconnect();
		$this->connect();
		
	}
	
	public function messageCount() {
		return imap_num_msg($this->mImapConnection);
	}
	
	public function getMailHeader($messageNumber) {
		return imap_headerinfo($this->mImapConnection, $messageNumber);
	}
	
	public function  getMailRawBody($messageNumber) {
		return imap_body($this->mImapConnection, $messageNumber);
	}
	
	public function getEMailMessageById($messageNumber) {
		
		$msg = new EMailMessage();
		$msg->setHeader($this->getMailHeader($messageNumber));
		$msg->setBody($this->getMailRawBody($messageNumber));
		return $msg; 
		
	}
	
		
	public function getFolders() {
		$folders = imap_listmailbox($this->mImapConnection, $this->serverConnectionString, "*");
		if ($folders == false) {
			return false;
		} else {
			$ret = array();
			foreach ($folders as $val) {
				$ret[] = substr($val, strlen($this->serverConnectionString));
			}
			return $ret;
		}
		
	}
	
	public function disconnect() {
		imap_close($this->mImapConnection);
	}
	
	
};

/*
print_r($connection);

$folders = imap_listmailbox($connection,$server, "*");

if ($folders == false) {
    echo "Abruf fehlgeschlagen<br />\n";
} else {
    foreach ($folders as $val) {
        echo $val . "<br />\n";
    }
}



$count = imap_num_msg($connection);

for ($i = $count; $i <= $count; $i++) {
	$header = imap_headerinfo($connection, $i);
	var_dump($header);
	$raw_body = imap_body($connection, $i);
	
	var_dump($raw_body);
	break;
}
*/