Code

Removed object dn
[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","/tmp/gosa");
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 CopyPasteHandler(&$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)
65   {
66     if(!class_exists($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       $this->queue[]    = $tmp; 
90       $this->require_update = TRUE;
91     }
92   }
95   /* This removes all objects from queue.
96    * Remove hdd dumps of current entries too.
97    * Remove entries older than 24 hours.
98    */
99   function cleanup_queue()
100   {
101     $this->current = FALSE;
102     $this->require_update = TRUE;
103     $this->setvar_array = array();
105     /* Remove all entries from queue */  
106     foreach($this->queue as $key => $entry){
107       @rmdir($entry['file_name']);  
108       unset($this->queue[$key]);
109     }
111     /* Create patch if it doesn't exists */
112     if(!is_dir(LDAP_DUMP_PATH)){
113       @mkdir(LDAP_DUMP_PATH);
114     }    
115   
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     /* check if we are able to create a new file the given directory */
125     if(!is_writeable(LDAP_DUMP_PATH)){
126       $msg= _("Copy and paste failed!")."<br><br>"._("Error").": <i>".msgPool::cannotWrite(LDAP_DUMP_PATH)."</i>";
127       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
128       new log("copy","all/all","copy & paste, event queue.",array(), $msg);
129       return(FALSE);
130     }
132     /* Remove entries from hdd that are older than24 hours */
133     $fp = opendir(LDAP_DUMP_PATH);
134     while($file = readdir($fp)){
135       if(is_file(LDAP_DUMP_PATH."/".$file) && !preg_match("/^\./",$file)){
136         $file_time = fileatime(LDAP_DUMP_PATH."/".$file);
137         if($file_time < (time() - (24* 60 *60))){
138           @unlink(LDAP_DUMP_PATH."/".$file);
139         }
140       }
141     }
142   }
145   /* To increase performance we save the ldap dump on hdd 
146    * This function automatically creates the dumps and returns 
147    *  the name of the dumpfile we created 
148    */
149   function save_dn_attributes_to_hdd($dn)
150   {
151     $filename = "Should not be returned";
152     $ldap = $this->config->get_ldap_link();
153     $ldap->cd($this->config->current['BASE']);
154     $res  = $ldap->cat($dn);
156     /* Check if given dn is valid and ldap search was succesfull */ 
157     if(!$res){
158       $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": <i>"._("'%s' is no vaild LDAP object"), LDAP::fix($dn));
159       msg_dialog::display(_("Internal error"), $msg, ERROR_DIALOG);
160       new log("copy","all/all",$dn,array(), $msg);
161       return(FALSE);
162     }
164     /* Create data to save given ldap dump on the hdd */
165     $filename = "gosa_copy-paste_dump_".preg_replace("/[^0-9]/","",microtime());
166     $path     = LDAP_DUMP_PATH;
168     /* Create patch if it doesn't exists */
169     if(!is_dir($path)){
170       @mkdir($path);
171     }    
173     /* check if we are able to create a new file the given directory */
174     if(!is_writeable($path)){
175       $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": <i>"._("No write permission in '%s'"),LDAP_DUMP_PATH);
176       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
177       new log("copy","all/all",$dn,array(), $msg);
178       return(FALSE);
179     }  
181     /* Update folder permissions */
182     if(!@chmod(LDAP_DUMP_PATH,0700)){
183       $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": <i>"._("Cannot set permission for '%s'"),LDAP_DUMP_PATH);
184       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
185       new log("copy","all/all","copy & paste, event queue.",array(), $msg);
186       return(FALSE);
187     }
189     /* Create file handle */
190     $fp = @fopen($path."/".$filename,"w+");
191     if(!$fp){
192       $msg= _("Copy and paste failed!")."<br><br>"._("Error").": <i>".msgPool::cannotWriteFile("$path/$filename")."</i>";
193       msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
194       new log("copy","all/all",$dn,array(), $msg);
195       return(FALSE);
196     }    
198     $data = serialize($ldap->fetch());
199     fwrite($fp,$data,strlen($data));
200     fclose($fp);
202     /* Only the webserver should be able to read those files */
203     @chmod($path."/".$filename,0600); 
204     return($path."/".$filename);
205   }
208   /* Check if there are still entries the object queue */
209   function entries_queued()
210   {
211     return( count($this->queue) >=1 || $this->current != FALSE);
212   }
215   /* Paste one entry from queue */
216   function load_entry_from_queue($entry)
217   {
218     if(!isset($entry['tab_class'])){
219       return(array());
220     }
223     $tab_c = $entry['tab_class'];
224     $tab_o = $entry['tab_object'];
225     $tab_a = $entry['tab_acl_category'];
227     if($entry['method'] == "copy"){
228       $entry['object']      = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],"new",$tab_a);
229     }else{
230       $entry['object']      = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],$entry['dn'],$tab_a);
231     }
233     $entry['source_data'] = $this->load_attributes_from_hdd($entry['file_name']);
235     if($entry['method'] == "copy"){
237       /* Prepare each plugin of this tab object to be posted */
238       foreach($entry['object']->by_object as $name => $obj){
240         /* Prepare every single class, to be copied  */
241         $entry['object']->by_object[$name]->PrepareForCopyPaste($entry['source_data']);
243         /* handle some special vars */
244         foreach(array("is_account") as $attr){
245           if(isset($entry['source_data'][$attr])){
246             $entry['object']->by_object[$name]->$attr = $entry['source_data'][$attr];
247           }
248         }
249       }
250     }
251     return($entry);
252   }
255   /* Load dumped ldap entry specified by $filename and 
256    *  return data an unserailized data array
257    */
258   function load_attributes_from_hdd($filename)
259   {
260     $fp = @fopen($filename,"r");
261     if(is_file($filename) && is_readable($filename) && $fp){
262       $data = "";
263       while($str = fgets($fp,512)){
264         $data .= $str;
265       }
266       return(unserialize($data));
267     }else{
268       $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": <i>".msgPool::cannotReadFile($filename)."</i>");
269       msg_dialog::display(_("Internal error"), $msg, ERROR_DIALOG);
270       new log("copy","all/all",$dn,array(), $msg);
271       return(FALSE);
272     }
273   }
276   /* Displays a dialog which allows the user to fix all dependencies of this object.
277      Create unique names, ids, or what ever */
278   function execute()
279   {
280     $ui = get_userinfo();
281     $type = $this->current['method'];
283     /* Check which entries can be pasted directly.
284      * Create a list of all entries that can be pasted directly.
285      */
286     if($this->require_update){
287       $this->clean_objects      = array();
288       $this->objects_to_fix     = array();
289       $this->disallowed_objects = array();
291       /* Put each queued object in one of the above arrays 
292        */
293       foreach($this->queue as $key => $entry){
294   
295         /* Update entries on demand 
296          */
297         if(!isset($entry['object'])){
298           $entry = $this->load_entry_from_queue($entry);
299           $this->queue[$key] = $entry;
300         }
301         $msgs = $entry['object']->check();
302         $acl = $ui->get_category_permissions($entry['dn'], $entry['tab_acl_category']);
304         /* Check permissions */
305         if(!preg_match("/((c|w)|(w|c))/",$acl)){
306           $this->disallowed_objects[$key] = $entry;
307         }elseif(!count($msgs)){
308           $this->clean_objects[$key]  = $entry;
309         }else{
310           $this->objects_to_fix[$key] = $entry;
311         }
312       }
313       $this->require_update = FALSE;
314     }
316     /* Save objects that can be pasted directly 
317      */
318     if(isset($_POST['PerformCopyPaste']) && count($this->clean_objects)){
319       $this->save_object();
320       $this->current = FALSE;
321       foreach($this->clean_objects as $key => $entry){
323         /* Remove from queue -> avoid saving twice */
324         unset($this->queue[$key]);
325         unset($this->clean_objects[$key]);
327         /* Load next queue entry */
328         $this->current = $entry;
329         $this->lastdn = $this->current['object']->dn;
330         $this->current= $this->_update_vars($this->current);
331         $this->current['object']->save();
332         $this->current = FALSE;
333       } 
334     }
336     /* Save edited entry and force loading new one 
337      */
338     if(isset($_POST['PerformCopyPaste']) && $this->current){
339       $this->current['object']->saveCopyDialog();
340       $msgs = $this->check();
342       /* Load next queue entry */
343       if(!count($msgs)){
344         $this->lastdn = $this->current['object']->dn;
345         $this->current= $this->_update_vars($this->current);
346         $this->current['object']->save();
347         $this->current = FALSE;
348       }else{
349         foreach( $msgs as $msg){
350           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
351         }
352       }
353     }
354       
355     /* Display a list of all pastable entries 
356      */
357     if(count($this->clean_objects)){
358       $smarty = get_smarty();
359       $smarty->assign("type","directly");
360       $smarty->assign("Complete",false);
361       $smarty->assign("AttributesToFix","&nbsp;");
362       $smarty->assign("SubDialog","");
363       $smarty->assign("message"  , sprintf(_("These objects will be pasted: %s"), "<br>".msgPool::buildList($this->clean_objects)));
364       return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
365     }
367     /* Display a list of all pastable entries 
368      */
369     if($this->current || count($this->objects_to_fix)){
370       $this->save_object(); 
371       if(!$this->current){
372         $key = key($this->objects_to_fix);
373         if(isset($this->objects_to_fix[$key])){
374           $this->current = $this->objects_to_fix[$key];
375           unset($this->objects_to_fix[$key]);
376           unset($this->queue[$key]); 
377         }
378       }
379       if($this->current){
380         $smarty = get_smarty();
381         $smarty->assign("type","modified");
382         $smarty->assign("Complete",false);
383         $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
384         $smarty->assign("SubDialog",$this->current['object']->SubDialog);
385         $smarty->assign("objectDN",$this->current['source_data']['dn']);
386         $smarty->assign("message", sprintf(_("This object will be pasted: %s"), "<br><br>".$this->current['source_data']['dn']));
387         return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
388       }
389     }
390     return("");
391   }
394   /* Return the dn of the last edited entry */
395   function last_entry()
396   {
397     return($this->lastdn);
398   }
401   /* Save new values posted by copy & paste dialog */
402   function save_object()
403   {
404     if(isset($_POST['abort_current_cut-copy_operation'])){
405       $this->current = FALSE;
406     }
408     if(isset($_POST['abort_all_cut-copy_operations'])){
409       $this->cleanup_queue();
410       $this->current = FALSE;
411     }
412   }
415   /* Create dialog which asks unique attributes/values ... 
416    *  call tabs -> getCopyDialog() 
417    *    which calls tab -> getCopyDialog()  */
418   function generateAttributesToFix()
419   {
420     if($this->current){
421       return($this->current['object']->getCopyDialog());  
422     }
423   }
426   /* Set a single attribute to specified value
427    *  example :   ("base", $newBase );    */
428   function SetVar($name,$value)
429   {
430     $this->setvar_array[$name]=$value; 
431   }
434   /* Update current object attributes, collected via SetVar */
435   function _update_vars($entry)
436   {
437     /* Update all attributes specified with SetVar */
438     foreach($this->setvar_array as $name => $value){
439       if(isset($entry['object']->$name)){
440         $entry['object']->$name = $value;
441       }
442     }
444     /* Walk through tabs */
445     foreach($entry['object']->by_object as $key => $obj){
447       /* Update all attributes specified with SetVar */
448       foreach($this->setvar_array as $name => $value){
449         if(isset($entry['object']->by_object[$key]->$name)){
450           $entry['object']->by_object[$key]->$name = $value;
451         }
452       }
453     }
454     return($entry);
455   }
458   /* Returns errors from including tabs. */
459   function check()
460   {
461     $ret = array();
462     foreach($this->current['object']->by_object as $obj){
463       if($obj->is_account || $obj->ignore_account){
464         $ret = array_merge($ret , $obj->check());
465       }
466     }
467     return($ret);
468   }
471   /* returns the paste icon for headpages */ 
472   function generatePasteIcon()
473   {
474     $Copy_Paste= "&nbsp;<img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
475     if($this->entries_queued()){
476       $img= "images/copypaste.png";
477       $Copy_Paste.= "<input type='image' name='editPaste' class='center'
478         src='".$img."' alt='"._("Paste")."'>&nbsp;";
479     }else{
480       $Copy_Paste.= "<img class='center' src='images/cant_editpaste.png' alt=\""._("Cannot paste")."\">&nbsp;";
481     }
482     return ($Copy_Paste);
483   }
485 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
486 ?>