Code

Starting move
[gosa.git] / gosa-core / plugins / admin / ogroups / class_ogroupManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2004 Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class ogroupManagement extends plugin
22 {
23   /* Definitions */
24   var $plHeadline= "Object groups";
25   var $plDescription= "This does something";
27   /* attribute list for save action */
28   var $attributes= array();
29   var $objectclasses= array();
30   var $obtypes= array();
31   var $ogroup;
33   var $CopyPasteHandler = NULL;
34   var $DivListOGroup    = NULL;
35   var $start_pasting_copied_objects = FALSE;
37   function ogroupManagement (&$config, $dn= NULL)
38   {
39     /* Include config object */
40     $this->config= &$config;
41     $this->ui= get_userinfo();
43     /* Copy & Paste enabled ?
44      */
45     if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE'] ))){
46       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
47     }
49     /* Div lsit management */
50     $this->DivListOGroup = new divListOGroup($this->config,$this);
51   }
53   function execute()
54   {
55     /* Call parent execute */
56     plugin::execute();
58     /****************
59       Variable intialisation && Check posts for commands  
60      ****************/
62     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^group_edit_/","/^group_del_/","/^item_selected/","/^remove_multiple_ogroups/");
64     $smarty     = get_smarty();
65     $s_action   = "";
66     $s_entry    = "";
68     /* Test Posts */
69     foreach($_POST as $key => $val){
70       // Post for delete
71       if(preg_match("/^group_del.*/",$key)){
72         $s_action = "del";
73         $s_entry  = preg_replace("/^group_".$s_action."_/i","",$key);
74         // Post for edit
75       }elseif(preg_match("/^group_edit_.*/",$key)){
76         $s_action="edit";
77         $s_entry  = preg_replace("/^group_".$s_action."_/i","",$key);
78         // Post for new
79       }elseif(preg_match("/^group_new.*/",$key)){
80         $s_action="new";
81       }elseif(preg_match("/^editPaste.*/i",$key)){
82         $s_action="editPaste";
83       }elseif(preg_match("/^copy_.*/",$key)){
84         $s_action="copy";
85         $s_entry  = preg_replace("/^copy_/i","",$key);
86       }elseif(preg_match("/^cut_.*/",$key)){
87         $s_action="cut";
88         $s_entry  = preg_replace("/^cut_/i","",$key);
89       }elseif(preg_match("/^remove_multiple_ogroups/",$key)){
90         $s_action="del_multiple";
91       }elseif(preg_match("/^multiple_cut_ogroups/",$key)){
92         $s_action = "cut_multiple";
93       }elseif(preg_match("/^multiple_copy_ogroups/",$key)){
94         $s_action = "copy_multiple";
95       }elseif(preg_match("/_group_edit_/",$key)){
96         $type = preg_replace("/_group_edit_.*$/","",$key);
97         $s_action="edit";
98         $s_entry  = preg_replace("/".$type."_group_edit_/i","",$key);
99         $_POST['arg'] = $type;
100       }
101     }
102     $s_entry  = preg_replace("/_.$/","",$s_entry);
104     // Edit if
105     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
106       $s_action ="edit";
107       $s_entry  = $_GET['id'];
108     }
112     /* handle C&P from layers menu */
113     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
114       $s_action = "copy_multiple";
115     }
116     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
117       $s_action = "cut_multiple";
118     }
119     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
120       $s_action = "editPaste";
121     }
123     /* Create options */
124     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "group_new"){
125       $s_action = "new";
126     }
128     /* handle remove from layers menu */
129     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
130       $s_action = "del_multiple";
131     }
134     /****************
135       Copy & Paste handling 
136      ****************/
138     /* Display the copy & paste dialog, if it is currently open */
139     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
140     if($ret){
141       return($ret);
142     }
145     /***************
146       Create a new object group 
147      ****************/
149     /* New group? */
150     if ($s_action=="new"){
152       /* By default we set 'dn' to 'new', all relevant plugins will
153          react on this. */
154       $this->dn= "new";
156       /* Create new usertab object */
157       $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $this->dn,"ogroups");
158       $this->ogroup->set_acl_base($this->DivListOGroup->selectedBase);
159     }
162     /********************
163       Delete MULTIPLE entries requested, display confirm dialog
164      ********************/
165     if ($s_action=="del_multiple"){
166       $ids = $this->list_get_selected_items();
167       if(count($ids)){
168         foreach($ids as $id){
169           $dn = $this->ogrouplist[$id]['dn'];
170           if (($user= get_lock($dn)) != ""){
171             return(gen_locked_message ($user, $dn));
172           }
173           $this->dns[$id] = $dn;
174         }
175         $dns_names = "<br><pre>";
176         foreach($this->dns as $dn){
177           add_lock ($dn, $this->ui->dn);
178           $dns_names .= $dn."\n";
179         }
180         $dns_names .="</pre>";
181         /* Lock the current entry, so nobody will edit it during deletion */
182         if (count($this->dns) == 1){
183           $smarty->assign("info",     sprintf(_("You're about to delete the following object entry %s"), @LDAP::fix($dns_names)));
184         } else {
185           $smarty->assign("info",     sprintf(_("You're about to delete the following object entries %s"), @LDAP::fix($dns_names)));
186         }
187         $smarty->assign("multiple", true);
188         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
189       }
190     }
193     /********************
194       Delete MULTIPLE entries confirmed
195      ********************/
197     /* Confirmation for deletion has been passed. Users should be deleted. */
198     if (isset($_POST['delete_multiple_ogroup_confirm'])){
200       /* Remove user by user and check acls before removeing them */
201       foreach($this->dns as $key => $dn){
203         $acl = $this->ui->get_permissions($dn, "ogroups");
204         if (preg_match('/d/', $acl)){
206           /* Delete request is permitted, perform LDAP action */
207           $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $dn);
208           $this->ogroup->delete ();
209           unset ($this->ogroup);
210           $this->ogroup= NULL;
211         } else {
213           /* Normally this shouldn't be reached, send some extra
214              logs to notify the administrator */
215           print_red (_("You are not allowed to delete this object group!"));
216           new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
217         }
218         /* Remove lock file after successfull deletion */
219         del_lock ($dn);
220         unset($this->dns[$key]);
221       }
222     }
225     /********************
226       Delete MULTIPLE entries Canceled
227      ********************/
229     /* Remove lock */
230     if(isset($_POST['delete_multiple_ogroup_cancel'])){
231       foreach($this->dns as $key => $dn){
232         del_lock ($dn);
233         unset($this->dns[$key]);
234       }
235     }
238     /****************
239       Delete object group
240      ****************/
242     if ($s_action=="del"){
244       /* Get 'dn' from posted 'uid' */
245       $this->dn= $this->ogrouplist[$s_entry]['dn'];
247       /* Load permissions for selected 'dn' and check if
248          we're allowed to remove this 'dn' */
249       $acl = $this->ui->get_permissions($this->dn,"ogroups");
250       if(preg_match("/d/",$acl)){
252         /* Check locking, save current plugin in 'back_plugin', so
253            the dialog knows where to return. */
254         if (($user= get_lock($this->dn)) != ""){
255           return(gen_locked_message ($user, $this->dn));
256         }
258         /* Lock the current entry, so nobody will edit it during deletion */
259         add_lock ($this->dn, $this->ui->dn);
260         $smarty->assign("info", sprintf(_("You're about to delete the object group '%s'."), @LDAP::fix($this->dn)));
261         $smarty->assign("multiple", false);
262         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
263       } else {
265         /* Obviously the user isn't allowed to delete. Show message and
266            clean session. */
267         print_red (_("You are not allowed to delete this object group!"));
268       }
269     }
272     /****************
273       Delete confirmed 
274      ****************/
276     /* Confirmation for deletion has been passed. Group should be deleted. */
277     if (isset($_POST['delete_group_confirm'])){
279       /* Some nice guy may send this as POST, so we've to check
280          for the permissions again. */
281       $acl = $this->ui->get_permissions($this->dn,"ogroups");
282       if(preg_match("/d/",$acl)){
284         /* Delete request is permitted, perform LDAP action */
285         $this->ogroup= new ogrouptabs($this->config,
286             $this->config->data['TABS']['OGROUPTABS'], $this->dn);
287         $this->ogroup->delete ();
288         unset ($this->ogroup);
289         $this->ogroup= NULL;
290       } else {
292         /* Normally this shouldn't be reached, send some extra
293            logs to notify the administrator */
294         print_red (_("You are not allowed to delete this object group!"));
295         new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
296       }
298       /* Remove lock file after successfull deletion */
299       del_lock ($this->dn);
300       unset($_SESSION['objectinfo']);
301     }
304     /****************
305       Delete object group Canceled
306      ****************/
308     /* Delete group canceled? */
309     if (isset($_POST['delete_cancel'])){
310       del_lock ($this->dn);
311       unset($_SESSION['objectinfo']);
312     }
315     /****************
316       Edit group
317      ****************/
319     if (($s_action=="edit") && (! isset($this->ogroup->config)) ){
321       /* Get 'dn' from posted 'uid', must be unique */
322       $this->dn= $this->ogrouplist[$s_entry]['dn'];
324       /* Check locking, save current plugin in 'back_plugin', so
325          the dialog knows where to return. */
326       if (($user= get_lock($this->dn)) != ""){
327         return(gen_locked_message ($user, $this->dn));
328       }
330       /* Lock the current entry, so everyone will get the
331          above dialog */
332       add_lock ($this->dn, $this->ui->dn);
334       /* Register grouptab to trigger edit dialog */
335       $this->ogroup= new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $this->dn, "ogroups");
336       $this->ogroup->set_acl_base($this->dn);
337       $_SESSION['objectinfo']= $this->dn;
338     }
341     /****************
342       Edit finished save 
343      ****************/
345     /* Finish button has been pressed */
346     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->ogroup->config)) ){
348       /* Check tabs, will feed message array */
349       $message= $this->ogroup->check();
351       /* Save, or display error message? */
352       if (count($message) == 0){
354         /* Save user data to ldap */
355         $this->ogroup->save();
357         if (!isset($_POST['edit_apply'])){
358           /* Group has been saved successfully, remove lock from
359              LDAP. */
360           if ($this->dn != "new"){
361             del_lock ($this->dn);
362           }
364           unset ($this->ogroup);
365           $this->ogroup= NULL;
366           unset ($_SESSION['objectinfo']);
367         }
368       } else {
369         /* Ok. There seem to be errors regarding to the tab data,
370            show message and continue as usual. */
371         show_errors($message);
372       }
373     }
376     /****************
377       Cancel edit object group
378      ****************/
380     /* Cancel dialogs */
381     if ((isset($_POST['edit_cancel']))  && (isset($this->ogroup->dn))){
382       del_lock ($this->ogroup->dn);
383       unset ($this->ogroup);
384       $this->ogroup= NULL;
385       unset($_SESSION['objectinfo']);
386     }
389     /****************
390       Display open dialogs 
391      ****************/
393     /* Show dialog if object is present */
394     if (isset($this->ogroup->config)){
395       $display= $this->ogroup->execute();
397       /* Don't show buttons if tab dialog requests this */
398       if (!$this->ogroup->by_object[$this->ogroup->current]->dialog){
399         $display.= "<p style=\"text-align:right\">\n";
400         $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
401         $display.= "&nbsp;\n";
402         if ($this->dn != "new"){
403           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
404           $display.= "&nbsp;\n";
405         }
406         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
407         $display.= "</p>";
408       }
409       return ($display);
410     }
413     /****************
414       Display list 
415      ****************/
417     /* Check if there is a snapshot dialog open */
418     $base = $this->DivListOGroup->selectedBase;
419     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
420       return($str);
421     }
423     /* Display dialog with group list */
424     $this->DivListOGroup->parent = $this;
425     $this->DivListOGroup->execute();
427     /* Add departments if subsearch is disabled */
428     if(!$this->DivListOGroup->SubSearch){
429       $this->DivListOGroup->AddDepartments($this->DivListOGroup->selectedBase,4,1);
430     }
431     $this->reload ();
432     $this->DivListOGroup->setEntries($this->ogrouplist);
433     return($this->DivListOGroup->Draw());
434   }
437   /* Return departments, that will be included within snapshot detection */
438   function get_used_snapshot_bases()
439   {
440     return(array(get_groups_ou().$this->DivListOGroup->selectedBase));
441   }
443   
445   function convert_list($input)
446   {
447     $temp= "";
449     $conv= array(   
450         "U" => array("select_user.png"        ,_("User")        , "ogroup"),
451         "G" => array("select_groups.png"      ,_("Group")       , "ogroup"),
452         "A" => array("select_application.png" ,_("Application") , "ogroup"),
453         "D" => array("select_department.png"  ,_("Department")  , "ogroup"),
454         "S" => array("select_server.png"      ,_("Server")      , "ogroup"),
455         "F" => array("select_phone.png"       ,_("Phone")       , "phonequeue"),
456         "W" => array("select_workstation.png" ,_("Workstation") , "workstartup"),
457         "T" => array("select_terminal.png"    ,_("Terminal")    , "termgroup"),
458         "P" => array("select_printer.png"     ,_("Printer")     , "ogroup"));
460     /* Assemble picture */
461     $type= $input['gosaGroupObjects'][0];
462     $type= preg_replace("/[^A-Z]/i","",$type);
463     if (isset($type[0])){
464       $p1['pic']= $conv[$type[0]][0];
465       $p1['tit']= $conv[$type[0]][1];
466       $p1['alt']= $type[0];
467       $p1['lnk']= $conv[$type[0]][2];
468       $html_object_1 = "<input type='image' name='".$p1['lnk']."_group_edit_%KEY' ";
469     } else {
470       $p1['pic']= "empty.png";
471       $p1['tit']= "";
472       $p1['alt']= "";
473       $p1['lnk']= "";//$conv[$type[0]][2];
474       $html_object_1 = "<img ";
475     }
477     if (isset($type[1])){
478       $p2['pic']= $conv[$type[1]][0];
479       $p2['alt']= $type[1];
480       $p2['tit']= $conv[$type[1]][1];
481       $p2['lnk']= $conv[$type[1]][2];
482       $html_object_2 = "<input type='image' name='".$p2['lnk']."_group_edit_%KEY' ";
483     } else {
484       $p2['pic']= "empty.png";
485       $p2['alt']= "";
486       $p2['tit']= "";
487       $p2['lnk']= ""; //$conv[$type[0]][2];
488       $html_object_2 = "<img ";
489     }
490     
491     $temp = $html_object_1." class='center' id='iconA".preg_replace("/_/","",$p1['lnk']."_group_edit_%KEY")."' 
492       src='images/".$p1['pic']."' alt='".$p1['alt']."' title='".$p1['tit']."'>";
493     $temp.= $html_object_2." class='center' id='iconB".preg_replace("/_/","",$p2['lnk']."_group_edit_%KEY")."' 
494       src='images/".$p2['pic']."' alt='".$p2['alt']."' title='".$p2['tit']."'>";
495     return ($temp);
496   }
499   function reload()
500   {
501     /* Set base for all searches && initialise some vars */
502     $this->ogrouplist= array();
503     $base     = $this->DivListOGroup->selectedBase;
504     $filter   = "(gosaGroupObjects=[])";
505     $Regex    = $this->DivListOGroup->Regex;
507     $chk = array(
508         "UserGroups"          => "(gosaGroupObjects=*U*)" ,
509         "GroupGroups"         => "(gosaGroupObjects=*G*)" ,
510         "ApplicationGroups"   => "(gosaGroupObjects=*A*)" ,
511         "DepartmentGroups"    => "(gosaGroupObjects=*D*)" ,
512         "ServerGroups"        => "(gosaGroupObjects=*S*)" ,
513         "WorkstationGroups"   => "(gosaGroupObjects=*W*)" ,
514         "TerminalGroups"      => "(gosaGroupObjects=*T*)" ,
515         "PrinterGroups"       => "(gosaGroupObjects=*P*)" ,
516         "PhoneGroups"         => "(gosaGroupObjects=*F*)" );
517     /* Create filter */ 
518     foreach($chk as $chkBox => $FilterPart){
519       if($this->DivListOGroup->$chkBox){
520         $filter .=  $FilterPart;
521       }
522     }
523     $filter= "(&(cn=$Regex)(objectClass=gosaGroupOfNames)(|$filter))";
525     if($this->DivListOGroup->SubSearch){
526       $res= get_list($filter, "ogroups", $base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT | GL_SUBSEARCH);
527     }else{
528       $res= get_list($filter, "ogroups", get_groups_ou().$base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT );
529     }
531     $this->ogrouplist= $res;
532     ksort ($this->ogrouplist);
533     reset ($this->ogrouplist);
534     $tmp=array();
535     foreach($this->ogrouplist as $tkey => $val ){
536       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
537     }
538     ksort($tmp);
539     $this->ogrouplist=array();
540     foreach($tmp as $val){
541       $this->ogrouplist[]=$val;
542     }
543     reset ($this->ogrouplist);
544   }
547   function list_get_selected_items()
548   {
549     $ids = array();
550     foreach($_POST as $name => $value){
551       if(preg_match("/^item_selected_[0-9]*$/",$name)){
552         $id   = preg_replace("/^item_selected_/","",$name);
553         $ids[$id] = $id;
554       }
555     }
556     return($ids);
557   }
560   function copyPasteHandling_from_queue($s_action,$s_entry)
561   {
562     /* Check if Copy & Paste is disabled */
563     if(!is_object($this->CopyPasteHandler)){
564       return("");
565     }
567     /* Add a single entry to queue */
568     if($s_action == "cut" || $s_action == "copy"){
570       /* Cleanup object queue */
571       $this->CopyPasteHandler->cleanup_queue();
572       $dn = $this->ogrouplist[$s_entry]['dn'];
573       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"ogrouptabs","OGROUPTABS","ogroups");
574     }
577     /* Add entries to queue */
578     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
580       /* Cleanup object queue */
581       $this->CopyPasteHandler->cleanup_queue();
583       /* Add new entries to CP queue */
584       foreach($this->list_get_selected_items() as $id){
585         $dn = $this->ogrouplist[$id]['dn'];
587         if($s_action == "copy_multiple"){
588           $this->CopyPasteHandler->add_to_queue($dn,"copy","ogrouptabs","OGROUPTABS","ogroups");
589         }
590         if($s_action == "cut_multiple"){
591           $this->CopyPasteHandler->add_to_queue($dn,"cut","ogrouptabs","OGROUPTABS","ogroups");
592         }
593       }
594     }
596     /* Start pasting entries */
597     if($s_action == "editPaste"){
598       $this->start_pasting_copied_objects = TRUE;
599     }
601     /* Return C&P dialog */
602     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
604       /* Load entry from queue and set base */
605       $this->CopyPasteHandler->load_entry_from_queue();
606       $this->CopyPasteHandler->SetVar("base",$this->DivListOGroup->selectedBase);
608       /* Get dialog */
609       $data = $this->CopyPasteHandler->execute();
611       /* Return dialog data */
612       if(!empty($data)){
613         return($data);
614       }
615     }
616     /* Automatically disable status for pasting */
617     if(!$this->CopyPasteHandler->entries_queued()){
618       $this->start_pasting_copied_objects = FALSE;
619     }
620     return("");
621   }
624   function save_object()
625   {
626     $this->DivListOGroup->save_object();
627   }
631 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
632 ?>