Code

Removed unwanted print
[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      /* set permissions */
87     $ui= get_userinfo();
88     $acl= get_permissions ($ui->dn, $ui->subtreeACL);
89     $this->acl= get_module_permission($acl, "ogroup", $ui->dn);
92     /* Load member data */
93     $this->reload();
94   }
96   function AddDelMembership(){
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     }
120   }
122   function execute()
123   {
124         /* Call parent execute */
125         plugin::execute();
127     $this->reload();
129     /* Do we represent a valid group? */
130     if (!$this->is_account){
131       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
132         _("This 'dn' is no object group.")."</b>";
133       return ($display);
134     }
136     /* Delete objects from group */
137     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
138       foreach ($_POST['members'] as $value){
139         if(isset($this->memberList[$value])){
140           $this->objects["$value"]= $this->memberList[$value];
141           unset ($this->memberList["$value"]);
142           unset ($this->member["$value"]);
143           uasort ($this->objects, 'sort_list');
144           reset ($this->objects);
145         }
146       }
147       $this->reload();
148     }
150     /* Add objects to group */
151     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
152       foreach ($_POST['objects'] as $value){
153         if(isset($this->objects[$value])){
154           $this->memberList["$value"]= $this->objects[$value];
155           $this->member["$value"]= $value;
156           unset ($this->objects[$value]);
157           uasort ($this->memberList, 'sort_list');
158           reset ($this->memberList);
159         }
160       }
161       $this->reload();
162     }
164     /* Load templating engine */
165     $smarty= get_smarty();
167     /* Add objects? */
168     if (isset($_POST["edit_membership"])){
169       $this->group_dialog= TRUE;
170       $this->dialog= TRUE;
171     }
173     /* Add objects finished? */
174     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
175       $this->group_dialog= FALSE;
176       $this->dialog= FALSE;
177     }
179     /* Manage object add dialog */
180     if ($this->group_dialog){
182       /* Save data */
183       $ogfilter= get_global("ogfilter");
184       foreach( array("dselect", "regex") as $type){
185         if (isset($_POST[$type])){
186           $ogfilter[$type]= $_POST[$type];
187         }
188       }
189       if (isset($_POST['dselect'])){
190         foreach( array("accounts", "groups", "applications", "departments",
191               "servers", "workstations", "terminals", "printers",
192               "phones") as $type){
194           if (isset($_POST[$type])) {
195             $ogfilter[$type]= "checked";
196           } else {
197             $ogfilter[$type]= "";
198           }
199         }
200       }
201       if (isset($_GET['search'])){
202         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
203         if ($s == "**"){
204           $s= "*";
205         }
206         $ogfilter['regex']= $s;
207       }
208       register_global("ogfilter", $ogfilter);
209       $this->reload();
211       /* Calculate actual groups */
212       $smarty->assign("objects", $this->convert_list($this->objects));
214       /* Show dialog */
215       $smarty->assign("search_image", get_template_path('images/search.png'));
216       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
217       $smarty->assign("tree_image", get_template_path('images/tree.png'));
218       $smarty->assign("deplist", $this->config->idepartments);
219       $smarty->assign("alphabet", generate_alphabet());
220       foreach( array("dselect", "regex", "accounts", "groups", "applications",
221             "departments", "servers", "workstations", "terminals",
222             "printers", "phones") as $type){
223         $smarty->assign("$type", $ogfilter[$type]);
224       }
225       $smarty->assign("hint", print_sizelimit_warning());
226       $smarty->assign("apply", apply_filter());
228       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
229       return ($display);
230     }
232     /* Bases / Departments */
233    
234       if (isset($_POST['base'])){
235         $this->base= $_POST['base'];
236       }
238     /* Assemble combine string */
239     if ($this->gosaGroupObjects == "[]"){
240       $smarty->assign("combinedObjects", _("none"));
241     } elseif (strlen($this->gosaGroupObjects) > 4){
242       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
243     } else {
244       $conv= array(   "U" => _("users"),
245           "G" => _("groups"),
246           "A" => _("applications"),
247           "D" => _("departments"),
248           "S" => _("servers"),
249           "W" => _("workstations"),
250           "T" => _("terminals"),
251           "F" => _("phones"),
252           "P" => _("printers"));
254       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
255       $p1= $conv[$type[0]];
256       error_reporting(0);
257       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
258         $p2= $conv[$type[1]];
259         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
260       } else {
261         $smarty->assign("combinedObjects", "$p1");
262       }
263       error_reporting(E_ALL);
264     }
266     /* Assign variables */
267     $smarty->assign("bases", $this->config->idepartments);
268     $smarty->assign("base_select", $this->base);
269     $smarty->assign("department", $this->department);
270     $smarty->assign("members", $this->convert_list($this->memberList));
272     /* Objects have to be tuned... */
273     $smarty->assign("objects", $this->convert_list($this->objects));
275     /* Fields */
276     foreach ($this->attributes as $val){
277       $smarty->assign("$val", $this->$val);
278       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
279     }
281     /* Assign ACL's */
282     foreach (array("base", "members") as $val){
283       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
284     }
286     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
287   }
290   /* Save data to object */
291   function save_object()
292   {
293     /* Save additional values for possible next step */
294     if (isset($_POST['ogroupedit'])){
295       plugin::save_object();
297       if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
298         $this->base= $_POST["base"];
299       }
301     }
302   }
305   /* (Re-)Load objects */
306   function reload()
307   {
308     /* Generate object list */
309     $this->objects= array();
310     $this->allobjects= array();
311     $ldap= $this->config->get_ldap_link();
313     /* Assemble filter */
314     $ogfilter= get_global("ogfilter");
316     $ldap->cd ($ogfilter['dselect']);
318     $filter= "";
319     if ($ogfilter['accounts'] == "checked"){
320       $filter.= "(objectClass=gosaAccount)";
321     }
322     if ($ogfilter['groups'] == "checked"){
323       $filter.= "(objectClass=posixGroup)";
324     }
325     if ($ogfilter['applications'] == "checked"){
326       $filter.= "(objectClass=gosaApplication)";
327     }
328     if ($ogfilter['departments'] == "checked"){
329       $filter.= "(objectClass=gosaDepartment)";
330     }
331     if ($ogfilter['servers'] == "checked"){
332       $filter.= "(objectClass=goServer)";
333     }
334     if ($ogfilter['workstations'] == "checked"){
335       $filter.= "(objectClass=gotoWorkstation)";
336     }
337     if ($ogfilter['terminals'] == "checked"){
338       $filter.= "(objectClass=gotoTerminal)";
339     }
340     if ($ogfilter['printers'] == "checked"){
341       $filter.= "(objectClass=gotoPrinter)";
342     }
343     if ($ogfilter['phones'] == "checked"){
344       $filter.= "(objectClass=goFonHardware)";
345     }
346     $regex= $ogfilter['regex'];
348     $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
349     while ($attrs= $ldap->fetch()){
351       /* Get type */
352       $type= $this->getObjectType($attrs);
353       $name= $this->getObjectName($attrs);
355       /* Fill array */
356       if (isset($attrs["description"][0])){
357         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
358       } elseif (isset($attrs["uid"][0])) {
359         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
360       } else {
361         $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
362       }
363     }
364     uasort ($this->objects, 'sort_list');
365     reset ($this->objects);
367     $ldap->cd ($this->config->current['BASE']);
368     $filter= "(objectClass=gosaAccount)(objectClass=posixGroup)(objectClass=gosaApplication)(objectClass=gosaDepartment)(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal)(objectClass=gotoPrinter)(objectClass=goFonHardware)";
369     $regex= "*";
371     $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
372     while ($attrs= $ldap->fetch()){
374       /* Get type */
375       $type= $this->getObjectType($attrs);
376       $name= $this->getObjectName($attrs);
378       /* Fill array */
379       if (isset($attrs["description"][0])){
380         $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
381       } elseif (isset($attrs["uid"][0])) {
382         $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
383       } else {
384         $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
385       }
386       $this->allobjects[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
387       if(isset($attrs['uid'])){
388         $this->allobjects[$attrs["dn"]]['uid']          = $attrs['uid'];
389       }
390     }
391     uasort ($this->allobjects, 'sort_list');
392     reset ($this->allobjects);
394     /* Build member list */
395     $this->memberList= array();
396     foreach($this->member as $dn){
398       /* Object in object list? */
399       if (isset($this->allobjects[$dn])){
400         $this->memberList[$dn]= $this->allobjects[$dn];
401         if (isset ($this->objects[$dn])){
402           unset ($this->objects[$dn]);
403         }
405       } else {
407         /* No, try to ge informations from LDAP */
408         $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
409         if ($ldap->error != "success"){
410           $this->memberList[$dn]= array('text' => _("Non existing dn: ")."$dn",
411               "type" => "I");
412         } else {
413           $ldap->cat($dn);
414           $attrs= $ldap->fetch();
415           $type= $this->getObjectType($attrs);
416           $name= $this->getObjectName($attrs);
418           /* Fill array */
419           if (isset($attrs["description"][0])){
420             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
421           } else {
422             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
423           }
425         }
426       }
427     }
428     uasort ($this->memberList, 'sort_list');
429     reset ($this->memberList);
431     /* Assemble types of currently combined objects */
432     $objectTypes= "";
433     foreach ($this->memberList as $dn => $desc){
435       /* Invalid object? */
436       if ($desc['type'] == 'I'){
437         continue;
438       }
440       /* Fine. Add to list. */
441       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
442         $objectTypes.= $desc['type'];
443       }
444     }
445     $this->gosaGroupObjects= "[$objectTypes]";
446   }
449   function convert_list($input)
450   {
451     $temp= "";
452     $conv= array(  "U" => "select_user.png",
453         "G" => "select_groups.png",
454         "A" => "select_application.png",
455         "D" => "select_department.png",
456         "S" => "select_server.png",
457         "W" => "select_workstation.png",
458         "T" => "select_terminal.png",
459         "F" => "select_phone.png",
460         "I" => "flag.png",
461         "P" => "select_printer.png");
463     foreach ($input as $key => $value){
464       /* Generate output */
465       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
466     }
468     return ($temp);
469   }
472   function getObjectType($attrs)
473   {
474     $type= "I";
476     foreach(array(  "U" => "gosaAccount",
477           "G" => "posixGroup",
478           "A" => "gosaApplication",
479           "D" => "gosaDepartment",
480           "S" => "goServer",
481           "W" => "gotoWorkstation",
482           "T" => "gotoTerminal",
483           "F" => "goFonHardware",
484           "P" => "gotoPrinter") as $index => $class){
485       if (in_array($class, $attrs['objectClass'])){
486         $type= $index;
487         break;
488       }
489     }
491     return ($type);
492   }
495   function getObjectName($attrs)
496   {
497     /* Person? */
498     if (in_array('gosaAccount', $attrs['objectClass'])){
499       if(isset($attrs['sn']) && isset($attrs['givenName'])){
500         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
501       } else {
502         $name= $attrs['uid'][0];
503       }
504     } else {
505       if(isset($attrs["cn"][0])) {
506         $name= $attrs['cn'][0];
507       } else {
508         $name= $attrs['ou'][0];
509       }
510     }
512     return ($name);
513   }
516   function check()
517   {
518     $message= array();
520     /* Permissions for that base? */
521     if ($this->base != ""){
522       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
523     } else {
524       $new_dn= $this->dn;
525     }
527     $ui= get_userinfo();
528     $acl= get_permissions ($new_dn, $ui->subtreeACL);
529     $acl= get_module_permission($acl, "group", $new_dn);
530     if (chkacl($acl, "create") != ""){
531       $message[]= _("You have no permissions to create a group on this 'Base'.");
532     }
534     /* must: cn */
535     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
536       $message[]= "The required field 'Name' is not set.";
537     }
539     /* To many different object types? */
540     if (strlen($this->gosaGroupObjects) > 4){
541       $message[]= _("You can combine two different object types at maximum only!");
542     }
544     return ($message);
545   }
548   /* Save to LDAP */
549   function save()
550   {
551     plugin::save();
553     /* Move members to target array */
554     foreach ($this->member as $key => $desc){
555       $this->attrs['member'][]= $key;
556     }
558     $ldap= $this->config->get_ldap_link();
560     /* New accounts need proper 'dn', propagate it to remaining objects */
561     if ($this->dn == 'new'){
562       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
563     }
565     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
566        new entries. So do a check first... */
567     $ldap->cat ($this->dn);
568     if ($ldap->fetch()){
569       /* Modify needs array() to remove values :-( */
570       if (!count ($this->member)){
571         $this->attrs['member']= array();
572       }
573       $mode= "modify";
574     } else {
575       $mode= "add";
576       $ldap->cd($this->config->current['BASE']);
577       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
578     }
580     /* Write back to ldap */
581     $ldap->cd($this->dn);
582     $ldap->$mode($this->attrs);
584     /* Trigger post signal */
585     $this->handle_post_events($mode);
587     $ret= 0;
588     if (show_ldap_error($ldap->get_error())){
589       $ret= 1;
590     }
592     return ($ret);
593   }
595   function remove_from_parent()
596   {
597     plugin::remove_from_parent();
599     $ldap= $this->config->get_ldap_link();
600     $ldap->rmdir($this->dn);
601     show_ldap_error($ldap->get_error());
603     /* Trigger remove signal */
604     $this->handle_post_events("remove");
605   }
609 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
610 ?>