Файловый менеджер - Редактировать - /var/www/html/administrator/components/com_jdownloads/src/Controller/ToolsController.php
Ðазад
<?php /** * @package jDownloads * @version 4.1 * @copyright (C) 2007 - 2026 Arno Betz - www.jdownloads.com * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt * * jDownloads is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ namespace JDownloads\Component\JDownloads\Administrator\Controller; \defined( '_JEXEC' ) or die; use Joomla\Utilities\ArrayHelper; use Joomla\CMS\Language\Text; use Joomla\CMS\Factory; use Joomla\CMS\Session\Session; use Joomla\CMS\MVC\Controller\AdminController; use Joomla\Filesystem\File; use Joomla\Filesystem\Folder; use Joomla\CMS\Router\Route; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Table\Table; use Joomla\Database\DatabaseInterface; use JDownloads\Component\JDownloads\Administrator\Helper\JDownloadsHelper; /** * jDownloads Tools Controller * */ class ToolsController extends AdminController { function __construct() { parent::__construct(); // Register Extra task $this->registerTask( 'resetDownloadCounter', 'resetDownloadCounter' ); $this->registerTask( 'resetBatchSwitch', 'resetBatchSwitch' ); $this->registerTask( 'resetCom', 'resetCom' ); $this->registerTask( 'cleanImageFolders', 'cleanImageFolders' ); $this->registerTask( 'deleteBackupTables', 'deleteBackupTables' ); $this->registerTask( 'resetCategoriesRules', 'resetCategoriesRules' ); $this->registerTask( 'resetDownloadsRules', 'resetDownloadsRules' ); $this->registerTask( 'checkTables', 'checkTables' ); } /** * Restores the historical default download permission at component level, * if the root asset is empty or set to "inherit" only. * * @return void */ protected function ensureDefaultComponentDownloadRule(): void { $db = Factory::getContainer()->get(DatabaseInterface::class); $query = $db->getQuery(true) ->select($db->quoteName('rules')) ->from($db->quoteName('#__assets')) ->where($db->quoteName('name') . ' = ' . $db->quote('com_jdownloads')); $db->setQuery($query); $component_rules = trim((string) $db->loadResult()); if ($component_rules !== '' && $component_rules !== '{}') { return; } $query = $db->getQuery(true) ->update($db->quoteName('#__assets')) ->set($db->quoteName('rules') . ' = ' . $db->quote('{"download":{"1":1}}')) ->where($db->quoteName('name') . ' = ' . $db->quote('com_jdownloads')); $db->setQuery($query); $db->execute(); } /** * Resets asset rules back to inheritance. * * @param string $asset_name_pattern SQL-LIKE pattern for asset names. * * @return int */ protected function resetInheritedAssetRules(string $asset_name_pattern): int { $db = Factory::getContainer()->get(DatabaseInterface::class); $query = $db->getQuery(true) ->select(array($db->quoteName('id'), $db->quoteName('rules'))) ->from($db->quoteName('#__assets')) ->where($db->quoteName('name') . ' LIKE ' . $db->quote($asset_name_pattern)); $db->setQuery($query); $asset_rows = $db->loadAssocList(); if (empty($asset_rows)) { return 0; } $asset_ids = array(); foreach ($asset_rows as $asset_row) { $normalized_rules = preg_replace('/\s+/', '', (string) ($asset_row['rules'] ?? '')); if ($normalized_rules !== '{}') { $asset_ids[] = (int) $asset_row['id']; } } if (empty($asset_ids)) { return 0; } $query = $db->getQuery(true) ->update($db->quoteName('#__assets')) ->set($db->quoteName('rules') . ' = ' . $db->quote('{}')) ->where($db->quoteName('id') . ' IN (' . implode(',', $asset_ids) . ')'); $db->setQuery($query); $db->execute(); return count($asset_ids); } /** * Returns the component asset id. * * @return int */ protected function getComponentAssetId(): int { $db = Factory::getContainer()->get(DatabaseInterface::class); $query = $db->getQuery(true) ->select($db->quoteName('id')) ->from($db->quoteName('#__assets')) ->where($db->quoteName('name') . ' = ' . $db->quote('com_jdownloads')); $db->setQuery($query); return (int) $db->loadResult(); } /** * Returns the root category id from the nested set. * * @return int */ protected function getCategoriesRootId(): int { $db = Factory::getContainer()->get(DatabaseInterface::class); $query = $db->getQuery(true) ->select($db->quoteName('id')) ->from($db->quoteName('#__jdownloads_categories')) ->where($db->quoteName('parent_id') . ' = 0'); $db->setQuery($query, 0, 1); return (int) $db->loadResult(); } /** * Rebuilds the Joomla asset nested set after parent repairs. * * @return void */ protected function rebuildAssetsTree(): void { $asset_table = Table::getInstance('Asset'); if (!$asset_table->rebuild()) { throw new \RuntimeException((string) $asset_table->getError()); } } /** * Loads jDownloads assets by name prefix and returns id/parent maps keyed by record id suffix. * * @param string $asset_prefix * * @return array<string, array<int, int>> */ protected function getAssetMapsByPrefix(string $asset_prefix): array { $db = Factory::getContainer()->get(DatabaseInterface::class); $query = $db->getQuery(true) ->select(array($db->quoteName('id'), $db->quoteName('name'), $db->quoteName('parent_id'))) ->from($db->quoteName('#__assets')) ->where($db->quoteName('name') . ' LIKE ' . $db->quote($asset_prefix . '.%')); $db->setQuery($query); $asset_rows = (array) $db->loadAssocList(); $asset_ids = array(); $asset_parent_ids = array(); foreach ($asset_rows as $asset_row) { $record_id = (int) substr((string) $asset_row['name'], strlen($asset_prefix) + 1); if ($record_id <= 0) { continue; } $asset_ids[$record_id] = (int) $asset_row['id']; $asset_parent_ids[$record_id] = (int) $asset_row['parent_id']; } return array( 'asset_ids' => $asset_ids, 'asset_parent_ids' => $asset_parent_ids, ); } /** * Checks whether a table column exists. * * @param string $table_name * @param string $column_name * * @return bool */ protected function hasTableColumn(string $table_name, string $column_name): bool { $db = Factory::getContainer()->get(DatabaseInterface::class); $table_columns = $db->getTableColumns($table_name); return isset($table_columns[$column_name]); } /** * Backfills invalid category extension values. * * @return int */ protected function repairCategoryExtensions(): int { if (!$this->hasTableColumn('#__jdownloads_categories', 'extension')) { return 0; } $db = Factory::getContainer()->get(DatabaseInterface::class); $query = $db->getQuery(true) ->update($db->quoteName('#__jdownloads_categories')) ->set($db->quoteName('extension') . ' = ' . $db->quote('com_jdownloads')) ->where('(' . $db->quoteName('extension') . ' IS NULL' . ' OR ' . $db->quoteName('extension') . ' = ' . $db->quote('') . ' OR ' . $db->quoteName('extension') . ' <> ' . $db->quote('com_jdownloads') . ')'); $db->setQuery($query); $db->execute(); return (int) $db->getAffectedRows(); } /** * Removes obvious orphan assets left behind by failed category/download writes. * * @return int */ protected function cleanupBrokenJDownloadsAssets(): int { $db = Factory::getContainer()->get(DatabaseInterface::class); $query = $db->getQuery(true) ->select(array($db->quoteName('id'), $db->quoteName('name'))) ->from($db->quoteName('#__assets')) ->where('(' . $db->quoteName('name') . ' = ' . $db->quote('com_jdownloads.category.0') . ' OR ' . $db->quoteName('name') . ' LIKE ' . $db->quote('com_jdownloads.category.%') . ' OR ' . $db->quoteName('name') . ' LIKE ' . $db->quote('com_jdownloads.download.%') . ')'); $db->setQuery($query); $asset_rows = (array) $db->loadAssocList(); if (empty($asset_rows)) { return 0; } $query = $db->getQuery(true) ->select($db->quoteName('id')) ->from($db->quoteName('#__jdownloads_categories')); $db->setQuery($query); $category_ids = array_map('intval', (array) $db->loadColumn()); $category_lookup = array_fill_keys($category_ids, true); $query = $db->getQuery(true) ->select($db->quoteName('id')) ->from($db->quoteName('#__jdownloads_files')); $db->setQuery($query); $download_ids = array_map('intval', (array) $db->loadColumn()); $download_lookup = array_fill_keys($download_ids, true); $delete_ids = array(); foreach ($asset_rows as $asset_row) { $asset_id = (int) ($asset_row['id'] ?? 0); $asset_name = (string) ($asset_row['name'] ?? ''); if ($asset_name === 'com_jdownloads.category.0') { $delete_ids[] = $asset_id; continue; } if (strpos($asset_name, 'com_jdownloads.category.') === 0) { $record_id = (int) substr($asset_name, strlen('com_jdownloads.category.')); if ($record_id <= 0 || !isset($category_lookup[$record_id])) { $delete_ids[] = $asset_id; } continue; } if (strpos($asset_name, 'com_jdownloads.download.') === 0) { $record_id = (int) substr($asset_name, strlen('com_jdownloads.download.')); if ($record_id <= 0 || !isset($download_lookup[$record_id])) { $delete_ids[] = $asset_id; } } } $delete_ids = array_values(array_unique(array_filter($delete_ids))); if (empty($delete_ids)) { return 0; } $query = $db->getQuery(true) ->delete($db->quoteName('#__assets')) ->where($db->quoteName('id') . ' IN (' . implode(',', array_map('intval', $delete_ids)) . ')'); $db->setQuery($query); $db->execute(); $this->rebuildAssetsTree(); return count($delete_ids); } /** * Loads one asset row by name. * * @param string $asset_name * * @return array|null */ protected function loadAssetByName(string $asset_name): ?array { $db = Factory::getContainer()->get(DatabaseInterface::class); $query = $db->getQuery(true) ->select(array($db->quoteName('id'), $db->quoteName('parent_id'), $db->quoteName('title'))) ->from($db->quoteName('#__assets')) ->where($db->quoteName('name') . ' = ' . $db->quote($asset_name)); $db->setQuery($query); $row = $db->loadAssoc(); return $row ?: null; } /** * Creates or updates one asset row. * * @param string $asset_name * @param int $parent_asset_id * @param string $asset_title * * @return array{asset_id:int,db_updates:int,rebuild_required:bool} */ protected function ensureAssetRecord(string $asset_name, int $parent_asset_id, string $asset_title): array { $db = Factory::getContainer()->get(DatabaseInterface::class); $existing_asset = $this->loadAssetByName($asset_name); if ($existing_asset !== null) { $field_map = array(); if ($parent_asset_id > 0 && (int) ($existing_asset['parent_id'] ?? 0) !== $parent_asset_id) { $field_map[] = $db->quoteName('parent_id') . ' = ' . (int) $parent_asset_id; } if ((string) ($existing_asset['title'] ?? '') !== $asset_title) { $field_map[] = $db->quoteName('title') . ' = ' . $db->quote($asset_title); } if (!empty($field_map)) { $query = $db->getQuery(true) ->update($db->quoteName('#__assets')) ->set($field_map) ->where($db->quoteName('id') . ' = ' . (int) $existing_asset['id']); $db->setQuery($query); $db->execute(); } return array( 'asset_id' => (int) $existing_asset['id'], 'db_updates' => !empty($field_map) ? 1 : 0, 'rebuild_required' => !empty($field_map), ); } $query = $db->getQuery(true) ->insert($db->quoteName('#__assets')) ->columns(array( $db->quoteName('parent_id'), $db->quoteName('lft'), $db->quoteName('rgt'), $db->quoteName('level'), $db->quoteName('name'), $db->quoteName('title'), $db->quoteName('rules'), )) ->values( (int) $parent_asset_id . ', 0, 0, 0, ' . $db->quote($asset_name) . ', ' . $db->quote($asset_title) . ', ' . $db->quote('{}') ); $db->setQuery($query); $db->execute(); return array( 'asset_id' => (int) $db->insertid(), 'db_updates' => 1, 'rebuild_required' => true, ); } /** * Repairs category asset_id links and asset parent ids for upgraded installations. * * @return int */ protected function repairCategoryAssetHierarchy(): int { $db = Factory::getContainer()->get(DatabaseInterface::class); $component_asset_id = $this->getComponentAssetId(); $root_category_id = $this->getCategoriesRootId(); if ($component_asset_id <= 0 || $root_category_id <= 0) { return 0; } $query = $db->getQuery(true) ->select(array($db->quoteName('id'), $db->quoteName('parent_id'), $db->quoteName('asset_id'), $db->quoteName('title'), $db->quoteName('lft'))) ->from($db->quoteName('#__jdownloads_categories')) ->order($db->quoteName('lft') . ' ASC'); $db->setQuery($query); $categories = (array) $db->loadAssocList(); if (empty($categories)) { return 0; } $asset_maps = $this->getAssetMapsByPrefix('com_jdownloads.category'); $category_asset_ids = $asset_maps['asset_ids']; $category_asset_parent_ids = $asset_maps['asset_parent_ids']; $count = 0; $rebuild_assets = false; foreach ($categories as $category) { $category_id = (int) $category['id']; if ($category_id === $root_category_id) { continue; } $expected_parent_asset_id = $component_asset_id; $parent_id = (int) $category['parent_id']; if ($parent_id > 0 && $parent_id !== $root_category_id && isset($category_asset_ids[$parent_id])) { $expected_parent_asset_id = (int) $category_asset_ids[$parent_id]; } $asset_sync = $this->ensureAssetRecord( 'com_jdownloads.category.' . $category_id, $expected_parent_asset_id, (string) ($category['title'] ?? '') ); $expected_asset_id = (int) ($asset_sync['asset_id'] ?? 0); if ($expected_asset_id <= 0) { continue; } $category_asset_ids[$category_id] = $expected_asset_id; $category_asset_parent_ids[$category_id] = $expected_parent_asset_id; $count += (int) ($asset_sync['db_updates'] ?? 0); $rebuild_assets = $rebuild_assets || !empty($asset_sync['rebuild_required']); if ((int) $category['asset_id'] !== $expected_asset_id) { $query = $db->getQuery(true) ->update($db->quoteName('#__jdownloads_categories')) ->set($db->quoteName('asset_id') . ' = ' . (int) $expected_asset_id) ->where($db->quoteName('id') . ' = ' . $category_id); $db->setQuery($query); $db->execute(); $count++; } $current_parent_asset_id = isset($category_asset_parent_ids[$category_id]) ? (int) $category_asset_parent_ids[$category_id] : 0; if ($current_parent_asset_id !== $expected_parent_asset_id) { $query = $db->getQuery(true) ->update($db->quoteName('#__assets')) ->set($db->quoteName('parent_id') . ' = ' . (int) $expected_parent_asset_id) ->where($db->quoteName('id') . ' = ' . (int) $expected_asset_id); $db->setQuery($query); $db->execute(); $count++; $rebuild_assets = true; } } if ($rebuild_assets) { $this->rebuildAssetsTree(); } return $count; } /** * Repairs download asset_id links and asset parent ids for upgraded installations. * * @return int */ protected function repairDownloadAssetHierarchy(): int { $db = Factory::getContainer()->get(DatabaseInterface::class); $component_asset_id = $this->getComponentAssetId(); if ($component_asset_id <= 0) { return 0; } $query = $db->getQuery(true) ->select(array($db->quoteName('id'), $db->quoteName('catid'), $db->quoteName('asset_id'), $db->quoteName('title'))) ->from($db->quoteName('#__jdownloads_files')); $db->setQuery($query); $downloads = (array) $db->loadAssocList(); if (empty($downloads)) { return 0; } $category_asset_maps = $this->getAssetMapsByPrefix('com_jdownloads.category'); $category_asset_ids = $category_asset_maps['asset_ids']; $asset_maps = $this->getAssetMapsByPrefix('com_jdownloads.download'); $download_asset_ids = $asset_maps['asset_ids']; $download_asset_parent_ids = $asset_maps['asset_parent_ids']; $count = 0; $rebuild_assets = false; foreach ($downloads as $download) { $download_id = (int) $download['id']; $expected_parent_asset_id = $component_asset_id; $category_id = (int) $download['catid']; if ($category_id > 1 && isset($category_asset_ids[$category_id])) { $expected_parent_asset_id = (int) $category_asset_ids[$category_id]; } $asset_sync = $this->ensureAssetRecord( 'com_jdownloads.download.' . $download_id, $expected_parent_asset_id, (string) ($download['title'] ?? '') ); $expected_asset_id = (int) ($asset_sync['asset_id'] ?? 0); if ($expected_asset_id <= 0) { continue; } $download_asset_ids[$download_id] = $expected_asset_id; $download_asset_parent_ids[$download_id] = $expected_parent_asset_id; $count += (int) ($asset_sync['db_updates'] ?? 0); $rebuild_assets = $rebuild_assets || !empty($asset_sync['rebuild_required']); if ((int) $download['asset_id'] !== $expected_asset_id) { $query = $db->getQuery(true) ->update($db->quoteName('#__jdownloads_files')) ->set($db->quoteName('asset_id') . ' = ' . (int) $expected_asset_id) ->where($db->quoteName('id') . ' = ' . $download_id); $db->setQuery($query); $db->execute(); $count++; } $current_parent_asset_id = isset($download_asset_parent_ids[$download_id]) ? (int) $download_asset_parent_ids[$download_id] : 0; if ($current_parent_asset_id !== $expected_parent_asset_id) { $query = $db->getQuery(true) ->update($db->quoteName('#__assets')) ->set($db->quoteName('parent_id') . ' = ' . (int) $expected_parent_asset_id) ->where($db->quoteName('id') . ' = ' . (int) $expected_asset_id); $db->setQuery($query); $db->execute(); $count++; $rebuild_assets = true; } } if ($rebuild_assets) { $this->rebuildAssetsTree(); } return $count; } /** * Reset all download counters to zero */ public function resetDownloadCounter() { // Check for request forgeries $this->checkToken('GET'); // check user access right $app = Factory::getApplication(); if ($app->getIdentity()->authorise('core.admin','com_jdownloads')) { $db = Factory::getContainer()->get(DatabaseInterface::class); $query = $db->getQuery(true); $query->update($db->quoteName('#__jdownloads_files')); $query->set('downloads = \'0\''); $db->setQuery($query); try { $result = $db->execute(); } catch (\Exception $e) { $app->enqueueMessage($e->getMessage(), 'error'); $this->setRedirect(Route::_('index.php?option=com_jdownloads&view=tools', false)); return; } Factory::getApplication()->enqueueMessage(Text::_('COM_JDOWNLOADS_TOOLS_RESET_RESULT_OKAY_MSG')); } $this->setRedirect(Route::_('index.php?option=com_jdownloads&view=tools', false)); } /** * reset all categories permissions settings to 'inherited' */ public function resetCategoriesRules() { // Check for request forgeries $this->checkToken('GET'); // check user access right $app = Factory::getApplication(); if ($app->getIdentity()->authorise('core.admin','com_jdownloads')) { $count = 0; try { $this->ensureDefaultComponentDownloadRule(); $count += $this->repairCategoryExtensions(); $count += $this->cleanupBrokenJDownloadsAssets(); $count += $this->repairCategoryAssetHierarchy(); $count += $this->resetInheritedAssetRules('%com_jdownloads.category%'); } catch (\Exception $e) { $app->enqueueMessage($e->getMessage(), 'error'); $this->setRedirect(Route::_('index.php?option=com_jdownloads&view=tools', false)); return; } Factory::getApplication()->enqueueMessage(sprintf(Text::_('COM_JDOWNLOADS_TOOLS_RESET_RESULTS_MSG'),(int)$count)); } $this->setRedirect(Route::_('index.php?option=com_jdownloads&view=tools', false)); } /** * reset all downloads permissions settings to 'inherited' */ public function resetDownloadsRules() { // Check for request forgeries $this->checkToken('GET'); // Check user access right $app = Factory::getApplication(); if ($app->getIdentity()->authorise('core.admin','com_jdownloads')) { $count = 0; try { $this->ensureDefaultComponentDownloadRule(); $count += $this->repairCategoryExtensions(); $count += $this->cleanupBrokenJDownloadsAssets(); $count += $this->repairCategoryAssetHierarchy(); $count += $this->repairDownloadAssetHierarchy(); $count += $this->resetInheritedAssetRules('%com_jdownloads.download%'); } catch (\Exception $e) { $app->enqueueMessage($e->getMessage(), 'error'); $this->setRedirect(Route::_('index.php?option=com_jdownloads&view=tools', false)); return; } Factory::getApplication()->enqueueMessage(sprintf(Text::_('COM_JDOWNLOADS_TOOLS_RESET_RESULTS_MSG'),(int)$count)); } $this->setRedirect(Route::_('index.php?option=com_jdownloads&view=tools', false)); } /** * */ public function resetCom() { // Check for request forgeries $this->checkToken('GET'); // Check user access right $app = Factory::getApplication(); if ($app->getIdentity()->authorise('core.admin','com_jdownloads')) { $result = JDownloadsHelper::changeParamSetting('com', ''); if ($result){ Factory::getApplication()->enqueueMessage(Text::_('COM_JDOWNLOADS_TOOLS_RESET_RESULT_OKAY_MSG')); } } $this->setRedirect(Route::_('index.php?option=com_jdownloads&view=tools', false)); } /** * */ public function resetBatchSwitch() { // Check for request forgeries $this->checkToken('GET'); // Check user access right $app = Factory::getApplication(); if ($app->getIdentity()->authorise('core.admin','com_jdownloads')) { $result = JDownloadsHelper::changeParamSetting('categories_batch_in_progress', '0'); $result2 = JDownloadsHelper::changeParamSetting('downloads_batch_in_progress', '0'); Factory::getApplication()->enqueueMessage(Text::_('COM_JDOWNLOADS_TOOLS_RESET_RESULT_OKAY_MSG')); } $this->setRedirect(Route::_('index.php?option=com_jdownloads&view=tools', false)); } /** * Clean the image folders 'screenshot' and 'thumbnails' and delete all not used images */ public function cleanImageFolders() { // Check for request forgeries $this->checkToken('GET'); // Check user access right $app = Factory::getApplication(); $db = Factory::getContainer()->get(DatabaseInterface::class); if ($app->getIdentity()->authorise('core.admin','com_jdownloads')) { $pics_folder = JPATH_SITE.'/images/jdownloads/screenshots/'; $thumbs_folder = JPATH_SITE.'/images/jdownloads/screenshots/thumbnails/'; $used_image_list = array(); $images = array(); $del_result = false; $sum = 0; $query = $db->getQuery(true); $query->select('images'); $query->from('#__jdownloads_files'); $query->where('images != '.$db->Quote('')); $db->setQuery($query); $result = $db->loadObjectList(); // create a array with all used images for ($i=0; $i < count($result); $i++){ $images = explode('|', $result[$i]->images); foreach ($images as $image){ if (!in_array($image, $used_image_list)){ $used_image_list[] = $image; } } } // get a files list with all images from folder $files_list = Folder::files( $pics_folder, $filter= '.', $recurse=false, $fullpath=false, $exclude=array('index.html', 'no_pic.gif') ); // compare and get the difference $delete_files_list = array_diff($files_list, $used_image_list); // delete the founded files if ($delete_files_list){ foreach ($delete_files_list as $delete_file_list){ $del_result = File::delete($pics_folder.$delete_file_list); File::delete($thumbs_folder.$delete_file_list); if (!$del_result){ Factory::getApplication()->enqueueMessage( Text::sprintf('COM_JDOWNLOADS_TOOLS_DELETE_NOT_USED_PICS_ERROR', $delete_file_list), 'warning'); } else { $sum++; } } } Factory::getApplication()->enqueueMessage( Text::sprintf('COM_JDOWNLOADS_TOOLS_DELETE_NOT_USED_PICS_SUM', $sum), 'notice'); } $this->setRedirect(Route::_('index.php?option=com_jdownloads&view=tools', false)); } /** * Clean the preview folder and delete all not used files from it */ public function cleanPreviewFolder() { // Check for request forgeries $this->checkToken('GET'); $params = ComponentHelper::getParams('com_jdownloads'); $files_uploaddir = $params->get('files_uploaddir'); $tempdir = $params->get('preview_files_folder_name'); $app = Factory::getApplication(); // check user access right if ($app->getIdentity()->authorise('core.admin','com_jdownloads')) { $preview_folder = $files_uploaddir.'/'.$tempdir.'/'; $used_files_list = array(); $images = array(); $del_result = false; $sum = 0; $db = Factory::getContainer()->get(DatabaseInterface::class); $query = $db->getQuery(true); $query->select('preview_filename'); $query->from('#__jdownloads_files'); $query->where('preview_filename != '.$db->Quote('')); $db->setQuery($query); $result = $db->loadObjectList(); // create a array with all used images for ($i=0; $i < count($result); $i++){ $used_files_list[] = $result[$i]->preview_filename; } // get a files list with all images from folder $files_list = Folder::files( $preview_folder, $filter= '.', $recurse=false, $fullpath=false, $exclude=array('index.html') ); // compare and get the difference $delete_files_list = array_diff($files_list, $used_files_list); // delete the founded files if ($delete_files_list){ foreach ($delete_files_list as $delete_file_list){ $del_result = File::delete($preview_folder.$delete_file_list); if (!$del_result){ Factory::getApplication()->enqueueMessage( Text::sprintf('COM_JDOWNLOADS_TOOLS_DELETE_NOT_USED_PREVIEWS_ERROR', $delete_file_list), 'warning'); } else { $sum++; } } } Factory::getApplication()->enqueueMessage( Text::sprintf('COM_JDOWNLOADS_TOOLS_DELETE_NOT_USED_PREVIEWS_SUM', $sum), 'notice'); } $this->setRedirect(Route::_('index.php?option=com_jdownloads&view=tools', false)); } /** * Delete the log data from the last auto monitoring action */ public function deleteMonitoringLog() { // Check for request forgeries $this->checkToken('GET'); if (File::exists(JPATH_ADMINISTRATOR.'/components/com_jdownloads/monitoring_logs.txt')){ File::delete(JPATH_ADMINISTRATOR.'/components/com_jdownloads/monitoring_logs.txt'); } $this->setRedirect(Route::_('index.php?option=com_jdownloads', false)); } /** * Delete the log data from the last restoration action */ public function deleteRestorationLog() { // Check for request forgeries $this->checkToken('GET'); if (File::exists(JPATH_ADMINISTRATOR.'/components/com_jdownloads/restore_logs.txt')){ File::delete(JPATH_ADMINISTRATOR.'/components/com_jdownloads/restore_logs.txt'); } $this->setRedirect(Route::_('index.php?option=com_jdownloads', false)); } /** * Delete the log data from the jD installation process */ public function deleteInstallationLog() { // Check for request forgeries $this->checkToken('GET'); $log_file = Factory::getApplication()->getConfig()->get('log_path').'/com_jdownloads_install_logs.php'; if (File::exists($log_file)){ File::delete($log_file); } $this->setRedirect(Route::_('index.php?option=com_jdownloads', false)); } /** * Check DB tables for 4.0 compatibility and try to repair when required */ public function checkTables() { // Check for request forgeries $this->checkToken('GET'); // Check user access right $app = Factory::getApplication(); if ($app->getIdentity()->authorise('core.admin','com_jdownloads')){ $result = array(); $error = array(); $final = array(); $db = Factory::getContainer()->get(DatabaseInterface::class); $query = $db->getQuery(true); $prefix = $db->getPrefix(); // Get jDownloads version as we only perform the check if version 4 is installed $version = JDownloadsHelper::getjDownloadsVersion(); if (version_compare($version, '4.0', '>=')) { // Cache the current value of sql_mode. $db->setQuery('SELECT @@sql_mode;'); $original_value = $db->loadResult(); // Reset the setting for the current session to prevent errors. $db->setQuery("SET SESSION sql_mode = ''"); $db->execute(); $result[] = '<h2>'.Text::_('COM_JDOWNLOADS_TOOLS_CHECK_DB_TABLES_40').'</h2>'; $result[] = 'The jDownloads DB tables are now checked for the correct definitions of the datetime fields.'; // Determine whether the field is defined incorrectly. $db->setQuery("SELECT COLUMN_NAME, COLUMN_DEFAULT from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = '" . $prefix . "jdownloads_categories' and COLUMN_NAME = 'checked_out_time'"); $column = $db->loadObject(); $result[] = '#__jdownloads_categories check in progress...'; if (isset($column)){ if ($column->COLUMN_DEFAULT === "'0000-00-00 00:00:00'"){ // There is at least one field with an incorrect definition, so we try to fix them all. $db->setQuery('ALTER TABLE '. $db->quoteName('#__jdownloads_categories') . ' CHANGE `checked_out_time` `checked_out_time` DATETIME NULL DEFAULT NULL'); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('UPDATE '. $db->quoteName('#__jdownloads_categories') . ' SET `checked_out_time` = NULL WHERE `checked_out_time` = '. $db->quote('0000-00-00 00:00:00')); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('ALTER TABLE '. $db->quoteName('#__jdownloads_categories') . ' CHANGE `created_time` `created_time` DATETIME NOT NULL'); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('ALTER TABLE '. $db->quoteName('#__jdownloads_categories') . ' CHANGE `modified_time` `modified_time` DATETIME NOT NULL'); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('UPDATE '. $db->quoteName('#__jdownloads_categories') . ' SET `modified_time` = `created_time` WHERE `modified_time` = '. $db->quote('0000-00-00 00:00:00')); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } if (!count($error)){ // Problems have been fixed. $result[] = '<u><b>#__jdownloads_categories finished - mistakes have been corrected.</b></u>'; } else { // Error could probably not be rectified, see messages. $result[] = '<b>Errors occurred when trying to correct the fields: </b>'; foreach ($error as $err){ $result[] = $err; } } } else { // No problem found. $result[] = '#__jdownloads_categories finished - No mistakes found.</u>'; } } $error = array(); // Determine whether the field is defined incorrectly. $db->setQuery("SELECT COLUMN_NAME, COLUMN_DEFAULT from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = '" . $prefix . "jdownloads_files' and COLUMN_NAME = 'checked_out_time'"); $column = $db->loadObject(); $result[] = '#__jdownloads_files check in progress...'; if (isset($column)){ if ($column->COLUMN_DEFAULT === "'0000-00-00 00:00:00'"){ // There is at least one field with an incorrect definition, so we try to fix them all. $db->setQuery('ALTER TABLE '. $db->quoteName('#__jdownloads_files') . ' CHANGE `publish_down` `publish_down` DATETIME NULL DEFAULT NULL'); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('UPDATE '. $db->quoteName('#__jdownloads_files') . ' SET `publish_down` = NULL WHERE `publish_down` = '. $db->quote('0000-00-00 00:00:00')); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('ALTER TABLE '. $db->quoteName('#__jdownloads_files') . ' CHANGE `publish_up` `publish_up` DATETIME NULL DEFAULT NULL'); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('UPDATE '. $db->quoteName('#__jdownloads_files') . ' SET `publish_up` = NULL WHERE `publish_up` = '. $db->quote('0000-00-00 00:00:00')); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('ALTER TABLE '. $db->quoteName('#__jdownloads_files') . ' CHANGE `file_date` `file_date` DATETIME NULL DEFAULT NULL'); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('UPDATE '. $db->quoteName('#__jdownloads_files') . ' SET `file_date` = NULL WHERE `file_date` = '. $db->quote('0000-00-00 00:00:00')); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('ALTER TABLE '. $db->quoteName('#__jdownloads_files') . ' CHANGE `checked_out_time` `checked_out_time` DATETIME NULL DEFAULT NULL'); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('UPDATE '. $db->quoteName('#__jdownloads_files') . ' SET `checked_out_time` = NULL WHERE `checked_out_time` = '. $db->quote('0000-00-00 00:00:00')); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('ALTER TABLE '. $db->quoteName('#__jdownloads_files') . ' CHANGE `created` `created` DATETIME NOT NULL'); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('ALTER TABLE '. $db->quoteName('#__jdownloads_files') . ' CHANGE `modified` `modified` DATETIME NOT NULL'); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } // Copy the created date to the field modify when empty $db->setQuery('UPDATE '. $db->quoteName('#__jdownloads_files') . ' SET `modified` = `created` WHERE `modified` = '. $db->quote('0000-00-00 00:00:00')); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } if (!count($error)){ // Problems have been fixed. $result[] = '<u><b>#__jdownloads_files finished - mistakes have been corrected.</b></u>'; } else { // Error could probably not be rectified, see messages. $result[] = '<b>Errors occurred when trying to correct the fields: </b>'; foreach ($error as $err){ $result[] = $err; } } } else { // No problem found. $result[] = '#__jdownloads_files finished - No mistakes found.'; } } $error = array(); // Determine whether the field is defined incorrectly. $db->setQuery("SELECT COLUMN_NAME, COLUMN_DEFAULT from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = '" . $prefix . "jdownloads_licenses' and COLUMN_NAME = 'checked_out_time'"); $column = $db->loadObject(); $result[] = '#__jdownloads_licenses check in progress...'; if (isset($column)){ if ($column->COLUMN_DEFAULT === "'0000-00-00 00:00:00'"){ // There is at least one field with an incorrect definition, so we try to fix them all. $db->setQuery('ALTER TABLE '. $db->quoteName('#__jdownloads_licenses') . ' CHANGE `checked_out_time` `checked_out_time` DATETIME NULL DEFAULT NULL'); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('UPDATE '. $db->quoteName('#__jdownloads_licenses') . ' SET `checked_out_time` = NULL WHERE `checked_out_time` = '. $db->quote('0000-00-00 00:00:00')); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } if (!count($error)){ // Problems have been fixed. $result[] = '<u><b>#__jdownloads_licenses finished - mistakes have been corrected.</b></u>'; } else { // Error could probably not be rectified, see messages. $result[] = '<b>Errors occurred when trying to correct the fields: </b>'; foreach ($error as $err){ $result[] = $err; } } } else { // No problem found. $result[] = '#__jdownloads_licenses finished - No mistakes found.'; } } $error = array(); // Determine whether the field is defined incorrectly. $db->setQuery("SELECT COLUMN_NAME, COLUMN_DEFAULT from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = '" . $prefix . "jdownloads_templates' and COLUMN_NAME = 'checked_out_time'"); $column = $db->loadObject(); $result[] = '#__jdownloads_templates check in progress...'; if (isset($column)){ if ($column->COLUMN_DEFAULT === "'0000-00-00 00:00:00'"){ // There is at least one field with an incorrect definition, so we try to fix them all. $db->setQuery('ALTER TABLE '. $db->quoteName('#__jdownloads_templates') . ' CHANGE `checked_out_time` `checked_out_time` DATETIME NULL DEFAULT NULL'); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('UPDATE '. $db->quoteName('#__jdownloads_templates') . ' SET `checked_out_time` = NULL WHERE `checked_out_time` = '. $db->quote('0000-00-00 00:00:00')); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } if (!count($error)){ // Problems have been fixed. $result[] = '<u><b>#__jdownloads_templates finished - mistakes have been corrected.</b></u>'; } else { // Error could probably not be rectified, see messages. $result[] = '<b>Errors occurred when trying to correct the fields: </b>'; foreach ($error as $err){ $result[] = $err; } } } else { // No problem found. $result[] = '#__jdownloads_templates finished - No mistakes found.'; } } $error = array(); // Determine whether the field is defined incorrectly. $db->setQuery("SELECT COLUMN_NAME, COLUMN_DEFAULT from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = '" . $prefix . "jdownloads_logs' and COLUMN_NAME = 'log_datetime'"); $column = $db->loadObject(); $result[] = '#__jdownloads_logs check in progress...'; if (isset($column)){ if ($column->COLUMN_DEFAULT === "'0000-00-00 00:00:00'"){ // There is at least one field with an incorrect definition, so we try to fix them all. $db->setQuery('ALTER TABLE '. $db->quoteName('#__jdownloads_logs') . ' CHANGE `log_datetime` `log_datetime` DATETIME NOT NULL'); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } if (!count($error)){ // Problems have been fixed. $result[] = '<u><b>#__jdownloads_logs finished - mistakes have been corrected.</b></u>'; } else { // Error could probably not be rectified, see messages. $result[] = '<b>Errors occurred when trying to correct the fields: </b>'; foreach ($error as $err){ $result[] = $err; } } } else { // No problem found. $result[] = '#__jdownloads_logs finished - No mistakes found.'; } } $error = array(); // Determine whether the field is defined incorrectly. $db->setQuery("SELECT COLUMN_NAME, COLUMN_DEFAULT from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = '" . $prefix . "jdownloads_usergroups_limits' and COLUMN_NAME = 'checked_out_time'"); $column = $db->loadObject(); $result[] = '#__jdownloads_usergroups_limits check in progress...'; if (isset($column)){ if ($column->COLUMN_DEFAULT === "'0000-00-00 00:00:00'"){ // There is at least one field with an incorrect definition, so we try to fix them all. $db->setQuery('ALTER TABLE '. $db->quoteName('#__jdownloads_usergroups_limits') . ' CHANGE `checked_out_time` `checked_out_time` DATETIME NULL DEFAULT NULL'); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } $db->setQuery('UPDATE '. $db->quoteName('#__jdownloads_usergroups_limits') . ' SET `checked_out_time` = NULL WHERE `checked_out_time` = '. $db->quote('0000-00-00 00:00:00')); try { $db->execute(); } catch(RuntimeException $e) { $error[] = Text::_($e->getMessage()); } if (!count($error)){ // Problems have been fixed. $result[] = '<u><b>#__jdownloads_usergroups_limits finished - mistakes have been corrected.</b></u>'; } else { // Error could probably not be rectified, see messages. $result[] = '<b>Errors occurred when trying to correct the fields: </b>'; foreach ($error as $err){ $result[] = $err; } } } else { // No problem found. $result[] = '#__jdownloads_usergroups_limits finished - No mistakes found.'; } } // Reset the sql_mode to the original value $db->setQuery("SET sql_mode = '".$original_value."'"); $db->execute(); $final = "<pre>".implode("\n",$result)."</pre>"; Factory::getApplication()->enqueueMessage($final); } else { // Wrong jD version Factory::getApplication()->enqueueMessage( Text::_('This function cannot be executed with the installed jDownloads version.'), 'warning'); } $this->setRedirect(Route::_('index.php?option=com_jdownloads&view=tools', false)); } } } ?>
| ver. 1.1 | |
.
| PHP 8.4.18 | Ð“ÐµÐ½ÐµÑ€Ð°Ñ†Ð¸Ñ Ñтраницы: 0 |
proxy
|
phpinfo
|
ÐаÑтройка