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 "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->objcache[$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);
439 /*###########
440 Build member list and try to detect obsolete entries
441 ###########*/
443 $this->memberList = array();
445 /* Walk through all single member entry */
446 foreach($this->member as $dn){
448 /* The dn for the current member can't be resolved
449 it seams that this entry was removed
450 */
451 /* Try to resolv the entry again, if it still fails, display error msg */
452 $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
454 /* It has failed, add entry with type flag I (Invalid)*/
455 if ($ldap->error != "Success"){
456 $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
458 } else {
460 /* Append this entry to our all object list */
462 /* Fetch object */
463 $attrs= $ldap->fetch();
465 $type= $this->getObjectType($attrs);
466 $name= $this->getObjectName($attrs);
468 if (isset($attrs["description"][0])){
469 $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
470 } elseif (isset($attrs["uid"][0])) {
471 $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
472 } else {
473 $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
474 }
475 $this->objcache[$attrs["dn"]]['objectClass'] = $attrs['objectClass'];
476 if(isset($attrs['uid'])){
477 $this->objcache[$attrs["dn"]]['uid'] = $attrs['uid'];
478 }
480 /* Fill array */
481 if (isset($attrs["description"][0])){
482 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
483 } else {
484 $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
485 }
487 $this->memberList[$dn]= $this->objects[$attrs["dn"]];
488 }
489 }
490 uasort ($this->memberList, 'sort_list');
491 reset ($this->memberList);
493 /* Assemble types of currently combined objects */
494 $objectTypes= "";
495 foreach ($this->memberList as $dn => $desc){
497 /* Invalid object? */
498 if ($desc['type'] == 'I'){
499 continue;
500 }
502 /* Fine. Add to list. */
503 if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
504 $objectTypes.= $desc['type'];
505 }
506 }
507 $this->gosaGroupObjects= "[$objectTypes]";
508 }
511 function convert_list($input)
512 {
513 $temp= "";
514 $conv= array( "U" => "select_user.png",
515 "G" => "select_groups.png",
516 "A" => "select_application.png",
517 "D" => "select_department.png",
518 "S" => "select_server.png",
519 "W" => "select_workstation.png",
520 "T" => "select_terminal.png",
521 "F" => "select_phone.png",
522 "I" => "flag.png",
523 "P" => "select_printer.png");
525 foreach ($input as $key => $value){
526 /* Generate output */
527 $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
528 }
530 return ($temp);
531 }
534 function getObjectType($attrs)
535 {
536 $type= "I";
538 foreach(array( "U" => "gosaAccount",
539 "G" => "posixGroup",
540 "A" => "gosaApplication",
541 "D" => "gosaDepartment",
542 "S" => "goServer",
543 "W" => "gotoWorkstation",
544 "T" => "gotoTerminal",
545 "F" => "goFonHardware",
546 "P" => "gotoPrinter") as $index => $class){
547 if (in_array($class, $attrs['objectClass'])){
548 $type= $index;
549 break;
550 }
551 }
553 return ($type);
554 }
557 function getObjectName($attrs)
558 {
559 /* Person? */
560 $name ="";
561 if (in_array('gosaAccount', $attrs['objectClass'])){
562 if(isset($attrs['sn']) && isset($attrs['givenName'])){
563 $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
564 } else {
565 $name= $attrs['uid'][0];
566 }
567 } else {
568 if(isset($attrs["cn"][0])) {
569 $name= $attrs['cn'][0];
570 } else {
571 $name= $attrs['ou'][0];
572 }
573 }
575 return ($name);
576 }
579 function check()
580 {
581 /* Call common method to give check the hook */
582 $message= plugin::check();
584 /* Permissions for that base? */
585 if ($this->base != ""){
586 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
587 } else {
588 $new_dn= $this->dn;
589 }
592 $ldap = $this->config->get_ldap_link();
593 if($this->dn != $new_dn){
594 $ldap->cat ($new_dn, array('dn'));
595 }
597 if($ldap->count() !=0){
598 $message[]= _("There is already an object with this cn.");
599 }
601 if ($this->orig_dn == "new" && !$this->acl_is_createable()){
602 $message[]= _("You have no permissions to create a group on this 'Base'.");
603 }
605 /* must: cn */
606 if ($this->cn == "" && $this->acl_is_writeable("cn")){
607 $message[]= "The required field 'Name' is not set.";
608 }
610 /* To many different object types? */
611 if (strlen($this->gosaGroupObjects) > 4){
612 $message[]= _("You can combine two different object types at maximum only!");
613 }
615 return ($message);
616 }
619 /* Save to LDAP */
620 function save()
621 {
622 plugin::save();
624 /* Move members to target array */
625 $this->attrs['member'] =array();
626 foreach ($this->member as $key => $desc){
627 $this->attrs['member'][]= @LDAP::fix($key);
628 }
630 $ldap= $this->config->get_ldap_link();
632 /* New accounts need proper 'dn', propagate it to remaining objects */
633 if ($this->dn == 'new'){
634 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
635 }
637 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
638 new entries. So do a check first... */
639 $ldap->cat ($this->dn, array('dn'));
640 if ($ldap->fetch()){
641 /* Modify needs array() to remove values :-( */
642 if (!count ($this->member)){
643 $this->attrs['member']= array();
644 }
645 $mode= "modify";
646 } else {
647 $mode= "add";
648 $ldap->cd($this->config->current['BASE']);
649 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
650 }
652 /* Write back to ldap */
653 $ldap->cd($this->dn);
654 $this->cleanup();
655 $ldap->$mode($this->attrs);
657 /* Trigger post signal */
658 $this->handle_post_events($mode);
660 $ret= 0;
661 if (show_ldap_error($ldap->get_error(), sprintf(_("Saving of object group/generic with dn '%s' failed."),$this->dn))){
662 $ret= 1;
663 }
665 return ($ret);
666 }
668 function remove_from_parent()
669 {
670 plugin::remove_from_parent();
672 $ldap= $this->config->get_ldap_link();
673 $ldap->rmdir($this->dn);
674 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object group/generic with dn '%s' failed."),$this->dn));
676 /* Trigger remove signal */
677 $this->handle_post_events("remove");
678 }
680 function getCopyDialog()
681 {
682 $str = "";
683 $str .= _("Group name");
684 $str .= " <input type='text' name='cn' value='".$this->cn."'>";
685 return($str);
686 }
688 function saveCopyDialog()
689 {
690 if(isset($_POST['cn'])){
691 $this->cn = $_POST['cn'];
692 }
693 }
696 function plInfo()
697 {
698 return (array(
699 "plShortName" => _("Generic"),
700 "plDescription" => _("Object group generic"),
701 "plSelfModify" => FALSE,
702 "plDepends" => array(),
703 "plPriority" => 0,
704 "plSection" => array("administration"),
705 "plCategory" => array("ogroups" => array("description" => _("Object groups"),
706 "objectClass" => "gosaGroupOfNames")),
707 "plProvidedAcls"=> array(
708 "cn" => _("Name"),
709 "base" => _("Base"),
710 "description" => _("Description"),
711 "member" => _("Member"))
712 ));
713 }
714 }
716 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
717 ?>