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 $allobjects= 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 "P" => "printers") as $key => $val){
76 if (preg_match("/$key/", $this->gosaGroupObjects)){
77 $ogfilter[$val]= "checked";
78 } else {
79 $ogfilter[$val]= "";
80 }
81 }
82 register_global("ogfilter", $ogfilter);
84 if(isset($_SESSION['ogroupfilter']['depselect'])){
85 $this->base = $_SESSION['ogroupfilter']['depselect'];
86 }
88 /* set permissions */
89 $ui= get_userinfo();
90 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
91 $this->acl= get_module_permission($acl, "ogroup", $ui->dn);
94 /* Load member data */
95 $this->reload();
96 }
98 function AddDelMembership($NewMember = false){
100 if($NewMember){
101 $this->memberList[$NewMember]= $this->allobjects[$NewMember];
102 $this->member[$NewMember]= $NewMember;
103 unset ($this->objects[$NewMember]);
104 uasort ($this->memberList, 'sort_list');
105 reset ($this->memberList);
106 $this->reload();
107 }else{
108 /* Delete objects from group */
109 if (isset($_POST['delete_membership']) && isset($_POST['members'])){
110 foreach ($_POST['members'] as $value){
111 $this->objects["$value"]= $this->memberList[$value];
112 unset ($this->memberList["$value"]);
113 unset ($this->member["$value"]);
114 uasort ($this->objects, 'sort_list');
115 reset ($this->objects);
116 }
117 $this->reload();
118 }
120 /* Add objects to group */
121 if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
122 foreach ($_POST['objects'] as $value){
123 $this->memberList["$value"]= $this->objects[$value];
124 $this->member["$value"]= $value;
125 unset ($this->objects[$value]);
126 uasort ($this->memberList, 'sort_list');
127 reset ($this->memberList);
128 }
129 $this->reload();
130 }
131 }
132 }
134 function execute()
135 {
136 /* Call parent execute */
137 plugin::execute();
139 // $this->reload();
141 /* Do we represent a valid group? */
142 if (!$this->is_account){
143 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
144 _("This 'dn' is no object group.")."</b>";
145 return ($display);
146 }
148 /* Delete objects from group */
149 if (isset($_POST['delete_membership']) && isset($_POST['members'])){
150 foreach ($_POST['members'] as $value){
151 if(isset($this->memberList[$value])){
152 $this->objects["$value"]= $this->memberList[$value];
153 unset ($this->memberList["$value"]);
154 unset ($this->member["$value"]);
155 uasort ($this->objects, 'sort_list');
156 reset ($this->objects);
157 }
158 }
159 $this->reload();
160 }
162 /* Add objects to group */
163 if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
164 foreach ($_POST['objects'] as $value){
165 if(isset($this->objects[$value])){
166 $this->memberList["$value"]= $this->objects[$value];
167 $this->member["$value"]= $value;
168 unset ($this->objects[$value]);
169 uasort ($this->memberList, 'sort_list');
170 reset ($this->memberList);
171 }
172 }
173 $this->reload();
174 }
176 /* Load templating engine */
177 $smarty= get_smarty();
179 /* Add objects? */
180 if (isset($_POST["edit_membership"])){
181 $this->group_dialog= TRUE;
182 $this->dialog= TRUE;
183 }
185 /* Add objects finished? */
186 if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
187 $this->group_dialog= FALSE;
188 $this->dialog= FALSE;
189 }
191 /* Manage object add dialog */
192 if ($this->group_dialog){
194 /* Save data */
195 $ogfilter= get_global("ogfilter");
196 foreach( array("dselect", "regex") as $type){
197 if (isset($_POST[$type])){
198 $ogfilter[$type]= $_POST[$type];
199 }
200 }
201 if (isset($_POST['dselect'])){
202 foreach( array("accounts", "groups", "applications", "departments",
203 "servers", "workstations", "terminals", "printers",
204 "phones") as $type){
206 if (isset($_POST[$type])) {
207 $ogfilter[$type]= "checked";
208 } else {
209 $ogfilter[$type]= "";
210 }
211 }
212 }
213 if (isset($_GET['search'])){
214 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
215 if ($s == "**"){
216 $s= "*";
217 }
218 $ogfilter['regex']= $s;
219 }
220 register_global("ogfilter", $ogfilter);
221 $this->reload();
223 /* Calculate actual groups */
224 $smarty->assign("objects", $this->convert_list($this->objects));
226 /* Show dialog */
227 $smarty->assign("search_image", get_template_path('images/search.png'));
228 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
229 $smarty->assign("tree_image", get_template_path('images/tree.png'));
230 $smarty->assign("deplist", $this->config->idepartments);
231 $smarty->assign("alphabet", generate_alphabet());
232 foreach( array("dselect", "regex", "accounts", "groups", "applications",
233 "departments", "servers", "workstations", "terminals",
234 "printers", "phones") as $type){
235 $smarty->assign("$type", $ogfilter[$type]);
236 }
237 $smarty->assign("hint", print_sizelimit_warning());
238 $smarty->assign("apply", apply_filter());
240 $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
241 return ($display);
242 }
244 /* Bases / Departments */
246 if (isset($_POST['base'])){
247 $this->base= $_POST['base'];
248 }
250 /* Assemble combine string */
251 if ($this->gosaGroupObjects == "[]"){
252 $smarty->assign("combinedObjects", _("none"));
253 } elseif (strlen($this->gosaGroupObjects) > 4){
254 $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
255 } else {
256 $conv= array( "U" => _("users"),
257 "G" => _("groups"),
258 "A" => _("applications"),
259 "D" => _("departments"),
260 "S" => _("servers"),
261 "W" => _("workstations"),
262 "T" => _("terminals"),
263 "F" => _("phones"),
264 "P" => _("printers"));
266 $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
267 $p1= $conv[$type[0]];
268 error_reporting(0);
269 if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
270 $p2= $conv[$type[1]];
271 $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
272 } else {
273 $smarty->assign("combinedObjects", "$p1");
274 }
275 error_reporting(E_ALL);
276 }
278 /* Assign variables */
279 $smarty->assign("bases", $this->config->idepartments);
280 $smarty->assign("base_select", $this->base);
281 $smarty->assign("department", $this->department);
282 $smarty->assign("members", $this->convert_list($this->memberList));
284 /* Objects have to be tuned... */
285 $smarty->assign("objects", $this->convert_list($this->objects));
287 /* Fields */
288 foreach ($this->attributes as $val){
289 $smarty->assign("$val", $this->$val);
290 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
291 }
293 /* Assign ACL's */
294 foreach (array("base", "members") as $val){
295 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
296 }
298 return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
299 }
302 /* Save data to object */
303 function save_object()
304 {
305 /* Save additional values for possible next step */
306 if (isset($_POST['ogroupedit'])){
307 plugin::save_object();
309 if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
310 $this->base= $_POST["base"];
311 }
313 }
314 }
317 /* (Re-)Load objects */
318 function reload()
319 {
320 /*###########
321 Variable initialisation
322 ###########*/
324 $this->objects = array();
325 $this->ui = get_userinfo();
326 $filter = "";
327 $objectClasses = array();
329 $ogfilter = get_global("ogfilter");
330 $regex = $ogfilter['regex'];
332 /* Get ldap connection */
333 $ldap= $this->config->get_ldap_link();
334 $ldap->cd ($ogfilter['dselect']);
337 /*###########
338 Generate Filter
339 ###########*/
341 /* Assemble filter */
342 if ($ogfilter['accounts'] == "checked"){
343 $filter.= "(objectClass=gosaAccount)";
344 $objectClasses["gosaAccount"] = get_people_ou();
345 }
346 if ($ogfilter['groups'] == "checked"){
347 $filter.= "(objectClass=posixGroup)";
348 $objectClasses["posixGroup"] = get_groups_ou();
349 }
350 if ($ogfilter['applications'] == "checked"){
351 $filter.= "(objectClass=gosaApplication)";
352 $objectClasses["gosaApplication"] = "ou=apps,";
353 }
354 if ($ogfilter['departments'] == "checked"){
355 $filter.= "(objectClass=gosaDepartment)";
356 $objectClasses["gosaDepartment"] = "";
357 }
358 if ($ogfilter['servers'] == "checked"){
359 $filter.= "(objectClass=goServer)";
360 $objectClasses["goServer"] = "ou=servers,ou=systems,";
361 }
362 if ($ogfilter['workstations'] == "checked"){
363 $filter.= "(objectClass=gotoWorkstation)";
364 $objectClasses["gotoWorkstation"] = "ou=workstations,ou=systems,";
365 }
366 if ($ogfilter['terminals'] == "checked"){
367 $filter.= "(objectClass=gotoTerminal)";
368 $objectClasses["gotoTerminal"] = "ou=terminals,ou=systems,";
369 }
370 if ($ogfilter['printers'] == "checked"){
371 $filter.= "(objectClass=gotoPrinter)";
373 $objectClasses["gotoPrinter"] = "ou=printers,ou=systems,";
374 }
375 if ($ogfilter['phones'] == "checked"){
376 $filter.= "(objectClass=goFonHardware)";
377 $objectClasses["goFonHardware"] = "ou=phones,ou=systems,";
378 }
381 /*###########
382 Perform search for selected objectClasses & regex to fill list with objects
383 ###########*/
385 /* Perform search for selected objectClasses */
386 foreach($objectClasses as $class=> $basedn){
387 $ldap->ls("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",$basedn.$ogfilter['dselect'] ,
388 array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
390 /* fetch results and append them to the list */
391 while($attrs = $ldap->fetch()){
393 $type= $this->getObjectType($attrs);
394 $name= $this->getObjectName($attrs);
396 /* Fill array */
397 if (isset($attrs["description"][0])){
398 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
399 } elseif (isset($attrs["uid"][0])) {
400 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
401 } else {
402 $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
403 }
404 }
405 }
406 uasort ($this->objects, 'sort_list');
407 reset ($this->objects);
409 /*###########
410 Get a list with all possible objects, to detect objects which doesn't exists anymore ...
411 ###########*/
413 /* Only do this, if this wasn't already done */
414 if(count($this->allobjects) == 0){
415 $ldap->cd ($this->config->current['BASE']);
416 $filter="(objectClass=gosaAccount)".
417 "(objectClass=posixGroup)".
418 "(objectClass=gosaApplication)".
419 "(objectClass=gosaDepartment)".
420 "(objectClass=goServer)".
421 "(objectClass=gotoWorkstation)".
422 "(objectClass=gotoTerminal)".
423 "(objectClass=gotoPrinter)".
424 "(objectClass=goFonHardware)";
425 $regex= "*";
427 $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
428 while ($attrs= $ldap->fetch()){
430 $type= $this->getObjectType($attrs);
431 $name= $this->getObjectName($attrs);
433 if (isset($attrs["description"][0])){
434 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
435 } elseif (isset($attrs["uid"][0])) {
436 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
437 } else {
438 $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
439 }
440 $this->allobjects[$attrs["dn"]]['objectClass'] = $attrs['objectClass'];
441 if(isset($attrs['uid'])){
442 $this->allobjects[$attrs["dn"]]['uid'] = $attrs['uid'];
443 }
444 }
445 uasort ($this->allobjects, 'sort_list');
446 reset ($this->allobjects);
447 }
450 /*###########
451 Build member list and try to detect obsolete entries
452 ###########*/
454 $this->memberList = array();
456 /* Walk through all single member entry */
457 foreach($this->member as $dn){
459 /* Object in object list? */
460 if (isset($this->allobjects[$dn])){
462 /* Add this entry to member list, its dn is in allobjects
463 this means it still exists
464 */
465 $this->memberList[$dn]= $this->allobjects[$dn];
467 /* Remove this from selectable entries */
468 if (isset ($this->objects[$dn])){
469 unset ($this->objects[$dn]);
470 }
473 } else {
475 /* The dn for the current member can't be resolved
476 it seams that this entry was removed
477 */
478 /* Try to resolv the entry again, if it still fails, display error msg */
479 $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
481 /* It has failed, add entry with type flag I (Invalid)*/
482 if ($ldap->error != "success"){
483 $this->memberList[$dn]= array('text' => _("Non existing dn: ")."$dn","type" => "I");
485 } else {
487 /* Append this entry to our all object list */
489 /* Fetch object */
490 $attrs= $ldap->fetch();
492 $type= $this->getObjectType($attrs);
493 $name= $this->getObjectName($attrs);
495 if (isset($attrs["description"][0])){
496 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
497 } elseif (isset($attrs["uid"][0])) {
498 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
499 } else {
500 $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
501 }
502 $this->allobjects[$attrs["dn"]]['objectClass'] = $attrs['objectClass'];
503 if(isset($attrs['uid'])){
504 $this->allobjects[$attrs["dn"]]['uid'] = $attrs['uid'];
505 }
507 /* Fill array */
508 if (isset($attrs["description"][0])){
509 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
510 } else {
511 $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
512 }
513 }
514 }
515 }
516 uasort ($this->memberList, 'sort_list');
517 reset ($this->memberList);
519 /* Assemble types of currently combined objects */
520 $objectTypes= "";
521 foreach ($this->memberList as $dn => $desc){
523 /* Invalid object? */
524 if ($desc['type'] == 'I'){
525 continue;
526 }
528 /* Fine. Add to list. */
529 if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
530 $objectTypes.= $desc['type'];
531 }
532 }
533 $this->gosaGroupObjects= "[$objectTypes]";
534 }
537 function convert_list($input)
538 {
539 $temp= "";
540 $conv= array( "U" => "select_user.png",
541 "G" => "select_groups.png",
542 "A" => "select_application.png",
543 "D" => "select_department.png",
544 "S" => "select_server.png",
545 "W" => "select_workstation.png",
546 "T" => "select_terminal.png",
547 "F" => "select_phone.png",
548 "I" => "flag.png",
549 "P" => "select_printer.png");
551 foreach ($input as $key => $value){
552 /* Generate output */
553 $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
554 }
556 return ($temp);
557 }
560 function getObjectType($attrs)
561 {
562 $type= "I";
564 foreach(array( "U" => "gosaAccount",
565 "G" => "posixGroup",
566 "A" => "gosaApplication",
567 "D" => "gosaDepartment",
568 "S" => "goServer",
569 "W" => "gotoWorkstation",
570 "T" => "gotoTerminal",
571 "F" => "goFonHardware",
572 "P" => "gotoPrinter") as $index => $class){
573 if (in_array($class, $attrs['objectClass'])){
574 $type= $index;
575 break;
576 }
577 }
579 return ($type);
580 }
583 function getObjectName($attrs)
584 {
585 /* Person? */
586 $name ="";
587 if (in_array('gosaAccount', $attrs['objectClass'])){
588 if(isset($attrs['sn']) && isset($attrs['givenName'])){
589 $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
590 } else {
591 $name= $attrs['uid'][0];
592 }
593 } else {
594 if(isset($attrs["cn"][0])) {
595 $name= $attrs['cn'][0];
596 } else {
597 $name= $attrs['ou'][0];
598 }
599 }
601 return ($name);
602 }
605 function check()
606 {
607 $message= array();
609 /* Permissions for that base? */
610 if ($this->base != ""){
611 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
612 } else {
613 $new_dn= $this->dn;
614 }
617 $ldap = $this->config->get_ldap_link();
618 if($this->dn != $new_dn){
619 $ldap->cat ($new_dn);
620 }
622 if($ldap->count() !=0){
623 $message[]= _("There is already an object with this cn.");
624 }
626 $ui= get_userinfo();
627 $acl= get_permissions ($new_dn, $ui->subtreeACL);
628 $acl= get_module_permission($acl, "group", $new_dn);
629 if (chkacl($acl, "create") != ""){
630 $message[]= _("You have no permissions to create a group on this 'Base'.");
631 }
633 /* must: cn */
634 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
635 $message[]= "The required field 'Name' is not set.";
636 }
638 /* To many different object types? */
639 if (strlen($this->gosaGroupObjects) > 4){
640 $message[]= _("You can combine two different object types at maximum only!");
641 }
643 return ($message);
644 }
647 /* Save to LDAP */
648 function save()
649 {
650 plugin::save();
652 /* Move members to target array */
653 $this->attrs['member'] =array();
654 foreach ($this->member as $key => $desc){
655 $this->attrs['member'][]= $key;
656 }
658 $ldap= $this->config->get_ldap_link();
660 /* New accounts need proper 'dn', propagate it to remaining objects */
661 if ($this->dn == 'new'){
662 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
663 }
665 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
666 new entries. So do a check first... */
667 $ldap->cat ($this->dn);
668 if ($ldap->fetch()){
669 /* Modify needs array() to remove values :-( */
670 if (!count ($this->member)){
671 $this->attrs['member']= array();
672 }
673 $mode= "modify";
674 } else {
675 $mode= "add";
676 $ldap->cd($this->config->current['BASE']);
677 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
678 }
680 /* Write back to ldap */
681 $ldap->cd($this->dn);
682 $this->cleanup();
683 $ldap->$mode($this->attrs);
685 /* Trigger post signal */
686 $this->handle_post_events($mode);
688 $ret= 0;
689 if (show_ldap_error($ldap->get_error())){
690 $ret= 1;
691 }
693 return ($ret);
694 }
696 function remove_from_parent()
697 {
698 plugin::remove_from_parent();
700 $ldap= $this->config->get_ldap_link();
701 $ldap->rmdir($this->dn);
702 show_ldap_error($ldap->get_error());
704 /* Trigger remove signal */
705 $this->handle_post_events("remove");
706 }
708 }
710 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
711 ?>