<?php
//
//	file: includes/acp/acp_attach_search.php
//	author: ptirhiik
//	begin: 27/02/2006
//	version: 1.6.0 - 10/06/2006
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hack attempt');
}

// get attachment fields def
include_once($config->url('includes/class_attach_cp_fields'));

// ok, we will need forums as well auths
if ( !is_object($forums) )
{
	include_once($config->url('includes/class_forums'));
	$forums = new forums();
	$forums->read();
}
$caches = array();
if ( !$user->cache[POST_FORUM_URL] )
{
	$caches[] = POST_FORUM_URL;
}
if ( !$user->cache[POST_FORUM_URL . 'jbox'] )
{
	$caches[] = POST_FORUM_URL . 'jbox';
}
if ( !empty($caches) )
{
	$user->get_cache(array(POST_FORUM_URL, POST_FORUM_URL . 'jbox'));
}

$cp_title = 'Attach_search';
$cp_title_explain = 'Attach_search_explain';

$available_sort = array(
	'filename' => 'Sort_Filename',
	'comment' => 'Sort_Comment',
	'extension' => 'Sort_Extension',
	'filesize' => 'Sort_Size',
	'downloads' => 'Sort_Downloads',
	'posttime' => 'Sort_Posttime',
	'author' => 'Sort_Username',
);
$sort_fields = array(
	'filename' => 'd.real_filename',
	'comment' => 'd.file_comment',
	'extension' => 'd.extension',
	'filesize' => 'd.filesize',
	'downloads' => 'd.download_count',
	'posttime' => 'd.filetime',
	'author' => 'max_username',
);
$available_order = array(
	'' => 'Sort_Ascending',
	'desc' => 'Sort_Descending',
);
$order_fields = array(
	'' => '',
	'desc' => ' DESC',
);
$dft_sort = 'filename';
$dft_order = '';

$topicdays_list = array(0 => 'All_Attachments', 1 => '1_Day', 7 => '7_Days', 14 => '2_Weeks', 30 => '1_Month', 90 => '3_Months', 180 => '6_Months', 364 =>'1_Year');

// get the forums list selection
$front_pic = $forums->get_front_pic();
$forums_list = array(0 => $user->lang('All_Attachments'));
if ( !empty($front_pic) )
{
	foreach ( $front_pic as $cur_id => $front )
	{
		if ( $cur_id != 0 )
		{
			$option = '';
			$count_front = strlen($front);
			for ( $i = 0; $i < $count_front; $i++ )
			{
				$option .= $user->lang('tree_pic_' . $front[$i]);
			}
			if ( $cur_id > 0 )
			{
				$option .= $user->lang($forums->data[$cur_id]['forum_name']);
			}
			$forums_list[$cur_id] = $option;
		}
	}
}

$parm_user_id = _read(POST_USERS_URL, TYPE_INT);
$parm_username = '';
if ( !empty($parm_user_id) )
{
	$sql = 'SELECT username
				FROM ' . USERS_TABLE . '
				WHERE user_id = ' . intval($parm_user_id);
	$result = $db->sql_query($sql, false, __LINE__, __FILE__);
	$parm_username = ($row = $db->sql_fetchrow($result)) ? $row['username'] : '';
	$db->sql_freeresult($result);
}

