<?php
/**
 * Main Plugin File
 * Does all the magic!
 *
 * @package     Cache Cleaner
 * @version     1.3.0
 *
 * @author      Peter van Westen <peter@nonumber.nl>
 * @link        http://www.nonumber.nl
 * @copyright   Copyright (C) 2010 NoNumber! All Rights Reserved
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

// Import library dependencies
jimport( 'joomla.event.plugin' );

/**
* Plugin that cleans cache
*/
class plgSystemCacheCleaner extends JPlugin
{
	/**
	* Constructor
	*
	* For php4 compatability we must not use the __constructor as a constructor for
	* plugins because func_get_args ( void ) returns a copy of all passed arguments
	* NOT references. This causes problems with cross-referencing necessary for the
	* observer design pattern.
	*/
	function plgSystemCacheCleaner( &$subject, $config )
	{
		$mainframe =& JFactory::getApplication();

		// Load plugin parameters
		require_once JPATH_PLUGINS.DS.'system'.DS.'nonumberelements'.DS.'helpers'.DS.'parameters.php';
		$parameters =& NNePparameters::getParameters();
		$params = $parameters->getParams( $config['params'], JPATH_PLUGINS.DS.$config['type'].DS.$config['name'].'.xml' );
		
		$clean = 0;
		$show_msg = 0;

		if ( !$clean ) {
			$cleancache = JRequest::getVar( 'cleancache' );
			if ( $cleancache != '' ) {
				if ( $mainframe->isSite() && $cleancache != $params->frontend_secret ) {
					return;
				}
				$clean = 'clean';
				$show_msg = 1;
			}
		}

		if ( !$clean ) {
			$task = JRequest::getVar( 'task' );
			if ( $task == 'save' || $task == 'apply' ) {
				if ( $mainframe->isAdmin() && $params->auto_save_admin ) {
					$clean = 'save';
					$show_msg = $params->auto_save_admin_msg;
				} else if ( $mainframe->isSite() && $params->auto_save_front ) {
					$clean = 'save';
					$show_msg = $params->auto_save_front_msg;
				}
			}
		}

		if ( !$clean ) {
			if ( $mainframe->isAdmin() && $params->auto_interval_admin ) {
				$user =& JFactory::getUser();
				if ( $user->id ) {
					$clean = 'interval';
					$show_msg = $params->auto_interval_admin_msg;
					$secs = $params->auto_interval_admin_secs;
				}
			} else if ( $mainframe->isSite() && $params->auto_interval_front ) {
				$clean = 'interval';
				$show_msg = $params->auto_interval_front_msg;
				$secs = $params->auto_interval_front_secs;
			}
			if ( $clean ) {
				jimport( 'joomla.filesystem.file' );
				$file = JPATH_PLUGINS.DS.'system'.DS.'cachecleaner'.DS.'lastclean.txt';
				if ( JFile::exists( $file ) ) {
					$lastclean = JFile::read( $file );
					if ( $lastclean ) {
						$inttime =  ( time() - $secs );
						if ( $lastclean > $inttime ) {
							$clean = 0;
						}
					}
				}
			}
		}
		
		if ( !$clean ) {
			return;
		}
		
		parent::__construct( $subject, $config );

		//load the admin language file
		$this->loadLanguage( 'plg_'.$config['type'].'_'.$config['name'], JPATH_ADMINISTRATOR );

		// Include the Helper
		require_once JPATH_PLUGINS.DS.$config['type'].DS.$config['name'].DS.'helper.php';
		$class = get_class( $this ).'Helper';
		$this->helper = new $class ( $params, $clean, $show_msg );
	}
}