<?

if ($permissions['unlimitedTraffic'] || $quota->canAddTraffic()) {



if ($sys['uid']) {

	$shareInfo = $share->getInfo($sys['uid'], $sys['path'], $sys['object']);
	$sys['fullPath'] = $shareInfo['path'];

} else {

$tmp['path'] = gluePath($permissions['path'], $sys['path']);
if (is_dir($tmp['path'])) {
	if (is_readable($tmp['path'])) {
	} else {
		$sys['path'] = "";
	}
} else {
	$sys['path'] = "";
}
$sys['fullPath'] = gluePath($permissions['path'], $sys['path']);

}





$split = explode("|", $str->fromHTML($_POST['itemlist']));
foreach ($split as $key => $file) {
	if ($file != "") {
		if (isCleanFilename($file)) {
		$items[] = $file;
		}
	}
}



if ($permissions['byemail']) {

	/*
	// a default "from" address
	if (strlen(trim($_POST['fromaddr'])) == 0) {
		$_POST['fromaddr'] = "";
	}
	*/



if ($_POST['submited']) {
	if (strlen($str->fromHTML($_POST['fromaddr'])) < 5) {
		$msg = $lang->get("Please fill the \"from address\" field.");
		$statusBarMessage[] = array($msg, 1);
	} else if (strlen($str->fromHTML($_POST['toaddr'])) < 5) {
		$msg = $lang->get("Please enter an e-mail address.");
		$statusBarMessage[] = array($msg, 1);
	} else {
		$mail = new htmlMimeMail();
		$message = $str->fromHTML($_POST['message']);
		$message_footer_file = gluePath($sys['root'], "customizable/private/send_by_mail_message_footer.txt");
		if (file_exists($message_footer_file)) {
			$message .= file_get_contents($message_footer_file);
		}
		//echo $message;
		$mail->setText($message);
		$logText = "";
		for ($i = 0 ; $i < sizeof($items) ; $i++) {
			$file = $str->fromHTML($items[$i]);
			//attachment part
			$path = gluePath($sys['fullPath'], $file);
			$filepath = $mail->getFile($path, $file);
			$mail->addAttachment($filepath, $file, "application/octet-stream");
			$logText .= $path."\r\n";
		}
		$mail->setFrom($str->fromHTML($_POST['fromaddr']));
		$mail->setSubject($str->fromHTML($_POST['subject']));
		$result = @$mail->send(array($str->fromHTML($_POST['toaddr'])));
		if(!empty($result)) {
		$msg = $lang->get("E-mail sent.");
		$statusBarMessage[] = array($msg, 0);
		$log->act("0", "email", $logText);
		} else {
		$msg = $lang->get("Failed to send e-mail.");
		$statusBarMessage[] = array($msg, 1);
		$log->act("1", "email", $logText);
		}
	}
}

$title = $lang->get("Send by e-mail");
//template header
require_once($sys['root']."/system/templates/popup/header.inc");
?>



<form action="?file=byemail&uid=<?echo $str->forURL($sys['uid'])?>" method="post" name="byEmailForm">
<table cellspacing="0" cellpadding="0" border="0" style="width:100%;height:100%;background-color:#F2F2F2">
<tr>
    <td background="images/popup/top-bg.gif" height="26">&nbsp;</td>
</tr>

<tr>
    <td style="padding:10px;">
<!-- Popup Contents -->

<table cellspacing="0" cellpadding="2" border="0">
<tr>
    <td><b><?$lang->display("From address");?></b></td>
    <td><input type="text" name="fromaddr" value="<?echo $str->safeHTML($str->fromHTML($_POST['fromaddr']))?>"></td>
</tr>
<tr>
    <td><b><?$lang->display("Send to");?></b></td>
    <td><input type="text" name="toaddr" value="<?echo $str->safeHTML($str->fromHTML($_POST['toaddr']))?>"></td>
</tr>
<tr>
    <td><b><?$lang->display("Subject");?></b></td>
    <td><input type="text" name="subject" value="<?echo $str->safeHTML($str->fromHTML($_POST['subject']))?>" style="width:330px"></td>
</tr>
<tr>
	<td valign="top"><b><?$lang->display("Message");?></b></td>
	<td>
<textarea cols="39" rows="7" name="message" style="width:330px"><?
if (strlen($_POST['message']) > 0) {
	echo $str->safeHTML($str->fromHTML($_POST['message']));
} else {
	for ($i = 0 ; $i < sizeof($items) ; $i++) {
		$file = $items[$i];
		$path = gluePath($sys['fullPath'],$file);
		if (@is_file($path)) {
			$res = $comments->get($path);
			if (is_array($res)) {
				for ($j = 0 ; $j < sizeof($res) ; $j++) {
				?>
=====================================
<?echo $items[$i];?>

-------------------------------------

<?echo $comments->tmpdb->db[$res[$j]]['user'];?>:
<?echo $comments->tmpdb->db[$res[$j]]['msg'];?>


<?
				}
			}
		}
	}
}
?></textarea>
	</td>
</tr>


<tr>
    <td valign="top"><b><?$lang->display("Files");?></b><br><br>
<span class="comment" style="white-space:nowrap">(<?
$size = 0;
for ($i = 0 ; $i < sizeof($items) ; $i++) {
	$file = $items[$i];
	$path = gluePath($sys['fullPath'],$file);
	if (is_file($path)) {
		$size = $size+filesize($path);
	}
}?>
<?
echo $fm->formatFileSize($size);
?>)</span>
	 
	 </td>
    <td valign="top">
<div style="width:330px;height:70px;overflow:auto;padding-top:10px;padding-left:10px;border-top: 2px solid #808080;border-left:2px solid #808080;border-right:1px solid #D8D0C8;border-bottom:1px solid #D8D0C8;background-color:white">
<?
for ($i = 0 ; $i < sizeof($items) ; $i++) {
	$file = $items[$i];
	$extension = $fm->getExtension($file);
	$fileinfo = $fm->fileTypeInfo($extension);
	$iconfolder = "images/icons/filetypes/small";
	$icon = $img->icon($file, false, false, false);
	$path = gluePath($sys['fullPath'], $file);
	
	if (is_file($path)) {
?><img src="<?echo $icon?>" align="middle" width="16" height="16" border="0"> <?echo $file?> <span class="comment">(<?echo $fm->formatFileSize(filesize($path))?>)</span><br>
<?}
}
?>
</div>
	</td>
</tr>
</table>


	</td>
</tr>

<tr>
    <td align="center">
	
<table border="0" cellspacing="0" cellpadding="1" style="position:relative;top:16px;z-index:1" width="120">
<tr>
	<td>
<?
$tpl->btn(
$lang->get("Send"), 
"document.byEmailForm.submit()"
)
?>
	</td>
	<td>
<?
$tpl->btn(
$lang->get("Close"), 
"self.close();window.close()"
)
?>
	</td>
</tr>
</table>

	</td>
</tr>

<tr>
    <td background="images/popup/bottom-bg.gif" height="46">&nbsp;</td>
</tr>
</table>
<input type="hidden" name="submited" value="1">
<input type="hidden" name="path" value="<?echo $str->forURL($sys['path'])?>">
<input type="hidden" name="itemlist" value="<?echo $str->forURL($str->fromHTML($_POST['itemlist']))?>">
</form>

<?
//template footer
require_once($sys['root']."/system/templates/popup/footer.inc");
}

} else {
?>
<script language="JavaScript1.2" type="text/javascript">
alert('<?$lang->display("Your traffic quota has been exceeded!")?>');
window.close();
self.close();
</script>
<?
}
?>