2016-05-18 21:43:59 +02:00
|
|
|
<?php
|
2017-03-08 19:59:00 +01:00
|
|
|
|
|
|
|
use Shaarli\Config\Exception\PluginConfigOrderException;
|
2020-07-26 14:43:10 +02:00
|
|
|
use Shaarli\Plugin\PluginManager;
|
2017-03-03 23:06:12 +01:00
|
|
|
|
2016-05-18 21:43:59 +02:00
|
|
|
/**
|
2016-05-18 21:48:24 +02:00
|
|
|
* Plugin configuration helper functions.
|
|
|
|
*
|
|
|
|
* Note: no access to configuration files here.
|
2016-05-18 21:43:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process plugin administration form data and save it in an array.
|
|
|
|
*
|
|
|
|
* @param array $formData Data sent by the plugin admin form.
|
|
|
|
*
|
|
|
|
* @return array New list of enabled plugin, ordered.
|
|
|
|
*
|
|
|
|
* @throws PluginConfigOrderException Plugins can't be sorted because their order is invalid.
|
|
|
|
*/
|
|
|
|
function save_plugin_config($formData)
|
|
|
|
{
|
2020-07-26 14:43:10 +02:00
|
|
|
// We can only save existing plugins
|
|
|
|
$directories = str_replace(
|
|
|
|
PluginManager::$PLUGINS_PATH . '/',
|
|
|
|
'',
|
|
|
|
glob(PluginManager::$PLUGINS_PATH . '/*')
|
|
|
|
);
|
|
|
|
$formData = array_filter(
|
|
|
|
$formData,
|
|
|
|
function ($value, string $key) use ($directories) {
|
|
|
|
return startsWith($key, 'order') || in_array($key, $directories);
|
|
|
|
},
|
|
|
|
ARRAY_FILTER_USE_BOTH
|
|
|
|
);
|
|
|
|
|
2016-05-18 21:43:59 +02:00
|
|
|
// Make sure there are no duplicates in orders.
|
|
|
|
if (!validate_plugin_order($formData)) {
|
|
|
|
throw new PluginConfigOrderException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$plugins = array();
|
|
|
|
$newEnabledPlugins = array();
|
|
|
|
foreach ($formData as $key => $data) {
|
|
|
|
if (startsWith($key, 'order')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no order, it means a disabled plugin has been enabled.
|
|
|
|
if (isset($formData['order_' . $key])) {
|
|
|
|
$plugins[(int) $formData['order_' . $key]] = $key;
|
2018-10-13 00:19:03 +02:00
|
|
|
} else {
|
2016-05-18 21:43:59 +02:00
|
|
|
$newEnabledPlugins[] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New enabled plugins will be added at the end of order.
|
|
|
|
$plugins = array_merge($plugins, $newEnabledPlugins);
|
|
|
|
|
|
|
|
// Sort plugins by order.
|
|
|
|
if (!ksort($plugins)) {
|
|
|
|
throw new PluginConfigOrderException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$finalPlugins = array();
|
|
|
|
// Make plugins order continuous.
|
|
|
|
foreach ($plugins as $plugin) {
|
|
|
|
$finalPlugins[] = $plugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $finalPlugins;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate plugin array submitted.
|
|
|
|
* Will fail if there is duplicate orders value.
|
|
|
|
*
|
|
|
|
* @param array $formData Data from submitted form.
|
|
|
|
*
|
|
|
|
* @return bool true if ok, false otherwise.
|
|
|
|
*/
|
|
|
|
function validate_plugin_order($formData)
|
|
|
|
{
|
|
|
|
$orders = array();
|
|
|
|
foreach ($formData as $key => $value) {
|
|
|
|
// No duplicate order allowed.
|
2020-07-26 14:43:10 +02:00
|
|
|
if (in_array($value, $orders, true)) {
|
2016-05-18 21:43:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startsWith($key, 'order')) {
|
|
|
|
$orders[] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-02 11:02:20 +02:00
|
|
|
* Affect plugin parameters values from the ConfigManager into plugins array.
|
2016-05-18 21:43:59 +02:00
|
|
|
*
|
2016-08-02 11:02:20 +02:00
|
|
|
* @param mixed $plugins Plugins array:
|
|
|
|
* $plugins[<plugin_name>]['parameters'][<param_name>] = [
|
|
|
|
* 'value' => <value>,
|
|
|
|
* 'desc' => <description>
|
|
|
|
* ]
|
2016-05-18 21:43:59 +02:00
|
|
|
* @param mixed $conf Plugins configuration.
|
|
|
|
*
|
|
|
|
* @return mixed Updated $plugins array.
|
|
|
|
*/
|
|
|
|
function load_plugin_parameter_values($plugins, $conf)
|
|
|
|
{
|
|
|
|
$out = $plugins;
|
|
|
|
foreach ($plugins as $name => $plugin) {
|
|
|
|
if (empty($plugin['parameters'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($plugin['parameters'] as $key => $param) {
|
|
|
|
if (!empty($conf[$key])) {
|
2016-08-02 11:02:20 +02:00
|
|
|
$out[$name]['parameters'][$key]['value'] = $conf[$key];
|
2016-05-18 21:43:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|