diff --git a/classes/controller.php b/classes/controller.php index ffe67242..8f63efc5 100644 --- a/classes/controller.php +++ b/classes/controller.php @@ -682,6 +682,36 @@ class AdminController return true; } + /** + * Handles updating Grav + * + * @return bool True is the action was performed + */ + public function taskUpdategrav() + { + require_once __DIR__ . '/gpm.php'; + + //TODO: manage permissions + + $result = \Grav\Plugin\Admin\Gpm::selfupgrade(); + + if ($result) { + $this->admin->json_response = ['status' => 'success', 'message' => 'Everything updated']; + } else { + $this->admin->json_response = ['status' => 'error', 'message' => 'Updates failed']; + } + + if ($result) { + $this->admin->setMessage("Installation successful.", 'info'); + } else { + $this->admin->setMessage("Installation failed.", 'error'); + } + + //$this->post = array('_redirect' => $this->view . '/' . $this->route); + + return true; + } + /** * Handles updating plugins and themes * diff --git a/classes/gpm.php b/classes/gpm.php index f83e559c..f64b36a3 100644 --- a/classes/gpm.php +++ b/classes/gpm.php @@ -5,6 +5,7 @@ use Grav\Common\GravTrait; use Grav\Common\GPM\GPM as GravGPM; use Grav\Common\GPM\Installer; use Grav\Common\GPM\Response; +use Grav\Common\GPM\Upgrader; use Grav\Common\Filesystem\Folder; use Grav\Common\GPM\Common\Package; @@ -156,4 +157,32 @@ class Gpm return $cache_dir . DS . $filename; } + + private static function _downloadSelfupgrade($package, $tmp) + { + $output = Response::get($package['download'], []); + Folder::mkdir($tmp); + file_put_contents($tmp . DS . $package['name'], $output); + return $tmp . DS . $package['name']; + } + + public static function selfupgrade() { + $upgrader = new Upgrader(); + $update = $upgrader->getAssets()['grav-update']; + $tmp = CACHE_DIR . 'tmp/Grav-' . uniqid(); + $file = self::_downloadSelfupgrade($update, $tmp); + + Installer::install($file, GRAV_ROOT, + ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => false]); + + $errorCode = Installer::lastErrorCode(); + + Folder::delete($tmp); + + if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) { + return false; + } + + return true; + } } \ No newline at end of file diff --git a/themes/grav/js/admin-all.js b/themes/grav/js/admin-all.js index ad32bd3c..a513151f 100644 --- a/themes/grav/js/admin-all.js +++ b/themes/grav/js/admin-all.js @@ -112,7 +112,7 @@ $(function () { }); // Update plugins/themes - $('[data-maintenance-update]').on('click', function(e) { + $(document).on('click', '[data-maintenance-update]', function(e) { $(this).attr('disabled','disabled').find('> .fa').removeClass('fa-cloud-download').addClass('fa-refresh fa-spin'); var url = $(this).data('maintenanceUpdate'); @@ -223,7 +223,7 @@ $(function () { if (grav.isUpdatable) { var icon = ' '; content = 'Grav v{available} is now available! (Current: v{version}) ', - button = ''; + button = ''; content = jQuery.substitute(content, {available: grav.available, version: grav.version}); $('[data-gpm-grav]').addClass('grav').html('
' + icon + content + button + '
'); diff --git a/themes/grav/templates/partials/base.html.twig b/themes/grav/templates/partials/base.html.twig index d79d5fa4..a8026bf9 100644 --- a/themes/grav/templates/partials/base.html.twig +++ b/themes/grav/templates/partials/base.html.twig @@ -77,7 +77,7 @@Grav v{{ admin.gpm.grav.getVersion() }} is now available! (Current: v{{ constant('GRAV_VERSION') }}) - +
{% endif %}