7d8138348388885a26eeb771b6259af2ca87dbc7
1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class listing {
25 var $xmlData;
26 var $entries;
27 var $departments= array();
28 var $departmentBrowser= false;
29 var $departmentRootVisible= false;
30 var $multiSelect= false;
31 var $template;
32 var $headline;
33 var $module;
34 var $base;
35 var $sortDirection= null;
36 var $sortColumn= null;
37 var $sortAttribute;
38 var $sortType;
39 var $numColumns;
40 var $baseMode= false;
41 var $bases= array();
42 var $header= array();
43 var $colprops= array();
44 var $filters= array();
45 var $pid;
46 var $objectTypes= array();
47 var $objectTypeCount= array();
48 var $copyPasteHandler= null;
49 var $snapshotHandler= null;
52 function listing($filename)
53 {
54 global $config;
56 // Initialize pid
57 $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
59 if (!$this->load($filename)) {
60 die("Cannot parse $filename!");
61 }
63 // Set base for filter
64 $this->base= session::global_get("CurrentMainBase");
65 if ($this->base == null) {
66 $this->base= $config->current['BASE'];
67 }
68 $this->refreshBasesList();
70 // Move footer information
71 $this->showFooter= ($config->get_cfg_value("listSummary") == "true");
73 // Register build in filters
74 $this->registerElementFilter("objectType", "listing::filterObjectType");
75 $this->registerElementFilter("departmentLink", "listing::filterDepartmentLink");
76 $this->registerElementFilter("link", "listing::filterLink");
77 $this->registerElementFilter("actions", "listing::filterActions");
78 }
81 function setCopyPasteHandler($handler)
82 {
83 $this->copyPasteHandler= &$handler;
84 }
87 function setSnapshotHandler($handler)
88 {
89 $this->snapshotHandler= &$handler;
90 }
93 function setFilter($filter)
94 {
95 $this->filter= &$filter;
96 if ($this->departmentBrowser){
97 $this->departments= $this->getDepartments();
98 }
99 $this->filter->setBase($this->base);
100 $this->entries= $this->filter->query();
101 }
104 function registerElementFilter($name, $call)
105 {
106 if (!isset($this->filters[$name])) {
107 $this->filters[$name]= $call;
108 return true;
109 }
111 return false;
112 }
115 function load($filename)
116 {
117 $contents = file_get_contents($filename);
118 $this->xmlData= xml::xml2array($contents, 1);
120 if (!isset($this->xmlData['list'])) {
121 return false;
122 }
124 $this->xmlData= $this->xmlData["list"];
126 // Load some definition values
127 foreach (array("departmentBrowser", "departmentRootVisible", "multiSelect", "baseMode") as $token) {
128 if (isset($this->xmlData['definition'][$token]) &&
129 $this->xmlData['definition'][$token] == "true"){
130 $this->$token= true;
131 }
132 }
134 // Fill objectTypes from departments and xml definition
135 $types = departmentManagement::get_support_departments();
136 foreach ($types as $class => $data) {
137 $this->objectTypes[]= array("label" => $data['TITLE'],
138 "objectClass" => $data['OC'],
139 "image" => $data['IMG']);
140 }
141 $this->categories= array();
142 if (isset($this->xmlData['definition']['objectType'])) {
143 foreach ($this->xmlData['definition']['objectType'] as $index => $otype) {
144 $this->objectTypes[]= $this->xmlData['definition']['objectType'][$index];
145 if (isset($this->xmlData['definition']['objectType'][$index]['category'])){
146 $this->categories[]= $this->xmlData['definition']['objectType'][$index]['category'];
147 }
148 }
149 }
151 // Parse layout per column
152 $this->colprops= $this->parseLayout($this->xmlData['table']['layout']);
154 // Prepare table headers
155 $this->renderHeader();
157 // Assign headline/module
158 $this->headline= _($this->xmlData['definition']['label']);
159 $this->module= $this->xmlData['definition']['module'];
160 if (!is_array($this->categories)){
161 $this->categories= array($this->categories);
162 }
164 return true;
165 }
168 function renderHeader()
169 {
170 $this->header= array();
172 // Initialize sort?
173 $sortInit= false;
174 if (!$this->sortDirection) {
175 $this->sortColumn= 0;
176 if (isset($this->xmlData['definition']['defaultSortColumn'])){
177 $this->sortColumn= $this->xmlData['definition']['defaultSortColumn'];
178 } else {
179 $this->sortAttribute= "";
180 }
181 $this->sortDirection= array();
182 $sortInit= true;
183 }
185 if (isset($this->xmlData['table']['column'])){
186 foreach ($this->xmlData['table']['column'] as $index => $config) {
187 // Initialize everything to one direction
188 if ($sortInit) {
189 $this->sortDirection[$index]= false;
190 }
192 $sorter= "";
193 if ($index == $this->sortColumn && isset($config['sortAttribute']) &&
194 isset($config['sortType'])) {
195 $this->sortAttribute= $config['sortAttribute'];
196 $this->sortType= $config['sortType'];
197 $sorter= " <img border='0' title='".($this->sortDirection[$index]?_("Up"):_("Down"))."' src='images/lists/sort-".($this->sortDirection[$index]?"up":"down").".png' align='top'>";
198 }
199 $sortable= (isset($config['sortAttribute']));
201 $link= "href='?plug=".$_GET['plug']."&PID=".$this->pid."&act=SORT_$index'";
202 if (isset($config['label'])) {
203 if ($sortable) {
204 $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."><a $link>"._($config['label'])."$sorter</a></td>";
205 } else {
206 $this->header[$index]= "<td class='listheader' ".$this->colprops[$index].">"._($config['label'])."</td>";
207 }
208 } else {
209 if ($sortable) {
210 $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."><a $link> $sorter</a></td>";
211 } else {
212 $this->header[$index]= "<td class='listheader' ".$this->colprops[$index]."> </td>";
213 }
214 }
215 }
216 }
217 }
219 function render()
220 {
221 // Check for exeeded sizelimit
222 if (($message= check_sizelimit()) != ""){
223 return($message);
224 }
226 // Initialize list
227 $result= "<input type='hidden' value='$this->pid' name='PID'>";
228 $result.= "<div class='contentboxb' id='listing_container' style='border-top:1px solid #B0B0B0;'>";
229 $result.= "<table summary='$this->headline' style='width:600px;height:450px;' cellspacing='0' id='t_scrolltable'>
230 <tr><td class='scrollhead'><table summary='' style='width:100%;' cellspacing='0' id='t_scrollhead'>";
231 $this->numColumns= count($this->colprops) + ($this->multiSelect?1:0);
233 // Build list header
234 $result.= "<tr>";
235 if ($this->multiSelect) {
236 $result.= "<td class='listheader' style='width:20px;'><input type='checkbox' id='select_all' name='select_all' title='"._("Select all")."' onClick='toggle_all_(\"listing_selected_[0-9]*$\",\"select_all\");' ></td>";
237 }
238 foreach ($this->header as $header) {
239 $result.= $header;
240 }
242 // Add 13px for scroller
243 $result.= "<td class='listheader' style='width:13px;border-right:0px;'> </td></table></td></tr>";
245 // New table for the real list contents
246 $result.= "<tr><td colspan='$this->numColumns' class='scrollbody'><div style='width:600px;height:430px;' id='d_scrollbody' class='scrollbody'><table summary='' style='height:100%;width:581px;' cellspacing='0' id='t_scrollbody'>";
248 // No results? Just take an empty colspanned row
249 if (count($this->entries) + count($this->departments) == 0) {
250 $result.= "<tr class='rowxp0'><td class='list1nohighlight' colspan='$this->numColumns' style='height:100%;border-right:0px;width:100%;'> </td></tr>";
251 }
253 // Line color alternation
254 $alt= 0;
255 $deps= 0;
257 // Draw department browser if configured and we're not in sub mode
258 if ($this->departmentBrowser && $this->filter->scope != "sub") {
259 // Fill with department browser if configured this way
260 $departmentIterator= new departmentSortIterator($this->departments, $this->sortDirection[$this->sortColumn]);
261 foreach ($departmentIterator as $row => $entry){
262 $result.="<tr class='rowxp".($alt&1)."'>";
264 // Render multi select if needed
265 if ($this->multiSelect) {
266 $result.="<td style='text-align:center;width:20px;' class='list1'> </td>";
267 }
269 // Render defined department columns, fill the rest with some stuff
270 $rest= $this->numColumns - 1;
271 foreach ($this->xmlData['table']['department'] as $index => $config) {
272 $colspan= 1;
273 if (isset($config['span'])){
274 $colspan= $config['span'];
275 }
276 $result.="<td colspan='$colspan' ".$this->colprops[$index]." class='list1'>".$this->renderCell($config['value'], $entry, $row)."</td>";
277 $rest-= $colspan;
278 }
280 // Fill remaining cols with nothing
281 $last= $this->numColumns - $rest;
282 for ($i= 0; $i<$rest; $i++){
283 $result.= "<td ".$this->colprops[$last+$i-1]." class='list1'> </td>";
284 }
285 $result.="</tr>";
287 $alt++;
288 }
289 $deps= $alt;
290 }
292 // Fill with contents, sort as configured
293 $entryIterator= new listingSortIterator($this->entries, $this->sortDirection[$this->sortColumn], $this->sortAttribute, $this->sortType);
294 foreach ($entryIterator as $row => $entry){
295 $result.="<tr class='rowxp".($alt&1)."'>";
297 // Render multi select if needed
298 if ($this->multiSelect) {
299 $result.="<td style='text-align:center;width:20px;' class='list0'><input type='checkbox' id='listing_selected_$row' name='listing_selected_$row'></td>";
300 }
302 foreach ($this->xmlData['table']['column'] as $index => $config) {
303 $result.="<td ".$this->colprops[$index]." class='list0'>".$this->renderCell($config['value'], $entry, $row)."</td>";
304 }
305 $result.="</tr>";
307 $alt++;
308 }
310 // Need to fill the list if it's not full (nobody knows why this is 22 ;-))
311 $emptyListStyle= (count($this->entries) + count($deps) == 0)?"border:0;":"";
312 if (count($this->entries) + count($deps) < 22) {
313 $result.= "<tr>";
314 for ($i= 0; $i<$this->numColumns; $i++) {
315 if ($i == 0) {
316 $result.= "<td class='list1nohighlight' style='$emptyListStyle height:100%;'> </td>";
317 continue;
318 }
319 if ($i != $this->numColumns-1) {
320 $result.= "<td class='list1nohighlight' style='$emptyListStyle'> </td>";
321 } else {
322 $result.= "<td class='list1nohighlight' style='border-right:1px solid #AAA;$emptyListStyle'> </td>";
323 }
324 }
325 $result.= "</tr>";
326 }
328 $result.= "</table></div></td></tr>";
330 // Add the footer if requested
331 if ($this->showFooter) {
332 $result.= "<tr><td class='scrollhead'><table summary='' style='width:100%' cellspacing='0' id='t_scrollfoot'><tr><td class='listfooter' style='border-bottom:0px;'>";
334 foreach ($this->objectTypes as $objectType) {
335 if (isset($this->objectTypeCount[$objectType['label']])) {
336 $label= _($objectType['label']);
337 $result.= "<img class='center' src='".$objectType['image']."' title='$label' alt='$label'> ".$this->objectTypeCount[$objectType['label']]." ";
338 }
339 }
341 $result.= "<td class='listfooter' style='width:13px;border-right:0px;'> </td></table></td></tr>";
342 }
344 $result.= "</table></div>";
346 $smarty= get_smarty();
347 $smarty->assign("FILTER", $this->filter->render());
348 $smarty->assign("SIZELIMIT", print_sizelimit_warning());
349 $smarty->assign("LIST", $result);
351 // Assign navigation elements
352 $nav= $this->renderNavigation();
353 foreach ($nav as $key => $html) {
354 $smarty->assign($key, $html);
355 }
357 // Assign action menu / base
358 $smarty->assign("ACTIONS", $this->renderActionMenu());
359 $smarty->assign("BASE", $this->renderBase());
361 // Assign separator
362 $smarty->assign("SEPARATOR", "<img src='images/lists/seperator.png' alt='-' align='middle' height='16' width='1' class='center'>");
364 // Assign summary
365 $smarty->assign("HEADLINE", $this->headline);
367 return ($smarty->fetch(get_template_path($this->xmlData['definition']['template'], true)));
368 }
371 function update()
372 {
373 global $config;
374 $ui= get_userinfo();
376 // Do not do anything if this is not our PID
377 if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->pid) {
378 return;
379 }
381 // Save base
382 if (isset($_POST['BASE']) && $this->baseMode == true) {
383 $base= validate($_POST['BASE']);
384 if (isset($this->bases[$base])) {
385 $this->base= $base;
386 }
387 }
389 // Override the base if we got a message from the browser navigation
390 if ($this->departmentBrowser && isset($_GET['act'])) {
391 if (preg_match('/^department_([0-9]+)$/', validate($_GET['act']), $match)){
392 if (isset($this->departments[$match[1]])){
393 $this->base= $this->departments[$match[1]]['dn'];
394 }
395 }
396 }
398 // Filter GET with "act" attributes
399 if (isset($_GET['act'])) {
400 $key= validate($_GET['act']);
401 if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
402 // Switch to new column or invert search order?
403 $column= $match[1];
404 if ($this->sortColumn != $column) {
405 $this->sortColumn= $column;
406 } else {
407 $this->sortDirection[$column]= !$this->sortDirection[$column];
408 }
410 // Allow header to update itself according to the new sort settings
411 $this->renderHeader();
412 }
413 }
415 // Override base if we got signals from the navigation elements
416 $action= "";
417 foreach ($_POST as $key => $value) {
418 if (preg_match('/^(ROOT|BACK|HOME)_x$/', $key, $match)) {
419 $action= $match[1];
420 break;
421 }
422 }
424 // Navigation handling
425 if ($action == 'ROOT') {
426 $deps= $ui->get_module_departments($this->module);
427 $this->base= $deps[0];
428 }
429 if ($action == 'BACK') {
430 $deps= $ui->get_module_departments($this->module);
431 $base= preg_replace("/^[^,]+,/", "", $this->base);
432 if(in_array_ics($base, $deps)){
433 $this->base= $base;
434 }
435 }
436 if ($action == 'HOME') {
437 $ui= get_userinfo();
438 $this->base= get_base_from_people($ui->dn);
439 }
441 // Reload departments
442 if ($this->departmentBrowser){
443 $this->departments= $this->getDepartments();
444 }
446 // Update filter and refresh entries
447 $this->filter->setBase($this->base);
448 $this->entries= $this->filter->query();
449 }
452 function parseLayout($layout)
453 {
454 $result= array();
455 $layout= preg_replace("/^\|/", "", $layout);
456 $layout= preg_replace("/\|$/", "", $layout);
457 $cols= split("\|", $layout);
458 foreach ($cols as $index => $config) {
459 if ($config != "") {
460 $components= split(';', $config);
461 $config= "";
462 foreach ($components as $part) {
463 if (preg_match("/^r$/", $part)) {
464 $config.= "text-align:right;";
465 continue;
466 }
467 if (preg_match("/^l$/", $part)) {
468 $config.= "text-align:left;";
469 continue;
470 }
471 if (preg_match("/^c$/", $part)) {
472 $config.= "text-align:center;";
473 continue;
474 }
475 if (preg_match("/^[0-9]+(|px|%)$/", $part)) {
476 $config.= "width:$part;";
477 continue;
478 }
479 }
481 $result[$index]= " style='$config' ";
482 } else {
483 $result[$index]= null;
484 }
485 }
487 // Save number of columns for later use
488 $this->numColumns= count($cols);
490 return $result;
491 }
494 function renderCell($data, $config, $row)
495 {
496 // Replace flat attributes in data string
497 for ($i= 0; $i<$config['count']; $i++) {
498 $attr= $config[$i];
499 $value= "";
500 if (is_array($config[$attr])) {
501 $value= $config[$attr][0];
502 } else {
503 $value= $config[$attr];
504 }
505 $data= preg_replace("/%\{$attr\}/", $value, $data);
506 }
508 // Watch out for filters and prepare to execute them
509 $data= $this->processElementFilter($data, $config, $row);
511 return $data;
512 }
515 function renderBase()
516 {
517 $result= "<select name='BASE' onChange='mainform.submit()' size='1'>";
518 $firstDN= null;
519 $found= false;
521 foreach ($this->bases as $key=>$value) {
522 // Keep first entry to fall back eventually
523 if(!$firstDN) {
524 $firstDN= $key;
525 }
527 // Prepare to render entry
528 $selected= "";
529 if ($key == $this->base) {
530 $selected= " selected";
531 $found= true;
532 }
533 $result.= "<option value='".$key."'$selected>".$value."</option>";
534 }
535 $result.= "</select>";
537 // Reset the currently used base to the first DN we found if there
538 // was no match.
539 if(!$found){
540 $this->base = $firstDN;
541 }
543 return $result;
544 }
547 function processElementFilter($data, $config, $row)
548 {
549 preg_match_all("/%\{filter:([^(]+)\((.*)\)\}/", $data, $matches, PREG_SET_ORDER);
551 foreach ($matches as $match) {
552 if (!isset($this->filters[$match[1]])) {
553 continue;
554 }
555 $cl= preg_replace('/::.*$/', '', $this->filters[$match[1]]);
556 $method= preg_replace('/^.*::/', '', $this->filters[$match[1]]);
558 // Prepare params for function call
559 $params= array();
560 preg_match_all('/"[^"]+"|[^,]+/', $match[2], $parts);
561 foreach ($parts[0] as $param) {
563 // Row is replaced by the row number
564 if ($param == "row") {
565 $params[]= $row;
566 }
568 // pid is replaced by the current PID
569 if ($param == "pid") {
570 $params[]= $this->pid;
571 }
573 // Fixie with "" is passed directly
574 if (preg_match('/^".*"$/', $param)){
575 $params[]= preg_replace('/"/', '', $param);
576 }
578 // LDAP variables get replaced by their objects
579 for ($i= 0; $i<$config['count']; $i++) {
580 if ($param == $config[$i]) {
581 $values= $config[$config[$i]];
582 if (is_array($values)){
583 unset($values['count']);
584 }
585 $params[]= $values;
586 }
587 }
589 // Move dn if needed
590 if ($param == "dn") {
591 $params[]= LDAP::fix($config["dn"]);
592 }
593 }
595 // Replace information
596 if ($cl == "listing") {
597 // Non static call - seems to result in errors
598 $data= @preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($this, "$method"), $params), $data);
599 } else {
600 // Static call
601 $data= preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($cl, $method), $params), $data);
602 }
603 }
605 return $data;
606 }
609 function getObjectType($types, $classes)
610 {
611 // Walk thru types and see if there's something matching
612 foreach ($types as $objectType) {
613 $ocs= $objectType['objectClass'];
614 if (!is_array($ocs)){
615 $ocs= array($ocs);
616 }
618 $found= true;
619 foreach ($ocs as $oc){
620 if (preg_match('/^!(.*)$/', $oc, $match)) {
621 $oc= $match[1];
622 if (in_array($oc, $classes)) {
623 $found= false;
624 }
625 } else {
626 if (!in_array($oc, $classes)) {
627 $found= false;
628 }
629 }
630 }
632 if ($found) {
633 return $objectType;
634 }
635 }
637 return null;
638 }
641 function filterObjectType($dn, $classes)
642 {
643 // Walk thru classes and return on first match
644 $result= " ";
646 $objectType= $this->getObjectType($this->objectTypes, $classes);
647 if ($objectType) {
648 $result= "<img class='center' title='".LDAP::fix($dn)."' src='".$objectType["image"]."'>";
649 if (!isset($this->objectTypeCount[$objectType['label']])) {
650 $this->objectTypeCount[$objectType['label']]= 0;
651 }
652 $this->objectTypeCount[$objectType['label']]++;
653 }
654 return $result;
655 }
658 function filterActions($dn, $row, $classes)
659 {
660 // Do nothing if there's no menu defined
661 if (!isset($this->xmlData['actiontriggers']['action'])) {
662 return " ";
663 }
665 // Go thru all actions
666 $result= "";
667 $actions= $this->xmlData['actiontriggers']['action'];
668 foreach($actions as $action) {
669 // Skip the entry completely if there's no permission to execute it
670 if (!$this->hasActionPermission($action, $dn)) {
671 continue;
672 }
674 // If there's an objectclass definition and we don't have it
675 // add an empty picture here.
676 if (isset($action['objectclass'])){
677 $objectclass= $action['objectclass'];
678 if (preg_match('/^!(.*)$/', $objectclass, $m)){
679 $objectclass= $m[1];
680 if(in_array($objectclass, $classes)) {
681 $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
682 continue;
683 }
684 } else {
685 if(!in_array($objectclass, $classes)) {
686 $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
687 continue;
688 }
689 }
690 }
692 // Render normal entries as usual
693 if ($action['type'] == "entry") {
694 $label= $this->processElementFilter($action['label'], $this->entries[$row], $row);
695 $image= $this->processElementFilter($action['image'], $this->entries[$row], $row);
696 $result.="<input class='center' type='image' src='$image' title='$label' ".
697 "name='listing_".$action['name']."_$row' style='padding:1px'>";
698 }
700 // Handle special types
701 if ($action['type'] == "copypaste" || $action['type'] == "snapshot") {
703 $objectType= $this->getObjectType($this->objectTypes, $this->entries[$row]['objectClass']);
704 $category= $class= null;
705 if ($objectType) {
706 $category= $objectType['category'];
707 $class= $objectType['class'];
708 }
710 if ($action['type'] == "copypaste") {
711 $result.= $this->renderCopyPasteActions($row, $this->entries[$row]['dn'], $category, $class);
712 } else {
713 $result.= $this->renderSnapshotActions($row, $this->entries[$row]['dn'], $category, $class);
714 }
715 }
716 }
718 return $result;
719 }
722 function filterDepartmentLink($row, $dn, $description)
723 {
724 $attr= $this->departments[$row]['sort-attribute'];
725 $name= $this->departments[$row][$attr];
726 if (is_array($name)){
727 $name= $name[0];
728 }
729 $result= sprintf("%s [%s]", $name, $description[0]);
730 return("<a href='?plug=".$_GET['plug']."&PID=$this->pid&act=department_$row' title='$dn'>$result</a>");
731 }
734 function filterLink()
735 {
736 $result= " ";
738 $row= func_get_arg(0);
739 $pid= $this->pid;
740 $dn= LDAP::fix(func_get_arg(1));
741 $params= array(func_get_arg(2));
743 // Collect sprintf params
744 for ($i = 3;$i < func_num_args();$i++) {
745 $val= func_get_arg($i);
746 if (is_array($val)){
747 $params[]= $val[0];
748 continue;
749 }
750 $params[]= $val;
751 }
753 $result= " ";
754 $trans= call_user_func_array("sprintf", $params);
755 if ($trans != "") {
756 return("<a href='?plug=".$_GET['plug']."&PID=$pid&act=listing_edit_$row' title='$dn'>$trans</a>");
757 }
759 return $result;
760 }
763 function renderNavigation()
764 {
765 $result= array();
766 $enableBack = true;
767 $enableRoot = true;
768 $enableHome = true;
770 $ui = get_userinfo();
772 /* Check if base = first available base */
773 $deps = $ui->get_module_departments($this->module);
775 if(!count($deps) || $deps[0] == $this->filter->base){
776 $enableBack = false;
777 $enableRoot = false;
778 }
780 $listhead ="";
782 /* Check if we are in users home department */
783 if(!count($deps) ||$this->filter->base == get_base_from_people($ui->dn)){
784 $enableHome = false;
785 }
787 /* Draw root button */
788 if($enableRoot){
789 $result["ROOT"]= "<input class='center' type='image' src='images/lists/root.png' align='middle' ".
790 "title='"._("Go to root department")."' name='ROOT' alt='"._("Root")."'>";
791 }else{
792 $result["ROOT"]= "<img src='images/lists/root_grey.png' class='center' alt='"._("Root")."'>";
793 }
795 /* Draw back button */
796 if($enableBack){
797 $result["BACK"]= "<input class='center' type='image' align='middle' src='images/lists/back.png' ".
798 "title='"._("Go up one department")."' alt='"._("Up")."' name='BACK'>";
799 }else{
800 $result["BACK"]= "<img src='images/lists/back_grey.png' class='center' alt='"._("Up")."'>";
801 }
803 /* Draw home button */
804 if($enableHome){
805 $result["HOME"]= "<input class='center' type='image' align='middle' src='images/lists/home.png' ".
806 "title='"._("Go to users department")."' alt='"._("Home")."' name='HOME'>";
807 }else{
808 $result["HOME"]= "<img src='images/lists/home_grey.png' class='center' alt='"._("Home")."'>";
809 }
811 /* Draw reload button, this button is enabled everytime */
812 $result["RELOAD"]= "<input class='center' type='image' src='images/lists/reload.png' align='middle' ".
813 "title='"._("Reload list")."' name='REFRESH' alt='"._("Submit")."'>";
815 return ($result);
816 }
819 function getAction()
820 {
821 // Do not do anything if this is not our PID
822 if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->pid) {
823 return;
824 }
826 $result= array("targets" => array(), "action" => "");
828 // Filter GET with "act" attributes
829 if (isset($_GET['act'])) {
830 $key= validate($_GET['act']);
831 $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)$/', '$1', $key);
832 if (isset($this->entries[$target]['dn'])) {
833 $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+$/', '$1', $key);
834 $result['targets'][]= $this->entries[$target]['dn'];
835 }
837 // Drop targets if empty
838 if (count($result['targets']) == 0) {
839 unset($result['targets']);
840 }
841 return $result;
842 }
844 // Filter POST with "listing_" attributes
845 foreach ($_POST as $key => $prop) {
847 // Capture selections
848 if (preg_match('/^listing_selected_[0-9]+$/', $key)) {
849 $target= preg_replace('/^listing_selected_([0-9]+)$/', '$1', $key);
850 if (isset($this->entries[$target]['dn'])) {
851 $result['targets'][]= $this->entries[$target]['dn'];
852 }
853 continue;
854 }
856 // Capture action with target - this is a one shot
857 if (preg_match('/^listing_[a-zA-Z_]+_[0-9]+(|_x)$/', $key)) {
858 $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)(|_x)$/', '$1', $key);
859 if (isset($this->entries[$target]['dn'])) {
860 $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+(|_x)$/', '$1', $key);
861 $result['targets']= array($this->entries[$target]['dn']);
862 }
863 break;
864 }
866 // Capture action without target
867 if (preg_match('/^listing_[a-zA-Z_]+(|_x)$/', $key)) {
868 $result['action']= preg_replace('/^listing_([a-zA-Z_]+)(|_x)$/', '$1', $key);
869 continue;
870 }
871 }
873 // Filter POST with "act" attributes -> posted from action menu
874 if (isset($_POST['act']) && $_POST['act'] != '') {
875 $result['action']= validate($_POST['act']);
876 }
878 // Drop targets if empty
879 if (count($result['targets']) == 0) {
880 unset($result['targets']);
881 }
882 return $result;
883 }
886 function renderActionMenu()
887 {
888 // Don't send anything if the menu is not defined
889 if (!isset($this->xmlData['actionmenu']['action'])){
890 return "";
891 }
893 // Load shortcut
894 $actions= &$this->xmlData['actionmenu']['action'];
895 $result= "<input type='hidden' name='act' id='actionmenu' value=''>".
896 "<ul class='level1' id='root'><li><a href='#'>Aktionen <img ".
897 "border=0 src='images/lists/sort-down.png'></a>";
899 // Build ul/li list
900 $result.= $this->recurseActions($actions);
902 return "<div id='pulldown'>".$result."</li></ul><div>";
903 }
906 function recurseActions($actions)
907 {
908 static $level= 2;
909 $result= "<ul class='level$level'>";
910 $separator= "";
912 foreach ($actions as $action) {
914 // Skip the entry completely if there's no permission to execute it
915 if (!$this->hasActionPermission($action, $this->filter->base)) {
916 continue;
917 }
919 // Fill image if set
920 $img= "";
921 if (isset($action['image'])){
922 $img= "<img border=0 src='".$action['image']."'> ";
923 }
925 if ($action['type'] == "separator"){
926 $separator= " style='border-top:1px solid #AAA' ";
927 continue;
928 }
930 // Dive into subs
931 if ($action['type'] == "sub" && isset($action['action'])) {
932 $level++;
933 if (isset($action['label'])){
934 $result.= "<li$separator><a href='#'>$img"._($action['label'])." <img border='0' src='images/forward-arrow.png'></a>";
935 }
936 $result.= $this->recurseActions($action['action'])."</li>";
937 $level--;
938 $separator= "";
939 continue;
940 }
942 // Render entry elseways
943 if (isset($action['label'])){
944 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"".$action['name']."\";mainform.submit();'>$img"._($action['label'])."</a></li>";
945 }
947 // Check for special types
948 switch ($action['type']) {
949 case 'copypaste':
950 $result.= $this->renderCopyPasteMenu($separator);
951 break;
953 case 'snapshot':
954 $result.= $this->renderSnapshotMenu($separator);
955 break;
957 case 'daemon':
958 $result.= $this->renderDaemonMenu($separator);
959 break;
960 }
962 $separator= "";
963 }
965 $result.= "</ul>";
966 return $result;
967 }
970 function hasActionPermission($action, $dn)
971 {
972 $ui= get_userinfo();
974 if (isset($action['acl'])) {
975 $acls= $action['acl'];
976 if (!is_array($acls)) {
977 $acls= array($acls);
978 }
980 // Every ACL has to pass
981 foreach ($acls as $acl) {
982 $module= $this->module;
983 $acllist= array();
985 // Split for category and plugins if needed
986 // match for "[rw]" style entries
987 if (preg_match('/^\[([rwcdm]+)\]$/', $acl, $match)){
988 $aclList= array($match[1]);
989 }
991 // match for "users[rw]" style entries
992 if (preg_match('/^([a-zA-Z0-9]+)\[([rwcdm]+)\]$/', $acl, $match)){
993 $module= $match[1];
994 $aclList= array($match[2]);
995 }
997 // match for "users/user[rw]" style entries
998 if (preg_match('/^([a-zA-Z0-9]+\/[a-zA-Z0-9]+)\[([rwcdm]+)\]$/', $acl, $match)){
999 $module= $match[1];
1000 $aclList= array($match[2]);
1001 }
1003 // match "users/user[userPassword:rw(,...)*]" style entries
1004 if (preg_match('/^([a-zA-Z0-9]+\/[a-zA-Z0-9]+)\[([a-zA-Z0-9]+:[rwcdm]+(,[a-zA-Z0-9]+:[rwcdm]+)*)\]$/', $acl, $match)){
1005 $module= $match[1];
1006 $aclList= split(',', $match[2]);
1007 }
1009 // Walk thru prepared ACL by using $module
1010 foreach($aclList as $sAcl) {
1011 $checkAcl= "";
1013 // Category or detailed permission?
1014 if (strpos('/', $module) === false) {
1015 if (preg_match('/([a-zA-Z0-9]+):([rwcdm]+)/', $sAcl, $m) ) {
1016 $checkAcl= $ui->get_permissions($dn, $module, $m[1]);
1017 $sAcl= $m[2];
1018 } else {
1019 $checkAcl= $ui->get_permissions($dn, $module, '0');
1020 }
1021 } else {
1022 $checkAcl= $ui->get_category_permissions($dn, $module);
1023 }
1025 // Split up remaining part of the acl and check if it we're
1026 // allowed to do something...
1027 $parts= str_split($sAcl);
1028 foreach ($parts as $part) {
1029 if (strpos($checkAcl, $part) === false){
1030 return false;
1031 }
1032 }
1034 }
1035 }
1036 }
1038 return true;
1039 }
1042 function refreshBasesList()
1043 {
1044 global $config;
1045 $ui= get_userinfo();
1047 // Do some array munching to get it user friendly
1048 $ids= $config->idepartments;
1049 $d= $ui->get_module_departments($this->module);
1050 $k_ids= array_keys($ids);
1051 $deps= array_intersect($d,$k_ids);
1053 // Fill internal bases list
1054 $this->bases= array();
1055 foreach($k_ids as $department){
1056 $this->bases[$department] = $ids[$department];
1057 }
1058 }
1061 function getDepartments()
1062 {
1063 $departments= array();
1064 $ui= get_userinfo();
1066 // Get list of supported department types
1067 $types = departmentManagement::get_support_departments();
1069 // Load departments allowed by ACL
1070 $validDepartments = $ui->get_module_departments($this->module);
1072 // Build filter and look in the LDAP for possible sub departments
1073 // of current base
1074 $filter= "(&(objectClass=gosaDepartment)(|";
1075 $attrs= array("description", "objectClass");
1076 foreach($types as $name => $data){
1077 $filter.= "(objectClass=".$data['OC'].")";
1078 $attrs[]= $data['ATTR'];
1079 }
1080 $filter.= "))";
1081 $res= get_list($filter, $this->module, $this->base, $attrs, GL_NONE | GL_SIZELIMIT);
1083 // Analyze list of departments
1084 foreach ($res as $department) {
1085 if (!in_array($department['dn'], $validDepartments)) {
1086 continue;
1087 }
1089 // Add the attribute where we use for sorting
1090 $oc= null;
1091 foreach(array_keys($types) as $type) {
1092 if (in_array($type, $department['objectClass'])) {
1093 $oc= $type;
1094 break;
1095 }
1096 }
1097 $department['sort-attribute']= $types[$oc]['ATTR'];
1099 // Move to the result list
1100 $departments[]= $department;
1101 }
1103 return $departments;
1104 }
1107 function renderCopyPasteMenu($separator, $copy= true, $cut= true)
1108 {
1109 // We can only provide information if we've got a copypaste handler
1110 // instance
1111 if(!(isset($this->copyPasteHandler) && is_object($this->copyPasteHandler))){
1112 return "";
1113 }
1115 // Presets
1116 $result= "";
1117 $read= $paste= false;
1118 $ui= get_userinfo();
1120 // Switch flags to on if there's at least one category which allows read/paste
1121 foreach($this->categories as $category){
1122 $read= $read || preg_match('/r/', $ui->get_category_permissions($this->base, $category));
1123 $paste= $paste || $ui->is_pasteable($this->base, $category) == 1;
1124 }
1127 // Draw entries that allow copy and cut
1128 if($read){
1130 // Copy entry
1131 if($copy){
1132 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"copy\";mainform.submit();'><img src='images/lists/copy.png' alt='' border='0' class='center'> "._("Copy")."</a></li>";
1133 $separator= "";
1134 }
1136 // Cut entry
1137 if($cut){
1138 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"cut\";mainform.submit();'><img src='images/lists/cut.png' alt='' border='0' class='center'> "._("Cut")."</a></li>";
1139 $separator= "";
1140 }
1141 }
1143 // Draw entries that allow pasting entries
1144 if($paste){
1145 if($this->copyPasteHandler->entries_queued()){
1146 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"paste\";mainform.submit();'><img src='images/lists/paste.png' alt='' border='0' class='center'> "._("Paste")."</a></li>";
1147 }else{
1148 $result.= "<li$separator><a href='#'><img src='images/lists/paste-grey.png' alt='' border='0' class='center'> "._("Paste")."</a></li>";
1149 }
1150 }
1152 return($result);
1153 }
1156 function renderCopyPasteActions($row, $dn, $category, $class, $copy= true, $cut= true)
1157 {
1158 // We can only provide information if we've got a copypaste handler
1159 // instance
1160 if(!(isset($this->copyPasteHandler) && is_object($this->copyPasteHandler))){
1161 return "";
1162 }
1164 // Presets
1165 $ui = get_userinfo();
1166 $result = "";
1168 // Render cut entries
1169 if($cut){
1170 if($ui->is_cutable($dn, $category, $class)){
1171 $result .= "<input class='center' type='image'
1172 src='images/lists/cut.png' alt='"._("Cut")."' name='listing_cut_$row' title='"._("Cut this entry")."' style='padding:1px'>";
1173 }else{
1174 $result.="<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1175 }
1176 }
1178 // Render copy entries
1179 if($copy){
1180 if($ui->is_copyable($dn, $category, $class)){
1181 $result.= "<input class='center' type='image'
1182 src='images/lists/copy.png' alt='"._("Copy")."' name='listing_copy_$row' title='"._("Copy this entry")."' style='padding:1px'>";
1183 }else{
1184 $result.="<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1185 }
1186 }
1188 return($result);
1189 }
1192 function renderSnapshotMenu($separator)
1193 {
1194 // We can only provide information if we've got a snapshot handler
1195 // instance
1196 if(!(isset($this->snapshotHandler) && is_object($this->snapshotHandler))){
1197 return "";
1198 }
1200 // Presets
1201 $result = "";
1202 $ui = get_userinfo();
1204 if($this->snapshotHandler->enabled() && $ui->allow_snapshot_restore($this->base, $this->module)){
1206 // Check if there is something to restore
1207 $restore= false;
1208 foreach($this->snapshotHandler->getSnapshotBases() as $base){
1209 $restore= $restore || count($this->snapshotHandler->getDeletedSnapshots($base)) > 0;
1210 }
1212 // Draw icons according to the restore flag
1213 if($restore){
1214 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"restore\";mainform.submit();'><img src='images/lists/restore.png' alt='' border='0' class='center'> "._("Restore snapshots")."</a></li>";
1215 }else{
1216 $result.= "<li$separator><a href='#'><img src='images/lists/restore_grey.png' alt='' border='0' class='center'> "._("Restore snapshots")."</a></li>";
1217 }
1218 }
1220 return($result);
1221 }
1224 function renderSnapshotActions($row, $dn, $category, $class, $copy= true, $cut= true)
1225 {
1226 // We can only provide information if we've got a snapshot handler
1227 // instance
1228 if(!(isset($this->snapshotHandler) && is_object($this->snapshotHandler))){
1229 return "";
1230 }
1232 // Presets
1233 $result= "";
1234 $ui = get_userinfo();
1236 // Only act if enabled here
1237 if($this->snapshotHandler->enabled()){
1239 // Draw restore button
1240 if ($ui->allow_snapshot_restore($dn, $category)){
1242 // Do we have snapshots for this dn?
1243 if($this->snapshotHandler->hasSnapshots($dn)){
1244 $result.= "<input class='center' type='image' src='images/lists/restore.png' ".
1245 "alt='"._("Restore snapshot")."' name='listing_restore_$row' title='".
1246 _("Restore snapshot")."' style='padding:1px'>";
1247 } else {
1248 $result.= "<img src='images/lists/restore_grey.png' alt=' ' class='center' style='padding:1px'>";
1249 }
1250 }
1252 // Draw snapshot button
1253 if($ui->allow_snapshot_create($dn, $category)){
1254 $result.= "<input class='center' type='image' src='images/snapshot.png' ".
1255 "alt='"._("Create snapshot")."' name='listing_snapshot_$row' title='".
1256 _("Create a new snapshot from this object")."' style='padding:1px'>";
1257 }else{
1258 $result.= "<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
1259 }
1260 }
1262 return($result);
1263 }
1266 function renderDaemonMenu($separator)
1267 {
1268 $result= "";
1270 // If there is a daemon registered, draw the menu entries
1271 if(class_available("DaemonEvent")){
1272 $events= DaemonEvent::get_event_types_by_category($this->categories);
1273 if(count($events['BY_CLASS'])){
1274 foreach($events['BY_CLASS'] as $name => $event){
1275 $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"$name\";mainform.submit();'><img src='".$event['MenuImage']."' alt='' border='0' class='center'> ".$event['s_Menu_Name']."</a></li>";
1276 $separator= "";
1277 }
1278 }
1279 }
1281 return $result;
1282 }
1284 }
1286 ?>