Code

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