<?php
/*======================================================================*\
   || #################################################################### ||
   || # vBulletin 4.0.0 Alpha 5
   || # ---------------------------------------------------------------- # ||
   || # Copyright ©2000-2009 Jelsoft Enterprises Ltd. All Rights Reserved. ||
   || # This file may not be redistributed in whole or significant part. # ||
   || # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
   || # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
   || #################################################################### ||
   \*======================================================================*/

if (!isset($GLOBALS['vbulletin']->db))
{
	exit;
}
require_once(DIR . '/includes/blog_functions.php');

/**
 * Class for displaying a vBulletin article attachment
 *
 * @package 		vBulletin
 * @version		$Revision: 32366 $
 * @date 		$Date: 2009-10-05 18:40:04 -0500 (Mon, 05 Oct 2009) $
 *
 */
class vB_Attachment_Display_Single_vBCMS_Article extends vB_Attachment_Display_Single
{
	/**
	 * Verify permissions of a single attachment
	 *
	 * @return	bool
	 */
	public function verify_attachment()
	{

		if (! isset($this->contentid))
		{
			vB::$vbulletin->input->clean_array_gpc('r', array(
				'f' => vB_Input::TYPE_UINT,
				'attachmentid' => vB_Input::TYPE_UINT
				));
		}

		if (vB::$vbulletin->GPC_exists['values'] and isset(vB::$vbulletin->GPC['values']['f']))
		{
			$this->contentid = vB::$vbulletin->GPC['values']['f'];
		}

		if (isset($this->contentid))
		{
			return vBCMS_Permissions::canView($this->contentid);
		}

		if (vB::$vbulletin->GPC_exists['attachmentid'] AND $record = vB::$vbulletin->db->query_first('SELECT contentid FROM ' .
			TABLE_PREFIX . "attachment WHERE attachmentid = " . vB::$vbulletin->GPC['attachmentid']))
		{
			$this->contentid = $record['contentid'];
			return vBCMS_Permissions::canView($this->contentid);
		}

	}

	public function __construct(&$registry, $attachmentid, $thumbnail, $attachmentid_2 = false)
	{
		parent::__construct($registry, $attachmentid, $thumbnail, $attachmentid_2 = false);
		$this->attachmentinfo = vB::$vbulletin->db->query_first("SELECT a.attachmentid, a.contenttypeid,
			a.contentid, a.userid, a.dateline, a.state, a.posthash, a.filename, a.caption,
			f.filedataid, f.filedata, f.filesize, f.filehash, f.thumbnail, f.extension,
			f.width, f.height, f.thumbnail_width, f.thumbnail_height
			FROM " . TABLE_PREFIX . "filedata f INNER JOIN " . TABLE_PREFIX . "attachment a ON a.filedataid = f.filedataid
			WHERE a.attachmentid = " . $this->attachmentid);
		 $this->attachmentinfo['mimetype'] = 'a:1:{i:0;s:23:"Content-type: image/' . $this->attachmentinfo['extension'] . '";}' ;

	}
}

/**
 * Class for display of multiple vBulletin blog entry attachments
 *
 * @package 		vBulletin
 * @version		$Revision: 32366 $
 * @date 		$Date: 2009-10-05 18:40:04 -0500 (Mon, 05 Oct 2009) $
 *
 */
class vB_Attachment_Display_Multiple_vBCMS_Article extends vB_Attachment_Display_Multiple
{
	/**
	 * Constructor
	 *
	 * @param	vB_Registry
	 * @param	integer			Unique id of this contenttype (forum post, blog entry, etc)
	 *
	 * @return	void
	 */
	public function __construct(&$registry, $contenttypeid)
	{
		parent::__construct($registry);
		$this->contenttypeid = $contenttypeid;
	}



