Skip to content

Commit

Permalink
Merge pull request #1326 from RotherOSS/issue-#1317-sync_with_10_1
Browse files Browse the repository at this point in the history
Issue #1317 sync with 10 1
  • Loading branch information
bschmalhofer committed Oct 15, 2021
2 parents dfff897 + ec4d404 commit 84c5c89
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 131 deletions.
144 changes: 122 additions & 22 deletions Kernel/System/Main.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,26 @@ package Kernel::System::Main;
use strict;
use warnings;

# core modules
use Digest::MD5 qw(md5_hex);
use Data::Dumper;
use File::stat;
use Unicode::Normalize;
use List::Util qw();
use List::Util qw(first);
use Fcntl qw(:flock);
use Encode;
use Math::Random::Secure qw();

# CPAN modules
use Math::Random::Secure qw(rand);

# OTOBO modules
use Kernel::System::VariableCheck qw(IsStringWithData);

# md5_hex, LOCK_SH, LOCK_EX, LOCK_NB, LOCK_UN, rand, IsStringWithData
# should not be available as methods.
# On the other hand, new should not be purged.
use namespace::autoclean;

our @ObjectDependencies = (
'Kernel::System::Encode',
'Kernel::System::Log',
Expand All @@ -44,26 +53,35 @@ Kernel::System::Main - main object
=head1 DESCRIPTION
All main functions to load modules, die, and handle files.
A collection of utility functions to:
=over 4
=item load modules
=item die
=item generate random strings
=item handle files
=back
=head1 PUBLIC INTERFACE
=head2 new()
create new object. Do not use it directly, instead use:
create a new object. Do not use it directly, instead use:
my $MainObject = $Kernel::OM->Get('Kernel::System::Main');
=cut

sub new {
my ( $Type, %Param ) = @_;

# allocate new hash for object
my $Self = {};
bless( $Self, $Type );
my ( $Class, %Param ) = @_;

return $Self;
# allocate a new empty hash for object
return bless {}, $Class;
}

=head2 Require()
Expand Down Expand Up @@ -130,7 +148,8 @@ sub RequireBaseClass {
my ( $Self, $Module ) = @_;

# Load the module, if not already loaded.
return if !$Self->Require($Module);
# Give up when the module can't be loaded.
return unless $Self->Require($Module);

my $CallingClass = caller(0);

Expand All @@ -139,9 +158,7 @@ sub RequireBaseClass {

# Check if the base class was already loaded.
# This can happen in persistent environments as mod_perl (see bug#9686).
if ( List::Util::first { $_ eq $Module } @{"${CallingClass}::ISA"} ) {
return 1; # nothing to do now
}
return 1 if first { $_ eq $Module } @{"${CallingClass}::ISA"};

push @{"${CallingClass}::ISA"}, $Module;
}
Expand Down Expand Up @@ -635,21 +652,24 @@ sub FileDelete {

=head2 FileGetMTime()
get timestamp of file change time
get epoch of file change time
# specify either Directory and Filename
my $FileMTime = $MainObject->FileGetMTime(
Directory => 'c:\some\location',
Directory => '/some/location',
Filename => 'me_to/alal.xml',
# or Location
Location => 'c:\some\location\me_to\alal.xml'
);
# or Location
my $FileMTime = $MainObject->FileGetMTime(
Location => '/some/location/me_to/alal.xml'
);
=cut

sub FileGetMTime {
my ( $Self, %Param ) = @_;

my $FH;
if ( $Param{Filename} && $Param{Directory} ) {

# filename clean up
Expand All @@ -669,7 +689,6 @@ sub FileGetMTime {
Priority => 'error',
Message => 'Need Filename and Directory or Location!',
);

}

# get file metadata
Expand Down Expand Up @@ -700,6 +719,84 @@ sub FileGetMTime {
return $Stat->mtime();
}

=head2 GetReleaseInfo()
extract the Product and Version from a RELEASE file
# specify either Directory and Filename
my $ReleaseInfo = $MainObject->GetReleaseInfo(
Directory => '/opt/otobo',
Filename => 'RELEASE',
);
# or Location
my $ReleaseInfo = $MainObject->GetReleaseInfo(
Location => '/opt/otobo/RELEASE'
);
The returned value is a hashref. There are two possible keys: B<Product> and B<Version>.
The keys are only set when they are found in the release file.
=cut

sub GetReleaseInfo {
my ( $Self, %Param ) = @_;

if ( $Param{Filename} && $Param{Directory} ) {

# filename clean up
$Param{Filename} = $Self->FilenameCleanUp(
Filename => $Param{Filename},
Type => $Param{Type} || 'Local', # Local|Attachment|MD5
);
$Param{Location} = "$Param{Directory}/$Param{Filename}";
}
elsif ( $Param{Location} ) {

# filename clean up
$Param{Location} =~ s{//}{/}xmsg;
}
else {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => 'Need Filename and Directory or Location!',
);

return {};
}

if ( open( my $ReleaseFH, '<', $Param{Location} ) ) { ## no critic qw(InputOutput::RequireBriefOpen OTOBO::ProhibitOpen)

# extract the release info from the file content
my %ReleaseInfo;
LINE:
while ( my $Line = <$ReleaseFH> ) {

# filtering of comment lines
next LINE if $Line =~ m/^#/;

if ( $Line =~ m/^PRODUCT\s{0,2}=\s{0,2}(.*)\s{0,2}$/i ) {
$ReleaseInfo{Product} = $1;
}
elsif ( $Line =~ m/^VERSION\s{0,2}=\s{0,2}(.*)\s{0,2}$/i ) {
$ReleaseInfo{Version} = $1;
}

# all other lines are ignored
}

return \%ReleaseInfo;
}

# log error when the file could not be read
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Could not open $Param{Location} for reading.",
);

return {};
}

=head2 MD5sum()
get an C<MD5> sum of a file or a string
Expand Down Expand Up @@ -1028,7 +1125,7 @@ sub DirectoryRead {
=head2 GenerateRandomString()
generate a random string of defined length, and of a defined alphabet.
defaults to a length of 16 and alphanumerics ( 0..9, A-Z and a-z).
Defaults to a length of 16 and alphanumerics ( 0..9, A-Z and a-z).
my $String = $MainObject->GenerateRandomString();
Expand All @@ -1051,7 +1148,7 @@ with specific length and alphabet:
my $String = $MainObject->GenerateRandomString(
Length => 32,
Dictionary => [ 0..9, 'a'..'f' ], # hexadecimal
);
);
returns
Expand All @@ -1063,6 +1160,8 @@ returns
sub GenerateRandomString {
my ( $Self, %Param ) = @_;

# negative $Param{Length} produce an empty string
# fractional $Param{Length} is truncated to the integer portion
my $Length = $Param{Length} || 16;

# The standard list of characters in the dictionary. Don't use special chars here.
Expand All @@ -1073,6 +1172,7 @@ sub GenerateRandomString {
@DictionaryChars = @{ $Param{Dictionary} };
}

# assuming that there are no dictionaries larger than 2^32
my $DictionaryLength = scalar @DictionaryChars;

# generate the string
Expand Down
Loading

0 comments on commit 84c5c89

Please sign in to comment.