$fields = array(
	'filename' => array('type' => 'attach_search_username', 'legend' => 'File_name', 'explain' => 'Search_wildcard_explain'),
	'file_comment' => array('type' => 'attach_search_username', 'legend' => 'File_comment', 'explain' => 'Search_wildcard_explain'),
	'author' => array('type' => 'attach_search_username', 'legend' => 'Search_author', 'explain' => 'Search_author_explain', 'length' => 25, 'value' => $parm_username),
	'sizemin' => array('type' => 'attach_filesize', 'legend' => 'Size_between'),
	'sizemax' => array('type' => 'attach_filesize', 'combined' => true, 'linefeed' => true),
	'countmin' => array('type' => 'int', 'legend' => 'Count_between'),
	'countmax' => array('type' => 'int', 'combined' => true, 'linefeed' => true),
	'topicdays' => array('type' => 'list', 'legend' => 'More_days_old', 'options' => $topicdays_list),
	POST_FORUM_URL => array('type' => 'list', 'legend' => 'Forum', 'options' => $forums_list),
	'sort' => array('type' => 'list', 'legend' => 'Sort_by', 'options' => $available_sort, 'value' => $dft_sort),
	'order' => array('type' => 'list', 'combined' => true, 'options' => $available_order, 'value' => $dft_order),
);

class attach_search extends form
{
	var $data;
	var $total;
	var $start;
	var $ppage;
	var $now;

	var $handler;

	function attach_search($requester, $parms='', &$fields)
	{
		global $attach_config;

		$this->data = array();
		$this->total = 0;
		$this->requester = $requester;
		$this->parms = empty($parms) ? array() : $parms;
		$this->now = time();

		$this->handler = new file_handler($attach_config->data);

		parent::form($fields);
	}

	function process()
	{
		if ( $this->init() )
		{
			$this->check();
			$this->validate();
			$this->display();
			return true;
		}
		return false;
	}

