<?php
//
//	file: includes/class_attach_display.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_posting_display
{
	var $data;
	var $update_views;
	var $handler;

	function attach_posting_display()
	{
		global $attach_config;

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

	function is_auth($forum_id=0)
	{
		global $user;
		return $user->auth(POST_FORUM_URL, 'auth_mod', $forum_id) || ($user->auth(POST_FORUM_URL, 'auth_download', $forum_id) && $user->auth(POST_FORUM_URL, 'auth_read', $forum_id));
	}

	function read($switch_attachment, &$postrow, $forum_id=0)
	{
		global $db, $config, $user;
		global $attach_config, $attach_extensions;

		$this->data = array();
		$this->update_views = array();

		// no attached files, no posts, or attachmod inactive
		if ( !intval($switch_attachment) || empty($postrow) || intval($attach_config->data['disable_mod']) )
		{
			return;
		}

		// auth
		if ( !empty($forum_id) && !$this->is_auth($forum_id) )
		{
			return;
		}

		// get post ids
		$post_ids = array();
		if ( empty($forum_id) )
		{
			foreach ( $postrow as $dummy => $data )
			{
				if ( intval($data['post_attachment']) && $this->is_auth($data['forum_id']) )
				{
					$post_ids[] = (int) $data['post_id'];
				}
			}
		}
		else
		{
			$post_ids = array_keys($postrow);
		}
		$this->data = array();
		if ( !empty($post_ids) )
		{
			$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 IN(' . implode(', ', array_map('intval', $post_ids)) . ')
						ORDER BY a.post_id, d.filetime' . (intval($attach_config->data['display_order']) ? '' : ' DESC');
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$this->data[ intval($row['post_id']) ][ intval($row['attach_id']) ] = $row;
			}
			$db->sql_freeresult($result);
		}
		$attach_extensions->read();
		$this->display_main();
	}

	function display_main()
	{
		global $user, $config, $template;

		$template->assign_vars(array(
			'L_DESCRIPTION' => $user->lang('Description'),
			'L_DOWNLOAD' => $user->lang('Download'),
			'L_FILENAME' => $user->lang('File_name'),
			'L_FILESIZE' => $user->lang('Filesize'),
			'L_POSTED_ATTACHMENTS' => $user->lang('Posted_attachments'),
		));
	}

	function display($post_id=0, $tpl_switch='')
	{
		global $db, $config, $user, $template;
		global $attach_config, $attach_extensions;

		if ( empty($this->data[$post_id]) )
		{
			return;
		}
		foreach ( $this->data[$post_id] as $attach_id => $data )
		{
			// Admin is allowed to view forbidden Attachments, but the error-message is displayed too to inform the Admin
			$denied = false;
			if ( !isset($attach_extensions->data[ $data['extension'] ]) )
			{
				$denied = true;
				$template->assign_block_vars('attach', array(
					'L_DENIED' => sprintf($user->lang('Extension_disabled_after_posting'), $data['extension']),
				));
				$template->set_switch('attach.denyrow');
			}

			if ( !$denied || ($user->data['user_level'] == ADMIN) )
			{
				// Some basic things...
				$filename = basename($data['physical_filename']);
				$thumbnail_filename = THUMB_DIR . '/t_' . basename($data['physical_filename']);
				$display_name = basename($data['real_filename']);
				$file_comment = str_replace("\n", '<br />', $data['file_comment']);
				$download_mode = isset($attach_extensions->data[ $data['extension'] ]) ? intval($attach_extensions->data[ $data['extension'] ]['download_mode']) : INLINE_LINK;
				$i_upload = !empty($attach_extensions->data[ $data['extension'] ]['upload_icon']) ? $attach_extensions->data[ $data['extension'] ]['upload_icon'] : (!empty($attach_config->data['upload_img']) ? $attach_config->data['upload_img'] : false);
				$l_upload = !empty($attach_extensions->data[ $data['extension'] ]['ext_group_name']) ? $attach_extensions->data[ $data['extension'] ]['ext_group_name'] : $data['extension'];
				$u_upload = $this->handler->get_download_url($filename, false);

				$units = array();
				$unit = size_get_units(intval($data['filesize']), $units);
				$filesize = round(intval($data['filesize']) / $unit, 2);
				$size_lang = $units[$unit];

				// define category
				$display_cat = '';
				switch ( $attach_extensions->data[ $data['extension'] ]['cat_id'] )
				{
					case IMAGE_CAT:
						$display_cat = 'link';
						if ( intval($attach_config->data['img_display_inlined']) )
						{
							if ( !intval($attach_config->data['img_link_width']) && !intval($attach_config->data['img_link_height']) )
							{
								$display_cat = 'image';
							}
							else
							{
								$image = new image_standard($this->handler);
								list($width, $height) = $image->image_size($this->handler->path($filename));
								unset($image);
								if ( ($width <= intval($attach_config->data['img_link_width'])) && ($height <= intval($attach_config->data['img_link_height'])) )
								{
									$display_cat = 'image';
								}
							}
						}
						if ( $data['thumbnail'] == 1 )
						{
							$display_cat = 'thumbnail';
						}
						break;

					case STREAM_CAT:
						$display_cat = 'stream';
						break;

					case SWF_CAT:
						$display_cat = 'swf';
						break;

					default:
						$display_cat = 'link';
						break;
				}

				// let's go with display
				$tpl_vars = array(
					'FILESIZE' => $filesize,
					'SIZE_VAR' => $user->lang($size_lang),
					'COMMENT' => $file_comment,
					'DOWNLOAD_NAME' => $display_name,
					'I_UPLOAD' => $i_upload ? $user->img_styled($i_upload) : '',
					'L_UPLOAD' => $l_upload,
					'L_DOWNLOAD_COUNT' => sprintf($user->lang('Download_number'), $data['download_count']),
				);
				switch ( $display_cat )
				{
					case 'image':
						if ( ($mode == PHYSICAL_LINK) && !$u_download )
						{
							$download_mode = INLINE_LINK;
						}
						$download_link = ($download_mode != PHYSICAL_LINK);

						// send to template
						$tpl_vars += array(
							'L_DOWNLOADED_VIEWED' => $user->lang('Viewed'),
							'IMG_SRC' => $download_link ? $config->url('viewtopic', array('mode' => 'attach', 'id' => $attach_id), true) : $u_download,
						);

						// Directly Viewed Image ... update the download count
						if ( !$download_link )
						{
							$this->update_views[] = intval($attach_id);
						}
						break;

					case 'thumbnail':
						if ( ($mode == PHYSICAL_LINK) && !($thumbnail_filename = $this->handler->get_download_url($thumbnail_filename, false)) )
						{
							$download_mode = INLINE_LINK;
						}
						$download_link = ($download_mode != PHYSICAL_LINK);

						// send to template
						$tpl_vars += array(
							'L_DOWNLOADED_VIEWED' => $user->lang('Viewed'),
							'IMG_SRC' =>  $config->url('viewtopic', array('mode' => 'attach', 'id' => $attach_id), true),
							'IMG_THUMB_SRC' => $download_link ? $config->url('viewtopic', array('mode' => 'attach', 'id' => $attach_id, 'thumb' => 1), true) : $thumbnail_filename,
						);
						break;

					case 'stream':
						// Streams
						$tpl_vars += array(
							'L_DOWNLOADED_VIEWED' => $user->lang('Viewed'),
							'U_DOWNLOAD_LINK' => $u_download,
							'S_AUTOPLAY' => intval($attach_config->data['wma_autoplay']),
						);

						// Viewed/Heared File ... update the download count (download.php is not called here)
						$this->update_views[] = intval($attach_id);
						break;

					case 'swf':
						// Macromedia Flash Files
						$image = new image_flash($this->handler);
						list($width, $height) = $image->image_size($this->handler->path($filename));
						unset($image);
						$tpl_vars += array(
							'L_DOWNLOADED_VIEWED' => $user->lang('Viewed'),
							'U_DOWNLOAD_LINK' => $u_download,
							'WIDTH' => $width,
							'HEIGHT' => $height,
							'S_AUTOPLAY' => intval($attach_config->data['flash_autoplay']),
						);

						// Viewed/Heared File ... update the download count (download.php is not called here)
						$this->update_views[] = intval($attach_id);
						break;

					case 'link':
						// display attachment
						$tpl_vars += array(
							'L_DOWNLOADED_VIEWED' => $user->lang('Downloaded'),
							'U_DOWNLOAD_LINK' => $config->url('viewtopic', array('mode' => 'attach', 'id' => $attach_id), true),
							'TARGET_BLANK' => ' target="phpbbattach"',
						);
						break;
				}

				// send all to template
				$template->assign_block_vars('attach', $tpl_vars);
				$template->set_switch('attach.cat_' . $display_cat);
				$template->set_switch('attach.cat_' . $display_cat . '.upload');
				$template->set_switch('attach.cat_' . $display_cat . '.upload.img', $i_upload);
				$template->set_switch('attach.cat_' . $display_cat . '.target_blank', !empty($tpl_vars['TARGET_BLANK']));
				$template->set_switch('attach.cat_' . $display_cat . '.autoplay', intval($tpl_vars['S_AUTOPLAY']));
			}
		}
		if ( empty($tpl_switch) )
		{
			$template->assign_vars(array(
				'ATTACHMENTS' => $template->include_file('attach_display_box.tpl', array('attach')),
			));
		}
		else
		{
			$template->assign_lastblock_vars($tpl_switch, array(
				'ATTACHMENTS' => $template->include_file('attach_display_box.tpl', array('attach')),
			));
		}
	}

	function update_views()
	{
		global $db;
		if ( !empty($this->update_views) )
		{
			$sql = 'UPDATE ' . ATT_DESC_TABLE . '
						SET download_count = download_count + 1
						WHERE attach_id IN(' . implode(', ', $this->update_views) . ')';
			$db->sql_query($sql, false, __LINE__, __FILE__);
			return;
		}
	}
}

