33
2
mirror of https://github.com/joomla-extensions/jedchecker.git synced 2024-11-28 07:46:34 +00:00
jedchecker/administrator/components/com_jedchecker/controllers/uploads.php

248 lines
5.5 KiB
PHP
Raw Normal View History

<?php
/**
2019-03-09 19:44:14 +00:00
* @package Joomla.JEDChecker
*
2019-03-10 16:09:42 +00:00
* @copyright Copyright (C) 2017 - 2019 Open Source Matters, Inc. All rights reserved.
* Copyright (C) 2008 - 2016 compojoom.com . All rights reserved.
2019-03-10 08:49:52 +00:00
* @author Daniel Dimitrov <daniel@compojoom.com>
* 02.06.12
*
2019-03-09 19:44:14 +00:00
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die('Restricted access');
jimport('joomla.filesystem');
2012-12-13 10:08:35 +00:00
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.archive');
2019-03-09 19:44:14 +00:00
use Joomla\Archive\Archive;
2015-10-26 09:03:44 +00:00
/**
* Class JedcheckerControllerUploads
*
* @since 1.0
*/
class JedcheckerControllerUploads extends JControllerlegacy
{
2019-03-09 19:44:14 +00:00
var $path = null;
var $pathArchive = null;
var $pathUnzipped = null;
2015-10-26 09:03:44 +00:00
/**
* Constructor.
*
*/
public function __construct()
{
$this->path = JFactory::getConfig()->get('tmp_path') . '/jed_checker';
$this->pathArchive = $this->path . '/archives';
$this->pathUnzipped = $this->path . '/unzipped';
parent::__construct();
}
/**
* basic upload
*
* @return bool
*/
public function upload()
{
$appl = JFactory::getApplication();
$input = JFactory::getApplication()->input;
2019-03-09 19:44:14 +00:00
// Check the sent token by the form
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
// Gets the uploaded file from the sent form
$file = $input->files->get('extension', null, 'raw');
2015-10-26 09:03:44 +00:00
2019-03-09 19:44:14 +00:00
if ( $file['tmp_name'] )
2015-10-26 09:03:44 +00:00
{
$path = $this->pathArchive;
// If the archive folder doesn't exist - create it!
if (!JFolder::exists($path))
{
JFolder::create($path);
}
else
{
// Let us remove all previous uploads
$archiveFiles = JFolder::files($path);
foreach ($archiveFiles as $archive)
{
if (!JFile::delete($this->pathArchive . '/' . $archive))
{
echo 'could not delete' . $archive;
}
}
}
$filepath = $path . '/' . strtolower($file['name']);
$object_file = new JObject($file);
$object_file->filepath = $filepath;
$file = (array) $object_file;
// Let us try to upload
if (!JFile::upload($file['tmp_name'], $file['filepath'], false, true))
{
// Error in upload - redirect back with an error notice
2016-05-25 17:05:25 +00:00
JFactory::getApplication()->enqueueMessage(JText::_('COM_JEDCHECKER_ERROR_UNABLE_TO_UPLOAD_FILE'), 'error');
2019-03-09 19:44:14 +00:00
$appl->redirect('index.php?option=com_jedchecker&view=uploads');
2015-10-26 09:03:44 +00:00
return false;
}
2019-03-09 19:44:14 +00:00
// Unzip uploaded files
$unzip_result = $this->unzip();
$this->setRedirect( 'index.php?option=com_jedchecker&view=uploads' );
2015-10-26 09:03:44 +00:00
return true;
2019-03-09 19:44:14 +00:00
} else {
$this->setRedirect('index.php?option=com_jedchecker&view=uploads');
2015-10-26 09:03:44 +00:00
}
return false;
}
/**
* unzip the file
*
* @return bool
*/
public function unzip()
{
$appl = JFactory::getApplication();
2019-03-09 19:44:14 +00:00
// Form check token
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
2015-10-26 09:03:44 +00:00
// If folder doesn't exist - create it!
if (!JFolder::exists($this->pathUnzipped))
{
JFolder::create($this->pathUnzipped);
}
else
{
// Let us remove all previous unzipped files
$folders = JFolder::folders($this->pathUnzipped);
foreach ($folders as $folder)
{
JFolder::delete($this->pathUnzipped . '/' . $folder);
}
}
$file = JFolder::files($this->pathArchive);
2019-03-09 19:44:14 +00:00
$origin = $this->pathArchive . DIRECTORY_SEPARATOR . $file[0];
$destination = $this->pathUnzipped . DIRECTORY_SEPARATOR . $file[0];
try
{
$archive = new Archive;
$result = $archive->extract($origin, $destination);
}
catch (\Exception $e)
{
$result = false;
}
2015-10-26 09:03:44 +00:00
if ($result)
{
// Scan unzipped folders if we find zip file -> unzip them as well
$this->unzipAll($this->pathUnzipped . '/' . $file[0]);
$message = 'COM_JEDCHECKER_UNZIP_SUCCESS';
2019-03-09 19:44:14 +00:00
JFactory::getApplication()->enqueueMessage(JText::_($message));
2015-10-26 09:03:44 +00:00
}
else
{
$message = 'COM_JEDCHECKER_UNZIP_FAILED';
}
2019-03-09 19:44:14 +00:00
//$appl->redirect('index.php?option=com_jedchecker&view=uploads', JText::_($message));
$message = 'COM_JEDCHECKER_UNZIP_FAILED';
2015-10-26 09:03:44 +00:00
2019-03-09 19:44:14 +00:00
return $message;
2015-10-26 09:03:44 +00:00
}
/**
* Recursively go through each folder and extract the archives
*
* @param string $start - the directory where we start the unzipping from
2015-10-26 09:03:44 +00:00
*
* @return void
*/
public function unzipAll($start)
{
$iterator = new RecursiveDirectoryIterator($start);
foreach ($iterator as $file)
{
if ($file->isFile())
{
$extension = pathinfo($file->getFilename(), PATHINFO_EXTENSION);
if ($extension == 'zip')
{
$unzip = $file->getPath() . '/' . $file->getBasename('.' . $extension);
2019-03-09 19:44:14 +00:00
try
{
$archive = new Archive;
$result = $archive->extract($file->getPathname(), $unzip);
}
catch (\Exception $e)
{
$result = false;
}
2015-10-26 09:03:44 +00:00
// Delete the archive once we extract it
if ($result)
{
JFile::delete($file->getPathname());
// Now check the new extracted folder for archive files
$this->unzipAll($unzip);
}
}
}
elseif (!$iterator->isDot())
{
$this->unzipAll($file->getPathname());
}
}
}
2019-03-09 19:44:14 +00:00
/**
* clear tmp folders
*
*/
public function clear()
{
if ( file_exists($this->path) )
{
$result = JFolder::delete($this->path);
if (!$result)
{
echo 'could not delete ' . $this->path;
$message = 'COM_JEDCHECKER_DELETE_FAILED';
}
$message = 'COM_JEDCHECKER_DELETE_SUCCESS';
//JFactory::getApplication()->redirect('index.php?option=com_jedchecker&view=uploads', JText::_($message));
$this->setRedirect( 'index.php?option=com_jedchecker&view=uploads' );
}
}
}