This file is indexed.

/usr/share/otrs/scripts/DBUpdate-to-3.2.pl is in otrs2 5.0.7-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#!/usr/bin/perl
# --
# DBUpdate-to-3.2.pl - update script to migrate OTRS 3.1.x to 3.2.x
# Copyright (C) 2001-2013 OTRS AG, http://otrs.com/
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
# or see http://www.gnu.org/licenses/agpl.txt.
# --

use strict;
use warnings;

# use ../ as lib location
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . '/Kernel/cpan-lib';

use Getopt::Std qw();
use Kernel::Config;
use Kernel::System::Log;
use Kernel::System::Time;
use Kernel::System::Encode;
use Kernel::System::DB;
use Kernel::System::Main;
use Kernel::System::SysConfig;
use Kernel::System::Cache;
use Kernel::System::VariableCheck qw(:all);

{

    # get options
    my %Opts;
    Getopt::Std::getopt( 'h', \%Opts );

    if ( exists $Opts{h} ) {
        print <<"EOF";

DBUpdate-to-3.2.pl - Upgrade scripts for OTRS 3.1.x to 3.2.x migration.
Copyright (C) 2001-2011 OTRS AG, http://otrs.org/

EOF
        exit 1;
    }

    # UID check if not on Windows
    if ( $^O ne 'MSWin32' && $> == 0 ) {    # $EFFECTIVE_USER_ID
        die "Cannot run this program as root. Please run it as the 'otrs' user.\n";
    }

    print "\nMigration started...\n\n";

    # create common objects
    my $CommonObject = _CommonObjectsBase();

    # define the number of steps
    my $Steps = 10;
    my $Step  = 1;

    print "Step $Step of $Steps: Refresh configuration cache... ";
    RebuildConfig($CommonObject) || die;
    print "done.\n\n";
    $Step++;

    # create common objects with new default config
    $CommonObject = _CommonObjectsBase();

    # check framework version
    print "Step $Step of $Steps: Check framework version... ";
    _CheckFrameworkVersion($CommonObject) || die;
    print "done.\n\n";
    $Step++;

    print "Step $Step of $Steps: Cleanup UserPreferences... ";
    _CleanupUserPreferences($CommonObject) || die;
    print "done.\n\n";
    $Step++;

    print "Step $Step of $Steps: Updating toolbar configuration... ";
    _MigrateToolbarConfig($CommonObject) || die;
    print "done.\n\n";
    $Step++;

    print "Step $Step of $Steps: Updating AgentTicketZoom window configuration... ";
    _MigrateAgentTicketZoomWindowConfiguration($CommonObject) || die;
    print "done.\n\n";
    $Step++;

    print "Step $Step of $Steps: Dropping obsolete columns from article_search... ";
    _DropArticleSearchColumns($CommonObject) || die;
    print "done.\n\n";
    $Step++;

    print "Step $Step of $Steps: Dropping unused indexes on article_flag... ";
    _DropArticleFlagIndexes($CommonObject) || die;
    $Step++;

    print "Step $Step of $Steps: Migration cache backend configuration... ";
    _MigrateCacheConfiguration($CommonObject) || die;
    print "done.\n\n";
    $Step++;

    # Clean up the cache completely at the end.
    print "Step $Step of $Steps: Clean up the cache... ";
    my $CacheObject = Kernel::System::Cache->new( %{$CommonObject} ) || die;
    $CacheObject->CleanUp();
    print "done.\n\n";
    $Step++;

    print "Step $Step of $Steps: Refresh configuration cache another time... ";
    RebuildConfig($CommonObject) || die;
    print "done.\n\n";

    print "Migration completed!\n";

    exit 0;
}

sub _CommonObjectsBase {
    my %CommonObject;
    $CommonObject{ConfigObject} = Kernel::Config->new();
    $CommonObject{LogObject}    = Kernel::System::Log->new(
        LogPrefix => 'OTRS-DBUpdate-to-3.2',
        %CommonObject,
    );
    $CommonObject{EncodeObject} = Kernel::System::Encode->new(%CommonObject);
    $CommonObject{MainObject}   = Kernel::System::Main->new(%CommonObject);
    $CommonObject{TimeObject}   = Kernel::System::Time->new(%CommonObject);
    $CommonObject{DBObject}     = Kernel::System::DB->new(%CommonObject);
    return \%CommonObject;
}