class attach_privmsg_display extends attach_posting_display
{
	function is_auth()
	{
		global $attach_config, $user;
		return ($user->data['user_level'] == ADMIN) || intval($attach_config->data['allow_pm_attach']);
	}

	function read($switch_attachment, &$privmsg)
	{
		global $db, $config, $user;
		global $attach_config, $attach_extensions;

		$this->data = array();
		$this->update_views = array();

		// no attached files, no pm, or attachmod inactive
		if ( !intval($switch_attachment) || empty($privmsg) || intval($attach_config->data['disable_mod']) )
		{
			return;
		}

		// auth
		if ( !$this->is_auth() )
		{
			return;
		}
		$this->data = array();
		if ( $privmsgs_id = intval($privmsg['privmsgs_id']) )
		{
			$sql = 'SELECT a.privmsgs_id, d.*
						FROM ' . ATT_LINKS_TABLE . ' a, ' . ATT_DESC_TABLE . ' d
						WHERE d.attach_id = a.attach_id
							AND a.post_id = 0
							AND a.privmsgs_id = ' . intval($privmsgs_id) . '
						ORDER BY d.filetime' . (intval($attach_config->data['display_order']) ? '' : ' DESC');
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$this->data[0][ intval($row['attach_id']) ] = $row;
			}
			$db->sql_freeresult($result);
		}

		$attach_extensions->read();
		$this->display_main();
	}

	function display_main()
	{
		global $user, $config, $template;

		parent::display_main();

		$count_data = count($this->data);
		$template->set_switch('switch_attachments', $count_data);
		if ( $count_data )
		{
			$template->assign_vars(array(
				'L_DELETE_ATTACHMENTS'	=> $user->lang($count_data == 1 ? 'Delete_attachment' : 'Delete_attachments'),
			));
		}
	}
}

?>