<?php
//
//	file: includes/class_attach_handler.php
//	author: ptirhiik
//	begin: 05/03/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('Hacking attempt');
	exit;
}

class attach_handler extends file_handler
{
	function attach_handler($page)
	{
		global $attach_config;

		$this->page = $page;
		parent::file_handler($attach_config->data);
	}

	function remove($_ids, $user_ids='')
	{
		global $db;

		$_ids = empty($_ids) ? array() : (!is_array($_ids) ? array(intval($_ids)) : $_ids);
		if ( empty($_ids) )
		{
			return false;
		}
		$user_ids = empty($user_ids) ? array() : (!is_array($user_ids) ? array(intval($user_ids)) : $user_ids);

		// get attach_id to delete
		$attach_ids = array();

		switch ( $this->page )
		{
			// we delete topics, but we have a list of posts
			case PAGE_INDEX:
				$sql = 'SELECT DISTINCT attach_id
							FROM ' . ATT_LINKS_TABLE . '
							WHERE post_id IN(' . implode(', ', $_ids) . ')
								AND post_id <> 0';
				unset($_ids);
				break;

			// we delete users pms, but we have a list of pms
			case PAGE_PROFILE:
			case PAGE_PRIVMSGS:
				$sql = 'SELECT DISTINCT attach_id
							FROM ' . ATT_LINKS_TABLE . '
							WHERE privmsgs_id IN(' . implode(', ', $_ids) . ')
								AND privmsgs_id <> 0';
				break;

			default:
				return false;
				break;
		}
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		while ( $row = $db->sql_fetchrow($result) )
		{
			$attach_ids[] = intval($row['attach_id']);
		}
		$db->sql_freeresult($result);
		if ( empty($attach_ids) )
		{
			return false;
		}

		// delete files
		$sql = 'SELECT physical_filename, thumbnail
					FROM ' . ATT_DESC_TABLE . '
					WHERE attach_id IN(' . implode(', ', $attach_ids) . ')';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		if ( $db->sql_numrows($result) )
		{
			$this->open_channel();
			while ( $row = $db->sql_fetchrow($result) )
			{
				$this->unlink($row['physical_filename'], $row['thumbnail']);
			}
			$this->close_channel();
		}
		$db->sql_freeresult($result);

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

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

		// reclaim some memory
		unset($attach_ids);

		// users prune
		if ( $this->page == PAGE_PROFILE )
		{
			// re-affect user deleted attachments to ANONYMOUS (posts attachments)
			$sql = 'UPDATE ' . ATT_LINKS_TABLE . '
						SET user_id = ' . ANONYMOUS . '
						WHERE user_id IN(' . implode(', ', $user_ids) . ')';
			$db->sql_query($sql, false, __LINE__, __FILE__);

			// delete users quotas
			$sql = 'DELETE FROM ' . ATT_QUOTA_TABLE . '
						WHERE group_id IN(' . $db->sql_subquery('group_id', '
							SELECT DISTINCT group_id
								FROM ' . GROUPS_TABLE . '
								WHERE group_user_id IN(' . implode(', ', $user_ids) . ')
							', __LINE__, __FILE__) . ')';
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}
		return true;
	}

	function sync($_ids)
	{
		global $db;

		$_ids = empty($_ids) ? array() : (!is_array($_ids) ? array(intval($_ids)) : $_ids);
		if ( empty($_ids) )
		{
			return false;
		}

		switch ( $this->page )
		{
			// $_ids are topic_ids
			case PAGE_INDEX:
				// get post ids & topic ids with attachments
				$sql = 'SELECT p.topic_id, p.post_id
							FROM ' . POSTS_TABLE . ' p, ' . ATT_LINKS_TABLE . ' a
							WHERE p.topic_id IN(' . implode(', ', $_ids) . ')
								AND a.post_id = p.post_id
								AND a.privmsgs_id = 0
							GROUP BY p.topic_id, p.post_id';
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				$post_ids = array();
				$topic_ids = array();
				while ( $row = $db->sql_fetchrow($result) )
				{
					$post_ids[] = intval($row['post_id']);
					$topic_ids[ intval($row['post_id']) ] = true;
				}
				$db->sql_freeresult($result);
				$topic_ids = empty($topic_ids) ? array() : array_keys($topic_ids);

				$sql = 'UPDATE ' . POSTS_TABLE . '
							SET post_attachment = 0
							WHERE topic_id IN(' . implode(', ', $_ids) . ')
								AND post_attachment = 1' . (empty($post_ids) ? '' : '
								AND post_id NOT IN(' . implode(', ', $post_ids) . ')');
				$db->sql_query($sql, false, __LINE__, __FILE__);

				if ( !empty($post_ids) )
				{
					$sql = 'UPDATE ' . POSTS_TABLE . '
								SET post_attachment = 1
								WHERE post_id IN(' . implode(', ', $post_ids) . ')
									AND post_attachment = 0';
					$db->sql_query($sql, false, __LINE__, __FILE__);
				}
				unset($post_ids);

				// sync topic
				$sql = 'UPDATE ' . TOPICS_TABLE . '
							SET topic_attachment = 0
							WHERE topic_id IN(' . implode(', ', $_ids) . ')
								AND topic_attachment = 1' . (empty($topic_ids) ? '' : '
								AND topic_id NOT IN(' . implode(', ', $topic_ids) . ')');
				$db->sql_query($sql, false, __LINE__, __FILE__);
				if ( !empty($topic_ids) )
				{
					$sql = 'UPDATE ' . TOPICS_TABLE . '
								SET topic_attachment = 1
								WHERE topic_id IN(' . implode(', ', $topic_ids) . ')
									AND topic_attachment = 0';
					$db->sql_query($sql, false, __LINE__, __FILE__);
				}
				unset($topic_ids);
				break;

			default:
				return false;
				break;
		}
		return true;
	}
}

?>