<commit_before><?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // date in the past

if(!isset($_GET['msg_id']) || !isset($_GET['box']) || !is_numeric($_GET['box']) || !isset($_GET['dir']) || strpos($_GET['dir'],'..')!==false || !is_numeric($_GET['msg_id']))
	die('Invalid request');

define('CID',false);
require_once('../../../include.php');
session_commit();
ModuleManager::load_modules();

if(!Acl::is_user()) die('Not logged in');

ini_set('include_path',dirname(__FILE__).'/PEAR'.PATH_SEPARATOR.ini_get('include_path'));
require_once('Mail/mimeDecode.php');

$box = Apps_MailClientCommon::get_mailbox_dir($_GET['box']).$_GET['dir'];

$message = @file_get_contents($box.'/'.$_GET['msg_id']);
if($message===false) {
	$err = 'Invalid message';
	header("Content-type: text/html");
	$script = 'parent.$(\''.$_GET['pid'].'_subject\').innerHTML=\''.Epesi::escapeJS(htmlentities($err),false).'\';'.
			'parent.$(\''.$_GET['pid'].'_address\').innerHTML=\''.Epesi::escapeJS(htmlentities($err),false).'\';'.
			'parent.$(\''.$_GET['pid'].'_attachments\').innerHTML=\''.Epesi::escapeJS('',false).'\';';

	$body = '<html>'.
			'<body>'.$err.'</body>'.
			'<script>'.$script.'</script>'.
			'</html>';
	print($body);
	exit();
}

$decode = new Mail_mimeDecode($message, "\r\n");
$structure = $decode->decode(array('decode_bodies'=>true,'include_bodies'=>true));
if(!isset($structure->headers['from']))
	$structure->headers['from'] = '';
if(!isset($structure->headers['to']))
	$structure->headers['to'] = '';
if(!isset($structure->headers['date']))
	$structure->headers['date'] = '';

if(isset($_GET['attachment_cid']) || isset($_GET['attachment_name'])) {
	if(isset($structure->parts)) {
		$parts = $structure->parts;
		for($i=0; $i<count($parts); $i++) {
			$part = $parts[$i];
			if($part->ctype_primary=='multipart' && isset($part->parts))
				$parts = array_merge($parts,$part->parts);
			//if(isset($part->disposition) && $part->disposition=='attachment' && $part->ctype_parameters['name']==$_GET['attachment']) {
			if((isset($part->headers['content-id']) && trim($part->headers['content-id'],'<>')==$_GET['attachment_cid']) || (isset($part->ctype_parameters['name']) && $part->ctype_parameters['name']==$_GET['attachment_name'])) {
				if(isset($part->headers['content-type']))
					header('Content-Type: '.$part->headers['content-type']);
				if(isset($part->headers['content-dispositon']))
					header('Content-disposotion: '.$part->headers['content-disposition']);
				echo $part->body;
				exit();
			}
		}
	}
	die('Invalid attachment');
} else {
	$body = false;
	$body_type = false;
	$body_ctype = false;
	$attachments = array();

	if($structure->ctype_primary=='multipart' && isset($structure->parts)) {
		$parts = $structure->parts;
		for($i=0; $i<count($parts); $i++) {
			$part = $parts[$i];
			if($part->ctype_primary=='multipart' && isset($part->parts))
				$parts = array_merge($parts,$part->parts);
			if($body===false && $part->ctype_primary=='text' && $part->ctype_secondary=='plain' && (!isset($part->disposition) || $part->disposition=='inline')) {
				$body = $part->body;
				$body_type = 'plain';
				$body_ctype = isset($structure->headers['content-type'])?$structure->headers['content-type']:'text/'.$body_type;
			} elseif($part->ctype_primary=='text' && $part->ctype_secondary=='html' && ($body===false || $body_type=='plain') && (!isset($part->disposition) || $part->disposition=='inline')) {
				$body = $part->body;
				$body_type = 'html';
			}
			//if(isset($part->disposition) && $part->disposition=='attachment')
			if(isset($part->ctype_parameters['name'])) {
				if(isset($part->headers['content-id']))
					$attachments[$part->ctype_parameters['name']] = trim($part->headers['content-id'],'><');
				else
					$attachments[$part->ctype_parameters['name']] = true;
			}
		}
	} elseif(isset($structure->body) && $structure->ctype_primary=='text') {
		$body = $structure->body;
		$body_type = $structure->ctype_secondary;
		$body_ctype = isset($structure->headers['content-type'])?$structure->headers['content-type']:'text/'.$body_type;
	}

	if($body===false) die('invalid message');

	$ret_attachments = '';
	if($attachments) {
		foreach($attachments as $name=>$a) {
			if($a===true)
				$ret_attachments .= '<a target="_blank" href="modules/Apps/MailClient/preview.php?'.http_build_query(array_merge($_GET,array('attachment_name'=>$name))).'">'.$name.'</a><br>';
			else
				$ret_attachments .= '<a target="_blank" href="modules/Apps/MailClient/preview.php?'.http_build_query(array_merge($_GET,array('attachment_cid'=>$a))).'">'.$name.'</a><br>';
		}
	}

	if(ereg('^(Sent|Drafts)',$_GET['dir']))
		$address = $structure->headers['to'];
	else
		$address = $structure->headers['from'];

	$subject = isset($structure->headers['subject'])?Apps_MailClientCommon::mime_header_decode($structure->headers['subject']):'no subject';
	$address = Apps_MailClientCommon::mime_header_decode($address);

	$script = 'parent.$(\''.$_GET['pid'].'_subject\').innerHTML=\''.Epesi::escapeJS(htmlentities($subject),false).'\';'.
			'parent.$(\''.$_GET['pid'].'_address\').innerHTML=\''.Epesi::escapeJS(htmlentities($address),false).'\';'.
			'parent.$(\''.$_GET['pid'].'_attachments\').innerHTML=\''.Epesi::escapeJS($ret_attachments,false).'\';';

	header("Content-type: text/html");
	if($body_type=='plain') {
		$body = htmlspecialchars(preg_replace("/(http:\/\/[a-z0-9]+(\.[a-z0-9]+)+(\/[\.a-z0-9]+)*)/i", "<a href='\\1'>\\1</a>", $body));
		$body = '<html>'.
			'<head><meta http-equiv=Content-Type content="'.$body_ctype.'"></head>'.
			'<body><pre>'.$body.'</pre></body>';
	} else {
		$body = trim($body);
		if(ereg('</html>$',$body))
			$body = substr($body,0,strlen($body)-7);
	}
	$body = preg_replace('/"cid:([^@]+@[^@]+)"/i','"preview.php?'.http_build_query($_GET).'&attachment_cid=$1"',$body);
	$body = preg_replace("/<a([^>]*)>(.*)<\/a>/i", '<a$1 target="_blank">$2</a>', $body);

	$body .= '<script>'.$script.'</script>'.
			'</html>';

	echo $body;//.'<pre>'.htmlentities(print_r($structure,true)).'</pre>';
}
Apps_MailClientCommon::read_msg($_GET['box'],$_GET['dir'],$_GET['msg_id']);

