| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package App::Squid::Redirector::Fugu::BDB; | ||
| 5 | |||
| 6 | use App::Squid::Redirector::Fugu::SQL; | ||
| 7 | |||
| 8 | use BerkeleyDB; | ||
| 9 | |||
| 10 | |||
| 11 | sub compile_file { | ||
| 12 | my($self, $bdbfile, $file, $fn) = @_; | ||
| 13 | |||
| 14 | my $conn = $self->open($bdbfile, 1); | ||
| 15 | |||
| 16 | # append dir | ||
| 17 | $file = $self->{dir}.'/'.$file; | ||
| 18 | |||
| 19 | # open text file | ||
| 20 | open(my $fh, '<', $file) or $self->{logger}->die("Unable to open file $file: $!"); | ||
| 21 | |||
| 22 | while(my $data = <$fh>) { | ||
| 23 | |||
| 24 | chomp($data); | ||
| 25 | |||
| 26 | # run $fn that will receive data (current line) as param | ||
| 27 | next unless(&$fn($data)); | ||
| 28 | |||
| 29 | # Add to db | ||
| 30 | $conn->db_put($data, 1) ; | ||
| 31 | |||
| 32 | } | ||
| 33 | close($fh); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub compile_domain_file { | ||
| 37 | my($self, $name, $file) = @_; | ||
| 38 | |||
| 39 | $self->compile_file("$name-domain.db", $file, sub { | ||
| 40 | my $data = shift; | ||
| 41 | # domain list cannot contain protocol | ||
| 42 | if($data =~ /^\w+\:\/\//) { | ||
| 43 | print "Domains list cannot contain protocol: $data\n"; | ||
| 44 | $self->{logger}->log("Domains list cannot contain protocol: $data"); | ||
| 45 | return 0; | ||
| 46 | } | ||
| 47 | # invalid characters | ||
| 48 | if($data =~ /\//) { | ||
| 49 | print "Domains cannot contain '/': $data\n"; | ||
| 50 | $self->{logger}->log("Domains cannot contain '/': $data"); | ||
| 51 | return 0; | ||
| 52 | } | ||
| 53 | return 1; | ||
| 54 | }); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub compile_url_file { | ||
| 58 | my($self, $name, $file) = @_; | ||
| 59 | |||
| 60 | $self->compile_file("$name-url.db", $file, sub { | ||
| 61 | my $data = shift; | ||
| 62 | # url list cannot contain protocol | ||
| 63 | if($data =~ /^\w+\:\/\//) { | ||
| 64 | print "URLs list cannot contain protocol: $data\n"; | ||
| 65 | $self->{logger}->log("URLs list cannot contain protocol: $data"); | ||
| 66 | return 0; | ||
| 67 | } | ||
| 68 | # invalid characters | ||
| 69 | unless($data =~ /\//) { | ||
| 70 | print "URLs must have the following structure www.domain.com/uri : $data\n"; | ||
| 71 | $self->{logger}->log("URLs must have the following structure www.domain.com/uri : $data"); | ||
| 72 | return 0; | ||
| 73 | } | ||
| 74 | return 1; | ||
| 75 | }); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub compile_sql { | ||
| 79 | my($self, $bdbfile, $query, $fn) = @_; | ||
| 80 | |||
| 81 | my $conn = $self->open($bdbfile, 1); | ||
| 82 | |||
| 83 | my $sql = App::Squid::Redirector::Fugu::SQL->new(); | ||
| 84 | $sql->set_logger($self->{logger}); | ||
| 85 | $sql->set_dbh($self->{dbh}); | ||
| 86 | $sql->run($query, sub { | ||
| 87 | my $sth = shift; | ||
| 88 | while(my @row = $sth->fetchrow_array()) { | ||
| 89 | my $data = $row[0]; | ||
| 90 | |||
| 91 | chomp($data); | ||
| 92 | |||
| 93 | # run $fn that will receive data (current line) as param | ||
| 94 | next unless(&$fn($data)); | ||
| 95 | |||
| 96 | # Add to db | ||
| 97 | $conn->db_put($data, 1) ; | ||
| 98 | |||
| 99 | } | ||
| 100 | }); | ||
| 101 | |||
| 102 | } | ||
| 103 | |||
| 104 | sub compile_domain_sql { | ||
| 105 | my($self, $name, $query) = @_; | ||
| 106 | |||
| 107 | $self->compile_sql("$name-domain.db", $query, sub { | ||
| 108 | my $data = shift; | ||
| 109 | # domain list cannot contain protocol | ||
| 110 | if($data =~ /^\w+\:\/\//) { | ||
| 111 | print "Domains list cannot contain protocol: $data\n"; | ||
| 112 | $self->{logger}->log("Domains list cannot contain protocol: $data"); | ||
| 113 | return 0; | ||
| 114 | } | ||
| 115 | # invalid characters | ||
| 116 | if($data =~ /\//) { | ||
| 117 | print "Domains cannot contain '/': $data\n"; | ||
| 118 | $self->{logger}->log("Domains cannot contain '/': $data"); | ||
| 119 | return 0; | ||
| 120 | } | ||
| 121 | return 1; | ||
| 122 | }); | ||
| 123 | } | ||
| 124 | sub compile_url_sql { | ||
| 125 | my($self, $name, $query) = @_; | ||
| 126 | |||
| 127 | $self->compile_sql("$name-url.db", $query, sub { | ||
| 128 | my $data = shift; | ||
| 129 | # url list cannot contain protocol | ||
| 130 | if($data =~ /^\w+\:\/\//) { | ||
| 131 | print "URLs list cannot contain protocol: $data\n"; | ||
| 132 | $self->{logger}->log("URLs list cannot contain protocol: $data"); | ||
| 133 | return 0; | ||
| 134 | } | ||
| 135 | # invalid characters | ||
| 136 | unless($data =~ /\//) { | ||
| 137 | print "URLs must have the following structure www.domain.com/uri : $data\n"; | ||
| 138 | $self->{logger}->log("URLs must have the following structure www.domain.com/uri : $data"); | ||
| 139 | return 0; | ||
| 140 | } | ||
| 141 | return 1; | ||
| 142 | }); | ||
| 143 | } | ||
| 144 | |||
| 145 | sub open { | ||
| 146 | my($self, $file, $mode) = @_; | ||
| 147 | |||
| 148 | # append dir | ||
| 149 | $file = $self->{dir}.'/'.$file; | ||
| 150 | |||
| 151 | # if write mode, delete previuos file | ||
| 152 | if($mode && -e $file) { | ||
| 153 | unlink($file) or $self->{logger}->die("Unable to delete previous version of file $file: $!"); | ||
| 154 | } | ||
| 155 | |||
| 156 | # define flag mode | ||
| 157 | my $dbflag = $mode ? DB_CREATE : DB_RDONLY; | ||
| 158 | |||
| 159 | # open bdb file | ||
| 160 | my $conn = new BerkeleyDB::Hash | ||
| 161 | -Filename => $file, | ||
| 162 | -Flags => $dbflag or $self->{logger}->die("Unable to open file $file: $! $BerkeleyDB::Error\n"); | ||
| 163 | |||
| 164 | # log file opened successfully | ||
| 165 | $self->{logger}->log("File $file opened successfully"); | ||
| 166 | |||
| 167 | return $conn; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub new { | ||
| 171 | my $class = shift; | ||
| 172 | return bless({}, $class); | ||
| 173 | } | ||
| 174 | |||
| 175 | sub set_dbh { | ||
| 176 | my($self, $dbh) = @_; | ||
| 177 | $self->{dbh} = $dbh; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub set_dir { | ||
| 181 | my($self, $dir) = @_; | ||
| 182 | $self->{dir} = $dir if($dir); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub set_logger { | ||
| 186 | my($self, $logger) = @_; | ||
| 187 | $self->{logger} = $logger; | ||
| 188 | } | ||
| 189 | |||
| 190 | 1; |