Code

Updated some plugins.
[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           "_" => "subtrees",
75           "P" => "printers") as $key => $val){
77       if (preg_match("/$key/", $this->gosaGroupObjects)){
78         $ogfilter[$val]= "checked";
79       } else {
80         $ogfilter[$val]= "";
81       }
82     }
83     register_global("ogfilter", $ogfilter);
84   
85     if(isset($_SESSION['CurrentMainBase'])){
86      $this->base = $_SESSION['CurrentMainBase'];
87     }
89     /* Set base */
90     if ($this->dn == "new"){
91       $this->base= $_SESSION['CurrentMainBase'];
92     } else {
93       $this->base= preg_replace("/^[^,]+,".get_groups_ou()."/","",$this->dn);
94     }
96     /* Load member data */
97     $this->reload();
98   }
100   function AddDelMembership($NewMember = false){
102     if($NewMember){
104       /* Add member and force reload */
105       $this->member[$NewMember]= $NewMember;
106       $this->reload(); 
108       $this->memberList[$NewMember]= $this->objcache[$NewMember];
109       unset ($this->objects[$NewMember]);
110       uasort ($this->memberList, 'sort_list');
111       reset ($this->memberList);
112     }else{
113       /* Delete objects from group */
114       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
115         foreach ($_POST['members'] as $value){
116           $this->objects["$value"]= $this->memberList[$value];
117           unset ($this->memberList["$value"]);
118           unset ($this->member["$value"]);
119           uasort ($this->objects, 'sort_list');
120           reset ($this->objects);
121         }
122         $this->reload();
123       }
125       /* Add objects to group */
126       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
127         foreach ($_POST['objects'] as $value){
128           $this->memberList["$value"]= $this->objects[$value];
129           $this->member["$value"]= $value;
130           unset ($this->objects[$value]);
131           uasort ($this->memberList, 'sort_list');
132           reset ($this->memberList);
133         }
134         $this->reload();
135       }
136     }
137   }
139   function execute()
140   {
141         /* Call parent execute */
142         plugin::execute();
144 //    $this->reload();
146     /* Do we represent a valid group? */
147     if (!$this->is_account){
148       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
149         _("This 'dn' is no object group.")."</b>";
150       return ($display);
151     }
153     /* Delete objects from group */
154     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
155       foreach ($_POST['members'] as $value){
156         if(isset($this->memberList[$value])){
157           $this->objects["$value"]= $this->memberList[$value];
158           unset ($this->memberList["$value"]);
159           unset ($this->member["$value"]);
160           uasort ($this->objects, 'sort_list');
161           reset ($this->objects);
162         }
163       }
164       $this->reload();
165     }
167     /* Add objects to group */
168     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
169       foreach ($_POST['objects'] as $value){
170         if(isset($this->objects[$value])){
171           $this->memberList["$value"]= $this->objects[$value];
172           $this->member["$value"]= $value;
173           unset ($this->objects[$value]);
174           uasort ($this->memberList, 'sort_list');
175           reset ($this->memberList);
176         }
177       }
178       $this->reload();
179     }
181     /* Load templating engine */
182     $smarty= get_smarty();
184     $tmp = $this->plInfo();
185     foreach($tmp['plProvidedAcls'] as $name => $translation){
186       $smarty->assign($name."ACL",$this->getacl($name));
187     }
189     /* Base select dialog */
190     $once = true;
191     foreach($_POST as $name => $value){
192       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
193         $once = false;
194         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
195         $this->dialog->setCurrentBase($this->base);
196       }
197     }
199     /* Dialog handling */
200     if(is_object($this->dialog) && $this->acl_is_moveable()){
201       /* Must be called before save_object */
202       $this->dialog->save_object();
204       if($this->dialog->isClosed()){
205         $this->dialog = false;
206       }elseif($this->dialog->isSelected()){
208         /* A new base was selected, check if it is a valid one */
209         $tmp = $this->get_allowed_bases();
210         if(isset($tmp[$this->dialog->isSelected()])){
211           $this->base = $this->dialog->isSelected();
212         }
213         $this->dialog= false;
214       }else{
215         return($this->dialog->execute());
216       }
217     }
219     /* Add objects? */
220     if (isset($_POST["edit_membership"])){
221       $this->group_dialog= TRUE;
222       $this->dialog= TRUE;
223     }
225     /* Add objects finished? */
226     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
227       $this->group_dialog= FALSE;
228       $this->dialog= FALSE;
229     }
231     /* Manage object add dialog */
232     if ($this->group_dialog){
234       /* Save data */
235       $ogfilter= get_global("ogfilter");
236       foreach( array("dselect", "regex") as $type){
237         if (isset($_POST[$type])){
238           $ogfilter[$type]= $_POST[$type];
239         }
240       }
241       if (isset($_POST['dselect'])){
242         foreach( array("accounts", "groups", "applications", "departments",
243               "servers", "workstations", "terminals", "printers","subtrees",
244               "phones") as $type){
246           if (isset($_POST[$type])) {
247             $ogfilter[$type]= "checked";
248           } else {
249             $ogfilter[$type]= "";
250           }
251         }
252       }
253       if (isset($_GET['search'])){
254         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
255         if ($s == "**"){
256           $s= "*";
257         }
258         $ogfilter['regex']= $s;
259       }
260       register_global("ogfilter", $ogfilter);
261       $this->reload();
263       /* Calculate actual groups */
264       $smarty->assign("objects", $this->convert_list($this->objects));
266       /* Show dialog */
267       $smarty->assign("search_image", get_template_path('images/search.png'));
268       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
269       $smarty->assign("tree_image", get_template_path('images/tree.png'));
270       $smarty->assign("deplist", $this->config->idepartments);
271       $smarty->assign("alphabet", generate_alphabet());
272       foreach( array("dselect", "regex", "accounts", "groups", "applications",
273             "departments", "servers", "workstations", "terminals","subtrees",
274             "printers", "phones") as $type){
275         $smarty->assign("$type", $ogfilter[$type]);
276       }
277       $smarty->assign("hint", print_sizelimit_warning());
278       $smarty->assign("apply", apply_filter());
280       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
281       return ($display);
282     }
284     /* Bases / Departments */
285       if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
286         $this->base= $_POST['base'];
287       }
289     /* Assemble combine string */
290     if ($this->gosaGroupObjects == "[]"){
291       $smarty->assign("combinedObjects", _("none"));
292     } elseif (strlen($this->gosaGroupObjects) > 4){
293       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
294     } else {
295       $conv= array(   "U" => _("users"),
296           "G" => _("groups"),
297           "A" => _("applications"),
298           "D" => _("departments"),
299           "S" => _("servers"),
300           "W" => _("workstations"),
301           "T" => _("terminals"),
302           "F" => _("phones"),
303           "P" => _("printers"));
305       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
306       $p1= $conv[$type[0]];
307       error_reporting(0);
308       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
309         $p2= $conv[$type[1]];
310         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
311       } else {
312         $smarty->assign("combinedObjects", "$p1");
313       }
314       error_reporting(E_ALL);
315     }
317     /* Assign variables */
318     $smarty->assign("bases", $this->get_allowed_bases());
319     $smarty->assign("base_select", $this->base);
320     $smarty->assign("department", $this->department);
321     $smarty->assign("members", $this->convert_list($this->memberList));
323     /* Objects have to be tuned... */
324     $smarty->assign("objects", $this->convert_list($this->objects));
326     /* Fields */
327     foreach ($this->attributes as $val){
328       $smarty->assign("$val", $this->$val);
329     }
331     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
332   }
335   /* Save data to object */
336   function save_object()
337   {
338     /* Save additional values for possible next step */
339     if (isset($_POST['ogroupedit'])){
341       /* Create a base backup and reset the
342          base directly after calling plugin::save_object();
343          Base will be set seperatly a few lines below */
344       $base_tmp = $this->base;
345       plugin::save_object();
346       $this->base = $base_tmp;
348       /* Save base, since this is no LDAP attribute */
349       $tmp = $this->get_allowed_bases();
350       if(isset($_POST['base'])){
351         if(isset($tmp[$_POST['base']])){
352           $this->base= $_POST['base'];
353         }
354       }
355     }
356   }
359   /* (Re-)Load objects */
360   function reload()
361   {
362     /*###########
363       Variable initialisation 
364       ###########*/
366     $this->objects                = array();
367     $this->ui                     = get_userinfo();
368     $filter                       = "";
369     $objectClasses                = array();
370     
371     $ogfilter               = get_global("ogfilter");
372     $regex                  = $ogfilter['regex'];
374     $ldap= $this->config->get_ldap_link();
375     $ldap->cd ($ogfilter['dselect']);
378     /*###########
379       Generate Filter 
380       ###########*/
382     $p_f= array("accounts"        => array("CLASS"=>"gosaAccount"    ,"DN"=> get_people_ou()                  ,"ACL" => "users"), 
383                 "groups"          => array("CLASS"=>"posixGroup"     ,"DN"=> get_groups_ou()                  ,"ACL" => "groups"), 
384                 "applications"    => array("CLASS"=>"gosaApplication","DN"=> "ou=apps,"                       ,"ACL" => "application"), 
385                 "departments"     => array("CLASS"=>"gosaDepartment" ,"DN"=> ""                               ,"ACL" => "department"), 
386                 "servers"         => array("CLASS"=>"goServer"       ,"DN"=> "ou=servers,ou=systems,"         ,"ACL" => "server"),
387                 "workstations"    => array("CLASS"=>"gotoWorkstation","DN"=> "ou=workstations,ou=systems,"    ,"ACL" => "workstation"),
388                 "terminals"       => array("CLASS"=>"gotoTerminal"   ,"DN"=> "ou=terminals,ou=systems,"       ,"ACL" => "terminal"),
389                 "printers"        => array("CLASS"=>"gotoPrinter"    ,"DN"=> "ou=printers,ou=systems,"        ,"ACL" => "printer"),
390                 "phones"          => array("CLASS"=>"goFonHardware"  ,"DN"=> "ou=phones,ou=systems,"          ,"ACL" => "phone"));
393     /*###########
394       Perform search for selected objectClasses & regex to fill list with objects   
395       ###########*/
397     $Get_list_flags = 0;
398     if($ogfilter['subtrees'] == "checked"){
399       $Get_list_flags |= GL_SUBSEARCH;
400     }    
402     foreach($p_f as $post_name => $data){
404       if($ogfilter[$post_name] == "checked"){
405         $filter = "(&(objectClass=".$data['CLASS'].")(|(uid=$regex)(cn=$regex)(ou=$regex)))";
406         $res    = get_list($filter, $data['ACL']  , $data['DN'].$ogfilter['dselect'], 
407                     array("description", "objectClass", "sn", "givenName", "uid","ou","cn"));
409         /* fetch results and append them to the list */
410         foreach($res as $attrs){
412           $type= $this->getObjectType($attrs);
413           $name= $this->getObjectName($attrs);
415           /* Fill array */
416           if (isset($attrs["description"][0])){
417             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
418           } elseif (isset($attrs["uid"][0])) {
419             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
420           } else {
421             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
422           }
423         }
424       }
425     }
426     uasort ($this->objects, 'sort_list');
427     reset ($this->objects);
429     
430     /*###########
431       Build member list and try to detect obsolete entries 
432       ###########*/
434     $this->memberList = array();
435   
436     /* Walk through all single member entry */
437     foreach($this->member as $dn){
439       /* The dn for the current member can't be resolved 
440          it seams that this entry was removed 
441        */ 
442       /* Try to resolv the entry again, if it still fails, display error msg */
443       $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
445       /* It has failed, add entry with type flag I (Invalid)*/
446       if ($ldap->error != "Success"){
447         $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
449       } else {
451         /* Append this entry to our all object list */
453         /* Fetch object */
454         $attrs= $ldap->fetch();
456         $type= $this->getObjectType($attrs);
457         $name= $this->getObjectName($attrs);
459         if (isset($attrs["description"][0])){
460           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
461         } elseif (isset($attrs["uid"][0])) {
462           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
463         } else {
464           $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
465         }
466         $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
467         if(isset($attrs['uid'])){
468           $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
469         }
471         /* Fill array */
472         if (isset($attrs["description"][0])){
473           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
474         } else {
475           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
476         }
478         $this->memberList[$dn]= $this->objects[$attrs["dn"]];
479       }
480     }
481     uasort ($this->memberList, 'sort_list');
482     reset ($this->memberList);
484     /* Assemble types of currently combined objects */
485     $objectTypes= "";
486     foreach ($this->memberList as $dn => $desc){
488       /* Invalid object? */
489       if ($desc['type'] == 'I'){
490         continue;
491       }
493       /* Fine. Add to list. */
494       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
495         $objectTypes.= $desc['type'];
496       }
497     }
498     $this->gosaGroupObjects= "[$objectTypes]";
499   }
502   function convert_list($input)
503   {
504     $temp= "";
505     $conv= array(  "U" => "select_user.png",
506         "G" => "select_groups.png",
507         "A" => "select_application.png",
508         "D" => "select_department.png",
509         "S" => "select_server.png",
510         "W" => "select_workstation.png",
511         "T" => "select_terminal.png",
512         "F" => "select_phone.png",
513         "I" => "flag.png",
514         "P" => "select_printer.png");
516     foreach ($input as $key => $value){
517       /* Generate output */
518       $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
519     }
521     return ($temp);
522   }
525   function getObjectType($attrs)
526   {
527     $type= "I";
529     foreach(array(  "U" => "gosaAccount",
530           "G" => "posixGroup",
531           "A" => "gosaApplication",
532           "D" => "gosaDepartment",
533           "S" => "goServer",
534           "W" => "gotoWorkstation",
535           "T" => "gotoTerminal",
536           "F" => "goFonHardware",
537           "P" => "gotoPrinter") as $index => $class){
538       if (in_array($class, $attrs['objectClass'])){
539         $type= $index;
540         break;
541       }
542     }
544     return ($type);
545   }
548   function getObjectName($attrs)
549   {
550     /* Person? */
551     $name =""; 
552     if (in_array('gosaAccount', $attrs['objectClass'])){
553       if(isset($attrs['sn']) && isset($attrs['givenName'])){
554         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
555       } else {
556         $name= $attrs['uid'][0];
557       }
558     } else {
559       if(isset($attrs["cn"][0])) {
560         $name= $attrs['cn'][0];
561       } else {
562         $name= $attrs['ou'][0];
563       }
564     }
566     return ($name);
567   }
570   function check()
571   {
572     /* Call common method to give check the hook */
573     $message= plugin::check();
575     /* Permissions for that base? */
576     if ($this->base != ""){
577       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
578     } else {
579       $new_dn= $this->dn;
580     }
583     $ldap = $this->config->get_ldap_link();
584     if($this->dn != $new_dn){
585       $ldap->cat ($new_dn, array('dn'));
586     }
587     
588     if($ldap->count() !=0){
589       $message[]= _("There is already an object with this cn.");
590     } 
592     /* Set new acl base */
593     if($this->dn == "new") {
594       $this->set_acl_base($this->base);
595     }
597     /* must: cn */
598     if ($this->cn == ""){
599       $message[]= "The required field 'Name' is not set.";
600     }
602     /* To many different object types? */
603     if (strlen($this->gosaGroupObjects) > 4){
604       $message[]= _("You can combine two different object types at maximum only!");
605     }
607     return ($message);
608   }
611   /* Save to LDAP */
612   function save()
613   {
614     plugin::save();
616     /* Move members to target array */
617     $this->attrs['member'] =array();
618     foreach ($this->member as $key => $desc){
619       $this->attrs['member'][]= @LDAP::fix($key);
620     }
622     $ldap= $this->config->get_ldap_link();
624     /* New accounts need proper 'dn', propagate it to remaining objects */
625     if ($this->dn == 'new'){
626       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
627     }
629     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
630        new entries. So do a check first... */
631     $ldap->cat ($this->dn, array('dn'));
632     if ($ldap->fetch()){
633       /* Modify needs array() to remove values :-( */
634       if (!count ($this->member)){
635         $this->attrs['member']= array();
636       }
637       $mode= "modify";
638     } else {
639       $mode= "add";
640       $ldap->cd($this->config->current['BASE']);
641       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
642     }
644     /* Write back to ldap */
645     $ldap->cd($this->dn);
646     $this->cleanup();
647     $ldap->$mode($this->attrs);
649     /* Trigger post signal */
650     $this->handle_post_events($mode);
652     $ret= 0;
653     if (show_ldap_error($ldap->get_error(), sprintf(_("Saving of object group/generic with dn '%s' failed."),$this->dn))){
654       $ret= 1;
655     }
657     return ($ret);
658   }
660   function remove_from_parent()
661   {
662     plugin::remove_from_parent();
664     $ldap= $this->config->get_ldap_link();
665     $ldap->rmdir($this->dn);
666     show_ldap_error($ldap->get_error(), sprintf(_("Removing of object group/generic with dn '%s' failed."),$this->dn));
668     /* Trigger remove signal */
669     $this->handle_post_events("remove");
670   }
673   function getCopyDialog()
674   {
675     $smarty = get_smarty();
676     $smarty->assign("cn",     $this->cn);
677     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
678     $ret = array();
679     $ret['string'] = $str;
680     $ret['status'] = "";
681     return($ret);
682   }
684   function saveCopyDialog()
685   {
686     if(isset($_POST['cn'])){
687       $this->cn = $_POST['cn'];
688     }
689   }
692   function plInfo()
693   {
694     return (array(
695           "plShortName"   => _("Generic"),
696           "plDescription" => _("Object group generic"),
697           "plSelfModify"  => FALSE,
698           "plDepends"     => array(),
699           "plPriority"    => 1,
700           "plSection"     => array("administration"),
701           "plCategory"    => array("ogroups" => array("description"  => _("Object groups"),
702                                                       "objectClass"  => "gosaGroupOfNames")),
703           "plProvidedAcls"=> array(
704             "cn"                => _("Name"),
705             "base"              => _("Base"),
706             "description"       => _("Description"),
707             "member"            => _("Member"))
708           ));
709   }
712 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
713 ?>