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['CurrentMainBase'])){
85 $this->base = $_SESSION['CurrentMainBase'];
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 /* Base select dialog */
180 $once = true;
181 foreach($_POST as $name => $value){
182 if(preg_match("/^chooseBase/",$name) && $once){
183 $once = false;
184 $this->dialog = new baseSelectDialog($this->config);
185 $this->dialog->setCurrentBase($this->base);
186 }
187 }
189 /* Dialog handling */
190 if(is_object($this->dialog)){
191 /* Must be called before save_object */
192 $this->dialog->save_object();
194 if($this->dialog->isClosed()){
195 $this->dialog = false;
196 }elseif($this->dialog->isSelected()){
197 $this->base = $this->dialog->isSelected();
198 $this->dialog= false;
199 }else{
200 return($this->dialog->execute());
201 }
202 }
204 /* Add objects? */
205 if (isset($_POST["edit_membership"])){
206 $this->group_dialog= TRUE;
207 $this->dialog= TRUE;
208 }
210 /* Add objects finished? */
211 if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
212 $this->group_dialog= FALSE;
213 $this->dialog= FALSE;
214 }
216 /* Manage object add dialog */
217 if ($this->group_dialog){
219 /* Save data */
220 $ogfilter= get_global("ogfilter");
221 foreach( array("dselect", "regex") as $type){
222 if (isset($_POST[$type])){
223 $ogfilter[$type]= $_POST[$type];
224 }
225 }
226 if (isset($_POST['dselect'])){
227 foreach( array("accounts", "groups", "applications", "departments",
228 "servers", "workstations", "terminals", "printers",
229 "phones") as $type){
231 if (isset($_POST[$type])) {
232 $ogfilter[$type]= "checked";
233 } else {
234 $ogfilter[$type]= "";
235 }
236 }
237 }
238 if (isset($_GET['search'])){
239 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
240 if ($s == "**"){
241 $s= "*";
242 }
243 $ogfilter['regex']= $s;
244 }
245 register_global("ogfilter", $ogfilter);
246 $this->reload();
248 /* Calculate actual groups */
249 $smarty->assign("objects", $this->convert_list($this->objects));
251 /* Show dialog */
252 $smarty->assign("search_image", get_template_path('images/search.png'));
253 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
254 $smarty->assign("tree_image", get_template_path('images/tree.png'));
255 $smarty->assign("deplist", $this->config->idepartments);
256 $smarty->assign("alphabet", generate_alphabet());
257 foreach( array("dselect", "regex", "accounts", "groups", "applications",
258 "departments", "servers", "workstations", "terminals",
259 "printers", "phones") as $type){
260 $smarty->assign("$type", $ogfilter[$type]);
261 }
262 $smarty->assign("hint", print_sizelimit_warning());
263 $smarty->assign("apply", apply_filter());
265 $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
266 return ($display);
267 }
269 /* Bases / Departments */
271 if (isset($_POST['base'])){
272 $this->base= $_POST['base'];
273 }
275 /* Assemble combine string */
276 if ($this->gosaGroupObjects == "[]"){
277 $smarty->assign("combinedObjects", _("none"));
278 } elseif (strlen($this->gosaGroupObjects) > 4){
279 $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
280 } else {
281 $conv= array( "U" => _("users"),
282 "G" => _("groups"),
283 "A" => _("applications"),
284 "D" => _("departments"),
285 "S" => _("servers"),
286 "W" => _("workstations"),
287 "T" => _("terminals"),
288 "F" => _("phones"),
289 "P" => _("printers"));
291 $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
292 $p1= $conv[$type[0]];
293 error_reporting(0);
294 if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
295 $p2= $conv[$type[1]];
296 $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
297 } else {
298 $smarty->assign("combinedObjects", "$p1");
299 }
300 error_reporting(E_ALL);
301 }
303 /* Assign variables */
304 $smarty->assign("bases", $this->config->idepartments);
305 $smarty->assign("base_select", $this->base);
306 $smarty->assign("department", $this->department);
307 $smarty->assign("members", $this->convert_list($this->memberList));
309 /* Objects have to be tuned... */
310 $smarty->assign("objects", $this->convert_list($this->objects));
312 /* Fields */
313 foreach ($this->attributes as $val){
314 $smarty->assign("$val", $this->$val);
315 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
316 }
318 /* Assign ACL's */
319 foreach (array("base", "members") as $val){
320 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
321 }
323 return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
324 }
327 /* Save data to object */
328 function save_object()
329 {
330 /* Save additional values for possible next step */
331 if (isset($_POST['ogroupedit'])){
332 plugin::save_object();
334 if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
335 $this->base= $_POST["base"];
336 }
338 }
339 }
342 /* (Re-)Load objects */
343 function reload()
344 {
345 /*###########
346 Variable initialisation
347 ###########*/
349 $this->objects = array();
350 $this->ui = get_userinfo();
351 $filter = "";
352 $objectClasses = array();
354 $ogfilter = get_global("ogfilter");
355 $regex = $ogfilter['regex'];
357 /* Get ldap connection */
358 $ldap= $this->config->get_ldap_link();
359 $ldap->cd ($ogfilter['dselect']);
362 /*###########
363 Generate Filter
364 ###########*/
366 /* Assemble filter */
367 if ($ogfilter['accounts'] == "checked"){
368 $filter.= "(objectClass=gosaAccount)";
369 $objectClasses["gosaAccount"] = get_people_ou();
370 }
371 if ($ogfilter['groups'] == "checked"){
372 $filter.= "(objectClass=posixGroup)";
373 $objectClasses["posixGroup"] = get_groups_ou();
374 }
375 if ($ogfilter['applications'] == "checked"){
376 $filter.= "(objectClass=gosaApplication)";
377 $objectClasses["gosaApplication"] = "ou=apps,";
378 }
379 if ($ogfilter['departments'] == "checked"){
380 $filter.= "(objectClass=gosaDepartment)";
381 $objectClasses["gosaDepartment"] = "";
382 }
383 if ($ogfilter['servers'] == "checked"){
384 $filter.= "(objectClass=goServer)";
385 $objectClasses["goServer"] = "ou=servers,ou=systems,";
386 }
387 if ($ogfilter['workstations'] == "checked"){
388 $filter.= "(objectClass=gotoWorkstation)";
389 $objectClasses["gotoWorkstation"] = "ou=workstations,ou=systems,";
390 }
391 if ($ogfilter['terminals'] == "checked"){
392 $filter.= "(objectClass=gotoTerminal)";
393 $objectClasses["gotoTerminal"] = "ou=terminals,ou=systems,";
394 }
395 if ($ogfilter['printers'] == "checked"){
396 $filter.= "(objectClass=gotoPrinter)";
398 $objectClasses["gotoPrinter"] = "ou=printers,ou=systems,";
399 }
400 if ($ogfilter['phones'] == "checked"){
401 $filter.= "(objectClass=goFonHardware)";
402 $objectClasses["goFonHardware"] = "ou=phones,ou=systems,";
403 }
406 /*###########
407 Perform search for selected objectClasses & regex to fill list with objects
408 ###########*/
410 /* Perform search for selected objectClasses */
411 foreach($objectClasses as $class=> $basedn){
412 $ldap->ls("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",$basedn.$ogfilter['dselect'] ,
413 array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
415 /* fetch results and append them to the list */
416 while($attrs = $ldap->fetch()){
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 uasort ($this->objects, 'sort_list');
432 reset ($this->objects);
434 /*###########
435 Get a list with all possible objects, to detect objects which doesn't exists anymore ...
436 ###########*/
438 /* Only do this, if this wasn't already done */
439 if(count($this->allobjects) == 0){
440 $ldap->cd ($this->config->current['BASE']);
441 $filter="(objectClass=gosaAccount)".
442 "(objectClass=posixGroup)".
443 "(objectClass=gosaApplication)".
444 "(objectClass=gosaDepartment)".
445 "(objectClass=goServer)".
446 "(objectClass=gotoWorkstation)".
447 "(objectClass=gotoTerminal)".
448 "(objectClass=gotoPrinter)".
449 "(objectClass=goFonHardware)";
450 $regex= "*";
452 $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
453 while ($attrs= $ldap->fetch()){
455 $type= $this->getObjectType($attrs);
456 $name= $this->getObjectName($attrs);
458 if (isset($attrs["description"][0])){
459 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
460 } elseif (isset($attrs["uid"][0])) {
461 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
462 } else {
463 $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
464 }
465 $this->allobjects[$attrs["dn"]]['objectClass'] = $attrs['objectClass'];
466 if(isset($attrs['uid'])){
467 $this->allobjects[$attrs["dn"]]['uid'] = $attrs['uid'];
468 }
469 }
470 uasort ($this->allobjects, 'sort_list');
471 reset ($this->allobjects);
472 }
475 /*###########
476 Build member list and try to detect obsolete entries
477 ###########*/
479 $this->memberList = array();
481 /* Walk through all single member entry */
482 foreach($this->member as $dn){
484 /* Object in object list? */
485 if (isset($this->allobjects[$dn])){
487 /* Add this entry to member list, its dn is in allobjects
488 this means it still exists
489 */
490 $this->memberList[$dn]= $this->allobjects[$dn];
492 /* Remove this from selectable entries */
493 if (isset ($this->objects[$dn])){
494 unset ($this->objects[$dn]);
495 }
498 } else {
500 /* The dn for the current member can't be resolved
501 it seams that this entry was removed
502 */
503 /* Try to resolv the entry again, if it still fails, display error msg */
504 $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
506 /* It has failed, add entry with type flag I (Invalid)*/
507 if ($ldap->error != "success"){
508 $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
510 } else {
512 /* Append this entry to our all object list */
514 /* Fetch object */
515 $attrs= $ldap->fetch();
517 $type= $this->getObjectType($attrs);
518 $name= $this->getObjectName($attrs);
520 if (isset($attrs["description"][0])){
521 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
522 } elseif (isset($attrs["uid"][0])) {
523 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
524 } else {
525 $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
526 }
527 $this->allobjects[$attrs["dn"]]['objectClass'] = $attrs['objectClass'];
528 if(isset($attrs['uid'])){
529 $this->allobjects[$attrs["dn"]]['uid'] = $attrs['uid'];
530 }
532 /* Fill array */
533 if (isset($attrs["description"][0])){
534 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
535 } else {
536 $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
537 }
538 }
539 }
540 }
541 uasort ($this->memberList, 'sort_list');
542 reset ($this->memberList);
544 /* Assemble types of currently combined objects */
545 $objectTypes= "";
546 foreach ($this->memberList as $dn => $desc){
548 /* Invalid object? */
549 if ($desc['type'] == 'I'){
550 continue;
551 }
553 /* Fine. Add to list. */
554 if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
555 $objectTypes.= $desc['type'];
556 }
557 }
558 $this->gosaGroupObjects= "[$objectTypes]";
559 }
562 function convert_list($input)
563 {
564 $temp= "";
565 $conv= array( "U" => "select_user.png",
566 "G" => "select_groups.png",
567 "A" => "select_application.png",
568 "D" => "select_department.png",
569 "S" => "select_server.png",
570 "W" => "select_workstation.png",
571 "T" => "select_terminal.png",
572 "F" => "select_phone.png",
573 "I" => "flag.png",
574 "P" => "select_printer.png");
576 foreach ($input as $key => $value){
577 /* Generate output */
578 $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
579 }
581 return ($temp);
582 }
585 function getObjectType($attrs)
586 {
587 $type= "I";
589 foreach(array( "U" => "gosaAccount",
590 "G" => "posixGroup",
591 "A" => "gosaApplication",
592 "D" => "gosaDepartment",
593 "S" => "goServer",
594 "W" => "gotoWorkstation",
595 "T" => "gotoTerminal",
596 "F" => "goFonHardware",
597 "P" => "gotoPrinter") as $index => $class){
598 if (in_array($class, $attrs['objectClass'])){
599 $type= $index;
600 break;
601 }
602 }
604 return ($type);
605 }
608 function getObjectName($attrs)
609 {
610 /* Person? */
611 $name ="";
612 if (in_array('gosaAccount', $attrs['objectClass'])){
613 if(isset($attrs['sn']) && isset($attrs['givenName'])){
614 $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
615 } else {
616 $name= $attrs['uid'][0];
617 }
618 } else {
619 if(isset($attrs["cn"][0])) {
620 $name= $attrs['cn'][0];
621 } else {
622 $name= $attrs['ou'][0];
623 }
624 }
626 return ($name);
627 }
630 function check()
631 {
632 /* Call common method to give check the hook */
633 $message= plugin::check();
635 /* Permissions for that base? */
636 if ($this->base != ""){
637 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
638 } else {
639 $new_dn= $this->dn;
640 }
643 $ldap = $this->config->get_ldap_link();
644 if($this->dn != $new_dn){
645 $ldap->cat ($new_dn);
646 }
648 if($ldap->count() !=0){
649 $message[]= _("There is already an object with this cn.");
650 }
652 $ui= get_userinfo();
653 $acl= get_permissions ($new_dn, $ui->subtreeACL);
654 $acl= get_module_permission($acl, "group", $new_dn);
655 if (chkacl($acl, "create") != ""){
656 $message[]= _("You have no permissions to create a group on this 'Base'.");
657 }
659 /* must: cn */
660 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
661 $message[]= "The required field 'Name' is not set.";
662 }
664 /* To many different object types? */
665 if (strlen($this->gosaGroupObjects) > 4){
666 $message[]= _("You can combine two different object types at maximum only!");
667 }
669 return ($message);
670 }
673 /* Save to LDAP */
674 function save()
675 {
676 plugin::save();
678 /* Move members to target array */
679 $this->attrs['member'] =array();
680 foreach ($this->member as $key => $desc){
681 $this->attrs['member'][]= $key;
682 }
684 $ldap= $this->config->get_ldap_link();
686 /* New accounts need proper 'dn', propagate it to remaining objects */
687 if ($this->dn == 'new'){
688 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
689 }
691 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
692 new entries. So do a check first... */
693 $ldap->cat ($this->dn);
694 if ($ldap->fetch()){
695 /* Modify needs array() to remove values :-( */
696 if (!count ($this->member)){
697 $this->attrs['member']= array();
698 }
699 $mode= "modify";
700 } else {
701 $mode= "add";
702 $ldap->cd($this->config->current['BASE']);
703 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
704 }
706 /* Write back to ldap */
707 $ldap->cd($this->dn);
708 $this->cleanup();
709 $ldap->$mode($this->attrs);
711 /* Trigger post signal */
712 $this->handle_post_events($mode);
714 $ret= 0;
715 if (show_ldap_error($ldap->get_error())){
716 $ret= 1;
717 }
719 return ($ret);
720 }
722 function remove_from_parent()
723 {
724 plugin::remove_from_parent();
726 $ldap= $this->config->get_ldap_link();
727 $ldap->rmdir($this->dn);
728 show_ldap_error($ldap->get_error());
730 /* Trigger remove signal */
731 $this->handle_post_events("remove");
732 }
734 function getCopyDialog()
735 {
736 $str = "";
737 $str .= _("Group name");
738 $str .= " <input type='text' name='cn' value='".$this->cn."'>";
739 return($str);
740 }
742 function saveCopyDialog()
743 {
744 if(isset($_POST['cn'])){
745 $this->cn = $_POST['cn'];
746 }
747 }
748 }
750 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
751 ?>