Code

Recent changes in win2003sp1 required some fixes (backward compatible).
[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 $objcache= 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","member");
36   var $objectclasses= array("top", "gosaGroupOfNames");
38   function ogroup ($config, $dn= NULL)
39   {
40     plugin::plugin ($config, $dn);
41     $this->orig_dn= $dn;
43     $this->member = array();
45     /* Load member objects */
46     if (isset($this->attrs['member'])){
47       foreach ($this->attrs['member'] as $key => $value){
48         if ("$key" != "count"){
49           $this->member["$value"]= "$value";
50         }
51       }
52     }
53     $this->is_account= TRUE;
55     /* Get global filter config */
56     if (!is_global("ogfilter")){
57       $ui= get_userinfo();
58       $base= get_base_from_people($ui->dn);
59       $ogfilter= array( "dselect"       => $base,
60           "regex"           => "*");
61       register_global("ogfilter", $ogfilter);
62     }
63     $ogfilter= get_global('ogfilter');
65     /* Adjust flags */
66     foreach( array(   "U" => "accounts",
67           "G" => "groups",
68           "A" => "applications",
69           "D" => "departments",
70           "S" => "servers",
71           "W" => "workstations",
72           "T" => "terminals",
73           "F" => "phones",
74           "P" => "printers") as $key => $val){
76       if (preg_match("/$key/", $this->gosaGroupObjects)){
77         $ogfilter[$val]= "checked";
78       } else {
79         $ogfilter[$val]= "";
80       }
81     }
82     register_global("ogfilter", $ogfilter);
83   
84     if(isset($_SESSION['CurrentMainBase'])){
85      $this->base = $_SESSION['CurrentMainBase'];
86     }
88     /* Load member data */
89     $this->reload();
90   }
92   function AddDelMembership($NewMember = false){
94     if($NewMember){
95       $this->memberList[$NewMember]= $this->objcache[$NewMember];
96       $this->member[$NewMember]= $NewMember;
97       unset ($this->objects[$NewMember]);
98       uasort ($this->memberList, 'sort_list');
99       reset ($this->memberList);
100       $this->reload();
101     }else{
102       /* Delete objects from group */
103       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
104         foreach ($_POST['members'] as $value){
105           $this->objects["$value"]= $this->memberList[$value];
106           unset ($this->memberList["$value"]);
107           unset ($this->member["$value"]);
108           uasort ($this->objects, 'sort_list');
109           reset ($this->objects);
110         }
111         $this->reload();
112       }
114       /* Add objects to group */
115       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
116         foreach ($_POST['objects'] as $value){
117           $this->memberList["$value"]= $this->objects[$value];
118           $this->member["$value"]= $value;
119           unset ($this->objects[$value]);
120           uasort ($this->memberList, 'sort_list');
121           reset ($this->memberList);
122         }
123         $this->reload();
124       }
125     }
126   }
128   function execute()
129   {
130         /* Call parent execute */
131         plugin::execute();
133 //    $this->reload();
135     /* Do we represent a valid group? */
136     if (!$this->is_account){
137       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
138         _("This 'dn' is no object group.")."</b>";
139       return ($display);
140     }
142     /* Delete objects from group */
143     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
144       foreach ($_POST['members'] as $value){
145         if(isset($this->memberList[$value])){
146           $this->objects["$value"]= $this->memberList[$value];
147           unset ($this->memberList["$value"]);
148           unset ($this->member["$value"]);
149           uasort ($this->objects, 'sort_list');
150           reset ($this->objects);
151         }
152       }
153       $this->reload();
154     }
156     /* Add objects to group */
157     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
158       foreach ($_POST['objects'] as $value){
159         if(isset($this->objects[$value])){
160           $this->memberList["$value"]= $this->objects[$value];
161           $this->member["$value"]= $value;
162           unset ($this->objects[$value]);
163           uasort ($this->memberList, 'sort_list');
164           reset ($this->memberList);
165         }
166       }
167       $this->reload();
168     }
170     /* Load templating engine */
171     $smarty= get_smarty();
173     $tmp = $this->plInfo();
174     foreach($tmp['plProvidedAcls'] as $name => $translation){
175       $smarty->assign($name."ACL",$this->getacl($name));
176     }
178     /* Create base acls */
179     $baseACL = $this->getacl("base",(!is_object($this->parent) && !isset($_SESSION['edit'])));
180     if(!$this->acl_is_moveable()) {
181       $baseACL = preg_replace("/w/","",$baseACL);
182     }
183     $smarty->assign("baseACL",          $baseACL);
185     /* Base select dialog */
186     $once = true;
187     foreach($_POST as $name => $value){
188       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
189         $once = false;
190         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
191         $this->dialog->setCurrentBase($this->base);
192       }
193     }
195     /* Dialog handling */
196     if(is_object($this->dialog) && $this->acl_is_moveable()){
197       /* Must be called before save_object */
198       $this->dialog->save_object();
200       if($this->dialog->isClosed()){
201         $this->dialog = false;
202       }elseif($this->dialog->isSelected()){
203         $this->base = $this->dialog->isSelected();
204         $this->dialog= false;
205       }else{
206         return($this->dialog->execute());
207       }
208     }
210     /* Add objects? */
211     if (isset($_POST["edit_membership"])){
212       $this->group_dialog= TRUE;
213       $this->dialog= TRUE;
214     }
216     /* Add objects finished? */
217     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
218       $this->group_dialog= FALSE;
219       $this->dialog= FALSE;
220     }
222     /* Manage object add dialog */
223     if ($this->group_dialog){
225       /* Save data */
226       $ogfilter= get_global("ogfilter");
227       foreach( array("dselect", "regex") as $type){
228         if (isset($_POST[$type])){
229           $ogfilter[$type]= $_POST[$type];
230         }
231       }
232       if (isset($_POST['dselect'])){
233         foreach( array("accounts", "groups", "applications", "departments",
234               "servers", "workstations", "terminals", "printers",
235               "phones") as $type){
237           if (isset($_POST[$type])) {
238             $ogfilter[$type]= "checked";
239           } else {
240             $ogfilter[$type]= "";
241           }
242         }
243       }
244       if (isset($_GET['search'])){
245         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
246         if ($s == "**"){
247           $s= "*";
248         }
249         $ogfilter['regex']= $s;
250       }
251       register_global("ogfilter", $ogfilter);
252       $this->reload();
254       /* Calculate actual groups */
255       $smarty->assign("objects", $this->convert_list($this->objects));
257       /* Show dialog */
258       $smarty->assign("search_image", get_template_path('images/search.png'));
259       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
260       $smarty->assign("tree_image", get_template_path('images/tree.png'));
261       $smarty->assign("deplist", $this->config->idepartments);
262       $smarty->assign("alphabet", generate_alphabet());
263       foreach( array("dselect", "regex", "accounts", "groups", "applications",
264             "departments", "servers", "workstations", "terminals",
265             "printers", "phones") as $type){
266         $smarty->assign("$type", $ogfilter[$type]);
267       }
268       $smarty->assign("hint", print_sizelimit_warning());
269       $smarty->assign("apply", apply_filter());
271       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
272       return ($display);
273     }
275     /* Bases / Departments */
276       if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
277         $this->base= $_POST['base'];
278       }
280     /* Assemble combine string */
281     if ($this->gosaGroupObjects == "[]"){
282       $smarty->assign("combinedObjects", _("none"));
283     } elseif (strlen($this->gosaGroupObjects) > 4){
284       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
285     } else {
286       $conv= array(   "U" => _("users"),
287           "G" => _("groups"),
288           "A" => _("applications"),
289           "D" => _("departments"),
290           "S" => _("servers"),
291           "W" => _("workstations"),
292           "T" => _("terminals"),
293           "F" => _("phones"),
294           "P" => _("printers"));
296       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
297       $p1= $conv[$type[0]];
298       error_reporting(0);
299       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
300         $p2= $conv[$type[1]];
301         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
302       } else {
303         $smarty->assign("combinedObjects", "$p1");
304       }
305       error_reporting(E_ALL);
306     }
308     /* Assign variables */
309     $smarty->assign("bases", $this->get_allowed_bases());
310     $smarty->assign("base_select", $this->base);
311     $smarty->assign("department", $this->department);
312     $smarty->assign("members", $this->convert_list($this->memberList));
314     /* Objects have to be tuned... */
315     $smarty->assign("objects", $this->convert_list($this->objects));
317     /* Fields */
318     foreach ($this->attributes as $val){
319       $smarty->assign("$val", $this->$val);
320     }
322     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
323   }
326   /* Save data to object */
327   function save_object()
328   {
329     /* Save additional values for possible next step */
330     if (isset($_POST['ogroupedit'])){
331       plugin::save_object();
332   
333     }
334   }
337   /* (Re-)Load objects */
338   function reload()
339   {
340     /*###########
341       Variable initialisation 
342       ###########*/
344     $this->objects                = array();
345     $this->ui                     = get_userinfo();
346     $filter                       = "";
347     $objectClasses                = array();
348     
349     $ogfilter               = get_global("ogfilter");
350     $regex                  = $ogfilter['regex'];
352     /* Get ldap connection */
353     $ldap= $this->config->get_ldap_link();
354     $ldap->cd ($ogfilter['dselect']);
357     /*###########
358       Generate Filter 
359       ###########*/
361     /* Assemble filter */
362     if ($ogfilter['accounts'] == "checked"){
363       $filter.= "(objectClass=gosaAccount)";
364       $objectClasses["gosaAccount"]     = get_people_ou();
365     }
366     if ($ogfilter['groups'] == "checked"){
367       $filter.= "(objectClass=posixGroup)";
368       $objectClasses["posixGroup"]      = get_groups_ou();
369     }
370     if ($ogfilter['applications'] == "checked"){
371       $filter.= "(objectClass=gosaApplication)";
372       $objectClasses["gosaApplication"] = "ou=apps,";
373     }
374     if ($ogfilter['departments'] == "checked"){
375       $filter.= "(objectClass=gosaDepartment)";
376       $objectClasses["gosaDepartment"]  = "";
377     }
378     if ($ogfilter['servers'] == "checked"){
379       $filter.= "(objectClass=goServer)";
380       $objectClasses["goServer"]        = "ou=servers,ou=systems,";
381     }
382     if ($ogfilter['workstations'] == "checked"){
383       $filter.= "(objectClass=gotoWorkstation)";
384       $objectClasses["gotoWorkstation"] = "ou=workstations,ou=systems,";
385     }
386     if ($ogfilter['terminals'] == "checked"){
387       $filter.= "(objectClass=gotoTerminal)";
388       $objectClasses["gotoTerminal"]    = "ou=terminals,ou=systems,";
389     }
390     if ($ogfilter['printers'] == "checked"){
391       $filter.= "(objectClass=gotoPrinter)";
393       $objectClasses["gotoPrinter"]     = "ou=printers,ou=systems,";
394     }
395     if ($ogfilter['phones'] == "checked"){
396       $filter.= "(objectClass=goFonHardware)";
397       $objectClasses["goFonHardware"]   = "ou=phones,ou=systems,";
398     }
401     /*###########
402       Perform search for selected objectClasses & regex to fill list with objects   
403       ###########*/
405     /* Perform search for selected objectClasses */
406     foreach($objectClasses as $class=> $basedn){
407       $ldap->ls("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",$basedn.$ogfilter['dselect'] ,
408           array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
409       
410       /* fetch results and append them to the list */
411       while($attrs = $ldap->fetch()){
413         $type= $this->getObjectType($attrs);
414         $name= $this->getObjectName($attrs);
416         /* Fill array */
417         if (isset($attrs["description"][0])){
418           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
419         } elseif (isset($attrs["uid"][0])) {
420           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
421         } else {
422           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
423         }
424       }
425     }
426     uasort ($this->objects, 'sort_list');
427     reset ($this->objects);
429     
430     /*###########
431       Build member list and try to detect obsolete entries 
432       ###########*/
434     $this->memberList = array();
435   
436     /* Walk through all single member entry */
437     foreach($this->member as $dn){
439       /* The dn for the current member can't be resolved 
440          it seams that this entry was removed 
441        */ 
442       /* Try to resolv the entry again, if it still fails, display error msg */
443       $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
445       /* It has failed, add entry with type flag I (Invalid)*/
446       if ($ldap->error != "Success"){
447         $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
449       } else {
451         /* Append this entry to our all object list */
453         /* Fetch object */
454         $attrs= $ldap->fetch();
456         $type= $this->getObjectType($attrs);
457         $name= $this->getObjectName($attrs);
459         if (isset($attrs["description"][0])){
460           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
461         } elseif (isset($attrs["uid"][0])) {
462           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
463         } else {
464           $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
465         }
466         $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
467         if(isset($attrs['uid'])){
468           $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
469         }
471         /* Fill array */
472         if (isset($attrs["description"][0])){
473           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
474         } else {
475           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
476         }
478         $this->memberList[$dn]= $this->objects[$attrs["dn"]];
479       }
480     }
481     uasort ($this->memberList, 'sort_list');
482     reset ($this->memberList);
484     /* Assemble types of currently combined objects */
485     $objectTypes= "";
486     foreach ($this->memberList as $dn => $desc){
488       /* Invalid object? */
489       if ($desc['type'] == 'I'){
490         continue;
491       }
493       /* Fine. Add to list. */
494       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
495         $objectTypes.= $desc['type'];
496       }
497     }
498     $this->gosaGroupObjects= "[$objectTypes]";
499   }
502   function convert_list($input)
503   {
504     $temp= "";
505     $conv= array(  "U" => "select_user.png",
506         "G" => "select_groups.png",
507         "A" => "select_application.png",
508         "D" => "select_department.png",
509         "S" => "select_server.png",
510         "W" => "select_workstation.png",
511         "T" => "select_terminal.png",
512         "F" => "select_phone.png",
513         "I" => "flag.png",
514         "P" => "select_printer.png");
516     foreach ($input as $key => $value){
517       /* Generate output */
518       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
519     }
521     return ($temp);
522   }
525   function getObjectType($attrs)
526   {
527     $type= "I";
529     foreach(array(  "U" => "gosaAccount",
530           "G" => "posixGroup",
531           "A" => "gosaApplication",
532           "D" => "gosaDepartment",
533           "S" => "goServer",
534           "W" => "gotoWorkstation",
535           "T" => "gotoTerminal",
536           "F" => "goFonHardware",
537           "P" => "gotoPrinter") as $index => $class){
538       if (in_array($class, $attrs['objectClass'])){
539         $type= $index;
540         break;
541       }
542     }
544     return ($type);
545   }
548   function getObjectName($attrs)
549   {
550     /* Person? */
551     $name =""; 
552     if (in_array('gosaAccount', $attrs['objectClass'])){
553       if(isset($attrs['sn']) && isset($attrs['givenName'])){
554         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
555       } else {
556         $name= $attrs['uid'][0];
557       }
558     } else {
559       if(isset($attrs["cn"][0])) {
560         $name= $attrs['cn'][0];
561       } else {
562         $name= $attrs['ou'][0];
563       }
564     }
566     return ($name);
567   }
570   function check()
571   {
572     /* Call common method to give check the hook */
573     $message= plugin::check();
575     /* Permissions for that base? */
576     if ($this->base != ""){
577       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
578     } else {
579       $new_dn= $this->dn;
580     }
583     $ldap = $this->config->get_ldap_link();
584     if($this->dn != $new_dn){
585       $ldap->cat ($new_dn, array('dn'));
586     }
587     
588     if($ldap->count() !=0){
589       $message[]= _("There is already an object with this cn.");
590     } 
592     if ($this->orig_dn == "new" && !$this->acl_is_createable()){
593       $message[]= _("You have no permissions to create a group on this 'Base'.");
594     }
596     /* must: cn */
597     if ($this->cn == "" && $this->acl_is_writeable("cn")){
598       $message[]= "The required field 'Name' is not set.";
599     }
601     /* To many different object types? */
602     if (strlen($this->gosaGroupObjects) > 4){
603       $message[]= _("You can combine two different object types at maximum only!");
604     }
606     return ($message);
607   }
610   /* Save to LDAP */
611   function save()
612   {
613     plugin::save();
615     /* Move members to target array */
616     $this->attrs['member'] =array();
617     foreach ($this->member as $key => $desc){
618       $this->attrs['member'][]= @LDAP::fix($key);
619     }
621     $ldap= $this->config->get_ldap_link();
623     /* New accounts need proper 'dn', propagate it to remaining objects */
624     if ($this->dn == 'new'){
625       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
626     }
628     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
629        new entries. So do a check first... */
630     $ldap->cat ($this->dn, array('dn'));
631     if ($ldap->fetch()){
632       /* Modify needs array() to remove values :-( */
633       if (!count ($this->member)){
634         $this->attrs['member']= array();
635       }
636       $mode= "modify";
637     } else {
638       $mode= "add";
639       $ldap->cd($this->config->current['BASE']);
640       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
641     }
643     /* Write back to ldap */
644     $ldap->cd($this->dn);
645     $this->cleanup();
646     $ldap->$mode($this->attrs);
648     /* Trigger post signal */
649     $this->handle_post_events($mode);
651     $ret= 0;
652     if (show_ldap_error($ldap->get_error(), sprintf(_("Saving of object group/generic with dn '%s' failed."),$this->dn))){
653       $ret= 1;
654     }
656     return ($ret);
657   }
659   function remove_from_parent()
660   {
661     plugin::remove_from_parent();
663     $ldap= $this->config->get_ldap_link();
664     $ldap->rmdir($this->dn);
665     show_ldap_error($ldap->get_error(), sprintf(_("Removing of object group/generic with dn '%s' failed."),$this->dn));
667     /* Trigger remove signal */
668     $this->handle_post_events("remove");
669   }
671   function getCopyDialog()
672   {
673     $str  = "";
674     $str .= _("Group name");
675     $str .= "&nbsp;<input type='text' name='cn' value='".$this->cn."'>";
676     return($str);
677   }
679   function saveCopyDialog()
680   {
681     if(isset($_POST['cn'])){
682       $this->cn = $_POST['cn'];
683     }
684   }
687   function plInfo()
688   {
689     return (array(
690           "plShortName"   => _("Generic"),
691           "plDescription" => _("Object group generic"),
692           "plSelfModify"  => FALSE,
693           "plDepends"     => array(),
694           "plPriority"    => 0,
695           "plSection"     => array("administration"),
696           "plCategory"    => array("ogroups" => array("description"  => _("Object groups"),
697                                                       "objectClass"  => "gosaGroupOfNames")),
698           "plProvidedAcls"=> array(
699             "cn"                => _("Name"),
700             "base"              => _("Base"),
701             "description"       => _("Description"),
702             "member"            => _("Member"))
703           ));
704   }
707 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
708 ?>