improve = new improve(); $this->type = $this->getType(); $this->module = $this->getModule(); $this->action = $this->getAction(); $this->doAction(); $this->displayPage(); } private function getType(): string { return $_REQUEST['type'] ?? 'plugin'; } private function getModule(): string { $module = $_REQUEST['module'] ?? ''; if (!in_array($module, $this->comboModules())) { $module = '-'; } return $module; } private function getAction(): ?action { return empty($_REQUEST['config']) ? null : $this->improve->module($_REQUEST['config']); } private function getPreference(): array { try { if (!empty($this->type)) { $preferences = dcCore::app()->blog->settings->improve->preferences; if (is_string($preferences)) { $preferences = unserialize($preferences); if (is_array($preferences)) { return array_key_exists($this->type, $preferences) ? $preferences[$this->type] : []; } } } } catch (Exception $e) { } return []; } private function setPreferences(): bool { if (!empty($_POST['save_preferences'])) { $preferences[$this->type] = []; if (!empty($_POST['actions'])) { foreach ($this->improve->modules() as $action) { if (in_array($this->type, $action->types()) && in_array($action->id(), $_POST['actions'])) { $preferences[$this->type][] = $action->id(); } } } dcCore::app()->blog->settings->improve->put('preferences', serialize($preferences), 'string', null, true, true); dcAdminNotices::addSuccessNotice(__('Configuration successfully updated')); return true; } return false; } private function comboModules(): array { $allow_distrib = (bool) dcCore::app()->blog->settings->improve->allow_distrib; $official = [ 'plugin' => explode(',', DC_DISTRIB_PLUGINS), 'theme' => explode(',', DC_DISTRIB_THEMES), ]; if (!isset(dcCore::app()->themes)) { dcCore::app()->themes = new dcThemes(); dcCore::app()->themes->loadModules(dcCore::app()->blog->themes_path, null); } $combo_modules = []; $modules = $this->type == 'plugin' ? dcCore::app()->plugins->getModules() : dcCore::app()->themes->getModules(); foreach ($modules as $id => $m) { if (!$m['root_writable'] || !$allow_distrib && in_array($id, $official[$this->type])) { continue; } $combo_modules[__($m['name'])] = $id; } dcUtils::lexicalKeySort($combo_modules); return array_merge([__('Select a module') => '-'], $combo_modules); } private function doAction(): void { $log_id = ''; $done = $this->setPreferences(); if (!empty($_POST['fix'])) { if (empty($_POST['actions'])) { dcAdminNotices::addWarningNotice(__('No action selected')); } elseif ($this->module == '-') { dcAdminNotices::addWarningNotice(__('No module selected')); } else { try { $time = $this->improve->fixModule( $this->type, $this->module, $this->type == 'plugin' ? dcCore::app()->plugins->getModules($this->module) : dcCore::app()->themes->getModules($this->module), $_POST['actions'] ); $log_id = $this->improve->writeLogs(); dcCore::app()->blog->triggerBlog(); if ($this->improve->hasLog('error')) { $notice = ['type' => dcAdminNotices::NOTICE_ERROR, 'msg' => __('Fix of "%s" complete in %s secondes with errors')]; } elseif ($this->improve->hasLog('warning')) { $notice = ['type' => dcAdminNotices::NOTICE_WARNING, 'msg' => __('Fix of "%s" complete in %s secondes with warnings')]; } elseif ($this->improve->hasLog('success')) { $notice = ['type' => dcAdminNotices::NOTICE_SUCCESS, 'msg' => __('Fix of "%s" complete in %s secondes')]; } else { $notice = ['type' => dcAdminNotices::NOTICE_SUCCESS, 'msg' => __('Fix of "%s" complete in %s secondes without messages')]; } dcAdminNotices::addNotice($notice['type'], sprintf($notice['msg'], $this->module, $time)); $done = true; } catch (Exception $e) { dcCore::app()->error->add($e->getMessage()); $done = false; } } } if ($done) { dcCore::app()->adminurl->redirect('admin.plugin.improve', ['type' => $this->type, 'module' => $this->module, 'upd' => $log_id]); } } private function displayPage(): void { $bc = empty($_REQUEST['config']) ? ($this->type == 'theme' ? __('Themes actions') : __('Plugins actions')) : __('Configure module'); echo '' . __('improve') . '' . dcPage::jsLoad(dcPage::getPF('improve/js/index.js')) . ($this->action === null ? '' : $this->action->header()) . '' . dcPage::notices() . dcPage::breadcrumb([ __('Plugins') => '', __('improve') => '', $bc => '', ]); if (empty($_REQUEST['config'])) { $this->displayActions(); } else { $this->displayConfigurator(); } echo ''; } private function displayConfigurator(): void { $back_url = $_REQUEST['redir'] ?? dcCore::app()->adminurl->get('admin.plugin.improve', ['type' => $this->type]); if (null === $this->action) { echo '

' . __('Unknow module') . '

' . __('Back') . '

'; } else { $redir = $_REQUEST['redir'] ?? dcCore::app()->adminurl->get('admin.plugin.improve', ['type' => $this->type, 'config' => $this->action->id()]); $res = $this->action->configure($redir); echo '

' . sprintf(__('Configure module "%s"'), $this->action->name()) . '

' . __('Back') . '

' . html::escapeHTML($this->action->description()) . '

' . (empty($res) ? '

' . __('Nothing to configure') . '

' : $res) . '

' . form::hidden('type', $this->type) . form::hidden('config', $this->action->id()) . form::hidden('redir', $redir) . dcCore::app()->formNonce() . '

' . '
'; } } private function displayActions(): void { echo '
' . '

' . form::combo('type', [__('Plugins') => 'plugin', __('Themes') => 'theme'], $this->type) . ' ' . '' . form::hidden('p', 'improve') . '

' . '
'; $combo_modules = $this->comboModules(); if (count($combo_modules) == 1) { echo '

' . __('No module to manage') . '

'; } else { echo '
' . '' . ''; foreach ($this->improve->modules() as $action) { if (!in_array($this->type, $action->types())) { continue; } echo '' . '' . '' . '' . '' . (DC_DEBUG ? '' : '') . /* @phpstan-ignore-line */ ''; } echo '
' . __('Action') . '' . '' . __('Description') . '' . '' . __('Configuration') . '' . (DC_DEBUG ? '' . __('Priority') . '' : '') . /* @phpstan-ignore-line */ '
' . form::checkbox( ['actions[]', 'action_' . $action->id(), ], $action->id(), in_array($action->id(), $this->getPreference()) && $action->isConfigured(), '', '', !$action->isConfigured() ) . '' . '' . '' . $action->description() . '' . ( false === $action->configurator() ? '' : 'name()) . '">' . __('Configure') . '' ) . '' . $action->priority() . '

' . form::combo('module', $combo_modules, $this->module) . ' ' . form::hidden(['type'], $this->type) . dcCore::app()->formNonce() . '


'; if (!empty($_REQUEST['upd']) && !dcCore::app()->blog->settings->improve->nodetails) { $logs = $this->improve->parseLogs((int) $_REQUEST['upd']); if (!empty($logs)) { echo '

' . __('Details') . '

'; foreach ($logs as $path => $types) { echo '
' . $path . '
'; foreach ($types as $type => $tools) { echo '
'; } echo ''; } echo '
'; } } } } } /* process */ new index();