2012-06-02 21:18:29 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2019-03-09 19:44:14 +00:00
|
|
|
* @package Joomla.JEDChecker
|
2012-06-02 21:18:29 +00:00
|
|
|
*
|
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
|
2012-06-02 21:18:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
defined('_JEXEC') or die('Restricted access');
|
|
|
|
|
2019-03-09 19:44:14 +00:00
|
|
|
use Joomla\Archive\Archive;
|
2023-08-13 09:22:27 +00:00
|
|
|
use Joomla\CMS\Factory;
|
|
|
|
use Joomla\CMS\Filesystem\File;
|
|
|
|
use Joomla\CMS\Filesystem\Folder;
|
|
|
|
use Joomla\CMS\Language\Text;
|
|
|
|
use Joomla\CMS\MVC\Controller\BaseController;
|
|
|
|
use Joomla\CMS\Session\Session;
|
2019-03-09 19:44:14 +00:00
|
|
|
|
2015-10-26 09:03:44 +00:00
|
|
|
/**
|
|
|
|
* Class JedcheckerControllerUploads
|
|
|
|
*
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
2023-08-13 09:22:27 +00:00
|
|
|
class JedcheckerControllerUploads extends BaseController
|
2012-06-02 21:18:29 +00:00
|
|
|
{
|
2021-04-04 10:25:35 +00:00
|
|
|
/** @var string */
|
2021-01-24 17:43:08 +00:00
|
|
|
public $path;
|
2021-04-04 10:25:35 +00:00
|
|
|
|
|
|
|
/** @var string */
|
2021-01-24 17:43:08 +00:00
|
|
|
public $pathArchive;
|
2021-04-04 10:25:35 +00:00
|
|
|
|
|
|
|
/** @var string */
|
2021-01-24 17:43:08 +00:00
|
|
|
public $pathUnzipped;
|
2019-03-09 19:44:14 +00:00
|
|
|
|
2015-10-26 09:03:44 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2023-08-13 09:22:27 +00:00
|
|
|
$this->path = Factory::getConfig()->get('tmp_path') . '/jed_checker';
|
2015-10-26 09:03:44 +00:00
|
|
|
$this->pathArchive = $this->path . '/archives';
|
|
|
|
$this->pathUnzipped = $this->path . '/unzipped';
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* basic upload
|
|
|
|
*
|
2021-04-04 10:25:35 +00:00
|
|
|
* @return boolean
|
2015-10-26 09:03:44 +00:00
|
|
|
*/
|
|
|
|
public function upload()
|
|
|
|
{
|
2023-08-13 09:22:27 +00:00
|
|
|
$appl = Factory::getApplication();
|
2021-05-10 17:23:29 +00:00
|
|
|
$input = $appl->input;
|
2019-03-09 19:44:14 +00:00
|
|
|
|
2017-01-13 10:27:07 +00:00
|
|
|
// Check the sent token by the form
|
2023-08-13 09:22:27 +00:00
|
|
|
Session::checkToken() or jexit(Text::_('JINVALID_TOKEN'));
|
2017-01-01 20:35:32 +00:00
|
|
|
|
|
|
|
// Gets the uploaded file from the sent form
|
|
|
|
$file = $input->files->get('extension', null, 'raw');
|
2015-10-26 09:03:44 +00:00
|
|
|
|
2021-04-04 10:25:35 +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!
|
2023-08-13 09:22:27 +00:00
|
|
|
if (!Folder::exists($path))
|
2015-10-26 09:03:44 +00:00
|
|
|
{
|
2023-08-13 09:22:27 +00:00
|
|
|
Folder::create($path);
|
2015-10-26 09:03:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Let us remove all previous uploads
|
2023-08-13 09:22:27 +00:00
|
|
|
$archiveFiles = Folder::files($path);
|
2015-10-26 09:03:44 +00:00
|
|
|
|
|
|
|
foreach ($archiveFiles as $archive)
|
|
|
|
{
|
2023-08-13 09:22:27 +00:00
|
|
|
if (!File::delete($this->pathArchive . '/' . $archive))
|
2015-10-26 09:03:44 +00:00
|
|
|
{
|
|
|
|
echo 'could not delete' . $archive;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 16:56:48 +00:00
|
|
|
$file['filepath'] = $path . '/' . strtolower($file['name']);
|
2015-10-26 09:03:44 +00:00
|
|
|
|
|
|
|
// Let us try to upload
|
2023-08-13 09:22:27 +00:00
|
|
|
if (!File::upload($file['tmp_name'], $file['filepath'], false, true))
|
2015-10-26 09:03:44 +00:00
|
|
|
{
|
2017-01-01 20:35:32 +00:00
|
|
|
// Error in upload - redirect back with an error notice
|
2023-08-13 09:22:27 +00:00
|
|
|
$appl->enqueueMessage(Text::_('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();
|
|
|
|
|
2021-04-04 10:25:35 +00:00
|
|
|
$this->setRedirect('index.php?option=com_jedchecker&view=uploads');
|
2015-10-26 09:03:44 +00:00
|
|
|
|
|
|
|
return true;
|
2021-04-04 10:25:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-09 19:44:14 +00:00
|
|
|
$this->setRedirect('index.php?option=com_jedchecker&view=uploads');
|
2015-10-26 09:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* unzip the file
|
|
|
|
*
|
2021-04-04 10:25:35 +00:00
|
|
|
* @return boolean
|
2015-10-26 09:03:44 +00:00
|
|
|
*/
|
|
|
|
public function unzip()
|
|
|
|
{
|
2023-08-13 09:22:27 +00:00
|
|
|
$appl = Factory::getApplication();
|
2019-03-09 19:44:14 +00:00
|
|
|
|
2017-01-13 10:43:36 +00:00
|
|
|
// Form check token
|
2023-08-13 09:22:27 +00:00
|
|
|
Session::checkToken() or jexit(Text::_('JINVALID_TOKEN'));
|
2015-10-26 09:03:44 +00:00
|
|
|
|
|
|
|
// If folder doesn't exist - create it!
|
2023-08-13 09:22:27 +00:00
|
|
|
if (!Folder::exists($this->pathUnzipped))
|
2015-10-26 09:03:44 +00:00
|
|
|
{
|
2023-08-13 09:22:27 +00:00
|
|
|
Folder::create($this->pathUnzipped);
|
2015-10-26 09:03:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Let us remove all previous unzipped files
|
2023-08-13 09:22:27 +00:00
|
|
|
$folders = Folder::folders($this->pathUnzipped);
|
2015-10-26 09:03:44 +00:00
|
|
|
|
|
|
|
foreach ($folders as $folder)
|
|
|
|
{
|
2023-08-13 09:22:27 +00:00
|
|
|
Folder::delete($this->pathUnzipped . '/' . $folder);
|
2015-10-26 09:03:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-13 09:22:27 +00:00
|
|
|
$file = Folder::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);
|
|
|
|
}
|
2023-08-13 09:22:27 +00:00
|
|
|
catch (Exception $e)
|
2019-03-09 19:44:14 +00:00
|
|
|
{
|
|
|
|
$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';
|
2023-08-13 09:22:27 +00:00
|
|
|
$appl->enqueueMessage(Text::_($message));
|
2015-10-26 09:03:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$message = 'COM_JEDCHECKER_UNZIP_FAILED';
|
|
|
|
}
|
|
|
|
|
2023-08-13 09:22:27 +00:00
|
|
|
// $appl->redirect('index.php?option=com_jedchecker&view=uploads', Text::_($message));
|
2019-03-09 19:44:14 +00:00
|
|
|
$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
|
|
|
|
*
|
2017-01-01 20:35:32 +00:00
|
|
|
* @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())
|
|
|
|
{
|
2021-11-16 16:56:11 +00:00
|
|
|
if (preg_match('/\.(?:zip|tar|tgz|tbz2|tar\.(?:gz|gzip|bz2|bzip2))$/', $file->getFilename(), $matches))
|
2015-10-26 09:03:44 +00:00
|
|
|
{
|
2021-11-16 16:56:11 +00:00
|
|
|
$unzip = $file->getPath() . '/' . $file->getBasename($matches[0]);
|
2021-04-04 10:25:35 +00:00
|
|
|
|
2019-03-09 19:44:14 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$archive = new Archive;
|
|
|
|
$result = $archive->extract($file->getPathname(), $unzip);
|
|
|
|
}
|
2023-08-13 09:22:27 +00:00
|
|
|
catch (Exception $e)
|
2019-03-09 19:44:14 +00:00
|
|
|
{
|
|
|
|
$result = false;
|
|
|
|
}
|
2015-10-26 09:03:44 +00:00
|
|
|
|
|
|
|
// Delete the archive once we extract it
|
|
|
|
if ($result)
|
|
|
|
{
|
2023-08-13 09:22:27 +00:00
|
|
|
File::delete($file->getPathname());
|
2015-10-26 09:03:44 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
*
|
2021-04-04 10:25:35 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2019-03-09 19:44:14 +00:00
|
|
|
public function clear()
|
|
|
|
{
|
2021-04-04 10:25:35 +00:00
|
|
|
if (file_exists($this->path))
|
2019-03-09 19:44:14 +00:00
|
|
|
{
|
2023-08-13 09:22:27 +00:00
|
|
|
$result = Folder::delete($this->path);
|
2019-03-09 19:44:14 +00:00
|
|
|
|
|
|
|
if (!$result)
|
|
|
|
{
|
|
|
|
echo 'could not delete ' . $this->path;
|
|
|
|
$message = 'COM_JEDCHECKER_DELETE_FAILED';
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = 'COM_JEDCHECKER_DELETE_SUCCESS';
|
|
|
|
|
2023-08-13 09:22:27 +00:00
|
|
|
// Factory::getApplication()->redirect('index.php?option=com_jedchecker&view=uploads', Text::_($message));
|
2021-04-04 10:25:35 +00:00
|
|
|
$this->setRedirect('index.php?option=com_jedchecker&view=uploads');
|
2019-03-09 19:44:14 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-02 21:18:29 +00:00
|
|
|
}
|