Code

a3a169a06e8685555823cbd6b80a26dd014c64c0
[gosa.git] / include / class_acl.inc
1 <?php
3 class acl extends plugin
4 {
5   /* Definitions */
6   var $plHeadline= "Access control";
7   var $plDescription= "This does something";
9   /* attribute list for save action */
10   var $attributes= array('gosaAclEntry');
11   var $objectclasses= array('gosaAcl');
13   /* Helpers */
14   var $dialogState= "head";
15   var $gosaAclEntry= array();
16   var $aclType= "";
17   var $aclObject= "";
18   var $aclContents= array();
19   var $target= "group";
20   var $aclTypes= array();
21   var $aclObjects= array();
22   var $aclMyObjects= array();
23   var $users= array();
24   var $groups= array();
25   var $recipients= array();
26   var $isContainer= FALSE;
27   var $currentIndex= 0;
28   var $wasNewEntry= FALSE;
29   var $ocMapping= array();
30   var $savedAclContents= array();
33   function acl ($config, $parent, $dn= NULL)
34   {
35     /* Include config object */
36     plugin::plugin($config, $dn);
38     /* Load ACL's */
39     $this->gosaAclEntry= array();
40     if (isset($this->attrs['gosaAclEntry'])){
41       for ($i= 0; $i<$this->attrs['gosaAclEntry']['count']; $i++){
42         $acl= $this->attrs['gosaAclEntry'][$i];
43         $this->gosaAclEntry= array_merge($this->gosaAclEntry, $this->explodeACL($acl));
44       }
45     }
46     ksort($this->gosaAclEntry);
48     /* Save parent - we've to know more about it than other plugins... */
49     $this->parent= $parent;
51     /* Container? */
52     if (preg_match('/^(ou|c|l|dc)=/i', $dn)){
53       $this->isContainer= TRUE;
54     }
56     /* Users */
57     $ui= get_userinfo();
58     $tag= $ui->gosaUnitTag;
59     $ldap= $config->get_ldap_link();
60     $ldap->cd($config->current['BASE']);
61     if ($tag == ""){
62       $ldap->search('(objectClass=gosaAccount)', array('uid', 'cn'));
63     } else {
64       $ldap->search('(&(objectClass=gosaAccount)(gosaUnitTag='.$tag.'))', array('uid', 'cn'));
65     }
66     while ($attrs= $ldap->fetch()){
67       $this->users['U:'.$attrs['dn']]= $attrs['cn'][0].' ['.$attrs['uid'][0].']';
68     }
69     ksort($this->users);
71     /* Groups */
72     $ldap->cd($config->current['BASE']);
73     if ($tag == ""){
74       $ldap->search('(objectClass=posixGroup)', array('cn', 'description'));
75     } else {
76       $ldap->search('(&(objectClass=posixGroup)(gosaUnitTag='.$tag.'))', array('cn', 'description'));
77     }
78     while ($attrs= $ldap->fetch()){
79       $dsc= "";
80       if (isset($attrs['description'][0])){
81         $dsc= $attrs['description'][0];
82       }
83       $this->groups['G:'.$attrs['dn']]= $attrs['cn'][0].' ['.$dsc.']';
84     }
85     ksort($this->groups);
87     /* Objects */
88     $tmp= get_global('plist');
89     $plist= $tmp->info;
90     if (isset($this->parent) && $this->parent != NULL){
91       $oc= array();
92       foreach ($this->parent->by_object as $key => $obj){
93         $oc= array_merge($oc, $obj->objectclasses);
94       }
95       if (in_array_ics('organizationalUnit', $oc)){
96         $this->isContainer= TRUE;
97       }
98     } else {
99       $oc=  $this->attrs['objectClass'];
100     }
103     /* Extract available categories from plugin info list */
104     foreach ($plist as $class => $acls){
106       /* Only feed categories */
107       if (isset($acls['plCategory'])){
109         /* Walk through supplied list and feed only translated categories */
110         foreach($acls['plCategory'] as $idx => $data){
112           /* Non numeric index means -> base object containing more informations */
113           if (preg_match('/^[0-9]+$/', $idx)){
114             if (!isset($this->ocMapping[$data])){
115               $this->ocMapping[$data]= array();
116               $this->ocMapping[$data][]= '0';
117             }
118             $this->ocMapping[$data][]= $class;
119           } else {
120             if (!isset($this->ocMapping[$idx])){
121               $this->ocMapping[$idx]= array();
122               $this->ocMapping[$idx][]= '0';
123             }
124             $this->ocMapping[$idx][]= $class;
125             $this->aclObjects[$idx]= $data['description'];
127             /* Additionally filter the classes we're interested in in "self edit" mode */
128             if (is_array($data['objectClass'])){
129               foreach($data['objectClass'] as $objectClass){
130                 if (in_array_ics($objectClass, $oc)){
131                   $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
132                   break;
133                 }
134               }
135             } else {
136               if (in_array_ics($data['objectClass'], $oc)){
137                 $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
138               }
139             }
140           }
142         }
143       }
144     }
145     $this->aclObjects['all']= '*&nbsp;'._("All categories");
146     $this->ocMapping['all']= array('0' => 'all');
148     /* Sort categories */
149     asort($this->aclObjects);
151     /* Fill acl types */
152     if ($this->isContainer){
153       $this->aclTypes= array("reset" => _("Reset ACLs"),
154                              "one" => _("One level"),
155                              "base" => _("Current object"),
156                              "sub" => _("Complete subtree"),
157                              "psub" => _("Complete subtree (permanent)"));
158                              //"role" => _("Use ACL defined in role"));
159     } else {
160       $this->aclTypes= array("base" => _("Current object"),
161           "role" => _("Use ACL defined in role"));
162     }
163     asort($this->aclTypes);
164     $this->targets= array("user" => _("Users"), "group" => _("Groups"));
165     asort($this->targets);
167     /* Finally - we want to get saved... */
168     $this->is_account= TRUE;
169   }
172   function execute()
173   {
174     /* Call parent execute */
175     plugin::execute();
177     $tmp= get_global('plist');
178     $plist= $tmp->info;
180     /* Handle posts */
181     if (isset($_POST['new_acl'])){
182       $this->dialogState= 'create';
183       $this->dialog= TRUE;
184       $this->currentIndex= count($this->gosaAclEntry);
185       $this->loadAclEntry(TRUE);
186     }
188     $new_acl= array();
189     $aclDialog= FALSE;
190     foreach($_POST as $name => $post){
192       /* Actions... */
193       if (preg_match('/^acl_edit_.*_x/', $name)){
194         $this->dialogState= 'create';
195         $this->dialog= TRUE;
196         $this->currentIndex= preg_replace('/^acl_edit_([0-9]+).*$/', '\1', $name);
197         $this->loadAclEntry();
198         continue;
199       }
200       if (preg_match('/^acl_del_.*_x/', $name)){
201         unset($this->gosaAclEntry[preg_replace('/^acl_del_([0-9]+).*$/', '\1', $name)]);
202         continue;
203       }
205       if (preg_match('/^cat_edit_.*_x/', $name)){
206         $this->aclObject= preg_replace('/^cat_edit_([^_]+)_.*$/', '\1', $name);
207         $this->dialogState= 'edit';
208         foreach ($this->ocMapping[$this->aclObject] as $oc){
209           if (isset($this->aclContents[$oc])){
210             $this->savedAclContents[$oc]= $this->aclContents[$oc];
211           }
212         }
213         continue;
214       }
215       if (preg_match('/^cat_del_.*_x/', $name)){
216         $idx= preg_replace('/^cat_del_([^_]+)_.*$/', '\1', $name);
217         foreach ($this->ocMapping[$idx] as $key){
218           unset($this->aclContents["$idx/$key"]);
219         }
220         continue;
221       }
223       /* Sorting... */
224       if (preg_match('/^sortup_.*_x/', $name)){
225         $index= preg_replace('/^sortup_([0-9]+).*$/', '\1', $name);
226         if ($index > 0){
227           $tmp= $this->gosaAclEntry[$index];
228           $this->gosaAclEntry[$index]= $this->gosaAclEntry[$index-1];
229           $this->gosaAclEntry[$index-1]= $tmp;
230         }
231         continue;
232       }
233       if (preg_match('/^sortdown_.*_x/', $name)){
234         $index= preg_replace('/^sortdown_([0-9]+).*$/', '\1', $name);
235         if ($index < count($this->gosaAclEntry)-1){
236           $tmp= $this->gosaAclEntry[$index];
237           $this->gosaAclEntry[$index]= $this->gosaAclEntry[$index+1];
238           $this->gosaAclEntry[$index+1]= $tmp;
239         }
240         continue;
241       }
243       /* ACL saving... */
244       if (preg_match('/^acl_.*_[^xy]$/', $name)){
245         $aclDialog= TRUE;
246         list($dummy, $object, $attribute, $value)= split('_', $name);
248         /* Skip for detection entry */
249         if ($object == 'dummy') {
250           continue;
251         }
253         /* Ordinary ACLs */
254         if (!isset($new_acl[$object])){
255           $new_acl[$object]= array();
256         }
257         if (isset($new_acl[$object][$attribute])){
258           $new_acl[$object][$attribute].= $value;
259         } else {
260           $new_acl[$object][$attribute]= $value;
261         }
262       }
263     }
264     
265     /* Only be interested in new acl's, if we're in the right _POST place */
266     if ($aclDialog && is_array($this->ocMapping[$this->aclObject])){
268       foreach ($this->ocMapping[$this->aclObject] as $oc){
269         unset($this->aclContents[$oc]);
270         unset($this->aclContents[$this->aclObject.'/'.$oc]);
271         if (isset($new_acl[$oc])){
272           $this->aclContents[$oc]= $new_acl[$oc];
273         }
274         if (isset($new_acl[$this->aclObject.'/'.$oc])){
275           $this->aclContents[$this->aclObject.'/'.$oc]= $new_acl[$this->aclObject.'/'.$oc];
276         }
277       }
278     }
280     /* Save new acl in case of base edit mode */
281     if ($this->aclType == 'base'){
282       $this->aclContents= $new_acl;
283     }
285     /* Cancel new acl? */
286     if (isset($_POST['cancel_new_acl'])){
287       $this->dialogState= 'head';
288       $this->dialog= FALSE;
289       if ($this->wasNewEntry){
290         unset ($this->gosaAclEntry[$this->currentIndex]);
291       }
292     }
294     /* Store ACL in main object? */
295     if (isset($_POST['submit_new_acl'])){
296       $this->gosaAclEntry[$this->currentIndex]['type']= $this->aclType;
297       $this->gosaAclEntry[$this->currentIndex]['members']= $this->recipients;
298       $this->gosaAclEntry[$this->currentIndex]['acl']= $this->aclContents;
299       $this->dialogState= 'head';
300       $this->dialog= FALSE;
301     }
303     /* Cancel edit acl? */
304     if (isset($_POST['cancel_edit_acl'])){
305       $this->dialogState= 'create';
306       foreach ($this->ocMapping[$this->aclObject] as $oc){
307         if (isset($this->savedAclContents[$oc])){
308           $this->aclContents[$oc]= $this->savedAclContents[$oc];
309         }
310       }
311     }
313     /* Save edit acl? */
314     if (isset($_POST['submit_edit_acl'])){
315       $this->dialogState= 'create';
316     }
318     /* Add acl? */
319     if (isset($_POST['add_acl']) && $_POST['aclObject'] != ""){
320       $this->dialogState= 'edit';
321       $this->savedAclContents= array();
322       foreach ($this->ocMapping[$this->aclObject] as $oc){
323         if (isset($this->aclContents[$oc])){
324           $this->savedAclContents[$oc]= $this->aclContents[$oc];
325         }
326       }
327     }
329     /* Add to list? */
330     if (isset($_POST['add']) && isset($_POST['source'])){
331       foreach ($_POST['source'] as $key){
332         if ($this->target == 'user'){
333           $this->recipients[$key]= $this->users[$key];
334         }
335         if ($this->target == 'group'){
336           $this->recipients[$key]= $this->groups[$key];
337         }
338       }
339       ksort($this->recipients);
340     }
342     /* Remove from list? */
343     if (isset($_POST['del']) && isset($_POST['recipient'])){
344       foreach ($_POST['recipient'] as $key){
345           unset($this->recipients[$key]);
346       }
347     }
349     /* Save common values */
350     foreach (array("aclType", "aclObject", "target") as $key){
351       if (isset($_POST[$key])){
352         $this->$key= validate($_POST[$key]);
353       }
354     }
356     /* Create templating instance */
357     $smarty= get_smarty();
359     if ($this->dialogState == 'head'){
360       /* Draw list */
361       $aclList= new DivSelectBox("aclList");
362       $aclList->SetHeight(450);
363       
364       /* Fill in entries */
365       foreach ($this->gosaAclEntry as $key => $entry){
366         $field1= array("string" => $this->aclTypes[$entry['type']], "attach" => "style='width:100px'");
367         $field2= array("string" => $this->assembleAclSummary($entry));
368         $action= "<input type='image' name='sortup_$key' alt='up' title='"._("Up")."' src='images/sort_up.png' align='top'>";
369         $action.= "<input type='image' name='sortdown_$key' alt='down' title='"._("Down")."' src='images/sort_down.png'>";
370         $action.= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."' name='acl_edit_$key' title='"._("Edit ACL")."'>";
371         $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."' name='acl_del_$key' title='"._("Delete ACL")."'>";
373         $field3= array("string" => $action, "attach" => "style='border-right:0px;width:50px;text-align:right;'");
374         $aclList->AddEntry(array($field1, $field2, $field3));
375       }
377       $smarty->assign("aclList", $aclList->DrawList());
378     }
380     if ($this->dialogState == 'create'){
381       /* Draw list */
382       $aclList= new DivSelectBox("aclList");
383       $aclList->SetHeight(150);
385       /* Add settings for all categories to the (permanent) list */
386       foreach ($this->aclObjects as $section => $dsc){
387         $summary= "";
388         foreach($this->ocMapping[$section] as $oc){
389           if (isset($this->aclContents[$oc]) && count($this->aclContents[$oc]) && isset($this->aclContents[$oc][0]) &&
390               $this->aclContents[$oc][0] != ""){
392             $summary.= "$oc, ";
393             continue;
394           }
395           if (isset($this->aclContents["$section/$oc"]) && count($this->aclContents["$section/$oc"]) && isset($this->aclContents["$section/$oc"][0]) &&
396               $this->aclContents["$section/$oc"][0] != ""){
398             $summary.= "$oc, ";
399             continue;
400           }
401           if (isset($this->aclContents[$oc]) && !isset($this->aclContents[$oc][0]) && count($this->aclContents[$oc])){
402             $summary.= "$oc, ";
403           }
404         }
406         /* Set summary... */
407         if ($summary == ""){
408           $summary= '<i>'._("No ACL settings for this category").'</i>';
409         } else {
410           $summary= sprintf(_("Contains ACLs for these objects: %s"), preg_replace('/, $/', '', $summary));
411         }
413         $field1= array("string" => $dsc, "attach" => "style='width:100px'");
414         $field2= array("string" => $summary);
415         $action= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."' name='cat_edit_$section' title='"._("Edit categories ACLs")."'>";
416         $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."' name='cat_del_$section' title='"._("Clear categories ACLs")."'>";
417         $field3= array("string" => $action, "attach" => "style='border-right:0px;width:50px'");
418         $aclList->AddEntry(array($field1, $field2, $field3));
419       }
421       $smarty->assign("aclList", $aclList->DrawList());
422       $smarty->assign("aclType", $this->aclType);
423       $smarty->assign("aclTypes", $this->aclTypes);
424       $smarty->assign("target", $this->target);
425       $smarty->assign("targets", $this->targets);
427       /* Assign possible target types */
428       $smarty->assign("targets", $this->targets);
429       foreach ($this->attributes as $attr){
430         $smarty->assign($attr, $this->$attr);
431       }
434       /* Generate list */
435       $tmp= array();
436       foreach (array("user" => "users", "group" => "groups") as $field => $arr){
437         if ($this->target == $field){
438           foreach ($this->$arr as $key => $value){
439             if (!isset($this->recipients[$key])){
440               $tmp[$key]= $value;
441             }
442           }
443         }
444       }
445       $smarty->assign('sources', $tmp);
446       $smarty->assign('recipients', $this->recipients);
448       /* Acl selector if scope is base */
449       if ($this->aclType == 'base'){
450         $smarty->assign('aclSelector', $this->buildAclSelector($this->myAclObjects));
451       }
452     }
454     if ($this->dialogState == 'edit'){
455       $smarty->assign('headline', sprintf(_("Edit ACL for '%s', scope is '%s'"), $this->aclObjects[$this->aclObject], $this->aclTypes[$this->aclType]));
457       /* Collect objects for selected category */
458       foreach ($this->ocMapping[$this->aclObject] as $idx => $class){
459         if ($idx == 0){
460           continue;
461         }
462         $aclObjects[$this->aclObject.'/'.$class]= $plist[$class]['plDescription'];
463       }
464       if ($this->aclObject == 'all'){
465         $aclObjects['all']= _("All objects in current subtree");
466       }
467       $smarty->assign('aclSelector', $this->buildAclSelector($aclObjects));
468     }
470     /* Show main page */
471     $smarty->assign("dialogState", $this->dialogState);
473     return ($smarty->fetch (get_template_path('acl.tpl')));
474   }
476   function sort_by_priority($list)
477   {
478     $tmp= get_global('plist');
479     $plist= $tmp->info;
480     asort($plist);
482     foreach($list as $name => $translation){
483       $na  =  preg_replace("/^.*\//","",$name);
484       $prio=  $plist[$na]['plPriority'] ;
486       $newSort[$name] = $prio;
487     }
489     asort($newSort);
491     $ret = array();
492     foreach($newSort as $name => $prio){
493       $ret[$name] = $list[$name];
494     }
495     return($ret);
496   }
498   function buildAclSelector($list)
499   {
500     $display= "<input type='hidden' name='acl_dummy_0_0_0' value='1'>";
501     $cols= 3;
502     $tmp= get_global('plist');
503     $plist= $tmp->info;
504     asort($plist);
506     /* Add select all/none buttons */
507     $style = "style='width:100px;'";
509     $display .= "<input ".$style." type='button' name='toggle_all_create' onClick=\"acl_toggle_all('_0_c$');\" value='Toggle C'>";
510     $display .= "<input ".$style." type='button' name='toggle_all_move'   onClick=\"acl_toggle_all('_0_m$');\" value='Toggle M'>";
511     $display .= "<input ".$style." type='button' name='toggle_all_remove' onClick=\"acl_toggle_all('_0_d$');\" value='Toggle D'> - ";
512     $display .= "<input ".$style." type='button' name='toggle_all_read'   onClick=\"acl_toggle_all('_0_r$');\" value='Toggle R'>";
513     $display .= "<input ".$style." type='button' name='toggle_all_write'  onClick=\"acl_toggle_all('_0_w$');\" value='Toggle W'> - ";
514     
515     $display .= "<input ".$style." type='button' name='toggle_all_sub_read'  onClick=\"acl_toggle_all('[^0]_r$');\" value='R+'>";
516     $display .= "<input ".$style." type='button' name='toggle_all_sub_write'  onClick=\"acl_toggle_all('[^0]_w$');\" value='W+'>";
517   
518     $display .= "<br>";
519   
520     $style = "style='width:50px;'";
521     $display .= "<input ".$style." type='button' name='set_true_all_create' onClick=\"acl_set_all('_0_c$',true);\" value='C+'>";
522     $display .= "<input ".$style." type='button' name='set_false_all_create' onClick=\"acl_set_all('_0_c$',false);\" value='C-'>";
523     $display .= "<input ".$style." type='button' name='set_true_all_move' onClick=\"acl_set_all('_0_m$',true);\" value='M+'>";
524     $display .= "<input ".$style." type='button' name='set_false_all_move' onClick=\"acl_set_all('_0_m$',false);\" value='M-'>";
525     $display .= "<input ".$style." type='button' name='set_true_all_remove' onClick=\"acl_set_all('_0_d$',true);\" value='D+'>";
526     $display .= "<input ".$style." type='button' name='set_false_all_remove' onClick=\"acl_set_all('_0_d$',false);\" value='D-'> - ";
527     $display .= "<input ".$style." type='button' name='set_true_all_read' onClick=\"acl_set_all('_0_r$',true);\" value='R+'>";
528     $display .= "<input ".$style." type='button' name='set_false_all_read' onClick=\"acl_set_all('_0_r$',false);\" value='R-'>";
529     $display .= "<input ".$style." type='button' name='set_true_all_write' onClick=\"acl_set_all('_0_w$',true);\" value='W+'>";
530     $display .= "<input ".$style." type='button' name='set_false_all_write' onClick=\"acl_set_all('_0_w$',false);\" value='W-'> - ";
532     $display .= "<input ".$style." type='button' name='set_true_all_read' onClick=\"acl_set_all('[^0]_r$',true);\" value='R+'>";
533     $display .= "<input ".$style." type='button' name='set_false_all_read' onClick=\"acl_set_all('[^0]_r$',false);\" value='R-'>";
534     $display .= "<input ".$style." type='button' name='set_true_all_write' onClick=\"acl_set_all('[^0]_w$',true);\" value='W+'>";
535     $display .= "<input ".$style." type='button' name='set_false_all_write' onClick=\"acl_set_all('[^0]_w$',false);\" value='W-'>";
536     /* Build general objects */
538     $list =$this->sort_by_priority($list);
539     foreach ($list as $key => $name){
541       /* Create sub acl if it does not exist */
542       if (!isset($this->aclContents[$key])){
543         $this->aclContents[$key]= array();
544         $this->aclContents[$key][0]= '';
545       }
546       $currentAcl= $this->aclContents[$key];
548       /* Object header */
549       if($_SESSION['js']) {
550         if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) {
551           $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
552                      "\n  <tr>".
553                      "\n    <td style='background-color:#C8C8C8;height:1.8em;' colspan=".($cols-1)."><b>"._("Object").": $name</b></td>".
554                      "\n    <td align='right' style='background-color:#C8C8C8;height:1.8em;'>".
555                      "\n    <input type='button' onclick='divtoggle(\"".preg_replace("/[^a-z0-9]/i","_",$name)."\");' value='"._("Show/Hide Advanced Settings")."' /></td>".
556                      "\n  </tr>";
557         } else if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT'])) {
558           $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
559                      "\n  <tr>".
560                      "\n    <td style='background-color:#C8C8C8;height:1.8em;' colspan=".($cols-1)."><b>"._("Object").": $name</b></td>".
561                      "\n    <td align='right' style='background-color:#C8C8C8;height:1.8em;'>".
562                      "\n    <input type='button' onclick='divtoggle(\"".preg_replace("/[^a-z0-9]/i","_",$name)."\");' value='"._("Show/Hide Advanced Settings")."' /></td>".
563                      "\n  </tr>";
564         } else {
565           $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
566                      "\n  <tr>".
567                      "\n    <td style='background-color:#C8C8C8;height:1.8em;' colspan=$cols><b>"._("Object").": $name</b></td>".
568                      "\n  </tr>";
569         }
570       } else {
571           $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
572                      "\n  <tr>".
573                      "\n    <td style='background-color:#C8C8C8;height:1.8em;' colspan=$cols><b>"._("Object").": $name</b></td>".
574                      "\n  </tr>";
575       }
577       /* Generate options */
578       $spc= "&nbsp;&nbsp;";
579       if ($this->isContainer && $this->aclType != 'base'){
580         $options= $this->mkchkbx($key."_0_c",  _("Create objects"), preg_match('/c/', $currentAcl[0])).$spc;
581         $options.= $this->mkchkbx($key."_0_m", _("Move objects"), preg_match('/m/', $currentAcl[0])).$spc;
582         $options.= $this->mkchkbx($key."_0_d", _("Remove objects"), preg_match('/d/', $currentAcl[0])).$spc;
583         if ($plist[preg_replace('%^.*/%', '', $key)]['plSelfModify']){
584           $options.= $this->mkchkbx($key."_0_s", _("Modifyable by owner"), preg_match('/s/', $currentAcl[0])).$spc;
585         }
586       } else {
587         $options= $this->mkchkbx($key."_0_m", _("Move object"), preg_match('/m/', $currentAcl[0])).$spc;
588         $options.= $this->mkchkbx($key."_0_d", _("Remove object"), preg_match('/d/', $currentAcl[0])).$spc;
589         if ($plist[preg_replace('%^.*/%', '', $key)]['plSelfModify']){
590           $options.= $this->mkchkbx($key."_0_s", _("Modifyable by owner"), preg_match('/s/', $currentAcl[0])).$spc;
591         }
592       }
594       /* Global options */
595       $more_options= $this->mkchkbx($key."_0_r",  _("read"), preg_match('/r/', $currentAcl[0])).$spc;
596       $more_options.= $this->mkchkbx($key."_0_w", _("write"), preg_match('/w/', $currentAcl[0]));
598       $display.= "\n  <tr>".
599                  "\n    <td style='background-color:#E0E0E0' colspan=".($cols-1).">$options</td>".
600                  "\n    <td style='background-color:#D4D4D4'>&nbsp;".("Complete object:")." $more_options</td>".
601                  "\n  </tr>";
603       /* Walk through the list of attributes */
604       $cnt= 1;
605       $splist= $plist[preg_replace('%^.*/%', '', $key)]['plProvidedAcls'];
606       asort($splist);
607       if($_SESSION['js']) {
608         if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) {
609           $display.= "\n  <tr id='tr_".preg_replace("/[^a-z0-9]/i","_",$name)."' style='vertical-align:top;height:0px;'>".
610                      "\n    <td colspan=".$cols.">".
611                      "\n      <div id='".preg_replace("/[^a-z0-9]/i","_",$name)."' style='overflow:hidden;visibility:hidden;height:0px;vertical-align:top;width:100%;'>".
612                      "\n        <table style='width:100%;'>";
613         } else if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT'])) {
614           $display.= "\n  <tr id='tr_".preg_replace("/[^a-z0-9]/i","_",$name)."' style='vertical-align:top;height:0px;'>".
615                      "\n    <td colspan=".$cols.">".
616                      "\n      <div id='".preg_replace("/[^a-z0-9]/i","_",$name)."' style='position:absolute;overflow:hidden;visibility:hidden;height:0px;vertical-align:top;width:100%;'>".
617                      "\n        <table style='width:100%;'>";
618         }
619       }
620       foreach($splist as $attr => $dsc){
622         /* Skip pl* attributes, they are internal... */
623         if (preg_match('/^pl[A-Z]+.*$/', $attr)){
624           continue;
625         }
627         /* Open table row */
628         if ($cnt == 1){
629           $display.= "\n  <tr>";
630         }
632         /* Close table row */
633         if ($cnt == $cols){
634           $cnt= 1;
635           $rb= "";
636           $end= "\n  </tr>";
637         } else {
638           $cnt++;
639           $rb= "border-right:1px solid #A0A0A0;";
640           $end= "";
641         }
643         /* Collect list of attributes */
644         $state= "";
645         if (isset($currentAcl[$attr])){
646           $state= $currentAcl[$attr];
647         }
648         $display.= "\n    <td style='border-top:1px solid #A0A0A0;${rb}width:".(int)(100/$cols)."%'>".
649                    "\n      <b>$dsc</b> ($attr)<br>".$this->mkrwbx($key."_".$attr, $state)."</td>$end";
650       }
651       
652       /* Fill missing td's if needed */
653       if (--$cnt != $cols && $cnt != 0){
654        $display.= str_repeat("\n    <td style='border-top:1px solid #A0A0A0; width:".(int)(100/$cols)."%'>&nbsp;</td>", $cols-$cnt); 
655       }
657       if($_SESSION['js']) {
658         if(isset($_SERVER['HTTP_USER_AGENT']) && (preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) || (preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT']))) {
659           $display.= "\n        </table>".
660                      "\n      </div>".
661                      "\n    </td>".
662                      "\n  </tr>";
663         }
664       }
666       $display.= "\n</table><br />\n";
667     }
669     return ($display);
670   }
673   function mkchkbx($name, $text, $state= FALSE)
674   {
675     $state= $state?"checked":"";
676     return "\n      <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."' type=checkbox name='acl_$name' $state>".
677            "\n      <label for='acl_$name'>$text</label>";
678   }
681   function mkrwbx($name, $state= "")
682   {
683     $rstate= preg_match('/r/', $state)?'checked':'';
684     $wstate= preg_match('/w/', $state)?'checked':'';
685     return ("\n      <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_r' type=checkbox name='acl_${name}_r' $rstate>".
686             "\n      <label for='acl_${name}_r'>"._("read")."</label>".
687             "\n      <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_w' type=checkbox name='acl_${name}_w' $wstate>".
688             "\n      <label for='acl_${name}_w'>"._("write")."</label>");
689   }
692   function explodeACL($acl)
693   {
694     list($index, $type)= split(':', $acl);
695     $a= array( $index => array("type" => $type,
696                                "members" => acl::extractMembers($acl)));
697     
698     /* Handle different types */
699     switch ($type){
701       case 'psub':
702       case 'sub':
703       case 'one':
704       case 'base':
705         $a[$index]['acl']= acl::extractACL($acl);
706         break;
707       
708       case 'role':
709         echo "Role";
710         break;
712       case 'reset':
713         break;
714       
715       default:
716         print_red(sprintf(_("Unkown ACL type '%s'. Don't know how to handle it."), $type));
717         $a= array();
718     }
719     
720     return ($a);
721   }
724   function extractMembers($acl)
725   {
726     global $config;
727     $a= array();
729     /* Rip acl off the string, seperate by ',' and place it in an array */
730     $ms= preg_replace('/^[^:]+:[^:]+:([^:]+).*$/', '\1', $acl);
731     if ($ms == $acl){
732       return $a;
733     }
734     $ma= split(',', $ms);
736     /* Decode dn's, fill with informations from LDAP */
737     $ldap= $config->get_ldap_link();
738     foreach ($ma as $memberdn){
739       $dn= base64_decode($memberdn);
740       $ldap->cat($dn, array('cn', 'objectClass', 'description', 'uid'));
742       /* Found entry... */
743       if ($ldap->count()){
744         $attrs= $ldap->fetch();
745         if (in_array_ics('gosaAccount', $attrs['objectClass'])){
746           $a['U:'.$dn]= $attrs['cn'][0]." [".$attrs['uid'][0]."]";
747         } else {
748           $a['G:'.$dn]= $attrs['cn'][0];
749           if (isset($attrs['description'][0])){
750             $a['G:'.$dn].= " [".$attrs['description'][0]."]";
751           }
752         }
754       /* ... or not */
755       } else {
756         $a['U:'.$dn]= sprintf(_("Unknown entry '%s'!"), $dn);
757       }
758     }
760     return ($a);
761   }
764   function extractACL($acl)
765   {
766     /* Rip acl off the string, seperate by ',' and place it in an array */
767     $as= preg_replace('/^[^:]+:[^:]+:[^:]*:(.*)$/', '\1', $acl);
768     $aa= split(',', $as);
769     $a= array();
771     /* Dis-assemble single ACLs */
772     foreach($aa as $sacl){
773       
774       /* Dis-assemble field ACLs */
775       $ao= split('#', $sacl);
776       $gobject= "";
777       foreach($ao as $idx => $ssacl){
779         /* First is department with global acl */
780         $object= preg_replace('/^([^;]+);.*$/', '\1', $ssacl);
781         $gacl=   preg_replace('/^[^;]+;(.*)$/', '\1', $ssacl);
782         if ($idx == 0){
783           /* Create hash for this object */
784           $gobject= $object;
785           $a[$gobject]= array();
787           /* Append ACL if set */
788           if ($gacl != ""){
789             $a[$gobject]= array($gacl);
790           }
791         } else {
793           /* All other entries get appended... */
794           list($field, $facl)= split(';', $ssacl);
795           $a[$gobject][$field]= $facl;
796         }
798       }
799     }
801     return ($a);
802   }
804   
805   function assembleAclSummary($entry)
806   {
807     $summary= "";
809     /* Summarize ACL */
810     if (isset($entry['acl'])){
811       $acl= "";
812       foreach ($entry['acl'] as $name => $object){
813         if (count($object)){
814           $acl.= "$name, ";
815         }
816       }
817       $summary.= sprintf(_("Contains settings for these objects: %s"), preg_replace('/, $/', '', $acl));
818     }
820     /* Summarize members */
821     if ($summary != ""){
822       $summary.= ", ";
823     }
824     if (count($entry['members'])){
825       $summary.= _("Members:")." ";
826       foreach ($entry['members'] as $cn){
827         $cn= preg_replace('/ \[.*$/', '', $cn);
828         $summary.= $cn.", ";
829       }
830     } else {
831       $summary.= _("ACL is valid for all users");
832     }
834     return (preg_replace('/, $/', '', $summary));
835   }
838   function loadAclEntry($new= FALSE)
839   {
840     /* New entry gets presets... */
841     if ($new){
842       $this->aclType= 'base';
843       $this->recipients= array();
844       $this->aclContents= array();
845     } else {
846       $acl= $this->gosaAclEntry[$this->currentIndex];
847       $this->aclType= $acl['type'];
848       $this->recipients= $acl['members'];
849       $this->aclContents= $acl['acl'];
850     }
852     $this->wasNewEntry= $new;
853   }
856   function aclPostHandler()
857   {
858     if (isset($_POST['save_acl'])){
859       $this->save();
860       return TRUE;
861     }
863     return FALSE;
864   }
867   function save()
868   {
869     /* Assemble ACL's */
870     $tmp_acl= array();
871     foreach ($this->gosaAclEntry as $prio => $entry){
872       $final= "";
873       $members= "";
874       if (isset($entry['members'])){
875         foreach ($entry['members'] as $key => $dummy){
876           $members.= base64_encode(preg_replace('/^.:/', '', $key)).',';
877         }
878       }
879       $final= $prio.":".$entry['type'].":".preg_replace('/,$/', '', $members);
881       /* ACL's if needed */
882       if ($entry['type'] != "reset" && $entry['type'] != "role"){
883         $acl= ":";
884         if (isset($entry['acl'])){
885           foreach ($entry['acl'] as $object => $contents){
887             /* Only save, if we've some contents in there... */
888             if (count($contents)){
889               $acl.= $object.";";
891               foreach($contents as $attr => $permission){
893                 /* First entry? Its the one for global settings... */
894                 if ($attr == '0'){
895                   $acl.= $permission;
896                 } else {
897                   $acl.= '#'.$attr.';'.$permission;
898                 }
900               }
901               $acl.= ',';
902             }
903             
904           }
905         }
906         $final.= preg_replace('/,$/', '', $acl);
907       }
909       $tmp_acl[]= $final;
910     } 
912     /* Call main method */
913     plugin::save();
915     /* Finally (re-)assign it... */
916     $this->attrs['gosaAclEntry']= $tmp_acl;
918     /* Remove acl from this entry if it is empty... */
919     if (!count($tmp_acl)){
920       /* Remove attribute */
921       if ($this->initially_was_account){
922         $this->attrs['gosaAclEntry']= array();
923       } else {
924         if (isset($this->attrs['gosaAclEntry'])){
925           unset($this->attrs['gosaAclEntry']);
926         }
927       }
929       /* Remove object class */
930       $this->attrs['objectClass']= array_remove_entries(array('gosaAcl'), $this->attrs['objectClass']);
931     }    
933     /* Do LDAP modifications */
934     $ldap= $this->config->get_ldap_link();
935     $ldap->cd($this->dn);
936     $this->cleanup();
937     $ldap->modify ($this->attrs);
939     show_ldap_error($ldap->get_error(), sprintf(_("Saving ACLs with dn '%s' failed."),$this->dn));
941     /* Refresh users ACLs */
942     $ui= get_userinfo();
943     $ui->loadACL();
944     $_SESSION['ui']= $ui;
945   }
948   function remove_from_parent()
949   {
950   }
954 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
955 ?>