Code

Moved to E_STRICT
[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 | E_STRICT);
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"){
412         if($ogfilter['subtrees']){
413           $base =  $ogfilter['dselect'];
414         }else{
415           $base =  $data['DN'].$ogfilter['dselect'];
416         }
417     
418         $filter = "(&(objectClass=".$data['CLASS'].")(|(uid=$regex)(cn=$regex)(ou=$regex)))";
419         $res    = get_list($filter, $data['ACL']  , $base, 
420                     array("description", "objectClass", "sn", "givenName", "uid","ou","cn"),$Get_list_flags);
422         /* fetch results and append them to the list */
423         foreach($res as $attrs){
425           $type= $this->getObjectType($attrs);
426           $name= $this->getObjectName($attrs);
428           /* Fill array */
429           if (isset($attrs["description"][0])){
430             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
431           } elseif (isset($attrs["uid"][0])) {
432             $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
433           } else {
434             $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
435           }
436         }
437       }
438     }
439     uasort ($this->objects, 'sort_list');
440     reset ($this->objects);
442     
443     /*###########
444       Build member list and try to detect obsolete entries 
445       ###########*/
447     $this->memberList = array();
448   
449     /* Walk through all single member entry */
450     foreach($this->member as $dn){
452       /* The dn for the current member can't be resolved 
453          it seams that this entry was removed 
454        */ 
455       /* Try to resolv the entry again, if it still fails, display error msg */
456       $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
458       /* It has failed, add entry with type flag I (Invalid)*/
459       if ($ldap->error != "Success"){
460         $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
462       } else {
464         /* Append this entry to our all object list */
466         /* Fetch object */
467         $attrs= $ldap->fetch();
469         $type= $this->getObjectType($attrs);
470         $name= $this->getObjectName($attrs);
472         if (isset($attrs["description"][0])){
473           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
474         } elseif (isset($attrs["uid"][0])) {
475           $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
476         } else {
477           $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
478         }
479         $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
480         if(isset($attrs['uid'])){
481           $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
482         }
484         /* Fill array */
485         if (isset($attrs["description"][0])){
486           $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
487         } else {
488           $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
489         }
491         $this->memberList[$dn]= $this->objects[$attrs["dn"]];
492       }
493     }
494     uasort ($this->memberList, 'sort_list');
495     reset ($this->memberList);
497     /* Assemble types of currently combined objects */
498     $objectTypes= "";
499     foreach ($this->memberList as $dn => $desc){
501       /* Invalid object? */
502       if ($desc['type'] == 'I'){
503         continue;
504       }
506       /* Fine. Add to list. */
507       if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
508         $objectTypes.= $desc['type'];
509       }
510     }
511     $this->gosaGroupObjects= "[$objectTypes]";
512   }
515   function convert_list($input)
516   {
517     $temp= "";
518     $conv= array(  "U" => "select_user.png",
519         "G" => "select_groups.png",
520         "A" => "select_application.png",
521         "D" => "select_department.png",
522         "S" => "select_server.png",
523         "W" => "select_workstation.png",
524         "T" => "select_terminal.png",
525         "F" => "select_phone.png",
526         "I" => "flag.png",
527         "P" => "select_printer.png");
529     foreach ($input as $key => $value){
530       /* Generate output */
531       $temp.= "<option title='".addslashes( $key)."' value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
532     }
534     return ($temp);
535   }
538   function getObjectType($attrs)
539   {
540     $type= "I";
542     foreach(array(  "U" => "gosaAccount",
543           "G" => "posixGroup",
544           "A" => "gosaApplication",
545           "D" => "gosaDepartment",
546           "S" => "goServer",
547           "W" => "gotoWorkstation",
548           "T" => "gotoTerminal",
549           "F" => "goFonHardware",
550           "P" => "gotoPrinter") as $index => $class){
551       if (in_array($class, $attrs['objectClass'])){
552         $type= $index;
553         break;
554       }
555     }
557     return ($type);
558   }
561   function getObjectName($attrs)
562   {
563     /* Person? */
564     $name =""; 
565     if (in_array('gosaAccount', $attrs['objectClass'])){
566       if(isset($attrs['sn']) && isset($attrs['givenName'])){
567         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
568       } else {
569         $name= $attrs['uid'][0];
570       }
571     } else {
572       if(isset($attrs["cn"][0])) {
573         $name= $attrs['cn'][0];
574       } else {
575         $name= $attrs['ou'][0];
576       }
577     }
579     return ($name);
580   }
583   function check()
584   {
585     /* Call common method to give check the hook */
586     $message= plugin::check();
588     /* Permissions for that base? */
589     if ($this->base != ""){
590       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
591     } else {
592       $new_dn= $this->dn;
593     }
596     $ldap = $this->config->get_ldap_link();
597     if($this->dn != $new_dn){
598       $ldap->cat ($new_dn, array('dn'));
599     }
600     
601     if($ldap->count() !=0){
602       $message[]= _("There is already an object with this cn.");
603     } 
605     /* Set new acl base */
606     if($this->dn == "new") {
607       $this->set_acl_base($this->base);
608     }
610     /* must: cn */
611     if ($this->cn == ""){
612       $message[]= "The required field 'Name' is not set.";
613     }
615     /* To many different object types? */
616     if (strlen($this->gosaGroupObjects) > 4){
617       $message[]= _("You can combine two different object types at maximum only!");
618     }
620     return ($message);
621   }
624   /* Save to LDAP */
625   function save()
626   {
627     plugin::save();
629     /* Move members to target array */
630     $this->attrs['member'] =array();
631     foreach ($this->member as $key => $desc){
632       $this->attrs['member'][]= @LDAP::fix($key);
633     }
635     $ldap= $this->config->get_ldap_link();
637     /* New accounts need proper 'dn', propagate it to remaining objects */
638     if ($this->dn == 'new'){
639       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
640     }
642     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
643        new entries. So do a check first... */
644     $ldap->cat ($this->dn, array('dn'));
645     if ($ldap->fetch()){
646       /* Modify needs array() to remove values :-( */
647       if (!count ($this->member)){
648         $this->attrs['member']= array();
649       }
650       $mode= "modify";
652     } else {
653       $mode= "add";
654       $ldap->cd($this->config->current['BASE']);
655       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
656     }
658     /* Write back to ldap */
659     $ldap->cd($this->dn);
660     $this->cleanup();
661     $ldap->$mode($this->attrs);
663     if($mode == "add"){
664       new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
665     }else{
666       new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
667     }
669     /* Trigger post signal */
670     $this->handle_post_events($mode);
672     $ret= 0;
673     if (show_ldap_error($ldap->get_error(), sprintf(_("Saving of object group/generic with dn '%s' failed."),$this->dn))){
674       $ret= 1;
675     }
677     return ($ret);
678   }
680   function remove_from_parent()
681   {
682     plugin::remove_from_parent();
684     $ldap= $this->config->get_ldap_link();
685     $ldap->rmdir($this->dn);
686     show_ldap_error($ldap->get_error(), sprintf(_("Removing of object group/generic with dn '%s' failed."),$this->dn));
688     new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
690     /* Trigger remove signal */
691     $this->handle_post_events("remove");
692   }
694   
695   function PrepareForCopyPaste($source)
696   {
697     /* Update available object types */
698     if(isset($source['gosaGroupObjects'][0])){
699       $this->gosaGroupObjects =  $source['gosaGroupObjects'][0];
700     }
702     /* Reload tabs */
703     $this->parent->reload($this->gosaGroupObjects );
704    
705     /* Reload plugins */ 
706     foreach($this->parent->by_object as $name => $class ){
707       if(get_class($this) != $name) {
708         $this->parent->by_object[$name]->PrepareForCopyPaste($source);
709       }
710     }
712     /* Load member objects */
713     if (isset($source['member'])){
714       foreach ($source['member'] as $key => $value){
715         if ("$key" != "count"){
716           $value= @LDAP::convert($value);
717           $this->member["$value"]= "$value";
718         }
719       }
720     }
722   }
725   function getCopyDialog()
726   {
727     $smarty = get_smarty();
728     $smarty->assign("cn",     $this->cn);
729     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
730     $ret = array();
731     $ret['string'] = $str;
732     $ret['status'] = "";
733     return($ret);
734   }
736   function saveCopyDialog()
737   {
738     if(isset($_POST['cn'])){
739       $this->cn = $_POST['cn'];
740     }
741   }
744   function plInfo()
745   {
746     return (array(
747           "plShortName"   => _("Generic"),
748           "plDescription" => _("Object group generic"),
749           "plSelfModify"  => FALSE,
750           "plDepends"     => array(),
751           "plPriority"    => 1,
752           "plSection"     => array("administration"),
753           "plCategory"    => array("ogroups" => array("description"  => _("Object groups"),
754                                                       "objectClass"  => "gosaGroupOfNames")),
755           "plProvidedAcls"=> array(
756             "cn"                => _("Name"),
757             "base"              => _("Base"),
758             "description"       => _("Description"),
759             "member"            => _("Member"))
760           ));
761   }
764 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
765 ?>