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 $value= @LDAP::convert($value);
50 $this->member["$value"]= "$value";
51 }
52 }
53 }
54 $this->is_account= TRUE;
56 /* Get global filter config */
57 if (!is_global("ogfilter")){
58 $ui= get_userinfo();
59 $base= get_base_from_people($ui->dn);
60 $ogfilter= array( "dselect" => $base,
61 "regex" => "*");
62 register_global("ogfilter", $ogfilter);
63 }
64 $ogfilter= get_global('ogfilter');
66 /* Adjust flags */
67 foreach( array( "U" => "accounts",
68 "G" => "groups",
69 "A" => "applications",
70 "D" => "departments",
71 "S" => "servers",
72 "W" => "workstations",
73 "T" => "terminals",
74 "F" => "phones",
75 "_" => "subtrees",
76 "P" => "printers") as $key => $val){
78 if (preg_match("/$key/", $this->gosaGroupObjects)){
79 $ogfilter[$val]= "checked";
80 } else {
81 $ogfilter[$val]= "";
82 }
83 }
84 register_global("ogfilter", $ogfilter);
86 if(isset($_SESSION['CurrentMainBase'])){
87 $this->base = $_SESSION['CurrentMainBase'];
88 }
90 /* Set base */
91 if ($this->dn == "new"){
92 $this->base= $_SESSION['CurrentMainBase'];
93 } else {
94 $this->base= preg_replace("/^[^,]+,".get_groups_ou()."/","",$this->dn);
95 }
97 if($this->is_account && $dn != "new"){
98 @log::log("view","ogroup/".get_class($this),$this->dn);
99 }
101 /* Load member data */
102 $this->reload();
103 }
105 function AddDelMembership($NewMember = false){
107 if($NewMember){
109 /* Add member and force reload */
110 $this->member[$NewMember]= $NewMember;
111 $this->reload();
113 $this->memberList[$NewMember]= $this->objcache[$NewMember];
114 unset ($this->objects[$NewMember]);
115 uasort ($this->memberList, 'sort_list');
116 reset ($this->memberList);
117 }else{
118 /* Delete objects from group */
119 if (isset($_POST['delete_membership']) && isset($_POST['members'])){
120 foreach ($_POST['members'] as $value){
121 $this->objects["$value"]= $this->memberList[$value];
122 unset ($this->memberList["$value"]);
123 unset ($this->member["$value"]);
124 uasort ($this->objects, 'sort_list');
125 reset ($this->objects);
126 }
127 $this->reload();
128 }
130 /* Add objects to group */
131 if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
132 foreach ($_POST['objects'] as $value){
133 $this->memberList["$value"]= $this->objects[$value];
134 $this->member["$value"]= $value;
135 unset ($this->objects[$value]);
136 uasort ($this->memberList, 'sort_list');
137 reset ($this->memberList);
138 }
139 $this->reload();
140 }
141 }
142 }
144 function execute()
145 {
146 /* Call parent execute */
147 plugin::execute();
149 // $this->reload();
151 /* Do we represent a valid group? */
152 if (!$this->is_account){
153 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
154 _("This 'dn' is no object group.")."</b>";
155 return ($display);
156 }
158 /* Delete objects from group */
159 if (isset($_POST['delete_membership']) && isset($_POST['members'])){
160 foreach ($_POST['members'] as $value){
161 if(isset($this->memberList[$value])){
162 $this->objects["$value"]= $this->memberList[$value];
163 unset ($this->memberList["$value"]);
164 unset ($this->member["$value"]);
165 uasort ($this->objects, 'sort_list');
166 reset ($this->objects);
167 }
168 }
169 $this->reload();
170 }
172 /* Add objects to group */
173 if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
174 foreach ($_POST['objects'] as $value){
175 if(isset($this->objects[$value])){
176 $this->memberList["$value"]= $this->objects[$value];
177 $this->member["$value"]= $value;
178 unset ($this->objects[$value]);
179 uasort ($this->memberList, 'sort_list');
180 reset ($this->memberList);
181 }
182 }
183 $this->reload();
184 }
186 /* Load templating engine */
187 $smarty= get_smarty();
189 $tmp = $this->plInfo();
190 foreach($tmp['plProvidedAcls'] as $name => $translation){
191 $smarty->assign($name."ACL",$this->getacl($name));
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,$this->get_allowed_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()){
213 /* A new base was selected, check if it is a valid one */
214 $tmp = $this->get_allowed_bases();
215 if(isset($tmp[$this->dialog->isSelected()])){
216 $this->base = $this->dialog->isSelected();
217 }
218 $this->dialog= false;
219 }else{
220 return($this->dialog->execute());
221 }
222 }
224 /* Add objects? */
225 if (isset($_POST["edit_membership"])){
226 $this->group_dialog= TRUE;
227 $this->dialog= TRUE;
228 }
230 /* Add objects finished? */
231 if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
232 $this->group_dialog= FALSE;
233 $this->dialog= FALSE;
234 }
236 /* Manage object add dialog */
237 if ($this->group_dialog){
239 /* Save data */
240 $ogfilter= get_global("ogfilter");
241 foreach( array("dselect", "regex") as $type){
242 if (isset($_POST[$type])){
243 $ogfilter[$type]= $_POST[$type];
244 }
245 }
246 if (isset($_POST['dselect'])){
247 foreach( array("accounts", "groups", "applications", "departments",
248 "servers", "workstations", "terminals", "printers","subtrees",
249 "phones") as $type){
251 if (isset($_POST[$type])) {
252 $ogfilter[$type]= "checked";
253 } else {
254 $ogfilter[$type]= "";
255 }
256 }
257 }
258 if (isset($_GET['search'])){
259 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
260 if ($s == "**"){
261 $s= "*";
262 }
263 $ogfilter['regex']= $s;
264 }
265 register_global("ogfilter", $ogfilter);
266 $this->reload();
268 /* Calculate actual groups */
269 $smarty->assign("objects", $this->convert_list($this->objects));
271 /* Show dialog */
272 $smarty->assign("search_image", get_template_path('images/search.png'));
273 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
274 $smarty->assign("tree_image", get_template_path('images/tree.png'));
275 $smarty->assign("deplist", $this->config->idepartments);
276 $smarty->assign("alphabet", generate_alphabet());
277 foreach( array("dselect", "regex", "accounts", "groups", "applications",
278 "departments", "servers", "workstations", "terminals","subtrees",
279 "printers", "phones") as $type){
280 $smarty->assign("$type", $ogfilter[$type]);
281 }
282 $smarty->assign("hint", print_sizelimit_warning());
283 $smarty->assign("apply", apply_filter());
285 $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
286 return ($display);
287 }
289 /* Bases / Departments */
290 if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
291 $this->base= $_POST['base'];
292 }
294 /* Assemble combine string */
295 if ($this->gosaGroupObjects == "[]"){
296 $smarty->assign("combinedObjects", _("none"));
297 } elseif (strlen($this->gosaGroupObjects) > 4){
298 $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
299 } else {
300 $conv= array( "U" => _("users"),
301 "G" => _("groups"),
302 "A" => _("applications"),
303 "D" => _("departments"),
304 "S" => _("servers"),
305 "W" => _("workstations"),
306 "T" => _("terminals"),
307 "F" => _("phones"),
308 "P" => _("printers"));
310 $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
311 $p1= $conv[$type[0]];
312 error_reporting(0);
313 if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
314 $p2= $conv[$type[1]];
315 $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
316 } else {
317 $smarty->assign("combinedObjects", "$p1");
318 }
319 error_reporting(E_ALL);
320 }
322 /* Assign variables */
323 $smarty->assign("bases", $this->get_allowed_bases());
324 $smarty->assign("base_select", $this->base);
325 $smarty->assign("department", $this->department);
326 $smarty->assign("members", $this->convert_list($this->memberList));
328 /* Objects have to be tuned... */
329 $smarty->assign("objects", $this->convert_list($this->objects));
331 /* Fields */
332 foreach ($this->attributes as $val){
333 $smarty->assign("$val", $this->$val);
334 }
336 return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
337 }
340 /* Save data to object */
341 function save_object()
342 {
343 /* Save additional values for possible next step */
344 if (isset($_POST['ogroupedit'])){
346 /* Create a base backup and reset the
347 base directly after calling plugin::save_object();
348 Base will be set seperatly a few lines below */
349 $base_tmp = $this->base;
350 plugin::save_object();
351 $this->base = $base_tmp;
353 /* Save base, since this is no LDAP attribute */
354 $tmp = $this->get_allowed_bases();
355 if(isset($_POST['base'])){
356 if(isset($tmp[$_POST['base']])){
357 $this->base= $_POST['base'];
358 }
359 }
360 }
361 }
364 /* (Re-)Load objects */
365 function reload()
366 {
367 /*###########
368 Variable initialisation
369 ###########*/
371 $this->objects = array();
372 $this->ui = get_userinfo();
373 $filter = "";
374 $objectClasses = array();
376 $ogfilter = get_global("ogfilter");
377 $regex = $ogfilter['regex'];
379 $ldap= $this->config->get_ldap_link();
380 $ldap->cd ($ogfilter['dselect']);
383 /*###########
384 Generate Filter
385 ###########*/
387 $p_f= array("accounts" => array("CLASS"=>"gosaAccount" ,"DN"=> get_people_ou() ,"ACL" => "users"),
388 "groups" => array("CLASS"=>"posixGroup" ,"DN"=> get_groups_ou() ,"ACL" => "groups"),
389 "applications" => array("CLASS"=>"gosaApplication","DN"=> "ou=apps," ,"ACL" => "application"),
390 "departments" => array("CLASS"=>"gosaDepartment" ,"DN"=> "" ,"ACL" => "department"),
391 "servers" => array("CLASS"=>"goServer" ,"DN"=> "ou=servers,ou=systems," ,"ACL" => "server"),
392 "workstations" => array("CLASS"=>"gotoWorkstation","DN"=> "ou=workstations,ou=systems," ,"ACL" => "workstation"),
393 "terminals" => array("CLASS"=>"gotoTerminal" ,"DN"=> "ou=terminals,ou=systems," ,"ACL" => "terminal"),
394 "printers" => array("CLASS"=>"gotoPrinter" ,"DN"=> "ou=printers,ou=systems," ,"ACL" => "printer"),
395 "phones" => array("CLASS"=>"goFonHardware" ,"DN"=> "ou=phones,ou=systems," ,"ACL" => "phone"));
398 /*###########
399 Perform search for selected objectClasses & regex to fill list with objects
400 ###########*/
402 $Get_list_flags = 0;
403 if($ogfilter['subtrees'] == "checked"){
404 $Get_list_flags |= GL_SUBSEARCH;
405 }
407 foreach($p_f as $post_name => $data){
409 if($ogfilter[$post_name] == "checked"){
410 $filter = "(&(objectClass=".$data['CLASS'].")(|(uid=$regex)(cn=$regex)(ou=$regex)))";
411 $res = get_list($filter, $data['ACL'] , $data['DN'].$ogfilter['dselect'],
412 array("description", "objectClass", "sn", "givenName", "uid","ou","cn"));
414 /* fetch results and append them to the list */
415 foreach($res as $attrs){
417 $type= $this->getObjectType($attrs);
418 $name= $this->getObjectName($attrs);
420 /* Fill array */
421 if (isset($attrs["description"][0])){
422 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
423 } elseif (isset($attrs["uid"][0])) {
424 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
425 } else {
426 $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
427 }
428 }
429 }
430 }
431 uasort ($this->objects, 'sort_list');
432 reset ($this->objects);
435 /*###########
436 Build member list and try to detect obsolete entries
437 ###########*/
439 $this->memberList = array();
441 /* Walk through all single member entry */
442 foreach($this->member as $dn){
444 /* The dn for the current member can't be resolved
445 it seams that this entry was removed
446 */
447 /* Try to resolv the entry again, if it still fails, display error msg */
448 $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
450 /* It has failed, add entry with type flag I (Invalid)*/
451 if ($ldap->error != "Success"){
452 $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
454 } else {
456 /* Append this entry to our all object list */
458 /* Fetch object */
459 $attrs= $ldap->fetch();
461 $type= $this->getObjectType($attrs);
462 $name= $this->getObjectName($attrs);
464 if (isset($attrs["description"][0])){
465 $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
466 } elseif (isset($attrs["uid"][0])) {
467 $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
468 } else {
469 $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
470 }
471 $this->objcache[$attrs["dn"]]['objectClass'] = $attrs['objectClass'];
472 if(isset($attrs['uid'])){
473 $this->objcache[$attrs["dn"]]['uid'] = $attrs['uid'];
474 }
476 /* Fill array */
477 if (isset($attrs["description"][0])){
478 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
479 } else {
480 $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
481 }
483 $this->memberList[$dn]= $this->objects[$attrs["dn"]];
484 }
485 }
486 uasort ($this->memberList, 'sort_list');
487 reset ($this->memberList);
489 /* Assemble types of currently combined objects */
490 $objectTypes= "";
491 foreach ($this->memberList as $dn => $desc){
493 /* Invalid object? */
494 if ($desc['type'] == 'I'){
495 continue;
496 }
498 /* Fine. Add to list. */
499 if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
500 $objectTypes.= $desc['type'];
501 }
502 }
503 $this->gosaGroupObjects= "[$objectTypes]";
504 }
507 function convert_list($input)
508 {
509 $temp= "";
510 $conv= array( "U" => "select_user.png",
511 "G" => "select_groups.png",
512 "A" => "select_application.png",
513 "D" => "select_department.png",
514 "S" => "select_server.png",
515 "W" => "select_workstation.png",
516 "T" => "select_terminal.png",
517 "F" => "select_phone.png",
518 "I" => "flag.png",
519 "P" => "select_printer.png");
521 foreach ($input as $key => $value){
522 /* Generate output */
523 $temp.= "<option title='".addslashes( $key)."' value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
524 }
526 return ($temp);
527 }
530 function getObjectType($attrs)
531 {
532 $type= "I";
534 foreach(array( "U" => "gosaAccount",
535 "G" => "posixGroup",
536 "A" => "gosaApplication",
537 "D" => "gosaDepartment",
538 "S" => "goServer",
539 "W" => "gotoWorkstation",
540 "T" => "gotoTerminal",
541 "F" => "goFonHardware",
542 "P" => "gotoPrinter") as $index => $class){
543 if (in_array($class, $attrs['objectClass'])){
544 $type= $index;
545 break;
546 }
547 }
549 return ($type);
550 }
553 function getObjectName($attrs)
554 {
555 /* Person? */
556 $name ="";
557 if (in_array('gosaAccount', $attrs['objectClass'])){
558 if(isset($attrs['sn']) && isset($attrs['givenName'])){
559 $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
560 } else {
561 $name= $attrs['uid'][0];
562 }
563 } else {
564 if(isset($attrs["cn"][0])) {
565 $name= $attrs['cn'][0];
566 } else {
567 $name= $attrs['ou'][0];
568 }
569 }
571 return ($name);
572 }
575 function check()
576 {
577 /* Call common method to give check the hook */
578 $message= plugin::check();
580 /* Permissions for that base? */
581 if ($this->base != ""){
582 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
583 } else {
584 $new_dn= $this->dn;
585 }
588 $ldap = $this->config->get_ldap_link();
589 if($this->dn != $new_dn){
590 $ldap->cat ($new_dn, array('dn'));
591 }
593 if($ldap->count() !=0){
594 $message[]= _("There is already an object with this cn.");
595 }
597 /* Set new acl base */
598 if($this->dn == "new") {
599 $this->set_acl_base($this->base);
600 }
602 /* must: cn */
603 if ($this->cn == ""){
604 $message[]= "The required field 'Name' is not set.";
605 }
607 /* To many different object types? */
608 if (strlen($this->gosaGroupObjects) > 4){
609 $message[]= _("You can combine two different object types at maximum only!");
610 }
612 return ($message);
613 }
616 /* Save to LDAP */
617 function save()
618 {
619 plugin::save();
621 /* Move members to target array */
622 $this->attrs['member'] =array();
623 foreach ($this->member as $key => $desc){
624 $this->attrs['member'][]= @LDAP::fix($key);
625 }
627 $ldap= $this->config->get_ldap_link();
629 /* New accounts need proper 'dn', propagate it to remaining objects */
630 if ($this->dn == 'new'){
631 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
632 }
634 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
635 new entries. So do a check first... */
636 $ldap->cat ($this->dn, array('dn'));
637 if ($ldap->fetch()){
638 /* Modify needs array() to remove values :-( */
639 if (!count ($this->member)){
640 $this->attrs['member']= array();
641 }
642 $mode= "modify";
644 } else {
645 $mode= "add";
646 $ldap->cd($this->config->current['BASE']);
647 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
648 }
650 /* Write back to ldap */
651 $ldap->cd($this->dn);
652 $this->cleanup();
653 $ldap->$mode($this->attrs);
655 if($mode == "add"){
656 @log::log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
657 }else{
658 @log::log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
659 }
661 /* Trigger post signal */
662 $this->handle_post_events($mode);
664 $ret= 0;
665 if (show_ldap_error($ldap->get_error(), sprintf(_("Saving of object group/generic with dn '%s' failed."),$this->dn))){
666 $ret= 1;
667 }
669 return ($ret);
670 }
672 function remove_from_parent()
673 {
674 plugin::remove_from_parent();
676 $ldap= $this->config->get_ldap_link();
677 $ldap->rmdir($this->dn);
678 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object group/generic with dn '%s' failed."),$this->dn));
680 @log::log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
682 /* Trigger remove signal */
683 $this->handle_post_events("remove");
684 }
687 function getCopyDialog()
688 {
689 $smarty = get_smarty();
690 $smarty->assign("cn", $this->cn);
691 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
692 $ret = array();
693 $ret['string'] = $str;
694 $ret['status'] = "";
695 return($ret);
696 }
698 function saveCopyDialog()
699 {
700 if(isset($_POST['cn'])){
701 $this->cn = $_POST['cn'];
702 }
703 }
706 function plInfo()
707 {
708 return (array(
709 "plShortName" => _("Generic"),
710 "plDescription" => _("Object group generic"),
711 "plSelfModify" => FALSE,
712 "plDepends" => array(),
713 "plPriority" => 1,
714 "plSection" => array("administration"),
715 "plCategory" => array("ogroups" => array("description" => _("Object groups"),
716 "objectClass" => "gosaGroupOfNames")),
717 "plProvidedAcls"=> array(
718 "cn" => _("Name"),
719 "base" => _("Base"),
720 "description" => _("Description"),
721 "member" => _("Member"))
722 ));
723 }
724 }
726 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
727 ?>