Code

46f427a28fe732500b423f6719494035226369bd
[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 $singleSelect= false;
32   var $template;
33   var $headline;
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 $filter= null;
46   var $pid;
47   var $objectTypes= array();
48   var $objectTypeCount= array();
49   var $objectDnMapping= array();
50   var $copyPasteHandler= null;
51   var $snapshotHandler= null;
52   var $exporter= array();
53   var $exportColumns= array();
54   var $useSpan= false;
55   var $height= 0;
56   var $scrollPosition= 0;
57   var $baseSelector;
60   function listing($filename)
61   {
62     global $config;
63     global $class_mapping;
65     // Initialize pid
66     $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
68     if (!$this->load($filename)) {
69       die("Cannot parse $filename!");
70     }
72     // Set base for filter
73     if ($this->baseMode) {
74       $this->base= session::global_get("CurrentMainBase");
75       if ($this->base == null) {
76         $this->base= $config->current['BASE'];
77       }
78       $this->refreshBasesList();
79     } else {
80       $this->base= $config->current['BASE'];
81     }
83     // Move footer information
84     $this->showFooter= ($config->get_cfg_value("listSummary") == "true");
86     // Register build in filters
87     $this->registerElementFilter("objectType", "listing::filterObjectType");
88     $this->registerElementFilter("departmentLink", "listing::filterDepartmentLink");
89     $this->registerElementFilter("link", "listing::filterLink");
90     $this->registerElementFilter("actions", "listing::filterActions");
92     // Load exporters
93     foreach($class_mapping as $class => $dummy) {
94       if (preg_match('/Exporter$/', $class)) {
95         $info= call_user_func(array($class, "getInfo"));
96         if ($info != null) {
97           $this->exporter= array_merge($this->exporter, $info);
98         }
99       }
100     }
102     // Instanciate base selector
103     $this->baseSelector= new baseSelector($this->bases, $this->base);
104   }
107   function setCopyPasteHandler($handler)
108   {
109     $this->copyPasteHandler= &$handler;
110   }
113   function setHeight($height)
114   {
115     $this->height= $height;
116   }
119   function setSnapshotHandler($handler)
120   {
121     $this->snapshotHandler= &$handler;
122   }
125   function getFilter()
126   { 
127     return($this->filter);
128   }  
131   function setFilter($filter)
132   {
133     $this->filter= &$filter;
134     if ($this->departmentBrowser){
135       $this->departments= $this->getDepartments();
136     }
137     $this->filter->setBase($this->base);
138   }
141   function registerElementFilter($name, $call)
142   {
143     if (!isset($this->filters[$name])) {
144       $this->filters[$name]= $call;
145       return true;
146     }
148     return false;
149   }
152   function load($filename)
153   {
154     $contents = file_get_contents($filename);
155     $this->xmlData= xml::xml2array($contents, 1);
157     if (!isset($this->xmlData['list'])) {
158       return false;
159     }
161     $this->xmlData= $this->xmlData["list"];
163     // Load some definition values
164     foreach (array("departmentBrowser", "departmentRootVisible", "multiSelect","singleSelect", "baseMode") as $token) {
165       if (isset($this->xmlData['definition'][$token]) &&
166           $this->xmlData['definition'][$token] == "true"){
167         $this->$token= true;
168       }
169     }
171     // Fill objectTypes from departments and xml definition
172     $types = departmentManagement::get_support_departments();
173     foreach ($types as $class => $data) {
174       $this->objectTypes[$data['OC']]= array("label" => $data['TITLE'],
175                                   "objectClass" => $data['OC'],
176                                   "image" => $data['IMG']);
177     }
178     $this->categories= array();
179     if (isset($this->xmlData['definition']['objectType'])) {
180       if(isset($this->xmlData['definition']['objectType']['label'])) {
181         $this->xmlData['definition']['objectType']= array($this->xmlData['definition']['objectType']);
182       }
183       foreach ($this->xmlData['definition']['objectType'] as $index => $otype) {
184         $tmp = $this->xmlData['definition']['objectType'][$index];
185         $this->objectTypes[$tmp['objectClass']]= $tmp;
186         if (isset($this->xmlData['definition']['objectType'][$index]['category'])){
187           $this->categories[]= $otype['category'];
188         }
189       }
190     }
191     $this->objectTypes = array_values($this->objectTypes);
193     // Parse layout per column
194     $this->colprops= $this->parseLayout($this->xmlData['table']['layout']);
196     // Prepare table headers
197     $this->renderHeader();
199     // Assign headline/Categories
200     $this->headline= _($this->xmlData['definition']['label']);
201     if (!is_array($this->categories)){
202       $this->categories= array($this->categories);
203     }
205     // Evaluate columns to be exported
206     if (isset($this->xmlData['table']['column'])){
207       foreach ($this->xmlData['table']['column'] as $index => $config) {
208         if (isset($config['export']) && $config['export'] == "true"){
209           $this->exportColumns[]= $index;
210         }
211       }
212     }
214     return true;  
215   }
218   function renderHeader()
219   {
220     $this->header= array();
221     $this->plainHeader= array();
223     // Initialize sort?
224     $sortInit= false;
225     if (!$this->sortDirection) {
226       $this->sortColumn= 0;
227       if (isset($this->xmlData['definition']['defaultSortColumn'])){
228         $this->sortColumn= $this->xmlData['definition']['defaultSortColumn'];
229       } else {
230         $this->sortAttribute= "";
231       }
232       $this->sortDirection= array();
233       $sortInit= true;
234     }
236     if (isset($this->xmlData['table']['column'])){
237       foreach ($this->xmlData['table']['column'] as $index => $config) {
238         // Initialize everything to one direction
239         if ($sortInit) {
240           $this->sortDirection[$index]= false;
241         }
243         $sorter= "";
244         if ($index == $this->sortColumn && isset($config['sortAttribute']) &&
245             isset($config['sortType'])) {
246           $this->sortAttribute= $config['sortAttribute'];
247           $this->sortType= $config['sortType'];
248           $sorter= "&nbsp;".image("images/lists/sort-".($this->sortDirection[$index]?"up":"down").".png", null, $this->sortDirection[$index]?_("Sort ascending"):_("Sort descending"), "text-top");
249         }
250         $sortable= (isset($config['sortAttribute']));
252         $link= "href='?plug=".$_GET['plug']."&amp;PID=".$this->pid."&amp;act=SORT_$index'";
253         if (isset($config['label'])) {
254           if ($sortable) {
255             $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."><a $link>"._($config['label'])."</a>$sorter</td>";
256           } else {
257             $this->header[$index]= "<td class='listheader' ".$this->colprops[$index].">"._($config['label'])."</td>";
258           }
259           $this->plainHeader[]= _($config['label']);
260         } else {
261           if ($sortable) {
262             $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."><a $link>&nbsp;</a>$sorter</td>";
263           } else {
264             $this->header[$index]= "<td class='listheader' ".$this->colprops[$index].">&nbsp;</td>";
265           }
266           $this->plainHeader[]= "";
267         }
268       }
269     }
270   }
273   function render()
274   {
275     // Check for exeeded sizelimit
276     if (($message= check_sizelimit()) != ""){
277       return($message);
278     }
280     // Some browsers don't have the ability do do scrollable table bodies, filter them
281     // here.
282     $switch= false;
283     if (preg_match('/(Opera|Konqueror|Safari)/i', $_SERVER['HTTP_USER_AGENT'])){
284       $switch= true;
285     }
287     // Initialize list
288     $result= "<input type='hidden' value='$this->pid' name='PID'>\n";
289     $result.= "<input type='hidden' name='position_".$this->pid."' id='position_".$this->pid."'>\n";
290     $height= 450;
291     if ($this->height != 0) {
292       $result.= "<input type='hidden' value='$this->height' id='d_height'>\n";
293       $height= $this->height;
294     }
295     
296     $result.= "<div class='listContainer' id='d_scrollbody' style='min-height:".($height+25)."px;'>\n";
297     $result.= "<table summary='$this->headline' style='width:100%;table-layout:fixed' cellspacing='0' cellpadding='0' id='t_scrolltable'>\n";
298     $this->numColumns= count($this->colprops) + (($this->multiSelect|$this->singleSelect)?1:0);
300     // Build list header
301     $result.= "<thead class='fixedListHeader listHeaderFormat'><tr>\n";
302     if ($this->multiSelect || $this->singleSelect) {
303       $width= "24px";
304       if (preg_match('/Konqueror/i', $_SERVER['HTTP_USER_AGENT'])){
305         $width= "28px";
306       }
307       $result.= "<td class='listheader' style='text-align:center;padding:0;width:$width;'>";
308       if($this->multiSelect){
309           $result.= "<input type='checkbox' id='select_all' name='select_all' 
310               title='"._("Select all")."' onClick='toggle_all_(\"listing_selected_[0-9]*$\",\"select_all\");' >";
311       }else{
312           $result.= "&nbsp;";
313       }
314       $result.="</td>\n";
315     }
316     foreach ($this->header as $header) {
317       $result.= $header;
318     }
319     $result.= "</tr></thead>\n";
321     // Build list body
322     $result.= "<tbody class='listScrollContent listBodyFormat' id='t_nscrollbody' style='height:".$height."px;'>\n";
324     // No results? Just take an empty colspanned row
325     if (count($this->entries) + count($this->departments) == 0) {
326       $result.= "<tr><td class='list1nohighlight' colspan='$this->numColumns' style='height:100%;border-right:0px;width:100%;'>&nbsp;</td></tr>";
327     }
329     // Line color alternation
330     $alt= 0;
331     $deps= 0;
333     // Draw department browser if configured and we're not in sub mode
334     $this->useSpan= false;
335     if ($this->departmentBrowser && $this->filter->scope != "sub") {
336       // Fill with department browser if configured this way
337       $departmentIterator= new departmentSortIterator($this->departments, $this->sortDirection[$this->sortColumn]);
338       foreach ($departmentIterator as $row => $entry){
339         $result.="<tr>";
341         // Render multi select if needed
342         if ($this->multiSelect || $this->singleSelect) {
343           $result.="<td style='text-align:center;padding:0;' class='list1'>&nbsp;</td>";
344         }
346         // Render defined department columns, fill the rest with some stuff
347         $rest= $this->numColumns - 1;
348         foreach ($this->xmlData['table']['department'] as $index => $config) {
349           $colspan= 1;
350           if (isset($config['span'])){
351             $colspan= $config['span'];
352             $this->useSpan= true;
353           }
354           $result.="<td colspan='$colspan' ".$this->colprops[$index]." class='list1'>".$this->renderCell($config['value'], $entry, $row)."</td>";
355           $rest-= $colspan;
356         }
358         // Fill remaining cols with nothing
359         $last= $this->numColumns - $rest;
360         for ($i= 0; $i<$rest; $i++){
361           $result.= "<td ".$this->colprops[$last+$i-1]." class='list1'>&nbsp;</td>";
362         }
363         $result.="</tr>";
365         $alt++;
366       }
367       $deps= $alt;
368     }
370     // Fill with contents, sort as configured
371     foreach ($this->entries as $row => $entry) {
372       $trow= "";
374       // Render multi select if needed
375       if ($this->multiSelect) {
376         $trow.="<td style='text-align:center;width:20px;' class='list0'><input type='checkbox' id='listing_selected_$row' name='listing_selected_$row'></td>\n";
377       }
379       if ($this->singleSelect) {
380         $trow.="<td style='text-align:center;width:20px;' class='list0'><input type='radio' id='listing_radio_selected_$row' name='listing_radio_selected' value='{$row}'></td>\n";
381       }
383       foreach ($this->xmlData['table']['column'] as $index => $config) {
384         $renderedCell= $this->renderCell($config['value'], $entry, $row);
385         $trow.="<td ".$this->colprops[$index]." class='list0'>".$renderedCell."</td>\n";
387         // Save rendered column
388         $sort= preg_replace('/.*>([^<]+)<.*$/', '$1', $renderedCell);
389         $sort= preg_replace('/&nbsp;/', '', $sort);
390         if (preg_match('/</', $sort)){
391           $sort= "";
392         }
393         $this->entries[$row]["_sort$index"]= $sort;
394       }
396       // Save rendered entry
397       $this->entries[$row]['_rendered']= $trow;
398     }
400     // Complete list by sorting entries for _sort$index and appending them to the output
401     $entryIterator= new listingSortIterator($this->entries, $this->sortDirection[$this->sortColumn], "_sort".$this->sortColumn, $this->sortType);
402     foreach ($entryIterator as $row => $entry){
404       // Apply label to objecttype icon?
405        if (preg_match("/<objectType:([^:]+):(.*)\/>/i", $entry['_rendered'], $matches)){
406            if (preg_match("/<rowLabel:([a-z0-9_-]*)\/>/i", $entry['_rendered'], $m)) {
407                $objectType= image($matches[1]."[".$m[1]."]", null, LDAP::fix(base64_decode($matches[2])));
408            } else {
409                $objectType= image($matches[1], null, LDAP::fix(base64_decode($matches[2])));
410            }
411            $entry['_rendered']= preg_replace("/<objectType[^>]+>/", $objectType, $entry['_rendered']);
412            $entry['_rendered']= preg_replace("/<rowLabel[^>]+>/", '', $entry['_rendered']);
413       }
415       // Apply custom class to row?
416       if (preg_match("/<rowClass:([a-z0-9_-]*)\/>/i", $entry['_rendered'], $matches)) {
417         $result.="<tr class='".$matches[1]."'>\n";
418         $result.= preg_replace("/<rowClass[^>]+>/", '', $entry['_rendered']);
419       } else {
420         $result.="<tr>\n";
421         $result.= $entry['_rendered'];
422       }
424       $result.="</tr>\n";
425       $alt++;
426     }
428     // Need to fill the list if it's not full (nobody knows why this is 22 ;-))
429     $emptyListStyle= (count($this->entries) + (($this->useSpan && count($this->entries))?$deps:0) == 0)?"border:0;":"";
430     if ((count($this->entries) + $deps) < 22) {
431       $result.= "<tr>";
432       for ($i= 0; $i<$this->numColumns; $i++) {
433         if ($i == 0) {
434           $result.= "<td class='list1nohighlight' style='$emptyListStyle height:100%;'>&nbsp;</td>";
435           continue;
436         }
437         if ($i != $this->numColumns-1) {
438           $result.= "<td class='list1nohighlight' style='$emptyListStyle'>&nbsp;</td>";
439         } else {
440           $result.= "<td class='list1nohighlight' style='border-right:0;$emptyListStyle'>&nbsp;</td>";
441         }
442       }
444       $result.= "</tr>";
445     }
447     // Close list body
448     $result.= "</tbody></table></div>";
450     // Add the footer if requested
451     if ($this->showFooter) {
452       $result.= "<div class='nlistFooter'><div style='padding:3px'>";
454       foreach ($this->objectTypes as $objectType) {
455         if (isset($this->objectTypeCount[$objectType['label']])) {
456           $label= _($objectType['label']);
457           $result.= image($objectType['image'], null, $label)."&nbsp;".$this->objectTypeCount[$objectType['label']]."&nbsp;&nbsp;";
458         }
459       }
461       $result.= "</div></div>";
462     }
464     // Close list
465     $result.= $switch?"<input type='hidden' id='list_workaround'>":"";
467     // Add scroll positioner
468     $result.= '<script type="text/javascript" language="javascript">';
469     $result.= '$("t_nscrollbody").scrollTop= '.$this->scrollPosition.';';
470     $result.= 'var box = $("t_nscrollbody").onscroll= function() {$("position_'.$this->pid.'").value= this.scrollTop;}';
471     $result.= '</script>';
473     $smarty= get_smarty();
475     $smarty->assign("FILTER", $this->filter->render());
476     $smarty->assign("SIZELIMIT", print_sizelimit_warning());
477     $smarty->assign("LIST", $result);
479     // Assign navigation elements
480     $nav= $this->renderNavigation();
481     foreach ($nav as $key => $html) {
482       $smarty->assign($key, $html);
483     }
485     // Assign action menu / base
486     $smarty->assign("HEADLINE", $this->headline);
487     $smarty->assign("ACTIONS", $this->renderActionMenu());
488     $smarty->assign("BASE", $this->renderBase());
490     // Assign separator
491     $smarty->assign("SEPARATOR", "<img src='images/lists/seperator.png' alt='-' align='middle' height='16' width='1' class='center'>");
493     // Assign summary
494     $smarty->assign("HEADLINE", $this->headline);
496     // Try to load template from plugin the folder first...
497     $file = get_template_path($this->xmlData['definition']['template'], true);
499     // ... if this fails, try to load the file from the theme folder.
500     if(!file_exists($file)){
501       $file = get_template_path($this->xmlData['definition']['template']);
502     }
504     return ($smarty->fetch($file));
505   }
508   function update()
509   {
510     global $config;
511     $ui= get_userinfo();
513     // Take care of base selector
514     if ($this->baseMode) {
515       $this->baseSelector->update();
517       // Check if a wrong base was supplied
518       if(!$this->baseSelector->checkLastBaseUpdate()){
519          msg_dialog::display(_("Error"), msgPool::check_base(), ERROR_DIALOG);
520       }
521     }
523     // Save base
524     $refresh= false;
525     if ($this->baseMode) {
526       $this->base= $this->baseSelector->getBase();
527       session::global_set("CurrentMainBase", $this->base);
528       $refresh= true;
529     }
532     // Reset object counter / DN mapping
533     $this->objectTypeCount= array();
534     $this->objectDnMapping= array();
536     // Do not do anything if this is not our PID
537     if($refresh || !(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->pid)) {
539       // Save position if set
540       if (isset($_POST['position_'.$this->pid]) && is_numeric($_POST['position_'.$this->pid])) {
541         $this->scrollPosition= $_POST['position_'.$this->pid];
542       }
544       // Override the base if we got a message from the browser navigation
545       if ($this->departmentBrowser && isset($_GET['act'])) {
546         if (preg_match('/^department_([0-9]+)$/', validate($_GET['act']), $match)){
547           if (isset($this->departments[$match[1]])){
548             $this->base= $this->departments[$match[1]]['dn'];
549             if ($this->baseMode) {
550               $this->baseSelector->setBase($this->base);
551             }
552             session::global_set("CurrentMainBase", $this->base);
553           }
554         }
555       }
557       // Filter POST with "act" attributes -> posted from action menu
558       if (isset($_POST['exec_act']) && $_POST['act'] != '') {
559         if (preg_match('/^export.*$/', $_POST['act']) && isset($this->exporter[$_POST['act']])) {
560           $exporter= $this->exporter[$_POST['act']];
561           $userinfo= ", "._("created by")." ".$ui->cn." - ".strftime('%A, %d. %B %Y, %H:%M:%S');
562           $entryIterator= new listingSortIterator($this->entries, $this->sortDirection[$this->sortColumn], "_sort".$this->sortColumn, $this->sortType);
563           $sortedEntries= array();
564           foreach ($entryIterator as $entry){
565             $sortedEntries[]= $entry;
566           }
567           $instance= new $exporter['class']($this->headline.$userinfo, $this->plainHeader, $sortedEntries, $this->exportColumns);
568           $type= call_user_func(array($exporter['class'], "getInfo"));
569           $type= $type[$_POST['act']];
570           send_binary_content($instance->query(), $type['filename'], $type= $type['mime']);
571         }
572       }
574       // Filter GET with "act" attributes
575       if (isset($_GET['act'])) {
576         $key= validate($_GET['act']);
577         if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
578           // Switch to new column or invert search order?
579           $column= $match[1];
580           if ($this->sortColumn != $column) {
581             $this->sortColumn= $column;
582           } else {
583             $this->sortDirection[$column]= !$this->sortDirection[$column];
584           }
586           // Allow header to update itself according to the new sort settings
587           $this->renderHeader();
588         }
589       }
591       // Override base if we got signals from the navigation elements
592       $action= "";
593       foreach ($_POST as $key => $value) {
594         if (preg_match('/^(ROOT|BACK|HOME)(_x)?$/', $key, $match)) {
595           $action= $match[1];
596           break;
597         }
598       }
600       // Navigation handling
601       if ($action == 'ROOT') {
602         $deps= $ui->get_module_departments($this->categories);
603         $this->base= $deps[0];
604         $this->baseSelector->setBase($this->base);
605         session::global_set("CurrentMainBase", $this->base);
606       }
607       if ($action == 'BACK') {
608         $deps= $ui->get_module_departments($this->categories);
609         $base= preg_replace("/^[^,]+,/", "", $this->base);
610         if(in_array_ics($base, $deps)){
611           $this->base= $base;
612           $this->baseSelector->setBase($this->base);
613           session::global_set("CurrentMainBase", $this->base);
614         }
615       }
616       if ($action == 'HOME') {
617         $ui= get_userinfo();
618         $this->base= get_base_from_people($ui->dn);
619         $this->baseSelector->setBase($this->base);
620         session::global_set("CurrentMainBase", $this->base);
621       }
622     }
624     // Reload departments
625     if ($this->departmentBrowser){
626       $this->departments= $this->getDepartments();
627     }
629     // Update filter and refresh entries
630     $this->filter->setBase($this->base);
631     $this->entries= $this->filter->query();
633     // Fix filter if querie returns NULL
634     if ($this->entries == null) {
635       $this->entries= array();
636     }
637   }
640   function setBase($base)
641   {
642     $this->base= $base;
643     if ($this->baseMode) {
644       $this->baseSelector->setBase($this->base);
645     }
646   }
649   function getBase()
650   {
651     return $this->base;
652   }
655   function parseLayout($layout)
656   {
657     $result= array();
658     $layout= preg_replace("/^\|/", "", $layout);
659     $layout= preg_replace("/\|$/", "", $layout);
660     $cols= explode("|", $layout);
662     foreach ($cols as $index => $config) {
663       if ($config != "") {
664         $res= "";
665         $components= explode(';', $config);
666         foreach ($components as $part) {
667           if (preg_match("/^r$/", $part)) {
668             $res.= "text-align:right;";
669             continue;
670           }
671           if (preg_match("/^l$/", $part)) {
672             $res.= "text-align:left;";
673             continue;
674           }
675           if (preg_match("/^c$/", $part)) {
676             $res.= "text-align:center;";
677             continue;
678           }
679           if (preg_match("/^[0-9]+(|px|%)$/", $part)) {
680             $res.= "width:$part;min-width:$part;";
681             continue;
682           }
683         }
685         // Add minimum width for scalable columns
686         if (!preg_match('/width:/', $res)){
687           $res.= "min-width:200px;";
688         }
690         $result[$index]= " style='$res'";
691       } else {
692         $result[$index]= " style='min-width:100px;'";
693       }
694     }
696     // Save number of columns for later use
697     $this->numColumns= count($cols);
699     // Add no border to the last column
700     $result[$this->numColumns-1]= preg_replace("/'$/", "border-right:0;'", $result[$this->numColumns-1]);
702     return $result;
703   }
706   function renderCell($data, $config, $row)
707   {
708     // Replace flat attributes in data string
709     for ($i= 0; $i<$config['count']; $i++) {
710       $attr= $config[$i];
711       $value= "";
712       if (is_array($config[$attr])) {
713         $value= $config[$attr][0];
714       } else {
715         $value= $config[$attr];
716       }
717       $data= preg_replace("/%\{$attr\}/", $value, $data);
718     }
720     // Watch out for filters and prepare to execute them
721     $data= $this->processElementFilter($data, $config, $row);
723     // Replace all non replaced %{...} instances because they
724     // are non resolved attributes or filters
725     $data= preg_replace('/%{[^}]+}/', '&nbsp;', $data);
727     return $data;
728   }
731   function renderBase()
732   {
733     if (!$this->baseMode) {
734       return;
735     }
737     return $this->baseSelector->render();
738   }
741   function processElementFilter($data, $config, $row)
742   {
743     preg_match_all("/%\{filter:([^(]+)\((.*)\)\}/", $data, $matches, PREG_SET_ORDER);
745     foreach ($matches as $match) {
746       $cl= "";
747       $method= "";
748       if (preg_match('/::/', $match[1])) {
749         $cl= preg_replace('/::.*$/', '', $match[1]);
750         $method= preg_replace('/^.*::/', '', $match[1]);
751       } else {
752         if (!isset($this->filters[$match[1]])) {
753           continue;
754         }
755         $cl= preg_replace('/::.*$/', '', $this->filters[$match[1]]);
756         $method= preg_replace('/^.*::/', '', $this->filters[$match[1]]);
757       }
759       // Prepare params for function call
760       $params= array();
761       preg_match_all('/"[^"]+"|[^,]+/', $match[2], $parts);
762       foreach ($parts[0] as $param) {
764         // Row is replaced by the row number
765         if ($param == "row") {
766           $params[]= $row;
767           continue;
768         }
770         // pid is replaced by the current PID
771         if ($param == "pid") {
772           $params[]= $this->pid;
773           continue;
774         }
776         // base is replaced by the current base
777         if ($param == "base") {
778           $params[]= $this->getBase();
779           continue;
780         }
782         // Fixie with "" is passed directly
783         if (preg_match('/^".*"$/', $param)){
784           $params[]= preg_replace('/"/', '', $param);
785           continue;
786         }
788         // Move dn if needed
789         if ($param == "dn") {
790           $params[]= LDAP::fix($config["dn"]);
791           continue;
792         }
794         // LDAP variables get replaced by their objects
795         for ($i= 0; $i<$config['count']; $i++) {
796           if ($param == $config[$i]) {
797             $values= $config[$config[$i]];
798             if (is_array($values)){
799               unset($values['count']);
800             }
801             $params[]= $values;
802             break;
803           }
804         }
805       }
807       // Replace information
808       if ($cl == "listing") {
809         // Non static call - seems to result in errors
810         $data= @preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($this, "$method"), $params), $data);
811       } else {
812         // Static call
813         $data= preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($cl, $method), $params), $data);
814       }
815     }
817     return $data;
818   }
821   function getObjectType($types, $classes)
822   {
823     // Walk thru types and see if there's something matching
824     foreach ($types as $objectType) {
825       $ocs= $objectType['objectClass'];
826       if (!is_array($ocs)){
827         $ocs= array($ocs);
828       }
830       $found= true;
831       foreach ($ocs as $oc){
832         if (preg_match('/^!(.*)$/', $oc, $match)) {
833           $oc= $match[1];
834           if (in_array($oc, $classes)) {
835             $found= false;
836           }
837         } else {
838           if (!in_array($oc, $classes)) {
839             $found= false;
840           }
841         }
842       }
844       if ($found) {
845         return $objectType;
846       }
847     }
849     return null;
850   }
853   function filterObjectType($dn, $classes)
854   {
855     // Walk thru classes and return on first match
856     $result= "&nbsp;";
858     $objectType= $this->getObjectType($this->objectTypes, $classes);
859     if ($objectType) {
860       $this->objectDnMapping[$dn]= $objectType["objectClass"];
861       $result= "<objectType:".$objectType["image"].":".base64_encode(LDAP::fix($dn))."/>";
862       if (!isset($this->objectTypeCount[$objectType['label']])) {
863         $this->objectTypeCount[$objectType['label']]= 0;
864       }
865       $this->objectTypeCount[$objectType['label']]++;
866     }
868     return $result;
869   }
872   function filterActions($dn, $row, $classes)
873   {
874     // Do nothing if there's no menu defined
875     if (!isset($this->xmlData['actiontriggers']['action'])) {
876       return "&nbsp;";
877     }
879     // Go thru all actions
880     $result= "";
881     $actions= $this->xmlData['actiontriggers']['action'];
883     // Ensure we've a valid actions array, if there is only one action in the actiontriggers col
884     //  then we've to create a valid array here.
885     if(isset($actions['name'])) $actions = array($actions);
887     foreach($actions as $action) {
888       // Skip the entry completely if there's no permission to execute it
889       if (!$this->hasActionPermission($action, $dn, $classes)) {
890         $result.= image('images/empty.png');
891         continue;
892       }
894       // Skip entry if the pseudo filter does not fit
895       if (isset($action['filter']) && preg_match('/^[a-z0-9_]+!?=[a-z0-9_]+$/i', $action['filter'])) {
896         list($fa, $fv)= explode('=', $action['filter']);
897         if (preg_match('/^(.*)!$/', $fa, $m)){
898           $fa= $m[1];
899           if (isset($this->entries[$row][$fa]) && $this->entries[$row][$fa][0] == $fv) {
900             $result.= image('images/empty.png');
901             continue;
902           }
903         } else {
904           if (!isset($this->entries[$row][$fa]) && !$this->entries[$row][$fa][0] == $fv) {
905             $result.= image('images/empty.png');
906             continue;
907           }
908         }
909       }
912       // If there's an objectclass definition and we don't have it
913       // add an empty picture here.
914       if (isset($action['objectclass'])){
915         $objectclass= $action['objectclass'];
916         if (preg_match('/^!(.*)$/', $objectclass, $m)){
917           $objectclass= $m[1];
918           if(in_array($objectclass, $classes)) {
919             $result.= image('images/empty.png');
920             continue;
921           }
922         } elseif (is_string($objectclass)) {
923           if(!in_array($objectclass, $classes)) {
924             $result.= image('images/empty.png');
925             continue;
926           }
927         } elseif (is_array($objectclass)) {
928           if(count(array_intersect($objectclass, $classes)) != count($objectclass)){
929             $result.= image('images/empty.png');
930             continue;
931           }
932         }
933       }
935       // Render normal entries as usual
936       if ($action['type'] == "entry") {
937         $label= $this->processElementFilter($action['label'], $this->entries[$row], $row);
938         $image= $this->processElementFilter($action['image'], $this->entries[$row], $row);
939         $result.= image($image, "listing_".$action['name']."_$row", $label);
940       }
942       // Handle special types
943       if ($action['type'] == "copypaste" || $action['type'] == "snapshot") {
945         $objectType= $this->getObjectType($this->objectTypes, $this->entries[$row]['objectClass']);
946         $category= $class= null;
947         if ($objectType) {
948           $category= $objectType['category'];
949           $class= $objectType['class'];
950         }
952         if ($action['type'] == "copypaste") {
953           $copy = !isset($action['copy']) || $action['copy'] == "true";
954           $cut = !isset($action['cut']) || $action['cut'] == "true";
955           $result.= $this->renderCopyPasteActions($row, $this->entries[$row]['dn'], $category, $class,$copy,$cut);
956         } else {
957           $result.= $this->renderSnapshotActions($row, $this->entries[$row]['dn'], $category, $class);
958         }
959       }
960     }
962     return $result;
963   }
966   function filterDepartmentLink($row, $dn, $description)
967   {
968     $attr= $this->departments[$row]['sort-attribute'];
969     $name= $this->departments[$row][$attr];
970     if (is_array($name)){
971       $name= $name[0];
972     }
973     $result= sprintf("%s [%s]", $name, $description[0]);
974     return("<a href='?plug=".$_GET['plug']."&amp;PID=$this->pid&amp;act=department_$row' title='$dn'>$result</a>");
975   }
978   function filterLink()
979   {
980     $result= "&nbsp;";
982     $row= func_get_arg(0);
983     $pid= $this->pid;
984     $dn= LDAP::fix(func_get_arg(1));
985     $params= array(func_get_arg(2));
987     // Collect sprintf params
988     for ($i = 3;$i < func_num_args();$i++) {
989       $val= func_get_arg($i);
990       if (is_array($val)){
991         $params[]= $val[0];
992         continue;
993       }
994       $params[]= $val;
995     }
997     $result= "&nbsp;";
998     $trans= call_user_func_array("sprintf", $params);
999     if ($trans != "") {
1000       return("<a href='?plug=".$_GET['plug']."&amp;PID=$pid&amp;act=listing_edit_$row' title='$dn'>$trans</a>");
1001     }
1003     return $result;
1004   }
1007   function renderNavigation()
1008   {
1009     $result= array();
1010     $enableBack = true;
1011     $enableRoot = true;
1012     $enableHome = true;
1014     $ui = get_userinfo();
1016     /* Check if base = first available base */
1017     $deps = $ui->get_module_departments($this->categories);
1019     if(!count($deps) || $deps[0] == $this->filter->base){
1020       $enableBack = false;
1021       $enableRoot = false;
1022     }
1024     $listhead ="";
1026     /* Check if we are in users home  department */
1027     if(!count($deps) || $this->filter->base == get_base_from_people($ui->dn)){
1028       $enableHome = false;
1029     }
1031     /* Draw root button */
1032     if($enableRoot){
1033       $result["ROOT"]= image('images/lists/root.png', 'ROOT', _("Root"));
1034     }else{
1035       $result["ROOT"]= image('images/lists/root-grey.png', null, _("Root"));
1036     }
1038     /* Draw back button */
1039     if($enableBack){
1040       $result["BACK"]= image('images/lists/back.png', 'BACK', _("Go to preceding level"));
1041     }else{
1042       $result["BACK"]= image('images/lists/back-grey.png', null, _("Go to preceding level"));
1043     }
1045     /* Draw home button */
1046    /* Draw home button */
1047     if($enableHome){
1048       $result["HOME"]= image('images/lists/home.png', 'HOME', _("Go to current users level"));
1049     }else{
1050       $result["HOME"]= image('images/lists/home-grey.png', null, _("Go to current users level"));
1051     }
1054     /* Draw reload button, this button is enabled everytime */
1055     $result["RELOAD"]= image('images/lists/reload.png', 'REFRESH', _("Reload list"));
1057     return ($result);
1058   }
1061   function getAction()
1062   {
1063     // Do not do anything if this is not our PID, or there's even no PID available...
1064     if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->pid) {
1065       return;
1066     }
1068     // Save position if set
1069     if (isset($_POST['position_'.$this->pid]) && is_numeric($_POST['position_'.$this->pid])) {
1070       $this->scrollPosition= $_POST['position_'.$this->pid];
1071     }
1073     $result= array("targets" => array(), "action" => "");
1075     // Filter GET with "act" attributes
1076     if (isset($_GET['act'])) {
1077       $key= validate($_GET['act']);
1078       $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)$/', '$1', $key);
1079       if (isset($this->entries[$target]['dn'])) {
1080         $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+$/', '$1', $key);
1081         $result['targets'][]= $this->entries[$target]['dn'];
1082       }
1084       // Drop targets if empty
1085       if (count($result['targets']) == 0) {
1086         unset($result['targets']);
1087       }
1088       return $result;
1089     }
1091     // Get single selection (radio box)
1092     if($this->singleSelect && isset($_POST['listing_radio_selected'])){
1093         $entry = $_POST['listing_radio_selected'];
1094         $result['targets']= array($this->entries[$entry]['dn']);
1095     }
1097     // Filter POST with "listing_" attributes
1098     foreach ($_POST as $key => $prop) {
1100       // Capture selections
1101       if (preg_match('/^listing_selected_[0-9]+$/', $key)) {
1102         $target= preg_replace('/^listing_selected_([0-9]+)$/', '$1', $key);
1103         if (isset($this->entries[$target]['dn'])) {
1104           $result['targets'][]= $this->entries[$target]['dn'];
1105         }
1106         continue;
1107       }
1109       // Capture action with target - this is a one shot
1110       if (preg_match('/^listing_[a-zA-Z_]+_[0-9]+(|_x)$/', $key)) {
1111         $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)(|_x)$/', '$1', $key);
1112         if (isset($this->entries[$target]['dn'])) {
1113           $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+(|_x)$/', '$1', $key);
1114           $result['targets']= array($this->entries[$target]['dn']);
1115         }
1116         break;
1117       }
1119       // Capture action without target
1120       if (preg_match('/^listing_[a-zA-Z_]+(|_x)$/', $key)) {
1121         $result['action']= preg_replace('/^listing_([a-zA-Z_]+)(|_x)$/', '$1', $key);
1122         continue;
1123       }
1124     }
1126     // Filter POST with "act" attributes -> posted from action menu
1127     if (isset($_POST['act']) && $_POST['act'] != '') {
1128       if (!preg_match('/^export.*$/', $_POST['act'])){
1129         $result['action']= validate($_POST['act']);
1130       }
1131     }
1133     // Drop targets if empty
1134     if (count($result['targets']) == 0) {
1135       unset($result['targets']);
1136     }
1137     return $result;
1138   }
1141   function renderActionMenu()
1142   {
1143     $result= "<input type='hidden' name='act' id='act' value=''><div style='display:none'><input type='submit' name='exec_act' id='exec_act' value=''></div>";
1145     // Don't send anything if the menu is not defined
1146     if (!isset($this->xmlData['actionmenu']['action'])){
1147       return $result;
1148     }
1150     // Array?
1151     if (isset($this->xmlData['actionmenu']['action']['type'])){
1152       $this->xmlData['actionmenu']['action']= array($this->xmlData['actionmenu']['action']);
1153     }
1155     // Load shortcut
1156     $actions= &$this->xmlData['actionmenu']['action'];
1157     $result.= "<ul class='level1' id='root'><li><a href='#'>"._("Actions")."&nbsp;".image("images/lists/sort-down.png")."</a>";
1159     // Build ul/li list
1160     $result.= $this->recurseActions($actions);
1162     return "<div id='pulldown'>".$result."</li></ul></div>";
1163   }
1166   function recurseActions($actions)
1167   {
1168     global $class_mapping;
1169     static $level= 2;
1170     $result= "<ul class='level$level'>";
1171     $separator= "";
1173     foreach ($actions as $action) {
1175       // Skip the entry completely if there's no permission to execute it
1176       if (!$this->hasActionPermission($action, $this->filter->base)) {
1177         continue;
1178       }
1180       // Skip entry if there're missing dependencies
1181       if (isset($action['depends'])) {
1182         $deps= is_array($action['depends'])?$action['depends']:array($action['depends']);
1183         foreach($deps as $clazz) {
1184           if (!isset($class_mapping[$clazz])){
1185             continue 2;
1186           }
1187         }
1188       }
1190       // Fill image if set
1191       $img= "";
1192       if (isset($action['image'])){
1193         $img= image($action['image'])."&nbsp;";
1194       }
1196       if ($action['type'] == "separator"){
1197         $separator= " style='border-top:1px solid #AAA' ";
1198         continue;
1199       }
1201       // Dive into subs
1202       if ($action['type'] == "sub" && isset($action['action'])) {
1203         $level++;
1204         if (isset($action['label'])){
1205           $result.= "<li$separator><a href='#'>$img"._($action['label'])."&nbsp;".image('images/forward-arrow.png')."</a>";
1206         }
1208         // Ensure we've an array of actions, this enables sub menus with only one action.
1209         if(isset($action['action']['type'])){
1210           $action['action'] = array($action['action']);
1211         }
1213         $result.= $this->recurseActions($action['action'])."</li>";
1214         $level--;
1215         $separator= "";
1216         continue;
1217       }
1219       // Render entry elseways
1220       if (isset($action['label'])){
1221         $result.= "<li$separator><a href='#' onClick='\$(\"act\").value= \"".$action['name']."\";\$(\"exec_act\").click();'>$img"._($action['label'])."</a></li>";
1222       }
1224       // Check for special types
1225       switch ($action['type']) {
1226         case 'copypaste':
1227           $cut = !isset($action['cut']) || $action['cut'] != "false";
1228           $copy = !isset($action['copy']) || $action['copy'] != "false";
1229           $result.= $this->renderCopyPasteMenu($separator, $copy , $cut);
1230           break;
1232         case 'snapshot':
1233           $result.= $this->renderSnapshotMenu($separator);
1234           break;
1236         case 'exporter':
1237           $result.= $this->renderExporterMenu($separator);
1238           break;
1240         case 'daemon':
1241           $result.= $this->renderDaemonMenu($separator);
1242           break;
1243       }
1245       $separator= "";
1246     }
1248     $result.= "</ul>";
1249     return $result;
1250   }
1253   function hasActionPermission($action, $dn, $classes= null)
1254   {
1255     $ui= get_userinfo();
1257     if (isset($action['acl'])) {
1258       $acls= $action['acl'];
1259       if (!is_array($acls)) {
1260         $acls= array($acls);
1261       }
1263       // Every ACL has to pass
1264       foreach ($acls as $acl) {
1265         $module= $this->categories;
1266         $aclList= array();
1268         // Replace %acl if available
1269         if ($classes) {
1270           $otype= $this->getObjectType($this->objectTypes, $classes);
1271           $acl= str_replace('%acl', $otype['category']."/".$otype['class'], $acl);
1272         }
1274         // Split for category and plugins if needed
1275         // match for "[rw]" style entries
1276         if (preg_match('/^\[([rwcdm]+)\]$/', $acl, $match)){
1277           $aclList= array($match[1]);
1278         }
1280         // match for "users[rw]" style entries
1281         if (preg_match('/^([a-zA-Z0-9]+)\[([rwcdm]+)\]$/', $acl, $match)){
1282           $module= $match[1];
1283           $aclList= array($match[2]);
1284         }
1286         // match for "users/user[rw]" style entries
1287         if (preg_match('/^([a-zA-Z0-9]+\/[a-zA-Z0-9]+)\[([rwcdm]+)\]$/', $acl, $match)){
1288           $module= $match[1];
1289           $aclList= array($match[2]);
1290         }
1292         // match "users/user[userPassword:rw(,...)*]" style entries
1293         if (preg_match('/^([a-zA-Z0-9]+\/[a-zA-Z0-9]+)\[([a-zA-Z0-9]+:[rwcdm]+(,[a-zA-Z0-9]+:[rwcdm]+)*)\]$/', $acl, $match)){
1294           $module= $match[1];
1295           $aclList= explode(',', $match[2]);
1296         }
1298         // Walk thru prepared ACL by using $module
1299         foreach($aclList as $sAcl) {
1300           $checkAcl= "";
1302           // Category or detailed permission?
1303           if (strpos($module, '/') !== false) {
1304             if (preg_match('/([a-zA-Z0-9]+):([rwcdm]+)/', $sAcl, $m) ) {
1305               $checkAcl= $ui->get_permissions($dn, $module, $m[1]);
1306               $sAcl= $m[2];
1307             } else {
1308               $checkAcl= $ui->get_permissions($dn, $module, '0');
1309             }
1310           } else {
1311             $checkAcl= $ui->get_category_permissions($dn, $module);
1312           }
1314           // Split up remaining part of the acl and check if it we're
1315           // allowed to do something...
1316           $parts= str_split($sAcl);
1317           foreach ($parts as $part) {
1318             if (strpos($checkAcl, $part) === false){
1319               return false;
1320             }
1321           }
1323         }
1324       }
1325     }
1327     return true;
1328   }
1331   function refreshBasesList()
1332   {
1333     global $config;
1334     $ui= get_userinfo();
1336     // Do some array munching to get it user friendly
1337     $ids= $config->idepartments;
1338     $d= $ui->get_module_departments($this->categories);
1339     $k_ids= array_keys($ids);
1340     $deps= array_intersect($d,$k_ids);
1342     // Fill internal bases list
1343     $this->bases= array();
1344     foreach($k_ids as $department){
1345       $this->bases[$department] = $ids[$department];
1346     }
1348     // Populate base selector if already present
1349     if ($this->baseSelector && $this->baseMode) {
1350       $this->baseSelector->setBases($this->bases);
1351       $this->baseSelector->update(TRUE);
1352     }
1353   }
1356   function getDepartments()
1357   {
1358     $departments= array();
1359     $ui= get_userinfo();
1361     // Get list of supported department types
1362     $types = departmentManagement::get_support_departments();
1364     // Load departments allowed by ACL
1365     $validDepartments = $ui->get_module_departments($this->categories);
1367     // Build filter and look in the LDAP for possible sub departments
1368     // of current base
1369     $filter= "(&(objectClass=gosaDepartment)(|";
1370     $attrs= array("description", "objectClass");
1371     foreach($types as $name => $data){
1372       $filter.= "(objectClass=".$data['OC'].")";
1373       $attrs[]= $data['ATTR'];
1374     }
1375     $filter.= "))";
1376     $res= get_list($filter, $this->categories, $this->base, $attrs, GL_NONE);
1378     // Analyze list of departments
1379     foreach ($res as $department) {
1380       if (!in_array($department['dn'], $validDepartments)) {
1381         continue;
1382       }
1384       // Add the attribute where we use for sorting
1385       $oc= null;
1386       foreach(array_keys($types) as $type) {
1387         if (in_array($type, $department['objectClass'])) {
1388           $oc= $type;
1389           break;
1390         }
1391       }
1392       $department['sort-attribute']= $types[$oc]['ATTR'];
1394       // Move to the result list
1395       $departments[]= $department;
1396     }
1398     return $departments;
1399   }
1402   function renderCopyPasteMenu($separator, $copy= true, $cut= true)
1403   {
1404     // We can only provide information if we've got a copypaste handler
1405     // instance
1406     if(!(isset($this->copyPasteHandler) && is_object($this->copyPasteHandler))){
1407       return "";
1408     }
1410     // Presets
1411     $result= "";
1412     $read= $paste= false;
1413     $ui= get_userinfo();
1415     // Switch flags to on if there's at least one category which allows read/paste
1416     foreach($this->categories as $category){
1417       $read= $read || preg_match('/r/', $ui->get_category_permissions($this->base, $category));
1418       $paste= $paste || $ui->is_pasteable($this->base, $category) == 1;
1419     }
1422     // Draw entries that allow copy and cut
1423     if($read){
1425       // Copy entry
1426       if($copy){
1427         $result.= "<li$separator><a href='#' onClick='\$(\"act\").value= \"copy\";\$(\"exec_act\").click();'>".image('images/lists/copy.png')."&nbsp;"._("Copy")."</a></li>";
1428         $separator= "";
1429       }
1431       // Cut entry
1432       if($cut){
1433         $result.= "<li$separator><a href='#' onClick='\$(\"act\").value= \"cut\";\$(\"exec_act\").click();'>".image("images/lists/cut.png")."&nbsp;"._("Cut")."</a></li>";
1434         $separator= "";
1435       }
1436     }
1438     // Draw entries that allow pasting entries
1439     if($paste){
1440       if($this->copyPasteHandler->entries_queued()){
1441         $result.= "<li$separator><a href='#' onClick='\$(\"act\").value= \"paste\";\$(\"exec_act\").click();'>".image("images/lists/paste.png")."&nbsp;"._("Paste")."</a></li>";
1442       }else{
1443         $result.= "<li$separator><a href='#'>".image('images/lists/paste-grey.png')."&nbsp;"._("Paste")."</a></li>";
1444       }
1445     }
1446     
1447     return($result);
1448   }
1451   function renderCopyPasteActions($row, $dn, $category, $class, $copy= true, $cut= true)
1452   {
1453     // We can only provide information if we've got a copypaste handler
1454     // instance
1455     if(!(isset($this->copyPasteHandler) && is_object($this->copyPasteHandler))){
1456       return "";
1457     }
1459     // Presets
1460     $ui = get_userinfo();
1461     $result = "";
1463     // Render cut entries
1464     if($cut){
1465       if($ui->is_cutable($dn, $category, $class)){
1466         $result.= image('images/lists/cut.png', "listing_cut_$row", _("Cut this entry"));
1467       }else{
1468         $result.= image('images/empty.png');
1469       }
1470     }
1472     // Render copy entries
1473     if($copy){
1474       if($ui->is_copyable($dn, $category, $class)){
1475         $result.= image('images/lists/copy.png', "listing_copy_$row", _("Copy this entry"));
1476       }else{
1477         $result.= image('images/empty.png');
1478       }
1479     }
1481     return($result);
1482   }
1485   function renderSnapshotMenu($separator)
1486   {
1487     // We can only provide information if we've got a snapshot handler
1488     // instance
1489     if(!(isset($this->snapshotHandler) && is_object($this->snapshotHandler))){
1490       return "";
1491     }
1493     // Presets
1494     $result = "";
1495     $ui = get_userinfo();
1497     if($this->snapshotHandler->enabled() && $ui->allow_snapshot_restore($this->base, $this->categories)){
1499       // Check if there is something to restore
1500       $restore= false;
1501       foreach($this->snapshotHandler->getSnapshotBases() as $base){
1502         $restore= $restore || count($this->snapshotHandler->getDeletedSnapshots($base)) > 0;
1503       }
1505       // Draw icons according to the restore flag
1506       if($restore){
1507         $result.= "<li$separator><a href='#' onClick='\$(\"act\").value= \"restore\";\$(\"exec_act\").click();'>".image('images/lists/restore.png')."&nbsp;"._("Restore snapshots")."</a></li>";
1508       }else{
1509         $result.= "<li$separator><a href='#'>".image('images/lists/restore-grey.png')."&nbsp;"._("Restore snapshots")."</a></li>";
1510       }
1511     }
1513     return($result);
1514   }
1517   function renderExporterMenu($separator)
1518   {
1519     // Presets
1520     $result = "";
1522     // Draw entries
1523     $result.= "<li$separator><a href='#'>".image('images/lists/export.png')."&nbsp;"._("Export list")."&nbsp;".image("images/forward-arrow.png")."</a><ul class='level3'>";
1525     // Export CVS as build in exporter
1526     foreach ($this->exporter as $action => $exporter) {
1527       $result.= "<li><a href='#' onClick='\$(\"act\").value= \"$action\";\$(\"exec_act\").click();'>".image($exporter['image'])."&nbsp;".$exporter['label']."</a></li>";
1528     }
1530     // Finalize list
1531     $result.= "</ul></li>";
1533     return($result);
1534   }
1537   function renderSnapshotActions($row, $dn, $category, $class, $copy= true, $cut= true)
1538   {
1539     // We can only provide information if we've got a snapshot handler
1540     // instance
1541     if(!(isset($this->snapshotHandler) && is_object($this->snapshotHandler))){
1542       return "";
1543     }
1545     // Presets
1546     $result= "";
1547     $ui = get_userinfo();
1549     // Only act if enabled here
1550     if($this->snapshotHandler->enabled()){
1552       // Draw restore button
1553       if ($ui->allow_snapshot_restore($dn, $category)){
1555         // Do we have snapshots for this dn?
1556         if($this->snapshotHandler->hasSnapshots($dn)){
1557           $result.= image('images/lists/restore.png', "listing_restore_$row", _("Restore snapshot"));
1558         } else {
1559           $result.= image('images/lists/restore-grey.png');
1560         }
1561       }
1563       // Draw snapshot button
1564       if($ui->allow_snapshot_create($dn, $category)){
1565           $result.= image('images/snapshot.png', "listing_snapshot_$row", _("Create new snapshot for this object"));
1566       }else{
1567           $result.= image('images/empty.png');
1568       }
1569     }
1571     return($result);
1572   }
1575   function renderDaemonMenu($separator)
1576   {
1577     $result= "";
1579     // If there is a daemon registered, draw the menu entries
1580     if(class_available("DaemonEvent")){
1581       $events= DaemonEvent::get_event_types_by_category($this->categories);
1582       if(isset($events['BY_CLASS']) && count($events['BY_CLASS'])){
1583         foreach($events['BY_CLASS'] as $name => $event){
1584           $result.= "<li$separator><a href='#' onClick='\$(\"act\").value=\"$name\";\$(\"exec_act\").click();'>".$event['MenuImage']."&nbsp;".$event['s_Menu_Name']."</a></li>";
1585           $separator= "";
1586         }
1587       }
1588     }
1590     return $result;
1591   }
1594   function getEntry($dn)
1595   {
1596     foreach ($this->entries as $entry) {
1597       if (isset($entry['dn']) && strcasecmp($dn, $entry['dn']) == 0){
1598         return $entry;
1599       }
1600     }
1601     return null;
1602   }
1605   function getEntries()
1606   {
1607     return $this->entries;
1608   }
1611   function getType($dn)
1612   {
1613     $dn = LDAP::fix($dn);
1614     if (isset($this->objectDnMapping[$dn])) {
1615       return $this->objectDnMapping[$dn];
1616     }
1617     return null;
1618   }
1622 ?>