-
-
Notifications
You must be signed in to change notification settings - Fork 262
Improvement of the experimental plugin system #3852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
d0ubIeU
wants to merge
11
commits into
thorsten:main
Choose a base branch
from
d0ubIeU:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,317
−57
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fdd837b
Feature: Extension of the plugin system to include configuration and …
d0ubIeU bdaef85
Merge branch 'main' of https://github.com/d0ubIeU/phpMyFAQ
d0ubIeU 0b1f72b
remove comment
d0ubIeU 2ea3fe2
fix
d0ubIeU 27ab9dd
fix2
d0ubIeU c52c3ec
Fix_sql
d0ubIeU 8aa2adc
fix
d0ubIeU fbddd00
fix formatting
d0ubIeU e3a2210
formatting und enhanced plugin doc
d0ubIeU ebf90bf
formatting
d0ubIeU d495931
fix
d0ubIeU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /** | ||
| * Plugin API calls | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public License, | ||
| * v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
| * obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * @package phpMyFAQ | ||
| * @author Thorsten Rinne | ||
| * @copyright 2025-2026 phpMyFAQ Team | ||
| * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 | ||
| * @link https://www.phpmyfaq.de | ||
| * @since 2025-01-07 | ||
| */ | ||
|
|
||
| import { Response } from '../interfaces'; | ||
|
|
||
| /** | ||
| * Toggle plugin status | ||
| * | ||
| * @param name | ||
| * @param active | ||
| * @param csrfToken | ||
| */ | ||
| export const togglePluginStatus = async (name: string, active: boolean, csrfToken: string): Promise<Response> => { | ||
| const response = await fetch('api/plugin/toggle', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'X-CSRF-Token': csrfToken, | ||
| }, | ||
| body: JSON.stringify({ | ||
| name, | ||
| active, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CSRF protection is missing |
||
| }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const errorData = await response.json().catch(() => ({})); | ||
| throw new Error(errorData.message || `HTTP error! status: ${response.status} ${response.statusText}`); | ||
| } | ||
|
|
||
| return (await response.json()) as Response; | ||
| }; | ||
|
|
||
| /** | ||
| * Save plugin configuration | ||
| * | ||
| * @param name | ||
| * @param config | ||
| * @param csrfToken | ||
| */ | ||
| export const savePluginConfig = async ( | ||
| name: string, | ||
| config: Record<string, any>, | ||
| csrfToken: string | ||
| ): Promise<Response> => { | ||
| const response = await fetch('api/plugin/config', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'X-CSRF-Token': csrfToken, | ||
| }, | ||
| body: JSON.stringify({ | ||
| name, | ||
| config, | ||
| csrf: csrfToken, // Also including in body as requested for backend validation | ||
| }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const errorData = await response.json().catch(() => ({})); | ||
| throw new Error(errorData.message || `HTTP error! status: ${response.status} ${response.statusText}`); | ||
| } | ||
|
|
||
| return (await response.json()) as Response; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| /** | ||
| * Plugin management logic for phpMyFAQ admin backend | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public License, | ||
| * v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
| * obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * @package phpMyFAQ | ||
| * @author Thorsten Rinne | ||
| * @copyright 2025-2026 phpMyFAQ Team | ||
| * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 | ||
| * @link https://www.phpmyfaq.de | ||
| * @since 2025-01-07 | ||
| */ | ||
|
|
||
| import { togglePluginStatus, savePluginConfig } from '../api'; | ||
| import { addElement, pushNotification, pushErrorNotification, TranslationService } from '../../../../assets/src/utils'; | ||
|
|
||
| /** | ||
| * Handles plugin status toggling and configuration modal | ||
| */ | ||
| export const handlePlugins = async (): Promise<void> => { | ||
| const Translator = new TranslationService(); | ||
| await Translator.loadTranslations(document.documentElement.lang); | ||
|
|
||
| const getCsrfToken = () => document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || ''; | ||
|
|
||
| const toggleCheckboxes = document.querySelectorAll<HTMLInputElement>('.plugin-toggle'); | ||
| toggleCheckboxes.forEach((checkbox) => { | ||
| checkbox.addEventListener('change', async (event) => { | ||
| const target = event.target as HTMLInputElement; | ||
| const row = target.closest('tr'); | ||
| const pluginName = row ? row.getAttribute('data-plugin-name') : null; | ||
| const active = target.checked; | ||
| const csrfToken = getCsrfToken(); | ||
|
|
||
| if (!pluginName) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const result = await togglePluginStatus(pluginName, active, csrfToken); | ||
|
|
||
| if (!result.success) { | ||
| target.checked = !active; // Revert | ||
| pushErrorNotification( | ||
| Translator.translate('msgPluginStatusError') + ' ' + (result.message || Translator.translate('msgUnknownError')), | ||
| ); | ||
| } else { | ||
| pushNotification(Translator.translate('msgPluginStatusSuccess')); | ||
| } | ||
| } catch (error: any) { | ||
| target.checked = !active; // Revert | ||
| console.error('Error toggling plugin:', error); | ||
| pushErrorNotification(error.message || Translator.translate('msgUnknownError')); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| // Configuration Modal | ||
| const pluginConfigModal = document.getElementById('pluginConfigModal'); | ||
| if (pluginConfigModal) { | ||
| pluginConfigModal.addEventListener('show.bs.modal', (event: any) => { | ||
| const button = event.relatedTarget as HTMLElement; | ||
| const pluginName = button.getAttribute('data-plugin-name'); | ||
| const pluginDescription = button.getAttribute('data-plugin-description'); | ||
| const pluginImplementation = button.getAttribute('data-plugin-implementation'); | ||
| const configJson = button.getAttribute('data-plugin-config'); | ||
|
|
||
| const modalTitle = pluginConfigModal.querySelector('.modal-title'); | ||
| const nameInput = pluginConfigModal.querySelector<HTMLInputElement>('#configPluginName'); | ||
| const container = pluginConfigModal.querySelector('#configFieldsContainer'); | ||
| const descText = pluginConfigModal.querySelector('#pluginDescriptionText'); | ||
| const implContainer = pluginConfigModal.querySelector('#pluginImplementationContainer'); | ||
| const implCode = pluginConfigModal.querySelector('#pluginImplementationCode'); | ||
| const noConfigMsg = pluginConfigModal.querySelector('#pluginNoConfigMsg'); | ||
| const saveBtn = document.getElementById('savePluginConfigBtn'); | ||
|
|
||
| if (modalTitle && pluginName) { | ||
| modalTitle.textContent = Translator.translate('msgConfig') + ': ' + pluginName; | ||
| } | ||
| if (nameInput && pluginName) { | ||
| nameInput.value = pluginName; | ||
| } | ||
| if (descText) { | ||
| descText.textContent = pluginDescription || '-'; | ||
| } | ||
|
|
||
| if (implContainer && implCode) { | ||
| if (pluginImplementation) { | ||
| implCode.textContent = pluginImplementation; | ||
| implContainer.classList.remove('d-none'); | ||
| } else { | ||
| implContainer.classList.add('d-none'); | ||
| } | ||
| } | ||
|
|
||
| if (container) { | ||
| container.innerHTML = ''; // Clear previous fields | ||
| } | ||
| if (noConfigMsg) noConfigMsg.classList.add('d-none'); | ||
| if (saveBtn) saveBtn.style.display = 'none'; | ||
|
|
||
| let hasConfig = false; | ||
|
|
||
| if (configJson) { | ||
| try { | ||
| const configData = JSON.parse(configJson); | ||
|
|
||
| if (configData && typeof configData === 'object' && Object.keys(configData).length > 0) { | ||
| hasConfig = true; | ||
| if (saveBtn) saveBtn.style.display = 'block'; | ||
|
|
||
| Object.keys(configData).forEach((key) => { | ||
| const value = configData[key]; | ||
| let input: HTMLElement; | ||
|
|
||
| if (typeof value === 'boolean') { | ||
| input = addElement('div', { className: 'form-check form-switch mb-3' }, [ | ||
| addElement('input', { | ||
| type: 'checkbox', | ||
| className: 'form-check-input', | ||
| checked: value, | ||
| value: '1', | ||
| id: 'config_' + key, | ||
| name: 'config[' + key + ']', | ||
| }), | ||
| addElement('label', { | ||
| className: 'form-check-label', | ||
| textContent: key, | ||
| htmlFor: 'config_' + key, | ||
| }), | ||
| ]); | ||
| } else { | ||
| const type = (typeof value === 'number' || !isNaN(Number(value)) && String(value).trim() !== '') ? 'number' : | ||
| (key.toLowerCase().includes('email')) ? 'email' : | ||
| (key.toLowerCase().includes('date')) ? 'date' : 'text'; | ||
|
|
||
| const props: Record<string, any> = { | ||
| className: 'form-control', | ||
| value: String(value), | ||
| id: 'config_' + key, | ||
| name: 'config[' + key + ']', | ||
| }; | ||
|
|
||
| if (String(value).length > 50 && type === 'text') { | ||
| input = addElement('div', { className: 'mb-3' }, [ | ||
| addElement('label', { className: 'form-label', textContent: key, htmlFor: 'config_' + key }), | ||
| addElement('textarea', { ...props, rows: 3 }), | ||
| ]); | ||
| } else { | ||
| input = addElement('div', { className: 'mb-3' }, [ | ||
| addElement('label', { className: 'form-label', textContent: key, htmlFor: 'config_' + key }), | ||
| addElement('input', { ...props, type }), | ||
| ]); | ||
| } | ||
| } | ||
|
|
||
| if (container) { | ||
| container.appendChild(input); | ||
| } | ||
| }); | ||
| } | ||
| } catch (e) { | ||
| console.error('Error parsing config:', e); | ||
| } | ||
| } | ||
|
|
||
| if (!hasConfig && noConfigMsg) { | ||
| noConfigMsg.classList.remove('d-none'); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // Save Configuration | ||
| const saveBtn = document.getElementById('savePluginConfigBtn'); | ||
| if (saveBtn) { | ||
| saveBtn.addEventListener('click', async () => { | ||
| const form = document.getElementById('pluginConfigForm') as HTMLFormElement; | ||
| const container = document.getElementById('configFieldsContainer'); | ||
| if (!form || !container) return; | ||
|
|
||
| const nameInput = form.querySelector<HTMLInputElement>('input[name="name"]'); | ||
| const pluginName = nameInput?.value; | ||
| const csrfToken = getCsrfToken(); | ||
| const configData: Record<string, any> = {}; | ||
|
|
||
| const inputs = container.querySelectorAll<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>( | ||
| 'input, select, textarea', | ||
| ); | ||
| inputs.forEach((input) => { | ||
| const nameAttr = input.getAttribute('name'); | ||
| if (nameAttr && nameAttr.startsWith('config[')) { | ||
| const key = nameAttr.substring(7, nameAttr.length - 1); | ||
| if (input instanceof HTMLInputElement && input.type === 'checkbox') { | ||
| configData[key] = input.checked; | ||
| } else if (input instanceof HTMLInputElement && input.type === 'number') { | ||
| configData[key] = input.value.includes('.') ? parseFloat(input.value) : parseInt(input.value, 10); | ||
| if (isNaN(configData[key])) { | ||
| configData[key] = 0; | ||
| } | ||
| } else { | ||
| configData[key] = input.value; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| if (!pluginName) return; | ||
|
|
||
| try { | ||
| const result = await savePluginConfig(pluginName, configData, csrfToken); | ||
|
|
||
| if (result.success) { | ||
| pushNotification(Translator.translate('msgPluginConfigSuccess')); | ||
| window.location.reload(); | ||
| } else { | ||
| pushErrorNotification( | ||
| Translator.translate('msgPluginConfigError') + ' ' + (result.message || Translator.translate('msgUnknownError')), | ||
| ); | ||
| } | ||
| } catch (error: any) { | ||
| console.error('Error saving config:', error); | ||
| pushErrorNotification(error.message || Translator.translate('msgUnknownError')); | ||
| } | ||
| }); | ||
| } | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File header data is not correct