Code

136562a6308b0383e877bc82469b7847e8873365
[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($NewMember = false){
98     if($NewMember){
99       $this->memberList[$NewMember]= $this->allobjects[$NewMember];
100       $this->member[$NewMember]= $NewMember;
101       unset ($this->objects[$NewMember]);
102       uasort ($this->memberList, 'sort_list');
103       reset ($this->memberList);
104       $this->reload();
105     }else{
106       /* Delete objects from group */
107       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
108         foreach ($_POST['members'] as $value){
109           $this->objects["$value"]= $this->memberList[$value];
110           unset ($this->memberList["$value"]);
111           unset ($this->member["$value"]);
112           uasort ($this->objects, 'sort_list');
113           reset ($this->objects);
114         }
115         $this->reload();
116       }
118       /* Add objects to group */
119       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
120         foreach ($_POST['objects'] as $value){
121           $this->memberList["$value"]= $this->objects[$value];
122           $this->member["$value"]= $value;
123           unset ($this->objects[$value]);
124           uasort ($this->memberList, 'sort_list');
125           reset ($this->memberList);
126         }
127         $this->reload();
128       }
129     }
130   }
132   function execute()
133   {
134         /* Call parent execute */
135         plugin::execute();
137 //    $this->reload();
139     /* Do we represent a valid group? */
140     if (!$this->is_account){
141       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
142         _("This 'dn' is no object group.")."</b>";
143       return ($display);
144     }
146     /* Delete objects from group */
147     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
148       foreach ($_POST['members'] as $value){
149         if(isset($this->memberList[$value])){
150           $this->objects["$value"]= $this->memberList[$value];
151           unset ($this->memberList["$value"]);
152           unset ($this->member["$value"]);
153           uasort ($this->objects, 'sort_list');
154           reset ($this->objects);
155         }
156       }
157       $this->reload();
158     }
160     /* Add objects to group */
161     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
162       foreach ($_POST['objects'] as $value){
163         if(isset($this->objects[$value])){
164           $this->memberList["$value"]= $this->objects[$value];
165           $this->member["$value"]= $value;
166           unset ($this->objects[$value]);
167           uasort ($this->memberList, 'sort_list');
168           reset ($this->memberList);
169         }
170       }
171       $this->reload();
172     }
174     /* Load templating engine */
175     $smarty= get_smarty();
177     /* Add objects? */
178     if (isset($_POST["edit_membership"])){
179       $this->group_dialog= TRUE;
180       $this->dialog= TRUE;
181     }
183     /* Add objects finished? */
184     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
185       $this->group_dialog= FALSE;
186       $this->dialog= FALSE;
187     }
189     /* Manage object add dialog */
190     if ($this->group_dialog){
192       /* Save data */
193       $ogfilter= get_global("ogfilter");
194       foreach( array("dselect", "regex") as $type){
195         if (isset($_POST[$type])){
196           $ogfilter[$type]= $_POST[$type];
197         }
198       }
199       if (isset($_POST['dselect'])){
200         foreach( array("accounts", "groups", "applications", "departments",
201               "servers", "workstations", "terminals", "printers",
202               "phones") as $type){
204           if (isset($_POST[$type])) {
205             $ogfilter[$type]= "checked";
206           } else {
207             $ogfilter[$type]= "";
208           }
209         }
210       }
211       if (isset($_GET['search'])){
212         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
213         if ($s == "**"){
214           $s= "*";
215         }
216         $ogfilter['regex']= $s;
217       }
218       register_global("ogfilter", $ogfilter);
219       $this->reload();
221       /* Calculate actual groups */
222       $smarty->assign("objects", $this->convert_list($this->objects));
224       /* Show dialog */
225       $smarty->assign("search_image", get_template_path('images/search.png'));
226       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
227       $smarty->assign("tree_image", get_template_path('images/tree.png'));
228       $smarty->assign("deplist", $this->config->idepartments);
229       $smarty->assign("alphabet", generate_alphabet());
230       foreach( array("dselect", "regex", "accounts", "groups", "applications",
231             "departments", "servers", "workstations", "terminals",
232             "printers", "phones") as $type){
233         $smarty->assign("$type", $ogfilter[$type]);
234       }
235       $smarty->assign("hint", print_sizelimit_warning());
236       $smarty->assign("apply", apply_filter());
238       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
239       return ($display);
240     }
242     /* Bases / Departments */
243    
244       if (isset($_POST['base'])){
245         $this->base= $_POST['base'];
246       }
248     /* Assemble combine string */
249     if ($this->gosaGroupObjects == "[]"){
250       $smarty->assign("combinedObjects", _("none"));
251     } elseif (strlen($this->gosaGroupObjects) > 4){
252       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
253     } else {
254       $conv= array(   "U" => _("users"),
255           "G" => _("groups"),
256           "A" => _("applications"),
257           "D" => _("departments"),
258           "S" => _("servers"),
259           "W" => _("workstations"),
260           "T" => _("terminals"),
261           "F" => _("phones"),
262           "P" => _("printers"));
264       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
265       $p1= $conv[$type[0]];
266       error_reporting(0);
267       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
268         $p2= $conv[$type[1]];
269         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
270       } else {
271         $smarty->assign("combinedObjects", "$p1");
272       }
273       error_reporting(E_ALL);
274     }
276     /* Assign variables */
277     $smarty->assign("bases", $this->config->idepartments);
278     $smarty->assign("base_select", $this->base);
279     $smarty->assign("department", $this->department);
280     $smarty->assign("members", $this->convert_list($this->memberList));
282     /* Objects have to be tuned... */
283     $smarty->assign("objects", $this->convert_list($this->objects));
285     /* Fields */
286     foreach ($this->attributes as $val){
287       $smarty->assign("$val", $this->$val);
288       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
289     }
291     /* Assign ACL's */
292     foreach (array("base", "members") as $val){
293       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
294     }
296     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
297   }
300   /* Save data to object */
301   function save_object()
302   {
303     /* Save additional values for possible next step */
304     if (isset($_POST['ogroupedit'])){
305       plugin::save_object();
307       if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
308         $this->base= $_POST["base"];
309       }
311     }
312   }
315   /* (Re-)Load objects */
316   function reload()
317   {
318     /*###########
319       Variable initialisation 
320       ###########*/
322     $this->objects                = array();
323     $this->ui                     = get_userinfo();
324     $filter                       = "";
325     $objectClasses                = array();
326     
327     $ogfilter               = get_global("ogfilter");
328     $regex                  = $ogfilter['regex'];
330     /* Get ldap connection */
331     $ldap= $this->config->get_ldap_link();
332     $ldap->cd ($ogfilter['dselect']);
335     /*###########
336       Generate Filter 
337       ###########*/
339     /* Assemble filter */
340     if ($ogfilter['accounts'] == "checked"){
341       $filter.= "(objectClass=gosaAccount)";
342       $objectClasses["gosaAccount"]     = get_people_ou();
343     }
344     if ($ogfilter['groups'] == "checked"){
345       $filter.= "(objectClass=posixGroup)";
346       $objectClasses["posixGroup"]      = get_groups_ou();
347     }
348     if ($ogfilter['applications'] == "checked"){
349       $filter.= "(objectClass=gosaApplication)";
350       $objectClasses["gosaApplication"] = "ou=apps,";
351     }
352     if ($ogfilter['departments'] == "checked"){
353       $filter.= "(objectClass=gosaDepartment)";
354       $objectClasses["gosaDepartment"]  = "";
355     }
356     if ($ogfilter['servers'] == "checked"){
357       $filter.= "(objectClass=goServer)";
358       $objectClasses["goServer"]        = "ou=servers,ou=systems,";
359     }
360     if ($ogfilter['workstations'] == "checked"){
361       $filter.= "(objectClass=gotoWorkstation)";
362       $objectClasses["gotoWorkstation"] = "ou=workstations,ou=systems,";
363     }
364     if ($ogfilter['terminals'] == "checked"){
365       $filter.= "(objectClass=gotoTerminal)";
366       $objectClasses["gotoTerminal"]    = "ou=terminals,ou=systems,";
367     }
368     if ($ogfilter['printers'] == "checked"){
369       $filter.= "(objectClass=gotoPrinter)";
371       $objectClasses["gotoPrinter"]     = "ou=printers,ou=systems,";
372     }
373     if ($ogfilter['phones'] == "checked"){
374       $filter.= "(objectClass=goFonHardware)";
375       $objectClasses["goFonHardware"]   = "ou=phones,ou=systems,";
376     }
379     /*###########
380       Perform search for selected objectClasses & regex to fill list with objects   
381       ###########*/
383     /* Perform search for selected objectClasses */
384     foreach($objectClasses as $class=> $basedn){
385       $ldap->ls("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",$basedn.$ogfilter['dselect'] ,
386           array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
387       
388       /* fetch results and append them to the list */
389       while($attrs = $ldap->fetch()){
391         $type= $this->getObjectType($attrs);
392         $name= $this->getObjectName($attrs);
394         /* Fill array */
395         if (isset($attrs["description"][0])){
396           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
397         } elseif (isset($attrs["uid"][0])) {
398           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
399         } else {
400           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
401         }
402       }
403     }
404     uasort ($this->objects, 'sort_list');
405     reset ($this->objects);
407     /*###########
408       Get a list with all possible objects, to detect objects which doesn't exists anymore ... 
409       ###########*/
411     /* Only do this, if this wasn't already done */
412     if(count($this->allobjects) == 0){
413       $ldap->cd ($this->config->current['BASE']);
414       $filter="(objectClass=gosaAccount)".        
415               "(objectClass=posixGroup)".
416               "(objectClass=gosaApplication)".
417               "(objectClass=gosaDepartment)".
418               "(objectClass=goServer)".
419               "(objectClass=gotoWorkstation)".
420               "(objectClass=gotoTerminal)".
421               "(objectClass=gotoPrinter)".
422               "(objectClass=goFonHardware)";
423       $regex= "*";
425       $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
426       while ($attrs= $ldap->fetch()){
428         $type= $this->getObjectType($attrs);
429         $name= $this->getObjectName($attrs);
431         if (isset($attrs["description"][0])){
432           $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
433         } elseif (isset($attrs["uid"][0])) {
434           $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
435         } else {
436           $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
437         }
438         $this->allobjects[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
439         if(isset($attrs['uid'])){
440           $this->allobjects[$attrs["dn"]]['uid']          = $attrs['uid'];
441         }
442       }
443       uasort ($this->allobjects, 'sort_list');
444       reset ($this->allobjects);
445     }      
447     
448     /*###########
449       Build member list and try to detect obsolete entries 
450       ###########*/
452     $this->memberList = array();
453   
454     /* Walk through all single member entry */
455     foreach($this->member as $dn){
457       /* Object in object list? */
458       if (isset($this->allobjects[$dn])){
459         
460         /* Add this entry to member list, its dn is in allobjects
461             this means it still exists 
462          */
463         $this->memberList[$dn]= $this->allobjects[$dn];
465         /* Remove this from selectable entries */
466         if (isset ($this->objects[$dn])){
467           unset ($this->objects[$dn]);
468         }
470       
471       } else {
473         /* The dn for the current member can't be resolved 
474             it seams that this entry was removed 
475          */ 
476         /* Try to resolv the entry again, if it still fails, display error msg */
477         $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
479         /* It has failed, add entry with type flag I (Invalid)*/
480         if ($ldap->error != "success"){
481           $this->memberList[$dn]= array('text' => _("Non existing dn: ")."$dn","type" => "I");
483         } else {
484           
485           /* Append this entry to our all object list */
486     
487           /* Fetch object */
488           $attrs= $ldap->fetch();
490           $type= $this->getObjectType($attrs);
491           $name= $this->getObjectName($attrs);
493           if (isset($attrs["description"][0])){
494             $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
495           } elseif (isset($attrs["uid"][0])) {
496             $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
497           } else {
498             $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
499           }
500           $this->allobjects[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
501           if(isset($attrs['uid'])){
502             $this->allobjects[$attrs["dn"]]['uid']          = $attrs['uid'];
503           }
505           /* Fill array */
506           if (isset($attrs["description"][0])){
507             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
508           } else {
509             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
510           }
511         }
512       }
513     }
514     uasort ($this->memberList, 'sort_list');
515     reset ($this->memberList);
517     /* Assemble types of currently combined objects */
518     $objectTypes= "";
519     foreach ($this->memberList as $dn => $desc){
521       /* Invalid object? */
522       if ($desc['type'] == 'I'){
523         continue;
524       }
526       /* Fine. Add to list. */
527       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
528         $objectTypes.= $desc['type'];
529       }
530     }
531     $this->gosaGroupObjects= "[$objectTypes]";
532   }
535   function convert_list($input)
536   {
537     $temp= "";
538     $conv= array(  "U" => "select_user.png",
539         "G" => "select_groups.png",
540         "A" => "select_application.png",
541         "D" => "select_department.png",
542         "S" => "select_server.png",
543         "W" => "select_workstation.png",
544         "T" => "select_terminal.png",
545         "F" => "select_phone.png",
546         "I" => "flag.png",
547         "P" => "select_printer.png");
549     foreach ($input as $key => $value){
550       /* Generate output */
551       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
552     }
554     return ($temp);
555   }
558   function getObjectType($attrs)
559   {
560     $type= "I";
562     foreach(array(  "U" => "gosaAccount",
563           "G" => "posixGroup",
564           "A" => "gosaApplication",
565           "D" => "gosaDepartment",
566           "S" => "goServer",
567           "W" => "gotoWorkstation",
568           "T" => "gotoTerminal",
569           "F" => "goFonHardware",
570           "P" => "gotoPrinter") as $index => $class){
571       if (in_array($class, $attrs['objectClass'])){
572         $type= $index;
573         break;
574       }
575     }
577     return ($type);
578   }
581   function getObjectName($attrs)
582   {
583     /* Person? */
584     $name =""; 
585     if (in_array('gosaAccount', $attrs['objectClass'])){
586       if(isset($attrs['sn']) && isset($attrs['givenName'])){
587         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
588       } else {
589         $name= $attrs['uid'][0];
590       }
591     } else {
592       if(isset($attrs["cn"][0])) {
593         $name= $attrs['cn'][0];
594       } else {
595         $name= $attrs['ou'][0];
596       }
597     }
599     return ($name);
600   }
603   function check()
604   {
605     $message= array();
607     /* Permissions for that base? */
608     if ($this->base != ""){
609       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
610     } else {
611       $new_dn= $this->dn;
612     }
615     $ldap = $this->config->get_ldap_link();
616     if($this->dn != $new_dn){
617       $ldap->cat ($new_dn);
618     }
619     
620     if($ldap->count() !=0){
621       $message[]= _("There is already an object with this cn.");
622     } 
624     $ui= get_userinfo();
625     $acl= get_permissions ($new_dn, $ui->subtreeACL);
626     $acl= get_module_permission($acl, "group", $new_dn);
627     if (chkacl($acl, "create") != ""){
628       $message[]= _("You have no permissions to create a group on this 'Base'.");
629     }
631     /* must: cn */
632     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
633       $message[]= "The required field 'Name' is not set.";
634     }
636     /* To many different object types? */
637     if (strlen($this->gosaGroupObjects) > 4){
638       $message[]= _("You can combine two different object types at maximum only!");
639     }
641     return ($message);
642   }
645   /* Save to LDAP */
646   function save()
647   {
648     plugin::save();
650     /* Move members to target array */
651     foreach ($this->member as $key => $desc){
652       $this->attrs['member'][]= $key;
653     }
655     $ldap= $this->config->get_ldap_link();
657     /* New accounts need proper 'dn', propagate it to remaining objects */
658     if ($this->dn == 'new'){
659       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
660     }
662     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
663        new entries. So do a check first... */
664     $ldap->cat ($this->dn);
665     if ($ldap->fetch()){
666       /* Modify needs array() to remove values :-( */
667       if (!count ($this->member)){
668         $this->attrs['member']= array();
669       }
670       $mode= "modify";
671     } else {
672       $mode= "add";
673       $ldap->cd($this->config->current['BASE']);
674       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
675     }
677     /* Write back to ldap */
678     $ldap->cd($this->dn);
679     $ldap->$mode($this->attrs);
681     /* Trigger post signal */
682     $this->handle_post_events($mode);
684     $ret= 0;
685     if (show_ldap_error($ldap->get_error())){
686       $ret= 1;
687     }
689     return ($ret);
690   }
692   function remove_from_parent()
693   {
694     plugin::remove_from_parent();
696     $ldap= $this->config->get_ldap_link();
697     $ldap->rmdir($this->dn);
698     show_ldap_error($ldap->get_error());
700     /* Trigger remove signal */
701     $this->handle_post_events("remove");
702   }
706 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
707 ?>