Code

Updated client dns handling
[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     $cats = array();
116     if (isset($this->parent) && $this->parent != NULL){
117       $oc= array();
118       foreach ($this->parent->by_object as $key => $obj){
119         $oc= array_merge($oc, $obj->objectclasses);
120         if(isset($obj->acl_category)){
121           $cats[preg_replace("/\//","",$obj->acl_category)] = preg_replace("/\//","",$obj->acl_category);
122         }
123       }
124       if (in_array_ics('organizationalUnit', $oc)){
125         $this->isContainer= TRUE;
126       }
127     } else {
128       $oc=  $this->attrs['objectClass'];
129     }
131     /* Extract available categories from plugin info list */
132     foreach ($plist as $class => $acls){
134       /* Only feed categories */
135       if (isset($acls['plCategory'])){
137         /* Walk through supplied list and feed only translated categories */
138         foreach($acls['plCategory'] as $idx => $data){
140           /* Non numeric index means -> base object containing more informations */
141           if (preg_match('/^[0-9]+$/', $idx)){
142             if (!isset($this->ocMapping[$data])){
143               $this->ocMapping[$data]= array();
144               $this->ocMapping[$data][]= '0';
145             }
147             if(isset($cats[$data])){
148               $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
149             }
150             $this->ocMapping[$data][]= $class;
151           } else {
152             if (!isset($this->ocMapping[$idx])){
153               $this->ocMapping[$idx]= array();
154               $this->ocMapping[$idx][]= '0';
155             }
156             $this->ocMapping[$idx][]= $class;
157             $this->aclObjects[$idx]= $data['description'];
159             /* Additionally filter the classes we're interested in in "self edit" mode */
160             if (is_array($data['objectClass'])){
161               foreach($data['objectClass'] as $objectClass){
162                 if (in_array_ics($objectClass, $oc)){
163                   $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
164                   break;
165                 }
166               }
167             } else {
168               if (in_array_ics($data['objectClass'], $oc)){
169                 $this->myAclObjects[$idx.'/'.$class]= $acls['plDescription'];
170               }
171             }
172           }
174         }
175       }
176     }
177     $this->aclObjects['all']= '*&nbsp;'._("All categories");
178     $this->ocMapping['all']= array('0' => 'all');
180     /* Sort categories */
181     asort($this->aclObjects);
183     /* Fill acl types */
184     if ($this->isContainer){
185       $this->aclTypes= array("reset" => _("Reset ACLs"),
186                              "one" => _("One level"),
187                              "base" => _("Current object"),
188                              "sub" => _("Complete subtree"),
189                              "psub" => _("Complete subtree (permanent)"),
190                              "role" => _("Use ACL defined in role"));
191     } else {
192       $this->aclTypes= array("base" => _("Current object"),
193           "role" => _("Use ACL defined in role"));
194     }
195     asort($this->aclTypes);
196     $this->targets= array("user" => _("Users"), "group" => _("Groups"));
197     asort($this->targets);
199     /* Finally - we want to get saved... */
200     $this->is_account= TRUE;
201   }
204   function execute()
205   {
206     /* Call parent execute */
207     plugin::execute();
208   
209     $tmp= get_global('plist');
210     $plist= $tmp->info;
212     /* Handle posts */
213     if (isset($_POST['new_acl'])){
214       $this->dialogState= 'create';
215       $this->dialog= TRUE;
216       $this->currentIndex= count($this->gosaAclEntry);
217       $this->loadAclEntry(TRUE);
218     }
220     $new_acl= array();
221     $aclDialog= FALSE;
222     $firstedit= FALSE;
223     foreach($_POST as $name => $post){
225       /* Actions... */
226       if (preg_match('/^acl_edit_.*_x/', $name)){
227         $this->dialogState= 'create';
228         $firstedit= TRUE;
229         $this->dialog= TRUE;
230         $this->currentIndex= preg_replace('/^acl_edit_([0-9]+).*$/', '\1', $name);
231         $this->loadAclEntry();
232         continue;
233       }
234       if (preg_match('/^acl_del_.*_x/', $name)){
235         unset($this->gosaAclEntry[preg_replace('/^acl_del_([0-9]+).*$/', '\1', $name)]);
236         continue;
237       }
239       if (preg_match('/^cat_edit_.*_x/', $name)){
240         $this->aclObject= preg_replace('/^cat_edit_([^_]+)_.*$/', '\1', $name);
241         $this->dialogState= 'edit';
242         foreach ($this->ocMapping[$this->aclObject] as $oc){
243           if (isset($this->aclContents[$oc])){
244             $this->savedAclContents[$oc]= $this->aclContents[$oc];
245           }
246         }
247         continue;
248       }
249       if (preg_match('/^cat_del_.*_x/', $name)){
250         $idx= preg_replace('/^cat_del_([^_]+)_.*$/', '\1', $name);
251         foreach ($this->ocMapping[$idx] as $key){
252           unset($this->aclContents["$idx/$key"]);
253         }
254         continue;
255       }
257       /* Sorting... */
258       if (preg_match('/^sortup_.*_x/', $name)){
259         $index= preg_replace('/^sortup_([0-9]+).*$/', '\1', $name);
260         if ($index > 0){
261           $tmp= $this->gosaAclEntry[$index];
262           $this->gosaAclEntry[$index]= $this->gosaAclEntry[$index-1];
263           $this->gosaAclEntry[$index-1]= $tmp;
264         }
265         continue;
266       }
267       if (preg_match('/^sortdown_.*_x/', $name)){
268         $index= preg_replace('/^sortdown_([0-9]+).*$/', '\1', $name);
269         if ($index < count($this->gosaAclEntry)-1){
270           $tmp= $this->gosaAclEntry[$index];
271           $this->gosaAclEntry[$index]= $this->gosaAclEntry[$index+1];
272           $this->gosaAclEntry[$index+1]= $tmp;
273         }
274         continue;
275       }
277       /* ACL saving... */
278       if (preg_match('/^acl_.*_[^xy]$/', $name)){
279         $aclDialog= TRUE;
280         list($dummy, $object, $attribute, $value)= split('_', $name);
282         /* Skip for detection entry */
283         if ($object == 'dummy') {
284           continue;
285         }
287         /* Ordinary ACLs */
288         if (!isset($new_acl[$object])){
289           $new_acl[$object]= array();
290         }
291         if (isset($new_acl[$object][$attribute])){
292           $new_acl[$object][$attribute].= $value;
293         } else {
294           $new_acl[$object][$attribute]= $value;
295         }
296       }
298       if(isset($_POST['selected_role'])){
299         $this->aclContents = "";
300         $this->aclContents = base64_decode($_POST['selected_role']);
301       }
302     }
303     
304     /* Only be interested in new acl's, if we're in the right _POST place */
305     if ($aclDialog && $this->aclObject != "" && is_array($this->ocMapping[$this->aclObject])){
307       foreach ($this->ocMapping[$this->aclObject] as $oc){
308         unset($this->aclContents[$oc]);
309         unset($this->aclContents[$this->aclObject.'/'.$oc]);
310         if (isset($new_acl[$oc])){
311           $this->aclContents[$oc]= $new_acl[$oc];
312         }
313         if (isset($new_acl[$this->aclObject.'/'.$oc])){
314           $this->aclContents[$this->aclObject.'/'.$oc]= $new_acl[$this->aclObject.'/'.$oc];
315         }
316       }
317     }
319     /* Save new acl in case of base edit mode */
320     if ($this->aclType == 'base' && !$firstedit){
321       $this->aclContents= $new_acl;
322     }
324     /* Cancel new acl? */
325     if (isset($_POST['cancel_new_acl'])){
326       $this->dialogState= 'head';
327       $this->dialog= FALSE;
328       if ($this->wasNewEntry){
329         unset ($this->gosaAclEntry[$this->currentIndex]);
330       }
331     }
333     /* Store ACL in main object? */
334     if (isset($_POST['submit_new_acl'])){
335       $this->gosaAclEntry[$this->currentIndex]['type']= $this->aclType;
336       $this->gosaAclEntry[$this->currentIndex]['members']= $this->recipients;
337       $this->gosaAclEntry[$this->currentIndex]['acl']= $this->aclContents;
338       $this->dialogState= 'head';
339       $this->dialog= FALSE;
340     }
342     /* Cancel edit acl? */
343     if (isset($_POST['cancel_edit_acl'])){
344       $this->dialogState= 'create';
345       foreach ($this->ocMapping[$this->aclObject] as $oc){
346         if (isset($this->savedAclContents[$oc])){
347           $this->aclContents[$oc]= $this->savedAclContents[$oc];
348         }
349       }
350     }
352     /* Save edit acl? */
353     if (isset($_POST['submit_edit_acl'])){
354       $this->dialogState= 'create';
355     }
357     /* Add acl? */
358     if (isset($_POST['add_acl']) && $_POST['aclObject'] != ""){
359       $this->dialogState= 'edit';
360       $this->savedAclContents= array();
361       foreach ($this->ocMapping[$this->aclObject] as $oc){
362         if (isset($this->aclContents[$oc])){
363           $this->savedAclContents[$oc]= $this->aclContents[$oc];
364         }
365       }
366     }
368     /* Add to list? */
369     if (isset($_POST['add']) && isset($_POST['source'])){
370       foreach ($_POST['source'] as $key){
371         if ($this->target == 'user'){
372           $this->recipients[$key]= $this->users[$key];
373         }
374         if ($this->target == 'group'){
375           $this->recipients[$key]= $this->groups[$key];
376         }
377       }
378       ksort($this->recipients);
379     }
381     /* Remove from list? */
382     if (isset($_POST['del']) && isset($_POST['recipient'])){
383       foreach ($_POST['recipient'] as $key){
384           unset($this->recipients[$key]);
385       }
386     }
388     /* Save common values */
389     foreach (array("aclType", "aclObject", "target") as $key){
390       if (isset($_POST[$key])){
391         $this->$key= validate($_POST[$key]);
392       }
393     }
395     /* Create templating instance */
396     $smarty= get_smarty();
398     if ($this->dialogState == 'head'){
399       /* Draw list */
400       $aclList= new DivSelectBox("aclList");
401       $aclList->SetHeight(450);
402       
403       /* Fill in entries */
404       foreach ($this->gosaAclEntry as $key => $entry){
405         $field1= array("string" => $this->aclTypes[$entry['type']], "attach" => "style='width:150px'");
406         $field2= array("string" => $this->assembleAclSummary($entry));
407         $action= "<input type='image' name='sortup_$key' alt='up' title='"._("Up")."' src='images/sort_up.png' align='top'>";
408         $action.= "<input type='image' name='sortdown_$key' alt='down' title='"._("Down")."' src='images/sort_down.png'>";
409         $action.= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."' name='acl_edit_$key' title='"._("Edit ACL")."'>";
410         $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."' name='acl_del_$key' title='"._("Delete ACL")."'>";
412         $field3= array("string" => $action, "attach" => "style='border-right:0px;width:50px;text-align:right;'");
413         $aclList->AddEntry(array($field1, $field2, $field3));
414       }
416       $smarty->assign("aclList", $aclList->DrawList());
417     }
419     if ($this->dialogState == 'create'){
420       /* Draw list */
421       $aclList= new DivSelectBox("aclList");
422       $aclList->SetHeight(150);
424       /* Add settings for all categories to the (permanent) list */
425       foreach ($this->aclObjects as $section => $dsc){
426         $summary= "";
427         foreach($this->ocMapping[$section] as $oc){
428           if (isset($this->aclContents[$oc]) && count($this->aclContents[$oc]) && isset($this->aclContents[$oc][0]) &&
429               $this->aclContents[$oc][0] != ""){
431             $summary.= "$oc, ";
432             continue;
433           }
434           if (isset($this->aclContents["$section/$oc"]) && count($this->aclContents["$section/$oc"]) && isset($this->aclContents["$section/$oc"][0]) &&
435               $this->aclContents["$section/$oc"][0] != ""){
437             $summary.= "$oc, ";
438             continue;
439           }
440           if (isset($this->aclContents[$oc]) && !isset($this->aclContents[$oc][0]) && count($this->aclContents[$oc])){
441             $summary.= "$oc, ";
442           }
443         }
445         /* Set summary... */
446         if ($summary == ""){
447           $summary= '<i>'._("No ACL settings for this category").'</i>';
448         } else {
449           $summary= sprintf(_("Contains ACLs for these objects: %s"), preg_replace('/, $/', '', $summary));
450         }
452         $field1= array("string" => $dsc, "attach" => "style='width:100px'");
453         $field2= array("string" => $summary);
454         $action= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."' name='cat_edit_$section' title='"._("Edit categories ACLs")."'>";
455         $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."' name='cat_del_$section' title='"._("Clear categories ACLs")."'>";
456         $field3= array("string" => $action, "attach" => "style='border-right:0px;width:50px'");
457         $aclList->AddEntry(array($field1, $field2, $field3));
458       }
460       $smarty->assign("aclList", $aclList->DrawList());
461       $smarty->assign("aclType", $this->aclType);
462       $smarty->assign("aclTypes", $this->aclTypes);
463       $smarty->assign("target", $this->target);
464       $smarty->assign("targets", $this->targets);
466       /* Assign possible target types */
467       $smarty->assign("targets", $this->targets);
468       foreach ($this->attributes as $attr){
469         $smarty->assign($attr, $this->$attr);
470       }
473       /* Generate list */
474       $tmp= array();
475       foreach (array("user" => "users", "group" => "groups") as $field => $arr){
476         if ($this->target == $field){
477           foreach ($this->$arr as $key => $value){
478             if (!isset($this->recipients[$key])){
479               $tmp[$key]= $value;
480             }
481           }
482         }
483       }
484       $smarty->assign('sources', $tmp);
485       $smarty->assign('recipients', $this->recipients);
487       /* Acl selector if scope is base */
488       if ($this->aclType == 'base'){
489         $smarty->assign('aclSelector', $this->buildAclSelector($this->myAclObjects));
490       }
492       /* Role selector if scope is base */
493       if ($this->aclType == 'role'){
494         $smarty->assign('roleSelector', "Role selector");#, $this->buildRoleSelector($this->myAclObjects));
495         $smarty->assign('roleSelector', $this->buildRoleSelector($this->roles));
496       }
497     }
499     if ($this->dialogState == 'edit'){
500       $smarty->assign('headline', sprintf(_("Edit ACL for '%s', scope is '%s'"), $this->aclObjects[$this->aclObject], $this->aclTypes[$this->aclType]));
502       /* Collect objects for selected category */
503       foreach ($this->ocMapping[$this->aclObject] as $idx => $class){
504         if ($idx == 0){
505           continue;
506         }
507         $aclObjects[$this->aclObject.'/'.$class]= $plist[$class]['plDescription'];
508       }
509       if ($this->aclObject == 'all'){
510         $aclObjects['all']= _("All objects in current subtree");
511       }
513       /* Role selector if scope is base */
514       if ($this->aclType == 'role'){
515         $smarty->assign('roleSelector', $this->buildRoleSelector($this->roles));
516       } else {
517         $smarty->assign('aclSelector', $this->buildAclSelector($aclObjects));
518       }
519     }
521     /* Show main page */
522     $smarty->assign("dialogState", $this->dialogState);
524     return ($smarty->fetch (get_template_path('acl.tpl')));
525   }
528   function sort_by_priority($list)
529   {
530     $tmp= get_global('plist');
531     $plist= $tmp->info;
532     asort($plist);
533     $newSort = array();
535     foreach($list as $name => $translation){
536       $na  =  preg_replace("/^.*\//","",$name);
537       $prio = 0;
538       if(isset($plist[$na]['plPriority'])){
539         $prio=  $plist[$na]['plPriority'] ;
540       }
542       $newSort[$name] = $prio;
543     }
545     asort($newSort);
547     $ret = array();
548     foreach($newSort as $name => $prio){
549       $ret[$name] = $list[$name];
550     }
551     return($ret);
552   }
555   function buildRoleSelector($list)
556   {
557     $D_List =new DivSelectBox("Acl_Roles");
558  
559     $selected = $this->aclContents;
560     if(!is_string($this->aclContents) || !isset($list[$this->aclContents])){
561       $selected = key($list);
562     }
564     $str ="";
565     foreach($list as $dn => $values){
567       if($dn == $selected){    
568         $option = "<input type='radio' name='selected_role' value='".base64_encode($dn)."' checked>";
569       }else{
570         $option = "<input type='radio' name='selected_role' value='".base64_encode($dn)."'>";
571       }
572  
573       $field1 = array("string" => $option) ;
574       $field2 = array("string" => $values['cn'], "attach" => "style='width:200px;'") ;
575       $field3 = array("string" => $values['description'],"attach" => "style='border-right:0px;'") ;
577       $D_List->AddEntry(array($field1,$field2,$field3));
578     }
579     return($D_List->DrawList());
580   } 
583   function buildAclSelector($list)
584   {
585     $display= "<input type='hidden' name='acl_dummy_0_0_0' value='1'>";
586     $cols= 3;
587     $tmp= get_global('plist');
588     $plist= $tmp->info;
589     asort($plist);
591     /* Add select all/none buttons */
592     $style = "style='width:100px;'";
594     $display .= "<input ".$style." type='button' name='toggle_all_create' onClick=\"acl_toggle_all('_0_c$');\" value='Toggle C'>";
595     $display .= "<input ".$style." type='button' name='toggle_all_move'   onClick=\"acl_toggle_all('_0_m$');\" value='Toggle M'>";
596     $display .= "<input ".$style." type='button' name='toggle_all_remove' onClick=\"acl_toggle_all('_0_d$');\" value='Toggle D'> - ";
597     $display .= "<input ".$style." type='button' name='toggle_all_read'   onClick=\"acl_toggle_all('_0_r$');\" value='Toggle R'>";
598     $display .= "<input ".$style." type='button' name='toggle_all_write'  onClick=\"acl_toggle_all('_0_w$');\" value='Toggle W'> - ";
599     
600     $display .= "<input ".$style." type='button' name='toggle_all_sub_read'  onClick=\"acl_toggle_all('[^0]_r$');\" value='R+'>";
601     $display .= "<input ".$style." type='button' name='toggle_all_sub_write'  onClick=\"acl_toggle_all('[^0]_w$');\" value='W+'>";
602   
603     $display .= "<br>";
604   
605     $style = "style='width:50px;'";
606     $display .= "<input ".$style." type='button' name='set_true_all_create' onClick=\"acl_set_all('_0_c$',true);\" value='C+'>";
607     $display .= "<input ".$style." type='button' name='set_false_all_create' onClick=\"acl_set_all('_0_c$',false);\" value='C-'>";
608     $display .= "<input ".$style." type='button' name='set_true_all_move' onClick=\"acl_set_all('_0_m$',true);\" value='M+'>";
609     $display .= "<input ".$style." type='button' name='set_false_all_move' onClick=\"acl_set_all('_0_m$',false);\" value='M-'>";
610     $display .= "<input ".$style." type='button' name='set_true_all_remove' onClick=\"acl_set_all('_0_d$',true);\" value='D+'>";
611     $display .= "<input ".$style." type='button' name='set_false_all_remove' onClick=\"acl_set_all('_0_d$',false);\" value='D-'> - ";
612     $display .= "<input ".$style." type='button' name='set_true_all_read' onClick=\"acl_set_all('_0_r$',true);\" value='R+'>";
613     $display .= "<input ".$style." type='button' name='set_false_all_read' onClick=\"acl_set_all('_0_r$',false);\" value='R-'>";
614     $display .= "<input ".$style." type='button' name='set_true_all_write' onClick=\"acl_set_all('_0_w$',true);\" value='W+'>";
615     $display .= "<input ".$style." type='button' name='set_false_all_write' onClick=\"acl_set_all('_0_w$',false);\" value='W-'> - ";
617     $display .= "<input ".$style." type='button' name='set_true_all_read' onClick=\"acl_set_all('[^0]_r$',true);\" value='R+'>";
618     $display .= "<input ".$style." type='button' name='set_false_all_read' onClick=\"acl_set_all('[^0]_r$',false);\" value='R-'>";
619     $display .= "<input ".$style." type='button' name='set_true_all_write' onClick=\"acl_set_all('[^0]_w$',true);\" value='W+'>";
620     $display .= "<input ".$style." type='button' name='set_false_all_write' onClick=\"acl_set_all('[^0]_w$',false);\" value='W-'>";
622     /* Build general objects */
623     $list =$this->sort_by_priority($list);
624     foreach ($list as $key => $name){
626       /* Create sub acl if it does not exist */
627       if (!isset($this->aclContents[$key])){
628         $this->aclContents[$key]= array();
629         $this->aclContents[$key][0]= '';
630       }
631       $currentAcl= $this->aclContents[$key];
633       /* Object header */
634       if($_SESSION['js']) {
635         if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/gecko/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 if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT'])) {
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-1)."><b>"._("Object").": $name</b></td>".
646                      "\n    <td align='right' style='background-color:#C8C8C8;height:1.8em;'>".
647                      "\n    <input type='button' onclick='divtoggle(\"".preg_replace("/[^a-z0-9]/i","_",$name)."\");' value='"._("Show/Hide Advanced Settings")."' /></td>".
648                      "\n  </tr>";
649         } else {
650           $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
651                      "\n  <tr>".
652                      "\n    <td style='background-color:#C8C8C8;height:1.8em;' colspan=$cols><b>"._("Object").": $name</b></td>".
653                      "\n  </tr>";
654         }
655       } else {
656           $display.= "\n<table style='width:100%;border:1px solid #A0A0A0' cellspacing=0 cellpadding=2>".
657                      "\n  <tr>".
658                      "\n    <td style='background-color:#C8C8C8;height:1.8em;' colspan=$cols><b>"._("Object").": $name</b></td>".
659                      "\n  </tr>";
660       }
662       /* Generate options */
663       $spc= "&nbsp;&nbsp;";
664       if ($this->isContainer && $this->aclType != 'base'){
665         $options= $this->mkchkbx($key."_0_c",  _("Create objects"), preg_match('/c/', $currentAcl[0])).$spc;
666         $options.= $this->mkchkbx($key."_0_m", _("Move objects"), preg_match('/m/', $currentAcl[0])).$spc;
667         $options.= $this->mkchkbx($key."_0_d", _("Remove objects"), preg_match('/d/', $currentAcl[0])).$spc;
668         if ($plist[preg_replace('%^.*/%', '', $key)]['plSelfModify']){
669           $options.= $this->mkchkbx($key."_0_s", _("Modifyable by owner"), preg_match('/s/', $currentAcl[0])).$spc;
670         }
671       } else {
672         $options= $this->mkchkbx($key."_0_m", _("Move object"), preg_match('/m/', $currentAcl[0])).$spc;
673         $options.= $this->mkchkbx($key."_0_d", _("Remove object"), preg_match('/d/', $currentAcl[0])).$spc;
674         if ($plist[preg_replace('%^.*/%', '', $key)]['plSelfModify']){
675           $options.= $this->mkchkbx($key."_0_s", _("Modifyable by owner"), preg_match('/s/', $currentAcl[0])).$spc;
676         }
677       }
679       /* Global options */
680       $more_options= $this->mkchkbx($key."_0_r",  _("read"), preg_match('/r/', $currentAcl[0])).$spc;
681       $more_options.= $this->mkchkbx($key."_0_w", _("write"), preg_match('/w/', $currentAcl[0]));
683       $display.= "\n  <tr>".
684                  "\n    <td style='background-color:#E0E0E0' colspan=".($cols-1).">$options</td>".
685                  "\n    <td style='background-color:#D4D4D4'>&nbsp;"._("Complete object").": $more_options</td>".
686                  "\n  </tr>";
688       /* Walk through the list of attributes */
689       $cnt= 1;
690       $splist= $plist[preg_replace('%^.*/%', '', $key)]['plProvidedAcls'];
691       asort($splist);
692       if($_SESSION['js']) {
693         if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) {
694           $display.= "\n  <tr id='tr_".preg_replace("/[^a-z0-9]/i","_",$name)."' style='vertical-align:top;height:0px;'>".
695                      "\n    <td colspan=".$cols.">".
696                      "\n      <div id='".preg_replace("/[^a-z0-9]/i","_",$name)."' style='overflow:hidden;visibility:hidden;height:0px;vertical-align:top;width:100%;'>".
697                      "\n        <table style='width:100%;'>";
698         } else if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT'])) {
699           $display.= "\n  <tr id='tr_".preg_replace("/[^a-z0-9]/i","_",$name)."' style='vertical-align:top;height:0px;'>".
700                      "\n    <td colspan=".$cols.">".
701                      "\n      <div id='".preg_replace("/[^a-z0-9]/i","_",$name)."' style='position:absolute;overflow:hidden;visibility:hidden;height:0px;vertical-align:top;width:100%;'>".
702                      "\n        <table style='width:100%;'>";
703         }
704       }
705       foreach($splist as $attr => $dsc){
707         /* Skip pl* attributes, they are internal... */
708         if (preg_match('/^pl[A-Z]+.*$/', $attr)){
709           continue;
710         }
712         /* Open table row */
713         if ($cnt == 1){
714           $display.= "\n  <tr>";
715         }
717         /* Close table row */
718         if ($cnt == $cols){
719           $cnt= 1;
720           $rb= "";
721           $end= "\n  </tr>";
722         } else {
723           $cnt++;
724           $rb= "border-right:1px solid #A0A0A0;";
725           $end= "";
726         }
728         /* Collect list of attributes */
729         $state= "";
730         if (isset($currentAcl[$attr])){
731           $state= $currentAcl[$attr];
732         }
733         $display.= "\n    <td style='border-top:1px solid #A0A0A0;${rb}width:".(int)(100/$cols)."%'>".
734                    "\n      <b>$dsc</b> ($attr)<br>".$this->mkrwbx($key."_".$attr, $state)."</td>$end";
735       }
736       
737       /* Fill missing td's if needed */
738       if (--$cnt != $cols && $cnt != 0){
739        $display.= str_repeat("\n    <td style='border-top:1px solid #A0A0A0; width:".(int)(100/$cols)."%'>&nbsp;</td>", $cols-$cnt); 
740       }
742       if($_SESSION['js']) {
743         if(isset($_SERVER['HTTP_USER_AGENT']) && (preg_match("/gecko/i",$_SERVER['HTTP_USER_AGENT'])) || (preg_match("/ie/i",$_SERVER['HTTP_USER_AGENT']))) {
744           $display.= "\n        </table>".
745                      "\n      </div>".
746                      "\n    </td>".
747                      "\n  </tr>";
748         }
749       }
751       $display.= "\n</table><br />\n";
752     }
754     return ($display);
755   }
758   function mkchkbx($name, $text, $state= FALSE)
759   {
760     $state= $state?"checked":"";
761     return "\n      <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."' type=checkbox name='acl_$name' $state>".
762            "\n      <label for='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."'>$text</label>";
763   }
766   function mkrwbx($name, $state= "")
767   {
768     $rstate= preg_match('/r/', $state)?'checked':'';
769     $wstate= preg_match('/w/', $state)?'checked':'';
770     return ("\n      <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_r' type=checkbox name='acl_${name}_r' $rstate>".
771             "\n      <label for='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_r'>"._("read")."</label>".
772             "\n      <input id='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_w' type=checkbox name='acl_${name}_w' $wstate>".
773             "\n      <label for='acl_".preg_replace("/[^a-z0-9]/i","_",$name)."_w'>"._("write")."</label>");
774   }
777   function explodeACL($acl)
778   {
779     list($index, $type)= split(':', $acl);
780     $a= array( $index => array("type" => $type,
781                                "members" => acl::extractMembers($acl,$type == "role")));
782    
783     /* Handle different types */
784     switch ($type){
786       case 'psub':
787       case 'sub':
788       case 'one':
789       case 'base':
790         $a[$index]['acl']= acl::extractACL($acl);
791         break;
792       
793       case 'role':
794         $a[$index]['acl']= base64_decode(preg_replace('/^[^:]+:[^:]+:([^:]+).*$/', '\1', $acl));
795         break;
797       case 'reset':
798         break;
799       
800       default:
801         print_red(sprintf(_("Unkown ACL type '%s'. Don't know how to handle it."), $type));
802         $a= array();
803     }
804     return ($a);
805   }
808   function extractMembers($acl,$role = FALSE)
809   {
810     global $config;
811     $a= array();
813     /* Rip acl off the string, seperate by ',' and place it in an array */
814     if($role){
815       $ms= preg_replace('/^[^:]+:[^:]+:[^:]+:([^:]+).*$/', '\1', $acl);
816     }else{
817       $ms= preg_replace('/^[^:]+:[^:]+:([^:]+).*$/', '\1', $acl);
818     }
819     if ($ms == $acl){
820       return $a;
821     }
822     $ma= split(',', $ms);
824     /* Decode dn's, fill with informations from LDAP */
825     $ldap= $config->get_ldap_link();
826     foreach ($ma as $memberdn){
827       $dn= base64_decode($memberdn);
828       $ldap->cat($dn, array('cn', 'objectClass', 'description', 'uid'));
830       /* Found entry... */
831       if ($ldap->count()){
832         $attrs= $ldap->fetch();
833         if (in_array_ics('gosaAccount', $attrs['objectClass'])){
834           $a['U:'.$dn]= $attrs['cn'][0]." [".$attrs['uid'][0]."]";
835         } else {
836           $a['G:'.$dn]= $attrs['cn'][0];
837           if (isset($attrs['description'][0])){
838             $a['G:'.$dn].= " [".$attrs['description'][0]."]";
839           }
840         }
842       /* ... or not */
843       } else {
844         $a['U:'.$dn]= sprintf(_("Unknown entry '%s'!"), $dn);
845       }
846     }
848     return ($a);
849   }
852   function extractACL($acl)
853   {
854     /* Rip acl off the string, seperate by ',' and place it in an array */
855     $as= preg_replace('/^[^:]+:[^:]+:[^:]*:(.*)$/', '\1', $acl);
856     $aa= split(',', $as);
857     $a= array();
859     /* Dis-assemble single ACLs */
860     foreach($aa as $sacl){
861       
862       /* Dis-assemble field ACLs */
863       $ao= split('#', $sacl);
864       $gobject= "";
865       foreach($ao as $idx => $ssacl){
867         /* First is department with global acl */
868         $object= preg_replace('/^([^;]+);.*$/', '\1', $ssacl);
869         $gacl=   preg_replace('/^[^;]+;(.*)$/', '\1', $ssacl);
870         if ($idx == 0){
871           /* Create hash for this object */
872           $gobject= $object;
873           $a[$gobject]= array();
875           /* Append ACL if set */
876           if ($gacl != ""){
877             $a[$gobject]= array($gacl);
878           }
879         } else {
881           /* All other entries get appended... */
882           list($field, $facl)= split(';', $ssacl);
883           $a[$gobject][$field]= $facl;
884         }
886       }
887     }
889     return ($a);
890   }
892   
893   function assembleAclSummary($entry)
894   {
895     $summary= "";
897     /* Summarize ACL */
898     if (isset($entry['acl'])){
899       $acl= "";
901       if($entry['type'] == "role"){
903         if(isset($this->roles[$entry['acl']])){  
904           $summary.= sprintf(_("Role: %s"), $this->roles[$entry['acl']]['cn']);
905         }else{
906           $summary.= sprintf(_("Role: %s"), "<i>"._("Unknown role, possibly removed")."</i>");
907         }
908       }else{
909         foreach ($entry['acl'] as $name => $object){
910           if (count($object)){
911             $acl.= "$name, ";
912           }
913         }
914         $summary.= sprintf(_("Contains settings for these objects: %s"), preg_replace('/, $/', '', $acl));
915       }
916     }
918     /* Summarize members */
919     if ($summary != ""){
920       $summary.= ", ";
921     }
922     if (count($entry['members'])){
923       $summary.= _("Members:")." ";
924       foreach ($entry['members'] as $cn){
925         $cn= preg_replace('/ \[.*$/', '', $cn);
926         $summary.= $cn.", ";
927       }
928     } else {
929       $summary.= _("ACL is valid for all users");
930     }
932     return (preg_replace('/, $/', '', $summary));
933   }
936   function loadAclEntry($new= FALSE)
937   {
938     /* New entry gets presets... */
939     if ($new){
940       $this->aclType= 'base';
941       $this->recipients= array();
942       $this->aclContents= array();
943     } else {
944       $acl= $this->gosaAclEntry[$this->currentIndex];
945       $this->aclType= $acl['type'];
946       $this->recipients= $acl['members'];
947       $this->aclContents= $acl['acl'];
948     }
950     $this->wasNewEntry= $new;
951   }
954   function aclPostHandler()
955   {
956     if (isset($_POST['save_acl'])){
957       $this->save();
958       return TRUE;
959     }
961     return FALSE;
962   }
964   
965   function PrepareForCopyPaste($source)
966   {
967     plugin::PrepareForCopyPaste($source);
968     
969     $dn = $source['dn'];
970     $acl_c = new acl($this->config, $this->parent,$dn);
971     $this->gosaAclEntry = $acl_c->gosaAclEntry;
972   }
975   function save()
976   {
977     /* Assemble ACL's */
978     $tmp_acl= array();
979     foreach ($this->gosaAclEntry as $prio => $entry){
980       $final= "";
981       $members= "";
982       if (isset($entry['members'])){
983         foreach ($entry['members'] as $key => $dummy){
984           $members.= base64_encode(preg_replace('/^.:/', '', $key)).',';
985         }
986       }
988       if($entry['type'] != "role"){
989         $final= $prio.":".$entry['type'].":".preg_replace('/,$/', '', $members);
990       }else{
991         $final= $prio.":".$entry['type'].":".base64_encode($entry['acl']).":".preg_replace('/,$/', '', $members);
992       }
994       /* ACL's if needed */
995       if ($entry['type'] != "reset" && $entry['type'] != "role"){
996         $acl= ":";
997         if (isset($entry['acl'])){
998           foreach ($entry['acl'] as $object => $contents){
1000             /* Only save, if we've some contents in there... */
1001             if (count($contents)){
1002               $acl.= $object.";";
1004               foreach($contents as $attr => $permission){
1006                 /* First entry? Its the one for global settings... */
1007                 if ($attr == '0'){
1008                   $acl.= $permission;
1009                 } else {
1010                   $acl.= '#'.$attr.';'.$permission;
1011                 }
1013               }
1014               $acl.= ',';
1015             }
1016             
1017           }
1018         }
1019         $final.= preg_replace('/,$/', '', $acl);
1020       }
1022       $tmp_acl[]= $final;
1023     } 
1025     /* Call main method */
1026     plugin::save();
1028     /* Finally (re-)assign it... */
1029     $this->attrs['gosaAclEntry']= $tmp_acl;
1031     /* Remove acl from this entry if it is empty... */
1032     if (!count($tmp_acl)){
1033       /* Remove attribute */
1034       if ($this->initially_was_account){
1035         $this->attrs['gosaAclEntry']= array();
1036       } else {
1037         if (isset($this->attrs['gosaAclEntry'])){
1038           unset($this->attrs['gosaAclEntry']);
1039         }
1040       }
1042       /* Remove object class */
1043       $this->attrs['objectClass']= array_remove_entries(array('gosaAcl'), $this->attrs['objectClass']);
1044     }    
1046     /* Do LDAP modifications */
1047     $ldap= $this->config->get_ldap_link();
1048     $ldap->cd($this->dn);
1049     $this->cleanup();
1050     $ldap->modify ($this->attrs);
1052     if(count($this->attrs)){
1053       new log("modify","acls/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1054     }
1056     show_ldap_error($ldap->get_error(), sprintf(_("Saving ACLs with dn '%s' failed."),$this->dn));
1058     /* Refresh users ACLs */
1059     $ui= get_userinfo();
1060     $ui->loadACL();
1061     $_SESSION['ui']= $ui;
1062   }
1065   function remove_from_parent()
1066   {
1067     plugin::remove_from_parent();
1069     /* include global link_info */
1070     $ldap= $this->config->get_ldap_link();
1072     $ldap->cd($this->dn);
1073     $this->cleanup();
1074     $ldap->modify ($this->attrs);
1076     new log("remove","acls/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1078     /* Optionally execute a command after we're done */
1079     $this->handle_post_events("remove",array("uid" => $this->uid));
1080   }
1084 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1085 ?>