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(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
174 $o_queue->get_error()),ERROR_DIALOG);
175 }else{
176 $this->ogroup = FALSE;
177 }
178 }
180 /* Display event */
181 if($this->ogroup instanceof DaemonEvent){
182 $this->ogroup->save_object();
183 return($this->ogroup->execute());
184 }
187 /****************
188 Copy & Paste handling
189 ****************/
191 /* Display the copy & paste dialog, if it is currently open */
192 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
193 if($ret){
194 return($ret);
195 }
198 /***************
199 Create a new object group
200 ****************/
202 /* New group? */
203 if ($s_action=="new"){
205 /* By default we set 'dn' to 'new', all relevant plugins will
206 react on this. */
207 $this->dn= "new";
209 /* Create new ogroup- object */
210 $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $this->dn,"ogroups");
211 $this->ogroup->set_acl_base($this->DivListOGroup->selectedBase);
212 }
215 /********************
216 Delete MULTIPLE entries requested, display confirm dialog
217 ********************/
218 if ($s_action=="del_multiple"){
219 $ids = $this->list_get_selected_items();
220 if(count($ids)){
221 foreach($ids as $id){
222 $dn = $this->ogrouplist[$id]['dn'];
223 if (($user= get_lock($dn)) != ""){
224 return(gen_locked_message ($user, $dn));
225 }
226 $this->dns[$id] = $dn;
227 }
228 $dns_names = "<br><pre>";
229 foreach($this->dns as $dn){
230 add_lock ($dn, $this->ui->dn);
231 $dns_names .= $dn."\n";
232 }
233 $dns_names .="</pre>";
234 /* Lock the current entry, so nobody will edit it during deletion */
235 if (count($this->dns) == 1){
236 $smarty->assign("info", sprintf(_("You're about to delete the following object entry %s"), @LDAP::fix($dns_names)));
237 } else {
238 $smarty->assign("info", sprintf(_("You're about to delete the following object entries %s"), @LDAP::fix($dns_names)));
239 }
240 $smarty->assign("multiple", true);
241 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
242 }
243 }
246 /********************
247 Delete MULTIPLE entries confirmed
248 ********************/
250 /* Confirmation for deletion has been passed. Users should be deleted. */
251 if (isset($_POST['delete_multiple_ogroup_confirm'])){
253 /* Remove user by user and check acls before removeing them */
254 foreach($this->dns as $key => $dn){
256 $acl = $this->ui->get_permissions($dn, "ogroups");
257 if (preg_match('/d/', $acl)){
259 /* Delete request is permitted, perform LDAP action */
260 $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $dn);
261 $this->ogroup->delete ();
262 unset ($this->ogroup);
263 $this->ogroup= NULL;
264 } else {
266 /* Normally this shouldn't be reached, send some extra
267 logs to notify the administrator */
268 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), INFO_DIALOG);
269 new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
270 }
271 /* Remove lock file after successfull deletion */
272 del_lock ($dn);
273 unset($this->dns[$key]);
274 }
275 }
278 /********************
279 Delete MULTIPLE entries Canceled
280 ********************/
282 /* Remove lock */
283 if(isset($_POST['delete_multiple_ogroup_cancel'])){
284 foreach($this->dns as $key => $dn){
285 del_lock ($dn);
286 unset($this->dns[$key]);
287 }
288 }
291 /****************
292 Delete object group
293 ****************/
295 if ($s_action=="del"){
297 /* Get 'dn' from posted 'uid' */
298 $this->dn= $this->ogrouplist[$s_entry]['dn'];
300 /* Load permissions for selected 'dn' and check if
301 we're allowed to remove this 'dn' */
302 $acl = $this->ui->get_permissions($this->dn,"ogroups");
303 if(preg_match("/d/",$acl)){
305 /* Check locking, save current plugin in 'back_plugin', so
306 the dialog knows where to return. */
307 if (($user= get_lock($this->dn)) != ""){
308 return(gen_locked_message ($user, $this->dn));
309 }
311 /* Lock the current entry, so nobody will edit it during deletion */
312 add_lock ($this->dn, $this->ui->dn);
313 $smarty->assign("info", sprintf(_("You're about to delete the object group '%s'."), @LDAP::fix($this->dn)));
314 $smarty->assign("multiple", false);
315 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
316 } else {
318 /* Obviously the user isn't allowed to delete. Show message and
319 clean session. */
320 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), INFO_DIALOG);
321 }
322 }
325 /****************
326 Delete confirmed
327 ****************/
329 /* Confirmation for deletion has been passed. Group should be deleted. */
330 if (isset($_POST['delete_group_confirm'])){
332 /* Some nice guy may send this as POST, so we've to check
333 for the permissions again. */
334 $acl = $this->ui->get_permissions($this->dn,"ogroups");
335 if(preg_match("/d/",$acl)){
337 /* Delete request is permitted, perform LDAP action */
338 $this->ogroup= new ogrouptabs($this->config,
339 $this->config->data['TABS']['OGROUPTABS'], $this->dn);
340 $this->ogroup->delete ();
341 unset ($this->ogroup);
342 $this->ogroup= NULL;
343 } else {
345 /* Normally this shouldn't be reached, send some extra
346 logs to notify the administrator */
347 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), INFO_DIALOG);
348 new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
349 }
351 /* Remove lock file after successfull deletion */
352 del_lock ($this->dn);
353 session::un_set('objectinfo');
354 }
357 /****************
358 Delete object group Canceled
359 ****************/
361 /* Delete group canceled? */
362 if (isset($_POST['delete_cancel'])){
363 del_lock ($this->dn);
364 session::un_set('objectinfo');
365 }
368 /****************
369 Edit group
370 ****************/
372 if (($s_action=="edit") && (! isset($this->ogroup->config)) ){
374 /* Get 'dn' from posted 'uid', must be unique */
375 $this->dn= $this->ogrouplist[$s_entry]['dn'];
377 /* Check locking, save current plugin in 'back_plugin', so
378 the dialog knows where to return. */
379 if (($user= get_lock($this->dn)) != ""){
380 return(gen_locked_message ($user, $this->dn));
381 }
383 /* Lock the current entry, so everyone will get the
384 above dialog */
385 add_lock ($this->dn, $this->ui->dn);
387 /* Register grouptab to trigger edit dialog */
388 $this->ogroup= new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $this->dn, "ogroups");
389 $this->ogroup->set_acl_base($this->dn);
390 session::set('objectinfo',$this->dn);
391 }
394 /****************
395 Edit finished save
396 ****************/
398 /* Finish button has been pressed */
399 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->ogroup->config)) ){
401 /* Check tabs, will feed message array */
402 $message= $this->ogroup->check();
404 /* Save, or display error message? */
405 if (count($message) == 0){
407 /* Save user data to ldap */
408 $this->ogroup->save();
410 if (!isset($_POST['edit_apply'])){
411 /* Group has been saved successfully, remove lock from
412 LDAP. */
413 if ($this->dn != "new"){
414 del_lock ($this->dn);
415 }
417 unset ($this->ogroup);
418 $this->ogroup= NULL;
419 session::un_set('objectinfo');
420 }
421 } else {
422 /* Ok. There seem to be errors regarding to the tab data,
423 show message and continue as usual. */
424 show_errors($message);
425 }
426 }
429 /****************
430 Cancel edit object group
431 ****************/
433 /* Cancel dialogs */
434 if ((isset($_POST['edit_cancel'])) && (isset($this->ogroup->dn))){
435 del_lock ($this->ogroup->dn);
436 unset ($this->ogroup);
437 $this->ogroup= NULL;
438 session::un_set('objectinfo');
439 }
442 /****************
443 Display open dialogs
444 ****************/
446 /* Show dialog if object is present */
447 if (isset($this->ogroup->config)){
448 $display= $this->ogroup->execute();
450 /* Don't show buttons if tab dialog requests this */
451 if (!$this->ogroup->by_object[$this->ogroup->current]->dialog){
452 $display.= "<p style=\"text-align:right\">\n";
453 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
454 $display.= " \n";
455 if ($this->dn != "new"){
456 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
457 $display.= " \n";
458 }
459 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
460 $display.= "</p>";
461 }
462 return ($display);
463 }
466 /****************
467 Display list
468 ****************/
470 /* Check if there is a snapshot dialog open */
471 $base = $this->DivListOGroup->selectedBase;
472 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
473 return($str);
474 }
476 /* Display dialog with group list */
477 $this->DivListOGroup->parent = $this;
478 $this->DivListOGroup->execute();
480 /* Add departments if subsearch is disabled */
481 if(!$this->DivListOGroup->SubSearch){
482 $this->DivListOGroup->AddDepartments($this->DivListOGroup->selectedBase,4,1);
483 }
484 $this->reload ();
485 $this->DivListOGroup->setEntries($this->ogrouplist);
486 return($this->DivListOGroup->Draw());
487 }
490 /* Return departments, that will be included within snapshot detection */
491 function get_used_snapshot_bases()
492 {
493 return(array(get_ou('ogroupou').$this->DivListOGroup->selectedBase));
494 }
498 function convert_list($input)
499 {
500 $temp= "";
502 $conv= array(
503 "U" => array("select_user.png" ,_("User") , "ogroup"),
504 "G" => array("select_groups.png" ,_("Group") , "ogroup"),
505 "A" => array("select_application.png" ,_("Application") , "ogroup"),
506 "D" => array("select_department.png" ,_("Department") , "ogroup"),
507 "S" => array("select_server.png" ,_("Server") , "ogroup"),
508 "F" => array("select_phone.png" ,_("Phone") , "phonequeue"),
509 "W" => array("select_workstation.png" ,_("Workstation") , "workstartup"),
510 "O" => array("select_winstation.png" ,_("Windows Install") , "ogroup"),
511 "T" => array("select_terminal.png" ,_("Terminal") , "termgroup"),
512 "P" => array("select_printer.png" ,_("Printer") , "ogroup"));
514 /* Assemble picture */
515 $type= $input['gosaGroupObjects'][0];
516 $type= preg_replace("/[^A-Z]/i","",$type);
517 if (isset($type[0])){
518 $p1['pic']= $conv[$type[0]][0];
519 $p1['tit']= $conv[$type[0]][1];
520 $p1['alt']= $type[0];
521 $p1['lnk']= $conv[$type[0]][2];
522 $html_object_1 = "<input type='image' name='".$p1['lnk']."_group_edit_%KEY' ";
523 } else {
524 $p1['pic']= "empty.png";
525 $p1['tit']= "";
526 $p1['alt']= "";
527 $p1['lnk']= "";//$conv[$type[0]][2];
528 $html_object_1 = "<img ";
529 }
531 if (isset($type[1])){
532 $p2['pic']= $conv[$type[1]][0];
533 $p2['alt']= $type[1];
534 $p2['tit']= $conv[$type[1]][1];
535 $p2['lnk']= $conv[$type[1]][2];
536 $html_object_2 = "<input type='image' name='".$p2['lnk']."_group_edit_%KEY' ";
537 } else {
538 $p2['pic']= "empty.png";
539 $p2['alt']= "";
540 $p2['tit']= "";
541 $p2['lnk']= ""; //$conv[$type[0]][2];
542 $html_object_2 = "<img ";
543 }
545 $temp = $html_object_1." class='center' id='iconA".preg_replace("/_/","",$p1['lnk']."_group_edit_%KEY")."'
546 src='images/".$p1['pic']."' alt='".$p1['alt']."' title='".$p1['tit']."'>";
547 $temp.= $html_object_2." class='center' id='iconB".preg_replace("/_/","",$p2['lnk']."_group_edit_%KEY")."'
548 src='images/".$p2['pic']."' alt='".$p2['alt']."' title='".$p2['tit']."'>";
549 return ($temp);
550 }
553 function reload()
554 {
555 /* Set base for all searches && initialise some vars */
556 $this->ogrouplist= array();
557 $base = $this->DivListOGroup->selectedBase;
558 $filter = "(gosaGroupObjects=[])";
559 $Regex = $this->DivListOGroup->Regex;
561 $chk = array(
562 "UserGroups" => "(gosaGroupObjects=*U*)" ,
563 "GroupGroups" => "(gosaGroupObjects=*G*)" ,
564 "ApplicationGroups" => "(gosaGroupObjects=*A*)" ,
565 "DepartmentGroups" => "(gosaGroupObjects=*D*)" ,
566 "ServerGroups" => "(gosaGroupObjects=*S*)" ,
567 "WorkstationGroups" => "(gosaGroupObjects=*W*)" ,
568 "WindowsGroups" => "(gosaGroupObjects=*O*)" ,
569 "TerminalGroups" => "(gosaGroupObjects=*T*)" ,
570 "PrinterGroups" => "(gosaGroupObjects=*P*)" ,
571 "PhoneGroups" => "(gosaGroupObjects=*F*)" );
572 /* Create filter */
573 foreach($chk as $chkBox => $FilterPart){
574 if($this->DivListOGroup->$chkBox){
575 $filter .= $FilterPart;
576 }
577 }
578 $filter= "(&(cn=$Regex)(objectClass=gosaGroupOfNames)(|$filter))";
580 if($this->DivListOGroup->SubSearch){
581 $res= get_sub_list($filter, "ogroups",get_ou('ogroupou'), $base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT | GL_SUBSEARCH);
582 }else{
583 $res= get_sub_list($filter, "ogroups",get_ou('ogroupou'), get_ou('ogroupou').$base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT );
584 }
586 $this->ogrouplist= $res;
587 ksort ($this->ogrouplist);
588 reset ($this->ogrouplist);
589 $tmp=array();
590 $tmp2 = array();
591 foreach($this->ogrouplist as $tkey => $val ){
592 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
593 $tmp2[strtolower($val['cn'][0]).$val['cn'][0]] = strtolower($val['cn'][0]).$val['cn'][0];
594 }
595 natcasesort($tmp2);
596 $this->ogrouplist=array();
597 foreach($tmp2 as $val){
598 $this->ogrouplist[]=$tmp[$val];
599 }
600 reset ($this->ogrouplist);
601 }
604 function list_get_selected_items()
605 {
606 $ids = array();
607 foreach($_POST as $name => $value){
608 if(preg_match("/^item_selected_[0-9]*$/",$name)){
609 $id = preg_replace("/^item_selected_/","",$name);
610 $ids[$id] = $id;
611 }
612 }
613 return($ids);
614 }
617 function copyPasteHandling_from_queue($s_action,$s_entry)
618 {
619 /* Check if Copy & Paste is disabled */
620 if(!is_object($this->CopyPasteHandler)){
621 return("");
622 }
624 /* Add a single entry to queue */
625 if($s_action == "cut" || $s_action == "copy"){
627 /* Cleanup object queue */
628 $this->CopyPasteHandler->cleanup_queue();
629 $dn = $this->ogrouplist[$s_entry]['dn'];
630 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"ogrouptabs","OGROUPTABS","ogroups");
631 }
634 /* Add entries to queue */
635 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
637 /* Cleanup object queue */
638 $this->CopyPasteHandler->cleanup_queue();
640 /* Add new entries to CP queue */
641 foreach($this->list_get_selected_items() as $id){
642 $dn = $this->ogrouplist[$id]['dn'];
644 if($s_action == "copy_multiple"){
645 $this->CopyPasteHandler->add_to_queue($dn,"copy","ogrouptabs","OGROUPTABS","ogroups");
646 }
647 if($s_action == "cut_multiple"){
648 $this->CopyPasteHandler->add_to_queue($dn,"cut","ogrouptabs","OGROUPTABS","ogroups");
649 }
650 }
651 }
653 /* Start pasting entries */
654 if($s_action == "editPaste"){
655 $this->start_pasting_copied_objects = TRUE;
656 }
658 /* Return C&P dialog */
659 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
661 /* Get dialog */
662 $data = $this->CopyPasteHandler->execute();
663 $this->CopyPasteHandler->SetVar("base",$this->DivListOGroup->selectedBase);
665 /* Return dialog data */
666 if(!empty($data)){
667 return($data);
668 }
669 }
670 /* Automatically disable status for pasting */
671 if(!$this->CopyPasteHandler->entries_queued()){
672 $this->start_pasting_copied_objects = FALSE;
673 }
674 return("");
675 }
678 function save_object()
679 {
680 $this->DivListOGroup->save_object();
681 }
683 }
685 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
686 ?>