Code

Closes #309 - The Copy&Paste directory (LDAP_DUMP_PATH) will now only be readable...
[gosa.git] / gosa-core / include / class_CopyPasteHandler.inc
1 <?php
3 define("LDAP_DUMP_PATH","/tmp/gosa");
5 class CopyPasteHandler {
7   var $config;
8   var $current;
10   /* This array contains all dns of the currently copyied objects */
11   var $queue       = array(); 
13   /* Attributes that should be overwritten */
14   var $setvar_array= array();
16   /* The dn of the last edited object */
17   var $lastdn      = "";
20   /* Create CP handler  */
21   function CopyPasteHandler(&$config)
22   {
23     $this->config = &$config;   
24     $this->current= NULL;
25     $this->queue  = array();
26     $this->setvar_array = array();
27   }
30   /* Entry entry to Copy & Paste queue.
31    * A Queue entry is represented as follows.
32    *  array['file_name']  - Position on hdd 
33    *  array['method']     - copy/cut
34    *  array['dn']         - the dn of the object added to the queue 
35    *  array['tab_class']  - Tab object that should be used to initialize the new object
36    *  array['tab_object'] - Tab object name used to initialize correct object Type like USERTABS
37    */
38   function add_to_queue($dn,$action,$tab_class,$tab_object,$tab_acl_category)
39   {
40     if(!class_exists($tab_class)){
41       trigger_error(sprintf("Specified class object '%s' does not exists.",$tab_class));
42       return(FALSE);
43     }
45     if(!isset($this->config->data['TABS'][$tab_object])){
46       trigger_error(sprintf("Specified tab object '%s' does not exists.",$tab_object));
47       return(FALSE);
48     }
50     if(!in_array($action,array("cut","copy"))){
51       trigger_error(sprintf("Specified action '%s' does not exists for copy & paste.",$action));
52       return(FALSE);
53     } 
55     if($file_name = $this->save_dn_attributes_to_hdd($dn)){
56       $tmp = array();
57       $tmp['file_name'] = $file_name;
58       $tmp['method']    = $action;  
59       $tmp['dn']        = $dn;
60       $tmp['tab_class'] = $tab_class;
61       $tmp['tab_object']= $tab_object;
62       $tmp['tab_acl_category']= $tab_acl_category;
63       $this->queue[]    = $tmp; 
64     }
65   }
68   /* This removes all objects from queue.
69    * Remove hdd dumps of current entries too.
70    * Remove entries older than 24 hours.
71    */
72   function cleanup_queue()
73   {
74     $this->current = FALSE;
75     $this->setvar_array = array();
77     /* Remove all entries from queue */  
78     foreach($this->queue as $key => $entry){
79       @rmdir($entry['file_name']);  
80       unset($this->queue[$key]);
81     }
83     /* Create patch if it doesn't exists */
84     if(!is_dir(LDAP_DUMP_PATH)){
85       @mkdir(LDAP_DUMP_PATH);
86       @chmod(LDAP_DUMP_PATH,0700);
87     }    
88     
89     /* check if we are able to create a new file the given directory */
90     if(!is_writeable(LDAP_DUMP_PATH)){
91       $msg= sprintf(_("Cannot cleanup copy & paste queue: no write permission inside '%s'!"),LDAP_DUMP_PATH);
92       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
93       new log("copy","all/all","copy & paste, event queue.",array(), $msg);
94       return(FALSE);
95     }
97     /* Remove entries from hdd that are older than24 hours */
98     $fp = opendir(LDAP_DUMP_PATH);
99     while($file = readdir($fp)){
100       if(is_file(LDAP_DUMP_PATH."/".$file) && !preg_match("/^\./",$file)){
101         $file_time = fileatime(LDAP_DUMP_PATH."/".$file);
102         if($file_time < (time() - (24* 60 *60))){
103           @unlink(LDAP_DUMP_PATH."/".$file);
104         }
105       }
106     }
107   }
110   /* To increase performance we save the ldap dump on hdd 
111    * This function automatically creates the dumps and returns 
112    *  the name of the dumpfile we created 
113    */
114   function save_dn_attributes_to_hdd($dn)
115   {
116     $filename = "Should not be returned";
117     $ldap = $this->config->get_ldap_link();
118     $ldap->cd($this->config->current['BASE']);
119     $res  = $ldap->cat($dn);
121     /* Check if given dn is valid and ldap search was succesfull */ 
122     if(!$res){
123       $msg= sprintf(_("Copy and paste failed: object '%s' is not a valid ldap object!"), LDAP::fix($dn));
124       msg_dialog::display(_("Internal error"), $msg, ERROR_DIALOG);
125       new log("copy","all/all",$dn,array(), $msg);
126       return(FALSE);
127     }
129     /* Create data to save given ldap dump on the hdd */
130     $filename = "gosa_copy-paste_dump_".preg_replace("/[^0-9]/","",microtime());
131     $path     = LDAP_DUMP_PATH;
133     /* Create patch if it doesn't exists */
134     if(!is_dir($path)){
135       @mkdir($path);
136       @chmod($path,0700);
137     }    
139     /* check if we are able to create a new file the given directory */
140     if(!is_writeable($path)){
141       $msg= sprintf(_("Cannot save LDAP dump: no write permission inside '%s'!"),LDAP_DUMP_PATH);
142       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
143       new log("copy","all/all",$dn,array(), $msg);
144       return(FALSE);
145     }  
147     /* Create file handle */
148     $fp = @fopen($path."/".$filename,"w+");
149     if(!$fp){
150       $msg= sprintf(_("Cannot save LDAP dump: no write permission to '%s/%s'!"),$path,$filename);
151       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
152       new log("copy","all/all",$dn,array(), $msg);
153       return(FALSE);
154     }    
156     $data = serialize($ldap->fetch());
157     fwrite($fp,$data,strlen($data));
158     fclose($fp);
160     /* Only the webserver should be able to read those files */
161     @chmod($path."/".$filename,0600); 
162     return($path."/".$filename);
163   }
166   /* Check if there are still entries the object queue */
167   function entries_queued()
168   {
169     return( count($this->queue) >=1 || $this->current != FALSE);
170   }
173   /* Paste one entry from queue */
174   function load_entry_from_queue()
175   {
177     /* Save posted variables, handle dialog posts like 'cancel' */
178     $this->save_object();
180     /* If there is currently no object pasted 
181      *  create new object and prepare object to be pasted
182      */
183     if(!$this->current && $this->entries_queued()){
184       $key    = key($this->queue);
185       $entry  = $this->queue[$key];
186       $tab_c = $entry['tab_class'];
187       $tab_o = $entry['tab_object'];
188       $tab_a = $entry['tab_acl_category'];
190       if($entry['method'] == "copy"){
191         $entry['object']      = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],"new",$tab_a);
192       }else{
193         $entry['object']      = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],$entry['dn'],$tab_a);
194       }
196       $entry['source_data'] = $this->load_attributes_from_hdd($entry['file_name']);
198       if($entry['method'] == "copy"){
200         /* Prepare each plugin of this tab object to be posted */
201         foreach($entry['object']->by_object as $name => $obj){
203           /* Prepare every single class, to be copied  */
204           $entry['object']->by_object[$name]->PrepareForCopyPaste($entry['source_data']);
206           /* handle some special vars */
207           foreach(array("is_account") as $attr){
208             if(isset($entry['source_data'][$attr])){
209               $entry['object']->by_object[$name]->$attr = $entry['source_data'][$attr];
210             }
211           }
212         }
213       }
215       /* Assign created object as current */
216       $this->current = $entry;
217       unset($this->queue[$key]);
218     }
219   }
222   /* Load dumped ldap entry specified by $filename and 
223    *  return data an unserailized data array
224    */
225   function load_attributes_from_hdd($filename)
226   {
227     $fp = @fopen($filename,"r");
228     if(is_file($filename) && is_readable($filename) && $fp){
229       $data = "";
230       while($str = fgets($fp,512)){
231         $data .= $str;
232       }
233       return(unserialize($data));
234     }else{
235       $msg= sprintf(_("Cannot load dumped file '%s'!"),$filename);
236       msg_dialog::display(_("Internal error"), $msg, ERROR_DIALOG);
237       new log("copy","all/all",$dn,array(), $msg);
238       return(FALSE);
239     }
240   }
243   /* Displays a dialog which allows the user to fix all dependencies of this object.
244      Create unique names, ids, or what ever */
245   function execute()
246   {
247     $ui = get_userinfo();
248     $type = $this->current['method'];
249     if($type == "cut"){
250       if(isset($_POST['PerformCopyPaste'])){
251         while($this->entries_queued()){
252           $this->load_entry_from_queue();      
253           $this->_update_vars();
255           $msgs = $this->check();
256           $acl = $ui->get_category_permissions($this->current['dn'], $this->current['tab_acl_category']);
257  
258           /* Check permissions */ 
259           if(!preg_match("/((c|w)|(w|c))/",$acl)){
260             msg_dialog::display(_("Error"), sprintf(_("You have no permission to save object '%s'."), $this->current['dn']), ERROR_DIALOG);
261           }elseif(count ($msgs) ){
262             foreach( $msgs as $msg){
263               msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
264             }
265           }else{
267             /* Load next queue entry */
268             $this->lastdn = $this->current['object']->dn;
269             $this->current['object']->save();
270           }
272           $this->current =FALSE;
273         }
274       }
275       if($this->current){
277         $dns = $this->current['dn']."\n";
278         foreach($this->queue as $entry){
279           $dns .= $entry['dn']."\n";
280         }
282         $smarty = get_smarty();
283         $smarty->assign("type","cut");
284         $smarty->assign("Complete",false);
285         $smarty->assign("AttributesToFix","&nbsp;");
286         $smarty->assign("SubDialog",$this->current['object']->SubDialog);
287         $smarty->assign("objectDN"     ,$dns);
288         $smarty->assign("message", sprintf(_("You are going to paste the cutted entry '%s'."), "<pre>".$dns."</pre>"));
289         return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
290       }
291     }
292     if($type == "copy"){
293       if(isset($_POST['PerformCopyPaste'])){
294         $this->_update_vars();
295         $msgs = $this->check();
297         $acl = $ui->get_category_permissions($this->current['dn'], $this->current['tab_acl_category']);
298  
299         /* Check permissions */ 
300         if(!preg_match("/((c|w)|(w|c))/",$acl)){
301           msg_dialog::display(_("Error"), sprintf(_("You have no permission to copy and paste object '%s'!"),$this->current['dn']), ERROR_DIALOG);
302         }elseif(count ($msgs) ){
303           foreach( $msgs as $msg){
304             msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
305           }
306         }else{
307           $this->current['object']->save();
308           $this->lastdn = $this->current['object']->dn;
309           $this->current =FALSE;
311           /* Load next queue entry */
312           $this->load_entry_from_queue();
313         }
314       }
315       if($this->current){
316         $smarty = get_smarty();
317         $smarty->assign("type","copy");
318         $smarty->assign("Complete",false);
319         $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
320         $smarty->assign("SubDialog",$this->current['object']->SubDialog);
321         $smarty->assign("objectDN"     ,$this->current['source_data']['dn']);
322         $smarty->assign("message", sprintf(_("You are going to paste the copied entry '%s'."), $this->current['source_data']['dn']));
323         return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
324       }
325     }
326   }
329   /* Return the dn of the last edited entry */
330   function last_entry()
331   {
332     return($this->lastdn);
333   }
336   /* Save new values posted by copy & paste dialog */
337   function save_object()
338   {
339     if(isset($_POST['abort_current_cut-copy_operation'])){
340       $this->current = FALSE;
341     }
343     if(isset($_POST['abort_all_cut-copy_operations'])){
344       $this->cleanup_queue();
345       $this->current = FALSE;
346     }
348     /* Assign posted var to all tabs
349      */
350     if($this->current){
351       $this->current['object']->saveCopyDialog();
352     }
353   }
356   /* Create dialog which asks unique attributes/values ... 
357    *  call tabs -> getCopyDialog() 
358    *    which calls tab -> getCopyDialog()  */
359   function generateAttributesToFix()
360   {
361     if($this->current){
362       return($this->current['object']->getCopyDialog());  
363     }
364   }
367   /* Set a single attribute to specified value
368    *  example :   ("base", $newBase );    */
369   function SetVar($name,$value)
370   {
371     $this->setvar_array[$name]=$value; 
372   }
375   /* Update current object attributes, collected via SetVar */
376   function _update_vars()
377   {
378     if($this->current){
380       /* Update all attributes specified with SetVar */
381       foreach($this->setvar_array as $name => $value){
382         if(isset($this->current['object']->$name)){
383           $this->current['object']->$name = $value;
384         }
385       }
387       /* Walk through tabs */
388       foreach($this->current['object']->by_object as $key => $obj){
390         /* Update all attributes specified with SetVar */
391         foreach($this->setvar_array as $name => $value){
392           if(isset($this->current['object']->by_object[$key]->$name)){
393             $this->current['object']->by_object[$key]->$name = $value;
394           }
395         }
396       }
397     }
398   }
401   /* Returns errors from including tabs. */
402   function check()
403   {
404     $ret = array();
405     foreach($this->current['object']->by_object as $obj){
406       if($obj->is_account || $obj->ignore_account){
407         $ret = array_merge($ret , $obj->check());
408       }
409     }
410     return($ret);
411   }
414   /* returns the paste icon for headpages */ 
415   function generatePasteIcon()
416   {
417     $Copy_Paste= "&nbsp;<img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
418     if($this->entries_queued()){
419       $img= "images/copypaste.png";
420       $Copy_Paste.= "<input type='image' name='editPaste' class='center'
421         src='".$img."' alt='"._("Paste")."'>&nbsp;";
422     }else{
423       $Copy_Paste.= "<img class='center' src='images/cant_editpaste.png' alt=\""._("Can't paste")."\">&nbsp;";
424     }
425     return ($Copy_Paste);
426   }
428 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
429 ?>