<?php
/**
 * @copyright (C) 2016 - 2025 Holger Brandt IT Solutions
 * @license GNU/GPL, see license.txt
 * WP Mailster is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.
 *
 * WP Mailster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WP Mailster; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * or see http://www.gnu.org/licenses/.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Not the droids
}

$model = MstFactory::getMailModel();

if( isset($_REQUEST['id']) ) {
	$id = intval($_REQUEST['id']); // thread id
} else {
	$id = 0;
}
if( isset($_REQUEST['bl']) ) {
	$backlink = base64_decode( sanitize_text_field($_REQUEST['bl']) );
} else {
	$backlink = "";
}

$mail = &$model->getData($id, true);

$subject = (is_null($mail->subject) || strlen($mail->subject) == 0) ? '<em>'.__( '(No Subject)', 'wp-mailster' ).'</em>' : $mail->subject;

$page_sfx_attr = ''; // TODO FIXME DYNAMIC PARAMETER DRIVEN PARAMETER

$mailUtils 		= MstFactory::getMailUtils();
$dateUtils 		= MstFactory::getDateUtils();
$listUtils      = MstFactory::getMailingListUtils();
$attachUtils 	= MstFactory::getAttachmentsUtils();

$mListName = '';
$receiveTimestamp = '';
if($mail && $mail->list_id){
    $mList = $listUtils->getMailingList($mail->list_id);
    $mListName = $mList->name;
    $receiveTimestamp = $dateUtils->formatDateAsConfigured($mail->receive_timestamp);
}

?>
<div class="mailster_mail list_name"><h5><?php echo esc_html($mListName); ?></h5></div>
<div class="componentheading<?php echo $page_sfx_attr; ?>">
	<h2><?php echo esc_html($subject); ?></h2>
</div>
<div class="mailster_mail receive_timestamp"><h5><?php echo $receiveTimestamp; ?></h5></div>
<?php if($backlink): ?><div class="mailster_mail_backlink<?php echo $page_sfx_attr; ?>"><a href="<?php echo esc_url($backlink); ?>"><?php echo __( 'Back to previous view', 'wp-mailster'); ?></a></div><?php endif; ?>
<div class="mailster_mail contentpane<?php echo $page_sfx_attr; ?>">
	<div class="mailster_mail attachment_contentpane<?php echo $page_sfx_attr; ?>">
		<?php
		$attachments = $attachUtils->getAttachmentsOfMail($mail->id);
		if(count($attachments)>0){
			?>
			<h3><?php _e( 'Attachments', 'wp-mailster'); ?></h3>
			<table class="mailster_mail_attachment_link_tbl<?php echo $page_sfx_attr; ?>">
				<tr class="mailster_mail_attachment_link_tbl_line<?php echo $page_sfx_attr; ?>">
					<?php
					$attachStr = '';
					$upload_dir = wp_upload_dir();
					$dir = $upload_dir["baseurl"];
					for($j=0; $j < count($attachments); $j++){
						$attach = &$attachments[$j];
						if($attach->disposition == MstConsts::DISPOSITION_TYPE_ATTACH){
                            $dwlLinkNonce = wp_create_nonce( 'mst_dwl_attach_frontend'.intval($attach->id) );
                            $dwlLink = site_url('index.php?mst_download=attachment&id='.(int)$attach->id.'&_wpnonce='.$dwlLinkNonce.'&b=0');
							?>
							<td class="mailster_mail_attachment<?php echo $page_sfx_attr; ?>">
								<a href="<?php echo esc_url($dwlLink); ?>" ><?php echo esc_html(rawurldecode($attach->filename)); ?></a>
							</td>
							<?php
						}
					}
					?>
				</tr>
			</table>
			<?php
		}
		?>
	</div>
	<div class="mailster_mail text_contentpane<?php echo $page_sfx_attr; ?>">
		<?php
		if(is_null($mail->html) || strlen(trim($mail->html))<1){
			$body =  nl2br($mail->body);
			$content = $body;
		}else{
			$content = $mail->html;
			$content = $mailUtils->replaceContentIdsWithAttachments($content, $attachments);
		}
		echo $content;
		?>
	</div>
</div>
