FOSSology  4.4.0
Open Source License Compliance by Open Source Software
common-plugin.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2012 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: LGPL-2.1-only
6 */
7 
9 
18 global $Plugins;
19 $Plugins = array();
20 
32 function plugin_cmp($a, $b)
33 {
34  /* Sort by plugin version only when the name is the same */
35  if (0 == strcmp($a->Name, $b->Name)) {
36  /* Sort by plugin version (descending order) */
37  $rc = strcmp($a->Version, $b->Version);
38  if ($rc != 0) {
39  return (- $rc);
40  }
41  }
42 
43  /* Sort by dependencies. */
44  /* check if $a is a dependency for $b */
45  // print "BEGIN Comparing $a->Name with $b->Name\n";
46  foreach ($a->Dependency as $val) {
47  // print "Comparing $a->Name :: $val with $b->Name\n";
48  if ($val == $b->Name) {
49  return (1);
50  }
51  }
52  /* check if $b is a dependency for $a */
53  foreach ($b->Dependency as $val) {
54  // print "Comparing $b->Name :: $val with $a->Name\n";
55  if ($val == $a->Name) {
56  return (- 1);
57  }
58  }
59  // print "STILL Comparing $a->Name with $b->Name\n";
60 
61  /* If same dependencies, then sort by plugin level (highest comes first) */
62  if ($a->PluginLevel > $b->PluginLevel) {
63  return (- 1);
64  } elseif ($a->PluginLevel < $b->PluginLevel) {
65  return (1);
66  }
67 
68  /* Nothing else to sort by -- sort by number of dependencies */
69  $rc = count($a->Dependency) - count($b->Dependency);
70  return ($rc);
71 } // plugin_cmp()
72 
79 function plugin_disable($Level)
80 {
82  global $Plugins;
83 
84  /* Disable all plugins with >= $Level access */
85  //echo "<pre>COMP: starting to disable plugins\n</pre>";
86  $LoginFlag = empty($_SESSION['User']);
87  foreach ($Plugins as $pluginName => &$P) {
88  if ($P->State == PLUGIN_STATE_INVALID) {
89  // echo "<pre>COMP: Plugin $P->Name is in INVALID state\n</pre>";
90  continue;
91  }
92  if ($P->DBaccess > $Level) {
93  // echo "<pre>COMP: Going to disable $P->Name\n</pre>";
94  // echo "<pre>COMP: disabling plugins with $P->DBaccess >=
95  // $Level\n</pre>";
96  $P->unInstall();
97  unset($Plugins[$pluginName]);
98  }
99  unset($P);
100  }
101 } // plugin_disable
102 
107 function plugin_sort()
108 {
109  global $Plugins;
110 
111  /* Ideally, I would like to use usort. However, there are
112  dependency issues. Specifically: usort only works where there are
113  direct comparisons. It does not work with indirect dependencies.
114  For example:
115  A depends on B (A->B).
116  B depends on C (B->C).
117  If I just use usort, then C may be sorted AFTER A since there is
118  no explicit link from A->C. The array (B,A,C) is a possible usort
119  return, and it is wrong.
120  Before I can sort, we must fill out the dependency arrays.
121  */
122 
123  /* for each plugin, store the dependencies in a matrix */
124  $DepArray = array();
125  foreach ($Plugins as &$P) {
126  if (empty($P->Dependency[0])) {
127  continue; // ignore no dependencies
128  }
129  $DepArray[$P->Name] = array();
130  $D = &$DepArray[$P->Name];
131  for ($j = 0; $j < count($P->Dependency); $j ++) {
132  $D[$P->Dependency[$j]] = $P->PluginLevel;
133  }
134  unset($P);
135  }
136 
137  /* Now iterate through the array.
138  This converts implied dependencies into direct dependencies. */
139  foreach ($DepArray as $A => $a) {
140  $Aa = &$DepArray[$A];
141  /*
142  * Find every element that depends on this element and merge the
143  * dependency lists
144  */
145  foreach ($DepArray as $B => $b) {
146  $Bb = $DepArray[$B];
147  if (! empty($Bb[$A])) {
148  /* merge in the entire list */
149  $DepArray[$B] = array_merge($Aa, $Bb);
150  }
151  }
152  }
153 
154  /* Finally: Put the direct dependencies back into the structures */
155  foreach ($Plugins as &$P) {
156  if (empty($P->Dependency[0])) {
157  continue; // ignore no dependencies
158  }
159  $P->Dependency = array_keys($DepArray[$P->Name]);
160  unset($P);
161  }
162 
163  /* Now it is safe to sort */
164  uasort($Plugins, 'plugin_cmp');
165 } // plugin_sort()
166 
175 function plugin_find_id($pluginName)
176 {
179  global $Plugins;
180 
181  if (array_key_exists($pluginName, $Plugins)) {
182  $plugin = $Plugins[$pluginName];
183  return $plugin->State === PLUGIN_STATE_READY ? $pluginName : - 1;
184  }
185 
186  return -1;
187 }
188 
197 function plugin_find($pluginName)
198 {
199  global $Plugins;
200  return array_key_exists($pluginName, $Plugins) ? $Plugins[$pluginName] : null;
201 }
202 
210 function plugin_preinstall()
211 {
213  global $Plugins;
214 
215  plugin_sort();
216 
217  foreach (array_keys($Plugins) as $pluginName) {
218  if (array_key_exists($pluginName, $Plugins)) {
219  $Plugins[$pluginName]->preInstall();
220  }
221  }
222 }
223 
227 function plugin_postinstall()
228 {
230  global $Plugins;
231 
232  foreach ($Plugins as &$plugin) {
233  $plugin->postInstall();
234  }
235 }
236 
240 function plugin_load()
241 {
242  global $SYSCONFDIR;
243 
244  $ModsEnabledDir = "$SYSCONFDIR/mods-enabled";
245 
246  /* Open $ModsEnabledDir and include all the php files found in the ui/ subdirectory */
247 
248  if (is_dir($ModsEnabledDir)) {
249  foreach (glob("$ModsEnabledDir/*") as $ModDirPath) {
250  foreach (array(
251  "/ui",
252  ""
253  ) as $subdir) {
254  $targetPath = $ModDirPath . $subdir;
255 
256  if (is_dir($targetPath)) {
257  foreach (glob("$targetPath/*.php") as $phpFile) {
258  if (! strstr($phpFile, 'ndex.php')) {
259  include_once ("$phpFile");
260  }
261  }
262  break;
263  }
264  }
265  }
266  }
267 }
268 
272 function plugin_unload()
273 {
275  global $Plugins;
276 
277  foreach ($Plugins as $key => $plugin) {
278  if ($key == - 1) {
279  break;
280  }
281  if (empty($plugin)) {
282  continue;
283  }
284 
285  $plugin->unInstall();
286  }
287 } // plugin_unload()
288 
294 function register_plugin(Plugin $plugin)
295 {
297  global $Plugins;
298 
299  $name = $plugin->getName();
300 
301  if (empty($name)) {
302  throw new \Exception("cannot create module without name");
303  }
304 
305  if (array_key_exists($name, $Plugins)) {
306  throw new \Exception("duplicate definition of plugin with name $name");
307  }
308 
309  $Plugins[$name] = $plugin;
310 }
311 
318 function getStringRepresentation($vars, $classname)
319 {
320  $output = $classname . " {\n";
321  foreach ($vars as $name => $value) {
322  if (! is_object($value)) {
323  $representation = print_r($value, true);
324  $lines = explode("\n", $representation);
325  $lines = array_map(function ($line){
326  return " " . $line;
327  }, $lines);
328  $representation = trim(implode("\n", $lines));
329 
330  $output .= " $name: " . $representation . "\n";
331  }
332  }
333  $output .= "}\n";
334  return $output;
335 }
global $Plugins
Global plugins array.
plugin_cmp($a, $b)
Sort compare function.
plugin_load()
Load every module ui found in mods-enabled.
getStringRepresentation($vars, $classname)
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
plugin_sort()
Sort the global $Plugins by dependencies. This way plugins get loaded in the correct order.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690