#!/usr/bin/perl -w use strict; use warnings FATAL => 'all'; use CGI::Pretty 1.08 qw(:standard *div); use CGI::Carp 1.29 qw(fatalsToBrowser warningsToBrowser); ######################################## my $restricted = 0; my $acl_file = 'acl.conf'; my $img_dir = '.'; my $imgwrapper = 0; my $unauth_regexp = '.*'; ######################################## sub error { my ($str, @args) = @_; if ($#args >= 0) { $str = sprintf $str, @args; } print header, start_html( -title => 'Junigraph: error', -style => { -src => 'styles.css', }, ), start_div({ -align => 'center', }), $str, "\n", end_div, end_html; exit; } sub imgoutput { my $file = shift; unless (open F, $file) { unless (open F, 'error.png') { print header('text/plain'); exit; } } print header('image/png'); print ; close F; exit; } sub imgerror { imgoutput 'error.png'; } my ($user, $acl_re, $title); my $self = url(-absolute => 1); if ($restricted) { unless (open F, $acl_file) { error "Can't open ACL file"; } $user = remote_user; if ($user) { while () { chomp; next if /^#/; next unless /^[\s\xA0]*(\S+)[\s\xA0]+(\S+)[\s\xA0]*#*/; if ($1 eq $user) { $acl_re = qr/$2/i; last; } } close F; unless ($acl_re) { error "No ACL entry found for user %s", b($user); } } elsif ($unauth_regexp) { $user = remote_addr; $acl_re = qr/$unauth_regexp/i; } else { error 'Authentication failed, no username found'; } $title = sprintf "%s @ Junigraph", $user; } else { $title = 'Junigraph'; } if ($imgwrapper && (my $img = url_param 'img')) { if ($img =~ /^([a-z0-9-]{1,255})(?:6h|12h|1d|1w|1m|1y)\.png$/i) { my $name = $1; if ($restricted && $name !~ /$acl_re/) { imgerror; } my $file = sprintf "%s/%s", $img_dir, $img; unless (-f $file && -s $file) { imgerror; } imgoutput $file; } else { imgerror; } } my $param = url_param 'name'; if ($param) { if ($param =~ /^[a-z0-9-]{1,255}$/i) { if ($restricted && $param !~ /$acl_re/) { error "Access to %s denied for user %s", b($param), b($user); } my $title_param = $param; $title_param =~ s/[_-]+/ /g; $title .= sprintf " / %s", $title_param; } else { error 'Invalid graph name'; } } print header, start_html( -title => $title, -style => { -src => 'styles.css', }, ), start_div({ -align => 'center', }); if ($param) { my $images_found = 0; foreach (qw(6h 12h 1d 1w 1m 1y)) { my $img = sprintf "%s-%s.png", $param, $_; my $img_src = sprintf "%s/%s", $img_dir, $img; $img_src =~ s|^\./||; next unless -f $img_src && -s $img_src; $images_found = 1; if ($imgwrapper) { $img_src = sprintf "%s?img=%s", $self, $img; } print img({ -src => $img_src, -alt => $img, -title => $img, }), br; } unless ($images_found) { printf "No graphs found for %s\n", b($param); } } elsif (opendir D, $img_dir) { my $images_found = 0; foreach my $file (sort readdir D) { my ($name) = $file =~ /^([a-z0-9-]+)-1d\.png$/i; next unless $name; next if $restricted && $name !~ /$acl_re/; my $url = sprintf "%s?name=%s", $self, $name; my $img_src = sprintf "%s/%s", $img_dir, $file; $img_src =~ s|^\./||; next unless -f $img_src && -s $img_src; $images_found = 1; if ($imgwrapper) { $img_src = sprintf "%s?img=%s", $self, $file; } print a( { href => $url, }, img({ -src => $img_src, -alt => $file, -title => $file, -border => 0, }), ), br; } closedir D; unless ($images_found) { printf "No graphs found for user %s\n", b($user); } } print end_div, end_html;