X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FCommon.pm;h=33d4d41cf48705c563500b6defac83667a66ae7f;hb=fee5532a132c44dcfc1743393cba00c8e3397176;hp=4c65042cb28cbe30a341589c12e2085e32eb4327;hpb=d4dd649f8f6e32bc31929bf83d536344745cca05;p=kivitendo-erp.git diff --git a/SL/Common.pm b/SL/Common.pm index 4c65042cb..33d4d41cf 100644 --- a/SL/Common.pm +++ b/SL/Common.pm @@ -12,11 +12,16 @@ use utf8; use strict; use Carp; +use English qw(-no_match_vars); use Time::HiRes qw(gettimeofday); use Data::Dumper; -use File::Copy; +use File::Copy (); use File::stat; use File::Slurp; +use File::Spec; +use List::MoreUtils qw(apply); +use POSIX (); +use Encode qw(decode); use SL::DBUtils; @@ -351,7 +356,7 @@ sub webdav_folder { my $base_path = $ENV{'SCRIPT_NAME'}; $base_path =~ s|[^/]+$||; if (opendir my $dir, $path) { - foreach my $file (sort { lc $a cmp lc $b } readdir $dir) { + foreach my $file (sort { lc $a cmp lc $b } map { decode("UTF-8", $_) } readdir $dir) { next if (($file eq '.') || ($file eq '..')); my $fname = $file; @@ -392,11 +397,13 @@ sub get_vc_details { vc.*, pt.description AS payment_terms, b.description AS business, - l.description AS language + l.description AS language, + dt.description AS delivery_terms FROM ${vc} vc LEFT JOIN payment_terms pt ON (vc.payment_id = pt.id) LEFT JOIN business b ON (vc.business_id = b.id) LEFT JOIN language l ON (vc.language_id = l.id) + LEFT JOIN delivery_terms dt ON (vc.delivery_term_id = dt.id) WHERE vc.id = ?|; my $ref = selectfirst_hashref_query($form, $dbh, $query, $vc_id); @@ -595,70 +602,73 @@ sub get_webdav_folder { } sub copy_file_to_webdav_folder { - $main::lxdebug->enter_sub(); + $::lxdebug->enter_sub(); my ($form) = @_; my ($last_mod_time, $latest_file_name, $complete_path); # checks foreach my $item (qw(tmpdir tmpfile type)){ - if (!$form->{$item}){ - $main::lxdebug->message(0, 'Missing parameter'); - $main::form->error($main::locale->text("Missing parameter for webdav file copy")); - } + next if $form->{$item}; + $::lxdebug->message(LXDebug::WARN(), 'Missing parameter'); + $::form->error($::locale->text("Missing parameter for WebDAV file copy")); } my ($webdav_folder, $document_name) = get_webdav_folder($form); if (! $webdav_folder){ - $main::lxdebug->leave_sub(); - $main::form->error($main::locale->text("Cannot check correct webdav folder")); + $::lxdebug->leave_sub(); + $::form->error($::locale->text("Cannot check correct WebDAV folder")); return undef; } - $complete_path = join('/', $form->{cwd}, $webdav_folder); + $complete_path = File::Spec->catfile($form->{cwd}, $webdav_folder); + + # maybe the path does not exist (automatic printing), see #2446 + if (!-d $complete_path) { + # we need a chdir and restore old dir + my $current_dir = POSIX::getcwd(); + chdir("$form->{cwd}"); + mkdir_with_parents($webdav_folder); + chdir($current_dir); + } + opendir my $dh, $complete_path or die "Could not open $complete_path: $!"; my ($newest_name, $newest_time); while ( defined( my $file = readdir( $dh ) ) ) { my $path = File::Spec->catfile( $complete_path, $file ); next if -d $path; # skip directories, or anything else you like - ( $newest_name, $newest_time ) = ( $file, -M _ ) - if( ! defined $newest_time or -M $path < $newest_time ); - } - $latest_file_name = $complete_path .'/' . $newest_name; - my $filesize = stat($latest_file_name)->size; + ( $newest_name, $newest_time ) = ( $file, -M _ ) if( ! defined $newest_time or -M $path < $newest_time ); + } + + closedir $dh; + + $latest_file_name = File::Spec->catfile($complete_path, $newest_name); + my $filesize = stat($latest_file_name)->size; + + my $current_file = File::Spec->catfile($form->{tmpdir}, apply { s:.*/:: } $form->{tmpfile}); + my $current_filesize = -f $current_file ? stat($current_file)->size : 0; - my ($ext) = $form->{tmpfile} =~ /(\.[^.]+)$/; - my $current_file = join('/', $form->{tmpdir}, $form->{tmpfile}); - my $current_filesize = stat($current_file)->size; if ($current_filesize == $filesize) { - $main::lxdebug->leave_sub(); + $::lxdebug->leave_sub(); return; } - my $timestamp = get_current_formatted_time(); - my $myfilename = $form->generate_attachment_filename(); - $myfilename =~ s/\./$timestamp\./; + my $timestamp = get_current_formatted_time(); + my $new_file = File::Spec->catfile($form->{cwd}, $webdav_folder, $form->generate_attachment_filename()); + $new_file =~ s{(.*)\.}{$1$timestamp\.}; - if (!copy(join('/', $form->{tmpdir}, $form->{tmpfile}), join('/', $form->{cwd}, $webdav_folder, $myfilename))) { - my $j = join('/', $form->{tmpdir}, $form->{tmpfile}); - my $k = join('/', $form->{cwd}, $webdav_folder); - $main::lxdebug->message(0, "Copy file from $j to $k failed"); - $main::form->error($main::locale->text("Copy file from #1 to #2 failed", $j, $k)); + if (!File::Copy::copy($current_file, $new_file)) { + $::lxdebug->message(LXDebug::WARN(), "Copy file from $current_file to $new_file failed: $ERRNO"); + $::form->error($::locale->text("Copy file from #1 to #2 failed: #3", $current_file, $new_file, $ERRNO)); } - $main::lxdebug->leave_sub(); + $::lxdebug->leave_sub(); } -sub get_current_formatted_time { - $main::lxdebug->enter_sub(); - - my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); - my $formatted_current_time = sprintf ( "_%04d%02d%02d_%02d%02d%02d", - $year+1900,$mon+1,$mday,$hour,$min,$sec); - $main::lxdebug->leave_sub(); - return $formatted_current_time; +sub get_current_formatted_time { + return POSIX::strftime('_%Y%m%d_%H%M%S', localtime()); } 1;