#!/usr/bin/perl # use strict; use CGI; use Data::Dumper; use Storable; use Image::Info qw/image_info/; use POSIX qw/strftime/; use Digest::MD5 qw/md5_base64/; use DBI; my $c = CGI->new; print "Content-type: text/html\n\n"; print <

TOP10 of viewed pictures

xxEOF my $ID = $c->param('sessionID') || $c->cookie( 'sessionID' ) || time; my $MYPICS = $c->param('sessionID') || $c->param('mypics') || 0; my $ALBUM = $c->param('album') || 0; my $RANK = $c->param('rank') || 10; my $past = defined $c->param('past') ? $c->param('past') : 7; my $DBH = DBI->connect('DBI:mysql:TOPPICS', 'root'); unless (ref $DBH) { print '

Cannot connect to database

'; exit; } my $SQL = q!select file,count(distinct(session)),count(*),max(concat(unix_timestamp(access_dt),' ', remote_host)),max(concat(unix_timestamp(access_dt),' ', remote_ip)),max(access_dt),max(concat(unix_timestamp(access_dt),' ', session)), max(concat(unix_timestamp(access_dt),' ', R)) from webstats where !; $SQL .= qq!album='$ALBUM' and ! if $ALBUM; $SQL .= qq!session='$ID' and ! if $MYPICS; $SQL .= qq!access_dt > DATE_SUB(now(), INTERVAL $past DAY) and ! if $past; $SQL .= qq!1=1 group by file order by 2 desc limit $RANK!; my $TT = $DBH->selectall_arrayref($SQL); $DBH->disconnect(); #print STDERR Dumper($TT); my $pchoices = { 0 => 'unlimited', 1 => 'last day', 7 => 'last week', 30 => 'last month', 180 => 'last 6 month', 365 => 'last year', }; my $rchoices = { 10 => 'TOP 10', 20 => 'TOP 20', 50 => 'TOP 50', 100 => 'TOP 100', 999999 => 'all pictures ever', }; my $achoices = { 0 => '-- all --', }; opendir(DIR, '/var/www/tosti/html/albums'); my @AA = grep -d, readdir DIR; closedir(DIR); foreach (@AA) { next unless $_ =~ /^[A-Za-z]/; $achoices->{$_} = $_; } my $P = $c->popup_menu('past', [sort {$a <=> $b} (keys(%$pchoices))], $past, $pchoices, 1); my $R = $c->popup_menu('rank', [sort {$a <=> $b} (keys(%$rchoices))], $c->param('rank') || 0, $rchoices, 1); my $M = sprintf '', $c->param('mypics') ? 'checked' : ''; my $A = $c->popup_menu('album', [sort {$a cmp $b} (keys(%$achoices))], $c->param('album') || 0, $achoices, 1); print <
$R Time range: $P Album: $A Only my stats: $M    
xxEOF unless (@$TT) { print '

There are no stats available for given query.

'; exit; } print ''; my $i = 1; foreach my $p (@$TT) { my $P = '/var/www/tosti/html/albums'.$p->[0]; my $Info = image_info( $P ); next unless $Info->{width}; my $access = sprintf( 'Access count: %d (%d)

Last access:
', $p->[1], $p->[2] ); $access .= sprintf( 'Time: %s
', $p->[5] ); if ($p->[3] =~ /^\d+\s(\S+)/) { $access .= sprintf( 'Host: %s
', $1 ); } if ($p->[4] =~ /^\d+\s(\S+)/) { $access .= sprintf( 'IP: %s
', $1 ); } if ($p->[6] =~ /^\d+\s(\S+)/) { $access .= sprintf( 'Session: %s
', $1 ); } if ($p->[7] =~ /^\d+\s+(\S+)/) { $p->[7] = $1; } else { $p->[7] = 0; } my $info = sprintf('DIM: %dx%d SIZE: %d KB
', $Info->{width}, $Info->{height}, (stat($P))[7]/1024); $info .= sprintf('TIME: %s GMT
', $Info->{DateTimeOriginal}) if $Info->{DateTimeOriginal}; $info .= sprintf('FLASH: %s
', $Info->{Flash}) if $Info->{Flash}; $info .= sprintf('CAM: %s %s
', $Info->{Make}, $Info->{Model} || '') if $Info->{Make}; $info .= sprintf('ALBUM: %s', $p->[0]=~m!^/(.+)/!, $p->[0]=~m!^/(.+)/! ); my $ppath = sprintf( '%s~x=200~c=1~q=50%s', $p->[0], $p->[7] ? '~r='.$p->[7] : ''); my $MD = md5_base64($ppath); $MD =~ s!/!.!g; my $CFILE = '/imgcache/' . $MD . '.jpg'; my $cfile = '/var/www/tosti/html' . $CFILE; if (-e $cfile) { qx!touch $cfile!; $ppath = $CFILE; } else { $ppath = 'img.cgi' . $ppath; } printf qq!
\n!, $i, $p->[0], $p->[7] ? '~r='.$p->[7] : '', $i, $ppath, $access, $info; last if ++$i > $RANK; } print '
RankPictureAccessPicture info
%d.%s%s
';