	/**
	 * Return content specific information that relates to the ownership of attachments
	 *
	 * @param	array		List of attachmentids to query
	 *
	 * @return	void
	 */
	public function fetch_sql($attachmentids)
	{
		$selectsql = array(
			"node.nodeid, info.title, node.setpublish, node.publishdate, node.userid, node.permissionsfrom ",
			"perms.permissions"
		);

		$joinsql = array(
			"LEFT JOIN " . TABLE_PREFIX . "cms_node AS node ON (node.nodeid = a.contentid)",
			"LEFT JOIN " . TABLE_PREFIX. "user AS user ON (a.userid = user.userid)",
			"LEFT JOIN " . TABLE_PREFIX . "cms_nodeinfo AS info ON (info.nodeid = node.nodeid)",
			"LEFT JOIN " . TABLE_PREFIX . "usergroup AS usergroup ON (usergroup.usergroupid = user.usergroupid)",
			"LEFT JOIN " . TABLE_PREFIX . "cms_permissions AS perms ON (perms.usergroupid = usergroup.usergroupid AND perms.nodeid = node.permissionsfrom)",
	);


		return $this->fetch_sql_specific($attachmentids, $selectsql, $joinsql);
	}

	/**
	 * Fetches the SQL to be queried as part of a UNION ALL of an attachment query, verifying read permissions
	 * @param	string	SQL WHERE criteria
	 * @param	string	Contents of the SELECT portion of the main query
	 *
	 * @return	string
	 */
	protected function fetch_sql_ids($criteria, $selectfields)
	{
		$subwheresql = array(
			"a.contentid <> 0",
		);
		$joinsql = array(
			"LEFT JOIN " . TABLE_PREFIX . "cms_node AS node ON (a.contentid = node.nodeid)",
			"LEFT JOIN " . TABLE_PREFIX . "user AS user ON (user.userid = node.userid)",
		);




		return $this->fetch_sql_ids_specific($this->contenttypeid, $criteria, $selectfields, $subwheresql, $joinsql);
	}

	/**
	 * Formats $bloginfo content for display
	 *
	 * @param	array		Entry information
	 *
	 * @return	array
	 */
	public function process_attachment_template($article_info)
	{
		global $show, $vbphrase;
		return array();
	}

	/**
	 * Return item-specific url to the owner of an attachment
	 *
	 * @param	array		Content information
	 *
	 * @return	string
	 */
	protected function fetch_content_url_instance($contentinfo)
	{
		return "content.php?content/" . $this->values['nodeid'] .
			($this->values['url'] = '' ? '' : '-' . $this->values['url'])
			.'/view'	;
	}
}

// #######################################################################
// ############################# STORAGE #################################
// #######################################################################

/**
 * Class for storing an article attachment
 *
 * @package 		vBulletin
 * @version		$Revision: 32366 $
 * @date 		$Date: 2009-10-05 18:40:04 -0500 (Mon, 05 Oct 2009) $
 *
 */
class vB_Attachment_Store_vBCMS_Article extends vB_Attachment_Store
{
	/**
	 *	Bloginfo
	 *
	 * @var	array
	 */
	protected $bloginfo = array();
	protected $contentid = false;

	/**
	 * Verifies permissions to attach content to entries
	 *
	 * @return	boolean
	 */
	public function verify_permissions()
	{
		global $vbulletin;

		if (! isset($this->contentid) and !vB::$vbulletin->GPC_exists['values'])
		{
			vB::$vbulletin->input->clean_array_gpc('r', array(
				'f' => vB_Input::TYPE_UINT,
				'attachmentid' => vB_Input::TYPE_UINT
				));

		}

		if (vB::$vbulletin->GPC_exists['values'] and isset(vB::$vbulletin->GPC['values']['f']))
		{
			$this->contentid = vB::$vbulletin->GPC['values']['f'];
		}

		if (isset($this->contentid))
		{
			return vBCMS_Permissions::canView($this->contentid);
		}

		if (vB::$vbulletin->GPC_exists['attachmentid'] AND $record = vB::$vbulletin->db->query_first('SELECT contentid FROM ' .
			TABLE_PREFIX . "attachment WHERE attachmentid = " . vB::$vbulletin->GPC['attachmentid']))
		{
			$this->contentid = $record['contentid'];
			$canview =  vBCMS_Permissions::canView($this->contentid);
			return vBCMS_Permissions::canView($this->contentid);
		}

	}

