]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/config/ConfigPlugin.php
Apply PHP Code Beautifier on source code for linter automatic fixes
[github/shaarli/Shaarli.git] / application / config / ConfigPlugin.php
1 <?php
2
3 use Shaarli\Config\Exception\PluginConfigOrderException;
4 use Shaarli\Plugin\PluginManager;
5
6 /**
7 * Plugin configuration helper functions.
8 *
9 * Note: no access to configuration files here.
10 */
11
12 /**
13 * Process plugin administration form data and save it in an array.
14 *
15 * @param array $formData Data sent by the plugin admin form.
16 *
17 * @return array New list of enabled plugin, ordered.
18 *
19 * @throws PluginConfigOrderException Plugins can't be sorted because their order is invalid.
20 */
21 function save_plugin_config($formData)
22 {
23 // We can only save existing plugins
24 $directories = str_replace(
25 PluginManager::$PLUGINS_PATH . '/',
26 '',
27 glob(PluginManager::$PLUGINS_PATH . '/*')
28 );
29 $formData = array_filter(
30 $formData,
31 function ($value, string $key) use ($directories) {
32 return startsWith($key, 'order') || in_array($key, $directories);
33 },
34 ARRAY_FILTER_USE_BOTH
35 );
36
37 // Make sure there are no duplicates in orders.
38 if (!validate_plugin_order($formData)) {
39 throw new PluginConfigOrderException();
40 }
41
42 $plugins = [];
43 $newEnabledPlugins = [];
44 foreach ($formData as $key => $data) {
45 if (startsWith($key, 'order')) {
46 continue;
47 }
48
49 // If there is no order, it means a disabled plugin has been enabled.
50 if (isset($formData['order_' . $key])) {
51 $plugins[(int) $formData['order_' . $key]] = $key;
52 } else {
53 $newEnabledPlugins[] = $key;
54 }
55 }
56
57 // New enabled plugins will be added at the end of order.
58 $plugins = array_merge($plugins, $newEnabledPlugins);
59
60 // Sort plugins by order.
61 if (!ksort($plugins)) {
62 throw new PluginConfigOrderException();
63 }
64
65 $finalPlugins = [];
66 // Make plugins order continuous.
67 foreach ($plugins as $plugin) {
68 $finalPlugins[] = $plugin;
69 }
70
71 return $finalPlugins;
72 }
73
74 /**
75 * Validate plugin array submitted.
76 * Will fail if there is duplicate orders value.
77 *
78 * @param array $formData Data from submitted form.
79 *
80 * @return bool true if ok, false otherwise.
81 */
82 function validate_plugin_order($formData)
83 {
84 $orders = [];
85 foreach ($formData as $key => $value) {
86 // No duplicate order allowed.
87 if (in_array($value, $orders, true)) {
88 return false;
89 }
90
91 if (startsWith($key, 'order')) {
92 $orders[] = $value;
93 }
94 }
95
96 return true;
97 }
98
99 /**
100 * Affect plugin parameters values from the ConfigManager into plugins array.
101 *
102 * @param mixed $plugins Plugins array:
103 * $plugins[<plugin_name>]['parameters'][<param_name>] = [
104 * 'value' => <value>,
105 * 'desc' => <description>
106 * ]
107 * @param mixed $conf Plugins configuration.
108 *
109 * @return mixed Updated $plugins array.
110 */
111 function load_plugin_parameter_values($plugins, $conf)
112 {
113 $out = $plugins;
114 foreach ($plugins as $name => $plugin) {
115 if (empty($plugin['parameters'])) {
116 continue;
117 }
118
119 foreach ($plugin['parameters'] as $key => $param) {
120 if (!empty($conf[$key])) {
121 $out[$name]['parameters'][$key]['value'] = $conf[$key];
122 }
123 }
124 }
125
126 return $out;
127 }