Code

Next bunch of renamings
[gosa.git] / gosa-core / plugins / admin / ogroups / class_ogroup.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 /* Sort multidimensional arrays for key 'text' */
24 function sort_list($val1, $val2)
25 {
26   $v1= strtolower($val1['text']);
27   $v2= strtolower($val2['text']);
28   if ($v1 > $v2){
29     return 1;
30   }
31   if ($v1 < $v2){
32     return -1;
33   }
34   return 0;
35 }
38 class ogroup extends plugin
39 {
40   /* Variables */
41   var $cn= "";
42   var $description= "";
43   var $base= "";
44   var $gosaGroupObjects= "";
45   var $department= "";
46   var $objects= array();
47   var $objcache= array();
48   var $memberList= array();
49   var $member= array();
50   var $orig_dn= "";
51   var $orig_cn= "";
52   var $orig_base= "";
53   var $group_dialog= FALSE;
54   var $view_logged = FALSE;
56   var $accessTo= array();
57   var $trustModel= "";
58   var $show_ws_dialog = FALSE;
60   var $was_trust_account= FALSE;
62   /* Already assigned Workstations. Will be hidden in selection. 
63    */
64   var $used_workstations = array();
66   /* attribute list for save action */
67   var $attributes= array("cn", "description", "gosaGroupObjects","member","accessTo","trustModel");
68   var $objectclasses= array("top", "gosaGroupOfNames");
70   function ogroup (&$config, $dn= NULL)
71   {
72     plugin::plugin ($config, $dn);
73     $this->orig_dn= $dn;
75     $this->member = array();
77     /* Load member objects */
78     if (isset($this->attrs['member'])){
79       foreach ($this->attrs['member'] as $key => $value){
80         if ("$key" != "count"){
81           $value= @LDAP::convert($value);
82           $this->member["$value"]= "$value";
83         }
84       }
85     }
86     $this->is_account= TRUE;
88     /* Get global filter config */
89     if (!session::is_set("ogfilter")){
90       $ui= get_userinfo();
91       $base= get_base_from_people($ui->dn);
92       $ogfilter= array( "dselect"       => $base,
93           "regex"           => "*");
94       session::set("ogfilter", $ogfilter);
95     }
96     $ogfilter= session::get('ogfilter');
98     /* Adjust flags */
99     foreach( array(   "U" => "accounts",
100           "G" => "groups",
101           "A" => "applications",
102           "D" => "departments",
103           "S" => "servers",
104           "W" => "workstations",
105           "O" => "winstations",
106           "T" => "terminals",
107           "F" => "phones",
108           "_" => "subtrees",
109           "P" => "printers") as $key => $val){
111       if (preg_match("/$key/", $this->gosaGroupObjects)){
112         $ogfilter[$val]= "checked";
113       } else {
114         $ogfilter[$val]= "";
115       }
116     }
117     session::set("ogfilter", $ogfilter);
118   
119     if(session::is_set('CurrentMainBase')){
120      $this->base  = session::get('CurrentMainBase');
121     }
123     /* Set base */
124     if ($this->dn == "new"){
125       $this->base = session::get('CurrentMainBase');
126     } else {
127       $this->base= preg_replace("/^[^,]+,".normalizePreg(get_ou("ogroupou"))."/","",$this->dn);
129       /* Is this account a trustAccount? */
130       if ($this->is_account && isset($this->attrs['trustModel'])){
131         $this->trustModel= $this->attrs['trustModel'][0];
132         $this->was_trust_account= TRUE;
133       } else {
134         $this->was_trust_account= FALSE;
135         $this->trustModel= "";
136       }
138       $this->accessTo = array();
139       if ($this->is_account && isset($this->attrs['accessTo'])){
140         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
141           $tmp= $this->attrs['accessTo'][$i];
142           $this->accessTo[$tmp]= $tmp;
143         }
144       }
145     }
147     /* Detect all workstations, which are already assigned to an object group  
148         - Those objects will be hidden in the add object dialog.
149         - Check() will complain if such a system is assigned to this object group.
150      */ 
151     $base = $this->config->current['BASE'];
152     $res    = get_list("(objectClass=gotoWorkstation)","none" , 
153         $base, array("dn"),GL_NO_ACL_CHECK|GL_SUBSEARCH);
154     $ws_dns = array();
155     foreach($res as $data){
156       $ws_dns[] = $data['dn'];
157     }
158     $res=get_list("(&(member=*)(objectClass=gosaGroupOfNames))","none",
159         $base, array("dn","member"),GL_NO_ACL_CHECK|GL_SUBSEARCH);
160     $this->used_workstations = array();
161     foreach($res as $og){
162       if($og['dn'] == $this->dn) continue;
163       $test = array_intersect($ws_dns,$og['member']);
164       if(count($test)){
165         $this->used_workstations = array_merge($this->used_workstations,$test);
166       }
167     }
169     $this->orig_cn = $this->cn;
170     $this->orig_base = $this->base;
172     /* Get global filter config */
173     if (!session::is_set("sysfilter")){
174       $ui= get_userinfo();
175       $base= get_base_from_people($ui->dn);
176       $sysfilter= array( "depselect"       => $base,
177           "regex"           => "*");
178       session::set("sysfilter", $sysfilter);
179     }
181     /* Load member data */
182     $this->reload();
183   }
185   function AddDelMembership($NewMember = false){
187     if($NewMember){
189       /* Add member and force reload */
190       $this->member[$NewMember]= $NewMember;
191       $this->reload(); 
193       $this->memberList[$NewMember]= $this->objcache[$NewMember];
194       unset ($this->objects[$NewMember]);
195       uasort ($this->memberList, 'sort_list');
196       reset ($this->memberList);
197     }else{
198       /* Delete objects from group */
199       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
200         foreach ($_POST['members'] as $value){
201           $this->objects["$value"]= $this->memberList[$value];
202           unset ($this->memberList["$value"]);
203           unset ($this->member["$value"]);
204           uasort ($this->objects, 'sort_list');
205           reset ($this->objects);
206         }
207         $this->reload();
208       }
210       /* Add objects to group */
211       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
213         $tmp = "";
214         foreach($this->memberList as $obj){
215           $tmp .= $obj['type'];
216         }
217         $skipped = FALSE;
218         foreach ($_POST['objects'] as $value){
219           if(preg_match("/T/",$tmp) && $this->objects[$value]['type'] == "W"){
220             $skipped =TRUE;
221           }elseif(preg_match("/W/",$tmp) && $this->objects[$value]['type'] == "T"){
222             $skipped =TRUE;
223           }else{
224             $this->memberList["$value"]= $this->objects[$value];
225             $this->member["$value"]= $value;
226             unset ($this->objects[$value]);
227             uasort ($this->memberList, 'sort_list');
228             reset ($this->memberList);
229           }
230         }
231         if($skipped){
232           msg_dialog::display(_("Information"), _("You cannot combine terminals and workstations in one object group!"), INFO_DIALOG);
233         }
234         $this->reload();
235       }
236     }
237   }
239   function execute()
240   {
241     /* Call parent execute */
242     plugin::execute();
244     if(!$this->view_logged){
245       $this->view_logged = TRUE;
246       new log("view","ogroups/".get_class($this),$this->dn);
247     }
250     /* Do we represent a valid group? */
251     if (!$this->is_account){
252       $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
253         msgPool::noValidExtension("object group")."</b>";
254       return ($display);
255     }
258     /* Load templating engine */
259     $smarty= get_smarty();
261     $tmp = $this->plInfo();
262     foreach($tmp['plProvidedAcls'] as $name => $translation){
263       $smarty->assign($name."ACL",$this->getacl($name));
264     }
266     /* Base select dialog */
267     $once = true;
268     foreach($_POST as $name => $value){
269       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
270         $once = false;
271         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
272         $this->dialog->setCurrentBase($this->base);
273       }
274     }
276     /***********
277      * Trusts 
278      ***********/
280     /* Add user workstation? */
281     if (isset($_POST["add_ws"])){
282       $this->show_ws_dialog= TRUE;
283       $this->dialog= TRUE;
284     }
286     /* Add user workstation? */
287     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
288       foreach($_POST['wslist'] as $ws){
289         $this->accessTo[$ws]= $ws;
290       }
291       ksort($this->accessTo);
292       $this->is_modified= TRUE;
293     }
295     /* Remove user workstations? */
296     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
297       foreach($_POST['workstation_list'] as $name){
298         unset ($this->accessTo[$name]);
299       }
300       $this->is_modified= TRUE;
301     }
303     /* Add user workstation finished? */
304     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
305       $this->show_ws_dialog= FALSE;
306       $this->dialog= FALSE;
307     }
309     /* Show ws dialog */
310     if ($this->show_ws_dialog){
311       /* Save data */
312       $sysfilter= session::get("sysfilter");
313       foreach( array("depselect", "regex") as $type){
314         if (isset($_POST[$type])){
315           $sysfilter[$type]= $_POST[$type];
316         }
317       }
318       if (isset($_GET['search'])){
319         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
320         if ($s == "**"){
321           $s= "*";
322         }
323         $sysfilter['regex']= $s;
324       }
325       session::set("sysfilter", $sysfilter);
327       /* Get workstation list */
328       $exclude= "";
329       foreach($this->accessTo as $ws){
330         $exclude.= "(cn=$ws)";
331       }
332       if ($exclude != ""){
333         $exclude= "(!(|$exclude))";
334       }
335       $regex= $sysfilter['regex'];
336       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
338       $deps_a = array(get_ou("serverRDN"),
339           get_ou("terminalRDN"),
340           get_ou("workstationRDN"));
341       $res= get_sub_list($filter, array("terminal","server","workstation"), $deps_a, get_ou("systemRDN").$sysfilter['depselect'],
342           array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
343       $wslist= array();
344       foreach ($res as $attrs){
345         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
346       }
347       asort($wslist);
348       $smarty->assign("search_image", get_template_path('images/lists/search.png'));
349       $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
350       $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
351       $smarty->assign("deplist", $this->config->idepartments);
352       $smarty->assign("alphabet", generate_alphabet());
353       foreach( array("depselect", "regex") as $type){
354         $smarty->assign("$type", $sysfilter[$type]);
355       }
356       $smarty->assign("hint", print_sizelimit_warning());
357       $smarty->assign("wslist", $wslist);
358       $smarty->assign("apply", apply_filter());
359       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
360       return ($display);
361     }
363     /***********
364      * Ende - Trusts 
365      ***********/
368     /* Dialog handling */
369     if(is_object($this->dialog) && $this->acl_is_moveable()){
370       /* Must be called before save_object */
371       $this->dialog->save_object();
373       if($this->dialog->isClosed()){
374         $this->dialog = false;
375       }elseif($this->dialog->isSelected()){
377         /* A new base was selected, check if it is a valid one */
378         $tmp = $this->get_allowed_bases();
379         if(isset($tmp[$this->dialog->isSelected()])){
380           $this->base = $this->dialog->isSelected();
381         }
382         $this->dialog= false;
383       }else{
384         return($this->dialog->execute());
385       }
386     }
388     /* Add objects? */
389     if (isset($_POST["edit_membership"])){
390       $this->group_dialog= TRUE;
391       $this->dialog= TRUE;
392     }
394     /* Add objects finished? */
395     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
396       $this->group_dialog= FALSE;
397       $this->dialog= FALSE;
398     }
400     /* Manage object add dialog */
401     if ($this->group_dialog){
403       /* Save data */
404       $ogfilter= session::get("ogfilter");
405       foreach( array("dselect", "regex") as $type){
406         if (isset($_POST[$type])){
407           $ogfilter[$type]= $_POST[$type];
408         }
409       }
410       if (isset($_POST['dselect'])){
411         foreach( array("accounts", "groups", "applications", "departments",
412               "servers", "workstations", "winstations", "terminals", "printers","subtrees",
413               "phones") as $type){
415           if (isset($_POST[$type])) {
416             $ogfilter[$type]= "checked";
417           } else {
418             $ogfilter[$type]= "";
419           }
420         }
421       }
422       if (isset($_GET['search'])){
423         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
424         if ($s == "**"){
425           $s= "*";
426         }
427         $ogfilter['regex']= $s;
428       }
429       session::set("ogfilter", $ogfilter);
430       $this->reload();
432       /* Calculate actual groups */
433       $smarty->assign("objects", $this->convert_list($this->objects));
435       /* Show dialog */
436       $smarty->assign("search_image", get_template_path('images/lists/search.png'));
437       $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
438       $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
439       $smarty->assign("deplist", $this->config->idepartments);
440       $smarty->assign("alphabet", generate_alphabet());
441       foreach( array("dselect", "regex", "subtrees") as $type){
442         $smarty->assign("$type", $ogfilter[$type]);
443       }
444       $smarty->assign("hint", print_sizelimit_warning());
445       $smarty->assign("apply", apply_filter());
447       /* Build up checkboxes 
448        */
449       $ar = array(
450           "departments" => array(
451             "T" => msgPool::selectToView(_("departments")),
452             "C" => (isset($ogfilter['departments']) && ($ogfilter['departments'])),
453             "L" => sprintf(_("Show %s"),_("departments"))),
454           "accounts" => array(
455             "T" => msgPool::selectToView(_("people")),
456             "C" => (isset($ogfilter['accounts']) && ($ogfilter['accounts'])),
457             "L" => sprintf(_("Show %s"),_("people"))),
458           "groups"=> array(
459             "T" => msgPool::selectToView(_("groups")),
460             "C" => (isset($ogfilter['groups']) && ($ogfilter['groups'])),
461             "L" => sprintf(_("Show %s"),_("groups"))),
462           "servers"=> array(
463             "T" => msgPool::selectToView(_("servers")),
464             "C" => (isset($ogfilter['servers']) && ($ogfilter['servers'])),
465             "L" => sprintf(_("Show %s"),_("servers"))),
466           "workstations"=> array(
467             "T" => msgPool::selectToView(_("workstations")),
468             "C" => (isset($ogfilter['workstations']) && ($ogfilter['workstations'])),
469             "L" => sprintf(_("Show %s"),_("workstations"))),
470           "terminals"=> array(
471             "T" => msgPool::selectToView(_("terminals")),
472             "C" => (isset($ogfilter['terminals']) && ($ogfilter['terminals'])),
473             "L" => sprintf(_("Show %s"),_("terminals"))),
474           "printers"=> array(
475             "T" => msgPool::selectToView(_("printer")),
476             "C" => (isset($ogfilter['printers']) && ($ogfilter['printers'])),
477             "L" => sprintf(_("Show %s"),_("printers"))),
478           "phones"=> array(
479             "T" => msgPool::selectToView(_("phones")),
480             "C" => (isset($ogfilter['phones']) && ($ogfilter['phones'])),
481             "L" => sprintf(_("Show %s"),_("phones"))));
482  
483       /* Allow selecting applications if we are having a non 
484           release managed application storage */ 
485       if(!$this->IsReleaseManagementActivated()){
486         $ar["applications"] = array(
487             "T" => msgPool::selectToView(_("applications")),
488             "C" => (isset($ogfilter['applications']) && ($ogfilter['applications'])),
489             "L" => sprintf(_("Show %s"),_("applications")));
490       }
492       $smarty->assign("checkboxes",$ar);
493       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
494       return ($display);
495     }
497     /* Bases / Departments */
498       if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
499         $this->base= $_POST['base'];
500       }
502     /* Assemble combine string */
503     if ($this->gosaGroupObjects == "[]"){
504       $smarty->assign("combinedObjects", _("none"));
505     } elseif (strlen($this->gosaGroupObjects) > 4){
506       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
507     } else {
508       $conv= array(   "U" => _("users"),
509           "G" => _("groups"),
510           "A" => _("applications"),
511           "D" => _("departments"),
512           "S" => _("servers"),
513           "W" => _("workstations"),
514           "O" => _("winstations"),
515           "T" => _("terminals"),
516           "F" => _("phones"),
517           "P" => _("printers"));
519       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
520       $p1= $conv[$type[0]];
521       error_reporting(0);
522       if (isset($type[1]) && preg_match('/[UGADSFOWTP]/', $type[1])){
523         $p2= $conv[$type[1]];
524         $smarty->assign("combinedObjects", sprintf("'%s' and '%s'", $p1, $p2));
525       } else {
526         $smarty->assign("combinedObjects", "$p1");
527       }
528       error_reporting(E_ALL | E_STRICT);
529     }
531     /* Assign variables */
532     $smarty->assign("bases", $this->get_allowed_bases());
533     $smarty->assign("base_select", $this->base);
534     $smarty->assign("department", $this->department);
535     $smarty->assign("members", $this->convert_list($this->memberList));
537     /* Objects have to be tuned... */
538     $smarty->assign("objects", $this->convert_list($this->objects));
540     /* Fields */
541     foreach ($this->attributes as $val){
542       $smarty->assign("$val", $this->$val);
543     }
545     /******
546       Trust account
547      ******/
548     $smarty->assign("trusthide", " disabled ");
549     $smarty->assign("trustmodeACL",  $this->getacl("trustModel"));
550     if ($this->trustModel == "fullaccess"){
551       $trustmode= 1;
552       // pervent double disable tag in html code, this will disturb our clean w3c html
553       $smarty->assign("trustmode",  $this->getacl("trustModel"));
555     } elseif ($this->trustModel == "byhost"){
556       $trustmode= 2;
557       $smarty->assign("trusthide", "");
558     } else {
559       // pervent double disable tag in html code, this will disturb our clean w3c html
560       $smarty->assign("trustmode",  $this->getacl("trustModel"));
561       $trustmode= 0;
562     }
563     $smarty->assign("trustmode", $trustmode);
564     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
565           2 => _("allow access to these hosts")));
567     $smarty->assign("workstations", $this->accessTo);
569     if((count($this->accessTo))==0){
570       $smarty->assign("emptyArrAccess",true);
571     }else{
572       $smarty->assign("emptyArrAccess",false);
573     }
574     /******
575       Ende - Trust account
576      ******/
578     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
579   }
582   /* Save data to object */
583   function save_object()
584   {
585     /* Save additional values for possible next step */
586     if (isset($_POST['ogroupedit'])){
588       /******
589         Trust account 
590        ******/
592       if($this->acl_is_writeable("trustModel")){
593         if (isset($_POST['trustmode'])){
594           $saved= $this->trustModel;
595           if ($_POST['trustmode'] == "1"){
596             $this->trustModel= "fullaccess";
597           } elseif ($_POST['trustmode'] == "2"){
598             $this->trustModel= "byhost";
599           } else {
600             $this->trustModel= "";
601           }
602           if ($this->trustModel != $saved){
603             $this->is_modified= TRUE;
604           }
605         }
606       }
607       /******
608         Ende Trust account
609        ******/
611       /* Create a base backup and reset the
612          base directly after calling plugin::save_object();
613          Base will be set seperatly a few lines below */
614       $base_tmp = $this->base;
615       plugin::save_object();
616       $this->base = $base_tmp;
618       /* Save base, since this is no LDAP attribute */
619       $tmp = $this->get_allowed_bases();
620       if(isset($_POST['base'])){
621         if(isset($tmp[$_POST['base']])){
622           $this->base= $_POST['base'];
623         }
624       }
625     }
626   }
629   /* (Re-)Load objects */
630   function reload()
631   {
632     /*###########
633       Variable initialisation 
634       ###########*/
636     $this->objects                = array();
637     $this->ui                     = get_userinfo();
638     $filter                       = "";
639     $objectClasses                = array();
640     
641     $ogfilter               = session::get("ogfilter");
642     $regex                  = $ogfilter['regex'];
644     $ldap= $this->config->get_ldap_link();
645     $ldap->cd ($ogfilter['dselect']);
648     /*###########
649       Generate Filter 
650       ###########*/
652     $p_f= array("accounts"=> array("OBJ"=>"user", "CLASS"=>"gosaAccount"    ,
653           "DN"=> get_people_ou()           ,"ACL" => "users"), 
654         "groups"          => array("OBJ"=>"group", "CLASS"=>"posixGroup"     ,
655           "DN"=> get_groups_ou('ogroupou') ,"ACL" => "groups"), 
656         "departments"     => array("OBJ"=>"department", "CLASS"=>"gosaDepartment" ,
657           "DN"=> ""                        ,"ACL" => "department"), 
658         "servers"         => array("OBJ"=>"servgeneric", "CLASS"=>"goServer"       ,
659           "DN"=> get_ou('serverRDN')        ,"ACL" => "server"),
660         "workstations"    => array("OBJ"=>"workgeneric", "CLASS"=>"gotoWorkstation",
661           "DN"=> get_ou('workstationRDN')   ,"ACL" => "workstation"),
662         "winstations"     => array("OBJ"=>"wingeneric", "CLASS"=>"opsiClient",        
663           "DN"=> get_ou('SAMBAMACHINEACCOUNTRDN')     ,"ACL" => "winstation"),
664         "terminals"       => array("OBJ"=>"termgeneric", "CLASS"=>"gotoTerminal"   ,
665           "DN"=> get_ou('terminalRDN')      ,"ACL" => "terminal"),
666         "printers"        => array("OBJ"=>"printgeneric", "CLASS"=>"gotoPrinter"    ,
667           "DN"=> get_ou('printerRDN')       ,"ACL" => "printer"),
668         "phones"          => array("OBJ"=>"phoneGeneric", "CLASS"=>"goFonHardware"  ,
669           "DN"=> get_ou('phoneRDN')         ,"ACL" => "phone"));
672     /* Allow searching for applications, if we are not using release managed applications 
673       */
674     if(!$this->IsReleaseManagementActivated()){
675       $p_f[      "applications"]    = array("OBJ"=>"application", "CLASS"=>"gosaApplication",
676           "DN"=> get_ou('applicationou')   ,"ACL" => "application"); 
677     }
678            
679     /*###########
680       Perform search for selected objectClasses & regex to fill list with objects   
681       ###########*/
683     $Get_list_flags = 0;
684     if($ogfilter['subtrees'] == "checked"){
685       $Get_list_flags |= GL_SUBSEARCH;
686     }    
688     foreach($p_f as $post_name => $data){
690       if($ogfilter[$post_name] == "checked" && class_available($data['OBJ'])){
692         if($ogfilter['subtrees']){
693           $base =  $ogfilter['dselect'];
694         }else{
695           $base =  $data['DN'].$ogfilter['dselect'];
696         }
697    
698          
699         $filter = "(&(objectClass=".$data['CLASS'].")(|(uid=$regex)(cn=$regex)(ou=$regex)))";
700         $res    = get_list($filter, $data['ACL']  , $base, 
701                     array("description", "objectClass", "sn", "givenName", "uid","ou","cn"),$Get_list_flags);
703         /* fetch results and append them to the list */
704         foreach($res as $attrs){
706           /* Skip workstations which are already assigned to an object group.
707            */
708           if(in_array($attrs['dn'],$this->used_workstations)){
709             continue;
710           }
712           $type= $this->getObjectType($attrs);
713           $name= $this->getObjectName($attrs);
715           /* Fill array */
716           if (isset($attrs["description"][0])){
717             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
718           } elseif (isset($attrs["uid"][0])) {
719             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
720           } else {
721             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
722           }
723         }
724       }
725     }
726     uasort ($this->objects, 'sort_list');
727     reset ($this->objects);
729     
730     /*###########
731       Build member list and try to detect obsolete entries 
732       ###########*/
734     $this->memberList = array();
735   
736     /* Walk through all single member entry */
737     foreach($this->member as $dn){
739       /* The dn for the current member can't be resolved 
740          it seams that this entry was removed 
741        */ 
742       /* Try to resolv the entry again, if it still fails, display error msg */
743       $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass", "macAddress"));
745       /* It has failed, add entry with type flag I (Invalid)*/
746       if (!$ldap->success()){
747         $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
749       } else {
751         /* Append this entry to our all object list */
753         /* Fetch object */
754         $attrs= $ldap->fetch();
756         $type= $this->getObjectType($attrs);
757         $name= $this->getObjectName($attrs);
759         if (isset($attrs["description"][0])){
760           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
761         } elseif (isset($attrs["uid"][0])) {
762           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
763         } else {
764           $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
765         }
766         $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
768         if(isset($attrs['macAddress'][0])){
769           $this->objcache[$attrs["dn"]]['macAddress']  = $attrs['macAddress'][0];
770         }else{
771           $this->objcache[$attrs["dn"]]['macAddress']  = "";
772         }
774         if(isset($attrs['uid'])){
775           $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
776         }
778         /* Fill array */
779         if (isset($attrs["description"][0])){
780           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
781         } else {
782           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
783         }
785         $this->memberList[$dn]= $this->objects[$attrs["dn"]];
786       }
787     }
788     uasort ($this->memberList, 'sort_list');
789     reset ($this->memberList);
791     /* Assemble types of currently combined objects */
792     $objectTypes= "";
793     foreach ($this->memberList as $dn => $desc){
795       /* Invalid object? */
796       if ($desc['type'] == 'I'){
797         continue;
798       }
800       /* Fine. Add to list. */
801       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
802         $objectTypes.= $desc['type'];
803       }
804     }
805     $this->gosaGroupObjects= "[$objectTypes]";
806   }
809   function convert_list($input)
810   {
811     $temp= "";
812     $conv= array(  
813         "U" => "plugins/generic/images/head.png",
814         "G" => "plugins/groups/images/groups.png",
815         "A" => "plugins/ogroups/images/application.png",
816         "D" => "plugins/departments/images/department.png",
817         "S" => "plugins/ogroups/images/server.png",
818         "W" => "plugins/ogroups/images/workstation.png",
819         "O" => "plugins/ogroups/images/winstation.png",
820         "T" => "plugins/ogroups/images/terminal.png",
821         "F" => "plugins/ogroups/images/phone.png",
822         "I" => "images/lists/flag.png",
823         "P" => "plugins/ogroups/images/printer.png");
825     foreach ($input as $key => $value){
826       /* Generate output */
827       $temp.= "<option title='".addslashes( $key)."' value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path($conv[$value['type']])."');\">".$value['text']."</option>\n";
828     }
830     return ($temp);
831   }
834   function getObjectType($attrs)
835   {
836     $type= "I";
838     foreach(array(  "U" => "gosaAccount",
839           "G" => "posixGroup",
840           "A" => "gosaApplication",
841           "D" => "gosaDepartment",
842           "S" => "goServer",
843           "W" => "gotoWorkstation",
844           "O" => "opsiClient",
845           "T" => "gotoTerminal",
846           "F" => "goFonHardware",
847           "P" => "gotoPrinter") as $index => $class){
848       if (in_array($class, $attrs['objectClass'])){
849         $type= $index;
850         break;
851       }
852     }
854     return ($type);
855   }
858   function getObjectName($attrs)
859   {
860     /* Person? */
861     $name =""; 
862     if (in_array('gosaAccount', $attrs['objectClass'])){
863       if(isset($attrs['sn']) && isset($attrs['givenName'])){
864         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
865       } else {
866         $name= $attrs['uid'][0];
867       }
868     } else {
869       if(isset($attrs["cn"][0])) {
870         $name= $attrs['cn'][0];
871       } else {
872         $name= $attrs['ou'][0];
873       }
874     }
876     return ($name);
877   }
880   function check()
881   {
882     /* Call common method to give check the hook */
883     $message= plugin::check();
885     /* Permissions for that base? */
886     if ($this->base != ""){
887       $new_dn= 'cn='.$this->cn.','.get_ou('ogroupou').$this->base;
888     } else {
889       $new_dn= $this->dn;
890     }
892     /* Check if we have workstations assigned, that are already assigned to
893         another object group.
894      */
895     $test =array_intersect($this->used_workstations,$this->member); 
896     if(count($test)){
897       $str = "";
898       $cnt = 0;
899       foreach($test as $dn){
900         $str .= $dn.",  ";
901         $cnt ++;
902         if($cnt > 2){
903           $str .= "...";
904           break;
905         }
906       }
907 #      $message[] = sprintf(_("The following workstations are already member in another object group and can not be added: '%s'"),$str);
908     }
910     $ldap = $this->config->get_ldap_link();
911     if($this->dn != $new_dn){
912       $ldap->cat ($new_dn, array('dn'));
913     }
914     
915     if($ldap->count() !=0){
916       $message[]= msgPool::duplicated(_("Name"));
917     } 
919     /* Set new acl base */
920     if($this->dn == "new") {
921       $this->set_acl_base($this->base);
922     }
924     /* must: cn */
925     if ($this->cn == ""){
926       $message[]= msgPool::required(_("Name"));
927     }
929     /* To many different object types? */
930     if (strlen($this->gosaGroupObjects) > 4){
931       $message[]= _("You can combine two different object types at maximum, only!");
932     }
934     /* Check if we are allowed to create or move this object 
935      */
936     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
937       $message[] = msgPool::permCreate();
938     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
939       $message[] = msgPool::permMove();
940     }
942     return ($message);
943   }
946   /* Save to LDAP */
947   function save()
948   {
949     plugin::save();
951     /* Move members to target array */
952     $this->attrs['member'] =array();
953     foreach ($this->member as $key => $desc){
954       $this->attrs['member'][]= @LDAP::fix($key);
955     }
957     $ldap= $this->config->get_ldap_link();
959     /* New accounts need proper 'dn', propagate it to remaining objects */
960     if ($this->dn == 'new'){
961       $this->dn= 'cn='.$this->cn.','.get_ou('ogroupou').$this->base;
962     }
964     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
965        new entries. So do a check first... */
966     $ldap->cat ($this->dn, array('dn'));
967     if ($ldap->fetch()){
968       /* Modify needs array() to remove values :-( */
969       if (!count ($this->member)){
970         $this->attrs['member']= array();
971       }
972       $mode= "modify";
974     } else {
975       $mode= "add";
976       $ldap->cd($this->config->current['BASE']);
977       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
978     }
980     /******
981       Trust accounts 
982      ******/
983     $objectclasses= array();
984     foreach ($this->attrs['objectClass'] as $key => $class){
985       if (preg_match('/trustAccount/i', $class)){
986         continue;
987       }
988       $objectclasses[]= $this->attrs['objectClass'][$key];
989     }
990     $this->attrs['objectClass']= $objectclasses;
991     if ($this->trustModel != ""){
992       $this->attrs['objectClass'][]= "trustAccount";
993       $this->attrs['trustModel']= $this->trustModel;
994       $this->attrs['accessTo']= array();
995       if ($this->trustModel == "byhost"){
996         foreach ($this->accessTo as $host){
997           $this->attrs['accessTo'][]= $host;
998         }
999       }
1000     } else {
1001       if ($this->was_trust_account){
1002         $this->attrs['accessTo']= array();
1003         $this->attrs['trustModel']= array();
1004       }
1005     }
1007     /******
1008       Ende - Trust accounts 
1009      ******/
1011     /* Write back to ldap */
1012     $ldap->cd($this->dn);
1013     $this->cleanup();
1014     $ldap->$mode($this->attrs);
1016     if($mode == "add"){
1017       new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1018     }else{
1019       new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1020     }
1022     /* Trigger post signal */
1023     $this->handle_post_events($mode);
1025     $ret= 0;
1026     if (!$ldap->success()){
1027       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
1028       $ret= 1;
1029     }
1031     return ($ret);
1032   }
1034   function remove_from_parent()
1035   {
1036     plugin::remove_from_parent();
1038     $ldap= $this->config->get_ldap_link();
1039     $ldap->rmdir($this->dn);
1040     if (!$ldap->success()){
1041       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
1042     }
1044     new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1046     /* Trigger remove signal */
1047     $this->handle_post_events("remove");
1048   }
1050   
1051   function PrepareForCopyPaste($source)
1052   {
1053     /* Update available object types */
1054     if(isset($source['gosaGroupObjects'][0])){
1055       $this->gosaGroupObjects =  $source['gosaGroupObjects'][0];
1056     }
1058     /* Reload tabs */
1059     $this->parent->reload($this->gosaGroupObjects );
1060    
1061     /* Reload plugins */ 
1062     foreach($this->parent->by_object as $name => $class ){
1063       if(get_class($this) != $name) {
1064         $this->parent->by_object[$name]->PrepareForCopyPaste($source);
1065       }
1066     }
1068     /* Load member objects */
1069     if (isset($source['member'])){
1070       foreach ($source['member'] as $key => $value){
1071         if ("$key" != "count"){
1072           $value= @LDAP::convert($value);
1073           $this->member["$value"]= "$value";
1074         }
1075       }
1076     }
1078   }
1081   function getCopyDialog()
1082   {
1083     $smarty = get_smarty();
1084     $smarty->assign("cn",     $this->cn);
1085     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1086     $ret = array();
1087     $ret['string'] = $str;
1088     $ret['status'] = "";
1089     return($ret);
1090   }
1092   function saveCopyDialog()
1093   {
1094     if(isset($_POST['cn'])){
1095       $this->cn = $_POST['cn'];
1096     }
1097   }
1100   function IsReleaseManagementActivated()
1101   {
1102     /* Check if we should enable the release selection */
1103     $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
1104     if(!empty($tmp)){
1105       return(true);
1106     }
1107     return(false);
1108   }
1111   static function plInfo()
1112   {
1113     return (array(
1114           "plShortName"   => _("Generic"),
1115           "plDescription" => _("Object group generic"),
1116           "plSelfModify"  => FALSE,
1117           "plDepends"     => array(),
1118           "plPriority"    => 1,
1119           "plSection"     => array("administration"),
1120           "plCategory"    => array("ogroups" => array("description"  => _("Object groups"),
1121                                                       "objectClass"  => "gosaGroupOfNames")),
1122           "plProvidedAcls"=> array(
1123             "cn"                => _("Name"),
1124             "base"              => _("Base"),
1125             "description"       => _("Description"),
1126             "trustModel"        => _("Sytem trust"),
1127             "member"            => _("Member"))
1128           ));
1129   }
1132 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1133 ?>