1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class listing {
25 var $xmlData;
26 var $entries;
27 var $departments= array();
28 var $departmentBrowser= false;
29 var $departmentRootVisible= false;
30 var $multiSelect= false;
31 var $template;
32 var $headline;
33 var $module;
34 var $base;
35 var $sortDirection= null;
36 var $sortColumn= null;
37 var $sortAttribute;
38 var $sortType;
39 var $numColumns;
40 var $baseMode= false;
41 var $bases= array();
42 var $header= array();
43 var $colprops= array();
44 var $filters= array();
45 var $pid;
46 var $objectTypes= array();
47 var $objectTypeCount= array();
48 var $copyPasteHandler= null;
49 var $snapshotHandler= null;
50 var $exporter= array();
51 var $exportColumns= array();
52 var $useSpan= false;
53 var $height= 0;
56 function listing($filename)
57 {
58 global $config;
59 global $class_mapping;
61 // Initialize pid
62 $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
64 if (!$this->load($filename)) {
65 die("Cannot parse $filename!");
66 }
68 // Set base for filter
69 if ($this->baseMode) {
70 $this->base= session::global_get("CurrentMainBase");
71 if ($this->base == null) {
72 $this->base= $config->current['BASE'];
73 }
74 $this->refreshBasesList();
75 } else {
76 $this->base= $config->current['BASE'];
77 }
79 // Move footer information
80 $this->showFooter= ($config->get_cfg_value("listSummary") == "true");
82 // Register build in filters
83 $this->registerElementFilter("objectType", "listing::filterObjectType");
84 $this->registerElementFilter("departmentLink", "listing::filterDepartmentLink");
85 $this->registerElementFilter("link", "listing::filterLink");
86 $this->registerElementFilter("actions", "listing::filterActions");
88 // Load exporters
89 foreach($class_mapping as $class => $dummy) {
90 if (preg_match('/Exporter$/', $class)) {
91 $info= call_user_func(array($class, "getInfo"));
92 if ($info != null) {
93 $this->exporter= array_merge($this->exporter, $info);
94 }
95 }
96 }
97 }
100 function setCopyPasteHandler($handler)
101 {
102 $this->copyPasteHandler= &$handler;
103 }
106 function setHeight($height)
107 {
108 $this->height= $height;
109 }
112 function setSnapshotHandler($handler)
113 {
114 $this->snapshotHandler= &$handler;
115 }
118 function setFilter($filter)
119 {
120 $this->filter= &$filter;
121 if ($this->departmentBrowser){
122 $this->departments= $this->getDepartments();
123 }
124 $this->filter->setBase($this->base);
125 }
128 function registerElementFilter($name, $call)
129 {
130 if (!isset($this->filters[$name])) {
131 $this->filters[$name]= $call;
132 return true;
133 }
135 return false;
136 }
139 function load($filename)
140 {
141 $contents = file_get_contents($filename);
142 $this->xmlData= xml::xml2array($contents, 1);
144 if (!isset($this->xmlData['list'])) {
145 return false;
146 }
148 $this->xmlData= $this->xmlData["list"];
150 // Load some definition values
151 foreach (array("departmentBrowser", "departmentRootVisible", "multiSelect", "baseMode") as $token) {
152 if (isset($this->xmlData['definition'][$token]) &&
153 $this->xmlData['definition'][$token] == "true"){
154 $this->$token= true;
155 }
156 }
158 // Fill objectTypes from departments and xml definition
159 $types = departmentManagement::get_support_departments();
160 foreach ($types as $class => $data) {
161 $this->objectTypes[]= array("label" => $data['TITLE'],
162 "objectClass" => $data['OC'],
163 "image" => $data['IMG']);
164 }
165 $this->categories= array();
166 if (isset($this->xmlData['definition']['objectType'])) {
167 if(isset($this->xmlData['definition']['objectType']['label'])) {
168 $this->xmlData['definition']['objectType']= array($this->xmlData['definition']['objectType']);
169 }
170 foreach ($this->xmlData['definition']['objectType'] as $index => $otype) {
171 $this->objectTypes[]= $this->xmlData['definition']['objectType'][$index];
172 if (isset($this->xmlData['definition']['objectType'][$index]['category'])){
173 $this->categories[]= $this->xmlData['definition']['objectType'][$index]['category'];
174 }
175 }
176 }
178 // Parse layout per column
179 $this->colprops= $this->parseLayout($this->xmlData['table']['layout']);
181 // Prepare table headers
182 $this->renderHeader();
184 // Assign headline/module
185 $this->headline= _($this->xmlData['definition']['label']);
186 $this->module= $this->xmlData['definition']['module'];
187 if (!is_array($this->categories)){
188 $this->categories= array($this->categories);
189 }
191 // Evaluate columns to be exported
192 if (isset($this->xmlData['table']['column'])){
193 foreach ($this->xmlData['table']['column'] as $index => $config) {
194 if (isset($config['export']) && $config['export'] == "true"){
195 $this->exportColumns[]= $index;
196 }
197 }
198 }
200 return true;
201 }
204 function renderHeader()
205 {
206 $this->header= array();
207 $this->plainHeader= array();
209 // Initialize sort?
210 $sortInit= false;
211 if (!$this->sortDirection) {
212 $this->sortColumn= 0;
213 if (isset($this->xmlData['definition']['defaultSortColumn'])){
214 $this->sortColumn= $this->xmlData['definition']['defaultSortColumn'];
215 } else {
216 $this->sortAttribute= "";
217 }
218 $this->sortDirection= array();
219 $sortInit= true;
220 }
222 if (isset($this->xmlData['table']['column'])){
223 foreach ($this->xmlData['table']['column'] as $index => $config) {
224 // Initialize everything to one direction
225 if ($sortInit) {
226 $this->sortDirection[$index]= false;
227 }
229 $sorter= "";
230 if ($index == $this->sortColumn && isset($config['sortAttribute']) &&
231 isset($config['sortType'])) {
232 $this->sortAttribute= $config['sortAttribute'];
233 $this->sortType= $config['sortType'];
234 $sorter= " <img border='0' title='".($this->sortDirection[$index]?_("Up"):_("Down"))."' src='images/lists/sort-".($this->sortDirection[$index]?"up":"down").".png' align='top'>";
235 }
236 $sortable= (isset($config['sortAttribute']));
238 $link= "href='?plug=".$_GET['plug']."&PID=".$this->pid."&act=SORT_$index'";
239 if (isset($config['label'])) {
240 if ($sortable) {
241 $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."><a $link>"._($config['label'])."$sorter</a></td>";
242 } else {
243 $this->header[$index]= "<td class='listheader' ".$this->colprops[$index].">"._($config['label'])."</td>";
244 }
245 $this->plainHeader[]= _($config['label']);
246 } else {
247 if ($sortable) {
248 $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."><a $link> $sorter</a></td>";
249 } else {
250 $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."> </td>";
251 }
252 $this->plainHeader[]= "";
253 }
254 }
255 }
256 }
258 function render()
259 {
260 // Check for exeeded sizelimit
261 if (($message= check_sizelimit()) != ""){
262 return($message);
263 }
265 // Initialize list
266 $result= "<input type='hidden' value='$this->pid' name='PID'>\n";
267 $height= 450;
268 if ($this->height != 0) {
269 $result.= "<input type='hidden' value='$this->height' id='d_height'>\n";
270 $height= $this->height;
271 }
273 $result.= "<div class='contentboxb' id='listing_container' style='border-top:1px solid #B0B0B0;'>\n";
274 $result.= "<table summary='$this->headline' style='width:600px;height:".$height."px;' cellspacing='0' id='t_scrolltable'>
275 <tr><td class='scrollhead'><table summary='' style='width:100%;' cellspacing='0' id='t_scrollhead'>\n";
276 $this->numColumns= count($this->colprops) + ($this->multiSelect?1:0);
278 // Build list header
279 $result.= "<tr>\n";
280 if ($this->multiSelect) {
281 $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";
282 }
283 foreach ($this->header as $header) {
284 $result.= $header;
285 }
287 // Add 13px for scroller
288 $result.= "<td class='listheader' style='width:13px;border-right:0px;'> </td></table></td></tr>\n";
290 // New table for the real list contents
291 $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";
293 // No results? Just take an empty colspanned row
294 if (count($this->entries) + count($this->departments) == 0) {
295 $result.= "<tr class='rowxp0'><td class='list1nohighlight' colspan='$this->numColumns' style='height:100%;border-right:0px;width:100%;'> </td></tr>";
296 }
298 // Line color alternation
299 $alt= 0;
300 $deps= 0;
302 // Draw department browser if configured and we're not in sub mode
303 $this->useSpan= false;
304 if ($this->departmentBrowser && $this->filter->scope != "sub") {
305 // Fill with department browser if configured this way
306 $departmentIterator= new departmentSortIterator($this->departments, $this->sortDirection[$this->sortColumn]);
307 foreach ($departmentIterator as $row => $entry){
308 $result.="<tr class='rowxp".($alt&1)."'>";
310 // Render multi select if needed
311 if ($this->multiSelect) {
312 $result.="<td style='text-align:center;width:20px;' class='list1'> </td>";
313 }
315 // Render defined department columns, fill the rest with some stuff
316 $rest= $this->numColumns - 1;
317 foreach ($this->xmlData['table']['department'] as $index => $config) {
318 $colspan= 1;
319 if (isset($config['span'])){
320 $colspan= $config['span'];
321 $this->useSpan= true;
322 }
323 $result.="<td colspan='$colspan' ".$this->colprops[$index]." class='list1'>".$this->renderCell($config['value'], $entry, $row)."</td>";
324 $rest-= $colspan;
325 }
327 // Fill remaining cols with nothing
328 $last= $this->numColumns - $rest;
329 for ($i= 0; $i<$rest; $i++){
330 $result.= "<td ".$this->colprops[$last+$i-1]." class='list1'> </td>";
331 }
332 $result.="</tr>";
334 $alt++;
335 }
336 $deps= $alt;
337 }
339 // Fill with contents, sort as configured
340 foreach ($this->entries as $row => $entry) {
341 $trow= "";
343 // Render multi select if needed
344 if ($this->multiSelect) {
345 $trow.="<td style='text-align:center;width:20px;' class='list0'><input type='checkbox' id='listing_selected_$row' name='listing_selected_$row'></td>\n";
346 }
348 foreach ($this->xmlData['table']['column'] as $index => $config) {
349 $renderedCell= $this->renderCell($config['value'], $entry, $row);
350 $trow.="<td ".$this->colprops[$index]." class='list0'>".$renderedCell."</td>\n";
352 // Save rendered column
353 $sort= preg_replace('/.*>([^<]+)<.*$/', '$1', $renderedCell);
354 $sort= preg_replace('/ /', '', $sort);
355 if (preg_match('/</', $sort)){
356 $sort= "";
357 }
358 $this->entries[$row]["_sort$index"]= $sort;
359 }
361 // Save rendered entry
362 $this->entries[$row]['_rendered']= $trow;
363 }
365 // Complete list by sorting entries for _sort$index and appending them to the output
366 $entryIterator= new listingSortIterator($this->entries, $this->sortDirection[$this->sortColumn], "_sort".$this->sortColumn, $this->sortType);
367 foreach ($entryIterator as $row => $entry){
368 $alt++;
369 $result.="<tr class='rowxp".($alt&1)."'>\n";
370 $result.= $entry['_rendered'];
371 $result.="</tr>\n";
372 }
374 // Need to fill the list if it's not full (nobody knows why this is 22 ;-))
375 $emptyListStyle= (count($this->entries) + (($this->useSpan && count($this->entries))?$deps:0) == 0)?"border:0;":"";
376 if ((count($this->entries) + $deps) < 22) {
377 $result.= "<tr>";
378 for ($i= 0; $i<$this->numColumns; $i++) {
379 if ($i == 0) {
380 $result.= "<td class='list1nohighlight' style='$emptyListStyle height:100%;'> </td>";
381 continue;
382 }
383 if ($i != $this->numColumns-1) {
384 $result.= "<td class='list1nohighlight' style='$emptyListStyle'> </td>";
385 } else {
386 $result.= "<td class='list1nohighlight' style='border-right:1px solid #AAA;$emptyListStyle'> </td>";
387 }
388 }
389 $result.= "</tr>";
390 }
392 $result.= "</table></div></td></tr>";
394 // Add the footer if requested
395 if ($this->showFooter) {
396 $result.= "<tr><td class='scrollhead'><table summary='' style='width:100%' cellspacing='0' id='t_scrollfoot'><tr><td class='listfooter' style='border-bottom:0px;'>";
398 foreach ($this->objectTypes as $objectType) {
399 if (isset($this->objectTypeCount[$objectType['label']])) {
400 $label= _($objectType['label']);
401 $result.= "<img class='center' src='".$objectType['image']."' title='$label' alt='$label'> ".$this->objectTypeCount[$objectType['label']]." ";
402 }
403 }
405 $result.= "<td class='listfooter' style='width:13px;border-right:0px;'> </td></table></td></tr>";
406 }
408 $result.= "</table></div>";
410 $smarty= get_smarty();
411 $smarty->assign("usePrototype", "true");
412 $smarty->assign("FILTER", $this->filter->render());
413 $smarty->assign("SIZELIMIT", print_sizelimit_warning());
414 $smarty->assign("LIST", $result);
416 // Assign navigation elements
417 $nav= $this->renderNavigation();
418 foreach ($nav as $key => $html) {
419 $smarty->assign($key, $html);
420 }
422 // Assign action menu / base
423 $smarty->assign("ACTIONS", $this->renderActionMenu());
424 $smarty->assign("BASE", $this->renderBase());
426 // Assign separator
427 $smarty->assign("SEPARATOR", "<img src='images/lists/seperator.png' alt='-' align='middle' height='16' width='1' class='center'>");
429 // Assign summary
430 $smarty->assign("HEADLINE", $this->headline);
432 return ($smarty->fetch(get_template_path($this->xmlData['definition']['template'], true)));
433 }
436 function update()
437 {
438 global $config;
439 $ui= get_userinfo();
441 // Reset object counter
442 $this->objectTypeCount= array();
444 // Do not do anything if this is not our PID
445 if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->pid) {
446 return;
447 }
449 // Save base
450 if (isset($_POST['BASE']) && $this->baseMode) {
451 $base= validate($_POST['BASE']);
452 if (isset($this->bases[$base])) {
453 $this->base= $base;
454 }
455 }
457 // Override the base if we got a message from the browser navigation
458 if ($this->departmentBrowser && isset($_GET['act'])) {
459 if (preg_match('/^department_([0-9]+)$/', validate($_GET['act']), $match)){
460 if (isset($this->departments[$match[1]])){
461 $this->base= $this->departments[$match[1]]['dn'];
462 }
463 }
464 }
466 // Filter POST with "act" attributes -> posted from action menu
467 if (isset($_POST['exec_act']) && $_POST['act'] != '') {
468 if (preg_match('/^export.*$/', $_POST['act']) && isset($this->exporter[$_POST['act']])) {
469 $exporter= $this->exporter[$_POST['act']];
470 $userinfo= ", "._("created by")." ".$ui->cn." - ".strftime('%A, %d. %B %Y, %H:%M:%S');
471 $entryIterator= new listingSortIterator($this->entries, $this->sortDirection[$this->sortColumn], "_sort".$this->sortColumn, $this->sortType);
472 $sortedEntries= array();
473 foreach ($entryIterator as $entry){
474 $sortedEntries[]= $entry;
475 }
476 $instance= new $exporter['class']($this->headline.$userinfo, $this->plainHeader, $sortedEntries, $this->exportColumns);
477 $type= call_user_func(array($exporter['class'], "getInfo"));
478 $type= $type[$_POST['act']];
479 send_binary_content($instance->query(), $type['filename'], $type= $type['mime']);
480 }
481 }
483 // Filter GET with "act" attributes
484 if (isset($_GET['act'])) {
485 $key= validate($_GET['act']);
486 if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
487 // Switch to new column or invert search order?
488 $column= $match[1];
489 if ($this->sortColumn != $column) {
490 $this->sortColumn= $column;
491 } else {
492 $this->sortDirection[$column]= !$this->sortDirection[$column];
493 }
495 // Allow header to update itself according to the new sort settings
496 $this->renderHeader();
497 }
498 }
500 // Override base if we got signals from the navigation elements
501 $action= "";
502 foreach ($_POST as $key => $value) {
503 if (preg_match('/^(ROOT|BACK|HOME)_x$/', $key, $match)) {
504 $action= $match[1];
505 break;
506 }
507 }
509 // Navigation handling
510 if ($action == 'ROOT') {
511 $deps= $ui->get_module_departments($this->module);
512 $this->base= $deps[0];
513 }
514 if ($action == 'BACK') {
515 $deps= $ui->get_module_departments($this->module);
516 $base= preg_replace("/^[^,]+,/", "", $this->base);
517 if(in_array_ics($base, $deps)){
518 $this->base= $base;
519 }
520 }
521 if ($action == 'HOME') {
522 $ui= get_userinfo();
523 $this->base= $this->filter->getObjectBase($ui->dn);
524 }
526 // Reload departments
527 if ($this->departmentBrowser){
528 $this->departments= $this->getDepartments();
529 }
531 // Update filter and refresh entries
532 $this->filter->setBase($this->base);
533 $this->entries= $this->filter->query();
534 }
537 function setBase($base)
538 {
539 $this->base= $base;
540 }
543 function getBase()
544 {
545 return $this->base;
546 }
549 function parseLayout($layout)
550 {
551 $result= array();
552 $layout= preg_replace("/^\|/", "", $layout);
553 $layout= preg_replace("/\|$/", "", $layout);
554 $cols= split("\|", $layout);
555 foreach ($cols as $index => $config) {
556 if ($config != "") {
557 $components= split(';', $config);
558 $config= "";
559 foreach ($components as $part) {
560 if (preg_match("/^r$/", $part)) {
561 $config.= "text-align:right;";
562 continue;
563 }
564 if (preg_match("/^l$/", $part)) {
565 $config.= "text-align:left;";
566 continue;
567 }
568 if (preg_match("/^c$/", $part)) {
569 $config.= "text-align:center;";
570 continue;
571 }
572 if (preg_match("/^[0-9]+(|px|%)$/", $part)) {
573 $config.= "width:$part;";
574 continue;
575 }
576 }
578 $result[$index]= " style='$config' ";
579 } else {
580 $result[$index]= null;
581 }
582 }
584 // Save number of columns for later use
585 $this->numColumns= count($cols);
587 return $result;
588 }
591 function renderCell($data, $config, $row)
592 {
593 // Replace flat attributes in data string
594 for ($i= 0; $i<$config['count']; $i++) {
595 $attr= $config[$i];
596 $value= "";
597 if (is_array($config[$attr])) {
598 $value= $config[$attr][0];
599 } else {
600 $value= $config[$attr];
601 }
602 $data= preg_replace("/%\{$attr\}/", $value, $data);
603 }
605 // Watch out for filters and prepare to execute them
606 $data= $this->processElementFilter($data, $config, $row);
608 // Replace all non replaced %{...} instances because they
609 // are non resolved attributes or filters
610 $data= preg_replace('/%{[^}]+}/', ' ', $data);
612 return $data;
613 }
616 function renderBase()
617 {
618 if (!$this->baseMode) {
619 return;
620 }
622 $result= "<select name='BASE' onChange='mainform.submit()' size='1'>";
623 $firstDN= null;
624 $found= false;
626 foreach ($this->bases as $key=>$value) {
627 // Keep first entry to fall back eventually
628 if(!$firstDN) {
629 $firstDN= $key;
630 }
632 // Prepare to render entry
633 $selected= "";
634 if ($key == $this->base) {
635 $selected= " selected";
636 $found= true;
637 }
638 $result.= "<option value='".$key."'$selected>".$value."</option>";
639 }
640 $result.= "</select>";
642 // Reset the currently used base to the first DN we found if there
643 // was no match.
644 if(!$found){
645 $this->base = $firstDN;
646 }
648 return $result;
649 }
652 function processElementFilter($data, $config, $row)
653 {
654 preg_match_all("/%\{filter:([^(]+)\((.*)\)\}/", $data, $matches, PREG_SET_ORDER);
656 foreach ($matches as $match) {
657 if (!isset($this->filters[$match[1]])) {
658 continue;
659 }
660 $cl= preg_replace('/::.*$/', '', $this->filters[$match[1]]);
661 $method= preg_replace('/^.*::/', '', $this->filters[$match[1]]);
663 // Prepare params for function call
664 $params= array();
665 preg_match_all('/"[^"]+"|[^,]+/', $match[2], $parts);
666 foreach ($parts[0] as $param) {
668 // Row is replaced by the row number
669 if ($param == "row") {
670 $params[]= $row;
671 }
673 // pid is replaced by the current PID
674 if ($param == "pid") {
675 $params[]= $this->pid;
676 }
678 // Fixie with "" is passed directly
679 if (preg_match('/^".*"$/', $param)){
680 $params[]= preg_replace('/"/', '', $param);
681 }
683 // LDAP variables get replaced by their objects
684 for ($i= 0; $i<$config['count']; $i++) {
685 if ($param == $config[$i]) {
686 $values= $config[$config[$i]];
687 if (is_array($values)){
688 unset($values['count']);
689 }
690 $params[]= $values;
691 }
692 }
694 // Move dn if needed
695 if ($param == "dn") {
696 $params[]= LDAP::fix($config["dn"]);
697 }
698 }
700 // Replace information
701 if ($cl == "listing") {
702 // Non static call - seems to result in errors
703 $data= @preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($this, "$method"), $params), $data);
704 } else {
705 // Static call
706 $data= preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($cl, $method), $params), $data);
707 }
708 }
710 return $data;
711 }
714 function getObjectType($types, $classes)
715 {
716 // Walk thru types and see if there's something matching
717 foreach ($types as $objectType) {
718 $ocs= $objectType['objectClass'];
719 if (!is_array($ocs)){
720 $ocs= array($ocs);
721 }
723 $found= true;
724 foreach ($ocs as $oc){
725 if (preg_match('/^!(.*)$/', $oc, $match)) {
726 $oc= $match[1];
727 if (in_array($oc, $classes)) {
728 $found= false;
729 }
730 } else {
731 if (!in_array($oc, $classes)) {
732 $found= false;
733 }
734 }
735 }
737 if ($found) {
738 return $objectType;
739 }
740 }
742 return null;
743 }
746 function filterObjectType($dn, $classes)
747 {
748 // Walk thru classes and return on first match
749 $result= " ";
751 $objectType= $this->getObjectType($this->objectTypes, $classes);
752 if ($objectType) {
753 $result= "<img class='center' title='".LDAP::fix($dn)."' src='".$objectType["image"]."'>";
754 if (!isset($this->objectTypeCount[$objectType['label']])) {
755 $this->objectTypeCount[$objectType['label']]= 0;
756 }
757 $this->objectTypeCount[$objectType['label']]++;
758 }
759 return $result;
760 }
763 function filterActions($dn, $row, $classes)
764 {
765 // Do nothing if there's no menu defined
766 if (!isset($this->xmlData['actiontriggers']['action'])) {
767 return " ";
768 }
770 // Go thru all actions
771 $result= "";
772 $actions= $this->xmlData['actiontriggers']['action'];
773 foreach($actions as $action) {
774 // Skip the entry completely if there's no permission to execute it
775 if (!$this->hasActionPermission($action, $dn)) {
776 $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
777 continue;
778 }
780 // Skip entry if the pseudo filter does not fit
781 if (isset($action['filter']) && preg_match('/^[a-z0-9_]+=[a-z0-9_]+$/i', $action['filter'])) {
782 list($fa, $fv)= split('=', $action['filter']);
783 if (!isset($this->entries[$row][$fa]) && !$this->entries[$row][$fa][0] == $fv) {
784 $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
785 continue;
786 }
787 }
790 // If there's an objectclass definition and we don't have it
791 // add an empty picture here.
792 if (isset($action['objectclass'])){
793 $objectclass= $action['objectclass'];
794 if (preg_match('/^!(.*)$/', $objectclass, $m)){
795 $objectclass= $m[1];
796 if(in_array($objectclass, $classes)) {
797 $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
798 continue;
799 }
800 } else {
801 if(!in_array($objectclass, $classes)) {
802 $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
803 continue;
804 }
805 }
806 }
808 // Render normal entries as usual
809 if ($action['type'] == "entry") {
810 $label= $this->processElementFilter($action['label'], $this->entries[$row], $row);
811 $image= $this->processElementFilter($action['image'], $this->entries[$row], $row);
812 $result.="<input class='center' type='image' src='$image' title='$label' ".
813 "name='listing_".$action['name']."_$row' style='padding:1px'>";
814 }
816 // Handle special types
817 if ($action['type'] == "copypaste" || $action['type'] == "snapshot") {
819 $objectType= $this->getObjectType($this->objectTypes, $this->entries[$row]['objectClass']);
820 $category= $class= null;
821 if ($objectType) {
822 $category= $objectType['category'];
823 $class= $objectType['class'];
824 }
826 if ($action['type'] == "copypaste") {
827 $result.= $this->renderCopyPasteActions($row, $this->entries[$row]['dn'], $category, $class);
828 } else {
829 $result.= $this->renderSnapshotActions($row, $this->entries[$row]['dn'], $category, $class);
830 }
831 }
832 }
834 return $result;
835 }
838 function filterDepartmentLink($row, $dn, $description)
839 {
840 $attr= $this->departments[$row]['sort-attribute'];
841 $name= $this->departments[$row][$attr];
842 if (is_array($name)){
843 $name= $name[0];
844 }
845 $result= sprintf("%s [%s]", $name, $description[0]);
846 return("<a href='?plug=".$_GET['plug']."&PID=$this->pid&act=department_$row' title='$dn'>$result</a>");
847 }
850 function filterLink()
851 {
852 $result= " ";
854 $row= func_get_arg(0);
855 $pid= $this->pid;
856 $dn= LDAP::fix(func_get_arg(1));
857 $params= array(func_get_arg(2));
859 // Collect sprintf params
860 for ($i = 3;$i < func_num_args();$i++) {
861 $val= func_get_arg($i);
862 if (is_array($val)){
863 $params[]= $val[0];
864 continue;
865 }
866 $params[]= $val;
867 }
869 $result= " ";
870 $trans= call_user_func_array("sprintf", $params);
871 if ($trans != "") {
872 return("<a href='?plug=".$_GET['plug']."&PID=$pid&act=listing_edit_$row' title='$dn'>$trans</a>");
873 }
875 return $result;
876 }
879 function renderNavigation()
880 {
881 $result= array();
882 $enableBack = true;
883 $enableRoot = true;
884 $enableHome = true;
886 $ui = get_userinfo();
888 /* Check if base = first available base */
889 $deps = $ui->get_module_departments($this->module);
891 if(!count($deps) || $deps[0] == $this->filter->base){
892 $enableBack = false;
893 $enableRoot = false;
894 }
896 $listhead ="";
898 /* Check if we are in users home department */
899 if(!count($deps) || $this->filter->base == $this->filter->getObjectBase($ui->dn)){
900 $enableHome = false;
901 }
903 /* Draw root button */
904 if($enableRoot){
905 $result["ROOT"]= "<input class='center' type='image' src='images/lists/root.png' align='middle' ".
906 "title='"._("Go to root department")."' name='ROOT' alt='"._("Root")."'>";
907 }else{
908 $result["ROOT"]= "<img src='images/lists/root_grey.png' class='center' alt='"._("Root")."'>";
909 }
911 /* Draw back button */
912 if($enableBack){
913 $result["BACK"]= "<input class='center' type='image' align='middle' src='images/lists/back.png' ".
914 "title='"._("Go up one department")."' alt='"._("Up")."' name='BACK'>";
915 }else{
916 $result["BACK"]= "<img src='images/lists/back_grey.png' class='center' alt='"._("Up")."'>";
917 }
919 /* Draw home button */
920 if($enableHome){
921 $result["HOME"]= "<input class='center' type='image' align='middle' src='images/lists/home.png' ".
922 "title='"._("Go to users department")."' alt='"._("Home")."' name='HOME'>";
923 }else{
924 $result["HOME"]= "<img src='images/lists/home_grey.png' class='center' alt='"._("Home")."'>";
925 }
927 /* Draw reload button, this button is enabled everytime */
928 $result["RELOAD"]= "<input class='center' type='image' src='images/lists/reload.png' align='middle' ".
929 "title='"._("Reload list")."' name='REFRESH' alt='"._("Submit")."'>";
931 return ($result);
932 }
935 function getAction()
936 {
937 // Do not do anything if this is not our PID, or there's even no PID available...
938 if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->pid) {
939 return;
940 }
942 $result= array("targets" => array(), "action" => "");
944 // Filter GET with "act" attributes
945 if (isset($_GET['act'])) {
946 $key= validate($_GET['act']);
947 $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)$/', '$1', $key);
948 if (isset($this->entries[$target]['dn'])) {
949 $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+$/', '$1', $key);
950 $result['targets'][]= $this->entries[$target]['dn'];
951 }
953 // Drop targets if empty
954 if (count($result['targets']) == 0) {
955 unset($result['targets']);
956 }
957 return $result;
958 }
960 // Filter POST with "listing_" attributes
961 foreach ($_POST as $key => $prop) {
963 // Capture selections
964 if (preg_match('/^listing_selected_[0-9]+$/', $key)) {
965 $target= preg_replace('/^listing_selected_([0-9]+)$/', '$1', $key);
966 if (isset($this->entries[$target]['dn'])) {
967 $result['targets'][]= $this->entries[$target]['dn'];
968 }
969 continue;
970 }
972 // Capture action with target - this is a one shot
973 if (preg_match('/^listing_[a-zA-Z_]+_[0-9]+(|_x)$/', $key)) {
974 $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)(|_x)$/', '$1', $key);
975 if (isset($this->entries[$target]['dn'])) {
976 $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+(|_x)$/', '$1', $key);
977 $result['targets']= array($this->entries[$target]['dn']);
978 }
979 break;
980 }
982 // Capture action without target
983 if (preg_match('/^listing_[a-zA-Z_]+(|_x)$/', $key)) {
984 $result['action']= preg_replace('/^listing_([a-zA-Z_]+)(|_x)$/', '$1', $key);
985 continue;
986 }
987 }
989 // Filter POST with "act" attributes -> posted from action menu
990 if (isset($_POST['act']) && $_POST['act'] != '') {
991 if (!preg_match('/^export.*$/', $_POST['act'])){
992 $result['action']= validate($_POST['act']);
993 }
994 }
996 // Drop targets if empty
997 if (count($result['targets']) == 0) {
998 unset($result['targets']);
999 }
1000 return $result;
1001 }
1004 function renderActionMenu()
1005 {
1006 // Don't send anything if the menu is not defined
1007 if (!isset($this->xmlData['actionmenu']['action'])){
1008 return "";
1009 }
1011 // Array?
1012 if (isset($this->xmlData['actionmenu']['action']['type'])){
1013 $this->xmlData['actionmenu']['action']= array($this->xmlData['actionmenu']['action']);
1014 }
1016 // Load shortcut
1017 $actions= &$this->xmlData['actionmenu']['action'];
1018 $result= "<input type='hidden' name='act' id='actionmenu' value=''><div style='display:none'><input type='submit' name='exec_act' id='exec_act' value=''></div>".
1019 "<ul class='level1' id='root'><li><a href='#'>Aktionen <img ".
1020 "border=0 class='center' src='images/lists/sort-down.png'></a>";
1022 // Build ul/li list
1023 $result.= $this->recurseActions($actions);
1025 return "<div id='pulldown'>".$result."</li></ul><div>";
1026 }
1029 function recurseActions($actions)
1030 {
1031 static $level= 2;
1032 $result= "<ul class='level$level'>";
1033 $separator= "";
1035 foreach ($actions as $action) {
1037 // Skip the entry completely if there's no permission to execute it
1038 if (!$this->hasActionPermission($action, $this->filter->base)) {
1039 continue;
1040 }
1042 // Fill image if set
1043 $img= "";
1044 if (isset($action['image'])){
1045 $img= "<img border='0' class='center' src='".$action['image']."'> ";
1046 }
1048 if ($action['type'] == "separator"){
1049 $separator= " style='border-top:1px solid #AAA' ";
1050 continue;
1051 }
1053 // Dive into subs
1054 if ($action['type'] == "sub" && isset($action['action'])) {
1055 $level++;
1056 if (isset($action['label'])){
1057 $result.= "<li$separator><a href='#'>$img"._($action['label'])." <img border='0' src='images/forward-arrow.png'></a>";
1058 }
1059 $result.= $this->recurseActions($action['action'])."</li>";
1060 $level--;
1061 $separator= "";
1062 continue;
1063 }
1065 // Render entry elseways
1066 if (isset($action['label'])){
1067 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"".$action['name']."\";mainform.submit();'>$img"._($action['label'])."</a></li>";
1068 }
1070 // Check for special types
1071 switch ($action['type']) {
1072 case 'copypaste':
1073 $result.= $this->renderCopyPasteMenu($separator);
1074 break;
1076 case 'snapshot':
1077 $result.= $this->renderSnapshotMenu($separator);
1078 break;
1080 case 'exporter':
1081 $result.= $this->renderExporterMenu($separator);
1082 break;
1084 case 'daemon':
1085 $result.= $this->renderDaemonMenu($separator);
1086 break;
1087 }
1089 $separator= "";
1090 }
1092 $result.= "</ul>";
1093 return $result;
1094 }
1097 function hasActionPermission($action, $dn)
1098 {
1099 $ui= get_userinfo();
1101 if (isset($action['acl'])) {
1102 $acls= $action['acl'];
1103 if (!is_array($acls)) {
1104 $acls= array($acls);
1105 }
1107 // Every ACL has to pass
1108 foreach ($acls as $acl) {
1109 $module= $this->module;
1110 $aclList= array();
1112 // Split for category and plugins if needed
1113 // match for "[rw]" style entries
1114 if (preg_match('/^\[([rwcdm]+)\]$/', $acl, $match)){
1115 $aclList= array($match[1]);
1116 }
1118 // match for "users[rw]" style entries
1119 if (preg_match('/^([a-zA-Z0-9]+)\[([rwcdm]+)\]$/', $acl, $match)){
1120 $module= $match[1];
1121 $aclList= array($match[2]);
1122 }
1124 // match for "users/user[rw]" style entries
1125 if (preg_match('/^([a-zA-Z0-9]+\/[a-zA-Z0-9]+)\[([rwcdm]+)\]$/', $acl, $match)){
1126 $module= $match[1];
1127 $aclList= array($match[2]);
1128 }
1130 // match "users/user[userPassword:rw(,...)*]" style entries
1131 if (preg_match('/^([a-zA-Z0-9]+\/[a-zA-Z0-9]+)\[([a-zA-Z0-9]+:[rwcdm]+(,[a-zA-Z0-9]+:[rwcdm]+)*)\]$/', $acl, $match)){
1132 $module= $match[1];
1133 $aclList= split(',', $match[2]);
1134 }
1136 // Walk thru prepared ACL by using $module
1137 foreach($aclList as $sAcl) {
1138 $checkAcl= "";
1140 // Category or detailed permission?
1141 if (strpos('/', $module) === false) {
1142 if (preg_match('/([a-zA-Z0-9]+):([rwcdm]+)/', $sAcl, $m) ) {
1143 $checkAcl= $ui->get_permissions($dn, $module, $m[1]);
1144 $sAcl= $m[2];
1145 } else {
1146 $checkAcl= $ui->get_permissions($dn, $module, '0');
1147 }
1148 } else {
1149 $checkAcl= $ui->get_category_permissions($dn, $module);
1150 }
1152 // Split up remaining part of the acl and check if it we're
1153 // allowed to do something...
1154 $parts= str_split($sAcl);
1155 foreach ($parts as $part) {
1156 if (strpos($checkAcl, $part) === false){
1157 return false;
1158 }
1159 }
1161 }
1162 }
1163 }
1165 return true;
1166 }
1169 function refreshBasesList()
1170 {
1171 global $config;
1172 $ui= get_userinfo();
1174 // Do some array munching to get it user friendly
1175 $ids= $config->idepartments;
1176 $d= $ui->get_module_departments($this->module);
1177 $k_ids= array_keys($ids);
1178 $deps= array_intersect($d,$k_ids);
1180 // Fill internal bases list
1181 $this->bases= array();
1182 foreach($k_ids as $department){
1183 $this->bases[$department] = $ids[$department];
1184 }
1185 }
1188 function getDepartments()
1189 {
1190 $departments= array();
1191 $ui= get_userinfo();
1193 // Get list of supported department types
1194 $types = departmentManagement::get_support_departments();
1196 // Load departments allowed by ACL
1197 $validDepartments = $ui->get_module_departments($this->module);
1199 // Build filter and look in the LDAP for possible sub departments
1200 // of current base
1201 $filter= "(&(objectClass=gosaDepartment)(|";
1202 $attrs= array("description", "objectClass");
1203 foreach($types as $name => $data){
1204 $filter.= "(objectClass=".$data['OC'].")";
1205 $attrs[]= $data['ATTR'];
1206 }
1207 $filter.= "))";
1208 $res= get_list($filter, $this->module, $this->base, $attrs, GL_NONE);
1210 // Analyze list of departments
1211 foreach ($res as $department) {
1212 if (!in_array($department['dn'], $validDepartments)) {
1213 continue;
1214 }
1216 // Add the attribute where we use for sorting
1217 $oc= null;
1218 foreach(array_keys($types) as $type) {
1219 if (in_array($type, $department['objectClass'])) {
1220 $oc= $type;
1221 break;
1222 }
1223 }
1224 $department['sort-attribute']= $types[$oc]['ATTR'];
1226 // Move to the result list
1227 $departments[]= $department;
1228 }
1230 return $departments;
1231 }
1234 function renderCopyPasteMenu($separator, $copy= true, $cut= true)
1235 {
1236 // We can only provide information if we've got a copypaste handler
1237 // instance
1238 if(!(isset($this->copyPasteHandler) && is_object($this->copyPasteHandler))){
1239 return "";
1240 }
1242 // Presets
1243 $result= "";
1244 $read= $paste= false;
1245 $ui= get_userinfo();
1247 // Switch flags to on if there's at least one category which allows read/paste
1248 foreach($this->categories as $category){
1249 $read= $read || preg_match('/r/', $ui->get_category_permissions($this->base, $category));
1250 $paste= $paste || $ui->is_pasteable($this->base, $category) == 1;
1251 }
1254 // Draw entries that allow copy and cut
1255 if($read){
1257 // Copy entry
1258 if($copy){
1259 $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>";
1260 $separator= "";
1261 }
1263 // Cut entry
1264 if($cut){
1265 $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>";
1266 $separator= "";
1267 }
1268 }
1270 // Draw entries that allow pasting entries
1271 if($paste){
1272 if($this->copyPasteHandler->entries_queued()){
1273 $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>";
1274 }else{
1275 $result.= "<li$separator><a href='#'><img src='images/lists/paste-grey.png' alt='' border='0' class='center'> "._("Paste")."</a></li>";
1276 }
1277 }
1279 return($result);
1280 }
1283 function renderCopyPasteActions($row, $dn, $category, $class, $copy= true, $cut= true)
1284 {
1285 // We can only provide information if we've got a copypaste handler
1286 // instance
1287 if(!(isset($this->copyPasteHandler) && is_object($this->copyPasteHandler))){
1288 return "";
1289 }
1291 // Presets
1292 $ui = get_userinfo();
1293 $result = "";
1295 // Render cut entries
1296 if($cut){
1297 if($ui->is_cutable($dn, $category, $class)){
1298 $result .= "<input class='center' type='image'
1299 src='images/lists/cut.png' alt='"._("Cut")."' name='listing_cut_$row' title='"._("Cut this entry")."' style='padding:1px'>";
1300 }else{
1301 $result.="<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1302 }
1303 }
1305 // Render copy entries
1306 if($copy){
1307 if($ui->is_copyable($dn, $category, $class)){
1308 $result.= "<input class='center' type='image'
1309 src='images/lists/copy.png' alt='"._("Copy")."' name='listing_copy_$row' title='"._("Copy this entry")."' style='padding:1px'>";
1310 }else{
1311 $result.="<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1312 }
1313 }
1315 return($result);
1316 }
1319 function renderSnapshotMenu($separator)
1320 {
1321 // We can only provide information if we've got a snapshot handler
1322 // instance
1323 if(!(isset($this->snapshotHandler) && is_object($this->snapshotHandler))){
1324 return "";
1325 }
1327 // Presets
1328 $result = "";
1329 $ui = get_userinfo();
1331 if($this->snapshotHandler->enabled() && $ui->allow_snapshot_restore($this->base, $this->module)){
1333 // Check if there is something to restore
1334 $restore= false;
1335 foreach($this->snapshotHandler->getSnapshotBases() as $base){
1336 $restore= $restore || count($this->snapshotHandler->getDeletedSnapshots($base)) > 0;
1337 }
1339 // Draw icons according to the restore flag
1340 if($restore){
1341 $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>";
1342 }else{
1343 $result.= "<li$separator><a href='#'><img src='images/lists/restore_grey.png' alt='' border='0' class='center'> "._("Restore snapshots")."</a></li>";
1344 }
1345 }
1347 return($result);
1348 }
1351 function renderExporterMenu($separator)
1352 {
1353 // Presets
1354 $result = "";
1356 // Draw entries
1357 $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'>";
1359 // Export CVS as build in exporter
1360 foreach ($this->exporter as $action => $exporter) {
1361 $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>";
1362 }
1364 // Finalize list
1365 $result.= "</ul></li>";
1367 return($result);
1368 }
1371 function renderSnapshotActions($row, $dn, $category, $class, $copy= true, $cut= true)
1372 {
1373 // We can only provide information if we've got a snapshot handler
1374 // instance
1375 if(!(isset($this->snapshotHandler) && is_object($this->snapshotHandler))){
1376 return "";
1377 }
1379 // Presets
1380 $result= "";
1381 $ui = get_userinfo();
1383 // Only act if enabled here
1384 if($this->snapshotHandler->enabled()){
1386 // Draw restore button
1387 if ($ui->allow_snapshot_restore($dn, $category)){
1389 // Do we have snapshots for this dn?
1390 if($this->snapshotHandler->hasSnapshots($dn)){
1391 $result.= "<input class='center' type='image' src='images/lists/restore.png' ".
1392 "alt='"._("Restore snapshot")."' name='listing_restore_$row' title='".
1393 _("Restore snapshot")."' style='padding:1px'>";
1394 } else {
1395 $result.= "<img src='images/lists/restore_grey.png' alt=' ' class='center' style='padding:1px'>";
1396 }
1397 }
1399 // Draw snapshot button
1400 if($ui->allow_snapshot_create($dn, $category)){
1401 $result.= "<input class='center' type='image' src='images/snapshot.png' ".
1402 "alt='"._("Create snapshot")."' name='listing_snapshot_$row' title='".
1403 _("Create a new snapshot from this object")."' style='padding:1px'>";
1404 }else{
1405 $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1406 }
1407 }
1409 return($result);
1410 }
1413 function renderDaemonMenu($separator)
1414 {
1415 $result= "";
1417 // If there is a daemon registered, draw the menu entries
1418 if(class_available("DaemonEvent")){
1419 $events= DaemonEvent::get_event_types_by_category($this->categories);
1420 if(isset($events['BY_CLASS']) && count($events['BY_CLASS'])){
1421 foreach($events['BY_CLASS'] as $name => $event){
1422 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value=\"$name\";document.getElementById(\"exec_act\").click();'>".$event['MenuImage']." ".$event['s_Menu_Name']."</a></li>";
1423 $separator= "";
1424 }
1425 }
1426 }
1428 return $result;
1429 }
1431 }
1433 ?>