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