Code

db0a5df52679753bf75c8adcd1629c042e85ae56
[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 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,&$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->handleReferences();
356         $this->current = FALSE;
357       } 
358     }
360     /* Save edited entry and force loading new one 
361      */
362     if(isset($this->current['object']) && method_exists($this->current['object'],"saveCopyDialog")) {
363       $this->current['object']->saveCopyDialog();
364     }
366     if(isset($_POST['PerformCopyPaste']) && $this->current){
367       $msgs = $this->check();
369       /* Load next queue entry */
370       if(!count($msgs)){
371         $this->current['object']->save();
372         $this->handleReferences();
373         $this->lastdn = $this->current['object']->dn;
374         $this->current = FALSE;
375       }else{
376         foreach( $msgs as $msg){
377           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
378         }
379       }
380     }
381       
382     /* Display a list of all pastable entries 
383      */
384     if(count($this->clean_objects)){
385       
386       $dns = array();
387       foreach($this->clean_objects as $object){
388         $dns[] = $object['dn'];
389       }
391       $smarty = get_smarty();
392       $smarty->assign("type","directly");
393       $smarty->assign("Complete",false);
394       $smarty->assign("AttributesToFix","&nbsp;");
395       $smarty->assign("SubDialog","");
396       $smarty->assign("message"  , sprintf(_("These objects will be pasted: %s"), "<br>".msgPool::buildList($dns)));
397       return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
398     }
400     /* Display a list of all pastable entries 
401      */
402     if($this->current || count($this->objects_to_fix)){
403       $this->save_object(); 
404       if(!$this->current){
405         $key = key($this->objects_to_fix);
406         if(isset($this->objects_to_fix[$key])){
407           $this->current = $this->objects_to_fix[$key];
408           $this->current= $this->_update_vars($this->current);
409           unset($this->objects_to_fix[$key]);
410           unset($this->queue[$key]); 
411         }
412       }
413       if($this->current){
414         $smarty = get_smarty();
415         $smarty->assign("type","modified");
416         $smarty->assign("Complete",false);
417         $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
418         $smarty->assign("SubDialog",$this->current['object']->SubDialog);
419         $smarty->assign("objectDN",$this->current['source_data']['dn']);
420         $smarty->assign("message", sprintf(_("This object will be pasted: %s"), "<br><br>".$this->current['source_data']['dn']));
421         return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
422       }
423     }
424     return("");
425   }
428   /* Return the dn of the last edited entry */
429   function last_entry()
430   {
431     return($this->lastdn);
432   }
435   /* Save new values posted by copy & paste dialog */
436   function save_object()
437   {
438     if(isset($_POST['abort_current_cut-copy_operation'])){
439       $this->current = FALSE;
440     }
442     if(isset($_POST['abort_all_cut-copy_operations'])){
443       $this->cleanup_queue();
444       $this->current = FALSE;
445     }
446   }
449   /* Create dialog which asks unique attributes/values ... 
450    *  call tabs -> getCopyDialog() 
451    *    which calls tab -> getCopyDialog()  */
452   function generateAttributesToFix()
453   {
454     if($this->current){
455       return($this->current['object']->getCopyDialog());  
456     }
457   }
460   /* Set a single attribute to specified value
461    *  example :   ("base", $newBase );    */
462   function SetVar($name,$value)
463   {
464     $this->setvar_array[$name]=$value; 
465   }
468   /* Update current object attributes, collected via SetVar */
469   function _update_vars($entry)
470   {
471     /* Update all attributes specified with SetVar */
472     foreach($this->setvar_array as $name => $value){
473       if(isset($entry['object']->$name)){
474         $entry['object']->$name = $value;
475       }
476     }
478     /* Walk through tabs */
479     foreach($entry['object']->by_object as $key => $obj){
481       /* Update all attributes specified with SetVar */
482       foreach($this->setvar_array as $name => $value){
484         /* Do not update parent for plugins, this may break things */
485         if($name == "parent") continue;
487         if(isset($entry['object']->by_object[$key]->$name)){
488           $entry['object']->by_object[$key]->$name = $value;
489         }
490       }
491     }
492     return($entry);
493   }
496   /* Returns errors from including tabs. */
497   function check()
498   {
499     $ret = array();
500     foreach($this->current['object']->by_object as $obj){
501       if($obj->is_account || $obj->ignore_account){
502         $ret = array_merge($ret , $obj->check());
503       }
504     }
505     return($ret);
506   }
509   function handleReferences()
510   {
511     $dst_dn = $this->current['object']->dn;
512     $src_dn = $this->current['dn'];
514     // Only copy references if required 
515     if($this->current['method'] != 'copy') return;
517     // Migrate objectgroups
518     $ogroups = get_sub_list("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter(LDAP::fix($src_dn))."))",
519             "ogroups", array(get_ou("ogroupRDN")),$this->config->current['BASE'],array("dn"), GL_SUBSEARCH | GL_NO_ACL_CHECK);
521     // Walk through all objectGroups
522     foreach($ogroups as $ogroup){
523         $o_ogroup= new ogroup($this->config,$ogroup['dn']);
524         $o_ogroup->member[$dst_dn]= $dst_dn;
525         $o_ogroup->save();
526     }
528     // Update roles 
529     $roles = get_sub_list("(&(objectClass=organizationalRole)(roleOccupant=".LDAP::prepare4filter(LDAP::fix($src_dn))."))",
530             "roles", array(get_ou("roleRDN")),$this->config->current['BASE'],array("dn"), GL_SUBSEARCH | GL_NO_ACL_CHECK);
532     // Walk through all roles
533     foreach($roles as $role){
534         $role = new roleGeneric($this->config,$role['dn']);
535         $role->roleOccupant[] = $dst_dn;
536         $role->save();
537     }
539     // Update groups
540     if(isset($this->current['object']->uid) && !empty($this->current['object']->uid)){
542         $ldap = $this->config->get_ldap_link();
543         $ldap->cd($this->config->current['BASE']);
544         $ldap->cat($src_dn);
545         $attrs = $ldap->fetch();
546         if(isset($attrs['uid'][0])){
547             $suid = $attrs['uid'][0];
549             $uid = $this->current['object']->uid;
550             $groups = get_sub_list("(&(objectClass=posixGroup)(memberUid={$suid}))",
551                     "groups",array(get_ou("groupRDN")),$this->config->current['BASE'],array("dn"), GL_SUBSEARCH | GL_NO_ACL_CHECK);
553             // Walk through all POSIX groups
554             foreach($groups as $group){
555                 $o_group= new group($this->config,$group['dn']);
556                 $o_group->addUser($uid);
557                 $o_group->save();
558             }
559         }
560     }
561   }
563   /* returns the paste icon for headpages */ 
564   function generatePasteIcon()
565   {
566     $Copy_Paste= "&nbsp;<img class='center' src='images/lists/seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
567     if($this->entries_queued()){
568       $img= "images/lists/paste.png";
569       $Copy_Paste.= "<input type='image' name='editPaste' class='center'
570         src='".$img."' alt='"._("Paste")."'>&nbsp;";
571     }else{
572       $Copy_Paste.= "<img class='center' src='images/lists/paste-grey.png' alt=\""._("Cannot paste")."\">&nbsp;";
573     }
574     return ($Copy_Paste);
575   }
577 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
578 ?>