Code

Moved snapshot and daemon handling to the listing class
[gosa.git] / gosa-core / include / class_listing.inc
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 listing {
25   var $xmlData;
26   var $entries;
27   var $departments= array();
28   var $departmentBrowser= false;
29   var $departmentRootVisible= false;
30   var $multiSelect= false;
31   var $template;
32   var $headline;
33   var $module;
34   var $base;
35   var $sortDirection= null;
36   var $sortColumn= null;
37   var $sortAttribute;
38   var $sortType;
39   var $numColumns;
40   var $baseMode= false;
41   var $bases= array();
42   var $header= array();
43   var $colprops= array();
44   var $filters= array();
45   var $pid;
46   var $objectTypes= array();
47   var $objectTypeCount= array();
48   var $copyPasteHandler= null;
49   var $snapshotHandler= null;
52   function listing($filename)
53   {
54     global $config;
56     // Initialize pid
57     $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
59     if (!$this->load($filename)) {
60       die("Cannot parse $filename!");
61     }
63     // Set base for filter
64     $this->base= session::global_get("CurrentMainBase");
65     if ($this->base == null) {
66       $this->base= $config->current['BASE'];
67     }
68     $this->refreshBasesList();
70     // Move footer information
71     $this->showFooter= ($config->get_cfg_value("listSummary") == "true");
73     // Register build in filters
74     $this->registerElementFilter("objectType", "listing::filterObjectType");
75     $this->registerElementFilter("departmentLink", "listing::filterDepartmentLink");
76     $this->registerElementFilter("link", "listing::filterLink");
77     $this->registerElementFilter("actions", "listing::filterActions");
78   }
81   function setCopyPasteHandler($handler)
82   {
83     $this->copyPasteHandler= &$handler;
84   }
87   function setSnapshotHandler($handler)
88   {
89     $this->snapshotHandler= &$handler;
90   }
93   function setFilter($filter)
94   {
95     $this->filter= &$filter;
96     if ($this->departmentBrowser){
97       $this->departments= $this->getDepartments();
98     }
99     $this->filter->setBase($this->base);
100     $this->entries= $this->filter->query();
101   }
104   function registerElementFilter($name, $call)
105   {
106     if (!isset($this->filters[$name])) {
107       $this->filters[$name]= $call;
108       return true;
109     }
111     return false;
112   }
115   function load($filename)
116   {
117     $contents = file_get_contents($filename);
118     $this->xmlData= xml::xml2array($contents, 1);
120     if (!isset($this->xmlData['list'])) {
121       return false;
122     }
124     $this->xmlData= $this->xmlData["list"];
126     // Load some definition values
127     foreach (array("departmentBrowser", "departmentRootVisible", "multiSelect", "baseMode") as $token) {
128       if (isset($this->xmlData['definition'][$token]) &&
129           $this->xmlData['definition'][$token] == "true"){
130         $this->$token= true;
131       }
132     }
134     // Fill objectTypes from departments and xml definition
135     $types = departmentManagement::get_support_departments();
136     foreach ($types as $class => $data) {
137       $this->objectTypes[]= array("label" => $data['TITLE'],
138                                   "objectClass" => $data['OC'],
139                                   "image" => $data['IMG']);
140     }
141     $this->categories= array();
142     if (isset($this->xmlData['definition']['objectType'])) {
143       foreach ($this->xmlData['definition']['objectType'] as $index => $otype) {
144         $this->objectTypes[]= $this->xmlData['definition']['objectType'][$index];
145         if (isset($this->xmlData['definition']['objectType'][$index]['category'])){
146           $this->categories[]= $this->xmlData['definition']['objectType'][$index]['category'];
147         }
148       }
149     }
151     // Parse layout per column
152     $this->colprops= $this->parseLayout($this->xmlData['table']['layout']);
154     // Prepare table headers
155     $this->renderHeader();
157     // Assign headline/module
158     $this->headline= _($this->xmlData['definition']['label']);
159     $this->module= $this->xmlData['definition']['module'];
160     if (!is_array($this->categories)){
161       $this->categories= array($this->categories);
162     }
164     return true;  
165   }
168   function renderHeader()
169   {
170     $this->header= array();
172     // Initialize sort?
173     $sortInit= false;
174     if (!$this->sortDirection) {
175       $this->sortColumn= 0;
176       if (isset($this->xmlData['definition']['defaultSortColumn'])){
177         $this->sortColumn= $this->xmlData['definition']['defaultSortColumn'];
178       } else {
179         $this->sortAttribute= "";
180       }
181       $this->sortDirection= array();
182       $sortInit= true;
183     }
185     if (isset($this->xmlData['table']['column'])){
186       foreach ($this->xmlData['table']['column'] as $index => $config) {
187         // Initialize everything to one direction
188         if ($sortInit) {
189           $this->sortDirection[$index]= false;
190         }
192         $sorter= "";
193         if ($index == $this->sortColumn && isset($config['sortAttribute']) &&
194             isset($config['sortType'])) {
195           $this->sortAttribute= $config['sortAttribute'];
196           $this->sortType= $config['sortType'];
197           $sorter= "&nbsp;<img border='0' title='".($this->sortDirection[$index]?_("Up"):_("Down"))."' src='images/lists/sort-".($this->sortDirection[$index]?"up":"down").".png' align='top'>";
198         }
199         $sortable= (isset($config['sortAttribute']));
201         $link= "href='?plug=".$_GET['plug']."&amp;PID=".$this->pid."&amp;act=SORT_$index'";
202         if (isset($config['label'])) {
203           if ($sortable) {
204             $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."><a $link>"._($config['label'])."$sorter</a></td>";
205           } else {
206             $this->header[$index]= "<td class='listheader' ".$this->colprops[$index].">"._($config['label'])."</td>";
207           }
208         } else {
209           if ($sortable) {
210             $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."><a $link>&nbsp;$sorter</a></td>";
211           } else {
212             $this->header[$index]= "<td class='listheader' ".$this->colprops[$index].">&nbsp;</td>";
213           }
214         }
215       }
216     }
217   }
219   function render()
220   {
221     // Check for exeeded sizelimit
222     if (($message= check_sizelimit()) != ""){
223       return($message);
224     }
226     // Initialize list
227     $result= "<input type='hidden' value='$this->pid' name='PID'>";
228     $result.= "<div class='contentboxb' id='listing_container' style='border-top:1px solid #B0B0B0;'>";
229     $result.= "<table summary='$this->headline' style='width:600px;height:450px;' cellspacing='0' id='t_scrolltable'>
230 <tr><td class='scrollhead'><table summary='' style='width:100%;' cellspacing='0' id='t_scrollhead'>";
231     $this->numColumns= count($this->colprops) + ($this->multiSelect?1:0);
233     // Build list header
234     $result.= "<tr>";
235     if ($this->multiSelect) {
236       $result.= "<td class='listheader' style='width:20px;'><input type='checkbox' id='select_all' name='select_all' title='"._("Select all")."' onClick='toggle_all_(\"listing_selected_[0-9]*$\",\"select_all\");' ></td>";
237     }
238     foreach ($this->header as $header) {
239       $result.= $header;
240     }
242     // Add 13px for scroller
243     $result.= "<td class='listheader' style='width:13px;border-right:0px;'>&nbsp;</td></table></td></tr>";
245     // New table for the real list contents
246     $result.= "<tr><td colspan='$this->numColumns' class='scrollbody'><div style='width:600px;height:430px;' id='d_scrollbody' class='scrollbody'><table summary='' style='height:100%;width:581px;' cellspacing='0' id='t_scrollbody'>";
248     // No results? Just take an empty colspanned row
249     if (count($this->entries) + count($this->departments) == 0) {
250       $result.= "<tr class='rowxp0'><td class='list1nohighlight' colspan='$this->numColumns' style='height:100%;border-right:0px;width:100%;'>&nbsp;</td></tr>";
251     }
253     // Line color alternation
254     $alt= 0;
255     $deps= 0;
257     // Draw department browser if configured and we're not in sub mode
258     if ($this->departmentBrowser && $this->filter->scope != "sub") {
259       // Fill with department browser if configured this way
260       $departmentIterator= new departmentSortIterator($this->departments, $this->sortDirection[$this->sortColumn]);
261       foreach ($departmentIterator as $row => $entry){
262         $result.="<tr class='rowxp".($alt&1)."'>";
264         // Render multi select if needed
265         if ($this->multiSelect) {
266           $result.="<td style='text-align:center;width:20px;' class='list1'>&nbsp;</td>";
267         }
269         // Render defined department columns, fill the rest with some stuff
270         foreach ($this->xmlData['table']['department'] as $index => $config) {
271           $result.="<td ".$this->colprops[$index]." class='list1'>".$this->renderCell($config['value'], $entry, $row)."</td>";
272         }
273         $last= count($this->xmlData['table']['department']) + 1;
274         $rest= $this->numColumns - $last;
275         for ($i= 0; $i<$rest; $i++){
276           $result.= "<td ".$this->colprops[$last+$i-1]." class='list1'>&nbsp;</td>";
277         }
278         $result.="</tr>";
280         $alt++;
281       }
282       $deps= $alt;
283     }
285     // Fill with contents, sort as configured
286     $entryIterator= new listingSortIterator($this->entries, $this->sortDirection[$this->sortColumn], $this->sortAttribute, $this->sortType);
287     foreach ($entryIterator as $row => $entry){
288       $result.="<tr class='rowxp".($alt&1)."'>";
290       // Render multi select if needed
291       if ($this->multiSelect) {
292         $result.="<td style='text-align:center;width:20px;' class='list0'><input type='checkbox' id='listing_selected_$row' name='listing_selected_$row'></td>";
293       }
295       foreach ($this->xmlData['table']['column'] as $index => $config) {
296         $result.="<td ".$this->colprops[$index]." class='list0'>".$this->renderCell($config['value'], $entry, $row)."</td>";
297       }
298       $result.="</tr>";
300       $alt++;
301     }
303     // Need to fill the list if it's not full (nobody knows why this is 22 ;-))
304     $emptyListStyle= (count($this->entries) + count($deps) == 0)?"border:0;":"";
305     if (count($this->entries) + count($deps) < 22) {
306       $result.= "<tr>";
307       for ($i= 0; $i<$this->numColumns; $i++) {
308         if ($i == 0) {
309           $result.= "<td class='list1nohighlight' style='$emptyListStyle height:100%;'>&nbsp;</td>";
310           continue;
311         }
312         if ($i != $this->numColumns-1) {
313           $result.= "<td class='list1nohighlight' style='$emptyListStyle'>&nbsp;</td>";
314         } else {
315           $result.= "<td class='list1nohighlight' style='border-right:1px solid #AAA;$emptyListStyle'>&nbsp;</td>";
316         }
317       }
318       $result.= "</tr>";
319     }
321     $result.= "</table></div></td></tr>";
323     // Add the footer if requested
324     if ($this->showFooter) {
325       $result.= "<tr><td class='scrollhead'><table summary='' style='width:100%' cellspacing='0' id='t_scrollfoot'><tr><td class='listfooter' style='border-bottom:0px;'>";
327       foreach ($this->objectTypes as $objectType) {
328         if (isset($this->objectTypeCount[$objectType['label']])) {
329           $label= _($objectType['label']);
330           $result.= "<img class='center' src='".$objectType['image']."' title='$label' alt='$label'>&nbsp;".$this->objectTypeCount[$objectType['label']]."&nbsp;&nbsp;&nbsp;&nbsp;";
331         }
332       }
334       $result.= "<td class='listfooter' style='width:13px;border-right:0px;'>&nbsp;</td></table></td></tr>";
335     }
337     $result.= "</table></div>";
339     $smarty= get_smarty();
340     $smarty->assign("FILTER", $this->filter->render());
341     $smarty->assign("SIZELIMIT", print_sizelimit_warning());
342     $smarty->assign("LIST", $result);
344     // Assign navigation elements
345     $nav= $this->renderNavigation();
346     foreach ($nav as $key => $html) {
347       $smarty->assign($key, $html);
348     }
350     // Assign action menu / base
351     $smarty->assign("ACTIONS", $this->renderActionMenu());
352     $smarty->assign("BASE", $this->renderBase());
354     // Assign separator
355     $smarty->assign("SEPARATOR", "<img src='images/lists/seperator.png' alt='-' align='middle' height='16' width='1' class='center'>");
357     // Assign summary
358     $smarty->assign("HEADLINE", $this->headline);
360     return ($smarty->fetch(get_template_path($this->xmlData['definition']['template'], true)));
361   }
364   function update()
365   {
366     global $config;
367     $ui= get_userinfo();
369     // Do not do anything if this is not our PID
370     if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->pid) {
371       return;
372     }
374     // Save base
375     if (isset($_POST['BASE']) && $this->baseMode == true) {
376       $base= validate($_POST['BASE']);
377       if (isset($this->bases[$base])) {
378         $this->base= $base;
379       }
380     }
382     // Override the base if we got a message from the browser navigation
383     if ($this->departmentBrowser && isset($_GET['act'])) {
384       if (preg_match('/^department_([0-9]+)$/', validate($_GET['act']), $match)){
385         if (isset($this->departments[$match[1]])){
386           $this->base= $this->departments[$match[1]]['dn'];
387         }
388       }
389     }
391     // Filter GET with "act" attributes
392     if (isset($_GET['act'])) {
393       $key= validate($_GET['act']);
394       if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
395         // Switch to new column or invert search order?
396         $column= $match[1];
397         if ($this->sortColumn != $column) {
398           $this->sortColumn= $column;
399         } else {
400           $this->sortDirection[$column]= !$this->sortDirection[$column];
401         }
403         // Allow header to update itself according to the new sort settings
404         $this->renderHeader();
405       }
406     }
408     // Override base if we got signals from the navigation elements
409     $action= "";
410     foreach ($_POST as $key => $value) {
411       if (preg_match('/^(ROOT|BACK|HOME)_x$/', $key, $match)) {
412         $action= $match[1];
413         break;
414       }
415     }
417     // Navigation handling
418     if ($action == 'ROOT') {
419       $deps= $ui->get_module_departments($this->module);
420       $this->base= $deps[0];
421     }
422     if ($action == 'BACK') {
423       $deps= $ui->get_module_departments($this->module);
424       $base= preg_replace("/^[^,]+,/", "", $this->base);
425       if(in_array_ics($base, $deps)){
426         $this->base= $base;
427       }
428     }
429     if ($action == 'HOME') {
430       $ui= get_userinfo();
431       $this->base= get_base_from_people($ui->dn);
432     }
434     // Reload departments
435     if ($this->departmentBrowser){
436       $this->departments= $this->getDepartments();
437     }
439     // Update filter and refresh entries
440     $this->filter->setBase($this->base);
441     $this->entries= $this->filter->query();
442   }
445   function parseLayout($layout)
446   {
447     $result= array();
448     $layout= preg_replace("/^\|/", "", $layout);
449     $layout= preg_replace("/\|$/", "", $layout);
450     $cols= split("\|", $layout);
451     foreach ($cols as $index => $config) {
452       if ($config != "") {
453         $components= split(';', $config);
454         $config= "";
455         foreach ($components as $part) {
456           if (preg_match("/^r$/", $part)) {
457             $config.= "text-align:right;";
458             continue;
459           }
460           if (preg_match("/^l$/", $part)) {
461             $config.= "text-align:left;";
462             continue;
463           }
464           if (preg_match("/^c$/", $part)) {
465             $config.= "text-align:center;";
466             continue;
467           }
468           if (preg_match("/^[0-9]+(|px|%)$/", $part)) {
469             $config.= "width:$part;";
470             continue;
471           }
472         }
474         $result[$index]= " style='$config' ";
475       } else {
476         $result[$index]= null;
477       }
478     }
480     // Save number of columns for later use
481     $this->numColumns= count($cols);
483     return $result;
484   }
487   function renderCell($data, $config, $row)
488   {
489     // Replace flat attributes in data string
490     for ($i= 0; $i<$config['count']; $i++) {
491       $attr= $config[$i];
492       $value= "";
493       if (is_array($config[$attr])) {
494         $value= $config[$attr][0];
495       } else {
496         $value= $config[$attr];
497       }
498       $data= preg_replace("/%\{$attr\}/", $value, $data);
499     }
501     // Watch out for filters and prepare to execute them
502     $data= $this->processElementFilter($data, $config, $row);
504     return $data;
505   }
508   function renderBase()
509   {
510     $result= "<select name='BASE' onChange='mainform.submit()' size='1'>";
511     $firstDN= null;
512     $found= false;
514     foreach ($this->bases as $key=>$value) {
515       // Keep first entry to fall back eventually
516       if(!$firstDN) {
517         $firstDN= $key;
518       }
520       // Prepare to render entry
521       $selected= "";
522       if ($key == $this->base) {
523         $selected= " selected";
524         $found= true;
525       }
526       $result.= "<option value='".$key."'$selected>".$value."</option>";
527     }
528     $result.= "</select>";
530     // Reset the currently used base to the first DN we found if there
531     // was no match.
532     if(!$found){
533       $this->base = $firstDN;
534     }
536     return $result;
537   }
540   function processElementFilter($data, $config, $row)
541   {
542     preg_match_all("/%\{filter:([^(]+)\((.*)\)\}/", $data, $matches, PREG_SET_ORDER);
544     foreach ($matches as $match) {
545       if (!isset($this->filters[$match[1]])) {
546         continue;
547       }
548       $cl= preg_replace('/::.*$/', '', $this->filters[$match[1]]);
549       $method= preg_replace('/^.*::/', '', $this->filters[$match[1]]);
551       // Prepare params for function call
552       $params= array();
553       preg_match_all('/"[^"]+"|[^,]+/', $match[2], $parts);
554       foreach ($parts[0] as $param) {
556         // Row is replaced by the row number
557         if ($param == "row") {
558           $params[]= $row;
559         }
561         // pid is replaced by the current PID
562         if ($param == "pid") {
563           $params[]= $this->pid;
564         }
566         // Fixie with "" is passed directly
567         if (preg_match('/^".*"$/', $param)){
568           $params[]= preg_replace('/"/', '', $param);
569         }
571         // LDAP variables get replaced by their objects
572         for ($i= 0; $i<$config['count']; $i++) {
573           if ($param == $config[$i]) {
574             $values= $config[$config[$i]];
575             if (is_array($values)){
576               unset($values['count']);
577             }
578             $params[]= $values;
579           }
580         }
582         // Move dn if needed
583         if ($param == "dn") {
584           $params[]= LDAP::fix($config["dn"]);
585         }
586       }
588       // Replace information
589       if ($cl == "listing") {
590         // Non static call - seems to result in errors
591         $data= @preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($this, "$method"), $params), $data);
592       } else {
593         // Static call
594         $data= preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($cl, $method), $params), $data);
595       }
596     }
598     return $data;
599   }
602   function getObjectType($types, $classes)
603   {
604     // Walk thru types and see if there's something matching
605     foreach ($types as $objectType) {
606       $ocs= $objectType['objectClass'];
607       if (!is_array($ocs)){
608         $ocs= array($ocs);
609       }
611       $found= true;
612       foreach ($ocs as $oc){
613         if (preg_match('/^!(.*)$/', $oc, $match)) {
614           $oc= $match[1];
615           if (in_array($oc, $classes)) {
616             $found= false;
617           }
618         } else {
619           if (!in_array($oc, $classes)) {
620             $found= false;
621           }
622         }
623       }
625       if ($found) {
626         return $objectType;
627       }
628     }
630     return null;
631   }
634   function filterObjectType($dn, $classes)
635   {
636     // Walk thru classes and return on first match
637     $result= "&nbsp;";
639     $objectType= $this->getObjectType($this->objectTypes, $classes);
640     if ($objectType) {
641       $result= "<img class='center' title='".LDAP::fix($dn)."' src='".$objectType["image"]."'>";
642       if (!isset($this->objectTypeCount[$objectType['label']])) {
643         $this->objectTypeCount[$objectType['label']]= 0;
644       }
645       $this->objectTypeCount[$objectType['label']]++;
646     }
647     return $result;
648   }
651   function filterActions($dn, $row, $classes)
652   {
653     // Do nothing if there's no menu defined
654     if (!isset($this->xmlData['actiontriggers']['action'])) {
655       return "&nbsp;";
656     }
658     // Go thru all actions
659     $result= "";
660     $actions= $this->xmlData['actiontriggers']['action'];
661     foreach($actions as $action) {
662       // Skip the entry completely if there's no permission to execute it
663       if (!$this->hasActionPermission($action, $dn)) {
664         continue;
665       }
667       // If there's an objectclass definition and we don't have it
668       // add an empty picture here.
669       if (isset($action['objectclass'])){
670         $objectclass= $action['objectclass'];
671         if (preg_match('/^!(.*)$/', $objectclass, $m)){
672           $objectclass= $m[1];
673           if(in_array($objectclass, $classes)) {
674             $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
675             continue;
676           }
677         } else {
678           if(!in_array($objectclass, $classes)) {
679             $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
680             continue;
681           }
682         }
683       }
685       // Render normal entries as usual
686       if ($action['type'] == "entry") {
687         $label= $this->processElementFilter($action['label'], $this->entries[$row], $row);
688         $image= $this->processElementFilter($action['image'], $this->entries[$row], $row);
689         $result.="<input class='center' type='image' src='$image' title='$label' ".
690                  "name='listing_".$action['name']."_$row' style='padding:1px'>";
691       }
693       // Handle special types
694       if ($action['type'] == "copypaste" || $action['type'] == "snapshot") {
696         $objectType= $this->getObjectType($this->objectTypes, $this->entries[$row]['objectClass']);
697         $category= $class= null;
698         if ($objectType) {
699           $category= $objectType['category'];
700           $class= $objectType['class'];
701         }
703         if ($action['type'] == "copypaste") {
704           $result.= $this->renderCopyPasteActions($row, $this->entries[$row]['dn'], $category, $class);
705         } else {
706           $result.= $this->renderSnapshotActions($row, $this->entries[$row]['dn'], $category, $class);
707         }
708       }
709     }
711     return $result;
712   }
715   function filterDepartmentLink($row, $dn, $description)
716   {
717     $attr= $this->departments[$row]['sort-attribute'];
718     $name= $this->departments[$row][$attr];
719     if (is_array($name)){
720       $name= $name[0];
721     }
722     $result= sprintf("%s [%s]", $name, $description[0]);
723     return("<a href='?plug=".$_GET['plug']."&amp;PID=$this->pid&amp;act=department_$row' title='$dn'>$result</a>");
724   }
727   function filterLink()
728   {
729     $result= "&nbsp;";
731     $row= func_get_arg(0);
732     $pid= $this->pid;
733     $dn= LDAP::fix(func_get_arg(1));
734     $params= array(func_get_arg(2));
736     // Collect sprintf params
737     for ($i = 3;$i < func_num_args();$i++) {
738       $val= func_get_arg($i);
739       if (is_array($val)){
740         $params[]= $val[0];
741         continue;
742       }
743       $params[]= $val;
744     }
746     $result= "&nbsp;";
747     $trans= call_user_func_array("sprintf", $params);
748     if ($trans != "") {
749       return("<a href='?plug=".$_GET['plug']."&amp;PID=$pid&amp;act=listing_edit_$row' title='$dn'>$trans</a>");
750     }
752     return $result;
753   }
756   function renderNavigation()
757   {
758     $result= array();
759     $enableBack = true;
760     $enableRoot = true;
761     $enableHome = true;
763     $ui = get_userinfo();
765     /* Check if base = first available base */
766     $deps = $ui->get_module_departments($this->module);
768     if(!count($deps) || $deps[0] == $this->filter->base){
769       $enableBack = false;
770       $enableRoot = false;
771     }
773     $listhead ="";
775     /* Check if we are in users home  department */
776     if(!count($deps) ||$this->filter->base == get_base_from_people($ui->dn)){
777       $enableHome = false;
778     }
780     /* Draw root button */
781     if($enableRoot){
782       $result["ROOT"]= "<input class='center' type='image' src='images/lists/root.png' align='middle' ".
783                        "title='"._("Go to root department")."' name='ROOT' alt='"._("Root")."'>";
784     }else{
785       $result["ROOT"]= "<img src='images/lists/root_grey.png' class='center' alt='"._("Root")."'>";
786     }
788     /* Draw back button */
789     if($enableBack){
790       $result["BACK"]= "<input class='center' type='image' align='middle' src='images/lists/back.png' ".
791                        "title='"._("Go up one department")."' alt='"._("Up")."' name='BACK'>";
792     }else{
793       $result["BACK"]= "<img src='images/lists/back_grey.png' class='center' alt='"._("Up")."'>";
794     }
796     /* Draw home button */
797     if($enableHome){
798       $result["HOME"]= "<input class='center' type='image' align='middle' src='images/lists/home.png' ".
799                        "title='"._("Go to users department")."' alt='"._("Home")."' name='HOME'>";
800     }else{
801       $result["HOME"]= "<img src='images/lists/home_grey.png' class='center' alt='"._("Home")."'>";
802     }
804     /* Draw reload button, this button is enabled everytime */
805     $result["RELOAD"]= "<input class='center' type='image' src='images/lists/reload.png' align='middle' ".
806                        "title='"._("Reload list")."' name='REFRESH' alt='"._("Submit")."'>";
808     return ($result);
809   }
812   function getAction()
813   {
814     // Do not do anything if this is not our PID
815     if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->pid) {
816       return;
817     }
819     $result= array("targets" => array(), "action" => "");
821     // Filter GET with "act" attributes
822     if (isset($_GET['act'])) {
823       $key= validate($_GET['act']);
824       $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)$/', '$1', $key);
825       if (isset($this->entries[$target]['dn'])) {
826         $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+$/', '$1', $key);
827         $result['targets'][]= $this->entries[$target]['dn'];
828       }
830       // Drop targets if empty
831       if (count($result['targets']) == 0) {
832         unset($result['targets']);
833       }
834       return $result;
835     }
837     // Filter POST with "listing_" attributes
838     foreach ($_POST as $key => $prop) {
840       // Capture selections
841       if (preg_match('/^listing_selected_[0-9]+$/', $key)) {
842         $target= preg_replace('/^listing_selected_([0-9]+)$/', '$1', $key);
843         if (isset($this->entries[$target]['dn'])) {
844           $result['targets'][]= $this->entries[$target]['dn'];
845         }
846         continue;
847       }
849       // Capture action with target - this is a one shot
850       if (preg_match('/^listing_[a-zA-Z_]+_[0-9]+(|_x)$/', $key)) {
851         $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)(|_x)$/', '$1', $key);
852         if (isset($this->entries[$target]['dn'])) {
853           $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+(|_x)$/', '$1', $key);
854           $result['targets']= array($this->entries[$target]['dn']);
855         }
856         break;
857       }
859       // Capture action without target
860       if (preg_match('/^listing_[a-zA-Z_]+(|_x)$/', $key)) {
861         $result['action']= preg_replace('/^listing_([a-zA-Z_]+)(|_x)$/', '$1', $key);
862         continue;
863       }
864     }
866     // Filter POST with "act" attributes -> posted from action menu
867     if (isset($_POST['act']) && $_POST['act'] != '') {
868       $result['action']= validate($_POST['act']);
869     }
871     // Drop targets if empty
872     if (count($result['targets']) == 0) {
873       unset($result['targets']);
874     }
875     return $result;
876   }
879   function renderActionMenu()
880   {
881     // Don't send anything if the menu is not defined
882     if (!isset($this->xmlData['actionmenu']['action'])){
883       return "";
884     }
886     // Load shortcut
887     $actions= &$this->xmlData['actionmenu']['action'];
888     $result= "<input type='hidden' name='act' id='actionmenu' value=''>".
889              "<ul class='level1' id='root'><li><a href='#'>Aktionen&nbsp;<img ".
890              "border=0 src='images/lists/sort-down.png'></a>";
892     // Build ul/li list
893     $result.= $this->recurseActions($actions);
895     return "<div id='pulldown'>".$result."</li></ul><div>";
896   }
899   function recurseActions($actions)
900   {
901     static $level= 2;
902     $result= "<ul class='level$level'>";
903     $separator= "";
905     foreach ($actions as $action) {
907       // Skip the entry completely if there's no permission to execute it
908       if (!$this->hasActionPermission($action, $this->filter->base)) {
909         continue;
910       }
912       // Fill image if set
913       $img= "";
914       if (isset($action['image'])){
915         $img= "<img border=0 src='".$action['image']."'>&nbsp;";
916       }
918       if ($action['type'] == "separator"){
919         $separator= " style='border-top:1px solid #AAA' ";
920         continue;
921       }
923       // Dive into subs
924       if ($action['type'] == "sub" && isset($action['action'])) {
925         $level++;
926         if (isset($action['label'])){
927           $result.= "<li$separator><a href='#'>$img"._($action['label'])."&nbsp;<img border='0' src='images/forward-arrow.png'></a>";
928         }
929         $result.= $this->recurseActions($action['action'])."</li>";
930         $level--;
931         $separator= "";
932         continue;
933       }
935       // Render entry elseways
936       if (isset($action['label'])){
937         $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"".$action['name']."\";mainform.submit();'>$img"._($action['label'])."</a></li>";
938       }
940       // Check for special types
941       switch ($action['type']) {
942         case 'copypaste':
943           $result.= $this->renderCopyPasteMenu($separator);
944           break;
946         case 'snapshot':
947           $result.= $this->renderSnapshotMenu($separator);
948           break;
950         case 'daemon':
951           $result.= $this->renderDaemonMenu($separator);
952           break;
953       }
955       $separator= "";
956     }
958     $result.= "</ul>";
959     return $result;
960   }
963   function hasActionPermission($action, $dn)
964   {
965     $ui= get_userinfo();
967     if (isset($action['acl'])) {
968       $acls= $action['acl'];
969       if (!is_array($acls)) {
970         $acls= array($acls);
971       }
973       // Every ACL has to pass
974       foreach ($acls as $acl) {
975         $module= $this->module;
976         $acllist= array();
978         // Split for category and plugins if needed
979         // match for "[rw]" style entries
980         if (preg_match('/^\[([rwcdm]+)\]$/', $acl, $match)){
981           $aclList= array($match[1]);
982         }
984         // match for "users[rw]" style entries
985         if (preg_match('/^([a-zA-Z0-9]+)\[([rwcdm]+)\]$/', $acl, $match)){
986           $module= $match[1];
987           $aclList= array($match[2]);
988         }
990         // match for "users/user[rw]" style entries
991         if (preg_match('/^([a-zA-Z0-9]+\/[a-zA-Z0-9]+)\[([rwcdm]+)\]$/', $acl, $match)){
992           $module= $match[1];
993           $aclList= array($match[2]);
994         }
996         // match "users/user[userPassword:rw(,...)*]" style entries
997         if (preg_match('/^([a-zA-Z0-9]+\/[a-zA-Z0-9]+)\[([a-zA-Z0-9]+:[rwcdm]+(,[a-zA-Z0-9]+:[rwcdm]+)*)\]$/', $acl, $match)){
998           $module= $match[1];
999           $aclList= split(',', $match[2]);
1000         }
1002         // Walk thru prepared ACL by using $module
1003         foreach($aclList as $sAcl) {
1004           $checkAcl= "";
1006           // Category or detailed permission?
1007           if (strpos('/', $module) === false) {
1008             if (preg_match('/([a-zA-Z0-9]+):([rwcdm]+)/', $sAcl, $m) ) {
1009               $checkAcl= $ui->get_permissions($dn, $module, $m[1]);
1010               $sAcl= $m[2];
1011             } else {
1012               $checkAcl= $ui->get_permissions($dn, $module, '0');
1013             }
1014           } else {
1015             $checkAcl= $ui->get_category_permissions($dn, $module);
1016           }
1018           // Split up remaining part of the acl and check if it we're
1019           // allowed to do something...
1020           $parts= str_split($sAcl);
1021           foreach ($parts as $part) {
1022             if (strpos($checkAcl, $part) === false){
1023               return false;
1024             }
1025           }
1027         }
1028       }
1029     }
1031     return true;
1032   }
1035   function refreshBasesList()
1036   {
1037     global $config;
1038     $ui= get_userinfo();
1040     // Do some array munching to get it user friendly
1041     $ids= $config->idepartments;
1042     $d= $ui->get_module_departments($this->module);
1043     $k_ids= array_keys($ids);
1044     $deps= array_intersect($d,$k_ids);
1046     // Fill internal bases list
1047     $this->bases= array();
1048     foreach($k_ids as $department){
1049       $this->bases[$department] = $ids[$department];
1050     }
1051   }
1054   function getDepartments()
1055   {
1056     $departments= array();
1057     $ui= get_userinfo();
1059     // Get list of supported department types
1060     $types = departmentManagement::get_support_departments();
1062     // Load departments allowed by ACL
1063     $validDepartments = $ui->get_module_departments($this->module);
1065     // Build filter and look in the LDAP for possible sub departments
1066     // of current base
1067     $filter= "(&(objectClass=gosaDepartment)(|";
1068     $attrs= array("description", "objectClass");
1069     foreach($types as $name => $data){
1070       $filter.= "(objectClass=".$data['OC'].")";
1071       $attrs[]= $data['ATTR'];
1072     }
1073     $filter.= "))";
1074     $res= get_list($filter, $this->module, $this->base, $attrs, GL_NONE | GL_SIZELIMIT);
1076     // Analyze list of departments
1077     foreach ($res as $department) {
1078       if (!in_array($department['dn'], $validDepartments)) {
1079         continue;
1080       }
1082       // Add the attribute where we use for sorting
1083       $oc= null;
1084       foreach(array_keys($types) as $type) {
1085         if (in_array($type, $department['objectClass'])) {
1086           $oc= $type;
1087           break;
1088         }
1089       }
1090       $department['sort-attribute']= $types[$oc]['ATTR'];
1092       // Move to the result list
1093       $departments[]= $department;
1094     }
1096     return $departments;
1097   }
1100   function renderCopyPasteMenu($separator, $copy= true, $cut= true)
1101   {
1102     // We can only provide information if we've got a copypaste handler
1103     // instance
1104     if(!(isset($this->copyPasteHandler) && is_object($this->copyPasteHandler))){
1105       return "";
1106     }
1108     // Presets
1109     $result= "";
1110     $read= $paste= false;
1111     $ui= get_userinfo();
1113     // Switch flags to on if there's at least one category which allows read/paste
1114     foreach($this->categories as $category){
1115       $read= $read || preg_match('/r/', $ui->get_category_permissions($this->base, $category));
1116       $paste= $paste || $ui->is_pasteable($this->base, $category) == 1;
1117     }
1120     // Draw entries that allow copy and cut
1121     if($read){
1123       // Copy entry
1124       if($copy){
1125         $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"copy\";mainform.submit();'><img src='images/lists/copy.png' alt='' border='0' class='center'>&nbsp;"._("Copy")."</a></li>";
1126         $separator= "";
1127       }
1129       // Cut entry
1130       if($cut){
1131         $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"cut\";mainform.submit();'><img src='images/lists/cut.png' alt='' border='0' class='center'>&nbsp;"._("Cut")."</a></li>";
1132         $separator= "";
1133       }
1134     }
1136     // Draw entries that allow pasting entries
1137     if($paste){
1138       if($this->copyPasteHandler->entries_queued()){
1139         $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"paste\";mainform.submit();'><img src='images/lists/paste.png' alt='' border='0' class='center'>&nbsp;"._("Paste")."</a></li>";
1140       }else{
1141         $result.= "<li$separator><a href='#'><img src='images/lists/paste-grey.png' alt='' border='0' class='center'>&nbsp;"._("Paste")."</a></li>";
1142       }
1143     }
1144     
1145     return($result);
1146   }
1149   function renderCopyPasteActions($row, $dn, $category, $class, $copy= true, $cut= true)
1150   {
1151     // We can only provide information if we've got a copypaste handler
1152     // instance
1153     if(!(isset($this->copyPasteHandler) && is_object($this->copyPasteHandler))){
1154       return "";
1155     }
1157     // Presets
1158     $ui = get_userinfo();
1159     $result = "";
1161     // Render cut entries
1162     if($cut){
1163       if($ui->is_cutable($dn, $category, $class)){
1164         $result .= "<input class='center' type='image'
1165           src='images/lists/cut.png' alt='"._("Cut")."' name='listing_cut_$row' title='"._("Cut this entry")."' style='padding:1px'>";
1166       }else{
1167         $result.="<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1168       }
1169     }
1171     // Render copy entries
1172     if($copy){
1173       if($ui->is_copyable($dn, $category, $class)){
1174         $result.= "<input class='center' type='image'
1175           src='images/lists/copy.png' alt='"._("Copy")."' name='listing_copy_$row' title='"._("Copy this entry")."' style='padding:1px'>";
1176       }else{
1177         $result.="<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1178       }
1179     }
1181     return($result);
1182   }
1185   function renderSnapshotMenu($separator)
1186   {
1187     // We can only provide information if we've got a snapshot handler
1188     // instance
1189     if(!(isset($this->snapshotHandler) && is_object($this->snapshotHandler))){
1190       return "";
1191     }
1193     // Presets
1194     $result = "";
1195     $ui = get_userinfo();
1197     if($this->snapshotHandler->enabled() && $ui->allow_snapshot_restore($this->base, $this->module)){
1199       // Check if there is something to restore
1200       $restore= false;
1201       foreach($this->snapshotHandler->getSnapshotBases() as $base){
1202         $restore= $restore || count($this->snapshotHandler->getDeletedSnapshots($base)) > 0;
1203       }
1205       // Draw icons according to the restore flag
1206       if($restore){
1207         $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"restore\";mainform.submit();'><img src='images/lists/restore.png' alt='' border='0' class='center'>&nbsp;"._("Restore snapshots")."</a></li>";
1208       }else{
1209         $result.= "<li$separator><a href='#'><img src='images/lists/restore_grey.png' alt='' border='0' class='center'>&nbsp;"._("Restore snapshots")."</a></li>";
1210       }
1211     }
1213     return($result);
1214   }
1217   function renderSnapshotActions($row, $dn, $category, $class, $copy= true, $cut= true)
1218   {
1219     // We can only provide information if we've got a snapshot handler
1220     // instance
1221     if(!(isset($this->snapshotHandler) && is_object($this->snapshotHandler))){
1222       return "";
1223     }
1225     // Presets
1226     $result= "";
1227     $ui = get_userinfo();
1229     // Only act if enabled here
1230     if($this->snapshotHandler->enabled()){
1232       // Draw restore button
1233       if ($ui->allow_snapshot_restore($dn, $category)){
1235         // Do we have snapshots for this dn?
1236         if($this->snapshotHandler->hasSnapshots($dn)){
1237           $result.= "<input class='center' type='image' src='images/lists/restore.png' ".
1238                      "alt='"._("Restore snapshot")."' name='listing_restore_$row' title='".
1239                      _("Restore snapshot")."' style='padding:1px'>";
1240         } else {
1241           $result.= "<img src='images/lists/restore_grey.png' alt=' ' class='center' style='padding:1px'>";
1242         }
1243       }
1245       // Draw snapshot button
1246       if($ui->allow_snapshot_create($dn, $category)){
1247           $result.= "<input class='center' type='image' src='images/snapshot.png' ".
1248                      "alt='"._("Create snapshot")."' name='listing_snapshot_$row' title='".
1249                      _("Create a new snapshot from this object")."' style='padding:1px'>";
1250       }else{
1251           $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1252       }
1253     }
1255     return($result);
1256   }
1259   function renderDaemonMenu($separator)
1260   {
1261     $result= "";
1263     // If there is a daemon registered, draw the menu entries
1264     if(class_available("DaemonEvent")){
1265       $events= DaemonEvent::get_event_types_by_category($this->categories);
1266       if(count($events['BY_CLASS'])){
1267         foreach($events['BY_CLASS'] as $name => $event){
1268           $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"$name\";mainform.submit();'><img src='".$event['MenuImage']."' alt='' border='0' class='center'>&nbsp;".$event['s_Menu_Name']."</a></li>";
1269           $separator= "";
1270         }
1271       }
1272     }
1274     return $result;
1275   }
1279 ?>