=item RebuildConfig($CommonObject)

refreshes the configuration to make sure that a ZZZAAuto.pm is present
after the upgrade.

    RebuildConfig($CommonObject);

=cut

sub RebuildConfig {
    my $CommonObject = shift;

    my $SysConfigObject = Kernel::System::SysConfig->new( %{$CommonObject} );

    # Rebuild ZZZAAuto.pm with current values
    if ( !$SysConfigObject->WriteDefault() ) {
        die "ERROR: Can't write default config files!";
    }

    # Force a reload of ZZZAuto.pm and ZZZAAuto.pm to get the new values
    for my $Module ( sort keys %INC ) {
        if ( $Module =~ m/ZZZAA?uto\.pm$/ ) {
            delete $INC{$Module};
        }
    }

    # reload config object
    print
        "\nIf you see warnings about 'Subroutine Load redefined', that's fine, no need to worry!\n";
    $CommonObject = _CommonObjectsBase();

    return 1;
}

=item _CheckFrameworkVersion()

Check if framework it's the correct one for Dinamic Fields migration.

    _CheckFrameworkVersion();

=cut

sub _CheckFrameworkVersion {
    my $CommonObject = shift;

    my $Home = $CommonObject->{ConfigObject}->Get('Home');

    # load RELEASE file
    if ( -e !"$Home/RELEASE" ) {
        die "ERROR: $Home/RELEASE does not exist!";
    }
    my $ProductName;
    my $Version;
    if ( open( my $Product, '<', "$Home/RELEASE" ) ) {    ## no critic
        while (<$Product>) {

            # filtering of comment lines
            if ( $_ !~ /^#/ ) {
                if ( $_ =~ /^PRODUCT\s{0,2}=\s{0,2}(.*)\s{0,2}$/i ) {
                    $ProductName = $1;
                }
                elsif ( $_ =~ /^VERSION\s{0,2}=\s{0,2}(.*)\s{0,2}$/i ) {
                    $Version = $1;
                }
            }
        }
        close($Product);
    }
    else {
        die "ERROR: Can't read $CommonObject->{Home}/RELEASE: $!";
    }

    if ( $ProductName ne 'OTRS' ) {
        die "Not framework version required"
    }
    #if ( $Version !~ /^3\.2(.*)$/ ) {
    #    die "Not framework version required"
    #}

    return 1;
}

=item _CleanupUserPreferences()

cleanup obsolete user settings from DB.

=cut

sub _CleanupUserPreferences {
    my $CommonObject = shift;

    # Remove useless UserLastLoginTimestamp because UserLastLogin contains the same data.
    # With OTRS 3.2 we convert the data from UserLastLogin to UserLastLoginTimestamp automatically.

    my $CustomerPreferencesTable
        = $CommonObject->{ConfigObject}->Get('CustomerPreferences')->{Params}->{Table}
        || 'customer_preferences';

    my $CustomerPreferencesTableKey
        = $CommonObject->{ConfigObject}->Get('CustomerPreferences')->{Params}->{TableKey}
        || 'preferences_key';

    $CommonObject->{DBObject}->Do(
        SQL =>
            "DELETE FROM $CustomerPreferencesTable WHERE $CustomerPreferencesTableKey = 'UserLastLoginTimestamp'",
    );

    # Also remove UserLastPw because it contains unsalted md5 hashes.

    my $PreferencesTable = $CommonObject->{ConfigObject}->Get('PreferencesTable')
        || 'user_preferences';

    my $PreferencesTableKey = $CommonObject->{ConfigObject}->Get('PreferencesTableKey')
        || 'preferences_key';

    $CommonObject->{DBObject}->Do(
        SQL => "DELETE FROM $PreferencesTable WHERE $PreferencesTableKey = 'UserLastPw'",
    );

    return 1;
}

=item _MigrateToolbarConfig()

changes the module name of an existing toolbar module (TicketSearchFulltext).

=cut

