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 $menu= "";
27 var $iconmenu= "";
28 var $menuparts= array();
29 var $config= NULL;
30 var $dirlist= array();
31 var $ui= NULL;
32 var $current= "";
33 var $info= array();
34 var $headlines = array();
35 var $silly_cache= array();
37 var $pluginList = array();
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){
192 $id ++;
193 list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
194 $class= "";
195 if($id == count($plug)) $class=" class='menu-last' \n ";
196 $menu .= "\n <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
197 }
198 $menu.= "\n </ul>";
199 $menu.= "\n <div></div>\n";
200 $menu.= "\n</div>\n";
201 $menu.= "\n<div></div>\n";
202 $menu.= "\n<div class='v-spacer'></div>\n";
203 }
204 $menu.= "\n</div>";
205 $this->menu = $menu;
207 // Add javascript method to print out warning messages while leaving an unsaved form.
208 // We do it in here to get the string translated.
209 $this->menu .= "
210 <script language='javascript' type='text/javascript'>
211 function openPlugin(id){
212 return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\",
213 \"main.php?plug=\" + id + \"&reset=1\");
214 }
215 </script>
216 ";
217 }
219 // Use javascript to mark the currently selected plugin.
220 $menu = $this->menu;
221 if(isset($_GET['plug'])){
222 $menu.= "
223 <script language='javascript' type='text/javascript'>
224 if($('plugMenuId_".$_GET['plug']."')){
225 $('plugMenuId_".$_GET['plug']."').className= 'current'
226 }
227 </script>
228 ";
229 }
231 // Return the generated/cached gosa menu.
232 return ($menu);
233 }
236 /*! \brief Generate the GOsa Icon-Menu here, this usually only done once during
237 * login.
238 * -----------------------------------------------------------------
239 * Do NOT add style changes here manually, use the style.css or
240 * if you prefer create your own theme!!
241 * -----------------------------------------------------------------
242 */
243 function show_iconmenu()
244 {
245 $add_hr =FALSE;
246 if ($this->iconmenu == ""){
247 $cfg= $this->config->data['MENU'];
248 foreach ($cfg as $headline => $plug){
249 $col= 0;
251 $this->iconmenu .= "\n <div class='clear'></div>\n";
252 if($add_hr){
253 $add_hr = FALSE;
254 $this->iconmenu .= "\n <hr>\n";
255 }
256 $this->iconmenu .= "\n <h3 class='icon-menu-title'>". _($headline)."</h3>\n";
258 foreach ($plug as $info){
260 // Get Plugin info
261 list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
263 // Add a seperating row
264 if (($col % 4) == 0){
265 $this->iconmenu .= "\n <div class='clear'></div>\n";
266 }
268 $this->iconmenu.= "\n <div class='container-element' style='width: 25%;' onclick='openPlugin({$index})'> ";
269 $this->iconmenu.= "\n <div class='icon-menu-item' style='background-image: url({$icon});'>";
270 $this->iconmenu.= "\n <h1>{$title}</h1>";
271 $this->iconmenu.= "\n <p>{$desc}</p>";
272 $this->iconmenu.= "\n </div>";
273 $this->iconmenu.= "\n </div>\n";
274 $col++ ;
275 }
276 $add_hr = TRUE;
277 }
278 }
279 return ($this->iconmenu);
280 }
283 function genPathMenu()
284 {
285 if(1 | empty($this->pathMenu)){
287 $this->pathMenu =
288 "\n <div class='plugin-path'>".
289 "\n <ul class='path-navigation'>".
290 "\n <li class='left right-border' onClick=\"openPlugin('');\">".
291 "\n <div class='nav-home'></div>".
292 "\n </li>".
293 "\n <li class='left'>Welcome to GOsa</li>";
295 if(isset($this->config->data['PATHMENU'])){
296 $cfg= &$this->config->data['PATHMENU'];
297 $rcfg = array_reverse($cfg);
298 foreach($rcfg as $id => $plug){
299 list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
300 $this->pathMenu.= "\n <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
301 }
302 }
303 $this->pathMenu.= "\n </ul>";
304 $this->pathMenu.= "\n </div>";
305 }
306 return($this->pathMenu);
307 }
310 function getPlugData($class)
311 {
312 global $class_mapping;
313 $vars= get_class_vars($class);
314 $plHeadline= _($vars['plHeadline']);
315 $plDescription= _($vars['plDescription']);
316 $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
317 $index= $this->get_index($class);
319 /* Check if class is available. If the class doesn't exists display error symbol
320 * to avoid that a user clicks on a non existing plugin
321 */
322 if(!$vars){
323 $plHeadline = $plDescription = _("Unknown");
324 $plIcon = "error.png";
325 $index = '';
326 }
328 // Detect the correct position of the plugin icon
329 if(!preg_match("/\//",$plIcon)){
330 $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1',
331 $class_mapping[$class])."/images/$plIcon");
332 }else{
333 $image = $plIcon;
334 }
335 return(array($index, $plHeadline, $plDescription, $image));
336 }
339 /*! \brief Returns the installation path of a plugin.
340 * e.g. '../plugins/admin/mimetypes'
341 */
342 function get_path($index)
343 {
344 if(!isset($this->dirlist[$index])){
345 return ("");
346 }
347 return ("../".$this->dirlist[$index]);
348 }
351 /*! \brief Returns the plugins id for a given class.
352 */
353 function get_index($class)
354 {
355 return (array_search($class, $this->pluginList));
356 }
359 /*! \brief This function checks if we are allowed to view the plugin with the given id
360 *
361 * @param $plug_id Integer The ID of the plugin.
362 * @return Boolean TRUE if we are allowed to view the plugin else FASLE
363 */
364 function plugin_access_allowed($plug_id)
365 {
366 return(isset($this->pluginList[$plug_id]));
367 }
370 /*! \brief Force the menu to be recreated
371 */
372 function reset_menus()
373 {
374 $this->menu = "";
375 $this->iconmenu ="";
376 }
379 /*! \brief Generates an array containing plugin names (headlines) and theirs ids.
380 * This is just used in the helpviewer.php
381 */
382 function gen_headlines()
383 {
384 $ret = array();
385 if(count($this->headlines) == 0){
386 foreach($this->config->data['MENU'] as $headline => $plugins){
387 foreach( $plugins as $id => $plug){
388 if (plugin_available($plug['CLASS'])){
389 $attrs = (get_class_vars($plug['CLASS']));
390 $ret[$id]['HEADLINE'] = $headline;
391 $ret[$id]['NAME'] = $attrs['plHeadline'];
392 }
393 }
394 }
395 $this->headlines = $ret;
396 }
397 return($this->headlines);
398 }
399 }
400 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
401 ?>