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)
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 $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);
115 /* Update folder permissions */
116 if(!@chmod(LDAP_DUMP_PATH,0700)){
117 $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": <i>"._("Cannot set permission for '%s'")."</i>" ,LDAP_DUMP_PATH);
118 msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
119 new log("copy","all/all","copy & paste, event queue.",array(), $msg);
120 return(FALSE);
121 }
122 }
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::cannotWriteFile(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 /* Create file handle */
182 $fp = @fopen($path."/".$filename,"w+");
183 if(!$fp){
184 $msg= _("Copy and paste failed!")."<br><br>"._("Error").": <i>".msgPool::cannotWriteFile("$path/$filename")."</i>";
185 msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
186 new log("copy","all/all",$dn,array(), $msg);
187 return(FALSE);
188 }
190 /* Update folder permissions */
191 if(!@chmod($path."/".$filename,0700)){
192 $msg= sprintf(_("Copy and paste failed!")."<br><br>"._("Error").": <i>"._("Cannot set permission for '%s'"),LDAP_DUMP_PATH);
193 msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
194 new log("copy","all/all","copy & paste, event queue.",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){
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 $entry= $this->_update_vars($entry);
302 $msgs = $entry['object']->check();
304 /* To copy an object we require full read access to the object category
305 */
306 $copy_acl = preg_match("/r/",$ui->has_complete_category_acls($entry['dn'], $entry['tab_acl_category']));
308 /* In order to copy an object we require read an delete acls
309 */
310 $cut_acl = preg_match("/d/",$ui->has_complete_category_acls($entry['dn'], $entry['tab_acl_category']));
311 $cut_acl &= preg_match("/r/",$ui->has_complete_category_acls($entry['dn'], $entry['tab_acl_category']));
313 /* Check permissions */
314 if($entry['method'] == "copy" && !$copy_acl){
315 $this->disallowed_objects[$key] = $entry;
316 }elseif($entry['method'] == "cut" && !$cut_acl){
317 $this->disallowed_objects[$key] = $entry;
318 }elseif(!count($msgs)){
319 $this->clean_objects[$key] = $entry;
320 }else{
321 $this->objects_to_fix[$key] = $entry;
322 }
323 }
324 if(count($this->disallowed_objects)){
325 $dns = array();
326 foreach($this->disallowed_objects as $entry){
327 $dns[] = $entry['dn'];
328 }
329 # msg_dialog::display(_("Permission"),msgPool::permCreate($dns),INFO_DIALOG);
330 }
331 $this->require_update = FALSE;
332 }
334 /* Save objects that can be pasted directly
335 */
336 if(isset($_POST['PerformCopyPaste']) && count($this->clean_objects)){
337 $this->save_object();
338 $this->current = FALSE;
339 foreach($this->clean_objects as $key => $entry){
341 /* Remove from queue -> avoid saving twice */
342 unset($this->queue[$key]);
343 unset($this->clean_objects[$key]);
345 /* Load next queue entry */
346 $this->current = $entry;
347 $this->lastdn = $this->current['object']->dn;
348 $this->current= $this->_update_vars($this->current);
349 $this->current['object']->save();
350 $this->current = FALSE;
351 }
352 }
354 /* Save edited entry and force loading new one
355 */
356 if(isset($_POST['PerformCopyPaste']) && $this->current){
357 $this->current['object']->saveCopyDialog();
358 $msgs = $this->check();
360 /* Load next queue entry */
361 if(!count($msgs)){
362 $this->lastdn = $this->current['object']->dn;
363 $this->current['object']->save();
364 $this->current = FALSE;
365 }else{
366 foreach( $msgs as $msg){
367 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
368 }
369 }
370 }
372 /* Display a list of all pastable entries
373 */
374 if(count($this->clean_objects)){
376 $dns = array();
377 foreach($this->clean_objects as $object){
378 $dns[] = $object['dn'];
379 }
381 $smarty = get_smarty();
382 $smarty->assign("type","directly");
383 $smarty->assign("Complete",false);
384 $smarty->assign("AttributesToFix"," ");
385 $smarty->assign("SubDialog","");
386 $smarty->assign("message" , sprintf(_("These objects will be pasted: %s"), "<br>".msgPool::buildList($dns)));
387 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
388 }
390 /* Display a list of all pastable entries
391 */
392 if($this->current || count($this->objects_to_fix)){
393 $this->save_object();
394 if(!$this->current){
395 $key = key($this->objects_to_fix);
396 if(isset($this->objects_to_fix[$key])){
397 $this->current = $this->objects_to_fix[$key];
398 $this->current= $this->_update_vars($this->current);
399 unset($this->objects_to_fix[$key]);
400 unset($this->queue[$key]);
401 }
402 }
403 if($this->current){
404 $smarty = get_smarty();
405 $smarty->assign("type","modified");
406 $smarty->assign("Complete",false);
407 $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
408 $smarty->assign("SubDialog",$this->current['object']->SubDialog);
409 $smarty->assign("objectDN",$this->current['source_data']['dn']);
410 $smarty->assign("message", sprintf(_("This object will be pasted: %s"), "<br><br>".$this->current['source_data']['dn']));
411 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
412 }
413 }
414 return("");
415 }
418 /* Return the dn of the last edited entry */
419 function last_entry()
420 {
421 return($this->lastdn);
422 }
425 /* Save new values posted by copy & paste dialog */
426 function save_object()
427 {
428 if(isset($_POST['abort_current_cut-copy_operation'])){
429 $this->current = FALSE;
430 }
432 if(isset($_POST['abort_all_cut-copy_operations'])){
433 $this->cleanup_queue();
434 $this->current = FALSE;
435 }
436 }
439 /* Create dialog which asks unique attributes/values ...
440 * call tabs -> getCopyDialog()
441 * which calls tab -> getCopyDialog() */
442 function generateAttributesToFix()
443 {
444 if($this->current){
445 return($this->current['object']->getCopyDialog());
446 }
447 }
450 /* Set a single attribute to specified value
451 * example : ("base", $newBase ); */
452 function SetVar($name,$value)
453 {
454 $this->setvar_array[$name]=$value;
455 }
458 /* Update current object attributes, collected via SetVar */
459 function _update_vars($entry)
460 {
461 /* Update all attributes specified with SetVar */
462 foreach($this->setvar_array as $name => $value){
463 if(isset($entry['object']->$name)){
464 $entry['object']->$name = $value;
465 }
466 }
468 /* Walk through tabs */
469 foreach($entry['object']->by_object as $key => $obj){
471 /* Update all attributes specified with SetVar */
472 foreach($this->setvar_array as $name => $value){
474 /* Do not update parent for plugins, this may break things */
475 if($name == "parent") continue;
477 if(isset($entry['object']->by_object[$key]->$name)){
478 $entry['object']->by_object[$key]->$name = $value;
479 }
480 }
481 }
482 return($entry);
483 }
486 /* Returns errors from including tabs. */
487 function check()
488 {
489 $ret = array();
490 foreach($this->current['object']->by_object as $obj){
491 if($obj->is_account || $obj->ignore_account){
492 $ret = array_merge($ret , $obj->check());
493 }
494 }
495 return($ret);
496 }
499 /* returns the paste icon for headpages */
500 function generatePasteIcon()
501 {
502 $Copy_Paste= " <img class='center' src='images/lists/seperator.png' align='middle' alt='' height='16' width='1'> ";
503 if($this->entries_queued()){
504 $img= "images/lists/paste.png";
505 $Copy_Paste.= "<input type='image' name='editPaste' class='center'
506 src='".$img."' alt='"._("Paste")."'> ";
507 }else{
508 $Copy_Paste.= "<img class='center' src='images/lists/paste-grey.png' alt=\""._("Cannot paste")."\"> ";
509 }
510 return ($Copy_Paste);
511 }
512 }
513 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
514 ?>