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 }
416 } else {
417 /* Ok. There seem to be errors regarding to the tab data,
418 show message and continue as usual. */
419 msg_dialog::displayChecks($message);
420 }
421 }
424 /****************
425 Cancel edit object group
426 ****************/
428 /* Cancel dialogs */
429 if ((isset($_POST['edit_cancel'])) && (isset($this->ogroup->dn))){
430 del_lock ($this->ogroup->dn);
431 unset ($this->ogroup);
432 $this->ogroup= NULL;
433 session::un_set('objectinfo');
434 }
437 /****************
438 Display open dialogs
439 ****************/
441 /* Show dialog if object is present */
442 if (isset($this->ogroup->config)){
443 $display= $this->ogroup->execute();
445 /* Don't show buttons if tab dialog requests this */
446 if (!$this->ogroup->by_object[$this->ogroup->current]->dialog){
447 $display.= "<p style=\"text-align:right\">\n";
448 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
449 $display.= " \n";
450 if ($this->dn != "new"){
451 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
452 $display.= " \n";
453 }
454 $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
455 $display.= "</p>";
456 }
457 return ($display);
458 }
461 /****************
462 Display list
463 ****************/
465 /* Check if there is a snapshot dialog open */
466 $base = $this->DivListOGroup->selectedBase;
467 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
468 return($str);
469 }
471 /* Display dialog with group list */
472 $this->DivListOGroup->parent = $this;
473 $this->DivListOGroup->execute();
475 /* Add departments if subsearch is disabled */
476 if(!$this->DivListOGroup->SubSearch){
477 $this->DivListOGroup->AddDepartments($this->DivListOGroup->selectedBase,4,1);
478 }
479 $this->reload ();
480 $this->DivListOGroup->setEntries($this->ogrouplist);
481 return($this->DivListOGroup->Draw());
482 }
485 /* Return departments, that will be included within snapshot detection */
486 function get_used_snapshot_bases()
487 {
488 return(array(get_ou('ogroupou').$this->DivListOGroup->selectedBase));
489 }
493 function convert_list($input)
494 {
495 $temp= "";
497 $conv= array(
498 "U" => array("plugins/generic/images/head.png" ,_("User") , "ogroup"),
499 "G" => array("plugins/groups/images/groups.png" ,_("Group") , "ogroup"),
500 "A" => array("plugins/ogroups/images/application.png" ,_("Application") , "ogroup"),
501 "D" => array("plugins/departments/department.png" ,_("Department") , "ogroup"),
502 "S" => array("plugins/ogroups/images/server.png" ,_("Server") , "ogroup"),
503 "F" => array("plugins/ogroups/images/asterisk.png" ,_("Phone") , "phonequeue"),
504 "W" => array("plugins/ogroups/images/workstation.png" ,_("Workstation") , "workstartup"),
505 "O" => array("plugins/ogroups/images/winstation.png" ,_("Windows Install") , "ogroup"),
506 "T" => array("plugins/ogroups/images/terminal.png" ,_("Terminal") , "termgroup"),
507 "P" => array("plugins/ogroups/images/printer.png" ,_("Printer") , "ogroup"));
509 /* Assemble picture */
510 $type= $input['gosaGroupObjects'][0];
511 $type= preg_replace("/[^A-Z]/i","",$type);
512 if (isset($type[0])){
513 $p1['pic']= $conv[$type[0]][0];
514 $p1['tit']= $conv[$type[0]][1];
515 $p1['alt']= $type[0];
516 $p1['lnk']= $conv[$type[0]][2];
517 $html_object_1 = "<input type='image' name='".$p1['lnk']."_group_edit_%KEY' ";
518 } else {
519 $p1['pic']= "images/empty.png";
520 $p1['tit']= "";
521 $p1['alt']= "";
522 $p1['lnk']= "";//$conv[$type[0]][2];
523 $html_object_1 = "<img ";
524 }
526 if (isset($type[1])){
527 $p2['pic']= $conv[$type[1]][0];
528 $p2['alt']= $type[1];
529 $p2['tit']= $conv[$type[1]][1];
530 $p2['lnk']= $conv[$type[1]][2];
531 $html_object_2 = "<input type='image' name='".$p2['lnk']."_group_edit_%KEY' ";
532 } else {
533 $p2['pic']= "images/empty.png";
534 $p2['alt']= "";
535 $p2['tit']= "";
536 $p2['lnk']= ""; //$conv[$type[0]][2];
537 $html_object_2 = "<img ";
538 }
540 $temp = $html_object_1." class='center' id='iconA".preg_replace("/_/","",$p1['lnk']."_group_edit_%KEY")."'
541 src='".$p1['pic']."' alt='".$p1['alt']."' title='".$p1['tit']."'>";
542 $temp.= $html_object_2." class='center' id='iconB".preg_replace("/_/","",$p2['lnk']."_group_edit_%KEY")."'
543 src='".$p2['pic']."' alt='".$p2['alt']."' title='".$p2['tit']."'>";
544 return ($temp);
545 }
548 function reload()
549 {
550 /* Set base for all searches && initialise some vars */
551 $this->ogrouplist= array();
552 $base = $this->DivListOGroup->selectedBase;
553 $filter = "(gosaGroupObjects=[])";
554 $Regex = $this->DivListOGroup->Regex;
556 $chk = array(
557 "UserGroups" => "(gosaGroupObjects=*U*)" ,
558 "GroupGroups" => "(gosaGroupObjects=*G*)" ,
559 "ApplicationGroups" => "(gosaGroupObjects=*A*)" ,
560 "DepartmentGroups" => "(gosaGroupObjects=*D*)" ,
561 "ServerGroups" => "(gosaGroupObjects=*S*)" ,
562 "WorkstationGroups" => "(gosaGroupObjects=*W*)" ,
563 "WindowsGroups" => "(gosaGroupObjects=*O*)" ,
564 "TerminalGroups" => "(gosaGroupObjects=*T*)" ,
565 "PrinterGroups" => "(gosaGroupObjects=*P*)" ,
566 "PhoneGroups" => "(gosaGroupObjects=*F*)" );
567 /* Create filter */
568 foreach($chk as $chkBox => $FilterPart){
569 if($this->DivListOGroup->$chkBox){
570 $filter .= $FilterPart;
571 }
572 }
573 $filter= "(&(cn=$Regex)(objectClass=gosaGroupOfNames)(|$filter))";
575 if($this->DivListOGroup->SubSearch){
576 $res= get_sub_list($filter, "ogroups",get_ou('ogroupou'), $base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT | GL_SUBSEARCH);
577 }else{
578 $res= get_sub_list($filter, "ogroups",get_ou('ogroupou'), get_ou('ogroupou').$base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT );
579 }
581 $this->ogrouplist= $res;
582 ksort ($this->ogrouplist);
583 reset ($this->ogrouplist);
584 $tmp=array();
585 $tmp2 = array();
586 foreach($this->ogrouplist as $tkey => $val ){
587 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
588 $tmp2[strtolower($val['cn'][0]).$val['cn'][0]] = strtolower($val['cn'][0]).$val['cn'][0];
589 }
590 natcasesort($tmp2);
591 $this->ogrouplist=array();
592 foreach($tmp2 as $val){
593 $this->ogrouplist[]=$tmp[$val];
594 }
595 reset ($this->ogrouplist);
596 }
599 function list_get_selected_items()
600 {
601 $ids = array();
602 foreach($_POST as $name => $value){
603 if(preg_match("/^item_selected_[0-9]*$/",$name)){
604 $id = preg_replace("/^item_selected_/","",$name);
605 $ids[$id] = $id;
606 }
607 }
608 return($ids);
609 }
612 function copyPasteHandling_from_queue($s_action,$s_entry)
613 {
614 /* Check if Copy & Paste is disabled */
615 if(!is_object($this->CopyPasteHandler)){
616 return("");
617 }
619 /* Add a single entry to queue */
620 if($s_action == "cut" || $s_action == "copy"){
622 /* Cleanup object queue */
623 $this->CopyPasteHandler->cleanup_queue();
624 $dn = $this->ogrouplist[$s_entry]['dn'];
625 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"ogrouptabs","OGROUPTABS","ogroups");
626 }
629 /* Add entries to queue */
630 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
632 /* Cleanup object queue */
633 $this->CopyPasteHandler->cleanup_queue();
635 /* Add new entries to CP queue */
636 foreach($this->list_get_selected_items() as $id){
637 $dn = $this->ogrouplist[$id]['dn'];
639 if($s_action == "copy_multiple"){
640 $this->CopyPasteHandler->add_to_queue($dn,"copy","ogrouptabs","OGROUPTABS","ogroups");
641 }
642 if($s_action == "cut_multiple"){
643 $this->CopyPasteHandler->add_to_queue($dn,"cut","ogrouptabs","OGROUPTABS","ogroups");
644 }
645 }
646 }
648 /* Start pasting entries */
649 if($s_action == "editPaste"){
650 $this->start_pasting_copied_objects = TRUE;
651 }
653 /* Return C&P dialog */
654 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
656 /* Get dialog */
657 $data = $this->CopyPasteHandler->execute();
658 $this->CopyPasteHandler->SetVar("base",$this->DivListOGroup->selectedBase);
660 /* Return dialog data */
661 if(!empty($data)){
662 return($data);
663 }
664 }
665 /* Automatically disable status for pasting */
666 if(!$this->CopyPasteHandler->entries_queued()){
667 $this->start_pasting_copied_objects = FALSE;
668 }
669 return("");
670 }
673 function save_object()
674 {
675 $this->DivListOGroup->save_object();
676 if(is_object($this->CopyPasteHandler)){
677 $this->CopyPasteHandler->save_object();
678 }
679 }
681 }
683 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
684 ?>