FusionDirectory
 All Data Structures Files Functions Variables
class_pluglist.inc
Go to the documentation of this file.
1 <?php
2 
3 /*
4  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
5  Copyright (C) 2003-2010 Cajus Pollmeier
6  Copyright (C) 2011-2016 FusionDirectory
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22 
34 class pluglist {
35  var $menu = "";
36  var $iconmenu = "";
37  var $current = "";
38 
43  var $info = array();
44 
48  var $dirlist = array();
49 
53  var $allowed_plugins = array();
54  var $silly_cache = array();
55 
59  function __construct()
60  {
61  global $class_mapping;
62 
63  /* Fill info part of pluglist */
64  $classes = get_declared_classes();
65  /* To avoid plugins changing index when reloading */
66  sort($classes);
67 
68  $index = 0;
69  $depends_infos = array();
70  $conflicts_infos = array();
71  $foreign_refs = array();
72  foreach ($classes as $cname) {
73  $cmethods = get_class_methods($cname);
74  if (in_array_ics('plInfo', $cmethods)) {
75  $infos = call_user_func(array($cname, 'plInfo'));
76  if (is_subclass_of($cname, 'simpleService')) {
77  $infos['plSelfModify'] = FALSE;
78  /* services are not part of any objectType */
79  unset($infos['plObjectType']);
80  $infos['plCategory'] = array('server');
81  } else {
82  if (!isset($infos['plSelfModify'])) {
83  $infos['plSelfModify'] = FALSE;
84  }
85  }
86  if (isset($class_mapping[$cname])) {
87  $infos['PATH'] = dirname($class_mapping[$cname]);
88  }
89  if (isset($infos['plDepends'])) {
90  $depends_infos[] = $cname;
91  }
92  if (isset($infos['plConflicts'])) {
93  $conflicts_infos[] = $cname;
94  }
95  if (isset($infos['plForeignKeys'])) {
96  foreach ($infos['plForeignKeys'] as $ofield => &$pfks) {
97  if (!is_array($pfks)) {
98  $pfks = array($pfks);
99  }
100  if (!is_array($pfks[0])) {
101  $pfks = array($pfks);
102  }
103  foreach ($pfks as &$pfk) {
104  $class = $pfk[0];
105  if (isset($pfk[1])) {
106  $field = $pfk[1];
107  } else {
108  $field = 'dn';
109  $pfk[1] = $field;
110  }
111  $filter = NULL;
112  if (isset($pfk[2])) {
113  $filter = $pfk[2];
114  }
115  if ($filter === NULL) {
116  $filter = "$ofield=%oldvalue%";
117  }
118  $pfk[2] = $filter;
119  if (!isset($foreign_refs[$class])) {
120  $foreign_refs[$class] = array();
121  }
122  if (!isset($foreign_refs[$class][$field])) {
123  $foreign_refs[$class][$field] = array();
124  }
125  $foreign_refs[$class][$field][] = array($cname, $ofield, $filter);
126  }
127  unset($pfk);
128  }
129  unset($pfks);
130  } else {
131  $infos['plForeignKeys'] = array();
132  }
133  if (!isset($infos['plProvidedAcls'])) {
134  $infos['plProvidedAcls'] = array();
135  }
136  if (!isset($infos['plCategory'])) {
137  $infos['plCategory'] = array();
138  }
139  if (!isset($infos['plTitle']) && isset($infos['plShortName'])) {
140  $infos['plTitle'] = $infos['plShortName'];
141  }
142  $infos['plForeignRefs'] = array();
143  $infos['INDEX'] = $index;
144  $this->info[$cname] = $infos;
145  $this->dirlist[$index++] = $cname;
146  }
147  }
148 
149  foreach ($depends_infos as $cname) {
150  foreach ($this->info[$cname]['plDepends'] as $depend) {
151  if (isset($this->info[$depend])) {
152  if (isset($this->info[$depend]['plDepending'])) {
153  $this->info[$depend]['plDepending'][] = $cname;
154  } else {
155  $this->info[$depend]['plDepending'] = array($cname);
156  }
157  } else {
158  trigger_error("$cname depends of the inexisting plugin $depend");
159  }
160  }
161  }
162  foreach ($conflicts_infos as $cname) {
163  foreach ($this->info[$cname]['plConflicts'] as $conflict) {
164  if (isset($this->info[$conflict])) {
165  if (isset($this->info[$conflict]['plConflicts'])) {
166  if (!in_array($cname, $this->info[$conflict]['plConflicts'])) {
167  $this->info[$conflict]['plConflicts'][] = $cname;
168  }
169  } else {
170  $this->info[$conflict]['plConflicts'] = array($cname);
171  }
172  }
173  }
174  }
175  foreach ($foreign_refs as $cname => $refs) {
176  if (isset($this->info[$cname])) {
177  $this->info[$cname]['plForeignRefs'] = $refs;
178  }
179  }
180 
181  /* Provide field for 'all' */
182  $this->info['all'] = array();
183 
184  $this->info['all']['plProvidedAcls'] = array();
185  $this->info['all']['plDescription'] = _("All objects in this category");
186  $this->info['all']['plSelfModify'] = FALSE;
187 
188  uasort($this->info,
189  function ($a, $b)
190  {
191  if (isset($a['plPriority']) && isset($b['plPriority'])) {
192  if ($a['plPriority'] == $b['plPriority']) {
193  return 0;
194  } elseif ($a['plPriority'] < $b['plPriority']) {
195  return -1;
196  } else {
197  return 1;
198  }
199  } elseif (isset($a['plPriority'])) {
200  return -1;
201  } elseif (isset($b['plPriority'])) {
202  return 1;
203  } else {
204  return 0;
205  }
206  }
207  );
208  }
209 
219  function check_access($infos)
220  {
221  global $ui;
222 
223  if (isset($infos['CLASS']) && $ui->isBlacklisted($infos['CLASS'])) {
224  return FALSE;
225  }
226 
227  if (!isset($infos['ACL'])) {
228  return TRUE;
229  }
230 
231  $aclname = $infos['ACL'];
232 
233  if (isset($this->silly_cache[$aclname])) {
234  return $this->silly_cache[$aclname];
235  }
236 
237  /* Split given acl string into an array.
238  e.g. "user,systems" => array("user","systems");
239  */
240  $acls_to_check = array();
241  if (preg_match("/,/", $aclname)) {
242  $acls_to_check = explode(",", $aclname);
243  } else {
244  $acls_to_check = array($aclname);
245  }
246 
247  foreach ($acls_to_check as $acl_to_check) {
248  $acl_to_check = trim($acl_to_check);
249 
250  /* Check if the given acl tag is only valid for self acl entries
251  <plugin acl="user/user:self" class="user"... */
252  if (preg_match("/:self$/", $acl_to_check)) {
253  $acl_to_check = preg_replace("/:self$/", "", $acl_to_check);
254  if (strpos($acl_to_check, '/')) {
255  if ($ui->get_permissions($ui->dn, $acl_to_check, "") != "") {
256  $this->silly_cache[$aclname] = TRUE;
257  return TRUE;
258  }
259  } else {
260  if ($ui->get_category_permissions($ui->dn, $acl_to_check) != '') {
261  $this->silly_cache[$aclname] = TRUE;
262  return TRUE;
263  }
264  }
265  } else {
266 
267  /* No self acls. Check if we have any acls for the given ACL type */
268  $deps = $ui->get_module_departments($acl_to_check, TRUE);
269  if (count($deps)) {
270  $this->silly_cache[$aclname] = TRUE;
271  return TRUE;
272  }
273  }
274  }
275 
276  $this->silly_cache[$aclname] = FALSE;
277  return FALSE;
278  }
279 
283  function get_infos($cname)
284  {
285  $plHeadline = FALSE;
286  $plIcon = FALSE;
287  $plDescription = FALSE;
288  $index = $this->get_index($cname);
289  $href = "main.php?plug=$index&amp;reset=1";
290  if (isset($this->info[$cname])) {
291  if (isset($this->info[$cname]['plShortName'])) {
292  $plHeadline = $this->info[$cname]['plShortName'];
293  }
294  if (isset($this->info[$cname]['plIcon'])) {
295  $plIcon = $this->info[$cname]['plIcon'];
296  }
297  if (isset($this->info[$cname]['plDescription'])) {
298  $plDescription = $this->info[$cname]['plDescription'];
299  }
300  if ($plHeadline && $plIcon && $plDescription) {
301  return array($plHeadline,$plDescription,$href,$plIcon);
302  }
303  }
304  $vars = get_class_vars($cname);
305  if ($vars) {
306  if (!$plHeadline && isset($vars['plHeadline'])) {
307  $plHeadline = _($vars['plHeadline']);
308  }
309  if (!$plDescription && isset($vars['plDescription'])) {
310  $plDescription = _($vars['plDescription']);
311  }
312  if (!$plIcon && isset($vars['plIcon'])) {
313  $plIcon = $vars['plIcon'];
314  }
315  } else {
316  die('Unknown class '.$cname);
317  }
318  if (!$plIcon) {
319  $plIcon = "icon.png";
320  }
321  return array($plHeadline,$plDescription,$href,$plIcon);
322  }
323 
327  function gen_menu()
328  {
329  global $config;
330  if ($this->menu == "") {
331  $this->menu = '<ul class="menu">'."\n";
332  /* Parse headlines */
333  foreach ($config->data['SECTIONS'] as $section => $section_infos) {
334  $entries = '';
335 
336  /* Parse sub-plugins */
337  foreach ($config->data['MENU'][$section] as $info) {
338  if (!$this->check_access($info)) {
339  continue;
340  }
341  if (isset($info['CLASS']) && plugin_available($info['CLASS'])) {
342  $index = $this->get_index($info['CLASS']);
343  $this->allowed_plugins[$index] = $index;
344  list ($plHeadline, $plDescription, $href, ) = $this->get_infos($info['CLASS']);
345  $id = $info['CLASS'];
346  } elseif (!isset($info['CLASS'])) {
347  $plHeadline = $info['TITLE'];
348  $plDescription = $info['DESCRIPTION'];
349  $href = $info['LINK'];
350  $id = $info['NAME'];
351  } else {
352  continue;
353  }
354 
355  $entries .= '<li class="menuitem" id="menuitem_'.$id.'">';
356  $entries .= '<a href="'.$href.'" title="'.$plDescription.'">'.$plHeadline.'</a></li>'."\n";
357  }
358 
359  /* Append to menu */
360  if ($entries != "") {
361  $this->menu .= '<li><a>'.$section_infos['NAME']."</a>\n<ul>\n".$entries."\n</ul></li>\n";
362  }
363  }
364  $this->menu .= '</ul>'."\n";
365  }
366 
367  /* Add the menucurrent class to current plugin */
368  if (isset($_GET['plug'])) {
369  $plug = $_GET['plug'];
370  } else {
371  $plug = "NOTHING";
372  }
373  $lines = preg_split("/\n/", $this->menu);
374  foreach ($lines as &$line) {
375  if (preg_match('/'.preg_quote("main.php?plug=$plug&amp;reset=1",'/').'/', $line)) {
376  $line = preg_replace('/class="menuitem"/', 'class="menuitem menucurrent"', $line);
377  } elseif (preg_match('/class="menuitem menucurrent"/', $line)) {
378  $line = preg_replace('/class="menuitem menucurrent"/', 'class="menuitem"', $line);
379  }
380  }
381  unset($line);
382 
383  /* Write menu output */
384  $this->menu = join("\n", $lines);
385  }
386 
390  function show_iconmenu()
391  {
392  global $class_mapping, $config;
393  if ($this->iconmenu == "") {
394 
395  /* Parse headlines */
396  foreach ($config->data['SECTIONS'] as $section => $section_infos) {
397  $entries = '';
398  $menu = '';
399  $menu = '<div class="iconmenu-section"><h1 class="menuheader">';
400  $menu .= $section_infos['NAME']."</h1>\n";
401 
402  foreach ($config->data['MENU'][$section] as $info) {
403  if (!$this->check_access($info)) {
404  continue;
405  }
406  if (isset($info['CLASS']) && plugin_available($info['CLASS'])) {
407  /* Read information from class variable */
408  list ($plHeadline, $plDescription, $href, $plIcon) = $this->get_infos($info['CLASS']);
409  $id = $info['CLASS'];
410  } elseif (!isset($info['CLASS'])) {
411  $plHeadline = $info['TITLE'];
412  $plDescription = $info['DESCRIPTION'];
413  $href = $info['LINK'];
414  $plIcon = $info['ICONPATH'];
415  $id = $info['NAME'];
416  } else {
417  continue;
418  }
419 
420  /* Load icon */
421  if (isset($info['CLASS']) && !preg_match("/\//", $plIcon) && !preg_match("/^geticon/", $plIcon)) {
422  $image = get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', $class_mapping[$info['CLASS']])."/images/$plIcon");
423  } else {
424  $image = htmlentities($plIcon, ENT_COMPAT, 'UTF-8');
425  }
426 
427  $entries .= '<div class="iconmenu" id="menuitem_icon_'.$id.'" onClick=\'location.href="'.$href.'"\' title="'.$plDescription.'">';
428  $item = '<div class="imgcontainer"><img src="'.$image.'" alt="'.$plHeadline.' icon"/></div><span>&nbsp;'.$plHeadline.'</span>';
429  $entries .= $item."</div>\n";
430  }
431 
432  /* Append to menu */
433  if ($entries != "") {
434  $this->iconmenu .= $menu.$entries."</div>\n";
435  }
436  }
437  }
438 
439  /* Write menu output */
440  return $this->iconmenu;
441  }
442 
443  /*
444  * \brief Get the path of the index
445  *
446  * \param string $index The index which we want the path
447  */
448  function get_path($index)
449  {
450  if (!isset($this->dirlist[$index])) {
451  return "";
452  }
453  return "../".$this->info[$this->dirlist[$index]]['PATH'];
454  }
455 
456  /*
457  * \brief Search for plugin index (id), identify entry by path and class
458  *
459  * \param string $class The name of the class
460  */
461  function get_index($class)
462  {
463  /* Search for plugin index (id), identify entry by class */
464  if (isset($this->info[$class])) {
465  return $this->info[$class]['INDEX'];
466  }
467 
468  /* Nothing */
469  return 0;
470  }
471 
479  function plugin_access_allowed($plug_id)
480  {
481  return isset($this->allowed_plugins[$plug_id]);
482  }
483 
484  static function pluginInfos($cname)
485  {
486  $plist = session::global_get('plist');
487  if ($plist) {
488  return $plist->info[$cname];
489  } else {
490  trigger_error('plist not loaded yet');
491  }
492  }
493 }
494 ?>
show_iconmenu()
Show the menu icon.
in_array_ics($value, array $items)
Check if a value exists in an array (case-insensitive)
Definition: functions.inc:1657
This class contains all the function needed to make list of plugin and manage them.
get_template_path($filename= '', $plugin=FALSE, $path= '')
Return themed path for specified base file.
Definition: functions.inc:296
check_access($infos)
Check whether we are allowed to modify the given acl or not.
$info
The plInfo result for all plugin, using class as key. Contains the plugin index in 'INDEX' and the pa...
$allowed_plugins
List plugin indexes of all plugin that the user have acl for.
__construct()
List the plugins.
static global_get($name)
Accessor of a session var.
plugin_access_allowed($plug_id)
This function checks if we are allowed to view the plugin with the given id.
$dirlist
Using the plugin index as a key, the class of the plugin.
plugin_available($plugin)
Check if plugin is available.
Definition: functions.inc:146
get_infos($cname)
Get headline, description and icon of a plugin.
gen_menu()
Generate menu.