<?php

/*
	[Discuz!] (C)2001-2006 Comsenz Inc.
	This is NOT a freeware, use is subject to license terms

	$RCSfile: attachments.inc.php,v $
	$Revision: 1.13 $
	$Date: 2006/08/28 00:33:07 $
*/

if(!defined('IN_DISCUZ') || !isset($PHP_SELF) || !preg_match("/[\/\\\\]admincp\.php$/", $PHP_SELF)) {
        exit('Access Denied');
}

cpheader();

$app = 35;	// Attachments per page

if(!submitcheck('deletesubmit') && !submitcheck('searchsubmit')) {
	require_once DISCUZ_ROOT.'./include/forum.func.php';
	shownav('attachments_search');

?>
<form method="post" action="admincp.php?action=attachments&">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">

<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td colspan="2"><?=$lang['attachments_search']?></td></tr>

<tr><td class="altbg1"><?=$lang['attachments_nomatched']?></td>
<td class="altbg2" align="right"><input class="checkbox" type="checkbox" name="nomatched" value="1"></td></tr>

<tr><td class="altbg1"><?=$lang['attachments_forum']?></td>
<td class="altbg2" align="right"><select name="inforum"><option value="all">&nbsp;&nbsp;> <?=$lang['all']?></option>
<option value="">&nbsp;</option><?=forumselect()?></select></td></tr>

<tr><td class="altbg1"><?=$lang['attachments_sizeless']?></td>
<td class="altbg2" align="right"><input type="text" name="sizeless" size="40"></td></tr>

<tr><td class="altbg1"><?=$lang['attachments_sizemore']?></td>
<td class="altbg2" align="right"><input type="text" name="sizemore" size="40"></td></tr>

<tr><td class="altbg1"><?=$lang['attachments_dlcountless']?></td>
<td class="altbg2" align="right"><input type="text" name="dlcountless" size="40"></td></tr>

<tr><td class="altbg1"><?=$lang['attachments_dlcountmore']?></td>
<td class="altbg2" align="right"><input type="text" name="dlcountmore" size="40"></td></tr>

<tr><td class="altbg1"><?=$lang['attachments_daysold']?></td>
<td class="altbg2" align="right"><input type="text" name="daysold" size="40"></td></tr>

<tr><td class="altbg1"><?=$lang['attachments_filename']?></td>
<td class="altbg2" align="right"><input type="text" name="filename" size="40"></td></tr>

<tr><td class="altbg1"><?=$lang['attachments_keyword']?></td>
<td class="altbg2" align="right"><input type="text" name="keywords" size="40"></td></tr>

<tr><td class="altbg1"><?=$lang['attachments_author']?></td>
<td class="altbg2" align="right"><input type="text" name="author" size="40"></td></tr>

</table><br><center>
<input class="button" type="submit" name="searchsubmit" value="<?=$lang['submit']?>"></center>
</form>
<?

} elseif(submitcheck('searchsubmit')) {

	require_once DISCUZ_ROOT.'./include/attachment.func.php';

	$sql = "a.pid=p.pid";

	if($inforum != 'all') {
		if($inforum) {
			$sql .= " AND p.fid='$inforum'";
		} else {
			cpmsg('attachments_forum_invalid');
		}
	}
	if($daysold) {
		$sql .= " AND p.dateline<='".($timestamp - (86400 * $daysold))."'";
	}
	if($author) {
		$sql .= " AND p.author='$author'";
	}
	if($filename) {
		$sql .= " AND a.filename LIKE '%$filename%'";
	}
	if($keywords) {
		$sqlkeywords = $or = '';
		foreach(explode(',', str_replace(' ', '', $keywords)) as $keyword) {
			$sqlkeywords .= " $or a.description LIKE '%$keyword%'";
			$or = 'OR';
		}
		$sql .= " AND ($sqlkeywords)";
	}
	if($sizeless) {
		$sql .= " AND a.filesize<'$sizeless'";
	}
	if($sizemore) {
		$sql .= " AND a.filesize>'$sizemore' ";
	}
	if($dlcountless) {
		$sql .= " AND a.downloads<'$dlcountless'";
	}
	if($dlcountmore) {
		$sql .= " AND a.downloads>'$dlcountmore'";
	}

	$attachments = '';
	$query = $db->query("SELECT a.*, p.fid, p.author, t.tid, t.tid, t.subject, f.name AS fname
		FROM {$tablepre}attachments a, {$tablepre}posts p, {$tablepre}threads t, {$tablepre}forums f
		WHERE t.tid=a.tid AND f.fid=p.fid AND t.displayorder>='0' AND p.invisible='0' AND $sql");
	while($attachment = $db->fetch_array($query)) {
		$matched = file_exists($attachdir.'/'.$attachment['attachment']) ? '' : "$lang[attachments_lost]";
		$attachsize = sizecount($attachment['filesize']);
		if(!$nomatched || ($nomatched && $matched)) {
			$attachments .= "<tr><td class=\"altbg1\" align=\"center\" valign=\"middle\"><input class=\"checkbox\" type=\"checkbox\" name=\"delete[]\" value=\"$attachment[aid]\"></td>\n".
				"<td class=\"altbg2\" align=\"center\"><b>$attachment[filename]</b><br>$attachment[description]</td>\n".
				"<td class=\"altbg1\" align=\"center\"><b>".($matched ? $matched : "<a href=\"attachment.php?aid=$attachment[aid]\" target=\"_blank\">[$lang[attachments_download]]</a>")."</b><br><a href=\"$attachurl/$attachment[attachment]\" class=\"smalltxt\" target=\"_blank\">$attachment[attachment]</a></td>\n".
				"<td class=\"altbg2\" align=\"center\">$attachment[author]</td>\n".
				"<td class=\"altbg1\" valign=\"middle\"><a href=\"viewthread.php?tid=$attachment[tid]\" target=\"_blank\"><b>".cutstr($attachment['subject'], 18)."</b></a><br>$lang[forum]:<a href=\"forumdisplay.php?fid=$attachment[fid]\" target=\"_blank\">$attachment[fname]</a></td>\n".
				"<td class=\"altbg2\" valign=\"middle\" align=\"center\">$attachsize</td>\n".
				"<td class=\"altbg1\" valign=\"middle\" align=\"center\">$attachment[downloads]</td></tr>\n";
		}
	}
	shownav('attachments_search');

?>
<form method="post" action="admincp.php?action=attachments&">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td><input class="checkbox" type="checkbox" name="chkall" onclick="checkall(this.form)"><?=$lang['del']?></td>
<td width="15%" align="center"><?=$lang['attachments_name']?></td>
<td width="25%" align="center"><?=$lang['filename']?></td>
<td width="14%" align="center"><?=$lang['author']?></td>
<td width="23%" align="center"><?=$lang['attachments_thread']?></td>
<td width="8%" align="center"><?=$lang['size']?></td>
<td width="8%" align="center"><?=$lang['download']?></td></tr>
<?=$attachments?>
</table><br>
<center><input class="button" type="submit" name="deletesubmit" value="<?=$lang['submit']?>"></center></form>
<?

} elseif(submitcheck('deletesubmit')) {

	if(is_array($delete)) {

		$ids = '\''.implode('\',\'', $delete).'\'';

		$tids = $pids = 0;
		$query = $db->query("SELECT tid, pid, attachment FROM {$tablepre}attachments WHERE aid IN ($ids)");
		while($attach = $db->fetch_array($query)) {
			@unlink($attachdir.'/'.$attach['attachment']);
			$tids .= ','.$attach['tid'];
			$pids .= ','.$attach['pid'];
		}
		$db->query("DELETE FROM {$tablepre}attachments WHERE aid IN ($ids)");
		$db->query("UPDATE {$tablepre}posts SET attachment='0' WHERE pid IN ($pids)");

		$attachtids = 0;
		$query = $db->query("SELECT tid FROM {$tablepre}attachments WHERE tid IN ($tids) GROUP BY tid ORDER BY pid DESC");
		while($attach = $db->fetch_array($query)) {
			$attachtids .= ','.$attach['tid'];
		}
		$db->query("UPDATE {$tablepre}threads SET attachment='' WHERE tid IN ($tids)".($attachtids ? " AND tid NOT IN ($attachtids)" : NULL));

		cpmsg('attachments_edit_succeed');

	} else {

		cpmsg('attachments_edit_invalid');

	}

}

?>