Code

Moved plugins
[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::set('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       session::un_set('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       session::un_set('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::set('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           session::un_set('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       session::un_set('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_ou('ogroupou').$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         "O" => array("select_winstation.png" ,_("Windows Install") , "ogroup"),
458         "T" => array("select_terminal.png"    ,_("Terminal")    , "termgroup"),
459         "P" => array("select_printer.png"     ,_("Printer")     , "ogroup"));
461     /* Assemble picture */
462     $type= $input['gosaGroupObjects'][0];
463     $type= preg_replace("/[^A-Z]/i","",$type);
464     if (isset($type[0])){
465       $p1['pic']= $conv[$type[0]][0];
466       $p1['tit']= $conv[$type[0]][1];
467       $p1['alt']= $type[0];
468       $p1['lnk']= $conv[$type[0]][2];
469       $html_object_1 = "<input type='image' name='".$p1['lnk']."_group_edit_%KEY' ";
470     } else {
471       $p1['pic']= "empty.png";
472       $p1['tit']= "";
473       $p1['alt']= "";
474       $p1['lnk']= "";//$conv[$type[0]][2];
475       $html_object_1 = "<img ";
476     }
478     if (isset($type[1])){
479       $p2['pic']= $conv[$type[1]][0];
480       $p2['alt']= $type[1];
481       $p2['tit']= $conv[$type[1]][1];
482       $p2['lnk']= $conv[$type[1]][2];
483       $html_object_2 = "<input type='image' name='".$p2['lnk']."_group_edit_%KEY' ";
484     } else {
485       $p2['pic']= "empty.png";
486       $p2['alt']= "";
487       $p2['tit']= "";
488       $p2['lnk']= ""; //$conv[$type[0]][2];
489       $html_object_2 = "<img ";
490     }
491     
492     $temp = $html_object_1." class='center' id='iconA".preg_replace("/_/","",$p1['lnk']."_group_edit_%KEY")."' 
493       src='images/".$p1['pic']."' alt='".$p1['alt']."' title='".$p1['tit']."'>";
494     $temp.= $html_object_2." class='center' id='iconB".preg_replace("/_/","",$p2['lnk']."_group_edit_%KEY")."' 
495       src='images/".$p2['pic']."' alt='".$p2['alt']."' title='".$p2['tit']."'>";
496     return ($temp);
497   }
500   function reload()
501   {
502     /* Set base for all searches && initialise some vars */
503     $this->ogrouplist= array();
504     $base     = $this->DivListOGroup->selectedBase;
505     $filter   = "(gosaGroupObjects=[])";
506     $Regex    = $this->DivListOGroup->Regex;
508     $chk = array(
509         "UserGroups"          => "(gosaGroupObjects=*U*)" ,
510         "GroupGroups"         => "(gosaGroupObjects=*G*)" ,
511         "ApplicationGroups"   => "(gosaGroupObjects=*A*)" ,
512         "DepartmentGroups"    => "(gosaGroupObjects=*D*)" ,
513         "ServerGroups"        => "(gosaGroupObjects=*S*)" ,
514         "WorkstationGroups"   => "(gosaGroupObjects=*W*)" ,
515         "WindowsGroups"       => "(gosaGroupObjects=*O*)" ,
516         "TerminalGroups"      => "(gosaGroupObjects=*T*)" ,
517         "PrinterGroups"       => "(gosaGroupObjects=*P*)" ,
518         "PhoneGroups"         => "(gosaGroupObjects=*F*)" );
519     /* Create filter */ 
520     foreach($chk as $chkBox => $FilterPart){
521       if($this->DivListOGroup->$chkBox){
522         $filter .=  $FilterPart;
523       }
524     }
525     $filter= "(&(cn=$Regex)(objectClass=gosaGroupOfNames)(|$filter))";
527     if($this->DivListOGroup->SubSearch){
528       $res= get_sub_list($filter, "ogroups",get_ou('ogroupou'), $base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT | GL_SUBSEARCH);
529     }else{
530       $res= get_sub_list($filter, "ogroups",get_ou('ogroupou'), get_ou('ogroupou').$base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT );
531     }
533     $this->ogrouplist= $res;
534     ksort ($this->ogrouplist);
535     reset ($this->ogrouplist);
536     $tmp=array();
537     $tmp2 = array();
538     foreach($this->ogrouplist as $tkey => $val ){
539       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
540       $tmp2[strtolower($val['cn'][0]).$val['cn'][0]] = strtolower($val['cn'][0]).$val['cn'][0];
541     }
542     natcasesort($tmp2);
543     $this->ogrouplist=array();
544     foreach($tmp2 as $val){
545       $this->ogrouplist[]=$tmp[$val];
546     }
547     reset ($this->ogrouplist);
548   }
551   function list_get_selected_items()
552   {
553     $ids = array();
554     foreach($_POST as $name => $value){
555       if(preg_match("/^item_selected_[0-9]*$/",$name)){
556         $id   = preg_replace("/^item_selected_/","",$name);
557         $ids[$id] = $id;
558       }
559     }
560     return($ids);
561   }
564   function copyPasteHandling_from_queue($s_action,$s_entry)
565   {
566     /* Check if Copy & Paste is disabled */
567     if(!is_object($this->CopyPasteHandler)){
568       return("");
569     }
571     /* Add a single entry to queue */
572     if($s_action == "cut" || $s_action == "copy"){
574       /* Cleanup object queue */
575       $this->CopyPasteHandler->cleanup_queue();
576       $dn = $this->ogrouplist[$s_entry]['dn'];
577       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"ogrouptabs","OGROUPTABS","ogroups");
578     }
581     /* Add entries to queue */
582     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
584       /* Cleanup object queue */
585       $this->CopyPasteHandler->cleanup_queue();
587       /* Add new entries to CP queue */
588       foreach($this->list_get_selected_items() as $id){
589         $dn = $this->ogrouplist[$id]['dn'];
591         if($s_action == "copy_multiple"){
592           $this->CopyPasteHandler->add_to_queue($dn,"copy","ogrouptabs","OGROUPTABS","ogroups");
593         }
594         if($s_action == "cut_multiple"){
595           $this->CopyPasteHandler->add_to_queue($dn,"cut","ogrouptabs","OGROUPTABS","ogroups");
596         }
597       }
598     }
600     /* Start pasting entries */
601     if($s_action == "editPaste"){
602       $this->start_pasting_copied_objects = TRUE;
603     }
605     /* Return C&P dialog */
606     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
608       /* Load entry from queue and set base */
609       $this->CopyPasteHandler->load_entry_from_queue();
610       $this->CopyPasteHandler->SetVar("base",$this->DivListOGroup->selectedBase);
612       /* Get dialog */
613       $data = $this->CopyPasteHandler->execute();
615       /* Return dialog data */
616       if(!empty($data)){
617         return($data);
618       }
619     }
620     /* Automatically disable status for pasting */
621     if(!$this->CopyPasteHandler->entries_queued()){
622       $this->start_pasting_copied_objects = FALSE;
623     }
624     return("");
625   }
628   function save_object()
629   {
630     $this->DivListOGroup->save_object();
631   }
635 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
636 ?>