	/**
	 * Verifies permissions to attach content to posts
	 *
	 * @param	object	vB_Upload
	 * @param	array		Information about uploaded attachment
	 *
	 * @return	integer
	 */
	protected function process_upload($upload, $attachment)
	{
		$attachmentid = parent::process_upload($upload, $attachment);

		if (!vB::$vbulletin->GPC['values']['f']) {
			vB::$vbulletin->input->clean_array_gpc('r', array(
				'values' => vB_Input::TYPE_ARRAY
				));

		}

		if ($attachmentid AND vB::$vbulletin->GPC_exists['values'] AND isset(vB::$vbulletin->GPC['values']['f']))
		{
			$this->contentid = vB::$vbulletin->GPC['values']['f'];
			$this->contentid = vB::$vbulletin->GPC['values']['f'];
			vB::$vbulletin->db->query_write("UPDATE " . TABLE_PREFIX . "attachment SET contentid = " . $this->contentid				. " WHERE attachmentid = $attachmentid" );
		}
		return $attachmentid;
	}
}

/**
 * Class for deleting a vBulletin blog entry attachment
 *
 * @package 		vBulletin
 * @version		$Revision: 32366 $
 * @date 		$Date: 2009-10-05 18:40:04 -0500 (Mon, 05 Oct 2009) $
 *
 */
class vB_Attachment_Dm_vBCMS_Article extends vB_Attachment_Dm
{
	/**
	 * pre_approve function - extend if the contenttype needs to do anything
	 *
	 * @param	array		list of moderated attachment ids to approve
	 * @param	boolean	verify permission to approve
	 *
	 * @return	boolean
	 */
	public function pre_approve($list, $checkperms = true)
	{
		if (! isset($this->contentid) and !vB::$vbulletin->GPC_exists['values'])
		{
			vB::$vbulletin->input->clean_array_gpc('r', array(
				'f' => vB_Input::TYPE_UINT
				));

		}

		if (vB::$vbulletin->GPC_exists['values'] and isset(vB::$vbulletin->GPC['values']['f']))
		{
			$this->contentid = vB::$vbulletin->GPC['values']['f'];
		}
		return vBCMS_Permissions::canEdit($this->contentid);
	}

	/**
	 * pre_delete function - extend if the contenttype needs to do anything
	 *
	 * @param	array		list of deleted attachment ids to delete
	 * @param	boolean	verify permission to delete
	 *
	 * @return	boolean
	 */
	public function pre_delete($list, $checkperms = true)
	{
		if (! isset($this->contentid) and !vB::$vbulletin->GPC_exists['values'])
		{
			vB::$vbulletin->input->clean_array_gpc('r', array(
				'f' => vB_Input::TYPE_UINT
				));

		}

		if (vB::$vbulletin->GPC_exists['values'] and isset(vB::$vbulletin->GPC['values']['f']))
		{
			$this->contentid = vB::$vbulletin->GPC['values']['f'];
		}
		return vBCMS_Permissions::canEdit($this->contentid);
	}

	/**
	 * post_delete function - extend if the contenttype needs to do anything
	 *
	 * @return	void
	 */
	public function post_delete()
	{
		return true;
	}
}

class vB_Attachment_Upload_Displaybit_vBCMS_Article extends vB_Attachment_Upload_Displaybit
{
	/**
	 *	Parses the appropiate template for contenttype that is to be updated on the calling window during an upload
	 *
	 * @param	array	Attachment information
	 * @param	array	Values array pertaining to contenttype
	 * @param	boolean	Disable template comments
	 *
	 * @return	string
	 */
	public function process_display_template($attach, $values = array(), $disablecomment = false)
	{
		$attach['extension'] = strtolower(file_extension($attach['filename']));
		$attach['filename']  = htmlspecialchars_uni($attach['filename']);
		$attach['filesize']  = vb_number_format($attach['filesize'], 1, true);
		$attach['imgpath']   = vB_Template_Runtime::fetchStyleVar('imgdir_attach') . "/$attach[filename].gif";

		$templater = vB_Template::create('vbcms_image_src');
		$templater->register('attachmentid', $attach['attachmentid']);
		$templater->register('contenttypeid', vB_Types::instance()->getContentTypeID("vBCms_Article"));
		$image_location = $templater->render(true);

		$templater = vB_Template::create('newpost_attachmentbit');
		$templater->register('attach', $attach);


		$result = "<li class=\"attachment\"><div><img src=\"$image_location\" alt=\"\" /></div></li>";
		return $result;
	}
}
/*======================================================================*\
   || ####################################################################
   
   || # CVS: $RCSfile$ - $Revision: 32366 $
   || ####################################################################
   \*======================================================================*/
?>