@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\ListModel; use Joomla\Utilities\ArrayHelper; /** * Downloads List Model */ class ExtensiondistributorModelDownloads extends ListModel { public function __construct($config = array()) { if (empty($config['filter_fields'])) { $config['filter_fields'] = array( 'a.id','id', 'a.published','published', 'a.ordering','ordering', 'a.created_by','created_by', 'a.modified_by','modified_by', 'a.transaction','transaction', 'g.name','package', 'a.user','user', 'a.ip','ip', 'a.domain','domain', 'a.created','created' ); } parent::__construct($config); } /** * Method to auto-populate the model state. * * Note. Calling getState in this method will result in recursion. * * @param string $ordering An optional ordering field. * @param string $direction An optional direction (asc|desc). * * @return void * */ protected function populateState($ordering = null, $direction = null) { $app = JFactory::getApplication(); // Adjust the context to support modal layouts. if ($layout = $app->input->get('layout')) { $this->context .= '.' . $layout; } // Check if the form was submitted $formSubmited = $app->input->post->get('form_submited'); $access = $this->getUserStateFromRequest($this->context . '.filter.access', 'filter_access', 0, 'int'); if ($formSubmited) { $access = $app->input->post->get('access'); $this->setState('filter.access', $access); } $created_by = $this->getUserStateFromRequest($this->context . '.filter.created_by', 'filter_created_by', ''); $this->setState('filter.created_by', $created_by); $sorting = $this->getUserStateFromRequest($this->context . '.filter.sorting', 'filter_sorting', 0, 'int'); $this->setState('filter.sorting', $sorting); $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search'); $this->setState('filter.search', $search); $transaction = $this->getUserStateFromRequest($this->context . '.filter.transaction', 'filter_transaction'); if ($formSubmited) { $transaction = $app->input->post->get('transaction'); $this->setState('filter.transaction', $transaction); } $package = $this->getUserStateFromRequest($this->context . '.filter.package', 'filter_package'); if ($formSubmited) { $package = $app->input->post->get('package'); $this->setState('filter.package', $package); } $user = $this->getUserStateFromRequest($this->context . '.filter.user', 'filter_user'); if ($formSubmited) { $user = $app->input->post->get('user'); $this->setState('filter.user', $user); } $ip = $this->getUserStateFromRequest($this->context . '.filter.ip', 'filter_ip'); if ($formSubmited) { $ip = $app->input->post->get('ip'); $this->setState('filter.ip', $ip); } $domain = $this->getUserStateFromRequest($this->context . '.filter.domain', 'filter_domain'); if ($formSubmited) { $domain = $app->input->post->get('domain'); $this->setState('filter.domain', $domain); } $created = $this->getUserStateFromRequest($this->context . '.filter.created', 'filter_created'); if ($formSubmited) { $created = $app->input->post->get('created'); $this->setState('filter.created', $created); } // List state information. parent::populateState($ordering, $direction); } /** * Method to get an array of data items. * * @return mixed An array of data items on success, false on failure. */ public function getItems() { // Check in items $this->checkInNow(); // load parent items $items = parent::getItems(); // set selection value to a translatable value if (ExtensiondistributorHelper::checkArray($items)) { foreach ($items as $nr => &$item) { // convert transaction $item->transaction = $this->selectionTranslation($item->transaction, 'transaction'); } } // return items return $items; } /** * Method to convert selection values to translatable string. * * @return translatable string */ public function selectionTranslation($value,$name) { // Array of transaction language strings if ($name === 'transaction') { $transactionArray = array( 'Web' => 'COM_EXTENSIONDISTRIBUTOR_DOWNLOAD_WEBSITE', 'Joomla' => 'COM_EXTENSIONDISTRIBUTOR_DOWNLOAD_JOOMLA_UPDATER', 'Watchful' => 'COM_EXTENSIONDISTRIBUTOR_DOWNLOAD_WATCHFUL', 'Other' => 'COM_EXTENSIONDISTRIBUTOR_DOWNLOAD_OTHER' ); // Now check if value is found in this array if (isset($transactionArray[$value]) && ExtensiondistributorHelper::checkString($transactionArray[$value])) { return $transactionArray[$value]; } } return $value; } /** * Method to build an SQL query to load the list data. * * @return string An SQL query */ protected function getListQuery() { // Get the user object. $user = JFactory::getUser(); // Create a new query object. $db = JFactory::getDBO(); $query = $db->getQuery(true); // Select some fields $query->select('a.*'); // From the extensiondistributor_item table $query->from($db->quoteName('#__extensiondistributor_download', 'a')); // From the extensiondistributor_package table. $query->select($db->quoteName('g.name','package_name')); $query->join('LEFT', $db->quoteName('#__extensiondistributor_package', 'g') . ' ON (' . $db->quoteName('a.package') . ' = ' . $db->quoteName('g.id') . ')'); // Filter by published state $published = $this->getState('filter.published'); if (is_numeric($published)) { $query->where('a.published = ' . (int) $published); } elseif ($published === '') { $query->where('(a.published = 0 OR a.published = 1)'); } // Filter by search. $search = $this->getState('filter.search'); if (!empty($search)) { if (stripos($search, 'id:') === 0) { $query->where('a.id = ' . (int) substr($search, 3)); } else { $search = $db->quote('%' . $db->escape($search) . '%'); $query->where('(a.transaction LIKE '.$search.' OR a.package LIKE '.$search.' OR g.name LIKE '.$search.' OR a.user LIKE '.$search.' OR a.ip LIKE '.$search.' OR a.domain LIKE '.$search.' OR a.created LIKE '.$search.' OR a.key LIKE '.$search.')'); } } // Filter by Transaction. $_transaction = $this->getState('filter.transaction'); if (is_numeric($_transaction)) { if (is_float($_transaction)) { $query->where('a.transaction = ' . (float) $_transaction); } else { $query->where('a.transaction = ' . (int) $_transaction); } } elseif (ExtensiondistributorHelper::checkString($_transaction)) { $query->where('a.transaction = ' . $db->quote($db->escape($_transaction))); } elseif (ExtensiondistributorHelper::checkArray($_transaction)) { // Secure the array for the query $_transaction = array_map( function ($val) use(&$db) { if (is_numeric($val)) { if (is_float($val)) { return (float) $val; } else { return (int) $val; } } elseif (ExtensiondistributorHelper::checkString($val)) { return $db->quote($db->escape($val)); } }, $_transaction); // Filter by the Transaction Array. $query->where('a.transaction IN (' . implode(',', $_transaction) . ')'); } // Filter by Package. $_package = $this->getState('filter.package'); if (is_numeric($_package)) { if (is_float($_package)) { $query->where('a.package = ' . (float) $_package); } else { $query->where('a.package = ' . (int) $_package); } } elseif (ExtensiondistributorHelper::checkString($_package)) { $query->where('a.package = ' . $db->quote($db->escape($_package))); } elseif (ExtensiondistributorHelper::checkArray($_package)) { // Secure the array for the query $_package = array_map( function ($val) use(&$db) { if (is_numeric($val)) { if (is_float($val)) { return (float) $val; } else { return (int) $val; } } elseif (ExtensiondistributorHelper::checkString($val)) { return $db->quote($db->escape($val)); } }, $_package); // Filter by the Package Array. $query->where('a.package IN (' . implode(',', $_package) . ')'); } // Filter by User. $_user = $this->getState('filter.user'); if (is_numeric($_user)) { if (is_float($_user)) { $query->where('a.user = ' . (float) $_user); } else { $query->where('a.user = ' . (int) $_user); } } elseif (ExtensiondistributorHelper::checkString($_user)) { $query->where('a.user = ' . $db->quote($db->escape($_user))); } elseif (ExtensiondistributorHelper::checkArray($_user)) { // Secure the array for the query $_user = array_map( function ($val) use(&$db) { if (is_numeric($val)) { if (is_float($val)) { return (float) $val; } else { return (int) $val; } } elseif (ExtensiondistributorHelper::checkString($val)) { return $db->quote($db->escape($val)); } }, $_user); // Filter by the User Array. $query->where('a.user IN (' . implode(',', $_user) . ')'); } // Add the list ordering clause. $orderCol = $this->state->get('list.ordering', 'a.id'); $orderDirn = $this->state->get('list.direction', 'desc'); if ($orderCol != '') { $query->order($db->escape($orderCol . ' ' . $orderDirn)); } return $query; } /** * Method to get a store id based on model configuration state. * * @return string A store id. * */ protected function getStoreId($id = '') { // Compile the store id. $id .= ':' . $this->getState('filter.id'); $id .= ':' . $this->getState('filter.search'); $id .= ':' . $this->getState('filter.ordering'); $id .= ':' . $this->getState('filter.created_by'); $id .= ':' . $this->getState('filter.modified_by'); // Check if the value is an array $_transaction = $this->getState('filter.transaction'); if (ExtensiondistributorHelper::checkArray($_transaction)) { $id .= ':' . implode(':', $_transaction); } // Check if this is only an number or string elseif (is_numeric($_transaction) || ExtensiondistributorHelper::checkString($_transaction)) { $id .= ':' . $_transaction; } // Check if the value is an array $_package = $this->getState('filter.package'); if (ExtensiondistributorHelper::checkArray($_package)) { $id .= ':' . implode(':', $_package); } // Check if this is only an number or string elseif (is_numeric($_package) || ExtensiondistributorHelper::checkString($_package)) { $id .= ':' . $_package; } // Check if the value is an array $_user = $this->getState('filter.user'); if (ExtensiondistributorHelper::checkArray($_user)) { $id .= ':' . implode(':', $_user); } // Check if this is only an number or string elseif (is_numeric($_user) || ExtensiondistributorHelper::checkString($_user)) { $id .= ':' . $_user; } $id .= ':' . $this->getState('filter.ip'); $id .= ':' . $this->getState('filter.domain'); $id .= ':' . $this->getState('filter.created'); return parent::getStoreId($id); } /** * Build an SQL query to checkin all items left checked out longer then a set time. * * @return a bool * */ protected function checkInNow() { // Get set check in time $time = JComponentHelper::getParams('com_extensiondistributor')->get('check_in'); if ($time) { // Get a db connection. $db = JFactory::getDbo(); // Reset query. $query = $db->getQuery(true); $query->select('*'); $query->from($db->quoteName('#__extensiondistributor_download')); // Only select items that are checked out. $query->where($db->quoteName('checked_out') . '!=0'); $db->setQuery($query, 0, 1); $db->execute(); if ($db->getNumRows()) { // Get Yesterdays date. $date = JFactory::getDate()->modify($time)->toSql(); // Reset query. $query = $db->getQuery(true); // Fields to update. $fields = array( $db->quoteName('checked_out_time') . '=\'0000-00-00 00:00:00\'', $db->quoteName('checked_out') . '=0' ); // Conditions for which records should be updated. $conditions = array( $db->quoteName('checked_out') . '!=0', $db->quoteName('checked_out_time') . '<\''.$date.'\'' ); // Check table. $query->update($db->quoteName('#__extensiondistributor_download'))->set($fields)->where($conditions); $db->setQuery($query); $db->execute(); } } return false; } }