Code

22111b073ab29748e54a666b69ac5d0a1ac6db38
[gosa.git] / plugins / admin / ogroups / class_ogroup.inc
1 <?php
4 /* Sort multidimensional arrays for key 'text' */
5 function sort_list($val1, $val2)
6 {
7   $v1= strtolower($val1['text']);
8   $v2= strtolower($val2['text']);
9   if ($v1 > $v2){
10     return 1;
11   }
12   if ($v1 < $v2){
13     return -1;
14   }
15   return 0;
16 }
19 class ogroup extends plugin
20 {
21   /* Variables */
22   var $cn= "";
23   var $description= "";
24   var $base= "";
25   var $gosaGroupObjects= "";
26   var $department= "";
27   var $objects= array();
28   var $allobjects= array();
29   var $memberList= array();
30   var $member= array();
31   var $orig_dn= "";
32   var $group_dialog= FALSE;
34   /* attribute list for save action */
35   var $attributes= array("cn", "description", "gosaGroupObjects");
36   var $objectclasses= array("top", "gosaGroupOfNames");
38   function ogroup ($config, $dn= NULL)
39   {
40     plugin::plugin ($config, $dn);
41     $this->orig_dn= $dn;
43     /* Load member objects */
44     if (isset($this->attrs['member'])){
45       foreach ($this->attrs['member'] as $key => $value){
46         if ("$key" != "count"){
47           $this->member["$value"]= "$value";
48         }
49       }
50     }
51     $this->is_account= TRUE;
53     /* Get global filter config */
54     if (!is_global("ogfilter")){
55       $ui= get_userinfo();
56       $base= get_base_from_people($ui->dn);
57       $ogfilter= array( "dselect"       => $base,
58           "regex"           => "*");
59       register_global("ogfilter", $ogfilter);
60     }
61     $ogfilter= get_global('ogfilter');
63     /* Adjust flags */
64     foreach( array(   "U" => "accounts",
65           "G" => "groups",
66           "A" => "applications",
67           "D" => "departments",
68           "S" => "servers",
69           "W" => "workstations",
70           "T" => "terminals",
71           "F" => "phones",
72           "P" => "printers") as $key => $val){
74       if (preg_match("/$key/", $this->gosaGroupObjects)){
75         $ogfilter[$val]= "checked";
76       } else {
77         $ogfilter[$val]= "";
78       }
79     }
80     register_global("ogfilter", $ogfilter);
81   
82     if(isset($_SESSION['ogroupfilter']['depselect'])){
83      $this->base = $_SESSION['ogroupfilter']['depselect'];
84     }
86      /* set permissions */
87     $ui= get_userinfo();
88     $acl= get_permissions ($ui->dn, $ui->subtreeACL);
89     $this->acl= get_module_permission($acl, "ogroup", $ui->dn);
92     /* Load member data */
93     $this->reload();
94   }
96   function AddDelMembership(){
97      /* Delete objects from group */
98     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
99       foreach ($_POST['members'] as $value){
100         $this->objects["$value"]= $this->memberList[$value];
101         unset ($this->memberList["$value"]);
102         unset ($this->member["$value"]);
103         uasort ($this->objects, 'sort_list');
104         reset ($this->objects);
105       }
106       $this->reload();
107     }
109     /* Add objects to group */
110     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
111       foreach ($_POST['objects'] as $value){
112         $this->memberList["$value"]= $this->objects[$value];
113         $this->member["$value"]= $value;
114         unset ($this->objects[$value]);
115         uasort ($this->memberList, 'sort_list');
116         reset ($this->memberList);
117       }
118       $this->reload();
119     }
120   }
122   function execute()
123   {
124         /* Call parent execute */
125         plugin::execute();
127 //    $this->reload();
129     /* Do we represent a valid group? */
130     if (!$this->is_account){
131       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
132         _("This 'dn' is no object group.")."</b>";
133       return ($display);
134     }
136     /* Delete objects from group */
137     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
138       foreach ($_POST['members'] as $value){
139         if(isset($this->memberList[$value])){
140           $this->objects["$value"]= $this->memberList[$value];
141           unset ($this->memberList["$value"]);
142           unset ($this->member["$value"]);
143           uasort ($this->objects, 'sort_list');
144           reset ($this->objects);
145         }
146       }
147       $this->reload();
148     }
150     /* Add objects to group */
151     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
152       foreach ($_POST['objects'] as $value){
153         if(isset($this->objects[$value])){
154           $this->memberList["$value"]= $this->objects[$value];
155           $this->member["$value"]= $value;
156           unset ($this->objects[$value]);
157           uasort ($this->memberList, 'sort_list');
158           reset ($this->memberList);
159         }
160       }
161       $this->reload();
162     }
164     /* Load templating engine */
165     $smarty= get_smarty();
167     /* Add objects? */
168     if (isset($_POST["edit_membership"])){
169       $this->group_dialog= TRUE;
170       $this->dialog= TRUE;
171     }
173     /* Add objects finished? */
174     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
175       $this->group_dialog= FALSE;
176       $this->dialog= FALSE;
177     }
179     /* Manage object add dialog */
180     if ($this->group_dialog){
182       /* Save data */
183       $ogfilter= get_global("ogfilter");
184       foreach( array("dselect", "regex") as $type){
185         if (isset($_POST[$type])){
186           $ogfilter[$type]= $_POST[$type];
187         }
188       }
189       if (isset($_POST['dselect'])){
190         foreach( array("accounts", "groups", "applications", "departments",
191               "servers", "workstations", "terminals", "printers",
192               "phones") as $type){
194           if (isset($_POST[$type])) {
195             $ogfilter[$type]= "checked";
196           } else {
197             $ogfilter[$type]= "";
198           }
199         }
200       }
201       if (isset($_GET['search'])){
202         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
203         if ($s == "**"){
204           $s= "*";
205         }
206         $ogfilter['regex']= $s;
207       }
208       register_global("ogfilter", $ogfilter);
209       $this->reload();
211       /* Calculate actual groups */
212       $smarty->assign("objects", $this->convert_list($this->objects));
214       /* Show dialog */
215       $smarty->assign("search_image", get_template_path('images/search.png'));
216       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
217       $smarty->assign("tree_image", get_template_path('images/tree.png'));
218       $smarty->assign("deplist", $this->config->idepartments);
219       $smarty->assign("alphabet", generate_alphabet());
220       foreach( array("dselect", "regex", "accounts", "groups", "applications",
221             "departments", "servers", "workstations", "terminals",
222             "printers", "phones") as $type){
223         $smarty->assign("$type", $ogfilter[$type]);
224       }
225       $smarty->assign("hint", print_sizelimit_warning());
226       $smarty->assign("apply", apply_filter());
228       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
229       return ($display);
230     }
232     /* Bases / Departments */
233    
234       if (isset($_POST['base'])){
235         $this->base= $_POST['base'];
236       }
238     /* Assemble combine string */
239     if ($this->gosaGroupObjects == "[]"){
240       $smarty->assign("combinedObjects", _("none"));
241     } elseif (strlen($this->gosaGroupObjects) > 4){
242       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
243     } else {
244       $conv= array(   "U" => _("users"),
245           "G" => _("groups"),
246           "A" => _("applications"),
247           "D" => _("departments"),
248           "S" => _("servers"),
249           "W" => _("workstations"),
250           "T" => _("terminals"),
251           "F" => _("phones"),
252           "P" => _("printers"));
254       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
255       $p1= $conv[$type[0]];
256       error_reporting(0);
257       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
258         $p2= $conv[$type[1]];
259         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
260       } else {
261         $smarty->assign("combinedObjects", "$p1");
262       }
263       error_reporting(E_ALL);
264     }
266     /* Assign variables */
267     $smarty->assign("bases", $this->config->idepartments);
268     $smarty->assign("base_select", $this->base);
269     $smarty->assign("department", $this->department);
270     $smarty->assign("members", $this->convert_list($this->memberList));
272     /* Objects have to be tuned... */
273     $smarty->assign("objects", $this->convert_list($this->objects));
275     /* Fields */
276     foreach ($this->attributes as $val){
277       $smarty->assign("$val", $this->$val);
278       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
279     }
281     /* Assign ACL's */
282     foreach (array("base", "members") as $val){
283       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
284     }
286     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
287   }
290   /* Save data to object */
291   function save_object()
292   {
293     /* Save additional values for possible next step */
294     if (isset($_POST['ogroupedit'])){
295       plugin::save_object();
297       if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
298         $this->base= $_POST["base"];
299       }
301     }
302   }
305   /* (Re-)Load objects */
306   function reload()
307   {
308     /*###########
309       Variable initialisation 
310       ###########*/
312     $this->objects                = array();
313     $this->ui                     = get_userinfo();
314     $filter                       = "";
315     $objectClasses                = array();
316     
317     $ogfilter               = get_global("ogfilter");
318     $regex                  = $ogfilter['regex'];
320     /* Get ldap connection */
321     $ldap= $this->config->get_ldap_link();
322     $ldap->cd ($ogfilter['dselect']);
325     /*###########
326       Generate Filter 
327       ###########*/
329     /* Assemble filter */
330     if ($ogfilter['accounts'] == "checked"){
331       $filter.= "(objectClass=gosaAccount)";
332       $objectClasses["gosaAccount"]     = get_people_ou();
333     }
334     if ($ogfilter['groups'] == "checked"){
335       $filter.= "(objectClass=posixGroup)";
336       $objectClasses["posixGroup"]      = get_groups_ou();
337     }
338     if ($ogfilter['applications'] == "checked"){
339       $filter.= "(objectClass=gosaApplication)";
340       $objectClasses["gosaApplication"] = "ou=apps,";
341     }
342     if ($ogfilter['departments'] == "checked"){
343       $filter.= "(objectClass=gosaDepartment)";
344       $objectClasses["gosaDepartment"]  = "";
345     }
346     if ($ogfilter['servers'] == "checked"){
347       $filter.= "(objectClass=goServer)";
348       $objectClasses["goServer"]        = "ou=servers,ou=systems,";
349     }
350     if ($ogfilter['workstations'] == "checked"){
351       $filter.= "(objectClass=gotoWorkstation)";
352       $objectClasses["gotoWorkstation"] = "ou=workstations,ou=systems,";
353     }
354     if ($ogfilter['terminals'] == "checked"){
355       $filter.= "(objectClass=gotoTerminal)";
356       $objectClasses["gotoTerminal"]    = "ou=terminals,ou=systems,";
357     }
358     if ($ogfilter['printers'] == "checked"){
359       $filter.= "(objectClass=gotoPrinter)";
361       $objectClasses["gotoPrinter"]     = "ou=printers,ou=systems,";
362     }
363     if ($ogfilter['phones'] == "checked"){
364       $filter.= "(objectClass=goFonHardware)";
365       $objectClasses["goFonHardware"]   = "ou=phones,ou=systems,";
366     }
369     /*###########
370       Perform search for selected objectClasses & regex to fill list with objects   
371       ###########*/
373     /* Perform search for selected objectClasses */
374     foreach($objectClasses as $class=> $basedn){
375       $ldap->ls("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",$basedn.$ogfilter['dselect'] ,
376           array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
377       
378       /* fetch results and append them to the list */
379       while($attrs = $ldap->fetch()){
381         $type= $this->getObjectType($attrs);
382         $name= $this->getObjectName($attrs);
384         /* Fill array */
385         if (isset($attrs["description"][0])){
386           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
387         } elseif (isset($attrs["uid"][0])) {
388           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
389         } else {
390           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
391         }
392       }
393     }
394     uasort ($this->objects, 'sort_list');
395     reset ($this->objects);
397     /*###########
398       Get a list with all possible objects, to detect objects which doesn't exists anymore ... 
399       ###########*/
401     /* Only do this, if this wasn't already done */
402     if(count($this->allobjects) == 0){
403       $ldap->cd ($this->config->current['BASE']);
404       $filter="(objectClass=gosaAccount)".        
405               "(objectClass=posixGroup)".
406               "(objectClass=gosaApplication)".
407               "(objectClass=gosaDepartment)".
408               "(objectClass=goServer)".
409               "(objectClass=gotoWorkstation)".
410               "(objectClass=gotoTerminal)".
411               "(objectClass=gotoPrinter)".
412               "(objectClass=goFonHardware)";
413       $regex= "*";
415       $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
416       while ($attrs= $ldap->fetch()){
418         $type= $this->getObjectType($attrs);
419         $name= $this->getObjectName($attrs);
421         if (isset($attrs["description"][0])){
422           $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
423         } elseif (isset($attrs["uid"][0])) {
424           $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
425         } else {
426           $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
427         }
428         $this->allobjects[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
429         if(isset($attrs['uid'])){
430           $this->allobjects[$attrs["dn"]]['uid']          = $attrs['uid'];
431         }
432       }
433       uasort ($this->allobjects, 'sort_list');
434       reset ($this->allobjects);
435     }      
437     
438     /*###########
439       Build member list and try to detect obsolete entries 
440       ###########*/
442     $this->memberList = array();
443   
444     /* Walk through all single member entry */
445     foreach($this->member as $dn){
447       /* Object in object list? */
448       if (isset($this->allobjects[$dn])){
449         
450         /* Add this entry to member list, its dn is in allobjects
451             this means it still exists 
452          */
453         $this->memberList[$dn]= $this->allobjects[$dn];
455         /* Remove this from selectable entries */
456         if (isset ($this->objects[$dn])){
457           unset ($this->objects[$dn]);
458         }
460       
461       } else {
463         /* The dn for the current member can't be resolved 
464             it seams that this entry was removed 
465          */ 
466         /* Try to resolv the entry again, if it still fails, display error msg */
467         $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
469         /* It has failed, add entry with type flag I (Invalid)*/
470         if ($ldap->error != "success"){
471           $this->memberList[$dn]= array('text' => _("Non existing dn: ")."$dn","type" => "I");
473         } else {
474           
475           /* Append this entry to our all object list */
476     
477           /* Fetch object */
478           $attrs= $ldap->fetch();
480           $type= $this->getObjectType($attrs);
481           $name= $this->getObjectName($attrs);
483           if (isset($attrs["description"][0])){
484             $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
485           } elseif (isset($attrs["uid"][0])) {
486             $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
487           } else {
488             $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
489           }
490           $this->allobjects[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
491           if(isset($attrs['uid'])){
492             $this->allobjects[$attrs["dn"]]['uid']          = $attrs['uid'];
493           }
495           /* Fill array */
496           if (isset($attrs["description"][0])){
497             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
498           } else {
499             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
500           }
501         }
502       }
503     }
504     uasort ($this->memberList, 'sort_list');
505     reset ($this->memberList);
507     /* Assemble types of currently combined objects */
508     $objectTypes= "";
509     foreach ($this->memberList as $dn => $desc){
511       /* Invalid object? */
512       if ($desc['type'] == 'I'){
513         continue;
514       }
516       /* Fine. Add to list. */
517       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
518         $objectTypes.= $desc['type'];
519       }
520     }
521     $this->gosaGroupObjects= "[$objectTypes]";
522   }
525   function convert_list($input)
526   {
527     $temp= "";
528     $conv= array(  "U" => "select_user.png",
529         "G" => "select_groups.png",
530         "A" => "select_application.png",
531         "D" => "select_department.png",
532         "S" => "select_server.png",
533         "W" => "select_workstation.png",
534         "T" => "select_terminal.png",
535         "F" => "select_phone.png",
536         "I" => "flag.png",
537         "P" => "select_printer.png");
539     foreach ($input as $key => $value){
540       /* Generate output */
541       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
542     }
544     return ($temp);
545   }
548   function getObjectType($attrs)
549   {
550     $type= "I";
552     foreach(array(  "U" => "gosaAccount",
553           "G" => "posixGroup",
554           "A" => "gosaApplication",
555           "D" => "gosaDepartment",
556           "S" => "goServer",
557           "W" => "gotoWorkstation",
558           "T" => "gotoTerminal",
559           "F" => "goFonHardware",
560           "P" => "gotoPrinter") as $index => $class){
561       if (in_array($class, $attrs['objectClass'])){
562         $type= $index;
563         break;
564       }
565     }
567     return ($type);
568   }
571   function getObjectName($attrs)
572   {
573     /* Person? */
574     $name =""; 
575     if (in_array('gosaAccount', $attrs['objectClass'])){
576       if(isset($attrs['sn']) && isset($attrs['givenName'])){
577         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
578       } else {
579         $name= $attrs['uid'][0];
580       }
581     } else {
582       if(isset($attrs["cn"][0])) {
583         $name= $attrs['cn'][0];
584       } else {
585         $name= $attrs['ou'][0];
586       }
587     }
589     return ($name);
590   }
593   function check()
594   {
595     $message= array();
597     /* Permissions for that base? */
598     if ($this->base != ""){
599       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
600     } else {
601       $new_dn= $this->dn;
602     }
605     $ldap = $this->config->get_ldap_link();
606     if($this->dn != $new_dn){
607       $ldap->cat ($new_dn);
608     }
609     
610     if($ldap->count() !=0){
611       $message[]= _("There is already an object with this cn.");
612     } 
614     $ui= get_userinfo();
615     $acl= get_permissions ($new_dn, $ui->subtreeACL);
616     $acl= get_module_permission($acl, "group", $new_dn);
617     if (chkacl($acl, "create") != ""){
618       $message[]= _("You have no permissions to create a group on this 'Base'.");
619     }
621     /* must: cn */
622     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
623       $message[]= "The required field 'Name' is not set.";
624     }
626     /* To many different object types? */
627     if (strlen($this->gosaGroupObjects) > 4){
628       $message[]= _("You can combine two different object types at maximum only!");
629     }
631     return ($message);
632   }
635   /* Save to LDAP */
636   function save()
637   {
638     plugin::save();
640     /* Move members to target array */
641     foreach ($this->member as $key => $desc){
642       $this->attrs['member'][]= $key;
643     }
645     $ldap= $this->config->get_ldap_link();
647     /* New accounts need proper 'dn', propagate it to remaining objects */
648     if ($this->dn == 'new'){
649       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
650     }
652     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
653        new entries. So do a check first... */
654     $ldap->cat ($this->dn);
655     if ($ldap->fetch()){
656       /* Modify needs array() to remove values :-( */
657       if (!count ($this->member)){
658         $this->attrs['member']= array();
659       }
660       $mode= "modify";
661     } else {
662       $mode= "add";
663       $ldap->cd($this->config->current['BASE']);
664       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
665     }
667     /* Write back to ldap */
668     $ldap->cd($this->dn);
669     $ldap->$mode($this->attrs);
671     /* Trigger post signal */
672     $this->handle_post_events($mode);
674     $ret= 0;
675     if (show_ldap_error($ldap->get_error())){
676       $ret= 1;
677     }
679     return ($ret);
680   }
682   function remove_from_parent()
683   {
684     plugin::remove_from_parent();
686     $ldap= $this->config->get_ldap_link();
687     $ldap->rmdir($this->dn);
688     show_ldap_error($ldap->get_error());
690     /* Trigger remove signal */
691     $this->handle_post_events("remove");
692   }
696 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
697 ?>