<?php
/**
 * recursive include to render the mime body parts of a message and attached renderable attachments (attached emails)
 * needs $this->messageArray, and $this->moduleBaseUrl;
 */
	if( !isset($this->messageArray) || !is_array($this->messageArray) ) 
	{
	
		//possible empty message
		return '';
		throw new Atmail_Exception('Need messageArray to render messageBody');
	
	}
	
?>
<?php if (isset($this->messageArray['bodyHtml']) ) { ?>
	<div class="messageBodyOriginal" style="display: none; visibility: hidden;">
		<?php echo htmlspecialchars($this->messageArray['bodyPreparedHtml'], ENT_QUOTES | ENT_IGNORE, "UTF-8"); ?>
	</div>
<?php } ?>
<div class="messageBody" style="display: none; visibility: hidden;">
<?php
	$text = isset($this->messageArray['bodyHtml']) ? $this->messageArray['bodyHtml'] : $this->messageArray['bodyPreparedHtml'];
	echo htmlspecialchars($text, ENT_QUOTES | ENT_IGNORE, "UTF-8");
?>
</div>

<?php if(isset($this->messageArray['displayImagesAnchor'])) : ?>
<script type="text/javascript" language="JavaScript">$('#<?php echo $this->messageArray['headers']['uniqueid']; ?>', '#<?php echo $this->messagePanelContextName ?>').find('#displayimage').show();</script>
<?php endif; ?>

<iframe title="message" class="messageIframe" scrolling="yes" frameborder="0" marginheight="0" marginwidth="0" width="100%" src="<?php echo $this->siteBaseUrl . 'index.php/default/index/blank'; ?>" onload="prepMessageIframe(this, '<?php echo $this->siteBaseUrl ?>', '<?php echo $this->moduleBaseUrl ?>viewmessage/index/folder/<?php echo urlencode( urlencode( $this->folderUTF7 ) ); ?><?php if(isset($this->attachmentId) && $this->attachmentId != '') echo '/attachmentId/' . $this->attachmentId; ?>/uniqueId/<?php echo $this->messageArray['headers']['uniqueid']; ?>/inline/true/messagePanelContextName/<?php echo $this->messagePanelContextName; ?>')"></iframe>
<?php

$allAttachments = array_merge($this->messageArray['forcedInlines'], $this->messageArray['forcedAttachments']);

if( count($allAttachments) > 0 ) :
?> 
	
<div class="attachments">
	<a name="attachments"></a>
   	<h4></h4>
   	<ul>
