Code

Added printer patch
[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 $objcache= 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","member");
36   var $objectclasses= array("top", "gosaGroupOfNames");
38   function ogroup ($config, $dn= NULL)
39   {
40     plugin::plugin ($config, $dn);
41     $this->orig_dn= $dn;
43     $this->member = array();
45     /* Load member objects */
46     if (isset($this->attrs['member'])){
47       foreach ($this->attrs['member'] as $key => $value){
48         if ("$key" != "count"){
49           $this->member["$value"]= "$value";
50         }
51       }
52     }
53     $this->is_account= TRUE;
55     /* Get global filter config */
56     if (!is_global("ogfilter")){
57       $ui= get_userinfo();
58       $base= get_base_from_people($ui->dn);
59       $ogfilter= array( "dselect"       => $base,
60           "regex"           => "*");
61       register_global("ogfilter", $ogfilter);
62     }
63     $ogfilter= get_global('ogfilter');
65     /* Adjust flags */
66     foreach( array(   "U" => "accounts",
67           "G" => "groups",
68           "A" => "applications",
69           "D" => "departments",
70           "S" => "servers",
71           "W" => "workstations",
72           "T" => "terminals",
73           "F" => "phones",
74           "P" => "printers") as $key => $val){
76       if (preg_match("/$key/", $this->gosaGroupObjects)){
77         $ogfilter[$val]= "checked";
78       } else {
79         $ogfilter[$val]= "";
80       }
81     }
82     register_global("ogfilter", $ogfilter);
83   
84     if(isset($_SESSION['CurrentMainBase'])){
85      $this->base = $_SESSION['CurrentMainBase'];
86     }
88      /* set permissions */
89     $ui= get_userinfo();
90     $acl= get_permissions ($ui->dn, $ui->subtreeACL);
91     $this->acl= get_module_permission($acl, "ogroup", $ui->dn);
94     /* Load member data */
95     $this->reload();
96   }
98   function AddDelMembership($NewMember = false)
99   {
100     if($NewMember){
101       $this->importMember($NewMember);
102       $this->memberList[$NewMember]= $this->objcache[$NewMember];
103       $this->member[$NewMember]= $NewMember;
104       unset ($this->objects[$NewMember]);
105       uasort ($this->memberList, 'sort_list');
106       reset ($this->memberList);
107       $this->reload();
108     }else{
109       /* Delete objects from group */
110       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
111         foreach ($_POST['members'] as $value){
112           $this->objects["$value"]= $this->memberList[$value];
113           unset ($this->memberList["$value"]);
114           unset ($this->member["$value"]);
115           uasort ($this->objects, 'sort_list');
116           reset ($this->objects);
117         }
118         $this->reload();
119       }
121       /* Add objects to group */
122       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
123         foreach ($_POST['objects'] as $value){
124           $this->memberList["$value"]= $this->objects[$value];
125           $this->member["$value"]= $value;
126           unset ($this->objects[$value]);
127           uasort ($this->memberList, 'sort_list');
128           reset ($this->memberList);
129         }
130         $this->reload();
131       }
132     }
133   }
135   function execute()
136   {
137         /* Call parent execute */
138         plugin::execute();
140 //    $this->reload();
142     /* Do we represent a valid group? */
143     if (!$this->is_account){
144       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
145         _("This 'dn' is no object group.")."</b>";
146       return ($display);
147     }
149     /* Delete objects from group */
150     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
151       foreach ($_POST['members'] as $value){
152         if(isset($this->memberList[$value])){
153           $this->objects["$value"]= $this->memberList[$value];
154           unset ($this->memberList["$value"]);
155           unset ($this->member["$value"]);
156           uasort ($this->objects, 'sort_list');
157           reset ($this->objects);
158         }
159       }
160       $this->reload();
161     }
163     /* Add objects to group */
164     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
165       foreach ($_POST['objects'] as $value){
166         if(isset($this->objects[$value])){
167           $this->memberList["$value"]= $this->objects[$value];
168           $this->member["$value"]= $value;
169           unset ($this->objects[$value]);
170           uasort ($this->memberList, 'sort_list');
171           reset ($this->memberList);
172         }
173       }
174       $this->reload();
175     }
177     /* Load templating engine */
178     $smarty= get_smarty();
180     /* Base select dialog */
181     $once = true;
182     foreach($_POST as $name => $value){
183       if(preg_match("/^chooseBase/",$name) && $once){
184         $once = false;
185         $this->dialog = new baseSelectDialog($this->config);
186         $this->dialog->setCurrentBase($this->base);
187       }
188     }
190     /* Dialog handling */
191     if(is_object($this->dialog)){
192       /* Must be called before save_object */
193       $this->dialog->save_object();
195       if($this->dialog->isClosed()){
196         $this->dialog = false;
197       }elseif($this->dialog->isSelected()){
198         $this->base = $this->dialog->isSelected();
199         $this->dialog= false;
200       }else{
201         return($this->dialog->execute());
202       }
203     }
205     /* Add objects? */
206     if (isset($_POST["edit_membership"])){
207       $this->group_dialog= TRUE;
208       $this->dialog= TRUE;
209     }
211     /* Add objects finished? */
212     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
213       $this->group_dialog= FALSE;
214       $this->dialog= FALSE;
215     }
217     /* Manage object add dialog */
218     if ($this->group_dialog){
220       /* Save data */
221       $ogfilter= get_global("ogfilter");
222       foreach( array("dselect", "regex") as $type){
223         if (isset($_POST[$type])){
224           $ogfilter[$type]= $_POST[$type];
225         }
226       }
227       if (isset($_POST['dselect'])){
228         foreach( array("accounts", "groups", "applications", "departments",
229               "servers", "workstations", "terminals", "printers",
230               "phones") as $type){
232           if (isset($_POST[$type])) {
233             $ogfilter[$type]= "checked";
234           } else {
235             $ogfilter[$type]= "";
236           }
237         }
238       }
239       if (isset($_GET['search'])){
240         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
241         if ($s == "**"){
242           $s= "*";
243         }
244         $ogfilter['regex']= $s;
245       }
246       register_global("ogfilter", $ogfilter);
247       $this->reload();
249       /* Calculate actual groups */
250       $smarty->assign("objects", $this->convert_list($this->objects));
252       /* Show dialog */
253       $smarty->assign("search_image", get_template_path('images/search.png'));
254       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
255       $smarty->assign("tree_image", get_template_path('images/tree.png'));
256       $smarty->assign("deplist", $this->config->idepartments);
257       $smarty->assign("alphabet", generate_alphabet());
258       foreach( array("dselect", "regex", "accounts", "groups", "applications",
259             "departments", "servers", "workstations", "terminals",
260             "printers", "phones") as $type){
261         $smarty->assign("$type", $ogfilter[$type]);
262       }
263       $smarty->assign("hint", print_sizelimit_warning());
264       $smarty->assign("apply", apply_filter());
266       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
267       return ($display);
268     }
270     /* Bases / Departments */
271    
272       if (isset($_POST['base'])){
273         $this->base= $_POST['base'];
274       }
276     /* Assemble combine string */
277     if ($this->gosaGroupObjects == "[]"){
278       $smarty->assign("combinedObjects", _("none"));
279     } elseif (strlen($this->gosaGroupObjects) > 4){
280       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
281     } else {
282       $conv= array(   "U" => _("users"),
283           "G" => _("groups"),
284           "A" => _("applications"),
285           "D" => _("departments"),
286           "S" => _("servers"),
287           "W" => _("workstations"),
288           "T" => _("terminals"),
289           "F" => _("phones"),
290           "P" => _("printers"));
292       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
293       $p1= $conv[$type[0]];
294       error_reporting(0);
295       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
296         $p2= $conv[$type[1]];
297         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
298       } else {
299         $smarty->assign("combinedObjects", "$p1");
300       }
301       error_reporting(E_ALL);
302     }
304     /* Assign variables */
305     $smarty->assign("bases", $this->config->idepartments);
306     $smarty->assign("base_select", $this->base);
307     $smarty->assign("department", $this->department);
308     $smarty->assign("members", $this->convert_list($this->memberList));
310     /* Objects have to be tuned... */
311     $smarty->assign("objects", $this->convert_list($this->objects));
313     /* Fields */
314     foreach ($this->attributes as $val){
315       $smarty->assign("$val", $this->$val);
316       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
317     }
319     /* Assign ACL's */
320     foreach (array("base", "members") as $val){
321       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
322     }
324     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
325   }
328   /* Save data to object */
329   function save_object()
330   {
331     /* Save additional values for possible next step */
332     if (isset($_POST['ogroupedit'])){
333       plugin::save_object();
335       if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
336         $this->base= $_POST["base"];
337       }
339     }
340   }
343   /* (Re-)Load objects */
344   function reload()
345   {
346     /*###########
347       Variable initialisation 
348       ###########*/
350     $this->objects                = array();
351     $this->ui                     = get_userinfo();
352     $filter                       = "";
353     $objectClasses                = array();
354     
355     $ogfilter               = get_global("ogfilter");
356     $regex                  = $ogfilter['regex'];
358     /* Get ldap connection */
359     $ldap= $this->config->get_ldap_link();
360     $ldap->cd ($ogfilter['dselect']);
363     /*###########
364       Generate Filter 
365       ###########*/
367     /* Assemble filter */
368     if ($ogfilter['accounts'] == "checked"){
369       $filter.= "(objectClass=gosaAccount)";
370       $objectClasses["gosaAccount"]     = get_people_ou();
371     }
372     if ($ogfilter['groups'] == "checked"){
373       $filter.= "(objectClass=posixGroup)";
374       $objectClasses["posixGroup"]      = get_groups_ou();
375     }
376     if ($ogfilter['applications'] == "checked"){
377       $filter.= "(objectClass=gosaApplication)";
378       $objectClasses["gosaApplication"] = "ou=apps,";
379     }
380     if ($ogfilter['departments'] == "checked"){
381       $filter.= "(objectClass=gosaDepartment)";
382       $objectClasses["gosaDepartment"]  = "";
383     }
384     if ($ogfilter['servers'] == "checked"){
385       $filter.= "(objectClass=goServer)";
386       $objectClasses["goServer"]        = "ou=servers,ou=systems,";
387     }
388     if ($ogfilter['workstations'] == "checked"){
389       $filter.= "(objectClass=gotoWorkstation)";
390       $objectClasses["gotoWorkstation"] = "ou=workstations,ou=systems,";
391     }
392     if ($ogfilter['terminals'] == "checked"){
393       $filter.= "(objectClass=gotoTerminal)";
394       $objectClasses["gotoTerminal"]    = "ou=terminals,ou=systems,";
395     }
396     if ($ogfilter['printers'] == "checked"){
397       $filter.= "(objectClass=gotoPrinter)";
399       $objectClasses["gotoPrinter"]     = "ou=printers,ou=systems,";
400     }
401     if ($ogfilter['phones'] == "checked"){
402       $filter.= "(objectClass=goFonHardware)";
403       $objectClasses["goFonHardware"]   = "ou=phones,ou=systems,";
404     }
407     /*###########
408       Perform search for selected objectClasses & regex to fill list with objects   
409       ###########*/
411     /* Perform search for selected objectClasses */
412     foreach($objectClasses as $class=> $basedn){
413       $ldap->ls("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",$basedn.$ogfilter['dselect'] ,
414           array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
415       
416       /* fetch results and append them to the list */
417       while($attrs = $ldap->fetch()){
419         $type= $this->getObjectType($attrs);
420         $name= $this->getObjectName($attrs);
422         /* Fill array */
423         if (isset($attrs["description"][0])){
424           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
425         } elseif (isset($attrs["uid"][0])) {
426           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
427         } else {
428           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
429         }
430       }
431     }
432     uasort ($this->objects, 'sort_list');
433     reset ($this->objects);
435     
436     /*###########
437       Build member list and try to detect obsolete entries 
438       ###########*/
440     $this->memberList = array();
441   
442     /* Walk through all single member entry */
443     foreach($this->member as $dn){
444       $this->importMember($dn);
445     }
446     uasort ($this->memberList, 'sort_list');
447     reset ($this->memberList);
449     /* Assemble types of currently combined objects */
450     $objectTypes= "";
451     foreach ($this->memberList as $dn => $desc){
453       /* Invalid object? */
454       if ($desc['type'] == 'I'){
455         continue;
456       }
458       /* Fine. Add to list. */
459       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
460         $objectTypes.= $desc['type'];
461       }
462     }
463     $this->gosaGroupObjects= "[$objectTypes]";
464   }
467   function importMember($dn)
468   {
469     $ldap= $this->config->get_ldap_link();
471     /* Try to resolv the entry again, if it still fails, display error msg */
472     $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
474     /* It has failed, add entry with type flag I (Invalid)*/
475     if ($ldap->error != "Success"){
476       $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
477     } else {
478       /* Append this entry to our all object list */
480       /* Fetch object */
481       $attrs= $ldap->fetch();
483       $type= $this->getObjectType($attrs);
484       $name= $this->getObjectName($attrs);
486       if (isset($attrs["description"][0])){
487         $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
488       } elseif (isset($attrs["uid"][0])) {
489         $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
490       } else {
491         $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
492       }
493       $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
494       if(isset($attrs['uid'])){
495         $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
496       }
498       /* Fill array */
499       if (isset($attrs["description"][0])){
500         $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
501       } else {
502         $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
503       }
505       $this->memberList[$dn]= $this->objects[$attrs["dn"]];
506     }
507   }
509   function convert_list($input)
510   {
511     $temp= "";
512     $conv= array(  "U" => "select_user.png",
513         "G" => "select_groups.png",
514         "A" => "select_application.png",
515         "D" => "select_department.png",
516         "S" => "select_server.png",
517         "W" => "select_workstation.png",
518         "T" => "select_terminal.png",
519         "F" => "select_phone.png",
520         "I" => "flag.png",
521         "P" => "select_printer.png");
523     foreach ($input as $key => $value){
524       /* Generate output */
525       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
526     }
528     return ($temp);
529   }
532   function getObjectType($attrs)
533   {
534     $type= "I";
536     foreach(array(  "U" => "gosaAccount",
537           "G" => "posixGroup",
538           "A" => "gosaApplication",
539           "D" => "gosaDepartment",
540           "S" => "goServer",
541           "W" => "gotoWorkstation",
542           "T" => "gotoTerminal",
543           "F" => "goFonHardware",
544           "P" => "gotoPrinter") as $index => $class){
545       if (in_array($class, $attrs['objectClass'])){
546         $type= $index;
547         break;
548       }
549     }
551     return ($type);
552   }
555   function getObjectName($attrs)
556   {
557     /* Person? */
558     $name =""; 
559     if (in_array('gosaAccount', $attrs['objectClass'])){
560       if(isset($attrs['sn']) && isset($attrs['givenName'])){
561         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
562       } else {
563         $name= $attrs['uid'][0];
564       }
565     } else {
566       if(isset($attrs["cn"][0])) {
567         $name= $attrs['cn'][0];
568       } else {
569         $name= $attrs['ou'][0];
570       }
571     }
573     return ($name);
574   }
577   function check()
578   {
579     /* Call common method to give check the hook */
580     $message= plugin::check();
582     /* Permissions for that base? */
583     if ($this->base != ""){
584       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
585     } else {
586       $new_dn= $this->dn;
587     }
590     $ldap = $this->config->get_ldap_link();
591     if($this->dn != $new_dn){
592       $ldap->cat ($new_dn, array('dn'));
593     }
594     
595     if($ldap->count() !=0){
596       $message[]= _("There is already an object with this cn.");
597     } 
599     $ui= get_userinfo();
600     $acl= get_permissions ($new_dn, $ui->subtreeACL);
601     $acl= get_module_permission($acl, "group", $new_dn);
602     if (chkacl($acl, "create") != ""){
603       $message[]= _("You have no permissions to create a group on this 'Base'.");
604     }
606     /* must: cn */
607     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
608       $message[]= "The required field 'Name' is not set.";
609     }
611     /* To many different object types? */
612     if (strlen($this->gosaGroupObjects) > 4){
613       $message[]= _("You can combine two different object types at maximum only!");
614     }
616     return ($message);
617   }
620   /* Save to LDAP */
621   function save()
622   {
623     plugin::save();
625     /* Move members to target array */
626     $this->attrs['member'] =array();
627     foreach ($this->member as $key => $desc){
628       $this->attrs['member'][]= @LDAP::fix($key);
629     }
631     $ldap= $this->config->get_ldap_link();
633     /* New accounts need proper 'dn', propagate it to remaining objects */
634     if ($this->dn == 'new'){
635       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
636     }
638     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
639        new entries. So do a check first... */
640     $ldap->cat ($this->dn, array('dn'));
641     if ($ldap->fetch()){
642       /* Modify needs array() to remove values :-( */
643       if (!count ($this->member)){
644         $this->attrs['member']= array();
645       }
646       $mode= "modify";
647     } else {
648       $mode= "add";
649       $ldap->cd($this->config->current['BASE']);
650       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
651     }
653     /* Write back to ldap */
654     $ldap->cd($this->dn);
655     $this->cleanup();
656     $ldap->$mode($this->attrs);
658     /* Trigger post signal */
659     $this->handle_post_events($mode);
661     $ret= 0;
662     if (show_ldap_error($ldap->get_error(), _("Saving object group failed"))){
663       $ret= 1;
664     }
666     return ($ret);
667   }
669   function remove_from_parent()
670   {
671     plugin::remove_from_parent();
673     $ldap= $this->config->get_ldap_link();
674     $ldap->rmdir($this->dn);
675     show_ldap_error($ldap->get_error(), _("Removing object group failed"));
677     /* Trigger remove signal */
678     $this->handle_post_events("remove");
679   }
681   function getCopyDialog()
682   {
683     $str  = "";
684     $str .= _("Group name");
685     $str .= "&nbsp;<input type='text' name='cn' value='".$this->cn."'>";
686     return($str);
687   }
689   function saveCopyDialog()
690   {
691     if(isset($_POST['cn'])){
692       $this->cn = $_POST['cn'];
693     }
694   }
697 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
698 ?>