Code

Hide not resolved attributes/filters
[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         $rest= $this->numColumns - 1;
271         foreach ($this->xmlData['table']['department'] as $index => $config) {
272           $colspan= 1;
273           if (isset($config['span'])){
274             $colspan= $config['span'];
275           }
276           $result.="<td colspan='$colspan' ".$this->colprops[$index]." class='list1'>".$this->renderCell($config['value'], $entry, $row)."</td>";
277           $rest-= $colspan;
278         }
280         // Fill remaining cols with nothing
281         $last= $this->numColumns - $rest;
282         for ($i= 0; $i<$rest; $i++){
283           $result.= "<td ".$this->colprops[$last+$i-1]." class='list1'>&nbsp;</td>";
284         }
285         $result.="</tr>";
287         $alt++;
288       }
289       $deps= $alt;
290     }
292     // Fill with contents, sort as configured
293     $entryIterator= new listingSortIterator($this->entries, $this->sortDirection[$this->sortColumn], $this->sortAttribute, $this->sortType);
294     foreach ($entryIterator as $row => $entry){
295       $result.="<tr class='rowxp".($alt&1)."'>";
297       // Render multi select if needed
298       if ($this->multiSelect) {
299         $result.="<td style='text-align:center;width:20px;' class='list0'><input type='checkbox' id='listing_selected_$row' name='listing_selected_$row'></td>";
300       }
302       foreach ($this->xmlData['table']['column'] as $index => $config) {
303         $result.="<td ".$this->colprops[$index]." class='list0'>".$this->renderCell($config['value'], $entry, $row)."</td>";
304       }
305       $result.="</tr>";
307       $alt++;
308     }
310     // Need to fill the list if it's not full (nobody knows why this is 22 ;-))
311     $emptyListStyle= (count($this->entries) + $deps == 0)?"border:0;":"";
312     if ((count($this->entries) + $deps) < 22) {
313       $result.= "<tr>";
314       for ($i= 0; $i<$this->numColumns; $i++) {
315         if ($i == 0) {
316           $result.= "<td class='list1nohighlight' style='$emptyListStyle height:100%;'>&nbsp;</td>";
317           continue;
318         }
319         if ($i != $this->numColumns-1) {
320           $result.= "<td class='list1nohighlight' style='$emptyListStyle'>&nbsp;</td>";
321         } else {
322           $result.= "<td class='list1nohighlight' style='border-right:1px solid #AAA;$emptyListStyle'>&nbsp;</td>";
323         }
324       }
325       $result.= "</tr>";
326     }
328     $result.= "</table></div></td></tr>";
330     // Add the footer if requested
331     if ($this->showFooter) {
332       $result.= "<tr><td class='scrollhead'><table summary='' style='width:100%' cellspacing='0' id='t_scrollfoot'><tr><td class='listfooter' style='border-bottom:0px;'>";
334       foreach ($this->objectTypes as $objectType) {
335         if (isset($this->objectTypeCount[$objectType['label']])) {
336           $label= _($objectType['label']);
337           $result.= "<img class='center' src='".$objectType['image']."' title='$label' alt='$label'>&nbsp;".$this->objectTypeCount[$objectType['label']]."&nbsp;&nbsp;&nbsp;&nbsp;";
338         }
339       }
341       $result.= "<td class='listfooter' style='width:13px;border-right:0px;'>&nbsp;</td></table></td></tr>";
342     }
344     $result.= "</table></div>";
346     $smarty= get_smarty();
347     $smarty->assign("FILTER", $this->filter->render());
348     $smarty->assign("SIZELIMIT", print_sizelimit_warning());
349     $smarty->assign("LIST", $result);
351     // Assign navigation elements
352     $nav= $this->renderNavigation();
353     foreach ($nav as $key => $html) {
354       $smarty->assign($key, $html);
355     }
357     // Assign action menu / base
358     $smarty->assign("ACTIONS", $this->renderActionMenu());
359     $smarty->assign("BASE", $this->renderBase());
361     // Assign separator
362     $smarty->assign("SEPARATOR", "<img src='images/lists/seperator.png' alt='-' align='middle' height='16' width='1' class='center'>");
364     // Assign summary
365     $smarty->assign("HEADLINE", $this->headline);
367     return ($smarty->fetch(get_template_path($this->xmlData['definition']['template'], true)));
368   }
371   function update()
372   {
373     global $config;
374     $ui= get_userinfo();
376     // Reset object counter
377     $this->objectTypeCount= array();
379     // Do not do anything if this is not our PID
380     if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->pid) {
381       return;
382     }
384     // Save base
385     if (isset($_POST['BASE']) && $this->baseMode == true) {
386       $base= validate($_POST['BASE']);
387       if (isset($this->bases[$base])) {
388         $this->base= $base;
389       }
390     }
392     // Override the base if we got a message from the browser navigation
393     if ($this->departmentBrowser && isset($_GET['act'])) {
394       if (preg_match('/^department_([0-9]+)$/', validate($_GET['act']), $match)){
395         if (isset($this->departments[$match[1]])){
396           $this->base= $this->departments[$match[1]]['dn'];
397         }
398       }
399     }
401     // Filter GET with "act" attributes
402     if (isset($_GET['act'])) {
403       $key= validate($_GET['act']);
404       if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
405         // Switch to new column or invert search order?
406         $column= $match[1];
407         if ($this->sortColumn != $column) {
408           $this->sortColumn= $column;
409         } else {
410           $this->sortDirection[$column]= !$this->sortDirection[$column];
411         }
413         // Allow header to update itself according to the new sort settings
414         $this->renderHeader();
415       }
416     }
418     // Override base if we got signals from the navigation elements
419     $action= "";
420     foreach ($_POST as $key => $value) {
421       if (preg_match('/^(ROOT|BACK|HOME)_x$/', $key, $match)) {
422         $action= $match[1];
423         break;
424       }
425     }
427     // Navigation handling
428     if ($action == 'ROOT') {
429       $deps= $ui->get_module_departments($this->module);
430       $this->base= $deps[0];
431     }
432     if ($action == 'BACK') {
433       $deps= $ui->get_module_departments($this->module);
434       $base= preg_replace("/^[^,]+,/", "", $this->base);
435       if(in_array_ics($base, $deps)){
436         $this->base= $base;
437       }
438     }
439     if ($action == 'HOME') {
440       $ui= get_userinfo();
441       $this->base= get_base_from_people($ui->dn);
442     }
444     // Reload departments
445     if ($this->departmentBrowser){
446       $this->departments= $this->getDepartments();
447     }
449     // Update filter and refresh entries
450     $this->filter->setBase($this->base);
451     $this->entries= $this->filter->query();
452   }
455   function parseLayout($layout)
456   {
457     $result= array();
458     $layout= preg_replace("/^\|/", "", $layout);
459     $layout= preg_replace("/\|$/", "", $layout);
460     $cols= split("\|", $layout);
461     foreach ($cols as $index => $config) {
462       if ($config != "") {
463         $components= split(';', $config);
464         $config= "";
465         foreach ($components as $part) {
466           if (preg_match("/^r$/", $part)) {
467             $config.= "text-align:right;";
468             continue;
469           }
470           if (preg_match("/^l$/", $part)) {
471             $config.= "text-align:left;";
472             continue;
473           }
474           if (preg_match("/^c$/", $part)) {
475             $config.= "text-align:center;";
476             continue;
477           }
478           if (preg_match("/^[0-9]+(|px|%)$/", $part)) {
479             $config.= "width:$part;";
480             continue;
481           }
482         }
484         $result[$index]= " style='$config' ";
485       } else {
486         $result[$index]= null;
487       }
488     }
490     // Save number of columns for later use
491     $this->numColumns= count($cols);
493     return $result;
494   }
497   function renderCell($data, $config, $row)
498   {
499     // Replace flat attributes in data string
500     for ($i= 0; $i<$config['count']; $i++) {
501       $attr= $config[$i];
502       $value= "";
503       if (is_array($config[$attr])) {
504         $value= $config[$attr][0];
505       } else {
506         $value= $config[$attr];
507       }
508       $data= preg_replace("/%\{$attr\}/", $value, $data);
509     }
511     // Watch out for filters and prepare to execute them
512     $data= $this->processElementFilter($data, $config, $row);
514     // Replace all non replaced %{...} instances because they
515     // are non resolved attributes or filters
516     $data= preg_replace('/%{[^}]+}/', '&nbsp;', $data);
518     return $data;
519   }
522   function renderBase()
523   {
524     $result= "<select name='BASE' onChange='mainform.submit()' size='1'>";
525     $firstDN= null;
526     $found= false;
528     foreach ($this->bases as $key=>$value) {
529       // Keep first entry to fall back eventually
530       if(!$firstDN) {
531         $firstDN= $key;
532       }
534       // Prepare to render entry
535       $selected= "";
536       if ($key == $this->base) {
537         $selected= " selected";
538         $found= true;
539       }
540       $result.= "<option value='".$key."'$selected>".$value."</option>";
541     }
542     $result.= "</select>";
544     // Reset the currently used base to the first DN we found if there
545     // was no match.
546     if(!$found){
547       $this->base = $firstDN;
548     }
550     return $result;
551   }
554   function processElementFilter($data, $config, $row)
555   {
556     preg_match_all("/%\{filter:([^(]+)\((.*)\)\}/", $data, $matches, PREG_SET_ORDER);
558     foreach ($matches as $match) {
559       if (!isset($this->filters[$match[1]])) {
560         continue;
561       }
562       $cl= preg_replace('/::.*$/', '', $this->filters[$match[1]]);
563       $method= preg_replace('/^.*::/', '', $this->filters[$match[1]]);
565       // Prepare params for function call
566       $params= array();
567       preg_match_all('/"[^"]+"|[^,]+/', $match[2], $parts);
568       foreach ($parts[0] as $param) {
570         // Row is replaced by the row number
571         if ($param == "row") {
572           $params[]= $row;
573         }
575         // pid is replaced by the current PID
576         if ($param == "pid") {
577           $params[]= $this->pid;
578         }
580         // Fixie with "" is passed directly
581         if (preg_match('/^".*"$/', $param)){
582           $params[]= preg_replace('/"/', '', $param);
583         }
585         // LDAP variables get replaced by their objects
586         for ($i= 0; $i<$config['count']; $i++) {
587           if ($param == $config[$i]) {
588             $values= $config[$config[$i]];
589             if (is_array($values)){
590               unset($values['count']);
591             }
592             $params[]= $values;
593           }
594         }
596         // Move dn if needed
597         if ($param == "dn") {
598           $params[]= LDAP::fix($config["dn"]);
599         }
600       }
602       // Replace information
603       if ($cl == "listing") {
604         // Non static call - seems to result in errors
605         $data= @preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($this, "$method"), $params), $data);
606       } else {
607         // Static call
608         $data= preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($cl, $method), $params), $data);
609       }
610     }
612     return $data;
613   }
616   function getObjectType($types, $classes)
617   {
618     // Walk thru types and see if there's something matching
619     foreach ($types as $objectType) {
620       $ocs= $objectType['objectClass'];
621       if (!is_array($ocs)){
622         $ocs= array($ocs);
623       }
625       $found= true;
626       foreach ($ocs as $oc){
627         if (preg_match('/^!(.*)$/', $oc, $match)) {
628           $oc= $match[1];
629           if (in_array($oc, $classes)) {
630             $found= false;
631           }
632         } else {
633           if (!in_array($oc, $classes)) {
634             $found= false;
635           }
636         }
637       }
639       if ($found) {
640         return $objectType;
641       }
642     }
644     return null;
645   }
648   function filterObjectType($dn, $classes)
649   {
650     // Walk thru classes and return on first match
651     $result= "&nbsp;";
653     $objectType= $this->getObjectType($this->objectTypes, $classes);
654     if ($objectType) {
655       $result= "<img class='center' title='".LDAP::fix($dn)."' src='".$objectType["image"]."'>";
656       if (!isset($this->objectTypeCount[$objectType['label']])) {
657         $this->objectTypeCount[$objectType['label']]= 0;
658       }
659       $this->objectTypeCount[$objectType['label']]++;
660     }
661     return $result;
662   }
665   function filterActions($dn, $row, $classes)
666   {
667     // Do nothing if there's no menu defined
668     if (!isset($this->xmlData['actiontriggers']['action'])) {
669       return "&nbsp;";
670     }
672     // Go thru all actions
673     $result= "";
674     $actions= $this->xmlData['actiontriggers']['action'];
675     foreach($actions as $action) {
676       // Skip the entry completely if there's no permission to execute it
677       if (!$this->hasActionPermission($action, $dn)) {
678         continue;
679       }
681       // If there's an objectclass definition and we don't have it
682       // add an empty picture here.
683       if (isset($action['objectclass'])){
684         $objectclass= $action['objectclass'];
685         if (preg_match('/^!(.*)$/', $objectclass, $m)){
686           $objectclass= $m[1];
687           if(in_array($objectclass, $classes)) {
688             $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
689             continue;
690           }
691         } else {
692           if(!in_array($objectclass, $classes)) {
693             $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
694             continue;
695           }
696         }
697       }
699       // Render normal entries as usual
700       if ($action['type'] == "entry") {
701         $label= $this->processElementFilter($action['label'], $this->entries[$row], $row);
702         $image= $this->processElementFilter($action['image'], $this->entries[$row], $row);
703         $result.="<input class='center' type='image' src='$image' title='$label' ".
704                  "name='listing_".$action['name']."_$row' style='padding:1px'>";
705       }
707       // Handle special types
708       if ($action['type'] == "copypaste" || $action['type'] == "snapshot") {
710         $objectType= $this->getObjectType($this->objectTypes, $this->entries[$row]['objectClass']);
711         $category= $class= null;
712         if ($objectType) {
713           $category= $objectType['category'];
714           $class= $objectType['class'];
715         }
717         if ($action['type'] == "copypaste") {
718           $result.= $this->renderCopyPasteActions($row, $this->entries[$row]['dn'], $category, $class);
719         } else {
720           $result.= $this->renderSnapshotActions($row, $this->entries[$row]['dn'], $category, $class);
721         }
722       }
723     }
725     return $result;
726   }
729   function filterDepartmentLink($row, $dn, $description)
730   {
731     $attr= $this->departments[$row]['sort-attribute'];
732     $name= $this->departments[$row][$attr];
733     if (is_array($name)){
734       $name= $name[0];
735     }
736     $result= sprintf("%s [%s]", $name, $description[0]);
737     return("<a href='?plug=".$_GET['plug']."&amp;PID=$this->pid&amp;act=department_$row' title='$dn'>$result</a>");
738   }
741   function filterLink()
742   {
743     $result= "&nbsp;";
745     $row= func_get_arg(0);
746     $pid= $this->pid;
747     $dn= LDAP::fix(func_get_arg(1));
748     $params= array(func_get_arg(2));
750     // Collect sprintf params
751     for ($i = 3;$i < func_num_args();$i++) {
752       $val= func_get_arg($i);
753       if (is_array($val)){
754         $params[]= $val[0];
755         continue;
756       }
757       $params[]= $val;
758     }
760     $result= "&nbsp;";
761     $trans= call_user_func_array("sprintf", $params);
762     if ($trans != "") {
763       return("<a href='?plug=".$_GET['plug']."&amp;PID=$pid&amp;act=listing_edit_$row' title='$dn'>$trans</a>");
764     }
766     return $result;
767   }
770   function renderNavigation()
771   {
772     $result= array();
773     $enableBack = true;
774     $enableRoot = true;
775     $enableHome = true;
777     $ui = get_userinfo();
779     /* Check if base = first available base */
780     $deps = $ui->get_module_departments($this->module);
782     if(!count($deps) || $deps[0] == $this->filter->base){
783       $enableBack = false;
784       $enableRoot = false;
785     }
787     $listhead ="";
789     /* Check if we are in users home  department */
790     if(!count($deps) ||$this->filter->base == get_base_from_people($ui->dn)){
791       $enableHome = false;
792     }
794     /* Draw root button */
795     if($enableRoot){
796       $result["ROOT"]= "<input class='center' type='image' src='images/lists/root.png' align='middle' ".
797                        "title='"._("Go to root department")."' name='ROOT' alt='"._("Root")."'>";
798     }else{
799       $result["ROOT"]= "<img src='images/lists/root_grey.png' class='center' alt='"._("Root")."'>";
800     }
802     /* Draw back button */
803     if($enableBack){
804       $result["BACK"]= "<input class='center' type='image' align='middle' src='images/lists/back.png' ".
805                        "title='"._("Go up one department")."' alt='"._("Up")."' name='BACK'>";
806     }else{
807       $result["BACK"]= "<img src='images/lists/back_grey.png' class='center' alt='"._("Up")."'>";
808     }
810     /* Draw home button */
811     if($enableHome){
812       $result["HOME"]= "<input class='center' type='image' align='middle' src='images/lists/home.png' ".
813                        "title='"._("Go to users department")."' alt='"._("Home")."' name='HOME'>";
814     }else{
815       $result["HOME"]= "<img src='images/lists/home_grey.png' class='center' alt='"._("Home")."'>";
816     }
818     /* Draw reload button, this button is enabled everytime */
819     $result["RELOAD"]= "<input class='center' type='image' src='images/lists/reload.png' align='middle' ".
820                        "title='"._("Reload list")."' name='REFRESH' alt='"._("Submit")."'>";
822     return ($result);
823   }
826   function getAction()
827   {
828     // Do not do anything if this is not our PID
829     if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->pid) {
830       return;
831     }
833     $result= array("targets" => array(), "action" => "");
835     // Filter GET with "act" attributes
836     if (isset($_GET['act'])) {
837       $key= validate($_GET['act']);
838       $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)$/', '$1', $key);
839       if (isset($this->entries[$target]['dn'])) {
840         $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+$/', '$1', $key);
841         $result['targets'][]= $this->entries[$target]['dn'];
842       }
844       // Drop targets if empty
845       if (count($result['targets']) == 0) {
846         unset($result['targets']);
847       }
848       return $result;
849     }
851     // Filter POST with "listing_" attributes
852     foreach ($_POST as $key => $prop) {
854       // Capture selections
855       if (preg_match('/^listing_selected_[0-9]+$/', $key)) {
856         $target= preg_replace('/^listing_selected_([0-9]+)$/', '$1', $key);
857         if (isset($this->entries[$target]['dn'])) {
858           $result['targets'][]= $this->entries[$target]['dn'];
859         }
860         continue;
861       }
863       // Capture action with target - this is a one shot
864       if (preg_match('/^listing_[a-zA-Z_]+_[0-9]+(|_x)$/', $key)) {
865         $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)(|_x)$/', '$1', $key);
866         if (isset($this->entries[$target]['dn'])) {
867           $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+(|_x)$/', '$1', $key);
868           $result['targets']= array($this->entries[$target]['dn']);
869         }
870         break;
871       }
873       // Capture action without target
874       if (preg_match('/^listing_[a-zA-Z_]+(|_x)$/', $key)) {
875         $result['action']= preg_replace('/^listing_([a-zA-Z_]+)(|_x)$/', '$1', $key);
876         continue;
877       }
878     }
880     // Filter POST with "act" attributes -> posted from action menu
881     if (isset($_POST['act']) && $_POST['act'] != '') {
882       $result['action']= validate($_POST['act']);
883     }
885     // Drop targets if empty
886     if (count($result['targets']) == 0) {
887       unset($result['targets']);
888     }
889     return $result;
890   }
893   function renderActionMenu()
894   {
895     // Don't send anything if the menu is not defined
896     if (!isset($this->xmlData['actionmenu']['action'])){
897       return "";
898     }
900     // Load shortcut
901     $actions= &$this->xmlData['actionmenu']['action'];
902     $result= "<input type='hidden' name='act' id='actionmenu' value=''>".
903              "<ul class='level1' id='root'><li><a href='#'>Aktionen&nbsp;<img ".
904              "border=0 src='images/lists/sort-down.png'></a>";
906     // Build ul/li list
907     $result.= $this->recurseActions($actions);
909     return "<div id='pulldown'>".$result."</li></ul><div>";
910   }
913   function recurseActions($actions)
914   {
915     static $level= 2;
916     $result= "<ul class='level$level'>";
917     $separator= "";
919     foreach ($actions as $action) {
921       // Skip the entry completely if there's no permission to execute it
922       if (!$this->hasActionPermission($action, $this->filter->base)) {
923         continue;
924       }
926       // Fill image if set
927       $img= "";
928       if (isset($action['image'])){
929         $img= "<img border=0 src='".$action['image']."'>&nbsp;";
930       }
932       if ($action['type'] == "separator"){
933         $separator= " style='border-top:1px solid #AAA' ";
934         continue;
935       }
937       // Dive into subs
938       if ($action['type'] == "sub" && isset($action['action'])) {
939         $level++;
940         if (isset($action['label'])){
941           $result.= "<li$separator><a href='#'>$img"._($action['label'])."&nbsp;<img border='0' src='images/forward-arrow.png'></a>";
942         }
943         $result.= $this->recurseActions($action['action'])."</li>";
944         $level--;
945         $separator= "";
946         continue;
947       }
949       // Render entry elseways
950       if (isset($action['label'])){
951         $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"".$action['name']."\";mainform.submit();'>$img"._($action['label'])."</a></li>";
952       }
954       // Check for special types
955       switch ($action['type']) {
956         case 'copypaste':
957           $result.= $this->renderCopyPasteMenu($separator);
958           break;
960         case 'snapshot':
961           $result.= $this->renderSnapshotMenu($separator);
962           break;
964         case 'daemon':
965           $result.= $this->renderDaemonMenu($separator);
966           break;
967       }
969       $separator= "";
970     }
972     $result.= "</ul>";
973     return $result;
974   }
977   function hasActionPermission($action, $dn)
978   {
979     $ui= get_userinfo();
981     if (isset($action['acl'])) {
982       $acls= $action['acl'];
983       if (!is_array($acls)) {
984         $acls= array($acls);
985       }
987       // Every ACL has to pass
988       foreach ($acls as $acl) {
989         $module= $this->module;
990         $aclList= array();
992         // Split for category and plugins if needed
993         // match for "[rw]" style entries
994         if (preg_match('/^\[([rwcdm]+)\]$/', $acl, $match)){
995           $aclList= array($match[1]);
996         }
998         // match for "users[rw]" style entries
999         if (preg_match('/^([a-zA-Z0-9]+)\[([rwcdm]+)\]$/', $acl, $match)){
1000           $module= $match[1];
1001           $aclList= array($match[2]);
1002         }
1004         // match for "users/user[rw]" style entries
1005         if (preg_match('/^([a-zA-Z0-9]+\/[a-zA-Z0-9]+)\[([rwcdm]+)\]$/', $acl, $match)){
1006           $module= $match[1];
1007           $aclList= array($match[2]);
1008         }
1010         // match "users/user[userPassword:rw(,...)*]" style entries
1011         if (preg_match('/^([a-zA-Z0-9]+\/[a-zA-Z0-9]+)\[([a-zA-Z0-9]+:[rwcdm]+(,[a-zA-Z0-9]+:[rwcdm]+)*)\]$/', $acl, $match)){
1012           $module= $match[1];
1013           $aclList= split(',', $match[2]);
1014         }
1016         // Walk thru prepared ACL by using $module
1017         foreach($aclList as $sAcl) {
1018           $checkAcl= "";
1020           // Category or detailed permission?
1021           if (strpos('/', $module) === false) {
1022             if (preg_match('/([a-zA-Z0-9]+):([rwcdm]+)/', $sAcl, $m) ) {
1023               $checkAcl= $ui->get_permissions($dn, $module, $m[1]);
1024               $sAcl= $m[2];
1025             } else {
1026               $checkAcl= $ui->get_permissions($dn, $module, '0');
1027             }
1028           } else {
1029             $checkAcl= $ui->get_category_permissions($dn, $module);
1030           }
1032           // Split up remaining part of the acl and check if it we're
1033           // allowed to do something...
1034           $parts= str_split($sAcl);
1035           foreach ($parts as $part) {
1036             if (strpos($checkAcl, $part) === false){
1037               return false;
1038             }
1039           }
1041         }
1042       }
1043     }
1045     return true;
1046   }
1049   function refreshBasesList()
1050   {
1051     global $config;
1052     $ui= get_userinfo();
1054     // Do some array munching to get it user friendly
1055     $ids= $config->idepartments;
1056     $d= $ui->get_module_departments($this->module);
1057     $k_ids= array_keys($ids);
1058     $deps= array_intersect($d,$k_ids);
1060     // Fill internal bases list
1061     $this->bases= array();
1062     foreach($k_ids as $department){
1063       $this->bases[$department] = $ids[$department];
1064     }
1065   }
1068   function getDepartments()
1069   {
1070     $departments= array();
1071     $ui= get_userinfo();
1073     // Get list of supported department types
1074     $types = departmentManagement::get_support_departments();
1076     // Load departments allowed by ACL
1077     $validDepartments = $ui->get_module_departments($this->module);
1079     // Build filter and look in the LDAP for possible sub departments
1080     // of current base
1081     $filter= "(&(objectClass=gosaDepartment)(|";
1082     $attrs= array("description", "objectClass");
1083     foreach($types as $name => $data){
1084       $filter.= "(objectClass=".$data['OC'].")";
1085       $attrs[]= $data['ATTR'];
1086     }
1087     $filter.= "))";
1088     $res= get_list($filter, $this->module, $this->base, $attrs, GL_NONE | GL_SIZELIMIT);
1090     // Analyze list of departments
1091     foreach ($res as $department) {
1092       if (!in_array($department['dn'], $validDepartments)) {
1093         continue;
1094       }
1096       // Add the attribute where we use for sorting
1097       $oc= null;
1098       foreach(array_keys($types) as $type) {
1099         if (in_array($type, $department['objectClass'])) {
1100           $oc= $type;
1101           break;
1102         }
1103       }
1104       $department['sort-attribute']= $types[$oc]['ATTR'];
1106       // Move to the result list
1107       $departments[]= $department;
1108     }
1110     return $departments;
1111   }
1114   function renderCopyPasteMenu($separator, $copy= true, $cut= true)
1115   {
1116     // We can only provide information if we've got a copypaste handler
1117     // instance
1118     if(!(isset($this->copyPasteHandler) && is_object($this->copyPasteHandler))){
1119       return "";
1120     }
1122     // Presets
1123     $result= "";
1124     $read= $paste= false;
1125     $ui= get_userinfo();
1127     // Switch flags to on if there's at least one category which allows read/paste
1128     foreach($this->categories as $category){
1129       $read= $read || preg_match('/r/', $ui->get_category_permissions($this->base, $category));
1130       $paste= $paste || $ui->is_pasteable($this->base, $category) == 1;
1131     }
1134     // Draw entries that allow copy and cut
1135     if($read){
1137       // Copy entry
1138       if($copy){
1139         $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>";
1140         $separator= "";
1141       }
1143       // Cut entry
1144       if($cut){
1145         $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>";
1146         $separator= "";
1147       }
1148     }
1150     // Draw entries that allow pasting entries
1151     if($paste){
1152       if($this->copyPasteHandler->entries_queued()){
1153         $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>";
1154       }else{
1155         $result.= "<li$separator><a href='#'><img src='images/lists/paste-grey.png' alt='' border='0' class='center'>&nbsp;"._("Paste")."</a></li>";
1156       }
1157     }
1158     
1159     return($result);
1160   }
1163   function renderCopyPasteActions($row, $dn, $category, $class, $copy= true, $cut= true)
1164   {
1165     // We can only provide information if we've got a copypaste handler
1166     // instance
1167     if(!(isset($this->copyPasteHandler) && is_object($this->copyPasteHandler))){
1168       return "";
1169     }
1171     // Presets
1172     $ui = get_userinfo();
1173     $result = "";
1175     // Render cut entries
1176     if($cut){
1177       if($ui->is_cutable($dn, $category, $class)){
1178         $result .= "<input class='center' type='image'
1179           src='images/lists/cut.png' alt='"._("Cut")."' name='listing_cut_$row' title='"._("Cut this entry")."' style='padding:1px'>";
1180       }else{
1181         $result.="<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1182       }
1183     }
1185     // Render copy entries
1186     if($copy){
1187       if($ui->is_copyable($dn, $category, $class)){
1188         $result.= "<input class='center' type='image'
1189           src='images/lists/copy.png' alt='"._("Copy")."' name='listing_copy_$row' title='"._("Copy this entry")."' style='padding:1px'>";
1190       }else{
1191         $result.="<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1192       }
1193     }
1195     return($result);
1196   }
1199   function renderSnapshotMenu($separator)
1200   {
1201     // We can only provide information if we've got a snapshot handler
1202     // instance
1203     if(!(isset($this->snapshotHandler) && is_object($this->snapshotHandler))){
1204       return "";
1205     }
1207     // Presets
1208     $result = "";
1209     $ui = get_userinfo();
1211     if($this->snapshotHandler->enabled() && $ui->allow_snapshot_restore($this->base, $this->module)){
1213       // Check if there is something to restore
1214       $restore= false;
1215       foreach($this->snapshotHandler->getSnapshotBases() as $base){
1216         $restore= $restore || count($this->snapshotHandler->getDeletedSnapshots($base)) > 0;
1217       }
1219       // Draw icons according to the restore flag
1220       if($restore){
1221         $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>";
1222       }else{
1223         $result.= "<li$separator><a href='#'><img src='images/lists/restore_grey.png' alt='' border='0' class='center'>&nbsp;"._("Restore snapshots")."</a></li>";
1224       }
1225     }
1227     return($result);
1228   }
1231   function renderSnapshotActions($row, $dn, $category, $class, $copy= true, $cut= true)
1232   {
1233     // We can only provide information if we've got a snapshot handler
1234     // instance
1235     if(!(isset($this->snapshotHandler) && is_object($this->snapshotHandler))){
1236       return "";
1237     }
1239     // Presets
1240     $result= "";
1241     $ui = get_userinfo();
1243     // Only act if enabled here
1244     if($this->snapshotHandler->enabled()){
1246       // Draw restore button
1247       if ($ui->allow_snapshot_restore($dn, $category)){
1249         // Do we have snapshots for this dn?
1250         if($this->snapshotHandler->hasSnapshots($dn)){
1251           $result.= "<input class='center' type='image' src='images/lists/restore.png' ".
1252                      "alt='"._("Restore snapshot")."' name='listing_restore_$row' title='".
1253                      _("Restore snapshot")."' style='padding:1px'>";
1254         } else {
1255           $result.= "<img src='images/lists/restore_grey.png' alt=' ' class='center' style='padding:1px'>";
1256         }
1257       }
1259       // Draw snapshot button
1260       if($ui->allow_snapshot_create($dn, $category)){
1261           $result.= "<input class='center' type='image' src='images/snapshot.png' ".
1262                      "alt='"._("Create snapshot")."' name='listing_snapshot_$row' title='".
1263                      _("Create a new snapshot from this object")."' style='padding:1px'>";
1264       }else{
1265           $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1266       }
1267     }
1269     return($result);
1270   }
1273   function renderDaemonMenu($separator)
1274   {
1275     $result= "";
1277     // If there is a daemon registered, draw the menu entries
1278     if(class_available("DaemonEvent")){
1279       $events= DaemonEvent::get_event_types_by_category($this->categories);
1280       if(isset($events['BY_CLASS']) && count($events['BY_CLASS'])){
1281         foreach($events['BY_CLASS'] as $name => $event){
1282           $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>";
1283           $separator= "";
1284         }
1285       }
1286     }
1288     return $result;
1289   }
1293 ?>