1 <?php
4 /* Sort multidimensional arrays for key 'text' */
5 function sort_list($val1, $val2)
6 {
7 $v1= strtolower($val1['text']);
8 $v2= strtolower($val2['text']);
9 if ($v1 > $v2){
10 return 1;
11 }
12 if ($v1 < $v2){
13 return -1;
14 }
15 return 0;
16 }
19 class ogroup extends plugin
20 {
21 /* Variables */
22 var $cn= "";
23 var $description= "";
24 var $base= "";
25 var $gosaGroupObjects= "";
26 var $department= "";
27 var $objects= array();
28 var $objcache= array();
29 var $memberList= array();
30 var $member= array();
31 var $orig_dn= "";
32 var $group_dialog= FALSE;
34 /* attribute list for save action */
35 var $attributes= array("cn", "description", "gosaGroupObjects","member");
36 var $objectclasses= array("top", "gosaGroupOfNames");
38 function ogroup ($config, $dn= NULL)
39 {
40 plugin::plugin ($config, $dn);
41 $this->orig_dn= $dn;
43 $this->member = array();
45 /* Load member objects */
46 if (isset($this->attrs['member'])){
47 foreach ($this->attrs['member'] as $key => $value){
48 if ("$key" != "count"){
49 $this->member["$value"]= "$value";
50 }
51 }
52 }
53 $this->is_account= TRUE;
55 /* Get global filter config */
56 if (!is_global("ogfilter")){
57 $ui= get_userinfo();
58 $base= get_base_from_people($ui->dn);
59 $ogfilter= array( "dselect" => $base,
60 "regex" => "*");
61 register_global("ogfilter", $ogfilter);
62 }
63 $ogfilter= get_global('ogfilter');
65 /* Adjust flags */
66 foreach( array( "U" => "accounts",
67 "G" => "groups",
68 "A" => "applications",
69 "D" => "departments",
70 "S" => "servers",
71 "W" => "workstations",
72 "T" => "terminals",
73 "F" => "phones",
74 "_" => "subtrees",
75 "P" => "printers") as $key => $val){
77 if (preg_match("/$key/", $this->gosaGroupObjects)){
78 $ogfilter[$val]= "checked";
79 } else {
80 $ogfilter[$val]= "";
81 }
82 }
83 register_global("ogfilter", $ogfilter);
85 if(isset($_SESSION['CurrentMainBase'])){
86 $this->base = $_SESSION['CurrentMainBase'];
87 }
89 /* Set base */
90 if ($this->dn == "new"){
91 $this->base= $_SESSION['CurrentMainBase'];
92 } else {
93 $this->base= preg_replace("/^[^,]+,".get_groups_ou()."/","",$this->dn);
94 }
96 /* Load member data */
97 $this->reload();
98 }
100 function AddDelMembership($NewMember = false){
102 if($NewMember){
103 $this->memberList[$NewMember]= $this->objcache[$NewMember];
104 $this->member[$NewMember]= $NewMember;
105 unset ($this->objects[$NewMember]);
106 uasort ($this->memberList, 'sort_list');
107 reset ($this->memberList);
108 $this->reload();
109 }else{
110 /* Delete objects from group */
111 if (isset($_POST['delete_membership']) && isset($_POST['members'])){
112 foreach ($_POST['members'] as $value){
113 $this->objects["$value"]= $this->memberList[$value];
114 unset ($this->memberList["$value"]);
115 unset ($this->member["$value"]);
116 uasort ($this->objects, 'sort_list');
117 reset ($this->objects);
118 }
119 $this->reload();
120 }
122 /* Add objects to group */
123 if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
124 foreach ($_POST['objects'] as $value){
125 $this->memberList["$value"]= $this->objects[$value];
126 $this->member["$value"]= $value;
127 unset ($this->objects[$value]);
128 uasort ($this->memberList, 'sort_list');
129 reset ($this->memberList);
130 }
131 $this->reload();
132 }
133 }
134 }
136 function execute()
137 {
138 /* Call parent execute */
139 plugin::execute();
141 // $this->reload();
143 /* Do we represent a valid group? */
144 if (!$this->is_account){
145 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
146 _("This 'dn' is no object group.")."</b>";
147 return ($display);
148 }
150 /* Delete objects from group */
151 if (isset($_POST['delete_membership']) && isset($_POST['members'])){
152 foreach ($_POST['members'] as $value){
153 if(isset($this->memberList[$value])){
154 $this->objects["$value"]= $this->memberList[$value];
155 unset ($this->memberList["$value"]);
156 unset ($this->member["$value"]);
157 uasort ($this->objects, 'sort_list');
158 reset ($this->objects);
159 }
160 }
161 $this->reload();
162 }
164 /* Add objects to group */
165 if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
166 foreach ($_POST['objects'] as $value){
167 if(isset($this->objects[$value])){
168 $this->memberList["$value"]= $this->objects[$value];
169 $this->member["$value"]= $value;
170 unset ($this->objects[$value]);
171 uasort ($this->memberList, 'sort_list');
172 reset ($this->memberList);
173 }
174 }
175 $this->reload();
176 }
178 /* Load templating engine */
179 $smarty= get_smarty();
181 $tmp = $this->plInfo();
182 foreach($tmp['plProvidedAcls'] as $name => $translation){
183 $smarty->assign($name."ACL",$this->getacl($name));
184 }
186 /* Base select dialog */
187 $once = true;
188 foreach($_POST as $name => $value){
189 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
190 $once = false;
191 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
192 $this->dialog->setCurrentBase($this->base);
193 }
194 }
196 /* Dialog handling */
197 if(is_object($this->dialog) && $this->acl_is_moveable()){
198 /* Must be called before save_object */
199 $this->dialog->save_object();
201 if($this->dialog->isClosed()){
202 $this->dialog = false;
203 }elseif($this->dialog->isSelected()){
205 /* A new base was selected, check if it is a valid one */
206 $tmp = $this->get_allowed_bases();
207 if(isset($tmp[$this->dialog->isSelected()])){
208 $this->base = $this->dialog->isSelected();
209 }
210 $this->dialog= false;
211 }else{
212 return($this->dialog->execute());
213 }
214 }
216 /* Add objects? */
217 if (isset($_POST["edit_membership"])){
218 $this->group_dialog= TRUE;
219 $this->dialog= TRUE;
220 }
222 /* Add objects finished? */
223 if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
224 $this->group_dialog= FALSE;
225 $this->dialog= FALSE;
226 }
228 /* Manage object add dialog */
229 if ($this->group_dialog){
231 /* Save data */
232 $ogfilter= get_global("ogfilter");
233 foreach( array("dselect", "regex") as $type){
234 if (isset($_POST[$type])){
235 $ogfilter[$type]= $_POST[$type];
236 }
237 }
238 if (isset($_POST['dselect'])){
239 foreach( array("accounts", "groups", "applications", "departments",
240 "servers", "workstations", "terminals", "printers","subtrees",
241 "phones") as $type){
243 if (isset($_POST[$type])) {
244 $ogfilter[$type]= "checked";
245 } else {
246 $ogfilter[$type]= "";
247 }
248 }
249 }
250 if (isset($_GET['search'])){
251 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
252 if ($s == "**"){
253 $s= "*";
254 }
255 $ogfilter['regex']= $s;
256 }
257 register_global("ogfilter", $ogfilter);
258 $this->reload();
260 /* Calculate actual groups */
261 $smarty->assign("objects", $this->convert_list($this->objects));
263 /* Show dialog */
264 $smarty->assign("search_image", get_template_path('images/search.png'));
265 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
266 $smarty->assign("tree_image", get_template_path('images/tree.png'));
267 $smarty->assign("deplist", $this->config->idepartments);
268 $smarty->assign("alphabet", generate_alphabet());
269 foreach( array("dselect", "regex", "accounts", "groups", "applications",
270 "departments", "servers", "workstations", "terminals","subtrees",
271 "printers", "phones") as $type){
272 $smarty->assign("$type", $ogfilter[$type]);
273 }
274 $smarty->assign("hint", print_sizelimit_warning());
275 $smarty->assign("apply", apply_filter());
277 $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
278 return ($display);
279 }
281 /* Bases / Departments */
282 if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
283 $this->base= $_POST['base'];
284 }
286 /* Assemble combine string */
287 if ($this->gosaGroupObjects == "[]"){
288 $smarty->assign("combinedObjects", _("none"));
289 } elseif (strlen($this->gosaGroupObjects) > 4){
290 $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
291 } else {
292 $conv= array( "U" => _("users"),
293 "G" => _("groups"),
294 "A" => _("applications"),
295 "D" => _("departments"),
296 "S" => _("servers"),
297 "W" => _("workstations"),
298 "T" => _("terminals"),
299 "F" => _("phones"),
300 "P" => _("printers"));
302 $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
303 $p1= $conv[$type[0]];
304 error_reporting(0);
305 if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
306 $p2= $conv[$type[1]];
307 $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
308 } else {
309 $smarty->assign("combinedObjects", "$p1");
310 }
311 error_reporting(E_ALL);
312 }
314 /* Assign variables */
315 $smarty->assign("bases", $this->get_allowed_bases());
316 $smarty->assign("base_select", $this->base);
317 $smarty->assign("department", $this->department);
318 $smarty->assign("members", $this->convert_list($this->memberList));
320 /* Objects have to be tuned... */
321 $smarty->assign("objects", $this->convert_list($this->objects));
323 /* Fields */
324 foreach ($this->attributes as $val){
325 $smarty->assign("$val", $this->$val);
326 }
328 return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
329 }
332 /* Save data to object */
333 function save_object()
334 {
335 /* Save additional values for possible next step */
336 if (isset($_POST['ogroupedit'])){
338 /* Create a base backup and reset the
339 base directly after calling plugin::save_object();
340 Base will be set seperatly a few lines below */
341 $base_tmp = $this->base;
342 plugin::save_object();
343 $this->base = $base_tmp;
345 /* Save base, since this is no LDAP attribute */
346 $tmp = $this->get_allowed_bases();
347 if(isset($_POST['base'])){
348 if(isset($tmp[$_POST['base']])){
349 $this->base= $_POST['base'];
350 }
351 }
352 }
353 }
356 /* (Re-)Load objects */
357 function reload()
358 {
359 /*###########
360 Variable initialisation
361 ###########*/
363 $this->objects = array();
364 $this->ui = get_userinfo();
365 $filter = "";
366 $objectClasses = array();
368 $ogfilter = get_global("ogfilter");
369 $regex = $ogfilter['regex'];
371 $ldap= $this->config->get_ldap_link();
372 $ldap->cd ($ogfilter['dselect']);
375 /*###########
376 Generate Filter
377 ###########*/
379 $p_f= array("accounts" => array("CLASS"=>"gosaAccount" ,"DN"=> get_people_ou() ,"ACL" => "users"),
380 "groups" => array("CLASS"=>"posixGroup" ,"DN"=> get_groups_ou() ,"ACL" => "groups"),
381 "applications" => array("CLASS"=>"gosaApplication","DN"=> "ou=apps," ,"ACL" => "application"),
382 "departments" => array("CLASS"=>"gosaDepartment" ,"DN"=> "" ,"ACL" => "department"),
383 "servers" => array("CLASS"=>"goServer" ,"DN"=> "ou=servers,ou=systems," ,"ACL" => "server"),
384 "workstations" => array("CLASS"=>"gotoWorkstation","DN"=> "ou=workstations,ou=systems," ,"ACL" => "workstation"),
385 "terminals" => array("CLASS"=>"gotoTerminal" ,"DN"=> "ou=terminals,ou=systems," ,"ACL" => "terminal"),
386 "printers" => array("CLASS"=>"gotoPrinter" ,"DN"=> "ou=printers,ou=systems," ,"ACL" => "printer"),
387 "phones" => array("CLASS"=>"goFonHardware" ,"DN"=> "ou=phones,ou=systems," ,"ACL" => "phone"));
390 /*###########
391 Perform search for selected objectClasses & regex to fill list with objects
392 ###########*/
394 $Get_list_flags = 0;
395 if($ogfilter['subtrees'] == "checked"){
396 $Get_list_flags |= GL_SUBSEARCH;
397 }
399 foreach($p_f as $post_name => $data){
401 if($ogfilter[$post_name] == "checked"){
402 $filter = "(&(objectClass=".$data['CLASS'].")(|(uid=$regex)(cn=$regex)(ou=$regex)))";
403 $res = get_list($filter, $data['ACL'] , $data['DN'].$ogfilter['dselect'],
404 array("description", "objectClass", "sn", "givenName", "uid","ou","cn"));
406 /* fetch results and append them to the list */
407 foreach($res as $attrs){
409 $type= $this->getObjectType($attrs);
410 $name= $this->getObjectName($attrs);
412 /* Fill array */
413 if (isset($attrs["description"][0])){
414 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
415 } elseif (isset($attrs["uid"][0])) {
416 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
417 } else {
418 $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
419 }
420 }
421 }
422 }
423 uasort ($this->objects, 'sort_list');
424 reset ($this->objects);
427 /*###########
428 Build member list and try to detect obsolete entries
429 ###########*/
431 $this->memberList = array();
433 /* Walk through all single member entry */
434 foreach($this->member as $dn){
436 /* The dn for the current member can't be resolved
437 it seams that this entry was removed
438 */
439 /* Try to resolv the entry again, if it still fails, display error msg */
440 $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
442 /* It has failed, add entry with type flag I (Invalid)*/
443 if ($ldap->error != "Success"){
444 $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
446 } else {
448 /* Append this entry to our all object list */
450 /* Fetch object */
451 $attrs= $ldap->fetch();
453 $type= $this->getObjectType($attrs);
454 $name= $this->getObjectName($attrs);
456 if (isset($attrs["description"][0])){
457 $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
458 } elseif (isset($attrs["uid"][0])) {
459 $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
460 } else {
461 $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
462 }
463 $this->objcache[$attrs["dn"]]['objectClass'] = $attrs['objectClass'];
464 if(isset($attrs['uid'])){
465 $this->objcache[$attrs["dn"]]['uid'] = $attrs['uid'];
466 }
468 /* Fill array */
469 if (isset($attrs["description"][0])){
470 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
471 } else {
472 $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
473 }
475 $this->memberList[$dn]= $this->objects[$attrs["dn"]];
476 }
477 }
478 uasort ($this->memberList, 'sort_list');
479 reset ($this->memberList);
481 /* Assemble types of currently combined objects */
482 $objectTypes= "";
483 foreach ($this->memberList as $dn => $desc){
485 /* Invalid object? */
486 if ($desc['type'] == 'I'){
487 continue;
488 }
490 /* Fine. Add to list. */
491 if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
492 $objectTypes.= $desc['type'];
493 }
494 }
495 $this->gosaGroupObjects= "[$objectTypes]";
496 }
499 function convert_list($input)
500 {
501 $temp= "";
502 $conv= array( "U" => "select_user.png",
503 "G" => "select_groups.png",
504 "A" => "select_application.png",
505 "D" => "select_department.png",
506 "S" => "select_server.png",
507 "W" => "select_workstation.png",
508 "T" => "select_terminal.png",
509 "F" => "select_phone.png",
510 "I" => "flag.png",
511 "P" => "select_printer.png");
513 foreach ($input as $key => $value){
514 /* Generate output */
515 $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
516 }
518 return ($temp);
519 }
522 function getObjectType($attrs)
523 {
524 $type= "I";
526 foreach(array( "U" => "gosaAccount",
527 "G" => "posixGroup",
528 "A" => "gosaApplication",
529 "D" => "gosaDepartment",
530 "S" => "goServer",
531 "W" => "gotoWorkstation",
532 "T" => "gotoTerminal",
533 "F" => "goFonHardware",
534 "P" => "gotoPrinter") as $index => $class){
535 if (in_array($class, $attrs['objectClass'])){
536 $type= $index;
537 break;
538 }
539 }
541 return ($type);
542 }
545 function getObjectName($attrs)
546 {
547 /* Person? */
548 $name ="";
549 if (in_array('gosaAccount', $attrs['objectClass'])){
550 if(isset($attrs['sn']) && isset($attrs['givenName'])){
551 $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
552 } else {
553 $name= $attrs['uid'][0];
554 }
555 } else {
556 if(isset($attrs["cn"][0])) {
557 $name= $attrs['cn'][0];
558 } else {
559 $name= $attrs['ou'][0];
560 }
561 }
563 return ($name);
564 }
567 function check()
568 {
569 /* Call common method to give check the hook */
570 $message= plugin::check();
572 /* Permissions for that base? */
573 if ($this->base != ""){
574 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
575 } else {
576 $new_dn= $this->dn;
577 }
580 $ldap = $this->config->get_ldap_link();
581 if($this->dn != $new_dn){
582 $ldap->cat ($new_dn, array('dn'));
583 }
585 if($ldap->count() !=0){
586 $message[]= _("There is already an object with this cn.");
587 }
589 /* Set new acl base */
590 if($this->dn == "new") {
591 $this->set_acl_base($this->base);
592 }
594 /* must: cn */
595 if ($this->cn == ""){
596 $message[]= "The required field 'Name' is not set.";
597 }
599 /* To many different object types? */
600 if (strlen($this->gosaGroupObjects) > 4){
601 $message[]= _("You can combine two different object types at maximum only!");
602 }
604 return ($message);
605 }
608 /* Save to LDAP */
609 function save()
610 {
611 plugin::save();
613 /* Move members to target array */
614 $this->attrs['member'] =array();
615 foreach ($this->member as $key => $desc){
616 $this->attrs['member'][]= @LDAP::fix($key);
617 }
619 $ldap= $this->config->get_ldap_link();
621 /* New accounts need proper 'dn', propagate it to remaining objects */
622 if ($this->dn == 'new'){
623 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
624 }
626 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
627 new entries. So do a check first... */
628 $ldap->cat ($this->dn, array('dn'));
629 if ($ldap->fetch()){
630 /* Modify needs array() to remove values :-( */
631 if (!count ($this->member)){
632 $this->attrs['member']= array();
633 }
634 $mode= "modify";
635 } else {
636 $mode= "add";
637 $ldap->cd($this->config->current['BASE']);
638 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
639 }
641 /* Write back to ldap */
642 $ldap->cd($this->dn);
643 $this->cleanup();
644 $ldap->$mode($this->attrs);
646 /* Trigger post signal */
647 $this->handle_post_events($mode);
649 $ret= 0;
650 if (show_ldap_error($ldap->get_error(), sprintf(_("Saving of object group/generic with dn '%s' failed."),$this->dn))){
651 $ret= 1;
652 }
654 return ($ret);
655 }
657 function remove_from_parent()
658 {
659 plugin::remove_from_parent();
661 $ldap= $this->config->get_ldap_link();
662 $ldap->rmdir($this->dn);
663 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object group/generic with dn '%s' failed."),$this->dn));
665 /* Trigger remove signal */
666 $this->handle_post_events("remove");
667 }
670 function getCopyDialog()
671 {
672 $smarty = get_smarty();
673 $smarty->assign("cn", $this->cn);
674 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
675 $ret = array();
676 $ret['string'] = $str;
677 $ret['status'] = "";
678 return($ret);
679 }
681 function saveCopyDialog()
682 {
683 if(isset($_POST['cn'])){
684 $this->cn = $_POST['cn'];
685 }
686 }
689 function plInfo()
690 {
691 return (array(
692 "plShortName" => _("Generic"),
693 "plDescription" => _("Object group generic"),
694 "plSelfModify" => FALSE,
695 "plDepends" => array(),
696 "plPriority" => 1,
697 "plSection" => array("administration"),
698 "plCategory" => array("ogroups" => array("description" => _("Object groups"),
699 "objectClass" => "gosaGroupOfNames")),
700 "plProvidedAcls"=> array(
701 "cn" => _("Name"),
702 "base" => _("Base"),
703 "description" => _("Description"),
704 "member" => _("Member"))
705 ));
706 }
707 }
709 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
710 ?>