<?php
/**
 * Plugin Helper File
 *
 * @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' );

/**
* Plugin that cleans cache
*/
class plgSystemCacheCleanerHelper
{
	function plgSystemCacheCleanerHelper( &$params, $type = 'clean', $show_msg = 1 )
	{
		$final_state = 1;

		$cache =& JFactory::getCache();

		// remove all folders in cache folder
		$cache->clean();
		$cache->gc();

		// remove all remaining folders and files in cache folder
		jimport('joomla.filesystem.folder');
		jimport('joomla.filesystem.file');

		$paths = array( JPATH_SITE, JPATH_ADMINISTRATOR );
		foreach ( $paths as $path ) {
			$path .= DS.'cache';
			$this->emptyFolder( $path );
		}

		// Empty cmslib cache
		if ( $params->clean_cmslib ) {
			$path = JPATH_SITE.DS.'components'.DS.'libraries'.DS.'cmslib'.DS.'cache';
			$this->emptyFolder( $path );
		}


		// Empty JRE cache db table
		if ( $params->clean_jre ) {
			$db =& JFactory::getDBO();
			$db->setQuery( 'show tables like \''.$db->_table_prefix.'jrecache_repository\'' );
			$exists = $db->loadResult();
			if ( $exists ) {
				$db->setQuery( 'TRUNCATE TABLE `#__jrecache_repository`' );
				$db->query();
			}
		}

		// Folders
		if (	$type == 'clean'
			||	( $type == 'interval' && $params->auto_interval_folders )
			||	( $type == 'save' && $params->auto_save_folders )
		) {
			// Empty tmp folder
			if ( $params->clean_tmp ) {
				$path = JPATH_SITE.DS.'tmp';
				$this->emptyFolder( $path );
			}
			// Empty custom folder
			if ( $params->clean_folders ) {
				$folders = explode( '\n', $params->clean_folders );
				foreach( $folders as $folder ) {
					if ( trim( $folder ) ) {
						$folder = str_replace( array( '\\', '/' ), DS, trim( $folder ) );
						$path = str_replace( DS.DS, DS, JPATH_SITE.DS.$folder );
						$this->emptyFolder( $path );
					}
				}
			}
		}

		// Write current time to text file
		$file = JPATH_PLUGINS.DS.'system'.DS.'cachecleaner'.DS.'lastclean.txt';
		JFile::write( $file, time() );

		// Load language for messaging
		$lang =& JFactory::getLanguage();
		$lang->load( 'mod_cachecleaner', JPATH_ADMINISTRATOR );
		$file = JPATH_ADMINISTRATOR.DS.'language'.DS.'en-GB'.DS.'en-GB.mod_cachecleaner.ini';
		$lang->_load( $file, 'mod_cachecleaner', 0 );

		$error = 0;
		if ( !$final_state ) {
			$msg = JText::_( 'Not all cache could be removed' );
			$error = 1;
		} else {
			$msg = JText::_( 'Cache cleaned' );
		}
		if( JRequest::getInt( 'break' ) ) {
			echo $msg;
			exit;
		} else if ( $show_msg ) {
			$mainframe =& JFactory::getApplication();
			$mainframe->enqueueMessage( $msg, ( $error ? 'error' : 'message' ) );
		}
	}

	function emptyFolder( $path )
	{
		jimport('joomla.filesystem.folder');
		jimport('joomla.filesystem.file');
		
		if ( JFolder::exists( $path ) ) {
			// remove folders
			$folders = JFolder::folders( $path );
			foreach ( $folders as $folder ) {
				$state = JFolder::delete( $path.DS.$folder );
				if ( !$state ) {
					$final_state = 0;
				}
			}
			// remove files
			$files = JFolder::files( $path );
			foreach ( $files as $file ) {
				if ( $file != 'index.html' ) {
					$state = JFile::delete( $path.DS.$file );
					if ( !$state ) {
						$final_state = 0;
					}
				}
			}
		}
	}
}