]> git.p6c8.net - devedit.git/blob - modules/Command.pm
- There is no need to load the POSIX module twice
[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-31
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
226 $tpl->parse_if_block("editable",-r $physical && -w $physical && -T $physical && not ($config->{'max_file_size'} && $size > $config->{'max_file_size'}) && $uselist->unused($virtual));
227
228 $tpl->fillin("CONTENT",encode_entities($$content));
229 }
230 }
231 }
232
233 my $output = header(-type => "text/html");
234 $output .= $tpl->get_template;
235
236 return \$output;
237 }
238
239 # exec_beginedit
240 #
241 # Lock a file and display a form to edit it
242 #
243 # Params: 1. Reference to user input hash
244 # 2. Reference to config hash
245 #
246 # Return: Output of the command (Scalar Reference)
247
248 sub exec_beginedit($$)
249 {
250 my ($data,$config) = @_;
251 my $physical = $data->{'physical'};
252 my $virtual = $data->{'virtual'};
253 my $uselist = $data->{'uselist'};
254
255 return error($config->{'errors'}->{'editdir'},upper_path($virtual)) if(-d $physical);
256 return error($config->{'errors'}->{'in_use'},upper_path($virtual),{FILE => $virtual}) if($uselist->in_use($virtual));
257 return error($config->{'errors'}->{'noedit'},upper_path($virtual)) unless(-r $physical && -w $physical);
258
259 # Check on binary files
260
261 unless(-T $physical)
262 {
263 # Binary file
264
265 return error($config->{'errors'}->{'binary'},upper_path($virtual));
266 }
267 else
268 {
269 if($config->{'max_file_size'} && (stat($physical))[7] > $config->{'max_file_size'})
270 {
271 return error($config->{'errors'}->{'file_too_large'},upper_path($virtual),{SIZE => $config->{'max_file_size'}})
272 }
273 else
274 {
275 # Text file
276
277 $uselist->add_file($virtual);
278 $uselist->save;
279
280 my $content = file_read($physical);
281 $$content =~ s/\015\012|\012|\015/\n/g;
282
283 my $tpl = new Template;
284 $tpl->read_file($config->{'templates'}->{'editfile'});
285
286 $tpl->fillin("FILE",$virtual);
287 $tpl->fillin("DIR",upper_path($virtual));
288 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
289 $tpl->fillin("SCRIPT",$script);
290 $tpl->fillin("CONTENT",encode_entities($$content));
291
292 my $output = header(-type => "text/html");
293 $output .= $tpl->get_template;
294
295 return \$output;
296 }
297 }
298 }
299
300 # exec_canceledit()
301 #
302 # Abort file editing
303 #
304 # Params: 1. Reference to user input hash
305 # 2. Reference to config hash
306 #
307 # Return: Output of the command (Scalar Reference)
308
309 sub exec_canceledit($$)
310 {
311 my ($data,$config) = @_;
312 my $virtual = $data->{'virtual'};
313
314 file_unlock($data->{'uselist'},$virtual);
315 return devedit_reload({command => 'show', file => upper_path($virtual)});
316 }
317
318 # exec_endedit()
319 #
320 # Save a file, unlock it and return to directory view
321 #
322 # Params: 1. Reference to user input hash
323 # 2. Reference to config hash
324 #
325 # Return: Output of the command (Scalar Reference)
326
327 sub exec_endedit($$)
328 {
329 my ($data,$config) = @_;
330 my $physical = $data->{'physical'};
331 my $virtual = $data->{'virtual'};
332 my $content = $data->{'cgi'}->param('filecontent');
333 my $uselist = $data->{'uselist'};
334
335 # We already unlock the file at the beginning of the
336 # subroutine, because if we have to abort this routine,
337 # the file keeps locked.
338 # Nobody else will access the file during this routine
339 # because of the concept of File::UseList.
340
341 file_unlock($uselist,$virtual);
342
343 # Normalize newlines
344
345 $content =~ s/\015\012|\012|\015/\n/g;
346
347 if($data->{'cgi'}->param('encode_iso'))
348 {
349 # Encode all ISO-8859-1 special chars
350
351 $content = encode_entities($content,"\200-\377");
352 }
353
354 if($data->{'cgi'}->param('saveas') && $data->{'new_physical'} ne '' && $data->{'new_virtual'} ne '')
355 {
356 # Create the new filename
357
358 $physical = $data->{'new_physical'};
359 $virtual = $data->{'new_virtual'};
360
361 # Check if someone else is editing the new file
362
363 return error($config->{'errors'}->{'in_use'},upper_path($virtual),{FILE => $virtual}) if($uselist->in_use($virtual));
364 }
365
366 return error($config->{'errors'}->{'text_to_binary'},upper_path($virtual)) unless(-T $physical);
367 return error($config->{'errors'}->{'editdir'},upper_path($virtual)) if(-d $physical);
368 return error($config->{'errors'}->{'noedit'}, upper_path($virtual)) if(-e $physical && !(-r $physical && -w $physical));
369
370 if(file_save($physical,\$content))
371 {
372 # Saving of the file was successful - so unlock it!
373
374 return devedit_reload({command => 'show', file => upper_path($virtual)});
375 }
376 else
377 {
378 return error($config->{'errors'}->{'edit_failed'},upper_path($virtual),{FILE => $virtual});
379 }
380 }
381
382 # exec_mkfile()
383 #
384 # Create a file and return to directory view
385 #
386 # Params: 1. Reference to user input hash
387 # 2. Reference to config hash
388 #
389 # Return: Output of the command (Scalar Reference)
390
391 sub exec_mkfile($$)
392 {
393 my ($data,$config) = @_;
394 my $new_physical = $data->{'new_physical'};
395 my $new_virtual = $data->{'new_virtual'};
396 my $dir = upper_path($new_virtual);
397 $new_virtual = encode_entities($new_virtual);
398
399 if($new_physical)
400 {
401 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
402
403 file_create($new_physical) or return error($config->{'errors'}->{'mkfile_failed'},$dir,{FILE => $new_virtual});
404 return devedit_reload({command => 'show', file => $dir});
405 }
406 else
407 {
408 my $tpl = new Template;
409 $tpl->read_file($config->{'templates'}->{'mkfile'});
410
411 $tpl->fillin("DIR","/");
412 $tpl->fillin("SCRIPT",$script);
413
414 my $output = header(-type => "text/html");
415 $output .= $tpl->get_template;
416
417 return \$output;
418 }
419 }
420
421 # exec_mkdir()
422 #
423 # Create a directory and return to directory view
424 #
425 # Params: 1. Reference to user input hash
426 # 2. Reference to config hash
427 #
428 # Return: Output of the command (Scalar Reference)
429
430 sub exec_mkdir($$)
431 {
432 my ($data,$config) = @_;
433 my $new_physical = $data->{'new_physical'};
434 my $new_virtual = $data->{'new_virtual'};
435 my $dir = upper_path($new_virtual);
436 $new_virtual = encode_entities($new_virtual);
437
438 return error($config->{'errors'}->{'file_exists'},$dir,{FILE => $new_virtual}) if(-e $new_physical);
439
440 if($new_physical)
441 {
442 mkdir($new_physical,0777) or return error($config->{'errors'}->{'mkdir_failed'},$dir,{DIR => $new_virtual});
443 return devedit_reload({command => 'show', file => $dir});
444 }
445 else
446 {
447 my $tpl = new Template;
448 $tpl->read_file($config->{'templates'}->{'mkdir'});
449
450 $tpl->fillin("DIR","/");
451 $tpl->fillin("SCRIPT",$script);
452
453 my $output = header(-type => "text/html");
454 $output .= $tpl->get_template;
455
456 return \$output;
457 }
458 }
459
460 # exec_upload()
461 #
462 # Upload a file
463 #
464 # Params: 1. Reference to user input hash
465 # 2. Reference to config hash
466 #
467 # Return: Output of the command (Scalar Reference)
468
469 sub exec_upload($$)
470 {
471 my ($data,$config) = @_;
472 my $physical = $data->{'physical'};
473 my $virtual = $data->{'virtual'};
474 my $cgi = $data->{'cgi'};
475
476 if(my $uploaded_file = $cgi->param('uploaded_file'))
477 {
478 # Process file upload
479
480 my $filename = file_name($uploaded_file);
481 my $file_phys = $physical."/".$filename;
482 my $file_virt = $virtual."".$filename;
483
484 return error($config->{'errors'}->{'file_exists'},$virtual,{FILE => $file_virt}) if(-e $file_phys && not $cgi->param('overwrite'));
485
486 my $ascii = $cgi->param('ascii');
487 my $handle = $cgi->upload('uploaded_file');
488
489 local *FILE;
490
491 open(FILE,">$file_phys") or return error($config->{'errors'}->{'mkfile_failed'},$virtual,{FILE => $file_virt});
492 binmode(FILE) unless($ascii);
493
494 # Read transferred file and write it to disk
495
496 read($handle, my $data, -s $handle);
497 $data =~ s/\015\012|\012|\015/\n/g if($ascii); # Replace line separators if transferring in ASCII mode
498 print FILE $data;
499
500 close(FILE);
501
502 return devedit_reload({command => "show", file => $virtual});
503 }
504 else
505 {
506 my $tpl = new Template;
507 $tpl->read_file($config->{'templates'}->{'upload'});
508
509 $tpl->fillin("DIR",$virtual);
510 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
511 $tpl->fillin("SCRIPT",$script);
512
513 my $output = header(-type => "text/html");
514 $output .= $tpl->get_template;
515
516 return \$output;
517 }
518 }
519
520 # exec_copy()
521 #
522 # Copy a file and return to directory view
523 #
524 # Params: 1. Reference to user input hash
525 # 2. Reference to config hash
526 #
527 # Return: Output of the command (Scalar Reference)
528
529 sub exec_copy($$)
530 {
531 my ($data,$config) = @_;
532 my $physical = $data->{'physical'};
533 my $virtual = encode_entities($data->{'virtual'});
534 my $new_physical = $data->{'new_physical'};
535
536 return error($config->{'errors'}->{'dircopy'}) if(-d $physical);
537 return error($config->{'errors'}->{'nocopy'}) unless(-r $physical);
538
539 if($new_physical)
540 {
541 my $new_virtual = $data->{'new_virtual'};
542 my $dir = upper_path($new_virtual);
543 $new_virtual = encode_entities($new_virtual);
544
545 if(-e $new_physical)
546 {
547 return error($config->{'errors'}->{'exist_edited'},$dir,{FILE => $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
548
549 if(-d $new_physical)
550 {
551 return error($config->{'errors'}->{'dir_replace'},$dir);
552 }
553 elsif(not $data->{'cgi'}->param('confirmed'))
554 {
555 my $tpl = new Template;
556 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
557
558 $tpl->fillin("FILE",$virtual);
559 $tpl->fillin("NEW_FILE",$new_virtual);
560 $tpl->fillin("NEW_FILENAME",file_name($new_virtual));
561 $tpl->fillin("NEW_DIR",$dir);
562 $tpl->fillin("DIR",upper_path($virtual));
563
564 $tpl->fillin("COMMAND","copy");
565 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
566 $tpl->fillin("SCRIPT",$script);
567
568 my $output = header(-type => "text/html");
569 $output .= $tpl->get_template;
570
571 return \$output;
572 }
573 }
574
575 copy($physical,$new_physical) or return error($config->{'errors'}->{'copy_failed'},upper_path($virtual),{FILE => $virtual, NEW_FILE => $new_virtual});
576 return devedit_reload({command => 'show', file => $dir});
577 }
578 else
579 {
580 my $tpl = new Template;
581 $tpl->read_file($config->{'templates'}->{'copyfile'});
582
583 $tpl->fillin("FILE",$virtual);
584 $tpl->fillin("DIR",upper_path($virtual));
585 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
586 $tpl->fillin("SCRIPT",$script);
587
588 my $output = header(-type => "text/html");
589 $output .= $tpl->get_template;
590
591 return \$output;
592 }
593 }
594
595 # exec_rename()
596 #
597 # Rename/move a file and return to directory view
598 #
599 # Params: 1. Reference to user input hash
600 # 2. Reference to config hash
601 #
602 # Return: Output of the command (Scalar Reference)
603
604 sub exec_rename($$)
605 {
606 my ($data,$config) = @_;
607 my $physical = $data->{'physical'};
608 my $virtual = $data->{'virtual'};
609 my $new_physical = $data->{'new_physical'};
610
611 return error($config->{'errors'}->{'rename_root'},"/") if($virtual eq "/");
612 return error($config->{'errors'}->{'in_use'},upper_path($virtual),{FILE => $virtual}) if($data->{'uselist'}->in_use($virtual));
613
614 if($new_physical)
615 {
616 my $new_virtual = $data->{'new_virtual'};
617 my $dir = upper_path($new_virtual);
618 $new_virtual = encode_entities($new_virtual);
619
620 if(-e $new_physical)
621 {
622 return error($config->{'errors'}->{'exist_edited'},$dir,{FILE => $new_virtual}) if($data->{'uselist'}->in_use($data->{'new_virtual'}));
623
624 if(-d $new_physical)
625 {
626 return error($config->{'errors'}->{'dir_replace'},$dir);
627 }
628 elsif(not $data->{'cgi'}->param('confirmed'))
629 {
630 my $tpl = new Template;
631 $tpl->read_file($config->{'templates'}->{'confirm_replace'});
632
633 $tpl->fillin("FILE",$virtual);
634 $tpl->fillin("NEW_FILE",$new_virtual);
635 $tpl->fillin("NEW_FILENAME",file_name($new_virtual));
636 $tpl->fillin("NEW_DIR",$dir);
637 $tpl->fillin("DIR",upper_path($virtual));
638
639 $tpl->fillin("COMMAND","rename");
640 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
641 $tpl->fillin("SCRIPT",$script);
642
643 my $output = header(-type => "text/html");
644 $output .= $tpl->get_template;
645
646 return \$output;
647 }
648 }
649
650 rename($physical,$new_physical) or return error($config->{'errors'}->{'rename_failed'},upper_path($virtual),{FILE => $virtual, NEW_FILE => $new_virtual});
651 return devedit_reload({command => 'show', file => $dir});
652 }
653 else
654 {
655 my $tpl = new Template;
656 $tpl->read_file($config->{'templates'}->{'renamefile'});
657
658 $tpl->fillin("FILE",$virtual);
659 $tpl->fillin("DIR",upper_path($virtual));
660 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
661 $tpl->fillin("SCRIPT",$script);
662
663 my $output = header(-type => "text/html");
664 $output .= $tpl->get_template;
665
666 return \$output;
667 }
668 }
669
670 # exec_remove()
671 #
672 # Remove a file or a directory and return to directory view
673 #
674 # Params: 1. Reference to user input hash
675 # 2. Reference to config hash
676 #
677 # Return: Output of the command (Scalar Reference)
678
679 sub exec_remove($$)
680 {
681 my ($data,$config) = @_;
682 my $physical = $data->{'physical'};
683 my $virtual = $data->{'virtual'};
684
685 return error($config->{'errors'}->{'remove_root'},"/") if($virtual eq "/");
686
687 if(-d $physical)
688 {
689 # Remove a directory
690
691 if($data->{'cgi'}->param('confirmed'))
692 {
693 rmtree($physical);
694 return devedit_reload({command => 'show', file => upper_path($virtual)});
695 }
696 else
697 {
698 my $tpl = new Template;
699 $tpl->read_file($config->{'templates'}->{'confirm_rmdir'});
700
701 $tpl->fillin("DIR",$virtual);
702 $tpl->fillin("UPPER_DIR",upper_path($virtual));
703 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
704 $tpl->fillin("SCRIPT",$script);
705
706 my $output = header(-type => "text/html");
707 $output .= $tpl->get_template;
708
709 return \$output;
710 }
711 }
712 else
713 {
714 # Remove a file
715
716 return error($config->{'errors'}->{'in_use'},upper_path($virtual),{FILE => $virtual}) if($data->{'uselist'}->in_use($virtual));
717
718 if($data->{'cgi'}->param('confirmed'))
719 {
720 unlink($physical) or return error($config->{'errors'}->{'delete_failed'},upper_path($virtual),{FILE => $virtual});
721 return devedit_reload({command => 'show', file => upper_path($virtual)});
722 }
723 else
724 {
725 my $tpl = new Template;
726 $tpl->read_file($config->{'templates'}->{'confirm_rmfile'});
727
728 $tpl->fillin("FILE",$virtual);
729 $tpl->fillin("DIR",upper_path($virtual));
730 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
731 $tpl->fillin("SCRIPT",$script);
732
733 my $output = header(-type => "text/html");
734 $output .= $tpl->get_template;
735
736 return \$output;
737 }
738 }
739 }
740
741 # exec_chprop()
742 #
743 # Change the mode and the group of a file or a directory
744 #
745 # Params: 1. Reference to user input hash
746 # 2. Reference to config hash
747 #
748 # Return: Output of the command (Scalar Reference)
749
750 sub exec_chprop($$)
751 {
752 my ($data,$config) = @_;
753 my $physical = $data->{'physical'};
754 my $virtual = $data->{'virtual'};
755 my $dir = upper_path($virtual);
756 my $cgi = $data->{'cgi'};
757 my $mode = $cgi->param('mode');
758 my $group = $cgi->param('group');
759
760 if($users)
761 {
762 if(-o $physical)
763 {
764 if($mode || $group)
765 {
766 if($mode)
767 {
768 my $oct_mode = $mode;
769 $oct_mode = "0".$oct_mode if(length($oct_mode) == 3);
770 $oct_mode = oct($oct_mode);
771
772 chmod($oct_mode,$physical);
773 }
774
775 if($group)
776 {
777 return error($config->{'errors'}->{'invalid_group'},$dir,{GROUP => $group}) unless($group =~ /^[a-z0-9_]+[a-z0-9_-]*$/i);
778 system("chgrp",$group,$physical);
779 }
780
781 return devedit_reload({command => 'show', file => $dir});
782 }
783 else
784 {
785 my @stat = stat($physical);
786
787 my $mode = $stat[2];
788 my $mode_oct = substr(sprintf("%04o",$mode),-4);
789 my $gid = $stat[5];
790 my $group = getgrgid($gid);
791
792 my $tpl = new Template;
793 $tpl->read_file($config->{'templates'}->{'chprop'});
794
795 $tpl->fillin("MODE_OCTAL",$mode_oct);
796 $tpl->fillin("MODE_STRING",mode_string($mode));
797 $tpl->fillin("GID",$gid);
798 $tpl->fillin("GROUP",$group);
799
800 $tpl->fillin("FILE",$virtual);
801 $tpl->fillin("DIR",$dir);
802 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
803 $tpl->fillin("SCRIPT",$script);
804
805 my $output = header(-type => "text/html");
806 $output .= $tpl->get_template;
807
808 return \$output;
809 }
810 }
811 else
812 {
813 return error($config->{'errors'}->{'not_owner'},$dir,{FILE => $virtual});
814 }
815 }
816 else
817 {
818 return error($config->{'errors'}->{'no_users'},$dir,{FILE => $virtual});
819 }
820 }
821
822 # exec_unlock()
823 #
824 # Remove a file from the list of used files and
825 # return to directory view
826 #
827 # Params: 1. Reference to user input hash
828 # 2. Reference to config hash
829 #
830 # Return: Output of the command (Scalar Reference)
831
832 sub exec_unlock($$)
833 {
834 my ($data,$config) = @_;
835 my $virtual = $data->{'virtual'};
836 my $uselist = $data->{'uselist'};
837
838 return devedit_reload({command => 'show', file => upper_path($virtual)}) if($uselist->unused($virtual));
839
840 if($data->{'cgi'}->param('confirmed'))
841 {
842 file_unlock($uselist,$virtual);
843 return devedit_reload({command => 'show', file => upper_path($virtual)});
844 }
845 else
846 {
847 my $tpl = new Template;
848 $tpl->read_file($config->{'templates'}->{'confirm_unlock'});
849
850 $tpl->fillin("FILE",$virtual);
851 $tpl->fillin("DIR",upper_path($virtual));
852 $tpl->fillin("URL",equal_url($config->{'httproot'},$virtual));
853 $tpl->fillin("SCRIPT",$script);
854
855 my $output = header(-type => "text/html");
856 $output .= $tpl->get_template;
857
858 return \$output;
859 }
860 }
861
862 # exec_about()
863 #
864 # Display some information about Dev-Editor
865 #
866 # Params: 1. Reference to user input hash
867 # 2. Reference to config hash
868 #
869 # Return: Output of the command (Scalar Reference)
870
871 sub exec_about($$)
872 {
873 my ($data,$config) = @_;
874
875 my $tpl = new Template;
876 $tpl->read_file($config->{'templates'}->{'about'});
877
878 $tpl->fillin("SCRIPT",$script);
879
880 # Dev-Editor's version number
881
882 $tpl->fillin("VERSION",$data->{'version'});
883
884 # Some path information
885
886 $tpl->fillin("SCRIPT_PHYS",$ENV{'SCRIPT_FILENAME'});
887 $tpl->fillin("CONFIG_PATH",$data->{'configfile'});
888 $tpl->fillin("FILE_ROOT",$config->{'fileroot'});
889 $tpl->fillin("HTTP_ROOT",$config->{'httproot'});
890
891 # Perl
892
893 $tpl->fillin("PERL_PROG",$^X);
894 $tpl->fillin("PERL_VER",sprintf("%vd",$^V));
895
896 # Information about the server
897
898 $tpl->fillin("HTTPD",$ENV{'SERVER_SOFTWARE'});
899 $tpl->fillin("OS",$^O);
900 $tpl->fillin("TIME",strftime($config->{'timeformat'},localtime));
901
902 # Process information
903
904 $tpl->fillin("PID",$$);
905
906 # Check if the functions getpwuid() and getgrgid() are available
907
908 if($users)
909 {
910 # Dev-Editor is running on a system which allows users and groups
911 # So we display the user and the group of our process
912
913 my $uid = POSIX::getuid;
914 my $gid = POSIX::getgid;
915
916 $tpl->parse_if_block("users",1);
917
918 # ID's of user and group
919
920 $tpl->fillin("UID",$uid);
921 $tpl->fillin("GID",$gid);
922
923 # Names of user and group
924
925 $tpl->fillin("USER",getpwuid($uid));
926 $tpl->fillin("GROUP",getgrgid($gid));
927
928 # Process umask
929
930 $tpl->fillin("UMASK",sprintf("%04o",umask));
931 }
932 else
933 {
934 $tpl->parse_if_block("users",0);
935 }
936
937 my $output = header(-type => "text/html");
938 $output .= $tpl->get_template;
939
940 return \$output;
941 }
942
943 # it's true, baby ;-)
944
945 1;
946
947 #
948 ### End ###

patrick-canterino.de