Your IP : 216.73.216.41


Current Path : /home/purehotels/public_html/components/com_easyfolderlistingpro/helpers/
Upload File :
Current File : /home/purehotels/public_html/components/com_easyfolderlistingpro/helpers/MultiDownload.Class.php

<?php
/**
 * @description   This class allows you to archive a set of files, and store the 
 *                archive as a zip, tar.gz, or tar.bz2. It uses ZipArchive and 
 *                PharData to perform this function. The resulting archive is 
 *                saved to a specified location on the server. 
 * @author        Michael Gilkes (Valor Apps)
 * @created       Friday, April 27, 2012
 * @modified      Monday, March 27, 2014
 * @version       1.1
 * @copyright     Copyright (C) 2012-2016 Michael Albert Gilkes. All rights reserved.
 * @license       GNU General Public License version 3 or later; see LICENSE
 */

defined('JPATH_PLATFORM') or die;



class MultiDownload extends JObject
{
	protected $prefix = "";
	protected $source_path;
	protected $source_files = array();
	
	protected $archive_path;
	
	//error codes key array is preset to AFL translation keys for backward compatibility
	protected $error_codes = array(
		'C0' => 'COM_ADVLISTING_ARCHIVE_CREATION_FAILED_ERROR',
		'O1' => 'COM_ADVLISTING_PHAR_CANT_OPEN_ERROR',
		'O2' => 'COM_ADVLISTING_ZIP_CANT_OPEN_ERROR',
		'F0' => 'COM_ADVLISTING_NO_VALID_FILES_ERROR',
		'F1' => 'COM_ADVLISTING_NO_BZIP_ERROR',
		'F2' => 'COM_ADVLISTING_NO_ZLIB_ERROR'
	);
	
	
	public function __construct($properties = null)
	{
		$this->prefix = 'afl';
		$this->archive_path = $this->tempPath();
		parent::__construct($properties);
	}
	
	public function setErrorCode($trans_key, $code = null)
	{
		if (is_array($trans_key))
		{
			$this->error_codes = array_merge($this->error_codes, $trans_key);
		}
		else
		{
			if (!empty($code) && array_key_exists($code, $this->error_codes))
			{
				$this->error_codes[$code] = $trans_key;
			}
		}
	}
	
	public static function canCreateArchive($archiveType)
	{
		if (empty($archiveType)) { return false; }
		
		$can = false;
		
		$ext = strtolower($archiveType);
		if ($ext == 'zip')
		{
			$can = class_exists('ZipArchive');
		}
		else
		{
			$tar = class_exists('PharData');;
			if ($tar && ($ext == 'tar.bz2' || $ext == 'bz2'))
			{
				$can = function_exists('bzcompress');
			}
			else if ($tar && ($ext == 'tar.gz' || $ext == 'gz'))
			{
				$can = function_exists('gzencode');
			}
		}
		
		return $can;
	}
	
	public function createArchive($type, $customName = null)
	{
		$success = false;
		
		$unique = $this->uniqueName($this->archive_path, $type);
		
		if ($type == 'bz2' || $type == 'gz')
		{
			$success = $this->create_tar($this->source_files, $this->archive_path.DIRECTORY_SEPARATOR.$unique, $type);
			$unique.= '.'.$type;
		}
		else //zip
		{
			$success = $this->create_zip($this->source_files, $this->archive_path.DIRECTORY_SEPARATOR.$unique);
		}
		
		if ($success == true)
		{
			$success = $unique;
		}
		
		return $success;
	}
	
	protected function tempPath()
	{
		$config = JFactory::getConfig();
		$tmpdir = $config->get('tmp_path');
		
		return $tmpdir;
	}
	
	public function getArchivePath()
	{
		return $this->archive_path;
	}
	
