Code

Updated headers
[gosa.git] / gosa-core / plugins / admin / ogroups / class_ogroupManagement.inc
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("/^trigger_event_DaemonEvent_notify/",$_POST['menu_action'])){
137       $s_action = "notify";
138     }
140     /********************
141       Create notification event
142      ********************/
144     if($s_action == "notify"){
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         $this->ogroup= new DaemonEvent_notify($this->config);
153         $this->ogroup->add_targets($uids);
154       }
156     }
158     /* Abort event dialog */
159     if(isset($_POST['abort_event_dialog'])){
160       $this->ogroup = FALSE;
161     }
163     /* Save event */
164     if(isset($_POST['save_event_dialog'])){
165       $o_queue = new gosaSupportDaemon();
166       $o_queue->append($this->ogroup);
167       if($o_queue->is_error()){
168         msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
169               $o_queue->get_error()),ERROR_DIALOG);
170       }else{
171         $this->ogroup = FALSE;
172       }
173     }
175     /* Display event */
176     if($this->ogroup instanceof DaemonEvent){
177       $this->ogroup->save_object();
178       return($this->ogroup->execute());
179     }
182     /****************
183       Copy & Paste handling 
184      ****************/
186     /* Display the copy & paste dialog, if it is currently open */
187     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
188     if($ret){
189       return($ret);
190     }
193     /***************
194       Create a new object group 
195      ****************/
197     /* New group? */
198     if ($s_action=="new"){
200       /* By default we set 'dn' to 'new', all relevant plugins will
201          react on this. */
202       $this->dn= "new";
204       /* Create new ogroup- object */
205       $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $this->dn,"ogroups");
206       $this->ogroup->set_acl_base($this->DivListOGroup->selectedBase);
207     }
210     /********************
211       Delete MULTIPLE entries requested, display confirm dialog
212      ********************/
213     if ($s_action=="del_multiple"){
214       $ids = $this->list_get_selected_items();
215       if(count($ids)){
216         foreach($ids as $id){
217           $dn = $this->ogrouplist[$id]['dn'];
218           if (($user= get_lock($dn)) != ""){
219             return(gen_locked_message ($user, $dn));
220           }
221           $this->dns[$id] = $dn;
222         }
223         $dns_names = "<br><pre>";
224         foreach($this->dns as $dn){
225           add_lock ($dn, $this->ui->dn);
226           $dns_names .= $dn."\n";
227         }
228         $dns_names .="</pre>";
229         /* Lock the current entry, so nobody will edit it during deletion */
230         if (count($this->dns) == 1){
231           $smarty->assign("info",     sprintf(_("You're about to delete the following object entry %s"), @LDAP::fix($dns_names)));
232         } else {
233           $smarty->assign("info",     sprintf(_("You're about to delete the following object entries %s"), @LDAP::fix($dns_names)));
234         }
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"), _("You have no permission to delete this entry!"), 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", sprintf(_("You're about to delete the object group '%s'."), @LDAP::fix($this->dn)));
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"), _("You have no permission to delete this entry!"), 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"), _("You have no permission to delete this entry!"), 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         show_errors($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=\""._("Ok")."\">\n";
449         $display.= "&nbsp;\n";
450         if ($this->dn != "new"){
451           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
452           $display.= "&nbsp;\n";
453         }
454         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\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   }
491   
493   function convert_list($input)
494   {
495     $temp= "";
497     $conv= array(   
498         "U" => array("select_user.png"        ,_("User")        , "ogroup"),
499         "G" => array("select_groups.png"      ,_("Group")       , "ogroup"),
500         "A" => array("select_application.png" ,_("Application") , "ogroup"),
501         "D" => array("select_department.png"  ,_("Department")  , "ogroup"),
502         "S" => array("select_server.png"      ,_("Server")      , "ogroup"),
503         "F" => array("select_phone.png"       ,_("Phone")       , "phonequeue"),
504         "W" => array("select_workstation.png" ,_("Workstation") , "workstartup"),
505         "O" => array("select_winstation.png" ,_("Windows Install") , "ogroup"),
506         "T" => array("select_terminal.png"    ,_("Terminal")    , "termgroup"),
507         "P" => array("select_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']= "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']= "empty.png";
534       $p2['alt']= "";
535       $p2['tit']= "";
536       $p2['lnk']= ""; //$conv[$type[0]][2];
537       $html_object_2 = "<img ";
538     }
539     
540     $temp = $html_object_1." class='center' id='iconA".preg_replace("/_/","",$p1['lnk']."_group_edit_%KEY")."' 
541       src='images/".$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='images/".$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   }
680 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
681 ?>