This file is indexed.

/usr/lib/swish-e/perl/SWISH/DateRanges.pm is in swish-e 2.4.7-4build1.

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

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
# $Id: DateRanges.pm 1137 2003-02-28 06:06:10Z whmoseley $

package SWISH::DateRanges;
use strict;

=head1 NAME

DateRanges

=head1 SYNOPSIS

    use DateRanges;
    use CGI;
    my $cgi = CGI->new();
     ...

    my %hash = (
        date_ranges     => {

            # Define what buttons to include
            time_periods    => [
                'All',
                'Today',
                'Yesterday',
                #'Yesterday onward',
                'This Week',
                'Last Week',
                'Last 90 Days',
                'This Month',
                'Last Month',
                #'Past',
                #'Future',
                #'Next 30 Days',
            ],

            # Default button
            default         => 'All',

            # Should buttons be in a row or a column?
            line_break      => 0,

            # Should a date input form be shown, too?
            date_range      => 1,
        },
    );

    


=head1 DESCRIPTION

This module provides I<basic> support for entering and using date ranges.  It
was written to use with swish-e (http://swish-e.org).

See swish.cgi in the swish-e distribution for an example.

Sorry about the interface -- if anyone really wants to use this please let me know and I'll
rewrite as OO interface!

=head1 FUNCTIONS

=cut

require Exporter;

use vars qw/$VERSION @ISA @EXPORT/;


@ISA = qw(Exporter);
$VERSION = '0.01';

@EXPORT = qw (
    DateRangeForm
    DateRangeParse
    GetDateRangeArgs
);

    # what to pick from
    my @TIME_PERIODS = (
        'Today',
        'Yesterday',
        'Yesterday onward',
        'This Week',
        'Last Week',
        'Last 90 Days',
        'This Month',
        'Last Month',
        'Past',
        'Future',
        'Next 30 Days',
        'All',
        'Select Date Range',
    );

    my %Num_to_label;
    my %Label_to_num;
    my $i = 1;
    for ( @TIME_PERIODS ) {
        my $label = $_;
        $label =~ s/ /&nbsp;/g;
        $Num_to_label{$i} = $label;
        $Label_to_num{$_} = $i++;
    }



use Date::Calc qw /
    Day_of_Week_to_Text
    Day_of_Week
    Date_to_Text
    Monday_of_Week
    Week_of_Year
    Today
    Add_Delta_Days
    Days_in_Month
    check_date
/;

my $prefix = 'dr';

use Time::Local;


=item DateRangeForm( $cgi, $params, $fields );

This function simple creates a simple form for selecting date ranges based
on the fields passed in C<$params>.  Will call C<die()> on errors.

C<$params> must be a hash reference with a key named C<time_periods> as shown in
B<SYNOPSIS> above.  This is used to select which time periods to display.

C<$fields> is a reference to a hash where C<DateRanges> returns data.

These store the HTML for display on your form.

    buttons - the buttons to select the different time ranges
    date_range_button - the button to select a date range
    date_range_low - the low range select form fields
    date_range_hi  - the hight range select form fields

=cut

sub DateRangeForm {
    my ( $CGI, $params, $fields ) = @_;

    die "Must supply array ref for 'options'"
        unless $params->{time_periods} && ref $params->{time_periods} eq 'ARRAY';



    my @time_periods;

    # Filter out valid selections for radio buttons
    for ( @{ $params->{time_periods} } ) {
        next if !$Label_to_num{$_} || $_ eq 'Select Date Range';
        push @time_periods, $Label_to_num{$_};
    }


    # Set default as default passed in, or the first one, or 'Select Date Range' if none passed in
    my $default = $params->{default} && $Label_to_num{$params->{default}}
                  ? $Label_to_num{$params->{default}}
                  : @time_periods ? $time_periods[0] :  $Label_to_num{'Select Date Range'};



    $fields->{buttons} = '';
    $fields->{date_range_button} = '';
    $fields->{date_range_low}  =  '';
    $fields->{date_range_high} =  '';

    my $autoescape = $CGI->autoEscape(undef);  # labels have HTML

    $fields->{buttons} = 
          $CGI->radio_group( 
            -name       => 'dr_o',
            -values     => \@time_periods,
            -default    => $default,
            -linebreak  => (exists $params->{line_break} ? $params->{line_break} : 1),
            -labels     => \%Num_to_label,
            #-columns=>2,
          ) if @time_periods;


    if ( $params->{date_range} ) {

        $fields->{date_range_button} = 
            $CGI->radio_group(
                -name       => 'dr_o',
                -values     => [$Label_to_num{'Select Date Range'}],
                -default    => $default,
                -labels     => \%Num_to_label,
                -linebreak  => (exists $params->{line_break} ? $params->{line_break} : 1),
              );



        $fields->{date_range_low}  =  show_date_input($CGI, 's');
        $fields->{date_range_high} =  show_date_input($CGI, 'e');
    }


    $CGI->autoEscape($autoescape);

}

=item my $args = GetDateRangeArgs( $cgi );

Returns a string to use in a HREF with all the parameters set.

=cut

sub GetDateRangeArgs {
    my $CGI = shift;

    my %args;

    
    $args{dr_o} = $CGI->param('dr_o')
        if defined $CGI->param('dr_o');


    for ( qw/ mon day year / ) {
        my $start = "dr_s_$_";
        my $end   =  "dr_e_$_";
        $args{$start} = $CGI->param($start) if defined $CGI->param($start);
        $args{$end} = $CGI->param($end) if defined $CGI->param($end);
    }

    return '' unless %args;

    return join '&amp;', map { "$_=" . $CGI->escape($args{$_}) } keys %args;
    
}

=item DateRangeParse( $cgi, $form )

Parses the date range form and returns a low and high range unix timestamp.
Returns false on error with the folowing key set in C<$form>:

    dr_error - error string explaining the problem

C<$form> is a hash reference where the following keys may be set:

    All - no date ranges were selected
    dr_time_low - low range unix timestamp
    dr_time_high - high range unix timestamp

=cut    
    



#------------------------ Get the report dates ---------------------
sub DateRangeParse {
    my ( $q, $form ) = @_;

    $form->{dr_error} = '';


    # For making HREFs
    $form->{data_range_href} =  GetDateRangeArgs( $q );


    
    

    # If requesting ALL (or not found in form) return true for all

    my $num = $q->param('dr_o') || $Label_to_num{All};

    # In range?
   
    if ( $num !~ /^\d+$/ || $num < 1 || $num > @TIME_PERIODS ) {
        return 1;
    }

    my $time = time();

    my ( @start, @end );

    for ( $TIME_PERIODS[$num-1] ) {

        /^All/        && do { return 1 };  # don't set any dates
    
        /^Today/      && do { @start = @end = Today(); last; };

        /^Yesterday onward/  && do {
            @start = Add_Delta_Days( Today(), -1 );
            @end = (2030,1,1);
            last;
         };
        
        /^Yesterday/  && do {
            @start = @end = Add_Delta_Days( Today(), -1 );
            last;
        };


        /^This Week/ && do {
            @start = Monday_of_Week( Week_of_Year( Today() ) );
            @end   = Add_Delta_Days( @start, 6 );
            last;
        };

        /^Last Week/ && do {
            @start = Monday_of_Week( Week_of_Year( Add_Delta_Days( Today(), -7 ) ) );
            @end   = Add_Delta_Days( @start, 6 );
            last;
        };

        /^This Month/ && do {
            @start = @end = Today();
            $start[2] = 1;
            $end[2] = Days_in_Month($end[0],$end[1]);
            last;
        };
        
        /^Last Month/ && do {
            @start = Today();
            $start[2] = 1;
            --$start[1];
            if ( $start[1] == 0 ) {
                $start[1] = 12;
                $start[0]--;
            }
            @end = @start;
            $end[2] = Days_in_Month($end[0],$end[1]);
            last;
        };

        /^Last 90 Days/ && do {
            @end = Today();
            @start = Add_Delta_Days( Today(), -90 );
            last
        };

        /^Past/ && return 1;  # use defaults;

        /^Future/ && do {
            $form->{dr_time_low} = time;
            delete $form->{dr_time_high};
            return 1;
        };

        /^Next 30 Days/   && do {
            @start = Today();
            @end = Add_Delta_Days( Today(), +30 );
            last
        };
        


        /^Select/ && do {
            my ( $day, $mon, $year );

            $day    = $q->param('dr_s_day') || 0;
            $mon    = $q->param('dr_s_mon') || 0;
            $year   = $q->param('dr_s_year') || 0;
            @start = ( $year, $mon, $day );

            $day    = $q->param('dr_e_day') || 0;
            $mon    = $q->param('dr_e_mon') || 0;
            $year   = $q->param('dr_e_year') || 0;
            @end = ( $year, $mon, $day );
            last;
        };

        $form->{dr_error} = 'Invalid Date Option ' . $q->param('dr_o') . ' Selected';
        return;
    }


    
    $form->{dr_error} = 'Invalid Start Date' && return if @start && !check_date( @start );
    $form->{dr_error} = 'Invalid Ending Date' && return if @end && !check_date( @end );


    my $start_time = @start ? timelocal( 0, 0, 0, $start[2], $start[1]-1, $start[0]-1900 ) : 0;
    my $end_time   = @end   ? timelocal( 59, 59, 23, $end[2], $end[1]-1, $end[0]-1900 ) : 0;


    $form->{dr_error} = "Starting time should be before now, don't you think?" && return
        if $start_time && $start_time > time();
        
    $form->{dr_error} = 'Start date must be same day or before end date' && return
        if $start_time && $end_time && $start_time > $end_time;


    $form->{dr_time_low} = $start_time;
    $form->{dr_time_high} = $end_time;

    print STDERR "$TIME_PERIODS[$num-1]: ", scalar localtime($start_time), " -> ", scalar localtime($end_time), "\n"
        if 0;

    return 1;
}


#----------------------------------------------------------------------------
sub show_date_input {
    my ( $CGI, $name ) = @_;

    $name = "dr_$name";

    my @months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
    my $x = 1;
    my %months = map { $x++, $_ } @months;

    my ($mon, $day, $year) = (localtime)[4,3,5];

    $year = $year + 1900;
    $mon++;

    my $cur_year = $year;

    #$cur_year += 5;

    ($year,$mon,$day) = Date::Calc::Add_Delta_Days($year,$mon,$day, -28 ) if $name eq 'start';


    my $str = join "\n", 
        $CGI->popup_menu(
            -name   => "${name}_mon",
            -values     => [1..12],
            -default    => $mon,
            -labels     => \%months
        ),
        '&nbsp;',
        $CGI->popup_menu(
            -name       => "${name}_day",
            -default    => $day,
            -values     => [1..31],
        ),

        '&nbsp;',
        $CGI->popup_menu(
            -name       => "${name}_year",
            -default    => $year,
            -values     => [reverse ($cur_year-8..$cur_year) ],
        );


    return $str;
}

#----------------------- ymd_to_unix --------------------------
# Silly Date::Calc
sub ymd_to_unix {
    my ($y, $m, $d ) = @_;
    $m--;
    $y -= 1900;
    return timelocal( 0, 0, 0, $d, $m, $y );
}

1;

=back

=head1 COPYRIGHT

Copyright 2001 Bill Moseley

This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=cut