<?php
/*---------------------------------------------------+
| PHP-Fusion 6 Content Management System
+----------------------------------------------------+
| Copyright © 2002 - 2006 Nick Jones
| http://www.php-fusion.co.uk/
+----------------------------------------------------+
| Released under the terms & conditions of v2 of the
| GNU General Public License. For details refer to
| the included gpl.txt file or visit http://gnu.org
+----------------------------------------------------+
| Modified by PHP-Fusion SF Group
| Copyright © 2007 by SF
| http://www.rus-phpfusion.com/
+----------------------------------------------------*/
require_once "../maincore.php";
require_once BASEDIR."subheader.php";
require_once ADMIN."navigation.php";

if (!checkrights("S3")) fallback("../index.php");
if (!isset($rowstart) || !isNum($rowstart)) $rowstart = 0;

if (isset($delattach)) {
	$result = dbquery("SELECT * FROM ".$db_prefix."downloads WHERE download_id='$download_id'");
	if (dbrows($result) != 0) {
		$attach = dbarray($result);
		@unlink(BASEDIR."downloads/".$attach['attach_name']);
		$result2 = dbquery("DELETE FROM ".$db_prefix."downloads WHERE download_id='$download_id'");
	}
}elseif(isset($_POST['delselect'])){
	if(is_array($chk_mark) && count($chk_mark)>1){
		$result_attach_id="download_id IN(".implode(',',$chk_mark).")";
	}else{
		$result_attach_id="download_id=".$chk_mark[0];
	}
	$result = dbquery("SELECT * FROM ".$db_prefix."forum_attachments WHERE $result_attach_id");
	if (dbrows($result) != 0) {
		while($attach = dbarray($result)){
			@unlink(FORUM."attachments/".$attach['attach_name']);
		}
		$result2 = dbquery("DELETE FROM ".$db_prefix."forum_attachments WHERE $result_attach_id");
	}
}

$rows = dbrows(dbquery("SELECT * FROM ".$db_prefix."forum_attachments"));
$result3 = dbquery("SELECT SUM(attach_size) as tsize FROM ".$db_prefix."forum_attachments");
$tsize = dbarray($result3);

opentable("Информация");
echo "<table align='center' width='100%'>\n<tr>\n<td width='33%' class='small'>
<span class='alt'>Всего файлов:</span> ".dbcount("(attach_id)", "forum_attachments", "")."<br>
<span class='alt'>Общий размер:</span> ".parsebytesize($tsize['tsize'])."<br>
</td>\n</tr>\n</table>\n";
closetable();
tablebreak();

opentable("Управление загрузками");
$result = dbquery("SELECT * FROM ".$db_prefix."forum_attachments LIMIT $rowstart,15");
if (dbrows($result) != 0) {
echo "<form name='delattachall' method='post' action='".FUSION_SELF."'>
<table width='100%' align='center' cellpadding='0' cellspacing='1' class='tbl-border'>
<tr>
<td width='1%' class='tbl2'></td>
<td class='tbl2'>Имя файла</td>
<td class='tbl2'>Пост</td>
<td class='tbl2'>Размер</td>
<td class='tbl2'>Файл</td>
<td class='tbl2'>Опции</td>
</tr>";

while($attach = dbarray($result)){
	$result2 = dbquery("SELECT * FROM ".$db_prefix."posts WHERE post_id=".$attach['post_id']." and thread_id=".$attach['thread_id']."");
	$post = dbarray($result2);

	$fileattach = FORUM."attachments/".$attach['attach_name'];
	if (!file_exists($fileattach)){
		$fs = "<font color='red'>Не найден</font>";
	}else{
		$fs = "<font color='green'>Найден</font>";
	}
	echo "
	<tr>
	<td class='tbl1'><input type='checkbox' name='chk_mark[]' value='".$attach['attach_id']."'></td>
	<td class='tbl1'><a href='".FORUM."attachments/".$attach['attach_name']."'>".$attach['attach_name']."</a></td>
	<td class='tbl1'><a href='".FORUM."viewthread.php?forum_id=".$post['forum_id']."&thread_id=".$post['thread_id']."#".$post['post_id']."'>".$post['post_subject']."</a></td>
	<td class='tbl1'>".parsebytesize($attach['attach_size'])."</td>
	<td class='tbl1'>".$fs."</td>
	<td class='tbl1'><a href='".FUSION_SELF."?delattach=".$attach['attach_id']."&post_id=".$attach['post_id']."'>Удалить</a></td>
	</tr>";
}

echo "<tr>
<td colspan='6' class='tbl2'><nobr><span class='small'>
<input type='button' class='button' title='Выделить все' onclick=\"javascript:setChecked('delattachall','chk_mark[]',1);\" value='Выделить все'</input>
<input type='button' class='button' title='Отменить выделение' onclick=\"javascript:setChecked('delattachall','chk_mark[]',0);\" value='Отменить выделение'</input>
<input type='submit' class='button' name='delselect' value='Удалить выбранное'><nobr></span>
</td>
</tr></table></form>
\n";

echo "<div align='center' style='margin-top:5px;'>
".makePageNav($rowstart,15,$rows,3,FUSION_SELF."?attachments_forum.php&")."
</div>\n";

echo "
<script language='Javascript'>
function setChecked(frmName,chkName,val){
	dml=document.forms[frmName];
	len=dml.elements.length;
	for(i=0;i<len;i++){
		if(dml.elements[i].name==chkName){
			dml.elements[i].checked=val;
		}
	}
}
</script>\n";

}else{
echo "<center><br />Файлы не найдены<br /><br /></center>\n";
}
closetable();

echo "</td>\n";
require_once BASEDIR."footer.php";
?>
