1 <?php
3 define("LDAP_DUMP_PATH","/tmp/gosa");
5 class CopyPasteHandler {
7 var $config;
8 var $current;
10 /* This array contains all dns of the currently copyied objects */
11 var $queue = array();
13 /* Attributes that should be overwritten */
14 var $setvar_array= array();
16 /* The dn of the last edited object */
17 var $lastdn = "";
20 /* Create CP handler */
21 function CopyPasteHandler(&$config)
22 {
23 $this->config = &$config;
24 $this->current= NULL;
25 $this->queue = array();
26 $this->setvar_array = 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,$tab_acl_category)
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 $tmp['tab_acl_category']= $tab_acl_category;
63 $this->queue[] = $tmp;
64 }
65 }
68 /* This removes all objects from queue.
69 * Remove hdd dumps of current entries too.
70 * Remove entries older than 24 hours.
71 */
72 function cleanup_queue()
73 {
74 $this->current = FALSE;
75 $this->setvar_array = array();
77 /* Remove all entries from queue */
78 foreach($this->queue as $key => $entry){
79 @rmdir($entry['file_name']);
80 unset($this->queue[$key]);
81 }
83 /* Create patch if it doesn't exists */
84 if(!is_dir(LDAP_DUMP_PATH)){
85 @mkdir(LDAP_DUMP_PATH);
86 }
88 /* check if we are able to create a new file the given directory */
89 if(!is_writeable(LDAP_DUMP_PATH)){
90 $msg= sprintf(_("Cannot cleanup copy & paste queue: no write permission inside '%s'!"),LDAP_DUMP_PATH);
91 msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
92 new log("copy","all/all","copy & paste, event queue.",array(), $msg);
93 return(FALSE);
94 }
96 /* Remove entries from hdd that are older than24 hours */
97 $fp = opendir(LDAP_DUMP_PATH);
98 while($file = readdir($fp)){
99 if(is_file(LDAP_DUMP_PATH."/".$file) && !preg_match("/^\./",$file)){
100 $file_time = fileatime(LDAP_DUMP_PATH."/".$file);
101 if($file_time < (time() - (24* 60 *60))){
102 @unlink(LDAP_DUMP_PATH."/".$file);
103 }
104 }
105 }
106 }
109 /* To increase performance we save the ldap dump on hdd
110 * This function automatically creates the dumps and returns
111 * the name of the dumpfile we created
112 */
113 function save_dn_attributes_to_hdd($dn)
114 {
115 $filename = "Should not be returned";
116 $ldap = $this->config->get_ldap_link();
117 $ldap->cd($this->config->current['BASE']);
118 $res = $ldap->cat($dn);
120 /* Check if given dn is valid and ldap search was succesfull */
121 if(!$res){
122 $msg= sprintf(_("Copy and paste failed: object '%s' is not a valid ldap object!"), LDAP::fix($dn));
123 msg_dialog::display(_("Internal error"), $msg, ERROR_DIALOG);
124 new log("copy","all/all",$dn,array(), $msg);
125 return(FALSE);
126 }
128 /* Create data to save given ldap dump on the hdd */
129 $filename = "gosa_copy-paste_dump_".preg_replace("/[^0-9]/","",microtime());
130 $path = LDAP_DUMP_PATH;
132 /* Create patch if it doesn't exists */
133 if(!is_dir($path)){
134 @mkdir($path);
135 }
137 /* check if we are able to create a new file the given directory */
138 if(!is_writeable($path)){
139 $msg= sprintf(_("Cannot save LDAP dump: no write permission inside '%s'!"),LDAP_DUMP_PATH);
140 msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
141 new log("copy","all/all",$dn,array(), $msg);
142 return(FALSE);
143 }
145 /* Create file handle */
146 $fp = @fopen($path."/".$filename,"w+");
147 if(!$fp){
148 $msg= sprintf(_("Cannot save LDAP dump: no write permission to '%s/%s'!"),$path,$filename);
149 msg_dialog::display(_("Configuration error"), $msg, ERROR_DIALOG);
150 new log("copy","all/all",$dn,array(), $msg);
151 return(FALSE);
152 }
154 $data = serialize($ldap->fetch());
155 fwrite($fp,$data,strlen($data));
156 fclose($fp);
158 /* Only the webserver should be able to read those files */
159 @chmod($path."/".$filename,0600);
160 return($path."/".$filename);
161 }
164 /* Check if there are still entries the object queue */
165 function entries_queued()
166 {
167 return( count($this->queue) >=1 || $this->current != FALSE);
168 }
171 /* Paste one entry from queue */
172 function load_entry_from_queue()
173 {
175 /* Save posted variables, handle dialog posts like 'cancel' */
176 $this->save_object();
178 /* If there is currently no object pasted
179 * create new object and prepare object to be pasted
180 */
181 if(!$this->current && $this->entries_queued()){
182 $key = key($this->queue);
183 $entry = $this->queue[$key];
184 $tab_c = $entry['tab_class'];
185 $tab_o = $entry['tab_object'];
186 $tab_a = $entry['tab_acl_category'];
188 if($entry['method'] == "copy"){
189 $entry['object'] = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],"new",$tab_a);
190 }else{
191 $entry['object'] = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],$entry['dn'],$tab_a);
192 }
194 $entry['source_data'] = $this->load_attributes_from_hdd($entry['file_name']);
196 if($entry['method'] == "copy"){
198 /* Prepare each plugin of this tab object to be posted */
199 foreach($entry['object']->by_object as $name => $obj){
201 /* Prepare every single class, to be copied */
202 $entry['object']->by_object[$name]->PrepareForCopyPaste($entry['source_data']);
204 /* handle some special vars */
205 foreach(array("is_account") as $attr){
206 if(isset($entry['source_data'][$attr])){
207 $entry['object']->by_object[$name]->$attr = $entry['source_data'][$attr];
208 }
209 }
210 }
211 }
213 /* Assign created object as current */
214 $this->current = $entry;
215 unset($this->queue[$key]);
216 }
217 }
220 /* Load dumped ldap entry specified by $filename and
221 * return data an unserailized data array
222 */
223 function load_attributes_from_hdd($filename)
224 {
225 $fp = @fopen($filename,"r");
226 if(is_file($filename) && is_readable($filename) && $fp){
227 $data = "";
228 while($str = fgets($fp,512)){
229 $data .= $str;
230 }
231 return(unserialize($data));
232 }else{
233 print_red(sprintf(_("Could not load dumped file '%s', from hard disk drive."),$filename));
234 new log("copy","all/all",$dn,array(),
235 sprintf(sprintf("Could not load dumped file '%s', from hard disk drive.",$filename)));
236 return(FALSE);
237 }
238 }
241 /* Displays a dialog which allows the user to fix all dependencies of this object.
242 Create unique names, ids, or what ever */
243 function execute()
244 {
245 $ui = get_userinfo();
246 $type = $this->current['method'];
247 if($type == "cut"){
248 if(isset($_POST['PerformCopyPaste'])){
249 while($this->entries_queued()){
250 $this->load_entry_from_queue();
251 $this->_update_vars();
253 $msgs = $this->check();
254 $acl = $ui->get_category_permissions($this->current['dn'], $this->current['tab_acl_category']);
256 /* Check permissions */
257 if(!preg_match("/((c|w)|(w|c))/",$acl)){
258 print_red(sprintf(_("You are not allowed to cut and paste the following object '%s'."),$this->current['dn']));
259 }elseif(count ($msgs) ){
260 foreach( $msgs as $msg){
261 print_red($msg);
262 }
263 }else{
265 /* Load next queue entry */
266 $this->lastdn = $this->current['object']->dn;
267 $this->current['object']->save();
268 }
270 $this->current =FALSE;
271 }
272 }
273 if($this->current){
275 $dns = $this->current['dn']."\n";
276 foreach($this->queue as $entry){
277 $dns .= $entry['dn']."\n";
278 }
280 $smarty = get_smarty();
281 $smarty->assign("type","cut");
282 $smarty->assign("Complete",false);
283 $smarty->assign("AttributesToFix"," ");
284 $smarty->assign("SubDialog",$this->current['object']->SubDialog);
285 $smarty->assign("objectDN" ,$dns);
286 $smarty->assign("message", sprintf(_("You are going to paste the cutted entry '%s'."), "<pre>".$dns."</pre>"));
287 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
288 }
289 }
290 if($type == "copy"){
291 if(isset($_POST['PerformCopyPaste'])){
292 $this->_update_vars();
293 $msgs = $this->check();
295 $acl = $ui->get_category_permissions($this->current['dn'], $this->current['tab_acl_category']);
297 /* Check permissions */
298 if(!preg_match("/((c|w)|(w|c))/",$acl)){
299 print_red(sprintf(_("You are not allowed to copy and paste the following object '%s'."),$this->current['dn']));
300 }elseif(count ($msgs) ){
301 foreach( $msgs as $msg){
302 print_red($msg);
303 }
304 }else{
305 $this->current['object']->save();
306 $this->lastdn = $this->current['object']->dn;
307 $this->current =FALSE;
309 /* Load next queue entry */
310 $this->load_entry_from_queue();
311 }
312 }
313 if($this->current){
314 $smarty = get_smarty();
315 $smarty->assign("type","copy");
316 $smarty->assign("Complete",false);
317 $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
318 $smarty->assign("SubDialog",$this->current['object']->SubDialog);
319 $smarty->assign("objectDN" ,$this->current['source_data']['dn']);
320 $smarty->assign("message", sprintf(_("You are going to paste the copied entry '%s'."), $this->current['source_data']['dn']));
321 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
322 }
323 }
324 }
327 /* Return the dn of the last edited entry */
328 function last_entry()
329 {
330 return($this->lastdn);
331 }
334 /* Save new values posted by copy & paste dialog */
335 function save_object()
336 {
337 if(isset($_POST['abort_current_cut-copy_operation'])){
338 $this->current = FALSE;
339 }
341 if(isset($_POST['abort_all_cut-copy_operations'])){
342 $this->cleanup_queue();
343 $this->current = FALSE;
344 }
346 /* Assign posted var to all tabs
347 */
348 if($this->current){
349 $this->current['object']->saveCopyDialog();
350 }
351 }
354 /* Create dialog which asks unique attributes/values ...
355 * call tabs -> getCopyDialog()
356 * which calls tab -> getCopyDialog() */
357 function generateAttributesToFix()
358 {
359 if($this->current){
360 return($this->current['object']->getCopyDialog());
361 }
362 }
365 /* Set a single attribute to specified value
366 * example : ("base", $newBase ); */
367 function SetVar($name,$value)
368 {
369 $this->setvar_array[$name]=$value;
370 }
373 /* Update current object attributes, collected via SetVar */
374 function _update_vars()
375 {
376 if($this->current){
378 /* Update all attributes specified with SetVar */
379 foreach($this->setvar_array as $name => $value){
380 if(isset($this->current['object']->$name)){
381 $this->current['object']->$name = $value;
382 }
383 }
385 /* Walk through tabs */
386 foreach($this->current['object']->by_object as $key => $obj){
388 /* Update all attributes specified with SetVar */
389 foreach($this->setvar_array as $name => $value){
390 if(isset($this->current['object']->by_object[$key]->$name)){
391 $this->current['object']->by_object[$key]->$name = $value;
392 }
393 }
394 }
395 }
396 }
399 /* Returns errors from including tabs. */
400 function check()
401 {
402 $ret = array();
403 foreach($this->current['object']->by_object as $obj){
404 if($obj->is_account || $obj->ignore_account){
405 $ret = array_merge($ret , $obj->check());
406 }
407 }
408 return($ret);
409 }
412 /* returns the paste icon for headpages */
413 function generatePasteIcon()
414 {
415 $Copy_Paste= " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ";
416 if($this->entries_queued()){
417 $img= "images/copypaste.png";
418 $Copy_Paste.= "<input type='image' name='editPaste' class='center'
419 src='".$img."' alt='"._("Paste")."'> ";
420 }else{
421 $Copy_Paste.= "<img class='center' src='images/cant_editpaste.png' alt=\""._("Can't paste")."\"> ";
422 }
423 return ($Copy_Paste);
424 }
425 }
426 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
427 ?>