summaryrefslogtreecommitdiffstats
path: root/lib/exilog_util.pm
blob: 2823bf2ca651cc59f898dd0ab768a4c8ee90207d (plain)
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
#!/usr/bin/perl -w
#
# This file is part of the exilog suite.
#
# http://duncanthrax.net/exilog/
#
# (c) Tom Kistner 2004
#
# See LICENSE for licensing information.
#

package exilog_util;
use Time::Local;
use POSIX qw( strftime );
use strict;
use exilog_config;

BEGIN {
  use Exporter;
  use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);

  # set the version for version checking
  $VERSION     = 0.1;
  @ISA         = qw(Exporter);
  @EXPORT      = qw(
                      &edt
                      &edv
                      &ina
                      &date_to_stamp
                      &stamp_to_date
                      &human_size
                      &dos2rx
                      &dos2sql
                      &png
                   );

  %EXPORT_TAGS = ();

  # your exported package globals go here,
  # as well as any optionally exported functions
  @EXPORT_OK   = qw();

}

sub png {
  my $image = shift;
  my $width = shift;
  my $height = shift;
  my $title = shift || "";

  return '<img src="'.$image.'" width="'.$width.'" height="'.$height.'" title="'.$title.'" border="0">';
}


# turns DOS wildcards (* and ?) into regular expressions
sub dos2rx {
  my $cand = shift;

  # quote every funky character
  $cand =~ s/([^A-Za-z0-9 _?*])/\\$1/g;

  $cand =~ s/\?/./g;
  $cand =~ s/\*/.*?/g;

  return '^'.$cand.'$';
};

# turns DOS wildcards (* and ?) into SQL wildcards (% and .)
sub dos2sql {
  my $cand = shift;

  $cand =~ s/\*/%/g;
  $cand =~ s/\?/./g;

  return $cand;
};

# checks if scalar is in array
sub ina {
  my $aref = shift || [];
  my $str = shift || "";

  unless (ref($aref) eq 'ARRAY') {
    $aref = [ $aref ];
  };

  foreach (@{ $aref }) {
    return 1 if ($_ eq $str);
  };
  return 0;
};


# exists, defined and true (in perl sense)
sub edt {
  my $h = shift;
  my $hkey = shift;
  return 0 unless (ref($h) eq 'HASH');
  return 1 if ( exists($h->{$hkey}) &&
                defined($h->{$hkey}) &&
                $h->{$hkey} );
  return 0;
};


# exists, defined and valid (that is, not empty)
sub edv {
  my $h = shift;
  my $hkey = shift;
  return 0 unless (ref($h) eq 'HASH');
  return 1 if ( exists($h->{$hkey}) &&
                defined($h->{$hkey}) &&
                $h->{$hkey} ne '' );
  return 0;
};


sub date_to_stamp {
  my $date = shift || "";
  my $tod = shift || "00:00:00";
  my ($year,$month,$mday) = split /\-/, $date;
  my ($hour,$minute,$second,$junk) = split /[: ]/, $tod;
  $year-=1900;
  $month--;

  # This is for parsing timestamps that include GMT offsets
  if (edv($junk)) {
    my $hoff = ($junk =~ /[-+](\d\d)\d\d/);
    my $moff = ($junk =~ /[-+]\d\d(\d\d)/);
    if ($junk =~ /\+/) {
      $hour = $hour - $hoff;
      $minute = $minute - $moff;
    }
    else {
      $hour = $hour + $hoff;
      $minute = $minute + $moff;
    }
  };

  if ($config->{web}->{timestamps} eq 'local') {
    return timelocal($second,$minute,$hour,$mday,$month,$year);
  }
  else {
    return timegm($second,$minute,$hour,$mday,$month,$year);
  };
};


sub stamp_to_date {
  my $stamp = shift;
  my $no_seconds = shift || 0;
  # convert to date/time string
  if ($config->{web}->{timestamps} eq 'local') {
    return ($no_seconds ? strftime("%Y-%m-%d %H:%M",localtime($stamp)) : strftime("%Y-%m-%d %H:%M:%S",localtime($stamp)));
  }
  else {
    return ($no_seconds ? strftime("%Y-%m-%d %H:%M",gmtime($stamp)) : strftime("%Y-%m-%d %H:%M:%S",gmtime($stamp)));
  };
};

sub human_size {
  my $size = shift;
  my @units = ( '', 'k', 'M', 'G' );
  while ( ($size > 9999) && ((scalar @units) > 1) ) {
    shift @units;
    $size = int($size/1024);
  };
  return $size.$units[0];
};


1;