sub _MigrateToolbarConfig {
    my $CommonObject = shift;

    my $ToolbarConfig = $CommonObject->{ConfigObject}->Get('Frontend::ToolBarModule');

    # Check if the the TicketSearchFulltext toolbar module is activated
    return 1 if !IsHashRefWithData($ToolbarConfig);
    return 1 if !IsHashRefWithData( $ToolbarConfig->{'10-Ticket::TicketSearchFulltext'} );

    # update to use the new generic module name
    $ToolbarConfig->{'10-Ticket::TicketSearchFulltext'}->{Module}
        = 'Kernel::Output::HTML::ToolBarGeneric';

    my $SysConfigObject = Kernel::System::SysConfig->new( %{$CommonObject} );

    my $Success = $SysConfigObject->ConfigItemUpdate(
        Valid => 1,
        Key   => 'Frontend::ToolBarModule###10-Ticket::TicketSearchFulltext',
        Value => $ToolbarConfig->{'10-Ticket::TicketSearchFulltext'},
    );

    return $Success;
}

=item _MigrateAgentTicketZoomWindowConfiguration($CommonObject)

migrates the configuration of the dynamic fields in AgentTicketZoom to be
compatible with ProcessManagement new feature.

original field configration is set to the fields in the Sidebar

    _MigrateAgentTicketZoomWindowConfiguration($CommonObject);

=cut

sub _MigrateAgentTicketZoomWindowConfiguration {
    my $CommonObject = shift;

    # create a new SysConfigObject
    my $SysConfigObject = Kernel::System::SysConfig->new( %{$CommonObject} );

    # Get the values to be set
    my $ExistingSetting = $CommonObject->{ConfigObject}->Get("Ticket::Frontend::AgentTicketZoom")
        || {};
    my %ValuesToSet = %{ $ExistingSetting->{DynamicField} || {} };

    # update configuration
    my $Success = $SysConfigObject->ConfigItemUpdate(
        Valid => 1,
        Key   => 'Ticket::Frontend::AgentTicketZoom###SidebarDynamicField',
        Value => \%ValuesToSet,
    );

    # print errors if any
    if ( !$Success ) {
        print "Could not migrate the config values on AgentTicketZoom window!\n";
        return 0;
    }

    return 1;
}

=item _DropArticleSearchColumns($CommonObject)

this will check if the table article_search has some obsolete columns that were forgotten
to drop for the migration to OTRS 3.1 but also absent from the 3.1 schema definition. So
on some systems (migrated from 3.0 or older) these will be present, on some not.

Therefore we first check if the columns are present, and then in a second step we will
drop them on the fly.

    _DropArticleSearchColumns($CommonObject);

=cut

sub _DropArticleSearchColumns {
    my $CommonObject = shift;

    my $ColumnExists;

    print
        "Check if columns exist.\n";

    {
        my $STDERR;

        # Catch STDERR log messages to not confuse the user. The Prepare() will fail
        #   if the columns are not present.
        local *STDERR;
        open STDERR, '>:utf8', \$STDERR;    ## no critic

        $ColumnExists = $CommonObject->{DBObject}->Prepare(
            SQL   => "SELECT a_freekey1 FROM article_search WHERE 1=0",
            Limit => 1,
        );
    }

    if ( !$ColumnExists ) {
        print "Columns are not present, no need to drop them.\n";
        return 1;
    }

    print "Columns found, drop them.\n";

    # fetch data to avoid warnings
    while ( my @Row = $CommonObject->{DBObject}->FetchrowArray() ) {

        # noop
    }

    my $XMLString = '
    <TableAlter Name="article_search">
        <ColumnDrop Name="a_freetext1"/>
        <ColumnDrop Name="a_freetext2"/>
        <ColumnDrop Name="a_freetext3"/>
        <ColumnDrop Name="a_freekey1"/>
        <ColumnDrop Name="a_freekey2"/>
        <ColumnDrop Name="a_freekey3"/>
    </TableAlter>';

    my @SQL;
    my @SQLPost;

    my $XMLObject = Kernel::System::XML->new( %{$CommonObject} );

    # create database specific SQL and PostSQL commands
    my @XMLARRAY = $XMLObject->XMLParse( String => $XMLString );

    # create database specific SQL
    push @SQL, $CommonObject->{DBObject}->SQLProcessor(
        Database => \@XMLARRAY,
    );

    # create database specific PostSQL
    push @SQLPost, $CommonObject->{DBObject}->SQLProcessorPost();

    # execute SQL
    for my $SQL ( @SQL, @SQLPost ) {
        print $SQL . "\n";
        my $Success = $CommonObject->{DBObject}->Do( SQL => $SQL );
        if ( !$Success ) {
            $CommonObject->{LogObject}->Log(
                Priority => 'error',
                Message  => "Error during execution of '$SQL'!",
            );
            return;
        }
    }
    return 1;
}

