<?php
//
//	file: includes/class_attach_posting.php
//	author: ptirhiik
//	begin: 05/03/2006
//	version: 1.6.1 - 24/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 !');
}

class attach_posting extends attach_parent
{
	var $forum_id;
	var $topic_id;
	var $post_id;

	function attach_posting()
	{
		parent::attach_parent(PAGE_POSTING);
	}

	function init($forum_id, $topic_id)
	{
		$this->forum_id = intval($forum_id);
		$this->topic_id = intval($topic_id);
		return parent::init();
	}

	// @private
	function get_auth()
	{
		global $user, $config, $forums;

		$auth_names = array('auth_edit', 'auth_delete', 'auth_mod', 'auth_attachments');
		$count_auth_names = count($auth_names);
		$this->is_auth = array();
		for ( $i = 0; $i < $count_auth_names; $i++ )
		{
			$this->is_auth[ $auth_names[$i] ] = $user->auth(POST_FORUM_URL, $auth_names[$i], $this->forum_id);
		}
	}

	// @private
	function get_attachments($post_id)
	{
		global $db;

		$sql = 'SELECT a.post_id, d.*
					FROM ' . ATT_LINKS_TABLE . ' a, ' . ATT_DESC_TABLE . ' d
					WHERE d.attach_id = a.attach_id
						AND a.privmsgs_id = 0
						AND a.post_id = ' . intval($post_id) . '
					ORDER BY d.filetime DESC';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$attachments = array();
		while ( $row = $db->sql_fetchrow($result) )
		{
			$attachments[ intval($row['post_id']) ][ intval($row['attach_id']) ] = $row;
		}
		$db->sql_freeresult($result);
		return $attachments;
	}

	// @private
	function get_rules_id()
	{
		return $this->forum_id;
	}

	// @private
	function check_extension_auth($auth_cache)
	{
		global $attach_extensions;
		return $attach_extensions->auth($auth_cache, $this->forum_id);
	}

	// @private
	function check_user_quota()
	{
		global $db, $attach_config, $error, $error_msg;
		global $config, $user;

		$this->get_quota_limits($user->data, PAGE_INDEX);
		if ( intval($attach_config->data['upload_filesize_limit']) )
		{
			$sql = 'SELECT SUM(d.filesize) AS sum_filesize
						FROM ' . ATT_DESC_TABLE . ' d, ' . ATT_LINKS_TABLE . ' a
						WHERE d.attach_id = a.attach_id
							AND a.user_id = ' . intval($user->data['user_id']) . '
							AND a.privmsgs_id = 0';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			$total_filesize = ($row = $db->sql_fetchrow($result)) ? intval($row['sum_filesize']) : 0;
			$db->sql_freeresult($result);

			if ( ($total_filesize + $this->filesize) > intval($attach_config->data['upload_filesize_limit']) )
			{
				$units = array();
				$unit = size_get_units(intval($attach_config->data['upload_filesize_limit']), $units);
				$allowed_filesize = round(intval($attach_config->data['upload_filesize_limit']) / $unit, 2);
				$size_lang = $units[$unit];
				_error(sprintf($user->lang('User_upload_quota_reached'), $allowed_filesize, $size_lang));
			}
		}
	}

	function get_mode($mode)
	{
		if ( ($mode == 'quote') || ($mode == 'reply') )
		{
			$mode = 'reply';
			$this->post_id = 0;
		}
		if ( $mode == 'newtopic' )
		{
			$this->post_id = 0;
		}
		return $mode;
	}

	function validate($mode, $topic_id, $post_id, $user_id, $post_attachment, $delete_topic)
	{
		$this->user_id = intval($user_id);
		$this->post_id = intval($post_id);
		$this->topic_id = intval($topic_id);
		switch ( $mode )
		{
			case 'editpost':
			case 'newtopic':
			case 'reply':
				if ( $this->is_active )
				{
					$link = array(
						'post_id' => (int) $post_id,
						'privmsgs_id' => 0,
						'user_id' => (int) $user_id,
					);
					if ( $this->submit_all($link) )
					{
						$this->resync($post_id);
					}
				}
				break;

			case 'delete':
				if ( $post_id && $post_attachment )
				{
					if ( $this->delete_all($post_id) && !$delete_topic )
					{
						$this->resync();
					}
				}
				break;
		}
	}

