1 <?php
3 define("LDAP_DUMP_PATH","/tmp/gosa");
5 class CopyPasteHandler {
7 var $config;
8 var $current;
10 var $copyCurrent = false;
11 var $cutCurrent = false;
12 var $dialogOpen = false;
13 var $objectdn = false;
15 var $lastdn = "";
16 var $was_successfull = false;
18 /* this array contains all dns of the currently copyied objects */
19 var $queue = array();
21 /* Create CP handler */
22 function CopyPasteHandler($config)
23 {
24 $this->config = $config;
25 $this->current= NULL;
26 $this->queue = array();
27 }
30 /* Entry entry to Copy & Paste queue.
31 * A Queue entry is represented as follows.
32 * array['file_name'] - Position on hdd
33 * array['method'] - copy/cut
34 * array['dn'] - the dn of the object added to the queue
35 * array['tab_class'] - Tab object that should be used to initialize the new object
36 * array['tab_object'] - Tab object name used to initialize correct object Type like USERTABS
37 */
38 function add_to_queue($dn,$action,$tab_class,$tab_object)
39 {
40 if(!class_exists($tab_class)){
41 trigger_error(sprintf("Specified class object '%s' does not exists.",$tab_class));
42 return(FALSE);
43 }
45 if(!isset($this->config->data['TABS'][$tab_object])){
46 trigger_error(sprintf("Specified tab object '%s' does not exists.",$tab_object));
47 return(FALSE);
48 }
50 if(!in_array($action,array("cut","copy"))){
51 trigger_error(sprintf("Specified action '%s' does not exists for copy & paste.",$action));
52 return(FALSE);
53 }
55 if($file_name = $this->save_dn_attributes_to_hdd($dn)){
56 $tmp = array();
57 $tmp['file_name'] = $file_name;
58 $tmp['method'] = $action;
59 $tmp['dn'] = $dn;
60 $tmp['tab_class'] = $tab_class;
61 $tmp['tab_object']= $tab_object;
62 $this->queue[] = $tmp;
63 }
64 }
67 /* This removes all objects from queue.
68 * Remove hdd dumps of current entries too.
69 * Remove entries older than 24 hours.
70 */
71 function cleanup_queue()
72 {
73 $this->current = FALSE;
75 /* Remove all entries from queue */
76 foreach($this->queue as $key => $entry){
77 @rmdir($entry['file_name']);
78 unset($this->queue[$key]);
79 }
81 /* Remove entries from hdd that are older than24 hours */
82 $fp = opendir(LDAP_DUMP_PATH);
83 while($file = readdir($fp)){
84 if(is_file(LDAP_DUMP_PATH."/".$file) && !preg_match("/^\./",$file)){
85 $file_time = fileatime(LDAP_DUMP_PATH."/".$file);
86 if($file_time < (time() - (24* 60 *60))){
87 @unlink(LDAP_DUMP_PATH."/".$file);
88 }
89 }
90 }
91 }
94 /* To increase performance we save the ldap dump on hdd
95 * This function automatically creates the dumps and returns
96 * the name of the dumpfile we created
97 */
98 function save_dn_attributes_to_hdd($dn)
99 {
100 $filename = "Should not be returned";
101 $ldap = $this->config->get_ldap_link();
102 $ldap->cd($this->config->current['BASE']);
103 $res = $ldap->cat($dn);
105 /* Check if given dn is valid and ldap search was succesfull */
106 if(!$res){
107 print_red(sprintf(_("Specified object '%s' is not a valid ldap object, please check copy & paste methods.")));
108 new log("copy","all/all",$dn,array(),"Could not create dump of ldap object, given object is not present in the ldap database.");
109 return(FALSE);
110 }
112 /* Create data to save given ldap dump on the hdd */
113 $filename = "gosa_copy-paste_dump_".preg_replace("/[^0-9]/","",microtime());
114 $path = LDAP_DUMP_PATH;
116 /* Create patch if it doesn't exists */
117 if(!is_dir($path)){
118 @mkdir($path);
119 }
121 /* check if we are able to create a new file the given directory */
122 if(!is_writeable($path)){
123 print_red(sprintf(_("We are not allowed to save ldap dump to '%s', please check permissions."),$path));
124 new log("copy","all/all",$dn,array(),
125 sprintf("We are not allowed to save ldap dump to '%s', please check permissions.",$path));
126 return(FALSE);
127 }
129 /* Create file handle */
130 $fp = @fopen($path."/".$filename,"w+");
131 if(!$fp){
132 print_red(sprintf(_("We are not allowed to save ldap dump to '%s/%s', please check permissions."),$path,$filename));
133 new log("copy","all/all",$dn,array(),
134 sprintf("We are not allowed to save ldap dump to '%s/%s', please check permissions.",$path,$filename));
135 return(FALSE);
136 }
138 $data = serialize($ldap->fetch());
139 fwrite($fp,$data,strlen($data));
140 fclose($fp);
142 /* Only the webserver should be able to read those files */
143 @chmod($path."/".$filename,0600);
144 return($path."/".$filename);
145 }
148 /* Check if there are still entries the object queue */
149 function entries_queued()
150 {
151 return( count($this->queue) >=1);
152 }
155 /* Paste one entry from queue */
156 function load_entry_from_queue()
157 {
159 /* Save posted variables, handle dialog posts like 'cancel' */
160 $this->save_object();
162 /* If there is currently no object pasted
163 * create new object and prepare object to be pasted
164 */
165 if(!$this->current && $this->entries_queued()){
166 $key = key($this->queue);
167 $entry = $this->queue[$key];
168 $tab_c = $entry['tab_class'];
169 $tab_o = $entry['tab_object'];
170 $entry['object'] = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],"new");
171 $entry['source_data'] = $this->load_attributes_from_hdd($entry['file_name']);
173 /* Prepare each plugin of this tab object to be posted */
174 foreach($entry['object']->by_object as $name => $obj){
176 /* Prepare every single class, to be copied */
177 $entry['object']->by_object[$name]->PrepareForCopyPaste($entry['source_data']);
179 /* handle some special vars */
180 foreach(array("is_account") as $attr){
181 if(isset($entry['source_data'][$attr])){
182 $entry['object']->by_object[$name]->$attr = $entry['source_data'][$attr];
183 }
184 }
185 }
187 /* Assign created object as current */
188 $this->current = $entry;
189 unset($this->queue[$key]);
190 }
191 }
194 /* Load dumped ldap entry specified by $filename and
195 * return data an unserailized data array
196 */
197 function load_attributes_from_hdd($filename)
198 {
199 $fp = @fopen($filename,"r");
200 if(is_file($filename) && is_readable($filename) && $fp){
201 $data = "";
202 while($str = fgets($fp,512)){
203 $data .= $str;
204 }
205 return(unserialize($data));
206 }else{
207 print_red(sprintf(_("Could not load dumped file '%s', from hard disk drive."),$filename));
208 new log("copy","all/all",$dn,array(),
209 sprintf(sprintf("Could not load dumped file '%s', from hard disk drive.",$filename)));
210 return(FALSE);
211 }
212 }
215 /* Displays a dialog which allows the user to fix all dependencies of this object.
216 Create unique names, ids, or what ever */
217 function execute()
218 {
219 $type = $this->current['method'];
220 if($type == "copy"){
221 if(isset($_POST['PerformCopyPaste'])){
222 $msgs = $this->check();
223 if(count ($msgs) ){
224 foreach( $msgs as $msg){
225 print_red($msg);
226 }
227 }else{
228 $this->current['object']->save();
229 $this->lastdn = $this->current['object']->dn;
230 $this->current =FALSE;
232 /* Load next queue entry */
233 $this->load_entry_from_queue();
234 }
235 }
236 if($this->current){
237 $smarty = get_smarty();
238 $smarty->assign("type","copy");
239 $smarty->assign("Complete",false);
240 $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
241 $smarty->assign("SubDialog",$this->current['object']->SubDialog);
242 $smarty->assign("objectDN" ,$this->current['source_data']['dn']);
243 $smarty->assign("message", sprintf(_("You are going to paste the copied entry '%s'."), $this->current['source_data']['dn']));
244 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
245 }
246 }
247 }
250 function last_entry()
251 {
252 return($this->lastdn);
253 }
256 /* Save new values posted by copy & paste dialog */
257 function save_object()
258 {
259 if(isset($_POST['abort_current_cut-copy_operation'])){
260 $this->current = FALSE;
261 }
263 if(isset($_POST['abort_all_cut-copy_operations'])){
264 $this->cleanup_queue();
265 $this->current = FALSE;
266 }
268 /* Assign posted var to all tabs
269 */
270 if($this->current){
271 $this->current['object']->saveCopyDialog();
272 }
273 }
276 /* Create dialog which asks unique attributes/values ...
277 * call tabs -> getCopyDialog()
278 * which calls tab -> getCopyDialog() */
279 function generateAttributesToFix()
280 {
281 if($this->current){
282 return($this->current['object']->getCopyDialog());
283 }
284 }
287 /* Set a single attribute to specified value
288 * example : ("base", $newBase ); */
289 function SetVar($name,$value)
290 {
291 if($this->current){
292 foreach($this->current['object']->by_object as $key => $obj){
293 if(isset($this->current['object']->by_object[$key]->$name)){
294 $this->current['object']->by_object[$key]->$name = $value;
295 }
296 }
297 }
298 }
301 /* Returns errors from including tabs. */
302 function check()
303 {
304 $ret = array();
305 foreach($this->current['object']->by_object as $obj){
306 if($obj->is_account){
307 $ret = array_merge($ret , $obj->check());
308 }
309 }
310 return($ret);
311 }
314 /* returns the paste icon for headpages */
315 function generatePasteIcon()
316 {
317 $Copy_Paste= " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ";
318 if($this->entries_queued()){
319 $img= "images/copypaste.png";
320 $Copy_Paste.= "<input type='image' name='editPaste' class='center'
321 src='".$img."' alt='"._("Paste")."'> ";
322 }else{
323 $Copy_Paste.= "<img class='center' src='images/cant_editpaste.png' alt=\""._("Can't paste")."\"> ";
324 }
326 return ($Copy_Paste);
327 }
346 /******** Functions below are unused and will be rewritten **********/
352 /* Add Object which should be cutted */
353 function Cut_old($obj)
354 {
355 $this->cutCurrent = true;
356 $this->current = $obj;
357 $this->objectdn = $obj->dn;
358 if($this->isCurrentObjectPastAble()){
359 return(true);
360 }else{
361 $this->cutCurrent = $this->copyCurrent = false;
362 $this->obj = NULL;
363 }
364 return(false);
365 }
368 /* Displays a dialog which allows the user to fix all dependencies of this object.
369 Create unique names, ids, or what ever */
370 function execute_old()
371 {
372 print_a($this->current);
374 return;
375 /* Cut & paste
376 */
377 if($this->cutCurrent){
379 if(isset($_POST['PerformCopyPaste'])){
380 $msgs = $this->check();
381 if(count ($msgs) ){
382 foreach( $msgs as $msg){
383 print_red($msg);
384 }
385 }else{
386 $this->current->save();
387 $this->dialogOpen =false;
388 $this->Clear();
389 }
390 }
391 if($this->current){
392 $smarty = get_smarty();
393 $smarty->assign("type","cut");
394 $smarty->assign("Complete",false);
395 $smarty->assign("AttributesToFix"," ");
396 $smarty->assign("SubDialog",$this->current->SubDialog);
397 $smarty->assign("objectDN" ,$this->objectdn);
398 $smarty->assign("message", sprintf(_("You are going to paste the cutted entry '%s'."), $this->objectdn));
399 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
400 }
402 /* Copy & paste
403 */
404 }else{
405 if(isset($_POST['PerformCopyPaste'])){
406 $msgs = $this->check();
407 if(count ($msgs) ){
408 foreach( $msgs as $msg){
409 print_red($msg);
410 }
411 }else{
412 $this->current->save();
413 $this->lastdn = $this->current->dn;
414 $this->dialogOpen =false;
415 $this->Clear();
416 }
417 }
418 if($this->current){
419 $smarty = get_smarty();
420 $smarty->assign("type","copy");
421 $smarty->assign("Complete",false);
422 $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
423 $smarty->assign("SubDialog",$this->current->SubDialog);
424 $smarty->assign("objectDN" ,$this->objectdn);
425 $smarty->assign("message", sprintf(_("You are going to paste the copied entry '%s'."), $this->objectdn));
426 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
427 }
428 }
429 }
431 /* Save new values posted by copy & paste dialog */
432 function save_object_2()
433 {
434 /* Assign posted var to all tabs
435 */
436 if($this->current){
437 $this->current->saveCopyDialog();
438 }
439 }
442 }
443 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
444 ?>