Code

Updated plugins
[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 $roles= array();
25   var $groups= array();
26   var $recipients= array();
27   var $isContainer= FALSE;
28   var $currentIndex= 0;
29   var $wasNewEntry= FALSE;
30   var $ocMapping= array();
31   var $savedAclContents= array();
32   var $myAclObjects = array();
34   function acl ($config, $parent, $dn= NULL)
35   {
36     /* Include config object */
37     plugin::plugin($config, $dn);
39     /* Load ACL's */
40     $this->gosaAclEntry= array();
41     if (isset($this->attrs['gosaAclEntry'])){
42       for ($i= 0; $i<$this->attrs['gosaAclEntry']['count']; $i++){
43         $acl= $this->attrs['gosaAclEntry'][$i];
44         $this->gosaAclEntry= array_merge($this->gosaAclEntry, $this->explodeACL($acl));
45       }
46     }
47     ksort($this->gosaAclEntry);
49     /* Save parent - we've to know more about it than other plugins... */
50     $this->parent= $parent;
52     /* Container? */
53     if (preg_match('/^(o|ou|c|l|dc)=/i', $dn)){
54       $this->isContainer= TRUE;
55     }
57     /* Users */
58     $ui= get_userinfo();
59     $tag= $ui->gosaUnitTag;
60     $ldap= $config->get_ldap_link();
61     $ldap->cd($config->current['BASE']);
62     if ($tag == ""){
63       $ldap->search('(objectClass=gosaAccount)', array('uid', 'cn'));
64     } else {
65       $ldap->search('(&(objectClass=gosaAccount)(gosaUnitTag='.$tag.'))', array('uid', 'cn'));
66     }
67     while ($attrs= $ldap->fetch()){
68       $this->users['U:'.$attrs['dn']]= $attrs['cn'][0].' ['.$attrs['uid'][0].']';
69     }
70     ksort($this->users);
72     /* Groups */
73     $ldap->cd($config->current['BASE']);
74     if ($tag == ""){
75       $ldap->search('(objectClass=posixGroup)', array('cn', 'description'));
76     } else {
77       $ldap->search('(&(objectClass=posixGroup)(gosaUnitTag='.$tag.'))', array('cn', 'description'));
78     }
79     while ($attrs= $ldap->fetch()){
80       $dsc= "";
81       if (isset($attrs['description'][0])){
82         $dsc= $attrs['description'][0];
83       }
84       $this->groups['G:'.$attrs['dn']]= $attrs['cn'][0].' ['.$dsc.']';
85     }
86     ksort($this->groups);
88     /* Roles */
89     $ldap->cd($config->current['BASE']);
90     if ($tag == ""){
91       $ldap->search('(objectClass=gosaRole)', array('cn', 'description','gosaAclTemplate','dn'));
92     } else {
93       $ldap->search('(&(objectClass=gosaRole)(gosaUnitTag='.$tag.'))', array('cn', 'description','gosaAclTemplate','dn'));
94     }
95     while ($attrs= $ldap->fetch()){
96       $dsc= "";
97       if (isset($attrs['description'][0])){
98         $dsc= $attrs['description'][0];
99       }
101       $role_id = $attrs['dn'];
103       $this->roles[$role_id]['acls'] =array();
104       for ($i= 0; $i < $attrs['gosaAclTemplate']['count']; $i++){
105         $acl= $attrs['gosaAclTemplate'][$i];
106         $this->roles[$role_id]['acls'] = array_merge($this->roles[$role_id]['acls'],$this->explodeACL($acl));
107       }
108       $this->roles[$role_id]['description'] = $dsc;
109       $this->roles[$role_id]['cn'] = $attrs['cn'][0];
110     }
112     /* Objects */
113     $tmp= get_global('plist');
114     $plist= $tmp->info;
115     if (isset($this->parent) && $this->parent != NULL){
116       $oc= array();
117       foreach ($this->parent->by_object as $key => $obj){
118         $oc= array_merge($oc, $obj->objectclasses);
119       }
120       if (in_array_ics('organizationalUnit', $oc)){
121         $this->isContainer= TRUE;
122       }
123     } else {
124       $oc=  $this->attrs['objectClass'];
125     }
128     /* Extract available categories from plugin info list */
129     foreach ($plist as $class => $acls){
131       /* Only feed categories */
132       if (isset($acls['plCategory'])){
134         /* Walk through supplied list and feed only translated categories */
135         foreach($acls['plCategory'] as $idx => $data){
137           /* Non numeric index means -> base object containing more informations */
138           if (preg_match('/^[0-9]+$/', $idx)){
139             if (!isset($this->ocMapping[$data])){
140               $this->ocMapping[$data]= array();
141               $this->ocMapping[$data][]= '0';
142             }
143             $this->ocMapping[$data][]= $class;
144           } else {
145             if (!isset($this->ocMapping[$idx])){
146               $this->ocMapping[$idx]= array();
147               $this->ocMapping[$idx][]= '0';
148             }
149             $this->ocMapping[$idx][]= $class;
150             $this->aclObjects[$idx]= $data['description'];
152             /* Additionally filter the classes we're interested in in "self edit" mode */
153             if (is_array($data['objectClass'])){
154               foreach($data['objectClass'] as $objectClass){
155                 if (in_array_ics($objectClass, $oc)){
156                   $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
157                   break;
158                 }
159               }
160             } else {
161               if (in_array_ics($data['objectClass'], $oc)){
162                 $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
163               }
164             }
165           }
167         }
168       }
169     }
170     $this->aclObjects['all']= '*&nbsp;'._("All categories");
171     $this->ocMapping['all']= array('0' => 'all');
173     /* Sort categories */
174     asort($this->aclObjects);
176     /* Fill acl types */
177     if ($this->isContainer){
178       $this->aclTypes= array("reset" => _("Reset ACLs"),
179                              "one" => _("One level"),
180                              "base" => _("Current object"),
181                              "sub" => _("Complete subtree"),
182                              "psub" => _("Complete subtree (permanent)"),
183                              "role" => _("Use ACL defined in role"));
184     } else {
185       $this->aclTypes= array("base" => _("Current object"),
186           "role" => _("Use ACL defined in role"));
187     }
188     asort($this->aclTypes);
189     $this->targets= array("user" => _("Users"), "group" => _("Groups"));
190     asort($this->targets);
192     /* Finally - we want to get saved... */
193     $this->is_account= TRUE;
194   }
197   function execute()
198   {
199     /* Call parent execute */
200     plugin::execute();
201   
202     $tmp= get_global('plist');
203     $plist= $tmp->info;
205     /* Handle posts */
206     if (isset($_POST['new_acl'])){
207       $this->dialogState= 'create';
208       $this->dialog= TRUE;
209       $this->currentIndex= count($this->gosaAclEntry);
210       $this->loadAclEntry(TRUE);
211     }
213     $new_acl= array();
214     $aclDialog= FALSE;
215     $firstedit= FALSE;
216     foreach($_POST as $name => $post){
218       /* Actions... */
219       if (preg_match('/^acl_edit_.*_x/', $name)){
220         $this->dialogState= 'create';
221         $firstedit= TRUE;
222         $this->dialog= TRUE;
223         $this->currentIndex= preg_replace('/^acl_edit_([0-9]+).*$/', '\1', $name);
224         $this->loadAclEntry();
225         continue;
226       }
227       if (preg_match('/^acl_del_.*_x/', $name)){
228         unset($this->gosaAclEntry[preg_replace('/^acl_del_([0-9]+).*$/', '\1', $name)]);
229         continue;
230       }
232       if (preg_match('/^cat_edit_.*_x/', $name)){
233         $this->aclObject= preg_replace('/^cat_edit_([^_]+)_.*$/', '\1', $name);
234         $this->dialogState= 'edit';
235         foreach ($this->ocMapping[$this->aclObject] as $oc){
236           if (isset($this->aclContents[$oc])){
237             $this->savedAclContents[$oc]= $this->aclContents[$oc];
238           }
239         }
240         continue;
241       }
242       if (preg_match('/^cat_del_.*_x/', $name)){
243         $idx= preg_replace('/^cat_del_([^_]+)_.*$/', '\1', $name);
244         foreach ($this->ocMapping[$idx] as $key){
245           unset($this->aclContents["$idx/$key"]);
246         }
247         continue;
248       }
250       /* Sorting... */
251       if (preg_match('/^sortup_.*_x/', $name)){
252         $index= preg_replace('/^sortup_([0-9]+).*$/', '\1', $name);
253         if ($index > 0){
254           $tmp= $this->gosaAclEntry[$index];
255           $this->gosaAclEntry[$index]= $this->gosaAclEntry[$index-1];
256           $this->gosaAclEntry[$index-1]= $tmp;
257         }
258         continue;
259       }
260       if (preg_match('/^sortdown_.*_x/', $name)){
261         $index= preg_replace('/^sortdown_([0-9]+).*$/', '\1', $name);
262         if ($index < count($this->gosaAclEntry)-1){
263           $tmp= $this->gosaAclEntry[$index];
264           $this->gosaAclEntry[$index]= $this->gosaAclEntry[$index+1];
265           $this->gosaAclEntry[$index+1]= $tmp;
266         }
267         continue;
268       }
270       /* ACL saving... */
271       if (preg_match('/^acl_.*_[^xy]$/', $name)){
272         $aclDialog= TRUE;
273         list($dummy, $object, $attribute, $value)= split('_', $name);
275         /* Skip for detection entry */
276         if ($object == 'dummy') {
277           continue;
278         }
280         /* Ordinary ACLs */
281         if (!isset($new_acl[$object])){
282           $new_acl[$object]= array();
283         }
284         if (isset($new_acl[$object][$attribute])){
285           $new_acl[$object][$attribute].= $value;
286         } else {
287           $new_acl[$object][$attribute]= $value;
288         }
289       }
291       if(isset($_POST['selected_role'])){
292         $this->aclContents = "";
293         $this->aclContents = base64_decode($_POST['selected_role']);
294       }
295     }
296     
297     /* Only be interested in new acl's, if we're in the right _POST place */
298     if ($aclDialog && $this->aclObject != "" && is_array($this->ocMapping[$this->aclObject])){
300       foreach ($this->ocMapping[$this->aclObject] as $oc){
301         unset($this->aclContents[$oc]);
302         unset($this->aclContents[$this->aclObject.'/'.$oc]);
303         if (isset($new_acl[$oc])){
304           $this->aclContents[$oc]= $new_acl[$oc];
305         }
306         if (isset($new_acl[$this->aclObject.'/'.$oc])){
307           $this->aclContents[$this->aclObject.'/'.$oc]= $new_acl[$this->aclObject.'/'.$oc];
308         }
309       }
310     }
312     /* Save new acl in case of base edit mode */
313     if ($this->aclType == 'base' && !$firstedit){
314       $this->aclContents= $new_acl;
315     }
317     /* Cancel new acl? */
318     if (isset($_POST['cancel_new_acl'])){
319       $this->dialogState= 'head';
320       $this->dialog= FALSE;
321       if ($this->wasNewEntry){
322         unset ($this->gosaAclEntry[$this->currentIndex]);
323       }
324     }
326     /* Store ACL in main object? */
327     if (isset($_POST['submit_new_acl'])){
328       $this->gosaAclEntry[$this->currentIndex]['type']= $this->aclType;
329       $this->gosaAclEntry[$this->currentIndex]['members']= $this->recipients;
330       $this->gosaAclEntry[$this->currentIndex]['acl']= $this->aclContents;
331       $this->dialogState= 'head';
332       $this->dialog= FALSE;
333     }
335     /* Cancel edit acl? */
336     if (isset($_POST['cancel_edit_acl'])){
337       $this->dialogState= 'create';
338       foreach ($this->ocMapping[$this->aclObject] as $oc){
339         if (isset($this->savedAclContents[$oc])){
340           $this->aclContents[$oc]= $this->savedAclContents[$oc];
341         }
342       }
343     }
345     /* Save edit acl? */
346     if (isset($_POST['submit_edit_acl'])){
347       $this->dialogState= 'create';
348     }
350     /* Add acl? */
351     if (isset($_POST['add_acl']) && $_POST['aclObject'] != ""){
352       $this->dialogState= 'edit';
353       $this->savedAclContents= array();
354       foreach ($this->ocMapping[$this->aclObject] as $oc){
355         if (isset($this->aclContents[$oc])){
356           $this->savedAclContents[$oc]= $this->aclContents[$oc];
357         }
358       }
359     }
361     /* Add to list? */
362     if (isset($_POST['add']) && isset($_POST['source'])){
363       foreach ($_POST['source'] as $key){
364         if ($this->target == 'user'){
365           $this->recipients[$key]= $this->users[$key];
366         }
367         if ($this->target == 'group'){
368           $this->recipients[$key]= $this->groups[$key];
369         }
370       }
371       ksort($this->recipients);
372     }
374     /* Remove from list? */
375     if (isset($_POST['del']) && isset($_POST['recipient'])){
376       foreach ($_POST['recipient'] as $key){
377           unset($this->recipients[$key]);
378       }
379     }
381     /* Save common values */
382     foreach (array("aclType", "aclObject", "target") as $key){
383       if (isset($_POST[$key])){
384         $this->$key= validate($_POST[$key]);
385       }
386     }
388     /* Create templating instance */
389     $smarty= get_smarty();
391     if ($this->dialogState == 'head'){
392       /* Draw list */
393       $aclList= new DivSelectBox("aclList");
394       $aclList->SetHeight(450);
395       
396       /* Fill in entries */
397       foreach ($this->gosaAclEntry as $key => $entry){
398         $field1= array("string" => $this->aclTypes[$entry['type']], "attach" => "style='width:150px'");
399         $field2= array("string" => $this->assembleAclSummary($entry));
400         $action= "<input type='image' name='sortup_$key' alt='up' title='"._("Up")."' src='images/sort_up.png' align='top'>";
401         $action.= "<input type='image' name='sortdown_$key' alt='down' title='"._("Down")."' src='images/sort_down.png'>";
402         $action.= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."' name='acl_edit_$key' title='"._("Edit ACL")."'>";
403         $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."' name='acl_del_$key' title='"._("Delete ACL")."'>";
405         $field3= array("string" => $action, "attach" => "style='border-right:0px;width:50px;text-align:right;'");
406         $aclList->AddEntry(array($field1, $field2, $field3));
407       }
409       $smarty->assign("aclList", $aclList->DrawList());
410     }
412     if ($this->dialogState == 'create'){
413       /* Draw list */
414       $aclList= new DivSelectBox("aclList");
415       $aclList->SetHeight(150);
417       /* Add settings for all categories to the (permanent) list */
418       foreach ($this->aclObjects as $section => $dsc){
419         $summary= "";
420         foreach($this->ocMapping[$section] as $oc){
421           if (isset($this->aclContents[$oc]) && count($this->aclContents[$oc]) && isset($this->aclContents[$oc][0]) &&
422               $this->aclContents[$oc][0] != ""){
424             $summary.= "$oc, ";
425             continue;
426           }
427           if (isset($this->aclContents["$section/$oc"]) && count($this->aclContents["$section/$oc"]) && isset($this->aclContents["$section/$oc"][0]) &&
428               $this->aclContents["$section/$oc"][0] != ""){
430             $summary.= "$oc, ";
431             continue;
432           }
433           if (isset($this->aclContents[$oc]) && !isset($this->aclContents[$oc][0]) && count($this->aclContents[$oc])){
434             $summary.= "$oc, ";
435           }
436         }
438         /* Set summary... */
439         if ($summary == ""){
440           $summary= '<i>'._("No ACL settings for this category").'</i>';
441         } else {
442           $summary= sprintf(_("Contains ACLs for these objects: %s"), preg_replace('/, $/', '', $summary));
443         }
445         $field1= array("string" => $dsc, "attach" => "style='width:100px'");
446         $field2= array("string" => $summary);
447         $action= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."' name='cat_edit_$section' title='"._("Edit categories ACLs")."'>";
448         $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."' name='cat_del_$section' title='"._("Clear categories ACLs")."'>";
449         $field3= array("string" => $action, "attach" => "style='border-right:0px;width:50px'");
450         $aclList->AddEntry(array($field1, $field2, $field3));
451       }
453       $smarty->assign("aclList", $aclList->DrawList());
454       $smarty->assign("aclType", $this->aclType);
455       $smarty->assign("aclTypes", $this->aclTypes);
456       $smarty->assign("target", $this->target);
457       $smarty->assign("targets", $this->targets);
459       /* Assign possible target types */
460       $smarty->assign("targets", $this->targets);
461       foreach ($this->attributes as $attr){
462         $smarty->assign($attr, $this->$attr);
463       }
466       /* Generate list */
467       $tmp= array();
468       foreach (array("user" => "users", "group" => "groups") as $field => $arr){
469         if ($this->target == $field){
470           foreach ($this->$arr as $key => $value){
471             if (!isset($this->recipients[$key])){
472               $tmp[$key]= $value;
473             }
474           }
475         }
476       }
477       $smarty->assign('sources', $tmp);
478       $smarty->assign('recipients', $this->recipients);
480       /* Acl selector if scope is base */
481       if ($this->aclType == 'base'){
482         $smarty->assign('aclSelector', $this->buildAclSelector($this->myAclObjects));
483       }
485       /* Role selector if scope is base */
486       if ($this->aclType == 'role'){
487         $smarty->assign('roleSelector', "Role selector");#, $this->buildRoleSelector($this->myAclObjects));
488         $smarty->assign('roleSelector', $this->buildRoleSelector($this->roles));
489       }
490     }
492     if ($this->dialogState == 'edit'){
493       $smarty->assign('headline', sprintf(_("Edit ACL for '%s', scope is '%s'"), $this->aclObjects[$this->aclObject], $this->aclTypes[$this->aclType]));
495       /* Collect objects for selected category */
496       foreach ($this->ocMapping[$this->aclObject] as $idx => $class){
497         if ($idx == 0){
498           continue;
499         }
500         $aclObjects[$this->aclObject.'/'.$class]= $plist[$class]['plDescription'];
501       }
502       if ($this->aclObject == 'all'){
503         $aclObjects['all']= _("All objects in current subtree");
504       }
506       /* Role selector if scope is base */
507       if ($this->aclType == 'role'){
508         $smarty->assign('roleSelector', $this->buildRoleSelector($this->roles));
509       } else {
510         $smarty->assign('aclSelector', $this->buildAclSelector($aclObjects));
511       }
512     }
514     /* Show main page */
515     $smarty->assign("dialogState", $this->dialogState);
517     return ($smarty->fetch (get_template_path('acl.tpl')));
518   }
521   function sort_by_priority($list)
522   {
523     $tmp= get_global('plist');
524     $plist= $tmp->info;
525     asort($plist);
526     $newSort = array();
528     foreach($list as $name => $translation){
529       $na  =  preg_replace("/^.*\//","",$name);
530       $prio = 0;
531       if(isset($plist[$na]['plPriority'])){
532         $prio=  $plist[$na]['plPriority'] ;
533       }
535       $newSort[$name] = $prio;
536     }
538     asort($newSort);
540     $ret = array();
541     foreach($newSort as $name => $prio){
542       $ret[$name] = $list[$name];
543     }
544     return($ret);
545   }
548   function buildRoleSelector($list)
549   {
550     $D_List =new DivSelectBox("Acl_Roles");
551  
552     $selected = $this->aclContents;
553     if(!is_string($this->aclContents) || !isset($list[$this->aclContents])){
554       $selected = key($list);
555     }
557     $str ="";
558     foreach($list as $dn => $values){
560       if($dn == $selected){    
561         $option = "<input type='radio' name='selected_role' value='".base64_encode($dn)."' checked>";
562       }else{
563         $option = "<input type='radio' name='selected_role' value='".base64_encode($dn)."'>";
564       }
565  
566       $field1 = array("string" => $option) ;
567       $field2 = array("string" => $values['cn'], "attach" => "style='width:200px;'") ;
568       $field3 = array("string" => $values['description'],"attach" => "style='border-right:0px;'") ;
570       $D_List->AddEntry(array($field1,$field2,$field3));
571     }
572     return($D_List->DrawList());
573   } 
576   function buildAclSelector($list)
577   {
578     $display= "<input type='hidden' name='acl_dummy_0_0_0' value='1'>";
579     $cols= 3;
580     $tmp= get_global('plist');
581     $plist= $tmp->info;
582     asort($plist);
584     /* Add select all/none buttons */
585     $style = "style='width:100px;'";
587     $display .= "<input ".$style." type='button' name='toggle_all_create' onClick=\"acl_toggle_all('_0_c$');\" value='Toggle C'>";
588     $display .= "<input ".$style." type='button' name='toggle_all_move'   onClick=\"acl_toggle_all('_0_m$');\" value='Toggle M'>";
589     $display .= "<input ".$style." type='button' name='toggle_all_remove' onClick=\"acl_toggle_all('_0_d$');\" value='Toggle D'> - ";
590     $display .= "<input ".$style." type='button' name='toggle_all_read'   onClick=\"acl_toggle_all('_0_r$');\" value='Toggle R'>";
591     $display .= "<input ".$style." type='button' name='toggle_all_write'  onClick=\"acl_toggle_all('_0_w$');\" value='Toggle W'> - ";
592     
593     $display .= "<input ".$style." type='button' name='toggle_all_sub_read'  onClick=\"acl_toggle_all('[^0]_r$');\" value='R+'>";
594     $display .= "<input ".$style." type='button' name='toggle_all_sub_write'  onClick=\"acl_toggle_all('[^0]_w$');\" value='W+'>";
595   
596     $display .= "<br>";
597   
598     $style = "style='width:50px;'";
599     $display .= "<input ".$style." type='button' name='set_true_all_create' onClick=\"acl_set_all('_0_c$',true);\" value='C+'>";
600     $display .= "<input ".$style." type='button' name='set_false_all_create' onClick=\"acl_set_all('_0_c$',false);\" value='C-'>";
601     $display .= "<input ".$style." type='button' name='set_true_all_move' onClick=\"acl_set_all('_0_m$',true);\" value='M+'>";
602     $display .= "<input ".$style." type='button' name='set_false_all_move' onClick=\"acl_set_all('_0_m$',false);\" value='M-'>";
603     $display .= "<input ".$style." type='button' name='set_true_all_remove' onClick=\"acl_set_all('_0_d$',true);\" value='D+'>";
604     $display .= "<input ".$style." type='button' name='set_false_all_remove' onClick=\"acl_set_all('_0_d$',false);\" value='D-'> - ";
605     $display .= "<input ".$style." type='button' name='set_true_all_read' onClick=\"acl_set_all('_0_r$',true);\" value='R+'>";
606     $display .= "<input ".$style." type='button' name='set_false_all_read' onClick=\"acl_set_all('_0_r$',false);\" value='R-'>";
607     $display .= "<input ".$style." type='button' name='set_true_all_write' onClick=\"acl_set_all('_0_w$',true);\" value='W+'>";
608     $display .= "<input ".$style." type='button' name='set_false_all_write' onClick=\"acl_set_all('_0_w$',false);\" value='W-'> - ";
610     $display .= "<input ".$style." type='button' name='set_true_all_read' onClick=\"acl_set_all('[^0]_r$',true);\" value='R+'>";
611     $display .= "<input ".$style." type='button' name='set_false_all_read' onClick=\"acl_set_all('[^0]_r$',false);\" value='R-'>";
612     $display .= "<input ".$style." type='button' name='set_true_all_write' onClick=\"acl_set_all('[^0]_w$',true);\" value='W+'>";
613     $display .= "<input ".$style." type='button' name='set_false_all_write' onClick=\"acl_set_all('[^0]_w$',false);\" value='W-'>";
615     /* Build general objects */
616     $list =$this->sort_by_priority($list);
617     foreach ($list as $key => $name){
619       /* Create sub acl if it does not exist */
620       if (!isset($this->aclContents[$key])){
621         $this->aclContents[$key]= array();
622         $this->aclContents[$key][0]= '';
623       }
624       $currentAcl= $this->aclContents[$key];
626       /* Object header */
627       if($_SESSION['js']) {
628         if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) {
629           $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
630                      "\n  <tr>".
631                      "\n    <td style='background-color:#C8C8C8;height:1.8em;' colspan=".($cols-1)."><b>"._("Object").": $name</b></td>".
632                      "\n    <td align='right' style='background-color:#C8C8C8;height:1.8em;'>".
633                      "\n    <input type='button' onclick='divtoggle(\"".preg_replace("/[^a-z0-9]/i","_",$name)."\");' value='"._("Show/Hide Advanced Settings")."' /></td>".
634                      "\n  </tr>";
635         } else if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT'])) {
636           $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
637                      "\n  <tr>".
638                      "\n    <td style='background-color:#C8C8C8;height:1.8em;' colspan=".($cols-1)."><b>"._("Object").": $name</b></td>".
639                      "\n    <td align='right' style='background-color:#C8C8C8;height:1.8em;'>".
640                      "\n    <input type='button' onclick='divtoggle(\"".preg_replace("/[^a-z0-9]/i","_",$name)."\");' value='"._("Show/Hide Advanced Settings")."' /></td>".
641                      "\n  </tr>";
642         } else {
643           $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
644                      "\n  <tr>".
645                      "\n    <td style='background-color:#C8C8C8;height:1.8em;' colspan=$cols><b>"._("Object").": $name</b></td>".
646                      "\n  </tr>";
647         }
648       } else {
649           $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
650                      "\n  <tr>".
651                      "\n    <td style='background-color:#C8C8C8;height:1.8em;' colspan=$cols><b>"._("Object").": $name</b></td>".
652                      "\n  </tr>";
653       }
655       /* Generate options */
656       $spc= "&nbsp;&nbsp;";
657       if ($this->isContainer && $this->aclType != 'base'){
658         $options= $this->mkchkbx($key."_0_c",  _("Create objects"), preg_match('/c/', $currentAcl[0])).$spc;
659         $options.= $this->mkchkbx($key."_0_m", _("Move objects"), preg_match('/m/', $currentAcl[0])).$spc;
660         $options.= $this->mkchkbx($key."_0_d", _("Remove objects"), preg_match('/d/', $currentAcl[0])).$spc;
661         if ($plist[preg_replace('%^.*/%', '', $key)]['plSelfModify']){
662           $options.= $this->mkchkbx($key."_0_s", _("Modifyable by owner"), preg_match('/s/', $currentAcl[0])).$spc;
663         }
664       } else {
665         $options= $this->mkchkbx($key."_0_m", _("Move object"), preg_match('/m/', $currentAcl[0])).$spc;
666         $options.= $this->mkchkbx($key."_0_d", _("Remove object"), preg_match('/d/', $currentAcl[0])).$spc;
667         if ($plist[preg_replace('%^.*/%', '', $key)]['plSelfModify']){
668           $options.= $this->mkchkbx($key."_0_s", _("Modifyable by owner"), preg_match('/s/', $currentAcl[0])).$spc;
669         }
670       }
672       /* Global options */
673       $more_options= $this->mkchkbx($key."_0_r",  _("read"), preg_match('/r/', $currentAcl[0])).$spc;
674       $more_options.= $this->mkchkbx($key."_0_w", _("write"), preg_match('/w/', $currentAcl[0]));
676       $display.= "\n  <tr>".
677                  "\n    <td style='background-color:#E0E0E0' colspan=".($cols-1).">$options</td>".
678                  "\n    <td style='background-color:#D4D4D4'>&nbsp;"._("Complete object").": $more_options</td>".
679                  "\n  </tr>";
681       /* Walk through the list of attributes */
682       $cnt= 1;
683       $splist= $plist[preg_replace('%^.*/%', '', $key)]['plProvidedAcls'];
684       asort($splist);
685       if($_SESSION['js']) {
686         if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) {
687           $display.= "\n  <tr id='tr_".preg_replace("/[^a-z0-9]/i","_",$name)."' style='vertical-align:top;height:0px;'>".
688                      "\n    <td colspan=".$cols.">".
689                      "\n      <div id='".preg_replace("/[^a-z0-9]/i","_",$name)."' style='overflow:hidden;visibility:hidden;height:0px;vertical-align:top;width:100%;'>".
690                      "\n        <table style='width:100%;'>";
691         } else if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT'])) {
692           $display.= "\n  <tr id='tr_".preg_replace("/[^a-z0-9]/i","_",$name)."' style='vertical-align:top;height:0px;'>".
693                      "\n    <td colspan=".$cols.">".
694                      "\n      <div id='".preg_replace("/[^a-z0-9]/i","_",$name)."' style='position:absolute;overflow:hidden;visibility:hidden;height:0px;vertical-align:top;width:100%;'>".
695                      "\n        <table style='width:100%;'>";
696         }
697       }
698       foreach($splist as $attr => $dsc){
700         /* Skip pl* attributes, they are internal... */
701         if (preg_match('/^pl[A-Z]+.*$/', $attr)){
702           continue;
703         }
705         /* Open table row */
706         if ($cnt == 1){
707           $display.= "\n  <tr>";
708         }
710         /* Close table row */
711         if ($cnt == $cols){
712           $cnt= 1;
713           $rb= "";
714           $end= "\n  </tr>";
715         } else {
716           $cnt++;
717           $rb= "border-right:1px solid #A0A0A0;";
718           $end= "";
719         }
721         /* Collect list of attributes */
722         $state= "";
723         if (isset($currentAcl[$attr])){
724           $state= $currentAcl[$attr];
725         }
726         $display.= "\n    <td style='border-top:1px solid #A0A0A0;${rb}width:".(int)(100/$cols)."%'>".
727                    "\n      <b>$dsc</b> ($attr)<br>".$this->mkrwbx($key."_".$attr, $state)."</td>$end";
728       }
729       
730       /* Fill missing td's if needed */
731       if (--$cnt != $cols && $cnt != 0){
732        $display.= str_repeat("\n    <td style='border-top:1px solid #A0A0A0; width:".(int)(100/$cols)."%'>&nbsp;</td>", $cols-$cnt); 
733       }
735       if($_SESSION['js']) {
736         if(isset($_SERVER['HTTP_USER_AGENT']) && (preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) || (preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT']))) {
737           $display.= "\n        </table>".
738                      "\n      </div>".
739                      "\n    </td>".
740                      "\n  </tr>";
741         }
742       }
744       $display.= "\n</table><br />\n";
745     }
747     return ($display);
748   }
751   function mkchkbx($name, $text, $state= FALSE)
752   {
753     $state= $state?"checked":"";
754     return "\n      <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."' type=checkbox name='acl_$name' $state>".
755            "\n      <label for='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."'>$text</label>";
756   }
759   function mkrwbx($name, $state= "")
760   {
761     $rstate= preg_match('/r/', $state)?'checked':'';
762     $wstate= preg_match('/w/', $state)?'checked':'';
763     return ("\n      <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_r' type=checkbox name='acl_${name}_r' $rstate>".
764             "\n      <label for='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_r'>"._("read")."</label>".
765             "\n      <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_w' type=checkbox name='acl_${name}_w' $wstate>".
766             "\n      <label for='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_w'>"._("write")."</label>");
767   }
770   function explodeACL($acl)
771   {
772     list($index, $type)= split(':', $acl);
773     $a= array( $index => array("type" => $type,
774                                "members" => acl::extractMembers($acl,$type == "role")));
775    
776     /* Handle different types */
777     switch ($type){
779       case 'psub':
780       case 'sub':
781       case 'one':
782       case 'base':
783         $a[$index]['acl']= acl::extractACL($acl);
784         break;
785       
786       case 'role':
787         $a[$index]['acl']= base64_decode(preg_replace('/^[^:]+:[^:]+:([^:]+).*$/', '\1', $acl));
788         break;
790       case 'reset':
791         break;
792       
793       default:
794         print_red(sprintf(_("Unkown ACL type '%s'. Don't know how to handle it."), $type));
795         $a= array();
796     }
797     return ($a);
798   }
801   function extractMembers($acl,$role = FALSE)
802   {
803     global $config;
804     $a= array();
806     /* Rip acl off the string, seperate by ',' and place it in an array */
807     if($role){
808       $ms= preg_replace('/^[^:]+:[^:]+:[^:]+:([^:]+).*$/', '\1', $acl);
809     }else{
810       $ms= preg_replace('/^[^:]+:[^:]+:([^:]+).*$/', '\1', $acl);
811     }
812     if ($ms == $acl){
813       return $a;
814     }
815     $ma= split(',', $ms);
817     /* Decode dn's, fill with informations from LDAP */
818     $ldap= $config->get_ldap_link();
819     foreach ($ma as $memberdn){
820       $dn= base64_decode($memberdn);
821       $ldap->cat($dn, array('cn', 'objectClass', 'description', 'uid'));
823       /* Found entry... */
824       if ($ldap->count()){
825         $attrs= $ldap->fetch();
826         if (in_array_ics('gosaAccount', $attrs['objectClass'])){
827           $a['U:'.$dn]= $attrs['cn'][0]." [".$attrs['uid'][0]."]";
828         } else {
829           $a['G:'.$dn]= $attrs['cn'][0];
830           if (isset($attrs['description'][0])){
831             $a['G:'.$dn].= " [".$attrs['description'][0]."]";
832           }
833         }
835       /* ... or not */
836       } else {
837         $a['U:'.$dn]= sprintf(_("Unknown entry '%s'!"), $dn);
838       }
839     }
841     return ($a);
842   }
845   function extractACL($acl)
846   {
847     /* Rip acl off the string, seperate by ',' and place it in an array */
848     $as= preg_replace('/^[^:]+:[^:]+:[^:]*:(.*)$/', '\1', $acl);
849     $aa= split(',', $as);
850     $a= array();
852     /* Dis-assemble single ACLs */
853     foreach($aa as $sacl){
854       
855       /* Dis-assemble field ACLs */
856       $ao= split('#', $sacl);
857       $gobject= "";
858       foreach($ao as $idx => $ssacl){
860         /* First is department with global acl */
861         $object= preg_replace('/^([^;]+);.*$/', '\1', $ssacl);
862         $gacl=   preg_replace('/^[^;]+;(.*)$/', '\1', $ssacl);
863         if ($idx == 0){
864           /* Create hash for this object */
865           $gobject= $object;
866           $a[$gobject]= array();
868           /* Append ACL if set */
869           if ($gacl != ""){
870             $a[$gobject]= array($gacl);
871           }
872         } else {
874           /* All other entries get appended... */
875           list($field, $facl)= split(';', $ssacl);
876           $a[$gobject][$field]= $facl;
877         }
879       }
880     }
882     return ($a);
883   }
885   
886   function assembleAclSummary($entry)
887   {
888     $summary= "";
890     /* Summarize ACL */
891     if (isset($entry['acl'])){
892       $acl= "";
894       if($entry['type'] == "role"){
896         if(isset($this->roles[$entry['acl']])){  
897           $summary.= sprintf(_("Role: %s"), $this->roles[$entry['acl']]['cn']);
898         }else{
899           $summary.= sprintf(_("Role: %s"), "<i>"._("Unknown role, possibly removed")."</i>");
900         }
901       }else{
902         foreach ($entry['acl'] as $name => $object){
903           if (count($object)){
904             $acl.= "$name, ";
905           }
906         }
907         $summary.= sprintf(_("Contains settings for these objects: %s"), preg_replace('/, $/', '', $acl));
908       }
909     }
911     /* Summarize members */
912     if ($summary != ""){
913       $summary.= ", ";
914     }
915     if (count($entry['members'])){
916       $summary.= _("Members:")." ";
917       foreach ($entry['members'] as $cn){
918         $cn= preg_replace('/ \[.*$/', '', $cn);
919         $summary.= $cn.", ";
920       }
921     } else {
922       $summary.= _("ACL is valid for all users");
923     }
925     return (preg_replace('/, $/', '', $summary));
926   }
929   function loadAclEntry($new= FALSE)
930   {
931     /* New entry gets presets... */
932     if ($new){
933       $this->aclType= 'base';
934       $this->recipients= array();
935       $this->aclContents= array();
936     } else {
937       $acl= $this->gosaAclEntry[$this->currentIndex];
938       $this->aclType= $acl['type'];
939       $this->recipients= $acl['members'];
940       $this->aclContents= $acl['acl'];
941     }
943     $this->wasNewEntry= $new;
944   }
947   function aclPostHandler()
948   {
949     if (isset($_POST['save_acl'])){
950       $this->save();
951       return TRUE;
952     }
954     return FALSE;
955   }
958   function save()
959   {
960     /* Assemble ACL's */
961     $tmp_acl= array();
962     foreach ($this->gosaAclEntry as $prio => $entry){
963       $final= "";
964       $members= "";
965       if (isset($entry['members'])){
966         foreach ($entry['members'] as $key => $dummy){
967           $members.= base64_encode(preg_replace('/^.:/', '', $key)).',';
968         }
969       }
971       if($entry['type'] != "role"){
972         $final= $prio.":".$entry['type'].":".preg_replace('/,$/', '', $members);
973       }else{
974         $final= $prio.":".$entry['type'].":".base64_encode($entry['acl']).":".preg_replace('/,$/', '', $members);
975       }
977       /* ACL's if needed */
978       if ($entry['type'] != "reset" && $entry['type'] != "role"){
979         $acl= ":";
980         if (isset($entry['acl'])){
981           foreach ($entry['acl'] as $object => $contents){
983             /* Only save, if we've some contents in there... */
984             if (count($contents)){
985               $acl.= $object.";";
987               foreach($contents as $attr => $permission){
989                 /* First entry? Its the one for global settings... */
990                 if ($attr == '0'){
991                   $acl.= $permission;
992                 } else {
993                   $acl.= '#'.$attr.';'.$permission;
994                 }
996               }
997               $acl.= ',';
998             }
999             
1000           }
1001         }
1002         $final.= preg_replace('/,$/', '', $acl);
1003       }
1005       $tmp_acl[]= $final;
1006     } 
1008     /* Call main method */
1009     plugin::save();
1011     /* Finally (re-)assign it... */
1012     $this->attrs['gosaAclEntry']= $tmp_acl;
1014     /* Remove acl from this entry if it is empty... */
1015     if (!count($tmp_acl)){
1016       /* Remove attribute */
1017       if ($this->initially_was_account){
1018         $this->attrs['gosaAclEntry']= array();
1019       } else {
1020         if (isset($this->attrs['gosaAclEntry'])){
1021           unset($this->attrs['gosaAclEntry']);
1022         }
1023       }
1025       /* Remove object class */
1026       $this->attrs['objectClass']= array_remove_entries(array('gosaAcl'), $this->attrs['objectClass']);
1027     }    
1029     /* Do LDAP modifications */
1030     $ldap= $this->config->get_ldap_link();
1031     $ldap->cd($this->dn);
1032     $this->cleanup();
1033     $ldap->modify ($this->attrs);
1035     if(count($this->attrs)){
1036       new log("modify","acls/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1037     }
1039     show_ldap_error($ldap->get_error(), sprintf(_("Saving ACLs with dn '%s' failed."),$this->dn));
1041     /* Refresh users ACLs */
1042     $ui= get_userinfo();
1043     $ui->loadACL();
1044     $_SESSION['ui']= $ui;
1045   }
1048   function remove_from_parent()
1049   {
1050     plugin::remove_from_parent();
1052     /* include global link_info */
1053     $ldap= $this->config->get_ldap_link();
1055     $ldap->cd($this->dn);
1056     $this->cleanup();
1057     $ldap->modify ($this->attrs);
1059     new log("remove","acls/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1061     /* Optionally execute a command after we're done */
1062     $this->handle_post_events("remove",array("uid" => $this->uid));
1063   }
1067 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1068 ?>