Code

1d7364dfcde91299c5d09afa1c2f4fdfc2f62080
[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           $entry = $this->_update_vars($entry);
280           $this->queue[$key] = $entry;
281         }
282         $msgs = $entry['object']->check();
283         $acl = $ui->get_category_permissions($entry['dn'], $entry['tab_acl_category']);
285         /* Check permissions */
286         if(!preg_match("/((c|w)|(w|c))/",$acl)){
287           $this->disallowed_objects[$key] = $entry;
288         }elseif(!count($msgs)){
289           $this->clean_objects[$key]  = $entry;
290         }else{
291           $this->objects_to_fix[$key] = $entry;
292         }
293       }
294       $this->require_update = FALSE;
295     }
297     /* Save objects that can be pasted directly 
298      */
299     if(isset($_POST['PerformCopyPaste']) && count($this->clean_objects)){
300       $this->save_object();
301       $this->current = FALSE;
302       foreach($this->clean_objects as $key => $entry){
304         /* Remove from queue -> avoid saving twice */
305         unset($this->queue[$key]);
306         unset($this->clean_objects[$key]);
308         /* Load next queue entry */
309         $this->current = $entry;
310         $this->lastdn = $this->current['object']->dn;
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['object']->save();
326         $this->current = FALSE;
327       }else{
328         foreach( $msgs as $msg){
329           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
330         }
331       }
332     }
333       
334     /* Display a list of all pastable entries 
335      */
336     if(count($this->clean_objects)){
337       $dns = "";
338       foreach($this->clean_objects as $entry){
339         $dns .= $entry['dn']."\n";
340       }
341       $smarty = get_smarty();
342       $smarty->assign("type","cut");
343       $smarty->assign("Complete",false);
344       $smarty->assign("AttributesToFix","&nbsp;");
345       $smarty->assign("SubDialog","");
346       $smarty->assign("objectDN" ,$dns);
347       $smarty->assign("message"  , sprintf(_("You are going to paste the cutted entry '%s'."), "<pre>".$dns."</pre>"));
348       return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
349     }
351     /* Display a list of all pastable entries 
352      */
353     if($this->current || count($this->objects_to_fix)){
354       $this->save_object(); 
355       if(!$this->current){
356         $key = key($this->objects_to_fix);
357         if(isset($this->objects_to_fix[$key])){
358           $this->current = $this->objects_to_fix[$key];
359           unset($this->objects_to_fix[$key]);
360           unset($this->queue[$key]); 
361         }
362       }
363       if($this->current){
364         $smarty = get_smarty();
365         $smarty->assign("type","copy");
366         $smarty->assign("Complete",false);
367         $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
368         $smarty->assign("SubDialog",$this->current['object']->SubDialog);
369         $smarty->assign("objectDN",$this->current['source_data']['dn']);
370         $smarty->assign("message", sprintf(_("You are going to paste the copied entry '%s'."), $this->current['source_data']['dn']));
371         return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
372       }
373     }
374     return("");
375   }
378   /* Return the dn of the last edited entry */
379   function last_entry()
380   {
381     return($this->lastdn);
382   }
385   /* Save new values posted by copy & paste dialog */
386   function save_object()
387   {
388     if(isset($_POST['abort_current_cut-copy_operation'])){
389       $this->current = FALSE;
390     }
392     if(isset($_POST['abort_all_cut-copy_operations'])){
393       $this->cleanup_queue();
394       $this->current = FALSE;
395     }
396   }
399   /* Create dialog which asks unique attributes/values ... 
400    *  call tabs -> getCopyDialog() 
401    *    which calls tab -> getCopyDialog()  */
402   function generateAttributesToFix()
403   {
404     if($this->current){
405       return($this->current['object']->getCopyDialog());  
406     }
407   }
410   /* Set a single attribute to specified value
411    *  example :   ("base", $newBase );    */
412   function SetVar($name,$value)
413   {
414     $this->setvar_array[$name]=$value; 
415   }
418   /* Update current object attributes, collected via SetVar */
419   function _update_vars($entry)
420   {
421     /* Update all attributes specified with SetVar */
422     foreach($this->setvar_array as $name => $value){
423       if(isset($entry['object']->$name)){
424         $entry['object']->$name = $value;
425       }
426     }
428     /* Walk through tabs */
429     foreach($entry['object']->by_object as $key => $obj){
431       /* Update all attributes specified with SetVar */
432       foreach($this->setvar_array as $name => $value){
433         if(isset($entry['object']->by_object[$key]->$name)){
434           $entry['object']->by_object[$key]->$name = $value;
435         }
436       }
437     }
438     return($entry);
439   }
442   /* Returns errors from including tabs. */
443   function check()
444   {
445     $ret = array();
446     foreach($this->current['object']->by_object as $obj){
447       if($obj->is_account || $obj->ignore_account){
448         $ret = array_merge($ret , $obj->check());
449       }
450     }
451     return($ret);
452   }
455   /* returns the paste icon for headpages */ 
456   function generatePasteIcon()
457   {
458     $Copy_Paste= "&nbsp;<img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
459     if($this->entries_queued()){
460       $img= "images/copypaste.png";
461       $Copy_Paste.= "<input type='image' name='editPaste' class='center'
462         src='".$img."' alt='"._("Paste")."'>&nbsp;";
463     }else{
464       $Copy_Paste.= "<img class='center' src='images/cant_editpaste.png' alt=\""._("Can't paste")."\">&nbsp;";
465     }
466     return ($Copy_Paste);
467   }
469 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
470 ?>