Skip to content

Commit 6230846

Browse files
committed
Redirect after plugins upgrade + test core upgrade
1 parent 5e29152 commit 6230846

File tree

6 files changed

+108
-76
lines changed

6 files changed

+108
-76
lines changed

featherbb/Controller/Admin/Updates.php

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use FeatherBB\Core\AutoUpdater;
1313
use FeatherBB\Core\PluginAutoUpdater;
14+
use FeatherBB\Core\CoreAutoUpdater;
1415
use FeatherBB\Core\AdminUtils;
1516
use FeatherBB\Core\Error;
1617
use FeatherBB\Core\Url;
@@ -28,37 +29,49 @@ public function display($req, $res, $args)
2829
{
2930
Container::get('hooks')->fire('controller.admin.updates.display');
3031

31-
$all_plugins = Lister::getPlugins();
32+
$core_updates = false;
33+
3234
$plugin_updates = array();
35+
$all_plugins = Lister::getPlugins();
3336

34-
foreach ($all_plugins as $plugin) {
35-
// If plugin isn't well formed or doesn't want to be auto-updated, skip it
36-
if (!isset($plugin->name) || !isset($plugin->version) || (isset($plugin->skip_update) && $plugin->skip_update == true)) {
37-
continue;
38-
}
39-
$updater = new PluginAutoUpdater($plugin);
40-
// $updater = new AutoUpdater(getcwd().'/temp/plugin-'.$plugin->name, getcwd().'/plugins');
41-
// $updater->setCurrentVersion($plugin->version);
42-
// $updater->setFolderName($plugin->name);
43-
// $updater->setUpdateUrl(isset($plugin->update_url) ? $plugin->update_url : 'https://api.github.com/repos/featherbb/'.$plugin->name.'/releases');
44-
// If check fails, go to next item
45-
if ($updater->checkUpdate() === false) {
46-
continue;
47-
}
37+
// Check FeatherBB core updates
38+
$coreUpdater = new CoreAutoUpdater();
39+
if ($coreUpdater->checkUpdate() === false) {
40+
// TODO: add error message to view
41+
} else {
4842
// If update available, add plugin to display in view
49-
if ($updater->newVersionAvailable()) {
50-
$plugin->last_version = $updater->getLatestVersion();
51-
$plugin_updates[] = $plugin;
43+
if ($coreUpdater->newVersionAvailable()) {
44+
$core_updates = $coreUpdater->getLatestVersion();
5245
}
5346
}
5447

48+
// Check plugins uavailable versions
49+
// foreach ($all_plugins as $plugin) {
50+
// // If plugin isn't well formed or doesn't want to be auto-updated, skip it
51+
// if (!isset($plugin->name) || !isset($plugin->version) || (isset($plugin->skip_update) && $plugin->skip_update == true)) {
52+
// continue;
53+
// }
54+
// $pluginsUpdater = new PluginAutoUpdater($plugin);
55+
// // If check fails, go to next item
56+
// if ($pluginsUpdater->checkUpdate() === false) {
57+
// // TODO: handle errors
58+
// continue;
59+
// }
60+
// // If update available, add plugin to display in view
61+
// if ($pluginsUpdater->newVersionAvailable()) {
62+
// $plugin->last_version = $pluginsUpdater->getLatestVersion();
63+
// $plugin_updates[] = $plugin;
64+
// }
65+
// }
66+
5567
AdminUtils::generateAdminMenu('updates');
5668

5769
return View::setPageInfo(array(
5870
'title' => array(Utils::escape(ForumSettings::get('o_board_title')), __('Admin'), __('Updates')),
5971
'active_page' => 'admin',
6072
'admin_console' => true,
61-
'plugin_updates' => $plugin_updates
73+
'plugin_updates' => $plugin_updates,
74+
'core_updates' => $core_updates
6275
)
6376
)->addTemplate('admin/updates.php')->display();
6477
}
@@ -98,31 +111,57 @@ public function check($req, $res, $args)
98111
public function upgradePlugins($req, $res, $args)
99112
{
100113
Container::get('hooks')->fire('controller.admin.updates.upgradePlugins');
101-
// return var_dump(Input::post('plugin_updates'));
114+
115+
// Check submit button has been clicked
116+
if (!Input::post('upgrade-plugins')) {
117+
throw new Error(__('Wrong form values'), 500);
118+
}
119+
120+
$errors = [];
121+
$upgradedPlugins = [];
102122

103123
foreach (Input::post('plugin_updates') as $plugin => $version) {
104124
if ($plugin = Lister::loadPlugin($plugin)) {
105125
// If plugin isn't well formed or doesn't want to be auto-updated, skip it
106126
if (!isset($plugin->name) || !isset($plugin->version) || (isset($plugin->skip_update) && $plugin->skip_update == true)) {
107127
continue;
108128
}
109-
$updater = new PluginAutoUpdater($plugin);
110-
// $updater = new AutoUpdater(getcwd().'/temp/plugin-'.$plugin->name, getcwd().'/plugins');
111-
// $updater->setCurrentVersion($plugin->version);
112-
// $updater->setFolderName($plugin->name);
113-
// $updater->setUpdateUrl(isset($plugin->update_url) ? $plugin->update_url : 'https://api.github.com/repos/featherbb/'.$plugin->name.'/releases');
114-
$result = $updater->update();
115-
if ($result === true) {
116-
echo 'Update successful<br>';
129+
$pluginsUpdater = new PluginAutoUpdater($plugin);
130+
$result = $pluginsUpdater->update();
131+
if ($result !== true) {
132+
$errors[$plugin->name] = $pluginsUpdater->getSimulationResults();
117133
} else {
118-
echo 'Update failed: ' . $result . '!<br>';
119-
if ($result = AutoUpdater::ERROR_SIMULATE) {
120-
echo '<pre>';
121-
var_dump($updater->getSimulationResults());
122-
echo '</pre>';
123-
}
134+
$upgradedPlugins[] = $plugin->name;
124135
}
136+
} else {
137+
continue;
125138
}
126139
}
140+
if (empty($errors)) {
141+
return Router::redirect(Router::pathFor('adminUpdates'), __(sizeof($upgradedPlugins).' plugins upgraded.'));
142+
}
143+
// TODO: handle errors
144+
}
145+
146+
public function upgradeCore($req, $res, $args)
147+
{
148+
Container::get('hooks')->fire('controller.admin.updates.upgradeCore');
149+
150+
// Check submit button has been clicked
151+
if (!Input::post('upgrade-core')) {
152+
throw new Error(__('Wrong form values'), 500);
153+
}
154+
155+
$errors = [];
156+
$coreUpdater = new CoreAutoUpdater();
157+
$result = $coreUpdater->update();
158+
if ($result !== true) {
159+
$errors['core'] = $coreUpdater->getSimulationResults();
160+
}
161+
162+
if (empty($errors)) {
163+
return Router::redirect(Router::pathFor('adminUpdates'), __('Core upgraded.'));
164+
}
165+
// TODO: handle errors
127166
}
128167
}

featherbb/Core/AutoUpdater.php

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -327,16 +327,6 @@ private function _removeDir($dir)
327327
return rmdir($dir);
328328
}
329329

330-
/**
331-
* Skip auto-update process if needed
332-
*
333-
* @return bool
334-
*/
335-
public function skipUpdate()
336-
{
337-
return $this->_skipUpdate === true;
338-
}
339-
340330
/**
341331
* Check for a new version
342332
*
@@ -347,8 +337,6 @@ public function skipUpdate()
347337
*/
348338
public function checkUpdate()
349339
{
350-
// $this->_log->addNotice('Checking for a new update...');
351-
352340
// Reset previous updates
353341
$this->_latestVersion = new version('0.0.0');
354342
$this->_updates = [];
@@ -380,8 +368,6 @@ public function checkUpdate()
380368
}
381369

382370
// $this->_cache->set('update-versions', $releases);
383-
// } else {
384-
// $this->_log->addDebug('Got updates from cache');
385371
// }
386372

387373
// Check for latest version
@@ -706,7 +692,6 @@ protected function _install($updateFile, $simulateInstall, $version)
706692

707693
zip_close($zip);
708694

709-
// TODO
710695
// $this->_log->addNotice(sprintf('Update "%s" successfully installed', $version));
711696

712697
return true;
@@ -723,8 +708,6 @@ protected function _install($updateFile, $simulateInstall, $version)
723708
*/
724709
public function update($simulateInstall = true, $deleteDownload = true)
725710
{
726-
// $this->_log->addInfo('Trying to perform update');
727-
728711
// Check for latest version
729712
if ($this->_latestVersion === null || count($this->_updates) === 0)
730713
$this->checkUpdate();
@@ -743,8 +726,6 @@ public function update($simulateInstall = true, $deleteDownload = true)
743726
}
744727

745728
foreach ($this->_updates as $update) {
746-
// $this->_log->addDebug(sprintf('Update to version "%s"', $update['version']));
747-
748729
// Check for temp directory
749730
if (empty($this->_tempDir) || !is_dir($this->_tempDir) || !is_writable($this->_tempDir)) {
750731
// $this->_log->addCritical(sprintf('Temporary directory "%s" does not exist or is not writeable!', $this->_tempDir));
@@ -759,7 +740,7 @@ public function update($simulateInstall = true, $deleteDownload = true)
759740
return self::ERROR_INSTALL_DIR;
760741
}
761742

762-
$updateFile = $this->_tempDir . $update['version'] . '.zip';
743+
$updateFile = $this->_tempDir . $this->_rootFolder . $update['version'] . '.zip';
763744

764745
// Download update
765746
if (!is_file($updateFile)) {
@@ -768,31 +749,21 @@ public function update($simulateInstall = true, $deleteDownload = true)
768749

769750
return self::ERROR_DOWNLOAD_UPDATE;
770751
}
771-
772-
// $this->_log->addDebug(sprintf('Latest update downloaded to "%s"', $updateFile));
773-
} else {
774-
// $this->_log->addInfo(sprintf('Latest update already downloaded to "%s"', $updateFile));
775752
}
776753

777754
// Install update
778755
$result = $this->_install($updateFile, $simulateInstall, $update['version']);
779756
if ($result === true) {
780757
if ($deleteDownload) {
781-
// $this->_log->addDebug(sprintf('Trying to delete update file "%s" after successfull update', $updateFile));
782-
if (@unlink($updateFile)) {
783-
// $this->_log->addInfo(sprintf('Update file "%s" deleted after successfull update', $updateFile));
784-
} else {
758+
if (!@unlink($updateFile)) {
785759
// $this->_log->addError(sprintf('Could not delete update file "%s" after successfull update!', $updateFile));
786760

787761
return self::ERROR_DELETE_TEMP_UPDATE;
788762
}
789763
}
790764
} else {
791765
if ($deleteDownload) {
792-
// $this->_log->addDebug(sprintf('Trying to delete update file "%s" after failed update', $updateFile));
793-
if (@unlink($updateFile)) {
794-
// $this->_log->addInfo(sprintf('Update file "%s" deleted after failed update', $updateFile));
795-
} else {
766+
if (!@unlink($updateFile)) {
796767
// $this->_log->addError(sprintf('Could not delete update file "%s" after failed update!', $updateFile));
797768
}
798769
}
@@ -859,11 +830,25 @@ class PluginAutoUpdater extends AutoUpdater {
859830
public function __construct($plugin)
860831
{
861832
// Construct parent class
862-
parent::__construct(getcwd().'/temp/plugin-'.$plugin->name, getcwd().'/plugins');
833+
parent::__construct(getcwd().'/temp/', getcwd().'/plugins');
863834

864835
// Set plugin informations
865836
$this->setRootFolder($plugin->name);
866837
$this->setCurrentVersion($plugin->version);
867838
$this->setUpdateUrl(isset($plugin->update_url) ? $plugin->update_url : 'https://api.github.com/repos/featherbb/'.$plugin->name.'/releases');
868839
}
869840
}
841+
842+
class CoreAutoUpdater extends AutoUpdater {
843+
844+
public function __construct()
845+
{
846+
// Construct parent class
847+
parent::__construct(getcwd().'/temp/', getcwd().'/');
848+
849+
// Set plugin informations
850+
$this->setRootFolder('featherbb');
851+
$this->setCurrentVersion(ForumEnv::get('FORUM_VERSION'));
852+
$this->setUpdateUrl('https://api.github.com/repos/featherbb/featherbb/releases');
853+
}
854+
}

featherbb/Middleware/Core.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function load_default_forum_env()
6363
'FEATHER_ROOT' => '',
6464
'FORUM_CONFIG_FILE' => 'config.php',
6565
'FORUM_CACHE_DIR' => 'cache/',
66-
'FORUM_VERSION' => '1.0.0',
66+
'FORUM_VERSION' => '1.0.0-beta.2',
6767
'FORUM_NAME' => 'FeatherBB',
6868
'FORUM_DB_REVISION' => 21,
6969
'FORUM_SI_REVISION' => 2,
@@ -154,7 +154,6 @@ public function __invoke($req, $res, $next)
154154
if ((isset($this->app->environment['HTTP_X_MOZ'])) && ($this->app->environment['HTTP_X_MOZ'] == 'prefetch')) {
155155
return $this->app->response->setStatus(403); // Send forbidden header
156156
}
157-
158157
// Populate Slim object with forum_env vars
159158
Container::set('forum_env', $this->forum_env);
160159
// Load FeatherBB utils class

featherbb/View/admin/updates.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,24 @@
2222
<div class="blockform">
2323
<h2><span><?php _e('Available updates') ?></span></h2>
2424
<div class="box">
25-
<form id="upgrade-core" method="get" action="<?= Router::pathFor('adminUpgradeCore') ?>">
26-
<p class="submittop"><input type="submit" name="save" value="<?php _e('Check for updates') ?>" /></p>
25+
<p class="submittop"><input type="submit" name="check-updates" value="<?php _e('Check for updates') ?>" /></p>
26+
<form id="upgrade-core" method="post" action="<?= Router::pathFor('adminUpgradeCore') ?>">
27+
<input type="hidden" name="csrf_name" value="<?= $csrf_name; ?>"><input type="hidden" name="csrf_value" value="<?= $csrf_value; ?>">
2728
<div class="inform">
2829
<fieldset>
2930
<legend><?php _e('FeatherBB core') ?></legend>
3031
<div class="infldset">
31-
<p><?php _e('Add word info'); echo (ForumSettings::get('o_censoring') == '1' ? sprintf(__('Censoring enabled'), '<a href="'.Router::pathFor('adminOptions').'#censoring">'.__('Options').'</a>') : sprintf(__('Censoring disabled'), '<a href="'.Router::pathFor('adminOptions').'#censoring">'.__('Options').'</a>')) ?></p>
32+
<p>
33+
<?php if ($core_updates): ?>
34+
<?php printf(__('FeatherBB version data')."\n", ForumEnv::get('FORUM_VERSION'), $core_updates) ?>
35+
<a href="https://github.com/featherbb/featherbb/releases/tag/<?= $core_updates; ?>" target="_blank"><?php _e('View changelog') ?></a>
36+
<?php else:
37+
_e('FeatherBB core up to date');
38+
endif; ?>
39+
</p>
3240
</div>
3341
</fieldset>
34-
<?php if (!empty($core_updates)): ?><p class="buttons"><input type="submit" name="upgrade" value="<?php _e('Upgrade core') ?>" /></p><?php endif; ?>
42+
<?php if ($core_updates): ?><p class="buttons"><input type="submit" name="upgrade-core" value="<?php _e('Upgrade core') ?>" /></p><?php endif; ?>
3543
</div>
3644
</form>
3745
<form id="upgrade-plugins" method="post" action="<?= Router::pathFor('adminUpgradePlugins') ?>">
@@ -74,7 +82,7 @@
7482
?>
7583
</div>
7684
</fieldset>
77-
<?php if (!empty($plugin_updates)): ?><p class="buttons"><input type="submit" name="upgrade" value="<?php _e('Upgrade plugins') ?>" /></p><?php endif; ?>
85+
<?php if (!empty($plugin_updates)): ?><p class="buttons"><input type="submit" name="upgrade-plugins" value="<?php _e('Upgrade plugins') ?>" /></p><?php endif; ?>
7886
</div>
7987
</form>
8088
<form id="upgrade-themes" method="post" action="<?= Router::pathFor('adminUpgradeThemes') ?>">
@@ -114,7 +122,7 @@
114122
</div>
115123
</fieldset>
116124
</div>
117-
<?php if (!empty($theme_updates)): ?><p class="buttons"><input type="submit" name="upgrade" value="<?php _e('Upgrade themes') ?>" /></p><?php endif; ?>
125+
<?php if (!empty($theme_updates)): ?><p class="buttons"><input type="submit" name="upgrade-themes" value="<?php _e('Upgrade themes') ?>" /></p><?php endif; ?>
118126
</form>
119127
</div>
120128
</div>

featherbb/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
// Admin updates
108108
Route::get('/updates', '\FeatherBB\Controller\Admin\Updates:display')->setName('adminUpdates');
109109
Route::get('/updates/check', '\FeatherBB\Controller\Admin\Updates:check')->setName('adminCheckUpdates');
110-
Route::get('/updates/upgrade-core', '\FeatherBB\Controller\Admin\Updates:upgradeCore')->setName('adminUpgradeCore');
110+
Route::post('/updates/upgrade-core', '\FeatherBB\Controller\Admin\Updates:upgradeCore')->setName('adminUpgradeCore');
111111
Route::post('/updates/upgrade-plugins', '\FeatherBB\Controller\Admin\Updates:upgradePlugins')->setName('adminUpgradePlugins');
112112
Route::post('/updates/upgrade-themes', '\FeatherBB\Controller\Admin\Updates:upgradeThemes')->setName('adminUpgradeThemes');
113113

vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
'FeatherBB\\Core\\AdminUtils' => $baseDir . '/featherbb/Core/AdminUtils.php',
5353
'FeatherBB\\Core\\AutoUpdater' => $baseDir . '/featherbb/Core/AutoUpdater.php',
5454
'FeatherBB\\Core\\Cache' => $baseDir . '/featherbb/Core/Cache.php',
55+
'FeatherBB\\Core\\CoreAutoUpdater' => $baseDir . '/featherbb/Core/AutoUpdater.php',
5556
'FeatherBB\\Core\\Database' => $baseDir . '/featherbb/Core/Database.php',
5657
'FeatherBB\\Core\\Email' => $baseDir . '/featherbb/Core/Email.php',
5758
'FeatherBB\\Core\\Error' => $baseDir . '/featherbb/Core/Error.php',

0 commit comments

Comments
 (0)