6436330550dec8d17e1b0caac0a4da8576389d06
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 = "";
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 }
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($_POST['PerformCopyPaste']) && $this->current){
362 $this->current['object']->saveCopyDialog();
363 $msgs = $this->check();
365 /* Load next queue entry */
366 if(!count($msgs)){
367 $this->current['object']->save();
368 $this->lastdn = $this->current['object']->dn;
369 $this->current = FALSE;
370 }else{
371 foreach( $msgs as $msg){
372 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
373 }
374 }
375 }
377 /* Display a list of all pastable entries
378 */
379 if(count($this->clean_objects)){
381 $dns = array();
382 foreach($this->clean_objects as $object){
383 $dns[] = $object['dn'];
384 }
386 $smarty = get_smarty();
387 $smarty->assign("type","directly");
388 $smarty->assign("Complete",false);
389 $smarty->assign("AttributesToFix"," ");
390 $smarty->assign("SubDialog","");
391 $smarty->assign("message" , sprintf(_("These objects will be pasted: %s"), "<br>".msgPool::buildList($dns)));
392 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
393 }
395 /* Display a list of all pastable entries
396 */
397 if($this->current || count($this->objects_to_fix)){
398 $this->save_object();
399 if(!$this->current){
400 $key = key($this->objects_to_fix);
401 if(isset($this->objects_to_fix[$key])){
402 $this->current = $this->objects_to_fix[$key];
403 $this->current= $this->_update_vars($this->current);
404 unset($this->objects_to_fix[$key]);
405 unset($this->queue[$key]);
406 }
407 }
408 if($this->current){
409 $smarty = get_smarty();
410 $smarty->assign("type","modified");
411 $smarty->assign("Complete",false);
412 $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
413 $smarty->assign("SubDialog",$this->current['object']->SubDialog);
414 $smarty->assign("objectDN",$this->current['source_data']['dn']);
415 $smarty->assign("message", sprintf(_("This object will be pasted: %s"), "<br><br>".$this->current['source_data']['dn']));
416 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
417 }
418 }
419 return("");
420 }
423 /* Return the dn of the last edited entry */
424 function last_entry()
425 {
426 return($this->lastdn);
427 }
430 /* Save new values posted by copy & paste dialog */
431 function save_object()
432 {
433 if(isset($_POST['abort_current_cut-copy_operation'])){
434 $this->current = FALSE;
435 }
437 if(isset($_POST['abort_all_cut-copy_operations'])){
438 $this->cleanup_queue();
439 $this->current = FALSE;
440 }
441 }
444 /* Create dialog which asks unique attributes/values ...
445 * call tabs -> getCopyDialog()
446 * which calls tab -> getCopyDialog() */
447 function generateAttributesToFix()
448 {
449 if($this->current){
450 return($this->current['object']->getCopyDialog());
451 }
452 }
455 /* Set a single attribute to specified value
456 * example : ("base", $newBase ); */
457 function SetVar($name,$value)
458 {
459 $this->setvar_array[$name]=$value;
460 }
463 /* Update current object attributes, collected via SetVar */
464 function _update_vars($entry)
465 {
466 /* Update all attributes specified with SetVar */
467 foreach($this->setvar_array as $name => $value){
468 if(isset($entry['object']->$name)){
469 $entry['object']->$name = $value;
470 }
471 }
473 /* Walk through tabs */
474 foreach($entry['object']->by_object as $key => $obj){
476 /* Update all attributes specified with SetVar */
477 foreach($this->setvar_array as $name => $value){
479 /* Do not update parent for plugins, this may break things */
480 if($name == "parent") continue;
482 if(isset($entry['object']->by_object[$key]->$name)){
483 $entry['object']->by_object[$key]->$name = $value;
484 }
485 }
486 }
487 return($entry);
488 }
491 /* Returns errors from including tabs. */
492 function check()
493 {
494 $ret = array();
495 foreach($this->current['object']->by_object as $obj){
496 if($obj->is_account || $obj->ignore_account){
497 $ret = array_merge($ret , $obj->check());
498 }
499 }
500 return($ret);
501 }
504 /* returns the paste icon for headpages */
505 function generatePasteIcon()
506 {
507 $Copy_Paste= " <img class='center' src='images/lists/seperator.png' align='middle' alt='' height='16' width='1'> ";
508 if($this->entries_queued()){
509 $img= "images/lists/paste.png";
510 $Copy_Paste.= "<input type='image' name='editPaste' class='center'
511 src='".$img."' alt='"._("Paste")."'> ";
512 }else{
513 $Copy_Paste.= "<img class='center' src='images/lists/paste-grey.png' alt=\""._("Cannot paste")."\"> ";
514 }
515 return ($Copy_Paste);
516 }
517 }
518 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
519 ?>