]> git.p6c8.net - devedit.git/blob - modules/Command.pm
Now we already unlock the file at the begining of exec_endedit(), because if an
[devedit.git] / modules / Command.pm
1 package Command;
2
3 #
4 # Dev-Editor - Module Command
5 #
6 # Execute Dev-Editor's commands
7 #
8 # Author: Patrick Canterino <patshaping@gmx.net>
9 # Last modified: 2004-10-22
10 #
11
12 use strict;
13
14 use vars qw(@EXPORT);
15
16 use File::Access;
17 use File::Copy;
18 use File::Path;
19
20 use POSIX qw(strftime);
21 use Tool;
22
23 use CGI qw(header);
24 use HTML::Entities;
25 use Output;
26 use Template;
27
28 my $script = $ENV{'SCRIPT_NAME'};
29 my $users = eval("getpwuid(0)") && eval("getgrgid(0)");
30
31 my %dispatch = ('show' => \&exec_show,
32 'beginedit' => \&exec_beginedit,
33 'canceledit' => \&exec_canceledit,
34 'endedit' => \&exec_endedit,
35 'mkdir' => \&exec_mkdir,
36 'mkfile' => \&exec_mkfile,
37 'upload' => \&exec_upload,
38 'copy' => \&exec_copy,
39 'rename' => \&exec_rename,
40 'remove' => \&exec_remove,
41 'chprop' => \&exec_chprop,
42 'unlock' => \&exec_unlock,
43 'about' => \&exec_about
44 );
45
46 ### Export ###
47
48 use base qw(Exporter);
49
50 @EXPORT = qw(exec_command);
51
52 # exec_command()
53 #
54 # Execute the specified command
55 #
56 # Params: 1. Command to execute
57 # 2. Reference to user input hash
58 # 3. Reference to config hash
59 #
60 # Return: Output of the command (Scalar Reference)
61
62 sub exec_command($$$)
63 {
64 my ($command,$data,$config) = @_;
65
66 foreach(keys(%dispatch))
67 {
68 if(lc($_) eq lc($command))
69 {
70 my $output = &{$dispatch{$_}}($data,$config);
71 return $output;
72 }
73 }
74
75 return error($config->{'errors'}->{'cmd_unknown'},'/',{COMMAND => $command});
76 }
77
78 # exec_show()
79 #
80 # View a directory or a file
81 #
82 # Params: 1. Reference to user input hash
83 # 2. Reference to config hash
84 #
85 # Return: Output of the command (Scalar Reference)
86
87 sub exec_show($$)
88 {
89 my ($data,$config) = @_;
90 my $physical = $data->{'physical'};
91 my $virtual = $data->{'virtual'};
92 my $uselist = $data->{'uselist'};
93
94 my $tpl = new Template;
95
96 if(-d $physical)
97 {
98 # Create directory listing
99
100 my $direntries = dir_read($physical);
101 return error($config->{'dir_read_failed'},upper_path($virtual),{DIR => '$virtual'}) unless($direntries);
102
103 my $files = $direntries->{'files'};
104 my $dirs = $direntries->{'dirs'};
105
106 my $dirlist = "";
107
108 # Create the link to the upper directory
109 # (only if we are not in the root directory)
110
111 unless($virtual eq "/")
112 {
113 my @stat = stat($physical."/..");
114
115 my $udtpl = new Template;
116 $udtpl->read_file($config->{'templates'}->{'dirlist_up'});
117
118 $udtpl->fillin("UPPER_DIR",encode_entities(upper_path($virtual)));
119 $udtpl->fillin("DATE",strftime($config->{'timeformat'},localtime($stat[9])));
120
121 $dirlist .= $udtpl->get_template;
122 }
123
124 # Directories
125
126 foreach my $dir(@$dirs)
127 {
128 my @stat = stat($physical."/".$dir);
129 my $virt_path = encode_entities($virtual.$dir."/");
130
131 my $dtpl = new Template;
132 $dtpl->read_file($config->{'templates'}->{'dirlist_dir'});
133
134 $dtpl->fillin("DIR",$virt_path);
135 $dtpl->fillin("DIR_NAME",$dir);
136 $dtpl->fillin("DATE",strftime($config->{'timeformat'},localtime($stat[9])));
137 $dtpl->fillin("URL",equal_url($config->{'httproot'},$virt_path));
138
139 $dtpl->parse_if_block("users",$users && -o $physical."/".$dir);
140
141 $dirlist .= $dtpl->get_template;
142 }
143
144 # Files
145
146 foreach my $file(@$files)
147 {
148 my $phys_path = $physical."/".$file;
149 my $virt_path = encode_entities($virtual.$file);
150
151 my @stat = stat($phys_path);
152 my $in_use = $uselist->in_use($virtual.$file);
153
154 my $ftpl = new Template;
155 $ftpl->read_file($config->{'templates'}->{'dirlist_file'});
156
157 $ftpl->fillin("FILE",$virt_path);
158 $ftpl->fillin("FILE_NAME",$file);
159 $ftpl->fillin("SIZE",$stat[7]);
160 $ftpl->fillin("DATE",strftime($config->{'timeformat'},localtime($stat[9])));
161 $ftpl->fillin("URL",equal_url($config->{'httproot'},$virt_path));
162
163 $ftpl->parse_if_block("not_readable",not -r $phys_path);
164 $ftpl->parse_if_block("binary",-B $phys_path);
165 $ftpl->parse_if_block("readonly",not -w $phys_path);
166
167 $ftpl->parse_if_block("viewable",-r $phys_path && -T $phys_path && not ($config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'}));
168
169 $ftpl->parse_if_block("editable",-r $phys_path && -w $phys_path && -T $phys_path && not ($config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'}) && not $in_use);
170
171 $ftpl->parse_if_block("in_use",$in_use);
172 $ftpl->parse_if_block("unused",not $in_use);
173
174 $ftpl->parse_if_block("too_large",$config->{'max_file_size'} && $stat[7] > $config->{'max_file_size'});
175
176 $ftpl->parse_if_block("users",$users && -o $phys_path);
177
178 $dirlist .= $ftpl->get_template;
179 }
180
181 $tpl->read_file($config->{'templates'}->{'dirlist'});
182
183 $tpl->fillin("DIRLIST",$dirlist);
184 $tpl->fillin("DIR",$virtual);
185 $tpl->fillin("SCRIPT",$script);
186 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
187 }
188 else
189 {
190 # View a file
191
192 return error($config->{'errors'}->{'noview'},upper_path($virtual)) unless(-r $physical);
193
194 # Check on binary files
195 # We have to do it in this way, or empty files
196 # will be recognized as binary files
197
198 unless(-T $physical)
199 {
200 # Binary file
201
202 return error($config->{'errors'}->{'binary'},upper_path($virtual));
203 }
204 else
205 {
206 # Text file
207
208 my $size = (stat($physical))[7];
209
210 if($config->{'max_file_size'} && $size > $config->{'max_file_size'})
211 {
212 return error($config->{'errors'}->{'file_too_large'},upper_path($virtual),{SIZE => $config->{'max_file_size'}})
213 }
214 else
215 {
216 my $content = file_read($physical);
217 $$content =~ s/\015\012|\012|\015/\n/g;
218
219 $tpl->read_file($config->{'templates'}->{'viewfile'});
220
221 $tpl->fillin("FILE",$virtual);
222 $tpl->fillin("DIR",upper_path($virtual));
223 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
224 $tpl->fillin("SCRIPT",$script);
225 $tpl->fillin("CONTENT",encode_entities($$content));
226
227 $tpl->parse_if_block("editable",-r $physical && -w $physical && -T $physical && not ($config->{'max_file_size'} && $size > $config->{'max_file_size'}) && $uselist->unused($virtual));
228 }
229 }
230 }
231
232 my $output = header(-type => "text/html");
233 $output .= $tpl->get_template;
234
235 return \$output;
236 }
237
238 # exec_beginedit
239 #
240 # Lock a file and display a form to edit it
241 #
242 # Params: 1. Reference to user input hash
243 # 2. Reference to config hash
244 #
245 # Return: Output of the command (Scalar Reference)
246
247 sub exec_beginedit($$)
248 {
249 my ($data,$config) = @_;
250 my $physical = $data->{'physical'};
251 my $virtual = $data->{'virtual'};
252 my $uselist = $data->{'uselist'};
253
254 return error($config->{'errors'}->{'editdir'},upper_path($virtual)) if(-d $physical);
255 return error($config->{'errors'}->{'in_use'},upper_path($virtual),{FILE => $virtual}) if($uselist->in_use($virtual));
256 return error($config->{'errors'}->{'noedit'},upper_path($virtual)) unless(-r $physical && -w $physical);
257
258 # Check on binary files
259
260 unless(-T $physical)
261 {
262 # Binary file
263
264 return error($config->{'errors'}->{'binary'},upper_path($virtual));
265 }
266 else
267 {
268 if($config->{'max_file_size'} && (stat($physical))[7] > $config->{'max_file_size'})
269 {
270 return error($config->{'errors'}->{'file_too_large'},upper_path($virtual),{SIZE => $config->{'max_file_size'}})
271 }
272 else
273 {
274 # Text file
275
276 $uselist->add_file($virtual);
277 $uselist->save;
278
279 my $content = file_read($physical);
280 $$content =~ s/\015\012|\012|\015/\n/g;
281
282 my $tpl = new Template;
283 $tpl->read_file($config->{'templates'}->{'editfile'});
284
285 $tpl->fillin("FILE",$virtual);
286 $tpl->fillin("DIR",upper_path($virtual));
287 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
288 $tpl->fillin("SCRIPT",$script);
289 $tpl->fillin("CONTENT",encode_entities($$content));
290
291 my $output = header(-type => "text/html");
292 $output .= $tpl->get_template;
293
294 return \$output;
295 }
296 }
297 }
298
299 # exec_canceledit()
300 #
301 # Abort file editing
302 #
303 # Params: 1. Reference to user input hash
304 # 2. Reference to config hash
305 #
306 # Return: Output of the command (Scalar Reference)
307
308 sub exec_canceledit($$)
309 {
310 my ($data,$config) = @_;
311 my $virtual = $data->{'virtual'};
312
313 file_unlock($data->{'uselist'},$virtual);
314 return devedit_reload({command => 'show', file => upper_path($virtual)});
315 }
316
317 # exec_endedit()
318 #
319 # Save a file, unlock it and return to directory view
320 #
321 # Params: 1. Reference to user input hash
322 # 2. Reference to config hash
323 #
324 # Return: Output of the command (Scalar Reference)
325
326 sub exec_endedit($$)
327 {
328 my ($data,$config) = @_;
329 my $physical = $data->{'physical'};
330 my $virtual = $data->{'virtual'};
331 my $content = $data->{'cgi'}->param('filecontent');
332 my $uselist = $data->{'uselist'};
333
334 # We already unlock the file at the beginning of the
335 # subroutine, because if we have to abort this routine,
336 # the file keeps locked.
337 # Nobody else will access the file during this routine
338 # because of the concept of File::UseList.
339
340 file_unlock($uselist,$virtual);
341
342 # Normalize newlines
343
344 $content =~ s/\015\012|\012|\015/\n/g;
345
346 if($data->{'cgi'}->param('encode_iso'))
347 {
348 # Encode all ISO-8859-1 special chars
349
350 $content = encode_entities($content,"\200-\377");
351 }
352
353 if($data->{'cgi'}->param('saveas') && $data->{'new_physical'} ne '' && $data->{'new_virtual'} ne '')
354 {
355 # Create the new filename
356
357 $physical = $data->{'new_physical'};
358 $virtual = $data->{'new_virtual'};
359
360 # Check if someone else is editing the new file
361
362 return error($config->{'errors'}->{'in_use'},upper_path($virtual),{FILE => $virtual}) if($uselist->in_use($virtual));
363 }
364
365 return error($config->{'errors'}->{'text_to_binary'},upper_path($virtual)) unless(-T $physical);
366 return error($config->{'errors'}->{'editdir'},upper_path($virtual)) if(-d $physical);
367 return error($config->{'errors'}->{'noedit'}, upper_path($virtual)) if(-e $physical && !(-r $physical && -w $physical));
368
369 if(file_save($physical,\$content))
370 {
371 # Saving of the file was successful - so unlock it!
372
373 return devedit_reload({command => 'show', file => upper_path($virtual)});
374 }
375 else
376 {
377 return error($config->{'errors'}->{'edit_failed'},upper_path($virtual),{FILE => $virtual});
378 }
379 }
380
381 # exec_mkfile()
382 #
383 # Create a file and return to directory view
384 #
385 # Params: 1. Reference to user input hash
386 # 2. Reference to config hash
387 #
388 # Return: Output of the command (Scalar Reference)
389
390 sub exec_mkfile($$)
391 {
392 my ($data,$config) = @_;
393 my $new_physical = $data->{'new_physical'};
394 my $new_virtual = $data->{'new_virtual'};
395 my $dir = upper_path($new_virtual);
396 $new_virtual = encode_entities($new_virtual);
397
398 if($new_physical)
399 {
400 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
401
402 file_create($new_physical) or return error($config->{'errors'}->{'mkfile_failed'},$dir,{FILE => $new_virtual});
403 return devedit_reload({command => 'show', file => $dir});
404 }
405 else
406 {
407 my $tpl = new Template;
408 $tpl->read_file($config->{'templates'}->{'mkfile'});
409
410 $tpl->fillin("DIR","/");
411 $tpl->fillin("SCRIPT",$script);
412
413 my $output = header(-type => "text/html");
414 $output .= $tpl->get_template;
415
416 return \$output;
417 }
418 }
419
420 # exec_mkdir()
421 #
422 # Create a directory and return to directory view
423 #
424 # Params: 1. Reference to user input hash
425 # 2. Reference to config hash
426 #
427 # Return: Output of the command (Scalar Reference)
428
429 sub exec_mkdir($$)
430 {
431 my ($data,$config) = @_;
432 my $new_physical = $data->{'new_physical'};
433 my $new_virtual = $data->{'new_virtual'};
434 my $dir = upper_path($new_virtual);
435 $new_virtual = encode_entities($new_virtual);
436
437 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
438
439 if($new_physical)
440 {
441 mkdir($new_physical,0777) or return error($config->{'errors'}->{'mkdir_failed'},$dir,{DIR => $new_virtual});
442 return devedit_reload({command => 'show', file => $dir});
443 }
444 else
445 {
446 my $tpl = new Template;
447 $tpl->read_file($config->{'templates'}->{'mkdir'});
448
449 $tpl->fillin("DIR","/");
450 $tpl->fillin("SCRIPT",$script);
451
452 my $output = header(-type => "text/html");
453 $output .= $tpl->get_template;
454
455 return \$output;
456 }
457 }
458
459 # exec_upload()
460 #
461 # Upload a file
462 #
463 # Params: 1. Reference to user input hash
464 # 2. Reference to config hash
465 #
466 # Return: Output of the command (Scalar Reference)
467
468 sub exec_upload($$)
469 {
470 my ($data,$config) = @_;
471 my $physical = $data->{'physical'};
472 my $virtual = $data->{'virtual'};
473 my $cgi = $data->{'cgi'};
474
475 if(my $uploaded_file = $cgi->param('uploaded_file'))
476 {
477 # Process file upload
478
479 my $filename = file_name($uploaded_file);
480 my $file_phys = $physical."/".$filename;
481 my $file_virt = $virtual."".$filename;
482
483 return error($config->{'errors'}->{'file_exists'},$virtual,{FILE => $file_virt}) if(-e $file_phys && not $cgi->param('overwrite'));
484
485 my $ascii = $cgi->param('ascii');
486 my $handle = $cgi->upload('uploaded_file');
487
488 local *FILE;
489
490 open(FILE,">$file_phys") or return error($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE => $file_virt});
491 binmode(FILE) unless($ascii);
492
493 # Read transferred file and write it to disk
494
495 read($handle, my $data, -s $handle);
496 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
497 print FILE $data;
498
499 close(FILE);
500
501 return devedit_reload({command => "show", file => $virtual});
502 }
503 else
504 {
505 my $tpl = new Template;
506 $tpl->read_file($config->{'templates'}->{'upload'});
507
508 $tpl->fillin("DIR",$virtual);
509 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
510 $tpl->fillin("SCRIPT",$script);
511
512 my $output = header(-type => "text/html");
513 $output .= $tpl->get_template;
514
515 return \$output;
516 }
517 }
518
519 # exec_copy()
520 #
521 # Copy a file and return to directory view
522 #
523 # Params: 1. Reference to user input hash
524 # 2. Reference to config hash
525 #
526 # Return: Output of the command (Scalar Reference)
527
528 sub exec_copy($$)
529 {
530 my ($data,$config) = @_;
531 my $physical = $data->{'physical'};
532 my $virtual = encode_entities($data->{'virtual'});
533 my $new_physical = $data->{'new_physical'};
534
535 return error($config->{'errors'}->{'dircopy'}) if(-d $physical);
536 return error($config->{'errors'}->{'nocopy'}) unless(-r $physical);
537
538 if($new_physical)
539 {
540 my $new_virtual = $data->{'new_virtual'};
541 my $dir = upper_path($new_virtual);
542 $new_virtual = encode_entities($new_virtual);
543
544 if(-e $new_physical)
545 {
546 return error($config->{'errors'}->{'exist_edited'},$dir,{FILE => $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
547
548 if(-d $new_physical)
549 {
550 return error($config->{'errors'}->{'dir_replace'},$dir);
551 }
552 elsif(not $data->{'cgi'}->param('confirmed'))
553 {
554 my $tpl = new Template;
555 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
556
557 $tpl->fillin("FILE",$virtual);
558 $tpl->fillin("NEW_FILE",$new_virtual);
559 $tpl->fillin("NEW_FILENAME",file_name($new_virtual));
560 $tpl->fillin("NEW_DIR",$dir);
561 $tpl->fillin("DIR",upper_path($virtual));
562
563 $tpl->fillin("COMMAND","copy");
564 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
565 $tpl->fillin("SCRIPT",$script);
566
567 my $output = header(-type => "text/html");
568 $output .= $tpl->get_template;
569
570 return \$output;
571 }
572 }
573
574 copy($physical,$new_physical) or return error($config->{'errors'}->{'copy_failed'},upper_path($virtual),{FILE => $virtual, NEW_FILE => $new_virtual});
575 return devedit_reload({command => 'show', file => $dir});
576 }
577 else
578 {
579 my $tpl = new Template;
580 $tpl->read_file($config->{'templates'}->{'copyfile'});
581
582 $tpl->fillin("FILE",$virtual);
583 $tpl->fillin("DIR",upper_path($virtual));
584 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
585 $tpl->fillin("SCRIPT",$script);
586
587 my $output = header(-type => "text/html");
588 $output .= $tpl->get_template;
589
590 return \$output;
591 }
592 }
593
594 # exec_rename()
595 #
596 # Rename/move a file and return to directory view
597 #
598 # Params: 1. Reference to user input hash
599 # 2. Reference to config hash
600 #
601 # Return: Output of the command (Scalar Reference)
602
603 sub exec_rename($$)
604 {
605 my ($data,$config) = @_;
606 my $physical = $data->{'physical'};
607 my $virtual = $data->{'virtual'};
608 my $new_physical = $data->{'new_physical'};
609
610 return error($config->{'errors'}->{'rename_root'},"/") if($virtual eq "/");
611 return error($config->{'errors'}->{'in_use'},upper_path($virtual),{FILE => $virtual}) if($data->{'uselist'}->in_use($virtual));
612
613 if($new_physical)
614 {
615 my $new_virtual = $data->{'new_virtual'};
616 my $dir = upper_path($new_virtual);
617 $new_virtual = encode_entities($new_virtual);
618
619 if(-e $new_physical)
620 {
621 return error($config->{'errors'}->{'exist_edited'},$dir,{FILE => $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
622
623 if(-d $new_physical)
624 {
625 return error($config->{'errors'}->{'dir_replace'},$dir);
626 }
627 elsif(not $data->{'cgi'}->param('confirmed'))
628 {
629 my $tpl = new Template;
630 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
631
632 $tpl->fillin("FILE",$virtual);
633 $tpl->fillin("NEW_FILE",$new_virtual);
634 $tpl->fillin("NEW_FILENAME",file_name($new_virtual));
635 $tpl->fillin("NEW_DIR",$dir);
636 $tpl->fillin("DIR",upper_path($virtual));
637
638 $tpl->fillin("COMMAND","rename");
639 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
640 $tpl->fillin("SCRIPT",$script);
641
642 my $output = header(-type => "text/html");
643 $output .= $tpl->get_template;
644
645 return \$output;
646 }
647 }
648
649 rename($physical,$new_physical) or return error($config->{'errors'}->{'rename_failed'},upper_path($virtual),{FILE => $virtual, NEW_FILE => $new_virtual});
650 return devedit_reload({command => 'show', file => $dir});
651 }
652 else
653 {
654 my $tpl = new Template;
655 $tpl->read_file($config->{'templates'}->{'renamefile'});
656
657 $tpl->fillin("FILE",$virtual);
658 $tpl->fillin("DIR",upper_path($virtual));
659 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
660 $tpl->fillin("SCRIPT",$script);
661
662 my $output = header(-type => "text/html");
663 $output .= $tpl->get_template;
664
665 return \$output;
666 }
667 }
668
669 # exec_remove()
670 #
671 # Remove a file or a directory and return to directory view
672 #
673 # Params: 1. Reference to user input hash
674 # 2. Reference to config hash
675 #
676 # Return: Output of the command (Scalar Reference)
677
678 sub exec_remove($$)
679 {
680 my ($data,$config) = @_;
681 my $physical = $data->{'physical'};
682 my $virtual = $data->{'virtual'};
683
684 return error($config->{'errors'}->{'remove_root'},"/") if($virtual eq "/");
685
686 if(-d $physical)
687 {
688 # Remove a directory
689
690 if($data->{'cgi'}->param('confirmed'))
691 {
692 rmtree($physical);
693 return devedit_reload({command => 'show', file => upper_path($virtual)});
694 }
695 else
696 {
697 my $tpl = new Template;
698 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
699
700 $tpl->fillin("DIR",$virtual);
701 $tpl->fillin("UPPER_DIR",upper_path($virtual));
702 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
703 $tpl->fillin("SCRIPT",$script);
704
705 my $output = header(-type => "text/html");
706 $output .= $tpl->get_template;
707
708 return \$output;
709 }
710 }
711 else
712 {
713 # Remove a file
714
715 return error($config->{'errors'}->{'in_use'},upper_path($virtual),{FILE => $virtual}) if($data->{'uselist'}->in_use($virtual));
716
717 if($data->{'cgi'}->param('confirmed'))
718 {
719 unlink($physical) or return error($config->{'errors'}->{'delete_failed'},upper_path($virtual),{FILE => $virtual});
720 return devedit_reload({command => 'show', file => upper_path($virtual)});
721 }
722 else
723 {
724 my $tpl = new Template;
725 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
726
727 $tpl->fillin("FILE",$virtual);
728 $tpl->fillin("DIR",upper_path($virtual));
729 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
730 $tpl->fillin("SCRIPT",$script);
731
732 my $output = header(-type => "text/html");
733 $output .= $tpl->get_template;
734
735 return \$output;
736 }
737 }
738 }
739
740 # exec_chprop()
741 #
742 # Change the mode and the group of a file or a directory
743 #
744 # Params: 1. Reference to user input hash
745 # 2. Reference to config hash
746 #
747 # Return: Output of the command (Scalar Reference)
748
749 sub exec_chprop($$)
750 {
751 my ($data,$config) = @_;
752 my $physical = $data->{'physical'};
753 my $virtual = $data->{'virtual'};
754 my $dir = upper_path($virtual);
755 my $cgi = $data->{'cgi'};
756 my $mode = $cgi->param('mode');
757 my $group = $cgi->param('group');
758
759 if($users)
760 {
761 if(-o $physical)
762 {
763 if($mode || $group)
764 {
765 if($mode)
766 {
767 my $oct_mode = $mode;
768 $oct_mode = "0".$oct_mode if(length($oct_mode) == 3);
769 $oct_mode = oct($oct_mode);
770
771 chmod($oct_mode,$physical);
772 }
773
774 chgrp($group,$physical) if($group);
775
776 return devedit_reload({command => 'show', file => $dir});
777 }
778 else
779 {
780 my @stat = stat($physical);
781
782 my $mode = $stat[2];
783 my $mode_oct = substr(sprintf("%04o",$mode),-4);
784 my $gid = $stat[5];
785 my $group = getgrgid($gid);
786
787 my $tpl = new Template;
788 $tpl->read_file($config->{'templates'}->{'chprop'});
789
790 $tpl->fillin("MODE_OCTAL",$mode_oct);
791 $tpl->fillin("MODE_STRING",mode_string($mode));
792 $tpl->fillin("GID",$gid);
793 $tpl->fillin("GROUP",$group);
794
795 $tpl->fillin("FILE",$virtual);
796 $tpl->fillin("DIR",$dir);
797 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
798 $tpl->fillin("SCRIPT",$script);
799
800 my $output = header(-type => "text/html");
801 $output .= $tpl->get_template;
802
803 return \$output;
804 }
805 }
806 else
807 {
808 return error($config->{'errors'}->{'not_owner'},$dir,{FILE => $virtual});
809 }
810 }
811 else
812 {
813 return error($config->{'errors'}->{'no_users'},$dir,{FILE => $virtual});
814 }
815 }
816
817 # exec_unlock()
818 #
819 # Remove a file from the list of used files and
820 # return to directory view
821 #
822 # Params: 1. Reference to user input hash
823 # 2. Reference to config hash
824 #
825 # Return: Output of the command (Scalar Reference)
826
827 sub exec_unlock($$)
828 {
829 my ($data,$config) = @_;
830 my $virtual = $data->{'virtual'};
831 my $uselist = $data->{'uselist'};
832
833 return devedit_reload({command => 'show', file => upper_path($virtual)}) if($uselist->unused($virtual));
834
835 if($data->{'cgi'}->param('confirmed'))
836 {
837 file_unlock($uselist,$virtual);
838 return devedit_reload({command => 'show', file => upper_path($virtual)});
839 }
840 else
841 {
842 my $tpl = new Template;
843 $tpl->read_file($config->{'templates'}->{'confirm_unlock'});
844
845 $tpl->fillin("FILE",$virtual);
846 $tpl->fillin("DIR",upper_path($virtual));
847 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
848 $tpl->fillin("SCRIPT",$script);
849
850 my $output = header(-type => "text/html");
851 $output .= $tpl->get_template;
852
853 return \$output;
854 }
855 }
856
857 # exec_about()
858 #
859 # Display some information about Dev-Editor
860 #
861 # Params: 1. Reference to user input hash
862 # 2. Reference to config hash
863 #
864 # Return: Output of the command (Scalar Reference)
865
866 sub exec_about($$)
867 {
868 my ($data,$config) = @_;
869
870 my $tpl = new Template;
871 $tpl->read_file($config->{'templates'}->{'about'});
872
873 $tpl->fillin("SCRIPT",$script);
874
875 # Dev-Editor's version number
876
877 $tpl->fillin("VERSION",$data->{'version'});
878
879 # Some path information
880
881 $tpl->fillin("SCRIPT_PHYS",$ENV{'SCRIPT_FILENAME'});
882 $tpl->fillin("CONFIG_PATH",$data->{'configfile'});
883 $tpl->fillin("FILE_ROOT",$config->{'fileroot'});
884 $tpl->fillin("HTTP_ROOT",$config->{'httproot'});
885
886 # Perl
887
888 $tpl->fillin("PERL_PROG",$^X);
889 $tpl->fillin("PERL_VER",sprintf("%vd",$^V));
890
891 # Information about the server
892
893 $tpl->fillin("HTTPD",$ENV{'SERVER_SOFTWARE'});
894 $tpl->fillin("OS",$^O);
895 $tpl->fillin("TIME",strftime($config->{'timeformat'},localtime));
896
897 # Process information
898
899 $tpl->fillin("PID",$$);
900
901 # Check if the functions getpwuid() and getgrgid() are available
902
903 if($users)
904 {
905 # Dev-Editor is running on a system which allows users and groups
906 # So we display the user and the group of our process
907
908 $tpl->parse_if_block("users",1);
909
910 # ID's of user and group
911
912 $tpl->fillin("UID",$<);
913 $tpl->fillin("GID",$();
914
915 # Names of user and group
916
917 $tpl->fillin("USER",getpwuid($<));
918 $tpl->fillin("GROUP",getgrgid($());
919 }
920 else
921 {
922 $tpl->parse_if_block("users",0);
923 }
924
925 my $output = header(-type => "text/html");
926 $output .= $tpl->get_template;
927
928 return \$output;
929 }
930
931 # it's true, baby ;-)
932
933 1;
934
935 #
936 ### End ###

patrick-canterino.de