1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class ogroupManagement extends plugin
24 {
25 /* Definitions */
26 var $plHeadline= "Object groups";
27 var $plDescription= "Manage object groups";
29 /* attribute list for save action */
30 var $attributes= array();
31 var $objectclasses= array();
32 var $obtypes= array();
33 var $ogroup;
35 var $CopyPasteHandler = NULL;
36 var $DivListOGroup = NULL;
37 var $start_pasting_copied_objects = FALSE;
39 function ogroupManagement (&$config, $dn= NULL)
40 {
41 /* Include config object */
42 $this->config= &$config;
43 $this->ui= get_userinfo();
45 /* Copy & Paste enabled ?
46 */
47 if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE'] ))){
48 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
49 }
51 /* Div lsit management */
52 $this->DivListOGroup = new divListOGroup($this->config,$this);
53 }
55 function execute()
56 {
57 /* Call parent execute */
58 plugin::execute();
60 /****************
61 Variable intialisation && Check posts for commands
62 ****************/
64 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^group_edit_/","/^group_del_/","/^item_selected/","/^remove_multiple_ogroups/"));
66 $smarty = get_smarty();
67 $s_action = "";
68 $s_entry = "";
70 /* Test Posts */
71 foreach($_POST as $key => $val){
72 // Post for delete
73 if(preg_match("/^group_del.*/",$key)){
74 $s_action = "del";
75 $s_entry = preg_replace("/^group_".$s_action."_/i","",$key);
76 // Post for edit
77 }elseif(preg_match("/^group_edit_.*/",$key)){
78 $s_action="edit";
79 $s_entry = preg_replace("/^group_".$s_action."_/i","",$key);
80 // Post for new
81 }elseif(preg_match("/^group_new.*/",$key)){
82 $s_action="new";
83 }elseif(preg_match("/^editPaste.*/i",$key)){
84 $s_action="editPaste";
85 }elseif(preg_match("/^copy_.*/",$key)){
86 $s_action="copy";
87 $s_entry = preg_replace("/^copy_/i","",$key);
88 }elseif(preg_match("/^cut_.*/",$key)){
89 $s_action="cut";
90 $s_entry = preg_replace("/^cut_/i","",$key);
91 }elseif(preg_match("/^remove_multiple_ogroups/",$key)){
92 $s_action="del_multiple";
93 }elseif(preg_match("/^multiple_cut_ogroups/",$key)){
94 $s_action = "cut_multiple";
95 }elseif(preg_match("/^multiple_copy_ogroups/",$key)){
96 $s_action = "copy_multiple";
97 }elseif(preg_match("/_group_edit_/",$key)){
98 $type = preg_replace("/_group_edit_.*$/","",$key);
99 $s_action="edit";
100 $s_entry = preg_replace("/".$type."_group_edit_/i","",$key);
101 $_POST['arg'] = $type;
102 }
103 }
104 $s_entry = preg_replace("/_.$/","",$s_entry);
106 // Edit if
107 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
108 $s_action ="edit";
109 $s_entry = $_GET['id'];
110 }
114 /* handle C&P from layers menu */
115 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
116 $s_action = "copy_multiple";
117 }
118 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
119 $s_action = "cut_multiple";
120 }
121 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
122 $s_action = "editPaste";
123 }
125 /* Create options */
126 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "group_new"){
127 $s_action = "new";
128 }
130 /* handle remove from layers menu */
131 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
132 $s_action = "del_multiple";
133 }
135 /* Hanlde notification event requests */
136 if(isset($_POST['menu_action']) && preg_match("/^event/",$_POST['menu_action'])){
137 $s_action = $_POST['menu_action'];
138 }
141 /********************
142 Create notification event
143 ********************/
145 if(preg_match("/^event_/",$s_action) && class_available("DaemonEvent")){
146 $ids = $this->list_get_selected_items();
147 $uids = array();
148 foreach($ids as $id){
149 $uids[] = $this->ogrouplist[$id]['cn'][0];
150 }
151 if(count($uids)){
152 $events = DaemonEvent::get_event_types(USER_EVENT);
153 $event = preg_replace("/^event_/","",$s_action);
154 if(isset($events['BY_CLASS'][$event])){
155 $type = $events['BY_CLASS'][$event];
156 $this->ogroup = new $type['CLASS_NAME']($this->config);
157 $this->ogroup->add_targets($uids);
158 $this->ogroup->set_type(TRIGGERED_EVENT);
159 }
160 }
161 }
163 /* Abort event dialog */
164 if(isset($_POST['abort_event_dialog'])){
165 $this->ogroup = FALSE;
166 }
168 /* Save event */
169 if(isset($_POST['save_event_dialog'])){
170 $o_queue = new gosaSupportDaemon();
171 $o_queue->append($this->ogroup);
172 if($o_queue->is_error()){
173 msg_dialog::display(_("Infrastructure error"), msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
174 }else{
175 $this->ogroup = FALSE;
176 }
177 }
179 /* Display event */
180 if($this->ogroup instanceof DaemonEvent){
181 $this->ogroup->save_object();
182 return($this->ogroup->execute());
183 }
186 /****************
187 Copy & Paste handling
188 ****************/
190 /* Display the copy & paste dialog, if it is currently open */
191 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
192 if($ret){
193 return($ret);
194 }
197 /***************
198 Create a new object group
199 ****************/
201 /* New group? */
202 if ($s_action=="new"){
204 /* By default we set 'dn' to 'new', all relevant plugins will
205 react on this. */
206 $this->dn= "new";
208 /* Create new ogroup- object */
209 $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $this->dn,"ogroups");
210 $this->ogroup->set_acl_base($this->DivListOGroup->selectedBase);
211 }
214 /********************
215 Delete MULTIPLE entries requested, display confirm dialog
216 ********************/
217 if ($s_action=="del_multiple"){
218 $ids = $this->list_get_selected_items();
219 if(count($ids)){
220 foreach($ids as $id){
221 $dn = $this->ogrouplist[$id]['dn'];
222 if (($user= get_lock($dn)) != ""){
223 return(gen_locked_message ($user, $dn));
224 }
225 $this->dns[$id] = $dn;
226 }
227 $dns_names = array();
228 foreach($this->dns as $dn){
229 add_lock ($dn, $this->ui->dn);
230 $dns_names[] = @LDAP::fix($dn);
231 }
233 /* Lock the current entry, so nobody will edit it during deletion */
234 $smarty->assign("info", msgPool::deleteInfo($dns_names,_("object group")));
235 $smarty->assign("multiple", true);
236 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
237 }
238 }
241 /********************
242 Delete MULTIPLE entries confirmed
243 ********************/
245 /* Confirmation for deletion has been passed. Users should be deleted. */
246 if (isset($_POST['delete_multiple_ogroup_confirm'])){
248 /* Remove user by user and check acls before removeing them */
249 foreach($this->dns as $key => $dn){
251 $acl = $this->ui->get_permissions($dn, "ogroups");
252 if (preg_match('/d/', $acl)){
254 /* Delete request is permitted, perform LDAP action */
255 $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $dn);
256 $this->ogroup->delete ();
257 unset ($this->ogroup);
258 $this->ogroup= NULL;
259 } else {
261 /* Normally this shouldn't be reached, send some extra
262 logs to notify the administrator */
263 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
264 new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
265 }
266 /* Remove lock file after successfull deletion */
267 del_lock ($dn);
268 unset($this->dns[$key]);
269 }
270 }
273 /********************
274 Delete MULTIPLE entries Canceled
275 ********************/
277 /* Remove lock */
278 if(isset($_POST['delete_multiple_ogroup_cancel'])){
279 foreach($this->dns as $key => $dn){
280 del_lock ($dn);
281 unset($this->dns[$key]);
282 }
283 }
286 /****************
287 Delete object group
288 ****************/
290 if ($s_action=="del"){
292 /* Get 'dn' from posted 'uid' */
293 $this->dn= $this->ogrouplist[$s_entry]['dn'];
295 /* Load permissions for selected 'dn' and check if
296 we're allowed to remove this 'dn' */
297 $acl = $this->ui->get_permissions($this->dn,"ogroups");
298 if(preg_match("/d/",$acl)){
300 /* Check locking, save current plugin in 'back_plugin', so
301 the dialog knows where to return. */
302 if (($user= get_lock($this->dn)) != ""){
303 return(gen_locked_message ($user, $this->dn));
304 }
306 /* Lock the current entry, so nobody will edit it during deletion */
307 add_lock ($this->dn, $this->ui->dn);
308 $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->dn),_("object group")));
309 $smarty->assign("multiple", false);
310 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
311 } else {
313 /* Obviously the user isn't allowed to delete. Show message and
314 clean session. */
315 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
316 }
317 }
320 /****************
321 Delete confirmed
322 ****************/
324 /* Confirmation for deletion has been passed. Group should be deleted. */
325 if (isset($_POST['delete_group_confirm'])){
327 /* Some nice guy may send this as POST, so we've to check
328 for the permissions again. */
329 $acl = $this->ui->get_permissions($this->dn,"ogroups");
330 if(preg_match("/d/",$acl)){
332 /* Delete request is permitted, perform LDAP action */
333 $this->ogroup= new ogrouptabs($this->config,
334 $this->config->data['TABS']['OGROUPTABS'], $this->dn);
335 $this->ogroup->delete ();
336 unset ($this->ogroup);
337 $this->ogroup= NULL;
338 } else {
340 /* Normally this shouldn't be reached, send some extra
341 logs to notify the administrator */
342 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
343 new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
344 }
346 /* Remove lock file after successfull deletion */
347 del_lock ($this->dn);
348 session::un_set('objectinfo');
349 }
352 /****************
353 Delete object group Canceled
354 ****************/
356 /* Delete group canceled? */
357 if (isset($_POST['delete_cancel'])){
358 del_lock ($this->dn);
359 session::un_set('objectinfo');
360 }
363 /****************
364 Edit group
365 ****************/
367 if (($s_action=="edit") && (! isset($this->ogroup->config)) ){
369 /* Get 'dn' from posted 'uid', must be unique */
370 $this->dn= $this->ogrouplist[$s_entry]['dn'];
372 /* Check locking, save current plugin in 'back_plugin', so
373 the dialog knows where to return. */
374 if (($user= get_lock($this->dn)) != ""){
375 return(gen_locked_message ($user, $this->dn));
376 }
378 /* Lock the current entry, so everyone will get the
379 above dialog */
380 add_lock ($this->dn, $this->ui->dn);
382 /* Register grouptab to trigger edit dialog */
383 $this->ogroup= new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $this->dn, "ogroups");
384 $this->ogroup->set_acl_base($this->dn);
385 session::set('objectinfo',$this->dn);
386 }
389 /****************
390 Edit finished save
391 ****************/
393 /* Finish button has been pressed */
394 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->ogroup->config)) ){
396 /* Check tabs, will feed message array */
397 $message= $this->ogroup->check();
399 /* Save, or display error message? */
400 if (count($message) == 0){
402 /* Save user data to ldap */
403 $this->ogroup->save();
405 if (!isset($_POST['edit_apply'])){
406 /* Group has been saved successfully, remove lock from
407 LDAP. */
408 if ($this->dn != "new"){
409 del_lock ($this->dn);
410 }
412 unset ($this->ogroup);
413 $this->ogroup= NULL;
414 session::un_set('objectinfo');
415 }else{
417 /* Reinitialize tab */
418 if($this->ogroup instanceof tabs){
419 $this->ogroup->re_init();
420 }
421 }
422 } else {
423 /* Ok. There seem to be errors regarding to the tab data,
424 show message and continue as usual. */
425 msg_dialog::displayChecks($message);
426 }
427 }
430 /****************
431 Cancel edit object group
432 ****************/
434 /* Cancel dialogs */
435 if ((isset($_POST['edit_cancel'])) && (isset($this->ogroup->dn))){
436 del_lock ($this->ogroup->dn);
437 unset ($this->ogroup);
438 $this->ogroup= NULL;
439 session::un_set('objectinfo');
440 }
443 /****************
444 Display open dialogs
445 ****************/
447 /* Show dialog if object is present */
448 if (isset($this->ogroup->config)){
449 $display= $this->ogroup->execute();
451 /* Don't show buttons if tab dialog requests this */
452 if (!$this->ogroup->by_object[$this->ogroup->current]->dialog){
453 $display.= "<p style=\"text-align:right\">\n";
454 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
455 $display.= " \n";
456 if ($this->dn != "new"){
457 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
458 $display.= " \n";
459 }
460 $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
461 $display.= "</p>";
462 }
463 return ($display);
464 }
467 /****************
468 Display list
469 ****************/
471 /* Check if there is a snapshot dialog open */
472 $base = $this->DivListOGroup->selectedBase;
473 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
474 return($str);
475 }
477 /* Display dialog with group list */
478 $this->DivListOGroup->parent = $this;
479 $this->DivListOGroup->execute();
481 /* Add departments if subsearch is disabled */
482 if(!$this->DivListOGroup->SubSearch){
483 $this->DivListOGroup->AddDepartments($this->DivListOGroup->selectedBase,4,1);
484 }
485 $this->reload ();
486 $this->DivListOGroup->setEntries($this->ogrouplist);
487 return($this->DivListOGroup->Draw());
488 }
491 /* Return departments, that will be included within snapshot detection */
492 function get_used_snapshot_bases()
493 {
494 return(array(get_ou('ogroupou').$this->DivListOGroup->selectedBase));
495 }
499 function convert_list($input)
500 {
501 $temp= "";
503 $conv= array(
504 "U" => array("plugins/generic/images/head.png" ,_("User") , "ogroup"),
505 "G" => array("plugins/groups/images/groups.png" ,_("Group") , "ogroup"),
506 "A" => array("plugins/ogroups/images/application.png" ,_("Application") , "ogroup"),
507 "D" => array("plugins/departments/department.png" ,_("Department") , "ogroup"),
508 "S" => array("plugins/ogroups/images/server.png" ,_("Server") , "ogroup"),
509 "F" => array("plugins/ogroups/images/asterisk.png" ,_("Phone") , "phonequeue"),
510 "W" => array("plugins/ogroups/images/workstation.png" ,_("Workstation") , "workstartup"),
511 "O" => array("plugins/ogroups/images/winstation.png" ,_("Windows Install") , "ogroup"),
512 "T" => array("plugins/ogroups/images/terminal.png" ,_("Terminal") , "termgroup"),
513 "P" => array("plugins/ogroups/images/printer.png" ,_("Printer") , "ogroup"));
515 /* Assemble picture */
516 $type= $input['gosaGroupObjects'][0];
517 $type= preg_replace("/[^A-Z]/i","",$type);
518 if (isset($type[0])){
519 $p1['pic']= $conv[$type[0]][0];
520 $p1['tit']= $conv[$type[0]][1];
521 $p1['alt']= $type[0];
522 $p1['lnk']= $conv[$type[0]][2];
523 $html_object_1 = "<input type='image' name='".$p1['lnk']."_group_edit_%KEY' ";
524 } else {
525 $p1['pic']= "images/empty.png";
526 $p1['tit']= "";
527 $p1['alt']= "";
528 $p1['lnk']= "";//$conv[$type[0]][2];
529 $html_object_1 = "<img ";
530 }
532 if (isset($type[1])){
533 $p2['pic']= $conv[$type[1]][0];
534 $p2['alt']= $type[1];
535 $p2['tit']= $conv[$type[1]][1];
536 $p2['lnk']= $conv[$type[1]][2];
537 $html_object_2 = "<input type='image' name='".$p2['lnk']."_group_edit_%KEY' ";
538 } else {
539 $p2['pic']= "images/empty.png";
540 $p2['alt']= "";
541 $p2['tit']= "";
542 $p2['lnk']= ""; //$conv[$type[0]][2];
543 $html_object_2 = "<img ";
544 }
546 $temp = $html_object_1." class='center' id='iconA".preg_replace("/_/","",$p1['lnk']."_group_edit_%KEY")."'
547 src='".$p1['pic']."' alt='".$p1['alt']."' title='".$p1['tit']."'>";
548 $temp.= $html_object_2." class='center' id='iconB".preg_replace("/_/","",$p2['lnk']."_group_edit_%KEY")."'
549 src='".$p2['pic']."' alt='".$p2['alt']."' title='".$p2['tit']."'>";
550 return ($temp);
551 }
554 function reload()
555 {
556 /* Set base for all searches && initialise some vars */
557 $this->ogrouplist= array();
558 $base = $this->DivListOGroup->selectedBase;
559 $filter = "(gosaGroupObjects=[])";
560 $Regex = $this->DivListOGroup->Regex;
562 $chk = array(
563 "UserGroups" => "(gosaGroupObjects=*U*)" ,
564 "GroupGroups" => "(gosaGroupObjects=*G*)" ,
565 "ApplicationGroups" => "(gosaGroupObjects=*A*)" ,
566 "DepartmentGroups" => "(gosaGroupObjects=*D*)" ,
567 "ServerGroups" => "(gosaGroupObjects=*S*)" ,
568 "WorkstationGroups" => "(gosaGroupObjects=*W*)" ,
569 "WindowsGroups" => "(gosaGroupObjects=*O*)" ,
570 "TerminalGroups" => "(gosaGroupObjects=*T*)" ,
571 "PrinterGroups" => "(gosaGroupObjects=*P*)" ,
572 "PhoneGroups" => "(gosaGroupObjects=*F*)" );
573 /* Create filter */
574 foreach($chk as $chkBox => $FilterPart){
575 if($this->DivListOGroup->$chkBox){
576 $filter .= $FilterPart;
577 }
578 }
579 $filter= "(&(cn=$Regex)(objectClass=gosaGroupOfNames)(|$filter))";
581 if($this->DivListOGroup->SubSearch){
582 $res= get_sub_list($filter, "ogroups",get_ou('ogroupou'), $base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT | GL_SUBSEARCH);
583 }else{
584 $res= get_sub_list($filter, "ogroups",get_ou('ogroupou'), get_ou('ogroupou').$base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT );
585 }
587 $this->ogrouplist= $res;
588 ksort ($this->ogrouplist);
589 reset ($this->ogrouplist);
590 $tmp=array();
591 $tmp2 = array();
592 foreach($this->ogrouplist as $tkey => $val ){
593 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
594 $tmp2[strtolower($val['cn'][0]).$val['cn'][0]] = strtolower($val['cn'][0]).$val['cn'][0];
595 }
596 natcasesort($tmp2);
597 $this->ogrouplist=array();
598 foreach($tmp2 as $val){
599 $this->ogrouplist[]=$tmp[$val];
600 }
601 reset ($this->ogrouplist);
602 }
605 function list_get_selected_items()
606 {
607 $ids = array();
608 foreach($_POST as $name => $value){
609 if(preg_match("/^item_selected_[0-9]*$/",$name)){
610 $id = preg_replace("/^item_selected_/","",$name);
611 $ids[$id] = $id;
612 }
613 }
614 return($ids);
615 }
618 function copyPasteHandling_from_queue($s_action,$s_entry)
619 {
620 /* Check if Copy & Paste is disabled */
621 if(!is_object($this->CopyPasteHandler)){
622 return("");
623 }
625 /* Add a single entry to queue */
626 if($s_action == "cut" || $s_action == "copy"){
628 /* Cleanup object queue */
629 $this->CopyPasteHandler->cleanup_queue();
630 $dn = $this->ogrouplist[$s_entry]['dn'];
631 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"ogrouptabs","OGROUPTABS","ogroups");
632 }
635 /* Add entries to queue */
636 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
638 /* Cleanup object queue */
639 $this->CopyPasteHandler->cleanup_queue();
641 /* Add new entries to CP queue */
642 foreach($this->list_get_selected_items() as $id){
643 $dn = $this->ogrouplist[$id]['dn'];
645 if($s_action == "copy_multiple"){
646 $this->CopyPasteHandler->add_to_queue($dn,"copy","ogrouptabs","OGROUPTABS","ogroups");
647 }
648 if($s_action == "cut_multiple"){
649 $this->CopyPasteHandler->add_to_queue($dn,"cut","ogrouptabs","OGROUPTABS","ogroups");
650 }
651 }
652 }
654 /* Start pasting entries */
655 if($s_action == "editPaste"){
656 $this->start_pasting_copied_objects = TRUE;
657 }
659 /* Return C&P dialog */
660 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
662 /* Get dialog */
663 $data = $this->CopyPasteHandler->execute();
664 $this->CopyPasteHandler->SetVar("base",$this->DivListOGroup->selectedBase);
666 /* Return dialog data */
667 if(!empty($data)){
668 return($data);
669 }
670 }
671 /* Automatically disable status for pasting */
672 if(!$this->CopyPasteHandler->entries_queued()){
673 $this->start_pasting_copied_objects = FALSE;
674 }
675 return("");
676 }
679 function save_object()
680 {
681 $this->DivListOGroup->save_object();
682 if(is_object($this->CopyPasteHandler)){
683 $this->CopyPasteHandler->save_object();
684 }
685 }
687 }
689 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
690 ?>