Code

Hide tabs frames
[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           $value= @LDAP::convert($value);
50           $this->member["$value"]= "$value";
51         }
52       }
53     }
54     $this->is_account= TRUE;
56     /* Get global filter config */
57     if (!is_global("ogfilter")){
58       $ui= get_userinfo();
59       $base= get_base_from_people($ui->dn);
60       $ogfilter= array( "dselect"       => $base,
61           "regex"           => "*");
62       register_global("ogfilter", $ogfilter);
63     }
64     $ogfilter= get_global('ogfilter');
66     /* Adjust flags */
67     foreach( array(   "U" => "accounts",
68           "G" => "groups",
69           "A" => "applications",
70           "D" => "departments",
71           "S" => "servers",
72           "W" => "workstations",
73           "T" => "terminals",
74           "F" => "phones",
75           "_" => "subtrees",
76           "P" => "printers") as $key => $val){
78       if (preg_match("/$key/", $this->gosaGroupObjects)){
79         $ogfilter[$val]= "checked";
80       } else {
81         $ogfilter[$val]= "";
82       }
83     }
84     register_global("ogfilter", $ogfilter);
85  
86     /* Set base */
87     if ($this->dn == "new"){
88       $this->base= $_SESSION['CurrentMainBase'];
89     } else {
90       $this->base= preg_replace("/^[^,]+,".get_groups_ou()."/","",$this->dn);
91     }
93     /* set permissions */
94     $ui= get_userinfo();
95     $acl= get_permissions ($ui->dn, $ui->subtreeACL);
96     $this->acl= get_module_permission($acl, "ogroup", $ui->dn);
98     /* Load member data */
99     $this->reload();
100   }
102   function AddDelMembership($NewMember = false)
103   {
104     if($NewMember){
105       $this->importMember($NewMember);
106       $this->memberList[$NewMember]= $this->objcache[$NewMember];
107       $this->member[$NewMember]= $NewMember;
108       unset ($this->objects[$NewMember]);
109       uasort ($this->memberList, 'sort_list');
110       reset ($this->memberList);
111       $this->reload();
112     }else{
113       /* Delete objects from group */
114       if (isset($_POST['delete_membership']) && isset($_POST['members'])  && chkacl($this->acl,"gosaGroupObjects") == ""){
115         foreach ($_POST['members'] as $value){
116           $this->objects["$value"]= $this->memberList[$value];
117           unset ($this->memberList["$value"]);
118           unset ($this->member["$value"]);
119           uasort ($this->objects, 'sort_list');
120           reset ($this->objects);
121         }
122         $this->reload();
123       }
125       /* Add objects to group */
126       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])  && chkacl($this->acl,"gosaGroupObjects") == ""){
127         foreach ($_POST['objects'] as $value){
128           $this->memberList["$value"]= $this->objects[$value];
129           $this->member["$value"]= $value;
130           unset ($this->objects[$value]);
131           uasort ($this->memberList, 'sort_list');
132           reset ($this->memberList);
133         }
134         $this->reload();
135       }
136     }
137   }
139   function execute()
140   {
141         /* Call parent execute */
142         plugin::execute();
144 //    $this->reload();
146     /* Do we represent a valid group? */
147     if (!$this->is_account){
148       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
149         _("This 'dn' is no object group.")."</b>";
150       return ($display);
151     }
153     /* Delete objects from group */
154     if (isset($_POST['delete_membership']) && isset($_POST['members']) && chkacl($this->acl,"gosaGroupObjects") == ""){
155       foreach ($_POST['members'] as $value){
156         if(isset($this->memberList[$value])){
157           $this->objects["$value"]= $this->memberList[$value];
158           unset ($this->memberList["$value"]);
159           unset ($this->member["$value"]);
160           uasort ($this->objects, 'sort_list');
161           reset ($this->objects);
162         }
163       }
164       $this->reload();
165     }
167     /* Add objects to group */
168     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
169       foreach ($_POST['objects'] as $value){
170         if(isset($this->objects[$value])){
171           $this->memberList["$value"]= $this->objects[$value];
172           $this->member["$value"]= $value;
173           unset ($this->objects[$value]);
174           uasort ($this->memberList, 'sort_list');
175           reset ($this->memberList);
176         }
177       }
178       $this->reload();
179     }
181     /* Load templating engine */
182     $smarty= get_smarty();
184     /* Base select dialog */
185     $once = true;
186     foreach($_POST as $name => $value){
187       if(preg_match("/^chooseBase/",$name) && $once && chkacl($this->acl,"base") == ""){
188         $once = false;
189         $this->dialog = new baseSelectDialog($this->config);
190         $this->dialog->setCurrentBase($this->base);
191       }
192     }
194     /* Dialog handling */
195     if(is_object($this->dialog)){
196       /* Must be called before save_object */
197       $this->dialog->save_object();
199       if($this->dialog->isClosed()){
200         $this->dialog = false;
201       }elseif($this->dialog->isSelected()){
202         $this->base = $this->dialog->isSelected();
203         $this->dialog= false;
204       }else{
205         return($this->dialog->execute());
206       }
207     }
209     /* Add objects? */
210     if (isset($_POST["edit_membership"])  && chkacl($this->acl,"gosaGroupObjects") == ""){
211       $this->group_dialog= TRUE;
212       $this->dialog= TRUE;
213     }
215     /* Add objects finished? */
216     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
217       $this->group_dialog= FALSE;
218       $this->dialog= FALSE;
219     }
221     /* Manage object add dialog */
222     if ($this->group_dialog){
224       /* Save data */
225       $ogfilter= get_global("ogfilter");
226       foreach( array("dselect", "regex") as $type){
227         if (isset($_POST[$type])){
228           $ogfilter[$type]= $_POST[$type];
229         }
230       }
231       if (isset($_POST['dselect'])){
232         foreach( array("accounts", "groups", "applications", "departments",
233               "servers", "workstations", "terminals", "printers","subtrees",
234               "phones") as $type){
236           if (isset($_POST[$type])) {
237             $ogfilter[$type]= "checked";
238           } else {
239             $ogfilter[$type]= "";
240           }
241         }
242       }
243       if (isset($_GET['search'])){
244         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
245         if ($s == "**"){
246           $s= "*";
247         }
248         $ogfilter['regex']= $s;
249       }
250       register_global("ogfilter", $ogfilter);
251       $this->reload();
253       /* Calculate actual groups */
254       $smarty->assign("objects", $this->convert_list($this->objects));
256       /* Show dialog */
257       $smarty->assign("search_image", get_template_path('images/search.png'));
258       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
259       $smarty->assign("tree_image", get_template_path('images/tree.png'));
260       $smarty->assign("deplist", $this->config->idepartments);
261       $smarty->assign("alphabet", generate_alphabet());
262       foreach( array("dselect", "regex", "accounts", "groups", "applications",
263             "departments", "servers", "workstations", "terminals","subtrees",
264             "printers", "phones") as $type){
265         $smarty->assign("$type", $ogfilter[$type]);
266       }
267       $smarty->assign("hint", print_sizelimit_warning());
268       $smarty->assign("apply", apply_filter());
270       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
271       return ($display);
272     }
274     /* Bases / Departments */
275     if (isset($_POST['base']) && chkacl($this->acl,"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       }
341     }
342   }
345   /* (Re-)Load objects */
346   function reload()
347   {
348     /*###########
349       Variable initialisation 
350       ###########*/
352     $this->objects                = array();
353     $this->ui                     = get_userinfo();
354     $filter                       = "";
355     $objectClasses                = array();
356     
357     $ogfilter               = get_global("ogfilter");
358     $regex                  = $ogfilter['regex'];
360     /* Get ldap connection */
361     $ldap= $this->config->get_ldap_link();
362     $ldap->cd ($ogfilter['dselect']);
365     /*###########
366       Generate Filter 
367       ###########*/
369     /* Assemble filter */
370     if ($ogfilter['accounts'] == "checked"){
371       $filter.= "(objectClass=gosaAccount)";
372       $objectClasses["gosaAccount"]     = get_people_ou();
373     }
374     if ($ogfilter['groups'] == "checked"){
375       $filter.= "(objectClass=posixGroup)";
376       $objectClasses["posixGroup"]      = get_groups_ou();
377     }
378     if ($ogfilter['applications'] == "checked"){
379       $filter.= "(objectClass=gosaApplication)";
380       $objectClasses["gosaApplication"] = "ou=apps,";
381     }
382     if ($ogfilter['departments'] == "checked"){
383       $filter.= "(objectClass=gosaDepartment)";
384       $objectClasses["gosaDepartment"]  = "";
385     }
386     if ($ogfilter['servers'] == "checked"){
387       $filter.= "(objectClass=goServer)";
388       $objectClasses["goServer"]        = "ou=servers,ou=systems,";
389     }
390     if ($ogfilter['workstations'] == "checked"){
391       $filter.= "(objectClass=gotoWorkstation)";
392       $objectClasses["gotoWorkstation"] = "ou=workstations,ou=systems,";
393     }
394     if ($ogfilter['terminals'] == "checked"){
395       $filter.= "(objectClass=gotoTerminal)";
396       $objectClasses["gotoTerminal"]    = "ou=terminals,ou=systems,";
397     }
398     if ($ogfilter['printers'] == "checked"){
399       $filter.= "(objectClass=gotoPrinter)";
401       $objectClasses["gotoPrinter"]     = "ou=printers,ou=systems,";
402     }
403     if ($ogfilter['phones'] == "checked"){
404       $filter.= "(objectClass=goFonHardware)";
405       $objectClasses["goFonHardware"]   = "ou=phones,ou=systems,";
406     }
409     /*###########
410       Perform search for selected objectClasses & regex to fill list with objects   
411       ###########*/
413     /* Perform search for selected objectClasses */
414     foreach($objectClasses as $class=> $basedn){
416       if($ogfilter['subtrees'] == "checked"){
417         $ldap->cd($ogfilter['dselect']);
418         $ldap->search("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",
419             array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
420       }else{
421         $ldap->ls("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",$basedn.$ogfilter['dselect'] ,
422             array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
423       }
424       
425       /* fetch results and append them to the list */
426       while($attrs = $ldap->fetch()){
428         $type= $this->getObjectType($attrs);
429         $name= $this->getObjectName($attrs);
431         /* Fill array */
432         if (isset($attrs["description"][0])){
433           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
434         } elseif (isset($attrs["uid"][0])) {
435           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
436         } else {
437           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
438         }
439       }
440     }
441     uasort ($this->objects, 'sort_list');
442     reset ($this->objects);
444     
445     /*###########
446       Build member list and try to detect obsolete entries 
447       ###########*/
449     $this->memberList = array();
450   
451     /* Walk through all single member entry */
452     foreach($this->member as $dn){
453       $this->importMember($dn);
454     }
455     uasort ($this->memberList, 'sort_list');
456     reset ($this->memberList);
458     /* Assemble types of currently combined objects */
459     $objectTypes= "";
460     foreach ($this->memberList as $dn => $desc){
462       /* Invalid object? */
463       if ($desc['type'] == 'I'){
464         continue;
465       }
467       /* Fine. Add to list. */
468       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
469         $objectTypes.= $desc['type'];
470       }
471     }
472     $this->gosaGroupObjects= "[$objectTypes]";
473   }
476   function importMember($dn)
477   {
478     $ldap= $this->config->get_ldap_link();
480     /* Try to resolv the entry again, if it still fails, display error msg */
481     $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "uid", "description", "objectClass"));
483     /* It has failed, add entry with type flag I (Invalid)*/
484     if ($ldap->error != "Success"){
485       $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
486     } else {
487       /* Append this entry to our all object list */
489       /* Fetch object */
490       $attrs= $ldap->fetch();
492       $type= $this->getObjectType($attrs);
493       $name= $this->getObjectName($attrs);
495       if (isset($attrs["description"][0])){
496         $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
497       } elseif (isset($attrs["uid"][0])) {
498         $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
499       } else {
500         $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
501       }
502       $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
503       if(isset($attrs['uid'])){
504         $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
505       }
507       /* Fill array */
508       if (isset($attrs["description"][0])){
509         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
510       } else {
511         $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
512       }
514       $this->memberList[$dn]= $this->objects[$attrs["dn"]];
515     }
516   }
518   function convert_list($input)
519   {
520     $temp= "";
521     $conv= array(  "U" => "select_user.png",
522         "G" => "select_groups.png",
523         "A" => "select_application.png",
524         "D" => "select_department.png",
525         "S" => "select_server.png",
526         "W" => "select_workstation.png",
527         "T" => "select_terminal.png",
528         "F" => "select_phone.png",
529         "I" => "flag.png",
530         "P" => "select_printer.png");
532     foreach ($input as $key => $value){
533       /* Generate output */
534       $temp.= "<option title='".addslashes($key)."' value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
535     }
537     return ($temp);
538   }
541   function getObjectType($attrs)
542   {
543     $type= "I";
545     foreach(array(  "U" => "gosaAccount",
546           "G" => "posixGroup",
547           "A" => "gosaApplication",
548           "D" => "gosaDepartment",
549           "S" => "goServer",
550           "W" => "gotoWorkstation",
551           "T" => "gotoTerminal",
552           "F" => "goFonHardware",
553           "P" => "gotoPrinter") as $index => $class){
554       if (in_array($class, $attrs['objectClass'])){
555         $type= $index;
556         break;
557       }
558     }
560     return ($type);
561   }
564   function getObjectName($attrs)
565   {
566     /* Person? */
567     $name =""; 
568     if (in_array('gosaAccount', $attrs['objectClass'])){
569       if(isset($attrs['sn']) && isset($attrs['givenName'])){
570         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
571       } else {
572         $name= $attrs['uid'][0];
573       }
574     } else {
575       if(isset($attrs["cn"][0])) {
576         $name= $attrs['cn'][0];
577       } else {
578         $name= $attrs['ou'][0];
579       }
580     }
582     return ($name);
583   }
586   function check()
587   {
588     /* Call common method to give check the hook */
589     $message= plugin::check();
591     /* Permissions for that base? */
592     if ($this->base != ""){
593       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
594     } else {
595       $new_dn= $this->dn;
596     }
599     $ldap = $this->config->get_ldap_link();
600     if($this->dn != $new_dn){
601       $ldap->cat ($new_dn, array('dn'));
602     }
603     
604     if($ldap->count() !=0){
605       $message[]= _("There is already an object with this cn.");
606     } 
608     $ui= get_userinfo();
609     $acl= get_permissions ($new_dn, $ui->subtreeACL);
610     $acl= get_module_permission($acl, "group", $new_dn);
611     if (chkacl($acl, "create") != "" && $this->dn=="new"){
612       $message[]= _("You have no permissions to create a group on this 'Base'.");
613     }
615     /* must: cn */
616     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
617       $message[]= "The required field 'Name' is not set.";
618     }
620     /* To many different object types? */
621     if (strlen($this->gosaGroupObjects) > 4){
622       $message[]= _("You can combine two different object types at maximum only!");
623     }
625     return ($message);
626   }
629   /* Save to LDAP */
630   function save()
631   {
632     plugin::save();
634     /* Move members to target array */
635     $this->attrs['member'] =array();
636     foreach ($this->member as $key => $desc){
637       $this->attrs['member'][]= @LDAP::fix($key);
638     }
640     $ldap= $this->config->get_ldap_link();
642     /* New accounts need proper 'dn', propagate it to remaining objects */
643     if ($this->dn == 'new'){
644       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
645     }
647     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
648        new entries. So do a check first... */
649     $ldap->cat ($this->dn, array('dn'));
650     if ($ldap->fetch()){
651       /* Modify needs array() to remove values :-( */
652       if (!count ($this->member)){
653         $this->attrs['member']= array();
654       }
655       $mode= "modify";
656     } else {
657       $mode= "add";
658       $ldap->cd($this->config->current['BASE']);
659       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
660     }
662     /* Write back to ldap */
663     $ldap->cd($this->dn);
664     $this->cleanup();
665     $ldap->$mode($this->attrs);
667     /* Trigger post signal */
668     $this->handle_post_events($mode);
670     $ret= 0;
671     if (show_ldap_error($ldap->get_error(), _("Saving object group failed"))){
672       $ret= 1;
673     }
675     return ($ret);
676   }
678   function remove_from_parent()
679   {
680     plugin::remove_from_parent();
682     $ldap= $this->config->get_ldap_link();
683     $ldap->rmdir($this->dn);
684     show_ldap_error($ldap->get_error(), _("Removing object group failed"));
686     /* Trigger remove signal */
687     $this->handle_post_events("remove");
688   }
690   function getCopyDialog()
691   {
692     $str = "";
694     $smarty = get_smarty();
695     $smarty->assign("cn",     $this->cn);
696     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
697     $ret = array();
698     $ret['string'] = $str;
699     $ret['status'] = "";
700     return($ret);
701   }
703   function saveCopyDialog()
704   {
705     if(isset($_POST['cn'])){
706       $this->cn = $_POST['cn'];
707     }
708   }
711 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
712 ?>