Code

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