Joomla-Sermon-Distributor/admin/src/Model/Manual_updaterModel.php
Robot 60e9e3b32d
Release of v5.0.0-beta2
First release of Sermon Distributor towards Joomla 5. Add PHP check to install/update class. Add Database check and fix to install/update class.
2024-05-03 07:46:28 +02:00

291 lines
8.6 KiB
PHP

<?php
/*-------------------------------------------------------------------------------------------------------------| www.vdm.io |------/
____ ____ __ __ __
/\ _`\ /\ _`\ __ /\ \__ __/\ \ /\ \__
\ \,\L\_\ __ _ __ ___ ___ ___ ___ \ \ \/\ \/\_\ ____\ \ ,_\ _ __ /\_\ \ \____ __ __\ \ ,_\ ___ _ __
\/_\__ \ /'__`\/\`'__\/' __` __`\ / __`\ /' _ `\ \ \ \ \ \/\ \ /',__\\ \ \/ /\`'__\/\ \ \ '__`\/\ \/\ \\ \ \/ / __`\/\`'__\
/\ \L\ \/\ __/\ \ \/ /\ \/\ \/\ \/\ \L\ \/\ \/\ \ \ \ \_\ \ \ \/\__, `\\ \ \_\ \ \/ \ \ \ \ \L\ \ \ \_\ \\ \ \_/\ \L\ \ \ \/
\ `\____\ \____\\ \_\ \ \_\ \_\ \_\ \____/\ \_\ \_\ \ \____/\ \_\/\____/ \ \__\\ \_\ \ \_\ \_,__/\ \____/ \ \__\ \____/\ \_\
\/_____/\/____/ \/_/ \/_/\/_/\/_/\/___/ \/_/\/_/ \/___/ \/_/\/___/ \/__/ \/_/ \/_/\/___/ \/___/ \/__/\/___/ \/_/
/------------------------------------------------------------------------------------------------------------------------------------/
@version 5.0.x
@created 22nd October, 2015
@package Sermon Distributor
@subpackage Manual_updaterModel.php
@author Llewellyn van der Merwe <https://www.vdm.io/>
@copyright Copyright (C) 2015. All Rights Reserved
@license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
A sermon distributor that links to Dropbox.
/----------------------------------------------------------------------------------------------------------------------------------*/
namespace TrueChristianChurch\Component\Sermondistributor\Administrator\Model;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Router\Route;
use Joomla\CMS\User\User;
use Joomla\Utilities\ArrayHelper;
use Joomla\Input\Input;
use TrueChristianChurch\Component\Sermondistributor\Administrator\Helper\SermondistributorHelper;
use TrueChristianChurch\Joomla\FOF\Encrypt\AES;
use TrueChristianChurch\Joomla\Utilities\ArrayHelper as UtilitiesArrayHelper;
use TrueChristianChurch\Joomla\Utilities\JsonHelper;
// No direct access to this file
\defined('_JEXEC') or die;
/**
* Sermondistributor List Model for Manual_updater
*
* @since 1.6
*/
class Manual_updaterModel extends ListModel
{
/**
* Represents the current user object.
*
* @var User The user object representing the current user.
* @since 3.2.0
*/
protected User $user;
/**
* The unique identifier of the current user.
*
* @var int|null The ID of the current user.
* @since 3.2.0
*/
protected ?int $userId;
/**
* Flag indicating whether the current user is a guest.
*
* @var int 1 if the user is a guest, 0 otherwise.
* @since 3.2.0
*/
protected int $guest;
/**
* An array of groups that the current user belongs to.
*
* @var array|null An array of user group IDs.
* @since 3.2.0
*/
protected ?array $groups;
/**
* An array of view access levels for the current user.
*
* @var array|null An array of access level IDs.
* @since 3.2.0
*/
protected ?array $levels;
/**
* The application object.
*
* @var CMSApplicationInterface The application instance.
* @since 3.2.0
*/
protected CMSApplicationInterface $app;
/**
* The input object, providing access to the request data.
*
* @var Input The input object.
* @since 3.2.0
*/
protected Input $input;
/**
* The styles array.
*
* @var array
* @since 4.3
*/
protected array $styles = [
'administrator/components/com_sermondistributor/assets/css/admin.css',
'administrator/components/com_sermondistributor/assets/css/manual_updater.css'
];
/**
* The scripts array.
*
* @var array
* @since 4.3
*/
protected array $scripts = [
'administrator/components/com_sermondistributor/assets/js/admin.js'
];
/**
* A custom property for UIKit components. (not used unless you load v2)
*/
protected $uikitComp;
/**
* Constructor
*
* @param array $config An array of configuration options (name, state, dbo, table_path, ignore_request).
* @param ?MVCFactoryInterface $factory The factory.
*
* @since 1.6
* @throws \Exception
*/
public function __construct($config = [], MVCFactoryInterface $factory = null)
{
parent::__construct($config, $factory);
$this->app ??= Factory::getApplication();
$this->input ??= $this->app->getInput();
// Set the current user for authorisation checks (for those calling this model directly)
$this->user ??= $this->getCurrentUser();
$this->userId = $this->user->get('id');
$this->guest = $this->user->get('guest');
$this->groups = $this->user->get('groups');
$this->authorisedGroups = $this->user->getAuthorisedGroups();
$this->levels = $this->user->getAuthorisedViewLevels();
// will be removed
$this->initSet = true;
}
/**
* Method to build an SQL query to load the list data.
*
* @return string An SQL query
* @since 1.6
*/
protected function getListQuery()
{
// Make sure all records load, since no pagination allowed.
$this->setState('list.limit', 0);
// Get a db connection.
$db = $this->getDatabase();
// Create a new query object.
$query = $db->getQuery(true);
// Get from #__sermondistributor_external_source as a
$query->select($db->quoteName(
array('a.id','a.description','a.externalsources','a.build','a.update_method','a.update_timer','a.filetypes','a.oauthtoken','a.permissiontype','a.dropboxoptions','a.sharedurl','a.folder'),
array('id','description','externalsources','build','update_method','update_timer','filetypes','oauthtoken','permissiontype','dropboxoptions','sharedurl','folder')));
$query->from($db->quoteName('#__sermondistributor_external_source', 'a'));
// Get where a.update_method is 1
$query->where('a.update_method = 1');
// Get where a.published is 1
$query->where('a.published = 1');
$query->order('a.description ASC');
// return the query object
return $query;
}
/**
* Method to get an array of data items.
*
* @return mixed An array of data items on success, false on failure.
* @since 1.6
*/
public function getItems()
{
$user = $this->user;
// check if this user has permission to access items
if (!$user->authorise('manual_updater.access', 'com_sermondistributor'))
{
$this->app->enqueueMessage(Text::_('Not authorised!'), 'error');
// redirect away if not a correct to default view
$this->app->redirect('index.php?option=com_sermondistributor');
return false;
}
// load parent items
$items = parent::getItems();
// Get the global params
$globalParams = ComponentHelper::getParams('com_sermondistributor', true);
// Get the basic encryption.
$basickey = SermondistributorHelper::getCryptKey('basic');
// Get the encryption object.
$basic = new AES($basickey);
// Insure all item fields are adapted where needed.
if (UtilitiesArrayHelper::check($items))
{
foreach ($items as $nr => &$item)
{
// Always create a slug for sef URL's
$item->slug = ($item->id ?? '0') . (isset($item->alias) ? ':' . $item->alias : '');
// Check if we can decode filetypes
if (isset($item->filetypes) && JsonHelper::check($item->filetypes))
{
// Decode filetypes
$item->filetypes = json_decode($item->filetypes, true);
}
// Check if we can decode oauthtoken
if (!empty($item->oauthtoken) && $basickey && !is_numeric($item->oauthtoken) && $item->oauthtoken === base64_encode(base64_decode($item->oauthtoken, true)))
{
// Decode oauthtoken
$item->oauthtoken = rtrim($basic->decryptString($item->oauthtoken), "\0");
}
}
}
// return items
return $items;
}
/**
* Method to get the styles that have to be included on the view
*
* @return array styles files
* @since 4.3
*/
public function getStyles(): array
{
return $this->styles;
}
/**
* Method to set the styles that have to be included on the view
*
* @return void
* @since 4.3
*/
public function setStyles(string $path): void
{
$this->styles[] = $path;
}
/**
* Method to get the script that have to be included on the view
*
* @return array script files
* @since 4.3
*/
public function getScripts(): array
{
return $this->scripts;
}
/**
* Method to set the script that have to be included on the view
*
* @return void
* @since 4.3
*/
public function setScript(string $path): void
{
$this->scripts[] = $path;
}
}