e18c26b7c1cf37c130d3185a3f3a1d9d402aa80b
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 = "";
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 }
116 /* Update folder permissions */
117 if(!@chmod(LDAP_DUMP_PATH,0700)){
118 $msg= sprintf(_("Cannot cleanup copy & paste queue: setting permissions for '%s' failed!"),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 }
124 /* check if we are able to create a new file the given directory */
125 if(!is_writeable(LDAP_DUMP_PATH)){
126 $msg= sprintf(_("Cannot cleanup copy & paste queue: no write permission inside '%s'!"),LDAP_DUMP_PATH);
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: object '%s' is not a valid 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(_("Cannot save LDAP dump: no write permission inside '%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(_("Cannot save LDAP dump: setting permissions for '%s' failed!"),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= sprintf(_("Cannot save LDAP dump: no write permission to '%s/%s'!"),$path,$filename);
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(_("Cannot load dumped file '%s'!"),$filename);
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 $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 }
355 /* Display a list of all pastable entries
356 */
357 if(count($this->clean_objects)){
358 $dns = "";
359 foreach($this->clean_objects as $entry){
360 $dns .= $entry['dn']."\n";
361 }
362 $smarty = get_smarty();
363 $smarty->assign("type","directly");
364 $smarty->assign("Complete",false);
365 $smarty->assign("AttributesToFix"," ");
366 $smarty->assign("SubDialog","");
367 $smarty->assign("objectDN" ,$dns);
368 $smarty->assign("message" , sprintf(_("You are going to paste the following entries '%s'."), "<pre>".$dns."</pre>"));
369 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
370 }
372 /* Display a list of all pastable entries
373 */
374 if($this->current || count($this->objects_to_fix)){
375 $this->save_object();
376 if(!$this->current){
377 $key = key($this->objects_to_fix);
378 if(isset($this->objects_to_fix[$key])){
379 $this->current = $this->objects_to_fix[$key];
380 unset($this->objects_to_fix[$key]);
381 unset($this->queue[$key]);
382 }
383 }
384 if($this->current){
385 $smarty = get_smarty();
386 $smarty->assign("type","modified");
387 $smarty->assign("Complete",false);
388 $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
389 $smarty->assign("SubDialog",$this->current['object']->SubDialog);
390 $smarty->assign("objectDN",$this->current['source_data']['dn']);
391 $smarty->assign("message", sprintf(_("You are going to paste the following entry '%s'."), $this->current['source_data']['dn']));
392 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
393 }
394 }
395 return("");
396 }
399 /* Return the dn of the last edited entry */
400 function last_entry()
401 {
402 return($this->lastdn);
403 }
406 /* Save new values posted by copy & paste dialog */
407 function save_object()
408 {
409 if(isset($_POST['abort_current_cut-copy_operation'])){
410 $this->current = FALSE;
411 }
413 if(isset($_POST['abort_all_cut-copy_operations'])){
414 $this->cleanup_queue();
415 $this->current = FALSE;
416 }
417 }
420 /* Create dialog which asks unique attributes/values ...
421 * call tabs -> getCopyDialog()
422 * which calls tab -> getCopyDialog() */
423 function generateAttributesToFix()
424 {
425 if($this->current){
426 return($this->current['object']->getCopyDialog());
427 }
428 }
431 /* Set a single attribute to specified value
432 * example : ("base", $newBase ); */
433 function SetVar($name,$value)
434 {
435 $this->setvar_array[$name]=$value;
436 }
439 /* Update current object attributes, collected via SetVar */
440 function _update_vars($entry)
441 {
442 /* Update all attributes specified with SetVar */
443 foreach($this->setvar_array as $name => $value){
444 if(isset($entry['object']->$name)){
445 $entry['object']->$name = $value;
446 }
447 }
449 /* Walk through tabs */
450 foreach($entry['object']->by_object as $key => $obj){
452 /* Update all attributes specified with SetVar */
453 foreach($this->setvar_array as $name => $value){
454 if(isset($entry['object']->by_object[$key]->$name)){
455 $entry['object']->by_object[$key]->$name = $value;
456 }
457 }
458 }
459 return($entry);
460 }
463 /* Returns errors from including tabs. */
464 function check()
465 {
466 $ret = array();
467 foreach($this->current['object']->by_object as $obj){
468 if($obj->is_account || $obj->ignore_account){
469 $ret = array_merge($ret , $obj->check());
470 }
471 }
472 return($ret);
473 }
476 /* returns the paste icon for headpages */
477 function generatePasteIcon()
478 {
479 $Copy_Paste= " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ";
480 if($this->entries_queued()){
481 $img= "images/copypaste.png";
482 $Copy_Paste.= "<input type='image' name='editPaste' class='center'
483 src='".$img."' alt='"._("Paste")."'> ";
484 }else{
485 $Copy_Paste.= "<img class='center' src='images/cant_editpaste.png' alt=\""._("Can't paste")."\"> ";
486 }
487 return ($Copy_Paste);
488 }
489 }
490 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
491 ?>