<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS
 * @copyright Copyright (c) 2012 - 2013 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * cfBackendImagepreviewAction Class
 *
 * The action class to access a private file for use as a preview.
 *
 * @package clearFusionCMS
 */
class cfBackendImagepreviewAction extends cfBackendControllerAction {

	/**
	 * Process the page.
	 */
	function process() {
		$this->testPermission('cfContent.private');

		$cfg = clearFusionCMS::getSiteConfig('cfSystemFileStore');

		$file = $cfg->privateFiles . '/' . join('/', $this->request->getParam('*', array())) . ($this->request->getParam('extension', '') == '' ? '' : '.' . $this->request->getParam('extension'));

		if(!is_readable($file) || !preg_match('/\.(jpg|png|gif)$/i', $file))
			throw new flExceptionNotFound();

		// Send the file out
		$this->response->setContentType(flMimeTypes::getType(strtolower(basename($file))))
			->compressOutput(false)
			->noCache()
			->setHeader('Content-Disposition', 'inline; filename=' . basename($file))
			->setHeader('Content-Length', filesize($file))
			->sendHeaders();

			readfile($file);
	}
}
