/** * Handles getting GPM updates */ public function onTaskGPM() { $task = 'GPM'; if (!$this->admin->authorize(['admin.maintenance', 'admin.super'])) { $this->admin->json_response = ['status' => 'unauthorized', 'message' => $this->admin->translate('PLUGIN_ADMIN.INSUFFICIENT_PERMISSIONS_FOR_TASK') . ' ' . $task . '.']; return false; } $action = $_POST['action']; // getUpdatable | getUpdatablePlugins | getUpdatableThemes | gravUpdates $flush = isset($_POST['flush']) && $_POST['flush'] == true ? true : false; if (isset($this->grav['session'])) { $this->grav['session']->close(); } try { $gpm = new GPM($flush); switch ($action) { case 'getUpdates': $resources_updates = $gpm->getUpdatable(); if ($gpm->grav != null) { $grav_updates = ["isUpdatable" => $gpm->grav->isUpdatable(), "assets" => $gpm->grav->getAssets(), "version" => GRAV_VERSION, "available" => $gpm->grav->getVersion(), "date" => $gpm->grav->getDate(), "isSymlink" => $gpm->grav->isSymlink()]; echo json_encode(["status" => "success", "payload" => ["resources" => $resources_updates, "grav" => $grav_updates, "installed" => $gpm->countInstalled(), 'flushed' => $flush]]); } else { echo json_encode(["status" => "error", "message" => "Cannot connect to the GPM"]); } break; } } catch (\Exception $e) { echo json_encode(["status" => "error", "message" => $e->getMessage()]); } exit; }
/** * Checks if the user is allowed to perform the given task with its associated permissions * * @param string $task The task to execute * @param array $permissions The permissions given * * @return bool True if authorized. False if not. */ protected function authorizeTask($task = '', $permissions = []) { if (!$this->admin->authorize($permissions)) { if ($this->grav['uri']->extension() === 'json') { $this->admin->json_response = ['status' => 'unauthorized', 'message' => $this->admin->translate('PLUGIN_ADMIN.INSUFFICIENT_PERMISSIONS_FOR_TASK') . ' ' . $task . '.']; } else { $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.INSUFFICIENT_PERMISSIONS_FOR_TASK') . ' ' . $task . '.', 'error'); } return false; } return true; }