| 1 | package Net::Google::SafeBrowsing3::DBI; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Net::Google::SafeBrowsing3::Storage'; | ||
| 7 | |||
| 8 | use Carp; | ||
| 9 | use DBI; | ||
| 10 | use List::Util qw(first); | ||
| 11 | |||
| 12 | |||
| 13 | our $VERSION = '0.1'; | ||
| 14 | |||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Net::Google::SafeBrowsing3::DBI - Base class for all DBI-based back-end storage for the Google Safe Browsing v3 database | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | Net::Google::SafeBrowsing3::DBI cannot be used directly. Instead, use a class inheriting Net::Google::SafeBrowsing3::DBI, like L<Net::Google::SafeBrowsing3::MySQL>. | ||
| 23 | |||
| 24 | |||
| 25 | use Net::Google::SafeBrowsing3::MySQL; | ||
| 26 | |||
| 27 | my $storage = Net::Google::SafeBrowsing3::MySQL->new(host => '127.0.0.1', database => 'GoogleSafeBrowsingv3'); | ||
| 28 | ... | ||
| 29 | $storage->close(); | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | This is a base implementation of L<Net::Google::SafeBrowsing3::Storage> using DBI. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | |||
| 38 | =head1 CONSTRUCTOR | ||
| 39 | |||
| 40 | =over 4 | ||
| 41 | |||
| 42 | =back | ||
| 43 | |||
| 44 | =head2 new() | ||
| 45 | |||
| 46 | This method should be overwritten. | ||
| 47 | |||
| 48 | Arguments | ||
| 49 | |||
| 50 | =over 4 | ||
| 51 | |||
| 52 | =item keep_all | ||
| 53 | |||
| 54 | Optional. Set to 1 to keep old information (such as expiring full hashes) in the database. 0 (delete) by default. | ||
| 55 | |||
| 56 | |||
| 57 | =back | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub new { | ||
| 62 | my ($class, %args) = @_; | ||
| 63 | |||
| 64 | my $self = { # default arguments | ||
| 65 | keep_all => 0, | ||
| 66 | %args, | ||
| 67 | }; | ||
| 68 | |||
| 69 | bless $self, $class or croak "Can't bless $class: $!"; | ||
| 70 | |||
| 71 | |||
| 72 | $self->init(); | ||
| 73 | |||
| 74 | return $self; | ||
| 75 | } | ||
| 76 | |||
| 77 | =head1 PUBLIC FUNCTIONS | ||
| 78 | |||
| 79 | =over 4 | ||
| 80 | |||
| 81 | See L<Net::Google::SafeBrowsing3::Storage> for a complete list of public functions. | ||
| 82 | |||
| 83 | =back | ||
| 84 | |||
| 85 | =head2 close() | ||
| 86 | |||
| 87 | Cleanup old full hashes, and close the connection to the database. | ||
| 88 | |||
| 89 | $storage->close(); | ||
| 90 | |||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub close { | ||
| 95 | my ($self, %args) = @_; | ||
| 96 | |||
| 97 | if ($self->{keep_all} == 0) { | ||
| 98 | $self->{dbh}->do('DELETE FROM full_hashes WHERE `end` < ?', { }, time()); | ||
| 99 | } | ||
| 100 | |||
| 101 | $self->{dbh}->disconnect; | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | sub init { | ||
| 106 | my ($self, %args) = @_; | ||
| 107 | |||
| 108 | # Should connect to database | ||
| 109 | # Should check if all tables exist | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | sub create_table_updates { | ||
| 114 | my ($self, %args) = @_; | ||
| 115 | |||
| 116 | my $schema = qq{ | ||
| 117 | CREATE TABLE updates ( | ||
| 118 | last INT NOT NULL DEFAULT '0', | ||
| 119 | wait INT NOT NULL DEFAULT '0', | ||
| 120 | errors INT NOT NULL DEFAULT '1800', | ||
| 121 | list VARCHAR( 50 ) NOT NULL | ||
| 122 | ); | ||
| 123 | }; # Need to handle errors | ||
| 124 | |||
| 125 | $self->{dbh}->do($schema); | ||
| 126 | |||
| 127 | my $index = qq{ | ||
| 128 | CREATE UNIQUE INDEX list_unique ON updates (list); | ||
| 129 | }; | ||
| 130 | $self->{dbh}->do($index); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub create_table_a_chunks { | ||
| 134 | my ($self, %args) = @_; | ||
| 135 | |||
| 136 | my $schema = qq{ | ||
| 137 | CREATE TABLE a_chunks ( | ||
| 138 | hostkey VARCHAR( 8 ), | ||
| 139 | prefix VARCHAR( 32 ), | ||
| 140 | num INT NOT NULL, | ||
| 141 | list VARCHAR( 25 ) NOT NULL | ||
| 142 | ); | ||
| 143 | }; | ||
| 144 | |||
| 145 | $self->{dbh}->do($schema); | ||
| 146 | |||
| 147 | my $index = qq{ | ||
| 148 | CREATE INDEX a_chunks_hostkey ON a_chunks ( | ||
| 149 | hostkey | ||
| 150 | ); | ||
| 151 | }; | ||
| 152 | $self->{dbh}->do($index); | ||
| 153 | |||
| 154 | $index = qq{ | ||
| 155 | CREATE INDEX a_chunks_num_list ON a_chunks ( | ||
| 156 | num, | ||
| 157 | list | ||
| 158 | ); | ||
| 159 | }; | ||
| 160 | $self->{dbh}->do($index); | ||
| 161 | |||
| 162 | $index = qq{ | ||
| 163 | CREATE UNIQUE INDEX a_chunks_unique ON a_chunks ( | ||
| 164 | hostkey, | ||
| 165 | prefix, | ||
| 166 | num, | ||
| 167 | list | ||
| 168 | ); | ||
| 169 | }; | ||
| 170 | $self->{dbh}->do($index); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub create_table_s_chunks { | ||
| 174 | my ($self, %args) = @_; | ||
| 175 | |||
| 176 | my $schema = qq{ | ||
| 177 | CREATE TABLE s_chunks ( | ||
| 178 | hostkey VARCHAR( 8 ), | ||
| 179 | prefix VARCHAR( 32 ), | ||
| 180 | num INT NOT NULL, | ||
| 181 | add_num INT Default '0', | ||
| 182 | list VARCHAR( 25 ) NOT NULL | ||
| 183 | ); | ||
| 184 | }; | ||
| 185 | |||
| 186 | $self->{dbh}->do($schema); | ||
| 187 | |||
| 188 | my $index = qq{ | ||
| 189 | CREATE INDEX s_chunks_hostkey ON s_chunks ( | ||
| 190 | hostkey | ||
| 191 | ); | ||
| 192 | }; | ||
| 193 | $self->{dbh}->do($index); | ||
| 194 | |||
| 195 | $index = qq{ | ||
| 196 | CREATE INDEX s_chunks_num ON s_chunks ( | ||
| 197 | num | ||
| 198 | ); | ||
| 199 | }; | ||
| 200 | $self->{dbh}->do($index); | ||
| 201 | |||
| 202 | $index = qq{ | ||
| 203 | CREATE INDEX s_chunks_num_list ON s_chunks ( | ||
| 204 | num, | ||
| 205 | list | ||
| 206 | ); | ||
| 207 | }; | ||
| 208 | $self->{dbh}->do($index); | ||
| 209 | |||
| 210 | $index = qq{ | ||
| 211 | CREATE UNIQUE INDEX s_chunks_unique ON s_chunks ( | ||
| 212 | hostkey, | ||
| 213 | prefix, | ||
| 214 | num, | ||
| 215 | add_num, | ||
| 216 | list | ||
| 217 | ); | ||
| 218 | }; | ||
| 219 | $self->{dbh}->do($index); | ||
| 220 | } | ||
| 221 | |||
| 222 | sub create_table_full_hashes { | ||
| 223 | my ($self, %args) = @_; | ||
| 224 | |||
| 225 | my $schema = qq{ | ||
| 226 | CREATE TABLE full_hashes ( | ||
| 227 | id INT AUTO_INCREMENT PRIMARY KEY, | ||
| 228 | num INT, | ||
| 229 | hash VARCHAR( 32 ), | ||
| 230 | list VARCHAR( 25 ), | ||
| 231 | end INT Default '0', | ||
| 232 | type INT Default '0' | ||
| 233 | ); | ||
| 234 | }; | ||
| 235 | |||
| 236 | $self->{dbh}->do($schema); | ||
| 237 | |||
| 238 | my $index = qq{ | ||
| 239 | CREATE UNIQUE INDEX hash ON full_hashes ( | ||
| 240 | num, | ||
| 241 | hash, | ||
| 242 | list | ||
| 243 | ); | ||
| 244 | }; | ||
| 245 | $self->{dbh}->do($index); | ||
| 246 | } | ||
| 247 | |||
| 248 | sub create_table_full_hashes_errors { | ||
| 249 | my ($self, %args) = @_; | ||
| 250 | |||
| 251 | my $schema = qq{ | ||
| 252 | CREATE TABLE full_hashes_errors ( | ||
| 253 | id INT AUTO_INCREMENT PRIMARY KEY, | ||
| 254 | errors INT Default '0', | ||
| 255 | prefix VARCHAR( 32 ), | ||
| 256 | timestamp INT Default '0' | ||
| 257 | ); | ||
| 258 | }; | ||
| 259 | |||
| 260 | $self->{dbh}->do($schema); | ||
| 261 | } | ||
| 262 | |||
| 263 | |||
| 264 | sub add_chunks { | ||
| 265 | my ($self, %args) = @_; | ||
| 266 | my $type = $args{type} || 'a'; | ||
| 267 | my $chunknum = $args{chunknum} || 0; | ||
| 268 | my $chunks = $args{chunks} || []; | ||
| 269 | my $list = $args{'list'} || ''; | ||
| 270 | |||
| 271 | # $self->{dbh}->do("PRAGMA journal_mode = OFF"); | ||
| 272 | # $self->{dbh}->do("PRAGMA synchronous = OFF"); | ||
| 273 | |||
| 274 | if ($type eq 's') { | ||
| 275 | $self->add_chunks_s(chunknum => $chunknum, chunks => $chunks, list => $list); | ||
| 276 | } | ||
| 277 | elsif ($type eq 'a') { | ||
| 278 | $self->add_chunks_a(chunknum => $chunknum, chunks => $chunks, list => $list); | ||
| 279 | } | ||
| 280 | |||
| 281 | # $self->{dbh}->do("PRAGMA journal_mode = DELETE"); | ||
| 282 | # $self->{dbh}->do("PRAGMA synchronous = FULL"); | ||
| 283 | } | ||
| 284 | |||
| 285 | sub add_chunks_s { | ||
| 286 | my ($self, %args) = @_; | ||
| 287 | my $chunknum = $args{chunknum} || 0; | ||
| 288 | my $chunks = $args{chunks} || []; | ||
| 289 | my $list = $args{'list'} || ''; | ||
| 290 | |||
| 291 | my $add = $self->{dbh}->prepare('INSERT INTO s_chunks (prefix, num, add_num, list) VALUES (?, ?, ?, ?)'); | ||
| 292 | my $del = $self->{dbh}->prepare('DELETE FROM s_chunks WHERE prefix = ? AND num = ? AND add_num = ? AND list = ?'); | ||
| 293 | |||
| 294 | foreach my $chunk (@$chunks) { | ||
| 295 | $del->execute( $chunk->{prefix}, $chunknum, $chunk->{add_chunknum}, $list ); | ||
| 296 | $add->execute( $chunk->{prefix}, $chunknum, $chunk->{add_chunknum}, $list ); | ||
| 297 | } | ||
| 298 | |||
| 299 | if (scalar @$chunks == 0) { # keep empty chunks | ||
| 300 | $del->execute( '', $chunknum, '', $list ); | ||
| 301 | $add->execute( '', $chunknum, '', $list ); | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | sub add_chunks_a { | ||
| 306 | my ($self, %args) = @_; | ||
| 307 | my $chunknum = $args{chunknum} || 0; | ||
| 308 | my $chunks = $args{chunks} || []; | ||
| 309 | my $list = $args{'list'} || ''; | ||
| 310 | |||
| 311 | my $add = $self->{dbh}->prepare('INSERT INTO a_chunks (prefix, num, list) VALUES (?, ?, ?, ?)'); | ||
| 312 | my $del = $self->{dbh}->prepare('DELETE FROM a_chunks WHERE prefix = ? AND num = ? AND list = ?'); | ||
| 313 | |||
| 314 | foreach my $chunk (@$chunks) { | ||
| 315 | $del->execute( $chunk->{prefix}, $chunknum, $list ); | ||
| 316 | $add->execute( $chunk->{prefix}, $chunknum, $list ); | ||
| 317 | } | ||
| 318 | |||
| 319 | if (scalar @$chunks == 0) { # keep empty chunks | ||
| 320 | $del->execute( '', $chunknum, $list ); | ||
| 321 | $add->execute( '', $chunknum, $list ); | ||
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | sub get_add_chunks { | ||
| 326 | my ($self, %args) = @_; | ||
| 327 | my $prefix = $args{prefix} || ''; | ||
| 328 | |||
| 329 | my @list = (); | ||
| 330 | my $rows = $self->{dbh}->selectall_arrayref("SELECT * FROM a_chunks WHERE prefix = ?", { Slice => {} }, $prefix); | ||
| 331 | |||
| 332 | foreach my $row (@$rows) { | ||
| 333 | push(@list, { chunknum => $row->{num}, prefix => $prefix, list => $row->{list} }); | ||
| 334 | } | ||
| 335 | |||
| 336 | return @list; | ||
| 337 | } | ||
| 338 | |||
| 339 | sub get_sub_chunks { | ||
| 340 | my ($self, %args) = @_; | ||
| 341 | my $prefix = $args{prefix} || ''; | ||
| 342 | |||
| 343 | my @list = (); | ||
| 344 | my $rows = $self->{dbh}->selectall_arrayref("SELECT * FROM s_chunks WHERE prefix = ?", { Slice => {} }, $prefix); | ||
| 345 | |||
| 346 | foreach my $row (@$rows) { | ||
| 347 | push(@list, { chunknum => $row->{num}, prefix => $prefix, addchunknum => $row->{add_num}, list => $row->{list} }); | ||
| 348 | } | ||
| 349 | |||
| 350 | return @list; | ||
| 351 | } | ||
| 352 | |||
| 353 | sub get_add_chunks_nums { | ||
| 354 | my ($self, %args) = @_; | ||
| 355 | my $list = $args{'list'} || ''; | ||
| 356 | |||
| 357 | my @list = (); | ||
| 358 | my $rows = $self->{dbh}->selectall_arrayref("SELECT DISTINCT(num) FROM a_chunks WHERE list = ? ORDER BY num ASC", { Slice => {} }, $list); | ||
| 359 | foreach my $row (@$rows) { | ||
| 360 | push(@list, $row->{num}); | ||
| 361 | } | ||
| 362 | |||
| 363 | return @list; | ||
| 364 | } | ||
| 365 | |||
| 366 | sub get_sub_chunks_nums { | ||
| 367 | my ($self, %args) = @_; | ||
| 368 | my $list = $args{'list'} || ''; | ||
| 369 | |||
| 370 | my @list = (); | ||
| 371 | my $rows = $self->{dbh}->selectall_arrayref("SELECT DISTINCT(num) FROM s_chunks WHERE list = ? ORDER BY num ASC", { Slice => {} }, $list); | ||
| 372 | foreach my $row (@$rows) { | ||
| 373 | push(@list, $row->{num}); | ||
| 374 | } | ||
| 375 | |||
| 376 | return @list; | ||
| 377 | } | ||
| 378 | |||
| 379 | |||
| 380 | sub delete_add_ckunks { | ||
| 381 | my ($self, %args) = @_; | ||
| 382 | my $chunknums = $args{chunknums} || []; | ||
| 383 | my $list = $args{'list'} || ''; | ||
| 384 | |||
| 385 | |||
| 386 | $self->{delete_a_chunk} ||= $self->{dbh}->prepare("DELETE FROM a_chunks WHERE num IN (?) AND list = ?"); | ||
| 387 | my $sth = $self->{delete_a_chunk}; | ||
| 388 | |||
| 389 | $sth->execute( join(',', @{ $args{chunknums} }), $list); | ||
| 390 | } | ||
| 391 | |||
| 392 | |||
| 393 | sub delete_sub_ckunks { | ||
| 394 | my ($self, %args) = @_; | ||
| 395 | my $chunknums = $args{chunknums} || []; | ||
| 396 | my $list = $args{'list'} || ''; | ||
| 397 | |||
| 398 | $self->{delete_s_chunk} ||= $self->{dbh}->prepare("DELETE FROM s_chunks WHERE num IN (?) AND list = ?"); | ||
| 399 | my $sth = $self->{delete_s_chunk}; | ||
| 400 | $sth->execute( join(',', @{ $args{chunknums} }), $list); | ||
| 401 | } | ||
| 402 | |||
| 403 | sub get_full_hashes { | ||
| 404 | my ($self, %args) = @_; | ||
| 405 | my $hash = $args{hash} || ''; | ||
| 406 | my $list = $args{list} || ''; | ||
| 407 | my $timestamp = time(); | ||
| 408 | |||
| 409 | my @hashes = (); | ||
| 410 | |||
| 411 | my $rows = $self->{dbh}->selectall_arrayref("SELECT hash, type FROM full_hashes WHERE `end` <= ? AND list = ? AND hash = ?", { Slice => {} }, $timestamp, $list, $hash); | ||
| 412 | foreach my $row (@$rows) { | ||
| 413 | push(@hashes, $row); | ||
| 414 | } | ||
| 415 | |||
| 416 | return @hashes; | ||
| 417 | } | ||
| 418 | |||
| 419 | |||
| 420 | sub updated { | ||
| 421 | my ($self, %args) = @_; | ||
| 422 | my $time = $args{'time'} || time; | ||
| 423 | my $wait = $args{'wait'} || 1800; | ||
| 424 | my $list = $args{'list'} || ''; | ||
| 425 | |||
| 426 | if ($self->last_update(list => $list)->{'time'} == 0) { | ||
| 427 | $self->{dbh}->do("INSERT INTO updates (last, wait, errors, list) VALUES (?, ?, 0, ?)", undef, $time, $wait, $list); | ||
| 428 | } | ||
| 429 | else { | ||
| 430 | $self->{dbh}->do("UPDATE updates SET last = ?, wait = ?, errors = 0 WHERE list = ?", undef, $time, $wait, $list); | ||
| 431 | } | ||
| 432 | } | ||
| 433 | |||
| 434 | sub update_error { | ||
| 435 | my ($self, %args) = @_; | ||
| 436 | my $time = $args{'time'} || time; | ||
| 437 | my $list = $args{'list'} || ''; | ||
| 438 | my $wait = $args{'wait'} || 60; | ||
| 439 | my $errors = $args{errors} || 1; | ||
| 440 | |||
| 441 | if ($self->last_update(list => $list)->{'time'} == 0) { | ||
| 442 | $self->{dbh}->do("INSERT INTO updates (last, wait, errors, list) VALUES (?, ?, ?, ?)", undef, $time, $wait, $errors, $list); | ||
| 443 | } | ||
| 444 | else { | ||
| 445 | $self->{dbh}->do("UPDATE updates SET last = ?, wait = ?, errors = ? WHERE list = ?", undef, $time, $wait, $errors, $list); | ||
| 446 | } | ||
| 447 | } | ||
| 448 | |||
| 449 | sub last_update { | ||
| 450 | my ($self, %args) = @_; | ||
| 451 | my $list = $args{'list'} || ''; | ||
| 452 | |||
| 453 | my $rows = $self->{dbh}->selectall_arrayref("SELECT last, wait, errors FROM updates WHERE list = ? LIMIT 1", { Slice => {} }, $list); | ||
| 454 | |||
| 455 | foreach my $row (@$rows) { | ||
| 456 | return {'time' => $row->{'last'} || 0, 'wait' => $row->{'wait'} || 1800, errors => $row->{'errors'} || 0}; | ||
| 457 | } | ||
| 458 | |||
| 459 | return {'time' => 0, 'wait' => 1800}; | ||
| 460 | } | ||
| 461 | |||
| 462 | sub add_full_hashes { | ||
| 463 | my ($self, %args) = @_; | ||
| 464 | my $timestamp = $args{timestamp} || time(); | ||
| 465 | my $full_hashes = $args{full_hashes} || []; | ||
| 466 | |||
| 467 | foreach my $hash (@$full_hashes) { | ||
| 468 | $self->{dbh}->do("DELETE FROM full_hashes WHERE hash = ? AND list = ?", { }, $hash->{hash}, $hash->{list}); | ||
| 469 | $self->{dbh}->do("INSERT INTO full_hashes (hash, list, `end`, type) VALUES (?, ?, ?, ?)", { }, $hash->{hash}, $hash->{list}, $timestamp + $hash->{life}, $hash->{type} || 0); | ||
| 470 | } | ||
| 471 | } | ||
| 472 | |||
| 473 | sub full_hash_error { | ||
| 474 | my ($self, %args) = @_; | ||
| 475 | my $timestamp = $args{timestamp} || time(); | ||
| 476 | my $prefix = $args{prefix} || ''; | ||
| 477 | |||
| 478 | my $rows = $self->{dbh}->selectall_arrayref("SELECT id, errors FROM full_hashes_errors WHERE prefix = ? LIMIT 1", { Slice => {} }, $prefix); | ||
| 479 | |||
| 480 | if (scalar @$rows == 0) { | ||
| 481 | $self->{dbh}->do("INSERT INTO full_hashes_errors (prefix, errors, timestamp) VALUES (?, 1, ?)", { }, $prefix, $timestamp); | ||
| 482 | } | ||
| 483 | else { | ||
| 484 | my $errors = $rows->[0]->{errors} + 1; | ||
| 485 | $self->{dbh}->do("UPDATE full_hashes_errors SET errors = ?, timestamp = ? WHERE id = ?", $errors, $timestamp, $rows->[0]->{id}); | ||
| 486 | } | ||
| 487 | } | ||
| 488 | |||
| 489 | sub full_hash_ok { | ||
| 490 | my ($self, %args) = @_; | ||
| 491 | my $timestamp = $args{timestamp} || time(); | ||
| 492 | my $prefix = $args{prefix} || ''; | ||
| 493 | |||
| 494 | my $rows = $self->{dbh}->selectall_arrayref("SELECT id, errors FROM full_hashes_errors WHERE prefix = ? AND errors > 0 LIMIT 1", { Slice => {} }, $prefix); | ||
| 495 | |||
| 496 | if (scalar @$rows > 0) { | ||
| 497 | $self->{dbh}->do("UPDATE full_hashes_errors SET errors = 0, timestamp = ? WHERE id = ?", $timestamp, $rows->[0]->{id}); | ||
| 498 | $self->{dbh}->do("DELETE FROM full_hashes_errors WHERE id = ?", $rows->[0]->{id}); | ||
| 499 | } | ||
| 500 | } | ||
| 501 | |||
| 502 | sub get_full_hash_error { | ||
| 503 | my ($self, %args) = @_; | ||
| 504 | my $prefix = $args{prefix} || ''; | ||
| 505 | |||
| 506 | my $rows = $self->{dbh}->selectall_arrayref("SELECT timestamp, errors FROM full_hashes_errors WHERE prefix = ? LIMIT 1", { Slice => {} }, $prefix); | ||
| 507 | |||
| 508 | if (scalar @$rows == 0) { | ||
| 509 | return undef; | ||
| 510 | } | ||
| 511 | else { | ||
| 512 | return $rows->[0]; | ||
| 513 | } | ||
| 514 | } | ||
| 515 | |||
| 516 | sub reset_full_hashes { | ||
| 517 | my ($self, %args) = @_; | ||
| 518 | my $list = $args{'list'} || ''; | ||
| 519 | |||
| 520 | return if ($self->{keep_all} == 1); | ||
| 521 | |||
| 522 | $self->{delete_full_hashes} ||= $self->{dbh}->prepare('DELETE FROM full_hashes WHERE list = ?'); | ||
| 523 | my $sth = $self->{delete_full_hashes}; | ||
| 524 | $sth->execute( $list ); | ||
| 525 | } | ||
| 526 | |||
| 527 | sub reset { | ||
| 528 | my ($self, %args) = @_; | ||
| 529 | my $list = $args{'list'} || ''; | ||
| 530 | |||
| 531 | my $sth = $self->{dbh}->prepare('DELETE FROM s_chunks WHERE list = ?'); | ||
| 532 | $sth->execute( $list ); | ||
| 533 | |||
| 534 | $sth = $self->{dbh}->prepare('DELETE FROM a_chunks WHERE list = ?'); | ||
| 535 | $sth->execute( $list ); | ||
| 536 | |||
| 537 | $self->{delete_full_hashes} ||= $self->{dbh}->prepare('DELETE FROM full_hashes WHERE list = ?'); | ||
| 538 | $sth = $self->{delete_full_hashes}; | ||
| 539 | $sth->execute( $list ); | ||
| 540 | |||
| 541 | $sth = $self->{dbh}->prepare('DELETE FROM full_hashes_errors'); | ||
| 542 | $sth->execute(); | ||
| 543 | |||
| 544 | $sth = $self->{dbh}->prepare('DELETE FROM updates WHERE list = ?'); | ||
| 545 | $sth->execute( $list ); | ||
| 546 | } | ||
| 547 | |||
| 548 | sub create_range { | ||
| 549 | my ($self, %args) = @_; | ||
| 550 | my $numbers = $args{numbers} || []; # should already be ordered | ||
| 551 | |||
| 552 | return '' if (scalar @$numbers == 0); | ||
| 553 | |||
| 554 | my $range = $$numbers[0]; | ||
| 555 | my $new_range = 0; | ||
| 556 | for(my $i = 1; $i < scalar @$numbers; $i++) { | ||
| 557 | # next if ($$numbers[$i] == $$numbers[$i-1]); # should not happen | ||
| 558 | |||
| 559 | if ($$numbers[$i] != $$numbers[$i-1] + 1) { | ||
| 560 | $range .= $$numbers[$i-1] if ($i > 1 && $new_range == 1); | ||
| 561 | $range .= ',' . $$numbers[$i]; | ||
| 562 | |||
| 563 | $new_range = 0 | ||
| 564 | } | ||
| 565 | elsif ($new_range == 0) { | ||
| 566 | $range .= "-"; | ||
| 567 | $new_range = 1; | ||
| 568 | } | ||
| 569 | } | ||
| 570 | $range .= $$numbers[scalar @$numbers - 1] if ($new_range == 1); | ||
| 571 | |||
| 572 | return $range; | ||
| 573 | } | ||
| 574 | |||
| 575 | =head1 CHANGELOG | ||
| 576 | |||
| 577 | =over 4 | ||
| 578 | |||
| 579 | =item 0.1 | ||
| 580 | |||
| 581 | Initial release. | ||
| 582 | |||
| 583 | =back | ||
| 584 | |||
| 585 | =head1 SEE ALSO | ||
| 586 | |||
| 587 | See L<Net::Google::SafeBrowsing3> for handling Google Safe Browsing v3. | ||
| 588 | |||
| 589 | See L<Net::Google::SafeBrowsing3::Storage> for the list of public functions. | ||
| 590 | |||
| 591 | See L<Net::Google::SafeBrowsing3::Sqlite> for a back-end using Sqlite. | ||
| 592 | |||
| 593 | Google Safe Browsing v3 API: L<https://developers.google.com/safe-browsing/developers_guide_v3> | ||
| 594 | |||
| 595 | |||
| 596 | =head1 AUTHOR | ||
| 597 | |||
| 598 | Julien Sobrier, E<lt>julien@sobrier.netE<gt> | ||
| 599 | |||
| 600 | =head1 COPYRIGHT AND LICENSE | ||
| 601 | |||
| 602 | Copyright (C) 2015 by Julien Sobrier | ||
| 603 | |||
| 604 | This library is free software; you can redistribute it and/or modify | ||
| 605 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 606 | at your option, any later version of Perl 5 you may have available. | ||
| 607 | |||
| 608 | |||
| 609 | =cut | ||
| 610 | |||
| 611 | 1; |