File: Util.pm

package info (click to toggle)
znuny 6.5.18-1
  • links: PTS
  • area: non-free
  • in suites: forky, sid
  • size: 205,344 kB
  • sloc: perl: 1,038,694; xml: 74,551; javascript: 65,276; sql: 23,574; sh: 417; makefile: 63
file content (312 lines) | stat: -rw-r--r-- 8,860 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# --
# Copyright (C) 2021 Znuny GmbH, https://znuny.org/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --
## nofilter(TidyAll::Plugin::Znuny::Perl::LayoutObject)

package Kernel::System::Util;

use strict;
use warnings;

use MIME::Base64;

use Kernel::System::VariableCheck qw(:all);

our @ObjectDependencies = (
    'Kernel::Config',
    'Kernel::Output::HTML::Layout',
    'Kernel::System::Main',
);

=head1 NAME

Kernel::System::Util

=head1 DESCRIPTION

All Util functions.

=head1 PUBLIC INTERFACE

=head2 new()

Create an object. Do not use it directly, instead use:

    use Kernel::System::ObjectManager;
    local $Kernel::OM = Kernel::System::ObjectManager->new();
    my $UtilObject = $Kernel::OM->Get('Kernel::System::Util');

=cut

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

    my $Self = \%Param;
    bless( $Self, $Type );

    return $Self;
}

=head2 IsITSMInstalled()

Checks if ITSM is installed.

    my $IsITSMInstalled = $UtilObject->IsITSMInstalled();

    Returns 1 if ITSM is installed and 0 otherwise.

=cut

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

    # Use cached result because it won't change within the process.
    return $Self->{ITSMInstalled} if defined $Self->{ITSMInstalled};

    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');

    # Just use some arbitrary ITSM::Core SysConfig option to check if ITSM is present.
    $Self->{ITSMInstalled} = $ConfigObject->Get('Frontend::Module')->{AdminITSMCIPAllocate} ? 1 : 0;

    return $Self->{ITSMInstalled};
}

=head2 IsITSMIncidentProblemManagementInstalled()

Checks if ITSMIncidentProblemManagement is installed.

    my $IsITSMIncidentProblemManagementInstalled = $UtilObject->IsITSMIncidentProblemManagementInstalled();

    Returns 1 if ITSMIncidentProblemManagement is installed and 0 otherwise.

=cut

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

    # Use cached result because it won't change within the process.
    return $Self->{ITSMIncidentProblemManagementInstalled} if defined $Self->{ITSMIncidentProblemManagementInstalled};

    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');

    # Just use some arbitrary SysConfig option to check if IncidentProblemManagement is present.
    $Self->{ITSMIncidentProblemManagementInstalled}
        = $ConfigObject->Get('Frontend::Module')->{AgentITSMIncidentProblemManagement} ? 1 : 0;

    return $Self->{ITSMIncidentProblemManagementInstalled};
}

=head2 IsFrontendContext()

Checks if current code is being executed in frontend context, e.g. agent frontend.

    my $IsFrontendContext = $UtilObject->IsFrontendContext();

    Returns 1 if current code is being executed in frontend context.
    Returns 0 if otherwise (e.g. console command context).

=cut

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

    # Note that "exists" is required. Otherwise Perl will create the key
    # with an undefined value which causes crashes since the object manager
    # won't work properly anymore.
    return if !exists $Kernel::OM->{Objects}->{'Kernel::Output::HTML::Layout'};

    my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
    return if !$LayoutObject->{Action};

    return 1;
}

=head2 Base64DeepEncode()

    Base-64 encodes elements of given data for given keys.
    If data is an array, all of its child elements will be checked for given keys whose elements
    will be encoded recursively.

    my $Base64EncodedData = $UtilObject->Base64DeepEncode(
        # Data can be a scalar, hash or array
        Data => {
            Article => {
                # ...
            },
            # ...
        },
        HashKeys => [
            # All 'Body' elements of array $Hash->{Articles} will be base-64 encoded.
            # Also can mean: $Hash->{Articles}->{Body}, if 'Articles' is a hash.
            # Will encode nothing if last key ('Body') cannot be reached or is not a scalar/string.
            'Articles->Body',

            'QueueData->Comment',
            # ...
        ],
    );

