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 }
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,$this);
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 }
317 return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
318 }
321 /* Save data to object */
322 function save_object()
323 {
324 /* Save additional values for possible next step */
325 if (isset($_POST['ogroupedit'])){
326 plugin::save_object();
327 }
328 }
331 /* (Re-)Load objects */
332 function reload()
333 {
334 /*###########
335 Variable initialisation
336 ###########*/
338 $this->objects = array();
339 $this->ui = get_userinfo();
340 $filter = "";
341 $objectClasses = array();
343 $ogfilter = get_global("ogfilter");
344 $regex = $ogfilter['regex'];
346 /* Get ldap connection */
347 $ldap= $this->config->get_ldap_link();
348 $ldap->cd ($ogfilter['dselect']);
351 /*###########
352 Generate Filter
353 ###########*/
355 /* Assemble filter */
356 if ($ogfilter['accounts'] == "checked"){
357 $filter.= "(objectClass=gosaAccount)";
358 $objectClasses["gosaAccount"] = get_people_ou();
359 }
360 if ($ogfilter['groups'] == "checked"){
361 $filter.= "(objectClass=posixGroup)";
362 $objectClasses["posixGroup"] = get_groups_ou();
363 }
364 if ($ogfilter['applications'] == "checked"){
365 $filter.= "(objectClass=gosaApplication)";
366 $objectClasses["gosaApplication"] = "ou=apps,";
367 }
368 if ($ogfilter['departments'] == "checked"){
369 $filter.= "(objectClass=gosaDepartment)";
370 $objectClasses["gosaDepartment"] = "";
371 }
372 if ($ogfilter['servers'] == "checked"){
373 $filter.= "(objectClass=goServer)";
374 $objectClasses["goServer"] = "ou=servers,ou=systems,";
375 }
376 if ($ogfilter['workstations'] == "checked"){
377 $filter.= "(objectClass=gotoWorkstation)";
378 $objectClasses["gotoWorkstation"] = "ou=workstations,ou=systems,";
379 }
380 if ($ogfilter['terminals'] == "checked"){
381 $filter.= "(objectClass=gotoTerminal)";
382 $objectClasses["gotoTerminal"] = "ou=terminals,ou=systems,";
383 }
384 if ($ogfilter['printers'] == "checked"){
385 $filter.= "(objectClass=gotoPrinter)";
387 $objectClasses["gotoPrinter"] = "ou=printers,ou=systems,";
388 }
389 if ($ogfilter['phones'] == "checked"){
390 $filter.= "(objectClass=goFonHardware)";
391 $objectClasses["goFonHardware"] = "ou=phones,ou=systems,";
392 }
395 /*###########
396 Perform search for selected objectClasses & regex to fill list with objects
397 ###########*/
399 /* Perform search for selected objectClasses */
400 foreach($objectClasses as $class=> $basedn){
401 $ldap->ls("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",$basedn.$ogfilter['dselect'] ,
402 array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
404 /* fetch results and append them to the list */
405 while($attrs = $ldap->fetch()){
407 $type= $this->getObjectType($attrs);
408 $name= $this->getObjectName($attrs);
410 /* Fill array */
411 if (isset($attrs["description"][0])){
412 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
413 } elseif (isset($attrs["uid"][0])) {
414 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
415 } else {
416 $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
417 }
418 }
419 }
420 uasort ($this->objects, 'sort_list');
421 reset ($this->objects);
423 /*###########
424 Get a list with all possible objects, to detect objects which doesn't exists anymore ...
425 ###########*/
427 /* Only do this, if this wasn't already done */
428 if(count($this->allobjects) == 0){
429 $ldap->cd ($this->config->current['BASE']);
430 $filter="(objectClass=gosaAccount)".
431 "(objectClass=posixGroup)".
432 "(objectClass=gosaApplication)".
433 "(objectClass=gosaDepartment)".
434 "(objectClass=goServer)".
435 "(objectClass=gotoWorkstation)".
436 "(objectClass=gotoTerminal)".
437 "(objectClass=gotoPrinter)".
438 "(objectClass=goFonHardware)";
439 $regex= "*";
441 $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
442 while ($attrs= $ldap->fetch()){
444 $type= $this->getObjectType($attrs);
445 $name= $this->getObjectName($attrs);
447 if (isset($attrs["description"][0])){
448 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
449 } elseif (isset($attrs["uid"][0])) {
450 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
451 } else {
452 $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
453 }
454 $this->allobjects[$attrs["dn"]]['objectClass'] = $attrs['objectClass'];
455 if(isset($attrs['uid'])){
456 $this->allobjects[$attrs["dn"]]['uid'] = $attrs['uid'];
457 }
458 }
459 uasort ($this->allobjects, 'sort_list');
460 reset ($this->allobjects);
461 }
464 /*###########
465 Build member list and try to detect obsolete entries
466 ###########*/
468 $this->memberList = array();
470 /* Walk through all single member entry */
471 foreach($this->member as $dn){
473 /* Object in object list? */
474 if (isset($this->allobjects[$dn])){
476 /* Add this entry to member list, its dn is in allobjects
477 this means it still exists
478 */
479 $this->memberList[$dn]= $this->allobjects[$dn];
481 /* Remove this from selectable entries */
482 if (isset ($this->objects[$dn])){
483 unset ($this->objects[$dn]);
484 }
487 } else {
489 /* The dn for the current member can't be resolved
490 it seams that this entry was removed
491 */
492 /* Try to resolv the entry again, if it still fails, display error msg */
493 $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
495 /* It has failed, add entry with type flag I (Invalid)*/
496 if ($ldap->error != "success"){
497 $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
499 } else {
501 /* Append this entry to our all object list */
503 /* Fetch object */
504 $attrs= $ldap->fetch();
506 $type= $this->getObjectType($attrs);
507 $name= $this->getObjectName($attrs);
509 if (isset($attrs["description"][0])){
510 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
511 } elseif (isset($attrs["uid"][0])) {
512 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
513 } else {
514 $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
515 }
516 $this->allobjects[$attrs["dn"]]['objectClass'] = $attrs['objectClass'];
517 if(isset($attrs['uid'])){
518 $this->allobjects[$attrs["dn"]]['uid'] = $attrs['uid'];
519 }
521 /* Fill array */
522 if (isset($attrs["description"][0])){
523 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
524 } else {
525 $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
526 }
527 }
528 }
529 }
530 uasort ($this->memberList, 'sort_list');
531 reset ($this->memberList);
533 /* Assemble types of currently combined objects */
534 $objectTypes= "";
535 foreach ($this->memberList as $dn => $desc){
537 /* Invalid object? */
538 if ($desc['type'] == 'I'){
539 continue;
540 }
542 /* Fine. Add to list. */
543 if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
544 $objectTypes.= $desc['type'];
545 }
546 }
547 $this->gosaGroupObjects= "[$objectTypes]";
548 }
551 function convert_list($input)
552 {
553 $temp= "";
554 $conv= array( "U" => "select_user.png",
555 "G" => "select_groups.png",
556 "A" => "select_application.png",
557 "D" => "select_department.png",
558 "S" => "select_server.png",
559 "W" => "select_workstation.png",
560 "T" => "select_terminal.png",
561 "F" => "select_phone.png",
562 "I" => "flag.png",
563 "P" => "select_printer.png");
565 foreach ($input as $key => $value){
566 /* Generate output */
567 $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
568 }
570 return ($temp);
571 }
574 function getObjectType($attrs)
575 {
576 $type= "I";
578 foreach(array( "U" => "gosaAccount",
579 "G" => "posixGroup",
580 "A" => "gosaApplication",
581 "D" => "gosaDepartment",
582 "S" => "goServer",
583 "W" => "gotoWorkstation",
584 "T" => "gotoTerminal",
585 "F" => "goFonHardware",
586 "P" => "gotoPrinter") as $index => $class){
587 if (in_array($class, $attrs['objectClass'])){
588 $type= $index;
589 break;
590 }
591 }
593 return ($type);
594 }
597 function getObjectName($attrs)
598 {
599 /* Person? */
600 $name ="";
601 if (in_array('gosaAccount', $attrs['objectClass'])){
602 if(isset($attrs['sn']) && isset($attrs['givenName'])){
603 $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
604 } else {
605 $name= $attrs['uid'][0];
606 }
607 } else {
608 if(isset($attrs["cn"][0])) {
609 $name= $attrs['cn'][0];
610 } else {
611 $name= $attrs['ou'][0];
612 }
613 }
615 return ($name);
616 }
619 function check()
620 {
621 /* Call common method to give check the hook */
622 $message= plugin::check();
624 /* Permissions for that base? */
625 if ($this->base != ""){
626 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
627 } else {
628 $new_dn= $this->dn;
629 }
632 $ldap = $this->config->get_ldap_link();
633 if($this->dn != $new_dn){
634 $ldap->cat ($new_dn, array('dn'));
635 }
637 if($ldap->count() !=0){
638 $message[]= _("There is already an object with this cn.");
639 }
641 if ($this->orig_dn == "new" && !$this->acl_is_createable()){
642 $message[]= _("You have no permissions to create a group on this 'Base'.");
643 }
645 /* must: cn */
646 if ($this->cn == "" && $this->acl_is_writeable("cn")){
647 $message[]= "The required field 'Name' is not set.";
648 }
650 /* To many different object types? */
651 if (strlen($this->gosaGroupObjects) > 4){
652 $message[]= _("You can combine two different object types at maximum only!");
653 }
655 return ($message);
656 }
659 /* Save to LDAP */
660 function save()
661 {
662 plugin::save();
664 /* Move members to target array */
665 $this->attrs['member'] =array();
666 foreach ($this->member as $key => $desc){
667 $this->attrs['member'][]= $key;
668 }
670 $ldap= $this->config->get_ldap_link();
672 /* New accounts need proper 'dn', propagate it to remaining objects */
673 if ($this->dn == 'new'){
674 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
675 }
677 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
678 new entries. So do a check first... */
679 $ldap->cat ($this->dn, array('dn'));
680 if ($ldap->fetch()){
681 /* Modify needs array() to remove values :-( */
682 if (!count ($this->member)){
683 $this->attrs['member']= array();
684 }
685 $mode= "modify";
686 } else {
687 $mode= "add";
688 $ldap->cd($this->config->current['BASE']);
689 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
690 }
692 /* Write back to ldap */
693 $ldap->cd($this->dn);
694 $this->cleanup();
695 $ldap->$mode($this->attrs);
697 /* Trigger post signal */
698 $this->handle_post_events($mode);
700 $ret= 0;
701 if (show_ldap_error($ldap->get_error(), sprintf(_("Saving of object group/generic with dn '%s' failed."),$this->dn))){
702 $ret= 1;
703 }
705 return ($ret);
706 }
708 function remove_from_parent()
709 {
710 plugin::remove_from_parent();
712 $ldap= $this->config->get_ldap_link();
713 $ldap->rmdir($this->dn);
714 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object group/generic with dn '%s' failed."),$this->dn));
716 /* Trigger remove signal */
717 $this->handle_post_events("remove");
718 }
720 function getCopyDialog()
721 {
722 $str = "";
723 $str .= _("Group name");
724 $str .= " <input type='text' name='cn' value='".$this->cn."'>";
725 return($str);
726 }
728 function saveCopyDialog()
729 {
730 if(isset($_POST['cn'])){
731 $this->cn = $_POST['cn'];
732 }
733 }
736 function plInfo()
737 {
738 return (array(
739 "plShortName" => _("Generic"),
740 "plDescription" => _("Object group generic"),
741 "plSelfModify" => FALSE,
742 "plDepends" => array(),
743 "plPriority" => 0,
744 "plSection" => array("administration"),
745 "plCategory" => array("ogroups" => array("description" => _("Object groups"),
746 "objectClass" => "gosaGroupOfNames")),
747 "plProvidedAcls"=> array(
748 "cn" => _("Name"),
749 "base" => _("Base"),
750 "description" => _("Description"),
751 "member" => _("Member"))
752 ));
753 }
754 }
756 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
757 ?>