<?php
	$attachId = 1;
    
	foreach( $allAttachments as $attachment ) { 
		
		if( 
			( array_key_exists('sizeRaw', $attachment) && $attachment['sizeRaw'] <  10 && strpos($attachment['filenameOriginal'], 'unknownfilename') !== false ) 
		)
		{
		    
			continue;
			
		}
		
		$previewArgs = array(
			'moduleBaseUrl' => $this->moduleBaseUrl, 
			'currentFolder' => $this->folderUTF7, 
			'uniqueId' => $this->messageArray['headers']['uniqueid'], 
			'filenameFS' => $attachment['filenameFS'], 
			'filenameOriginal' => $attachment['filenameOriginal'], 
			'fileType' => $attachment['iconClass'], 
			'attachId' => $attachId, 
			'size' => bytesToHumanReadable($attachment['sizeRaw']), 
			'content' => isset($attachment['content']) ? $attachment['content'] : ''
		);
				
        $thumbnailHtml = $this->pluginCall('filePreviewHtml', $previewArgs);

        // Zend/Debug::dump($thumbnailHtml);
        
		//need to double encode vars that could mess with the url (#?&/)
		$folderUTF7UrlencodedDouble = urlencode( urlencode( $this->folderUTF7 ) );
		$mimeTypeUrlencodedB64 = base64_encode( $attachment['mimeType'] );
		$filenameOriginalUrlencodedDouble = urlencode( urlencode( $attachment['filenameOriginal'] ) );
		$filenameOriginalHtmlentities = htmlentities($attachment['filenameOriginal'], ENT_QUOTES, 'UTF-8');
		$attachmentUrl = $this->moduleBaseUrl . 'viewmessage/getattachment/folder/' . $folderUTF7UrlencodedDouble . '/uniqueId/' . $this->messageArray['headers']['uniqueid'] .'/mimeType/' . $mimeTypeUrlencodedB64 . '/filenameOriginal/' . md5($attachment['filenameOriginal']);

		$mimeTypeParts = explode('/', $attachment['mimeType'], 2);
		
		if( isset($thumbnailHtml) && $thumbnailHtml != null && $thumbnailHtml != false && strlen($thumbnailHtml) > 0 )
		{
			echo $thumbnailHtml;
			
		}
		else
		{

			
			
			$mimeTypeParts = explode('/', $attachment['mimeType'], 2);
			
			
			//CONSIDER: to enable download links to parts that are .eml, find a simple naming convention to access .eml mime parts that dont have a filename to uniquely id them (like use part position as an identifier)
			if($mimeTypeParts[0] == 'message') 
			{
				echo '<li class="attachment ' . $attachment['iconClass'] . '" id="attachment' . $attachId . '">';
				// ensure that the next two calls DO NOT use the jscript cache.
				// else they will pull the current email, not the inline attachement !
				$args = array('messageArray' => $attachment['content'], 'moduleBaseUrl' => $this->moduleBaseUrl, 'siteBaseUrl' => $this->siteBaseUrl, 'folderUTF7' => $this->folderUTF7, 'messagePanelContextName' => $this->messagePanelContextName, 'attachmentId' => $attachId);

				echo $this->partial('viewmessage/renderemailheaderneat.phtml', $args);
				echo $this->partial('viewmessage/recursivelyrendermessagebody.phtml', $args );
                //if message is actually an attachment then render a link to download the email attachment
				if( isset($attachment['filenameOriginal']) )
				{
					
					echo "\n\t" . '<br />
						<a class="filename"  target="_blank" href="' . $attachmentUrl . '">' .  $filenameOriginalHtmlentities . '</a> <span class="filesize">' . $attachment['fileTypeName'] . ' - ' . bytesToHumanReadable($attachment['sizeRaw']) . '</span>
					';
					
				}

			}
			elseif( $mimeTypeParts[0] == 'image' && in_array($mimeTypeParts[1], array('jpg', 'jpeg', 'gif', 'png', 'bmp')) ) 
			{
				echo '<li class="attachment">';
				//list($width, $height, $type, $attr) = getimagesize( $attachment['tmpUrl'] );
				//if($width > 800)
				echo "\n\t" . '<a target="_blank" href="' . $attachmentUrl . '">
						<img class="attachedImg" src="' . $attachmentUrl . '" />
					</a>';                           
					echo '</li><li class="attachment ' . $attachment['iconClass'] . '" id="attachment' . $attachId . '">';
					echo '<a class="filename"  target="_blank" href="' . $attachmentUrl . '">' . $filenameOriginalHtmlentities . '</a> <span class="filesize">' . $attachment['fileTypeName'] . ' - ' . bytesToHumanReadable($attachment['sizeRaw']) . '</span>
				';
			}
			elseif( $attachment['mimeType'] == 'text/plain' )
			{
				echo '<li><pre>';
				echo htmlentities($attachment['content']);
				echo '</pre></li>';
				
				echo '<li class="attachment ' . $attachment['iconClass'] . '" id="attachment' . $attachId . '">';
				
				echo '<a class="filename"  target="_blank" href="' . $attachmentUrl . '">' . $filenameOriginalHtmlentities . '</a> <span class="filesize">' . $attachment['fileTypeName'] . ' - ' . bytesToHumanReadable($attachment['sizeRaw']) . '</span>
			';
			}
			else
			{
				echo '<li class="attachment ' . $attachment['iconClass'] . '" id="attachment' . $attachId . '">';
				
				echo "\n\t" . '<a class="filename" target="_blank" href="' . $attachmentUrl . '">' . $filenameOriginalHtmlentities . '</a> <span class="filesize">' . $attachment['fileTypeName'] . ' - ' . bytesToHumanReadable($attachment['sizeRaw']) . '</span>
				';
			}
		    
			
			echo '</li>';
			
	    }
	    $attachId++;
	} //EO foreach

?>
  	</ul> 
</div> <!-- EO div class attachments -->
<?php endif;