	function delete_all($post_id)
	{
		global $db;

		$found = false;
		if ( !intval($post_id) )
		{
			return;
		}

		// read all physical name
		$sql = 'SELECT d.attach_id, d.physical_filename, d.thumbnail
					FROM ' . ATT_DESC_TABLE . ' d, ' . ATT_LINKS_TABLE . ' a
					WHERE d.attach_id = a.attach_id
						AND a.post_id = ' . intval($post_id);
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$files = array();
		while ( $row = $db->sql_fetchrow($result) )
		{
			$files[ intval($row['attach_id']) ] = array('physical_filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail']);
		}
		$db->sql_freeresult($result);

		if ( !empty($files) )
		{
			// delete the links to the posts
			$sql = 'DELETE FROM ' . ATT_LINKS_TABLE . '
						WHERE post_id = ' . intval($post_id);
			$db->sql_query($sql, false, __LINE__, __FILE__);

			// is the file shared ? if so, don't delete it
			$sql = 'SELECT DISTINCT attach_id
						FROM ' . ATT_LINKS_TABLE . '
						WHERE attach_id IN(' . implode(', ', array_keys($files)) . ')';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				unset($files[ intval($row['attach_id']) ]);
			}
			$db->sql_freeresult($result);
		}

		// delete the files on the ftp
		if ( !empty($files) )
		{
			$this->handler->open_channel();
			foreach ($files as $attach_id => $file )
			{
				$this->handler->unlink($file['physical_filename'], $file['thumbnail']);
			}
			$this->handler->close_channel();
			$attach_ids = array_keys($files);
			unset($files);

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

			$found = true;
		}
		return $found;
	}

	function delete_one($post_id, $attach_id)
	{
		global $db;

		if ( empty($this->data) || !isset($this->data[ intval($attach_id) ]) )
		{
			return;
		}

		// remove the link to the post if exists
		if ( intval($post_id) )
		{
			$sql = 'DELETE FROM ' . ATT_LINKS_TABLE . '
						WHERE post_id = ' . intval($post_id) . '
							AND attach_id = ' . intval($attach_id);
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}

		// is the file shared ?
		$found = false;
		if ( !$this->data[ intval($attach_id) ]['pending'] )
		{
			$sql = 'SELECT attach_id
						FROM ' . ATT_LINKS_TABLE . '
						WHERE attach_id = ' . intval($attach_id) . '
						LIMIT 1';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			$found = $db->sql_numrows($result);
			$db->sql_freeresult($result);
		}

		// remove the file if not shared
		if ( !$found )
		{
			$this->handler->open_channel();
			$this->handler->unlink($this->data[ intval($attach_id) ]['physical_filename'], $this->data[ intval($attach_id) ]['thumbnail']);
			$this->handler->close_channel();

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

		// adjust the post if exists
		if ( intval($post_id) && !$this->data[ intval($attach_id) ]['pending'] )
		{
			$sav_data = $this->data;
			unset($sav_data[ intval($attach_id) ]);
			$no_more = empty($sav_data);
			unset($sav_data);

			if ( $no_more && intval($post_id) )
			{
				$sav_data = $this->data;
				$this->data = array();
				$this->resync($post_id);
				$this->data = $sav_data;
			}
		}
	}

	function resync($post_id=0)
	{
		global $db;

		$found = empty($this->data) ? 0 : 1;
		if ( intval($post_id) )
		{
			$sql = 'UPDATE ' . POSTS_TABLE . '
						SET post_attachment = ' . intval($found) . '
						WHERE post_id = ' . intval($post_id);
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}
		if ( intval($this->topic_id) )
		{
			$sql = 'SELECT post_attachment
						FROM ' . POSTS_TABLE . '
						WHERE topic_id = ' . intval($this->topic_id) . '
						ORDER BY post_attachment DESC
						LIMIT 1';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			$topic_attachment = ($row = $db->sql_fetchrow($result)) ? intval($row['post_attachment']) : 0;
			$db->sql_freeresult($result);

			$sql = 'UPDATE ' . TOPICS_TABLE . '
						SET topic_attachment = ' . intval($topic_attachment) . '
						WHERE topic_id = ' . intval($this->topic_id);
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}
	}

	function preview($mode, $post_id)
	{
		global $db, $config, $user, $template;
		global $attach_config, $attach_extensions;

		if ( !$this->is_active )
		{
			return;
		}
		if ( !$this->processed )
		{
			$this->process($mode, $post_id, true);
		}
		if ( !empty($this->data) )
		{
			$preview = new attach_posting_display();
			$preview->data[0] = &$this->data;
			$preview->display_main();
			$attach_extensions->read();
			$preview->display(0, 'preview');
			unset($preview);
		}
	}
}

?>