c11849e2fffc857f8a0864a6ef65bb3b1f239f4b
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 /* Remove entries from hdd that are older than24 hours */
84 $fp = opendir(LDAP_DUMP_PATH);
85 while($file = readdir($fp)){
86 if(is_file(LDAP_DUMP_PATH."/".$file) && !preg_match("/^\./",$file)){
87 $file_time = fileatime(LDAP_DUMP_PATH."/".$file);
88 if($file_time < (time() - (24* 60 *60))){
89 @unlink(LDAP_DUMP_PATH."/".$file);
90 }
91 }
92 }
93 }
96 /* To increase performance we save the ldap dump on hdd
97 * This function automatically creates the dumps and returns
98 * the name of the dumpfile we created
99 */
100 function save_dn_attributes_to_hdd($dn)
101 {
102 $filename = "Should not be returned";
103 $ldap = $this->config->get_ldap_link();
104 $ldap->cd($this->config->current['BASE']);
105 $res = $ldap->cat($dn);
107 /* Check if given dn is valid and ldap search was succesfull */
108 if(!$res){
109 print_red(sprintf(_("Specified object '%s' is not a valid ldap object, please check copy & paste methods.")));
110 new log("copy","all/all",$dn,array(),"Could not create dump of ldap object, given object is not present in the ldap database.");
111 return(FALSE);
112 }
114 /* Create data to save given ldap dump on the hdd */
115 $filename = "gosa_copy-paste_dump_".preg_replace("/[^0-9]/","",microtime());
116 $path = LDAP_DUMP_PATH;
118 /* Create patch if it doesn't exists */
119 if(!is_dir($path)){
120 @mkdir($path);
121 }
123 /* check if we are able to create a new file the given directory */
124 if(!is_writeable($path)){
125 print_red(sprintf(_("We are not allowed to save ldap dump to '%s', please check permissions."),$path));
126 new log("copy","all/all",$dn,array(),
127 sprintf("We are not allowed to save ldap dump to '%s', please check permissions.",$path));
128 return(FALSE);
129 }
131 /* Create file handle */
132 $fp = @fopen($path."/".$filename,"w+");
133 if(!$fp){
134 print_red(sprintf(_("We are not allowed to save ldap dump to '%s/%s', please check permissions."),$path,$filename));
135 new log("copy","all/all",$dn,array(),
136 sprintf("We are not allowed to save ldap dump to '%s/%s', please check permissions.",$path,$filename));
137 return(FALSE);
138 }
140 $data = serialize($ldap->fetch());
141 fwrite($fp,$data,strlen($data));
142 fclose($fp);
144 /* Only the webserver should be able to read those files */
145 @chmod($path."/".$filename,0600);
146 return($path."/".$filename);
147 }
150 /* Check if there are still entries the object queue */
151 function entries_queued()
152 {
153 return( count($this->queue) >=1 || $this->current != FALSE);
154 }
157 /* Paste one entry from queue */
158 function load_entry_from_queue()
159 {
161 /* Save posted variables, handle dialog posts like 'cancel' */
162 $this->save_object();
164 /* If there is currently no object pasted
165 * create new object and prepare object to be pasted
166 */
167 if(!$this->current && $this->entries_queued()){
168 $key = key($this->queue);
169 $entry = $this->queue[$key];
170 $tab_c = $entry['tab_class'];
171 $tab_o = $entry['tab_object'];
172 $tab_a = $entry['tab_acl_category'];
174 if($entry['method'] == "copy"){
175 $entry['object'] = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],"new",$tab_a);
176 }else{
177 $entry['object'] = new $tab_c($this->config,$this->config->data['TABS'][$tab_o],$entry['dn'],$tab_a);
178 }
180 $entry['source_data'] = $this->load_attributes_from_hdd($entry['file_name']);
182 if($entry['method'] == "copy"){
184 /* Prepare each plugin of this tab object to be posted */
185 foreach($entry['object']->by_object as $name => $obj){
187 /* Prepare every single class, to be copied */
188 $entry['object']->by_object[$name]->PrepareForCopyPaste($entry['source_data']);
190 /* handle some special vars */
191 foreach(array("is_account") as $attr){
192 if(isset($entry['source_data'][$attr])){
193 $entry['object']->by_object[$name]->$attr = $entry['source_data'][$attr];
194 }
195 }
196 }
197 }
199 /* Assign created object as current */
200 $this->current = $entry;
201 unset($this->queue[$key]);
202 }
203 }
206 /* Load dumped ldap entry specified by $filename and
207 * return data an unserailized data array
208 */
209 function load_attributes_from_hdd($filename)
210 {
211 $fp = @fopen($filename,"r");
212 if(is_file($filename) && is_readable($filename) && $fp){
213 $data = "";
214 while($str = fgets($fp,512)){
215 $data .= $str;
216 }
217 return(unserialize($data));
218 }else{
219 print_red(sprintf(_("Could not load dumped file '%s', from hard disk drive."),$filename));
220 new log("copy","all/all",$dn,array(),
221 sprintf(sprintf("Could not load dumped file '%s', from hard disk drive.",$filename)));
222 return(FALSE);
223 }
224 }
227 /* Displays a dialog which allows the user to fix all dependencies of this object.
228 Create unique names, ids, or what ever */
229 function execute()
230 {
231 $ui = get_userinfo();
232 $type = $this->current['method'];
233 if($type == "cut"){
234 if(isset($_POST['PerformCopyPaste'])){
235 while($this->entries_queued()){
236 $this->load_entry_from_queue();
237 $this->_update_vars();
239 $msgs = $this->check();
240 $acl = $ui->get_category_permissions($this->current['dn'], $this->current['tab_acl_category']);
242 /* Check permissions */
243 if(!preg_match("/((c|w)|(w|c))/",$acl)){
244 print_red(sprintf(_("You are not allowed to cut and paste the following object '%s'."),$this->current['dn']));
245 }elseif(count ($msgs) ){
246 foreach( $msgs as $msg){
247 print_red($msg);
248 }
249 }else{
251 /* Load next queue entry */
252 $this->lastdn = $this->current['object']->dn;
253 $this->current['object']->save();
254 }
256 $this->current =FALSE;
257 }
258 }
259 if($this->current){
261 $dns = $this->current['dn']."\n";
262 foreach($this->queue as $entry){
263 $dns .= $entry['dn']."\n";
264 }
266 $smarty = get_smarty();
267 $smarty->assign("type","cut");
268 $smarty->assign("Complete",false);
269 $smarty->assign("AttributesToFix"," ");
270 $smarty->assign("SubDialog",$this->current['object']->SubDialog);
271 $smarty->assign("objectDN" ,$dns);
272 $smarty->assign("message", sprintf(_("You are going to paste the cutted entry '%s'."), "<pre>".$dns."</pre>"));
273 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
274 }
275 }
276 if($type == "copy"){
277 if(isset($_POST['PerformCopyPaste'])){
278 $this->_update_vars();
279 $msgs = $this->check();
281 $acl = $ui->get_category_permissions($this->current['dn'], $this->current['tab_acl_category']);
283 /* Check permissions */
284 if(!preg_match("/((c|w)|(w|c))/",$acl)){
285 print_red(sprintf(_("You are not allowed to copy and paste the following object '%s'."),$this->current['dn']));
286 }elseif(count ($msgs) ){
287 foreach( $msgs as $msg){
288 print_red($msg);
289 }
290 }else{
291 $this->current['object']->save();
292 $this->lastdn = $this->current['object']->dn;
293 $this->current =FALSE;
295 /* Load next queue entry */
296 $this->load_entry_from_queue();
297 }
298 }
299 if($this->current){
300 $smarty = get_smarty();
301 $smarty->assign("type","copy");
302 $smarty->assign("Complete",false);
303 $smarty->assign("AttributesToFix",$this->generateAttributesToFix());
304 $smarty->assign("SubDialog",$this->current['object']->SubDialog);
305 $smarty->assign("objectDN" ,$this->current['source_data']['dn']);
306 $smarty->assign("message", sprintf(_("You are going to paste the copied entry '%s'."), $this->current['source_data']['dn']));
307 return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
308 }
309 }
310 }
313 /* Return the dn of the last edited entry */
314 function last_entry()
315 {
316 return($this->lastdn);
317 }
320 /* Save new values posted by copy & paste dialog */
321 function save_object()
322 {
323 if(isset($_POST['abort_current_cut-copy_operation'])){
324 $this->current = FALSE;
325 }
327 if(isset($_POST['abort_all_cut-copy_operations'])){
328 $this->cleanup_queue();
329 $this->current = FALSE;
330 }
332 /* Assign posted var to all tabs
333 */
334 if($this->current){
335 $this->current['object']->saveCopyDialog();
336 }
337 }
340 /* Create dialog which asks unique attributes/values ...
341 * call tabs -> getCopyDialog()
342 * which calls tab -> getCopyDialog() */
343 function generateAttributesToFix()
344 {
345 if($this->current){
346 return($this->current['object']->getCopyDialog());
347 }
348 }
351 /* Set a single attribute to specified value
352 * example : ("base", $newBase ); */
353 function SetVar($name,$value)
354 {
355 $this->setvar_array[$name]=$value;
356 }
359 /* Update current object attributes, collected via SetVar */
360 function _update_vars()
361 {
362 if($this->current){
364 /* Update all attributes specified with SetVar */
365 foreach($this->setvar_array as $name => $value){
366 if(isset($this->current['object']->$name)){
367 $this->current['object']->$name = $value;
368 }
369 }
371 /* Walk through tabs */
372 foreach($this->current['object']->by_object as $key => $obj){
374 /* Update all attributes specified with SetVar */
375 foreach($this->setvar_array as $name => $value){
376 if(isset($this->current['object']->by_object[$key]->$name)){
377 $this->current['object']->by_object[$key]->$name = $value;
378 }
379 }
380 }
381 }
382 }
385 /* Returns errors from including tabs. */
386 function check()
387 {
388 $ret = array();
389 foreach($this->current['object']->by_object as $obj){
390 if($obj->is_account){
391 $ret = array_merge($ret , $obj->check());
392 }
393 }
394 return($ret);
395 }
398 /* returns the paste icon for headpages */
399 function generatePasteIcon()
400 {
401 $Copy_Paste= " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ";
402 if($this->entries_queued()){
403 $img= "images/copypaste.png";
404 $Copy_Paste.= "<input type='image' name='editPaste' class='center'
405 src='".$img."' alt='"._("Paste")."'> ";
406 }else{
407 $Copy_Paste.= "<img class='center' src='images/cant_editpaste.png' alt=\""._("Can't paste")."\"> ";
408 }
409 return ($Copy_Paste);
410 }
411 }
412 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
413 ?>