<?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 - 2015 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * cfBackendFingerprintAction Class
 *
 * The action class to create and test system fingerprints.
 *
 * @package clearFusionCMS
 */
class cfBackendFingerprintAction extends cfBackendControllerAction {
	/**
	 * Process the page.
	 */
	function process() {
		$this->testPermission('core.fingerprint');
		$this->breadcrumbTrail->addCrumb(__('Fingerprint', 'cfBackend'), $this->assemble('', 'fingerprint'));

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

		// If creating fingerprint
		if(($params = $this->request->getParam('*', array())) && count($params) == 1 && $params[0] == 'create') {

			$integrity = new flIntegrity();
			$integrity->build(FLROOTPATH, array('system/cache/', $cfg->publicFiles, $cfg->privateFiles, $cfg->publicStorage, $cfg->privateStorage));
			$fingerprint = gzencode(serialize($integrity->getFileHash()));

			$this->response->noCache()
				->compressOutput(false)
				->setContentType(flMimeTypes::getType('txt'))
				->setHeader('Content-Disposition', 'attachment; filename=system.fingerprint.gz')
				->setBody($fingerprint)
				->sendResponse();

			die;
		}

		$msg = '';

		$form = new flForm($this->request, 'themeform', $this->assemble('', 'fingerprint'), true);

		$pane = new flFormElementPane($form, 'info', $this->alertMessage('Info', sprintf(__('Use the link below to download a fingerprint file for the system, you can upload this file at any point to detect changes in the installation. This can allow you to detect unauthorized changes to your system if you test and regenerate the fingerprint before and after each upgrade.<p><a href="%s">Download Fingerprint File</a></p><p><strong>Note:</strong> When prompted you need to save the file in a secure location.</p>', 'cfBackend'), $this->assemble('', 'fingerprint', array('*' => 'create')))));

		$file = new flFormElementFile($form, 'fingerprint');
		$file->setLabel(__('Fingerprint File From Your Computer', 'cfBackend'))->setCSS('primary');

		$submit = new flFormElementSubmit($form, 'upload');
		$submit->setCaption(__('Upload and Test', 'cfBackend'))->setCSS('primary');

		$form->setGroupLabel(null, __('Upload and Test Fingerprint File', 'cfBackend'));
		$form->focusElement($file);

		$form->groupElements('leftcolfp', array($pane))
			->groupElements('rightcolfp', array($file, $submit));

		if($form->isSubmitted() && $form->isValid()) {

			$integrity = new flIntegrity();
			$integrity->build(FLROOTPATH, array('system/cache/', $cfg->publicFiles, $cfg->privateFiles, $cfg->publicStorage, $cfg->privateStorage));

			ob_start();
			@readgzfile($file->getTmpFileName());
			$fingerprint = @unserialize(ob_get_clean());

			if($fingerprint === false) {
				$msg = $this->alertMessage('Error', __('The supplied file is not a valid fingerprint file please select a different file and try again.', 'cfBackend'));
			}
			else {
				$diff = $integrity->getChanges($fingerprint);
				if(count($diff))
					$msg = $this->alertMessage('Warning', __('The following changes to files were detected:', 'cfBackend') . $integrity->printChanges($diff));
				else
					$msg = $this->alertMessage('Done', __('There were no changes to files detected.', 'cfBackend'));
			}
		}

		// Draw the page
		$this->renderPage('System Fingerprint', $this->renderBlock(__('Create or Test a Fingerprint', 'cfBackend'),
			$form->render($this->tpl->getCodeBuffer()), '', $msg));
	}
}
