Code

Apply fix for #3867
[gosa.git] / trunk / 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("/^[^,]+,".preg_quote(get_ou("ogroupRDN"), '/')."/","",$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)(objectClass=gotoTerminal))","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", "gosaGroupObjects"),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(($og['gosaGroupObjects'][0] == "[W]" || $og['gosaGroupObjects'][0] == "[T]") && 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'])){
212         $test = array_unique(array_intersect($this->used_workstations, $_POST['objects']));
213         if(count($test)) {
214           $str = "";
215           foreach($test as $dn) {
216             $str .= "<li>".$dn."</li>";
217           }
218           msg_dialog::display(_("Error"), sprintf(_("These systems are already configured by other object groups and cannot be added:")."<br><ul>%s</ul>", $str), ERROR_DIALOG);
219         }
220         $tmp = "";
221         foreach($this->memberList as $obj){
222           $tmp .= $obj['type'];
223         }
224         $skipped = FALSE;
225         foreach ($_POST['objects'] as $value){
226           if(preg_match("/T/",$tmp) && $this->objects[$value]['type'] == "W"){
227             $skipped =TRUE;
228           }elseif(preg_match("/W/",$tmp) && $this->objects[$value]['type'] == "T"){
229             $skipped =TRUE;
230           }elseif(in_array($value, $this->used_workstations)) {
231             continue;
232           }else{
233             $this->memberList["$value"]= $this->objects[$value];
234             $this->member["$value"]= $value;
235             unset ($this->objects[$value]);
236             uasort ($this->memberList, 'sort_list');
237             reset ($this->memberList);
238           }
239         }
240         if($skipped){
241           msg_dialog::display(_("Information"), _("You cannot combine terminals and workstations in one object group!"), INFO_DIALOG);
242         }
243         $this->reload();
244       }
245     }
246   }
248   function execute()
249   {
250     /* Call parent execute */
251     plugin::execute();
253     if(!$this->view_logged){
254       $this->view_logged = TRUE;
255       new log("view","ogroups/".get_class($this),$this->dn);
256     }
259     /* Do we represent a valid group? */
260     if (!$this->is_account){
261       $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
262         msgPool::noValidExtension("object group")."</b>";
263       return ($display);
264     }
267     /* Load templating engine */
268     $smarty= get_smarty();
270     $tmp = $this->plInfo();
271     foreach($tmp['plProvidedAcls'] as $name => $translation){
272       $smarty->assign($name."ACL",$this->getacl($name));
273     }
275     /* Base select dialog */
276     $once = true;
277     foreach($_POST as $name => $value){
278       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
279         $once = false;
280         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
281         $this->dialog->setCurrentBase($this->base);
282       }
283     }
285     /***********
286      * Trusts 
287      ***********/
289     /* Add user workstation? */
290     if (isset($_POST["add_ws"])){
291       $this->show_ws_dialog= TRUE;
292       $this->dialog= TRUE;
293     }
295     /* Add user workstation? */
296     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
297       foreach($_POST['wslist'] as $ws){
298         $this->accessTo[$ws]= $ws;
299       }
300       ksort($this->accessTo);
301       $this->is_modified= TRUE;
302     }
304     /* Remove user workstations? */
305     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
306       foreach($_POST['workstation_list'] as $name){
307         unset ($this->accessTo[$name]);
308       }
309       $this->is_modified= TRUE;
310     }
312     /* Add user workstation finished? */
313     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
314       $this->show_ws_dialog= FALSE;
315       $this->dialog= FALSE;
316     }
318     /* Show ws dialog */
319     if ($this->show_ws_dialog){
320       /* Save data */
321       $sysfilter= session::get("sysfilter");
322       foreach( array("depselect", "regex") as $type){
323         if (isset($_POST[$type])){
324           $sysfilter[$type]= $_POST[$type];
325         }
326       }
327       if (isset($_GET['search'])){
328         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
329         if ($s == "**"){
330           $s= "*";
331         }
332         $sysfilter['regex']= $s;
333       }
334       session::set("sysfilter", $sysfilter);
336       /* Get workstation list */
337       $exclude= "";
338       foreach($this->accessTo as $ws){
339         $exclude.= "(cn=$ws)";
340       }
341       if ($exclude != ""){
342         $exclude= "(!(|$exclude))";
343       }
344       $regex= $sysfilter['regex'];
345       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
347       $deps_a = array(get_ou("serverRDN"),
348           get_ou("terminalRDN"),
349           get_ou("workstationRDN"));
350       $res= get_sub_list($filter, array("terminal","server","workstation"), $deps_a, get_ou("systemRDN").$sysfilter['depselect'],
351           array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
352       $wslist= array();
353       foreach ($res as $attrs){
354         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
355       }
356       asort($wslist);
357       $smarty->assign("search_image", get_template_path('images/lists/search.png'));
358       $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
359       $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
360       $smarty->assign("deplist", $this->config->idepartments);
361       $smarty->assign("alphabet", generate_alphabet());
362       foreach( array("depselect", "regex") as $type){
363         $smarty->assign("$type", $sysfilter[$type]);
364       }
365       $smarty->assign("hint", print_sizelimit_warning());
366       $smarty->assign("wslist", $wslist);
367       $smarty->assign("apply", apply_filter());
368       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
369       return ($display);
370     }
372     /***********
373      * Ende - Trusts 
374      ***********/
377     /* Dialog handling */
378     if(is_object($this->dialog) && $this->acl_is_moveable()){
379       /* Must be called before save_object */
380       $this->dialog->save_object();
382       if($this->dialog->isClosed()){
383         $this->dialog = false;
384       }elseif($this->dialog->isSelected()){
386         /* A new base was selected, check if it is a valid one */
387         $tmp = $this->get_allowed_bases();
388         if(isset($tmp[$this->dialog->isSelected()])){
389           $this->base = $this->dialog->isSelected();
390         }
391         $this->dialog= false;
392       }else{
393         return($this->dialog->execute());
394       }
395     }
397     /* Add objects? */
398     if (isset($_POST["edit_membership"])){
399       $this->group_dialog= TRUE;
400       $this->dialog= TRUE;
401     }
403     /* Add objects finished? */
404     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
405       $this->group_dialog= FALSE;
406       $this->dialog= FALSE;
407     }
409     /* Manage object add dialog */
410     if ($this->group_dialog){
412       /* Save data */
413       $ogfilter= session::get("ogfilter");
414       foreach( array("dselect", "regex") as $type){
415         if (isset($_POST[$type])){
416           $ogfilter[$type]= $_POST[$type];
417         }
418       }
419       if (isset($_POST['dselect'])){
420         foreach( array("accounts", "groups", "applications", "departments",
421               "servers", "workstations", "winstations", "terminals", "printers","subtrees",
422               "phones") as $type){
424           if (isset($_POST[$type])) {
425             $ogfilter[$type]= "checked";
426           } else {
427             $ogfilter[$type]= "";
428           }
429         }
430       }
431       if (isset($_GET['search'])){
432         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
433         if ($s == "**"){
434           $s= "*";
435         }
436         $ogfilter['regex']= $s;
437       }
438       session::set("ogfilter", $ogfilter);
439       $this->reload();
441       /* Calculate actual groups / remove entries that are already inside... */
442       $strippedObjects= array();
443       foreach ($this->objects as $key => $value){
444         if (!isset($this->member[$key])){
445           $strippedObjects[$key]= $value;
446         }
447       }
448       $smarty->assign("objects", $this->convert_list($strippedObjects));
450       /* Show dialog */
451       $smarty->assign("search_image", get_template_path('images/lists/search.png'));
452       $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
453       $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
454       $smarty->assign("deplist", $this->config->idepartments);
455       $smarty->assign("alphabet", generate_alphabet());
456       foreach( array("dselect", "regex", "subtrees") as $type){
457         $smarty->assign("$type", $ogfilter[$type]);
458       }
459       $smarty->assign("hint", print_sizelimit_warning());
460       $smarty->assign("apply", apply_filter());
462       /* Build up checkboxes 
463        */
464       $ar = array(
465           "departments" => array(
466             "T" => msgPool::selectToView(_("departments")),
467             "C" => (isset($ogfilter['departments']) && ($ogfilter['departments'])),
468             "L" => sprintf(_("Show %s"),_("departments"))),
469           "accounts" => array(
470             "T" => msgPool::selectToView(_("people")),
471             "C" => (isset($ogfilter['accounts']) && ($ogfilter['accounts'])),
472             "L" => sprintf(_("Show %s"),_("people"))),
473           "groups"=> array(
474             "T" => msgPool::selectToView(_("groups")),
475             "C" => (isset($ogfilter['groups']) && ($ogfilter['groups'])),
476             "L" => sprintf(_("Show %s"),_("groups"))),
477           "servers"=> array(
478             "T" => msgPool::selectToView(_("servers")),
479             "C" => (isset($ogfilter['servers']) && ($ogfilter['servers'])),
480             "L" => sprintf(_("Show %s"),_("servers"))),
481           "workstations"=> array(
482             "T" => msgPool::selectToView(_("workstations")),
483             "C" => (isset($ogfilter['workstations']) && ($ogfilter['workstations'])),
484             "L" => sprintf(_("Show %s"),_("workstations"))),
485           "terminals"=> array(
486             "T" => msgPool::selectToView(_("terminals")),
487             "C" => (isset($ogfilter['terminals']) && ($ogfilter['terminals'])),
488             "L" => sprintf(_("Show %s"),_("terminals"))),
489           "printers"=> array(
490             "T" => msgPool::selectToView(_("printer")),
491             "C" => (isset($ogfilter['printers']) && ($ogfilter['printers'])),
492             "L" => sprintf(_("Show %s"),_("printers"))),
493           "phones"=> array(
494             "T" => msgPool::selectToView(_("phones")),
495             "C" => (isset($ogfilter['phones']) && ($ogfilter['phones'])),
496             "L" => sprintf(_("Show %s"),_("phones"))));
497  
498       /* Allow selecting applications if we are having a non 
499           release managed application storage */ 
500       if(!$this->IsReleaseManagementActivated()){
501         $ar["applications"] = array(
502             "T" => msgPool::selectToView(_("applications")),
503             "C" => (isset($ogfilter['applications']) && ($ogfilter['applications'])),
504             "L" => sprintf(_("Show %s"),_("applications")));
505       }
507       $smarty->assign("checkboxes",$ar);
508       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
509       return ($display);
510     }
512     /* Bases / Departments */
513       if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
514         $this->base= $_POST['base'];
515       }
517     /* Assemble combine string */
518     if ($this->gosaGroupObjects == "[]"){
519       $smarty->assign("combinedObjects", _("none"));
520     } elseif (strlen($this->gosaGroupObjects) > 4){
521       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
522     } else {
523       $conv= array(   "U" => _("users"),
524           "G" => _("groups"),
525           "A" => _("applications"),
526           "D" => _("departments"),
527           "S" => _("servers"),
528           "W" => _("workstations"),
529           "O" => _("winstations"),
530           "T" => _("terminals"),
531           "F" => _("phones"),
532           "P" => _("printers"));
534       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
535       $p1= $conv[$type[0]];
536       error_reporting(0);
537       if (isset($type[1]) && preg_match('/[UGADSFOWTP]/', $type[1])){
538         $p2= $conv[$type[1]];
539         $smarty->assign("combinedObjects", sprintf("'%s' and '%s'", $p1, $p2));
540       } else {
541         $smarty->assign("combinedObjects", "$p1");
542       }
543       error_reporting(E_ALL | E_STRICT);
544     }
546     /* Assign variables */
547     $smarty->assign("bases", $this->get_allowed_bases());
548     $smarty->assign("base_select", $this->base);
549     $smarty->assign("department", $this->department);
550     $smarty->assign("members", $this->convert_list($this->memberList));
552     /* Objects have to be tuned... */
553     $smarty->assign("objects", $this->convert_list($this->objects));
555     /* Fields */
556     foreach ($this->attributes as $val){
557       $smarty->assign("$val", $this->$val);
558     }
560     /******
561       Trust account
562      ******/
563     $smarty->assign("trusthide", " disabled ");
564     $smarty->assign("trustmodeACL",  $this->getacl("trustModel"));
565     if ($this->trustModel == "fullaccess"){
566       $trustmode= 1;
567       // pervent double disable tag in html code, this will disturb our clean w3c html
568       $smarty->assign("trustmode",  $this->getacl("trustModel"));
570     } elseif ($this->trustModel == "byhost"){
571       $trustmode= 2;
572       $smarty->assign("trusthide", "");
573     } else {
574       // pervent double disable tag in html code, this will disturb our clean w3c html
575       $smarty->assign("trustmode",  $this->getacl("trustModel"));
576       $trustmode= 0;
577     }
578     $smarty->assign("trustmode", $trustmode);
579     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
580           2 => _("allow access to these hosts")));
582     $smarty->assign("workstations", $this->accessTo);
584     if((count($this->accessTo))==0){
585       $smarty->assign("emptyArrAccess",true);
586     }else{
587       $smarty->assign("emptyArrAccess",false);
588     }
589     /******
590       Ende - Trust account
591      ******/
593     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
594   }
597   /* Save data to object */
598   function save_object()
599   {
600     /* Save additional values for possible next step */
601     if (isset($_POST['ogroupedit'])){
603       /******
604         Trust account 
605        ******/
607       if($this->acl_is_writeable("trustModel")){
608         if (isset($_POST['trustmode'])){
609           $saved= $this->trustModel;
610           if ($_POST['trustmode'] == "1"){
611             $this->trustModel= "fullaccess";
612           } elseif ($_POST['trustmode'] == "2"){
613             $this->trustModel= "byhost";
614           } else {
615             $this->trustModel= "";
616           }
617           if ($this->trustModel != $saved){
618             $this->is_modified= TRUE;
619           }
620         }
621       }
622       /******
623         Ende Trust account
624        ******/
626       /* Create a base backup and reset the
627          base directly after calling plugin::save_object();
628          Base will be set seperatly a few lines below */
629       $base_tmp = $this->base;
630       plugin::save_object();
631       $this->base = $base_tmp;
633       /* Save base, since this is no LDAP attribute */
634       $tmp = $this->get_allowed_bases();
635       if(isset($_POST['base'])){
636         if(isset($tmp[$_POST['base']])){
637           $this->base= $_POST['base'];
638         }
639       }
640     }
641   }
644   /* (Re-)Load objects */
645   function reload()
646   {
647     /*###########
648       Variable initialisation 
649       ###########*/
651     $this->objects                = array();
652     $this->ui                     = get_userinfo();
653     $filter                       = "";
654     $objectClasses                = array();
655     
656     $ogfilter               = session::get("ogfilter");
657     $regex                  = $ogfilter['regex'];
659     $ldap= $this->config->get_ldap_link();
660     $ldap->cd ($ogfilter['dselect']);
663     /*###########
664       Generate Filter 
665       ###########*/
667     $p_f= array("accounts"=> array("OBJ"=>"user", "CLASS"=>"gosaAccount"    ,
668           "DN"=> get_people_ou()           ,"ACL" => "users"), 
669         "groups"          => array("OBJ"=>"group", "CLASS"=>"posixGroup"     ,
670           "DN"=> get_groups_ou('ogroupRDN') ,"ACL" => "groups"), 
671         "departments"     => array("OBJ"=>"department", "CLASS"=>"gosaDepartment" ,
672           "DN"=> ""                        ,"ACL" => "department"), 
673         "servers"         => array("OBJ"=>"servgeneric", "CLASS"=>"goServer"       ,
674           "DN"=> get_ou('serverRDN')        ,"ACL" => "server"),
675         "workstations"    => array("OBJ"=>"workgeneric", "CLASS"=>"gotoWorkstation",
676           "DN"=> get_ou('workstationRDN')   ,"ACL" => "workstation"),
677         "winstations"     => array("OBJ"=>"wingeneric", "CLASS"=>"opsiClient",        
678           "DN"=> get_ou('SAMBAMACHINEACCOUNTRDN')     ,"ACL" => "winstation"),
679         "terminals"       => array("OBJ"=>"termgeneric", "CLASS"=>"gotoTerminal"   ,
680           "DN"=> get_ou('terminalRDN')      ,"ACL" => "terminal"),
681         "printers"        => array("OBJ"=>"printgeneric", "CLASS"=>"gotoPrinter"    ,
682           "DN"=> get_ou('printerRDN')       ,"ACL" => "printer"),
683         "phones"          => array("OBJ"=>"phoneGeneric", "CLASS"=>"goFonHardware"  ,
684           "DN"=> get_ou('phoneRDN')         ,"ACL" => "phone"));
687     /* Allow searching for applications, if we are not using release managed applications 
688       */
689     if(!$this->IsReleaseManagementActivated()){
690       $p_f[      "applications"]    = array("OBJ"=>"application", "CLASS"=>"gosaApplication",
691           "DN"=> get_ou('applicationRDN')   ,"ACL" => "application"); 
692     }
693            
694     /*###########
695       Perform search for selected objectClasses & regex to fill list with objects   
696       ###########*/
698     $Get_list_flags = 0;
699     if($ogfilter['subtrees'] == "checked"){
700       $Get_list_flags |= GL_SUBSEARCH;
701     }    
703     foreach($p_f as $post_name => $data){
705       if($ogfilter[$post_name] == "checked" && class_available($data['OBJ'])){
707         if($ogfilter['subtrees']){
708           $base =  $ogfilter['dselect'];
709         }else{
710           $base =  $data['DN'].$ogfilter['dselect'];
711         }
712    
713          
714         $filter = "(&(objectClass=".$data['CLASS'].")(|(uid=$regex)(cn=$regex)(ou=$regex)))";
715         $res    = get_list($filter, $data['ACL']  , $base, 
716                     array("description", "objectClass", "sn", "givenName", "uid","ou","cn"),$Get_list_flags);
718         /* fetch results and append them to the list */
719         foreach($res as $attrs){
721           /* Skip workstations which are already assigned to an object group.
722            */
723           if ($this->gosaGroupObjects == "[W]" || $this->gosaGroupObjects == "[T]"){
724             if(in_array($attrs['dn'],$this->used_workstations)){
725               continue;
726             }
727           }
729           $type= $this->getObjectType($attrs);
730           $name= $this->getObjectName($attrs);
732           /* Fill array */
733           if (isset($attrs["description"][0])){
734             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
735           } elseif (isset($attrs["uid"][0])) {
736             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
737           } else {
738             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
739           }
740         }
741       }
742     }
743     uasort ($this->objects, 'sort_list');
744     reset ($this->objects);
746     
747     /*###########
748       Build member list and try to detect obsolete entries 
749       ###########*/
751     $this->memberList = array();
752   
753     /* Walk through all single member entry */
754     foreach($this->member as $dn){
756       /* The dn for the current member can't be resolved 
757          it seams that this entry was removed 
758        */ 
759       /* Try to resolv the entry again, if it still fails, display error msg */
760       $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass", "macAddress"));
762       /* It has failed, add entry with type flag I (Invalid)*/
763       if (!$ldap->success()){
764         $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".LDAP::fix($dn),"type" => "I");
766       } else {
768         /* Append this entry to our all object list */
770         /* Fetch object */
771         $attrs= $ldap->fetch();
773         $type= $this->getObjectType($attrs);
774         $name= $this->getObjectName($attrs);
776         if (isset($attrs["description"][0])){
777           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
778         } elseif (isset($attrs["uid"][0])) {
779           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
780         } else {
781           $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
782         }
783         $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
785         if(isset($attrs['macAddress'][0])){
786           $this->objcache[$attrs["dn"]]['macAddress']  = $attrs['macAddress'][0];
787         }else{
788           $this->objcache[$attrs["dn"]]['macAddress']  = "";
789         }
791         if(isset($attrs['uid'])){
792           $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
793         }
795         /* Fill array */
796         if (isset($attrs["description"][0])){
797           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
798         } else {
799           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
800         }
802         $this->memberList[$dn]= $this->objects[$attrs["dn"]];
803       }
804     }
805     uasort ($this->memberList, 'sort_list');
806     reset ($this->memberList);
808     /* Assemble types of currently combined objects */
809     $objectTypes= "";
810     foreach ($this->memberList as $dn => $desc){
812       /* Invalid object? */
813       if ($desc['type'] == 'I'){
814         continue;
815       }
817       /* Fine. Add to list. */
818       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
819         $objectTypes.= $desc['type'];
820       }
821     }
822     $this->gosaGroupObjects= "[$objectTypes]";
823   }
826   function convert_list($input)
827   {
828     $temp= "";
829     $conv= array(  
830         "Y" => "plugins/users/images/select_template.png",
831         "U" => "plugins/generic/images/head.png",
832         "G" => "plugins/groups/images/groups.png",
833         "A" => "plugins/ogroups/images/application.png",
834         "D" => "plugins/departments/images/department.png",
835         "S" => "plugins/ogroups/images/server.png",
836         "W" => "plugins/ogroups/images/workstation.png",
837         "O" => "plugins/ogroups/images/winstation.png",
838         "T" => "plugins/ogroups/images/terminal.png",
839         "F" => "plugins/ogroups/images/phone.png",
840         "P" => "plugins/ogroups/images/printer.png",
841         "I" => "images/false.png");
843     foreach ($input as $key => $value){
844       /* Generate output */
845       $temp.= "<option title='".addslashes( $key)."' value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path($conv[$value['type']])."');\">".$value['text']."</option>\n";
846     }
848     return ($temp);
849   }
852   function getObjectType($attrs)
853   {
854     $type= "I";
856     foreach(array(  
857           "Y" => "gosaUserTemplate",
858           "U" => "gosaAccount",
859           "G" => "posixGroup",
860           "A" => "gosaApplication",
861           "D" => "gosaDepartment",
862           "S" => "goServer",
863           "W" => "gotoWorkstation",
864           "O" => "opsiClient",
865           "T" => "gotoTerminal",
866           "F" => "goFonHardware",
867           "P" => "gotoPrinter") as $index => $class){
868       if (in_array($class, $attrs['objectClass'])){
869         $type= $index;
870         break;
871       }
872     }
874     return ($type);
875   }
878   function getObjectName($attrs)
879   {
880     /* Person? */
881     $name =""; 
882     if (in_array('gosaAccount', $attrs['objectClass'])){
883       if(isset($attrs['sn']) && isset($attrs['givenName'])){
884         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
885       } else {
886         $name= $attrs['uid'][0];
887       }
888     } else {
889       if(isset($attrs["cn"][0])) {
890         $name= $attrs['cn'][0];
891       } else {
892         $name= $attrs['ou'][0];
893       }
894     }
896     return ($name);
897   }
900   function check()
901   {
902     /* Call common method to give check the hook */
903     $message= plugin::check();
905     /* Permissions for that base? */
906     if ($this->base != ""){
907       $new_dn= 'cn='.$this->cn.','.get_ou('ogroupRDN').$this->base;
908     } else {
909       $new_dn= $this->dn;
910     }
912     $ldap = $this->config->get_ldap_link();
913     if(LDAP::fix($this->dn) != LDAP::fix($new_dn)){
914       $ldap->cat ($new_dn, array('dn'));
915     }
916     
917     if($ldap->count() !=0){
918       $message[]= msgPool::duplicated(_("Name"));
919     } 
921     /* Set new acl base */
922     if($this->dn == "new") {
923       $this->set_acl_base($this->base);
924     }
926     /* must: cn */
927     if ($this->cn == ""){
928       $message[]= msgPool::required(_("Name"));
929     }
931     if (preg_match('/[=,+<>#;]/', $this->cn)) {
932       $message[] = msgPool::invalid(_("Name"), $this->cn, "/[^=+,<>#;]/");
933     }
935     /* To many different object types? */
936     if (strlen($this->gosaGroupObjects) > 4){
937       $message[]= _("You can combine two different object types at maximum, only!");
938     }
940     /* Check if we are allowed to create or move this object 
941      */
942     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
943       $message[] = msgPool::permCreate();
944     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
945       $message[] = msgPool::permMove();
946     }
948     return ($message);
949   }
952   /* Save to LDAP */
953   function save()
954   {
955     plugin::save();
957     /* Move members to target array */
958     $this->attrs['member'] =array();
959     foreach ($this->member as $key => $desc){
960       $this->attrs['member'][]= LDAP::fix($key);
961     }
963     $ldap= $this->config->get_ldap_link();
965     /* New accounts need proper 'dn', propagate it to remaining objects */
966     if ($this->dn == 'new'){
967       $this->dn= 'cn='.$this->cn.','.get_ou('ogroupRDN').$this->base;
968     }
970     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
971        new entries. So do a check first... */
972     $ldap->cat ($this->dn, array('dn'));
973     if ($ldap->fetch()){
974       /* Modify needs array() to remove values :-( */
975       if (!count ($this->member)){
976         $this->attrs['member']= array();
977       }
978       $mode= "modify";
980     } else {
981       $mode= "add";
982       $ldap->cd($this->config->current['BASE']);
983       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
984     }
986     /******
987       Trust accounts 
988      ******/
989     $objectclasses= array();
990     foreach ($this->attrs['objectClass'] as $key => $class){
991       if (preg_match('/trustAccount/i', $class)){
992         continue;
993       }
994       $objectclasses[]= $this->attrs['objectClass'][$key];
995     }
996     $this->attrs['objectClass']= $objectclasses;
997     if ($this->trustModel != ""){
998       $this->attrs['objectClass'][]= "trustAccount";
999       $this->attrs['trustModel']= $this->trustModel;
1000       $this->attrs['accessTo']= array();
1001       if ($this->trustModel == "byhost"){
1002         foreach ($this->accessTo as $host){
1003           $this->attrs['accessTo'][]= $host;
1004         }
1005       }
1006     } else {
1007       if ($this->was_trust_account){
1008         $this->attrs['accessTo']= array();
1009         $this->attrs['trustModel']= array();
1010       }
1011     }
1013     /******
1014       Ende - Trust accounts 
1015      ******/
1017     /* Write back to ldap */
1018     $ldap->cd($this->dn);
1019     $this->cleanup();
1020     $ldap->$mode($this->attrs);
1022     if($mode == "add"){
1023       new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1024     }else{
1025       new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1026     }
1028     /* Trigger post signal */
1029     $this->handle_post_events($mode);
1031     $ret= 0;
1032     if (!$ldap->success()){
1033       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
1034       $ret= 1;
1035     }
1037     return ($ret);
1038   }
1040   function remove_from_parent()
1041   {
1042     plugin::remove_from_parent();
1044     $ldap= $this->config->get_ldap_link();
1045     $ldap->rmdir($this->dn);
1046     if (!$ldap->success()){
1047       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
1048     }
1050     new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1052     /* Trigger remove signal */
1053     $this->handle_post_events("remove");
1054   }
1056   
1057   function PrepareForCopyPaste($source)
1058   {
1059     /* Update available object types */
1060     if(isset($source['gosaGroupObjects'][0])){
1061       $this->gosaGroupObjects =  $source['gosaGroupObjects'][0];
1062     }
1064     /* Reload tabs */
1065     $this->parent->reload($this->gosaGroupObjects );
1066    
1067     /* Reload plugins */ 
1068     foreach($this->parent->by_object as $name => $class ){
1069       if(get_class($this) != $name) {
1070         $this->parent->by_object[$name]->PrepareForCopyPaste($source);
1071       }
1072     }
1074     /* Load member objects */
1075     if (isset($source['member'])){
1076       foreach ($source['member'] as $key => $value){
1077         if ("$key" != "count"){
1078           $value= @LDAP::convert($value);
1079           $this->member["$value"]= "$value";
1080         }
1081       }
1082     }
1084   }
1087   function getCopyDialog()
1088   {
1089     $smarty = get_smarty();
1090     $smarty->assign("cn",     $this->cn);
1091     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1092     $ret = array();
1093     $ret['string'] = $str;
1094     $ret['status'] = "";
1095     return($ret);
1096   }
1098   function saveCopyDialog()
1099   {
1100     if(isset($_POST['cn'])){
1101       $this->cn = $_POST['cn'];
1102     }
1103   }
1106   function IsReleaseManagementActivated()
1107   {
1108     /* Check if we should enable the release selection */
1109     $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
1110     if(!empty($tmp)){
1111       return(true);
1112     }
1113     return(false);
1114   }
1117   static function plInfo()
1118   {
1119     return (array(
1120           "plShortName"   => _("Generic"),
1121           "plDescription" => _("Object group generic"),
1122           "plSelfModify"  => FALSE,
1123           "plDepends"     => array(),
1124           "plPriority"    => 1,
1125           "plSection"     => array("administration"),
1126           "plCategory"    => array("ogroups" => array("description"  => _("Object groups"),
1127                                                       "objectClass"  => "gosaGroupOfNames")),
1128           "plProvidedAcls"=> array(
1129             "cn"                => _("Name"),
1130             "base"              => _("Base"),
1131             "description"       => _("Description"),
1132             "trustModel"        => _("Sytem trust"),
1133             "member"            => _("Member"))
1134           ));
1135   }
1138 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1139 ?>