3fda66e3397645bf20c37e63868c42398ad39215
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 echo "validate given data here !!!!!!!";
42 if($file_name = $this->save_dn_attributes_to_hdd($dn)){
43 $tmp = array();
44 $tmp['file_name'] = $file_name;
45 $tmp['method'] = $action;
46 $tmp['dn'] = $dn;
47 $tmp['tab_class'] = $tab_class;
48 $tmp['tab_object']= $tab_object;
49 $this->queue[] = $tmp;
50 }
51 }
54 /* This removes all objects from queue.
55 * Remove hdd dumps of current entries too.
56 * Remove entries older than 24 hours.
57 */
58 function cleanup_queue()
59 {
60 $this->current = FALSE;
62 /* Remove all entries from queue */
63 foreach($this->queue as $key => $entry){
64 @rmdir($entry['file_name']);
65 unset($this->queue[$key]);
66 }
68 /* Remove entries from hdd that are older than24 hours */
69 $fp = opendir(LDAP_DUMP_PATH);
70 while($file = readdir($fp)){
71 if(is_file(LDAP_DUMP_PATH."/".$file) && !preg_match("/^\./",$file)){
72 $file_time = fileatime(LDAP_DUMP_PATH."/".$file);
73 if($file_time < (time() - (24* 60 *60))){
74 @unlink(LDAP_DUMP_PATH."/".$file);
75 }
76 }
77 }
78 }
81 /* To increase performance we save the ldap dump on hdd
82 * This function automatically creates the dumps and returns
83 * the name of the dumpfile we created
84 */
85 function save_dn_attributes_to_hdd($dn)
86 {
87 $filename = "Should not be returned";
88 $ldap = $this->config->get_ldap_link();
89 $ldap->cd($this->config->current['BASE']);
90 $res = $ldap->cat($dn);
92 /* Check if given dn is valid and ldap search was succesfull */
93 if(!$res){
94 print_red(sprintf(_("Specified object '%s' is not a valid ldap object, please check copy & paste methods.")));
95 new log("copy","all/all",$dn,array(),"Could not create dump of ldap object, given object is not present in the ldap database.");
96 return(FALSE);
97 }
99 /* Create data to save given ldap dump on the hdd */
100 $filename = "gosa_copy-paste_dump_".preg_replace("/[^0-9]/","",microtime());
101 $path = LDAP_DUMP_PATH;
103 /* Create patch if it doesn't exists */
104 if(!is_dir($path)){
105 @mkdir($path);
106 }
108 /* check if we are able to create a new file the given directory */
109 if(!is_writeable($path)){
110 print_red(sprintf(_("We are not allowed to save ldap dump to '%s', please check permissions."),$path));
111 new log("copy","all/all",$dn,array(),
112 sprintf("We are not allowed to save ldap dump to '%s', please check permissions.",$path));
113 return(FALSE);
114 }
116 /* Create file handle */
117 $fp = @fopen($path."/".$filename,"w+");
118 if(!$fp){
119 print_red(sprintf(_("We are not allowed to save ldap dump to '%s/%s', please check permissions."),$path,$filename));
120 new log("copy","all/all",$dn,array(),
121 sprintf("We are not allowed to save ldap dump to '%s/%s', please check permissions.",$path,$filename));
122 return(FALSE);
123 }
125 $data = serialize($ldap->fetch());
126 fwrite($fp,$data,strlen($data));
127 fclose($fp);
129 /* Only the webserver should be able to read those files */
130 @chmod($path."/".$filename,0600);
131 return($path."/".$filename);
132 }
135 /* Check if there are still entries the object queue */
136 function entries_queued()
137 {
138 return( count($this->queue) >=1);
139 }
142 /* Paste one entry from queue */
143 function paste_entries()
144 {
146 /* Save posted variables, handle dialog posts like 'cancel' */
147 $this->save_object();
149 /* If there is currently no object pasted
150 * create new object and prepare object to be pasted
151 */
152 if(!$this->current && $this->entries_queued()){
153 $key = key($this->queue);
154 $entry = $this->queue[$key];
155 $tab_c = $entry['tab_class'];
156 $tab_o = $entry['tab_object'];
157 $entry['object'] = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],"new");
158 $entry['source_data'] = $this->load_attributes_from_hdd($entry['file_name']);
160 /* Prepare each plugin of this tab object to be posted */
161 foreach($entry['object']->by_object as $name => $obj){
163 /* Prepare every single class, to be copied */
164 $entry['object']->by_object[$name]->PrepareForCopyPaste($entry['source_data']);
166 /* handle some special vars */
167 foreach(array("is_account") as $attr){
168 if(isset($entry['source_data'][$attr])){
169 $entry['object']->by_object[$name]->$attr = $entry['source_data'][$attr];
170 }
171 }
172 }
174 /* Assign created object as current */
175 $this->current = $entry;
176 unset($this->queue[$key]);
177 }
179 return($this->execute());
180 }
183 /* Load dumped ldap entry specified by $filename and
184 * return data an unserailized data array
185 */
186 function load_attributes_from_hdd($filename)
187 {
188 $fp = @fopen($filename,"r");
189 if(is_file($filename) && is_readable($filename) && $fp){
190 $data = "";
191 while($str = fgets($fp,512)){
192 $data .= $str;
193 }
194 return(unserialize($data));
195 }else{
196 print_red(sprintf(_("Could not load dumped file '%s', from hard disk drive."),$filename));
197 new log("copy","all/all",$dn,array(),
198 sprintf(sprintf("Could not load dumped file '%s', from hard disk drive.",$filename)));
199 return(FALSE);
200 }
201 }
204 /* Displays a dialog which allows the user to fix all dependencies of this object.
205 Create unique names, ids, or what ever */
206 function execute()
207 {
208 $type = $this->current['method'];
209 if($type == "copy"){
210 if(isset($_POST['PerformCopyPaste'])){
211 $msgs = $this->check();
212 if(count ($msgs) ){
213 foreach( $msgs as $msg){
214 print_red($msg);
215 }
216 }else{
217 $this->current['object']->save();
218 $this->lastdn = $this->current['object']->dn;
219 $this->current =FALSE;
220 }
221 }
222 if($this->current){
223 $smarty = get_smarty();
224 $smarty->assign("type","copy");
225 $smarty->assign("Complete",false);
226 $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
227 $smarty->assign("SubDialog",$this->current['object']->SubDialog);
228 $smarty->assign("objectDN" ,$this->current['source_data']['dn']);
229 $smarty->assign("message", sprintf(_("You are going to paste the copied entry '%s'."), $this->current['source_data']['dn']));
230 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
231 }
232 }
233 }
236 /* Save new values posted by copy & paste dialog */
237 function save_object()
238 {
239 if(isset($_POST['abort_current_cut-copy_operation'])){
240 $this->current = FALSE;
241 }
243 if(isset($_POST['abort_all_cut-copy_operations'])){
244 $this->cleanup_queue();
245 $this->current = FALSE;
246 }
248 /* Assign posted var to all tabs
249 */
250 if($this->current){
251 $this->current['object']->saveCopyDialog();
252 }
253 }
256 /* Create dialog which asks unique attributes/values ...
257 * call tabs -> getCopyDialog()
258 * which calls tab -> getCopyDialog() */
259 function generateAttributesToFix()
260 {
261 if($this->current){
262 return($this->current['object']->getCopyDialog());
263 }
264 }
267 /* Set a single attribute to specified value
268 * example : ("base", $newBase ); */
269 function SetVar($name,$value)
270 {
271 echo $name.$value."<br>";
272 foreach($this-> current->by_object as $key => $obj){
273 if(isset($this->current->by_object[$key]->$name)){
274 $this->current->by_object[$key]->$name = $value;
275 }
276 }
277 }
280 /* Returns errors from including tabs. */
281 function check()
282 {
283 $ret = array();
284 foreach($this->current['object']->by_object as $obj){
285 if($obj->is_account){
286 $ret = array_merge($ret , $obj->check());
287 }
288 }
289 return($ret);
290 }
293 /* returns the paste icon for headpages */
294 function generatePasteIcon()
295 {
296 $Copy_Paste= " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ";
297 if($this->entries_queued()){
298 $img= "images/copypaste.png";
299 $Copy_Paste.= "<input type='image' name='editPaste' class='center'
300 src='".$img."' alt='"._("Paste")."'> ";
301 }else{
302 $Copy_Paste.= "<img class='center' src='images/cant_editpaste.png' alt=\""._("Can't paste")."\"> ";
303 }
305 return ($Copy_Paste);
306 }
325 /******** Functions below are unused and will be rewritten **********/
331 /* Add Object which should be cutted */
332 function Cut_old($obj)
333 {
334 $this->cutCurrent = true;
335 $this->current = $obj;
336 $this->objectdn = $obj->dn;
337 if($this->isCurrentObjectPastAble()){
338 return(true);
339 }else{
340 $this->cutCurrent = $this->copyCurrent = false;
341 $this->obj = NULL;
342 }
343 return(false);
344 }
347 /* Displays a dialog which allows the user to fix all dependencies of this object.
348 Create unique names, ids, or what ever */
349 function execute_old()
350 {
351 print_a($this->current);
353 return;
354 /* Cut & paste
355 */
356 if($this->cutCurrent){
358 if(isset($_POST['PerformCopyPaste'])){
359 $msgs = $this->check();
360 if(count ($msgs) ){
361 foreach( $msgs as $msg){
362 print_red($msg);
363 }
364 }else{
365 $this->current->save();
366 $this->dialogOpen =false;
367 $this->Clear();
368 }
369 }
370 if($this->current){
371 $smarty = get_smarty();
372 $smarty->assign("type","cut");
373 $smarty->assign("Complete",false);
374 $smarty->assign("AttributesToFix"," ");
375 $smarty->assign("SubDialog",$this->current->SubDialog);
376 $smarty->assign("objectDN" ,$this->objectdn);
377 $smarty->assign("message", sprintf(_("You are going to paste the cutted entry '%s'."), $this->objectdn));
378 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
379 }
381 /* Copy & paste
382 */
383 }else{
384 if(isset($_POST['PerformCopyPaste'])){
385 $msgs = $this->check();
386 if(count ($msgs) ){
387 foreach( $msgs as $msg){
388 print_red($msg);
389 }
390 }else{
391 $this->current->save();
392 $this->lastdn = $this->current->dn;
393 $this->dialogOpen =false;
394 $this->Clear();
395 }
396 }
397 if($this->current){
398 $smarty = get_smarty();
399 $smarty->assign("type","copy");
400 $smarty->assign("Complete",false);
401 $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
402 $smarty->assign("SubDialog",$this->current->SubDialog);
403 $smarty->assign("objectDN" ,$this->objectdn);
404 $smarty->assign("message", sprintf(_("You are going to paste the copied entry '%s'."), $this->objectdn));
405 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
406 }
407 }
408 }
410 /* Save new values posted by copy & paste dialog */
411 function save_object_2()
412 {
413 /* Assign posted var to all tabs
414 */
415 if($this->current){
416 $this->current->saveCopyDialog();
417 }
418 }
421 }
422 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
423 ?>