Code

changed picture 'nothing.png' to 'empty.png', because 'nothing.png' was not present
[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 alt=\"\" src=\"images/stop.png\" align=\"middle\">&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());
183       $smarty->assign("apply", apply_filter());
185       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
186       return ($display);
187     }
189     /* Bases / Departments */
190     if ($this->dn == "new"){
191       $ui= get_userinfo();
192       $this->base= dn2base($ui->dn);
193     } else {
194       if (isset($_POST['base'])){
195         $this->base= $_POST['base'];
196       } else {
197         $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
198       }
199     }
201     /* Assemble combine string */
202     if ($this->gosaGroupObjects == "[]"){
203       $smarty->assign("combinedObjects", _("none"));
204     } elseif (strlen($this->gosaGroupObjects) > 4){
205       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
206     } else {
207       $conv= array(   "U" => _("users"),
208           "G" => _("groups"),
209           "A" => _("applications"),
210           "D" => _("departments"),
211           "S" => _("servers"),
212           "W" => _("workstations"),
213           "T" => _("terminals"),
214           "F" => _("phones"),
215           "P" => _("printers"));
217       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
218       $p1= $conv[$type[0]];
219       error_reporting(0);
220       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
221         $p2= $conv[$type[1]];
222         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
223       } else {
224         $smarty->assign("combinedObjects", "$p1");
225       }
226       error_reporting(E_ALL);
227     }
229     /* Assign variables */
230     $smarty->assign("bases", $this->config->idepartments);
231     $smarty->assign("base_select", $this->base);
232     $smarty->assign("department", $this->department);
233     $smarty->assign("members", $this->convert_list($this->memberList));
235     /* Objects have to be tuned... */
236     $smarty->assign("objects", $this->convert_list($this->objects));
238     /* Fields */
239     foreach ($this->attributes as $val){
240       $smarty->assign("$val", $this->$val);
241       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
242     }
244     /* Assign ACL's */
245     foreach (array("base", "members") as $val){
246       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
247     }
249     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
250   }
253   /* Save data to object */
254   function save_object()
255   {
256     /* Save additional values for possible next step */
257     if (isset($_POST['ogroupedit'])){
258       plugin::save_object();
260       if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
261         $this->base= $_POST["base"];
262       }
264     }
265   }
268   /* (Re-)Load objects */
269   function reload()
270   {
271     /* Generate object list */
272     $this->objects= array();
273     $this->allobjects= array();
274     $ldap= $this->config->get_ldap_link();
276     /* Assemble filter */
277     $ogfilter= get_global("ogfilter");
279     $ldap->cd ($ogfilter['dselect']);
281     $filter= "";
282     if ($ogfilter['accounts'] == "checked"){
283       $filter.= "(objectClass=gosaAccount)";
284     }
285     if ($ogfilter['groups'] == "checked"){
286       $filter.= "(objectClass=posixGroup)";
287     }
288     if ($ogfilter['applications'] == "checked"){
289       $filter.= "(objectClass=gosaApplication)";
290     }
291     if ($ogfilter['departments'] == "checked"){
292       $filter.= "(objectClass=gosaDepartment)";
293     }
294     if ($ogfilter['servers'] == "checked"){
295       $filter.= "(objectClass=goServer)";
296     }
297     if ($ogfilter['workstations'] == "checked"){
298       $filter.= "(objectClass=gotoWorkstation)";
299     }
300     if ($ogfilter['terminals'] == "checked"){
301       $filter.= "(objectClass=gotoTerminal)";
302     }
303     if ($ogfilter['printers'] == "checked"){
304       $filter.= "(objectClass=gotoPrinter)";
305     }
306     if ($ogfilter['phones'] == "checked"){
307       $filter.= "(objectClass=goFonHardware)";
308     }
309     $regex= $ogfilter['regex'];
311     $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
312     while ($attrs= $ldap->fetch()){
314       /* Get type */
315       $type= $this->getObjectType($attrs);
316       $name= $this->getObjectName($attrs);
318       /* Fill array */
319       if (isset($attrs["description"][0])){
320         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
321       } elseif (isset($attrs["uid"][0])) {
322         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
323       } else {
324         $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
325       }
326     }
327     uasort ($this->objects, 'sort_list');
328     reset ($this->objects);
330     $ldap->cd ($this->config->current['BASE']);
331     $filter= "(objectClass=gosaAccount)(objectClass=posixGroup)(objectClass=gosaApplication)(objectClass=gosaDepartment)(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal)(objectClass=gotoPrinter)(objectClass=goFonHardware)";
332     $regex= "*";
334     $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
335     while ($attrs= $ldap->fetch()){
337       /* Get type */
338       $type= $this->getObjectType($attrs);
339       $name= $this->getObjectName($attrs);
341       /* Fill array */
342       if (isset($attrs["description"][0])){
343         $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
344       } elseif (isset($attrs["uid"][0])) {
345         $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
346       } else {
347         $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
348       }
349     }
350     uasort ($this->allobjects, 'sort_list');
351     reset ($this->allobjects);
353     /* Build member list */
354     $this->memberList= array();
355     foreach($this->member as $dn){
357       /* Object in object list? */
358       if (isset($this->allobjects[$dn])){
359         $this->memberList[$dn]= $this->allobjects[$dn];
360         if (isset ($this->objects[$dn])){
361           unset ($this->objects[$dn]);
362         }
364       } else {
366         /* No, try to ge informations from LDAP */
367         $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
368         if ($ldap->error != "success"){
369           $this->memberList[$dn]= array('text' => _("Non existing dn: ")."$dn",
370               "type" => "I");
371         } else {
372           $ldap->cat($dn);
373           $attrs= $ldap->fetch();
374           $type= $this->getObjectType($attrs);
375           $name= $this->getObjectName($attrs);
377           /* Fill array */
378           if (isset($attrs["description"][0])){
379             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
380           } else {
381             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
382           }
384         }
385       }
386     }
387     uasort ($this->memberList, 'sort_list');
388     reset ($this->memberList);
390     /* Assemble types of currently combined objects */
391     $objectTypes= "";
392     foreach ($this->memberList as $dn => $desc){
394       /* Invalid object? */
395       if ($desc['type'] == 'I'){
396         continue;
397       }
399       /* Fine. Add to list. */
400       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
401         $objectTypes.= $desc['type'];
402       }
403     }
404     $this->gosaGroupObjects= "[$objectTypes]";
405   }
408   function convert_list($input)
409   {
410     $temp= "";
411     $conv= array(  "U" => "select_user.png",
412         "G" => "select_groups.png",
413         "A" => "select_application.png",
414         "D" => "select_department.png",
415         "S" => "select_server.png",
416         "W" => "select_workstation.png",
417         "T" => "select_terminal.png",
418         "F" => "select_phone.png",
419         "I" => "flag.png",
420         "P" => "select_printer.png");
422     foreach ($input as $key => $value){
423       /* Generate output */
424       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
425     }
427     return ($temp);
428   }
431   function getObjectType($attrs)
432   {
433     $type= "I";
435     foreach(array(  "U" => "gosaAccount",
436           "G" => "posixGroup",
437           "A" => "gosaApplication",
438           "D" => "gosaDepartment",
439           "S" => "goServer",
440           "W" => "gotoWorkstation",
441           "T" => "gotoTerminal",
442           "F" => "goFonHardware",
443           "P" => "gotoPrinter") as $index => $class){
444       if (in_array($class, $attrs['objectClass'])){
445         $type= $index;
446         break;
447       }
448     }
450     return ($type);
451   }
454   function getObjectName($attrs)
455   {
456     /* Person? */
457     if (in_array('gosaAccount', $attrs['objectClass'])){
458       if(isset($attrs['sn']) && isset($attrs['givenName'])){
459         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
460       } else {
461         $name= $attrs['uid'][0];
462       }
463     } else {
464       if(isset($attrs["cn"][0])) {
465         $name= $attrs['cn'][0];
466       } else {
467         $name= $attrs['ou'][0];
468       }
469     }
471     return ($name);
472   }
475   function check()
476   {
477     $message= array();
479     /* Permissions for that base? */
480     if ($this->base != ""){
481       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
482     } else {
483       $new_dn= $this->dn;
484     }
486     $ui= get_userinfo();
487     $acl= get_permissions ($new_dn, $ui->subtreeACL);
488     $acl= get_module_permission($acl, "group", $new_dn);
489     if (chkacl($acl, "create") != ""){
490       $message[]= _("You have no permissions to create a group on this 'Base'.");
491     }
493     /* must: cn */
494     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
495       $message[]= "The required field 'Name' is not set.";
496     }
498     /* Need at least one member */
499     if (count($this->member) == 0){
500       $message[]= _("Object groups need at least one member!");
501     }
503     /* To many different object types? */
504     if (strlen($this->gosaGroupObjects) > 4){
505       $message[]= _("You can combine two different object types at maximum only!");
506     }
508     return ($message);
509   }
512   /* Save to LDAP */
513   function save()
514   {
515     plugin::save();
517     /* Move members to target array */
518     foreach ($this->member as $key => $desc){
519       $this->attrs['member'][]= $key;
520     }
522     $ldap= $this->config->get_ldap_link();
524     /* New accounts need proper 'dn', propagate it to remaining objects */
525     if ($this->dn == 'new'){
526       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
527     }
529     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
530        new entries. So do a check first... */
531     $ldap->cat ($this->dn);
532     if ($ldap->fetch()){
533       /* Modify needs array() to remove values :-( */
534       if (!count ($this->member)){
535         $this->attrs['member']= array();
536       }
537       $mode= "modify";
538     } else {
539       $mode= "add";
540       $ldap->cd($this->config->current['BASE']);
541       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
542     }
544     /* Write back to ldap */
545     $ldap->cd($this->dn);
546     $ldap->$mode($this->attrs);
548     /* Trigger post signal */
549     $this->handle_post_events($mode);
551     $ret= 0;
552     if (show_ldap_error($ldap->get_error())){
553       $ret= 1;
554     }
556     return ($ret);
557   }
559   function remove_from_parent()
560   {
561     plugin::remove_from_parent();
563     $ldap= $this->config->get_ldap_link();
564     $ldap->rmdir($this->dn);
565     show_ldap_error($ldap->get_error());
567     /* Trigger remove signal */
568     $this->handle_post_events("remove");
569   }
573 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
574 ?>