=cut

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

    if ( !ref $Param{Data} ) {
        return encode_base64( $Param{Data} // '' );
    }
    elsif ( ref $Param{Data} eq 'ARRAY' ) {
        for my $Element ( @{ $Param{Data} } ) {
            $Element = $Self->Base64DeepEncode(
                Data     => $Element,
                HashKeys => $Param{HashKeys},
            );
        }
    }
    elsif ( ref $Param{Data} eq 'HASH' ) {
        return $Param{Data} if !IsArrayRefWithData( $Param{HashKeys} );

        NESTEDHASHKEYS:
        for my $NestedHashKeys ( @{ $Param{HashKeys} } ) {
            my @HashKeys = split '->', $NestedHashKeys;
            while (@HashKeys) {
                my $HashKey = shift @HashKeys;

                next NESTEDHASHKEYS if !exists $Param{Data}->{$HashKey};

                $Param{Data}->{$HashKey} = $Self->Base64DeepEncode(
                    Data     => $Param{Data}->{$HashKey},
                    HashKeys => [
                        ( join '->', @HashKeys ),
                    ],
                );
            }
        }
    }

    return $Param{Data};
}

=head2 DataStructureRemoveElements()

    Removes elements of given data for given keys.
    If data is an array, all of its child elements will be checked for given keys whose elements
    will be removed recursively.

    my $Data = $UtilObject->DataStructureRemoveElements(
        # Data can be a scalar, hash or array
        Data => {
            Article => {
                # ...
            },
            # ...
        },
        HashKeys => [
            # All 'Body' elements of array $Hash->{Articles} will be removed.
            # Also can mean: $Hash->{Articles}->{Body}, if 'Articles' is a hash.
            # Will remove nothing if last key ('Body') cannot be reached or is not a scalar/string.
            'Articles->Body',

            'QueueData->Comment',
            # ...
        ],
    );

=cut

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

    if ( ref $Param{Data} eq 'ARRAY' ) {
        for my $Element ( @{ $Param{Data} } ) {
            $Element = $Self->DataStructureRemoveElements(
                Data     => $Element,
                HashKeys => $Param{HashKeys},
            );
        }
    }
    elsif ( ref $Param{Data} eq 'HASH' ) {
        return $Param{Data} if !IsArrayRefWithData( $Param{HashKeys} );

        NESTEDHASHKEYS:
        for my $NestedHashKeys ( @{ $Param{HashKeys} } ) {
            my @HashKeys = split '->', $NestedHashKeys;
            while (@HashKeys) {
                my $HashKey = shift @HashKeys;

                next NESTEDHASHKEYS if !exists $Param{Data}->{$HashKey};

                # If last key, remove entire element.
                if ( !@HashKeys ) {
                    delete $Param{Data}->{$HashKey};
                    next NESTEDHASHKEYS;
                }

                $Param{Data}->{$HashKey} = $Self->DataStructureRemoveElements(
                    Data     => $Param{Data}->{$HashKey},
                    HashKeys => [
                        ( join '->', @HashKeys ),
                    ],
                );
            }
        }
    }

    return $Param{Data};
}

=head2 GetInstalledDBCRUDObjects()

    Returns installed DBCRUD objects.

    my $DBCRUDObjects = $UtilObject->GetInstalledDBCRUDObjects();

    Returns:
    my $DBCRUDObjects = [
        # DBCRUD objects as returned by $Kernel::OM->Get('Kernel::System::...')
    ];

=cut

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

    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
    my $MainObject   = $Kernel::OM->Get('Kernel::System::Main');

    my $KernelSystemPath = $ConfigObject->Get('Home') . '/Kernel/System';

    my @FilePaths = $MainObject->DirectoryRead(
        Directory => $KernelSystemPath,
        Filter    => '*.pm',
        Recursive => 1,
    );

    my @DBCRUDObjects;

    FILEPATH:
    for my $FilePath (@FilePaths) {
        my $FileContentRef = $MainObject->FileRead(
            Location => $FilePath,
        );
        next FILEPATH if ref $FileContentRef ne 'SCALAR';
        next FILEPATH if ${$FileContentRef} !~ m{^use parent\b.*?\bKernel::System::DBCRUD\b}m;

        next FILEPATH if ${$FileContentRef} !~ m{^package (.*?);$}m;
        my $DBCRUDPackage = $1;
        next FILEPATH if $DBCRUDPackage eq 'Kernel::System::DBCRUD';
        next FILEPATH if $DBCRUDPackage eq 'Kernel::System::UnitTest::DBCRUD';

        my $DBCRUDObject = $Kernel::OM->Get($DBCRUDPackage);
        next FILEPATH if !$DBCRUDObject;

        push @DBCRUDObjects, $DBCRUDObject;
    }

    return \@DBCRUDObjects;
}

1;