Code

25b10247ce7f05e04420c8d4533451a25156b952
[gosa.git] / plugins / admin / ogroups / class_ogroup.inc
1 <?php
4 /* Sort multidimensional arrays for key 'text' */
5 function sort_list($val1, $val2)
6 {
7   $v1= strtolower($val1['text']);
8   $v2= strtolower($val2['text']);
9   if ($v1 > $v2){
10     return 1;
11   }
12   if ($v1 < $v2){
13     return -1;
14   }
15   return 0;
16 }
19 class ogroup extends plugin
20 {
21   /* Variables */
22   var $cn= "";
23   var $description= "";
24   var $base= "";
25   var $gosaGroupObjects= "";
26   var $department= "";
27   var $objects= array();
28   var $allobjects= array();
29   var $memberList= array();
30   var $member= array();
31   var $orig_dn= "";
32   var $group_dialog= FALSE;
34   /* attribute list for save action */
35   var $attributes= array("cn", "description", "gosaGroupObjects");
36   var $objectclasses= array("top", "gosaGroupOfNames");
38   function ogroup ($config, $dn= NULL)
39   {
40     plugin::plugin ($config, $dn);
41     $this->orig_dn= $dn;
43     /* Load member objects */
44     if (isset($this->attrs['member'])){
45       foreach ($this->attrs['member'] as $key => $value){
46         if ("$key" != "count"){
47           $this->member["$value"]= "$value";
48         }
49       }
50     }
51     $this->is_account= TRUE;
53     /* Get global filter config */
54     if (!is_global("ogfilter")){
55       $ui= get_userinfo();
56       $base= get_base_from_people($ui->dn);
57       $ogfilter= array( "dselect"       => $base,
58           "regex"           => "*");
59       register_global("ogfilter", $ogfilter);
60     }
61     $ogfilter= get_global('ogfilter');
63     /* Adjust flags */
64     foreach( array(   "U" => "accounts",
65           "G" => "groups",
66           "A" => "applications",
67           "D" => "departments",
68           "S" => "servers",
69           "W" => "workstations",
70           "T" => "terminals",
71           "F" => "phones",
72           "P" => "printers") as $key => $val){
74       if (preg_match("/$key/", $this->gosaGroupObjects)){
75         $ogfilter[$val]= "checked";
76       } else {
77         $ogfilter[$val]= "";
78       }
79     }
80     register_global("ogfilter", $ogfilter);
81   
82     if(isset($_SESSION['ogroupfilter']['depselect'])){
83      $this->base = $_SESSION['ogroupfilter']['depselect'];
84     }
86     /* Load member data */
87     $this->reload();
88   }
90   function AddDelMembership(){
91      /* Delete objects from group */
92     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
93       foreach ($_POST['members'] as $value){
94         $this->objects["$value"]= $this->memberList[$value];
95         unset ($this->memberList["$value"]);
96         unset ($this->member["$value"]);
97         uasort ($this->objects, 'sort_list');
98         reset ($this->objects);
99       }
100       $this->reload();
101     }
103     /* Add objects to group */
104     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
105       foreach ($_POST['objects'] as $value){
106         $this->memberList["$value"]= $this->objects[$value];
107         $this->member["$value"]= $value;
108         unset ($this->objects[$value]);
109         uasort ($this->memberList, 'sort_list');
110         reset ($this->memberList);
111       }
112       $this->reload();
113     }
114   }
116   function execute()
117   {
118         /* Call parent execute */
119         plugin::execute();
120     
121     $this->reload();
123     /* Do we represent a valid group? */
124     if (!$this->is_account){
125       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
126         _("This 'dn' is no object group.")."</b>";
127       return ($display);
128     }
130     /* Delete objects from group */
131     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
132       foreach ($_POST['members'] as $value){
133         if(isset($this->memberList[$value])){
134           $this->objects["$value"]= $this->memberList[$value];
135           unset ($this->memberList["$value"]);
136           unset ($this->member["$value"]);
137           uasort ($this->objects, 'sort_list');
138           reset ($this->objects);
139         }
140       }
141       $this->reload();
142     }
144     /* Add objects to group */
145     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
146       foreach ($_POST['objects'] as $value){
147         if(isset($this->objects[$value])){
148           $this->memberList["$value"]= $this->objects[$value];
149           $this->member["$value"]= $value;
150           unset ($this->objects[$value]);
151           uasort ($this->memberList, 'sort_list');
152           reset ($this->memberList);
153         }
154       }
155       $this->reload();
156     }
158     /* Load templating engine */
159     $smarty= get_smarty();
161     /* Add objects? */
162     if (isset($_POST["edit_membership"])){
163       $this->group_dialog= TRUE;
164       $this->dialog= TRUE;
165     }
167     /* Add objects finished? */
168     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
169       $this->group_dialog= FALSE;
170       $this->dialog= FALSE;
171     }
173     /* Manage object add dialog */
174     if ($this->group_dialog){
176       /* Save data */
177       $ogfilter= get_global("ogfilter");
178       foreach( array("dselect", "regex") as $type){
179         if (isset($_POST[$type])){
180           $ogfilter[$type]= $_POST[$type];
181         }
182       }
183       if (isset($_POST['dselect'])){
184         foreach( array("accounts", "groups", "applications", "departments",
185               "servers", "workstations", "terminals", "printers",
186               "phones") as $type){
188           if (isset($_POST[$type])) {
189             $ogfilter[$type]= "checked";
190           } else {
191             $ogfilter[$type]= "";
192           }
193         }
194       }
195       if (isset($_GET['search'])){
196         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
197         if ($s == "**"){
198           $s= "*";
199         }
200         $ogfilter['regex']= $s;
201       }
202       register_global("ogfilter", $ogfilter);
203       $this->reload();
205       /* Calculate actual groups */
206       $smarty->assign("objects", $this->convert_list($this->objects));
208       /* Show dialog */
209       $smarty->assign("search_image", get_template_path('images/search.png'));
210       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
211       $smarty->assign("tree_image", get_template_path('images/tree.png'));
212       $smarty->assign("deplist", $this->config->idepartments);
213       $smarty->assign("alphabet", generate_alphabet());
214       foreach( array("dselect", "regex", "accounts", "groups", "applications",
215             "departments", "servers", "workstations", "terminals",
216             "printers", "phones") as $type){
217         $smarty->assign("$type", $ogfilter[$type]);
218       }
219       $smarty->assign("hint", print_sizelimit_warning());
220       $smarty->assign("apply", apply_filter());
222       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
223       return ($display);
224     }
226     /* Bases / Departments */
227    
228       if (isset($_POST['base'])){
229         $this->base= $_POST['base'];
230       }
232     /* Assemble combine string */
233     if ($this->gosaGroupObjects == "[]"){
234       $smarty->assign("combinedObjects", _("none"));
235     } elseif (strlen($this->gosaGroupObjects) > 4){
236       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
237     } else {
238       $conv= array(   "U" => _("users"),
239           "G" => _("groups"),
240           "A" => _("applications"),
241           "D" => _("departments"),
242           "S" => _("servers"),
243           "W" => _("workstations"),
244           "T" => _("terminals"),
245           "F" => _("phones"),
246           "P" => _("printers"));
248       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
249       $p1= $conv[$type[0]];
250       error_reporting(0);
251       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
252         $p2= $conv[$type[1]];
253         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
254       } else {
255         $smarty->assign("combinedObjects", "$p1");
256       }
257       error_reporting(E_ALL);
258     }
260     /* Assign variables */
261     $smarty->assign("bases", $this->config->idepartments);
262     $smarty->assign("base_select", $this->base);
263     $smarty->assign("department", $this->department);
264     $smarty->assign("members", $this->convert_list($this->memberList));
266     /* Objects have to be tuned... */
267     $smarty->assign("objects", $this->convert_list($this->objects));
269     /* Fields */
270     foreach ($this->attributes as $val){
271       $smarty->assign("$val", $this->$val);
272       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
273     }
275     /* Assign ACL's */
276     foreach (array("base", "members") as $val){
277       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
278     }
280     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
281   }
284   /* Save data to object */
285   function save_object()
286   {
287     /* Save additional values for possible next step */
288     if (isset($_POST['ogroupedit'])){
289       plugin::save_object();
291       if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
292         $this->base= $_POST["base"];
293       }
295     }
296   }
299   /* (Re-)Load objects */
300   function reload()
301   {
302     /* Generate object list */
303     $this->objects= array();
304     $this->allobjects= array();
305     $ldap= $this->config->get_ldap_link();
307     /* Assemble filter */
308     $ogfilter= get_global("ogfilter");
310     $ldap->cd ($ogfilter['dselect']);
312     $filter= "";
313     if ($ogfilter['accounts'] == "checked"){
314       $filter.= "(objectClass=gosaAccount)";
315     }
316     if ($ogfilter['groups'] == "checked"){
317       $filter.= "(objectClass=posixGroup)";
318     }
319     if ($ogfilter['applications'] == "checked"){
320       $filter.= "(objectClass=gosaApplication)";
321     }
322     if ($ogfilter['departments'] == "checked"){
323       $filter.= "(objectClass=gosaDepartment)";
324     }
325     if ($ogfilter['servers'] == "checked"){
326       $filter.= "(objectClass=goServer)";
327     }
328     if ($ogfilter['workstations'] == "checked"){
329       $filter.= "(objectClass=gotoWorkstation)";
330     }
331     if ($ogfilter['terminals'] == "checked"){
332       $filter.= "(objectClass=gotoTerminal)";
333     }
334     if ($ogfilter['printers'] == "checked"){
335       $filter.= "(objectClass=gotoPrinter)";
336     }
337     if ($ogfilter['phones'] == "checked"){
338       $filter.= "(objectClass=goFonHardware)";
339     }
340     $regex= $ogfilter['regex'];
342     $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
343     while ($attrs= $ldap->fetch()){
345       /* Get type */
346       $type= $this->getObjectType($attrs);
347       $name= $this->getObjectName($attrs);
349       /* Fill array */
350       if (isset($attrs["description"][0])){
351         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
352       } elseif (isset($attrs["uid"][0])) {
353         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
354       } else {
355         $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
356       }
357     }
358     uasort ($this->objects, 'sort_list');
359     reset ($this->objects);
361     $ldap->cd ($this->config->current['BASE']);
362     $filter= "(objectClass=gosaAccount)(objectClass=posixGroup)(objectClass=gosaApplication)(objectClass=gosaDepartment)(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal)(objectClass=gotoPrinter)(objectClass=goFonHardware)";
363     $regex= "*";
365     $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
366     while ($attrs= $ldap->fetch()){
368       /* Get type */
369       $type= $this->getObjectType($attrs);
370       $name= $this->getObjectName($attrs);
372       /* Fill array */
373       if (isset($attrs["description"][0])){
374         $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
375       } elseif (isset($attrs["uid"][0])) {
376         $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
377       } else {
378         $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
379       }
380       $this->allobjects[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
381       if(isset($attrs['uid'])){
382         $this->allobjects[$attrs["dn"]]['uid']          = $attrs['uid'];
383       }
384     }
385     uasort ($this->allobjects, 'sort_list');
386     reset ($this->allobjects);
388     /* Build member list */
389     $this->memberList= array();
390     foreach($this->member as $dn){
392       /* Object in object list? */
393       if (isset($this->allobjects[$dn])){
394         $this->memberList[$dn]= $this->allobjects[$dn];
395         if (isset ($this->objects[$dn])){
396           unset ($this->objects[$dn]);
397         }
399       } else {
401         /* No, try to ge informations from LDAP */
402         $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
403         if ($ldap->error != "success"){
404           $this->memberList[$dn]= array('text' => _("Non existing dn: ")."$dn",
405               "type" => "I");
406         } else {
407           $ldap->cat($dn);
408           $attrs= $ldap->fetch();
409           $type= $this->getObjectType($attrs);
410           $name= $this->getObjectName($attrs);
412           /* Fill array */
413           if (isset($attrs["description"][0])){
414             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
415           } else {
416             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
417           }
419         }
420       }
421     }
422     uasort ($this->memberList, 'sort_list');
423     reset ($this->memberList);
425     /* Assemble types of currently combined objects */
426     $objectTypes= "";
427     foreach ($this->memberList as $dn => $desc){
429       /* Invalid object? */
430       if ($desc['type'] == 'I'){
431         continue;
432       }
434       /* Fine. Add to list. */
435       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
436         $objectTypes.= $desc['type'];
437       }
438     }
439     $this->gosaGroupObjects= "[$objectTypes]";
440   }
443   function convert_list($input)
444   {
445     $temp= "";
446     $conv= array(  "U" => "select_user.png",
447         "G" => "select_groups.png",
448         "A" => "select_application.png",
449         "D" => "select_department.png",
450         "S" => "select_server.png",
451         "W" => "select_workstation.png",
452         "T" => "select_terminal.png",
453         "F" => "select_phone.png",
454         "I" => "flag.png",
455         "P" => "select_printer.png");
457     foreach ($input as $key => $value){
458       /* Generate output */
459       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
460     }
462     return ($temp);
463   }
466   function getObjectType($attrs)
467   {
468     $type= "I";
470     foreach(array(  "U" => "gosaAccount",
471           "G" => "posixGroup",
472           "A" => "gosaApplication",
473           "D" => "gosaDepartment",
474           "S" => "goServer",
475           "W" => "gotoWorkstation",
476           "T" => "gotoTerminal",
477           "F" => "goFonHardware",
478           "P" => "gotoPrinter") as $index => $class){
479       if (in_array($class, $attrs['objectClass'])){
480         $type= $index;
481         break;
482       }
483     }
485     return ($type);
486   }
489   function getObjectName($attrs)
490   {
491     /* Person? */
492     if (in_array('gosaAccount', $attrs['objectClass'])){
493       if(isset($attrs['sn']) && isset($attrs['givenName'])){
494         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
495       } else {
496         $name= $attrs['uid'][0];
497       }
498     } else {
499       if(isset($attrs["cn"][0])) {
500         $name= $attrs['cn'][0];
501       } else {
502         $name= $attrs['ou'][0];
503       }
504     }
506     return ($name);
507   }
510   function check()
511   {
512     $message= array();
514     /* Permissions for that base? */
515     if ($this->base != ""){
516       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
517     } else {
518       $new_dn= $this->dn;
519     }
521     $ui= get_userinfo();
522     $acl= get_permissions ($new_dn, $ui->subtreeACL);
523     $acl= get_module_permission($acl, "group", $new_dn);
524     if (chkacl($acl, "create") != ""){
525       $message[]= _("You have no permissions to create a group on this 'Base'.");
526     }
528     /* must: cn */
529     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
530       $message[]= "The required field 'Name' is not set.";
531     }
533     /* To many different object types? */
534     if (strlen($this->gosaGroupObjects) > 4){
535       $message[]= _("You can combine two different object types at maximum only!");
536     }
538     return ($message);
539   }
542   /* Save to LDAP */
543   function save()
544   {
545     plugin::save();
547     /* Move members to target array */
548     foreach ($this->member as $key => $desc){
549       $this->attrs['member'][]= $key;
550     }
552     $ldap= $this->config->get_ldap_link();
554     /* New accounts need proper 'dn', propagate it to remaining objects */
555     if ($this->dn == 'new'){
556       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
557     }
559     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
560        new entries. So do a check first... */
561     $ldap->cat ($this->dn);
562     if ($ldap->fetch()){
563       /* Modify needs array() to remove values :-( */
564       if (!count ($this->member)){
565         $this->attrs['member']= array();
566       }
567       $mode= "modify";
568     } else {
569       $mode= "add";
570       $ldap->cd($this->config->current['BASE']);
571       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
572     }
574     /* Write back to ldap */
575     $ldap->cd($this->dn);
576     $ldap->$mode($this->attrs);
578     /* Trigger post signal */
579     $this->handle_post_events($mode);
581     $ret= 0;
582     if (show_ldap_error($ldap->get_error())){
583       $ret= 1;
584     }
586     return ($ret);
587   }
589   function remove_from_parent()
590   {
591     plugin::remove_from_parent();
593     $ldap= $this->config->get_ldap_link();
594     $ldap->rmdir($this->dn);
595     show_ldap_error($ldap->get_error());
597     /* Trigger remove signal */
598     $this->handle_post_events("remove");
599   }
603 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
604 ?>