Your IP : 216.73.216.41


Current Path : /home/purehotels/public_html/components/com_easyfolderlistingpro/models/
Upload File :
Current File : /home/purehotels/public_html/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');


class EasyFolderListingProModelArchive extends JModelItem
{
	/**
	 * Check the registed archives and delete the expired archives from the server file system
	 * and the database.
	 */
	public function cleanup()
	{
		$expired = array();
		$now = time();
		$all_archives = $this->getTempArchives();
		
		foreach ($all_archives as $i => $archive)
		{
			$expire = strtotime($archive['created_time']) + intval($archive['life_time']) * 60;
			if ($now > $expire)
			{
				//add the id to the expired list
				$expired[] = $archive['id'];
				
				//delete all files related to the record
				if (file_exists($archive['file_path']))
				{
					JFile::delete($archive['file_path']);
					
					//also, delete the intermediate tar file
					if (substr($archive['file_path'], -3) == '.gz')
					{
						$tar = substr($archive['file_path'], 0, -3);
						if (file_exists($tar))
						{
							JFile::delete(substr($archive['file_path'], 0, -3));
						}
					}
					else if (substr($archive['file_path'], -4) == '.bz2')
					{
						$tar = substr($archive['file_path'], 0, -4);
						if (file_exists($tar))
						{
							JFile::delete(substr($archive['file_path'], 0, -4));
						}
					}
				}
			}
		}
		
		return $this->unregisterExpiredArchives($expired);
	}
	
	/**
	 * Get the list of the archives registered in the database.
	 */
	public function getTempArchives()
	{
		$db = JFactory::getDbo();
		
		$query = "SELECT ";
		$query.= "id, file_path, created_time, life_time ";
		$query.= "FROM #__eflp_archives;";
		
		$db->setQuery($query);
		$trash = $db->loadAssocList();
		
		// Check for a database error.
		if ($db->getErrorNum())
		{
			$this->setError($db->getErrorMsg());
		}
		
		return $trash;
	}
	
	/**
	 * Removes the registered archives in the database
	 */
	public function unregisterExpiredArchives($ids)
	{
		if (empty($ids))
		{
			//leave since there is nothing to delete
			return true;
		}
		$db = JFactory::getDbo();
		
		$query = 'DELETE FROM #__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;
	}
	
	/**
	 * Register newly created archives
	 */
	public function registerNewArchive($file_path, $life_time)
	{
		$db = JFactory::getDbo();
		
		$query = "INSERT INTO #__eflp_archives (".$db->quoteName('file_path').", ".$db->quoteName('created_time').", ".$db->quoteName('life_time').") ";
		$query.= "VALUES (".$db->quote($file_path).", NOW(), ".$db->quote($life_time)." )";
		
		//also, register the intermediate tar file
		if (substr($file_path, -3) == '.gz')
		{
			$tar = substr($file_path, 0, -3);
			if (file_exists($tar))
			{
				$query.= ", (".$db->quote($tar).", NOW(), ".$db->quote($life_time)." )";
			}
		}
		else if (substr($file_path, -4) == '.bz2')
		{
			$tar = substr($file_path, 0, -4);
			if (file_exists($tar))
			{
				$query.= ", (".$db->quote($tar).", NOW(), ".$db->quote($life_time)." )";
			}
		}
		
		$db->setQuery($query);
		$resource = false;
		$resource = $db->execute();
		if (!$resource)
		{
			$this->setError($db->getErrorMsg());
			return false;
		}
		return true;
	}
	
	/**
	 * Logs each file in the archive as a download hit
	 */
	public function logDownloadHits($filepaths, $user_id, $ip)
	{
		if (empty($filepaths))
		{
			//leave since there is nothing to delete
			return true;
		}
		$db = JFactory::getDbo();
		
		$query = "INSERT INTO #__eflp_logs (`filepath`, `user_id_fk`, `dl_or_pv`, `ip_address`, `access_time`) VALUES";
		for ($i=0; $i < count($filepaths); $i++)
		{
			if (!empty($filepaths[$i]))
			{
				if ($i > 0)
				{
					$query.= ",";
				}
				$query.= " (".$db->quote($filepaths[$i]).", ".$user_id.", 0, ".$db->quote($ip).", NOW() )";
			}
		}
		$db->setQuery($query);
		$resource = false;
		$resource = $db->execute();
		if (!$resource)
		{
			$this->setError($db->getErrorMsg());
			return false;
		}
		return true;
	}
}