Code

Switched log call
[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;
33   var $view_logged = FALSE;
35   /* attribute list for save action */
36   var $attributes= array("cn", "description", "gosaGroupObjects","member");
37   var $objectclasses= array("top", "gosaGroupOfNames");
39   function ogroup ($config, $dn= NULL)
40   {
41     plugin::plugin ($config, $dn);
42     $this->orig_dn= $dn;
44     $this->member = array();
46     /* Load member objects */
47     if (isset($this->attrs['member'])){
48       foreach ($this->attrs['member'] as $key => $value){
49         if ("$key" != "count"){
50           $value= @LDAP::convert($value);
51           $this->member["$value"]= "$value";
52         }
53       }
54     }
55     $this->is_account= TRUE;
57     /* Get global filter config */
58     if (!is_global("ogfilter")){
59       $ui= get_userinfo();
60       $base= get_base_from_people($ui->dn);
61       $ogfilter= array( "dselect"       => $base,
62           "regex"           => "*");
63       register_global("ogfilter", $ogfilter);
64     }
65     $ogfilter= get_global('ogfilter');
67     /* Adjust flags */
68     foreach( array(   "U" => "accounts",
69           "G" => "groups",
70           "A" => "applications",
71           "D" => "departments",
72           "S" => "servers",
73           "W" => "workstations",
74           "T" => "terminals",
75           "F" => "phones",
76           "_" => "subtrees",
77           "P" => "printers") as $key => $val){
79       if (preg_match("/$key/", $this->gosaGroupObjects)){
80         $ogfilter[$val]= "checked";
81       } else {
82         $ogfilter[$val]= "";
83       }
84     }
85     register_global("ogfilter", $ogfilter);
86   
87     if(isset($_SESSION['CurrentMainBase'])){
88      $this->base = $_SESSION['CurrentMainBase'];
89     }
91     /* Set base */
92     if ($this->dn == "new"){
93       $this->base= $_SESSION['CurrentMainBase'];
94     } else {
95       $this->base= preg_replace("/^[^,]+,".get_groups_ou()."/","",$this->dn);
96     }
98     /* Load member data */
99     $this->reload();
100   }
102   function AddDelMembership($NewMember = false){
104     if($NewMember){
106       /* Add member and force reload */
107       $this->member[$NewMember]= $NewMember;
108       $this->reload(); 
110       $this->memberList[$NewMember]= $this->objcache[$NewMember];
111       unset ($this->objects[$NewMember]);
112       uasort ($this->memberList, 'sort_list');
113       reset ($this->memberList);
114     }else{
115       /* Delete objects from group */
116       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
117         foreach ($_POST['members'] as $value){
118           $this->objects["$value"]= $this->memberList[$value];
119           unset ($this->memberList["$value"]);
120           unset ($this->member["$value"]);
121           uasort ($this->objects, 'sort_list');
122           reset ($this->objects);
123         }
124         $this->reload();
125       }
127       /* Add objects to group */
128       if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
129         foreach ($_POST['objects'] as $value){
130           $this->memberList["$value"]= $this->objects[$value];
131           $this->member["$value"]= $value;
132           unset ($this->objects[$value]);
133           uasort ($this->memberList, 'sort_list');
134           reset ($this->memberList);
135         }
136         $this->reload();
137       }
138     }
139   }
141   function execute()
142   {
143     /* Call parent execute */
144     plugin::execute();
146     if(!$this->view_logged){
147       $this->view_logged = TRUE;
148       new log("view","ogroups/".get_class($this),$this->dn);
149     }
152     /* Do we represent a valid group? */
153     if (!$this->is_account){
154       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
155         _("This 'dn' is no object group.")."</b>";
156       return ($display);
157     }
159     /* Delete objects from group */
160     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
161       foreach ($_POST['members'] as $value){
162         if(isset($this->memberList[$value])){
163           $this->objects["$value"]= $this->memberList[$value];
164           unset ($this->memberList["$value"]);
165           unset ($this->member["$value"]);
166           uasort ($this->objects, 'sort_list');
167           reset ($this->objects);
168         }
169       }
170       $this->reload();
171     }
173     /* Add objects to group */
174     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
175       foreach ($_POST['objects'] as $value){
176         if(isset($this->objects[$value])){
177           $this->memberList["$value"]= $this->objects[$value];
178           $this->member["$value"]= $value;
179           unset ($this->objects[$value]);
180           uasort ($this->memberList, 'sort_list');
181           reset ($this->memberList);
182         }
183       }
184       $this->reload();
185     }
187     /* Load templating engine */
188     $smarty= get_smarty();
190     $tmp = $this->plInfo();
191     foreach($tmp['plProvidedAcls'] as $name => $translation){
192       $smarty->assign($name."ACL",$this->getacl($name));
193     }
195     /* Base select dialog */
196     $once = true;
197     foreach($_POST as $name => $value){
198       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
199         $once = false;
200         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
201         $this->dialog->setCurrentBase($this->base);
202       }
203     }
205     /* Dialog handling */
206     if(is_object($this->dialog) && $this->acl_is_moveable()){
207       /* Must be called before save_object */
208       $this->dialog->save_object();
210       if($this->dialog->isClosed()){
211         $this->dialog = false;
212       }elseif($this->dialog->isSelected()){
214         /* A new base was selected, check if it is a valid one */
215         $tmp = $this->get_allowed_bases();
216         if(isset($tmp[$this->dialog->isSelected()])){
217           $this->base = $this->dialog->isSelected();
218         }
219         $this->dialog= false;
220       }else{
221         return($this->dialog->execute());
222       }
223     }
225     /* Add objects? */
226     if (isset($_POST["edit_membership"])){
227       $this->group_dialog= TRUE;
228       $this->dialog= TRUE;
229     }
231     /* Add objects finished? */
232     if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
233       $this->group_dialog= FALSE;
234       $this->dialog= FALSE;
235     }
237     /* Manage object add dialog */
238     if ($this->group_dialog){
240       /* Save data */
241       $ogfilter= get_global("ogfilter");
242       foreach( array("dselect", "regex") as $type){
243         if (isset($_POST[$type])){
244           $ogfilter[$type]= $_POST[$type];
245         }
246       }
247       if (isset($_POST['dselect'])){
248         foreach( array("accounts", "groups", "applications", "departments",
249               "servers", "workstations", "terminals", "printers","subtrees",
250               "phones") as $type){
252           if (isset($_POST[$type])) {
253             $ogfilter[$type]= "checked";
254           } else {
255             $ogfilter[$type]= "";
256           }
257         }
258       }
259       if (isset($_GET['search'])){
260         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
261         if ($s == "**"){
262           $s= "*";
263         }
264         $ogfilter['regex']= $s;
265       }
266       register_global("ogfilter", $ogfilter);
267       $this->reload();
269       /* Calculate actual groups */
270       $smarty->assign("objects", $this->convert_list($this->objects));
272       /* Show dialog */
273       $smarty->assign("search_image", get_template_path('images/search.png'));
274       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
275       $smarty->assign("tree_image", get_template_path('images/tree.png'));
276       $smarty->assign("deplist", $this->config->idepartments);
277       $smarty->assign("alphabet", generate_alphabet());
278       foreach( array("dselect", "regex", "accounts", "groups", "applications",
279             "departments", "servers", "workstations", "terminals","subtrees",
280             "printers", "phones") as $type){
281         $smarty->assign("$type", $ogfilter[$type]);
282       }
283       $smarty->assign("hint", print_sizelimit_warning());
284       $smarty->assign("apply", apply_filter());
286       $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
287       return ($display);
288     }
290     /* Bases / Departments */
291       if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
292         $this->base= $_POST['base'];
293       }
295     /* Assemble combine string */
296     if ($this->gosaGroupObjects == "[]"){
297       $smarty->assign("combinedObjects", _("none"));
298     } elseif (strlen($this->gosaGroupObjects) > 4){
299       $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
300     } else {
301       $conv= array(   "U" => _("users"),
302           "G" => _("groups"),
303           "A" => _("applications"),
304           "D" => _("departments"),
305           "S" => _("servers"),
306           "W" => _("workstations"),
307           "T" => _("terminals"),
308           "F" => _("phones"),
309           "P" => _("printers"));
311       $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
312       $p1= $conv[$type[0]];
313       error_reporting(0);
314       if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
315         $p2= $conv[$type[1]];
316         $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
317       } else {
318         $smarty->assign("combinedObjects", "$p1");
319       }
320       error_reporting(E_ALL);
321     }
323     /* Assign variables */
324     $smarty->assign("bases", $this->get_allowed_bases());
325     $smarty->assign("base_select", $this->base);
326     $smarty->assign("department", $this->department);
327     $smarty->assign("members", $this->convert_list($this->memberList));
329     /* Objects have to be tuned... */
330     $smarty->assign("objects", $this->convert_list($this->objects));
332     /* Fields */
333     foreach ($this->attributes as $val){
334       $smarty->assign("$val", $this->$val);
335     }
337     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
338   }
341   /* Save data to object */
342   function save_object()
343   {
344     /* Save additional values for possible next step */
345     if (isset($_POST['ogroupedit'])){
347       /* Create a base backup and reset the
348          base directly after calling plugin::save_object();
349          Base will be set seperatly a few lines below */
350       $base_tmp = $this->base;
351       plugin::save_object();
352       $this->base = $base_tmp;
354       /* Save base, since this is no LDAP attribute */
355       $tmp = $this->get_allowed_bases();
356       if(isset($_POST['base'])){
357         if(isset($tmp[$_POST['base']])){
358           $this->base= $_POST['base'];
359         }
360       }
361     }
362   }
365   /* (Re-)Load objects */
366   function reload()
367   {
368     /*###########
369       Variable initialisation 
370       ###########*/
372     $this->objects                = array();
373     $this->ui                     = get_userinfo();
374     $filter                       = "";
375     $objectClasses                = array();
376     
377     $ogfilter               = get_global("ogfilter");
378     $regex                  = $ogfilter['regex'];
380     $ldap= $this->config->get_ldap_link();
381     $ldap->cd ($ogfilter['dselect']);
384     /*###########
385       Generate Filter 
386       ###########*/
388     $p_f= array("accounts"        => array("CLASS"=>"gosaAccount"    ,"DN"=> get_people_ou()                  ,"ACL" => "users"), 
389                 "groups"          => array("CLASS"=>"posixGroup"     ,"DN"=> get_groups_ou()                  ,"ACL" => "groups"), 
390                 "applications"    => array("CLASS"=>"gosaApplication","DN"=> "ou=apps,"                       ,"ACL" => "application"), 
391                 "departments"     => array("CLASS"=>"gosaDepartment" ,"DN"=> ""                               ,"ACL" => "department"), 
392                 "servers"         => array("CLASS"=>"goServer"       ,"DN"=> "ou=servers,ou=systems,"         ,"ACL" => "server"),
393                 "workstations"    => array("CLASS"=>"gotoWorkstation","DN"=> "ou=workstations,ou=systems,"    ,"ACL" => "workstation"),
394                 "terminals"       => array("CLASS"=>"gotoTerminal"   ,"DN"=> "ou=terminals,ou=systems,"       ,"ACL" => "terminal"),
395                 "printers"        => array("CLASS"=>"gotoPrinter"    ,"DN"=> "ou=printers,ou=systems,"        ,"ACL" => "printer"),
396                 "phones"          => array("CLASS"=>"goFonHardware"  ,"DN"=> "ou=phones,ou=systems,"          ,"ACL" => "phone"));
399     /*###########
400       Perform search for selected objectClasses & regex to fill list with objects   
401       ###########*/
403     $Get_list_flags = 0;
404     if($ogfilter['subtrees'] == "checked"){
405       $Get_list_flags |= GL_SUBSEARCH;
406     }    
408     foreach($p_f as $post_name => $data){
410       if($ogfilter[$post_name] == "checked"){
411         $filter = "(&(objectClass=".$data['CLASS'].")(|(uid=$regex)(cn=$regex)(ou=$regex)))";
412         $res    = get_list($filter, $data['ACL']  , $data['DN'].$ogfilter['dselect'], 
413                     array("description", "objectClass", "sn", "givenName", "uid","ou","cn"));
415         /* fetch results and append them to the list */
416         foreach($res as $attrs){
418           $type= $this->getObjectType($attrs);
419           $name= $this->getObjectName($attrs);
421           /* Fill array */
422           if (isset($attrs["description"][0])){
423             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
424           } elseif (isset($attrs["uid"][0])) {
425             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
426           } else {
427             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
428           }
429         }
430       }
431     }
432     uasort ($this->objects, 'sort_list');
433     reset ($this->objects);
435     
436     /*###########
437       Build member list and try to detect obsolete entries 
438       ###########*/
440     $this->memberList = array();
441   
442     /* Walk through all single member entry */
443     foreach($this->member as $dn){
445       /* The dn for the current member can't be resolved 
446          it seams that this entry was removed 
447        */ 
448       /* Try to resolv the entry again, if it still fails, display error msg */
449       $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
451       /* It has failed, add entry with type flag I (Invalid)*/
452       if ($ldap->error != "Success"){
453         $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
455       } else {
457         /* Append this entry to our all object list */
459         /* Fetch object */
460         $attrs= $ldap->fetch();
462         $type= $this->getObjectType($attrs);
463         $name= $this->getObjectName($attrs);
465         if (isset($attrs["description"][0])){
466           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
467         } elseif (isset($attrs["uid"][0])) {
468           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
469         } else {
470           $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
471         }
472         $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
473         if(isset($attrs['uid'])){
474           $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
475         }
477         /* Fill array */
478         if (isset($attrs["description"][0])){
479           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
480         } else {
481           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
482         }
484         $this->memberList[$dn]= $this->objects[$attrs["dn"]];
485       }
486     }
487     uasort ($this->memberList, 'sort_list');
488     reset ($this->memberList);
490     /* Assemble types of currently combined objects */
491     $objectTypes= "";
492     foreach ($this->memberList as $dn => $desc){
494       /* Invalid object? */
495       if ($desc['type'] == 'I'){
496         continue;
497       }
499       /* Fine. Add to list. */
500       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
501         $objectTypes.= $desc['type'];
502       }
503     }
504     $this->gosaGroupObjects= "[$objectTypes]";
505   }
508   function convert_list($input)
509   {
510     $temp= "";
511     $conv= array(  "U" => "select_user.png",
512         "G" => "select_groups.png",
513         "A" => "select_application.png",
514         "D" => "select_department.png",
515         "S" => "select_server.png",
516         "W" => "select_workstation.png",
517         "T" => "select_terminal.png",
518         "F" => "select_phone.png",
519         "I" => "flag.png",
520         "P" => "select_printer.png");
522     foreach ($input as $key => $value){
523       /* Generate output */
524       $temp.= "<option title='".addslashes( $key)."' value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
525     }
527     return ($temp);
528   }
531   function getObjectType($attrs)
532   {
533     $type= "I";
535     foreach(array(  "U" => "gosaAccount",
536           "G" => "posixGroup",
537           "A" => "gosaApplication",
538           "D" => "gosaDepartment",
539           "S" => "goServer",
540           "W" => "gotoWorkstation",
541           "T" => "gotoTerminal",
542           "F" => "goFonHardware",
543           "P" => "gotoPrinter") as $index => $class){
544       if (in_array($class, $attrs['objectClass'])){
545         $type= $index;
546         break;
547       }
548     }
550     return ($type);
551   }
554   function getObjectName($attrs)
555   {
556     /* Person? */
557     $name =""; 
558     if (in_array('gosaAccount', $attrs['objectClass'])){
559       if(isset($attrs['sn']) && isset($attrs['givenName'])){
560         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
561       } else {
562         $name= $attrs['uid'][0];
563       }
564     } else {
565       if(isset($attrs["cn"][0])) {
566         $name= $attrs['cn'][0];
567       } else {
568         $name= $attrs['ou'][0];
569       }
570     }
572     return ($name);
573   }
576   function check()
577   {
578     /* Call common method to give check the hook */
579     $message= plugin::check();
581     /* Permissions for that base? */
582     if ($this->base != ""){
583       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
584     } else {
585       $new_dn= $this->dn;
586     }
589     $ldap = $this->config->get_ldap_link();
590     if($this->dn != $new_dn){
591       $ldap->cat ($new_dn, array('dn'));
592     }
593     
594     if($ldap->count() !=0){
595       $message[]= _("There is already an object with this cn.");
596     } 
598     /* Set new acl base */
599     if($this->dn == "new") {
600       $this->set_acl_base($this->base);
601     }
603     /* must: cn */
604     if ($this->cn == ""){
605       $message[]= "The required field 'Name' is not set.";
606     }
608     /* To many different object types? */
609     if (strlen($this->gosaGroupObjects) > 4){
610       $message[]= _("You can combine two different object types at maximum only!");
611     }
613     return ($message);
614   }
617   /* Save to LDAP */
618   function save()
619   {
620     plugin::save();
622     /* Move members to target array */
623     $this->attrs['member'] =array();
624     foreach ($this->member as $key => $desc){
625       $this->attrs['member'][]= @LDAP::fix($key);
626     }
628     $ldap= $this->config->get_ldap_link();
630     /* New accounts need proper 'dn', propagate it to remaining objects */
631     if ($this->dn == 'new'){
632       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
633     }
635     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
636        new entries. So do a check first... */
637     $ldap->cat ($this->dn, array('dn'));
638     if ($ldap->fetch()){
639       /* Modify needs array() to remove values :-( */
640       if (!count ($this->member)){
641         $this->attrs['member']= array();
642       }
643       $mode= "modify";
645     } else {
646       $mode= "add";
647       $ldap->cd($this->config->current['BASE']);
648       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
649     }
651     /* Write back to ldap */
652     $ldap->cd($this->dn);
653     $this->cleanup();
654     $ldap->$mode($this->attrs);
656     if($mode == "add"){
657       new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
658     }else{
659       new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
660     }
662     /* Trigger post signal */
663     $this->handle_post_events($mode);
665     $ret= 0;
666     if (show_ldap_error($ldap->get_error(), sprintf(_("Saving of object group/generic with dn '%s' failed."),$this->dn))){
667       $ret= 1;
668     }
670     return ($ret);
671   }
673   function remove_from_parent()
674   {
675     plugin::remove_from_parent();
677     $ldap= $this->config->get_ldap_link();
678     $ldap->rmdir($this->dn);
679     show_ldap_error($ldap->get_error(), sprintf(_("Removing of object group/generic with dn '%s' failed."),$this->dn));
681     new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
683     /* Trigger remove signal */
684     $this->handle_post_events("remove");
685   }
688   function getCopyDialog()
689   {
690     $smarty = get_smarty();
691     $smarty->assign("cn",     $this->cn);
692     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
693     $ret = array();
694     $ret['string'] = $str;
695     $ret['status'] = "";
696     return($ret);
697   }
699   function saveCopyDialog()
700   {
701     if(isset($_POST['cn'])){
702       $this->cn = $_POST['cn'];
703     }
704   }
707   function plInfo()
708   {
709     return (array(
710           "plShortName"   => _("Generic"),
711           "plDescription" => _("Object group generic"),
712           "plSelfModify"  => FALSE,
713           "plDepends"     => array(),
714           "plPriority"    => 1,
715           "plSection"     => array("administration"),
716           "plCategory"    => array("ogroups" => array("description"  => _("Object groups"),
717                                                       "objectClass"  => "gosaGroupOfNames")),
718           "plProvidedAcls"=> array(
719             "cn"                => _("Name"),
720             "base"              => _("Base"),
721             "description"       => _("Description"),
722             "member"            => _("Member"))
723           ));
724   }
727 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
728 ?>