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 $objectDnMapping= array();
49 var $copyPasteHandler= null;
50 var $snapshotHandler= null;
51 var $exporter= array();
52 var $exportColumns= array();
53 var $useSpan= false;
54 var $height= 0;
57 function listing($filename)
58 {
59 global $config;
60 global $class_mapping;
62 // Initialize pid
63 $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
65 if (!$this->load($filename)) {
66 die("Cannot parse $filename!");
67 }
69 // Set base for filter
70 if ($this->baseMode) {
71 $this->base= session::global_get("CurrentMainBase");
72 if ($this->base == null) {
73 $this->base= $config->current['BASE'];
74 }
75 $this->refreshBasesList();
76 } else {
77 $this->base= $config->current['BASE'];
78 }
80 // Move footer information
81 $this->showFooter= ($config->get_cfg_value("listSummary") == "true");
83 // Register build in filters
84 $this->registerElementFilter("objectType", "listing::filterObjectType");
85 $this->registerElementFilter("departmentLink", "listing::filterDepartmentLink");
86 $this->registerElementFilter("link", "listing::filterLink");
87 $this->registerElementFilter("actions", "listing::filterActions");
89 // Load exporters
90 foreach($class_mapping as $class => $dummy) {
91 if (preg_match('/Exporter$/', $class)) {
92 $info= call_user_func(array($class, "getInfo"));
93 if ($info != null) {
94 $this->exporter= array_merge($this->exporter, $info);
95 }
96 }
97 }
98 }
101 function setCopyPasteHandler($handler)
102 {
103 $this->copyPasteHandler= &$handler;
104 }
107 function setHeight($height)
108 {
109 $this->height= $height;
110 }
113 function setSnapshotHandler($handler)
114 {
115 $this->snapshotHandler= &$handler;
116 }
119 function setFilter($filter)
120 {
121 $this->filter= &$filter;
122 if ($this->departmentBrowser){
123 $this->departments= $this->getDepartments();
124 }
125 $this->filter->setBase($this->base);
126 }
129 function registerElementFilter($name, $call)
130 {
131 if (!isset($this->filters[$name])) {
132 $this->filters[$name]= $call;
133 return true;
134 }
136 return false;
137 }
140 function load($filename)
141 {
142 $contents = file_get_contents($filename);
143 $this->xmlData= xml::xml2array($contents, 1);
145 if (!isset($this->xmlData['list'])) {
146 return false;
147 }
149 $this->xmlData= $this->xmlData["list"];
151 // Load some definition values
152 foreach (array("departmentBrowser", "departmentRootVisible", "multiSelect", "baseMode") as $token) {
153 if (isset($this->xmlData['definition'][$token]) &&
154 $this->xmlData['definition'][$token] == "true"){
155 $this->$token= true;
156 }
157 }
159 // Fill objectTypes from departments and xml definition
160 $types = departmentManagement::get_support_departments();
161 foreach ($types as $class => $data) {
162 $this->objectTypes[]= array("label" => $data['TITLE'],
163 "objectClass" => $data['OC'],
164 "image" => $data['IMG']);
165 }
166 $this->categories= array();
167 if (isset($this->xmlData['definition']['objectType'])) {
168 if(isset($this->xmlData['definition']['objectType']['label'])) {
169 $this->xmlData['definition']['objectType']= array($this->xmlData['definition']['objectType']);
170 }
171 foreach ($this->xmlData['definition']['objectType'] as $index => $otype) {
172 $this->objectTypes[]= $this->xmlData['definition']['objectType'][$index];
173 if (isset($this->xmlData['definition']['objectType'][$index]['category'])){
174 $this->categories[]= $this->xmlData['definition']['objectType'][$index]['category'];
175 }
176 }
177 }
179 // Parse layout per column
180 $this->colprops= $this->parseLayout($this->xmlData['table']['layout']);
182 // Prepare table headers
183 $this->renderHeader();
185 // Assign headline/module
186 $this->headline= _($this->xmlData['definition']['label']);
187 $this->module= $this->xmlData['definition']['module'];
188 if (!is_array($this->categories)){
189 $this->categories= array($this->categories);
190 }
192 // Evaluate columns to be exported
193 if (isset($this->xmlData['table']['column'])){
194 foreach ($this->xmlData['table']['column'] as $index => $config) {
195 if (isset($config['export']) && $config['export'] == "true"){
196 $this->exportColumns[]= $index;
197 }
198 }
199 }
201 return true;
202 }
205 function renderHeader()
206 {
207 $this->header= array();
208 $this->plainHeader= array();
210 // Initialize sort?
211 $sortInit= false;
212 if (!$this->sortDirection) {
213 $this->sortColumn= 0;
214 if (isset($this->xmlData['definition']['defaultSortColumn'])){
215 $this->sortColumn= $this->xmlData['definition']['defaultSortColumn'];
216 } else {
217 $this->sortAttribute= "";
218 }
219 $this->sortDirection= array();
220 $sortInit= true;
221 }
223 if (isset($this->xmlData['table']['column'])){
224 foreach ($this->xmlData['table']['column'] as $index => $config) {
225 // Initialize everything to one direction
226 if ($sortInit) {
227 $this->sortDirection[$index]= false;
228 }
230 $sorter= "";
231 if ($index == $this->sortColumn && isset($config['sortAttribute']) &&
232 isset($config['sortType'])) {
233 $this->sortAttribute= $config['sortAttribute'];
234 $this->sortType= $config['sortType'];
235 $sorter= " <img border='0' title='".($this->sortDirection[$index]?_("Up"):_("Down"))."' src='images/lists/sort-".($this->sortDirection[$index]?"up":"down").".png' align='top'>";
236 }
237 $sortable= (isset($config['sortAttribute']));
239 $link= "href='?plug=".$_GET['plug']."&PID=".$this->pid."&act=SORT_$index'";
240 if (isset($config['label'])) {
241 if ($sortable) {
242 $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."><a $link>"._($config['label'])."$sorter</a></td>";
243 } else {
244 $this->header[$index]= "<td class='listheader' ".$this->colprops[$index].">"._($config['label'])."</td>";
245 }
246 $this->plainHeader[]= _($config['label']);
247 } else {
248 if ($sortable) {
249 $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."><a $link> $sorter</a></td>";
250 } else {
251 $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."> </td>";
252 }
253 $this->plainHeader[]= "";
254 }
255 }
256 }
257 }
259 function render()
260 {
261 // Check for exeeded sizelimit
262 if (($message= check_sizelimit()) != ""){
263 return($message);
264 }
266 // Initialize list
267 $result= "<input type='hidden' value='$this->pid' name='PID'>\n";
268 $height= 450;
269 if ($this->height != 0) {
270 $result.= "<input type='hidden' value='$this->height' id='d_height'>\n";
271 $height= $this->height;
272 }
274 $result.= "<div class='contentboxb' id='listing_container' style='border-top:1px solid #B0B0B0;'>\n";
275 $result.= "<table summary='$this->headline' style='width:600px;height:".$height."px;' cellspacing='0' id='t_scrolltable'>
276 <tr><td class='scrollhead'><table summary='' style='width:100%;' cellspacing='0' id='t_scrollhead'>\n";
277 $this->numColumns= count($this->colprops) + ($this->multiSelect?1:0);
279 // Build list header
280 $result.= "<tr>\n";
281 if ($this->multiSelect) {
282 $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>\n";
283 }
284 foreach ($this->header as $header) {
285 $result.= $header;
286 }
288 // Add 13px for scroller
289 $result.= "<td class='listheader' style='width:13px;border-right:0px;'> </td></table></td></tr>\n";
291 // New table for the real list contents
292 $result.= "<tr><td colspan='$this->numColumns' class='scrollbody'><div style='width:600px;height:".($height-20)."px;' id='d_scrollbody' class='scrollbody'><table summary='' style='height:100%;width:581px;' cellspacing='0' id='t_scrollbody'>\n";
294 // No results? Just take an empty colspanned row
295 if (count($this->entries) + count($this->departments) == 0) {
296 $result.= "<tr class='rowxp0'><td class='list1nohighlight' colspan='$this->numColumns' style='height:100%;border-right:0px;width:100%;'> </td></tr>";
297 }
299 // Line color alternation
300 $alt= 0;
301 $deps= 0;
303 // Draw department browser if configured and we're not in sub mode
304 $this->useSpan= false;
305 if ($this->departmentBrowser && $this->filter->scope != "sub") {
306 // Fill with department browser if configured this way
307 $departmentIterator= new departmentSortIterator($this->departments, $this->sortDirection[$this->sortColumn]);
308 foreach ($departmentIterator as $row => $entry){
309 $result.="<tr class='rowxp".($alt&1)."'>";
311 // Render multi select if needed
312 if ($this->multiSelect) {
313 $result.="<td style='text-align:center;width:20px;' class='list1'> </td>";
314 }
316 // Render defined department columns, fill the rest with some stuff
317 $rest= $this->numColumns - 1;
318 foreach ($this->xmlData['table']['department'] as $index => $config) {
319 $colspan= 1;
320 if (isset($config['span'])){
321 $colspan= $config['span'];
322 $this->useSpan= true;
323 }
324 $result.="<td colspan='$colspan' ".$this->colprops[$index]." class='list1'>".$this->renderCell($config['value'], $entry, $row)."</td>";
325 $rest-= $colspan;
326 }
328 // Fill remaining cols with nothing
329 $last= $this->numColumns - $rest;
330 for ($i= 0; $i<$rest; $i++){
331 $result.= "<td ".$this->colprops[$last+$i-1]." class='list1'> </td>";
332 }
333 $result.="</tr>";
335 $alt++;
336 }
337 $deps= $alt;
338 }
340 // Fill with contents, sort as configured
341 foreach ($this->entries as $row => $entry) {
342 $trow= "";
344 // Render multi select if needed
345 if ($this->multiSelect) {
346 $trow.="<td style='text-align:center;width:20px;' class='list0'><input type='checkbox' id='listing_selected_$row' name='listing_selected_$row'></td>\n";
347 }
349 foreach ($this->xmlData['table']['column'] as $index => $config) {
350 $renderedCell= $this->renderCell($config['value'], $entry, $row);
351 $trow.="<td ".$this->colprops[$index]." class='list0'>".$renderedCell."</td>\n";
353 // Save rendered column
354 $sort= preg_replace('/.*>([^<]+)<.*$/', '$1', $renderedCell);
355 $sort= preg_replace('/ /', '', $sort);
356 if (preg_match('/</', $sort)){
357 $sort= "";
358 }
359 $this->entries[$row]["_sort$index"]= $sort;
360 }
362 // Save rendered entry
363 $this->entries[$row]['_rendered']= $trow;
364 }
366 // Complete list by sorting entries for _sort$index and appending them to the output
367 $entryIterator= new listingSortIterator($this->entries, $this->sortDirection[$this->sortColumn], "_sort".$this->sortColumn, $this->sortType);
368 foreach ($entryIterator as $row => $entry){
369 $alt++;
370 $result.="<tr class='rowxp".($alt&1)."'>\n";
371 $result.= $entry['_rendered'];
372 $result.="</tr>\n";
373 }
375 // Need to fill the list if it's not full (nobody knows why this is 22 ;-))
376 $emptyListStyle= (count($this->entries) + (($this->useSpan && count($this->entries))?$deps:0) == 0)?"border:0;":"";
377 if ((count($this->entries) + $deps) < 22) {
378 $result.= "<tr>";
379 for ($i= 0; $i<$this->numColumns; $i++) {
380 if ($i == 0) {
381 $result.= "<td class='list1nohighlight' style='$emptyListStyle height:100%;'> </td>";
382 continue;
383 }
384 if ($i != $this->numColumns-1) {
385 $result.= "<td class='list1nohighlight' style='$emptyListStyle'> </td>";
386 } else {
387 $result.= "<td class='list1nohighlight' style='border-right:1px solid #AAA;$emptyListStyle'> </td>";
388 }
389 }
390 $result.= "</tr>";
391 }
393 $result.= "</table></div></td></tr>";
395 // Add the footer if requested
396 if ($this->showFooter) {
397 $result.= "<tr><td class='scrollhead'><table summary='' style='width:100%' cellspacing='0' id='t_scrollfoot'><tr><td class='listfooter' style='border-bottom:0px;'>";
399 foreach ($this->objectTypes as $objectType) {
400 if (isset($this->objectTypeCount[$objectType['label']])) {
401 $label= _($objectType['label']);
402 $result.= "<img class='center' src='".$objectType['image']."' title='$label' alt='$label'> ".$this->objectTypeCount[$objectType['label']]." ";
403 }
404 }
406 $result.= "<td class='listfooter' style='width:13px;border-right:0px;'> </td></table></td></tr>";
407 }
409 $result.= "</table></div>";
411 $smarty= get_smarty();
412 $smarty->assign("usePrototype", "true");
413 $smarty->assign("FILTER", $this->filter->render());
414 $smarty->assign("SIZELIMIT", print_sizelimit_warning());
415 $smarty->assign("LIST", $result);
417 // Assign navigation elements
418 $nav= $this->renderNavigation();
419 foreach ($nav as $key => $html) {
420 $smarty->assign($key, $html);
421 }
423 // Assign action menu / base
424 $smarty->assign("ACTIONS", $this->renderActionMenu());
425 $smarty->assign("BASE", $this->renderBase());
427 // Assign separator
428 $smarty->assign("SEPARATOR", "<img src='images/lists/seperator.png' alt='-' align='middle' height='16' width='1' class='center'>");
430 // Assign summary
431 $smarty->assign("HEADLINE", $this->headline);
433 return ($smarty->fetch(get_template_path($this->xmlData['definition']['template'], true)));
434 }
437 function update()
438 {
439 global $config;
440 $ui= get_userinfo();
442 // Reset object counter / DN mapping
443 $this->objectTypeCount= array();
444 $this->objectDnMapping= array();
446 // Do not do anything if this is not our PID
447 if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->pid) {
448 return;
449 }
451 // Save base
452 if (isset($_POST['BASE']) && $this->baseMode) {
453 $base= validate($_POST['BASE']);
454 if (isset($this->bases[$base])) {
455 $this->base= $base;
456 session::global_set("CurrentMainBase", $this->base);
457 }
458 }
460 // Override the base if we got a message from the browser navigation
461 if ($this->departmentBrowser && isset($_GET['act'])) {
462 if (preg_match('/^department_([0-9]+)$/', validate($_GET['act']), $match)){
463 if (isset($this->departments[$match[1]])){
464 $this->base= $this->departments[$match[1]]['dn'];
465 }
466 }
467 }
469 // Filter POST with "act" attributes -> posted from action menu
470 if (isset($_POST['exec_act']) && $_POST['act'] != '') {
471 if (preg_match('/^export.*$/', $_POST['act']) && isset($this->exporter[$_POST['act']])) {
472 $exporter= $this->exporter[$_POST['act']];
473 $userinfo= ", "._("created by")." ".$ui->cn." - ".strftime('%A, %d. %B %Y, %H:%M:%S');
474 $entryIterator= new listingSortIterator($this->entries, $this->sortDirection[$this->sortColumn], "_sort".$this->sortColumn, $this->sortType);
475 $sortedEntries= array();
476 foreach ($entryIterator as $entry){
477 $sortedEntries[]= $entry;
478 }
479 $instance= new $exporter['class']($this->headline.$userinfo, $this->plainHeader, $sortedEntries, $this->exportColumns);
480 $type= call_user_func(array($exporter['class'], "getInfo"));
481 $type= $type[$_POST['act']];
482 send_binary_content($instance->query(), $type['filename'], $type= $type['mime']);
483 }
484 }
486 // Filter GET with "act" attributes
487 if (isset($_GET['act'])) {
488 $key= validate($_GET['act']);
489 if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
490 // Switch to new column or invert search order?
491 $column= $match[1];
492 if ($this->sortColumn != $column) {
493 $this->sortColumn= $column;
494 } else {
495 $this->sortDirection[$column]= !$this->sortDirection[$column];
496 }
498 // Allow header to update itself according to the new sort settings
499 $this->renderHeader();
500 }
501 }
503 // Override base if we got signals from the navigation elements
504 $action= "";
505 foreach ($_POST as $key => $value) {
506 if (preg_match('/^(ROOT|BACK|HOME)_x$/', $key, $match)) {
507 $action= $match[1];
508 break;
509 }
510 }
512 // Navigation handling
513 if ($action == 'ROOT') {
514 $deps= $ui->get_module_departments($this->module);
515 $this->base= $deps[0];
516 }
517 if ($action == 'BACK') {
518 $deps= $ui->get_module_departments($this->module);
519 $base= preg_replace("/^[^,]+,/", "", $this->base);
520 if(in_array_ics($base, $deps)){
521 $this->base= $base;
522 }
523 }
524 if ($action == 'HOME') {
525 $ui= get_userinfo();
526 $this->base= $this->filter->getObjectBase($ui->dn);
527 }
529 // Reload departments
530 if ($this->departmentBrowser){
531 $this->departments= $this->getDepartments();
532 }
534 // Update filter and refresh entries
535 $this->filter->setBase($this->base);
536 $this->entries= $this->filter->query();
537 }
540 function setBase($base)
541 {
542 $this->base= $base;
543 }
546 function getBase()
547 {
548 return $this->base;
549 }
552 function parseLayout($layout)
553 {
554 $result= array();
555 $layout= preg_replace("/^\|/", "", $layout);
556 $layout= preg_replace("/\|$/", "", $layout);
557 $cols= split("\|", $layout);
558 foreach ($cols as $index => $config) {
559 if ($config != "") {
560 $res= "";
561 $components= split(';', $config);
562 foreach ($components as $part) {
563 if (preg_match("/^r$/", $part)) {
564 $res.= "text-align:right;";
565 continue;
566 }
567 if (preg_match("/^l$/", $part)) {
568 $res.= "text-align:left;";
569 continue;
570 }
571 if (preg_match("/^c$/", $part)) {
572 $res.= "text-align:center;";
573 continue;
574 }
575 if (preg_match("/^[0-9]+(|px|%)$/", $part)) {
576 $res.= "width:$part;min-width:$part;";
577 continue;
578 }
579 }
581 // Add minimum width for scalable columns
582 if (!preg_match('/width:/', $res)){
583 $res.= "min-width:200px;";
584 }
586 $result[$index]= " style='$res' ";
587 } else {
588 $result[$index]= " style='min-width:100px'";
589 }
590 }
592 // Save number of columns for later use
593 $this->numColumns= count($cols);
595 return $result;
596 }
599 function renderCell($data, $config, $row)
600 {
601 // Replace flat attributes in data string
602 for ($i= 0; $i<$config['count']; $i++) {
603 $attr= $config[$i];
604 $value= "";
605 if (is_array($config[$attr])) {
606 $value= $config[$attr][0];
607 } else {
608 $value= $config[$attr];
609 }
610 $data= preg_replace("/%\{$attr\}/", $value, $data);
611 }
613 // Watch out for filters and prepare to execute them
614 $data= $this->processElementFilter($data, $config, $row);
616 // Replace all non replaced %{...} instances because they
617 // are non resolved attributes or filters
618 $data= preg_replace('/%{[^}]+}/', ' ', $data);
620 return $data;
621 }
624 function renderBase()
625 {
626 if (!$this->baseMode) {
627 return;
628 }
630 $result= "<select name='BASE' onChange='mainform.submit()' size='1'>";
631 $firstDN= null;
632 $found= false;
634 foreach ($this->bases as $key=>$value) {
635 // Keep first entry to fall back eventually
636 if(!$firstDN) {
637 $firstDN= $key;
638 }
640 // Prepare to render entry
641 $selected= "";
642 if ($key == $this->base) {
643 $selected= " selected";
644 $found= true;
645 }
646 $result.= "<option value='".$key."'$selected>".$value."</option>";
647 }
648 $result.= "</select>";
650 // Reset the currently used base to the first DN we found if there
651 // was no match.
652 if(!$found){
653 $this->base = $firstDN;
654 }
656 return $result;
657 }
660 function processElementFilter($data, $config, $row)
661 {
662 preg_match_all("/%\{filter:([^(]+)\((.*)\)\}/", $data, $matches, PREG_SET_ORDER);
664 foreach ($matches as $match) {
665 if (!isset($this->filters[$match[1]])) {
666 continue;
667 }
668 $cl= preg_replace('/::.*$/', '', $this->filters[$match[1]]);
669 $method= preg_replace('/^.*::/', '', $this->filters[$match[1]]);
671 // Prepare params for function call
672 $params= array();
673 preg_match_all('/"[^"]+"|[^,]+/', $match[2], $parts);
674 foreach ($parts[0] as $param) {
676 // Row is replaced by the row number
677 if ($param == "row") {
678 $params[]= $row;
679 }
681 // pid is replaced by the current PID
682 if ($param == "pid") {
683 $params[]= $this->pid;
684 }
686 // base is replaced by the current base
687 if ($param == "base") {
688 $params[]= $this->getBase();
689 }
691 // Fixie with "" is passed directly
692 if (preg_match('/^".*"$/', $param)){
693 $params[]= preg_replace('/"/', '', $param);
694 }
696 // LDAP variables get replaced by their objects
697 for ($i= 0; $i<$config['count']; $i++) {
698 if ($param == $config[$i]) {
699 $values= $config[$config[$i]];
700 if (is_array($values)){
701 unset($values['count']);
702 }
703 $params[]= $values;
704 }
705 }
707 // Move dn if needed
708 if ($param == "dn") {
709 $params[]= LDAP::fix($config["dn"]);
710 }
711 }
713 // Replace information
714 if ($cl == "listing") {
715 // Non static call - seems to result in errors
716 $data= @preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($this, "$method"), $params), $data);
717 } else {
718 // Static call
719 $data= preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($cl, $method), $params), $data);
720 }
721 }
723 return $data;
724 }
727 function getObjectType($types, $classes)
728 {
729 // Walk thru types and see if there's something matching
730 foreach ($types as $objectType) {
731 $ocs= $objectType['objectClass'];
732 if (!is_array($ocs)){
733 $ocs= array($ocs);
734 }
736 $found= true;
737 foreach ($ocs as $oc){
738 if (preg_match('/^!(.*)$/', $oc, $match)) {
739 $oc= $match[1];
740 if (in_array($oc, $classes)) {
741 $found= false;
742 }
743 } else {
744 if (!in_array($oc, $classes)) {
745 $found= false;
746 }
747 }
748 }
750 if ($found) {
751 return $objectType;
752 }
753 }
755 return null;
756 }
759 function filterObjectType($dn, $classes)
760 {
761 // Walk thru classes and return on first match
762 $result= " ";
764 $objectType= $this->getObjectType($this->objectTypes, $classes);
765 if ($objectType) {
766 $this->objectDnMapping[$dn]= $objectType["objectClass"];
767 $result= "<img class='center' title='".LDAP::fix($dn)."' src='".$objectType["image"]."'>";
768 if (!isset($this->objectTypeCount[$objectType['label']])) {
769 $this->objectTypeCount[$objectType['label']]= 0;
770 }
771 $this->objectTypeCount[$objectType['label']]++;
772 }
774 return $result;
775 }
778 function filterActions($dn, $row, $classes)
779 {
780 // Do nothing if there's no menu defined
781 if (!isset($this->xmlData['actiontriggers']['action'])) {
782 return " ";
783 }
785 // Go thru all actions
786 $result= "";
787 $actions= $this->xmlData['actiontriggers']['action'];
788 foreach($actions as $action) {
789 // Skip the entry completely if there's no permission to execute it
790 if (!$this->hasActionPermission($action, $dn)) {
791 $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
792 continue;
793 }
795 // Skip entry if the pseudo filter does not fit
796 if (isset($action['filter']) && preg_match('/^[a-z0-9_]+!?=[a-z0-9_]+$/i', $action['filter'])) {
797 list($fa, $fv)= split('=', $action['filter']);
798 if (preg_match('/^(.*)!$/', $fa, $m)){
799 $fa= $m[1];
800 if (isset($this->entries[$row][$fa]) && $this->entries[$row][$fa][0] == $fv) {
801 $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
802 continue;
803 }
804 } else {
805 if (!isset($this->entries[$row][$fa]) && !$this->entries[$row][$fa][0] == $fv) {
806 $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
807 continue;
808 }
809 }
810 }
813 // If there's an objectclass definition and we don't have it
814 // add an empty picture here.
815 if (isset($action['objectclass'])){
816 $objectclass= $action['objectclass'];
817 if (preg_match('/^!(.*)$/', $objectclass, $m)){
818 $objectclass= $m[1];
819 if(in_array($objectclass, $classes)) {
820 $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
821 continue;
822 }
823 } else {
824 if(!in_array($objectclass, $classes)) {
825 $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
826 continue;
827 }
828 }
829 }
831 // Render normal entries as usual
832 if ($action['type'] == "entry") {
833 $label= $this->processElementFilter($action['label'], $this->entries[$row], $row);
834 $image= $this->processElementFilter($action['image'], $this->entries[$row], $row);
835 $result.="<input class='center' type='image' src='$image' title='$label' ".
836 "name='listing_".$action['name']."_$row' style='padding:1px'>";
837 }
839 // Handle special types
840 if ($action['type'] == "copypaste" || $action['type'] == "snapshot") {
842 $objectType= $this->getObjectType($this->objectTypes, $this->entries[$row]['objectClass']);
843 $category= $class= null;
844 if ($objectType) {
845 $category= $objectType['category'];
846 $class= $objectType['class'];
847 }
849 if ($action['type'] == "copypaste") {
850 $result.= $this->renderCopyPasteActions($row, $this->entries[$row]['dn'], $category, $class);
851 } else {
852 $result.= $this->renderSnapshotActions($row, $this->entries[$row]['dn'], $category, $class);
853 }
854 }
855 }
857 return $result;
858 }
861 function filterDepartmentLink($row, $dn, $description)
862 {
863 $attr= $this->departments[$row]['sort-attribute'];
864 $name= $this->departments[$row][$attr];
865 if (is_array($name)){
866 $name= $name[0];
867 }
868 $result= sprintf("%s [%s]", $name, $description[0]);
869 return("<a href='?plug=".$_GET['plug']."&PID=$this->pid&act=department_$row' title='$dn'>$result</a>");
870 }
873 function filterLink()
874 {
875 $result= " ";
877 $row= func_get_arg(0);
878 $pid= $this->pid;
879 $dn= LDAP::fix(func_get_arg(1));
880 $params= array(func_get_arg(2));
882 // Collect sprintf params
883 for ($i = 3;$i < func_num_args();$i++) {
884 $val= func_get_arg($i);
885 if (is_array($val)){
886 $params[]= $val[0];
887 continue;
888 }
889 $params[]= $val;
890 }
892 $result= " ";
893 $trans= call_user_func_array("sprintf", $params);
894 if ($trans != "") {
895 return("<a href='?plug=".$_GET['plug']."&PID=$pid&act=listing_edit_$row' title='$dn'>$trans</a>");
896 }
898 return $result;
899 }
902 function renderNavigation()
903 {
904 $result= array();
905 $enableBack = true;
906 $enableRoot = true;
907 $enableHome = true;
909 $ui = get_userinfo();
911 /* Check if base = first available base */
912 $deps = $ui->get_module_departments($this->module);
914 if(!count($deps) || $deps[0] == $this->filter->base){
915 $enableBack = false;
916 $enableRoot = false;
917 }
919 $listhead ="";
921 /* Check if we are in users home department */
922 if(!count($deps) || $this->filter->base == $this->filter->getObjectBase($ui->dn)){
923 $enableHome = false;
924 }
926 /* Draw root button */
927 if($enableRoot){
928 $result["ROOT"]= "<input class='center' type='image' src='images/lists/root.png' align='middle' ".
929 "title='"._("Go to root department")."' name='ROOT' alt='"._("Root")."'>";
930 }else{
931 $result["ROOT"]= "<img src='images/lists/root_grey.png' class='center' alt='"._("Root")."'>";
932 }
934 /* Draw back button */
935 if($enableBack){
936 $result["BACK"]= "<input class='center' type='image' align='middle' src='images/lists/back.png' ".
937 "title='"._("Go up one department")."' alt='"._("Up")."' name='BACK'>";
938 }else{
939 $result["BACK"]= "<img src='images/lists/back_grey.png' class='center' alt='"._("Up")."'>";
940 }
942 /* Draw home button */
943 if($enableHome){
944 $result["HOME"]= "<input class='center' type='image' align='middle' src='images/lists/home.png' ".
945 "title='"._("Go to users department")."' alt='"._("Home")."' name='HOME'>";
946 }else{
947 $result["HOME"]= "<img src='images/lists/home_grey.png' class='center' alt='"._("Home")."'>";
948 }
950 /* Draw reload button, this button is enabled everytime */
951 $result["RELOAD"]= "<input class='center' type='image' src='images/lists/reload.png' align='middle' ".
952 "title='"._("Reload list")."' name='REFRESH' alt='"._("Submit")."'>";
954 return ($result);
955 }
958 function getAction()
959 {
960 // Do not do anything if this is not our PID, or there's even no PID available...
961 if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->pid) {
962 return;
963 }
965 $result= array("targets" => array(), "action" => "");
967 // Filter GET with "act" attributes
968 if (isset($_GET['act'])) {
969 $key= validate($_GET['act']);
970 $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)$/', '$1', $key);
971 if (isset($this->entries[$target]['dn'])) {
972 $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+$/', '$1', $key);
973 $result['targets'][]= $this->entries[$target]['dn'];
974 }
976 // Drop targets if empty
977 if (count($result['targets']) == 0) {
978 unset($result['targets']);
979 }
980 return $result;
981 }
983 // Filter POST with "listing_" attributes
984 foreach ($_POST as $key => $prop) {
986 // Capture selections
987 if (preg_match('/^listing_selected_[0-9]+$/', $key)) {
988 $target= preg_replace('/^listing_selected_([0-9]+)$/', '$1', $key);
989 if (isset($this->entries[$target]['dn'])) {
990 $result['targets'][]= $this->entries[$target]['dn'];
991 }
992 continue;
993 }
995 // Capture action with target - this is a one shot
996 if (preg_match('/^listing_[a-zA-Z_]+_[0-9]+(|_x)$/', $key)) {
997 $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)(|_x)$/', '$1', $key);
998 if (isset($this->entries[$target]['dn'])) {
999 $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+(|_x)$/', '$1', $key);
1000 $result['targets']= array($this->entries[$target]['dn']);
1001 }
1002 break;
1003 }
1005 // Capture action without target
1006 if (preg_match('/^listing_[a-zA-Z_]+(|_x)$/', $key)) {
1007 $result['action']= preg_replace('/^listing_([a-zA-Z_]+)(|_x)$/', '$1', $key);
1008 continue;
1009 }
1010 }
1012 // Filter POST with "act" attributes -> posted from action menu
1013 if (isset($_POST['act']) && $_POST['act'] != '') {
1014 if (!preg_match('/^export.*$/', $_POST['act'])){
1015 $result['action']= validate($_POST['act']);
1016 }
1017 }
1019 // Drop targets if empty
1020 if (count($result['targets']) == 0) {
1021 unset($result['targets']);
1022 }
1023 return $result;
1024 }
1027 function renderActionMenu()
1028 {
1029 // Don't send anything if the menu is not defined
1030 if (!isset($this->xmlData['actionmenu']['action'])){
1031 return "";
1032 }
1034 // Array?
1035 if (isset($this->xmlData['actionmenu']['action']['type'])){
1036 $this->xmlData['actionmenu']['action']= array($this->xmlData['actionmenu']['action']);
1037 }
1039 // Load shortcut
1040 $actions= &$this->xmlData['actionmenu']['action'];
1041 $result= "<input type='hidden' name='act' id='actionmenu' value=''><div style='display:none'><input type='submit' name='exec_act' id='exec_act' value=''></div>".
1042 "<ul class='level1' id='root'><li><a href='#'>"._("Actions")." <img ".
1043 "border=0 class='center' src='images/lists/sort-down.png'></a>";
1045 // Build ul/li list
1046 $result.= $this->recurseActions($actions);
1048 return "<div id='pulldown'>".$result."</li></ul><div>";
1049 }
1052 function recurseActions($actions)
1053 {
1054 global $class_mapping;
1055 static $level= 2;
1056 $result= "<ul class='level$level'>";
1057 $separator= "";
1059 foreach ($actions as $action) {
1061 // Skip the entry completely if there's no permission to execute it
1062 if (!$this->hasActionPermission($action, $this->filter->base)) {
1063 continue;
1064 }
1066 // Skip entry if there're missing dependencies
1067 if (isset($action['depends'])) {
1068 $deps= is_array($action['depends'])?$action['depends']:array($action['depends']);
1069 foreach($deps as $clazz) {
1070 if (!isset($class_mapping[$clazz])){
1071 continue 2;
1072 }
1073 }
1074 }
1076 // Fill image if set
1077 $img= "";
1078 if (isset($action['image'])){
1079 $img= "<img border='0' class='center' src='".$action['image']."'> ";
1080 }
1082 if ($action['type'] == "separator"){
1083 $separator= " style='border-top:1px solid #AAA' ";
1084 continue;
1085 }
1087 // Dive into subs
1088 if ($action['type'] == "sub" && isset($action['action'])) {
1089 $level++;
1090 if (isset($action['label'])){
1091 $result.= "<li$separator><a href='#'>$img"._($action['label'])." <img border='0' src='images/forward-arrow.png'></a>";
1092 }
1093 $result.= $this->recurseActions($action['action'])."</li>";
1094 $level--;
1095 $separator= "";
1096 continue;
1097 }
1099 // Render entry elseways
1100 if (isset($action['label'])){
1101 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"".$action['name']."\";mainform.submit();'>$img"._($action['label'])."</a></li>";
1102 }
1104 // Check for special types
1105 switch ($action['type']) {
1106 case 'copypaste':
1107 $result.= $this->renderCopyPasteMenu($separator);
1108 break;
1110 case 'snapshot':
1111 $result.= $this->renderSnapshotMenu($separator);
1112 break;
1114 case 'exporter':
1115 $result.= $this->renderExporterMenu($separator);
1116 break;
1118 case 'daemon':
1119 $result.= $this->renderDaemonMenu($separator);
1120 break;
1121 }
1123 $separator= "";
1124 }
1126 $result.= "</ul>";
1127 return $result;
1128 }
1131 function hasActionPermission($action, $dn)
1132 {
1133 $ui= get_userinfo();
1135 if (isset($action['acl'])) {
1136 $acls= $action['acl'];
1137 if (!is_array($acls)) {
1138 $acls= array($acls);
1139 }
1141 // Every ACL has to pass
1142 foreach ($acls as $acl) {
1143 $module= $this->module;
1144 $aclList= array();
1146 // Split for category and plugins if needed
1147 // match for "[rw]" style entries
1148 if (preg_match('/^\[([rwcdm]+)\]$/', $acl, $match)){
1149 $aclList= array($match[1]);
1150 }
1152 // match for "users[rw]" style entries
1153 if (preg_match('/^([a-zA-Z0-9]+)\[([rwcdm]+)\]$/', $acl, $match)){
1154 $module= $match[1];
1155 $aclList= array($match[2]);
1156 }
1158 // match for "users/user[rw]" style entries
1159 if (preg_match('/^([a-zA-Z0-9]+\/[a-zA-Z0-9]+)\[([rwcdm]+)\]$/', $acl, $match)){
1160 $module= $match[1];
1161 $aclList= array($match[2]);
1162 }
1164 // match "users/user[userPassword:rw(,...)*]" style entries
1165 if (preg_match('/^([a-zA-Z0-9]+\/[a-zA-Z0-9]+)\[([a-zA-Z0-9]+:[rwcdm]+(,[a-zA-Z0-9]+:[rwcdm]+)*)\]$/', $acl, $match)){
1166 $module= $match[1];
1167 $aclList= split(',', $match[2]);
1168 }
1170 // Walk thru prepared ACL by using $module
1171 foreach($aclList as $sAcl) {
1172 $checkAcl= "";
1174 // Category or detailed permission?
1175 if (strpos('/', $module) === false) {
1176 if (preg_match('/([a-zA-Z0-9]+):([rwcdm]+)/', $sAcl, $m) ) {
1177 $checkAcl= $ui->get_permissions($dn, $module, $m[1]);
1178 $sAcl= $m[2];
1179 } else {
1180 $checkAcl= $ui->get_permissions($dn, $module, '0');
1181 }
1182 } else {
1183 $checkAcl= $ui->get_category_permissions($dn, $module);
1184 }
1186 // Split up remaining part of the acl and check if it we're
1187 // allowed to do something...
1188 $parts= str_split($sAcl);
1189 foreach ($parts as $part) {
1190 if (strpos($checkAcl, $part) === false){
1191 return false;
1192 }
1193 }
1195 }
1196 }
1197 }
1199 return true;
1200 }
1203 function refreshBasesList()
1204 {
1205 global $config;
1206 $ui= get_userinfo();
1208 // Do some array munching to get it user friendly
1209 $ids= $config->idepartments;
1210 $d= $ui->get_module_departments($this->module);
1211 $k_ids= array_keys($ids);
1212 $deps= array_intersect($d,$k_ids);
1214 // Fill internal bases list
1215 $this->bases= array();
1216 foreach($k_ids as $department){
1217 $this->bases[$department] = $ids[$department];
1218 }
1219 }
1222 function getDepartments()
1223 {
1224 $departments= array();
1225 $ui= get_userinfo();
1227 // Get list of supported department types
1228 $types = departmentManagement::get_support_departments();
1230 // Load departments allowed by ACL
1231 $validDepartments = $ui->get_module_departments($this->module);
1233 // Build filter and look in the LDAP for possible sub departments
1234 // of current base
1235 $filter= "(&(objectClass=gosaDepartment)(|";
1236 $attrs= array("description", "objectClass");
1237 foreach($types as $name => $data){
1238 $filter.= "(objectClass=".$data['OC'].")";
1239 $attrs[]= $data['ATTR'];
1240 }
1241 $filter.= "))";
1242 $res= get_list($filter, $this->module, $this->base, $attrs, GL_NONE);
1244 // Analyze list of departments
1245 foreach ($res as $department) {
1246 if (!in_array($department['dn'], $validDepartments)) {
1247 continue;
1248 }
1250 // Add the attribute where we use for sorting
1251 $oc= null;
1252 foreach(array_keys($types) as $type) {
1253 if (in_array($type, $department['objectClass'])) {
1254 $oc= $type;
1255 break;
1256 }
1257 }
1258 $department['sort-attribute']= $types[$oc]['ATTR'];
1260 // Move to the result list
1261 $departments[]= $department;
1262 }
1264 return $departments;
1265 }
1268 function renderCopyPasteMenu($separator, $copy= true, $cut= true)
1269 {
1270 // We can only provide information if we've got a copypaste handler
1271 // instance
1272 if(!(isset($this->copyPasteHandler) && is_object($this->copyPasteHandler))){
1273 return "";
1274 }
1276 // Presets
1277 $result= "";
1278 $read= $paste= false;
1279 $ui= get_userinfo();
1281 // Switch flags to on if there's at least one category which allows read/paste
1282 foreach($this->categories as $category){
1283 $read= $read || preg_match('/r/', $ui->get_category_permissions($this->base, $category));
1284 $paste= $paste || $ui->is_pasteable($this->base, $category) == 1;
1285 }
1288 // Draw entries that allow copy and cut
1289 if($read){
1291 // Copy entry
1292 if($copy){
1293 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"copy\";document.getElementById(\"exec_act\").click();'><img src='images/lists/copy.png' alt='' border='0' class='center'> "._("Copy")."</a></li>";
1294 $separator= "";
1295 }
1297 // Cut entry
1298 if($cut){
1299 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"cut\";document.getElementById(\"exec_act\").click();'><img src='images/lists/cut.png' alt='' border='0' class='center'> "._("Cut")."</a></li>";
1300 $separator= "";
1301 }
1302 }
1304 // Draw entries that allow pasting entries
1305 if($paste){
1306 if($this->copyPasteHandler->entries_queued()){
1307 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"paste\";document.getElementById(\"exec_act\").click();'><img src='images/lists/paste.png' alt='' border='0' class='center'> "._("Paste")."</a></li>";
1308 }else{
1309 $result.= "<li$separator><a href='#'><img src='images/lists/paste-grey.png' alt='' border='0' class='center'> "._("Paste")."</a></li>";
1310 }
1311 }
1313 return($result);
1314 }
1317 function renderCopyPasteActions($row, $dn, $category, $class, $copy= true, $cut= true)
1318 {
1319 // We can only provide information if we've got a copypaste handler
1320 // instance
1321 if(!(isset($this->copyPasteHandler) && is_object($this->copyPasteHandler))){
1322 return "";
1323 }
1325 // Presets
1326 $ui = get_userinfo();
1327 $result = "";
1329 // Render cut entries
1330 if($cut){
1331 if($ui->is_cutable($dn, $category, $class)){
1332 $result .= "<input class='center' type='image'
1333 src='images/lists/cut.png' alt='"._("Cut")."' name='listing_cut_$row' title='"._("Cut this entry")."' style='padding:1px'>";
1334 }else{
1335 $result.="<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1336 }
1337 }
1339 // Render copy entries
1340 if($copy){
1341 if($ui->is_copyable($dn, $category, $class)){
1342 $result.= "<input class='center' type='image'
1343 src='images/lists/copy.png' alt='"._("Copy")."' name='listing_copy_$row' title='"._("Copy this entry")."' style='padding:1px'>";
1344 }else{
1345 $result.="<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1346 }
1347 }
1349 return($result);
1350 }
1353 function renderSnapshotMenu($separator)
1354 {
1355 // We can only provide information if we've got a snapshot handler
1356 // instance
1357 if(!(isset($this->snapshotHandler) && is_object($this->snapshotHandler))){
1358 return "";
1359 }
1361 // Presets
1362 $result = "";
1363 $ui = get_userinfo();
1365 if($this->snapshotHandler->enabled() && $ui->allow_snapshot_restore($this->base, $this->module)){
1367 // Check if there is something to restore
1368 $restore= false;
1369 foreach($this->snapshotHandler->getSnapshotBases() as $base){
1370 $restore= $restore || count($this->snapshotHandler->getDeletedSnapshots($base)) > 0;
1371 }
1373 // Draw icons according to the restore flag
1374 if($restore){
1375 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"restore\";document.getElementById(\"exec_act\").click();'><img src='images/lists/restore.png' alt='' border='0' class='center'> "._("Restore snapshots")."</a></li>";
1376 }else{
1377 $result.= "<li$separator><a href='#'><img src='images/lists/restore_grey.png' alt='' border='0' class='center'> "._("Restore snapshots")."</a></li>";
1378 }
1379 }
1381 return($result);
1382 }
1385 function renderExporterMenu($separator)
1386 {
1387 // Presets
1388 $result = "";
1390 // Draw entries
1391 $result.= "<li$separator><a href='#'><img border='0' class='center' src='images/lists/export.png'> "._("Export list")." <img border='0' src='images/forward-arrow.png'></a><ul class='level3'>";
1393 // Export CVS as build in exporter
1394 foreach ($this->exporter as $action => $exporter) {
1395 $result.= "<li><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"$action\";document.getElementById(\"exec_act\").click();'><img border='0' class='center' src='".$exporter['image']."'> ".$exporter['label']."</a></li>";
1396 }
1398 // Finalize list
1399 $result.= "</ul></li>";
1401 return($result);
1402 }
1405 function renderSnapshotActions($row, $dn, $category, $class, $copy= true, $cut= true)
1406 {
1407 // We can only provide information if we've got a snapshot handler
1408 // instance
1409 if(!(isset($this->snapshotHandler) && is_object($this->snapshotHandler))){
1410 return "";
1411 }
1413 // Presets
1414 $result= "";
1415 $ui = get_userinfo();
1417 // Only act if enabled here
1418 if($this->snapshotHandler->enabled()){
1420 // Draw restore button
1421 if ($ui->allow_snapshot_restore($dn, $category)){
1423 // Do we have snapshots for this dn?
1424 if($this->snapshotHandler->hasSnapshots($dn)){
1425 $result.= "<input class='center' type='image' src='images/lists/restore.png' ".
1426 "alt='"._("Restore snapshot")."' name='listing_restore_$row' title='".
1427 _("Restore snapshot")."' style='padding:1px'>";
1428 } else {
1429 $result.= "<img src='images/lists/restore_grey.png' alt=' ' class='center' style='padding:1px'>";
1430 }
1431 }
1433 // Draw snapshot button
1434 if($ui->allow_snapshot_create($dn, $category)){
1435 $result.= "<input class='center' type='image' src='images/snapshot.png' ".
1436 "alt='"._("Create snapshot")."' name='listing_snapshot_$row' title='".
1437 _("Create a new snapshot from this object")."' style='padding:1px'>";
1438 }else{
1439 $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1440 }
1441 }
1443 return($result);
1444 }
1447 function renderDaemonMenu($separator)
1448 {
1449 $result= "";
1451 // If there is a daemon registered, draw the menu entries
1452 if(class_available("DaemonEvent")){
1453 $events= DaemonEvent::get_event_types_by_category($this->categories);
1454 if(isset($events['BY_CLASS']) && count($events['BY_CLASS'])){
1455 foreach($events['BY_CLASS'] as $name => $event){
1456 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value=\"$name\";document.getElementById(\"exec_act\").click();'>".$event['MenuImage']." ".$event['s_Menu_Name']."</a></li>";
1457 $separator= "";
1458 }
1459 }
1460 }
1462 return $result;
1463 }
1466 function getType($dn)
1467 {
1468 if (isset($this->objectDnMapping[$dn])) {
1469 return $this->objectDnMapping[$dn];
1470 }
1471 return null;
1472 }
1474 }
1476 ?>