Code

Updated copy & paste acls, centralized checks
[gosa.git] / gosa-plugins / gofon / gofon / macro / class_gofonMacroManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  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 goFonMacro extends plugin
22 {
23   /* Definitions */
24   var $plHeadline                   = "Phone macros";
25   var $plDescription      = "This does something";
27   /* Dialog attributes */
28   var $macrotabs                    = NULL;
29   var $macros                   = array();
30   var $ui                                       = NULL;
31   var $DivListMacro     = NULL;
33   var $CopyPasteHandler = NULL;
34   var $start_pasting_copied_objects = FALSE;
36   var $dns              = array();
38   function gofonMacro(&$config, $ui)
39   {
40     /* Save configuration for internal use */
41     $this->config= $config;
42     $this->ui= $ui;
44     $this->DivListMacro = new divListMacro($this->config,$this);
46     /* Copy & Paste enabled ?*/
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     }
50   }
52   function execute()
53   {
54     /* Call parent execute */
55     plugin::execute();
57     session::set('LOCK_VARS_TO_USE',array("/^menu_action/","/^goFonMacro_/","/^act$/","/^id$/","/^item_selected/","/^remove_multiple_macros/"));
59     /*****************
60       Variable initialisation
61      *****************/
63     $s_action     = "";                       // Contains the action to proceed
64     $s_entry      = "";                       // The value for s_action
65     $base_back    = "";                       // The Link for Backbutton
66     $smarty= get_smarty();
69     /*****************
70       Check Posts 
71      *****************/
73     /* Test Posts */
74     foreach($_POST as $key => $val){
75       // Post for delete
76       if(preg_match("/^goFonMacro_del/",$key)){
77         $s_action = "del";
78         $s_entry  = preg_replace("/^goFonMacro_del_/i","",$key);
79         // Post for edit
80       }elseif(preg_match("/^goFonMacro_edit_/",$key)){
81         $s_action="edit";
82         $s_entry  = preg_replace("/^goFonMacro_edit_/i","",$key);
83         // Post for new
84       }elseif(preg_match("/^goFonMacro_new/",$key)){
85         $s_action="new";
86       }elseif(preg_match("/^remove_multiple_macros/",$key)){
87         $s_action="del_multiple";
88       }elseif(preg_match("/^editPaste.*/i",$key)){
89         $s_action="editPaste";
90       }elseif(preg_match("/^copy_.*/",$key)){
91         $s_action="copy";
92         $s_entry  = preg_replace("/^copy_/i","",$key);
93       }elseif(preg_match("/^cut_.*/",$key)){
94         $s_action="cut";
95         $s_entry  = preg_replace("/^cut_/i","",$key);
96       }elseif(preg_match("/^multiple_copy_objects/",$key)){
97         $s_action = "copy_multiple";
98       }elseif(preg_match("/^multiple_cut_objects/",$key)){
99         $s_action = "cut_multiple";
100       }
101     }
102     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
103       $s_action ="edit";
104       $s_entry  = $_GET['id'];
105     }
106     $s_entry  = preg_replace("/_.$/","",$s_entry);
109     /********************
110       Copy & Paste Handling  ...
111      ********************/
113     
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'] == "goFonMacro_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     /* Display the copy & paste dialog, if it is currently open */
136     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
137     if($ret){
138       return($ret);
139     }
142     /*****************
143       Create a new Macro 
144      *****************/
146     /* New macro? */
147     if ($s_action=="new"){
149       /* By default we set 'dn' to 'new', all relevant plugins will
150          react on this. */
151       $this->dn= "new";
153       /* Create new usertab object */
154       $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
155       $this->macrotabs->set_acl_base($this->DivListMacro->selectedBase);
156     }
158     /*****************
159       Edit || Password canceled  
160      *****************/
162     /* Cancel dialogs */
163     if (isset($_POST['edit_cancel'])){
164       $this->remove_lock();
165       unset ($this->macrotabs);
166       $this->macrotabs= NULL;
167       session::un_set('objectinfo');
168     }
171     /*****************
172       Edit finised
173      *****************/
175     /* Finish mac edit is triggered by the tabulator dialog, so
176        the user wants to save edited data. Check and save at this
177        point. */
178     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->macrotabs->config))){
180       /* Check tabs, will feed message array */
181       $this->macrotabs->last= $this->macrotabs->current;
182       $this->macrotabs->save_object();
183       $message= $this->macrotabs->check();
185       /* Save, or display error message? */
186       if (count($message) == 0){
188         /* Save data data to ldap */
189         $this->macrotabs->save();
191         if (!isset($_POST['edit_apply'])){
192           /* macro has been saved successfully, remove lock from
193              LDAP. */
194           if ($this->dn != "new"){
195             $this->remove_lock();
196           }
198           unset ($this->macrotabs);
199           $this->macrotabs= NULL;
200           session::un_set('objectinfo');
201         }else{
203           /* Reinitialize tab */
204           if($this->macrotabs instanceof tabs){
205             $this->macrotabs->re_init();
206           }
207         }
208       } else {
209         /* Ok. There seem to be errors regarding to the tab data,
210            show message and continue as usual. */
211         msg_dialog::displayChecks($message);
212       }
213     }
216     /*****************
217       Edit macro 
218      *****************/
220     /* User wants to edit data */
221     if (($s_action=="edit") && (!isset($this->macrotabs->config))){
223       $dn  = $this->macros[$s_entry]['dn'];
224       $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
225       if(preg_match("/r/",$acl)){
226         $this->dn = $dn;
228         /* Check locking, save current plugin in 'back_plugin', so
229            the dialog knows where to return. */
230         if (($user= get_lock($this->dn)) != ""){
231           return(gen_locked_message ($user, $this->dn));
232         }
234         /* Lock the current entry, so everyone will get the
235            above dialog */
236         add_lock ($this->dn, $this->ui->dn);
238         /* Register macrotabs to trigger edit dialog */
239         $this->macrotabs= new macrotabs($this->config,
240             $this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
241         session::set('objectinfo',$this->dn);
242       }
243     }
247     /********************
248       Delete MULTIPLE entries requested, display confirm dialog
249      ********************/
251     if ($s_action=="del_multiple"){
252       $ids = $this->list_get_selected_items();
255       $this->dns = array();
256       if(count($ids)){
257         $disallowed = array();
258         foreach($ids as $id){
259           $dn = $this->macros[$id]['dn'];
260           $acl = $this->ui->get_permissions($dn, "gofonmacro/macro");
261           if(preg_match("/d/",$acl)){
262             $this->dns[$id] = $dn;
263           }else{
264             $disallowed[] = $dn;
265           }
266         }
268         if(count($disallowed)){
269           msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
270         }
272         if(count($this->dns)){
274           /* Check locks */
275           if ($user= get_multiple_locks($this->dns)){
276             return(gen_locked_message($user,$this->dns));
277           }
279           $dns_names = array();
280           foreach($this->dns as $dn){
281             $dns_names[] = @LDAP::fix($dn);
282           }
284           /* Add entry locks */
285           add_lock ($this->dns, $this->ui->dn);
287           /* Lock the current entry, so nobody will edit it during deletion */
288           $smarty->assign("intro", msgPool::deleteInfo($dns_names,("macro")));
289           $smarty->assign("multiple", true);
290           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
291         }
292       }
293     }
296     /********************
297       Delete MULTIPLE entries confirmed
298      ********************/
300     /* Confirmation for deletion has been passed. Users should be deleted. */
301     if (isset($_POST['delete_multiple_macro_confirm'])){
303       /* Remove user by user and check acls before removeing them */
304       foreach($this->dns as $key => $dn){
306         $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
307         if(preg_match("/d/",$acl)){
309           /* Delete request is permitted, perform LDAP action */
310           $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $dn,"gofonmacro");
311           $this->macrotabs->delete ();
312           unset ($this->macrotabs);
313           $this->macrotabs= NULL;
315         } else {
316           msg_dialog::display(_("Permission error"),msgPool::permDelete(), ERROR_DIALOG);
317         }
319       }
321       /* Remove lock file after successfull deletion */
322       $this->remove_lock();
323       $this->dns = array();
324     }
327     /********************
328       Delete MULTIPLE entries Canceled
329      ********************/
331     /* Remove lock */
332     if(isset($_POST['delete_multiple_macro_cancel'])){
333       $this->remove_lock();
334       $this->dns = array();
335     }
338     /*****************
339       Remove macro
340      *****************/
342     /* Remove user was requested */
343     if ($s_action=="del"){
345       /* Get 'dn' from posted 'uid' */
346       $dn  = $this->macros[$s_entry]['dn'];
347       $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
349       if(preg_match("/d/",$acl)){
351         $this->dn = $dn;
352   
353         /* Check locking, save current plugin in 'back_plugin', so
354            the dialog knows where to return. */
355         if (($user= get_lock($this->dn)) != ""){
356           return (gen_locked_message ($user, $this->dn));
357         }
359         /* Lock the current entry, so nobody will edit it during deletion */
360         add_lock ($this->dn, $this->ui->dn);
361         $smarty= get_smarty();
362         $smarty->assign("intro", msgPool::deleteInfo(@LDAP::fix($this->dn),_("macro")));
363         $smarty->assign("multiple", false);
364         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
365       } else {
367         /* Obviously the user isn't allowed to delete. Show message and
368            clean session. */
369         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
370       }
371     }
374     /*****************
375       Remove macro
376      *****************/
378     /* Confirmation for deletion has been passed. Macro should be deleted. */
379     if (isset($_POST['delete_macro_confirm'])){
381       $acl = $this->ui->get_permissions($this->dn,"gofonmacro/macro");
383       if(preg_match("/r/",$acl)){
385         /* Delete request is permitted, perform LDAP action */
386         $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
387         $this->macrotabs->delete ();
388         unset ($this->macrotabs);
389         $this->macrotabs= NULL;
390       } else {
391         /* Normally this shouldn't be reached, send some extra
392            logs to notify the administrator */
393         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
394         new log("security","gofonmacro/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
395       }
397       /* Remove lock file after successfull deletion */
398       $this->remove_lock();
399     }
402     /*****************
403       Display open dialogs  
404      *****************/
406     /* Show tab dialog if object is present */
407     if (($this->macrotabs) && (isset($this->macrotabs->config))){
408       $display= $this->macrotabs->execute();
410       /* Don't show buttons if tab dialog requests this */
411       if (!$this->macrotabs->by_object[$this->macrotabs->current]->dialog){
412         $display.= "<p style=\"text-align:right\">\n";
413         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
414         $display.= "&nbsp;\n";
415         if ($this->dn != "new"){
416           $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
417           $display.= "&nbsp;\n";
418         }
419         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
420         $display.= "</p>";
421       }
422       return ($display);
423     }
426     /*****************
427       Display entry list 
428      *****************/
430     /* Check if there is a snapshot dialog open */
431     $base = $this->DivListMacro->selectedBase;
432     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
433       return($str);
434     }
436     /* Return rendered main page */
437     /* Display dialog with system list */
438     $this->DivListMacro->parent = $this;
439     $this->DivListMacro->execute();
441     /* Add departments if subsearch is disabled */
442     if(!$this->DivListMacro->SubSearch){
443       $this->DivListMacro->AddDepartments($this->DivListMacro->selectedBase,4,1);
444     }
445     $this->reload();
446     $this->DivListMacro->setEntries($this->macros);
447     return($this->DivListMacro->Draw());
448   }
451   function copyPasteHandling_from_queue($s_action,$s_entry)
452   {
453     /* Check if Copy & Paste is disabled */
454     if(!is_object($this->CopyPasteHandler)){
455       return("");
456     }
458     $ui = get_userinfo();
460     /* Add a single entry to queue */
461     if($s_action == "cut" || $s_action == "copy"){
463       /* Cleanup object queue */
464       $this->CopyPasteHandler->cleanup_queue();
465       $dn = $this->macros[$s_entry]['dn'];
467       if($s_action == "copy" && $ui->is_copyable($dn,"gofonmacro","macro")){ 
468         $this->CopyPasteHandler->add_to_queue($dn,$s_action,"macrotabs","MACROTABS","gofonmacro");
469       }
470       if($s_action == "cut" && $ui->is_cutable($dn,"gofonmacro","macro")){
471         $this->CopyPasteHandler->add_to_queue($dn,$s_action,"macrotabs","MACROTABS","gofonmacro");
472       }
473     }
475     /* Add entries to queue */
476     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
478       /* Cleanup object queue */
479       $this->CopyPasteHandler->cleanup_queue();
481       /* Add new entries to CP queue */
482       foreach($this->list_get_selected_items() as $id){
483         $dn = $this->macros[$id]['dn'];
485         if($s_action == "copy_multiple" && $ui->is_copyable($dn,"gofonmacro","macro")){ 
486           $this->CopyPasteHandler->add_to_queue($dn,"copy","macrotabs","MACROTABS","gofonmacro");
487         }
488         if($s_action == "cut_multiple" && $ui->is_cutable($dn,"gofonmacro","macro")){
489           $this->CopyPasteHandler->add_to_queue($dn,"cut","macrotabs","MACROTABS","gofonmacro");
490         }
491       }
492     }
494     /* Start pasting entries */
495     if($s_action == "editPaste"){
496       $this->start_pasting_copied_objects = TRUE;
497     }
499     /* Return C&P dialog */
500     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
502       /* Get dialog */
503       $this->CopyPasteHandler->SetVar("base",$this->DivListMacro->selectedBase);
504       $data = $this->CopyPasteHandler->execute();
506       /* Return dialog data */
507       if(!empty($data)){
508         return($data);
509       }
510     }
512     /* Automatically disable status for pasting */
513     if(!$this->CopyPasteHandler->entries_queued()){
514       $this->start_pasting_copied_objects = FALSE;
515     }
516     return("");
517   }
521   /* Return departments, that will be included within snapshot detection */
522   function get_used_snapshot_bases()
523   {
524     return(array(get_ou('macroou').$this->DivListMacro->selectedBase));
525   }
528   function reload()
529   {
530     /* Set base for all searches */
531     $base       = $this->DivListMacro->selectedBase;
532     $SubSearch  = $this->DivListMacro->SubSearch;
533     $Regex      = $this->DivListMacro->Regex;
534     $Filter     = "(&(cn=".$Regex.")(objectClass=goFonMacro))";
535     $Flags      = GL_SIZELIMIT;
536     $Attrs      = array("cn","description","displayName","goFonMacroVisible");
538     /* Prepare for ls or search*/        
539     if($SubSearch){
540       $Flags |= GL_SUBSEARCH;
541     }else{
542       $base = get_ou('macroou').$base;
543     }
545     /* Generate macro list */
546     $res= get_list($Filter, "gofonmacro", $base, $Attrs, $Flags);
548     $tmp=array();
549     foreach($res as $tkey => $val ){
550       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
551     }
552     ksort($tmp);
553     $this->macros=array();
554     foreach($tmp as $val){
555       $this->macros[]=$val;
556     }
558     reset ($this->macros);
559   }
562   /* Save data to object */
563   function save_object()
564   {
565     $this->DivListMacro->save_object();
566     if(is_object($this->CopyPasteHandler)){
567       $this->CopyPasteHandler->save_object();
568     }
569   }
572   function list_get_selected_items()
573   {
574     $ids = array();
575     foreach($_POST as $name => $value){
576       if(preg_match("/^item_selected_[0-9]*$/",$name)){
577         $id   = preg_replace("/^item_selected_/","",$name);
578         $ids[$id] = $id;
579       }
580     }
581     return($ids);
582   }
584   /*! \brief Remove entry locks if the plugin was aborted. 
585    */
586   function remove_lock()
587   {
588     if($this->dn) del_lock($this->dn);
589     if(is_array($this->dns) && count($this->dns)) del_lock($this->dns);
590   }
592   function adapt_from_template($dn, $skip= array())  { }
593   function check() { }
595 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
596 ?>