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 /* Load member data */
89 $this->reload();
90 }
92 function AddDelMembership($NewMember = false){
94 if($NewMember){
95 $this->memberList[$NewMember]= $this->allobjects[$NewMember];
96 $this->member[$NewMember]= $NewMember;
97 unset ($this->objects[$NewMember]);
98 uasort ($this->memberList, 'sort_list');
99 reset ($this->memberList);
100 $this->reload();
101 }else{
102 /* Delete objects from group */
103 if (isset($_POST['delete_membership']) && isset($_POST['members'])){
104 foreach ($_POST['members'] as $value){
105 $this->objects["$value"]= $this->memberList[$value];
106 unset ($this->memberList["$value"]);
107 unset ($this->member["$value"]);
108 uasort ($this->objects, 'sort_list');
109 reset ($this->objects);
110 }
111 $this->reload();
112 }
114 /* Add objects to group */
115 if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
116 foreach ($_POST['objects'] as $value){
117 $this->memberList["$value"]= $this->objects[$value];
118 $this->member["$value"]= $value;
119 unset ($this->objects[$value]);
120 uasort ($this->memberList, 'sort_list');
121 reset ($this->memberList);
122 }
123 $this->reload();
124 }
125 }
126 }
128 function execute()
129 {
130 /* Call parent execute */
131 plugin::execute();
133 // $this->reload();
135 /* Do we represent a valid group? */
136 if (!$this->is_account){
137 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
138 _("This 'dn' is no object group.")."</b>";
139 return ($display);
140 }
142 /* Delete objects from group */
143 if (isset($_POST['delete_membership']) && isset($_POST['members'])){
144 foreach ($_POST['members'] as $value){
145 if(isset($this->memberList[$value])){
146 $this->objects["$value"]= $this->memberList[$value];
147 unset ($this->memberList["$value"]);
148 unset ($this->member["$value"]);
149 uasort ($this->objects, 'sort_list');
150 reset ($this->objects);
151 }
152 }
153 $this->reload();
154 }
156 /* Add objects to group */
157 if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
158 foreach ($_POST['objects'] as $value){
159 if(isset($this->objects[$value])){
160 $this->memberList["$value"]= $this->objects[$value];
161 $this->member["$value"]= $value;
162 unset ($this->objects[$value]);
163 uasort ($this->memberList, 'sort_list');
164 reset ($this->memberList);
165 }
166 }
167 $this->reload();
168 }
170 /* Load templating engine */
171 $smarty= get_smarty();
173 $tmp = $this->plInfo();
174 foreach($tmp['plProvidedAcls'] as $name => $translation){
175 $smarty->assign($name."ACL",$this->getacl($name));
176 }
178 /* Create base acls */
179 $baseACL = $this->getacl("base",(!is_object($this->parent) && !isset($_SESSION['edit'])));
180 if(!$this->acl_is_moveable()) {
181 $baseACL = preg_replace("/w/","",$baseACL);
182 }
183 $smarty->assign("baseACL", $baseACL);
186 /* Get bases */
187 $ui = get_userinfo();
188 $check = $ui->get_module_departments("ogroups");
189 $bases = array();
190 foreach($check as $dn_allowed){
191 $bases[$dn_allowed] = $this->config->idepartments[$dn_allowed];
192 }
194 /* Base select dialog */
195 $once = true;
196 foreach($_POST as $name => $value){
197 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
198 $once = false;
199 $this->dialog = new baseSelectDialog($this->config,$this,$bases);
200 $this->dialog->setCurrentBase($this->base);
201 }
202 }
204 /* Dialog handling */
205 if(is_object($this->dialog) && $this->acl_is_moveable()){
206 /* Must be called before save_object */
207 $this->dialog->save_object();
209 if($this->dialog->isClosed()){
210 $this->dialog = false;
211 }elseif($this->dialog->isSelected()){
212 $this->base = $this->dialog->isSelected();
213 $this->dialog= false;
214 }else{
215 return($this->dialog->execute());
216 }
217 }
219 /* Add objects? */
220 if (isset($_POST["edit_membership"])){
221 $this->group_dialog= TRUE;
222 $this->dialog= TRUE;
223 }
225 /* Add objects finished? */
226 if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
227 $this->group_dialog= FALSE;
228 $this->dialog= FALSE;
229 }
231 /* Manage object add dialog */
232 if ($this->group_dialog){
234 /* Save data */
235 $ogfilter= get_global("ogfilter");
236 foreach( array("dselect", "regex") as $type){
237 if (isset($_POST[$type])){
238 $ogfilter[$type]= $_POST[$type];
239 }
240 }
241 if (isset($_POST['dselect'])){
242 foreach( array("accounts", "groups", "applications", "departments",
243 "servers", "workstations", "terminals", "printers",
244 "phones") as $type){
246 if (isset($_POST[$type])) {
247 $ogfilter[$type]= "checked";
248 } else {
249 $ogfilter[$type]= "";
250 }
251 }
252 }
253 if (isset($_GET['search'])){
254 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
255 if ($s == "**"){
256 $s= "*";
257 }
258 $ogfilter['regex']= $s;
259 }
260 register_global("ogfilter", $ogfilter);
261 $this->reload();
263 /* Calculate actual groups */
264 $smarty->assign("objects", $this->convert_list($this->objects));
266 /* Show dialog */
267 $smarty->assign("search_image", get_template_path('images/search.png'));
268 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
269 $smarty->assign("tree_image", get_template_path('images/tree.png'));
270 $smarty->assign("deplist", $this->config->idepartments);
271 $smarty->assign("alphabet", generate_alphabet());
272 foreach( array("dselect", "regex", "accounts", "groups", "applications",
273 "departments", "servers", "workstations", "terminals",
274 "printers", "phones") as $type){
275 $smarty->assign("$type", $ogfilter[$type]);
276 }
277 $smarty->assign("hint", print_sizelimit_warning());
278 $smarty->assign("apply", apply_filter());
280 $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
281 return ($display);
282 }
284 /* Bases / Departments */
285 if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
286 $this->base= $_POST['base'];
287 }
289 /* Assemble combine string */
290 if ($this->gosaGroupObjects == "[]"){
291 $smarty->assign("combinedObjects", _("none"));
292 } elseif (strlen($this->gosaGroupObjects) > 4){
293 $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
294 } else {
295 $conv= array( "U" => _("users"),
296 "G" => _("groups"),
297 "A" => _("applications"),
298 "D" => _("departments"),
299 "S" => _("servers"),
300 "W" => _("workstations"),
301 "T" => _("terminals"),
302 "F" => _("phones"),
303 "P" => _("printers"));
305 $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
306 $p1= $conv[$type[0]];
307 error_reporting(0);
308 if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
309 $p2= $conv[$type[1]];
310 $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
311 } else {
312 $smarty->assign("combinedObjects", "$p1");
313 }
314 error_reporting(E_ALL);
315 }
317 /* Assign variables */
318 $smarty->assign("bases", $bases);
319 $smarty->assign("base_select", $this->base);
320 $smarty->assign("department", $this->department);
321 $smarty->assign("members", $this->convert_list($this->memberList));
323 /* Objects have to be tuned... */
324 $smarty->assign("objects", $this->convert_list($this->objects));
326 /* Fields */
327 foreach ($this->attributes as $val){
328 $smarty->assign("$val", $this->$val);
329 }
331 return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
332 }
335 /* Save data to object */
336 function save_object()
337 {
338 /* Save additional values for possible next step */
339 if (isset($_POST['ogroupedit'])){
340 plugin::save_object();
342 }
343 }
346 /* (Re-)Load objects */
347 function reload()
348 {
349 /*###########
350 Variable initialisation
351 ###########*/
353 $this->objects = array();
354 $this->ui = get_userinfo();
355 $filter = "";
356 $objectClasses = array();
358 $ogfilter = get_global("ogfilter");
359 $regex = $ogfilter['regex'];
361 /* Get ldap connection */
362 $ldap= $this->config->get_ldap_link();
363 $ldap->cd ($ogfilter['dselect']);
366 /*###########
367 Generate Filter
368 ###########*/
370 /* Assemble filter */
371 if ($ogfilter['accounts'] == "checked"){
372 $filter.= "(objectClass=gosaAccount)";
373 $objectClasses["gosaAccount"] = get_people_ou();
374 }
375 if ($ogfilter['groups'] == "checked"){
376 $filter.= "(objectClass=posixGroup)";
377 $objectClasses["posixGroup"] = get_groups_ou();
378 }
379 if ($ogfilter['applications'] == "checked"){
380 $filter.= "(objectClass=gosaApplication)";
381 $objectClasses["gosaApplication"] = "ou=apps,";
382 }
383 if ($ogfilter['departments'] == "checked"){
384 $filter.= "(objectClass=gosaDepartment)";
385 $objectClasses["gosaDepartment"] = "";
386 }
387 if ($ogfilter['servers'] == "checked"){
388 $filter.= "(objectClass=goServer)";
389 $objectClasses["goServer"] = "ou=servers,ou=systems,";
390 }
391 if ($ogfilter['workstations'] == "checked"){
392 $filter.= "(objectClass=gotoWorkstation)";
393 $objectClasses["gotoWorkstation"] = "ou=workstations,ou=systems,";
394 }
395 if ($ogfilter['terminals'] == "checked"){
396 $filter.= "(objectClass=gotoTerminal)";
397 $objectClasses["gotoTerminal"] = "ou=terminals,ou=systems,";
398 }
399 if ($ogfilter['printers'] == "checked"){
400 $filter.= "(objectClass=gotoPrinter)";
402 $objectClasses["gotoPrinter"] = "ou=printers,ou=systems,";
403 }
404 if ($ogfilter['phones'] == "checked"){
405 $filter.= "(objectClass=goFonHardware)";
406 $objectClasses["goFonHardware"] = "ou=phones,ou=systems,";
407 }
410 /*###########
411 Perform search for selected objectClasses & regex to fill list with objects
412 ###########*/
414 /* Perform search for selected objectClasses */
415 foreach($objectClasses as $class=> $basedn){
416 $ldap->ls("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",$basedn.$ogfilter['dselect'] ,
417 array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
419 /* fetch results and append them to the list */
420 while($attrs = $ldap->fetch()){
422 $type= $this->getObjectType($attrs);
423 $name= $this->getObjectName($attrs);
425 /* Fill array */
426 if (isset($attrs["description"][0])){
427 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
428 } elseif (isset($attrs["uid"][0])) {
429 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
430 } else {
431 $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
432 }
433 }
434 }
435 uasort ($this->objects, 'sort_list');
436 reset ($this->objects);
438 /*###########
439 Get a list with all possible objects, to detect objects which doesn't exists anymore ...
440 ###########*/
442 /* Only do this, if this wasn't already done */
443 if(count($this->allobjects) == 0){
444 $ldap->cd ($this->config->current['BASE']);
445 $filter="(objectClass=gosaAccount)".
446 "(objectClass=posixGroup)".
447 "(objectClass=gosaApplication)".
448 "(objectClass=gosaDepartment)".
449 "(objectClass=goServer)".
450 "(objectClass=gotoWorkstation)".
451 "(objectClass=gotoTerminal)".
452 "(objectClass=gotoPrinter)".
453 "(objectClass=goFonHardware)";
454 $regex= "*";
456 $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
457 while ($attrs= $ldap->fetch()){
459 $type= $this->getObjectType($attrs);
460 $name= $this->getObjectName($attrs);
462 if (isset($attrs["description"][0])){
463 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
464 } elseif (isset($attrs["uid"][0])) {
465 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
466 } else {
467 $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
468 }
469 $this->allobjects[$attrs["dn"]]['objectClass'] = $attrs['objectClass'];
470 if(isset($attrs['uid'])){
471 $this->allobjects[$attrs["dn"]]['uid'] = $attrs['uid'];
472 }
473 }
474 uasort ($this->allobjects, 'sort_list');
475 reset ($this->allobjects);
476 }
479 /*###########
480 Build member list and try to detect obsolete entries
481 ###########*/
483 $this->memberList = array();
485 /* Walk through all single member entry */
486 foreach($this->member as $dn){
488 /* Object in object list? */
489 if (isset($this->allobjects[$dn])){
491 /* Add this entry to member list, its dn is in allobjects
492 this means it still exists
493 */
494 $this->memberList[$dn]= $this->allobjects[$dn];
496 /* Remove this from selectable entries */
497 if (isset ($this->objects[$dn])){
498 unset ($this->objects[$dn]);
499 }
502 } else {
504 /* The dn for the current member can't be resolved
505 it seams that this entry was removed
506 */
507 /* Try to resolv the entry again, if it still fails, display error msg */
508 $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
510 /* It has failed, add entry with type flag I (Invalid)*/
511 if ($ldap->error != "success"){
512 $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
514 } else {
516 /* Append this entry to our all object list */
518 /* Fetch object */
519 $attrs= $ldap->fetch();
521 $type= $this->getObjectType($attrs);
522 $name= $this->getObjectName($attrs);
524 if (isset($attrs["description"][0])){
525 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
526 } elseif (isset($attrs["uid"][0])) {
527 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
528 } else {
529 $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
530 }
531 $this->allobjects[$attrs["dn"]]['objectClass'] = $attrs['objectClass'];
532 if(isset($attrs['uid'])){
533 $this->allobjects[$attrs["dn"]]['uid'] = $attrs['uid'];
534 }
536 /* Fill array */
537 if (isset($attrs["description"][0])){
538 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
539 } else {
540 $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
541 }
542 }
543 }
544 }
545 uasort ($this->memberList, 'sort_list');
546 reset ($this->memberList);
548 /* Assemble types of currently combined objects */
549 $objectTypes= "";
550 foreach ($this->memberList as $dn => $desc){
552 /* Invalid object? */
553 if ($desc['type'] == 'I'){
554 continue;
555 }
557 /* Fine. Add to list. */
558 if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
559 $objectTypes.= $desc['type'];
560 }
561 }
562 $this->gosaGroupObjects= "[$objectTypes]";
563 }
566 function convert_list($input)
567 {
568 $temp= "";
569 $conv= array( "U" => "select_user.png",
570 "G" => "select_groups.png",
571 "A" => "select_application.png",
572 "D" => "select_department.png",
573 "S" => "select_server.png",
574 "W" => "select_workstation.png",
575 "T" => "select_terminal.png",
576 "F" => "select_phone.png",
577 "I" => "flag.png",
578 "P" => "select_printer.png");
580 foreach ($input as $key => $value){
581 /* Generate output */
582 $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
583 }
585 return ($temp);
586 }
589 function getObjectType($attrs)
590 {
591 $type= "I";
593 foreach(array( "U" => "gosaAccount",
594 "G" => "posixGroup",
595 "A" => "gosaApplication",
596 "D" => "gosaDepartment",
597 "S" => "goServer",
598 "W" => "gotoWorkstation",
599 "T" => "gotoTerminal",
600 "F" => "goFonHardware",
601 "P" => "gotoPrinter") as $index => $class){
602 if (in_array($class, $attrs['objectClass'])){
603 $type= $index;
604 break;
605 }
606 }
608 return ($type);
609 }
612 function getObjectName($attrs)
613 {
614 /* Person? */
615 $name ="";
616 if (in_array('gosaAccount', $attrs['objectClass'])){
617 if(isset($attrs['sn']) && isset($attrs['givenName'])){
618 $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
619 } else {
620 $name= $attrs['uid'][0];
621 }
622 } else {
623 if(isset($attrs["cn"][0])) {
624 $name= $attrs['cn'][0];
625 } else {
626 $name= $attrs['ou'][0];
627 }
628 }
630 return ($name);
631 }
634 function check()
635 {
636 /* Call common method to give check the hook */
637 $message= plugin::check();
639 /* Permissions for that base? */
640 if ($this->base != ""){
641 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
642 } else {
643 $new_dn= $this->dn;
644 }
647 $ldap = $this->config->get_ldap_link();
648 if($this->dn != $new_dn){
649 $ldap->cat ($new_dn, array('dn'));
650 }
652 if($ldap->count() !=0){
653 $message[]= _("There is already an object with this cn.");
654 }
656 if ($this->orig_dn == "new" && !$this->acl_is_createable()){
657 $message[]= _("You have no permissions to create a group on this 'Base'.");
658 }
660 /* must: cn */
661 if ($this->cn == "" && $this->acl_is_writeable("cn")){
662 $message[]= "The required field 'Name' is not set.";
663 }
665 /* To many different object types? */
666 if (strlen($this->gosaGroupObjects) > 4){
667 $message[]= _("You can combine two different object types at maximum only!");
668 }
670 return ($message);
671 }
674 /* Save to LDAP */
675 function save()
676 {
677 plugin::save();
679 /* Move members to target array */
680 $this->attrs['member'] =array();
681 foreach ($this->member as $key => $desc){
682 $this->attrs['member'][]= $key;
683 }
685 $ldap= $this->config->get_ldap_link();
687 /* New accounts need proper 'dn', propagate it to remaining objects */
688 if ($this->dn == 'new'){
689 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
690 }
692 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
693 new entries. So do a check first... */
694 $ldap->cat ($this->dn, array('dn'));
695 if ($ldap->fetch()){
696 /* Modify needs array() to remove values :-( */
697 if (!count ($this->member)){
698 $this->attrs['member']= array();
699 }
700 $mode= "modify";
701 } else {
702 $mode= "add";
703 $ldap->cd($this->config->current['BASE']);
704 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
705 }
707 /* Write back to ldap */
708 $ldap->cd($this->dn);
709 $this->cleanup();
710 $ldap->$mode($this->attrs);
712 /* Trigger post signal */
713 $this->handle_post_events($mode);
715 $ret= 0;
716 if (show_ldap_error($ldap->get_error(), sprintf(_("Saving of object group/generic with dn '%s' failed."),$this->dn))){
717 $ret= 1;
718 }
720 return ($ret);
721 }
723 function remove_from_parent()
724 {
725 plugin::remove_from_parent();
727 $ldap= $this->config->get_ldap_link();
728 $ldap->rmdir($this->dn);
729 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object group/generic with dn '%s' failed."),$this->dn));
731 /* Trigger remove signal */
732 $this->handle_post_events("remove");
733 }
735 function getCopyDialog()
736 {
737 $str = "";
738 $str .= _("Group name");
739 $str .= " <input type='text' name='cn' value='".$this->cn."'>";
740 return($str);
741 }
743 function saveCopyDialog()
744 {
745 if(isset($_POST['cn'])){
746 $this->cn = $_POST['cn'];
747 }
748 }
751 function plInfo()
752 {
753 return (array(
754 "plShortName" => _("Generic"),
755 "plDescription" => _("Object group generic"),
756 "plSelfModify" => FALSE,
757 "plDepends" => array(),
758 "plPriority" => 0,
759 "plSection" => array("administration"),
760 "plCategory" => array("ogroups" => array("description" => _("Object groups"),
761 "objectClass" => "gosaGroupOfNames")),
762 "plProvidedAcls"=> array(
763 "cn" => _("Name"),
764 "description" => _("Description"),
765 "member" => _("Member"))
766 ));
767 }
768 }
770 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
771 ?>