Code

63124a1945a9e7c917e23dfd0829f8aabda15e0c
[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;
51   function listing($filename)
52   {
53     global $config;
55     // Initialize pid
56     $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
58     if (!$this->load($filename)) {
59       die("Cannot parse $filename!");
60     }
62     // Set base for filter
63     $this->base= session::global_get("CurrentMainBase");
64     if ($this->base == null) {
65       $this->base= $config->current['BASE'];
66     }
67     $this->refreshBasesList();
69     // Move footer information
70     $this->showFooter= ($config->get_cfg_value("listSummary") == "true");
72     // Register build in filters
73     $this->registerElementFilter("objectType", "listing::filterObjectType");
74     $this->registerElementFilter("departmentLink", "listing::filterDepartmentLink");
75     $this->registerElementFilter("link", "listing::filterLink");
76     $this->registerElementFilter("actions", "listing::filterActions");
77   }
80   function setCopyPasteHandler($handler)
81   {
82     $this->CopyPasteHandler= &$handler;
83   }
86   function registerElementFilter($name, $call)
87   {
88     if (!isset($this->filters[$name])) {
89       $this->filters[$name]= $call;
90       return true;
91     }
93     return false;
94   }
97   function load($filename)
98   {
99     $contents = file_get_contents($filename);
100     $this->xmlData= xml::xml2array($contents, 1);
102     if (!isset($this->xmlData['list'])) {
103       return false;
104     }
106     $this->xmlData= $this->xmlData["list"];
108     // Load some definition values
109     foreach (array("departmentBrowser", "departmentRootVisible", "multiSelect", "baseMode") as $token) {
110       if (isset($this->xmlData['definition'][$token]) &&
111           $this->xmlData['definition'][$token] == "true"){
112         $this->$token= true;
113       }
114     }
116     // Fill objectTypes from departments and xml definition
117     $types = departmentManagement::get_support_departments();
118     foreach ($types as $class => $data) {
119       $this->objectTypes[]= array("label" => $data['TITLE'],
120                                   "objectClass" => $data['OC'],
121                                   "image" => $data['IMG']);
122     }
123     $this->categories= array();
124     if (isset($this->xmlData['definition']['objectType'])) {
125       foreach ($this->xmlData['definition']['objectType'] as $index => $otype) {
126         $this->objectTypes[]= $this->xmlData['definition']['objectType'][$index];
127         if (isset($this->xmlData['definition']['objectType'][$index]['category'])){
128           $this->categories[]= $this->xmlData['definition']['objectType'][$index]['category'];
129         }
130       }
131     }
133     // Parse layout per column
134     $this->colprops= $this->parseLayout($this->xmlData['table']['layout']);
136     // Prepare table headers
137     $this->renderHeader();
139     // Assign headline/module
140     $this->headline= _($this->xmlData['definition']['label']);
141     $this->module= $this->xmlData['definition']['module'];
142     if (!is_array($this->categories)){
143       $this->categories= array($this->categories);
144     }
146     return true;  
147   }
150   function renderHeader()
151   {
152     $this->header= array();
154     // Initialize sort?
155     $sortInit= false;
156     if (!$this->sortDirection) {
157       $this->sortColumn= 0;
158       if (isset($this->xmlData['definition']['defaultSortColumn'])){
159         $this->sortColumn= $this->xmlData['definition']['defaultSortColumn'];
160       } else {
161         $this->sortAttribute= "";
162       }
163       $this->sortDirection= array();
164       $sortInit= true;
165     }
167     if (isset($this->xmlData['table']['column'])){
168       foreach ($this->xmlData['table']['column'] as $index => $config) {
169         // Initialize everything to one direction
170         if ($sortInit) {
171           $this->sortDirection[$index]= false;
172         }
174         $sorter= "";
175         if ($index == $this->sortColumn && isset($config['sortAttribute']) &&
176             isset($config['sortType'])) {
177           $this->sortAttribute= $config['sortAttribute'];
178           $this->sortType= $config['sortType'];
179           $sorter= "&nbsp;<img border='0' title='".($this->sortDirection[$index]?_("Up"):_("Down"))."' src='images/lists/sort-".($this->sortDirection[$index]?"up":"down").".png' align='top'>";
180         }
181         $sortable= (isset($config['sortAttribute']));
183         $link= "href='?plug=".$_GET['plug']."&amp;PID=".$this->pid."&amp;act=SORT_$index'";
184         if (isset($config['label'])) {
185           if ($sortable) {
186             $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."><a $link>"._($config['label'])."$sorter</a></td>";
187           } else {
188             $this->header[$index]= "<td class='listheader' ".$this->colprops[$index].">"._($config['label'])."</td>";
189           }
190         } else {
191           if ($sortable) {
192             $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."><a $link>&nbsp;$sorter</a></td>";
193           } else {
194             $this->header[$index]= "<td class='listheader' ".$this->colprops[$index].">&nbsp;</td>";
195           }
196         }
197       }
198     }
199   }
201   function render()
202   {
203 echo "snapshot handler, daemon handler<br>";
204     // Check for exeeded sizelimit
205     if (($message= check_sizelimit()) != ""){
206       return($message);
207     }
209     // Initialize list
210     $result= "<input type='hidden' value='$this->pid' name='PID'>";
211     $result.= "<div class='contentboxb' id='listing_container' style='border-top:1px solid #B0B0B0;'>";
212     $result.= "<table summary='$this->headline' style='width:600px;height:450px;' cellspacing='0' id='t_scrolltable'>
213 <tr><td class='scrollhead'><table summary='' style='width:100%;' cellspacing='0' id='t_scrollhead'>";
214     $this->numColumns= count($this->colprops) + ($this->multiSelect?1:0);
216     // Build list header
217     $result.= "<tr>";
218     if ($this->multiSelect) {
219       $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>";
220     }
221     foreach ($this->header as $header) {
222       $result.= $header;
223     }
225     // Add 13px for scroller
226     $result.= "<td class='listheader' style='width:13px;border-right:0px;'>&nbsp;</td></table></td></tr>";
228     // New table for the real list contents
229     $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'>";
231     // No results? Just take an empty colspanned row
232     if (count($this->entries) + count($this->departments) == 0) {
233       $result.= "<tr class='rowxp0'><td class='list1nohighlight' colspan='$this->numColumns' style='height:100%;border-right:0px;width:100%;'>&nbsp;</td></tr>";
234     }
236     // Line color alternation
237     $alt= 0;
238     $deps= 0;
240     // Draw department browser if configured and we're not in sub mode
241     if ($this->departmentBrowser && $this->filter->scope != "sub") {
242       // Fill with department browser if configured this way
243       $departmentIterator= new departmentSortIterator($this->departments, $this->sortDirection[$this->sortColumn]);
244       foreach ($departmentIterator as $row => $entry){
245         $result.="<tr class='rowxp".($alt&1)."'>";
247         // Render multi select if needed
248         if ($this->multiSelect) {
249           $result.="<td style='text-align:center;width:20px;' class='list1'>&nbsp;</td>";
250         }
252         // Render defined department columns, fill the rest with some stuff
253         foreach ($this->xmlData['table']['department'] as $index => $config) {
254           $result.="<td ".$this->colprops[$index]." class='list1'>".$this->renderCell($config['value'], $entry, $row)."</td>";
255         }
256         $last= count($this->xmlData['table']['department']) + 1;
257         $rest= $this->numColumns - $last;
258         for ($i= 0; $i<$rest; $i++){
259           $result.= "<td ".$this->colprops[$last+$i-1]." class='list1'>&nbsp;</td>";
260         }
261         $result.="</tr>";
263         $alt++;
264       }
265       $deps= $alt;
266     }
268     // Fill with contents, sort as configured
269     $entryIterator= new listingSortIterator($this->entries, $this->sortDirection[$this->sortColumn], $this->sortAttribute, $this->sortType);
270     foreach ($entryIterator as $row => $entry){
271       $result.="<tr class='rowxp".($alt&1)."'>";
273       // Render multi select if needed
274       if ($this->multiSelect) {
275         $result.="<td style='text-align:center;width:20px;' class='list0'><input type='checkbox' id='listing_selected_$row' name='listing_selected_$row'></td>";
276       }
278       foreach ($this->xmlData['table']['column'] as $index => $config) {
279         $result.="<td ".$this->colprops[$index]." class='list0'>".$this->renderCell($config['value'], $entry, $row)."</td>";
280       }
281       $result.="</tr>";
283       $alt++;
284     }
286     // Need to fill the list if it's not full (nobody knows why this is 22 ;-))
287     $emptyListStyle= (count($this->entries) + count($deps) == 0)?"border:0;":"";
288     if (count($this->entries) + count($deps) < 22) {
289       $result.= "<tr>";
290       for ($i= 0; $i<$this->numColumns; $i++) {
291         if ($i == 0) {
292           $result.= "<td class='list1nohighlight' style='$emptyListStyle height:100%;'>&nbsp;</td>";
293           continue;
294         }
295         if ($i != $this->numColumns-1) {
296           $result.= "<td class='list1nohighlight' style='$emptyListStyle'>&nbsp;</td>";
297         } else {
298           $result.= "<td class='list1nohighlight' style='border-right:1px solid #AAA;$emptyListStyle'>&nbsp;</td>";
299         }
300       }
301       $result.= "</tr>";
302     }
304     $result.= "</table></div></td></tr>";
306     // Add the footer if requested
307     if ($this->showFooter) {
308       $result.= "<tr><td class='scrollhead'><table summary='' style='width:100%' cellspacing='0' id='t_scrollfoot'><tr><td class='listfooter' style='border-bottom:0px;'>";
310       foreach ($this->objectTypes as $objectType) {
311         if (isset($this->objectTypeCount[$objectType['label']])) {
312           $label= _($objectType['label']);
313           $result.= "<img class='center' src='".$objectType['image']."' title='$label' alt='$label'>&nbsp;".$this->objectTypeCount[$objectType['label']]."&nbsp;&nbsp;&nbsp;&nbsp;";
314         }
315       }
317       $result.= "<td class='listfooter' style='width:13px;border-right:0px;'>&nbsp;</td></table></td></tr>";
318     }
320     $result.= "</table></div>";
322     $smarty= get_smarty();
323     $smarty->assign("FILTER", $this->filter->render());
324     $smarty->assign("SIZELIMIT", print_sizelimit_warning());
325     $smarty->assign("LIST", $result);
327     // Assign navigation elements
328     $nav= $this->renderNavigation();
329     foreach ($nav as $key => $html) {
330       $smarty->assign($key, $html);
331     }
333     // Assign action menu / base
334     $smarty->assign("ACTIONS", $this->renderActionMenu());
335     $smarty->assign("BASE", $this->renderBase());
337     // Assign separator
338     $smarty->assign("SEPARATOR", "<img src='images/lists/seperator.png' alt='-' align='middle' height='16' width='1' class='center'>");
340     // Assign summary
341     $smarty->assign("HEADLINE", $this->headline);
343     return ($smarty->fetch(get_template_path($this->xmlData['definition']['template'], true)));
344   }
347   function setFilter($filter)
348   {
349     $this->filter= &$filter;
350     if ($this->departmentBrowser){
351       $this->departments= $this->getDepartments();
352     }
353     $this->filter->setBase($this->base);
354     $this->entries= $this->filter->query();
355   }
358   function update()
359   {
360     global $config;
361     $ui= get_userinfo();
363     // Do not do anything if this is not our PID
364     if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->pid) {
365       return;
366     }
368     // Save base
369     if (isset($_POST['BASE']) && $this->baseMode == true) {
370       $base= validate($_POST['BASE']);
371       if (isset($this->bases[$base])) {
372         $this->base= $base;
373       }
374     }
376     // Override the base if we got a message from the browser navigation
377     if ($this->departmentBrowser && isset($_GET['act'])) {
378       if (preg_match('/^department_([0-9]+)$/', validate($_GET['act']), $match)){
379         if (isset($this->departments[$match[1]])){
380           $this->base= $this->departments[$match[1]]['dn'];
381         }
382       }
383     }
385     // Filter GET with "act" attributes
386     if (isset($_GET['act'])) {
387       $key= validate($_GET['act']);
388       if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
389         // Switch to new column or invert search order?
390         $column= $match[1];
391         if ($this->sortColumn != $column) {
392           $this->sortColumn= $column;
393         } else {
394           $this->sortDirection[$column]= !$this->sortDirection[$column];
395         }
397         // Allow header to update itself according to the new sort settings
398         $this->renderHeader();
399       }
400     }
402     // Override base if we got signals from the navigation elements
403     $action= "";
404     foreach ($_POST as $key => $value) {
405       if (preg_match('/^(ROOT|BACK|HOME)_x$/', $key, $match)) {
406         $action= $match[1];
407         break;
408       }
409     }
411     // Navigation handling
412     if ($action == 'ROOT') {
413       $deps= $ui->get_module_departments($this->module);
414       $this->base= $deps[0];
415     }
416     if ($action == 'BACK') {
417       $deps= $ui->get_module_departments($this->module);
418       $base= preg_replace("/^[^,]+,/", "", $this->base);
419       if(in_array_ics($base, $deps)){
420         $this->base= $base;
421       }
422     }
423     if ($action == 'HOME') {
424       $ui= get_userinfo();
425       $this->base= get_base_from_people($ui->dn);
426     }
428     // Reload departments
429     if ($this->departmentBrowser){
430       $this->departments= $this->getDepartments();
431     }
433     // Update filter and refresh entries
434     $this->filter->setBase($this->base);
435     $this->entries= $this->filter->query();
436   }
439   function parseLayout($layout)
440   {
441     $result= array();
442     $layout= preg_replace("/^\|/", "", $layout);
443     $layout= preg_replace("/\|$/", "", $layout);
444     $cols= split("\|", $layout);
445     foreach ($cols as $index => $config) {
446       if ($config != "") {
447         $components= split(';', $config);
448         $config= "";
449         foreach ($components as $part) {
450           if (preg_match("/^r$/", $part)) {
451             $config.= "text-align:right;";
452             continue;
453           }
454           if (preg_match("/^l$/", $part)) {
455             $config.= "text-align:left;";
456             continue;
457           }
458           if (preg_match("/^c$/", $part)) {
459             $config.= "text-align:center;";
460             continue;
461           }
462           if (preg_match("/^[0-9]+(|px|%)$/", $part)) {
463             $config.= "width:$part;";
464             continue;
465           }
466         }
468         $result[$index]= " style='$config' ";
469       } else {
470         $result[$index]= null;
471       }
472     }
474     // Save number of columns for later use
475     $this->numColumns= count($cols);
477     return $result;
478   }
481   function renderCell($data, $config, $row)
482   {
483     // Replace flat attributes in data string
484     for ($i= 0; $i<$config['count']; $i++) {
485       $attr= $config[$i];
486       $value= "";
487       if (is_array($config[$attr])) {
488         $value= $config[$attr][0];
489       } else {
490         $value= $config[$attr];
491       }
492       $data= preg_replace("/%\{$attr\}/", $value, $data);
493     }
495     // Watch out for filters and prepare to execute them
496     $data= $this->processElementFilter($data, $config, $row);
498     return $data;
499   }
502   function renderBase()
503   {
504     $result= "<select name='BASE' onChange='mainform.submit()' size='1'>";
505     $firstDN= null;
506     $found= false;
508     foreach ($this->bases as $key=>$value) {
509       // Keep first entry to fall back eventually
510       if(!$firstDN) {
511         $firstDN= $key;
512       }
514       // Prepare to render entry
515       $selected= "";
516       if ($key == $this->base) {
517         $selected= " selected";
518         $found= true;
519       }
520       $result.= "<option value='".$key."'$selected>".$value."</option>";
521     }
522     $result.= "</select>";
524     // Reset the currently used base to the first DN we found if there
525     // was no match.
526     if(!$found){
527       $this->base = $firstDN;
528     }
530     return $result;
531   }
534   function processElementFilter($data, $config, $row)
535   {
536     preg_match_all("/%\{filter:([^(]+)\((.*)\)\}/", $data, $matches, PREG_SET_ORDER);
538     foreach ($matches as $match) {
539       if (!isset($this->filters[$match[1]])) {
540         continue;
541       }
542       $cl= preg_replace('/::.*$/', '', $this->filters[$match[1]]);
543       $method= preg_replace('/^.*::/', '', $this->filters[$match[1]]);
545       // Prepare params for function call
546       $params= array();
547       preg_match_all('/"[^"]+"|[^,]+/', $match[2], $parts);
548       foreach ($parts[0] as $param) {
550         // Row is replaced by the row number
551         if ($param == "row") {
552           $params[]= $row;
553         }
555         // pid is replaced by the current PID
556         if ($param == "pid") {
557           $params[]= $this->pid;
558         }
560         // Fixie with "" is passed directly
561         if (preg_match('/^".*"$/', $param)){
562           $params[]= preg_replace('/"/', '', $param);
563         }
565         // LDAP variables get replaced by their objects
566         for ($i= 0; $i<$config['count']; $i++) {
567           if ($param == $config[$i]) {
568             $values= $config[$config[$i]];
569             if (is_array($values)){
570               unset($values['count']);
571             }
572             $params[]= $values;
573           }
574         }
576         // Move dn if needed
577         if ($param == "dn") {
578           $params[]= LDAP::fix($config["dn"]);
579         }
580       }
582       // Replace information
583       if ($cl == "listing") {
584         // Non static call - seems to result in errors
585         $data= @preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($this, "$method"), $params), $data);
586       } else {
587         // Static call
588         $data= preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($cl, $method), $params), $data);
589       }
590     }
592     return $data;
593   }
596   function getObjectType($types, $classes)
597   {
598     // Walk thru types and see if there's something matching
599     foreach ($types as $objectType) {
600       $ocs= $objectType['objectClass'];
601       if (!is_array($ocs)){
602         $ocs= array($ocs);
603       }
605       $found= true;
606       foreach ($ocs as $oc){
607         if (preg_match('/^!(.*)$/', $oc, $match)) {
608           $oc= $match[1];
609           if (in_array($oc, $classes)) {
610             $found= false;
611           }
612         } else {
613           if (!in_array($oc, $classes)) {
614             $found= false;
615           }
616         }
617       }
619       if ($found) {
620         return $objectType;
621       }
622     }
624     return null;
625   }
628   function filterObjectType($dn, $classes)
629   {
630     // Walk thru classes and return on first match
631     $result= "&nbsp;";
633     $objectType= $this->getObjectType($this->objectTypes, $classes);
634     if ($objectType) {
635       $result= "<img class='center' title='".LDAP::fix($dn)."' src='".$objectType["image"]."'>";
636       if (!isset($this->objectTypeCount[$objectType['label']])) {
637         $this->objectTypeCount[$objectType['label']]= 0;
638       }
639       $this->objectTypeCount[$objectType['label']]++;
640     }
641     return $result;
642   }
645   function filterActions($dn, $row, $classes)
646   {
647     // Do nothing if there's no menu defined
648     if (!isset($this->xmlData['actiontriggers']['action'])) {
649       return "&nbsp;";
650     }
652     // Go thru all actions
653     $result= "";
654     $actions= $this->xmlData['actiontriggers']['action'];
655     foreach($actions as $action) {
656       // Skip the entry completely if there's no permission to execute it
657       if (!$this->hasActionPermission($action, $dn)) {
658         continue;
659       }
661       // If there's an objectclass definition and we don't have it
662       // add an empty picture here.
663       if (isset($action['objectclass'])){
664         $objectclass= $action['objectclass'];
665         if (preg_match('/^!(.*)$/', $objectclass, $m)){
666           $objectclass= $m[1];
667           if(in_array($objectclass, $classes)) {
668             $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
669             continue;
670           }
671         } else {
672           if(!in_array($objectclass, $classes)) {
673             $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
674             continue;
675           }
676         }
677       }
679       // Render normal entries as usual
680       if ($action['type'] == "entry") {
681         $label= $this->processElementFilter($action['label'], $this->entries[$row], $row);
682         $image= $this->processElementFilter($action['image'], $this->entries[$row], $row);
683         $result.="<input class='center' type='image' src='$image' title='$label' ".
684                  "name='listing_".$action['name']."_$row' style='padding:1px'>";
685       }
687       // Handle special types
688       if ($action['type'] == "snapshot") {
689         #TODO
690         #echo "actiontriggers: snapshot missing<br>";
691       }
692       if ($action['type'] == "copypaste") {
694         $objectType= $this->getObjectType($this->objectTypes, $this->entries[$row]['objectClass']);
695         $category= $class= null;
696         if ($objectType) {
697           $category= $objectType['category'];
698           $class= $objectType['class'];
699         }
701         $result.= $this->renderCopyPasteActions($row, $this->entries[$row]['dn'], $category, $class);
702       }
703       if ($action['type'] == "daemon") {
704         #TODO
705         #echo "actiontriggers: daemon missing<br>";
706       }
708     }
710     return $result;
711   }
714   function filterDepartmentLink($row, $dn, $description)
715   {
716     $attr= $this->departments[$row]['sort-attribute'];
717     $name= $this->departments[$row][$attr];
718     if (is_array($name)){
719       $name= $name[0];
720     }
721     $result= sprintf("%s [%s]", $name, $description[0]);
722     return("<a href='?plug=".$_GET['plug']."&amp;PID=$this->pid&amp;act=department_$row' title='$dn'>$result</a>");
723   }
726   function filterLink()
727   {
728     $result= "&nbsp;";
730     $row= func_get_arg(0);
731     $pid= $this->pid;
732     $dn= LDAP::fix(func_get_arg(1));
733     $params= array(func_get_arg(2));
735     // Collect sprintf params
736     for ($i = 3;$i < func_num_args();$i++) {
737       $val= func_get_arg($i);
738       if (is_array($val)){
739         $params[]= $val[0];
740         continue;
741       }
742       $params[]= $val;
743     }
745     $result= "&nbsp;";
746     $trans= call_user_func_array("sprintf", $params);
747     if ($trans != "") {
748       return("<a href='?plug=".$_GET['plug']."&amp;PID=$pid&amp;act=listing_edit_$row' title='$dn'>$trans</a>");
749     }
751     return $result;
752   }
755   function renderNavigation()
756   {
757     $result= array();
758     $enableBack = true;
759     $enableRoot = true;
760     $enableHome = true;
762     $ui = get_userinfo();
764     /* Check if base = first available base */
765     $deps = $ui->get_module_departments($this->module);
767     if(!count($deps) || $deps[0] == $this->filter->base){
768       $enableBack = false;
769       $enableRoot = false;
770     }
772     $listhead ="";
774     /* Check if we are in users home  department */
775     if(!count($deps) ||$this->filter->base == get_base_from_people($ui->dn)){
776       $enableHome = false;
777     }
779     /* Draw root button */
780     if($enableRoot){
781       $result["ROOT"]= "<input class='center' type='image' src='images/lists/root.png' align='middle' ".
782                        "title='"._("Go to root department")."' name='ROOT' alt='"._("Root")."'>";
783     }else{
784       $result["ROOT"]= "<img src='images/lists/root_grey.png' class='center' alt='"._("Root")."'>";
785     }
787     /* Draw back button */
788     if($enableBack){
789       $result["BACK"]= "<input class='center' type='image' align='middle' src='images/lists/back.png' ".
790                        "title='"._("Go up one department")."' alt='"._("Up")."' name='BACK'>";
791     }else{
792       $result["BACK"]= "<img src='images/lists/back_grey.png' class='center' alt='"._("Up")."'>";
793     }
795     /* Draw home button */
796     if($enableHome){
797       $result["HOME"]= "<input class='center' type='image' align='middle' src='images/lists/home.png' ".
798                        "title='"._("Go to users department")."' alt='"._("Home")."' name='HOME'>";
799     }else{
800       $result["HOME"]= "<img src='images/lists/home_grey.png' class='center' alt='"._("Home")."'>";
801     }
803     /* Draw reload button, this button is enabled everytime */
804     $result["RELOAD"]= "<input class='center' type='image' src='images/lists/reload.png' align='middle' ".
805                        "title='"._("Reload list")."' name='REFRESH' alt='"._("Submit")."'>";
807     return ($result);
808   }
811   function getAction()
812   {
813     // Do not do anything if this is not our PID
814     if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->pid) {
815       return;
816     }
818     $result= array("targets" => array(), "action" => "");
820     // Filter GET with "act" attributes
821     if (isset($_GET['act'])) {
822       $key= validate($_GET['act']);
823       $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)$/', '$1', $key);
824       if (isset($this->entries[$target]['dn'])) {
825         $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+$/', '$1', $key);
826         $result['targets'][]= $this->entries[$target]['dn'];
827       }
829       // Drop targets if empty
830       if (count($result['targets']) == 0) {
831         unset($result['targets']);
832       }
833       return $result;
834     }
836     // Filter POST with "listing_" attributes
837     foreach ($_POST as $key => $prop) {
839       // Capture selections
840       if (preg_match('/^listing_selected_[0-9]+$/', $key)) {
841         $target= preg_replace('/^listing_selected_([0-9]+)$/', '$1', $key);
842         if (isset($this->entries[$target]['dn'])) {
843           $result['targets'][]= $this->entries[$target]['dn'];
844         }
845         continue;
846       }
848       // Capture action with target - this is a one shot
849       if (preg_match('/^listing_[a-zA-Z_]+_[0-9]+(|_x)$/', $key)) {
850         $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)(|_x)$/', '$1', $key);
851         if (isset($this->entries[$target]['dn'])) {
852           $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+(|_x)$/', '$1', $key);
853           $result['targets']= array($this->entries[$target]['dn']);
854         }
855         break;
856       }
858       // Capture action without target
859       if (preg_match('/^listing_[a-zA-Z_]+(|_x)$/', $key)) {
860         $result['action']= preg_replace('/^listing_([a-zA-Z_]+)(|_x)$/', '$1', $key);
861         continue;
862       }
863     }
865     // Filter POST with "act" attributes -> posted from action menu
866     if (isset($_POST['act']) && $_POST['act'] != '') {
867       $result['action']= validate($_POST['act']);
868     }
870     // Drop targets if empty
871     if (count($result['targets']) == 0) {
872       unset($result['targets']);
873     }
874     return $result;
875   }
878   function renderActionMenu()
879   {
880     // Don't send anything if the menu is not defined
881     if (!isset($this->xmlData['actionmenu']['action'])){
882       return "";
883     }
885     // Load shortcut
886     $actions= &$this->xmlData['actionmenu']['action'];
887     $result= "<input type='hidden' name='act' id='actionmenu' value=''>".
888              "<ul class='level1' id='root'><li><a href='#'>Aktionen&nbsp;<img ".
889              "border=0 src='images/lists/sort-down.png'></a>";
891     // Build ul/li list
892     $result.= $this->recurseActions($actions);
894     return "<div id='pulldown'>".$result."</li></ul><div>";
895   }
898   function recurseActions($actions)
899   {
900     static $level= 2;
901     $result= "<ul class='level$level'>";
902     $separator= "";
904     foreach ($actions as $action) {
906       // Skip the entry completely if there's no permission to execute it
907       if (!$this->hasActionPermission($action, $this->filter->base)) {
908         continue;
909       }
911       // Fill image if set
912       $img= "";
913       if (isset($action['image'])){
914         $img= "<img border=0 src='".$action['image']."'>&nbsp;";
915       }
917       if ($action['type'] == "separator"){
918         $separator= " style='border-top:1px solid #AAA' ";
919         continue;
920       }
922       // Dive into subs
923       if ($action['type'] == "sub" && isset($action['action'])) {
924         $level++;
925         if (isset($action['label'])){
926           $result.= "<li$separator><a href='#'>$img"._($action['label'])."&nbsp;<img border='0' src='images/forward-arrow.png'></a>";
927         }
928         $result.= $this->recurseActions($action['action'])."</li>";
929         $level--;
930         $separator= "";
931         continue;
932       }
934       // Render entry elseways
935       if (isset($action['label'])){
936         $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"".$action['name']."\";mainform.submit();'>$img"._($action['label'])."</a></li>";
937       }
939       // Check for special types
940       switch ($action['type']) {
941         case 'copypaste':
942           $result.= $this->renderCopyPasteMenu($separator);
943           break;
945         case 'snapshot':
946           #TODO
947           #echo "actionmenu: snapshot missing<br>";
948           break;
950         case 'daemon':
951           #TODO
952           #echo "actionmenu: daemon missing<br>";
953           break;
954       }
956       $separator= "";
957     }
959     $result.= "</ul>";
960     return $result;
961   }
964   function hasActionPermission($action, $dn)
965   {
966     $ui= get_userinfo();
968     if (isset($action['acl'])) {
969       $acls= $action['acl'];
970       if (!is_array($acls)) {
971         $acls= array($acls);
972       }
974       // Every ACL has to pass
975       foreach ($acls as $acl) {
976         $module= $this->module;
977         $acllist= array();
979         // Split for category and plugins if needed
980         // match for "[rw]" style entries
981         if (preg_match('/^\[([rwcdm]+)\]$/', $acl, $match)){
982           $aclList= array($match[1]);
983         }
985         // match for "users[rw]" style entries
986         if (preg_match('/^([a-zA-Z0-9]+)\[([rwcdm]+)\]$/', $acl, $match)){
987           $module= $match[1];
988           $aclList= array($match[2]);
989         }
991         // match for "users/user[rw]" style entries
992         if (preg_match('/^([a-zA-Z0-9]+\/[a-zA-Z0-9]+)\[([rwcdm]+)\]$/', $acl, $match)){
993           $module= $match[1];
994           $aclList= array($match[2]);
995         }
997         // match "users/user[userPassword:rw(,...)*]" style entries
998         if (preg_match('/^([a-zA-Z0-9]+\/[a-zA-Z0-9]+)\[([a-zA-Z0-9]+:[rwcdm]+(,[a-zA-Z0-9]+:[rwcdm]+)*)\]$/', $acl, $match)){
999           $module= $match[1];
1000           $aclList= split(',', $match[2]);
1001         }
1003         // Walk thru prepared ACL by using $module
1004         foreach($aclList as $sAcl) {
1005           $checkAcl= "";
1007           // Category or detailed permission?
1008           if (strpos('/', $module) === false) {
1009             if (preg_match('/([a-zA-Z0-9]+):([rwcdm]+)/', $sAcl, $m) ) {
1010               $checkAcl= $ui->get_permissions($dn, $module, $m[1]);
1011               $sAcl= $m[2];
1012             } else {
1013               $checkAcl= $ui->get_permissions($dn, $module, '0');
1014             }
1015           } else {
1016             $checkAcl= $ui->get_category_permissions($dn, $module);
1017           }
1019           // Split up remaining part of the acl and check if it we're
1020           // allowed to do something...
1021           $parts= str_split($sAcl);
1022           foreach ($parts as $part) {
1023             if (strpos($checkAcl, $part) === false){
1024               return false;
1025             }
1026           }
1028         }
1029       }
1030     }
1032     return true;
1033   }
1036   function refreshBasesList()
1037   {
1038     global $config;
1039     $ui= get_userinfo();
1041     // Do some array munching to get it user friendly
1042     $ids= $config->idepartments;
1043     $d= $ui->get_module_departments($this->module);
1044     $k_ids= array_keys($ids);
1045     $deps= array_intersect($d,$k_ids);
1047     // Fill internal bases list
1048     $this->bases= array();
1049     foreach($k_ids as $department){
1050       $this->bases[$department] = $ids[$department];
1051     }
1052   }
1055   function getDepartments()
1056   {
1057     $departments= array();
1058     $ui= get_userinfo();
1060     // Get list of supported department types
1061     $types = departmentManagement::get_support_departments();
1063     // Load departments allowed by ACL
1064     $validDepartments = $ui->get_module_departments($this->module);
1066     // Build filter and look in the LDAP for possible sub departments
1067     // of current base
1068     $filter= "(&(objectClass=gosaDepartment)(|";
1069     $attrs= array("description", "objectClass");
1070     foreach($types as $name => $data){
1071       $filter.= "(objectClass=".$data['OC'].")";
1072       $attrs[]= $data['ATTR'];
1073     }
1074     $filter.= "))";
1075     $res= get_list($filter, $this->module, $this->base, $attrs, GL_NONE | GL_SIZELIMIT);
1077     // Analyze list of departments
1078     foreach ($res as $department) {
1079       if (!in_array($department['dn'], $validDepartments)) {
1080         continue;
1081       }
1083       // Add the attribute where we use for sorting
1084       $oc= null;
1085       foreach(array_keys($types) as $type) {
1086         if (in_array($type, $department['objectClass'])) {
1087           $oc= $type;
1088           break;
1089         }
1090       }
1091       $department['sort-attribute']= $types[$oc]['ATTR'];
1093       // Move to the result list
1094       $departments[]= $department;
1095     }
1097     return $departments;
1098   }
1101   function renderCopyPasteMenu($separator, $copy= true, $cut= true)
1102   {
1103     // We can only provide information if we've got a copypaste handler
1104     // instance
1105     if(!(isset($this->CopyPasteHandler) && is_object($this->CopyPasteHandler))){
1106       return "";
1107     }
1109     // Presets
1110     $result= "";
1111     $read= $paste= false;
1112     $ui= get_userinfo();
1114     // Switch flags to on if there's at least one category which allows read/paste
1115     foreach($this->categories as $category){
1116       $read= $read || preg_match('/r/', $ui->get_category_permissions($this->base, $category));
1117       $paste= $paste || $ui->is_pasteable($this->base, $category) == 1;
1118     }
1121     // Draw entries that allow copy and cut
1122     if($read){
1124       // Copy entry
1125       if($copy){
1126         $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>";
1127         $separator= "";
1128       }
1130       // Cut entry
1131       if($cut){
1132         $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>";
1133         $separator= "";
1134       }
1135     }
1137     // Draw entries that allow pasting entries
1138     if($paste){
1139       if($this->CopyPasteHandler->entries_queued()){
1140         $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>";
1141       }else{
1142         $result.= "<li$separator><a href='#'><img src='images/lists/paste-grey.png' alt='' border='0' class='center'>&nbsp;"._("Paste")."</a></li>";
1143       }
1144     }
1145     
1146     return($result);
1147   }
1150   function renderCopyPasteActions($row, $dn, $category, $class, $copy= true, $cut= true)
1151   {
1152     // We can only provide information if we've got a copypaste handler
1153     // instance
1154     if(!(isset($this->CopyPasteHandler) && is_object($this->CopyPasteHandler))){
1155       return "";
1156     }
1158     // Presets
1159     $ui = get_userinfo();
1160     $result = "";
1162     // Render cut entries
1163     if($cut){
1164       if($ui->is_cutable($dn, $category, $class)){
1165         $result .= "<input class='center' type='image'
1166           src='images/lists/cut.png' alt='"._("Cut")."' name='listing_cut_$row' title='"._("Cut this entry")."' style='padding:1px'>";
1167       }else{
1168         $result.="<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1169       }
1170     }
1172     // Render copy entries
1173     if($copy){
1174       if($ui->is_copyable($dn, $category, $class)){
1175         $result.= "<input class='center' type='image'
1176           src='images/lists/copy.png' alt='"._("Copy")."' name='listing_copy_$row' title='"._("Copy this entry")."' style='padding:1px'>";
1177       }else{
1178         $result.="<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1179       }
1180     }
1182     return($result);
1183   }
1187 ?>