	function init()
	{
		global $db, $config, $user, $forums;
		global $sort_fields, $order_fields;
		global $dft_sort, $dbt_order;

		parent::init();
		$this->start = _read('start', TYPE_INT);
		$this->ppage = _read('ppage', TYPE_INT, intval($config->data['topics_per_page']));
		$this->total = 0;
		$this->data = array();

		// check searches
		$filename = preg_replace('/\*/', '%', $this->fields['filename']->value);
		$filename = trim(str_replace('%', '', $filename)) == '' ? '' : $filename;

		$file_comment = preg_replace('/\*/', '%', $this->fields['file_comment']->value);
		$file_comment = trim(str_replace('%', '', $file_comment)) == '' ? '' : $file_comment;

		$author = preg_replace('/\*/', '%', $this->fields['author']->value);
		$author = trim(str_replace('%', '', $author)) == '' ? '' : $author;

		$size_min = $this->fields['sizemin']->value;
		$size_max = $this->fields['sizemax']->value;

		$download_min = $this->fields['countmin']->value;
		$download_max = $this->fields['countmax']->value;

		$topicdays = $this->fields['topicdays']->value;

		$forum_id = ($this->fields[POST_FORUM_URL]->value > 0) ? $this->fields[POST_FORUM_URL]->value : 0;

		$author_id = _read(POST_USERS_URL, TYPE_INT);
		$author_ids = array();
		if ( $author || $author_id )
		{
			$sql = 'SELECT user_id
						FROM ' . USERS_TABLE . '
						WHERE ' . (empty($author_id) ? 'username LIKE \'' . $db->sql_escape_string($author) . '\'' : 'user_id = ' . intval($author_id));
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$author_ids[] = intval($row['user_id']);
			}
			$db->sql_freeresult($result);
		}

		$sql_where = array();
		if ( !empty($filename) )
		{
			$sql_where[] = 'd.real_filename LIKE \'' . $db->sql_escape_string($filename) . '\'';
		}
		if ( !empty($file_comment) )
		{
			$sql_where[] = 'd.file_comment LIKE \'' . $db->sql_escape_string($file_comment) . '\'';
		}
		if ( !empty($size_min) )
		{
			$sql_where[] = 'd.filesize > ' . intval($size_min);
		}
		if ( !empty($size_max) )
		{
			$sql_where[] = 'd.filesize <= ' . intval($size_max);
		}
		if ( !empty($download_min) )
		{
			$sql_where[] = 'd.download_count > ' . intval($download_min);
		}
		if ( !empty($download_max) )
		{
			$sql_where[] = 'd.download_count <= ' . intval($download_max);
		}
		if ( !empty($topicdays) )
		{
			$sql_where[] = 'd.filetime > ' . ($this->now - (intval($topicdays) * 86400));
		}
		if ( !empty($forum_id) )
		{
			$sql_where[] = 'p.forum_id = ' . intval($forum_id);
		}
		if ( !empty($author_ids) )
		{
			$sql_where[] = 'a.user_id IN(' . implode(', ', $author_ids) . ')';
		}

		// get total of rows
		if ( empty($author_ids) && empty($forum_id) )
		{
			$sql = 'SELECT DISTINCT d.attach_id
						FROM ' . ATT_DESC_TABLE . ' d' . (empty($sql_where) ? '' : '
						WHERE ' . implode('
							AND ', $sql_where));
		}
		else
		{
			$sql = 'SELECT d.attach_id
						FROM ' . ATT_DESC_TABLE . ' d, ' . ATT_LINKS_TABLE . ' a' . (empty($forum_id) ? '' : ', ' . POSTS_TABLE . ' p') . '
						WHERE d.attach_id = a.attach_id' . (empty($forum_id) ? '' : '
							AND p.post_id = a.post_id') . (empty($sql_where) ? '' : '
							AND ' . implode('
							AND ', $sql_where)) . '
						GROUP BY d.attach_id';
		}
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$this->total = $db->sql_numrows($result);
		$db->sql_freeresult($result);

		// get rows
		if ( $this->total )
		{
			// get sort and order
			$sort_field = $sort_fields[ $this->fields['sort']->value ];
			$order_field = $order_fields[ $this->fields['order']->value ];

			// topics titles to get
			$post_ids = array();
			$user_ids = array();
			$req_user = $this->fields['sort']->value == 'author';

			$basic_fields = array(
				'd.attach_id',
				'd.physical_filename',
				'd.real_filename',
				'd.download_count',
				'd.file_comment',
				'd.extension',
				'd.filesize',
				'd.filetime',
			);
			$computed_fields = array(
				'MAX(a.user_id) AS max_user_id',
				'MAX(a.post_id) AS max_post_id',
				'COUNT(a.attach_id) AS count_attach_id',
			);
			if ( $req_user )
			{
				$computed_fields[] = 'MAX(u.username) AS max_username';
			}

			// we don't need a strict join between attachments desc & attachments
			if ( empty($forum_id) && empty($author_ids) )
			{
				$sql = 'SELECT ' . implode(', ', $basic_fields) . ', ' . implode(', ', $computed_fields) . '
							FROM ' . ATT_DESC_TABLE . ' d
								LEFT JOIN ' . ATT_LINKS_TABLE . ' a
									ON a.attach_id = d.attach_id' . (!$req_user ? '' : '
								LEFT JOIN ' . USERS_TABLE . ' u
									ON u.user_id = a.user_id') . (empty($sql_where) ? '' : '
							WHERE ' . implode('
								AND ', $sql_where)) . '
							GROUP BY ' . implode(', ', $basic_fields) . '
							ORDER BY ' . $sort_field . $order_field . ', d.attach_id DESC
							LIMIT ' . $this->start . ', ' . $this->ppage;
			}
			// we need a strict join between attachments desc & attachments
			else
			{
				$sql = 'SELECT ' . implode(', ', $basic_fields) . ', ' . implode(', ', $computed_fields) . '
							FROM ' . ATT_DESC_TABLE . ' d, ' . ATT_LINKS_TABLE . ' a' . (empty($forum_id) ? '' : ', ' . POSTS_TABLE . ' p') . (!$req_user ? '' : '
								LEFT JOIN ' . USERS_TABLE . ' u
									ON u.user_id = a.user_id') . '
							WHERE d.attach_id = a.attach_id' . (empty($forum_id) ? '' : '
								AND p.post_id = a.post_id') . (empty($sql_where) ? '' : '
								AND ' . implode('
								AND ', $sql_where)) . '
							GROUP BY ' . implode(', ', $basic_fields) . '
							ORDER BY ' . $sort_field . $order_field . ', d.attach_id DESC
							LIMIT ' . $this->start . ', ' . $this->ppage;
			}
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$this->data[ intval($row['attach_id']) ] = $row;
				if ( intval($row['max_post_id']) )
				{
					if ( isset($post_ids[ intval($row['max_post_id']) ]) )
					{
						$post_ids[ intval($row['max_post_id']) ][] = intval($row['attach_id']);
					}
					else
					{
						$post_ids[ intval($row['max_post_id']) ] = array(intval($row['attach_id']));
					}
				}
				if ( intval($row['max_user_id']) && !$req_user )
				{
					if ( isset($user_ids[ intval($row['max_user_id']) ]) )
					{
						$user_ids[ intval($row['max_user_id']) ][] = intval($row['attach_id']);
					}
					else
					{
						$user_ids[ intval($row['max_user_id']) ] = array(intval($row['attach_id']));
					}
				}
			}
			$db->sql_freeresult($result);

			// get usernames
			if ( !empty($user_ids) )
			{
				$sql = 'SELECT user_id, username
							FROM ' . USERS_TABLE . '
							WHERE user_id IN(' . implode(', ', array_keys($user_ids)) . ')';
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				while ( $row = $db->sql_fetchrow($result) )
				{
					$count_attach_ids = count($user_ids[ intval($row['user_id']) ]);
					for ( $i = 0; $i < $count_attach_ids; $i++ )
					{
						$this->data[ $user_ids[ intval($row['user_id']) ][$i] ]['max_username'] = $row['username'];
					}
				}
				$db->sql_freeresult($result);
			}
			unset($user_ids);

			// get topics titles
			if ( !empty($post_ids) )
			{
				$sql = 'SELECT p.post_id, t.topic_title, t.forum_id
							FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
							WHERE t.topic_id = p.topic_id
								AND p.post_id IN(' . implode(', ', array_keys($post_ids)) . ')';
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				while ( $row = $db->sql_fetchrow($result) )
				{
					if ( !$user->auth(POST_FORUM_URL, 'auth_read', intval($row['forum_id'])) )
					{
						$row['topic_title'] = $user->lang('Not_Authorised');
					}
					$count_attach_ids = count($post_ids[ intval($row['post_id']) ]);
					for ( $i = 0; $i < $count_attach_ids; $i++ )
					{
						$this->data[ $post_ids[ intval($row['post_id']) ][$i] ]['topic_title'] = $row['topic_title'];
					}
				}
				$db->sql_freeresult($result);
			}
			unset($post_ids);
		}

		return true;
	}

	function check()
	{
		global $config, $user;
		global $error, $error_msg;

		if ( _button('submit_form') )
		{
			parent::check();
		}
		if ( $error )
		{
			message_return($error_msg, 'Click_return_attach_userlist', $config->url($this->requester, $this->parms, true));
		}
	}

	function validate()
	{
		global $db, $config, $user;

		$return_msg = '';
		if ( _button('edit_details') || _button('delete_details') )
		{
			// get vars
			$attach_ids = get_var('attach_ids', array(0));
			$attach_ids = empty($attach_ids) ? array() : array_flip($attach_ids);
			$file_comments = get_var('file_comments', array(''));
			$download_counts = get_var('download_counts', array(0));
			$delete_box = get_var('delete_box', array(0));
			$delete_box = empty($delete_box) ? array() : array_flip($delete_box);

			$deleted_ids = array();
			$updated = false;
			foreach ( $this->data as $attach_id => $data )
			{
				if ( isset($attach_ids[$attach_id]) )
				{
					$idx = $attach_ids[$attach_id];
					if ( _button('delete_details') )
					{
						if ( isset($delete_box[$attach_id]) )
						{
							$deleted_ids[] = $attach_id;
						}
					}
					else if ( _button('edit_details') )
					{
						$fields = array();
						if ( isset($file_comments[$idx]) )
						{
							$file_comments[$idx] = trim(htmlspecialchars(stripslashes($file_comments[$idx])));
							if ( $file_comments[$idx] != $data['file_comment'] )
							{
								$fields['file_comment'] = $file_comments[$idx];
							}
						}
						if ( isset($download_counts[$idx]) )
						{
							$download_counts[$idx] = intval($download_counts[$idx]);
							if ( $download_counts[$idx] != $data['download_count'] )
							{
								$fields['download_count'] = $download_counts[$idx];
							}
						}
						if ( !empty($fields) )
						{
							$db->sql_statement($fields);
							$sql = 'UPDATE ' . ATT_DESC_TABLE . '
										SET ' . $db->sql_update . '
										WHERE attach_id = ' . intval($attach_id);
							$db->sql_query($sql, false, __LINE__, __FILE__);
							$updated = true;
						}
					}
				}
			}

			// prepare return
			if ( $updated )
			{
				$return_msg = 'Attachments_updated';
			}

			$privmsgs_ids = array();
			$post_ids = array();
			$topic_ids = array();
			if ( $count_deleted_ids = count($deleted_ids) )
			{
				// delete physical files
				$this->handler->open_channel();
				for ( $i = 0; $i < $count_deleted_id; $i++ )
				{
					$this->handler->unlink($data['physical_filename'], $data['thumbnail']);
				}
				$this->handler->close_channel();

				// delete attachments descriptions
				$sql = 'DELETE FROM ' . ATT_DESC_TABLE . '
							WHERE attach_id IN(' . implode(', ', $deleted_ids) . ')';
				$db->sql_query($sql, false, __LINE__, __FILE__);

				// get posts and private messages linked to these attachments
				$sql = 'SELECT post_id, privmsgs_id
							FROM ' . ATT_LINKS_TABLE . '
							WHERE attach_id IN(' . implode(', ', $deleted_ids) . ')';
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				while ( $row = $db->sql_fetchrow($result) )
				{
					if ( intval($row['post_id']) )
					{
						$post_ids[ intval($row['post_id']) ] = true;
					}
					if ( intval($row['privmsgs_id']) )
					{
						$privmsgs_ids[ intval($row['privmsgs_id']) ] = true;
					}
				}
				$db->sql_freeresult($result);

				// delete attachments links
				$sql = 'DELETE FROM ' . ATT_LINKS_TABLE . '
							WHERE attach_id IN(' . implode(', ', $deleted_ids) . ')';
				$db->sql_query($sql, false, __LINE__, __FILE__);

				// prepare return
				$return_msg = 'Attachments_deleted';
			}
			unset($deleted_ids);

			// get pms with remaining attachments and remove them from the pms list to reset
			if ( !empty($privmsgs_ids) )
			{
				$sql = 'SELECT privmsgs_id, COUNT(attach_id) AS count_attach_id
							FROM ' . ATT_LINKS_TABLE . '
							WHERE privmsgs_id IN(' . implode(', ', array_keys($privmsgs_ids)) . ')
							GROUP BY privmsgs_id';
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				while ( $row = $db->sql_fetchrow($result) )
				{
					unset($privmsgs_ids[ intval($row['privmsgs_id']) ]);
				}
				$db->sql_freeresult($result);
			}

			// reset pms flag
			if ( !empty($privmsgs_ids) )
			{
				$sql = 'UPDATE ' . PRIVMSGS_TABLE . '
							SET privmsgs_attachment = 0
							WHERE privmsgs_id IN(' . implode(', ', array_keys($privmsgs_ids)) . ')';
				$db->sql_query($sql, false, __LINE__, __FILE__);
			}
			unset($privmsgs_ids);

			// get post with remaining attachments and remove them from the posts list to reset
			if ( !empty($post_ids) )
			{
				$sql = 'SELECT post_id, COUNT(attach_id) AS count_attach_id
							FROM ' . ATT_LINKS_TABLE . '
							WHERE post_id IN(' . implode(', ', array_keys($post_ids)) . ')
							GROUP BY post_id';
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				while ( $row = $db->sql_fetchrow($result) )
				{
					unset($post_ids[ intval($row['post_id']) ]);
				}
				$db->sql_freeresult($result);
			}

			// reset posts flag
			if ( !empty($post_ids) )
			{
				$sql = 'UPDATE ' . POSTS_TABLE . '
							SET post_attachment = 0
							WHERE post_id IN(' . implode(', ', array_keys($post_ids)) . ')';
				$db->sql_query($sql, false, __LINE__, __FILE__);
			}

			// get topics with no remaining attachments
			if ( !empty($post_ids) )
			{
				$sql = 'SELECT topic_id, MAX(post_attachment) AS max_post_attachment
							FROM ' . POSTS_TABLE . '
							WHERE post_id IN(' . implode(', ', array_keys($post_ids)) . ')
							GROUP BY topic_id
							HAVING MAX(post_attachment) = 0';
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				while ( $row = $db->sql_fetchrow($result) )
				{
					$topic_ids[] = intval($row['topic_id']);
				}
				$db->sql_freeresult($result);
			}
			unset($post_ids);

			// reset topics flag
			if ( !empty($topic_ids) )
			{
				$sql = 'UPDATE ' . TOPICS_TABLE . '
							SET topic_attachment = 0
							WHERE topic_id IN(' . implode(', ', $topic_ids) . ')';
				$db->sql_query($sql, false, __LINE__, __FILE__);
			}
			unset($topic_ids);

			// return message
			if ( !empty($return_msg) )
			{
				message_return($return_msg, 'Click_return_attach_search', $config->url($this->requester, $this->get_link_parms(), true));
			}
		}
	}

	function get_link_parms()
	{
		global $dft_sort, $dft_order;

		$parms = $this->parms;
		if ( $this->fields['sort']->value != $dft_sort )
		{
			$parms['sort'] = $this->fields['sort']->value;
		}
		if ( $this->fields['order']->value != $dft_order )
		{
			$parms['order'] = $this->fields['order']->value;
		}

		if ( trim($this->fields['filename']->value) != '' )
		{
			$parms['filename'] = rawurlencode($this->fields['filename']->value);
		}
		if ( trim($this->fields['file_comment']->value) != '' )
		{
			$parms['file_comment'] = rawurlencode($this->fields['file_comment']->value);
		}
		if ( trim($this->fields['author']->value) != '' )
		{
			$parms['author'] = rawurlencode($this->fields['author']->value);
		}
		if ( intval($this->fields['sizemin']->value) )
		{
			$parms['sizemin'] = intval($this->fields['sizemin']->value);
		}
		if ( intval($this->fields['sizemax']->value) )
		{
			$parms['sizemax'] = intval($this->fields['sizemax']->value);
		}
		if ( intval($this->fields['countmin']->value) )
		{
			$parms['countmin'] = intval($this->fields['countmin']->value);
		}
		if ( intval($this->fields['countmax']->value) )
		{
			$parms['countmax'] = intval($this->fields['countmax']->value);
		}
		if ( intval($this->fields['topicdays']->value) )
		{
			$parms['topicdays'] = intval($this->fields['topicdays']->value);
		}
		if ( intval($this->fields[POST_FORUM_URL]->value) )
		{
			$parms[POST_FORUM_URL] = intval($this->fields[POST_FORUM_URL]->value);
		}
		return $parms;
	}

	function display()
	{
		global $db, $template, $user, $config;
		global $sort_fields, $order_fields;
		global $dft_sort, $dft_order;

		// display search form
		foreach ( $this->fields as $field_name => $field )
		{
			$this->fields[$field_name]->display();
		}
		display_buttons($this->buttons);
		$template->assign_vars(array('SEARCH_FORM' => $template->include_file('form_fields.tpl', array('field'))));

		// display details
		if ( !empty($this->data) )
		{
			$color = false;
			$i = 0;
			foreach ( $this->data as $attach_id => $row )
			{
				$i++;
				$color = !$color;
				$file_size = format_size(intval($row['filesize']));
				$template->assign_block_vars('row', array(
					'ROW_NUMBER' => $i,
					'ATTACH_ID' => $attach_id,
					'U_FILE_NAME' => $config->url('viewtopic', array('mode' => 'attach', 'id' => $attach_id), true),
					'FILE_NAME' => $row['real_filename'],
					'FILE_COMMENT' => empty($row['file_comment']) ? '' : str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $row['file_comment']),
					'EXTENSION' => $row['extension'],
					'FILE_SIZE' => $file_size['value'],
					'UNIT' => $user->lang($file_size['unit']),
					'DOWNLOAD_COUNT' => intval($row['download_count']),
					'ATTACH_COUNT' => intval($row['count_attach_id']),
					'TOPIC_TITLE' => $row['topic_title'],
					'U_VIEW_POST' => $config->url('viewtopic', array(POST_POST_URL => intval($row['max_post_id'])), true, intval($row['max_post_id'])),
					'FILE_TIME' => $user->date(intval($row['filetime'])),
					'U_USERNAME' => intval($row['max_user_id']) ? $config->url('usercp', array(POST_USERS_URL => intval($row['max_user_id'])), true) : '',
					'USERNAME' => !in_array(intval($row['max_user_id']), array(0, ANONYMOUS)) ? $row['max_username'] : $user->lang('Guest'),
				));
				$template->set_switch('row.light', $color);
				$template->set_switch('row.privmsgs', !intval($row['max_post_id']));
				if ( !intval($row['max_post_id']) )
				{
					$template->set_switch('row.privmsgs.received', intval($row['count_attach_id']));
				}
				$template->set_switch('row.user_link', intval($row['max_user_id']));
			}
		}

		// display header
		$template->assign_vars(array(
			'L_FILE_NAME' => $user->lang('File_name'),
			'L_FILE_SIZE' => $user->lang('Filesize'),
			'L_DOWNLOAD_COUNT' => $user->lang('Downloads'),
			'L_LOCATION' => $user->lang('Posted_in_topic'),
			'L_PRIVMSGS' => $user->lang('Private_Message'),
			'L_VIEW_POST' => $user->lang('View_topic'),

			'L_NO_MATCHES' => $user->lang('No_attach_search_match'),
		));
		$template->set_switch('details');
		$details_buttons = array(
			'edit_details' => array('txt' => 'Submit_changes', 'img' => 'cmd_edit', 'key' => 'cmd_edit'),
			'delete_details' => array('txt' => 'Delete_marked', 'img' => 'cmd_delete', 'key' => 'cmd_delete'),
		);
		display_buttons($details_buttons, 'details');

		$parms = $this->get_link_parms();
		$pagination = new pagination($this->requester, $parms);
		$pagination->display('pagination', $this->total, $this->ppage, $this->start, true, 'Attachments_count');
		unset($pagination);

		// send all to template
		$template->assign_block_vars('cp_content', array('BOX' => $template->include_file('acp/acp_attach_search_box.tpl')));
	}
}

// instantiate the display
$parms = array(
	'mode' => $menu_id,
	'sub' => $subm_id == $menu_id ? '' : $subm_id,
	'ctx' => $ctx_id == $subm_id ? '' : $ctx_id,
);
$form = new attach_search($cp_requester, $cp_parms + $parms, $fields);
$form->process();
$template->set_filenames(array('body' => 'cp_generic.tpl'));

?>