Code

PHP5 flush
[gosa.git] / gosa-core / include / class_CopyPasteHandler.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 define("LDAP_DUMP_PATH","/var/cache/gosa/tmp");
25 class CopyPasteHandler {
27   var $config;
28   var $current;
30   /* This array contains all dns of the currently copyied objects */
31   var $queue       = array(); 
33   /* Attributes that should be overwritten */
34   var $setvar_array= array();
36   /* The dn of the last edited object */
37   var $lastdn      = "";
38   
39   var $disallowed_objects = array();
40   var $objects_to_fix     = array();
41   var $clean_objects      = array();
42   var $require_update     = FALSE;
46   /* Create CP handler  */
47   function __construct(&$config)
48   {
49     $this->config = &$config;   
50     $this->current= NULL;
51     $this->queue  = array();
52     $this->setvar_array = array();
53   }
56   /* Entry entry to Copy & Paste queue.
57    * A Queue entry is represented as follows.
58    *  array['file_name']  - Position on hdd 
59    *  array['method']     - copy/cut
60    *  array['dn']         - the dn of the object added to the queue 
61    *  array['tab_class']  - Tab object that should be used to initialize the new object
62    *  array['tab_object'] - Tab object name used to initialize correct object Type like USERTABS
63    */
64   function add_to_queue($dn,$action,$tab_class,$tab_object,$tab_acl_category,&$parent = NULL)
65   {
66     if(!class_available($tab_class)){
67       trigger_error(sprintf("Specified class object '%s' does not exists.",$tab_class));
68       return(FALSE);
69     }
71     if(!isset($this->config->data['TABS'][$tab_object])){
72       trigger_error(sprintf("Specified tab object '%s' does not exists.",$tab_object));
73       return(FALSE);
74     }
76     if(!in_array($action,array("cut","copy"))){
77       trigger_error(sprintf("Specified action '%s' does not exists for copy & paste.",$action));
78       return(FALSE);
79     } 
81     if($file_name = $this->save_dn_attributes_to_hdd($dn)){
82       $tmp = array();
83       $tmp['file_name'] = $file_name;
84       $tmp['method']    = $action;  
85       $tmp['dn']        = $dn;
86       $tmp['tab_class'] = $tab_class;
87       $tmp['tab_object']= $tab_object;
88       $tmp['tab_acl_category']= $tab_acl_category;
89       $tmp['parent']    = $parent;
90       $this->queue[]    = $tmp; 
91       $this->require_update = TRUE;
92     }
93   }
96   /* This removes all objects from queue.
97    * Remove hdd dumps of current entries too.
98    * Remove entries older than 24 hours.
99    */
100   function cleanup_queue()
101   {
102     $this->current = FALSE;
103     $this->require_update = TRUE;
104     $this->setvar_array = array();
106     /* Remove all entries from queue */  
107     foreach($this->queue as $key => $entry){
108       @rmdir($entry['file_name']);  
109       unset($this->queue[$key]);
110     }
112     /* Create patch if it doesn't exists */
113     if(!is_dir(LDAP_DUMP_PATH)){
114       @mkdir(LDAP_DUMP_PATH);
116       /* Update folder permissions */
117       if(!@chmod(LDAP_DUMP_PATH,0700)){
118         $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": <i>"._("Cannot set permission for '%s'")."</i>" ,LDAP_DUMP_PATH);
119         msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
120         new log("copy","all/all","copy & paste, event queue.",array(), $msg);
121         return(FALSE);
122       }
123     }    
124     
125     /* check if we are able to create a new file the given directory */
126     if(!is_writeable(LDAP_DUMP_PATH)){
127       $msg= _("Copy and paste failed!")."<br><br>"._("Error").": <i>".msgPool::cannotWriteFile(LDAP_DUMP_PATH)."</i>";
128       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
129       new log("copy","all/all","copy & paste, event queue.",array(), $msg);
130       return(FALSE);
131     }
133     /* Remove entries from hdd that are older than24 hours */
134     $fp = opendir(LDAP_DUMP_PATH);
135     while($file = readdir($fp)){
136       if(is_file(LDAP_DUMP_PATH."/".$file) && !preg_match("/^\./",$file)){
137         $file_time = fileatime(LDAP_DUMP_PATH."/".$file);
138         if($file_time < (time() - (24* 60 *60))){
139           @unlink(LDAP_DUMP_PATH."/".$file);
140         }
141       }
142     }
143   }
146   /* To increase performance we save the ldap dump on hdd 
147    * This function automatically creates the dumps and returns 
148    *  the name of the dumpfile we created 
149    */
150   function save_dn_attributes_to_hdd($dn)
151   {
152     $filename = "Should not be returned";
153     $ldap = $this->config->get_ldap_link();
154     $ldap->cd($this->config->current['BASE']);
155     $res  = $ldap->cat($dn);
157     /* Check if given dn is valid and ldap search was succesfull */ 
158     if(!$res){
159       $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": <i>"._("'%s' is no vaild LDAP object"), LDAP::fix($dn));
160       msg_dialog::display(_("Internal error"), $msg, ERROR_DIALOG);
161       new log("copy","all/all",$dn,array(), $msg);
162       return(FALSE);
163     }
165     /* Create data to save given ldap dump on the hdd */
166     $filename = "gosa_copy-paste_dump_".preg_replace("/[^0-9]/","",microtime());
167     $path     = LDAP_DUMP_PATH;
169     /* Create patch if it doesn't exists */
170     if(!is_dir($path)){
171       @mkdir($path);
172     }    
174     /* check if we are able to create a new file the given directory */
175     if(!is_writeable($path)){
176       $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": <i>"._("No write permission in '%s'"),LDAP_DUMP_PATH);
177       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
178       new log("copy","all/all",$dn,array(), $msg);
179       return(FALSE);
180     }  
182     /* Create file handle */
183     $fp = @fopen($path."/".$filename,"w+");
184     if(!$fp){
185       $msg= _("Copy and paste failed!")."<br><br>"._("Error").": <i>".msgPool::cannotWriteFile("$path/$filename")."</i>";
186       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
187       new log("copy","all/all",$dn,array(), $msg);
188       return(FALSE);
189     }    
191     /* Update folder permissions */
192     if(!@chmod($path."/".$filename,0700)){
193       $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": <i>"._("Cannot set permission for '%s'"),LDAP_DUMP_PATH);
194       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
195       new log("copy","all/all","copy & paste, event queue.",array(), $msg);
196       return(FALSE);
197     }
199     $data = serialize($ldap->fetch());
200     fwrite($fp,$data,strlen($data));
201     fclose($fp);
203     /* Only the webserver should be able to read those files */
204     @chmod($path."/".$filename,0600); 
205     return($path."/".$filename);
206   }
209   /* Check if there are still entries the object queue */
210   function entries_queued()
211   {
212     return( count($this->queue) >=1 || $this->current != FALSE);
213   }
216   /* Paste one entry from queue */
217   function load_entry_from_queue($entry)
218   {
219     if(!isset($entry['tab_class'])){
220       return(array());
221     }
224     $tab_c = $entry['tab_class'];
225     $tab_o = $entry['tab_object'];
226     $tab_a = $entry['tab_acl_category'];
227     $parent = $entry['parent'];
229     if($entry['method'] == "copy"){
230       $entry['object']      = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],"new",$tab_a);
231     }else{
232       $entry['object']      = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],$entry['dn'],$tab_a);
233     }
235     if($parent ){
236       $entry['object']->parent = $parent;
237     }
238     $entry['source_data'] = $this->load_attributes_from_hdd($entry['file_name']);
240     if($entry['method'] == "copy"){
242       /* Prepare each plugin of this tab object to be posted */
243       foreach($entry['object']->by_object as $name => $obj){
245         /* Prepare every single class, to be copied  */
246         $entry['object']->by_object[$name]->PrepareForCopyPaste($entry['source_data']);
248         /* handle some special vars */
249         foreach(array("is_account") as $attr){
250           if(isset($entry['source_data'][$attr])){
251             $entry['object']->by_object[$name]->$attr = $entry['source_data'][$attr];
252           }
253         }
254       }
255     }
256     return($entry);
257   }
260   /* Load dumped ldap entry specified by $filename and 
261    *  return data an unserailized data array
262    */
263   function load_attributes_from_hdd($filename)
264   {
265     $fp = @fopen($filename,"r");
266     if(is_file($filename) && is_readable($filename) && $fp){
267       $data = "";
268       while($str = fgets($fp,512)){
269         $data .= $str;
270       }
271       return(unserialize($data));
272     }else{
273       $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": <i>".msgPool::cannotReadFile($filename)."</i>");
274       msg_dialog::display(_("Internal error"), $msg, ERROR_DIALOG);
275       new log("copy","all/all",$dn,array(), $msg);
276       return(FALSE);
277     }
278   }
281   /* Displays a dialog which allows the user to fix all dependencies of this object.
282      Create unique names, ids, or what ever */
283   function execute()
284   {
285     $ui = get_userinfo();
286     $type = $this->current['method'];
288     /* Check which entries can be pasted directly.
289      * Create a list of all entries that can be pasted directly.
290      */
291     if($this->require_update){
292       $this->clean_objects      = array();
293       $this->objects_to_fix     = array();
294       $this->disallowed_objects = array();
296       /* Put each queued object in one of the above arrays 
297        */
298       foreach($this->queue as $key => $entry){
300         /* Update entries on demand 
301          */
302         if(!isset($entry['object'])){
303           $entry = $this->load_entry_from_queue($entry);
304           $this->queue[$key] = $entry;
305         }
306         $entry= $this->_update_vars($entry);
307         $msgs = $entry['object']->check();
309         /* To copy an object we require full read access to the object category 
310          */
311         $copy_acl = preg_match("/r/",$ui->has_complete_category_acls($entry['dn'], $entry['tab_acl_category']));
313         /* In order to copy an object we require read an delete acls 
314          */
315         $cut_acl  = preg_match("/d/",$ui->has_complete_category_acls($entry['dn'], $entry['tab_acl_category']));
316         $cut_acl &= preg_match("/r/",$ui->has_complete_category_acls($entry['dn'], $entry['tab_acl_category']));
318         /* Check permissions */
319         if($entry['method'] == "copy" && !$copy_acl){
320           $this->disallowed_objects[$key] = $entry;
321         }elseif($entry['method'] == "cut" && !$cut_acl){
322           $this->disallowed_objects[$key] = $entry;
323         }elseif(!count($msgs)){
324           $this->clean_objects[$key]  = $entry;
325         }else{
326           $this->objects_to_fix[$key] = $entry;
327         }
328       }
329       if(count($this->disallowed_objects)){
330         $dns = array();
331         foreach($this->disallowed_objects as $entry){
332           $dns[] = $entry['dn'];
333         }
334 #        msg_dialog::display(_("Permission"),msgPool::permCreate($dns),INFO_DIALOG);
335       }
336       $this->require_update = FALSE;
337     }
339     /* Save objects that can be pasted directly 
340      */
341     if(isset($_POST['PerformCopyPaste']) && count($this->clean_objects)){
342       $this->save_object();
343       $this->current = FALSE;
344       foreach($this->clean_objects as $key => $entry){
346         /* Remove from queue -> avoid saving twice */
347         unset($this->queue[$key]);
348         unset($this->clean_objects[$key]);
350         /* Load next queue entry */
351         $this->current = $entry;
352         $this->lastdn = $this->current['object']->dn;
353         $this->current= $this->_update_vars($this->current);
354         $this->current['object']->save();
355         $this->current = FALSE;
356       } 
357     }
359     /* Save edited entry and force loading new one 
360      */
361     if(isset($this->current['object']) && method_exists($this->current['object'],"saveCopyDialog")) {
362       $this->current['object']->saveCopyDialog();
363     }
365     if(isset($_POST['PerformCopyPaste']) && $this->current){
366       $msgs = $this->check();
368       /* Load next queue entry */
369       if(!count($msgs)){
370         $this->current['object']->save();
371         $this->lastdn = $this->current['object']->dn;
372         $this->current = FALSE;
373       }else{
374         foreach( $msgs as $msg){
375           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
376         }
377       }
378     }
379       
380     /* Display a list of all pastable entries 
381      */
382     if(count($this->clean_objects)){
383       
384       $dns = array();
385       foreach($this->clean_objects as $object){
386         $dns[] = $object['dn'];
387       }
389       $smarty = get_smarty();
390       $smarty->assign("type","directly");
391       $smarty->assign("Complete",false);
392       $smarty->assign("AttributesToFix","&nbsp;");
393       $smarty->assign("SubDialog","");
394       $smarty->assign("message"  , sprintf(_("These objects will be pasted: %s"), "<br>".msgPool::buildList($dns)));
395       return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
396     }
398     /* Display a list of all pastable entries 
399      */
400     if($this->current || count($this->objects_to_fix)){
401       $this->save_object(); 
402       if(!$this->current){
403         $key = key($this->objects_to_fix);
404         if(isset($this->objects_to_fix[$key])){
405           $this->current = $this->objects_to_fix[$key];
406           $this->current= $this->_update_vars($this->current);
407           unset($this->objects_to_fix[$key]);
408           unset($this->queue[$key]); 
409         }
410       }
411       if($this->current){
412         $smarty = get_smarty();
413         $smarty->assign("type","modified");
414         $smarty->assign("Complete",false);
415         $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
416         $smarty->assign("SubDialog",$this->current['object']->SubDialog);
417         $smarty->assign("objectDN",$this->current['source_data']['dn']);
418         $smarty->assign("message", sprintf(_("This object will be pasted: %s"), "<br><br>".$this->current['source_data']['dn']));
419         return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
420       }
421     }
422     return("");
423   }
426   /* Return the dn of the last edited entry */
427   function last_entry()
428   {
429     return($this->lastdn);
430   }
433   /* Save new values posted by copy & paste dialog */
434   function save_object()
435   {
436     if(isset($_POST['abort_current_cut-copy_operation'])){
437       $this->current = FALSE;
438     }
440     if(isset($_POST['abort_all_cut-copy_operations'])){
441       $this->cleanup_queue();
442       $this->current = FALSE;
443     }
444   }
447   /* Create dialog which asks unique attributes/values ... 
448    *  call tabs -> getCopyDialog() 
449    *    which calls tab -> getCopyDialog()  */
450   function generateAttributesToFix()
451   {
452     if($this->current){
453       return($this->current['object']->getCopyDialog());  
454     }
455   }
458   /* Set a single attribute to specified value
459    *  example :   ("base", $newBase );    */
460   function SetVar($name,$value)
461   {
462     $this->setvar_array[$name]=$value; 
463   }
466   /* Update current object attributes, collected via SetVar */
467   function _update_vars($entry)
468   {
469     /* Update all attributes specified with SetVar */
470     foreach($this->setvar_array as $name => $value){
471       if(isset($entry['object']->$name)){
472         $entry['object']->$name = $value;
473       }
474     }
476     /* Walk through tabs */
477     foreach($entry['object']->by_object as $key => $obj){
479       /* Update all attributes specified with SetVar */
480       foreach($this->setvar_array as $name => $value){
482         /* Do not update parent for plugins, this may break things */
483         if($name == "parent") continue;
485         if(isset($entry['object']->by_object[$key]->$name)){
486           $entry['object']->by_object[$key]->$name = $value;
487         }
488       }
489     }
490     return($entry);
491   }
494   /* Returns errors from including tabs. */
495   function check()
496   {
497     $ret = array();
498     foreach($this->current['object']->by_object as $obj){
499       if($obj->is_account || $obj->ignore_account){
500         $ret = array_merge($ret , $obj->check());
501       }
502     }
503     return($ret);
504   }
507   /* returns the paste icon for headpages */ 
508   function generatePasteIcon()
509   {
510     $Copy_Paste= "&nbsp;<img class='center' src='images/lists/seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
511     if($this->entries_queued()){
512       $img= "images/lists/paste.png";
513       $Copy_Paste.= "<input type='image' name='editPaste' class='center'
514         src='".$img."' alt='"._("Paste")."'>&nbsp;";
515     }else{
516       $Copy_Paste.= "<img class='center' src='images/lists/paste-grey.png' alt=\""._("Cannot paste")."\">&nbsp;";
517     }
518     return ($Copy_Paste);
519   }
521 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
522 ?>