1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class pluglist
24 {
25 // The id of the last registered plugin id
26 var $index= 0;
28 var $config= NULL;
29 var $ui= NULL;
31 // Contains a list of the paths for all registered plugins.
32 var $dirlist= array();
34 // Seems to be used in the help menu to generate an overview.
35 var $headlines = array();
36 // Remember stuff in here.
37 var $silly_cache= array();
39 // The pluglist keeps track of all registered plugins.
40 // (Registered plugins are those we are able to access)
41 var $pluginList = array();
43 // Some cache vars to avoid regenration of the menus.
44 var $pathMenu = "";
45 var $menu= "";
46 var $iconmenu= "";
48 // Classes plInfo list
49 var $info = array();
51 function pluglist(&$config, &$ui)
52 {
53 $this->ui= &$ui;
54 $this->config= &$config;
55 $this->loadPluginList();
56 }
58 function loadPluginList()
59 {
60 $this->pluginList = array();
62 /* Fill info part of pluglist */
63 $classes= get_declared_classes();
64 foreach ($classes as $cname){
65 $cmethods = get_class_methods($cname);
66 if (in_array_ics('plInfo',$cmethods)){
67 $this->info[$cname]= call_user_func(array($cname, 'plInfo'));
68 }
69 }
71 /* Provide field for 'all' */
72 $this->info['all']= array();
73 $this->info['all']['plProvidedAcls']= array();
74 $this->info['all']['plDescription']= _("All objects in this category");
75 $this->info['all']['plSelfModify']= FALSE;
77 // First load the menu plugins and try to register them in the pluglist
78 // if this fails for some reason, then remove the plugin from the menu.
79 if(isset($this->config->data['MENU'])){
80 foreach($this->config->data['MENU'] as $section => $plugins){
81 foreach($plugins as $id => $plug){
82 if(!$this->registerPlugin($plug)){
83 unset($this->config->data['MENU'][$section][$id]);
84 }
85 }
86 }
87 }
89 // Now load the path menu and try to register the plugins in the pluglist
90 // if this fails for some reason, then remove the plugin from the menu.
91 if(isset($this->config->data['PATHMENU'])){
92 foreach($this->config->data['PATHMENU'] as $id => $plugin){
93 if(!$this->registerPlugin($plugin)){
94 unset($this->config->data['PATHMENU'][$id]);
95 }
96 }
97 }
99 if(!session::is_set('maxC')){
100 session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
101 }
103 // Now generate menu - usually they are cached
104 $this->gen_menu();
105 $this->show_iconmenu();
106 $this->genPathMenu();
107 }
110 /*! \brief Tries to register a plugin in the pluglist
111 * Checks existence and ACL for the given plugin.
112 * Returns true in case of success else false.
113 */
114 function registerPlugin($plug)
115 {
116 global $class_mapping;
118 if (!isset($plug['CLASS'])){
119 msg_dialog::display(
120 _("Configuration error"),
121 _("The configuration format has changed. Please re-run setup!"),
122 FATAL_ERROR_DIALOG);
123 exit();
124 }
125 if (!plugin_available($plug['CLASS'])){
126 return(FALSE);
127 }
128 if (!$this->check_access($plug['ACL'])){
129 return(FALSE);
130 }
131 $this->dirlist[$this->index] = dirname($class_mapping[$plug['CLASS']]);
132 $this->pluginList[$this->index] = $plug['CLASS'];
133 $this->index++;
134 return(TRUE);
135 }
138 /*! \brief Check whether we are allowed to modify the given acl or not..
139 * This function is used to check which plugins are visible.
140 *
141 * @param The acl tag to check for, eg. "users/user:self", "systems", ...
142 * @return Boolean TRUE on success else FALSE
143 */
144 function check_access($aclname)
145 {
146 if (isset($this->silly_cache[$aclname])) {
147 return $this->silly_cache[$aclname];
148 }
150 // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
151 $acls_to_check = array();
152 if(preg_match("/,/",$aclname)){
153 $acls_to_check = explode(",",$aclname);
154 }else{
155 $acls_to_check = array($aclname);
156 }
158 foreach($acls_to_check as $acl_to_check){
159 $acl_to_check = trim($acl_to_check);
161 /* Check if the given acl tag is only valid for self acl entries
162 * <plugin acl="users/user:self" class="user"...
163 */
164 if(preg_match("/:self$/",$acl_to_check)){
165 $acl_to_check = preg_replace("/:self$/","",$acl_to_check);
166 if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
167 $this->silly_cache[$aclname]= TRUE;
168 return(TRUE);
169 }
170 $this->silly_cache[$aclname]= FALSE;
171 return(FALSE);
172 }else{
174 // No self acls. Check if we have any acls for the given ACL type
175 $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
176 if(count($deps)){
177 $this->silly_cache[$aclname]= TRUE;
178 return TRUE;
179 }
180 }
181 }
183 $this->silly_cache[$aclname]= FALSE;
184 return (FALSE);
185 }
189 /*! \brief Generate the GOsa Main-Menu here (The menu on the left),
190 * this usually only done once during login.
191 * -----------------------------------------------------------------
192 * Do NOT add style changes here manually, use the style.css or
193 * if you prefer create your own theme!!
194 * -----------------------------------------------------------------
195 */
196 function gen_menu()
197 {
198 if ($this->menu == ""){
199 $cfg= $this->config->data['MENU'];
200 $menu = "\n <div class='navigation'>";
201 foreach ($cfg as $headline => $plug){
203 if(!count($plug)) continue;
205 $menu.= "\n <div class='menu'>";
206 $menu.= "\n <ul>";
207 $menu.= "\n <li class='menu-header'>"._($headline)."</li>";
208 $id = 0;
209 foreach ($plug as $info){
211 // Used to detect the last element in the menu
212 $id ++;
214 list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
215 $class= "";
216 if($id == count($plug)) $class=" class='menu-last' ";
217 $menu .= "\n <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
218 }
219 $menu.= "\n </ul>";
220 $menu.= "\n <div class='v-spacer'></div>\n";
221 $menu.= "\n </div>\n";
222 $menu.= "\n <div class='v-spacer'></div>\n";
223 }
224 $menu.= "\n </div>\n";
225 $this->menu = $menu;
227 // Add javascript method to print out warning messages while leaving an unsaved form.
228 // We do it in here to get the string translated.
229 $this->menu .=
230 "\n <script language='javascript' type='text/javascript'>".
231 "\n function openPlugin(id){".
232 "\n return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\",".
233 "\n \"main.php?plug=\" + id + \"&reset=1\");".
234 "\n }".
235 "\n </script>\n";
236 }
238 // Use javascript to mark the currently selected plugin.
239 $menu = $this->menu;
240 if(isset($_GET['plug'])){
241 $menu.=
242 "\n <script language='javascript' type='text/javascript'>".
243 "\n if($('plugMenuId_".$_GET['plug']."')){".
244 "\n $('plugMenuId_".$_GET['plug']."').className= 'current'".
245 "\n }".
246 "\n </script>\n";
247 }
249 // Return the generated/cached gosa menu.
250 return ($menu);
251 }
254 /*! \brief Generate the GOsa Icon-Menu here, this usually only done once during
255 * login.
256 * -----------------------------------------------------------------
257 * Do NOT add style changes here manually, use the style.css or
258 * if you prefer create your own theme!!
259 * -----------------------------------------------------------------
260 */
261 function show_iconmenu()
262 {
263 $add_hr =FALSE;
264 $this->iconmenu = "";
265 if ($this->iconmenu == ""){
266 $cfg= $this->config->data['MENU'];
267 foreach ($cfg as $headline => $plug){
268 $col= 0;
270 $this->iconmenu .= "\n <div class='clear'></div>";
271 if($add_hr){
272 $add_hr = FALSE;
273 $this->iconmenu .= "\n <hr>";
274 }
275 $this->iconmenu .= "\n <h3 class='icon-menu-title'>". _($headline)."</h3>";
277 foreach ($plug as $info){
279 // Get Plugin info
280 list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
282 // Add a seperating row
283 if (($col % 4) == 0){
284 $this->iconmenu .= "\n <div class='clear'></div>";
285 }
287 $this->iconmenu.= "\n <div class='icon-menu-item' style='width: 25%;' onclick='openPlugin({$index})'>";
288 $this->iconmenu.= "\n ".image($icon);
289 $this->iconmenu.= "\n <div class='dsc'>";
290 $this->iconmenu.= "\n <h1>{$title}</h1>";
291 $this->iconmenu.= "\n <p>{$desc}</p>";
292 $this->iconmenu.= "\n </div>";
293 $this->iconmenu.= "\n </div>";
294 $col++ ;
295 }
296 $add_hr = TRUE;
297 }
298 }
299 return ($this->iconmenu);
300 }
303 /*! \brieg Generates and the path menu (the one on the upper right) and keeps
304 * the generated HTML content, so we are not forced to generate it on every
305 * page request.
306 * (See <pathMenu> of your gosa.conf)
307 */
308 function genPathMenu()
309 {
310 if(empty($this->pathMenu)){
311 $this->pathMenu =
312 "\n <div class='plugin-path'>".
313 "\n <ul class='path-navigation'>".
314 "\n <li class='left right-border' onClick=\"openPlugin('');\">".
315 "\n <div class='nav-home'></div>".
316 "\n </li>".
317 "\n <li class='left'>Welcome to GOsa</li>";
319 // Check if we've at least one entry defined ih the pathmenu
320 if(isset($this->config->data['PATHMENU'])){
321 $cfg= &$this->config->data['PATHMENU'];
322 $rcfg = array_reverse($cfg);
323 foreach($rcfg as $id => $plug){
324 list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
325 $this->pathMenu.= "\n <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
326 }
327 }
328 $this->pathMenu.= "\n </ul>";
329 $this->pathMenu.= "\n </div>";
330 }
331 return($this->pathMenu);
332 }
335 /*! \brief Returns additional info for a given class name, like
336 * plugin-icon, title, description and the index of the element
337 in the pluglist which uses this class.
338 */
339 function getPlugData($class)
340 {
341 global $class_mapping;
342 $vars= get_class_vars($class);
343 $plHeadline= _($vars['plHeadline']);
344 $plDescription= _($vars['plDescription']);
345 $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
346 $index= $this->get_index($class);
348 /* Check if class is available. If the class doesn't exists display error symbol
349 * to avoid that a user clicks on a non existing plugin
350 */
351 if(!$vars){
352 $plHeadline = $plDescription = _("Unknown");
353 $plIcon = "error.png";
354 $index = '';
355 }
357 // Detect the correct position of the plugin icon
358 if(!preg_match("/\//",$plIcon)){
359 $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1',
360 $class_mapping[$class])."/images/$plIcon");
361 }else{
362 $image = $plIcon;
363 }
364 return(array($index, $plHeadline, $plDescription, $image));
365 }
368 /*! \brief Returns the installation path of a plugin.
369 * e.g. '../plugins/admin/mimetypes'
370 */
371 function get_path($index)
372 {
373 if(!isset($this->dirlist[$index])){
374 return ("");
375 }
376 return ("../".$this->dirlist[$index]);
377 }
380 /*! \brief Returns the plugins id for a given class.
381 */
382 function get_index($class)
383 {
384 return (array_search($class, $this->pluginList));
385 }
388 /*! \brief This function checks if we are allowed to view the plugin with the given id
389 *
390 * @param $plug_id Integer The ID of the plugin.
391 * @return Boolean TRUE if we are allowed to view the plugin else FASLE
392 */
393 function plugin_access_allowed($plug_id)
394 {
395 return(isset($this->pluginList[$plug_id]));
396 }
399 /*! \brief Force the menu to be recreated
400 */
401 function reset_menus()
402 {
403 $this->menu = "";
404 $this->iconmenu ="";
405 }
408 /*! \brief Generates an array containing plugin names (headlines) and theirs ids.
409 * This is just used in the helpviewer.php
410 */
411 function gen_headlines()
412 {
413 $ret = array();
414 if(count($this->headlines) == 0){
415 foreach($this->config->data['MENU'] as $headline => $plugins){
416 foreach( $plugins as $id => $plug){
417 if (plugin_available($plug['CLASS'])){
418 $attrs = (get_class_vars($plug['CLASS']));
419 $ret[$id]['HEADLINE'] = $headline;
420 $ret[$id]['NAME'] = $attrs['plHeadline'];
421 }
422 }
423 }
424 $this->headlines = $ret;
425 }
426 return($this->headlines);
427 }
428 }
429 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
430 ?>