| 1 | package Apache::SearchEngineLog; | ||
| 2 | # Logging of terms used in searchengines | ||
| 3 | |||
| 4 | require 5.005; | ||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 0 | ▶ | use Apache; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use Apache::Log; | ||
| 10 | use DBI; | ||
| 11 | |||
| 12 | use vars qw#$SERVER $REGEXEN $DBH $STH $TIMEOUT $LASTPING#; | ||
| 13 | |||
| 14 | our $VERSION = '0.51'; | ||
| 15 | |||
| 16 | # to have the test-script run ok | ||
| 17 | return 1 if $0 eq 'test.pl'; | ||
| 18 | |||
| 19 | init (); | ||
| 20 | |||
| 21 | return 1; | ||
| 22 | #List of subroutines: | ||
| 23 | # check_alive_dbi | ||
| 24 | # check_regexen | ||
| 25 | # cleanup | ||
| 26 | # connect_dbi | ||
| 27 | # db_save | ||
| 28 | # handler | ||
| 29 | # init | ||
| 30 | |||
| 31 | sub check_alive_dbi | ||
| 32 | ########################################################################## | ||
| 33 | # Checks wether DB connection is alive by pinging it periodically. # | ||
| 34 | # Reconnects if neccessary. # | ||
| 35 | # # | ||
| 36 | # Arguments: # | ||
| 37 | # 0: Apache::Log object # | ||
| 38 | # # | ||
| 39 | # Returns: # | ||
| 40 | # 0: true if (re)conntected; false if reconnect fails # | ||
| 41 | ########################################################################## | ||
| 42 | { | ||
| 43 | my $l = shift; | ||
| 44 | |||
| 45 | my $time = time; | ||
| 46 | |||
| 47 | if (($time - $TIMEOUT) < $LASTPING) | ||
| 48 | { | ||
| 49 | return 1; | ||
| 50 | } | ||
| 51 | |||
| 52 | $l->debug ('Apache::SearchEngineLog: Timeout reached, pinging'); | ||
| 53 | |||
| 54 | if ($DBH->ping ()) | ||
| 55 | { | ||
| 56 | $LASTPING = $time; | ||
| 57 | return 1; | ||
| 58 | } | ||
| 59 | |||
| 60 | $l->info ('Apache::SearchEngineLog: Connection to database died: Reconnecting'); | ||
| 61 | return connect_dbi ($l); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub check_regexen | ||
| 65 | ########################################################################## | ||
| 66 | # Uses regexen to check which rule applies to a given server (if any) # | ||
| 67 | # and returns the parameter field which contains usefull information. # | ||
| 68 | # # | ||
| 69 | # Arguments: # | ||
| 70 | # 0: Name of server as string # | ||
| 71 | # # | ||
| 72 | # Returns: # | ||
| 73 | # 0: Name of parameter # | ||
| 74 | ########################################################################## | ||
| 75 | { | ||
| 76 | my $server = shift; | ||
| 77 | my $retval = ''; | ||
| 78 | |||
| 79 | foreach my $re (keys %$REGEXEN) | ||
| 80 | { | ||
| 81 | if ($server =~ m#$re#) | ||
| 82 | { | ||
| 83 | $retval = $REGEXEN->{$re}; | ||
| 84 | last; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | return $retval; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub cleanup | ||
| 92 | ########################################################################## | ||
| 93 | # Checks wether DB connection is alive by pinging it periodically. # | ||
| 94 | # Reconnects if neccessary. # | ||
| 95 | # # | ||
| 96 | # Arguments: # | ||
| 97 | # None # | ||
| 98 | # # | ||
| 99 | # Returns: # | ||
| 100 | # 0: true # | ||
| 101 | ########################################################################## | ||
| 102 | { | ||
| 103 | $DBH->disconnect (); | ||
| 104 | return 1; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub connect_dbi | ||
| 108 | ########################################################################## | ||
| 109 | # Connects to the database. # | ||
| 110 | # # | ||
| 111 | # Arguments: # | ||
| 112 | # 0: Apache::Log object # | ||
| 113 | # # | ||
| 114 | # Returns: # | ||
| 115 | # 0: true if successfully connected, false otherwise # | ||
| 116 | ########################################################################## | ||
| 117 | { | ||
| 118 | my $l = shift; | ||
| 119 | |||
| 120 | my $db_source = $ENV{'DBI_data_source'} or $l->error ("Apache::SearchEngineLog: DBI_data_source not defined"); | ||
| 121 | my $db_user = $ENV{'DBI_username'} or $l->error ("Apache::SearchEngineLog: DBI_username not defined"); | ||
| 122 | my $db_passwd = $ENV{'DBI_password'} or $l->error ("Apache::SearchEngineLog: DBI_password not defined"); | ||
| 123 | my $db_table = (defined $ENV{'DBI_table'} ? $ENV{'DBI_table'} : 'hits'); | ||
| 124 | |||
| 125 | if ($DBH = DBI->connect ($db_source, $db_user, $db_passwd)) | ||
| 126 | { | ||
| 127 | $l->info ("Apache::SearchEngineLog: Database connection established"); | ||
| 128 | } | ||
| 129 | else | ||
| 130 | { | ||
| 131 | $l->error ('Apache::SearchEngineLog: Unable to connect: ' . DBI->errstr ()); | ||
| 132 | return 0; | ||
| 133 | } | ||
| 134 | |||
| 135 | if ($STH = $DBH->prepare ("INSERT INTO $db_table (date, domain, term, uri, vhost) VALUES (NOW(), ?, ?, ?, ?)")) | ||
| 136 | { | ||
| 137 | $LASTPING = time; | ||
| 138 | return 1; | ||
| 139 | } | ||
| 140 | else | ||
| 141 | { | ||
| 142 | $l->error ('Apache::SearchEngineLog: ' . $DBH->errstr ()); | ||
| 143 | return 0; | ||
| 144 | } | ||
| 145 | |||
| 146 | return undef; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub db_save | ||
| 150 | ########################################################################## | ||
| 151 | # Saves the given arguments to the database. # | ||
| 152 | # # | ||
| 153 | # Arguments: # | ||
| 154 | # 0: Name of the remote server # | ||
| 155 | # 1: The URI requested # | ||
| 156 | # 2: Name of the virtual host # | ||
| 157 | # @: Terms used in the search engine # | ||
| 158 | # # | ||
| 159 | # Returns: # | ||
| 160 | # 0: true # | ||
| 161 | ########################################################################## | ||
| 162 | { | ||
| 163 | my $server = shift; | ||
| 164 | my $uri = shift; | ||
| 165 | my $hostname = shift; | ||
| 166 | |||
| 167 | foreach my $term (@_) | ||
| 168 | { | ||
| 169 | if ($STH->execute ($server, $term, $uri, $hostname)) | ||
| 170 | { | ||
| 171 | $LASTPING = time; | ||
| 172 | } | ||
| 173 | else | ||
| 174 | { | ||
| 175 | warn $STH->errstr (); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | return 1; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub handler | ||
| 183 | ########################################################################## | ||
| 184 | # The handler called by Apache. It analyses the request and the referer # | ||
| 185 | # and eventually calls other subroutines to assist in this task. This # | ||
| 186 | # is the heart of this program.. # | ||
| 187 | # # | ||
| 188 | # # | ||
| 189 | # Arguments: # | ||
| 190 | # 0: Apache::Request object # | ||
| 191 | # # | ||
| 192 | # Returns: # | ||
| 193 | # 0: true # | ||
| 194 | ########################################################################## | ||
| 195 | { | ||
| 196 | my $r = shift or return undef; | ||
| 197 | my %h = $r->headers_in (); | ||
| 198 | my $l = $r->log (); | ||
| 199 | |||
| 200 | $l->debug ("Apache::SearchEngineLog: handling request.."); | ||
| 201 | |||
| 202 | # first step: check for a (valid and usfull) referer | ||
| 203 | unless (defined $h{'Referer'}) | ||
| 204 | { | ||
| 205 | $l->debug ("Apache::SearchEngineLog: no referer defined.."); | ||
| 206 | return 1; | ||
| 207 | } | ||
| 208 | |||
| 209 | my $referer = $h{'Referer'}; | ||
| 210 | |||
| 211 | my $status = $r->status (); | ||
| 212 | if ($status >= 400) | ||
| 213 | { | ||
| 214 | $l->debug ("Apache::SearchEngineLog: Not handling status code #$status.."); | ||
| 215 | return 1; | ||
| 216 | } | ||
| 217 | |||
| 218 | my ($server, $params); | ||
| 219 | # referers are always http.. prove me wrong if i should be.. | ||
| 220 | # https shouldn't work either I belive.. | ||
| 221 | if ($referer =~ m#^http://([^/]+)/[^\?]+\?(.+)$#) | ||
| 222 | { | ||
| 223 | $server = $1; | ||
| 224 | $params = $2; | ||
| 225 | } | ||
| 226 | else | ||
| 227 | { | ||
| 228 | $l->debug ("Apache::SearchEngineLog: No parameters present.."); | ||
| 229 | return 1; | ||
| 230 | } | ||
| 231 | |||
| 232 | # referer looks fairly usefull.. let's check this.. | ||
| 233 | my %params; # i know some people don't like this.. I do ;) | ||
| 234 | foreach (split (m#\&#, $params)) | ||
| 235 | { | ||
| 236 | my ($key, $value) = split (m#=#, $_, 2); | ||
| 237 | $value =~ y#+# #; | ||
| 238 | $value =~ s#%([a-fA-F0-9]{2})#pack ("C", hex ($1))#eg; | ||
| 239 | |||
| 240 | $params{$key} = $value; | ||
| 241 | } | ||
| 242 | |||
| 243 | my $field; | ||
| 244 | if (!defined $SERVER->{$server}) | ||
| 245 | { | ||
| 246 | $l->debug ("Apache::SearchEngineLog: Unknown server: $server! Checking.."); | ||
| 247 | |||
| 248 | # servers without an apropriate entry in $REGEXEN should | ||
| 249 | # leave us here.. | ||
| 250 | $field = check_regexen ($server) or return 1; | ||
| 251 | |||
| 252 | if (defined $params{$field}) | ||
| 253 | { | ||
| 254 | $SERVER->{$server} = $field; | ||
| 255 | |||
| 256 | check_alive_dbi ($l); | ||
| 257 | |||
| 258 | my $sth = $DBH->prepare ("INSERT INTO config (domain, field) VALUES (?, ?)"); | ||
| 259 | $sth->execute ($server, $field); | ||
| 260 | $sth->finish (); | ||
| 261 | |||
| 262 | $l->info ("Apache::SearchEngineLog: Added new domain: $server"); | ||
| 263 | } | ||
| 264 | } | ||
| 265 | else | ||
| 266 | { | ||
| 267 | $l->debug ("Apache::SearchEngineLog: Known server: $server"); | ||
| 268 | $field = $SERVER->{$server}; | ||
| 269 | } | ||
| 270 | |||
| 271 | unless (defined $params{$field}) | ||
| 272 | { | ||
| 273 | $l->info ("Apache::SearchEngineLog: Known server missing field: $server"); | ||
| 274 | return 1; | ||
| 275 | } | ||
| 276 | |||
| 277 | # ignore goggle's cache-parameters and related option | ||
| 278 | if ($params{$field} =~ m#^(?:cache|related):\S+\s#) | ||
| 279 | { | ||
| 280 | # $' == everything right of match, FYI | ||
| 281 | $params{$field} = $'; | ||
| 282 | } | ||
| 283 | |||
| 284 | my $uri = $r->uri (); | ||
| 285 | my $s = $r->server (); | ||
| 286 | my $virtual = $s->server_hostname (); | ||
| 287 | |||
| 288 | if ($status == 301 or $status == 302 or $status == 303 or $status == 307) | ||
| 289 | { | ||
| 290 | my $location; | ||
| 291 | $location = $r->header_out ('Location') or $location = ''; | ||
| 292 | |||
| 293 | if ($location =~ m#^http://([^/]+)(/[^\?]*)#) | ||
| 294 | { | ||
| 295 | my ($to_server, $to_uri) = ($1, $2); | ||
| 296 | if ($to_server eq $virtual) | ||
| 297 | { | ||
| 298 | $l->info ("Apache::SearchEngineLog: $uri was redirected to $to_uri; logging the latter"); | ||
| 299 | $uri = $to_uri; | ||
| 300 | } | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | my @terms = (); | ||
| 305 | foreach my $term (split (m#\s+#, $params{$field})) | ||
| 306 | { | ||
| 307 | $term =~ s#(^\W+)|(\W+$)##g; | ||
| 308 | push (@terms, $term); | ||
| 309 | } | ||
| 310 | |||
| 311 | $l->debug ("Apache::SearchEngineLog: Saving to database"); | ||
| 312 | |||
| 313 | check_alive_dbi ($l) or return 1; | ||
| 314 | db_save ($server, $uri, $virtual, @terms); | ||
| 315 | |||
| 316 | return 1; | ||
| 317 | } | ||
| 318 | |||
| 319 | sub init | ||
| 320 | ########################################################################## | ||
| 321 | # Initialises global variables, initiates the database connection, etc.. # | ||
| 322 | # Just what you'd expect an init routine to do.. ;) # | ||
| 323 | # # | ||
| 324 | # Arguments: # | ||
| 325 | # none # | ||
| 326 | # # | ||
| 327 | # Returns: # | ||
| 328 | # 0: true # | ||
| 329 | ########################################################################## | ||
| 330 | { | ||
| 331 | my $s = Apache->server (); | ||
| 332 | my $l = $s->log (); | ||
| 333 | |||
| 334 | $REGEXEN = | ||
| 335 | { | ||
| 336 | qr#yahoo\.# => 'p', | ||
| 337 | qr#altavista\.# => 'q', | ||
| 338 | qr#msn\.# => 'q', | ||
| 339 | qr#voila\.# => 'kw', | ||
| 340 | qr#lycos\.# => 'query', | ||
| 341 | qr#search\.terra\.# => 'query', | ||
| 342 | qr#google\.(?!yahoo)# => 'q', | ||
| 343 | qr#alltheweb\.com# => 'q', | ||
| 344 | qr#netscape\.# => 'search', | ||
| 345 | qr#northernlight\.# => 'qr', | ||
| 346 | qr#dmoz\.org# => 'search', | ||
| 347 | qr#search\.aol\.com# => 'query', | ||
| 348 | qr#www\.search\.com# => 'q', | ||
| 349 | qr#askjeeves\.# => 'ask', | ||
| 350 | qr#hotbot\.# => 'mt', | ||
| 351 | qr#metacrawler\.# => 'general' | ||
| 352 | }; | ||
| 353 | |||
| 354 | # ping database in this interval at the very most.. | ||
| 355 | $TIMEOUT = (defined $ENV{'DBI_timeout'} ? $ENV{'DBI_timeout'} : 120); | ||
| 356 | connect_dbi ($l); | ||
| 357 | |||
| 358 | Apache->server->register_cleanup (\&cleanup); | ||
| 359 | |||
| 360 | $SERVER = {}; | ||
| 361 | |||
| 362 | # load known servers from database.. this is mostly to speed up | ||
| 363 | # recognition later on.. | ||
| 364 | my $sth = $DBH->prepare ("SELECT domain, field FROM config"); | ||
| 365 | $sth->execute (); | ||
| 366 | while (my ($d, $f) = $sth->fetchrow_array ()) | ||
| 367 | { | ||
| 368 | $SERVER->{$d} = $f; | ||
| 369 | } | ||
| 370 | $sth->finish (); | ||
| 371 | |||
| 372 | $l->debug ("Apache::SearchEngineLog: init done"); | ||
| 373 | |||
| 374 | return 1; | ||
| 375 | } | ||
| 376 | |||
| 377 | __END__ | ||