|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright (C) 2015-2016 FeatherBB |
| 5 | + * based on code by (C) 2008-2015 FluxBB |
| 6 | + * and Rickard Andersson (C) 2002-2008 PunBB |
| 7 | + * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher |
| 8 | + */ |
| 9 | + |
| 10 | +namespace FeatherBB\Controller\Admin; |
| 11 | + |
| 12 | +use FeatherBB\Core\AutoUpdater; |
| 13 | +use FeatherBB\Core\PluginAutoUpdater; |
| 14 | +use FeatherBB\Core\CoreAutoUpdater; |
| 15 | +use FeatherBB\Core\AdminUtils; |
| 16 | +use FeatherBB\Core\Error; |
| 17 | +use FeatherBB\Core\Url; |
| 18 | +use FeatherBB\Core\Utils; |
| 19 | +use FeatherBB\Core\Lister; |
| 20 | +use FeatherBB\Core\Database; |
| 21 | + |
| 22 | +class Updates |
| 23 | +{ |
| 24 | + public function __construct() |
| 25 | + { |
| 26 | + translate('admin/index'); |
| 27 | + translate('admin/updates'); |
| 28 | + translate('admin/plugins'); |
| 29 | + } |
| 30 | + |
| 31 | + public function display($req, $res, $args) |
| 32 | + { |
| 33 | + Container::get('hooks')->fire('controller.admin.updates.display'); |
| 34 | + |
| 35 | + $coreUpdates = false; |
| 36 | + $coreUpdatesMessage = __('FeatherBB core up to date'); |
| 37 | + |
| 38 | + $pluginUpdates = array(); |
| 39 | + $allPlugins = Lister::getPlugins(); |
| 40 | + |
| 41 | + // Check FeatherBB core updates |
| 42 | + $coreUpdater = new CoreAutoUpdater(); |
| 43 | + if ($coreUpdater->checkUpdate() === false) { |
| 44 | + $coreUpdatesMessage = join('<br>', $coreUpdater->getErrors()); |
| 45 | + } else { |
| 46 | + // If update available |
| 47 | + if ($coreUpdater->newVersionAvailable()) { |
| 48 | + $coreUpdates = true; |
| 49 | + $coreUpdatesMessage = sprintf(__('FeatherBB core updates available'), ForumSettings::get('o_cur_version'), $coreUpdater->getLatestVersion()); |
| 50 | + $coreUpdatesMessage .= '<a href="https://github.com/featherbb/featherbb/releases/tag/'.$coreUpdater->getLatestVersion().'" target="_blank">'.__('View changelog').'</a>'; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Check plugins uavailable versions |
| 55 | + foreach ($allPlugins as $plugin) { |
| 56 | + // If plugin isn't well formed or doesn't want to be auto-updated, skip it |
| 57 | + if (!isset($plugin->name) || !isset($plugin->version) || (isset($plugin->skip_update) && $plugin->skip_update == true)) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + $pluginsUpdater = new PluginAutoUpdater($plugin); |
| 61 | + // If check fails, add errors to display in view |
| 62 | + if ($pluginsUpdater->checkUpdate() === false) { |
| 63 | + $plugin->errors = join('<br>', $pluginsUpdater->getErrors()); |
| 64 | + $pluginUpdates[] = $plugin; |
| 65 | + } |
| 66 | + // If update available, add plugin to display in view |
| 67 | + if ($pluginsUpdater->newVersionAvailable()) { |
| 68 | + $plugin->last_version = $pluginsUpdater->getLatestVersion(); |
| 69 | + $pluginUpdates[] = $plugin; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + AdminUtils::generateAdminMenu('updates'); |
| 74 | + |
| 75 | + return View::setPageInfo(array( |
| 76 | + 'title' => array(Utils::escape(ForumSettings::get('o_board_title')), __('Admin'), __('Updates')), |
| 77 | + 'active_page' => 'admin', |
| 78 | + 'admin_console' => true, |
| 79 | + 'plugin_updates' => $pluginUpdates, |
| 80 | + 'core_updates' => $coreUpdates, |
| 81 | + 'core_updates_message' => $coreUpdatesMessage |
| 82 | + ) |
| 83 | + )->addTemplate('admin/updates.php')->display(); |
| 84 | + } |
| 85 | + |
| 86 | + public function upgradePlugins($req, $res, $args) |
| 87 | + { |
| 88 | + Container::get('hooks')->fire('controller.admin.updates.upgradePlugins'); |
| 89 | + |
| 90 | + // Check submit button has been clicked |
| 91 | + if (!Input::post('upgrade-plugins')) { |
| 92 | + throw new Error(__('Wrong form values'), 500); |
| 93 | + } |
| 94 | + |
| 95 | + $upgrade_results = []; |
| 96 | + |
| 97 | + foreach (Input::post('plugin_updates') as $plugin => $version) { |
| 98 | + if ($plugin = Lister::loadPlugin($plugin)) { |
| 99 | + // If plugin isn't well formed or doesn't want to be auto-updated, skip it |
| 100 | + if (!isset($plugin->name) || !isset($plugin->version) || (isset($plugin->skip_update) && $plugin->skip_update == true)) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + $upgrade_results[$plugin->title] = []; |
| 104 | + $pluginsUpdater = new PluginAutoUpdater($plugin); |
| 105 | + $result = $pluginsUpdater->update(); |
| 106 | + if ($result !== true) { |
| 107 | + $upgrade_results[$plugin->title]['message'] = sprintf(__('Failed upgrade message'), $plugin->version, $pluginsUpdater->getLatestVersion()); |
| 108 | + $upgrade_results[$plugin->title]['errors'] = $pluginsUpdater->getErrors(); |
| 109 | + } else { |
| 110 | + $upgrade_results[$plugin->title]['message'] = sprintf(__('Successful upgrade message'), $plugin->version, $pluginsUpdater->getLatestVersion()); |
| 111 | + } |
| 112 | + // Will not be empty if upgrade has warnings (zip archive or _upgrade.php file could not be deleted) |
| 113 | + $upgrade_results[$plugin->title]['warnings'] = $pluginsUpdater->getWarnings(); |
| 114 | + } else { |
| 115 | + continue; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Reset cache |
| 120 | + Container::get('cache')->flush(); |
| 121 | + |
| 122 | + // Display upgrade results |
| 123 | + AdminUtils::generateAdminMenu('updates'); |
| 124 | + |
| 125 | + return View::setPageInfo(array( |
| 126 | + 'title' => array(Utils::escape(ForumSettings::get('o_board_title')), __('Admin'), __('Updates')), |
| 127 | + 'active_page' => 'admin', |
| 128 | + 'admin_console' => true, |
| 129 | + 'upgrade_results' => $upgrade_results |
| 130 | + ) |
| 131 | + )->addTemplate('admin/updates.php')->display(); |
| 132 | + } |
| 133 | + |
| 134 | + public function upgradeCore($req, $res, $args) |
| 135 | + { |
| 136 | + Container::get('hooks')->fire('controller.admin.updates.upgradeCore'); |
| 137 | + |
| 138 | + // Check submit button has been clicked |
| 139 | + if (!Input::post('upgrade-core')) { |
| 140 | + throw new Error(__('Wrong form values'), 500); |
| 141 | + } |
| 142 | + |
| 143 | + $key = __('FeatherBB core'); |
| 144 | + $upgrade_results = [$key => []]; |
| 145 | + $coreUpdater = new CoreAutoUpdater(); |
| 146 | + $result = $coreUpdater->update(); |
| 147 | + if ($result !== true) { |
| 148 | + $upgrade_results[$key]['message'] = sprintf(__('Failed upgrade message'), ForumEnv::get('FORUM_VERSION'), $coreUpdater->getLatestVersion()); |
| 149 | + $upgrade_results[$key]['errors'] = $coreUpdater->getErrors(); |
| 150 | + } else { |
| 151 | + $upgrade_results[$key]['message'] = sprintf(__('Successful upgrade message'), ForumEnv::get('FORUM_VERSION'), $coreUpdater->getLatestVersion()); |
| 152 | + // Reset cache and update core version in database |
| 153 | + Container::get('cache')->flush(); |
| 154 | + if (!Database::for_table('config')->raw_execute('UPDATE `'.ForumSettings::get('db_prefix').'config` SET `conf_value` = :value WHERE `conf_name` = "o_cur_version"', array('value' => ForumEnv::get('FORUM_VERSION')))) { |
| 155 | + $coreUpdater->_warnings[] = __('Could not update core version in database'); |
| 156 | + } |
| 157 | + } |
| 158 | + // Will not be empty if upgrade has warnings (zip archive or _upgrade.php file could not be deleted) |
| 159 | + $upgrade_results[$key]['warnings'] = $coreUpdater->getWarnings(); |
| 160 | + |
| 161 | + // Display upgrade results |
| 162 | + AdminUtils::generateAdminMenu('updates'); |
| 163 | + |
| 164 | + return View::setPageInfo(array( |
| 165 | + 'title' => array(Utils::escape(ForumSettings::get('o_board_title')), __('Admin'), __('Updates')), |
| 166 | + 'active_page' => 'admin', |
| 167 | + 'admin_console' => true, |
| 168 | + 'upgrade_results' => $upgrade_results |
| 169 | + ) |
| 170 | + )->addTemplate('admin/updates.php')->display(); |
| 171 | + } |
| 172 | +} |
0 commit comments