Code

Udapted Copy & Paste messages
[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      = "";
18   
19   var $disallowed_objects = array();
20   var $objects_to_fix     = array();
21   var $clean_objects      = array();
22   var $require_update     = FALSE;
26   /* Create CP handler  */
27   function CopyPasteHandler(&$config)
28   {
29     $this->config = &$config;   
30     $this->current= NULL;
31     $this->queue  = array();
32     $this->setvar_array = array();
33   }
36   /* Entry entry to Copy & Paste queue.
37    * A Queue entry is represented as follows.
38    *  array['file_name']  - Position on hdd 
39    *  array['method']     - copy/cut
40    *  array['dn']         - the dn of the object added to the queue 
41    *  array['tab_class']  - Tab object that should be used to initialize the new object
42    *  array['tab_object'] - Tab object name used to initialize correct object Type like USERTABS
43    */
44   function add_to_queue($dn,$action,$tab_class,$tab_object,$tab_acl_category)
45   {
46     if(!class_exists($tab_class)){
47       trigger_error(sprintf("Specified class object '%s' does not exists.",$tab_class));
48       return(FALSE);
49     }
51     if(!isset($this->config->data['TABS'][$tab_object])){
52       trigger_error(sprintf("Specified tab object '%s' does not exists.",$tab_object));
53       return(FALSE);
54     }
56     if(!in_array($action,array("cut","copy"))){
57       trigger_error(sprintf("Specified action '%s' does not exists for copy & paste.",$action));
58       return(FALSE);
59     } 
61     if($file_name = $this->save_dn_attributes_to_hdd($dn)){
62       $tmp = array();
63       $tmp['file_name'] = $file_name;
64       $tmp['method']    = $action;  
65       $tmp['dn']        = $dn;
66       $tmp['tab_class'] = $tab_class;
67       $tmp['tab_object']= $tab_object;
68       $tmp['tab_acl_category']= $tab_acl_category;
69       $this->queue[]    = $tmp; 
70       $this->require_update = TRUE;
71     }
72   }
75   /* This removes all objects from queue.
76    * Remove hdd dumps of current entries too.
77    * Remove entries older than 24 hours.
78    */
79   function cleanup_queue()
80   {
81     $this->current = FALSE;
82     $this->require_update = TRUE;
83     $this->setvar_array = array();
85     /* Remove all entries from queue */  
86     foreach($this->queue as $key => $entry){
87       @rmdir($entry['file_name']);  
88       unset($this->queue[$key]);
89     }
91     /* Create patch if it doesn't exists */
92     if(!is_dir(LDAP_DUMP_PATH)){
93       @mkdir(LDAP_DUMP_PATH);
94     }    
95   
96     /* Update folder permissions */
97     if(!@chmod(LDAP_DUMP_PATH,0700)){
98       $msg= sprintf(_("Cannot cleanup copy & paste queue: setting permissions for '%s' failed!"),LDAP_DUMP_PATH);
99       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
100       new log("copy","all/all","copy & paste, event queue.",array(), $msg);
101       return(FALSE);
102     }
103     
104     /* check if we are able to create a new file the given directory */
105     if(!is_writeable(LDAP_DUMP_PATH)){
106       $msg= sprintf(_("Cannot cleanup copy & paste queue: no write permission inside '%s'!"),LDAP_DUMP_PATH);
107       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
108       new log("copy","all/all","copy & paste, event queue.",array(), $msg);
109       return(FALSE);
110     }
112     /* Remove entries from hdd that are older than24 hours */
113     $fp = opendir(LDAP_DUMP_PATH);
114     while($file = readdir($fp)){
115       if(is_file(LDAP_DUMP_PATH."/".$file) && !preg_match("/^\./",$file)){
116         $file_time = fileatime(LDAP_DUMP_PATH."/".$file);
117         if($file_time < (time() - (24* 60 *60))){
118           @unlink(LDAP_DUMP_PATH."/".$file);
119         }
120       }
121     }
122   }
125   /* To increase performance we save the ldap dump on hdd 
126    * This function automatically creates the dumps and returns 
127    *  the name of the dumpfile we created 
128    */
129   function save_dn_attributes_to_hdd($dn)
130   {
131     $filename = "Should not be returned";
132     $ldap = $this->config->get_ldap_link();
133     $ldap->cd($this->config->current['BASE']);
134     $res  = $ldap->cat($dn);
136     /* Check if given dn is valid and ldap search was succesfull */ 
137     if(!$res){
138       $msg= sprintf(_("Copy and paste failed: object '%s' is not a valid ldap object!"), LDAP::fix($dn));
139       msg_dialog::display(_("Internal error"), $msg, ERROR_DIALOG);
140       new log("copy","all/all",$dn,array(), $msg);
141       return(FALSE);
142     }
144     /* Create data to save given ldap dump on the hdd */
145     $filename = "gosa_copy-paste_dump_".preg_replace("/[^0-9]/","",microtime());
146     $path     = LDAP_DUMP_PATH;
148     /* Create patch if it doesn't exists */
149     if(!is_dir($path)){
150       @mkdir($path);
151     }    
153     /* check if we are able to create a new file the given directory */
154     if(!is_writeable($path)){
155       $msg= sprintf(_("Cannot save LDAP dump: no write permission inside '%s'!"),LDAP_DUMP_PATH);
156       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
157       new log("copy","all/all",$dn,array(), $msg);
158       return(FALSE);
159     }  
161     /* Update folder permissions */
162     if(!@chmod(LDAP_DUMP_PATH,0700)){
163       $msg= sprintf(_("Cannot save LDAP dump: setting permissions for '%s' failed!"),LDAP_DUMP_PATH);
164       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
165       new log("copy","all/all","copy & paste, event queue.",array(), $msg);
166       return(FALSE);
167     }
169     /* Create file handle */
170     $fp = @fopen($path."/".$filename,"w+");
171     if(!$fp){
172       $msg= sprintf(_("Cannot save LDAP dump: no write permission to '%s/%s'!"),$path,$filename);
173       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
174       new log("copy","all/all",$dn,array(), $msg);
175       return(FALSE);
176     }    
178     $data = serialize($ldap->fetch());
179     fwrite($fp,$data,strlen($data));
180     fclose($fp);
182     /* Only the webserver should be able to read those files */
183     @chmod($path."/".$filename,0600); 
184     return($path."/".$filename);
185   }
188   /* Check if there are still entries the object queue */
189   function entries_queued()
190   {
191     return( count($this->queue) >=1 || $this->current != FALSE);
192   }
195   /* Paste one entry from queue */
196   function load_entry_from_queue($entry)
197   {
198     if(!isset($entry['tab_class'])){
199       return(array());
200     }
203     $tab_c = $entry['tab_class'];
204     $tab_o = $entry['tab_object'];
205     $tab_a = $entry['tab_acl_category'];
207     if($entry['method'] == "copy"){
208       $entry['object']      = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],"new",$tab_a);
209     }else{
210       $entry['object']      = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],$entry['dn'],$tab_a);
211     }
213     $entry['source_data'] = $this->load_attributes_from_hdd($entry['file_name']);
215     if($entry['method'] == "copy"){
217       /* Prepare each plugin of this tab object to be posted */
218       foreach($entry['object']->by_object as $name => $obj){
220         /* Prepare every single class, to be copied  */
221         $entry['object']->by_object[$name]->PrepareForCopyPaste($entry['source_data']);
223         /* handle some special vars */
224         foreach(array("is_account") as $attr){
225           if(isset($entry['source_data'][$attr])){
226             $entry['object']->by_object[$name]->$attr = $entry['source_data'][$attr];
227           }
228         }
229       }
230     }
231     return($entry);
232   }
235   /* Load dumped ldap entry specified by $filename and 
236    *  return data an unserailized data array
237    */
238   function load_attributes_from_hdd($filename)
239   {
240     $fp = @fopen($filename,"r");
241     if(is_file($filename) && is_readable($filename) && $fp){
242       $data = "";
243       while($str = fgets($fp,512)){
244         $data .= $str;
245       }
246       return(unserialize($data));
247     }else{
248       $msg= sprintf(_("Cannot load dumped file '%s'!"),$filename);
249       msg_dialog::display(_("Internal error"), $msg, ERROR_DIALOG);
250       new log("copy","all/all",$dn,array(), $msg);
251       return(FALSE);
252     }
253   }
256   /* Displays a dialog which allows the user to fix all dependencies of this object.
257      Create unique names, ids, or what ever */
258   function execute()
259   {
260     $ui = get_userinfo();
261     $type = $this->current['method'];
263     /* Check which entries can be pasted directly.
264      * Create a list of all entries that can be pasted directly.
265      */
266     if($this->require_update){
267       $this->clean_objects      = array();
268       $this->objects_to_fix     = array();
269       $this->disallowed_objects = array();
271       /* Put each queued object in one of the above arrays 
272        */
273       foreach($this->queue as $key => $entry){
274   
275         /* Update entries on demand 
276          */
277         if(!isset($entry['object'])){
278           $entry = $this->load_entry_from_queue($entry);
279           $this->queue[$key] = $entry;
280         }
281         $msgs = $entry['object']->check();
282         $acl = $ui->get_category_permissions($entry['dn'], $entry['tab_acl_category']);
284         /* Check permissions */
285         if(!preg_match("/((c|w)|(w|c))/",$acl)){
286           $this->disallowed_objects[$key] = $entry;
287         }elseif(!count($msgs)){
288           $this->clean_objects[$key]  = $entry;
289         }else{
290           $this->objects_to_fix[$key] = $entry;
291         }
292       }
293       $this->require_update = FALSE;
294     }
296     /* Save objects that can be pasted directly 
297      */
298     if(isset($_POST['PerformCopyPaste']) && count($this->clean_objects)){
299       $this->save_object();
300       $this->current = FALSE;
301       foreach($this->clean_objects as $key => $entry){
303         /* Remove from queue -> avoid saving twice */
304         unset($this->queue[$key]);
305         unset($this->clean_objects[$key]);
307         /* Load next queue entry */
308         $this->current = $entry;
309         $this->lastdn = $this->current['object']->dn;
310         $this->current= $this->_update_vars($this->current);
311         $this->current['object']->save();
312         $this->current = FALSE;
313       } 
314     }
316     /* Save edited entry and force loading new one 
317      */
318     if(isset($_POST['PerformCopyPaste']) && $this->current){
319       $this->current['object']->saveCopyDialog();
320       $msgs = $this->check();
322       /* Load next queue entry */
323       if(!count($msgs)){
324         $this->lastdn = $this->current['object']->dn;
325         $this->current= $this->_update_vars($this->current);
326         $this->current['object']->save();
327         $this->current = FALSE;
328       }else{
329         foreach( $msgs as $msg){
330           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
331         }
332       }
333     }
334       
335     /* Display a list of all pastable entries 
336      */
337     if(count($this->clean_objects)){
338       $dns = "";
339       foreach($this->clean_objects as $entry){
340         $dns .= $entry['dn']."\n";
341       }
342       $smarty = get_smarty();
343       $smarty->assign("type","directly");
344       $smarty->assign("Complete",false);
345       $smarty->assign("AttributesToFix","&nbsp;");
346       $smarty->assign("SubDialog","");
347       $smarty->assign("objectDN" ,$dns);
348       $smarty->assign("message"  , sprintf(_("You are going to paste the following entries '%s'."), "<pre>".$dns."</pre>"));
349       return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
350     }
352     /* Display a list of all pastable entries 
353      */
354     if($this->current || count($this->objects_to_fix)){
355       $this->save_object(); 
356       if(!$this->current){
357         $key = key($this->objects_to_fix);
358         if(isset($this->objects_to_fix[$key])){
359           $this->current = $this->objects_to_fix[$key];
360           unset($this->objects_to_fix[$key]);
361           unset($this->queue[$key]); 
362         }
363       }
364       if($this->current){
365         $smarty = get_smarty();
366         $smarty->assign("type","modified");
367         $smarty->assign("Complete",false);
368         $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
369         $smarty->assign("SubDialog",$this->current['object']->SubDialog);
370         $smarty->assign("objectDN",$this->current['source_data']['dn']);
371         $smarty->assign("message", sprintf(_("You are going to paste the following entry '%s'."), $this->current['source_data']['dn']));
372         return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
373       }
374     }
375     return("");
376   }
379   /* Return the dn of the last edited entry */
380   function last_entry()
381   {
382     return($this->lastdn);
383   }
386   /* Save new values posted by copy & paste dialog */
387   function save_object()
388   {
389     if(isset($_POST['abort_current_cut-copy_operation'])){
390       $this->current = FALSE;
391     }
393     if(isset($_POST['abort_all_cut-copy_operations'])){
394       $this->cleanup_queue();
395       $this->current = FALSE;
396     }
397   }
400   /* Create dialog which asks unique attributes/values ... 
401    *  call tabs -> getCopyDialog() 
402    *    which calls tab -> getCopyDialog()  */
403   function generateAttributesToFix()
404   {
405     if($this->current){
406       return($this->current['object']->getCopyDialog());  
407     }
408   }
411   /* Set a single attribute to specified value
412    *  example :   ("base", $newBase );    */
413   function SetVar($name,$value)
414   {
415     $this->setvar_array[$name]=$value; 
416   }
419   /* Update current object attributes, collected via SetVar */
420   function _update_vars($entry)
421   {
422     /* Update all attributes specified with SetVar */
423     foreach($this->setvar_array as $name => $value){
424       if(isset($entry['object']->$name)){
425         $entry['object']->$name = $value;
426       }
427     }
429     /* Walk through tabs */
430     foreach($entry['object']->by_object as $key => $obj){
432       /* Update all attributes specified with SetVar */
433       foreach($this->setvar_array as $name => $value){
434         if(isset($entry['object']->by_object[$key]->$name)){
435           $entry['object']->by_object[$key]->$name = $value;
436         }
437       }
438     }
439     return($entry);
440   }
443   /* Returns errors from including tabs. */
444   function check()
445   {
446     $ret = array();
447     foreach($this->current['object']->by_object as $obj){
448       if($obj->is_account || $obj->ignore_account){
449         $ret = array_merge($ret , $obj->check());
450       }
451     }
452     return($ret);
453   }
456   /* returns the paste icon for headpages */ 
457   function generatePasteIcon()
458   {
459     $Copy_Paste= "&nbsp;<img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
460     if($this->entries_queued()){
461       $img= "images/copypaste.png";
462       $Copy_Paste.= "<input type='image' name='editPaste' class='center'
463         src='".$img."' alt='"._("Paste")."'>&nbsp;";
464     }else{
465       $Copy_Paste.= "<img class='center' src='images/cant_editpaste.png' alt=\""._("Can't paste")."\">&nbsp;";
466     }
467     return ($Copy_Paste);
468   }
470 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
471 ?>