Code

Moved to trunk/branches/tags structure
[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);
82     /* Load member data */
83     $this->reload();
84   }
86   function execute()
87   {
88     /* Do we represent a valid group? */
89     if (!$this->is_account){
90       $display= "<img src=\"images/stop.png\" align=center>&nbsp;<b>".
91         _("This 'dn' is no object group.")."</b>";
92       return ($display);
93     }
97     /* Delete objects from group */
98     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
99       foreach ($_POST['members'] as $value){
100         $this->objects["$value"]= $this->memberList[$value];
101         unset ($this->memberList["$value"]);
102         unset ($this->member["$value"]);
103         uasort ($this->objects, 'sort_list');
104         reset ($this->objects);
105       }
106       $this->reload();
107     }
109     /* Add objects to group */
110     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
111       foreach ($_POST['objects'] as $value){
112         $this->memberList["$value"]= $this->objects[$value];
113         $this->member["$value"]= $value;
114         unset ($this->objects[$value]);
115         uasort ($this->memberList, 'sort_list');
116         reset ($this->memberList);
117       }
118       $this->reload();
119     }
121     /* Load templating engine */
122     $smarty= get_smarty();
124     /* Add objects? */
125     if (isset($_POST["edit_membership"])){
126       $this->group_dialog= TRUE;
127       $this->dialog= TRUE;
128     }
130     /* Add objects finished? */
131     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
132       $this->group_dialog= FALSE;
133       $this->dialog= FALSE;
134     }
136     /* Manage object add dialog */
137     if ($this->group_dialog){
139       /* Save data */
140       $ogfilter= get_global("ogfilter");
141       foreach( array("dselect", "regex") as $type){
142         if (isset($_POST[$type])){
143           $ogfilter[$type]= $_POST[$type];
144         }
145       }
146       if (isset($_POST['dselect'])){
147         foreach( array("accounts", "groups", "applications", "departments",
148               "servers", "workstations", "terminals", "printers",
149               "phones") as $type){
151           if (isset($_POST[$type])) {
152             $ogfilter[$type]= "checked";
153           } else {
154             $ogfilter[$type]= "";
155           }
156         }
157       }
158       if (isset($_GET['search'])){
159         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
160         if ($s == "**"){
161           $s= "*";
162         }
163         $ogfilter['regex']= $s;
164       }
165       register_global("ogfilter", $ogfilter);
166       $this->reload();
168       /* Calculate actual groups */
169       $smarty->assign("objects", $this->convert_list($this->objects));
171       /* Show dialog */
172       $smarty->assign("search_image", get_template_path('images/search.png'));
173       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
174       $smarty->assign("tree_image", get_template_path('images/tree.png'));
175       $smarty->assign("deplist", $this->config->idepartments);
176       $smarty->assign("alphabet", generate_alphabet());
177       foreach( array("dselect", "regex", "accounts", "groups", "applications",
178             "departments", "servers", "workstations", "terminals",
179             "printers", "phones") as $type){
180         $smarty->assign("$type", $ogfilter[$type]);
181       }
182       $smarty->assign("hint", print_sizelimit_warning());
184       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
185       return ($display);
186     }
188     /* Bases / Departments */
189     if ($this->dn == "new"){
190       $ui= get_userinfo();
191       $this->base= dn2base($ui->dn);
192     } else {
193       if (isset($_POST['base'])){
194         $this->base= $_POST['base'];
195       } else {
196         $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
197       }
198     }
200     /* Assemble combine string */
201     if ($this->gosaGroupObjects == "[]"){
202       $smarty->assign("combinedObjects", _("none"));
203     } elseif (strlen($this->gosaGroupObjects) > 4){
204       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
205     } else {
206       $conv= array(   "U" => _("users"),
207           "G" => _("groups"),
208           "A" => _("applications"),
209           "D" => _("departments"),
210           "S" => _("servers"),
211           "W" => _("workstations"),
212           "T" => _("terminals"),
213           "F" => _("phones"),
214           "P" => _("printers"));
216       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
217       $p1= $conv[$type[0]];
218       error_reporting(0);
219       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
220         $p2= $conv[$type[1]];
221         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
222       } else {
223         $smarty->assign("combinedObjects", "$p1");
224       }
225       error_reporting(E_ALL);
226     }
228     /* Assign variables */
229     $smarty->assign("bases", $this->config->idepartments);
230     $smarty->assign("base_select", $this->base);
231     $smarty->assign("department", $this->department);
232     $smarty->assign("members", $this->convert_list($this->memberList));
234     /* Objects have to be tuned... */
235     $smarty->assign("objects", $this->convert_list($this->objects));
237     /* Fields */
238     foreach ($this->attributes as $val){
239       $smarty->assign("$val", $this->$val);
240       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
241     }
243     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
244   }
247   /* Save data to object */
248   function save_object()
249   {
250     /* Save additional values for possible next step */
251     if (isset($_POST['ogroupedit'])){
252       plugin::save_object();
254       if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
255         $this->base= $_POST["base"];
256       }
258     }
259   }
262   /* (Re-)Load objects */
263   function reload()
264   {
265     /* Generate object list */
266     $this->objects= array();
267     $this->allobjects= array();
268     $ldap= $this->config->get_ldap_link();
270     /* Assemble filter */
271     $ogfilter= get_global("ogfilter");
273     $ldap->cd ($ogfilter['dselect']);
275     $filter= "";
276     if ($ogfilter['accounts'] == "checked"){
277       $filter.= "(objectClass=gosaAccount)";
278     }
279     if ($ogfilter['groups'] == "checked"){
280       $filter.= "(objectClass=posixGroup)";
281     }
282     if ($ogfilter['applications'] == "checked"){
283       $filter.= "(objectClass=gosaApplication)";
284     }
285     if ($ogfilter['departments'] == "checked"){
286       $filter.= "(objectClass=gosaDepartment)";
287     }
288     if ($ogfilter['servers'] == "checked"){
289       $filter.= "(objectClass=goServer)";
290     }
291     if ($ogfilter['workstations'] == "checked"){
292       $filter.= "(objectClass=gotoWorkstation)";
293     }
294     if ($ogfilter['terminals'] == "checked"){
295       $filter.= "(objectClass=gotoTerminal)";
296     }
297     if ($ogfilter['printers'] == "checked"){
298       $filter.= "(objectClass=gotoPrinter)";
299     }
300     if ($ogfilter['phones'] == "checked"){
301       $filter.= "(objectClass=goFonHardware)";
302     }
303     $regex= $ogfilter['regex'];
305     $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
306     while ($attrs= $ldap->fetch()){
308       /* Get type */
309       $type= $this->getObjectType($attrs);
310       $name= $this->getObjectName($attrs);
312       /* Fill array */
313       if (isset($attrs["description"][0])){
314         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
315       } elseif (isset($attrs["uid"][0])) {
316         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
317       } else {
318         $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
319       }
320     }
321     uasort ($this->objects, 'sort_list');
322     reset ($this->objects);
324     $ldap->cd ($this->config->current['BASE']);
325     $filter= "(objectClass=gosaAccount)(objectClass=posixGroup)(objectClass=gosaApplication)(objectClass=gosaDepartment)(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal)(objectClass=gotoPrinter)(objectClass=goFonHardware)";
326     $regex= "*";
328     $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
329     while ($attrs= $ldap->fetch()){
331       /* Get type */
332       $type= $this->getObjectType($attrs);
333       $name= $this->getObjectName($attrs);
335       /* Fill array */
336       if (isset($attrs["description"][0])){
337         $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
338       } elseif (isset($attrs["uid"][0])) {
339         $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
340       } else {
341         $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
342       }
343     }
344     uasort ($this->allobjects, 'sort_list');
345     reset ($this->allobjects);
347     /* Build member list */
348     $this->memberList= array();
349     foreach($this->member as $dn){
351       /* Object in object list? */
352       if (isset($this->allobjects[$dn])){
353         $this->memberList[$dn]= $this->allobjects[$dn];
354         if (isset ($this->objects[$dn])){
355           unset ($this->objects[$dn]);
356         }
358       } else {
360         /* No, try to ge informations from LDAP */
361         $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
362         if ($ldap->error != "success"){
363           $this->memberList[$dn]= array('text' => _("Non existing dn: ")."$dn",
364               "type" => "I");
365         } else {
366           $ldap->cat($dn);
367           $attrs= $ldap->fetch();
368           $type= $this->getObjectType($attrs);
369           $name= $this->getObjectName($attrs);
371           /* Fill array */
372           if (isset($attrs["description"][0])){
373             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
374           } else {
375             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
376           }
378         }
379       }
380     }
381     uasort ($this->memberList, 'sort_list');
382     reset ($this->memberList);
384     /* Assemble types of currently combined objects */
385     $objectTypes= "";
386     foreach ($this->memberList as $dn => $desc){
388       /* Invalid object? */
389       if ($desc['type'] == 'I'){
390         continue;
391       }
393       /* Fine. Add to list. */
394       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
395         $objectTypes.= $desc['type'];
396       }
397     }
398     $this->gosaGroupObjects= "[$objectTypes]";
399   }
402   function convert_list($input)
403   {
404     $temp= "";
405     $conv= array(  "U" => "select_user.png",
406         "G" => "select_groups.png",
407         "A" => "select_application.png",
408         "D" => "select_department.png",
409         "S" => "select_server.png",
410         "W" => "select_workstation.png",
411         "T" => "select_terminal.png",
412         "F" => "select_phone.png",
413         "I" => "flag.png",
414         "P" => "select_printer.png");
416     foreach ($input as $key => $value){
417       /* Generate output */
418       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
419     }
421     return ($temp);
422   }
425   function getObjectType($attrs)
426   {
427     $type= "I";
429     foreach(array(  "U" => "gosaAccount",
430           "G" => "posixGroup",
431           "A" => "gosaApplication",
432           "D" => "gosaDepartment",
433           "S" => "goServer",
434           "W" => "gotoWorkstation",
435           "T" => "gotoTerminal",
436           "F" => "goFonHardware",
437           "P" => "gotoPrinter") as $index => $class){
438       if (in_array($class, $attrs['objectClass'])){
439         $type= $index;
440         break;
441       }
442     }
444     return ($type);
445   }
448   function getObjectName($attrs)
449   {
450     /* Person? */
451     if (in_array('gosaAccount', $attrs['objectClass'])){
452       if(isset($attrs['sn']) && isset($attrs['givenName'])){
453         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
454       } else {
455         $name= $attrs['uid'][0];
456       }
457     } else {
458       if(isset($attrs["cn"][0])) {
459         $name= $attrs['cn'][0];
460       } else {
461         $name= $attrs['ou'][0];
462       }
463     }
465     return ($name);
466   }
469   function check()
470   {
471     $message= array();
473     /* Permissions for that base? */
474     if ($this->base != ""){
475       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
476     } else {
477       $new_dn= $this->dn;
478     }
480     $ui= get_userinfo();
481     $acl= get_permissions ($new_dn, $ui->subtreeACL);
482     $acl= get_module_permission($acl, "group", $new_dn);
483     if (chkacl($acl, "create") != ""){
484       $message[]= _("You have no permissions to create a group on this 'Base'.");
485     }
487     /* must: cn */
488     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
489       $message[]= "The required field 'Name' is not set.";
490     }
492     /* Need at least one member */
493     if (count($this->member) == 0){
494       $message[]= _("Object groups need at least one member!");
495     }
497     /* To many different object types? */
498     if (strlen($this->gosaGroupObjects) > 4){
499       $message[]= _("You can combine two different object types at maximum only!");
500     }
502     return ($message);
503   }
506   /* Save to LDAP */
507   function save()
508   {
509     plugin::save();
511     /* Move members to target array */
512     foreach ($this->member as $key => $desc){
513       $this->attrs['member'][]= $key;
514     }
516     $ldap= $this->config->get_ldap_link();
518     /* New accounts need proper 'dn', propagate it to remaining objects */
519     if ($this->dn == 'new'){
520       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
521     }
523     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
524        new entries. So do a check first... */
525     $ldap->cat ($this->dn);
526     if ($ldap->fetch()){
527       /* Modify needs array() to remove values :-( */
528       if (!count ($this->member)){
529         $this->attrs['member']= array();
530       }
531       $mode= "modify";
532     } else {
533       $mode= "add";
534       $ldap->cd($this->config->current['BASE']);
535       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
536     }
538     /* Write back to ldap */
539     $ldap->cd($this->dn);
540     $ldap->$mode($this->attrs);
542     /* Trigger post signal */
543     $this->handle_post_events($mode);
545     $ret= 0;
546     if (show_ldap_error($ldap->get_error())){
547       $ret= 1;
548     }
550     return ($ret);
551   }
553   function remove_from_parent()
554   {
555     plugin::remove_from_parent();
557     $ldap= $this->config->get_ldap_link();
558     $ldap->rmdir($this->dn);
559     show_ldap_error($ldap->get_error());
561     /* Trigger remove signal */
562     $this->handle_post_events("remove");
563   }
567 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
568 ?>