<?php
class PMAttachments_Postbit_PM extends vB_Postbit_Pm
{
	protected $attach;

	protected function replace_attachment_id($attach)
	{
		if (isset($this->post['attachmap'][$attach[2]]) AND isset($this->post['attachments'][$this->post['attachmap'][$attach[2]]]))
		{
			return '[ATTACH' . (($attach[1]) ? "=$attach[1]" : '') . ']' . $this->post['attachmap'][$attach[2]] . '[/ATTACH]';
		}
		else
		{
			return $attach[0];
		}
	}
	
	public function parse_bbcode()
	{
		require_once(DIR . '/includes/class_bbcode.php');
		$bbcode_parser = new vB_BbCodeParser($this->registry, fetch_tag_list());

		if ($this->post['attach'])
		{
			$this->load_attachments();
			$bbcode_parser->attachments =& $this->post['attachments'];
			$bbcode_parser->unsetattach = true;
			$this->post['message'] = preg_replace_callback('#\[attach(?:=(right|left|config))?\](\d+)\[/attach\]#i', array($this, 'replace_attachment_id'), $this->post['message']);
		}
		
		$this->post['message'] = $bbcode_parser->parse($this->post['message'], 'privatemessage', $this->post['allowsmilie']);
	}

	protected function load_attachments()
	{
		if (!isset($this->attach))
		{
			require_once(DIR . '/packages/vbattach/attach.php');
			$this->attach = new vB_Attach_Display_Content($this->registry, 'PMAttachments_PrivateMessage');
			$this->post['attachments'] = $this->attach->fetch_postattach(0, $this->post['pmid']);
			if (!empty($this->post['attachments']) AND isset($this->post['attachmap']) AND !is_array($this->post['attachmap']))
			{
				$this->post['attachmap'] = unserialize($this->post['attachmap']);
			}
		}
		
		return $this->attach;
	}
	
	public function process_attachments()
	{
		if ($this->post['attach'])
		{
			$this->load_attachments()->process_attachments(
				$this->post,
				$this->post['attachments'],
				false,
				false,
				$this->registry->userinfo['permissions']['advpmpermissions'] & $this->registry->bf_ugp_forumpermissions['cangetattach'],
				$this->registry->userinfo['permissions']['advpmpermissions'] & $this->registry->bf_ugp_forumpermissions['cangetattach']
			);
		}
	}
}
?>