?><commit_msg>BBCodes in MailClient. Maybe a little to intrusive, please revise.<commit_after><?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // date in the past

if(!isset($_GET['msg_id']) || !isset($_GET['box']) || !is_numeric($_GET['box']) || !isset($_GET['dir']) || strpos($_GET['dir'],'..')!==false || !is_numeric($_GET['msg_id']))
	die('Invalid request');

define('CID',false);
require_once('../../../include.php');
session_commit();
ModuleManager::load_modules();

if(!Acl::is_user()) die('Not logged in');

ini_set('include_path',dirname(__FILE__).'/PEAR'.PATH_SEPARATOR.ini_get('include_path'));
require_once('Mail/mimeDecode.php');

$box = Apps_MailClientCommon::get_mailbox_dir($_GET['box']).$_GET['dir'];

$message = @file_get_contents($box.'/'.$_GET['msg_id']);
if($message===false) {
	$err = 'Invalid message';
	header("Content-type: text/html");
	$script = 'parent.$(\''.$_GET['pid'].'_subject\').innerHTML=\''.Epesi::escapeJS(htmlentities($err),false).'\';'.
			'parent.$(\''.$_GET['pid'].'_address\').innerHTML=\''.Epesi::escapeJS(htmlentities($err),false).'\';'.
			'parent.$(\''.$_GET['pid'].'_attachments\').innerHTML=\''.Epesi::escapeJS('',false).'\';';

	$body = '<html>'.
			'<body>'.$err.'</body>'.
			'<script>'.$script.'</script>'.
			'</html>';
	print($body);
	exit();
}

$decode = new Mail_mimeDecode($message, "\r\n");
$structure = $decode->decode(array('decode_bodies'=>true,'include_bodies'=>true));
if(!isset($structure->headers['from']))
	$structure->headers['from'] = '';
if(!isset($structure->headers['to']))
	$structure->headers['to'] = '';
if(!isset($structure->headers['date']))
	$structure->headers['date'] = '';