=item _DropArticleFlagIndexes($CommonObject)

this will check if there are indexes present on article_flags that are no longer needed.
If the OTRS system was upgraded from 2.4 > 3.0 > 3.1 we'll not have these indexes.
If the OTRS system was installed at 3.0 or 3.1 and then upgraded we do have these.

    _DropArticleFlagIndexes($CommonObject);

=cut

sub _DropArticleFlagIndexes {

    my $CommonObject = shift;

    my $XMLString = <<DATABASEXML;
<database Name="otrs">
<!-- Drop unneeded indices in the article_flags table -->
<!-- First drop the foreign key constraints to work around a MySQL issue, see bug#9092. -->
<TableAlter Name="article_flag">
    <ForeignKeyDrop ForeignTable="article">
        <Reference Local="article_id" Foreign="id"/>
    </ForeignKeyDrop>
    <ForeignKeyDrop ForeignTable="users">
        <Reference Local="create_by" Foreign="id"/>
    </ForeignKeyDrop>
</TableAlter>
<!-- Now drop the indices. -->
<TableAlter Name="article_flag">
    <IndexDrop Name="article_flag_create_by"/>
    <IndexDrop Name="article_flag_article_id_article_key"/>
</TableAlter>
<!-- Now recreate the foreign key constraints. -->
<TableAlter Name="article_flag">
    <ForeignKeyCreate ForeignTable="article">
        <Reference Local="article_id" Foreign="id"/>
    </ForeignKeyCreate>
    <ForeignKeyCreate ForeignTable="users">
        <Reference Local="create_by" Foreign="id"/>
    </ForeignKeyCreate>
</TableAlter>
</database>
DATABASEXML

    my @SQL;
    my @SQLPost;

    my $XMLObject = Kernel::System::XML->new( %{$CommonObject} );

    # create database specific SQL and PostSQL commands
    my @XMLARRAY = $XMLObject->XMLParse( String => $XMLString );

    # create database specific SQL
    push @SQL, $CommonObject->{DBObject}->SQLProcessor(
        Database => \@XMLARRAY,
    );

    # create database specific PostSQL
    push @SQLPost, $CommonObject->{DBObject}->SQLProcessorPost();

    # execute SQL
    for my $SQL ( @SQL, @SQLPost ) {
        print $SQL . "\n";
        my $Success = $CommonObject->{DBObject}->Do( SQL => $SQL );
        if ($Success) {
            print "OK\n";
        }
        else {
            print "Index was not present\n";
        }
    }
    return 1;
}

=item _MigrateCacheConfiguration($CommonObject)

disable legacy cache setting. CacheFileRaw was removed.

    _MigrateCacheConfiguration($CommonObject);

=cut

sub _MigrateCacheConfiguration {
    my $CommonObject = shift;

    # create a new SysConfigObject
    my $SysConfigObject = Kernel::System::SysConfig->new( %{$CommonObject} );

    my $CacheModule = $CommonObject->{ConfigObject}->Get("Cache::Module");

    if ( $CacheModule eq 'Kernel::System::Cache::FileRaw' ) {

        my $Success = $SysConfigObject->ConfigItemUpdate(
            Valid => 1,
            Key   => 'Cache::Module',
            Value => 'Kernel::System::Cache::FileStorable',
        );

        if ( !$Success ) {
            print "Could not migrate the config value Cache::Module!\n";
            return 0;
        }
    }

    return 1;
}

1;