<?php namespace EH_CRM_PhpImap;

/** Class used to intend the Incoming Mails
 * 
 * @see    https://github.com/barbushin/php-imap
 */
class EH_CRM_IncomingMail {


	public $id;
	public $date;
	public $headersRaw;
	public $headers;
	public $subject;

	public $fromName;
	public $fromAddress;

	public $to = array();
	public $toString;
	public $cc      = array();
	public $bcc     = array();
	public $replyTo = array();

	public $messageId;

	public $textPlain;
	public $textHtml;
	/** Incoming mail attachment
	 *
	 * @var IncomingMailAttachment[] 
	 */
	protected $attachments = array();

	public function addAttachment( EH_CRM_IncomingMailAttachment $attachment ) {
		$this->attachments[ $attachment->id ] = $attachment;
	}

	/** Incoming mail attachment
	 * 
	 * @return IncomingMailAttachment[]
	 */
	public function getAttachments() {
		return $this->attachments;
	}

	/**
	 * Get array of internal HTML links placeholders
	 *
	 * @return array attachmentId => link placeholder
	 */
	public function getInternalLinksPlaceholders() {
		return preg_match_all( '/=["\'](ci?d:([\w\.%*@-]+))["\']/i', $this->textHtml, $matches ) ? array_combine( $matches[2], $matches[1] ) : array();

	}

	public function replaceInternalLinks( $baseUri ) {
		$baseUri     = rtrim( $baseUri, '\\/' ) . '/';
		$fetchedHtml = $this->textHtml;
		foreach ( $this->getInternalLinksPlaceholders() as $attachmentId => $placeholder ) {
			if ( isset( $this->attachments[ $attachmentId ] ) ) {
				$fetchedHtml = str_replace( $placeholder, $baseUri . basename( $this->attachments[ $attachmentId ]->filePath ), $fetchedHtml );
			}
		}
		return $fetchedHtml;
	}
}

class EH_CRM_IncomingMailAttachment {


	public $id;
	public $name;
	public $filePath;
	public $disposition;
}