if(isset($_GET['attachment_cid']) || isset($_GET['attachment_name'])) {
	if(isset($structure->parts)) {
		$parts = $structure->parts;
		for($i=0; $i<count($parts); $i++) {
			$part = $parts[$i];
			if($part->ctype_primary=='multipart' && isset($part->parts))
				$parts = array_merge($parts,$part->parts);
			//if(isset($part->disposition) && $part->disposition=='attachment' && $part->ctype_parameters['name']==$_GET['attachment']) {
			if((isset($part->headers['content-id']) && trim($part->headers['content-id'],'<>')==$_GET['attachment_cid']) || (isset($part->ctype_parameters['name']) && $part->ctype_parameters['name']==$_GET['attachment_name'])) {
				if(isset($part->headers['content-type']))
					header('Content-Type: '.$part->headers['content-type']);
				if(isset($part->headers['content-dispositon']))
					header('Content-disposotion: '.$part->headers['content-disposition']);
				echo $part->body;
				exit();
			}
		}
	}
	die('Invalid attachment');
} else {
	$body = false;
	$body_type = false;
	$body_ctype = false;
	$attachments = array();

	if($structure->ctype_primary=='multipart' && isset($structure->parts)) {
		$parts = $structure->parts;
		for($i=0; $i<count($parts); $i++) {
			$part = $parts[$i];
			if($part->ctype_primary=='multipart' && isset($part->parts))
				$parts = array_merge($parts,$part->parts);
			if($body===false && $part->ctype_primary=='text' && $part->ctype_secondary=='plain' && (!isset($part->disposition) || $part->disposition=='inline')) {
				$body = $part->body;
				$body_type = 'plain';
				$body_ctype = isset($structure->headers['content-type'])?$structure->headers['content-type']:'text/'.$body_type;
			} elseif($part->ctype_primary=='text' && $part->ctype_secondary=='html' && ($body===false || $body_type=='plain') && (!isset($part->disposition) || $part->disposition=='inline')) {
				$body = $part->body;
				$body_type = 'html';
			}
			//if(isset($part->disposition) && $part->disposition=='attachment')
			if(isset($part->ctype_parameters['name'])) {
				if(isset($part->headers['content-id']))
					$attachments[$part->ctype_parameters['name']] = trim($part->headers['content-id'],'><');
				else
					$attachments[$part->ctype_parameters['name']] = true;
			}
		}
	} elseif(isset($structure->body) && $structure->ctype_primary=='text') {
		$body = $structure->body;
		$body_type = $structure->ctype_secondary;
		$body_ctype = isset($structure->headers['content-type'])?$structure->headers['content-type']:'text/'.$body_type;
	}

	if($body===false) die('invalid message');

	$ret_attachments = '';
	if($attachments) {
		foreach($attachments as $name=>$a) {
			if($a===true)
				$ret_attachments .= '<a target="_blank" href="modules/Apps/MailClient/preview.php?'.http_build_query(array_merge($_GET,array('attachment_name'=>$name))).'">'.$name.'</a><br>';
			else
				$ret_attachments .= '<a target="_blank" href="modules/Apps/MailClient/preview.php?'.http_build_query(array_merge($_GET,array('attachment_cid'=>$a))).'">'.$name.'</a><br>';
		}
	}

	if(ereg('^(Sent|Drafts)',$_GET['dir']))
		$address = $structure->headers['to'];
	else
		$address = $structure->headers['from'];

	$subject = isset($structure->headers['subject'])?Apps_MailClientCommon::mime_header_decode($structure->headers['subject']):'no subject';
	$address = Apps_MailClientCommon::mime_header_decode($address);

	$script = 'parent.$(\''.$_GET['pid'].'_subject\').innerHTML=\''.Epesi::escapeJS(htmlentities($subject),false).'\';'.
			'parent.$(\''.$_GET['pid'].'_address\').innerHTML=\''.Epesi::escapeJS(htmlentities($address),false).'\';'.
			'parent.$(\''.$_GET['pid'].'_attachments\').innerHTML=\''.Epesi::escapeJS($ret_attachments,false).'\';';

	header("Content-type: text/html");
	if($body_type=='plain') {
		$body = htmlspecialchars(preg_replace("/(http:\/\/[a-z0-9]+(\.[a-z0-9]+)+(\/[\.a-z0-9]+)*)/i", "<a href='\\1'>\\1</a>", $body));
		$body = '<html>'.
			'<head><meta http-equiv=Content-Type content="'.$body_ctype.'"></head>'.
			'<body><pre>'.$body.'</pre></body>';
	} else {
		$body = trim($body);
		if(ereg('</html>$',$body))
			$body = substr($body,0,strlen($body)-7);
	}
	$body = preg_replace('/"cid:([^@]+@[^@]+)"/i','"preview.php?'.http_build_query($_GET).'&attachment_cid=$1"',$body);
	$body = preg_replace("/<a([^>]*)>(.*)<\/a>/i", '<a$1 target="_blank">$2</a>', $body);

	$body .= '<script>'.$script.'</script>'.
			'</html>';

	echo Utils_BBCodeCommon::parse($body);//.'<pre>'.htmlentities(print_r($structure,true)).'</pre>';
}
Apps_MailClientCommon::read_msg($_GET['box'],$_GET['dir'],$_GET['msg_id']);

?>
{'fraction_non_alphanumeric': 0.2808582449373192, 'fraction_numerical': 0.004339440694310511, 'mean_word_length': 6.277777777777778, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 203, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '347626', 'n_tokens_mistral': 4717, 'n_tokens_neox': 4372, 'n_words': 457}