Code

Initial multi server functionality for phone queues
[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           "_" => "subtrees",
75           "P" => "printers") as $key => $val){
77       if (preg_match("/$key/", $this->gosaGroupObjects)){
78         $ogfilter[$val]= "checked";
79       } else {
80         $ogfilter[$val]= "";
81       }
82     }
83     register_global("ogfilter", $ogfilter);
84  
85     /* Set base */
86     if ($this->dn == "new"){
87       $this->base= $_SESSION['CurrentMainBase'];
88     } else {
89       $this->base= preg_replace("/^[^,]+,".get_groups_ou()."/","",$this->dn);
90     }
92     /* set permissions */
93     $ui= get_userinfo();
94     $acl= get_permissions ($ui->dn, $ui->subtreeACL);
95     $this->acl= get_module_permission($acl, "ogroup", $ui->dn);
97     /* Load member data */
98     $this->reload();
99   }
101   function AddDelMembership($NewMember = false)
102   {
103     if($NewMember){
104       $this->importMember($NewMember);
105       $this->memberList[$NewMember]= $this->objcache[$NewMember];
106       $this->member[$NewMember]= $NewMember;
107       unset ($this->objects[$NewMember]);
108       uasort ($this->memberList, 'sort_list');
109       reset ($this->memberList);
110       $this->reload();
111     }else{
112       /* Delete objects from group */
113       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
114         foreach ($_POST['members'] as $value){
115           $this->objects["$value"]= $this->memberList[$value];
116           unset ($this->memberList["$value"]);
117           unset ($this->member["$value"]);
118           uasort ($this->objects, 'sort_list');
119           reset ($this->objects);
120         }
121         $this->reload();
122       }
124       /* Add objects to group */
125       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
126         foreach ($_POST['objects'] as $value){
127           $this->memberList["$value"]= $this->objects[$value];
128           $this->member["$value"]= $value;
129           unset ($this->objects[$value]);
130           uasort ($this->memberList, 'sort_list');
131           reset ($this->memberList);
132         }
133         $this->reload();
134       }
135     }
136   }
138   function execute()
139   {
140         /* Call parent execute */
141         plugin::execute();
143 //    $this->reload();
145     /* Do we represent a valid group? */
146     if (!$this->is_account){
147       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
148         _("This 'dn' is no object group.")."</b>";
149       return ($display);
150     }
152     /* Delete objects from group */
153     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
154       foreach ($_POST['members'] as $value){
155         if(isset($this->memberList[$value])){
156           $this->objects["$value"]= $this->memberList[$value];
157           unset ($this->memberList["$value"]);
158           unset ($this->member["$value"]);
159           uasort ($this->objects, 'sort_list');
160           reset ($this->objects);
161         }
162       }
163       $this->reload();
164     }
166     /* Add objects to group */
167     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
168       foreach ($_POST['objects'] as $value){
169         if(isset($this->objects[$value])){
170           $this->memberList["$value"]= $this->objects[$value];
171           $this->member["$value"]= $value;
172           unset ($this->objects[$value]);
173           uasort ($this->memberList, 'sort_list');
174           reset ($this->memberList);
175         }
176       }
177       $this->reload();
178     }
180     /* Load templating engine */
181     $smarty= get_smarty();
183     /* Base select dialog */
184     $once = true;
185     foreach($_POST as $name => $value){
186       if(preg_match("/^chooseBase/",$name) && $once){
187         $once = false;
188         $this->dialog = new baseSelectDialog($this->config);
189         $this->dialog->setCurrentBase($this->base);
190       }
191     }
193     /* Dialog handling */
194     if(is_object($this->dialog)){
195       /* Must be called before save_object */
196       $this->dialog->save_object();
198       if($this->dialog->isClosed()){
199         $this->dialog = false;
200       }elseif($this->dialog->isSelected()){
201         $this->base = $this->dialog->isSelected();
202         $this->dialog= false;
203       }else{
204         return($this->dialog->execute());
205       }
206     }
208     /* Add objects? */
209     if (isset($_POST["edit_membership"])){
210       $this->group_dialog= TRUE;
211       $this->dialog= TRUE;
212     }
214     /* Add objects finished? */
215     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
216       $this->group_dialog= FALSE;
217       $this->dialog= FALSE;
218     }
220     /* Manage object add dialog */
221     if ($this->group_dialog){
223       /* Save data */
224       $ogfilter= get_global("ogfilter");
225       foreach( array("dselect", "regex") as $type){
226         if (isset($_POST[$type])){
227           $ogfilter[$type]= $_POST[$type];
228         }
229       }
230       if (isset($_POST['dselect'])){
231         foreach( array("accounts", "groups", "applications", "departments",
232               "servers", "workstations", "terminals", "printers","subtrees",
233               "phones") as $type){
235           if (isset($_POST[$type])) {
236             $ogfilter[$type]= "checked";
237           } else {
238             $ogfilter[$type]= "";
239           }
240         }
241       }
242       if (isset($_GET['search'])){
243         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
244         if ($s == "**"){
245           $s= "*";
246         }
247         $ogfilter['regex']= $s;
248       }
249       register_global("ogfilter", $ogfilter);
250       $this->reload();
252       /* Calculate actual groups */
253       $smarty->assign("objects", $this->convert_list($this->objects));
255       /* Show dialog */
256       $smarty->assign("search_image", get_template_path('images/search.png'));
257       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
258       $smarty->assign("tree_image", get_template_path('images/tree.png'));
259       $smarty->assign("deplist", $this->config->idepartments);
260       $smarty->assign("alphabet", generate_alphabet());
261       foreach( array("dselect", "regex", "accounts", "groups", "applications",
262             "departments", "servers", "workstations", "terminals","subtrees",
263             "printers", "phones") as $type){
264         $smarty->assign("$type", $ogfilter[$type]);
265       }
266       $smarty->assign("hint", print_sizelimit_warning());
267       $smarty->assign("apply", apply_filter());
269       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
270       return ($display);
271     }
273     /* Bases / Departments */
274    
275       if (isset($_POST['base'])){
276         $this->base= $_POST['base'];
277       }
279     /* Assemble combine string */
280     if ($this->gosaGroupObjects == "[]"){
281       $smarty->assign("combinedObjects", _("none"));
282     } elseif (strlen($this->gosaGroupObjects) > 4){
283       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
284     } else {
285       $conv= array(   "U" => _("users"),
286           "G" => _("groups"),
287           "A" => _("applications"),
288           "D" => _("departments"),
289           "S" => _("servers"),
290           "W" => _("workstations"),
291           "T" => _("terminals"),
292           "F" => _("phones"),
293           "P" => _("printers"));
295       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
296       $p1= $conv[$type[0]];
297       error_reporting(0);
298       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
299         $p2= $conv[$type[1]];
300         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
301       } else {
302         $smarty->assign("combinedObjects", "$p1");
303       }
304       error_reporting(E_ALL);
305     }
307     /* Assign variables */
308     $smarty->assign("bases", $this->config->idepartments);
309     $smarty->assign("base_select", $this->base);
310     $smarty->assign("department", $this->department);
311     $smarty->assign("members", $this->convert_list($this->memberList));
313     /* Objects have to be tuned... */
314     $smarty->assign("objects", $this->convert_list($this->objects));
316     /* Fields */
317     foreach ($this->attributes as $val){
318       $smarty->assign("$val", $this->$val);
319       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
320     }
322     /* Assign ACL's */
323     foreach (array("base", "members") as $val){
324       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
325     }
327     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
328   }
331   /* Save data to object */
332   function save_object()
333   {
334     /* Save additional values for possible next step */
335     if (isset($_POST['ogroupedit'])){
336       plugin::save_object();
338       if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
339         $this->base= $_POST["base"];
340       }
342     }
343   }
346   /* (Re-)Load objects */
347   function reload()
348   {
349     /*###########
350       Variable initialisation 
351       ###########*/
353     $this->objects                = array();
354     $this->ui                     = get_userinfo();
355     $filter                       = "";
356     $objectClasses                = array();
357     
358     $ogfilter               = get_global("ogfilter");
359     $regex                  = $ogfilter['regex'];
361     /* Get ldap connection */
362     $ldap= $this->config->get_ldap_link();
363     $ldap->cd ($ogfilter['dselect']);
366     /*###########
367       Generate Filter 
368       ###########*/
370     /* Assemble filter */
371     if ($ogfilter['accounts'] == "checked"){
372       $filter.= "(objectClass=gosaAccount)";
373       $objectClasses["gosaAccount"]     = get_people_ou();
374     }
375     if ($ogfilter['groups'] == "checked"){
376       $filter.= "(objectClass=posixGroup)";
377       $objectClasses["posixGroup"]      = get_groups_ou();
378     }
379     if ($ogfilter['applications'] == "checked"){
380       $filter.= "(objectClass=gosaApplication)";
381       $objectClasses["gosaApplication"] = "ou=apps,";
382     }
383     if ($ogfilter['departments'] == "checked"){
384       $filter.= "(objectClass=gosaDepartment)";
385       $objectClasses["gosaDepartment"]  = "";
386     }
387     if ($ogfilter['servers'] == "checked"){
388       $filter.= "(objectClass=goServer)";
389       $objectClasses["goServer"]        = "ou=servers,ou=systems,";
390     }
391     if ($ogfilter['workstations'] == "checked"){
392       $filter.= "(objectClass=gotoWorkstation)";
393       $objectClasses["gotoWorkstation"] = "ou=workstations,ou=systems,";
394     }
395     if ($ogfilter['terminals'] == "checked"){
396       $filter.= "(objectClass=gotoTerminal)";
397       $objectClasses["gotoTerminal"]    = "ou=terminals,ou=systems,";
398     }
399     if ($ogfilter['printers'] == "checked"){
400       $filter.= "(objectClass=gotoPrinter)";
402       $objectClasses["gotoPrinter"]     = "ou=printers,ou=systems,";
403     }
404     if ($ogfilter['phones'] == "checked"){
405       $filter.= "(objectClass=goFonHardware)";
406       $objectClasses["goFonHardware"]   = "ou=phones,ou=systems,";
407     }
410     /*###########
411       Perform search for selected objectClasses & regex to fill list with objects   
412       ###########*/
414     /* Perform search for selected objectClasses */
415     foreach($objectClasses as $class=> $basedn){
417       if($ogfilter['subtrees'] == "checked"){
418         $ldap->cd($ogfilter['dselect']);
419         $ldap->search("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",
420             array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
421       }else{
422         $ldap->ls("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",$basedn.$ogfilter['dselect'] ,
423             array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
424       }
425       
426       /* fetch results and append them to the list */
427       while($attrs = $ldap->fetch()){
429         $type= $this->getObjectType($attrs);
430         $name= $this->getObjectName($attrs);
432         /* Fill array */
433         if (isset($attrs["description"][0])){
434           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
435         } elseif (isset($attrs["uid"][0])) {
436           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
437         } else {
438           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
439         }
440       }
441     }
442     uasort ($this->objects, 'sort_list');
443     reset ($this->objects);
445     
446     /*###########
447       Build member list and try to detect obsolete entries 
448       ###########*/
450     $this->memberList = array();
451   
452     /* Walk through all single member entry */
453     foreach($this->member as $dn){
454       $this->importMember($dn);
455     }
456     uasort ($this->memberList, 'sort_list');
457     reset ($this->memberList);
459     /* Assemble types of currently combined objects */
460     $objectTypes= "";
461     foreach ($this->memberList as $dn => $desc){
463       /* Invalid object? */
464       if ($desc['type'] == 'I'){
465         continue;
466       }
468       /* Fine. Add to list. */
469       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
470         $objectTypes.= $desc['type'];
471       }
472     }
473     $this->gosaGroupObjects= "[$objectTypes]";
474   }
477   function importMember($dn)
478   {
479     $ldap= $this->config->get_ldap_link();
481     /* Try to resolv the entry again, if it still fails, display error msg */
482     $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "uid", "description", "objectClass"));
484     /* It has failed, add entry with type flag I (Invalid)*/
485     if ($ldap->error != "Success"){
486       $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
487     } else {
488       /* Append this entry to our all object list */
490       /* Fetch object */
491       $attrs= $ldap->fetch();
493       $type= $this->getObjectType($attrs);
494       $name= $this->getObjectName($attrs);
496       if (isset($attrs["description"][0])){
497         $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
498       } elseif (isset($attrs["uid"][0])) {
499         $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
500       } else {
501         $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
502       }
503       $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
504       if(isset($attrs['uid'])){
505         $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
506       }
508       /* Fill array */
509       if (isset($attrs["description"][0])){
510         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
511       } else {
512         $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
513       }
515       $this->memberList[$dn]= $this->objects[$attrs["dn"]];
516     }
517   }
519   function convert_list($input)
520   {
521     $temp= "";
522     $conv= array(  "U" => "select_user.png",
523         "G" => "select_groups.png",
524         "A" => "select_application.png",
525         "D" => "select_department.png",
526         "S" => "select_server.png",
527         "W" => "select_workstation.png",
528         "T" => "select_terminal.png",
529         "F" => "select_phone.png",
530         "I" => "flag.png",
531         "P" => "select_printer.png");
533     foreach ($input as $key => $value){
534       /* Generate output */
535       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
536     }
538     return ($temp);
539   }
542   function getObjectType($attrs)
543   {
544     $type= "I";
546     foreach(array(  "U" => "gosaAccount",
547           "G" => "posixGroup",
548           "A" => "gosaApplication",
549           "D" => "gosaDepartment",
550           "S" => "goServer",
551           "W" => "gotoWorkstation",
552           "T" => "gotoTerminal",
553           "F" => "goFonHardware",
554           "P" => "gotoPrinter") as $index => $class){
555       if (in_array($class, $attrs['objectClass'])){
556         $type= $index;
557         break;
558       }
559     }
561     return ($type);
562   }
565   function getObjectName($attrs)
566   {
567     /* Person? */
568     $name =""; 
569     if (in_array('gosaAccount', $attrs['objectClass'])){
570       if(isset($attrs['sn']) && isset($attrs['givenName'])){
571         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
572       } else {
573         $name= $attrs['uid'][0];
574       }
575     } else {
576       if(isset($attrs["cn"][0])) {
577         $name= $attrs['cn'][0];
578       } else {
579         $name= $attrs['ou'][0];
580       }
581     }
583     return ($name);
584   }
587   function check()
588   {
589     /* Call common method to give check the hook */
590     $message= plugin::check();
592     /* Permissions for that base? */
593     if ($this->base != ""){
594       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
595     } else {
596       $new_dn= $this->dn;
597     }
600     $ldap = $this->config->get_ldap_link();
601     if($this->dn != $new_dn){
602       $ldap->cat ($new_dn, array('dn'));
603     }
604     
605     if($ldap->count() !=0){
606       $message[]= _("There is already an object with this cn.");
607     } 
609     $ui= get_userinfo();
610     $acl= get_permissions ($new_dn, $ui->subtreeACL);
611     $acl= get_module_permission($acl, "group", $new_dn);
612     if (chkacl($acl, "create") != ""){
613       $message[]= _("You have no permissions to create a group on this 'Base'.");
614     }
616     /* must: cn */
617     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
618       $message[]= "The required field 'Name' is not set.";
619     }
621     /* To many different object types? */
622     if (strlen($this->gosaGroupObjects) > 4){
623       $message[]= _("You can combine two different object types at maximum only!");
624     }
626     return ($message);
627   }
630   /* Save to LDAP */
631   function save()
632   {
633     plugin::save();
635     /* Move members to target array */
636     $this->attrs['member'] =array();
637     foreach ($this->member as $key => $desc){
638       $this->attrs['member'][]= @LDAP::fix($key);
639     }
641     $ldap= $this->config->get_ldap_link();
643     /* New accounts need proper 'dn', propagate it to remaining objects */
644     if ($this->dn == 'new'){
645       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
646     }
648     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
649        new entries. So do a check first... */
650     $ldap->cat ($this->dn, array('dn'));
651     if ($ldap->fetch()){
652       /* Modify needs array() to remove values :-( */
653       if (!count ($this->member)){
654         $this->attrs['member']= array();
655       }
656       $mode= "modify";
657     } else {
658       $mode= "add";
659       $ldap->cd($this->config->current['BASE']);
660       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
661     }
663     /* Write back to ldap */
664     $ldap->cd($this->dn);
665     $this->cleanup();
666     $ldap->$mode($this->attrs);
668     /* Trigger post signal */
669     $this->handle_post_events($mode);
671     $ret= 0;
672     if (show_ldap_error($ldap->get_error(), _("Saving object group failed"))){
673       $ret= 1;
674     }
676     return ($ret);
677   }
679   function remove_from_parent()
680   {
681     plugin::remove_from_parent();
683     $ldap= $this->config->get_ldap_link();
684     $ldap->rmdir($this->dn);
685     show_ldap_error($ldap->get_error(), _("Removing object group failed"));
687     /* Trigger remove signal */
688     $this->handle_post_events("remove");
689   }
691   function getCopyDialog()
692   {
693     $str = "";
695     $smarty = get_smarty();
696     $smarty->assign("cn",     $this->cn);
697     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
698     $ret = array();
699     $ret['string'] = $str;
700     $ret['status'] = "";
701     return($ret);
702   }
704   function saveCopyDialog()
705   {
706     if(isset($_POST['cn'])){
707       $this->cn = $_POST['cn'];
708     }
709   }
712 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
713 ?>