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 var $index= 0;
26 var $config= NULL;
27 var $dirlist= array();
28 var $ui= NULL;
29 var $info= array();
30 var $headlines = array();
31 var $silly_cache= array();
33 var $pluginList = array();
35 var $pathMenu = "";
36 var $menu= "";
37 var $iconmenu= "";
39 function pluglist(&$config, &$ui)
40 {
41 $this->ui= &$ui;
42 $this->config= &$config;
44 // Reserve a special ACL will allows us to display plugins/addons whenever a user
45 // is able to login into gosa. E.g. some kind of welcome page.
46 $this->info['all']= array();
47 $this->info['all']['plProvidedAcls']= array();
48 $this->info['all']['plDescription']= _("All objects in this category");
49 $this->info['all']['plSelfModify']= FALSE;
51 $this->loadPluginList();
52 }
54 function loadPluginList()
55 {
56 $this->pluginList = array();
58 // Detect installed plugins and their configuration, to be able to restrict access later.
59 $classes= get_declared_classes();
60 foreach ($classes as $cname){
61 $cmethods = get_class_methods($cname);
62 if (in_array_ics('plInfo',$cmethods)){
63 $this->info[$cname]= call_user_func(array($cname, 'plInfo'));
64 }
65 }
67 // First load Menu Plugins
68 if(isset($this->config->data['MENU'])){
69 foreach($this->config->data['MENU'] as $section => $plugins){
70 foreach($plugins as $id => $plug){
71 if(!$this->registerPlugin($plug)){
72 unset($this->config->data['MENU'][$section][$id]);
73 }
74 }
75 }
76 }
78 // Now register pathMenu plugins
79 if(isset($this->config->data['PATHMENU'])){
80 foreach($this->config->data['PATHMENU'] as $id => $plugin){
81 if(!$this->registerPlugin($plugin)){
82 unset($this->config->data['PATHMENU'][$id]);
83 }
84 }
85 }
87 if(!session::is_set('maxC')){
88 session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
89 }
90 $this->gen_menu();
91 $this->show_iconmenu();
92 $this->genPathMenu();
93 }
96 function registerPlugin($plug)
97 {
98 global $class_mapping;
100 if (!isset($plug['CLASS'])){
101 msg_dialog::display(
102 _("Configuration error"),
103 _("The configuration format has changed. Please re-run setup!"),
104 FATAL_ERROR_DIALOG);
105 exit();
106 }
107 if (!plugin_available($plug['CLASS'])){
108 return(FALSE);
109 }
110 if (!$this->check_access($plug['ACL'])){
111 return(FALSE);
112 }
113 $this->dirlist[$this->index] = dirname($class_mapping[$plug['CLASS']]);
114 $this->pluginList[$this->index] = $plug['CLASS'];
115 $this->index++;
116 return(TRUE);
117 }
120 /*! \brief Check whether we are allowed to modify the given acl or not..
121 * This function is used to check which plugins are visible.
122 *
123 * @param The acl tag to check for, eg. "users/user:self", "systems", ...
124 * @return Boolean TRUE on success else FALSE
125 */
126 function check_access($aclname)
127 {
128 if (isset($this->silly_cache[$aclname])) {
129 return $this->silly_cache[$aclname];
130 }
132 // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
133 $acls_to_check = array();
134 if(preg_match("/,/",$aclname)){
135 $acls_to_check = explode(",",$aclname);
136 }else{
137 $acls_to_check = array($aclname);
138 }
140 foreach($acls_to_check as $acl_to_check){
141 $acl_to_check = trim($acl_to_check);
143 /* Check if the given acl tag is only valid for self acl entries
144 * <plugin acl="users/user:self" class="user"...
145 */
146 if(preg_match("/:self$/",$acl_to_check)){
147 $acl_to_check = preg_replace("/:self$/","",$acl_to_check);
148 if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
149 $this->silly_cache[$aclname]= TRUE;
150 return(TRUE);
151 }
152 $this->silly_cache[$aclname]= FALSE;
153 return(FALSE);
154 }else{
156 // No self acls. Check if we have any acls for the given ACL type
157 $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
158 if(count($deps)){
159 $this->silly_cache[$aclname]= TRUE;
160 return TRUE;
161 }
162 }
163 }
165 $this->silly_cache[$aclname]= FALSE;
166 return (FALSE);
167 }
171 /*! \brief Generate the GOsa Main-Menu here (The menu on the left),
172 * this usually only done once during login.
173 * -----------------------------------------------------------------
174 * Do NOT add style changes here manually, use the style.css or
175 * if you prefer create your own theme!!
176 * -----------------------------------------------------------------
177 */
178 function gen_menu()
179 {
180 if ($this->menu == ""){
181 $cfg= $this->config->data['MENU'];
182 $menu = "\n<div class='navigation'>";
183 foreach ($cfg as $headline => $plug){
185 if(!count($plug)) continue;
187 $menu.= "\n<div class='menu'>";
188 $menu.= "\n <ul>";
189 $menu.= "\n <li class='menu-header'>"._($headline)."</li>";
190 $id = 0;
191 foreach ($plug as $info){
193 // Used to detect the last element in the menu
194 $id ++;
196 list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
197 $class= "";
198 if($id == count($plug)) $class=" class='menu-last' \n ";
199 $menu .= "\n <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
200 }
201 $menu.= "\n </ul>";
202 $menu.= "\n <div></div>\n";
203 $menu.= "\n</div>\n";
204 $menu.= "\n<div></div>\n";
205 $menu.= "\n<div class='v-spacer'></div>\n";
206 }
207 $menu.= "\n</div>";
208 $this->menu = $menu;
210 // Add javascript method to print out warning messages while leaving an unsaved form.
211 // We do it in here to get the string translated.
212 $this->menu .= "
213 <script language='javascript' type='text/javascript'>
214 function openPlugin(id){
215 return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\",
216 \"main.php?plug=\" + id + \"&reset=1\");
217 }
218 </script>
219 ";
220 }
222 // Use javascript to mark the currently selected plugin.
223 $menu = $this->menu;
224 if(isset($_GET['plug'])){
225 $menu.= "
226 <script language='javascript' type='text/javascript'>
227 if($('plugMenuId_".$_GET['plug']."')){
228 $('plugMenuId_".$_GET['plug']."').className= 'current'
229 }
230 </script>
231 ";
232 }
234 // Return the generated/cached gosa menu.
235 return ($menu);
236 }
239 /*! \brief Generate the GOsa Icon-Menu here, this usually only done once during
240 * login.
241 * -----------------------------------------------------------------
242 * Do NOT add style changes here manually, use the style.css or
243 * if you prefer create your own theme!!
244 * -----------------------------------------------------------------
245 */
246 function show_iconmenu()
247 {
248 $add_hr =FALSE;
249 if ($this->iconmenu == ""){
250 $cfg= $this->config->data['MENU'];
251 foreach ($cfg as $headline => $plug){
252 $col= 0;
254 $this->iconmenu .= "\n <div class='clear'></div>\n";
255 if($add_hr){
256 $add_hr = FALSE;
257 $this->iconmenu .= "\n <hr>\n";
258 }
259 $this->iconmenu .= "\n <h3 class='icon-menu-title'>". _($headline)."</h3>\n";
261 foreach ($plug as $info){
263 // Get Plugin info
264 list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
266 // Add a seperating row
267 if (($col % 4) == 0){
268 $this->iconmenu .= "\n <div class='clear'></div>\n";
269 }
271 $this->iconmenu.= "\n <div class='container-element' style='width: 25%;' onclick='openPlugin({$index})'> ";
272 $this->iconmenu.= "\n <div class='icon-menu-item' style='background-image: url({$icon});'>";
273 $this->iconmenu.= "\n <h1>{$title}</h1>";
274 $this->iconmenu.= "\n <p>{$desc}</p>";
275 $this->iconmenu.= "\n </div>";
276 $this->iconmenu.= "\n </div>\n";
277 $col++ ;
278 }
279 $add_hr = TRUE;
280 }
281 }
282 return ($this->iconmenu);
283 }
286 function genPathMenu()
287 {
288 if(empty($this->pathMenu)){
289 $this->pathMenu =
290 "\n <div class='plugin-path'>".
291 "\n <ul class='path-navigation'>".
292 "\n <li class='left right-border' onClick=\"openPlugin('');\">".
293 "\n <div class='nav-home'></div>".
294 "\n </li>".
295 "\n <li class='left'>Welcome to GOsa</li>";
297 if(isset($this->config->data['PATHMENU'])){
298 $cfg= &$this->config->data['PATHMENU'];
299 $rcfg = array_reverse($cfg);
300 foreach($rcfg as $id => $plug){
301 list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
302 $this->pathMenu.= "\n <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
303 }
304 }
305 $this->pathMenu.= "\n </ul>";
306 $this->pathMenu.= "\n </div>";
307 }
308 return($this->pathMenu);
309 }
312 function getPlugData($class)
313 {
314 global $class_mapping;
315 $vars= get_class_vars($class);
316 $plHeadline= _($vars['plHeadline']);
317 $plDescription= _($vars['plDescription']);
318 $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
319 $index= $this->get_index($class);
321 /* Check if class is available. If the class doesn't exists display error symbol
322 * to avoid that a user clicks on a non existing plugin
323 */
324 if(!$vars){
325 $plHeadline = $plDescription = _("Unknown");
326 $plIcon = "error.png";
327 $index = '';
328 }
330 // Detect the correct position of the plugin icon
331 if(!preg_match("/\//",$plIcon)){
332 $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1',
333 $class_mapping[$class])."/images/$plIcon");
334 }else{
335 $image = $plIcon;
336 }
337 return(array($index, $plHeadline, $plDescription, $image));
338 }
341 /*! \brief Returns the installation path of a plugin.
342 * e.g. '../plugins/admin/mimetypes'
343 */
344 function get_path($index)
345 {
346 if(!isset($this->dirlist[$index])){
347 return ("");
348 }
349 return ("../".$this->dirlist[$index]);
350 }
353 /*! \brief Returns the plugins id for a given class.
354 */
355 function get_index($class)
356 {
357 return (array_search($class, $this->pluginList));
358 }
361 /*! \brief This function checks if we are allowed to view the plugin with the given id
362 *
363 * @param $plug_id Integer The ID of the plugin.
364 * @return Boolean TRUE if we are allowed to view the plugin else FASLE
365 */
366 function plugin_access_allowed($plug_id)
367 {
368 return(isset($this->pluginList[$plug_id]));
369 }
372 /*! \brief Force the menu to be recreated
373 */
374 function reset_menus()
375 {
376 $this->menu = "";
377 $this->iconmenu ="";
378 }
381 /*! \brief Generates an array containing plugin names (headlines) and theirs ids.
382 * This is just used in the helpviewer.php
383 */
384 function gen_headlines()
385 {
386 $ret = array();
387 if(count($this->headlines) == 0){
388 foreach($this->config->data['MENU'] as $headline => $plugins){
389 foreach( $plugins as $id => $plug){
390 if (plugin_available($plug['CLASS'])){
391 $attrs = (get_class_vars($plug['CLASS']));
392 $ret[$id]['HEADLINE'] = $headline;
393 $ret[$id]['NAME'] = $attrs['plHeadline'];
394 }
395 }
396 }
397 $this->headlines = $ret;
398 }
399 return($this->headlines);
400 }
401 }
402 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
403 ?>