/** * Display the widget * * @return string The generated HTML */ public function display() { // The number of updates $updates = array(); $titles = array(); $api = new HawkApi(); $plugins = array_map(function ($plugin) { return $plugin->getDefinition('version'); }, Plugin::getAll(false)); $themes = array_map(function ($theme) { return $theme->getDefinition('version'); }, Theme::getAll()); try { $updates = $api->getAllAvailableUpdates($plugins, $themes); } catch (\Hawk\HawkApiException $e) { $updates = array(); } if (!empty($updates)) { if (!empty($updates['hawk'])) { \Hawk\Plugins\Main\MenuItem::getByName('admin.settings')->label .= View::make(Plugin::current()->getView('available-updates.tpl'), array('updates' => count($updates['hawk']), 'title' => Lang::get('admin.available-updates-title-core', array('number' => count($updates['hawk'])), count($updates['hawk'])))); } if (!empty($updates['plugins'])) { \Hawk\Plugins\Main\MenuItem::getByName('admin.plugins')->label .= View::make(Plugin::current()->getView('available-updates.tpl'), array('updates' => count($updates['plugins']), 'title' => Lang::get('admin.available-updates-title-plugins', array('number' => count($updates['plugins'])), count($updates['plugins'])))); } if (!empty($updates['themes'])) { \Hawk\Plugins\Main\MenuItem::getByName('admin.themes')->label .= View::make(Plugin::current()->getView('available-updates.tpl'), array('updates' => count($updates['themes']), 'title' => Lang::get('admin.available-updates-title-plugins', array('number' => count($updates['themes'])), count($updates['themes'])))); } } return ''; }
/** * Download a remote theme */ public function download() { App::response()->setContentType('json'); try { $api = new HawkApi(); $file = $api->downloadTheme($this->theme); $zip = new \ZipArchive(); if ($zip->open($file) !== true) { throw new \Exception('Impossible to open the zip archive'); } $zip->extractTo(THEMES_DIR); $theme = Theme::get($this->theme); if (!$theme) { throw new \Exception('An error occured while downloading the theme'); } unlink($file); return $theme; } catch (\Exception $e) { App::response()->setStatus(500); return array('message' => $e->getMessage()); } }
/** * Update Hawk */ public function updateHawk() { try { $api = new HawkApi(); $nextVersions = $api->getCoreAvailableUpdates(); if (empty($nextVersions)) { throw new \Exception("No newer version is available for Hawk"); } // Update incrementally all newer versions foreach ($nextVersions as $version) { // Download the update archive $archive = $api->getCoreUpdateArchive($version['version']); // Extract the downloaded file $zip = new \ZipArchive(); if ($zip->open($archive) !== true) { throw new \Exception('Impossible to open the zip archive'); } $zip->extractTo(TMP_DIR); // Put all modified or added files in the right folder $folder = TMP_DIR . 'update-v' . $version['version'] . '/'; App::fs()->copy($folder . 'to-update/*', ROOT_DIR); // Delete the files to delete $toDeleteFiles = explode(PHP_EOL, file_get_contents($folder . 'to-delete.txt')); foreach ($toDeleteFiles as $file) { if (is_file(ROOT_DIR . $file)) { unlink(ROOT_DIR . $file); } } // Remove temporary files and folders App::fs()->remove($folder); App::fs()->remove($archive); } // Execute the update method if exist $updater = new HawkUpdater(); $methods = get_class_methods($updater); foreach ($nextVersions as $version) { $method = 'v' . str_replace('.', '_', $version['version']); if (method_exists($updater, $method)) { $updater->{$method}(); } } App::cache()->clear('views'); App::cache()->clear('lang'); App::cache()->clear(Autoload::CACHE_FILE); App::cache()->clear(Lang::ORIGIN_CACHE_FILE); $response = array('status' => true); } catch (\Exception $e) { $response = array('status' => false, 'message' => DEBUG_MODE ? $e->getMessage() : Lang::get('admin.update-hawk-error')); } App::response()->setContentType('json'); return $response; }
/** * Update a plugin from the API */ public function update() { App::response()->setContentType('json'); try { $plugin = Plugin::get($this->plugin); if (!$plugin) { throw new \Exception('The plugin "' . $this->plugin . '" does not exist'); } $api = new HawkApi(); $updates = $api->getPluginsAvailableUpdates(array($plugin->getName() => $plugin->getDefinition('version'))); if (!empty($updates[$plugin->getName()])) { $file = $api->downloadPlugin($this->plugin); $zip = new \ZipArchive(); if ($zip->open($file) !== true) { throw new \Exception('Impossible to open the zip archive'); } // Copy the actual version of the plugin as backup $backup = TMP_DIR . $plugin->getName() . '.bak'; rename($plugin->getRootDir(), $backup); try { $zip->extractTo(PLUGINS_DIR); unset(Plugin::$instances[$this->plugin]); $plugin = Plugin::get($this->plugin); if (!$plugin) { throw new \Exception('An error occured while downloading the plugin'); } $this->installOrupdateDependencies($plugin); $installer = $plugin->getInstallerInstance(); foreach ($updates[$plugin->getName()] as $version) { $method = str_replace('.', '_', 'updateV' . $version); if (method_exists($installer, $method)) { $installer->{$method}(); } } App::fs()->remove($backup); } catch (\Exception $e) { // An error occured while installing the new version, rollback to the previous version App::fs()->remove($plugin->getRootDir()); rename($backup, $plugin->getRootDir()); App::fs()->remove($file); throw $e; } App::fs()->remove($file); } return array(); } catch (\Exception $e) { throw new InternalErrorException($e->getMessage()); } }