Your IP : 216.73.216.41


Current Path : /home/purehotels/public_html/administrator/components/com_easyfolderlistingpro/models/
Upload File :
Current File : /home/purehotels/public_html/administrator/components/com_easyfolderlistingpro/models/archive.php

<?php
/**
* @version		3.2
* @author		Michael A. Gilkes (michael@valorapps.com)
* @copyright	Michael Albert Gilkes
* @license		GNU/GPLv3

Easy Folder Listing Pro Component for Joomla!
Copyright (C) 2012-2016 Michael Albert Gilkes (Valor Apps)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

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

//load the JFile library
jimport('joomla.filesystem.file');


/**
 * EasyFolderListingPro Archive Model
 */
class EasyFolderListingProModelArchive extends JModelLegacy
{
	public function clean()
	{
		//Check for permission
		$user = JFactory::getUser();
		$permission = $user->authorise('core.delete', 'com_easyfolderlistingpro');
		if (!$permission)
		{
			$this->setError(JText::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'));
			return false;
		}
		
		//Step 1: Get records of all the archives
		$db	= $this->getDbo();
		$query = $db->getQuery(true);
		
		$query->select('`id`, `file_path`, `created_time`, `life_time`');
		$query->from('#__eflp_archives');
		
		$db->setQuery($query);
		$archives = $db->loadAssocList();
		
		// Check for a database error.
		if ($db->getErrorNum())
		{
			//JLog::add($db->getErrorMsg(), JLog::ERROR, 'com_easyfolderlistingpro');
			$this->setError($db->getErrorMsg());
			return false;
		}
		
		//Step 2: Find expired archives and delete the physical files if they exists
		$ids = $this->removeExpiredArchives($archives);
		
		//Step 3: Remove all the records of the expired Archives
		if (!empty($ids))
		{
			return $this->delete($ids);
		}
		else
		{
			$this->setError(JText::_('COM_EFLP_ARCHIVE_NONE'));
			return false;
		}
	}
	
	protected function removeExpiredArchives($archives)
	{
		//get the list from database trash table
		//$time = date('Y-m-d H:i:s', (time()-($archive_life*60)));
		
		//$trash_items = $this->archiveModel->getTrash($time);
		$deleted = array();
		
		$now = time();
		
		//delete each file if it exists
		foreach ($archives as $item)
		{
			$time = strtotime($item['created_time']) + intval($item['life_time'])*60;
			if ($now > $time)
			{
				//track the deleted record
				$deleted[] = $item['id'];
				
				//delete all files related to the record
				if (file_exists($item['file_path']))
				{
					JFile::delete($item['file_path']);
					
					//also, delete the intermediate tar file
					if (substr($item['file_path'], -3) == '.gz')
					{
						$tar = substr($item['file_path'], 0, -3);
						if (file_exists($tar))
						{
							JFile::delete($tar);
						}
					}
					else if (substr($item['file_path'], -4) == '.bz2')
					{
						$tar = substr($item['file_path'], 0, -4);
						if (file_exists($tar))
						{
							JFile::delete($tar);
						}
					}
				}
			}
		}
		
		return $deleted;
	}
	
	/**
	 * Method to delete one or more records.
	 *
	 * @param   array  $ids  An array of record primary keys.
	 *
	 * @return  boolean  True if successful, false if an error occurs.
	 */
	protected function delete($ids)
	{
		$ids = (array) $ids;
		
		$db	= $this->getDbo();
		$query = $db->getQuery(true);
		
		$query->delete('#__eflp_archives');
		$query->where('id IN ('.implode(',', $ids).')');
		
		$db->setQuery($query);
		
		$resource = false;
		$resource = $db->execute();
		if (!$resource)
		{
			$this->setError($db->getErrorMsg());
			return false;
		}
		
		return true;
	}
}