	protected function uniqueName($path, $type = 'zip')
	{
		$unique = $this->prefix.'';
		$ext = '.tar';
		$count = 0;
		do
		{
			/* we are trying to get a unique name within 9 times */
			if (($count+1)%3 == 0)
			{
				//reset unique after every 3 tries
				$unique = $this->prefix.'';
			}
			
			$unique.= time();
			
			if ($type == 'bz2' || $type == 'gz')
			{
				$ext.= '.'.$type;
			}
			else //zip
			{
				$ext = '.zip';
			}
			$count++;
		}
		/* only repeat if file already exists. */
		while (file_exists($path.DIRECTORY_SEPARATOR.$unique.$ext) && $count < 9);
		
		if ($type == 'bz2' || $type == 'gz')
		{
			$unique.= '.tar'; //only add .tar to the name
		}
		else //zip
		{
			$unique.= '.zip';
		}
		
		return $unique;
	}
	
	protected function create_tar($files, $zipfile_path, $type = 'gz')
	{
		$can = false;
		$valid_files = array();
		
		if(is_array($files))
		{
			foreach($files as $file)
			{
				if(file_exists($this->source_path.DIRECTORY_SEPARATOR.$file))
				{
					$valid_files[] = $file;
				}
			}
		}
		
		if(count($valid_files) > 0)
		{
			if (substr($zipfile_path, -4) != '.tar')
			{
				$zipfile_path.= '.tar';
			}
			
			try
			{
				$p = new PharData($zipfile_path);
				
				foreach ($valid_files as $file)
				{
					$p->addFile($this->source_path.DIRECTORY_SEPARATOR.$file, $file);
				}
				
				if ($type == 'bz2')
				{
					$p2 = $p->compress(Phar::BZ2);
					$can = file_exists($zipfile_path.'.bz2');
				}
				else //$type == 'gz'
				{
					$p1 = $p->compress(Phar::GZ);
					$can = file_exists($zipfile_path.'.gz');
				}
				
				if ($can == false)
				{
					//Tar archive creation failed. Code C0.
					$this->setError(JText::_($this->error_codes['C0']));
				}
			}
			catch (UnexpectedValueException $e)
			{
				//Phar archive can't be opened. Code O1.
				$this->setError(JText::_($this->error_codes['O1']));
				$this->setError($e->getMessage());
			}
			catch (BadMethodCallException $e)
			{
				if ($type == 'bz2')
				{
					//The PHP bzip2 extension is not enabled. Code F1
					$this->setError(JText::_($this->error_codes['F1']));
				}
				else
				{
					//The PHP zlib extension is not available. Code F2.
					$this->setError(JText::_($this->error_codes['F2']));
				}
				$this->setError($e->getMessage());
			}
			catch (Exception $e)
			{
				//$e->getMessage();
				$this->setError($e->getMessage());
			}
		}
		else
		{
			//No Valid Files Error. Code F0
			$this->setError(JText::_($this->error_codes['F0']));
		}
		
		return $can;
	}
	
	protected function create_zip($files, $zipfile_path)
	{
		$can = false;
		$valid_files = array();
		
		if(is_array($files))
		{
			foreach($files as $file)
			{
				if(file_exists($this->source_path.DIRECTORY_SEPARATOR.$file))
				{
					$valid_files[] = $file;
				}
			}
		}
		
		if(count($valid_files) > 0)
		{
			//create the archive
			$zip = new ZipArchive();
			
			if($zip->open($zipfile_path, ZIPARCHIVE::CREATE) !== true)
			{
				//Could not open the zip. Code O2.
				$this->setError(JText::_($this->error_codes['O2']));
			}
			else
			{
				//add the files
				foreach($valid_files as $file)
				{
					$zip->addFile($this->source_path.DIRECTORY_SEPARATOR.$file, $file);
				}
				
				//debug: error_log('The zip archive contains '.$zip->numFiles.' files with a status of '.$zip->status);
				
				//close the zip
				$zip->close();
				
				//check to make sure the file exists
				$can = file_exists($zipfile_path);
				
				//check the results
				if ($can == false)
				{
					//Zip archive creation failed. Code C0.
					$this->setError(JText::_($this->error_codes['C0']));
				}
			}
		}
		else
		{
			//No valid files found. Code F0.
			$this->setError(JText::_($this->error_codes['F0']));
			return false;
		}
		
		return $can;
	}
}