@copyright Copyright (C) 2021. All Rights Reserved. @license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html ____ _____ _____ __ __ __ __ ___ _____ __ __ ____ _____ _ _ ____ _ _ ____ (_ _)( _ )( _ )( \/ )( ) /__\ / __)( _ )( \/ )( _ \( _ )( \( )( ___)( \( )(_ _) .-_)( )(_)( )(_)( ) ( )(__ /(__)\ ( (__ )(_)( ) ( )___/ )(_)( ) ( )__) ) ( )( \____) (_____)(_____)(_/\/\_)(____)(__)(__) \___)(_____)(_/\/\_)(__) (_____)(_)\_)(____)(_)\_) (__) /------------------------------------------------------------------------------------------------------*/ // No direct access to this file defined('_JEXEC') or die('Restricted access'); use Joomla\CMS\MVC\Model\ItemModel; use Joomla\Utilities\ArrayHelper; /** * Extensiondistributor Maintenance Item Model */ class ExtensiondistributorModelMaintenance extends ItemModel { /** * Model context string. * * @var string */ protected $_context = 'com_extensiondistributor.maintenance'; /** * Model user data. * * @var strings */ protected $user; protected $userId; protected $guest; protected $groups; protected $levels; protected $app; protected $input; protected $uikitComp; /** * @var object item */ protected $item; /** * Method to auto-populate the model state. * * Note. Calling getState in this method will result in recursion. * * @since 1.6 * * @return void */ protected function populateState() { $this->app = JFactory::getApplication(); $this->input = $this->app->input; // Get the item main id $id = $this->input->getInt('id', null); $this->setState('maintenance.id', $id); // Load the parameters. parent::populateState(); } /** * Method to get article data. * * @param integer $pk The id of the article. * * @return mixed Menu item data object on success, false on failure. */ public function getItem($pk = null) { $this->user = JFactory::getUser(); // check if this user has permission to access item if (!$this->user->authorise('maintenance.access', 'com_extensiondistributor')) { $app = JFactory::getApplication(); $app->enqueueMessage(JText::_('Not authorised!'), 'error'); // redirect away if not a correct to cPanel/default view $app->redirect('index.php?option=com_extensiondistributor'); return false; } $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(); $this->initSet = true; $pk = (!empty($pk)) ? $pk : (int) $this->getState('maintenance.id'); if ($this->_item === null) { $this->_item = array(); } if (!isset($this->_item[$pk])) { try { // Get a db connection. $db = JFactory::getDbo(); // Create a new query object. $query = $db->getQuery(true); // Get data return false; // Reset the query using our newly populated query object. $db->setQuery($query); // Load the results as a stdClass object. $data = $db->loadObject(); if (empty($data)) { $app = JFactory::getApplication(); // If no data is found redirect to default page and show warning. $app->enqueueMessage(JText::_('COM_EXTENSIONDISTRIBUTOR_NOT_FOUND_OR_ACCESS_DENIED'), 'warning'); $app->redirect('index.php?option=com_extensiondistributor'); return false; } // set data object to item. $this->_item[$pk] = $data; } catch (Exception $e) { if ($e->getCode() == 404) { // Need to go thru the error handler to allow Redirect to work. JError::raiseWarning(404, $e->getMessage()); } else { $this->setError($e); $this->_item[$pk] = false; } } } return $this->_item[$pk]; } public function getDuplicates() { // Get files associated with releaseID $files = ExtensiondistributorHelper::getReleaseToFile(); // Find Duplicates $duplicates = array(); foreach(array_count_values($files) as $val => $c) { if($c > 1) $duplicates[] = $val; } return $duplicates; } public function getOrphans() { global $cparams; $path = JPATH_SITE . '/' . $cparams->get('releases_directory','packages') . '/'; $path = JPath::clean($path); $orphans = array(); // Get files associated with releaseID $files = ExtensiondistributorHelper::getReleaseToFile(); // Find Orphans $allfiles = JFolder::files($path,'.zip'); if (ExtensiondistributorHelper::checkArray($allfiles)) { $orphans = array_diff($allfiles,$files); } return $orphans; } public function getMissing() { global $cparams; $path = JPATH_SITE . '/' . $cparams->get('releases_directory','packages') . '/'; $path = JPath::clean($path); $missing = array(); // Get files associated with releaseID $files = ExtensiondistributorHelper::getReleaseToFile(); // Find Orphans $allfiles = JFolder::files($path,'.zip'); if (ExtensiondistributorHelper::checkArray($allfiles)) { $missing = array_diff($files,$allfiles); } return $missing; } public function getDirectoryForm() { global $cparams; // start the form $form = array(); JFormHelper::addFieldPath(JPATH_COMPONENT_ADMINISTRATOR . '/models/fields'); // sales attributes $attributes = array( 'type' => 'fileselect', 'name' => 'releases_directory', 'label' => 'Releases directory', 'class' => 'input-xlarge', 'color' => 'default', 'folder' => '/', 'folderonly' => 'true', 'showroot' => 'false' ); $form[] = ExtensiondistributorHelper::getFieldObject($attributes,$cparams->get('releases_directory','packages')); $attributes = array( 'type' => 'fileselect', 'name' => 'jcb_directory', 'label' => 'JCB directory', 'class' => 'input-xlarge', 'color' => 'default', 'folder' => '/', 'folderonly' => 'true', 'showroot' => 'false' ); $form[] = ExtensiondistributorHelper::getFieldObject($attributes,$cparams->get('jcb_directory','jcb')); $attributes = array( 'type' => 'fileselect', 'name' => 'updates_directory', 'label' => 'XML updates directory', 'class' => 'input-xlarge', 'color' => 'default', 'folder' => '/', 'folderonly' => 'true', 'showroot' => 'false' ); $form[] = ExtensiondistributorHelper::getFieldObject($attributes,$cparams->get('updates_directory','updates')); $attributes = array( 'type' => 'fileselect', 'name' => 'import_directory', 'label' => 'Import directory', 'class' => 'input-xlarge', 'color' => 'default', 'folder' => '/', 'folderonly' => 'true', 'showroot' => 'false' ); $form[] = ExtensiondistributorHelper::getFieldObject($attributes,$cparams->get('import_directory','extmanagerimport')); $attributes = array( 'type' => 'radio', 'name' => 'secure', 'label' => 'Secure folders', 'class' => 'btn-group btn-group-yesno span6', 'description' => 'Add the required files to avoid unauthorized access', 'default' => '1' ); $options = array( '1' => 'Yes', '0' => 'No' ); $form[] = ExtensiondistributorHelper::getFieldObject($attributes,1,$options); return $form; } }