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