Code

Fixed non-closing <script> tag
[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     /* Set base */
85     if ($this->dn == "new"){
86       $this->base= $_SESSION['CurrentMainBase'];
87     } else {
88       $this->base= preg_replace("/^[^,]+,".get_groups_ou()."/","",$this->dn);
89     }
91     /* set permissions */
92     $ui= get_userinfo();
93     $acl= get_permissions ($ui->dn, $ui->subtreeACL);
94     $this->acl= get_module_permission($acl, "ogroup", $ui->dn);
96     /* Load member data */
97     $this->reload();
98   }
100   function AddDelMembership($NewMember = false)
101   {
102     if($NewMember){
103       $this->importMember($NewMember);
104       $this->memberList[$NewMember]= $this->objcache[$NewMember];
105       $this->member[$NewMember]= $NewMember;
106       unset ($this->objects[$NewMember]);
107       uasort ($this->memberList, 'sort_list');
108       reset ($this->memberList);
109       $this->reload();
110     }else{
111       /* Delete objects from group */
112       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
113         foreach ($_POST['members'] as $value){
114           $this->objects["$value"]= $this->memberList[$value];
115           unset ($this->memberList["$value"]);
116           unset ($this->member["$value"]);
117           uasort ($this->objects, 'sort_list');
118           reset ($this->objects);
119         }
120         $this->reload();
121       }
123       /* Add objects to group */
124       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
125         foreach ($_POST['objects'] as $value){
126           $this->memberList["$value"]= $this->objects[$value];
127           $this->member["$value"]= $value;
128           unset ($this->objects[$value]);
129           uasort ($this->memberList, 'sort_list');
130           reset ($this->memberList);
131         }
132         $this->reload();
133       }
134     }
135   }
137   function execute()
138   {
139         /* Call parent execute */
140         plugin::execute();
142 //    $this->reload();
144     /* Do we represent a valid group? */
145     if (!$this->is_account){
146       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
147         _("This 'dn' is no object group.")."</b>";
148       return ($display);
149     }
151     /* Delete objects from group */
152     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
153       foreach ($_POST['members'] as $value){
154         if(isset($this->memberList[$value])){
155           $this->objects["$value"]= $this->memberList[$value];
156           unset ($this->memberList["$value"]);
157           unset ($this->member["$value"]);
158           uasort ($this->objects, 'sort_list');
159           reset ($this->objects);
160         }
161       }
162       $this->reload();
163     }
165     /* Add objects to group */
166     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
167       foreach ($_POST['objects'] as $value){
168         if(isset($this->objects[$value])){
169           $this->memberList["$value"]= $this->objects[$value];
170           $this->member["$value"]= $value;
171           unset ($this->objects[$value]);
172           uasort ($this->memberList, 'sort_list');
173           reset ($this->memberList);
174         }
175       }
176       $this->reload();
177     }
179     /* Load templating engine */
180     $smarty= get_smarty();
182     /* Base select dialog */
183     $once = true;
184     foreach($_POST as $name => $value){
185       if(preg_match("/^chooseBase/",$name) && $once){
186         $once = false;
187         $this->dialog = new baseSelectDialog($this->config);
188         $this->dialog->setCurrentBase($this->base);
189       }
190     }
192     /* Dialog handling */
193     if(is_object($this->dialog)){
194       /* Must be called before save_object */
195       $this->dialog->save_object();
197       if($this->dialog->isClosed()){
198         $this->dialog = false;
199       }elseif($this->dialog->isSelected()){
200         $this->base = $this->dialog->isSelected();
201         $this->dialog= false;
202       }else{
203         return($this->dialog->execute());
204       }
205     }
207     /* Add objects? */
208     if (isset($_POST["edit_membership"])){
209       $this->group_dialog= TRUE;
210       $this->dialog= TRUE;
211     }
213     /* Add objects finished? */
214     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
215       $this->group_dialog= FALSE;
216       $this->dialog= FALSE;
217     }
219     /* Manage object add dialog */
220     if ($this->group_dialog){
222       /* Save data */
223       $ogfilter= get_global("ogfilter");
224       foreach( array("dselect", "regex") as $type){
225         if (isset($_POST[$type])){
226           $ogfilter[$type]= $_POST[$type];
227         }
228       }
229       if (isset($_POST['dselect'])){
230         foreach( array("accounts", "groups", "applications", "departments",
231               "servers", "workstations", "terminals", "printers",
232               "phones") as $type){
234           if (isset($_POST[$type])) {
235             $ogfilter[$type]= "checked";
236           } else {
237             $ogfilter[$type]= "";
238           }
239         }
240       }
241       if (isset($_GET['search'])){
242         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
243         if ($s == "**"){
244           $s= "*";
245         }
246         $ogfilter['regex']= $s;
247       }
248       register_global("ogfilter", $ogfilter);
249       $this->reload();
251       /* Calculate actual groups */
252       $smarty->assign("objects", $this->convert_list($this->objects));
254       /* Show dialog */
255       $smarty->assign("search_image", get_template_path('images/search.png'));
256       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
257       $smarty->assign("tree_image", get_template_path('images/tree.png'));
258       $smarty->assign("deplist", $this->config->idepartments);
259       $smarty->assign("alphabet", generate_alphabet());
260       foreach( array("dselect", "regex", "accounts", "groups", "applications",
261             "departments", "servers", "workstations", "terminals",
262             "printers", "phones") as $type){
263         $smarty->assign("$type", $ogfilter[$type]);
264       }
265       $smarty->assign("hint", print_sizelimit_warning());
266       $smarty->assign("apply", apply_filter());
268       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
269       return ($display);
270     }
272     /* Bases / Departments */
273    
274       if (isset($_POST['base'])){
275         $this->base= $_POST['base'];
276       }
278     /* Assemble combine string */
279     if ($this->gosaGroupObjects == "[]"){
280       $smarty->assign("combinedObjects", _("none"));
281     } elseif (strlen($this->gosaGroupObjects) > 4){
282       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
283     } else {
284       $conv= array(   "U" => _("users"),
285           "G" => _("groups"),
286           "A" => _("applications"),
287           "D" => _("departments"),
288           "S" => _("servers"),
289           "W" => _("workstations"),
290           "T" => _("terminals"),
291           "F" => _("phones"),
292           "P" => _("printers"));
294       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
295       $p1= $conv[$type[0]];
296       error_reporting(0);
297       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
298         $p2= $conv[$type[1]];
299         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
300       } else {
301         $smarty->assign("combinedObjects", "$p1");
302       }
303       error_reporting(E_ALL);
304     }
306     /* Assign variables */
307     $smarty->assign("bases", $this->config->idepartments);
308     $smarty->assign("base_select", $this->base);
309     $smarty->assign("department", $this->department);
310     $smarty->assign("members", $this->convert_list($this->memberList));
312     /* Objects have to be tuned... */
313     $smarty->assign("objects", $this->convert_list($this->objects));
315     /* Fields */
316     foreach ($this->attributes as $val){
317       $smarty->assign("$val", $this->$val);
318       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
319     }
321     /* Assign ACL's */
322     foreach (array("base", "members") as $val){
323       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
324     }
326     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
327   }
330   /* Save data to object */
331   function save_object()
332   {
333     /* Save additional values for possible next step */
334     if (isset($_POST['ogroupedit'])){
335       plugin::save_object();
337       if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
338         $this->base= $_POST["base"];
339       }
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){
415       $ldap->ls("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",$basedn.$ogfilter['dselect'] ,
416           array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
417       
418       /* fetch results and append them to the list */
419       while($attrs = $ldap->fetch()){
421         $type= $this->getObjectType($attrs);
422         $name= $this->getObjectName($attrs);
424         /* Fill array */
425         if (isset($attrs["description"][0])){
426           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
427         } elseif (isset($attrs["uid"][0])) {
428           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
429         } else {
430           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
431         }
432       }
433     }
434     uasort ($this->objects, 'sort_list');
435     reset ($this->objects);
437     
438     /*###########
439       Build member list and try to detect obsolete entries 
440       ###########*/
442     $this->memberList = array();
443   
444     /* Walk through all single member entry */
445     foreach($this->member as $dn){
446       $this->importMember($dn);
447     }
448     uasort ($this->memberList, 'sort_list');
449     reset ($this->memberList);
451     /* Assemble types of currently combined objects */
452     $objectTypes= "";
453     foreach ($this->memberList as $dn => $desc){
455       /* Invalid object? */
456       if ($desc['type'] == 'I'){
457         continue;
458       }
460       /* Fine. Add to list. */
461       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
462         $objectTypes.= $desc['type'];
463       }
464     }
465     $this->gosaGroupObjects= "[$objectTypes]";
466   }
469   function importMember($dn)
470   {
471     $ldap= $this->config->get_ldap_link();
473     /* Try to resolv the entry again, if it still fails, display error msg */
474     $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
476     /* It has failed, add entry with type flag I (Invalid)*/
477     if ($ldap->error != "Success"){
478       $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
479     } else {
480       /* Append this entry to our all object list */
482       /* Fetch object */
483       $attrs= $ldap->fetch();
485       $type= $this->getObjectType($attrs);
486       $name= $this->getObjectName($attrs);
488       if (isset($attrs["description"][0])){
489         $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
490       } elseif (isset($attrs["uid"][0])) {
491         $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
492       } else {
493         $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
494       }
495       $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
496       if(isset($attrs['uid'])){
497         $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
498       }
500       /* Fill array */
501       if (isset($attrs["description"][0])){
502         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
503       } else {
504         $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
505       }
507       $this->memberList[$dn]= $this->objects[$attrs["dn"]];
508     }
509   }
511   function convert_list($input)
512   {
513     $temp= "";
514     $conv= array(  "U" => "select_user.png",
515         "G" => "select_groups.png",
516         "A" => "select_application.png",
517         "D" => "select_department.png",
518         "S" => "select_server.png",
519         "W" => "select_workstation.png",
520         "T" => "select_terminal.png",
521         "F" => "select_phone.png",
522         "I" => "flag.png",
523         "P" => "select_printer.png");
525     foreach ($input as $key => $value){
526       /* Generate output */
527       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
528     }
530     return ($temp);
531   }
534   function getObjectType($attrs)
535   {
536     $type= "I";
538     foreach(array(  "U" => "gosaAccount",
539           "G" => "posixGroup",
540           "A" => "gosaApplication",
541           "D" => "gosaDepartment",
542           "S" => "goServer",
543           "W" => "gotoWorkstation",
544           "T" => "gotoTerminal",
545           "F" => "goFonHardware",
546           "P" => "gotoPrinter") as $index => $class){
547       if (in_array($class, $attrs['objectClass'])){
548         $type= $index;
549         break;
550       }
551     }
553     return ($type);
554   }
557   function getObjectName($attrs)
558   {
559     /* Person? */
560     $name =""; 
561     if (in_array('gosaAccount', $attrs['objectClass'])){
562       if(isset($attrs['sn']) && isset($attrs['givenName'])){
563         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
564       } else {
565         $name= $attrs['uid'][0];
566       }
567     } else {
568       if(isset($attrs["cn"][0])) {
569         $name= $attrs['cn'][0];
570       } else {
571         $name= $attrs['ou'][0];
572       }
573     }
575     return ($name);
576   }
579   function check()
580   {
581     /* Call common method to give check the hook */
582     $message= plugin::check();
584     /* Permissions for that base? */
585     if ($this->base != ""){
586       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
587     } else {
588       $new_dn= $this->dn;
589     }
592     $ldap = $this->config->get_ldap_link();
593     if($this->dn != $new_dn){
594       $ldap->cat ($new_dn, array('dn'));
595     }
596     
597     if($ldap->count() !=0){
598       $message[]= _("There is already an object with this cn.");
599     } 
601     $ui= get_userinfo();
602     $acl= get_permissions ($new_dn, $ui->subtreeACL);
603     $acl= get_module_permission($acl, "group", $new_dn);
604     if (chkacl($acl, "create") != ""){
605       $message[]= _("You have no permissions to create a group on this 'Base'.");
606     }
608     /* must: cn */
609     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
610       $message[]= "The required field 'Name' is not set.";
611     }
613     /* To many different object types? */
614     if (strlen($this->gosaGroupObjects) > 4){
615       $message[]= _("You can combine two different object types at maximum only!");
616     }
618     return ($message);
619   }
622   /* Save to LDAP */
623   function save()
624   {
625     plugin::save();
627     /* Move members to target array */
628     $this->attrs['member'] =array();
629     foreach ($this->member as $key => $desc){
630       $this->attrs['member'][]= @LDAP::fix($key);
631     }
633     $ldap= $this->config->get_ldap_link();
635     /* New accounts need proper 'dn', propagate it to remaining objects */
636     if ($this->dn == 'new'){
637       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
638     }
640     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
641        new entries. So do a check first... */
642     $ldap->cat ($this->dn, array('dn'));
643     if ($ldap->fetch()){
644       /* Modify needs array() to remove values :-( */
645       if (!count ($this->member)){
646         $this->attrs['member']= array();
647       }
648       $mode= "modify";
649     } else {
650       $mode= "add";
651       $ldap->cd($this->config->current['BASE']);
652       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
653     }
655     /* Write back to ldap */
656     $ldap->cd($this->dn);
657     $this->cleanup();
658     $ldap->$mode($this->attrs);
660     /* Trigger post signal */
661     $this->handle_post_events($mode);
663     $ret= 0;
664     if (show_ldap_error($ldap->get_error(), _("Saving object group failed"))){
665       $ret= 1;
666     }
668     return ($ret);
669   }
671   function remove_from_parent()
672   {
673     plugin::remove_from_parent();
675     $ldap= $this->config->get_ldap_link();
676     $ldap->rmdir($this->dn);
677     show_ldap_error($ldap->get_error(), _("Removing object group failed"));
679     /* Trigger remove signal */
680     $this->handle_post_events("remove");
681   }
683   function getCopyDialog()
684   {
685     $str  = "";
686     $str .= _("Group name");
687     $str .= "&nbsp;<input type='text' name='cn' value='".$this->cn."'>";
688     return($str);
689   }
691   function saveCopyDialog()
692   {
693     if(isset($_POST['cn'])){
694       $this->cn = $_POST['cn'];
695     }
696   }
699 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
700 ?>